Fix usages of mountinfo.PrefixFilter

It says: The prefix path **must be absolute, have all symlinks resolved, and cleaned**. But those requirements are violated in lots of places.

What happens when it is given a non-canonicalized path is that `mountinfo.GetMounts` will not find mounts.

The trivial case is:
```
$ mkdir a && ln -s a b && mkdir b/c b/d && mount --bind b/c b/d && cat /proc/mounts | grep -- '[ab]/d'
/dev/sdd3 /home/user/a/d ext4 rw,noatime,discard 0 0
```
We asked to bind-mount b/c to b/d, but ended up with mount in a/d.
So, mount table always contains canonicalized mount points, and it is an error to look for non-canonicalized paths in it.

Signed-off-by: Marat Radchenko <marat@slonopotamus.org>
This commit is contained in:
Marat Radchenko
2023-09-04 12:39:47 +03:00
parent d015c99b2e
commit d94a789d15
7 changed files with 41 additions and 12 deletions

View File

@@ -20,7 +20,6 @@ package mount
import (
"os"
"path/filepath"
"sort"
"github.com/moby/sys/mountinfo"
@@ -28,15 +27,13 @@ import (
// SetTempMountLocation sets the temporary mount location
func SetTempMountLocation(root string) error {
root, err := filepath.Abs(root)
err := os.MkdirAll(root, 0700)
if err != nil {
return err
}
if err := os.MkdirAll(root, 0700); err != nil {
return err
}
tempMountLocation = root
return nil
// We need to pass canonicalized path to mountinfo.PrefixFilter in CleanupTempMounts
tempMountLocation, err = CanonicalizePath(root)
return err
}
// CleanupTempMounts all temp mounts and remove the directories
@@ -45,6 +42,7 @@ func CleanupTempMounts(flags int) (warnings []error, err error) {
if err != nil {
return nil, err
}
// Make the deepest mount be first
sort.Slice(mounts, func(i, j int) bool {
return len(mounts[i].Mountpoint) > len(mounts[j].Mountpoint)