| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
//go:build linux |
| 4 |
|
| 5 |
// Copyright (C) 2024-2025 Aleksa Sarai <[email protected]> |
| 6 |
// Copyright (C) 2024-2025 SUSE LLC |
| 7 |
// |
| 8 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 9 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 10 |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 11 |
|
| 12 |
package pathrs |
| 13 |
|
| 14 |
import ( |
| 15 |
"errors" |
| 16 |
"fmt" |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"runtime" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/stretchr/testify/assert" |
| 24 |
"github.com/stretchr/testify/require" |
| 25 |
"golang.org/x/sys/unix" |
| 26 |
|
| 27 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/fd" |
| 28 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/gocompat" |
| 29 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/procfs" |
| 30 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/testutils" |
| 31 |
) |
| 32 |
|
| 33 |
type partialLookupFunc func(root fd.Fd, unsafePath string) (*os.File, string, error) |
| 34 |
|
| 35 |
type lookupResult struct { |
| 36 |
handlePath, remainingPath string |
| 37 |
err error |
| 38 |
fileType uint32 |
| 39 |
} |
| 40 |
|
| 41 |
func checkPartialLookup(t *testing.T, partialLookupFn partialLookupFunc, rootDir fd.Fd, unsafePath string, expected lookupResult) { |
| 42 |
handle, remainingPath, err := partialLookupFn(rootDir, unsafePath) |
| 43 |
if handle != nil { |
| 44 |
defer handle.Close() //nolint:errcheck // test code |
| 45 |
} |
| 46 |
if expected.err != nil { |
| 47 |
if assert.Error(t, err) { |
| 48 |
assert.ErrorIs(t, err, expected.err) |
| 49 |
} |
| 50 |
if expected.handlePath == "" { |
| 51 |
require.Nil(t, handle, "expected to not get a handle") |
| 52 |
return |
| 53 |
} |
| 54 |
} else { |
| 55 |
if expected.remainingPath != "" { |
| 56 |
t.Errorf("we expect a remaining path, but no error? %q", expected.remainingPath) |
| 57 |
} |
| 58 |
require.NoError(t, err) |
| 59 |
} |
| 60 |
assert.NotNil(t, handle, "expected to get a handle") |
| 61 |
|
| 62 |
// Check the remainingPath. |
| 63 |
assert.Equal(t, expected.remainingPath, remainingPath, "remaining path") |
| 64 |
|
| 65 |
// Check the handle path. |
| 66 |
gotPath, err := procfs.ProcSelfFdReadlink(handle) |
| 67 |
require.NoError(t, err, "get real path of returned handle") |
| 68 |
assert.Equal(t, expected.handlePath, gotPath, "real handle path") |
| 69 |
// Make sure the handle matches the readlink path. |
| 70 |
assert.Equal(t, gotPath, handle.Name(), "handle.Name() matching real handle path") |
| 71 |
|
| 72 |
// Check the handle type. |
| 73 |
unixStat, err := fd.Fstat(handle) |
| 74 |
require.NoError(t, err, "fstat handle") |
| 75 |
assert.Equal(t, expected.fileType, unixStat.Mode&unix.S_IFMT, "handle S_IFMT type") |
| 76 |
} |
| 77 |
|
| 78 |
func testPartialLookup(t *testing.T, partialLookupFn partialLookupFunc) { |
| 79 |
tree := []string{ |
| 80 |
"dir a", |
| 81 |
"dir b/c/d/e/f", |
| 82 |
"file b/c/file", |
| 83 |
"symlink e /b/c/d/e", |
| 84 |
"symlink b-file b/c/file", |
| 85 |
// Dangling symlinks. |
| 86 |
"symlink a-fake1 a/fake", |
| 87 |
"symlink a-fake2 a/fake/foo/bar/..", |
| 88 |
"symlink a-fake3 a/fake/../../b", |
| 89 |
"dir c", |
| 90 |
"symlink c/a-fake1 a/fake", |
| 91 |
"symlink c/a-fake2 a/fake/foo/bar/..", |
| 92 |
"symlink c/a-fake3 a/fake/../../b", |
| 93 |
// Test non-lexical symlinks. |
| 94 |
"dir target", |
| 95 |
"dir link1", |
| 96 |
"symlink link1/target_abs /target", |
| 97 |
"symlink link1/target_rel ../target", |
| 98 |
"dir link2", |
| 99 |
"symlink link2/link1_abs /link1", |
| 100 |
"symlink link2/link1_rel ../link1", |
| 101 |
"dir link3", |
| 102 |
"symlink link3/target_abs /link2/link1_rel/target_rel", |
| 103 |
"symlink link3/target_rel ../link2/link1_rel/target_rel", |
| 104 |
"symlink link3/deep_dangling1 ../link2/link1_rel/target_rel/nonexist", |
| 105 |
"symlink link3/deep_dangling2 ../link2/link1_rel/target_rel/nonexist", |
| 106 |
// Deep dangling symlinks (with single components). |
| 107 |
"dir dangling", |
| 108 |
"symlink dangling/a b/c", |
| 109 |
"dir dangling/b", |
| 110 |
"symlink dangling/b/c ../c", |
| 111 |
"symlink dangling/c d/e", |
| 112 |
"dir dangling/d", |
| 113 |
"symlink dangling/d/e ../e", |
| 114 |
"symlink dangling/e f/../g", |
| 115 |
"dir dangling/f", |
| 116 |
"symlink dangling/g h/i/j/nonexistent", |
| 117 |
"dir dangling/h/i/j", |
| 118 |
// Deep dangling symlink using a non-dir component. |
| 119 |
"dir dangling-file", |
| 120 |
"symlink dangling-file/a b/c", |
| 121 |
"dir dangling-file/b", |
| 122 |
"symlink dangling-file/b/c ../c", |
| 123 |
"symlink dangling-file/c d/e", |
| 124 |
"dir dangling-file/d", |
| 125 |
"symlink dangling-file/d/e ../e", |
| 126 |
"symlink dangling-file/e f/../g", |
| 127 |
"dir dangling-file/f", |
| 128 |
"symlink dangling-file/g h/i/j/file/foo", |
| 129 |
"dir dangling-file/h/i/j", |
| 130 |
"file dangling-file/h/i/j/file", |
| 131 |
// Some "bad" inodes that a regular user can create. |
| 132 |
"fifo b/fifo", |
| 133 |
"sock b/sock", |
| 134 |
// Symlink loops. |
| 135 |
"dir loop", |
| 136 |
"symlink loop/basic-loop1 basic-loop1", |
| 137 |
"symlink loop/basic-loop2 /loop/basic-loop2", |
| 138 |
"symlink loop/basic-loop3 ../loop/basic-loop3", |
| 139 |
"dir loop/a", |
| 140 |
"symlink loop/a/link ../b/link", |
| 141 |
"dir loop/b", |
| 142 |
"symlink loop/b/link /loop/c/link", |
| 143 |
"dir loop/c", |
| 144 |
"symlink loop/c/link /loop/d/link", |
| 145 |
"symlink loop/d e", |
| 146 |
"dir loop/e", |
| 147 |
"symlink loop/e/link ../a/link", |
| 148 |
"symlink loop/link a/link", |
| 149 |
} |
| 150 |
|
| 151 |
root := testutils.CreateTree(t, tree...) |
| 152 |
|
| 153 |
rootDir, err := os.OpenFile(root, unix.O_PATH|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| 154 |
require.NoError(t, err) |
| 155 |
defer rootDir.Close() //nolint:errcheck // test code |
| 156 |
|
| 157 |
for name, test := range map[string]struct { |
| 158 |
unsafePath string |
| 159 |
expected lookupResult |
| 160 |
}{ |
| 161 |
// Complete lookups. |
| 162 |
"complete-dir1": {"a", lookupResult{handlePath: "/a", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 163 |
"complete-dir2": {"b/c/d/e/f", lookupResult{handlePath: "/b/c/d/e/f", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 164 |
"complete-dir3": {"b///././c////.//d/./././///e////.//./f//././././", lookupResult{handlePath: "/b/c/d/e/f", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 165 |
"complete-fifo": {"b/fifo", lookupResult{handlePath: "/b/fifo", remainingPath: "", fileType: unix.S_IFIFO}}, |
| 166 |
"complete-sock": {"b/sock", lookupResult{handlePath: "/b/sock", remainingPath: "", fileType: unix.S_IFSOCK}}, |
| 167 |
// Partial lookups. |
| 168 |
"partial-dir-basic": {"a/b/c/d/e/f/g/h", lookupResult{handlePath: "/a", remainingPath: "b/c/d/e/f/g/h", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 169 |
"partial-dir-dotdot": {"a/foo/../bar/baz", lookupResult{handlePath: "/a", remainingPath: "foo/../bar/baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 170 |
// Complete lookups of non-lexical symlinks. |
| 171 |
"nonlexical-basic-complete1": {"target", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 172 |
"nonlexical-basic-complete2": {"target/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 173 |
"nonlexical-basic-complete3": {"target//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 174 |
"nonlexical-basic-partial": {"target/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 175 |
"nonlexical-basic-partial-dotdot": {"target/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 176 |
"nonlexical-level1-abs-complete1": {"link1/target_abs", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 177 |
"nonlexical-level1-abs-complete2": {"link1/target_abs/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 178 |
"nonlexical-level1-abs-complete3": {"link1/target_abs//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 179 |
"nonlexical-level1-abs-partial": {"link1/target_abs/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 180 |
"nonlexical-level1-abs-partial-dotdot": {"link1/target_abs/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 181 |
"nonlexical-level1-rel-complete1": {"link1/target_rel", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 182 |
"nonlexical-level1-rel-complete2": {"link1/target_rel/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 183 |
"nonlexical-level1-rel-complete3": {"link1/target_rel//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 184 |
"nonlexical-level1-rel-partial": {"link1/target_rel/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 185 |
"nonlexical-level1-rel-partial-dotdot": {"link1/target_rel/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 186 |
"nonlexical-level2-abs-abs-complete1": {"link2/link1_abs/target_abs", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 187 |
"nonlexical-level2-abs-abs-complete2": {"link2/link1_abs/target_abs/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 188 |
"nonlexical-level2-abs-abs-complete3": {"link2/link1_abs/target_abs//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 189 |
"nonlexical-level2-abs-abs-partial": {"link2/link1_abs/target_abs/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 190 |
"nonlexical-level2-abs-abs-partial-dotdot": {"link2/link1_abs/target_abs/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 191 |
"nonlexical-level2-abs-rel-complete1": {"link2/link1_abs/target_rel", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 192 |
"nonlexical-level2-abs-rel-complete2": {"link2/link1_abs/target_rel/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 193 |
"nonlexical-level2-abs-rel-complete3": {"link2/link1_abs/target_rel//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 194 |
"nonlexical-level2-abs-rel-partial": {"link2/link1_abs/target_rel/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 195 |
"nonlexical-level2-abs-rel-partial-dotdot": {"link2/link1_abs/target_rel/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 196 |
"nonlexical-level2-abs-open-complete1": {"link2/link1_abs/../target", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 197 |
"nonlexical-level2-abs-open-complete2": {"link2/link1_abs/../target/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 198 |
"nonlexical-level2-abs-open-complete3": {"link2/link1_abs/../target//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 199 |
"nonlexical-level2-abs-open-partial": {"link2/link1_abs/../target/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 200 |
"nonlexical-level2-abs-open-partial-dotdot": {"link2/link1_abs/../target/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 201 |
"nonlexical-level2-rel-abs-complete1": {"link2/link1_rel/target_abs", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 202 |
"nonlexical-level2-rel-abs-complete2": {"link2/link1_rel/target_abs/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 203 |
"nonlexical-level2-rel-abs-complete3": {"link2/link1_rel/target_abs//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 204 |
"nonlexical-level2-rel-abs-partial": {"link2/link1_rel/target_abs/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 205 |
"nonlexical-level2-rel-abs-partial-dotdot": {"link2/link1_rel/target_abs/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 206 |
"nonlexical-level2-rel-rel-complete1": {"link2/link1_rel/target_rel", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 207 |
"nonlexical-level2-rel-rel-complete2": {"link2/link1_rel/target_rel/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 208 |
"nonlexical-level2-rel-rel-complete3": {"link2/link1_rel/target_rel//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 209 |
"nonlexical-level2-rel-rel-partial": {"link2/link1_rel/target_rel/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 210 |
"nonlexical-level2-rel-rel-partial-dotdot": {"link2/link1_rel/target_rel/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 211 |
"nonlexical-level2-rel-open-complete1": {"link2/link1_rel/../target", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 212 |
"nonlexical-level2-rel-open-complete2": {"link2/link1_rel/../target/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 213 |
"nonlexical-level2-rel-open-complete3": {"link2/link1_rel/../target//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 214 |
"nonlexical-level2-rel-open-partial": {"link2/link1_rel/../target/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 215 |
"nonlexical-level2-rel-open-partial-dotdot": {"link2/link1_rel/../target/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 216 |
"nonlexical-level3-abs-complete1": {"link3/target_abs", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 217 |
"nonlexical-level3-abs-complete2": {"link3/target_abs/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 218 |
"nonlexical-level3-abs-complete3": {"link3/target_abs//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 219 |
"nonlexical-level3-abs-partial": {"link3/target_abs/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 220 |
"nonlexical-level3-abs-partial-dotdot": {"link3/target_abs/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 221 |
"nonlexical-level3-rel-complete1": {"link3/target_rel", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 222 |
"nonlexical-level3-rel-complete2": {"link3/target_rel/", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 223 |
"nonlexical-level3-rel-complete3": {"link3/target_rel//", lookupResult{handlePath: "/target", remainingPath: "", fileType: unix.S_IFDIR}}, |
| 224 |
"nonlexical-level3-rel-partial": {"link3/target_rel/foo", lookupResult{handlePath: "/target", remainingPath: "foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 225 |
"nonlexical-level3-rel-partial-dotdot": {"link3/target_rel/../target/foo/bar/../baz", lookupResult{handlePath: "/target", remainingPath: "foo/bar/../baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 226 |
// Partial lookups due to hitting a non-directory. |
| 227 |
"partial-nondir-slash1": {"b/c/file/", lookupResult{handlePath: "/b/c/file", remainingPath: "", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 228 |
"partial-nondir-slash2": {"b/c/file//", lookupResult{handlePath: "/b/c/file", remainingPath: "/", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 229 |
"partial-nondir-dot": {"b/c/file/.", lookupResult{handlePath: "/b/c/file", remainingPath: ".", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 230 |
"partial-nondir-dotdot1": {"b/c/file/..", lookupResult{handlePath: "/b/c/file", remainingPath: "..", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 231 |
"partial-nondir-dotdot2": {"b/c/file/../foo/bar", lookupResult{handlePath: "/b/c/file", remainingPath: "../foo/bar", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 232 |
"partial-nondir-symlink-slash1": {"b-file/", lookupResult{handlePath: "/b/c/file", remainingPath: "", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 233 |
"partial-nondir-symlink-slash2": {"b-file//", lookupResult{handlePath: "/b/c/file", remainingPath: "/", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 234 |
"partial-nondir-symlink-dot": {"b-file/.", lookupResult{handlePath: "/b/c/file", remainingPath: ".", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 235 |
"partial-nondir-symlink-dotdot1": {"b-file/..", lookupResult{handlePath: "/b/c/file", remainingPath: "..", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 236 |
"partial-nondir-symlink-dotdot2": {"b-file/../foo/bar", lookupResult{handlePath: "/b/c/file", remainingPath: "../foo/bar", fileType: unix.S_IFREG, err: unix.ENOTDIR}}, |
| 237 |
"partial-fifo-slash1": {"b/fifo/", lookupResult{handlePath: "/b/fifo", remainingPath: "", fileType: unix.S_IFIFO, err: unix.ENOTDIR}}, |
| 238 |
"partial-fifo-slash2": {"b/fifo//", lookupResult{handlePath: "/b/fifo", remainingPath: "/", fileType: unix.S_IFIFO, err: unix.ENOTDIR}}, |
| 239 |
"partial-fifo-dot": {"b/fifo/.", lookupResult{handlePath: "/b/fifo", remainingPath: ".", fileType: unix.S_IFIFO, err: unix.ENOTDIR}}, |
| 240 |
"partial-fifo-dotdot1": {"b/fifo/..", lookupResult{handlePath: "/b/fifo", remainingPath: "..", fileType: unix.S_IFIFO, err: unix.ENOTDIR}}, |
| 241 |
"partial-fifo-dotdot2": {"b/fifo/../foo/bar", lookupResult{handlePath: "/b/fifo", remainingPath: "../foo/bar", fileType: unix.S_IFIFO, err: unix.ENOTDIR}}, |
| 242 |
"partial-sock-slash1": {"b/sock/", lookupResult{handlePath: "/b/sock", remainingPath: "", fileType: unix.S_IFSOCK, err: unix.ENOTDIR}}, |
| 243 |
"partial-sock-slash2": {"b/sock//", lookupResult{handlePath: "/b/sock", remainingPath: "/", fileType: unix.S_IFSOCK, err: unix.ENOTDIR}}, |
| 244 |
"partial-sock-dot": {"b/sock/.", lookupResult{handlePath: "/b/sock", remainingPath: ".", fileType: unix.S_IFSOCK, err: unix.ENOTDIR}}, |
| 245 |
"partial-sock-dotdot1": {"b/sock/..", lookupResult{handlePath: "/b/sock", remainingPath: "..", fileType: unix.S_IFSOCK, err: unix.ENOTDIR}}, |
| 246 |
"partial-sock-dotdot2": {"b/sock/../foo/bar", lookupResult{handlePath: "/b/sock", remainingPath: "../foo/bar", fileType: unix.S_IFSOCK, err: unix.ENOTDIR}}, |
| 247 |
// Dangling symlinks are treated as though they are non-existent. |
| 248 |
"dangling1-inroot-trailing": {"a-fake1", lookupResult{handlePath: "/", remainingPath: "a-fake1", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 249 |
"dangling1-inroot-partial": {"a-fake1/foo", lookupResult{handlePath: "/", remainingPath: "a-fake1/foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 250 |
"dangling1-inroot-partial-dotdot": {"a-fake1/../bar/baz", lookupResult{handlePath: "/", remainingPath: "a-fake1/../bar/baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 251 |
"dangling1-sub-trailing": {"c/a-fake1", lookupResult{handlePath: "/c", remainingPath: "a-fake1", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 252 |
"dangling1-sub-partial": {"c/a-fake1/foo", lookupResult{handlePath: "/c", remainingPath: "a-fake1/foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 253 |
"dangling1-sub-partial-dotdot": {"c/a-fake1/../bar/baz", lookupResult{handlePath: "/c", remainingPath: "a-fake1/../bar/baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 254 |
"dangling2-inroot-trailing": {"a-fake2", lookupResult{handlePath: "/", remainingPath: "a-fake2", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 255 |
"dangling2-inroot-partial": {"a-fake2/foo", lookupResult{handlePath: "/", remainingPath: "a-fake2/foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 256 |
"dangling2-inroot-partial-dotdot": {"a-fake2/../bar/baz", lookupResult{handlePath: "/", remainingPath: "a-fake2/../bar/baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 257 |
"dangling2-sub-trailing": {"c/a-fake2", lookupResult{handlePath: "/c", remainingPath: "a-fake2", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 258 |
"dangling2-sub-partial": {"c/a-fake2/foo", lookupResult{handlePath: "/c", remainingPath: "a-fake2/foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 259 |
"dangling2-sub-partial-dotdot": {"c/a-fake2/../bar/baz", lookupResult{handlePath: "/c", remainingPath: "a-fake2/../bar/baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 260 |
"dangling3-inroot-trailing": {"a-fake3", lookupResult{handlePath: "/", remainingPath: "a-fake3", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 261 |
"dangling3-inroot-partial": {"a-fake3/foo", lookupResult{handlePath: "/", remainingPath: "a-fake3/foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 262 |
"dangling3-inroot-partial-dotdot": {"a-fake3/../bar/baz", lookupResult{handlePath: "/", remainingPath: "a-fake3/../bar/baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 263 |
"dangling3-sub-trailing": {"c/a-fake3", lookupResult{handlePath: "/c", remainingPath: "a-fake3", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 264 |
"dangling3-sub-partial": {"c/a-fake3/foo", lookupResult{handlePath: "/c", remainingPath: "a-fake3/foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 265 |
"dangling3-sub-partial-dotdot": {"c/a-fake3/../bar/baz", lookupResult{handlePath: "/c", remainingPath: "a-fake3/../bar/baz", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 266 |
// Tricky dangling symlinks. |
| 267 |
"dangling-tricky1-trailing": {"link3/deep_dangling1", lookupResult{handlePath: "/link3", remainingPath: "deep_dangling1", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 268 |
"dangling-tricky1-partial": {"link3/deep_dangling1/foo", lookupResult{handlePath: "/link3", remainingPath: "deep_dangling1/foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 269 |
"dangling-tricky1-partial-dotdot": {"link3/deep_dangling1/..", lookupResult{handlePath: "/link3", remainingPath: "deep_dangling1/..", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 270 |
"dangling-tricky2-trailing": {"link3/deep_dangling2", lookupResult{handlePath: "/link3", remainingPath: "deep_dangling2", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 271 |
"dangling-tricky2-partial": {"link3/deep_dangling2/foo", lookupResult{handlePath: "/link3", remainingPath: "deep_dangling2/foo", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 272 |
"dangling-tricky2-partial-dotdot": {"link3/deep_dangling2/..", lookupResult{handlePath: "/link3", remainingPath: "deep_dangling2/..", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 273 |
// Really deep dangling links. |
| 274 |
"deep-dangling1": {"dangling/a", lookupResult{handlePath: "/dangling", remainingPath: "a", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 275 |
"deep-dangling2": {"dangling/b/c", lookupResult{handlePath: "/dangling/b", remainingPath: "c", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 276 |
"deep-dangling3": {"dangling/c", lookupResult{handlePath: "/dangling", remainingPath: "c", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 277 |
"deep-dangling4": {"dangling/d/e", lookupResult{handlePath: "/dangling/d", remainingPath: "e", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 278 |
"deep-dangling5": {"dangling/e", lookupResult{handlePath: "/dangling", remainingPath: "e", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 279 |
"deep-dangling6": {"dangling/g", lookupResult{handlePath: "/dangling", remainingPath: "g", fileType: unix.S_IFDIR, err: unix.ENOENT}}, |
| 280 |
"deep-dangling-fileasdir1": {"dangling-file/a", lookupResult{handlePath: "/dangling-file", remainingPath: "a", fileType: unix.S_IFDIR, err: unix.ENOTDIR}}, |
| 281 |
"deep-dangling-fileasdir2": {"dangling-file/b/c", lookupResult{handlePath: "/dangling-file/b", remainingPath: "c", fileType: unix.S_IFDIR, err: unix.ENOTDIR}}, |
| 282 |
"deep-dangling-fileasdir3": {"dangling-file/c", lookupResult{handlePath: "/dangling-file", remainingPath: "c", fileType: unix.S_IFDIR, err: unix.ENOTDIR}}, |
| 283 |
"deep-dangling-fileasdir4": {"dangling-file/d/e", lookupResult{handlePath: "/dangling-file/d", remainingPath: "e", fileType: unix.S_IFDIR, err: unix.ENOTDIR}}, |
| 284 |
"deep-dangling-fileasdir5": {"dangling-file/e", lookupResult{handlePath: "/dangling-file", remainingPath: "e", fileType: unix.S_IFDIR, err: unix.ENOTDIR}}, |
| 285 |
"deep-dangling-fileasdir6": {"dangling-file/g", lookupResult{handlePath: "/dangling-file", remainingPath: "g", fileType: unix.S_IFDIR, err: unix.ENOTDIR}}, |
| 286 |
// Symlink loops. |
| 287 |
"loop": {"loop/link", lookupResult{err: unix.ELOOP}}, |
| 288 |
"loop-basic1": {"loop/basic-loop1", lookupResult{err: unix.ELOOP}}, |
| 289 |
"loop-basic2": {"loop/basic-loop2", lookupResult{err: unix.ELOOP}}, |
| 290 |
"loop-basic3": {"loop/basic-loop3", lookupResult{err: unix.ELOOP}}, |
| 291 |
} { |
| 292 |
test := test // copy iterator |
| 293 |
// Update the handlePath to be inside our root. |
| 294 |
if test.expected.handlePath != "" { |
| 295 |
test.expected.handlePath = filepath.Join(root, test.expected.handlePath) |
| 296 |
} |
| 297 |
t.Run(name, func(t *testing.T) { |
| 298 |
checkPartialLookup(t, partialLookupFn, rootDir, test.unsafePath, test.expected) |
| 299 |
}) |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
func tRunWrapper(t *testing.T) testutils.TRunFunc { |
| 304 |
return func(name string, doFn testutils.TDoFunc) { |
| 305 |
t.Run(name, func(t *testing.T) { |
| 306 |
doFn(t) |
| 307 |
}) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
func TestPartialLookupInRoot(t *testing.T) { |
| 312 |
testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) { |
| 313 |
t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code |
| 314 |
testPartialLookup(t, partialLookupInRoot) |
| 315 |
}) |
| 316 |
} |
| 317 |
|
| 318 |
func TestPartialOpenat2(t *testing.T) { |
| 319 |
testPartialLookup(t, partialLookupOpenat2) |
| 320 |
} |
| 321 |
|
| 322 |
func TestPartialLookupInRoot_BadInode(t *testing.T) { |
| 323 |
testutils.RequireRoot(t) // mknod |
| 324 |
|
| 325 |
testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) { |
| 326 |
t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code |
| 327 |
partialLookupFn := partialLookupInRoot |
| 328 |
|
| 329 |
tree := []string{ |
| 330 |
// Make sure we don't open "bad" inodes. |
| 331 |
"dir foo", |
| 332 |
"char foo/whiteout 0 0", |
| 333 |
"block foo/whiteout-blk 0 0", |
| 334 |
} |
| 335 |
|
| 336 |
root := testutils.CreateTree(t, tree...) |
| 337 |
|
| 338 |
rootDir, err := os.OpenFile(root, unix.O_PATH|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| 339 |
require.NoError(t, err) |
| 340 |
defer rootDir.Close() //nolint:errcheck // test code |
| 341 |
|
| 342 |
for name, test := range map[string]struct { |
| 343 |
unsafePath string |
| 344 |
expected lookupResult |
| 345 |
}{ |
| 346 |
// Complete lookups. |
| 347 |
"char-trailing": {"foo/whiteout", lookupResult{handlePath: "/foo/whiteout", remainingPath: "", fileType: unix.S_IFCHR}}, |
| 348 |
"blk-trailing": {"foo/whiteout-blk", lookupResult{handlePath: "/foo/whiteout-blk", remainingPath: "", fileType: unix.S_IFBLK}}, |
| 349 |
// Partial lookups due to hitting a non-directory. |
| 350 |
"char-dot": {"foo/whiteout/.", lookupResult{handlePath: "/foo/whiteout", remainingPath: ".", fileType: unix.S_IFCHR, err: unix.ENOTDIR}}, |
| 351 |
"char-dotdot1": {"foo/whiteout/..", lookupResult{handlePath: "/foo/whiteout", remainingPath: "..", fileType: unix.S_IFCHR, err: unix.ENOTDIR}}, |
| 352 |
"char-dotdot2": {"foo/whiteout/../foo/bar", lookupResult{handlePath: "/foo/whiteout", remainingPath: "../foo/bar", fileType: unix.S_IFCHR, err: unix.ENOTDIR}}, |
| 353 |
"blk-dot": {"foo/whiteout-blk/.", lookupResult{handlePath: "/foo/whiteout-blk", remainingPath: ".", fileType: unix.S_IFBLK, err: unix.ENOTDIR}}, |
| 354 |
"blk-dotdot1": {"foo/whiteout-blk/..", lookupResult{handlePath: "/foo/whiteout-blk", remainingPath: "..", fileType: unix.S_IFBLK, err: unix.ENOTDIR}}, |
| 355 |
"blk-dotdot2": {"foo/whiteout-blk/../foo/bar", lookupResult{handlePath: "/foo/whiteout-blk", remainingPath: "../foo/bar", fileType: unix.S_IFBLK, err: unix.ENOTDIR}}, |
| 356 |
} { |
| 357 |
test := test // copy iterator |
| 358 |
// Update the handlePath to be inside our root. |
| 359 |
if test.expected.handlePath != "" { |
| 360 |
test.expected.handlePath = filepath.Join(root, test.expected.handlePath) |
| 361 |
} |
| 362 |
t.Run(name, func(t *testing.T) { |
| 363 |
checkPartialLookup(t, partialLookupFn, rootDir, test.unsafePath, test.expected) |
| 364 |
}) |
| 365 |
} |
| 366 |
}) |
| 367 |
} |
| 368 |
|
| 369 |
type racingLookupMeta struct { |
| 370 |
pauseCh chan struct{} |
| 371 |
passOkCount, passErrCount, skipCount, failCount, badErrCount int // test state counts |
| 372 |
badNameCount, fixRemainingPathCount int // workaround counts |
| 373 |
skipErrCounts map[error]int |
| 374 |
} |
| 375 |
|
| 376 |
func newRacingLookupMeta(pauseCh chan struct{}) *racingLookupMeta { |
| 377 |
return &racingLookupMeta{ |
| 378 |
pauseCh: pauseCh, |
| 379 |
skipErrCounts: map[error]int{}, |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
func (m *racingLookupMeta) checkPartialLookup(t *testing.T, rootDir fd.Fd, unsafePath string, skipErrs []error, allowedResults []lookupResult) { |
| 384 |
// Similar to checkPartialLookup, but with extra logic for |
| 385 |
// handling the lookup stopping partly through the lookup. |
| 386 |
handle, remainingPath, err := partialLookupInRoot(rootDir, unsafePath) |
| 387 |
var ( |
| 388 |
handleName string |
| 389 |
realPath string |
| 390 |
unixStat unix.Stat_t |
| 391 |
) |
| 392 |
if handle != nil { |
| 393 |
handleName = handle.Name() |
| 394 |
|
| 395 |
// Get the "proper" name from ProcSelfFdReadlink. |
| 396 |
m.pauseCh <- struct{}{} |
| 397 |
realPath, err = procfs.ProcSelfFdReadlink(handle) |
| 398 |
<-m.pauseCh |
| 399 |
require.NoError(t, err, "get real path of returned handle") |
| 400 |
|
| 401 |
unixStat, err = fd.Fstat(handle) |
| 402 |
require.NoError(t, err, "stat handle") |
| 403 |
|
| 404 |
_ = handle.Close() |
| 405 |
} else if err != nil { |
| 406 |
for _, skipErr := range skipErrs { |
| 407 |
if errors.Is(err, skipErr) { |
| 408 |
m.skipErrCounts[skipErr]++ |
| 409 |
m.skipCount++ |
| 410 |
return |
| 411 |
} |
| 412 |
} |
| 413 |
for _, allowed := range allowedResults { |
| 414 |
if allowed.err != nil && errors.Is(err, allowed.err) { |
| 415 |
m.passErrCount++ |
| 416 |
return |
| 417 |
} |
| 418 |
} |
| 419 |
// If we didn't hit any of the allowed errors, it's an |
| 420 |
// unexpected error. |
| 421 |
assert.NoError(t, err) |
| 422 |
m.badErrCount++ |
| 423 |
return |
| 424 |
} |
| 425 |
|
| 426 |
if realPath != handleName { |
| 427 |
// It's possible for handle.Name() to be wrong because while it was |
| 428 |
// correct when it was set, it might not match if the path was swapped |
| 429 |
// afterwards (for both openat2 and partialLookupInRoot). |
| 430 |
m.badNameCount++ |
| 431 |
} |
| 432 |
|
| 433 |
// It's possible for lookups with ".." components to decide to cut off the |
| 434 |
// lookup partially through the resolution when dealing with a swapping |
| 435 |
// attack, so for the purposes of validating our tests we clean up the |
| 436 |
// remainingPath so that it has all of the ".." components removed (but |
| 437 |
// include this in our statistics). |
| 438 |
fullLogicalPath := filepath.Join(realPath, remainingPath) |
| 439 |
newRemainingPath, err := filepath.Rel(realPath, fullLogicalPath) |
| 440 |
require.NoErrorf(t, err, "clean remaining path %s", remainingPath) |
| 441 |
if remainingPath != newRemainingPath { |
| 442 |
m.fixRemainingPathCount++ |
| 443 |
} |
| 444 |
remainingPath = newRemainingPath |
| 445 |
|
| 446 |
gotResult := lookupResult{ |
| 447 |
handlePath: realPath, |
| 448 |
remainingPath: remainingPath, |
| 449 |
fileType: unixStat.Mode & unix.S_IFMT, |
| 450 |
} |
| 451 |
counter := &m.passOkCount |
| 452 |
if !assert.Contains(t, allowedResults, gotResult) { |
| 453 |
counter = &m.failCount |
| 454 |
} |
| 455 |
(*counter)++ |
| 456 |
} |
| 457 |
|
| 458 |
// doRenameExchangeLoop runs in a loop swapping two paths, intended to be run |
| 459 |
// in a goroutine during a test. |
| 460 |
func doRenameExchangeLoop(pauseCh chan struct{}, exitCh <-chan struct{}, dir fd.Fd, pathA, pathB string) { |
| 461 |
for { |
| 462 |
select { |
| 463 |
case <-exitCh: |
| 464 |
return |
| 465 |
case <-pauseCh: |
| 466 |
// Wait for caller to unpause us. |
| 467 |
select { |
| 468 |
case pauseCh <- struct{}{}: |
| 469 |
case <-exitCh: |
| 470 |
return |
| 471 |
} |
| 472 |
default: |
| 473 |
// Do the swap twice so that we only pause when we are in a |
| 474 |
// "correct" state. |
| 475 |
for i := 0; i < 2; i++ { |
| 476 |
err := unix.Renameat2(int(dir.Fd()), pathA, int(dir.Fd()), pathB, unix.RENAME_EXCHANGE) |
| 477 |
if err != nil && int(dir.Fd()) != -1 && !errors.Is(err, unix.EBADF) { |
| 478 |
// Should never happen, and if it does we will potentially |
| 479 |
// enter a bad filesystem state if we get paused. |
| 480 |
panic(fmt.Sprintf("renameat2([%d]%q, %q, ..., %q, RENAME_EXCHANGE) = %v", int(dir.Fd()), dir.Name(), pathA, pathB, err)) |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
// Make sure GC doesn't close the directory handle. |
| 485 |
runtime.KeepAlive(dir) |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
func TestPartialLookup_RacingRename(t *testing.T) { |
| 490 |
if testing.Short() { |
| 491 |
t.Skip("skipping race tests in short mode") |
| 492 |
} |
| 493 |
testutils.RequireRenameExchange(t) |
| 494 |
|
| 495 |
testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) { |
| 496 |
t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code |
| 497 |
tree := []string{ |
| 498 |
"dir a/b/c/d", |
| 499 |
"symlink b-link ../b/../b/../b/../b/../b/../b/../b/../b/../b/../b/../b/../b/../b", |
| 500 |
"symlink c-link ../../b/c/../../b/c/../../b/c/../../b/c/../../b/c/../../b/c/../../b/c/../../b/c/../../b/c", |
| 501 |
"file file", |
| 502 |
"symlink bad-link /foobar", |
| 503 |
} |
| 504 |
|
| 505 |
var ( |
| 506 |
handlePath = "/a/b/c/d" |
| 507 |
remainingPath = "e" |
| 508 |
defaultExpected []lookupResult |
| 509 |
) |
| 510 |
// The lookup could stop at any component other than /a, so allow all |
| 511 |
// of them. |
| 512 |
for handlePath != "/" { |
| 513 |
defaultExpected = append(defaultExpected, lookupResult{ |
| 514 |
handlePath: handlePath, |
| 515 |
remainingPath: remainingPath, |
| 516 |
fileType: unix.S_IFDIR, |
| 517 |
}) |
| 518 |
handlePath, remainingPath = filepath.Dir(handlePath), filepath.Join(filepath.Base(handlePath), remainingPath) |
| 519 |
} |
| 520 |
for name, test := range map[string]struct { |
| 521 |
subPathA, subPathB string |
| 522 |
unsafePath string |
| 523 |
skipErrs []error |
| 524 |
allowedResults []lookupResult |
| 525 |
}{ |
| 526 |
// Swap a symlink in and out. |
| 527 |
"swap-dir-link1-basic": {"a/b", "b-link", "a/b/c/d/e", nil, gocompat.SlicesClone(defaultExpected)}, |
| 528 |
"swap-dir-link2-basic": {"a/b/c", "c-link", "a/b/c/d/e", nil, gocompat.SlicesClone(defaultExpected)}, |
| 529 |
"swap-dir-link1-dotdot1": {"a/b", "b-link", "a/b/../b/../b/../b/../b/../b/../b/c/d/../d/../d/../d/../d/../d/e", nil, gocompat.SlicesClone(defaultExpected)}, |
| 530 |
"swap-dir-link1-dotdot2": {"a/b", "b-link", "a/b/c/../c/../c/../c/../c/../c/../c/d/../d/../d/../d/../d/../d/e", nil, gocompat.SlicesClone(defaultExpected)}, |
| 531 |
"swap-dir-link2-dotdot": {"a/b/c", "c-link", "a/b/c/../c/../c/../c/../c/../c/../c/d/../d/../d/../d/../d/../d/e", nil, gocompat.SlicesClone(defaultExpected)}, |
| 532 |
// TODO: Swap a directory. |
| 533 |
// Swap a non-directory. |
| 534 |
"swap-dir-file-basic": {"a/b", "file", "a/b/c/d/e", []error{unix.ENOTDIR, unix.ENOENT}, append( |
| 535 |
// We could hit one of the final paths. |
| 536 |
gocompat.SlicesClone(defaultExpected), |
| 537 |
// We could hit the file and stop resolving. |
| 538 |
lookupResult{handlePath: "/file", remainingPath: "c/d/e", fileType: unix.S_IFREG}, |
| 539 |
)}, |
| 540 |
"swap-dir-file-dotdot": {"a/b", "file", "a/b/c/../c/../c/../c/../c/../c/../c/d/../d/../d/../d/../d/../d/e", []error{unix.ENOTDIR, unix.ENOENT}, append( |
| 541 |
// We could hit one of the final paths. |
| 542 |
gocompat.SlicesClone(defaultExpected), |
| 543 |
// We could hit the file and stop resolving. |
| 544 |
lookupResult{handlePath: "/file", remainingPath: "c/d/e", fileType: unix.S_IFREG}, |
| 545 |
)}, |
| 546 |
// Swap a dangling symlink. |
| 547 |
"swap-dir-danglinglink-basic": {"a/b", "bad-link", "a/b/c/d/e", []error{unix.ENOENT}, gocompat.SlicesClone(defaultExpected)}, |
| 548 |
"swap-dir-danglinglink-dotdot": {"a/b", "bad-link", "a/b/c/../c/../c/../c/../c/../c/../c/d/../d/../d/../d/../d/../d/e", []error{unix.ENOENT}, gocompat.SlicesClone(defaultExpected)}, |
| 549 |
// Swap the root. |
| 550 |
"swap-root-basic": {".", "../outsideroot", "a/b/c/d/e", nil, gocompat.SlicesClone(defaultExpected)}, |
| 551 |
"swap-root-dotdot": {".", "../outsideroot", "a/b/../../a/b/../../a/b/../../a/b/../../a/b/../../a/b/../../a/b/../../a/b/c/d/e", nil, gocompat.SlicesClone(defaultExpected)}, |
| 552 |
"swap-root-dotdot-extra": {".", "../outsideroot", "a/" + strings.Repeat("b/c/d/../../../", 10) + "b/c/d/e", nil, gocompat.SlicesClone(defaultExpected)}, |
| 553 |
// Swap one of our walking paths outside the root. |
| 554 |
"swap-dir-outsideroot-basic": {"a/b", "../outsideroot", "a/b/c/d/e", nil, append( |
| 555 |
// We could hit the expected path. |
| 556 |
gocompat.SlicesClone(defaultExpected), |
| 557 |
// We could also land in the "outsideroot" path. This is okay |
| 558 |
// because there was a moment when this directory was inside |
| 559 |
// the root, and the attacker moved it outside the root. If we |
| 560 |
// were to go into "..", the lookup would've failed (and we |
| 561 |
// would get an error here if that wasn't the case). |
| 562 |
lookupResult{handlePath: "../outsideroot", remainingPath: "c/d/e", fileType: unix.S_IFDIR}, |
| 563 |
lookupResult{handlePath: "../outsideroot/c", remainingPath: "d/e", fileType: unix.S_IFDIR}, |
| 564 |
lookupResult{handlePath: "../outsideroot/c/d", remainingPath: "e", fileType: unix.S_IFDIR}, |
| 565 |
)}, |
| 566 |
"swap-dir-outsideroot-dotdot": {"a/b", "../outsideroot", "a/b/../../a/b/../../a/b/../../a/b/../../a/b/../../a/b/../../a/b/../../a/b/c/d/e", nil, append( |
| 567 |
// We could hit the expected path. |
| 568 |
gocompat.SlicesClone(defaultExpected), |
| 569 |
// We could also land in the "outsideroot" path. This is okay |
| 570 |
// because there was a moment when this directory was inside |
| 571 |
// the root, and the attacker moved it outside the root. |
| 572 |
// |
| 573 |
// Neither openat2 nor partialLookupInRoot will allow us to |
| 574 |
// walk into ".." in this case (escaping the root), and we |
| 575 |
// would catch that if it did happen. |
| 576 |
lookupResult{handlePath: "../outsideroot", remainingPath: "c/d/e", fileType: unix.S_IFDIR}, |
| 577 |
lookupResult{handlePath: "../outsideroot/c", remainingPath: "d/e", fileType: unix.S_IFDIR}, |
| 578 |
lookupResult{handlePath: "../outsideroot/c/d", remainingPath: "e", fileType: unix.S_IFDIR}, |
| 579 |
)}, |
| 580 |
} { |
| 581 |
test := test // copy iterator |
| 582 |
test.skipErrs = append(test.skipErrs, unix.EAGAIN, unix.EXDEV) |
| 583 |
t.Run(name, func(t *testing.T) { |
| 584 |
root := testutils.CreateTree(t, tree...) |
| 585 |
|
| 586 |
// Update the handlePath to be inside our root. |
| 587 |
for idx := range test.allowedResults { |
| 588 |
test.allowedResults[idx].handlePath = filepath.Join(root, test.allowedResults[idx].handlePath) |
| 589 |
} |
| 590 |
|
| 591 |
// Create an "outsideroot" path as a sibling to our root, for |
| 592 |
// swapping. |
| 593 |
err := os.MkdirAll(filepath.Join(root, "../outsideroot"), 0o755) |
| 594 |
require.NoError(t, err) |
| 595 |
|
| 596 |
rootDir, err := os.OpenFile(root, unix.O_PATH|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| 597 |
require.NoError(t, err) |
| 598 |
defer rootDir.Close() //nolint:errcheck // test code |
| 599 |
|
| 600 |
// If the swapping subpaths are "." we need to use an absolute |
| 601 |
// path because renaming "." isn't allowed. |
| 602 |
for _, subPath := range []*string{&test.subPathA, &test.subPathB} { |
| 603 |
if filepath.Join(root, *subPath) == root { |
| 604 |
*subPath = root |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
// Run a goroutine that spams a rename in the root. |
| 609 |
pauseCh := make(chan struct{}) |
| 610 |
exitCh := make(chan struct{}) |
| 611 |
defer close(exitCh) |
| 612 |
go doRenameExchangeLoop(pauseCh, exitCh, rootDir, test.subPathA, test.subPathB) |
| 613 |
|
| 614 |
// Do several runs to try to catch bugs. |
| 615 |
const ( |
| 616 |
testRuns = 3000 |
| 617 |
minPassCount = 10 |
| 618 |
) |
| 619 |
m := newRacingLookupMeta(pauseCh) |
| 620 |
doneRuns := 0 |
| 621 |
for ; doneRuns < testRuns || m.passOkCount < minPassCount; doneRuns++ { |
| 622 |
m.checkPartialLookup(t, rootDir, test.unsafePath, test.skipErrs, test.allowedResults) |
| 623 |
// Make sure we don't infinite loop here. |
| 624 |
if doneRuns >= 50*testRuns { |
| 625 |
break |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
pct := func(count int) string { |
| 630 |
return fmt.Sprintf("%d(%.3f%%)", count, 100.0*float64(count)/float64(doneRuns)) |
| 631 |
} |
| 632 |
|
| 633 |
// No passing runs is a bit unfortunate, but some of our tests |
| 634 |
// can do that and failing here would just lead to flaky tests. |
| 635 |
if m.passOkCount == 0 { |
| 636 |
t.Logf("WARNING: NO PASSING RUNS!") |
| 637 |
} |
| 638 |
|
| 639 |
// Output some stats. |
| 640 |
t.Logf("after %d runs: passOk=%s passErr=%s skip=%s fail=%s (+badErr=%s)", |
| 641 |
// runs and breakdown of path-related (pass, fail) as well as skipped runs |
| 642 |
doneRuns, pct(m.passOkCount), pct(m.passErrCount), pct(m.skipCount), pct(m.failCount), |
| 643 |
// failures due to incorrect errors (rather than bad paths) |
| 644 |
pct(m.badErrCount)) |
| 645 |
t.Logf(" badHandleName=%s fixRemainingPath=%s", |
| 646 |
// stats for how many test runs had to have some "workarounds" |
| 647 |
pct(m.badNameCount), pct(m.fixRemainingPathCount)) |
| 648 |
if len(m.skipErrCounts) > 0 { |
| 649 |
t.Logf(" skipErr breakdown:") |
| 650 |
for err, count := range m.skipErrCounts { |
| 651 |
t.Logf(" %3.d: %v", count, err) |
| 652 |
} |
| 653 |
} |
| 654 |
}) |
| 655 |
} |
| 656 |
}) |
| 657 |
} |
| 658 |
|
| 659 |
type ssOperation interface { |
| 660 |
String() string |
| 661 |
Do(*testing.T, *symlinkStack) error |
| 662 |
} |
| 663 |
|
| 664 |
type ssOpPop struct{ part string } |
| 665 |
|
| 666 |
func (op ssOpPop) Do(_ *testing.T, s *symlinkStack) error { return s.PopPart(op.part) } |
| 667 |
|
| 668 |
func (op ssOpPop) String() string { return fmt.Sprintf("PopPart(%q)", op.part) } |
| 669 |
|
| 670 |
type ssOpSwapLink struct { |
| 671 |
part, dirName, expectedPath, linkTarget string |
| 672 |
} |
| 673 |
|
| 674 |
func fakeFile(name string) (*os.File, error) { |
| 675 |
fd, err := unix.Open(".", unix.O_PATH|unix.O_CLOEXEC, 0) |
| 676 |
if err != nil { |
| 677 |
return nil, &os.PathError{Op: "open", Path: ".", Err: err} |
| 678 |
} |
| 679 |
return os.NewFile(uintptr(fd), name), nil |
| 680 |
} |
| 681 |
|
| 682 |
func (op ssOpSwapLink) Do(t *testing.T, s *symlinkStack) error { |
| 683 |
f, err := fakeFile(op.dirName) |
| 684 |
require.NoErrorf(t, err, "make fake file with %q name", op.dirName) |
| 685 |
return s.SwapLink(op.part, f, op.expectedPath, op.linkTarget) |
| 686 |
} |
| 687 |
|
| 688 |
func (op ssOpSwapLink) String() string { |
| 689 |
return fmt.Sprintf("SwapLink(%q, <%s>, %q, %q)", op.part, op.dirName, op.expectedPath, op.linkTarget) |
| 690 |
} |
| 691 |
|
| 692 |
type ssOp struct { |
| 693 |
op ssOperation |
| 694 |
expectedErr error |
| 695 |
} |
| 696 |
|
| 697 |
func (t ssOp) String() string { return fmt.Sprintf("%s = %v", t.op, t.expectedErr) } |
| 698 |
|
| 699 |
func dumpStack(t *testing.T, ss symlinkStack) { |
| 700 |
for i, sym := range ss { |
| 701 |
t.Logf("ss[%d] %s", i, sym) |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
func testSymlinkStack(t *testing.T, ops ...ssOp) symlinkStack { |
| 706 |
var ss symlinkStack |
| 707 |
for _, op := range ops { |
| 708 |
err := op.op.Do(t, &ss) |
| 709 |
if !assert.ErrorIsf(t, err, op.expectedErr, "%s", op) { //nolint:testifylint |
| 710 |
dumpStack(t, ss) |
| 711 |
ss.Close() |
| 712 |
t.FailNow() |
| 713 |
} |
| 714 |
} |
| 715 |
return ss |
| 716 |
} |
| 717 |
|
| 718 |
func TestSymlinkStackBasic(t *testing.T) { |
| 719 |
ss := testSymlinkStack(t, |
| 720 |
ssOp{op: ssOpSwapLink{"foo", "A", "", "bar/baz"}}, |
| 721 |
ssOp{op: ssOpSwapLink{"bar", "B", "baz", "abcd"}}, |
| 722 |
ssOp{op: ssOpPop{"abcd"}}, |
| 723 |
ssOp{op: ssOpSwapLink{"baz", "C", "", "taillink"}}, |
| 724 |
ssOp{op: ssOpPop{"taillink"}}, |
| 725 |
ssOp{op: ssOpPop{"anotherbit"}}, |
| 726 |
) |
| 727 |
defer ss.Close() //nolint:errcheck // test code |
| 728 |
|
| 729 |
if !assert.True(t, ss.IsEmpty()) { |
| 730 |
dumpStack(t, ss) |
| 731 |
t.FailNow() |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
func TestSymlinkStackBadPop(t *testing.T) { |
| 736 |
ss := testSymlinkStack(t, |
| 737 |
ssOp{op: ssOpSwapLink{"foo", "A", "", "bar/baz"}}, |
| 738 |
ssOp{op: ssOpSwapLink{"bar", "B", "baz", "abcd"}}, |
| 739 |
ssOp{op: ssOpSwapLink{"bad", "C", "", "abcd"}, expectedErr: errBrokenSymlinkStack}, |
| 740 |
ssOp{op: ssOpPop{"abcd"}}, |
| 741 |
ssOp{op: ssOpSwapLink{"baz", "C", "", "abcd"}}, |
| 742 |
ssOp{op: ssOpSwapLink{"abcd", "D", "", ""}}, // TODO: This is technically an invalid thing to push. |
| 743 |
ssOp{op: ssOpSwapLink{"another", "E", "", ""}, expectedErr: errBrokenSymlinkStack}, |
| 744 |
) |
| 745 |
defer ss.Close() //nolint:errcheck // test code |
| 746 |
} |
| 747 |
|
| 748 |
type expectedStackEntry struct { |
| 749 |
expectedDirName string |
| 750 |
expectedUnwalked []string |
| 751 |
} |
| 752 |
|
| 753 |
func testStackContents(t *testing.T, msg string, ss symlinkStack, expected ...expectedStackEntry) { |
| 754 |
if len(expected) > 0 { |
| 755 |
require.Lenf(t, ss, len(expected), "%s: stack should be the expected length", msg) |
| 756 |
require.Falsef(t, ss.IsEmpty(), "%s: stack IsEmpty should be false", msg) |
| 757 |
} else { |
| 758 |
require.Emptyf(t, ss, "%s: stack should be empty", msg) |
| 759 |
require.Truef(t, ss.IsEmpty(), "%s: stack IsEmpty should be true", msg) |
| 760 |
} |
| 761 |
|
| 762 |
for idx, entry := range expected { |
| 763 |
assert.Equalf(t, entry.expectedDirName, ss[idx].dir.Name(), "%s: stack entry %d name mismatch", msg, idx) |
| 764 |
if len(entry.expectedUnwalked) > 0 { |
| 765 |
assert.Equalf(t, entry.expectedUnwalked, ss[idx].linkUnwalked, "%s: stack entry %d unwalked link entries mismatch", msg, idx) |
| 766 |
} else { |
| 767 |
assert.Emptyf(t, ss[idx].linkUnwalked, "%s: stack entry %d unwalked link entries", msg, idx) |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
// Fail the test immediately so we can get the current stack in the test output. |
| 772 |
if t.Failed() { |
| 773 |
t.FailNow() |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
func TestSymlinkStackBasicTailChain(t *testing.T) { |
| 778 |
ss := testSymlinkStack(t, |
| 779 |
ssOp{op: ssOpSwapLink{"foo", "A", "", "tailA"}}, |
| 780 |
ssOp{op: ssOpSwapLink{"tailA", "B", "", "tailB"}}, |
| 781 |
ssOp{op: ssOpSwapLink{"tailB", "C", "", "tailC"}}, |
| 782 |
ssOp{op: ssOpSwapLink{"tailC", "D", "", "tailD"}}, |
| 783 |
ssOp{op: ssOpSwapLink{"tailD", "E", "", "foo/taillink"}}, |
| 784 |
) |
| 785 |
defer func() { |
| 786 |
if t.Failed() { |
| 787 |
dumpStack(t, ss) |
| 788 |
} |
| 789 |
}() |
| 790 |
defer ss.Close() //nolint:errcheck // test code |
| 791 |
|
| 792 |
// Basic expected contents. |
| 793 |
testStackContents(t, "initial state", ss, |
| 794 |
// The top 4 entries should have no unwalked links. |
| 795 |
expectedStackEntry{"A", nil}, |
| 796 |
expectedStackEntry{"B", nil}, |
| 797 |
expectedStackEntry{"C", nil}, |
| 798 |
expectedStackEntry{"D", nil}, |
| 799 |
// And the final entry should just be foo/taillink. |
| 800 |
expectedStackEntry{"E", []string{"foo", "taillink"}}, |
| 801 |
) |
| 802 |
|
| 803 |
// Popping "foo" should keep the tail-chain. |
| 804 |
require.NoError(t, ss.PopPart("foo"), "pop foo") |
| 805 |
testStackContents(t, "pop tail-chain end", ss, |
| 806 |
// The top 4 entries should have no unwalked links. |
| 807 |
expectedStackEntry{"A", nil}, |
| 808 |
expectedStackEntry{"B", nil}, |
| 809 |
expectedStackEntry{"C", nil}, |
| 810 |
expectedStackEntry{"D", nil}, |
| 811 |
// And the final entry should just be foo/taillink. |
| 812 |
expectedStackEntry{"E", []string{"taillink"}}, |
| 813 |
) |
| 814 |
|
| 815 |
// Dropping taillink should empty the stack. |
| 816 |
require.NoError(t, ss.PopPart("taillink"), "pop taillink") |
| 817 |
testStackContents(t, "pop last element in tail-chain", ss) |
| 818 |
assert.True(t, ss.IsEmpty(), "pop last element in tail-chain should empty chain") |
| 819 |
} |
| 820 |
|
| 821 |
func TestSymlinkStackTailChain(t *testing.T) { |
| 822 |
ss := testSymlinkStack(t, |
| 823 |
ssOp{op: ssOpSwapLink{"foo", "A", "", "tailA/subdir1"}}, |
| 824 |
// First tail-chain. |
| 825 |
ssOp{op: ssOpSwapLink{"tailA", "B", "", "tailB"}}, |
| 826 |
ssOp{op: ssOpSwapLink{"tailB", "C", "", "tailC"}}, |
| 827 |
ssOp{op: ssOpSwapLink{"tailC", "D", "", "tailD"}}, |
| 828 |
ssOp{op: ssOpSwapLink{"tailD", "E", "", "taillink1/subdir2"}}, |
| 829 |
// Second tail-chain. |
| 830 |
ssOp{op: ssOpSwapLink{"taillink1", "F", "", "tailE"}}, |
| 831 |
ssOp{op: ssOpSwapLink{"tailE", "G", "", "tailF"}}, |
| 832 |
ssOp{op: ssOpSwapLink{"tailF", "H", "", "tailG"}}, |
| 833 |
ssOp{op: ssOpSwapLink{"tailG", "I", "", "tailH"}}, |
| 834 |
ssOp{op: ssOpSwapLink{"tailH", "J", "", "tailI"}}, |
| 835 |
ssOp{op: ssOpSwapLink{"tailI", "K", "", "taillink2/.."}}, |
| 836 |
) |
| 837 |
defer func() { |
| 838 |
if t.Failed() { |
| 839 |
dumpStack(t, ss) |
| 840 |
} |
| 841 |
}() |
| 842 |
defer ss.Close() //nolint:errcheck // test code |
| 843 |
|
| 844 |
// Basic expected contents. |
| 845 |
initialState := []expectedStackEntry{ |
| 846 |
// Top entry is not a tail-chain. |
| 847 |
{"A", []string{"subdir1"}}, |
| 848 |
// The first tail-chain should have no unwalked links. |
| 849 |
{"B", nil}, |
| 850 |
{"C", nil}, |
| 851 |
{"D", nil}, |
| 852 |
// Final entry in the first tail-chain. |
| 853 |
{"E", []string{"subdir2"}}, |
| 854 |
// The second tail-chain should have no unwalked links. |
| 855 |
{"F", nil}, |
| 856 |
{"G", nil}, |
| 857 |
{"H", nil}, |
| 858 |
{"I", nil}, |
| 859 |
{"J", nil}, |
| 860 |
// Final entry in the second tail-chain. |
| 861 |
{"K", []string{"taillink2", ".."}}, |
| 862 |
} |
| 863 |
|
| 864 |
testStackContents(t, "initial state", ss, initialState...) |
| 865 |
|
| 866 |
// Trying to pop "." does nothing. |
| 867 |
for i := 0; i < 20; i++ { |
| 868 |
require.NoError(t, ss.PopPart("."), `popping "." should never fail`) |
| 869 |
// NOTE: Same contents as above. |
| 870 |
testStackContents(t, "noop pop .", ss, initialState...) |
| 871 |
} |
| 872 |
|
| 873 |
// Popping any of the early tail chain entries must fail. |
| 874 |
for _, badPart := range []string{"subdir1", "subdir2", ".."} { |
| 875 |
require.ErrorIsf(t, ss.PopPart(badPart), errBrokenSymlinkStack, "bad pop %q", badPart) |
| 876 |
// NOTE: Same contents as above. |
| 877 |
testStackContents(t, "bad pop "+badPart, ss, initialState...) |
| 878 |
} |
| 879 |
|
| 880 |
// Dropping the second-last entry should keep the tail-chain. |
| 881 |
require.NoError(t, ss.PopPart("taillink2"), "pop taillink2") |
| 882 |
testStackContents(t, "pop non-last element in second tail-chain", ss, |
| 883 |
// Top entry is not a tail-chain. |
| 884 |
expectedStackEntry{"A", []string{"subdir1"}}, |
| 885 |
// The first tail-chain should have no unwalked links. |
| 886 |
expectedStackEntry{"B", nil}, |
| 887 |
expectedStackEntry{"C", nil}, |
| 888 |
expectedStackEntry{"D", nil}, |
| 889 |
// Final entry in the first tail-chain. |
| 890 |
expectedStackEntry{"E", []string{"subdir2"}}, |
| 891 |
// The second tail-chain should have no unwalked links. |
| 892 |
expectedStackEntry{"F", nil}, |
| 893 |
expectedStackEntry{"G", nil}, |
| 894 |
expectedStackEntry{"H", nil}, |
| 895 |
expectedStackEntry{"I", nil}, |
| 896 |
expectedStackEntry{"J", nil}, |
| 897 |
// Final entry in the second tail-chain. |
| 898 |
expectedStackEntry{"K", []string{".."}}, |
| 899 |
) |
| 900 |
|
| 901 |
// Dropping the last entry should only drop the final tail-chain. |
| 902 |
require.NoError(t, ss.PopPart(".."), "pop ..") |
| 903 |
testStackContents(t, "pop last element in second tail-chain", ss, |
| 904 |
// Top entry is not a tail-chain. |
| 905 |
expectedStackEntry{"A", []string{"subdir1"}}, |
| 906 |
// The first tail-chain should have no unwalked links. |
| 907 |
expectedStackEntry{"B", nil}, |
| 908 |
expectedStackEntry{"C", nil}, |
| 909 |
expectedStackEntry{"D", nil}, |
| 910 |
// Final entry in the first tail-chain. |
| 911 |
expectedStackEntry{"E", []string{"subdir2"}}, |
| 912 |
) |
| 913 |
|
| 914 |
// Dropping the last entry should only drop the tail-chain. |
| 915 |
require.NoError(t, ss.PopPart("subdir2"), "pop subdir2") |
| 916 |
testStackContents(t, "pop last element in first tail-chain", ss, |
| 917 |
// Top entry is not a tail-chain. |
| 918 |
expectedStackEntry{"A", []string{"subdir1"}}, |
| 919 |
) |
| 920 |
|
| 921 |
// Dropping the last entry should empty the stack. |
| 922 |
require.NoError(t, ss.PopPart("subdir1"), "pop subdir1") |
| 923 |
testStackContents(t, "pop last element", ss) |
| 924 |
assert.True(t, ss.IsEmpty(), "pop last element should empty stack") |
| 925 |
} |