From 2323c4c48d7f732289cbfb8a233df6c29befae41 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 30 Apr 2015 12:39:29 -0700 Subject: [PATCH] Use filepath.Rel for subdirectory comparison Signed-off-by: Michael Crosby --- rootfs_linux.go | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/rootfs_linux.go b/rootfs_linux.go index 89d8710a7..0cd60373c 100644 --- a/rootfs_linux.go +++ b/rootfs_linux.go @@ -218,32 +218,17 @@ func checkMountDestination(rootfs, dest string) error { "/sys", } for _, invalid := range invalidDestinations { - if dirIsChild(filepath.Join(rootfs, invalid), dest) { + path, err := filepath.Rel(filepath.Join(rootfs, invalid), dest) + if err != nil { + return err + } + if path == "." || !strings.HasPrefix(path, "..") { return fmt.Errorf("%q cannot be mounted because it is located inside %q", dest, invalid) } } return nil } -// dirIsChild compare the parts of the dir to check if it is located -// inside root. comparing the individual parts ensures that false positives -// are not found. -func dirIsChild(root, dir string) bool { - var ( - 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 - } - } - return true -} - func setupDevSymlinks(rootfs string) error { var links = [][2]string{ {"/proc/self/fd", "/dev/fd"},