Skip to content

Commit 92009b8

Browse files
authored
Clears bits for mount flags incompatible with remount, Closes #6145
See #6145 See https://bugs.launchpad.net/apparmor/+bug/2091424 Signed-off-by: ltsai-aig <ltsai@answersingenesis.org>
1 parent 2f0160e commit 92009b8

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

chroot/run_linux.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ var (
8888
}
8989
)
9090

91+
var makeFlags uintptr = uintptr(unix.MS_UNBINDABLE | unix.MS_PRIVATE | unix.MS_SLAVE | unix.MS_SHARED | unix.MS_REC)
92+
9193
func mountFlagNames(flags uintptr) []string {
9294
var names []string
9395
for flag, name := range mountFlagMap {
@@ -385,7 +387,7 @@ func makeReadOnly(mntpoint string, flags uintptr) error {
385387
if fs.Flags&unix.ST_RDONLY == 0 {
386388
// All callers currently pass MS_RDONLY in "flags", but in case they stop doing
387389
// that at some point in the future...
388-
if err := unix.Mount(mntpoint, mntpoint, "bind", flags|unix.MS_RDONLY|unix.MS_REMOUNT|unix.MS_BIND, ""); err != nil {
390+
if err := unix.Mount(mntpoint, mntpoint, "bind", (flags|unix.MS_RDONLY|unix.MS_REMOUNT|unix.MS_BIND)&^makeFlags, ""); err != nil {
389391
return fmt.Errorf("remounting %s in mount namespace read-only: %w", mntpoint, err)
390392
}
391393
}
@@ -440,7 +442,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
440442
return undoBinds, fmt.Errorf("checking if directory %q was bound read-only: %w", subDev, err)
441443
}
442444
if fs.Flags&unix.ST_RDONLY == 0 {
443-
if err := unix.Mount(subDev, subDev, "bind", devFlags|unix.MS_REMOUNT|unix.MS_BIND, ""); err != nil {
445+
if err := unix.Mount(subDev, subDev, "bind", (devFlags|unix.MS_REMOUNT|unix.MS_BIND)&^makeFlags, ""); err != nil {
444446
return undoBinds, fmt.Errorf("remounting /dev in mount namespace read-only: %w", err)
445447
}
446448
}
@@ -642,7 +644,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
642644
if effectiveImportantFlags != expectedImportantFlags {
643645
// Do a remount to try to get the desired flags to stick.
644646
effectiveUnimportantFlags := uintptr(fs.Flags) & ^possibleImportantFlags
645-
remountFlags := unix.MS_REMOUNT | bindFlags | requestFlags | mountFlagsForFSFlags(effectiveUnimportantFlags)
647+
remountFlags := (unix.MS_REMOUNT | bindFlags | requestFlags | mountFlagsForFSFlags(effectiveUnimportantFlags)) &^ makeFlags
646648
// If we are requesting a read-only mount, add any possibleImportantFlags present in fs.Flags to remountFlags.
647649
if requestFlags&unix.ST_RDONLY == unix.ST_RDONLY {
648650
remountFlags |= uintptr(fs.Flags) & possibleImportantFlags
@@ -701,7 +703,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
701703
return undoBinds, fmt.Errorf("checking if directory %q was bound read-only: %w", target, err)
702704
}
703705
if fs.Flags&unix.ST_RDONLY == 0 {
704-
if err := unix.Mount(target, target, "", unix.MS_REMOUNT|unix.MS_RDONLY|bindFlags|mountFlagsForFSFlags(uintptr(fs.Flags)), ""); err != nil {
706+
if err := unix.Mount(target, target, "", (unix.MS_REMOUNT|unix.MS_RDONLY|bindFlags|mountFlagsForFSFlags(uintptr(fs.Flags)))&^makeFlags, ""); err != nil {
705707
return undoBinds, fmt.Errorf("remounting %q in mount namespace read-only: %w", target, err)
706708
}
707709
}
@@ -814,7 +816,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
814816
return undoBinds, fmt.Errorf("checking if masked directory %q was mounted read-only in mount namespace: %w", target, err)
815817
}
816818
if fs.Flags&unix.ST_RDONLY == 0 {
817-
if err = unix.Mount(target, target, "", syscall.MS_REMOUNT|roFlags|mountFlagsForFSFlags(uintptr(fs.Flags)), ""); err != nil {
819+
if err = unix.Mount(target, target, "", (syscall.MS_REMOUNT|roFlags|mountFlagsForFSFlags(uintptr(fs.Flags)))&^makeFlags, ""); err != nil {
818820
return undoBinds, fmt.Errorf("making sure masked directory %q in mount namespace is read only: %w", target, err)
819821
}
820822
}

0 commit comments

Comments
 (0)