Skip to content

Commit 6e1b88b

Browse files
authored
Merge pull request #2046 from k8s-infra-cherrypick-robot/cherry-pick-2041-to-release-1.26
[release-1.26] fix: only use the permission bits for chmod
2 parents bd3389a + c7736f9 commit 6e1b88b

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

pkg/blob/blob.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,9 +1092,11 @@ func chmodIfPermissionMismatch(targetPath string, mode os.FileMode) error {
10921092
return err
10931093
}
10941094
perm := info.Mode() & os.ModePerm
1095-
if perm != mode {
1096-
klog.V(2).Infof("chmod targetPath(%s, mode:0%o) with permissions(0%o)", targetPath, info.Mode(), mode)
1097-
if err := os.Chmod(targetPath, mode); err != nil {
1095+
expectedPerms := mode & os.ModePerm
1096+
if perm != expectedPerms {
1097+
klog.V(2).Infof("chmod targetPath(%s, mode:0%o) with permissions(0%o)", targetPath, info.Mode(), expectedPerms)
1098+
// only change the permission mode bits, keep the other bits as is
1099+
if err := os.Chmod(targetPath, (info.Mode()&^os.ModePerm)|os.FileMode(expectedPerms)); err != nil {
10981100
return err
10991101
}
11001102
} else {

pkg/blob/blob_test.go

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"os"
2525
"reflect"
26+
"runtime"
2627
"sort"
2728
"strings"
2829
"testing"
@@ -1508,6 +1509,10 @@ func TestIsSupportedContainerNamePrefix(t *testing.T) {
15081509
}
15091510

15101511
func TestChmodIfPermissionMismatch(t *testing.T) {
1512+
if runtime.GOOS == "windows" {
1513+
t.Skip("Skipping test on Windows")
1514+
}
1515+
15111516
permissionMatchingPath, _ := getWorkDirPath("permissionMatchingPath")
15121517
_ = makeDir(permissionMatchingPath)
15131518
defer os.RemoveAll(permissionMatchingPath)
@@ -1516,11 +1521,23 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
15161521
_ = os.MkdirAll(permissionMismatchPath, os.FileMode(0721))
15171522
defer os.RemoveAll(permissionMismatchPath)
15181523

1524+
permissionMatchGidMismatchPath, _ := getWorkDirPath("permissionMatchGidMismatchPath")
1525+
_ = os.MkdirAll(permissionMatchGidMismatchPath, os.FileMode(0755))
1526+
_ = os.Chmod(permissionMatchGidMismatchPath, 0755|os.ModeSetgid) // Setgid bit is set
1527+
defer os.RemoveAll(permissionMatchGidMismatchPath)
1528+
1529+
permissionMismatchGidMismatch, _ := getWorkDirPath("permissionMismatchGidMismatch")
1530+
_ = os.MkdirAll(permissionMismatchGidMismatch, os.FileMode(0721))
1531+
_ = os.Chmod(permissionMismatchGidMismatch, 0721|os.ModeSetgid) // Setgid bit is set
1532+
defer os.RemoveAll(permissionMismatchGidMismatch)
1533+
15191534
tests := []struct {
1520-
desc string
1521-
path string
1522-
mode os.FileMode
1523-
expectedError error
1535+
desc string
1536+
path string
1537+
mode os.FileMode
1538+
expectedPerms os.FileMode
1539+
expectedGidBit bool
1540+
expectedError error
15241541
}{
15251542
{
15261543
desc: "Invalid path",
@@ -1529,16 +1546,52 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
15291546
expectedError: fmt.Errorf("CreateFile invalid-path: The system cannot find the file specified"),
15301547
},
15311548
{
1532-
desc: "permission matching path",
1533-
path: permissionMatchingPath,
1534-
mode: 0755,
1535-
expectedError: nil,
1549+
desc: "permission matching path",
1550+
path: permissionMatchingPath,
1551+
mode: 0755,
1552+
expectedPerms: 0755,
1553+
expectedGidBit: false,
1554+
expectedError: nil,
15361555
},
15371556
{
1538-
desc: "permission mismatch path",
1539-
path: permissionMismatchPath,
1540-
mode: 0755,
1541-
expectedError: nil,
1557+
desc: "permission mismatch path",
1558+
path: permissionMismatchPath,
1559+
mode: 0755,
1560+
expectedPerms: 0755,
1561+
expectedGidBit: false,
1562+
expectedError: nil,
1563+
},
1564+
{
1565+
desc: "permission mismatch path",
1566+
path: permissionMismatchPath,
1567+
mode: 0755,
1568+
expectedPerms: 0755,
1569+
expectedGidBit: false,
1570+
expectedError: nil,
1571+
},
1572+
{
1573+
desc: "only match the permission mode bits",
1574+
path: permissionMatchGidMismatchPath,
1575+
mode: 0755,
1576+
expectedPerms: 0755,
1577+
expectedGidBit: true,
1578+
expectedError: nil,
1579+
},
1580+
{
1581+
desc: "only change the permission mode bits when gid is set",
1582+
path: permissionMismatchGidMismatch,
1583+
mode: 0755,
1584+
expectedPerms: 0755,
1585+
expectedGidBit: true,
1586+
expectedError: nil,
1587+
},
1588+
{
1589+
desc: "only change the permission mode bits when gid is not set but mode bits have gid set",
1590+
path: permissionMismatchPath,
1591+
mode: 02755,
1592+
expectedPerms: 0755,
1593+
expectedGidBit: false,
1594+
expectedError: nil,
15421595
},
15431596
}
15441597

@@ -1549,7 +1602,19 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
15491602
t.Errorf("test[%s]: unexpected error: %v, expected error: %v", test.desc, err, test.expectedError)
15501603
}
15511604
}
1605+
1606+
if test.expectedError == nil {
1607+
info, _ := os.Lstat(test.path)
1608+
if test.expectedError == nil && (info.Mode()&os.ModePerm != test.expectedPerms) {
1609+
t.Errorf("test[%s]: unexpected perms: %v, expected perms: %v, ", test.desc, info.Mode()&os.ModePerm, test.expectedPerms)
1610+
}
1611+
1612+
if (info.Mode()&os.ModeSetgid != 0) != test.expectedGidBit {
1613+
t.Errorf("test[%s]: unexpected gid bit: %v, expected gid bit: %v", test.desc, info.Mode()&os.ModeSetgid != 0, test.expectedGidBit)
1614+
}
1615+
}
15521616
}
1617+
15531618
}
15541619

15551620
// getWorkDirPath returns the path to the current working directory

0 commit comments

Comments
 (0)