pod_security_policy.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package auth
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. policy "k8s.io/api/policy/v1beta1"
  18. rbacv1beta1 "k8s.io/api/rbac/v1beta1"
  19. apierrs "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime/schema"
  22. "k8s.io/apiserver/pkg/authentication/serviceaccount"
  23. clientset "k8s.io/client-go/kubernetes"
  24. restclient "k8s.io/client-go/rest"
  25. "k8s.io/kubernetes/pkg/security/apparmor"
  26. "k8s.io/kubernetes/pkg/security/podsecuritypolicy/seccomp"
  27. psputil "k8s.io/kubernetes/pkg/security/podsecuritypolicy/util"
  28. "k8s.io/kubernetes/test/e2e/common"
  29. "k8s.io/kubernetes/test/e2e/framework"
  30. "k8s.io/kubernetes/test/e2e/framework/auth"
  31. imageutils "k8s.io/kubernetes/test/utils/image"
  32. utilpointer "k8s.io/utils/pointer"
  33. "github.com/onsi/ginkgo"
  34. "github.com/onsi/gomega"
  35. )
  36. const nobodyUser = int64(65534)
  37. var _ = SIGDescribe("PodSecurityPolicy", func() {
  38. f := framework.NewDefaultFramework("podsecuritypolicy")
  39. f.SkipPrivilegedPSPBinding = true
  40. // Client that will impersonate the default service account, in order to run
  41. // with reduced privileges.
  42. var c clientset.Interface
  43. var ns string // Test namespace, for convenience
  44. ginkgo.BeforeEach(func() {
  45. if !framework.IsPodSecurityPolicyEnabled(f) {
  46. framework.Skipf("PodSecurityPolicy not enabled")
  47. }
  48. if !auth.IsRBACEnabled(f.ClientSet.RbacV1beta1()) {
  49. framework.Skipf("RBAC not enabled")
  50. }
  51. ns = f.Namespace.Name
  52. ginkgo.By("Creating a kubernetes client that impersonates the default service account")
  53. config, err := framework.LoadConfig()
  54. framework.ExpectNoError(err)
  55. config.Impersonate = restclient.ImpersonationConfig{
  56. UserName: serviceaccount.MakeUsername(ns, "default"),
  57. Groups: serviceaccount.MakeGroupNames(ns),
  58. }
  59. c, err = clientset.NewForConfig(config)
  60. framework.ExpectNoError(err)
  61. ginkgo.By("Binding the edit role to the default SA")
  62. err = auth.BindClusterRole(f.ClientSet.RbacV1beta1(), "edit", ns,
  63. rbacv1beta1.Subject{Kind: rbacv1beta1.ServiceAccountKind, Namespace: ns, Name: "default"})
  64. framework.ExpectNoError(err)
  65. })
  66. ginkgo.It("should forbid pod creation when no PSP is available", func() {
  67. ginkgo.By("Running a restricted pod")
  68. _, err := c.CoreV1().Pods(ns).Create(restrictedPod("restricted"))
  69. expectForbidden(err)
  70. })
  71. ginkgo.It("should enforce the restricted policy.PodSecurityPolicy", func() {
  72. ginkgo.By("Creating & Binding a restricted policy for the test service account")
  73. _, cleanup := createAndBindPSP(f, restrictedPSP("restrictive"))
  74. defer cleanup()
  75. ginkgo.By("Running a restricted pod")
  76. pod, err := c.CoreV1().Pods(ns).Create(restrictedPod("allowed"))
  77. framework.ExpectNoError(err)
  78. framework.ExpectNoError(framework.WaitForPodNameRunningInNamespace(c, pod.Name, pod.Namespace))
  79. testPrivilegedPods(func(pod *v1.Pod) {
  80. _, err := c.CoreV1().Pods(ns).Create(pod)
  81. expectForbidden(err)
  82. })
  83. })
  84. ginkgo.It("should allow pods under the privileged policy.PodSecurityPolicy", func() {
  85. ginkgo.By("Creating & Binding a privileged policy for the test service account")
  86. // Ensure that the permissive policy is used even in the presence of the restricted policy.
  87. _, cleanup := createAndBindPSP(f, restrictedPSP("restrictive"))
  88. defer cleanup()
  89. expectedPSP, cleanup := createAndBindPSP(f, privilegedPSP("permissive"))
  90. defer cleanup()
  91. testPrivilegedPods(func(pod *v1.Pod) {
  92. p, err := c.CoreV1().Pods(ns).Create(pod)
  93. framework.ExpectNoError(err)
  94. framework.ExpectNoError(framework.WaitForPodNameRunningInNamespace(c, p.Name, p.Namespace))
  95. // Verify expected PSP was used.
  96. p, err = c.CoreV1().Pods(ns).Get(p.Name, metav1.GetOptions{})
  97. framework.ExpectNoError(err)
  98. validated, found := p.Annotations[psputil.ValidatedPSPAnnotation]
  99. gomega.Expect(found).To(gomega.BeTrue(), "PSP annotation not found")
  100. gomega.Expect(validated).To(gomega.Equal(expectedPSP.Name), "Unexpected validated PSP")
  101. })
  102. })
  103. })
  104. func expectForbidden(err error) {
  105. framework.ExpectError(err, "should be forbidden")
  106. gomega.Expect(apierrs.IsForbidden(err)).To(gomega.BeTrue(), "should be forbidden error")
  107. }
  108. func testPrivilegedPods(tester func(pod *v1.Pod)) {
  109. ginkgo.By("Running a privileged pod", func() {
  110. privileged := restrictedPod("privileged")
  111. privileged.Spec.Containers[0].SecurityContext.Privileged = boolPtr(true)
  112. privileged.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation = nil
  113. tester(privileged)
  114. })
  115. ginkgo.By("Running a HostPath pod", func() {
  116. hostpath := restrictedPod("hostpath")
  117. hostpath.Spec.Containers[0].VolumeMounts = []v1.VolumeMount{{
  118. Name: "hp",
  119. MountPath: "/hp",
  120. }}
  121. hostpath.Spec.Volumes = []v1.Volume{{
  122. Name: "hp",
  123. VolumeSource: v1.VolumeSource{
  124. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  125. },
  126. }}
  127. tester(hostpath)
  128. })
  129. ginkgo.By("Running a HostNetwork pod", func() {
  130. hostnet := restrictedPod("hostnet")
  131. hostnet.Spec.HostNetwork = true
  132. tester(hostnet)
  133. })
  134. ginkgo.By("Running a HostPID pod", func() {
  135. hostpid := restrictedPod("hostpid")
  136. hostpid.Spec.HostPID = true
  137. tester(hostpid)
  138. })
  139. ginkgo.By("Running a HostIPC pod", func() {
  140. hostipc := restrictedPod("hostipc")
  141. hostipc.Spec.HostIPC = true
  142. tester(hostipc)
  143. })
  144. if common.IsAppArmorSupported() {
  145. ginkgo.By("Running a custom AppArmor profile pod", func() {
  146. aa := restrictedPod("apparmor")
  147. // Every node is expected to have the docker-default profile.
  148. aa.Annotations[apparmor.ContainerAnnotationKeyPrefix+"pause"] = "localhost/docker-default"
  149. tester(aa)
  150. })
  151. }
  152. ginkgo.By("Running an unconfined Seccomp pod", func() {
  153. unconfined := restrictedPod("seccomp")
  154. unconfined.Annotations[v1.SeccompPodAnnotationKey] = "unconfined"
  155. tester(unconfined)
  156. })
  157. ginkgo.By("Running a SYS_ADMIN pod", func() {
  158. sysadmin := restrictedPod("sysadmin")
  159. sysadmin.Spec.Containers[0].SecurityContext.Capabilities = &v1.Capabilities{
  160. Add: []v1.Capability{"SYS_ADMIN"},
  161. }
  162. sysadmin.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation = nil
  163. tester(sysadmin)
  164. })
  165. ginkgo.By("Running a RunAsGroup pod", func() {
  166. sysadmin := restrictedPod("runasgroup")
  167. gid := int64(0)
  168. sysadmin.Spec.Containers[0].SecurityContext.RunAsGroup = &gid
  169. tester(sysadmin)
  170. })
  171. ginkgo.By("Running a RunAsUser pod", func() {
  172. sysadmin := restrictedPod("runasuser")
  173. uid := int64(0)
  174. sysadmin.Spec.Containers[0].SecurityContext.RunAsUser = &uid
  175. tester(sysadmin)
  176. })
  177. }
  178. // createAndBindPSP creates a PSP in the policy API group.
  179. func createAndBindPSP(f *framework.Framework, pspTemplate *policy.PodSecurityPolicy) (psp *policy.PodSecurityPolicy, cleanup func()) {
  180. // Create the PodSecurityPolicy object.
  181. psp = pspTemplate.DeepCopy()
  182. // Add the namespace to the name to ensure uniqueness and tie it to the namespace.
  183. ns := f.Namespace.Name
  184. name := fmt.Sprintf("%s-%s", ns, psp.Name)
  185. psp.Name = name
  186. psp, err := f.ClientSet.PolicyV1beta1().PodSecurityPolicies().Create(psp)
  187. framework.ExpectNoError(err, "Failed to create PSP")
  188. // Create the Role to bind it to the namespace.
  189. _, err = f.ClientSet.RbacV1beta1().Roles(ns).Create(&rbacv1beta1.Role{
  190. ObjectMeta: metav1.ObjectMeta{
  191. Name: name,
  192. },
  193. Rules: []rbacv1beta1.PolicyRule{{
  194. APIGroups: []string{"policy"},
  195. Resources: []string{"podsecuritypolicies"},
  196. ResourceNames: []string{name},
  197. Verbs: []string{"use"},
  198. }},
  199. })
  200. framework.ExpectNoError(err, "Failed to create PSP role")
  201. // Bind the role to the namespace.
  202. err = auth.BindRoleInNamespace(f.ClientSet.RbacV1beta1(), name, ns, rbacv1beta1.Subject{
  203. Kind: rbacv1beta1.ServiceAccountKind,
  204. Namespace: ns,
  205. Name: "default",
  206. })
  207. framework.ExpectNoError(err)
  208. framework.ExpectNoError(auth.WaitForNamedAuthorizationUpdate(f.ClientSet.AuthorizationV1beta1(),
  209. serviceaccount.MakeUsername(ns, "default"), ns, "use", name,
  210. schema.GroupResource{Group: "policy", Resource: "podsecuritypolicies"}, true))
  211. return psp, func() {
  212. // Cleanup non-namespaced PSP object.
  213. f.ClientSet.PolicyV1beta1().PodSecurityPolicies().Delete(name, &metav1.DeleteOptions{})
  214. }
  215. }
  216. func restrictedPod(name string) *v1.Pod {
  217. return &v1.Pod{
  218. ObjectMeta: metav1.ObjectMeta{
  219. Name: name,
  220. Annotations: map[string]string{
  221. v1.SeccompPodAnnotationKey: v1.SeccompProfileRuntimeDefault,
  222. apparmor.ContainerAnnotationKeyPrefix + "pause": apparmor.ProfileRuntimeDefault,
  223. },
  224. },
  225. Spec: v1.PodSpec{
  226. Containers: []v1.Container{{
  227. Name: "pause",
  228. Image: imageutils.GetPauseImageName(),
  229. SecurityContext: &v1.SecurityContext{
  230. AllowPrivilegeEscalation: boolPtr(false),
  231. RunAsUser: utilpointer.Int64Ptr(nobodyUser),
  232. RunAsGroup: utilpointer.Int64Ptr(nobodyUser),
  233. },
  234. }},
  235. },
  236. }
  237. }
  238. // privilegedPSPInPolicy creates a PodSecurityPolicy (in the "policy" API Group) that allows everything.
  239. func privilegedPSP(name string) *policy.PodSecurityPolicy {
  240. return &policy.PodSecurityPolicy{
  241. ObjectMeta: metav1.ObjectMeta{
  242. Name: name,
  243. Annotations: map[string]string{seccomp.AllowedProfilesAnnotationKey: seccomp.AllowAny},
  244. },
  245. Spec: policy.PodSecurityPolicySpec{
  246. Privileged: true,
  247. AllowPrivilegeEscalation: utilpointer.BoolPtr(true),
  248. AllowedCapabilities: []v1.Capability{"*"},
  249. Volumes: []policy.FSType{policy.All},
  250. HostNetwork: true,
  251. HostPorts: []policy.HostPortRange{{Min: 0, Max: 65535}},
  252. HostIPC: true,
  253. HostPID: true,
  254. RunAsUser: policy.RunAsUserStrategyOptions{
  255. Rule: policy.RunAsUserStrategyRunAsAny,
  256. },
  257. RunAsGroup: &policy.RunAsGroupStrategyOptions{
  258. Rule: policy.RunAsGroupStrategyRunAsAny,
  259. },
  260. SELinux: policy.SELinuxStrategyOptions{
  261. Rule: policy.SELinuxStrategyRunAsAny,
  262. },
  263. SupplementalGroups: policy.SupplementalGroupsStrategyOptions{
  264. Rule: policy.SupplementalGroupsStrategyRunAsAny,
  265. },
  266. FSGroup: policy.FSGroupStrategyOptions{
  267. Rule: policy.FSGroupStrategyRunAsAny,
  268. },
  269. ReadOnlyRootFilesystem: false,
  270. },
  271. }
  272. }
  273. // restrictedPSPInPolicy creates a PodSecurityPolicy (in the "policy" API Group) that is most strict.
  274. func restrictedPSP(name string) *policy.PodSecurityPolicy {
  275. return &policy.PodSecurityPolicy{
  276. ObjectMeta: metav1.ObjectMeta{
  277. Name: name,
  278. Annotations: map[string]string{
  279. seccomp.AllowedProfilesAnnotationKey: v1.SeccompProfileRuntimeDefault,
  280. seccomp.DefaultProfileAnnotationKey: v1.SeccompProfileRuntimeDefault,
  281. apparmor.AllowedProfilesAnnotationKey: apparmor.ProfileRuntimeDefault,
  282. apparmor.DefaultProfileAnnotationKey: apparmor.ProfileRuntimeDefault,
  283. },
  284. },
  285. Spec: policy.PodSecurityPolicySpec{
  286. Privileged: false,
  287. AllowPrivilegeEscalation: utilpointer.BoolPtr(false),
  288. RequiredDropCapabilities: []v1.Capability{
  289. "AUDIT_WRITE",
  290. "CHOWN",
  291. "DAC_OVERRIDE",
  292. "FOWNER",
  293. "FSETID",
  294. "KILL",
  295. "MKNOD",
  296. "NET_RAW",
  297. "SETGID",
  298. "SETUID",
  299. "SYS_CHROOT",
  300. },
  301. Volumes: []policy.FSType{
  302. policy.ConfigMap,
  303. policy.EmptyDir,
  304. policy.PersistentVolumeClaim,
  305. "projected",
  306. policy.Secret,
  307. },
  308. HostNetwork: false,
  309. HostIPC: false,
  310. HostPID: false,
  311. RunAsUser: policy.RunAsUserStrategyOptions{
  312. Rule: policy.RunAsUserStrategyMustRunAsNonRoot,
  313. },
  314. RunAsGroup: &policy.RunAsGroupStrategyOptions{
  315. Rule: policy.RunAsGroupStrategyMustRunAs,
  316. Ranges: []policy.IDRange{
  317. {Min: nobodyUser, Max: nobodyUser}},
  318. },
  319. SELinux: policy.SELinuxStrategyOptions{
  320. Rule: policy.SELinuxStrategyRunAsAny,
  321. },
  322. SupplementalGroups: policy.SupplementalGroupsStrategyOptions{
  323. Rule: policy.SupplementalGroupsStrategyRunAsAny,
  324. },
  325. FSGroup: policy.FSGroupStrategyOptions{
  326. Rule: policy.FSGroupStrategyRunAsAny,
  327. },
  328. ReadOnlyRootFilesystem: false,
  329. },
  330. }
  331. }
  332. func boolPtr(b bool) *bool {
  333. return &b
  334. }