Skip to content

Commit

Permalink
Merge pull request #9054 from macOScontainers/canonicalize-filter-mou…
Browse files Browse the repository at this point in the history
…nt-path

Fix usages of `mountinfo.PrefixFilter`
  • Loading branch information
AkihiroSuda committed Sep 26, 2023
2 parents bcd658c + d94a789 commit 9ffb34a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 12 deletions.
7 changes: 2 additions & 5 deletions mount/lookup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ package mount

import (
"fmt"
"path/filepath"

"github.com/moby/sys/mountinfo"
)

// Lookup returns the mount info corresponds to the path.
func Lookup(dir string) (Info, error) {
dir = filepath.Clean(dir)

resolvedDir, err := filepath.EvalSymlinks(dir)
resolvedDir, err := CanonicalizePath(dir)
if err != nil {
return Info{}, fmt.Errorf("failed to resolve symlink for %q: %w", dir, err)
return Info{}, err
}

m, err := mountinfo.GetMounts(mountinfo.ParentsFilter(resolvedDir))
Expand Down
13 changes: 13 additions & 0 deletions mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package mount

import (
"fmt"
"path/filepath"
"strings"

"github.com/containerd/containerd/api/types"
Expand Down Expand Up @@ -69,6 +70,18 @@ func UnmountMounts(mounts []Mount, target string, flags int) error {
return nil
}

// CanonicalizePath makes path absolute and resolves symlinks in it.
// Path must exist.
func CanonicalizePath(path string) (string, error) {
// Abs also does Clean, so we do not need to call it separately
path, err := filepath.Abs(path)
if err != nil {
return "", err
}

return filepath.EvalSymlinks(path)
}

// ReadOnly returns a boolean value indicating whether this mount has the "ro"
// option set.
func (m *Mount) ReadOnly() bool {
Expand Down
6 changes: 6 additions & 0 deletions mount/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func UnmountRecursive(target string, flags int) error {
if target == "" {
return nil
}

target, err := CanonicalizePath(target)
if err != nil {
return err
}

mounts, err := mountinfo.GetMounts(mountinfo.PrefixFilter(target))
if err != nil {
return err
Expand Down
12 changes: 5 additions & 7 deletions mount/temp_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@ package mount

import (
"os"
"path/filepath"
"sort"

"github.com/moby/sys/mountinfo"
)

// 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
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pkg/cri/sbserver/helpers_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func openLogFile(path string) (*os.File, error) {
// unmountRecursive unmounts the target and all mounts underneath, starting with
// the deepest mount first.
func unmountRecursive(ctx context.Context, target string) error {
target, err := mount.CanonicalizePath(target)
if err != nil {
return err
}

toUnmount, err := mountinfo.GetMounts(mountinfo.PrefixFilter(target))
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/cri/sbserver/podsandbox/helpers_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func (c *Controller) seccompEnabled() bool {
// unmountRecursive unmounts the target and all mounts underneath, starting with
// the deepest mount first.
func unmountRecursive(ctx context.Context, target string) error {
target, err := mount.CanonicalizePath(target)
if err != nil {
return err
}

toUnmount, err := mountinfo.GetMounts(mountinfo.PrefixFilter(target))
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/cri/server/helpers_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ func openLogFile(path string) (*os.File, error) {
// unmountRecursive unmounts the target and all mounts underneath, starting with
// the deepest mount first.
func unmountRecursive(ctx context.Context, target string) error {
target, err := mount.CanonicalizePath(target)
if err != nil {
return err
}

toUnmount, err := mountinfo.GetMounts(mountinfo.PrefixFilter(target))
if err != nil {
return err
Expand Down

0 comments on commit 9ffb34a

Please sign in to comment.