|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +// Copyright (C) 2024-2025 SUSE LLC. All rights reserved. |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file. |
| 6 | + |
| 7 | +// This code is adapted to be a minimal version of the libpathrs proc resolver |
| 8 | +// <https://github.com/opensuse/libpathrs/blob/v0.1.3/src/resolvers/procfs.rs>. |
| 9 | +// As we only need O_PATH|O_NOFOLLOW support, this is not too much to port. |
| 10 | + |
| 11 | +package securejoin |
| 12 | + |
| 13 | +import ( |
| 14 | + "fmt" |
| 15 | + "os" |
| 16 | + "path" |
| 17 | + "path/filepath" |
| 18 | + "strings" |
| 19 | + |
| 20 | + "golang.org/x/sys/unix" |
| 21 | +) |
| 22 | + |
| 23 | +// procfsLookupInRoot is a stripped down version of completeLookupInRoot, |
| 24 | +// entirely designed to support the very small set of features necessary to |
| 25 | +// make procfs handling work. Unlike completeLookupInRoot, we always have |
| 26 | +// O_PATH|O_NOFOLLOW behaviour for trailing symlinks. |
| 27 | +// |
| 28 | +// The main restrictions are: |
| 29 | +// |
| 30 | +// - ".." is not supported (as it requires either os.Root-style replays, |
| 31 | +// which is more bug-prone; or procfs verification, which is not possible |
| 32 | +// due to re-entrancy issues). |
| 33 | +// - Absolute symlinks for the same reason (and all absolute symlinks in |
| 34 | +// procfs are magic-links, which we want to skip anyway). |
| 35 | +// - If statx is supported (checkSymlinkOvermount), any mount-point crossings |
| 36 | +// (which is the main attack of concern against /proc). |
| 37 | +// - Partial lookups are not supported, so the symlink stack is not needed. |
| 38 | +// - Trailing slash special handling is not necessary in most cases (if we |
| 39 | +// operating on procfs, it's usually with programmer-controlled strings |
| 40 | +// that will then be re-opened), so we skip it since whatever re-opens it |
| 41 | +// can deal with it. It's a creature comfort anyway. |
| 42 | +// |
| 43 | +// If the system supports openat2(), this is implemented using equivalent flags |
| 44 | +// (RESOLVE_BENEATH | RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS). |
| 45 | +func procfsLookupInRoot(procRoot *os.File, unsafePath string) (Handle *os.File, _ error) { |
| 46 | + unsafePath = filepath.ToSlash(unsafePath) // noop |
| 47 | + |
| 48 | + // Make sure that an empty unsafe path still returns something sane, even |
| 49 | + // with openat2 (which doesn't have AT_EMPTY_PATH semantics yet). |
| 50 | + if unsafePath == "" { |
| 51 | + unsafePath = "." |
| 52 | + } |
| 53 | + |
| 54 | + // This is already checked by getProcRoot, but make sure here since the |
| 55 | + // core security of this lookup is based on this assumption. |
| 56 | + if err := verifyProcRoot(procRoot); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + if hasOpenat2() { |
| 61 | + // We prefer being able to use RESOLVE_NO_XDEV if we can, to be |
| 62 | + // absolutely sure we are operating on a clean /proc handle that |
| 63 | + // doesn't have any cheeky overmounts that could trick us (including |
| 64 | + // symlink mounts on top of /proc/thread-self). RESOLVE_BENEATH isn't |
| 65 | + // strictly needed, but just use it since we have it. |
| 66 | + // |
| 67 | + // NOTE: /proc/self is technically a magic-link (the contents of the |
| 68 | + // symlink are generated dynamically), but it doesn't use |
| 69 | + // nd_jump_link() so RESOLVE_NO_MAGICLINKS allows it. |
| 70 | + // |
| 71 | + // NOTE: We MUST NOT use RESOLVE_IN_ROOT here, as openat2File uses |
| 72 | + // procSelfFdReadlink to clean up the returned f.Name() if we use |
| 73 | + // RESOLVE_IN_ROOT (which would lead to an infinite recursion). |
| 74 | + // |
| 75 | + // TODO: It would be nice to have RESOLVE_NO_DOTDOT, purely for |
| 76 | + // self-consistency with the backup O_PATH resolver. |
| 77 | + handle, err := openat2File(procRoot, unsafePath, &unix.OpenHow{ |
| 78 | + Flags: unix.O_PATH | unix.O_NOFOLLOW | unix.O_CLOEXEC, |
| 79 | + Resolve: unix.RESOLVE_BENEATH | unix.RESOLVE_NO_XDEV | unix.RESOLVE_NO_MAGICLINKS, |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + // TODO: Once we bump the minimum Go version to 1.20, we can use |
| 83 | + // multiple %w verbs for this wrapping. For now we need to use a |
| 84 | + // compatibility shim for older Go versions. |
| 85 | + // err = fmt.Errorf("%w: %w", errUnsafeProcfs, err) |
| 86 | + return nil, wrapBaseError(err, errUnsafeProcfs) |
| 87 | + } |
| 88 | + return handle, nil |
| 89 | + } |
| 90 | + |
| 91 | + // To mirror openat2(RESOLVE_BENEATH), we need to return an error if the |
| 92 | + // path is absolute. |
| 93 | + if path.IsAbs(unsafePath) { |
| 94 | + return nil, fmt.Errorf("%w: cannot resolve absolute paths in procfs resolver", errPossibleBreakout) |
| 95 | + } |
| 96 | + |
| 97 | + currentDir, err := dupFile(procRoot) |
| 98 | + if err != nil { |
| 99 | + return nil, fmt.Errorf("clone root fd: %w", err) |
| 100 | + } |
| 101 | + defer func() { |
| 102 | + // If a handle is not returned, close the internal handle. |
| 103 | + if Handle == nil { |
| 104 | + _ = currentDir.Close() |
| 105 | + } |
| 106 | + }() |
| 107 | + |
| 108 | + var ( |
| 109 | + linksWalked int |
| 110 | + currentPath string |
| 111 | + remainingPath = unsafePath |
| 112 | + ) |
| 113 | + for remainingPath != "" { |
| 114 | + // Get the next path component. |
| 115 | + var part string |
| 116 | + if i := strings.IndexByte(remainingPath, '/'); i == -1 { |
| 117 | + part, remainingPath = remainingPath, "" |
| 118 | + } else { |
| 119 | + part, remainingPath = remainingPath[:i], remainingPath[i+1:] |
| 120 | + } |
| 121 | + if part == "" { |
| 122 | + // no-op component, but treat it the same as "." |
| 123 | + part = "." |
| 124 | + } |
| 125 | + if part == ".." { |
| 126 | + // not permitted |
| 127 | + return nil, fmt.Errorf("%w: cannot walk into '..' in procfs resolver", errPossibleBreakout) |
| 128 | + } |
| 129 | + |
| 130 | + // Apply the component lexically to the path we are building. |
| 131 | + // currentPath does not contain any symlinks, and we are lexically |
| 132 | + // dealing with a single component, so it's okay to do a filepath.Clean |
| 133 | + // here. (Not to mention that ".." isn't allowed.) |
| 134 | + nextPath := path.Join("/", currentPath, part) |
| 135 | + // If we logically hit the root, just clone the root rather than |
| 136 | + // opening the part and doing all of the other checks. |
| 137 | + if nextPath == "/" { |
| 138 | + // Jump to root. |
| 139 | + rootClone, err := dupFile(procRoot) |
| 140 | + if err != nil { |
| 141 | + return nil, fmt.Errorf("clone root fd: %w", err) |
| 142 | + } |
| 143 | + _ = currentDir.Close() |
| 144 | + currentDir = rootClone |
| 145 | + currentPath = nextPath |
| 146 | + continue |
| 147 | + } |
| 148 | + |
| 149 | + // Try to open the next component. |
| 150 | + nextDir, err := openatFile(currentDir, part, unix.O_PATH|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + // Make sure we are still on procfs and haven't crossed mounts. |
| 156 | + if err := verifyProcHandle(nextDir); err != nil { |
| 157 | + _ = nextDir.Close() |
| 158 | + return nil, fmt.Errorf("check %q component is on procfs: %w", part, err) |
| 159 | + } |
| 160 | + if err := checkSubpathOvermount(procRoot, nextDir, ""); err != nil { |
| 161 | + _ = nextDir.Close() |
| 162 | + return nil, fmt.Errorf("check %q component is not overmounted: %w", part, err) |
| 163 | + } |
| 164 | + |
| 165 | + // We are emulating O_PATH|O_NOFOLLOW, so we only need to traverse into |
| 166 | + // trailing symlinks if we are not the final component. Otherwise we |
| 167 | + // can just return the currentDir. |
| 168 | + if remainingPath != "" { |
| 169 | + st, err := nextDir.Stat() |
| 170 | + if err != nil { |
| 171 | + _ = nextDir.Close() |
| 172 | + return nil, fmt.Errorf("stat component %q: %w", part, err) |
| 173 | + } |
| 174 | + |
| 175 | + if st.Mode()&os.ModeType == os.ModeSymlink { |
| 176 | + // readlinkat implies AT_EMPTY_PATH since Linux 2.6.39. See |
| 177 | + // Linux commit 65cfc6722361 ("readlinkat(), fchownat() and |
| 178 | + // fstatat() with empty relative pathnames"). |
| 179 | + linkDest, err := readlinkatFile(nextDir, "") |
| 180 | + // We don't need the handle anymore. |
| 181 | + _ = nextDir.Close() |
| 182 | + if err != nil { |
| 183 | + return nil, err |
| 184 | + } |
| 185 | + |
| 186 | + linksWalked++ |
| 187 | + if linksWalked > maxSymlinkLimit { |
| 188 | + return nil, &os.PathError{Op: "securejoin.procfsLookupInRoot", Path: "/proc/" + unsafePath, Err: unix.ELOOP} |
| 189 | + } |
| 190 | + |
| 191 | + // Update our logical remaining path. |
| 192 | + remainingPath = linkDest + "/" + remainingPath |
| 193 | + // Absolute symlinks are probably magiclinks, we reject them. |
| 194 | + if path.IsAbs(linkDest) { |
| 195 | + return nil, fmt.Errorf("%w: cannot jump to / in procfs resolver -- possible magiclink", errPossibleBreakout) |
| 196 | + } |
| 197 | + continue |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + // Walk into the next component. |
| 202 | + _ = currentDir.Close() |
| 203 | + currentDir = nextDir |
| 204 | + currentPath = nextPath |
| 205 | + } |
| 206 | + |
| 207 | + // One final sanity-check. |
| 208 | + if err := verifyProcHandle(currentDir); err != nil { |
| 209 | + return nil, fmt.Errorf("check final handle is on procfs: %w", err) |
| 210 | + } |
| 211 | + if err := checkSubpathOvermount(procRoot, currentDir, ""); err != nil { |
| 212 | + return nil, fmt.Errorf("check final handle is not overmounted: %w", err) |
| 213 | + } |
| 214 | + return currentDir, nil |
| 215 | +} |
0 commit comments