| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "argv-util.h" |
| 8 |
#include "chase.h" |
| 9 |
#include "dirent-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "fs-util.h" |
| 12 |
#include "id128-util.h" |
| 13 |
#include "mkdir.h" |
| 14 |
#include "path-util.h" |
| 15 |
#include "random-util.h" |
| 16 |
#include "rm-rf.h" |
| 17 |
#include "stat-util.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "tests.h" |
| 20 |
#include "tmpfile-util.h" |
| 21 |
#include "user-util.h" |
| 22 |
|
| 23 |
static const char *arg_test_dir = NULL; |
| 24 |
|
| 25 |
static void test_chase_extract_filename_one(const char *path, const char *root, const char *expected) { |
| 26 |
_cleanup_free_ char *ret1 = NULL, *ret2 = NULL, *fname = NULL; |
| 27 |
|
| 28 |
log_debug("/* %s(path=%s, root=%s) */", __func__, path, strnull(root)); |
| 29 |
|
| 30 |
ASSERT_OK_POSITIVE(chase(path, root, CHASE_EXTRACT_FILENAME, &ret1, NULL)); |
| 31 |
ASSERT_STREQ(ret1, expected); |
| 32 |
|
| 33 |
ASSERT_OK_POSITIVE(chase(path, root, 0, &ret2, NULL)); |
| 34 |
ASSERT_OK(chase_extract_filename(ret2, root, &fname)); |
| 35 |
ASSERT_STREQ(fname, expected); |
| 36 |
} |
| 37 |
|
| 38 |
TEST(chase) { |
| 39 |
_cleanup_free_ char *result = NULL, *pwd = NULL; |
| 40 |
_cleanup_close_ int pfd = -EBADF; |
| 41 |
char *temp; |
| 42 |
const char *top, *p, *pslash, *q, *qslash; |
| 43 |
struct stat st; |
| 44 |
|
| 45 |
temp = strjoina(arg_test_dir ?: "/tmp", "/test-chase.XXXXXX"); |
| 46 |
ASSERT_NOT_NULL(mkdtemp(temp)); |
| 47 |
|
| 48 |
top = strjoina(temp, "/top"); |
| 49 |
ASSERT_OK(mkdir(top, 0700)); |
| 50 |
|
| 51 |
p = strjoina(top, "/dot"); |
| 52 |
if (symlink(".", p) < 0) { |
| 53 |
ASSERT_TRUE(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM)); |
| 54 |
log_tests_skipped_errno(errno, "symlink() not possible"); |
| 55 |
goto cleanup; |
| 56 |
}; |
| 57 |
|
| 58 |
p = strjoina(top, "/dotdot"); |
| 59 |
ASSERT_OK_ERRNO(symlink("..", p)); |
| 60 |
|
| 61 |
p = strjoina(top, "/dotdota"); |
| 62 |
ASSERT_OK_ERRNO(symlink("../a", p)); |
| 63 |
|
| 64 |
p = strjoina(temp, "/a"); |
| 65 |
ASSERT_OK_ERRNO(symlink("b", p)); |
| 66 |
|
| 67 |
p = strjoina(temp, "/b"); |
| 68 |
ASSERT_OK_ERRNO(symlink("/usr", p)); |
| 69 |
|
| 70 |
p = strjoina(temp, "/start"); |
| 71 |
ASSERT_OK_ERRNO(symlink("top/dot/dotdota", p)); |
| 72 |
|
| 73 |
/* Paths that use symlinks underneath the "root" */ |
| 74 |
|
| 75 |
ASSERT_OK_POSITIVE(chase(p, NULL, 0, &result, NULL)); |
| 76 |
ASSERT_PATH_EQ(result, "/usr"); |
| 77 |
result = mfree(result); |
| 78 |
|
| 79 |
ASSERT_OK_POSITIVE(chase(p, "/.//../../../", 0, &result, NULL)); |
| 80 |
ASSERT_PATH_EQ(result, "/usr"); |
| 81 |
result = mfree(result); |
| 82 |
|
| 83 |
pslash = strjoina(p, "/"); |
| 84 |
ASSERT_OK_POSITIVE(chase(pslash, NULL, 0, &result, NULL)); |
| 85 |
ASSERT_PATH_EQ(result, "/usr/"); |
| 86 |
result = mfree(result); |
| 87 |
|
| 88 |
ASSERT_ERROR(chase(p, temp, 0, &result, NULL), ENOENT); |
| 89 |
ASSERT_ERROR(chase(pslash, temp, 0, &result, NULL), ENOENT); |
| 90 |
|
| 91 |
q = strjoina(temp, "/usr"); |
| 92 |
|
| 93 |
ASSERT_OK_ZERO(chase(p, temp, CHASE_NONEXISTENT, &result, NULL)); |
| 94 |
ASSERT_PATH_EQ(result, q); |
| 95 |
result = mfree(result); |
| 96 |
|
| 97 |
qslash = strjoina(q, "/"); |
| 98 |
|
| 99 |
ASSERT_OK_ZERO(chase(pslash, temp, CHASE_NONEXISTENT, &result, NULL)); |
| 100 |
ASSERT_PATH_EQ(result, qslash); |
| 101 |
result = mfree(result); |
| 102 |
|
| 103 |
ASSERT_OK_ERRNO(mkdir(q, 0700)); |
| 104 |
|
| 105 |
ASSERT_OK_POSITIVE(chase(p, temp, 0, &result, NULL)); |
| 106 |
ASSERT_PATH_EQ(result, q); |
| 107 |
result = mfree(result); |
| 108 |
|
| 109 |
ASSERT_OK_POSITIVE(chase(pslash, temp, 0, &result, NULL)); |
| 110 |
ASSERT_PATH_EQ(result, qslash); |
| 111 |
result = mfree(result); |
| 112 |
|
| 113 |
p = strjoina(temp, "/slash"); |
| 114 |
ASSERT_OK_ERRNO(symlink("/", p)); |
| 115 |
|
| 116 |
ASSERT_OK_POSITIVE(chase(p, NULL, 0, &result, NULL)); |
| 117 |
ASSERT_PATH_EQ(result, "/"); |
| 118 |
result = mfree(result); |
| 119 |
|
| 120 |
ASSERT_OK_POSITIVE(chase(p, temp, 0, &result, NULL)); |
| 121 |
ASSERT_PATH_EQ(result, temp); |
| 122 |
result = mfree(result); |
| 123 |
|
| 124 |
/* Tests for CHASE_EXTRACT_FILENAME and chase_extract_filename() */ |
| 125 |
|
| 126 |
p = strjoina(temp, "/start"); |
| 127 |
pslash = strjoina(p, "/"); |
| 128 |
test_chase_extract_filename_one(p, NULL, "usr"); |
| 129 |
test_chase_extract_filename_one(pslash, NULL, "usr"); |
| 130 |
test_chase_extract_filename_one(p, temp, "usr"); |
| 131 |
test_chase_extract_filename_one(pslash, temp, "usr"); |
| 132 |
|
| 133 |
p = strjoina(temp, "/slash"); |
| 134 |
test_chase_extract_filename_one(p, NULL, "."); |
| 135 |
test_chase_extract_filename_one(p, temp, "."); |
| 136 |
|
| 137 |
/* Paths that would "escape" outside of the "root" */ |
| 138 |
|
| 139 |
p = strjoina(temp, "/6dots"); |
| 140 |
ASSERT_OK_ERRNO(symlink("../../..", p)); |
| 141 |
|
| 142 |
ASSERT_OK_POSITIVE(chase(p, temp, 0, &result, NULL)); |
| 143 |
ASSERT_PATH_EQ(result, temp); |
| 144 |
result = mfree(result); |
| 145 |
|
| 146 |
p = strjoina(temp, "/6dotsusr"); |
| 147 |
ASSERT_OK_ERRNO(symlink("../../../usr", p)); |
| 148 |
|
| 149 |
ASSERT_OK_POSITIVE(chase(p, temp, 0, &result, NULL)); |
| 150 |
ASSERT_PATH_EQ(result, q); |
| 151 |
result = mfree(result); |
| 152 |
|
| 153 |
p = strjoina(temp, "/top/8dotsusr"); |
| 154 |
ASSERT_OK_ERRNO(symlink("../../../../usr", p)); |
| 155 |
|
| 156 |
ASSERT_OK_POSITIVE(chase(p, temp, 0, &result, NULL)); |
| 157 |
ASSERT_PATH_EQ(result, q); |
| 158 |
result = mfree(result); |
| 159 |
|
| 160 |
/* Paths that contain repeated slashes */ |
| 161 |
|
| 162 |
p = strjoina(temp, "/slashslash"); |
| 163 |
ASSERT_OK_ERRNO(symlink("///usr///", p)); |
| 164 |
|
| 165 |
ASSERT_OK_POSITIVE(chase(p, NULL, 0, &result, NULL)); |
| 166 |
ASSERT_PATH_EQ(result, "/usr"); |
| 167 |
ASSERT_STREQ(result, "/usr"); /* we guarantee that we drop redundant slashes */ |
| 168 |
result = mfree(result); |
| 169 |
|
| 170 |
ASSERT_OK_POSITIVE(chase(p, temp, 0, &result, NULL)); |
| 171 |
ASSERT_PATH_EQ(result, q); |
| 172 |
result = mfree(result); |
| 173 |
|
| 174 |
/* Paths underneath the "root" with different UIDs while using CHASE_SAFE */ |
| 175 |
|
| 176 |
if (geteuid() == 0 && !userns_has_single_user()) { |
| 177 |
p = strjoina(temp, "/user"); |
| 178 |
ASSERT_OK_ERRNO(mkdir(p, 0755)); |
| 179 |
ASSERT_OK_ERRNO(chown(p, UID_NOBODY, GID_NOBODY)); |
| 180 |
|
| 181 |
q = strjoina(temp, "/user/root"); |
| 182 |
ASSERT_OK_ERRNO(mkdir(q, 0755)); |
| 183 |
|
| 184 |
p = strjoina(q, "/link"); |
| 185 |
ASSERT_OK_ERRNO(symlink("/", p)); |
| 186 |
|
| 187 |
/* Fail when user-owned directories contain root-owned subdirectories. */ |
| 188 |
ASSERT_ERROR(chase(p, temp, CHASE_SAFE, &result, NULL), ENOLINK); |
| 189 |
result = mfree(result); |
| 190 |
|
| 191 |
/* Allow this when the user-owned directories are all in the "root". */ |
| 192 |
ASSERT_OK_POSITIVE(chase(p, q, CHASE_SAFE, &result, NULL)); |
| 193 |
result = mfree(result); |
| 194 |
} |
| 195 |
|
| 196 |
/* Paths using . */ |
| 197 |
|
| 198 |
ASSERT_OK_POSITIVE(chase("/etc/./.././", NULL, 0, &result, NULL)); |
| 199 |
ASSERT_PATH_EQ(result, "/"); |
| 200 |
result = mfree(result); |
| 201 |
|
| 202 |
ASSERT_OK_POSITIVE(chase("/etc/./.././", "/etc", 0, &result, NULL)); |
| 203 |
ASSERT_PATH_EQ(result, "/etc"); |
| 204 |
result = mfree(result); |
| 205 |
|
| 206 |
ASSERT_OK_POSITIVE(chase("/../.././//../../etc", NULL, 0, &result, NULL)); |
| 207 |
ASSERT_STREQ(result, "/etc"); |
| 208 |
result = mfree(result); |
| 209 |
|
| 210 |
ASSERT_OK_ZERO(chase("/../.././//../../test-chase.fsldajfl", NULL, CHASE_NONEXISTENT, &result, NULL)); |
| 211 |
ASSERT_STREQ(result, "/test-chase.fsldajfl"); |
| 212 |
result = mfree(result); |
| 213 |
|
| 214 |
ASSERT_OK_POSITIVE(chase("/../.././//../../etc", "/", CHASE_PREFIX_ROOT, &result, NULL)); |
| 215 |
ASSERT_STREQ(result, "/etc"); |
| 216 |
result = mfree(result); |
| 217 |
|
| 218 |
ASSERT_OK_ZERO(chase("/../.././//../../test-chase.fsldajfl", "/", CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result, NULL)); |
| 219 |
ASSERT_STREQ(result, "/test-chase.fsldajfl"); |
| 220 |
result = mfree(result); |
| 221 |
|
| 222 |
ASSERT_OK(chase("/.path/with/dot", temp, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result, NULL)); |
| 223 |
q = strjoina(temp, "/.path/with/dot"); |
| 224 |
ASSERT_STREQ(result, q); |
| 225 |
result = mfree(result); |
| 226 |
|
| 227 |
ASSERT_TRUE(IN_SET(chase("/etc/machine-id/foo", NULL, 0, &result, NULL), -ENOTDIR, -ENOENT)); |
| 228 |
result = mfree(result); |
| 229 |
|
| 230 |
/* Path that loops back to self */ |
| 231 |
|
| 232 |
p = strjoina(temp, "/recursive-symlink"); |
| 233 |
ASSERT_OK_ERRNO(symlink("recursive-symlink", p)); |
| 234 |
ASSERT_ERROR(chase(p, NULL, 0, &result, NULL), ELOOP); |
| 235 |
|
| 236 |
/* Path which doesn't exist */ |
| 237 |
|
| 238 |
p = strjoina(temp, "/idontexist"); |
| 239 |
ASSERT_ERROR(chase(p, NULL, 0, &result, NULL), ENOENT); |
| 240 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_NONEXISTENT, &result, NULL)); |
| 241 |
ASSERT_PATH_EQ(result, p); |
| 242 |
result = mfree(result); |
| 243 |
|
| 244 |
p = strjoina(temp, "/idontexist/meneither"); |
| 245 |
ASSERT_ERROR(chase(p, NULL, 0, &result, NULL), ENOENT); |
| 246 |
|
| 247 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_NONEXISTENT, &result, NULL)); |
| 248 |
ASSERT_PATH_EQ(result, p); |
| 249 |
result = mfree(result); |
| 250 |
|
| 251 |
/* Relative paths */ |
| 252 |
|
| 253 |
ASSERT_OK(safe_getcwd(&pwd)); |
| 254 |
|
| 255 |
ASSERT_OK_ERRNO(chdir(temp)); |
| 256 |
|
| 257 |
p = "this/is/a/relative/path"; |
| 258 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_NONEXISTENT, &result, NULL)); |
| 259 |
|
| 260 |
p = strjoina(temp, "/", p); |
| 261 |
ASSERT_PATH_EQ(result, p); |
| 262 |
result = mfree(result); |
| 263 |
|
| 264 |
p = "this/is/a/relative/path"; |
| 265 |
ASSERT_OK_ZERO(chase(p, temp, CHASE_NONEXISTENT, &result, NULL)); |
| 266 |
|
| 267 |
p = strjoina(temp, "/", p); |
| 268 |
ASSERT_PATH_EQ(result, p); |
| 269 |
result = mfree(result); |
| 270 |
|
| 271 |
ASSERT_OK_ERRNO(chdir(pwd)); |
| 272 |
|
| 273 |
/* Path which doesn't exist, but contains weird stuff */ |
| 274 |
|
| 275 |
p = strjoina(temp, "/idontexist/.."); |
| 276 |
ASSERT_ERROR(chase(p, NULL, 0, &result, NULL), ENOENT); |
| 277 |
|
| 278 |
ASSERT_ERROR(chase(p, NULL, CHASE_NONEXISTENT, &result, NULL), ENOENT); |
| 279 |
|
| 280 |
p = strjoina(temp, "/target"); |
| 281 |
q = strjoina(temp, "/top"); |
| 282 |
ASSERT_OK_ERRNO(symlink(q, p)); |
| 283 |
p = strjoina(temp, "/target/idontexist"); |
| 284 |
ASSERT_ERROR(chase(p, NULL, 0, &result, NULL), ENOENT); |
| 285 |
|
| 286 |
if (geteuid() == 0 && !userns_has_single_user()) { |
| 287 |
p = strjoina(temp, "/priv1"); |
| 288 |
ASSERT_OK_ERRNO(mkdir(p, 0755)); |
| 289 |
|
| 290 |
q = strjoina(p, "/priv2"); |
| 291 |
ASSERT_OK_ERRNO(mkdir(q, 0755)); |
| 292 |
|
| 293 |
ASSERT_OK(chase(q, NULL, CHASE_SAFE, NULL, NULL)); |
| 294 |
|
| 295 |
ASSERT_OK_ERRNO(chown(q, UID_NOBODY, GID_NOBODY)); |
| 296 |
ASSERT_OK(chase(q, NULL, CHASE_SAFE, NULL, NULL)); |
| 297 |
|
| 298 |
ASSERT_OK(chown(p, UID_NOBODY, GID_NOBODY)); |
| 299 |
ASSERT_OK(chase(q, NULL, CHASE_SAFE, NULL, NULL)); |
| 300 |
|
| 301 |
ASSERT_OK_ERRNO(chown(q, 0, 0)); |
| 302 |
ASSERT_ERROR(chase(q, NULL, CHASE_SAFE, NULL, NULL), ENOLINK); |
| 303 |
|
| 304 |
ASSERT_OK_ERRNO(rmdir(q)); |
| 305 |
ASSERT_OK_ERRNO(symlink("/etc/passwd", q)); |
| 306 |
ASSERT_ERROR(chase(q, NULL, CHASE_SAFE, NULL, NULL), ENOLINK); |
| 307 |
|
| 308 |
ASSERT_OK_ERRNO(chown(p, 0, 0)); |
| 309 |
ASSERT_OK(chase(q, NULL, CHASE_SAFE, NULL, NULL)); |
| 310 |
} |
| 311 |
|
| 312 |
p = strjoina(temp, "/machine-id-test"); |
| 313 |
ASSERT_OK_ERRNO(symlink("/usr/../etc/./machine-id", p)); |
| 314 |
|
| 315 |
if (chase(p, NULL, 0, NULL, &pfd) != -ENOENT && sd_id128_get_machine(NULL) >= 0) { |
| 316 |
_cleanup_close_ int fd = -EBADF; |
| 317 |
sd_id128_t a, b; |
| 318 |
|
| 319 |
ASSERT_OK(pfd); |
| 320 |
|
| 321 |
fd = fd_reopen(pfd, O_RDONLY|O_CLOEXEC); |
| 322 |
ASSERT_OK(fd); |
| 323 |
safe_close(pfd); |
| 324 |
|
| 325 |
ASSERT_OK(id128_read_fd(fd, ID128_FORMAT_PLAIN, &a)); |
| 326 |
ASSERT_OK(sd_id128_get_machine(&b)); |
| 327 |
ASSERT_TRUE(sd_id128_equal(a, b)); |
| 328 |
} |
| 329 |
|
| 330 |
ASSERT_OK_ERRNO(lstat(p, &st)); |
| 331 |
ASSERT_OK_ZERO(chase_and_unlink(p, NULL, 0, 0, &result)); |
| 332 |
ASSERT_PATH_EQ(result, p); |
| 333 |
result = mfree(result); |
| 334 |
ASSERT_ERROR_ERRNO(lstat(p, &st), ENOENT); |
| 335 |
|
| 336 |
/* Test CHASE_NOFOLLOW */ |
| 337 |
|
| 338 |
p = strjoina(temp, "/target"); |
| 339 |
q = strjoina(temp, "/symlink"); |
| 340 |
ASSERT_OK_ERRNO(symlink(p, q)); |
| 341 |
ASSERT_OK(chase(q, NULL, CHASE_NOFOLLOW, &result, &pfd)); |
| 342 |
ASSERT_PATH_EQ(result, q); |
| 343 |
ASSERT_OK_ERRNO(fstat(pfd, &st)); |
| 344 |
ASSERT_TRUE(S_ISLNK(st.st_mode)); |
| 345 |
result = mfree(result); |
| 346 |
pfd = safe_close(pfd); |
| 347 |
|
| 348 |
/* s1 -> s2 -> nonexistent */ |
| 349 |
q = strjoina(temp, "/s1"); |
| 350 |
ASSERT_OK_ERRNO(symlink("s2", q)); |
| 351 |
p = strjoina(temp, "/s2"); |
| 352 |
ASSERT_OK_ERRNO(symlink("nonexistent", p)); |
| 353 |
ASSERT_OK(chase(q, NULL, CHASE_NOFOLLOW, &result, &pfd)); |
| 354 |
ASSERT_PATH_EQ(result, q); |
| 355 |
ASSERT_OK_ERRNO(fstat(pfd, &st)); |
| 356 |
ASSERT_TRUE(S_ISLNK(st.st_mode)); |
| 357 |
result = mfree(result); |
| 358 |
pfd = safe_close(pfd); |
| 359 |
|
| 360 |
/* Test CHASE_STEP */ |
| 361 |
|
| 362 |
p = strjoina(temp, "/start"); |
| 363 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_STEP, &result, NULL)); |
| 364 |
p = strjoina(temp, "/top/dot/dotdota"); |
| 365 |
ASSERT_STREQ(p, result); |
| 366 |
result = mfree(result); |
| 367 |
|
| 368 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_STEP, &result, NULL)); |
| 369 |
p = strjoina(temp, "/top/dotdota"); |
| 370 |
ASSERT_STREQ(p, result); |
| 371 |
result = mfree(result); |
| 372 |
|
| 373 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_STEP, &result, NULL)); |
| 374 |
p = strjoina(temp, "/top/../a"); |
| 375 |
ASSERT_STREQ(p, result); |
| 376 |
result = mfree(result); |
| 377 |
|
| 378 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_STEP, &result, NULL)); |
| 379 |
p = strjoina(temp, "/a"); |
| 380 |
ASSERT_STREQ(p, result); |
| 381 |
result = mfree(result); |
| 382 |
|
| 383 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_STEP, &result, NULL)); |
| 384 |
p = strjoina(temp, "/b"); |
| 385 |
ASSERT_STREQ(p, result); |
| 386 |
result = mfree(result); |
| 387 |
|
| 388 |
ASSERT_OK_ZERO(chase(p, NULL, CHASE_STEP, &result, NULL)); |
| 389 |
ASSERT_STREQ(result, "/usr"); |
| 390 |
result = mfree(result); |
| 391 |
|
| 392 |
ASSERT_OK_POSITIVE(chase("/usr", NULL, CHASE_STEP, &result, NULL)); |
| 393 |
ASSERT_STREQ(result, "/usr"); |
| 394 |
result = mfree(result); |
| 395 |
|
| 396 |
/* Make sure that symlinks in the "root" path are not resolved, but those below are */ |
| 397 |
p = strjoina("/etc/..", temp, "/self"); |
| 398 |
ASSERT_OK_ERRNO(symlink(".", p)); |
| 399 |
q = strjoina(p, "/top/dot/dotdota"); |
| 400 |
ASSERT_OK_POSITIVE(chase(q, p, 0, &result, NULL)); |
| 401 |
ASSERT_PATH_EQ(path_startswith(result, p), "usr"); |
| 402 |
result = mfree(result); |
| 403 |
|
| 404 |
/* Test CHASE_PROHIBIT_SYMLINKS */ |
| 405 |
|
| 406 |
ASSERT_ERROR(chase("top/dot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, NULL, NULL), ELOOP); |
| 407 |
ASSERT_ERROR(chase("top/dot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_WARN, NULL, NULL), ELOOP); |
| 408 |
ASSERT_ERROR(chase("top/dotdot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, NULL, NULL), ELOOP); |
| 409 |
ASSERT_ERROR(chase("top/dotdot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_WARN, NULL, NULL), ELOOP); |
| 410 |
ASSERT_ERROR(chase("top/dot/dot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, NULL, NULL), ELOOP); |
| 411 |
ASSERT_ERROR(chase("top/dot/dot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_WARN, NULL, NULL), ELOOP); |
| 412 |
|
| 413 |
cleanup: |
| 414 |
ASSERT_OK(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL)); |
| 415 |
} |
| 416 |
|
| 417 |
TEST(chase_and_open) { |
| 418 |
_cleanup_free_ char *result = NULL; |
| 419 |
_cleanup_close_ int fd = -EBADF; |
| 420 |
|
| 421 |
/* Test chase_and_open() with various CHASE_PARENT / CHASE_EXTRACT_FILENAME combinations. */ |
| 422 |
|
| 423 |
/* No CHASE_PARENT, no CHASE_EXTRACT_FILENAME, with ret_path — opens the target, returns full path. */ |
| 424 |
fd = ASSERT_OK(chase_and_open("/usr/lib", NULL, 0, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 425 |
ASSERT_OK(fd_verify_directory(fd)); |
| 426 |
ASSERT_STREQ(result, "/usr/lib"); |
| 427 |
fd = safe_close(fd); |
| 428 |
result = mfree(result); |
| 429 |
|
| 430 |
/* CHASE_PARENT with ret_path — opens parent dir, returns full path including final component. */ |
| 431 |
fd = ASSERT_OK(chase_and_open("/usr/lib", NULL, CHASE_PARENT, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 432 |
ASSERT_OK(fd_verify_directory(fd)); |
| 433 |
ASSERT_OK_ERRNO(faccessat(fd, "lib", F_OK, 0)); |
| 434 |
ASSERT_STREQ(result, "/usr/lib"); |
| 435 |
fd = safe_close(fd); |
| 436 |
result = mfree(result); |
| 437 |
|
| 438 |
/* CHASE_PARENT|CHASE_EXTRACT_FILENAME — opens parent dir, returns just the filename. */ |
| 439 |
fd = ASSERT_OK(chase_and_open("/usr/lib", NULL, CHASE_PARENT|CHASE_EXTRACT_FILENAME, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 440 |
ASSERT_OK(fd_verify_directory(fd)); |
| 441 |
ASSERT_OK_ERRNO(faccessat(fd, "lib", F_OK, 0)); |
| 442 |
ASSERT_STREQ(result, "lib"); |
| 443 |
fd = safe_close(fd); |
| 444 |
result = mfree(result); |
| 445 |
|
| 446 |
/* CHASE_EXTRACT_FILENAME only — opens the target itself, returns just the filename. */ |
| 447 |
fd = ASSERT_OK(chase_and_open("/usr/lib", NULL, CHASE_EXTRACT_FILENAME, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 448 |
ASSERT_OK(fd_verify_directory(fd)); |
| 449 |
ASSERT_STREQ(result, "lib"); |
| 450 |
fd = safe_close(fd); |
| 451 |
result = mfree(result); |
| 452 |
|
| 453 |
/* CHASE_EXTRACT_FILENAME on a regular file (regression test for a bug where chase_and_open() |
| 454 |
* reopened the parent directory instead of the target file). */ |
| 455 |
fd = ASSERT_OK(chase_and_open("/etc/os-release", NULL, CHASE_EXTRACT_FILENAME, O_PATH|O_CLOEXEC, &result)); |
| 456 |
ASSERT_STREQ(result, "os-release"); |
| 457 |
ASSERT_OK(fd_verify_regular(fd)); |
| 458 |
fd = safe_close(fd); |
| 459 |
result = mfree(result); |
| 460 |
|
| 461 |
/* CHASE_PARENT through a symlink — symlink is followed, parent of the target is opened. */ |
| 462 |
fd = ASSERT_OK(chase_and_open("/etc/os-release", NULL, CHASE_PARENT, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 463 |
ASSERT_OK(fd_verify_directory(fd)); |
| 464 |
ASSERT_NOT_NULL(result); |
| 465 |
fd = safe_close(fd); |
| 466 |
result = mfree(result); |
| 467 |
|
| 468 |
/* CHASE_PARENT|CHASE_NOFOLLOW through a symlink — symlink is NOT followed, parent of the |
| 469 |
* symlink is opened. */ |
| 470 |
fd = ASSERT_OK(chase_and_open("/etc/os-release", NULL, CHASE_PARENT|CHASE_NOFOLLOW, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 471 |
ASSERT_OK(fd_verify_directory(fd)); |
| 472 |
ASSERT_OK_ERRNO(faccessat(fd, "os-release", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 473 |
ASSERT_STREQ(result, "/etc/os-release"); |
| 474 |
fd = safe_close(fd); |
| 475 |
result = mfree(result); |
| 476 |
|
| 477 |
/* CHASE_PARENT|CHASE_NOFOLLOW|CHASE_EXTRACT_FILENAME through a symlink — parent of the symlink |
| 478 |
* is opened, returns just the symlink name. */ |
| 479 |
fd = ASSERT_OK(chase_and_open("/etc/os-release", NULL, CHASE_PARENT|CHASE_NOFOLLOW|CHASE_EXTRACT_FILENAME, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 480 |
ASSERT_OK(fd_verify_directory(fd)); |
| 481 |
ASSERT_OK_ERRNO(faccessat(fd, "os-release", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 482 |
ASSERT_STREQ(result, "os-release"); |
| 483 |
fd = safe_close(fd); |
| 484 |
result = mfree(result); |
| 485 |
|
| 486 |
/* When the resolved path equals the root directory itself, the filename should be "." — not |
| 487 |
* the basename of the root directory. This is the edge case that chase_extract_filename() |
| 488 |
* handles by stripping the root prefix before extracting, which plain path_extract_filename() |
| 489 |
* would get wrong. */ |
| 490 |
_cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL; |
| 491 |
_cleanup_close_ int tfd = -EBADF; |
| 492 |
|
| 493 |
tfd = ASSERT_OK(mkdtemp_open(NULL, 0, &tmpdir)); |
| 494 |
/* Create a symlink to "/" — when chased under tmpdir as root, it resolves to tmpdir itself. */ |
| 495 |
ASSERT_OK_ERRNO(symlinkat("/", tfd, "to_root")); |
| 496 |
|
| 497 |
_cleanup_free_ char *link_path = ASSERT_NOT_NULL(path_join(tmpdir, "to_root")); |
| 498 |
fd = ASSERT_OK(chase_and_open(link_path, tmpdir, 0, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 499 |
ASSERT_OK(fd_verify_directory(fd)); |
| 500 |
ASSERT_PATH_EQ(result, tmpdir); |
| 501 |
fd = safe_close(fd); |
| 502 |
result = mfree(result); |
| 503 |
} |
| 504 |
|
| 505 |
TEST(chaseat) { |
| 506 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 507 |
_cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd2 = -EBADF; |
| 508 |
_cleanup_free_ char *result = NULL; |
| 509 |
_cleanup_closedir_ DIR *dir = NULL; |
| 510 |
_cleanup_fclose_ FILE *f = NULL; |
| 511 |
struct stat st; |
| 512 |
const char *p; |
| 513 |
|
| 514 |
ASSERT_OK(tfd = mkdtemp_open(NULL, 0, &t)); |
| 515 |
|
| 516 |
/* Test that AT_FDCWD resolves against / and not the current working |
| 517 |
* directory. */ |
| 518 |
|
| 519 |
ASSERT_OK_ERRNO(symlinkat("/usr", tfd, "abc")); |
| 520 |
|
| 521 |
p = strjoina(t, "/abc"); |
| 522 |
ASSERT_OK(chaseat(XAT_FDROOT, AT_FDCWD, p, 0, &result, NULL)); |
| 523 |
ASSERT_STREQ(result, "/usr"); |
| 524 |
result = mfree(result); |
| 525 |
|
| 526 |
/* If the file descriptor points to the root directory, the result will be absolute. */ |
| 527 |
|
| 528 |
fd = open("/", O_CLOEXEC | O_DIRECTORY | O_PATH); |
| 529 |
ASSERT_OK(fd); |
| 530 |
|
| 531 |
ASSERT_OK(chaseat(XAT_FDROOT, fd, p, 0, &result, NULL)); |
| 532 |
ASSERT_STREQ(result, "/usr"); |
| 533 |
result = mfree(result); |
| 534 |
|
| 535 |
/* (XAT_FDROOT, fd-of-/, relative): fd points to "/" which is the host root, so root_fd is |
| 536 |
* normalized to XAT_FDROOT internally. A relative path resolves from "/". Result is absolute. */ |
| 537 |
ASSERT_OK(chaseat(XAT_FDROOT, fd, "usr", 0, &result, &fd2)); |
| 538 |
ASSERT_STREQ(result, "/usr"); |
| 539 |
ASSERT_TRUE(inode_same_at(fd2, NULL, AT_FDCWD, "/usr", AT_EMPTY_PATH)); |
| 540 |
result = mfree(result); |
| 541 |
fd2 = safe_close(fd2); |
| 542 |
|
| 543 |
/* Same without ret_path to exercise the shortcut. */ |
| 544 |
ASSERT_OK(chaseat(XAT_FDROOT, fd, "usr", 0, NULL, &fd2)); |
| 545 |
ASSERT_TRUE(inode_same_at(fd2, NULL, AT_FDCWD, "/usr", AT_EMPTY_PATH)); |
| 546 |
fd2 = safe_close(fd2); |
| 547 |
|
| 548 |
ASSERT_OK(chaseat(fd, fd, p, 0, &result, NULL)); |
| 549 |
ASSERT_STREQ(result, "/usr"); |
| 550 |
result = mfree(result); |
| 551 |
|
| 552 |
fd = safe_close(fd); |
| 553 |
|
| 554 |
/* Same but with XAT_FDROOT */ |
| 555 |
_cleanup_close_ int found_fd1 = -EBADF; |
| 556 |
ASSERT_OK(chaseat(XAT_FDROOT, XAT_FDROOT, p, 0, &result, &found_fd1)); |
| 557 |
ASSERT_STREQ(result, "/usr"); |
| 558 |
result = mfree(result); |
| 559 |
|
| 560 |
_cleanup_close_ int found_fd2 = -EBADF; |
| 561 |
ASSERT_OK(chaseat(XAT_FDROOT, XAT_FDROOT, p, 0, &result, &found_fd2)); |
| 562 |
ASSERT_STREQ(result, "/usr"); |
| 563 |
result = mfree(result); |
| 564 |
assert(fd_inode_same(found_fd1, found_fd2) > 0); |
| 565 |
|
| 566 |
/* Do the same XAT_FDROOT tests again, this time without querying the path, so that the open_tree() |
| 567 |
* shortcut can work */ |
| 568 |
_cleanup_close_ int found_fd3 = -EBADF; |
| 569 |
ASSERT_OK(chaseat(XAT_FDROOT, XAT_FDROOT, p, 0, NULL, &found_fd3)); |
| 570 |
assert(fd_inode_same(found_fd1, found_fd3) > 0); |
| 571 |
assert(fd_inode_same(found_fd2, found_fd3) > 0); |
| 572 |
|
| 573 |
_cleanup_close_ int found_fd4 = -EBADF; |
| 574 |
ASSERT_OK(chaseat(XAT_FDROOT, XAT_FDROOT, p, 0, NULL, &found_fd4)); |
| 575 |
assert(fd_inode_same(found_fd1, found_fd4) > 0); |
| 576 |
assert(fd_inode_same(found_fd2, found_fd4) > 0); |
| 577 |
assert(fd_inode_same(found_fd3, found_fd4) > 0); |
| 578 |
|
| 579 |
found_fd1 = safe_close(found_fd1); |
| 580 |
found_fd2 = safe_close(found_fd2); |
| 581 |
found_fd3 = safe_close(found_fd3); |
| 582 |
found_fd4 = safe_close(found_fd4); |
| 583 |
|
| 584 |
/* (XAT_FDROOT, XAT_FDROOT, relative): relative path from host root. XAT_FDROOT as dir_fd |
| 585 |
* redirects to root_fd which is also XAT_FDROOT (/), so "usr" resolves to /usr. Result is |
| 586 |
* absolute because root_fd == XAT_FDROOT. */ |
| 587 |
ASSERT_OK(chaseat(XAT_FDROOT, XAT_FDROOT, "usr", 0, &result, NULL)); |
| 588 |
ASSERT_STREQ(result, "/usr"); |
| 589 |
result = mfree(result); |
| 590 |
|
| 591 |
/* Same without ret_path so the shortcut can fire. */ |
| 592 |
ASSERT_OK(chaseat(XAT_FDROOT, XAT_FDROOT, "usr", 0, NULL, &fd)); |
| 593 |
ASSERT_TRUE(inode_same_at(fd, NULL, AT_FDCWD, "/usr", AT_EMPTY_PATH)); |
| 594 |
fd = safe_close(fd); |
| 595 |
|
| 596 |
/* (XAT_FDROOT, AT_FDCWD, relative): relative path from current working directory. */ |
| 597 |
_cleanup_free_ char *cwd_saved = NULL; |
| 598 |
ASSERT_OK(safe_getcwd(&cwd_saved)); |
| 599 |
|
| 600 |
ASSERT_OK_ERRNO(chdir(t)); |
| 601 |
|
| 602 |
ASSERT_OK(chaseat(XAT_FDROOT, AT_FDCWD, "abc", 0, &result, &fd)); |
| 603 |
ASSERT_TRUE(inode_same_at(fd, NULL, AT_FDCWD, "/usr", AT_EMPTY_PATH)); |
| 604 |
ASSERT_STREQ(result, "/usr"); |
| 605 |
fd = safe_close(fd); |
| 606 |
result = mfree(result); |
| 607 |
|
| 608 |
/* Same without ret_path to exercise the shortcut. */ |
| 609 |
ASSERT_OK(chaseat(XAT_FDROOT, AT_FDCWD, "abc", 0, NULL, &fd)); |
| 610 |
ASSERT_TRUE(inode_same_at(fd, NULL, AT_FDCWD, "/usr", AT_EMPTY_PATH)); |
| 611 |
fd = safe_close(fd); |
| 612 |
|
| 613 |
/* A plain file (no symlink indirection) should also work. */ |
| 614 |
fd = ASSERT_OK_ERRNO(openat(tfd, "cwd_test", O_CREAT|O_CLOEXEC, 0600)); |
| 615 |
fd = safe_close(fd); |
| 616 |
|
| 617 |
ASSERT_OK(chaseat(XAT_FDROOT, AT_FDCWD, "cwd_test", 0, &result, &fd)); |
| 618 |
ASSERT_TRUE(inode_same_at(fd, NULL, tfd, "cwd_test", AT_EMPTY_PATH)); |
| 619 |
fd = safe_close(fd); |
| 620 |
result = mfree(result); |
| 621 |
|
| 622 |
ASSERT_OK_ERRNO(chdir(cwd_saved)); |
| 623 |
|
| 624 |
/* If the file descriptor does not point to the root directory, the result will be relative |
| 625 |
* unless the result is outside of the specified file descriptor. */ |
| 626 |
|
| 627 |
ASSERT_OK(chaseat(XAT_FDROOT, tfd, "abc", 0, &result, NULL)); |
| 628 |
ASSERT_STREQ(result, "/usr"); |
| 629 |
result = mfree(result); |
| 630 |
|
| 631 |
ASSERT_ERROR(chaseat(tfd, tfd, "abc", 0, NULL, NULL), ENOENT); |
| 632 |
ASSERT_ERROR(chaseat(tfd, tfd, "/abc", 0, NULL, NULL), ENOENT); |
| 633 |
|
| 634 |
ASSERT_OK(chaseat(tfd, tfd, "abc", CHASE_NONEXISTENT, &result, NULL)); |
| 635 |
ASSERT_STREQ(result, "usr"); |
| 636 |
result = mfree(result); |
| 637 |
|
| 638 |
ASSERT_OK(chaseat(tfd, tfd, "/abc", CHASE_NONEXISTENT, &result, NULL)); |
| 639 |
ASSERT_STREQ(result, "usr"); |
| 640 |
result = mfree(result); |
| 641 |
|
| 642 |
/* Test that absolute path or not are the same when resolving relative to a directory file |
| 643 |
* descriptor and that we always get a relative path back. */ |
| 644 |
|
| 645 |
fd = ASSERT_OK_ERRNO(openat(tfd, "def", O_CREAT|O_CLOEXEC, 0700)); |
| 646 |
fd = safe_close(fd); |
| 647 |
ASSERT_OK_ERRNO(symlinkat("/def", tfd, "qed")); |
| 648 |
ASSERT_OK(chaseat(tfd, tfd, "qed", 0, &result, NULL)); |
| 649 |
ASSERT_STREQ(result, "def"); |
| 650 |
result = mfree(result); |
| 651 |
ASSERT_OK(chaseat(tfd, tfd, "/qed", 0, &result, NULL)); |
| 652 |
ASSERT_STREQ(result, "def"); |
| 653 |
result = mfree(result); |
| 654 |
|
| 655 |
/* Valid directory file descriptor should resolve symlinks against |
| 656 |
* host's root. */ |
| 657 |
ASSERT_ERROR(chaseat(XAT_FDROOT, tfd, "/qed", 0, NULL, NULL), ENOENT); |
| 658 |
|
| 659 |
/* Test CHASE_PARENT */ |
| 660 |
|
| 661 |
fd = ASSERT_OK(open_mkdir_at(tfd, "chase", O_CLOEXEC, 0755)); |
| 662 |
ASSERT_OK_ERRNO(symlinkat("/def", fd, "parent")); |
| 663 |
fd = safe_close(fd); |
| 664 |
|
| 665 |
/* Make sure that when we chase a symlink parent directory, that we chase the parent directory of the |
| 666 |
* symlink target and not the symlink itself. But if we add CHASE_NOFOLLOW, we get the parent |
| 667 |
* directory of the symlink itself. */ |
| 668 |
|
| 669 |
ASSERT_OK(chaseat(tfd, tfd, "chase/parent", CHASE_PARENT, &result, &fd)); |
| 670 |
ASSERT_OK_ERRNO(faccessat(fd, "def", F_OK, 0)); |
| 671 |
ASSERT_STREQ(result, "def"); |
| 672 |
fd = safe_close(fd); |
| 673 |
result = mfree(result); |
| 674 |
|
| 675 |
ASSERT_OK(chaseat(tfd, tfd, "chase/parent", CHASE_PARENT|CHASE_NOFOLLOW, &result, &fd)); |
| 676 |
ASSERT_OK_ERRNO(faccessat(fd, "parent", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 677 |
ASSERT_STREQ(result, "chase/parent"); |
| 678 |
fd = safe_close(fd); |
| 679 |
result = mfree(result); |
| 680 |
|
| 681 |
ASSERT_OK(chaseat(tfd, tfd, "chase", CHASE_PARENT, &result, &fd)); |
| 682 |
ASSERT_OK_ERRNO(faccessat(fd, "chase", F_OK, 0)); |
| 683 |
ASSERT_STREQ(result, "chase"); |
| 684 |
fd = safe_close(fd); |
| 685 |
result = mfree(result); |
| 686 |
|
| 687 |
ASSERT_OK(chaseat(tfd, tfd, "/", CHASE_PARENT, &result, NULL)); |
| 688 |
ASSERT_STREQ(result, "."); |
| 689 |
result = mfree(result); |
| 690 |
|
| 691 |
ASSERT_OK(chaseat(tfd, tfd, ".", CHASE_PARENT, &result, NULL)); |
| 692 |
ASSERT_STREQ(result, "."); |
| 693 |
result = mfree(result); |
| 694 |
|
| 695 |
/* Test CHASE_MKDIR_0755 */ |
| 696 |
|
| 697 |
ASSERT_OK(chaseat(XAT_FDROOT, tfd, "m/k/d/i/r", CHASE_MKDIR_0755|CHASE_NONEXISTENT, &result, NULL)); |
| 698 |
ASSERT_OK_ERRNO(faccessat(tfd, "m/k/d/i", F_OK, 0)); |
| 699 |
ASSERT_ERROR(RET_NERRNO(faccessat(tfd, "m/k/d/i/r", F_OK, 0)), ENOENT); |
| 700 |
ASSERT_STREQ(result, "m/k/d/i/r"); |
| 701 |
result = mfree(result); |
| 702 |
|
| 703 |
ASSERT_OK(chaseat(XAT_FDROOT, tfd, "m/../q", CHASE_MKDIR_0755|CHASE_NONEXISTENT, &result, NULL)); |
| 704 |
ASSERT_OK_ERRNO(faccessat(tfd, "m", F_OK, 0)); |
| 705 |
ASSERT_ERROR(RET_NERRNO(faccessat(tfd, "q", F_OK, 0)), ENOENT); |
| 706 |
ASSERT_STREQ(result, "q"); |
| 707 |
result = mfree(result); |
| 708 |
|
| 709 |
ASSERT_ERROR(chaseat(XAT_FDROOT, tfd, "i/../p", CHASE_MKDIR_0755|CHASE_NONEXISTENT, NULL, NULL), ENOENT); |
| 710 |
|
| 711 |
/* Test CHASE_MKDIR_0755|CHASE_PARENT — creates intermediate dirs but not the final component */ |
| 712 |
|
| 713 |
ASSERT_OK(chaseat(XAT_FDROOT, tfd, "mkp/a/r/e/n/t/file", CHASE_MKDIR_0755|CHASE_PARENT, &result, &fd)); |
| 714 |
ASSERT_OK_ERRNO(faccessat(tfd, "mkp/a/r/e/n/t", F_OK, 0)); |
| 715 |
ASSERT_ERROR(RET_NERRNO(faccessat(tfd, "mkp/a/r/e/n/t/file", F_OK, 0)), ENOENT); |
| 716 |
ASSERT_OK(fd_verify_directory(fd)); |
| 717 |
fd = safe_close(fd); |
| 718 |
ASSERT_STREQ(result, "mkp/a/r/e/n/t/file"); |
| 719 |
result = mfree(result); |
| 720 |
|
| 721 |
/* Test CHASE_EXTRACT_FILENAME */ |
| 722 |
|
| 723 |
ASSERT_OK(chaseat(tfd, tfd, "chase/parent", CHASE_PARENT|CHASE_NOFOLLOW|CHASE_EXTRACT_FILENAME, &result, &fd)); |
| 724 |
ASSERT_OK_ERRNO(faccessat(fd, result, F_OK, AT_SYMLINK_NOFOLLOW)); |
| 725 |
ASSERT_STREQ(result, "parent"); |
| 726 |
fd = safe_close(fd); |
| 727 |
result = mfree(result); |
| 728 |
|
| 729 |
ASSERT_OK(chaseat(tfd, tfd, "chase", CHASE_PARENT|CHASE_EXTRACT_FILENAME, &result, &fd)); |
| 730 |
ASSERT_OK_ERRNO(faccessat(fd, result, F_OK, 0)); |
| 731 |
ASSERT_STREQ(result, "chase"); |
| 732 |
fd = safe_close(fd); |
| 733 |
result = mfree(result); |
| 734 |
|
| 735 |
ASSERT_OK(chaseat(tfd, tfd, "/", CHASE_PARENT|CHASE_EXTRACT_FILENAME, &result, NULL)); |
| 736 |
ASSERT_STREQ(result, "."); |
| 737 |
result = mfree(result); |
| 738 |
|
| 739 |
ASSERT_OK(chaseat(tfd, tfd, ".", CHASE_PARENT|CHASE_EXTRACT_FILENAME, &result, NULL)); |
| 740 |
ASSERT_STREQ(result, "."); |
| 741 |
result = mfree(result); |
| 742 |
|
| 743 |
ASSERT_OK(chaseat(tfd, tfd, NULL, CHASE_PARENT|CHASE_EXTRACT_FILENAME, &result, NULL)); |
| 744 |
ASSERT_STREQ(result, "."); |
| 745 |
result = mfree(result); |
| 746 |
|
| 747 |
/* Test chase_and_openat() */ |
| 748 |
|
| 749 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, "o/p/e/n/f/i/l/e", CHASE_MKDIR_0755, O_CREAT|O_EXCL|O_CLOEXEC, NULL)); |
| 750 |
ASSERT_OK(fd_verify_regular(fd)); |
| 751 |
fd = safe_close(fd); |
| 752 |
|
| 753 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, "o/p/e/n/d/i/r", CHASE_MKDIR_0755, O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, NULL)); |
| 754 |
ASSERT_OK(fd_verify_directory(fd)); |
| 755 |
fd = safe_close(fd); |
| 756 |
|
| 757 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, NULL, CHASE_PARENT|CHASE_EXTRACT_FILENAME, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 758 |
ASSERT_STREQ(result, "."); |
| 759 |
fd = safe_close(fd); |
| 760 |
result = mfree(result); |
| 761 |
|
| 762 |
/* Test chase_and_openat() with CHASE_MKDIR_0755|CHASE_PARENT — opens parent dir */ |
| 763 |
|
| 764 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, "mkopen/p/a/r/file.txt", CHASE_MKDIR_0755|CHASE_PARENT, O_RDONLY|O_CLOEXEC, NULL)); |
| 765 |
ASSERT_OK(fd_verify_directory(fd)); |
| 766 |
ASSERT_OK(faccessat(tfd, "mkopen/p/a/r", F_OK, 0)); |
| 767 |
ASSERT_ERROR(RET_NERRNO(faccessat(tfd, "mkopen/p/a/r/file.txt", F_OK, 0)), ENOENT); |
| 768 |
fd = safe_close(fd); |
| 769 |
|
| 770 |
/* Test chase_and_openat() with CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY + O_CREAT — creates and opens target dir */ |
| 771 |
|
| 772 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, "mkopen/d/i/r/target", CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY, O_CREAT|O_RDONLY|O_CLOEXEC, NULL)); |
| 773 |
ASSERT_OK(fd_verify_directory(fd)); |
| 774 |
ASSERT_OK(faccessat(tfd, "mkopen/d/i/r/target", F_OK, 0)); |
| 775 |
fd = safe_close(fd); |
| 776 |
|
| 777 |
/* Test chase_and_openat() with various CHASE_PARENT / CHASE_EXTRACT_FILENAME combinations */ |
| 778 |
|
| 779 |
/* No CHASE_PARENT, no CHASE_EXTRACT_FILENAME, with ret_path — opens the target, returns full path. */ |
| 780 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, "o/p/e/n/d/i/r", 0, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 781 |
ASSERT_OK(fd_verify_directory(fd)); |
| 782 |
ASSERT_STREQ(result, "o/p/e/n/d/i/r"); |
| 783 |
fd = safe_close(fd); |
| 784 |
result = mfree(result); |
| 785 |
|
| 786 |
/* CHASE_PARENT with ret_path — opens parent dir, returns full path including final component. */ |
| 787 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, "o/p/e/n/d/i/r", CHASE_PARENT, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 788 |
ASSERT_OK(fd_verify_directory(fd)); |
| 789 |
ASSERT_OK_ERRNO(faccessat(fd, "r", F_OK, 0)); |
| 790 |
ASSERT_STREQ(result, "o/p/e/n/d/i/r"); |
| 791 |
fd = safe_close(fd); |
| 792 |
result = mfree(result); |
| 793 |
|
| 794 |
/* CHASE_PARENT|CHASE_EXTRACT_FILENAME with a real multi-component path — opens parent dir, |
| 795 |
* returns just the filename. */ |
| 796 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, "o/p/e/n/d/i/r", CHASE_PARENT|CHASE_EXTRACT_FILENAME, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 797 |
ASSERT_OK(fd_verify_directory(fd)); |
| 798 |
ASSERT_OK_ERRNO(faccessat(fd, "r", F_OK, 0)); |
| 799 |
ASSERT_STREQ(result, "r"); |
| 800 |
fd = safe_close(fd); |
| 801 |
result = mfree(result); |
| 802 |
|
| 803 |
/* CHASE_EXTRACT_FILENAME only (without CHASE_PARENT) — opens the target itself, returns just |
| 804 |
* the filename. */ |
| 805 |
fd = ASSERT_OK(chase_and_openat(XAT_FDROOT, tfd, "o/p/e/n/d/i/r", CHASE_EXTRACT_FILENAME, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 806 |
ASSERT_OK(fd_verify_directory(fd)); |
| 807 |
ASSERT_STREQ(result, "r"); |
| 808 |
fd = safe_close(fd); |
| 809 |
result = mfree(result); |
| 810 |
|
| 811 |
/* CHASE_PARENT through a symlink — the symlink is followed, parent of the target is opened. |
| 812 |
* "chase/parent" where parent→/def: resolves to /def, parent is the root dir. */ |
| 813 |
fd = ASSERT_OK(chase_and_openat(tfd, tfd, "chase/parent", CHASE_PARENT, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 814 |
ASSERT_OK(fd_verify_directory(fd)); |
| 815 |
ASSERT_OK_ERRNO(faccessat(fd, "def", F_OK, 0)); |
| 816 |
ASSERT_STREQ(result, "def"); |
| 817 |
fd = safe_close(fd); |
| 818 |
result = mfree(result); |
| 819 |
|
| 820 |
/* CHASE_PARENT|CHASE_EXTRACT_FILENAME through a symlink — parent of the target is opened, |
| 821 |
* returns just the target filename. */ |
| 822 |
fd = ASSERT_OK(chase_and_openat(tfd, tfd, "chase/parent", CHASE_PARENT|CHASE_EXTRACT_FILENAME, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 823 |
ASSERT_OK(fd_verify_directory(fd)); |
| 824 |
ASSERT_OK_ERRNO(faccessat(fd, "def", F_OK, 0)); |
| 825 |
ASSERT_STREQ(result, "def"); |
| 826 |
fd = safe_close(fd); |
| 827 |
result = mfree(result); |
| 828 |
|
| 829 |
/* CHASE_PARENT|CHASE_NOFOLLOW through a symlink — the symlink is NOT followed, parent of the |
| 830 |
* symlink is opened. */ |
| 831 |
fd = ASSERT_OK(chase_and_openat(tfd, tfd, "chase/parent", CHASE_PARENT|CHASE_NOFOLLOW, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 832 |
ASSERT_OK(fd_verify_directory(fd)); |
| 833 |
ASSERT_OK_ERRNO(faccessat(fd, "parent", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 834 |
ASSERT_STREQ(result, "chase/parent"); |
| 835 |
fd = safe_close(fd); |
| 836 |
result = mfree(result); |
| 837 |
|
| 838 |
/* CHASE_PARENT|CHASE_NOFOLLOW|CHASE_EXTRACT_FILENAME through a symlink — parent of the symlink |
| 839 |
* is opened, returns just the symlink name. */ |
| 840 |
fd = ASSERT_OK(chase_and_openat(tfd, tfd, "chase/parent", CHASE_PARENT|CHASE_NOFOLLOW|CHASE_EXTRACT_FILENAME, O_PATH|O_DIRECTORY|O_CLOEXEC, &result)); |
| 841 |
ASSERT_OK(fd_verify_directory(fd)); |
| 842 |
ASSERT_OK_ERRNO(faccessat(fd, "parent", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 843 |
ASSERT_STREQ(result, "parent"); |
| 844 |
fd = safe_close(fd); |
| 845 |
result = mfree(result); |
| 846 |
|
| 847 |
/* Test chase_and_openatdir() */ |
| 848 |
|
| 849 |
ASSERT_OK(chase_and_opendirat(XAT_FDROOT, tfd, "o/p/e/n/d/i", 0, &result, &dir)); |
| 850 |
FOREACH_DIRENT(de, dir, assert_not_reached()) |
| 851 |
ASSERT_STREQ(de->d_name, "r"); |
| 852 |
ASSERT_STREQ(result, "o/p/e/n/d/i"); |
| 853 |
result = mfree(result); |
| 854 |
|
| 855 |
/* Test chase_and_statat() */ |
| 856 |
|
| 857 |
ASSERT_OK(chase_and_statat(XAT_FDROOT, tfd, "o/p", 0, &result, &st)); |
| 858 |
ASSERT_OK(stat_verify_directory(&st)); |
| 859 |
ASSERT_STREQ(result, "o/p"); |
| 860 |
result = mfree(result); |
| 861 |
|
| 862 |
/* Test chase_and_accessat() */ |
| 863 |
|
| 864 |
ASSERT_OK(chase_and_accessat(XAT_FDROOT, tfd, "o/p/e", 0, F_OK, &result)); |
| 865 |
ASSERT_STREQ(result, "o/p/e"); |
| 866 |
result = mfree(result); |
| 867 |
|
| 868 |
/* Test chase_and_fopenat_unlocked() */ |
| 869 |
|
| 870 |
ASSERT_OK(chase_and_fopenat_unlocked(XAT_FDROOT, tfd, "o/p/e/n/f/i/l/e", 0, "re", &result, &f)); |
| 871 |
ASSERT_EQ(fread(&(char[1]) {}, 1, 1, f), 0u); |
| 872 |
ASSERT_TRUE(feof(f)); |
| 873 |
f = safe_fclose(f); |
| 874 |
ASSERT_STREQ(result, "o/p/e/n/f/i/l/e"); |
| 875 |
result = mfree(result); |
| 876 |
|
| 877 |
/* Test chase_and_unlinkat() */ |
| 878 |
|
| 879 |
ASSERT_OK(chase_and_unlinkat(XAT_FDROOT, tfd, "o/p/e/n/f/i/l/e", 0, 0, &result)); |
| 880 |
ASSERT_STREQ(result, "o/p/e/n/f/i/l/e"); |
| 881 |
result = mfree(result); |
| 882 |
|
| 883 |
/* Test chase_and_open_parent_at() */ |
| 884 |
|
| 885 |
fd = ASSERT_OK(chase_and_open_parent_at(XAT_FDROOT, tfd, "chase/parent", CHASE_NOFOLLOW, &result)); |
| 886 |
ASSERT_OK_ERRNO(faccessat(fd, result, F_OK, AT_SYMLINK_NOFOLLOW)); |
| 887 |
ASSERT_STREQ(result, "parent"); |
| 888 |
fd = safe_close(fd); |
| 889 |
result = mfree(result); |
| 890 |
|
| 891 |
fd = ASSERT_OK(chase_and_open_parent_at(XAT_FDROOT, tfd, "chase", 0, &result)); |
| 892 |
ASSERT_OK_ERRNO(faccessat(fd, result, F_OK, 0)); |
| 893 |
ASSERT_STREQ(result, "chase"); |
| 894 |
fd = safe_close(fd); |
| 895 |
result = mfree(result); |
| 896 |
|
| 897 |
fd = ASSERT_OK(chase_and_open_parent_at(XAT_FDROOT, tfd, "/", 0, &result)); |
| 898 |
ASSERT_STREQ(result, "."); |
| 899 |
fd = safe_close(fd); |
| 900 |
result = mfree(result); |
| 901 |
|
| 902 |
fd = ASSERT_OK(chase_and_open_parent_at(XAT_FDROOT, tfd, ".", 0, &result)); |
| 903 |
ASSERT_STREQ(result, "."); |
| 904 |
fd = safe_close(fd); |
| 905 |
result = mfree(result); |
| 906 |
} |
| 907 |
|
| 908 |
TEST(chaseat_separate_root_and_dir) { |
| 909 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 910 |
_cleanup_close_ int root_fd = -EBADF, sub_fd = -EBADF, fd = -EBADF; |
| 911 |
_cleanup_free_ char *result = NULL; |
| 912 |
|
| 913 |
/* Exercise chaseat() with root_fd != dir_fd. The root marks the chroot boundary (symlinks may |
| 914 |
* not escape it, absolute symlinks resolve to it), while dir_fd is the starting directory for |
| 915 |
* relative paths. */ |
| 916 |
|
| 917 |
root_fd = ASSERT_OK(mkdtemp_open(NULL, 0, &t)); |
| 918 |
|
| 919 |
/* Create a file at the root and a subdirectory containing another file. */ |
| 920 |
ASSERT_OK_ERRNO(mkdirat(root_fd, "sub", 0755)); |
| 921 |
sub_fd = ASSERT_OK_ERRNO(openat(root_fd, "sub", O_CLOEXEC|O_DIRECTORY|O_PATH)); |
| 922 |
|
| 923 |
fd = ASSERT_OK_ERRNO(openat(root_fd, "outside", O_CREAT|O_CLOEXEC, 0600)); |
| 924 |
fd = safe_close(fd); |
| 925 |
|
| 926 |
fd = ASSERT_OK_ERRNO(openat(sub_fd, "inside", O_CREAT|O_CLOEXEC, 0600)); |
| 927 |
fd = safe_close(fd); |
| 928 |
|
| 929 |
/* Relative lookup from sub_fd under root_fd finds sub's own files. */ |
| 930 |
ASSERT_OK(chaseat(root_fd, sub_fd, "inside", 0, &result, NULL)); |
| 931 |
ASSERT_STREQ(result, "inside"); |
| 932 |
result = mfree(result); |
| 933 |
|
| 934 |
/* Absolute path with dir_fd=sub_fd and root_fd set: path is relative to root_fd so "/inside" finds |
| 935 |
* nothing. */ |
| 936 |
ASSERT_ERROR(chaseat(root_fd, sub_fd, "/inside", 0, &result, NULL), ENOENT); |
| 937 |
ASSERT_OK_ZERO(chaseat(root_fd, sub_fd, "/inside", CHASE_NONEXISTENT, &result, NULL)); |
| 938 |
result = mfree(result); |
| 939 |
|
| 940 |
/* "../outside" from sub_fd goes up one level (within root), finds root's file. */ |
| 941 |
ASSERT_OK(chaseat(root_fd, sub_fd, "../outside", 0, &result, NULL)); |
| 942 |
ASSERT_STREQ(result, "../outside"); |
| 943 |
result = mfree(result); |
| 944 |
|
| 945 |
/* "../../../outside" cannot escape above root_fd — clamped. Still resolves to root's file. */ |
| 946 |
ASSERT_OK(chaseat(root_fd, sub_fd, "../../../outside", 0, &result, NULL)); |
| 947 |
ASSERT_STREQ(result, "../outside"); |
| 948 |
result = mfree(result); |
| 949 |
|
| 950 |
/* Absolute symlink inside sub pointing at "/outside" — with root_fd set, /outside resolves to |
| 951 |
* root_fd/outside, not the host's /outside. */ |
| 952 |
ASSERT_OK_ERRNO(symlinkat("/outside", sub_fd, "escape_abs")); |
| 953 |
ASSERT_OK(chaseat(root_fd, sub_fd, "escape_abs", 0, &result, &fd)); |
| 954 |
ASSERT_TRUE(inode_same_at(fd, NULL, root_fd, "outside", AT_EMPTY_PATH)); |
| 955 |
ASSERT_STREQ(result, "/outside"); |
| 956 |
result = mfree(result); |
| 957 |
fd = safe_close(fd); |
| 958 |
|
| 959 |
/* Relative symlink trying to escape via many ".." — also clamped to root. */ |
| 960 |
ASSERT_OK_ERRNO(symlinkat("../../../../../outside", sub_fd, "escape_rel")); |
| 961 |
ASSERT_OK(chaseat(root_fd, sub_fd, "escape_rel", 0, &result, NULL)); |
| 962 |
ASSERT_STREQ(result, "../outside"); |
| 963 |
result = mfree(result); |
| 964 |
|
| 965 |
/* Symlink pointing to an absolute host path that does NOT exist under our root must fail, not |
| 966 |
* leak to the host. /etc almost always exists on the host; under our tmp root it doesn't. */ |
| 967 |
ASSERT_OK_ERRNO(symlinkat("/etc", sub_fd, "escape_host")); |
| 968 |
ASSERT_ERROR(chaseat(root_fd, sub_fd, "escape_host/hosts", 0, NULL, NULL), ENOENT); |
| 969 |
|
| 970 |
/* Chasing just ".." from root_fd itself stays at root. */ |
| 971 |
ASSERT_OK(chaseat(root_fd, root_fd, "..", 0, &result, NULL)); |
| 972 |
ASSERT_STREQ(result, "."); |
| 973 |
result = mfree(result); |
| 974 |
|
| 975 |
/* (real-fd, XAT_FDROOT, relative): XAT_FDROOT as dir_fd redirects to root_fd, so relative |
| 976 |
* paths start at root_fd. Result is relative because root_fd is a non-host-root fd and |
| 977 |
* dir_fd (after redirection) equals root_fd. */ |
| 978 |
ASSERT_OK(chaseat(root_fd, XAT_FDROOT, "sub/inside", 0, &result, &fd)); |
| 979 |
ASSERT_TRUE(inode_same_at(fd, NULL, sub_fd, "inside", AT_EMPTY_PATH)); |
| 980 |
ASSERT_STREQ(result, "sub/inside"); |
| 981 |
fd = safe_close(fd); |
| 982 |
result = mfree(result); |
| 983 |
|
| 984 |
/* (real-fd, XAT_FDROOT, absolute): same as relative — absolute paths also resolve from |
| 985 |
* root_fd. Leading slash is stripped. */ |
| 986 |
ASSERT_OK(chaseat(root_fd, XAT_FDROOT, "/sub/inside", 0, &result, &fd)); |
| 987 |
ASSERT_TRUE(inode_same_at(fd, NULL, sub_fd, "inside", AT_EMPTY_PATH)); |
| 988 |
ASSERT_STREQ(result, "sub/inside"); |
| 989 |
fd = safe_close(fd); |
| 990 |
result = mfree(result); |
| 991 |
|
| 992 |
/* (real-fd, XAT_FDROOT, absolute) resolving to root: "/outside" lives directly under |
| 993 |
* root_fd. */ |
| 994 |
ASSERT_OK(chaseat(root_fd, XAT_FDROOT, "/outside", 0, &result, &fd)); |
| 995 |
ASSERT_TRUE(inode_same_at(fd, NULL, root_fd, "outside", AT_EMPTY_PATH)); |
| 996 |
ASSERT_STREQ(result, "outside"); |
| 997 |
fd = safe_close(fd); |
| 998 |
result = mfree(result); |
| 999 |
|
| 1000 |
/* (real-fd, XAT_FDROOT) with an absolute symlink: the symlink target "/outside" resolves |
| 1001 |
* relative to root_fd, not the host root. Since dir_fd == root_fd (XAT_FDROOT was redirected), |
| 1002 |
* the result stays relative. */ |
| 1003 |
ASSERT_OK(chaseat(root_fd, XAT_FDROOT, "sub/escape_abs", 0, &result, &fd)); |
| 1004 |
ASSERT_TRUE(inode_same_at(fd, NULL, root_fd, "outside", AT_EMPTY_PATH)); |
| 1005 |
ASSERT_STREQ(result, "outside"); |
| 1006 |
fd = safe_close(fd); |
| 1007 |
result = mfree(result); |
| 1008 |
|
| 1009 |
/* (real-fd, XAT_FDROOT) with non-existent path. */ |
| 1010 |
ASSERT_OK_ZERO(chaseat(root_fd, XAT_FDROOT, "/nonexistent", CHASE_NONEXISTENT, &result, NULL)); |
| 1011 |
ASSERT_STREQ(result, "nonexistent"); |
| 1012 |
result = mfree(result); |
| 1013 |
ASSERT_ERROR(chaseat(root_fd, XAT_FDROOT, "/nonexistent", 0, NULL, NULL), ENOENT); |
| 1014 |
} |
| 1015 |
|
| 1016 |
TEST(chaseat_prefix_root) { |
| 1017 |
_cleanup_free_ char *cwd = NULL, *ret = NULL, *expected = NULL; |
| 1018 |
|
| 1019 |
ASSERT_OK(safe_getcwd(&cwd)); |
| 1020 |
|
| 1021 |
ASSERT_OK(chaseat_prefix_root("/hoge", NULL, &ret)); |
| 1022 |
ASSERT_STREQ(ret, "/hoge"); |
| 1023 |
|
| 1024 |
ret = mfree(ret); |
| 1025 |
|
| 1026 |
ASSERT_OK(chaseat_prefix_root("/hoge", "a/b/c", &ret)); |
| 1027 |
ASSERT_STREQ(ret, "/hoge"); |
| 1028 |
|
| 1029 |
ret = mfree(ret); |
| 1030 |
|
| 1031 |
ASSERT_OK(chaseat_prefix_root("hoge", "/a/b//./c///", &ret)); |
| 1032 |
ASSERT_STREQ(ret, "/a/b/c/hoge"); |
| 1033 |
|
| 1034 |
ret = mfree(ret); |
| 1035 |
|
| 1036 |
ASSERT_OK(chaseat_prefix_root("hoge", "a/b//./c///", &ret)); |
| 1037 |
expected = ASSERT_NOT_NULL(path_join(cwd, "a/b/c/hoge")); |
| 1038 |
ASSERT_STREQ(ret, expected); |
| 1039 |
|
| 1040 |
ret = mfree(ret); |
| 1041 |
expected = mfree(expected); |
| 1042 |
|
| 1043 |
ASSERT_OK(chaseat_prefix_root("./hoge/aaa/../././b", "/a/b//./c///", &ret)); |
| 1044 |
ASSERT_STREQ(ret, "/a/b/c/hoge/aaa/../././b"); |
| 1045 |
|
| 1046 |
ret = mfree(ret); |
| 1047 |
|
| 1048 |
ASSERT_OK(chaseat_prefix_root("./hoge/aaa/../././b", "a/b//./c///", &ret)); |
| 1049 |
expected = ASSERT_NOT_NULL(path_join(cwd, "a/b/c/hoge/aaa/../././b")); |
| 1050 |
ASSERT_STREQ(ret, expected); |
| 1051 |
} |
| 1052 |
|
| 1053 |
TEST(trailing_dot_dot) { |
| 1054 |
_cleanup_free_ char *path = NULL, *fdpath = NULL; |
| 1055 |
_cleanup_close_ int fd = -EBADF; |
| 1056 |
|
| 1057 |
ASSERT_OK(chase("/usr/..", NULL, CHASE_PARENT, &path, &fd)); |
| 1058 |
ASSERT_PATH_EQ(path, "/"); |
| 1059 |
ASSERT_OK(fd_get_path(fd, &fdpath)); |
| 1060 |
ASSERT_PATH_EQ(fdpath, "/"); |
| 1061 |
|
| 1062 |
path = mfree(path); |
| 1063 |
fdpath = mfree(fdpath); |
| 1064 |
fd = safe_close(fd); |
| 1065 |
|
| 1066 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 1067 |
ASSERT_OK(mkdtemp_malloc(NULL, &t)); |
| 1068 |
_cleanup_free_ char *sub = ASSERT_PTR(path_join(t, "a/b/c/d")); |
| 1069 |
ASSERT_OK(mkdir_p(sub, 0700)); |
| 1070 |
_cleanup_free_ char *suffixed = ASSERT_PTR(path_join(sub, "..")); |
| 1071 |
ASSERT_OK(chase(suffixed, NULL, CHASE_PARENT, &path, &fd)); |
| 1072 |
_cleanup_free_ char *expected1 = ASSERT_PTR(path_join(t, "a/b/c")); |
| 1073 |
_cleanup_free_ char *expected2 = ASSERT_PTR(path_join(t, "a/b")); |
| 1074 |
|
| 1075 |
ASSERT_PATH_EQ(path, expected1); |
| 1076 |
ASSERT_OK(fd_get_path(fd, &fdpath)); |
| 1077 |
ASSERT_PATH_EQ(fdpath, expected2); |
| 1078 |
} |
| 1079 |
|
| 1080 |
TEST(use_chase_as_mkdir_p) { |
| 1081 |
_cleanup_free_ char *p = NULL; |
| 1082 |
ASSERT_OK_ERRNO(asprintf(&p, "/tmp/chasemkdir%" PRIu64 "/a/b/c", random_u64())); |
| 1083 |
|
| 1084 |
_cleanup_close_ int fd = -EBADF; |
| 1085 |
ASSERT_OK(chase(p, NULL, CHASE_PREFIX_ROOT|CHASE_MKDIR_0755, NULL, &fd)); |
| 1086 |
|
| 1087 |
ASSERT_OK_EQ(inode_same_at(AT_FDCWD, p, fd, NULL, AT_EMPTY_PATH), 1); |
| 1088 |
|
| 1089 |
_cleanup_close_ int fd2 = -EBADF; |
| 1090 |
ASSERT_OK(chase(p, p, CHASE_PREFIX_ROOT|CHASE_MKDIR_0755, NULL, &fd2)); |
| 1091 |
|
| 1092 |
_cleanup_free_ char *pp = ASSERT_PTR(path_join(p, p)); |
| 1093 |
|
| 1094 |
ASSERT_OK_EQ(inode_same_at(AT_FDCWD, pp, fd2, NULL, AT_EMPTY_PATH), 1); |
| 1095 |
|
| 1096 |
_cleanup_free_ char *f = NULL; |
| 1097 |
ASSERT_OK(path_extract_directory(p, &f)); |
| 1098 |
|
| 1099 |
_cleanup_free_ char *ff = NULL; |
| 1100 |
ASSERT_OK(path_extract_directory(f, &ff)); |
| 1101 |
|
| 1102 |
_cleanup_free_ char *fff = NULL; |
| 1103 |
ASSERT_OK(path_extract_directory(ff, &fff)); |
| 1104 |
|
| 1105 |
ASSERT_OK(rm_rf(fff, REMOVE_PHYSICAL)); |
| 1106 |
} |
| 1107 |
|
| 1108 |
static int intro(void) { |
| 1109 |
arg_test_dir = saved_argv[1]; |
| 1110 |
return EXIT_SUCCESS; |
| 1111 |
} |
| 1112 |
|
| 1113 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |