|
| 1 | +package safepath |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/containerd/log" |
| 11 | + "github.com/moby/sys/mount" |
| 12 | + "github.com/pkg/errors" |
| 13 | + "golang.org/x/sys/unix" |
| 14 | +) |
| 15 | + |
| 16 | +// Join makes sure that the concatenation of path and subpath doesn't |
| 17 | +// resolve to a path outside of path and returns a path to a temporary file that is |
| 18 | +// a bind mount to the the exact same file/directory that was validated. |
| 19 | +// |
| 20 | +// After use, it is the caller's responsibility to call Close on the returned |
| 21 | +// SafePath object, which will unmount the temporary file/directory |
| 22 | +// and remove it. |
| 23 | +func Join(path, subpath string) (*SafePath, error) { |
| 24 | + base, subpart, err := evaluatePath(path, subpath) |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + |
| 29 | + runtime.LockOSThread() |
| 30 | + defer runtime.UnlockOSThread() |
| 31 | + fd, err := safeOpenFd(base, subpart) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + defer unix.Close(fd) |
| 37 | + |
| 38 | + tmpMount, err := tempMountPoint(fd) |
| 39 | + if err != nil { |
| 40 | + return nil, errors.Wrap(err, "failed to create temporary file for safe mount") |
| 41 | + } |
| 42 | + |
| 43 | + pid := strconv.Itoa(unix.Gettid()) |
| 44 | + // Using explicit pid path, because /proc/self/fd/<fd> fails with EACCES |
| 45 | + // when running under "Enhanced Container Isolation" in Docker Desktop |
| 46 | + // which uses sysbox runtime under the hood. |
| 47 | + // TODO(vvoland): Investigate. |
| 48 | + mountSource := "/proc/" + pid + "/fd/" + strconv.Itoa(fd) |
| 49 | + |
| 50 | + if err := unix.Mount(mountSource, tmpMount, "none", unix.MS_BIND, ""); err != nil { |
| 51 | + os.Remove(tmpMount) |
| 52 | + return nil, errors.Wrap(err, "failed to mount resolved path") |
| 53 | + } |
| 54 | + |
| 55 | + return &SafePath{ |
| 56 | + path: tmpMount, |
| 57 | + sourceBase: base, |
| 58 | + sourceSubpath: subpart, |
| 59 | + cleanup: cleanupSafePath(tmpMount), |
| 60 | + }, nil |
| 61 | +} |
| 62 | + |
| 63 | +// safeOpenFd opens the file at filepath.Join(path, subpath) in O_PATH |
| 64 | +// mode and returns the file descriptor if subpath is within the subtree |
| 65 | +// rooted at path. It is an error if any of components of path or subpath |
| 66 | +// are symbolic links. |
| 67 | +// |
| 68 | +// It is a caller's responsibility to close the returned file descriptor, if no |
| 69 | +// error was returned. |
| 70 | +func safeOpenFd(path, subpath string) (int, error) { |
| 71 | + // Open base volume path (_data directory). |
| 72 | + prevFd, err := unix.Open(path, unix.O_PATH|unix.O_DIRECTORY|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0) |
| 73 | + if err != nil { |
| 74 | + return -1, &ErrNotAccessible{Path: path, Cause: err} |
| 75 | + } |
| 76 | + defer unix.Close(prevFd) |
| 77 | + |
| 78 | + // Try to use the Openat2 syscall first (available on Linux 5.6+). |
| 79 | + fd, err := unix.Openat2(prevFd, subpath, &unix.OpenHow{ |
| 80 | + Flags: unix.O_PATH | unix.O_CLOEXEC, |
| 81 | + Mode: 0, |
| 82 | + Resolve: unix.RESOLVE_BENEATH | unix.RESOLVE_NO_MAGICLINKS | unix.RESOLVE_NO_SYMLINKS, |
| 83 | + }) |
| 84 | + |
| 85 | + switch { |
| 86 | + case errors.Is(err, unix.ENOSYS): |
| 87 | + // Openat2 is not available, fallback to Openat loop. |
| 88 | + return softOpenat2(prevFd, subpath) |
| 89 | + case errors.Is(err, unix.EXDEV): |
| 90 | + return -1, &ErrEscapesBase{Base: path, Subpath: subpath} |
| 91 | + case errors.Is(err, unix.ENOENT), errors.Is(err, unix.ELOOP): |
| 92 | + return -1, &ErrNotAccessible{Path: filepath.Join(path, subpath), Cause: err} |
| 93 | + case err != nil: |
| 94 | + return -1, &os.PathError{Op: "openat2", Path: subpath, Err: err} |
| 95 | + } |
| 96 | + |
| 97 | + // Openat2 is available and succeeded. |
| 98 | + return fd, nil |
| 99 | +} |
| 100 | + |
| 101 | +func softOpenat2(baseFd int, subpath string) (int, error) { |
| 102 | + return -1, errors.New("temporary stub, will be removed in later commit") |
| 103 | +} |
| 104 | + |
| 105 | +// tempMountPoint creates a temporary file/directory to act as mount |
| 106 | +// point for the file descriptor. |
| 107 | +func tempMountPoint(sourceFd int) (string, error) { |
| 108 | + var stat unix.Stat_t |
| 109 | + err := unix.Fstat(sourceFd, &stat) |
| 110 | + if err != nil { |
| 111 | + return "", errors.Wrap(err, "failed to Fstat mount source fd") |
| 112 | + } |
| 113 | + |
| 114 | + isDir := (stat.Mode & unix.S_IFMT) == unix.S_IFDIR |
| 115 | + if isDir { |
| 116 | + return os.MkdirTemp("", "safe-mount") |
| 117 | + } |
| 118 | + |
| 119 | + f, err := os.CreateTemp("", "safe-mount") |
| 120 | + if err != nil { |
| 121 | + return "", err |
| 122 | + } |
| 123 | + |
| 124 | + p := f.Name() |
| 125 | + if err := f.Close(); err != nil { |
| 126 | + return "", err |
| 127 | + } |
| 128 | + return p, nil |
| 129 | +} |
| 130 | + |
| 131 | +// cleanupSafePaths returns a function that unmounts the path and removes the |
| 132 | +// mountpoint. |
| 133 | +func cleanupSafePath(path string) func() error { |
| 134 | + return func() error { |
| 135 | + log.G(context.TODO()).WithField("path", path).Debug("removing safe temp mount") |
| 136 | + |
| 137 | + if err := unix.Unmount(path, unix.MNT_DETACH); err != nil { |
| 138 | + if errors.Is(err, unix.EINVAL) { |
| 139 | + log.G(context.TODO()).WithField("path", path).Warn("safe temp mount no longer exists?") |
| 140 | + return nil |
| 141 | + } |
| 142 | + return errors.Wrapf(err, "error unmounting safe mount %s", path) |
| 143 | + } |
| 144 | + if err := os.Remove(path); err != nil { |
| 145 | + if errors.Is(err, os.ErrNotExist) { |
| 146 | + log.G(context.TODO()).WithField("path", path).Warn("safe temp mount no longer exists?") |
| 147 | + return nil |
| 148 | + } |
| 149 | + return errors.Wrapf(err, "failed to delete temporary safe mount") |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | + } |
| 154 | +} |
0 commit comments