| 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 |
"os" |
| 16 |
"path/filepath" |
| 17 |
"runtime" |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/stretchr/testify/assert" |
| 21 |
"github.com/stretchr/testify/require" |
| 22 |
"golang.org/x/sys/unix" |
| 23 |
|
| 24 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/fd" |
| 25 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/procfs" |
| 26 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/testutils" |
| 27 |
) |
| 28 |
|
| 29 |
type openInRootFunc func(root, unsafePath string) (*os.File, error) |
| 30 |
|
| 31 |
type openResult struct { |
| 32 |
handlePath string |
| 33 |
err error |
| 34 |
fileType uint32 |
| 35 |
} |
| 36 |
|
| 37 |
// O_LARGEFILE is automatically added by the kernel when opening files on |
| 38 |
// 64-bit machines. Unfortunately, it is architecture-dependent and |
| 39 |
// unix.O_LARGEFILE is 0 (presumably to avoid users setting it). So we need to |
| 40 |
// initialise it at init. |
| 41 |
var O_LARGEFILE = 0x8000 //nolint:revive // unix.* name |
| 42 |
|
| 43 |
func init() { |
| 44 |
switch runtime.GOARCH { |
| 45 |
case "arm", "arm64": |
| 46 |
O_LARGEFILE = 0x20000 |
| 47 |
case "mips", "mips64", "mips64le", "mips64p32", "mips64p32le": |
| 48 |
O_LARGEFILE = 0x2000 |
| 49 |
case "ppc", "ppc64", "ppc64le": |
| 50 |
O_LARGEFILE = 0x10000 |
| 51 |
case "sparc", "sparc64": |
| 52 |
O_LARGEFILE = 0x40000 |
| 53 |
default: |
| 54 |
// 0x8000 is the default flag in asm-generic. |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func checkReopen(t *testing.T, handle *os.File, flags int, expectedErr error) { |
| 59 |
newHandle, err := Reopen(handle, flags) |
| 60 |
if newHandle != nil { |
| 61 |
defer newHandle.Close() //nolint:errcheck // test code |
| 62 |
} |
| 63 |
if expectedErr != nil { |
| 64 |
if assert.Error(t, err) { |
| 65 |
require.ErrorIs(t, err, expectedErr) |
| 66 |
} else { |
| 67 |
t.Errorf("unexpected handle %q", handle.Name()) |
| 68 |
} |
| 69 |
return |
| 70 |
} |
| 71 |
require.NoError(t, err) |
| 72 |
|
| 73 |
// Get the original handle path. |
| 74 |
handlePath, err := procfs.ProcSelfFdReadlink(handle) |
| 75 |
require.NoError(t, err, "get real path of original handle") |
| 76 |
// Make sure the handle matches the readlink path. |
| 77 |
assert.Equal(t, handlePath, handle.Name(), "handle.Name() matching real original handle path") |
| 78 |
|
| 79 |
// Check that the new and old handle have the same path. |
| 80 |
newHandlePath, err := procfs.ProcSelfFdReadlink(newHandle) |
| 81 |
require.NoError(t, err, "get real path of reopened handle") |
| 82 |
assert.Equal(t, handlePath, newHandlePath, "old and reopen handle paths") |
| 83 |
assert.Equal(t, handle.Name(), newHandle.Name(), "old and reopen handle.Name()") |
| 84 |
|
| 85 |
// Check the fd flags. |
| 86 |
newHandleFdFlags, err := unix.FcntlInt(newHandle.Fd(), unix.F_GETFD, 0) |
| 87 |
require.NoError(t, err, "fcntl(F_GETFD)") |
| 88 |
assert.Equal(t, unix.FD_CLOEXEC, newHandleFdFlags&unix.FD_CLOEXEC, "FD_CLOEXEC flag must be set") |
| 89 |
|
| 90 |
// Check the file handle flags. |
| 91 |
newHandleStatusFlags, err := unix.FcntlInt(newHandle.Fd(), unix.F_GETFL, 0) |
| 92 |
require.NoError(t, err, "fcntl(F_GETFL)") |
| 93 |
flags &^= unix.O_CLOEXEC // O_CLOEXEC is checked by F_GETFD |
| 94 |
newHandleStatusFlags &^= O_LARGEFILE // ignore the O_LARGEFILE flag |
| 95 |
assert.Equal(t, flags, newHandleStatusFlags, "re-opened handle status flags must match re-open flags (%+x)") |
| 96 |
} |
| 97 |
|
| 98 |
func checkOpenInRoot(t *testing.T, openInRootFn openInRootFunc, root, unsafePath string, expected openResult) { |
| 99 |
handle, err := openInRootFn(root, unsafePath) |
| 100 |
if handle != nil { |
| 101 |
defer handle.Close() //nolint:errcheck // test code |
| 102 |
} |
| 103 |
if expected.err != nil { |
| 104 |
if assert.Error(t, err) { |
| 105 |
require.ErrorIs(t, err, expected.err) |
| 106 |
} else { |
| 107 |
t.Errorf("unexpected handle %q", handle.Name()) |
| 108 |
} |
| 109 |
return |
| 110 |
} |
| 111 |
require.NoError(t, err) |
| 112 |
|
| 113 |
// Check the handle path. |
| 114 |
gotPath, err := procfs.ProcSelfFdReadlink(handle) |
| 115 |
require.NoError(t, err, "get real path of returned handle") |
| 116 |
assert.Equal(t, expected.handlePath, gotPath, "real handle path") |
| 117 |
// Make sure the handle matches the readlink path. |
| 118 |
assert.Equal(t, gotPath, handle.Name(), "handle.Name() matching real handle path") |
| 119 |
|
| 120 |
// Check the handle type. |
| 121 |
unixStat, err := fd.Fstat(handle) |
| 122 |
require.NoError(t, err, "fstat handle") |
| 123 |
assert.Equal(t, expected.fileType, unixStat.Mode&unix.S_IFMT, "handle S_IFMT type") |
| 124 |
|
| 125 |
// Check that re-opening produces a handle with the same path. |
| 126 |
switch expected.fileType { |
| 127 |
case unix.S_IFDIR: |
| 128 |
checkReopen(t, handle, unix.O_RDONLY, nil) |
| 129 |
checkReopen(t, handle, unix.O_DIRECTORY, nil) |
| 130 |
case unix.S_IFREG: |
| 131 |
checkReopen(t, handle, unix.O_RDWR, nil) |
| 132 |
checkReopen(t, handle, unix.O_DIRECTORY, unix.ENOTDIR) |
| 133 |
// Only files and directories are safe to open this way. Use O_PATH for |
| 134 |
// everything else. |
| 135 |
default: |
| 136 |
checkReopen(t, handle, unix.O_PATH, nil) |
| 137 |
checkReopen(t, handle, unix.O_PATH|unix.O_DIRECTORY, unix.ENOTDIR) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func testOpenInRoot(t *testing.T, openInRootFn openInRootFunc) { |
| 142 |
tree := []string{ |
| 143 |
"dir a", |
| 144 |
"dir b/c/d/e/f", |
| 145 |
"file b/c/file", |
| 146 |
"symlink e /b/c/d/e", |
| 147 |
"symlink b-file b/c/file", |
| 148 |
// Dangling symlinks. |
| 149 |
"symlink a-fake1 a/fake", |
| 150 |
"symlink a-fake2 a/fake/foo/bar/..", |
| 151 |
"symlink a-fake3 a/fake/../../b", |
| 152 |
"dir c", |
| 153 |
"symlink c/a-fake1 a/fake", |
| 154 |
"symlink c/a-fake2 a/fake/foo/bar/..", |
| 155 |
"symlink c/a-fake3 a/fake/../../b", |
| 156 |
// Test non-lexical symlinks. |
| 157 |
"dir target", |
| 158 |
"dir link1", |
| 159 |
"symlink link1/target_abs /target", |
| 160 |
"symlink link1/target_rel ../target", |
| 161 |
"dir link2", |
| 162 |
"symlink link2/link1_abs /link1", |
| 163 |
"symlink link2/link1_rel ../link1", |
| 164 |
"dir link3", |
| 165 |
"symlink link3/target_abs /link2/link1_rel/target_rel", |
| 166 |
"symlink link3/target_rel ../link2/link1_rel/target_rel", |
| 167 |
"symlink link3/deep_dangling1 ../link2/link1_rel/target_rel/nonexist", |
| 168 |
"symlink link3/deep_dangling2 ../link2/link1_rel/target_rel/nonexist", |
| 169 |
// Deep dangling symlinks (with single components). |
| 170 |
"dir dangling", |
| 171 |
"symlink dangling/a b/c", |
| 172 |
"dir dangling/b", |
| 173 |
"symlink dangling/b/c ../c", |
| 174 |
"symlink dangling/c d/e", |
| 175 |
"dir dangling/d", |
| 176 |
"symlink dangling/d/e ../e", |
| 177 |
"symlink dangling/e f/../g", |
| 178 |
"dir dangling/f", |
| 179 |
"symlink dangling/g h/i/j/nonexistent", |
| 180 |
"dir dangling/h/i/j", |
| 181 |
// Deep dangling symlink using a non-dir component. |
| 182 |
"dir dangling-file", |
| 183 |
"symlink dangling-file/a b/c", |
| 184 |
"dir dangling-file/b", |
| 185 |
"symlink dangling-file/b/c ../c", |
| 186 |
"symlink dangling-file/c d/e", |
| 187 |
"dir dangling-file/d", |
| 188 |
"symlink dangling-file/d/e ../e", |
| 189 |
"symlink dangling-file/e f/../g", |
| 190 |
"dir dangling-file/f", |
| 191 |
"symlink dangling-file/g h/i/j/file/foo", |
| 192 |
"dir dangling-file/h/i/j", |
| 193 |
"file dangling-file/h/i/j/file", |
| 194 |
// Some "bad" inodes that a regular user can create. |
| 195 |
"fifo b/fifo", |
| 196 |
"sock b/sock", |
| 197 |
// Symlink loops. |
| 198 |
"dir loop", |
| 199 |
"symlink loop/basic-loop1 basic-loop1", |
| 200 |
"symlink loop/basic-loop2 /loop/basic-loop2", |
| 201 |
"symlink loop/basic-loop3 ../loop/basic-loop3", |
| 202 |
"dir loop/a", |
| 203 |
"symlink loop/a/link ../b/link", |
| 204 |
"dir loop/b", |
| 205 |
"symlink loop/b/link /loop/c/link", |
| 206 |
"dir loop/c", |
| 207 |
"symlink loop/c/link /loop/d/link", |
| 208 |
"symlink loop/d e", |
| 209 |
"dir loop/e", |
| 210 |
"symlink loop/e/link ../a/link", |
| 211 |
"symlink loop/link a/link", |
| 212 |
} |
| 213 |
|
| 214 |
root := testutils.CreateTree(t, tree...) |
| 215 |
|
| 216 |
for name, test := range map[string]struct { |
| 217 |
unsafePath string |
| 218 |
expected openResult |
| 219 |
}{ |
| 220 |
// Complete lookups. |
| 221 |
"complete-dir1": {"a", openResult{handlePath: "/a", fileType: unix.S_IFDIR}}, |
| 222 |
"complete-dir2": {"b/c/d/e/f", openResult{handlePath: "/b/c/d/e/f", fileType: unix.S_IFDIR}}, |
| 223 |
"complete-dir3": {"b///././c////.//d/./././///e////.//./f//././././", openResult{handlePath: "/b/c/d/e/f", fileType: unix.S_IFDIR}}, |
| 224 |
"complete-file": {"b/c/file", openResult{handlePath: "/b/c/file", fileType: unix.S_IFREG}}, |
| 225 |
"complete-file-link": {"b-file", openResult{handlePath: "/b/c/file", fileType: unix.S_IFREG}}, |
| 226 |
"complete-fifo": {"b/fifo", openResult{handlePath: "/b/fifo", fileType: unix.S_IFIFO}}, |
| 227 |
"complete-sock": {"b/sock", openResult{handlePath: "/b/sock", fileType: unix.S_IFSOCK}}, |
| 228 |
// Partial lookups. |
| 229 |
"partial-dir-basic": {"a/b/c/d/e/f/g/h", openResult{err: unix.ENOENT}}, |
| 230 |
"partial-dir-dotdot": {"a/foo/../bar/baz", openResult{err: unix.ENOENT}}, |
| 231 |
// Complete lookups of non-lexical symlinks. |
| 232 |
"nonlexical-basic-complete1": {"target", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 233 |
"nonlexical-basic-complete2": {"target/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 234 |
"nonlexical-basic-complete3": {"target//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 235 |
"nonlexical-basic-partial": {"target/foo", openResult{err: unix.ENOENT}}, |
| 236 |
"nonlexical-basic-partial-dotdot": {"target/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 237 |
"nonlexical-level1-abs-complete1": {"link1/target_abs", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 238 |
"nonlexical-level1-abs-complete2": {"link1/target_abs/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 239 |
"nonlexical-level1-abs-complete3": {"link1/target_abs//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 240 |
"nonlexical-level1-abs-partial": {"link1/target_abs/foo", openResult{err: unix.ENOENT}}, |
| 241 |
"nonlexical-level1-abs-partial-dotdot": {"link1/target_abs/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 242 |
"nonlexical-level1-rel-complete1": {"link1/target_rel", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 243 |
"nonlexical-level1-rel-complete2": {"link1/target_rel/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 244 |
"nonlexical-level1-rel-complete3": {"link1/target_rel//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 245 |
"nonlexical-level1-rel-partial": {"link1/target_rel/foo", openResult{err: unix.ENOENT}}, |
| 246 |
"nonlexical-level1-rel-partial-dotdot": {"link1/target_rel/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 247 |
"nonlexical-level2-abs-abs-complete1": {"link2/link1_abs/target_abs", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 248 |
"nonlexical-level2-abs-abs-complete2": {"link2/link1_abs/target_abs/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 249 |
"nonlexical-level2-abs-abs-complete3": {"link2/link1_abs/target_abs//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 250 |
"nonlexical-level2-abs-abs-partial": {"link2/link1_abs/target_abs/foo", openResult{err: unix.ENOENT}}, |
| 251 |
"nonlexical-level2-abs-abs-partial-dotdot": {"link2/link1_abs/target_abs/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 252 |
"nonlexical-level2-abs-rel-complete1": {"link2/link1_abs/target_rel", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 253 |
"nonlexical-level2-abs-rel-complete2": {"link2/link1_abs/target_rel/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 254 |
"nonlexical-level2-abs-rel-complete3": {"link2/link1_abs/target_rel//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 255 |
"nonlexical-level2-abs-rel-partial": {"link2/link1_abs/target_rel/foo", openResult{err: unix.ENOENT}}, |
| 256 |
"nonlexical-level2-abs-rel-partial-dotdot": {"link2/link1_abs/target_rel/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 257 |
"nonlexical-level2-abs-open-complete1": {"link2/link1_abs/../target", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 258 |
"nonlexical-level2-abs-open-complete2": {"link2/link1_abs/../target/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 259 |
"nonlexical-level2-abs-open-complete3": {"link2/link1_abs/../target//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 260 |
"nonlexical-level2-abs-open-partial": {"link2/link1_abs/../target/foo", openResult{err: unix.ENOENT}}, |
| 261 |
"nonlexical-level2-abs-open-partial-dotdot": {"link2/link1_abs/../target/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 262 |
"nonlexical-level2-rel-abs-complete1": {"link2/link1_rel/target_abs", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 263 |
"nonlexical-level2-rel-abs-complete2": {"link2/link1_rel/target_abs/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 264 |
"nonlexical-level2-rel-abs-complete3": {"link2/link1_rel/target_abs//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 265 |
"nonlexical-level2-rel-abs-partial": {"link2/link1_rel/target_abs/foo", openResult{err: unix.ENOENT}}, |
| 266 |
"nonlexical-level2-rel-abs-partial-dotdot": {"link2/link1_rel/target_abs/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 267 |
"nonlexical-level2-rel-rel-complete1": {"link2/link1_rel/target_rel", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 268 |
"nonlexical-level2-rel-rel-complete2": {"link2/link1_rel/target_rel/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 269 |
"nonlexical-level2-rel-rel-complete3": {"link2/link1_rel/target_rel//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 270 |
"nonlexical-level2-rel-rel-partial": {"link2/link1_rel/target_rel/foo", openResult{err: unix.ENOENT}}, |
| 271 |
"nonlexical-level2-rel-rel-partial-dotdot": {"link2/link1_rel/target_rel/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 272 |
"nonlexical-level2-rel-open-complete1": {"link2/link1_rel/../target", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 273 |
"nonlexical-level2-rel-open-complete2": {"link2/link1_rel/../target/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 274 |
"nonlexical-level2-rel-open-complete3": {"link2/link1_rel/../target//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 275 |
"nonlexical-level2-rel-open-partial": {"link2/link1_rel/../target/foo", openResult{err: unix.ENOENT}}, |
| 276 |
"nonlexical-level2-rel-open-partial-dotdot": {"link2/link1_rel/../target/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 277 |
"nonlexical-level3-abs-complete1": {"link3/target_abs", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 278 |
"nonlexical-level3-abs-complete2": {"link3/target_abs/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 279 |
"nonlexical-level3-abs-complete3": {"link3/target_abs//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 280 |
"nonlexical-level3-abs-partial": {"link3/target_abs/foo", openResult{err: unix.ENOENT}}, |
| 281 |
"nonlexical-level3-abs-partial-dotdot": {"link3/target_abs/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 282 |
"nonlexical-level3-rel-complete1": {"link3/target_rel", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 283 |
"nonlexical-level3-rel-complete2": {"link3/target_rel/", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 284 |
"nonlexical-level3-rel-complete3": {"link3/target_rel//", openResult{handlePath: "/target", fileType: unix.S_IFDIR}}, |
| 285 |
"nonlexical-level3-rel-partial": {"link3/target_rel/foo", openResult{err: unix.ENOENT}}, |
| 286 |
"nonlexical-level3-rel-partial-dotdot": {"link3/target_rel/../target/foo/bar/../baz", openResult{err: unix.ENOENT}}, |
| 287 |
// Partial lookups due to hitting a non-directory. |
| 288 |
"partial-nondir-slash1": {"b/c/file/", openResult{err: unix.ENOTDIR}}, |
| 289 |
"partial-nondir-slash2": {"b/c/file//", openResult{err: unix.ENOTDIR}}, |
| 290 |
"partial-nondir-dot": {"b/c/file/.", openResult{err: unix.ENOTDIR}}, |
| 291 |
"partial-nondir-dotdot1": {"b/c/file/..", openResult{err: unix.ENOTDIR}}, |
| 292 |
"partial-nondir-dotdot2": {"b/c/file/../foo/bar", openResult{err: unix.ENOTDIR}}, |
| 293 |
"partial-nondir-symlink-slash1": {"b-file/", openResult{err: unix.ENOTDIR}}, |
| 294 |
"partial-nondir-symlink-slash2": {"b-file//", openResult{err: unix.ENOTDIR}}, |
| 295 |
"partial-nondir-symlink-dot": {"b-file/.", openResult{err: unix.ENOTDIR}}, |
| 296 |
"partial-nondir-symlink-dotdot1": {"b-file/..", openResult{err: unix.ENOTDIR}}, |
| 297 |
"partial-nondir-symlink-dotdot2": {"b-file/../foo/bar", openResult{err: unix.ENOTDIR}}, |
| 298 |
"partial-fifo-slash1": {"b/fifo/", openResult{err: unix.ENOTDIR}}, |
| 299 |
"partial-fifo-slash2": {"b/fifo//", openResult{err: unix.ENOTDIR}}, |
| 300 |
"partial-fifo-dot": {"b/fifo/.", openResult{err: unix.ENOTDIR}}, |
| 301 |
"partial-fifo-dotdot1": {"b/fifo/..", openResult{err: unix.ENOTDIR}}, |
| 302 |
"partial-fifo-dotdot2": {"b/fifo/../foo/bar", openResult{err: unix.ENOTDIR}}, |
| 303 |
"partial-sock-slash1": {"b/sock/", openResult{err: unix.ENOTDIR}}, |
| 304 |
"partial-sock-slash2": {"b/sock//", openResult{err: unix.ENOTDIR}}, |
| 305 |
"partial-sock-dot": {"b/sock/.", openResult{err: unix.ENOTDIR}}, |
| 306 |
"partial-sock-dotdot1": {"b/sock/..", openResult{err: unix.ENOTDIR}}, |
| 307 |
"partial-sock-dotdot2": {"b/sock/../foo/bar", openResult{err: unix.ENOTDIR}}, |
| 308 |
// Dangling symlinks are treated as though they are non-existent. |
| 309 |
"dangling1-inroot-trailing": {"a-fake1", openResult{err: unix.ENOENT}}, |
| 310 |
"dangling1-inroot-partial": {"a-fake1/foo", openResult{err: unix.ENOENT}}, |
| 311 |
"dangling1-inroot-partial-dotdot": {"a-fake1/../bar/baz", openResult{err: unix.ENOENT}}, |
| 312 |
"dangling1-sub-trailing": {"c/a-fake1", openResult{err: unix.ENOENT}}, |
| 313 |
"dangling1-sub-partial": {"c/a-fake1/foo", openResult{err: unix.ENOENT}}, |
| 314 |
"dangling1-sub-partial-dotdot": {"c/a-fake1/../bar/baz", openResult{err: unix.ENOENT}}, |
| 315 |
"dangling2-inroot-trailing": {"a-fake2", openResult{err: unix.ENOENT}}, |
| 316 |
"dangling2-inroot-partial": {"a-fake2/foo", openResult{err: unix.ENOENT}}, |
| 317 |
"dangling2-inroot-partial-dotdot": {"a-fake2/../bar/baz", openResult{err: unix.ENOENT}}, |
| 318 |
"dangling2-sub-trailing": {"c/a-fake2", openResult{err: unix.ENOENT}}, |
| 319 |
"dangling2-sub-partial": {"c/a-fake2/foo", openResult{err: unix.ENOENT}}, |
| 320 |
"dangling2-sub-partial-dotdot": {"c/a-fake2/../bar/baz", openResult{err: unix.ENOENT}}, |
| 321 |
"dangling3-inroot-trailing": {"a-fake3", openResult{err: unix.ENOENT}}, |
| 322 |
"dangling3-inroot-partial": {"a-fake3/foo", openResult{err: unix.ENOENT}}, |
| 323 |
"dangling3-inroot-partial-dotdot": {"a-fake3/../bar/baz", openResult{err: unix.ENOENT}}, |
| 324 |
"dangling3-sub-trailing": {"c/a-fake3", openResult{err: unix.ENOENT}}, |
| 325 |
"dangling3-sub-partial": {"c/a-fake3/foo", openResult{err: unix.ENOENT}}, |
| 326 |
"dangling3-sub-partial-dotdot": {"c/a-fake3/../bar/baz", openResult{err: unix.ENOENT}}, |
| 327 |
// Tricky dangling symlinks. |
| 328 |
"dangling-tricky1-trailing": {"link3/deep_dangling1", openResult{err: unix.ENOENT}}, |
| 329 |
"dangling-tricky1-partial": {"link3/deep_dangling1/foo", openResult{err: unix.ENOENT}}, |
| 330 |
"dangling-tricky1-partial-dotdot": {"link3/deep_dangling1/..", openResult{err: unix.ENOENT}}, |
| 331 |
"dangling-tricky2-trailing": {"link3/deep_dangling2", openResult{err: unix.ENOENT}}, |
| 332 |
"dangling-tricky2-partial": {"link3/deep_dangling2/foo", openResult{err: unix.ENOENT}}, |
| 333 |
"dangling-tricky2-partial-dotdot": {"link3/deep_dangling2/..", openResult{err: unix.ENOENT}}, |
| 334 |
// Really deep dangling links. |
| 335 |
"deep-dangling1": {"dangling/a", openResult{err: unix.ENOENT}}, |
| 336 |
"deep-dangling2": {"dangling/b/c", openResult{err: unix.ENOENT}}, |
| 337 |
"deep-dangling3": {"dangling/c", openResult{err: unix.ENOENT}}, |
| 338 |
"deep-dangling4": {"dangling/d/e", openResult{err: unix.ENOENT}}, |
| 339 |
"deep-dangling5": {"dangling/e", openResult{err: unix.ENOENT}}, |
| 340 |
"deep-dangling6": {"dangling/g", openResult{err: unix.ENOENT}}, |
| 341 |
"deep-dangling-fileasdir1": {"dangling-file/a", openResult{err: unix.ENOTDIR}}, |
| 342 |
"deep-dangling-fileasdir2": {"dangling-file/b/c", openResult{err: unix.ENOTDIR}}, |
| 343 |
"deep-dangling-fileasdir3": {"dangling-file/c", openResult{err: unix.ENOTDIR}}, |
| 344 |
"deep-dangling-fileasdir4": {"dangling-file/d/e", openResult{err: unix.ENOTDIR}}, |
| 345 |
"deep-dangling-fileasdir5": {"dangling-file/e", openResult{err: unix.ENOTDIR}}, |
| 346 |
"deep-dangling-fileasdir6": {"dangling-file/g", openResult{err: unix.ENOTDIR}}, |
| 347 |
// Symlink loops. |
| 348 |
"loop": {"loop/link", openResult{err: unix.ELOOP}}, |
| 349 |
"loop-basic1": {"loop/basic-loop1", openResult{err: unix.ELOOP}}, |
| 350 |
"loop-basic2": {"loop/basic-loop2", openResult{err: unix.ELOOP}}, |
| 351 |
"loop-basic3": {"loop/basic-loop3", openResult{err: unix.ELOOP}}, |
| 352 |
} { |
| 353 |
test := test // copy iterator |
| 354 |
// Update the handlePath to be inside our root. |
| 355 |
if test.expected.handlePath != "" { |
| 356 |
test.expected.handlePath = filepath.Join(root, test.expected.handlePath) |
| 357 |
} |
| 358 |
t.Run(name, func(t *testing.T) { |
| 359 |
checkOpenInRoot(t, openInRootFn, root, test.unsafePath, test.expected) |
| 360 |
}) |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
func TestOpenInRoot(t *testing.T) { |
| 365 |
testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) { |
| 366 |
t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code |
| 367 |
testOpenInRoot(t, OpenInRoot) |
| 368 |
}) |
| 369 |
} |
| 370 |
|
| 371 |
func TestOpenInRootHandle(t *testing.T) { |
| 372 |
testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) { |
| 373 |
t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code |
| 374 |
testOpenInRoot(t, func(root, unsafePath string) (*os.File, error) { |
| 375 |
rootDir, err := os.OpenFile(root, unix.O_PATH|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| 376 |
if err != nil { |
| 377 |
return nil, err |
| 378 |
} |
| 379 |
defer rootDir.Close() //nolint:errcheck // test code |
| 380 |
|
| 381 |
return OpenatInRoot(rootDir, unsafePath) |
| 382 |
}) |
| 383 |
}) |
| 384 |
} |
| 385 |
|
| 386 |
func TestOpenInRoot_BadInode(t *testing.T) { //nolint:revive // underscores are more readable for test helpers |
| 387 |
testutils.RequireRoot(t) // mknod |
| 388 |
|
| 389 |
testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) { |
| 390 |
t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code |
| 391 |
tree := []string{ |
| 392 |
// Make sure we don't open "bad" inodes. |
| 393 |
"dir foo", |
| 394 |
"char foo/whiteout 0 0", |
| 395 |
"block foo/whiteout-blk 0 0", |
| 396 |
} |
| 397 |
|
| 398 |
root := testutils.CreateTree(t, tree...) |
| 399 |
|
| 400 |
rootDir, err := os.OpenFile(root, unix.O_PATH|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| 401 |
require.NoError(t, err) |
| 402 |
defer rootDir.Close() //nolint:errcheck // test code |
| 403 |
|
| 404 |
for name, test := range map[string]struct { |
| 405 |
unsafePath string |
| 406 |
expected openResult |
| 407 |
}{ |
| 408 |
// Complete lookups. |
| 409 |
"char-trailing": {"foo/whiteout", openResult{handlePath: "/foo/whiteout", fileType: unix.S_IFCHR}}, |
| 410 |
"blk-trailing": {"foo/whiteout-blk", openResult{handlePath: "/foo/whiteout-blk", fileType: unix.S_IFBLK}}, |
| 411 |
// Partial lookups due to hitting a non-directory. |
| 412 |
"char-dot": {"foo/whiteout/.", openResult{err: unix.ENOTDIR}}, |
| 413 |
"char-dotdot1": {"foo/whiteout/..", openResult{err: unix.ENOTDIR}}, |
| 414 |
"char-dotdot2": {"foo/whiteout/../foo/bar", openResult{err: unix.ENOTDIR}}, |
| 415 |
"blk-dot": {"foo/whiteout-blk/.", openResult{err: unix.ENOTDIR}}, |
| 416 |
"blk-dotdot1": {"foo/whiteout-blk/..", openResult{err: unix.ENOTDIR}}, |
| 417 |
"blk-dotdot2": {"foo/whiteout-blk/../foo/bar", openResult{err: unix.ENOTDIR}}, |
| 418 |
} { |
| 419 |
test := test // copy iterator |
| 420 |
// Update the handlePath to be inside our root. |
| 421 |
if test.expected.handlePath != "" { |
| 422 |
test.expected.handlePath = filepath.Join(root, test.expected.handlePath) |
| 423 |
} |
| 424 |
t.Run(name, func(t *testing.T) { |
| 425 |
checkOpenInRoot(t, OpenInRoot, root, test.unsafePath, test.expected) |
| 426 |
}) |
| 427 |
} |
| 428 |
}) |
| 429 |
} |