From e3e7c47123cc146bf9b9a41b73b34aae2f7e2aea Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 30 Apr 2015 10:23:42 -0700 Subject: [PATCH] Prohibit bind mounts into / Signed-off-by: Michael Crosby --- rootfs_linux.go | 6 ++++++ rootfs_linux_test.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/rootfs_linux.go b/rootfs_linux.go index bbc59d190..e63a394fc 100644 --- a/rootfs_linux.go +++ b/rootfs_linux.go @@ -212,6 +212,9 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error { // top of /proc or /sys. // dest is required to be an abs path and have any symlinks resolved before calling this function. func checkMountDestination(rootfs, dest string) error { + if filepath.Clean(rootfs) == filepath.Clean(dest) { + return fmt.Errorf("mounting into / is prohibited") + } invalidDestinations := []string{ "/proc", "/sys", @@ -232,6 +235,9 @@ func dirIsChild(root, dir string) bool { rootParts = strings.Split(filepath.Clean(root), string(filepath.Separator)) dirParts = strings.Split(filepath.Clean(dir), string(filepath.Separator)) ) + if len(dirParts) < len(rootParts) { + return false + } for i, p := range rootParts { if p != dirParts[i] { return false diff --git a/rootfs_linux_test.go b/rootfs_linux_test.go index d3e0cf364..54df065cc 100644 --- a/rootfs_linux_test.go +++ b/rootfs_linux_test.go @@ -27,3 +27,11 @@ func TestCheckMountDestFalsePositive(t *testing.T) { t.Fatal(err) } } + +func TestCheckMountRoot(t *testing.T) { + dest := "/rootfs" + err := checkMountDestination("/rootfs", dest) + if err == nil { + t.Fatal(err) + } +}