whitelist.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 sysctl
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/kubernetes/pkg/apis/core/validation"
  18. policyvalidation "k8s.io/kubernetes/pkg/apis/policy/validation"
  19. "k8s.io/kubernetes/pkg/kubelet/lifecycle"
  20. )
  21. const (
  22. AnnotationInvalidReason = "InvalidSysctlAnnotation"
  23. ForbiddenReason = "SysctlForbidden"
  24. )
  25. // patternWhitelist takes a list of sysctls or sysctl patterns (ending in *) and
  26. // checks validity via a sysctl and prefix map, rejecting those which are not known
  27. // to be namespaced.
  28. type patternWhitelist struct {
  29. sysctls map[string]Namespace
  30. prefixes map[string]Namespace
  31. }
  32. var _ lifecycle.PodAdmitHandler = &patternWhitelist{}
  33. // NewWhitelist creates a new Whitelist from a list of sysctls and sysctl pattern (ending in *).
  34. func NewWhitelist(patterns []string) (*patternWhitelist, error) {
  35. w := &patternWhitelist{
  36. sysctls: map[string]Namespace{},
  37. prefixes: map[string]Namespace{},
  38. }
  39. for _, s := range patterns {
  40. if !policyvalidation.IsValidSysctlPattern(s) {
  41. return nil, fmt.Errorf("sysctl %q must have at most %d characters and match regex %s",
  42. s,
  43. validation.SysctlMaxLength,
  44. policyvalidation.SysctlPatternFmt,
  45. )
  46. }
  47. if strings.HasSuffix(s, "*") {
  48. prefix := s[:len(s)-1]
  49. ns := NamespacedBy(prefix)
  50. if ns == UnknownNamespace {
  51. return nil, fmt.Errorf("the sysctls %q are not known to be namespaced", s)
  52. }
  53. w.prefixes[prefix] = ns
  54. } else {
  55. ns := NamespacedBy(s)
  56. if ns == UnknownNamespace {
  57. return nil, fmt.Errorf("the sysctl %q are not known to be namespaced", s)
  58. }
  59. w.sysctls[s] = ns
  60. }
  61. }
  62. return w, nil
  63. }
  64. // validateSysctl checks that a sysctl is whitelisted because it is known
  65. // to be namespaced by the Linux kernel. Note that being whitelisted is required, but not
  66. // sufficient: the container runtime might have a stricter check and refuse to launch a pod.
  67. //
  68. // The parameters hostNet and hostIPC are used to forbid sysctls for pod sharing the
  69. // respective namespaces with the host. This check is only possible for sysctls on
  70. // the static default whitelist, not those on the custom whitelist provided by the admin.
  71. func (w *patternWhitelist) validateSysctl(sysctl string, hostNet, hostIPC bool) error {
  72. nsErrorFmt := "%q not allowed with host %s enabled"
  73. if ns, found := w.sysctls[sysctl]; found {
  74. if ns == IpcNamespace && hostIPC {
  75. return fmt.Errorf(nsErrorFmt, sysctl, ns)
  76. }
  77. if ns == NetNamespace && hostNet {
  78. return fmt.Errorf(nsErrorFmt, sysctl, ns)
  79. }
  80. return nil
  81. }
  82. for p, ns := range w.prefixes {
  83. if strings.HasPrefix(sysctl, p) {
  84. if ns == IpcNamespace && hostIPC {
  85. return fmt.Errorf(nsErrorFmt, sysctl, ns)
  86. }
  87. if ns == NetNamespace && hostNet {
  88. return fmt.Errorf(nsErrorFmt, sysctl, ns)
  89. }
  90. return nil
  91. }
  92. }
  93. return fmt.Errorf("%q not whitelisted", sysctl)
  94. }
  95. // Admit checks that all sysctls given in pod's security context
  96. // are valid according to the whitelist.
  97. func (w *patternWhitelist) Admit(attrs *lifecycle.PodAdmitAttributes) lifecycle.PodAdmitResult {
  98. pod := attrs.Pod
  99. if pod.Spec.SecurityContext == nil || len(pod.Spec.SecurityContext.Sysctls) == 0 {
  100. return lifecycle.PodAdmitResult{
  101. Admit: true,
  102. }
  103. }
  104. var hostNet, hostIPC bool
  105. if pod.Spec.SecurityContext != nil {
  106. hostNet = pod.Spec.HostNetwork
  107. hostIPC = pod.Spec.HostIPC
  108. }
  109. for _, s := range pod.Spec.SecurityContext.Sysctls {
  110. if err := w.validateSysctl(s.Name, hostNet, hostIPC); err != nil {
  111. return lifecycle.PodAdmitResult{
  112. Admit: false,
  113. Reason: ForbiddenReason,
  114. Message: fmt.Sprintf("forbidden sysctl: %v", err),
  115. }
  116. }
  117. }
  118. return lifecycle.PodAdmitResult{
  119. Admit: true,
  120. }
  121. }