1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#!/usr/bin/bats -t
# SPDX-License-Identifier: MPL-2.0
#
# libpathrs: safe path resolution on Linux
# Copyright (C) 2019-2025 Aleksa Sarai <cyphar@cyphar.com>
# Copyright (C) 2019-2025 SUSE LLC
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
load helpers
function setup() {
setup_tmpdirs
}
function teardown() {
teardown_tmpdirs
}
@test "root symlink" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/etc"
echo "passwd" >"$ROOT/etc/passwd"
pathrs-cmd root --root "$ROOT" symlink /etc/passwd passwd-link
[ "$status" -eq 0 ]
[ -f "$ROOT/etc/passwd" ]
[ -L "$ROOT/passwd-link" ]
sane_run readlink "$ROOT/passwd-link"
[ "$status" -eq 0 ]
[[ "$output" == "/etc/passwd" ]]
pathrs-cmd root --root "$ROOT" readlink /passwd-link
[ "$status" -eq 0 ]
grep -Fx 'LINK-TARGET /etc/passwd' <<<"$output"
}
@test "root symlink [no-clobber file]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/etc"
touch "$ROOT/link"
ino="$(stat -c '%i' "$ROOT/link")"
pathrs-cmd root --root "$ROOT" symlink ../target link
check-errno EEXIST
[ -f "$ROOT/link" ]
sane_run stat -c '%i' "$ROOT/link"
[[ "$output" == "$ino" ]]
}
@test "root symlink [no-clobber symlink]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/etc"
touch "$ROOT/new"
ln -s /old/link "$ROOT/link"
pathrs-cmd root --root "$ROOT" symlink /new link
check-errno EEXIST
[ -L "$ROOT/link" ]
sane_run readlink "$ROOT/link"
[ "$status" -eq 0 ]
[[ "$output" == "/old/link" ]]
pathrs-cmd root --root "$ROOT" readlink /link
[ "$status" -eq 0 ]
grep -Fx 'LINK-TARGET /old/link' <<<"$output"
}
@test "root symlink [directory]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/foo/bar/baz"
pathrs-cmd root --root "$ROOT" symlink ../foo/bar link
[ "$status" -eq 0 ]
[ -d "$ROOT/foo/bar" ]
[ -L "$ROOT/link" ]
sane_run readlink "$ROOT/link"
[ "$status" -eq 0 ]
[[ "$output" == "../foo/bar" ]]
pathrs-cmd root --root "$ROOT" readlink link
[ "$status" -eq 0 ]
grep -Fx 'LINK-TARGET ../foo/bar' <<<"$output"
}
@test "root symlink [symlink]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/foo"
touch "$ROOT/foo/bar"
ln -s /foo/bar "$ROOT/target-link"
pathrs-cmd root --root "$ROOT" symlink target-link link
[ "$status" -eq 0 ]
[ -f "$ROOT/foo/bar" ]
[ -L "$ROOT/target-link" ]
[ -L "$ROOT/link" ]
sane_run readlink "$ROOT/link"
[ "$status" -eq 0 ]
[[ "$output" == "target-link" ]]
pathrs-cmd root --root "$ROOT" readlink link
[ "$status" -eq 0 ]
grep -Fx 'LINK-TARGET target-link' <<<"$output"
}
@test "root symlink [non-existent target]" {
ROOT="$(setup_tmpdir)"
pathrs-cmd root --root "$ROOT" symlink ../..//some/dummy/path link
[ "$status" -eq 0 ]
! [ -e "$ROOT/some/dummy/path" ]
[ -L "$ROOT/link" ]
sane_run readlink "$ROOT/link"
[ "$status" -eq 0 ]
[[ "$output" == "../..//some/dummy/path" ]]
pathrs-cmd root --root "$ROOT" readlink link
[ "$status" -eq 0 ]
grep -Fx 'LINK-TARGET ../..//some/dummy/path' <<<"$output"
}
@test "root symlink [non-existent parent component]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/foo/bar/baz"
mkdir -p "$ROOT/etc"
echo "passwd" >"$ROOT/etc/passwd"
pathrs-cmd root --root "$ROOT" symlink /etc/passwd foo/nope/baz
check-errno ENOENT
! [ -e "$ROOT/foo/nope" ]
! [ -e "$ROOT/foo/nope/baz" ]
}
@test "root symlink [bad parent component]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/foo"
touch "$ROOT/foo/bar"
mkdir -p "$ROOT/etc"
echo "passwd" >"$ROOT/etc/passwd"
pathrs-cmd root --root "$ROOT" symlink /etc/passwd foo/bar/baz
check-errno ENOTDIR
[ -f "$ROOT/foo/bar" ]
! [ -e "$ROOT/foo/bar/baz" ]
}
@test "root hardlink" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/etc"
echo "passwd" >"$ROOT/etc/passwd"
pathrs-cmd root --root "$ROOT" hardlink /etc/passwd passwd-link
[ "$status" -eq 0 ]
[ -f "$ROOT/etc/passwd" ]
[ -f "$ROOT/passwd-link" ]
sane_run readlink "$ROOT/passwd-link"
[ "$status" -ne 0 ] # not a symlink!
# Hardlinks have the same inode.
sane_run stat -c '%i' "$ROOT/etc/passwd"
target_ino="$output"
sane_run stat -c '%i' "$ROOT/passwd-link"
link_ino="$output"
[[ "$target_ino" == "$link_ino" ]]
}
@test "root hardlink [no-clobber file]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/etc"
touch "$ROOT/target"
touch "$ROOT/link"
ino="$(stat -c '%i' "$ROOT/link")"
pathrs-cmd root --root "$ROOT" hardlink target link
check-errno EEXIST
[ -f "$ROOT/link" ]
sane_run stat -c '%i' "$ROOT/link"
[[ "$output" == "$ino" ]]
}
@test "root hardlink [no-clobber symlink]" {
ROOT="$(setup_tmpdir)"
touch "$ROOT/foobar"
touch "$ROOT/target"
ln -s /foobar "$ROOT/link"
ino="$(stat -c '%i' "$ROOT/link")"
pathrs-cmd root --root "$ROOT" hardlink /target link
check-errno EEXIST
[ -L "$ROOT/link" ]
sane_run stat -c '%i' "$ROOT/link"
[[ "$output" == "$ino" ]]
}
@test "root hardlink [directory]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/foo/bar/baz"
pathrs-cmd root --root "$ROOT" hardlink /foo/bar link
check-errno EPERM
[ -d "$ROOT/foo/bar" ]
! [ -e "$ROOT/link" ]
}
@test "root hardlink [non-existent target]" {
ROOT="$(setup_tmpdir)"
pathrs-cmd root --root "$ROOT" hardlink ../..//some/dummy/path link
check-errno ENOENT
}
@test "root hardlink [non-existent parent component]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/foo/bar/baz"
mkdir -p "$ROOT/etc"
echo "passwd" >"$ROOT/etc/passwd"
pathrs-cmd root --root "$ROOT" hardlink /etc/passwd foo/nope/baz
check-errno ENOENT
! [ -e "$ROOT/foo/nope" ]
! [ -e "$ROOT/foo/nope/baz" ]
}
@test "root hardlink [bad parent component]" {
ROOT="$(setup_tmpdir)"
mkdir -p "$ROOT/foo"
touch "$ROOT/foo/bar"
mkdir -p "$ROOT/etc"
echo "passwd" >"$ROOT/etc/passwd"
pathrs-cmd root --root "$ROOT" hardlink /etc/passwd foo/bar/baz
check-errno ENOTDIR
[ -f "$ROOT/foo/bar" ]
! [ -e "$ROOT/foo/bar/baz" ]
}
|