Skip to content

Commit 1e9d447

Browse files
authored
Merge pull request #2631 from k8s-infra-cherrypick-robot/cherry-pick-2600-to-release-1.33
[release-1.33] fix: only use the permission bits for chmod
2 parents c16d938 + 40b400a commit 1e9d447

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

pkg/azurefile/utils.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,11 @@ func chmodIfPermissionMismatch(targetPath string, mode os.FileMode) error {
244244
return err
245245
}
246246
perm := info.Mode() & os.ModePerm
247-
if perm != mode {
248-
klog.V(2).Infof("chmod targetPath(%s, mode:0%o) with permissions(0%o)", targetPath, info.Mode(), mode)
249-
if err := os.Chmod(targetPath, mode); err != nil {
247+
expectedPerms := mode & os.ModePerm
248+
if perm != expectedPerms {
249+
klog.V(2).Infof("chmod targetPath(%s, mode:0%o) with permissions(0%o)", targetPath, info.Mode(), expectedPerms)
250+
// only change the permission mode bits, keep the other bits as is
251+
if err := os.Chmod(targetPath, (info.Mode()&^os.ModePerm)|os.FileMode(expectedPerms)); err != nil {
250252
return err
251253
}
252254
} else {

pkg/azurefile/utils_test.go

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ func TestConvertTagsToMap(t *testing.T) {
432432
}
433433

434434
func TestChmodIfPermissionMismatch(t *testing.T) {
435+
skipIfTestingOnWindows(t)
435436
permissionMatchingPath, _ := getWorkDirPath("permissionMatchingPath")
436437
_ = makeDir(permissionMatchingPath, 0755)
437438
defer os.RemoveAll(permissionMatchingPath)
@@ -440,11 +441,23 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
440441
_ = makeDir(permissionMismatchPath, 0721)
441442
defer os.RemoveAll(permissionMismatchPath)
442443

444+
permissionMatchGidMismatchPath, _ := getWorkDirPath("permissionMatchGidMismatchPath")
445+
_ = makeDir(permissionMatchGidMismatchPath, 0755)
446+
_ = os.Chmod(permissionMatchGidMismatchPath, 0755|os.ModeSetgid) // Setgid bit is set
447+
defer os.RemoveAll(permissionMatchGidMismatchPath)
448+
449+
permissionMismatchGidMismatch, _ := getWorkDirPath("permissionMismatchGidMismatch")
450+
_ = makeDir(permissionMismatchGidMismatch, 0721)
451+
_ = os.Chmod(permissionMismatchGidMismatch, 0721|os.ModeSetgid) // Setgid bit is set
452+
defer os.RemoveAll(permissionMismatchGidMismatch)
453+
443454
tests := []struct {
444-
desc string
445-
path string
446-
mode os.FileMode
447-
expectedError error
455+
desc string
456+
path string
457+
mode os.FileMode
458+
expectedPerms os.FileMode
459+
expectedGidBit bool
460+
expectedError error
448461
}{
449462
{
450463
desc: "Invalid path",
@@ -453,16 +466,44 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
453466
expectedError: fmt.Errorf("CreateFile invalid-path: The system cannot find the file specified"),
454467
},
455468
{
456-
desc: "permission matching path",
457-
path: permissionMatchingPath,
458-
mode: 0755,
459-
expectedError: nil,
469+
desc: "permission matching path",
470+
path: permissionMatchingPath,
471+
mode: 0755,
472+
expectedPerms: 0755,
473+
expectedGidBit: false,
474+
expectedError: nil,
460475
},
461476
{
462-
desc: "permission mismatch path",
463-
path: permissionMismatchPath,
464-
mode: 0755,
465-
expectedError: nil,
477+
desc: "permission mismatch path",
478+
path: permissionMismatchPath,
479+
mode: 0755,
480+
expectedPerms: 0755,
481+
expectedGidBit: false,
482+
expectedError: nil,
483+
},
484+
{
485+
desc: "only match the permission mode bits",
486+
path: permissionMatchGidMismatchPath,
487+
mode: 0755,
488+
expectedPerms: 0755,
489+
expectedGidBit: true,
490+
expectedError: nil,
491+
},
492+
{
493+
desc: "only change the permission mode bits when gid is set",
494+
path: permissionMismatchGidMismatch,
495+
mode: 0755,
496+
expectedPerms: 0755,
497+
expectedGidBit: true,
498+
expectedError: nil,
499+
},
500+
{
501+
desc: "only change the permission mode bits when gid is not set but mode bits have gid set",
502+
path: permissionMismatchPath,
503+
mode: 02755,
504+
expectedPerms: 0755,
505+
expectedGidBit: false,
506+
expectedError: nil,
466507
},
467508
}
468509

@@ -473,6 +514,18 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
473514
t.Errorf("test[%s]: unexpected error: %v, expected error: %v", test.desc, err, test.expectedError)
474515
}
475516
}
517+
518+
if test.expectedError == nil {
519+
info, _ := os.Lstat(test.path)
520+
if test.expectedError == nil && (info.Mode()&os.ModePerm != test.expectedPerms) {
521+
t.Errorf("test[%s]: unexpected perms: %v, expected perms: %v, ", test.desc, info.Mode()&os.ModePerm, test.expectedPerms)
522+
}
523+
524+
if (info.Mode()&os.ModeSetgid != 0) != test.expectedGidBit {
525+
t.Errorf("test[%s]: unexpected gid bit: %v, expected gid bit: %v", test.desc, info.Mode()&os.ModeSetgid != 0, test.expectedGidBit)
526+
}
527+
}
528+
476529
}
477530
}
478531

0 commit comments

Comments
 (0)