provider.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. Copyright 2016 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 podsecuritypolicy
  14. import (
  15. "fmt"
  16. "strings"
  17. corev1 "k8s.io/api/core/v1"
  18. policy "k8s.io/api/policy/v1beta1"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. utilfeature "k8s.io/apiserver/pkg/util/feature"
  21. podutil "k8s.io/kubernetes/pkg/api/pod"
  22. api "k8s.io/kubernetes/pkg/apis/core"
  23. "k8s.io/kubernetes/pkg/apis/core/pods"
  24. "k8s.io/kubernetes/pkg/features"
  25. psputil "k8s.io/kubernetes/pkg/security/podsecuritypolicy/util"
  26. "k8s.io/kubernetes/pkg/securitycontext"
  27. )
  28. // simpleProvider is the default implementation of Provider.
  29. type simpleProvider struct {
  30. psp *policy.PodSecurityPolicy
  31. strategies *ProviderStrategies
  32. }
  33. // ensure we implement the interface correctly.
  34. var _ Provider = &simpleProvider{}
  35. // NewSimpleProvider creates a new Provider instance.
  36. func NewSimpleProvider(psp *policy.PodSecurityPolicy, namespace string, strategyFactory StrategyFactory) (Provider, error) {
  37. if psp == nil {
  38. return nil, fmt.Errorf("NewSimpleProvider requires a PodSecurityPolicy")
  39. }
  40. if strategyFactory == nil {
  41. return nil, fmt.Errorf("NewSimpleProvider requires a StrategyFactory")
  42. }
  43. strategies, err := strategyFactory.CreateStrategies(psp, namespace)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &simpleProvider{
  48. psp: psp,
  49. strategies: strategies,
  50. }, nil
  51. }
  52. // MutatePod sets the default values of the required but not filled fields.
  53. // Validation should be used after the context is defaulted to ensure it
  54. // complies with the required restrictions.
  55. func (s *simpleProvider) MutatePod(pod *api.Pod) error {
  56. sc := securitycontext.NewPodSecurityContextMutator(pod.Spec.SecurityContext)
  57. if sc.SupplementalGroups() == nil {
  58. supGroups, err := s.strategies.SupplementalGroupStrategy.Generate(pod)
  59. if err != nil {
  60. return err
  61. }
  62. sc.SetSupplementalGroups(supGroups)
  63. }
  64. if sc.FSGroup() == nil {
  65. fsGroup, err := s.strategies.FSGroupStrategy.GenerateSingle(pod)
  66. if err != nil {
  67. return err
  68. }
  69. sc.SetFSGroup(fsGroup)
  70. }
  71. if sc.SELinuxOptions() == nil {
  72. seLinux, err := s.strategies.SELinuxStrategy.Generate(pod, nil)
  73. if err != nil {
  74. return err
  75. }
  76. sc.SetSELinuxOptions(seLinux)
  77. }
  78. // This is only generated on the pod level. Containers inherit the pod's profile. If the
  79. // container has a specific profile set then it will be caught in the validation step.
  80. seccompProfile, err := s.strategies.SeccompStrategy.Generate(pod.Annotations, pod)
  81. if err != nil {
  82. return err
  83. }
  84. if seccompProfile != "" {
  85. if pod.Annotations == nil {
  86. pod.Annotations = map[string]string{}
  87. }
  88. pod.Annotations[api.SeccompPodAnnotationKey] = seccompProfile
  89. }
  90. pod.Spec.SecurityContext = sc.PodSecurityContext()
  91. if s.psp.Spec.RuntimeClass != nil && pod.Spec.RuntimeClassName == nil {
  92. pod.Spec.RuntimeClassName = s.psp.Spec.RuntimeClass.DefaultRuntimeClassName
  93. }
  94. var retErr error
  95. podutil.VisitContainers(&pod.Spec, func(c *api.Container) bool {
  96. retErr = s.mutateContainer(pod, c)
  97. if retErr != nil {
  98. return false
  99. }
  100. return true
  101. })
  102. return retErr
  103. }
  104. // mutateContainer sets the default values of the required but not filled fields.
  105. // It modifies the SecurityContext of the container and annotations of the pod. Validation should
  106. // be used after the context is defaulted to ensure it complies with the required restrictions.
  107. func (s *simpleProvider) mutateContainer(pod *api.Pod, container *api.Container) error {
  108. sc := securitycontext.NewEffectiveContainerSecurityContextMutator(
  109. securitycontext.NewPodSecurityContextAccessor(pod.Spec.SecurityContext),
  110. securitycontext.NewContainerSecurityContextMutator(container.SecurityContext),
  111. )
  112. if sc.RunAsUser() == nil {
  113. uid, err := s.strategies.RunAsUserStrategy.Generate(pod, container)
  114. if err != nil {
  115. return err
  116. }
  117. sc.SetRunAsUser(uid)
  118. }
  119. if utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) {
  120. if sc.RunAsGroup() == nil {
  121. gid, err := s.strategies.RunAsGroupStrategy.GenerateSingle(pod)
  122. if err != nil {
  123. return err
  124. }
  125. sc.SetRunAsGroup(gid)
  126. }
  127. }
  128. if sc.SELinuxOptions() == nil {
  129. seLinux, err := s.strategies.SELinuxStrategy.Generate(pod, container)
  130. if err != nil {
  131. return err
  132. }
  133. sc.SetSELinuxOptions(seLinux)
  134. }
  135. annotations, err := s.strategies.AppArmorStrategy.Generate(pod.Annotations, container)
  136. if err != nil {
  137. return err
  138. }
  139. // if we're using the non-root strategy set the marker that this container should not be
  140. // run as root which will signal to the kubelet to do a final check either on the runAsUser
  141. // or, if runAsUser is not set, the image UID will be checked.
  142. if sc.RunAsNonRoot() == nil && sc.RunAsUser() == nil && s.psp.Spec.RunAsUser.Rule == policy.RunAsUserStrategyMustRunAsNonRoot {
  143. nonRoot := true
  144. sc.SetRunAsNonRoot(&nonRoot)
  145. }
  146. caps, err := s.strategies.CapabilitiesStrategy.Generate(pod, container)
  147. if err != nil {
  148. return err
  149. }
  150. sc.SetCapabilities(caps)
  151. // if the PSP requires a read only root filesystem and the container has not made a specific
  152. // request then default ReadOnlyRootFilesystem to true.
  153. if s.psp.Spec.ReadOnlyRootFilesystem && sc.ReadOnlyRootFilesystem() == nil {
  154. readOnlyRootFS := true
  155. sc.SetReadOnlyRootFilesystem(&readOnlyRootFS)
  156. }
  157. // if the PSP sets DefaultAllowPrivilegeEscalation and the container security context
  158. // allowPrivilegeEscalation is not set, then default to that set by the PSP.
  159. if s.psp.Spec.DefaultAllowPrivilegeEscalation != nil && sc.AllowPrivilegeEscalation() == nil {
  160. sc.SetAllowPrivilegeEscalation(s.psp.Spec.DefaultAllowPrivilegeEscalation)
  161. }
  162. // if the PSP sets psp.AllowPrivilegeEscalation to false, set that as the default
  163. if !*s.psp.Spec.AllowPrivilegeEscalation && sc.AllowPrivilegeEscalation() == nil {
  164. sc.SetAllowPrivilegeEscalation(s.psp.Spec.AllowPrivilegeEscalation)
  165. }
  166. pod.Annotations = annotations
  167. container.SecurityContext = sc.ContainerSecurityContext()
  168. return nil
  169. }
  170. // ValidatePod ensure a pod is in compliance with the given constraints.
  171. func (s *simpleProvider) ValidatePod(pod *api.Pod) field.ErrorList {
  172. allErrs := field.ErrorList{}
  173. sc := securitycontext.NewPodSecurityContextAccessor(pod.Spec.SecurityContext)
  174. scPath := field.NewPath("spec", "securityContext")
  175. var fsGroups []int64
  176. if fsGroup := sc.FSGroup(); fsGroup != nil {
  177. fsGroups = []int64{*fsGroup}
  178. }
  179. allErrs = append(allErrs, s.strategies.FSGroupStrategy.Validate(scPath.Child("fsGroup"), pod, fsGroups)...)
  180. allErrs = append(allErrs, s.strategies.SupplementalGroupStrategy.Validate(scPath.Child("supplementalGroups"), pod, sc.SupplementalGroups())...)
  181. allErrs = append(allErrs, s.strategies.SeccompStrategy.ValidatePod(pod)...)
  182. allErrs = append(allErrs, s.strategies.SELinuxStrategy.Validate(scPath.Child("seLinuxOptions"), pod, nil, sc.SELinuxOptions())...)
  183. if !s.psp.Spec.HostNetwork && sc.HostNetwork() {
  184. allErrs = append(allErrs, field.Invalid(scPath.Child("hostNetwork"), sc.HostNetwork(), "Host network is not allowed to be used"))
  185. }
  186. if !s.psp.Spec.HostPID && sc.HostPID() {
  187. allErrs = append(allErrs, field.Invalid(scPath.Child("hostPID"), sc.HostPID(), "Host PID is not allowed to be used"))
  188. }
  189. if !s.psp.Spec.HostIPC && sc.HostIPC() {
  190. allErrs = append(allErrs, field.Invalid(scPath.Child("hostIPC"), sc.HostIPC(), "Host IPC is not allowed to be used"))
  191. }
  192. allErrs = append(allErrs, s.strategies.SysctlsStrategy.Validate(pod)...)
  193. allErrs = append(allErrs, s.validatePodVolumes(pod)...)
  194. if s.psp.Spec.RuntimeClass != nil {
  195. allErrs = append(allErrs, validateRuntimeClassName(pod.Spec.RuntimeClassName, s.psp.Spec.RuntimeClass.AllowedRuntimeClassNames)...)
  196. }
  197. pods.VisitContainersWithPath(&pod.Spec, func(c *api.Container, p *field.Path) bool {
  198. allErrs = append(allErrs, s.validateContainer(pod, c, p)...)
  199. return true
  200. })
  201. return allErrs
  202. }
  203. func (s *simpleProvider) validatePodVolumes(pod *api.Pod) field.ErrorList {
  204. allErrs := field.ErrorList{}
  205. if len(pod.Spec.Volumes) > 0 {
  206. allowsAllVolumeTypes := psputil.PSPAllowsAllVolumes(s.psp)
  207. allowedVolumes := psputil.FSTypeToStringSet(s.psp.Spec.Volumes)
  208. for i, v := range pod.Spec.Volumes {
  209. fsType, err := psputil.GetVolumeFSType(v)
  210. if err != nil {
  211. allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "volumes").Index(i), string(fsType), err.Error()))
  212. continue
  213. }
  214. if !allowsAllVolumeTypes && !allowedVolumes.Has(string(fsType)) {
  215. allErrs = append(allErrs, field.Invalid(
  216. field.NewPath("spec", "volumes").Index(i), string(fsType),
  217. fmt.Sprintf("%s volumes are not allowed to be used", string(fsType))))
  218. continue
  219. }
  220. switch fsType {
  221. case policy.HostPath:
  222. allows, mustBeReadOnly := psputil.AllowsHostVolumePath(s.psp, v.HostPath.Path)
  223. if !allows {
  224. allErrs = append(allErrs, field.Invalid(
  225. field.NewPath("spec", "volumes").Index(i).Child("hostPath", "pathPrefix"), v.HostPath.Path,
  226. fmt.Sprintf("is not allowed to be used")))
  227. } else if mustBeReadOnly {
  228. // Ensure all the VolumeMounts that use this volume are read-only
  229. pods.VisitContainersWithPath(&pod.Spec, func(c *api.Container, p *field.Path) bool {
  230. for i, cv := range c.VolumeMounts {
  231. if cv.Name == v.Name && !cv.ReadOnly {
  232. allErrs = append(allErrs, field.Invalid(p.Child("volumeMounts").Index(i).Child("readOnly"), cv.ReadOnly, "must be read-only"))
  233. }
  234. }
  235. return true
  236. })
  237. }
  238. case policy.FlexVolume:
  239. if len(s.psp.Spec.AllowedFlexVolumes) > 0 {
  240. found := false
  241. driver := v.FlexVolume.Driver
  242. for _, allowedFlexVolume := range s.psp.Spec.AllowedFlexVolumes {
  243. if driver == allowedFlexVolume.Driver {
  244. found = true
  245. break
  246. }
  247. }
  248. if !found {
  249. allErrs = append(allErrs,
  250. field.Invalid(field.NewPath("spec", "volumes").Index(i).Child("driver"), driver,
  251. "Flexvolume driver is not allowed to be used"))
  252. }
  253. }
  254. case policy.CSI:
  255. if utilfeature.DefaultFeatureGate.Enabled(features.CSIInlineVolume) {
  256. if len(s.psp.Spec.AllowedCSIDrivers) > 0 {
  257. found := false
  258. driver := v.CSI.Driver
  259. for _, allowedCSIDriver := range s.psp.Spec.AllowedCSIDrivers {
  260. if driver == allowedCSIDriver.Name {
  261. found = true
  262. break
  263. }
  264. }
  265. if !found {
  266. allErrs = append(allErrs,
  267. field.Invalid(field.NewPath("spec", "volumes").Index(i).Child("csi", "driver"), driver,
  268. "Inline CSI driver is not allowed to be used"))
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }
  275. return allErrs
  276. }
  277. // Ensure a container's SecurityContext is in compliance with the given constraints
  278. func (s *simpleProvider) validateContainer(pod *api.Pod, container *api.Container, containerPath *field.Path) field.ErrorList {
  279. allErrs := field.ErrorList{}
  280. podSC := securitycontext.NewPodSecurityContextAccessor(pod.Spec.SecurityContext)
  281. sc := securitycontext.NewEffectiveContainerSecurityContextAccessor(podSC, securitycontext.NewContainerSecurityContextMutator(container.SecurityContext))
  282. scPath := containerPath.Child("securityContext")
  283. allErrs = append(allErrs, s.strategies.RunAsUserStrategy.Validate(scPath, pod, container, sc.RunAsNonRoot(), sc.RunAsUser())...)
  284. if utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) {
  285. var runAsGroups []int64
  286. if sc.RunAsGroup() != nil {
  287. runAsGroups = []int64{*sc.RunAsGroup()}
  288. }
  289. allErrs = append(allErrs, s.strategies.RunAsGroupStrategy.Validate(scPath, pod, runAsGroups)...)
  290. }
  291. allErrs = append(allErrs, s.strategies.SELinuxStrategy.Validate(scPath.Child("seLinuxOptions"), pod, container, sc.SELinuxOptions())...)
  292. allErrs = append(allErrs, s.strategies.AppArmorStrategy.Validate(pod, container)...)
  293. allErrs = append(allErrs, s.strategies.SeccompStrategy.ValidateContainer(pod, container)...)
  294. privileged := sc.Privileged()
  295. if !s.psp.Spec.Privileged && privileged != nil && *privileged {
  296. allErrs = append(allErrs, field.Invalid(scPath.Child("privileged"), *privileged, "Privileged containers are not allowed"))
  297. }
  298. procMount := sc.ProcMount()
  299. allowedProcMounts := s.psp.Spec.AllowedProcMountTypes
  300. if len(allowedProcMounts) == 0 {
  301. allowedProcMounts = []corev1.ProcMountType{corev1.DefaultProcMount}
  302. }
  303. foundProcMountType := false
  304. for _, pm := range allowedProcMounts {
  305. if string(pm) == string(procMount) {
  306. foundProcMountType = true
  307. }
  308. }
  309. if !foundProcMountType {
  310. allErrs = append(allErrs, field.Invalid(scPath.Child("procMount"), procMount, "ProcMountType is not allowed"))
  311. }
  312. allErrs = append(allErrs, s.strategies.CapabilitiesStrategy.Validate(scPath.Child("capabilities"), pod, container, sc.Capabilities())...)
  313. allErrs = append(allErrs, s.hasInvalidHostPort(container, containerPath)...)
  314. if s.psp.Spec.ReadOnlyRootFilesystem {
  315. readOnly := sc.ReadOnlyRootFilesystem()
  316. if readOnly == nil {
  317. allErrs = append(allErrs, field.Invalid(scPath.Child("readOnlyRootFilesystem"), readOnly, "ReadOnlyRootFilesystem may not be nil and must be set to true"))
  318. } else if !*readOnly {
  319. allErrs = append(allErrs, field.Invalid(scPath.Child("readOnlyRootFilesystem"), *readOnly, "ReadOnlyRootFilesystem must be set to true"))
  320. }
  321. }
  322. allowEscalation := sc.AllowPrivilegeEscalation()
  323. if !*s.psp.Spec.AllowPrivilegeEscalation && (allowEscalation == nil || *allowEscalation) {
  324. allErrs = append(allErrs, field.Invalid(scPath.Child("allowPrivilegeEscalation"), allowEscalation, "Allowing privilege escalation for containers is not allowed"))
  325. }
  326. return allErrs
  327. }
  328. // hasInvalidHostPort checks whether the port definitions on the container fall outside of the ranges allowed by the PSP.
  329. func (s *simpleProvider) hasInvalidHostPort(container *api.Container, fldPath *field.Path) field.ErrorList {
  330. allErrs := field.ErrorList{}
  331. for _, cp := range container.Ports {
  332. if cp.HostPort > 0 && !s.isValidHostPort(cp.HostPort) {
  333. detail := fmt.Sprintf("Host port %d is not allowed to be used. Allowed ports: [%s]", cp.HostPort, hostPortRangesToString(s.psp.Spec.HostPorts))
  334. allErrs = append(allErrs, field.Invalid(fldPath.Child("hostPort"), cp.HostPort, detail))
  335. }
  336. }
  337. return allErrs
  338. }
  339. // isValidHostPort returns true if the port falls in any range allowed by the PSP.
  340. func (s *simpleProvider) isValidHostPort(port int32) bool {
  341. for _, hostPortRange := range s.psp.Spec.HostPorts {
  342. if port >= hostPortRange.Min && port <= hostPortRange.Max {
  343. return true
  344. }
  345. }
  346. return false
  347. }
  348. // Get the name of the PSP that this provider was initialized with.
  349. func (s *simpleProvider) GetPSPName() string {
  350. return s.psp.Name
  351. }
  352. func hostPortRangesToString(ranges []policy.HostPortRange) string {
  353. formattedString := ""
  354. if ranges != nil {
  355. strRanges := []string{}
  356. for _, r := range ranges {
  357. if r.Min == r.Max {
  358. strRanges = append(strRanges, fmt.Sprintf("%d", r.Min))
  359. } else {
  360. strRanges = append(strRanges, fmt.Sprintf("%d-%d", r.Min, r.Max))
  361. }
  362. }
  363. formattedString = strings.Join(strRanges, ",")
  364. }
  365. return formattedString
  366. }
  367. // validates that the actual RuntimeClassName is contained in the list of valid names.
  368. func validateRuntimeClassName(actual *string, validNames []string) field.ErrorList {
  369. if actual == nil {
  370. return nil // An unset RuntimeClassName is always allowed.
  371. }
  372. for _, valid := range validNames {
  373. if valid == policy.AllowAllRuntimeClassNames {
  374. return nil
  375. }
  376. if *actual == valid {
  377. return nil
  378. }
  379. }
  380. return field.ErrorList{field.Invalid(field.NewPath("spec", "runtimeClassName"), *actual, "")}
  381. }