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