From 692fab093660192ec9e76eef0142f711ea6c853d Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 4 Jan 2021 18:53:30 -0800 Subject: [PATCH] libct/checkProcMounts: optimize Commit 9c1242ecb ("Add white list for bind mount chec", Jan 6 2016) added a set of entries under /proc which we allow to be mounted to, for the benefit of lxcfs-like fuse-backed hack to have container's own version of /proc/meminfo etc. For some reason, the allow list check is performed at the very beginning of the function, which is not optimal. Move the check to the end -- at this point in the code we already know we're under /proc, so it make sense to consult the allow list. This makes the code slightly more logical and hopefully slightly faster. Signed-off-by: Kir Kolyshkin --- libcontainer/rootfs_linux.go | 47 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/libcontainer/rootfs_linux.go b/libcontainer/rootfs_linux.go index f9902e85b..e4792da1b 100644 --- a/libcontainer/rootfs_linux.go +++ b/libcontainer/rootfs_linux.go @@ -482,29 +482,6 @@ func getCgroupMounts(m *configs.Mount) ([]*configs.Mount, error) { // if source is nil, don't stat the filesystem. This is used for restore of a checkpoint. func checkProcMount(rootfs, dest, source string) error { const procPath = "/proc" - // White list, it should be sub directories of invalid destinations - validDestinations := []string{ - // These entries can be bind mounted by files emulated by fuse, - // so commands like top, free displays stats in container. - "/proc/cpuinfo", - "/proc/diskstats", - "/proc/meminfo", - "/proc/stat", - "/proc/swaps", - "/proc/uptime", - "/proc/loadavg", - "/proc/slabinfo", - "/proc/net/dev", - } - for _, valid := range validDestinations { - path, err := filepath.Rel(filepath.Join(rootfs, valid), dest) - if err != nil { - return err - } - if path == "." { - return nil - } - } path, err := filepath.Rel(filepath.Join(rootfs, procPath), dest) if err != nil { return err @@ -530,6 +507,30 @@ func checkProcMount(rootfs, dest, source string) error { } return fmt.Errorf("%q cannot be mounted because it is not of type proc", dest) } + + // Here dest is definitely under /proc. Do not allow those, + // except for a few specific entries emulated by lxcfs. + validProcMounts := []string{ + "/proc/cpuinfo", + "/proc/diskstats", + "/proc/meminfo", + "/proc/stat", + "/proc/swaps", + "/proc/uptime", + "/proc/loadavg", + "/proc/slabinfo", + "/proc/net/dev", + } + for _, valid := range validProcMounts { + path, err := filepath.Rel(filepath.Join(rootfs, valid), dest) + if err != nil { + return err + } + if path == "." { + return nil + } + } + return fmt.Errorf("%q cannot be mounted because it is inside /proc", dest) }