Skip to content

Commit 46fabcc

Browse files
authored
Merge pull request #12944 from k8s-infra-cherrypick-robot/cherry-pick-12941-to-release/2.2
[release/2.2] core/mount: fix getUnprivilegedMountFlags iterating over indices instead of values
2 parents 842cbd0 + ef7a8be commit 46fabcc

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

core/mount/mount_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func getUnprivilegedMountFlags(path string) (int, error) {
231231
}
232232

233233
var flags int
234-
for flag := range unprivilegedFlags {
234+
for _, flag := range unprivilegedFlags {
235235
if int(statfs.Flags)&flag == flag {
236236
flags |= flag
237237
}

core/mount/mount_linux_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,50 @@ func TestDoPrepareIDMappedOverlay(t *testing.T) {
404404
}
405405
}
406406

407+
func TestGetUnprivilegedMountFlags(t *testing.T) {
408+
testutil.RequiresRoot(t)
409+
410+
td := t.TempDir()
411+
target := filepath.Join(td, "mnt")
412+
require.NoError(t, os.Mkdir(target, 0755))
413+
414+
// Mount a tmpfs with noexec,noatime,nodiratime -- these are the flags
415+
// that were previously missed due to iterating over slice indices
416+
// instead of values.
417+
require.NoError(t, unix.Mount("tmpfs", target, "tmpfs", unix.MS_NOEXEC|unix.MS_NOATIME|unix.MS_NODIRATIME, ""))
418+
defer unix.Unmount(target, unix.MNT_DETACH)
419+
420+
flags, err := getUnprivilegedMountFlags(target)
421+
require.NoError(t, err)
422+
423+
for _, tc := range []struct {
424+
flag int
425+
name string
426+
}{
427+
{unix.MS_NOEXEC, "MS_NOEXEC"},
428+
{unix.MS_NOATIME, "MS_NOATIME"},
429+
{unix.MS_NODIRATIME, "MS_NODIRATIME"},
430+
} {
431+
if flags&tc.flag != tc.flag {
432+
t.Errorf("expected %s (0x%x) to be set in flags 0x%x", tc.name, tc.flag, flags)
433+
}
434+
}
435+
436+
// MS_NOSUID and MS_NODEV should NOT be set since we didn't mount with them.
437+
for _, tc := range []struct {
438+
flag int
439+
name string
440+
}{
441+
{unix.MS_NOSUID, "MS_NOSUID"},
442+
{unix.MS_NODEV, "MS_NODEV"},
443+
{unix.MS_RDONLY, "MS_RDONLY"},
444+
} {
445+
if flags&tc.flag != 0 {
446+
t.Errorf("expected %s (0x%x) to NOT be set in flags 0x%x", tc.name, tc.flag, flags)
447+
}
448+
}
449+
}
450+
407451
func setupMounts(t *testing.T) (target string, mounts []Mount) {
408452
dir1 := t.TempDir()
409453
dir2 := t.TempDir()

0 commit comments

Comments
 (0)