Skip to content

Commit 12f6a99

Browse files
committed
merge branch 'pr-1962'
rootfs: umount all procfs and sysfs with --no-pivot LGTMs: @mrunalp @cyphar Closes #1962
2 parents bbb17ef + 28a697c commit 12f6a99

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

libcontainer/rootfs_linux.go

+35
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,41 @@ func pivotRoot(rootfs string) error {
748748
}
749749

750750
func msMoveRoot(rootfs string) error {
751+
mountinfos, err := mount.GetMounts()
752+
if err != nil {
753+
return err
754+
}
755+
756+
absRootfs, err := filepath.Abs(rootfs)
757+
if err != nil {
758+
return err
759+
}
760+
761+
for _, info := range mountinfos {
762+
p, err := filepath.Abs(info.Mountpoint)
763+
if err != nil {
764+
return err
765+
}
766+
// Umount every syfs and proc file systems, except those under the container rootfs
767+
if (info.Fstype != "proc" && info.Fstype != "sysfs") || filepath.HasPrefix(p, absRootfs) {
768+
continue
769+
}
770+
// Be sure umount events are not propagated to the host.
771+
if err := unix.Mount("", p, "", unix.MS_SLAVE|unix.MS_REC, ""); err != nil {
772+
return err
773+
}
774+
if err := unix.Unmount(p, unix.MNT_DETACH); err != nil {
775+
if err != unix.EINVAL && err != unix.EPERM {
776+
return err
777+
} else {
778+
// If we have not privileges for umounting (e.g. rootless), then
779+
// cover the path.
780+
if err := unix.Mount("tmpfs", p, "tmpfs", 0, ""); err != nil {
781+
return err
782+
}
783+
}
784+
}
785+
}
751786
if err := unix.Mount(rootfs, "/", "", unix.MS_MOVE, ""); err != nil {
752787
return err
753788
}

0 commit comments

Comments
 (0)