| 1 | // Copyright 2012 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE.BSD file. |
| 4 | |
| 5 | // This code is a modified version of path/filepath/symlink.go from the Go |
| 6 | // standard library in [docker@fa3ec89], which was based on [go1.3.3], |
| 7 | // with Windows implementatinos being added in [docker@9b648df]. |
| 8 | // |
| 9 | // [docker@fa3ec89]: https://github.com/moby/moby/commit/fa3ec89515431ce425f924c8a9a804d5cb18382f |
| 10 | // [go1.3.3]: https://github.com/golang/go/blob/go1.3.3/src/pkg/path/filepath/symlink.go |
| 11 | // [docker@9b648df]: https://github.com/moby/moby/commit/9b648dfac6453de5944ee4bb749115d85a253a05 |
| 12 | |
| 13 | package symlink |
| 14 | |
| 15 | import ( |
| 16 | "bytes" |
| 17 | "errors" |
| 18 | "os" |
| 19 | "path/filepath" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | // FollowSymlinkInScope evaluates symbolic links in "path" within a scope "root" |
| 24 | // and returns a result guaranteed to be contained within the scope "root" at |
| 25 | // the time of the call. It returns an error of either "path" or "root" cannot |
| 26 | // be converted to an absolute path. |
| 27 | // |
| 28 | // Symbolic links in "root" are not evaluated and left as-is. Errors encountered |
| 29 | // while attempting to evaluate symlinks in path are returned, but non-existing |
| 30 | // paths are valid and do not constitute an error. "path" must contain "root" |
| 31 | // as a prefix, or else an error is returned. Trying to break out from "root" |
| 32 | // does not constitute an error, instead resolves the path within "root". |
| 33 | // |
| 34 | // Example: |
| 35 | // |
| 36 | // // If "/foo/bar" is a symbolic link to "/outside": |
| 37 | // FollowSymlinkInScope("/foo/bar", "/foo") // Returns "/foo/outside" instead of "/outside" |
| 38 | // |
| 39 | // IMPORTANT: It is the caller's responsibility to call FollowSymlinkInScope |
| 40 | // after relevant symbolic links are created to avoid Time-of-check Time-of-use |
| 41 | // (TOCTOU) race conditions ([CWE-367]). No additional symbolic links must be |
| 42 | // created after evaluating, as those could potentially make a previously-safe |
| 43 | // path unsafe. |
| 44 | // |
| 45 | // For example, if "/foo/bar" does not exist, FollowSymlinkInScope("/foo/bar", "/foo") |
| 46 | // evaluates the path to "/foo/bar". If one makes "/foo/bar" a symbolic link to |
| 47 | // "/baz" subsequently, then "/foo/bar" should no longer be considered safely |
| 48 | // contained in "/foo". |
| 49 | // |
| 50 | // [CWE-367]: https://cwe.mitre.org/data/definitions/367.html |
| 51 | func FollowSymlinkInScope(path, root string) (string, error) { |
| 52 | path, err := filepath.Abs(filepath.FromSlash(path)) |
| 53 | if err != nil { |
| 54 | return "", err |
| 55 | } |
| 56 | root, err = filepath.Abs(filepath.FromSlash(root)) |
| 57 | if err != nil { |
| 58 | return "", err |
| 59 | } |
| 60 | return evalSymlinksInScope(path, root) |
| 61 | } |
| 62 | |
| 63 | // evalSymlinksInScope evaluates symbolic links in "path" within a scope "root" |
| 64 | // and returns a result guaranteed to be contained within the scope "root" at |
| 65 | // the time of the call. Refer to [FollowSymlinkInScope] for details. |
| 66 | func evalSymlinksInScope(path, root string) (string, error) { |
| 67 | root = filepath.Clean(root) |
| 68 | if path == root { |
| 69 | return path, nil |
| 70 | } |
| 71 | if !strings.HasPrefix(path, root) { |
| 72 | return "", errors.New("evalSymlinksInScope: " + path + " is not in " + root) |
| 73 | } |
| 74 | const maxIter = 255 |
| 75 | originalPath := path |
| 76 | // given root of "/a" and path of "/a/b/../../c" we want path to be "/b/../../c" |
| 77 | path = path[len(root):] |
| 78 | if root == string(filepath.Separator) { |
| 79 | path = string(filepath.Separator) + path |
| 80 | } |
| 81 | if !strings.HasPrefix(path, string(filepath.Separator)) { |
| 82 | return "", errors.New("evalSymlinksInScope: " + path + " is not in " + root) |
| 83 | } |
| 84 | path = filepath.Clean(path) |
| 85 | // consume path by taking each frontmost path element, |
| 86 | // expanding it if it's a symlink, and appending it to b |
| 87 | var b bytes.Buffer |
| 88 | // b here will always be considered to be the "current absolute path inside |
| 89 | // root" when we append paths to it, we also append a slash and use |
| 90 | // filepath.Clean after the loop to trim the trailing slash |
| 91 | for n := 0; path != ""; n++ { |
| 92 | if n > maxIter { |
| 93 | return "", errors.New("evalSymlinksInScope: too many links in " + originalPath) |
| 94 | } |
| 95 | |
| 96 | // find next path component, p |
| 97 | i := strings.IndexRune(path, filepath.Separator) |
| 98 | var p string |
| 99 | if i == -1 { |
| 100 | p, path = path, "" |
| 101 | } else { |
| 102 | p, path = path[:i], path[i+1:] |
| 103 | } |
| 104 | |
| 105 | if p == "" { |
| 106 | continue |
| 107 | } |
| 108 | |
| 109 | // this takes a b.String() like "b/../" and a p like "c" and turns it |
| 110 | // into "/b/../c" which then gets filepath.Cleaned into "/c" and then |
| 111 | // root gets prepended and we Clean again (to remove any trailing slash |
| 112 | // if the first Clean gave us just "/") |
| 113 | pClean := filepath.Clean(string(filepath.Separator) + b.String() + p) |
| 114 | if isDriveOrRoot(pClean) { |
| 115 | // never Lstat "/" itself, or drive letters on Windows |
| 116 | b.Reset() |
| 117 | continue |
| 118 | } |
| 119 | pFull := filepath.Clean(root + pClean) |
| 120 | |
| 121 | fi, err := os.Lstat(pFull) |
| 122 | if os.IsNotExist(err) { |
| 123 | // if p does not exist, accept it |
| 124 | b.WriteString(p) |
| 125 | b.WriteRune(filepath.Separator) |
| 126 | continue |
| 127 | } |
| 128 | if err != nil { |
| 129 | return "", err |
| 130 | } |
| 131 | if fi.Mode()&os.ModeSymlink == 0 { |
| 132 | b.WriteString(p) |
| 133 | b.WriteRune(filepath.Separator) |
| 134 | continue |
| 135 | } |
| 136 | |
| 137 | // it's a symlink, put it at the front of path |
| 138 | dest, err := os.Readlink(pFull) |
| 139 | if err != nil { |
| 140 | return "", err |
| 141 | } |
| 142 | if isAbs(dest) { |
| 143 | b.Reset() |
| 144 | } |
| 145 | path = dest + string(filepath.Separator) + path |
| 146 | } |
| 147 | |
| 148 | // see note above on "fullP := ..." for why this is double-cleaned and |
| 149 | // what's happening here |
| 150 | return filepath.Clean(root + filepath.Clean(string(filepath.Separator)+b.String())), nil |
| 151 | } |
| 152 | |
| 153 | // EvalSymlinks is a modified version of [path/filepath.EvalSymlinks] from |
| 154 | // the Go standard library with support for Windows long paths (paths prepended |
| 155 | // with "\\?\"). On non-Windows platforms, it's an alias for [path/filepath.EvalSymlinks]. |
| 156 | // |
| 157 | // EvalSymlinks returns the path name after the evaluation of any symbolic |
| 158 | // links. If path is relative, the result will be relative to the current |
| 159 | // directory, unless one of the components is an absolute symbolic link. |
| 160 | // |
| 161 | // EvalSymlinks calls [path/filepath.Clean] on the result. |
| 162 | func EvalSymlinks(path string) (string, error) { |
| 163 | return evalSymlinks(path) |
| 164 | } |