b58745
From 28a697cce3e4f905dca700eda81d681a30eef9cd Mon Sep 17 00:00:00 2001
b58745
From: Giuseppe Scrivano <gscrivan@redhat.com>
b58745
Date: Fri, 11 Jan 2019 21:53:45 +0100
b58745
Subject: [PATCH] rootfs: umount all procfs and sysfs with --no-pivot
b58745
b58745
When creating a new user namespace, the kernel doesn't allow to mount
b58745
a new procfs or sysfs file system if there is not already one instance
b58745
fully visible in the current mount namespace.
b58745
b58745
When using --no-pivot we were effectively inhibiting this protection
b58745
from the kernel, as /proc and /sys from the host are still present in
b58745
the container mount namespace.
b58745
b58745
A container without full access to /proc could then create a new user
b58745
namespace, and from there able to mount a fully visible /proc, bypassing
b58745
the limitations in the container.
b58745
b58745
A simple reproducer for this issue is:
b58745
b58745
unshare -mrfp sh -c "mount -t proc none /proc && echo c > /proc/sysrq-trigger"
b58745
b58745
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
b58745
---
b58745
 libcontainer/rootfs_linux.go | 35 +++++++++++++++++++++++++++++++++++
b58745
 1 file changed, 35 insertions(+)
b58745
b58745
diff --git a/libcontainer/rootfs_linux.go b/libcontainer/rootfs_linux.go
b58745
index e7c2f8ada..6bd6da74a 100644
b58745
--- a/libcontainer/rootfs_linux.go
b58745
+++ b/libcontainer/rootfs_linux.go
b58745
@@ -748,6 +748,41 @@ func pivotRoot(rootfs string) error {
b58745
 }
b58745
 
b58745
 func msMoveRoot(rootfs string) error {
b58745
+	mountinfos, err := mount.GetMounts()
b58745
+	if err != nil {
b58745
+		return err
b58745
+	}
b58745
+
b58745
+	absRootfs, err := filepath.Abs(rootfs)
b58745
+	if err != nil {
b58745
+		return err
b58745
+	}
b58745
+
b58745
+	for _, info := range mountinfos {
b58745
+		p, err := filepath.Abs(info.Mountpoint)
b58745
+		if err != nil {
b58745
+			return err
b58745
+		}
b58745
+		// Umount every syfs and proc file systems, except those under the container rootfs
b58745
+		if (info.Fstype != "proc" && info.Fstype != "sysfs") || filepath.HasPrefix(p, absRootfs) {
b58745
+			continue
b58745
+		}
b58745
+		// Be sure umount events are not propagated to the host.
b58745
+		if err := unix.Mount("", p, "", unix.MS_SLAVE|unix.MS_REC, ""); err != nil {
b58745
+			return err
b58745
+		}
b58745
+		if err := unix.Unmount(p, unix.MNT_DETACH); err != nil {
b58745
+			if err != unix.EINVAL && err != unix.EPERM {
b58745
+				return err
b58745
+			} else {
b58745
+				// If we have not privileges for umounting (e.g. rootless), then
b58745
+				// cover the path.
b58745
+				if err := unix.Mount("tmpfs", p, "tmpfs", 0, ""); err != nil {
b58745
+					return err
b58745
+				}
b58745
+			}
b58745
+		}
b58745
+	}
b58745
 	if err := unix.Mount(rootfs, "/", "", unix.MS_MOVE, ""); err != nil {
b58745
 		return err
b58745
 	}