Skip to content

Commit 43f0423

Browse files
authored
Merge pull request kubernetes#72419 from liggitt/allowed-proc-mount-validation
Validate PSP allowedProcMountTypes
2 parents a65ba5f + cb76da9 commit 43f0423

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

pkg/apis/core/validation/validation.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5221,8 +5221,8 @@ func ValidateSecurityContext(sc *core.SecurityContext, fldPath *field.Path) fiel
52215221
}
52225222

52235223
if sc.ProcMount != nil {
5224-
if err := IsValidProcMount(*sc.ProcMount); err != nil {
5225-
allErrs = append(allErrs, field.NotSupported(fldPath.Child("procMount"), *sc.ProcMount, []string{string(core.DefaultProcMount), string(core.UnmaskedProcMount)}))
5224+
if err := ValidateProcMountType(fldPath.Child("procMount"), *sc.ProcMount); err != nil {
5225+
allErrs = append(allErrs, err)
52265226
}
52275227
}
52285228

@@ -5323,13 +5323,12 @@ func IsDecremented(update, old *int32) bool {
53235323
return *update < *old
53245324
}
53255325

5326-
// IsValidProcMount tests that the argument is a valid ProcMountType.
5327-
func IsValidProcMount(procMountType core.ProcMountType) error {
5326+
// ValidateProcMountType tests that the argument is a valid ProcMountType.
5327+
func ValidateProcMountType(fldPath *field.Path, procMountType core.ProcMountType) *field.Error {
53285328
switch procMountType {
5329-
case core.DefaultProcMount:
5330-
case core.UnmaskedProcMount:
5329+
case core.DefaultProcMount, core.UnmaskedProcMount:
5330+
return nil
53315331
default:
5332-
return fmt.Errorf("unsupported ProcMount type %s", procMountType)
5332+
return field.NotSupported(fldPath, procMountType, []string{string(core.DefaultProcMount), string(core.UnmaskedProcMount)})
53335333
}
5334-
return nil
53355334
}

pkg/apis/policy/validation/validation.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func ValidatePodSecurityPolicySpec(spec *policy.PodSecurityPolicySpec, fldPath *
121121
allErrs = append(allErrs, validatePSPCapsAgainstDrops(spec.RequiredDropCapabilities, spec.DefaultAddCapabilities, field.NewPath("defaultAddCapabilities"))...)
122122
allErrs = append(allErrs, validatePSPCapsAgainstDrops(spec.RequiredDropCapabilities, spec.AllowedCapabilities, field.NewPath("allowedCapabilities"))...)
123123
allErrs = append(allErrs, validatePSPDefaultAllowPrivilegeEscalation(fldPath.Child("defaultAllowPrivilegeEscalation"), spec.DefaultAllowPrivilegeEscalation, spec.AllowPrivilegeEscalation)...)
124+
allErrs = append(allErrs, validatePSPAllowedProcMountTypes(fldPath.Child("allowedProcMountTypes"), spec.AllowedProcMountTypes)...)
124125
allErrs = append(allErrs, validatePSPAllowedHostPaths(fldPath.Child("allowedHostPaths"), spec.AllowedHostPaths)...)
125126
allErrs = append(allErrs, validatePSPAllowedFlexVolumes(fldPath.Child("allowedFlexVolumes"), spec.AllowedFlexVolumes)...)
126127
allErrs = append(allErrs, validatePodSecurityPolicySysctls(fldPath.Child("allowedUnsafeSysctls"), spec.AllowedUnsafeSysctls)...)
@@ -328,6 +329,17 @@ func validatePSPDefaultAllowPrivilegeEscalation(fldPath *field.Path, defaultAllo
328329
return allErrs
329330
}
330331

332+
// validatePSPAllowedProcMountTypes validates the DefaultAllowPrivilegeEscalation field against the AllowPrivilegeEscalation field of a PodSecurityPolicy.
333+
func validatePSPAllowedProcMountTypes(fldPath *field.Path, allowedProcMountTypes []core.ProcMountType) field.ErrorList {
334+
allErrs := field.ErrorList{}
335+
for i, procMountType := range allowedProcMountTypes {
336+
if err := apivalidation.ValidateProcMountType(fldPath.Index(i), procMountType); err != nil {
337+
allErrs = append(allErrs, err)
338+
}
339+
}
340+
return allErrs
341+
}
342+
331343
const sysctlPatternSegmentFmt string = "([a-z0-9][-_a-z0-9]*)?[a-z0-9*]"
332344
const SysctlPatternFmt string = "(" + apivalidation.SysctlSegmentFmt + "\\.)*" + sysctlPatternSegmentFmt
333345

pkg/apis/policy/validation/validation_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
384384
nonEmptyFlexVolumes := validPSP()
385385
nonEmptyFlexVolumes.Spec.AllowedFlexVolumes = []policy.AllowedFlexVolume{{Driver: "example/driver"}}
386386

387+
invalidProcMount := validPSP()
388+
invalidProcMount.Spec.AllowedProcMountTypes = []api.ProcMountType{api.ProcMountType("bogus")}
389+
387390
type testCase struct {
388391
psp *policy.PodSecurityPolicy
389392
errorType field.ErrorType
@@ -550,6 +553,11 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
550553
errorType: field.ErrorTypeRequired,
551554
errorDetail: "must specify a driver",
552555
},
556+
"invalid allowedProcMountTypes": {
557+
psp: invalidProcMount,
558+
errorType: field.ErrorTypeNotSupported,
559+
errorDetail: `supported values: "Default", "Unmasked"`,
560+
},
553561
}
554562

555563
for k, v := range errorCases {
@@ -643,6 +651,10 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
643651
flexvolumeWhenAllVolumesAllowed.Spec.AllowedFlexVolumes = []policy.AllowedFlexVolume{
644652
{Driver: "example/driver2"},
645653
}
654+
655+
validProcMount := validPSP()
656+
validProcMount.Spec.AllowedProcMountTypes = []api.ProcMountType{api.DefaultProcMount, api.UnmaskedProcMount}
657+
646658
successCases := map[string]struct {
647659
psp *policy.PodSecurityPolicy
648660
}{
@@ -682,6 +694,9 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
682694
"allow white-listed flexVolume when all volumes are allowed": {
683695
psp: flexvolumeWhenAllVolumesAllowed,
684696
},
697+
"valid allowedProcMountTypes": {
698+
psp: validProcMount,
699+
},
685700
}
686701

687702
for k, v := range successCases {

0 commit comments

Comments
 (0)