Skip to content

Commit 4b816f9

Browse files
author
Rohit Patil
committed
UPSTREAM: 1234: Fix user namespace validation for runAsGroup, fsGroup, and supplementalGroups
1 parent 891f5bb commit 4b816f9

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

pkg/apis/core/validation/validation.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4532,7 +4532,7 @@ func ValidatePodSpec(spec *core.PodSpec, podMeta *metav1.ObjectMeta, fldPath *fi
45324532
allErrs = append(allErrs, validateRestartPolicy(&spec.RestartPolicy, fldPath.Child("restartPolicy"))...)
45334533
allErrs = append(allErrs, validateDNSPolicy(&spec.DNSPolicy, fldPath.Child("dnsPolicy"))...)
45344534
allErrs = append(allErrs, unversionedvalidation.ValidateLabels(spec.NodeSelector, fldPath.Child("nodeSelector"))...)
4535-
allErrs = append(allErrs, validatePodSpecSecurityContext(spec.SecurityContext, spec, fldPath, fldPath.Child("securityContext"), opts)...)
4535+
allErrs = append(allErrs, validatePodSpecSecurityContext(spec.SecurityContext, spec, fldPath, fldPath.Child("securityContext"), opts, hostUsers)...)
45364536
allErrs = append(allErrs, validateImagePullSecrets(spec.ImagePullSecrets, fldPath.Child("imagePullSecrets"))...)
45374537
allErrs = append(allErrs, validateAffinity(spec.Affinity, opts, fldPath.Child("affinity"))...)
45384538
allErrs = append(allErrs, validatePodDNSConfig(spec.DNSConfig, &spec.DNSPolicy, fldPath.Child("dnsConfig"), opts)...)
@@ -5402,29 +5402,45 @@ func validateSELinuxChangePolicy(seLinuxChangePolicy *core.PodSELinuxChangePolic
54025402
// validatePodSpecSecurityContext verifies the SecurityContext of a PodSpec,
54035403
// whether that is defined in a Pod or in an embedded PodSpec (e.g. a
54045404
// Deployment's pod template).
5405-
func validatePodSpecSecurityContext(securityContext *core.PodSecurityContext, spec *core.PodSpec, specPath, fldPath *field.Path, opts PodValidationOptions) field.ErrorList {
5405+
func validatePodSpecSecurityContext(securityContext *core.PodSecurityContext, spec *core.PodSpec, specPath, fldPath *field.Path, opts PodValidationOptions, hostUsers bool) field.ErrorList {
54065406
allErrs := field.ErrorList{}
54075407

54085408
if securityContext != nil {
54095409
if securityContext.FSGroup != nil {
54105410
for _, msg := range validation.IsValidGroupID(*securityContext.FSGroup) {
54115411
allErrs = append(allErrs, field.Invalid(fldPath.Child("fsGroup"), *(securityContext.FSGroup), msg))
54125412
}
5413+
// When user namespaces are enabled (hostUsers=false), GIDs must be in range 0-65535
5414+
if !hostUsers && *securityContext.FSGroup > 65535 {
5415+
allErrs = append(allErrs, field.Invalid(fldPath.Child("fsGroup"), *securityContext.FSGroup, "must be between 0 and 65535 when user namespaces are enabled (hostUsers=false)"))
5416+
}
54135417
}
54145418
if securityContext.RunAsUser != nil {
54155419
for _, msg := range validation.IsValidUserID(*securityContext.RunAsUser) {
54165420
allErrs = append(allErrs, field.Invalid(fldPath.Child("runAsUser"), *(securityContext.RunAsUser), msg))
54175421
}
5422+
// When user namespaces are enabled (hostUsers=false), UIDs must be in range 0-65535
5423+
if !hostUsers && *securityContext.RunAsUser > 65535 {
5424+
allErrs = append(allErrs, field.Invalid(fldPath.Child("runAsUser"), *securityContext.RunAsUser, "must be between 0 and 65535 when user namespaces are enabled (hostUsers=false)"))
5425+
}
54185426
}
54195427
if securityContext.RunAsGroup != nil {
54205428
for _, msg := range validation.IsValidGroupID(*securityContext.RunAsGroup) {
54215429
allErrs = append(allErrs, field.Invalid(fldPath.Child("runAsGroup"), *(securityContext.RunAsGroup), msg))
54225430
}
5431+
// When user namespaces are enabled (hostUsers=false), GIDs must be in range 0-65535
5432+
if !hostUsers && *securityContext.RunAsGroup > 65535 {
5433+
allErrs = append(allErrs, field.Invalid(fldPath.Child("runAsGroup"), *securityContext.RunAsGroup, "must be between 0 and 65535 when user namespaces are enabled (hostUsers=false)"))
5434+
}
54235435
}
54245436
for g, gid := range securityContext.SupplementalGroups {
54255437
for _, msg := range validation.IsValidGroupID(gid) {
54265438
allErrs = append(allErrs, field.Invalid(fldPath.Child("supplementalGroups").Index(g), gid, msg))
54275439
}
5440+
// When user namespaces are enabled (hostUsers=false), GIDs must be in range 0-65535
5441+
if !hostUsers && gid > 65535 {
5442+
allErrs = append(allErrs, field.Invalid(fldPath.Child("supplementalGroups").Index(g), gid, "must be between 0 and 65535 when user namespaces are enabled (hostUsers=false)"))
5443+
}
54285444
}
54295445
if securityContext.ShareProcessNamespace != nil && securityContext.HostPID && *securityContext.ShareProcessNamespace {
54305446
allErrs = append(allErrs, field.Invalid(fldPath.Child("shareProcessNamespace"), *securityContext.ShareProcessNamespace, "ShareProcessNamespace and HostPID cannot both be enabled"))
@@ -8072,12 +8088,20 @@ func ValidateSecurityContext(sc *core.SecurityContext, fldPath *field.Path, host
80728088
for _, msg := range validation.IsValidUserID(*sc.RunAsUser) {
80738089
allErrs = append(allErrs, field.Invalid(fldPath.Child("runAsUser"), *sc.RunAsUser, msg))
80748090
}
8091+
// When user namespaces are enabled (hostUsers=false), UIDs must be in range 0-65535
8092+
if !hostUsers && *sc.RunAsUser > 65535 {
8093+
allErrs = append(allErrs, field.Invalid(fldPath.Child("runAsUser"), *sc.RunAsUser, "must be between 0 and 65535 when user namespaces are enabled (hostUsers=false)"))
8094+
}
80758095
}
80768096

80778097
if sc.RunAsGroup != nil {
80788098
for _, msg := range validation.IsValidGroupID(*sc.RunAsGroup) {
80798099
allErrs = append(allErrs, field.Invalid(fldPath.Child("runAsGroup"), *sc.RunAsGroup, msg))
80808100
}
8101+
// When user namespaces are enabled (hostUsers=false), GIDs must be in range 0-65535
8102+
if !hostUsers && *sc.RunAsGroup > 65535 {
8103+
allErrs = append(allErrs, field.Invalid(fldPath.Child("runAsGroup"), *sc.RunAsGroup, "must be between 0 and 65535 when user namespaces are enabled (hostUsers=false)"))
8104+
}
80818105
}
80828106

80838107
if sc.ProcMount != nil {

0 commit comments

Comments
 (0)