Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 28a697c

Browse files
committed
rootfs: umount all procfs and sysfs with --no-pivot
When creating a new user namespace, the kernel doesn't allow to mount a new procfs or sysfs file system if there is not already one instance fully visible in the current mount namespace. When using --no-pivot we were effectively inhibiting this protection from the kernel, as /proc and /sys from the host are still present in the container mount namespace. A container without full access to /proc could then create a new user namespace, and from there able to mount a fully visible /proc, bypassing the limitations in the container. A simple reproducer for this issue is: unshare -mrfp sh -c "mount -t proc none /proc && echo c > /proc/sysrq-trigger" Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
1 parent bbb17ef commit 28a697c

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)