provider.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/apis/extensions"
  18. psputil "k8s.io/kubernetes/pkg/security/podsecuritypolicy/util"
  19. "k8s.io/kubernetes/pkg/util/maps"
  20. "k8s.io/kubernetes/pkg/util/validation/field"
  21. )
  22. // used to pass in the field being validated for reusable group strategies so they
  23. // can create informative error messages.
  24. const (
  25. fsGroupField = "fsGroup"
  26. supplementalGroupsField = "supplementalGroups"
  27. )
  28. // simpleProvider is the default implementation of Provider.
  29. type simpleProvider struct {
  30. psp *extensions.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 *extensions.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. // Create a PodSecurityContext based on the given constraints. If a setting is already set
  53. // on the PodSecurityContext it will not be changed. Validate should be used after the context
  54. // is created to ensure it complies with the required restrictions.
  55. //
  56. // NOTE: this method works on a copy of the PodSecurityContext. It is up to the caller to
  57. // apply the PSC if validation passes.
  58. func (s *simpleProvider) CreatePodSecurityContext(pod *api.Pod) (*api.PodSecurityContext, map[string]string, error) {
  59. var sc *api.PodSecurityContext = nil
  60. if pod.Spec.SecurityContext != nil {
  61. // work with a copy
  62. copy := *pod.Spec.SecurityContext
  63. sc = &copy
  64. } else {
  65. sc = &api.PodSecurityContext{}
  66. }
  67. annotations := maps.CopySS(pod.Annotations)
  68. if len(sc.SupplementalGroups) == 0 {
  69. supGroups, err := s.strategies.SupplementalGroupStrategy.Generate(pod)
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. sc.SupplementalGroups = supGroups
  74. }
  75. if sc.FSGroup == nil {
  76. fsGroup, err := s.strategies.FSGroupStrategy.GenerateSingle(pod)
  77. if err != nil {
  78. return nil, nil, err
  79. }
  80. sc.FSGroup = fsGroup
  81. }
  82. if sc.SELinuxOptions == nil {
  83. seLinux, err := s.strategies.SELinuxStrategy.Generate(pod, nil)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. sc.SELinuxOptions = seLinux
  88. }
  89. return sc, annotations, nil
  90. }
  91. // Create a SecurityContext based on the given constraints. If a setting is already set on the
  92. // container's security context then it will not be changed. Validation should be used after
  93. // the context is created to ensure it complies with the required restrictions.
  94. //
  95. // NOTE: this method works on a copy of the SC of the container. It is up to the caller to apply
  96. // the SC if validation passes.
  97. func (s *simpleProvider) CreateContainerSecurityContext(pod *api.Pod, container *api.Container) (*api.SecurityContext, map[string]string, error) {
  98. var sc *api.SecurityContext = nil
  99. if container.SecurityContext != nil {
  100. // work with a copy of the original
  101. copy := *container.SecurityContext
  102. sc = &copy
  103. } else {
  104. sc = &api.SecurityContext{}
  105. }
  106. annotations := maps.CopySS(pod.Annotations)
  107. if sc.RunAsUser == nil {
  108. uid, err := s.strategies.RunAsUserStrategy.Generate(pod, container)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. sc.RunAsUser = uid
  113. }
  114. if sc.SELinuxOptions == nil {
  115. seLinux, err := s.strategies.SELinuxStrategy.Generate(pod, container)
  116. if err != nil {
  117. return nil, nil, err
  118. }
  119. sc.SELinuxOptions = seLinux
  120. }
  121. annotations, err := s.strategies.AppArmorStrategy.Generate(annotations, container)
  122. if err != nil {
  123. return nil, nil, err
  124. }
  125. if sc.Privileged == nil {
  126. priv := false
  127. sc.Privileged = &priv
  128. }
  129. // if we're using the non-root strategy set the marker that this container should not be
  130. // run as root which will signal to the kubelet to do a final check either on the runAsUser
  131. // or, if runAsUser is not set, the image UID will be checked.
  132. if s.psp.Spec.RunAsUser.Rule == extensions.RunAsUserStrategyMustRunAsNonRoot {
  133. nonRoot := true
  134. sc.RunAsNonRoot = &nonRoot
  135. }
  136. caps, err := s.strategies.CapabilitiesStrategy.Generate(pod, container)
  137. if err != nil {
  138. return nil, nil, err
  139. }
  140. sc.Capabilities = caps
  141. // if the PSP requires a read only root filesystem and the container has not made a specific
  142. // request then default ReadOnlyRootFilesystem to true.
  143. if s.psp.Spec.ReadOnlyRootFilesystem && sc.ReadOnlyRootFilesystem == nil {
  144. readOnlyRootFS := true
  145. sc.ReadOnlyRootFilesystem = &readOnlyRootFS
  146. }
  147. return sc, annotations, nil
  148. }
  149. // Ensure a pod's SecurityContext is in compliance with the given constraints.
  150. func (s *simpleProvider) ValidatePodSecurityContext(pod *api.Pod, fldPath *field.Path) field.ErrorList {
  151. allErrs := field.ErrorList{}
  152. if pod.Spec.SecurityContext == nil {
  153. allErrs = append(allErrs, field.Invalid(fldPath.Child("securityContext"), pod.Spec.SecurityContext, "No security context is set"))
  154. return allErrs
  155. }
  156. fsGroups := []int64{}
  157. if pod.Spec.SecurityContext.FSGroup != nil {
  158. fsGroups = append(fsGroups, *pod.Spec.SecurityContext.FSGroup)
  159. }
  160. allErrs = append(allErrs, s.strategies.FSGroupStrategy.Validate(pod, fsGroups)...)
  161. allErrs = append(allErrs, s.strategies.SupplementalGroupStrategy.Validate(pod, pod.Spec.SecurityContext.SupplementalGroups)...)
  162. // make a dummy container context to reuse the selinux strategies
  163. container := &api.Container{
  164. Name: pod.Name,
  165. SecurityContext: &api.SecurityContext{
  166. SELinuxOptions: pod.Spec.SecurityContext.SELinuxOptions,
  167. },
  168. }
  169. allErrs = append(allErrs, s.strategies.SELinuxStrategy.Validate(pod, container)...)
  170. if !s.psp.Spec.HostNetwork && pod.Spec.SecurityContext.HostNetwork {
  171. allErrs = append(allErrs, field.Invalid(fldPath.Child("hostNetwork"), pod.Spec.SecurityContext.HostNetwork, "Host network is not allowed to be used"))
  172. }
  173. if !s.psp.Spec.HostPID && pod.Spec.SecurityContext.HostPID {
  174. allErrs = append(allErrs, field.Invalid(fldPath.Child("hostPID"), pod.Spec.SecurityContext.HostPID, "Host PID is not allowed to be used"))
  175. }
  176. if !s.psp.Spec.HostIPC && pod.Spec.SecurityContext.HostIPC {
  177. allErrs = append(allErrs, field.Invalid(fldPath.Child("hostIPC"), pod.Spec.SecurityContext.HostIPC, "Host IPC is not allowed to be used"))
  178. }
  179. allErrs = append(allErrs, s.strategies.SysctlsStrategy.Validate(pod)...)
  180. return allErrs
  181. }
  182. // Ensure a container's SecurityContext is in compliance with the given constraints
  183. func (s *simpleProvider) ValidateContainerSecurityContext(pod *api.Pod, container *api.Container, fldPath *field.Path) field.ErrorList {
  184. allErrs := field.ErrorList{}
  185. if container.SecurityContext == nil {
  186. allErrs = append(allErrs, field.Invalid(fldPath.Child("securityContext"), container.SecurityContext, "No security context is set"))
  187. return allErrs
  188. }
  189. sc := container.SecurityContext
  190. allErrs = append(allErrs, s.strategies.RunAsUserStrategy.Validate(pod, container)...)
  191. allErrs = append(allErrs, s.strategies.SELinuxStrategy.Validate(pod, container)...)
  192. allErrs = append(allErrs, s.strategies.AppArmorStrategy.Validate(pod, container)...)
  193. if !s.psp.Spec.Privileged && *sc.Privileged {
  194. allErrs = append(allErrs, field.Invalid(fldPath.Child("privileged"), *sc.Privileged, "Privileged containers are not allowed"))
  195. }
  196. allErrs = append(allErrs, s.strategies.CapabilitiesStrategy.Validate(pod, container)...)
  197. if len(pod.Spec.Volumes) > 0 && !psputil.PSPAllowsAllVolumes(s.psp) {
  198. allowedVolumes := psputil.FSTypeToStringSet(s.psp.Spec.Volumes)
  199. for i, v := range pod.Spec.Volumes {
  200. fsType, err := psputil.GetVolumeFSType(v)
  201. if err != nil {
  202. allErrs = append(allErrs, field.Invalid(fldPath.Child("volumes").Index(i), string(fsType), err.Error()))
  203. continue
  204. }
  205. if !allowedVolumes.Has(string(fsType)) {
  206. allErrs = append(allErrs, field.Invalid(
  207. fldPath.Child("volumes").Index(i), string(fsType),
  208. fmt.Sprintf("%s volumes are not allowed to be used", string(fsType))))
  209. }
  210. }
  211. }
  212. if !s.psp.Spec.HostNetwork && pod.Spec.SecurityContext.HostNetwork {
  213. allErrs = append(allErrs, field.Invalid(fldPath.Child("hostNetwork"), pod.Spec.SecurityContext.HostNetwork, "Host network is not allowed to be used"))
  214. }
  215. containersPath := fldPath.Child("containers")
  216. for idx, c := range pod.Spec.Containers {
  217. idxPath := containersPath.Index(idx)
  218. allErrs = append(allErrs, s.hasInvalidHostPort(&c, idxPath)...)
  219. }
  220. containersPath = fldPath.Child("initContainers")
  221. for idx, c := range pod.Spec.InitContainers {
  222. idxPath := containersPath.Index(idx)
  223. allErrs = append(allErrs, s.hasInvalidHostPort(&c, idxPath)...)
  224. }
  225. if !s.psp.Spec.HostPID && pod.Spec.SecurityContext.HostPID {
  226. allErrs = append(allErrs, field.Invalid(fldPath.Child("hostPID"), pod.Spec.SecurityContext.HostPID, "Host PID is not allowed to be used"))
  227. }
  228. if !s.psp.Spec.HostIPC && pod.Spec.SecurityContext.HostIPC {
  229. allErrs = append(allErrs, field.Invalid(fldPath.Child("hostIPC"), pod.Spec.SecurityContext.HostIPC, "Host IPC is not allowed to be used"))
  230. }
  231. if s.psp.Spec.ReadOnlyRootFilesystem {
  232. if sc.ReadOnlyRootFilesystem == nil {
  233. allErrs = append(allErrs, field.Invalid(fldPath.Child("readOnlyRootFilesystem"), sc.ReadOnlyRootFilesystem, "ReadOnlyRootFilesystem may not be nil and must be set to true"))
  234. } else if !*sc.ReadOnlyRootFilesystem {
  235. allErrs = append(allErrs, field.Invalid(fldPath.Child("readOnlyRootFilesystem"), *sc.ReadOnlyRootFilesystem, "ReadOnlyRootFilesystem must be set to true"))
  236. }
  237. }
  238. return allErrs
  239. }
  240. // hasHostPort checks the port definitions on the container for HostPort > 0.
  241. func (s *simpleProvider) hasInvalidHostPort(container *api.Container, fldPath *field.Path) field.ErrorList {
  242. allErrs := field.ErrorList{}
  243. for _, cp := range container.Ports {
  244. if cp.HostPort > 0 && !s.isValidHostPort(int(cp.HostPort)) {
  245. detail := fmt.Sprintf("Host port %d is not allowed to be used. Allowed ports: %v", cp.HostPort, s.psp.Spec.HostPorts)
  246. allErrs = append(allErrs, field.Invalid(fldPath.Child("hostPort"), cp.HostPort, detail))
  247. }
  248. }
  249. return allErrs
  250. }
  251. // isValidHostPort returns true if the port falls in any range allowed by the PSP.
  252. func (s *simpleProvider) isValidHostPort(port int) bool {
  253. for _, hostPortRange := range s.psp.Spec.HostPorts {
  254. if port >= hostPortRange.Min && port <= hostPortRange.Max {
  255. return true
  256. }
  257. }
  258. return false
  259. }
  260. // Get the name of the PSP that this provider was initialized with.
  261. func (s *simpleProvider) GetPSPName() string {
  262. return s.psp.Name
  263. }