| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/eventfd.h> |
| 5 |
#include <sys/mount.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "fs-util.h" |
| 11 |
#include "memfd-util.h" |
| 12 |
#include "mkdir.h" |
| 13 |
#include "mount-util.h" |
| 14 |
#include "namespace-util.h" |
| 15 |
#include "path-util.h" |
| 16 |
#include "process-util.h" |
| 17 |
#include "random-util.h" |
| 18 |
#include "rlimit-util.h" |
| 19 |
#include "rm-rf.h" |
| 20 |
#include "serialize.h" |
| 21 |
#include "stat-util.h" |
| 22 |
#include "tests.h" |
| 23 |
#include "tmpfile-util.h" |
| 24 |
|
| 25 |
TEST(close_many) { |
| 26 |
int fds[3]; |
| 27 |
_cleanup_(unlink_tempfilep) char name0[] = "/tmp/test-close-many.XXXXXX"; |
| 28 |
_cleanup_(unlink_tempfilep) char name1[] = "/tmp/test-close-many.XXXXXX"; |
| 29 |
_cleanup_(unlink_tempfilep) char name2[] = "/tmp/test-close-many.XXXXXX"; |
| 30 |
|
| 31 |
fds[0] = mkostemp_safe(name0); |
| 32 |
fds[1] = mkostemp_safe(name1); |
| 33 |
fds[2] = mkostemp_safe(name2); |
| 34 |
|
| 35 |
close_many(fds, 2); |
| 36 |
|
| 37 |
assert_se(fd_validate(fds[0]) == -EBADF); |
| 38 |
assert_se(fd_validate(fds[1]) == -EBADF); |
| 39 |
assert_se(fd_validate(fds[2]) >= 0); |
| 40 |
|
| 41 |
safe_close(fds[2]); |
| 42 |
} |
| 43 |
|
| 44 |
TEST(close_nointr) { |
| 45 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-test-close_nointr.XXXXXX"; |
| 46 |
int fd; |
| 47 |
|
| 48 |
fd = mkostemp_safe(name); |
| 49 |
assert_se(fd >= 0); |
| 50 |
assert_se(close_nointr(fd) >= 0); |
| 51 |
assert_se(close_nointr(fd) < 0); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(fd_validate) { |
| 55 |
assert_se(fd_validate(-EINVAL) == -EBADF); |
| 56 |
assert_se(fd_validate(-EBADF) == -EBADF); |
| 57 |
|
| 58 |
_cleanup_close_ int b = -EBADF; |
| 59 |
assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0); |
| 60 |
|
| 61 |
assert_se(fd_validate(b) == 0); |
| 62 |
safe_close(b); |
| 63 |
assert_se(fd_validate(b) == -EBADF); |
| 64 |
TAKE_FD(b); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(same_fd) { |
| 68 |
_cleanup_close_pair_ int p[2]; |
| 69 |
_cleanup_close_ int a, b, c, d, e; |
| 70 |
|
| 71 |
assert_se(pipe2(p, O_CLOEXEC) >= 0); |
| 72 |
assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0); |
| 73 |
assert_se((b = open("/bin/sh", O_RDONLY|O_CLOEXEC)) >= 0); |
| 74 |
assert_se((c = fcntl(a, F_DUPFD, 3)) >= 0); |
| 75 |
assert_se((d = open("/bin/sh", O_RDONLY|O_CLOEXEC|O_PATH)) >= 0); /* O_PATH changes error returns in F_DUPFD_QUERY, let's test explicitly */ |
| 76 |
assert_se((e = fcntl(d, F_DUPFD, 3)) >= 0); |
| 77 |
|
| 78 |
assert_se(same_fd(p[0], p[0]) > 0); |
| 79 |
assert_se(same_fd(p[1], p[1]) > 0); |
| 80 |
assert_se(same_fd(a, a) > 0); |
| 81 |
assert_se(same_fd(b, b) > 0); |
| 82 |
|
| 83 |
assert_se(same_fd(a, p[0]) > 0); |
| 84 |
assert_se(same_fd(p[0], a) > 0); |
| 85 |
assert_se(same_fd(c, p[0]) > 0); |
| 86 |
assert_se(same_fd(p[0], c) > 0); |
| 87 |
assert_se(same_fd(a, c) > 0); |
| 88 |
assert_se(same_fd(c, a) > 0); |
| 89 |
|
| 90 |
assert_se(same_fd(p[0], p[1]) == 0); |
| 91 |
assert_se(same_fd(p[1], p[0]) == 0); |
| 92 |
assert_se(same_fd(p[0], b) == 0); |
| 93 |
assert_se(same_fd(b, p[0]) == 0); |
| 94 |
assert_se(same_fd(p[1], a) == 0); |
| 95 |
assert_se(same_fd(a, p[1]) == 0); |
| 96 |
assert_se(same_fd(p[1], b) == 0); |
| 97 |
assert_se(same_fd(b, p[1]) == 0); |
| 98 |
|
| 99 |
assert_se(same_fd(a, b) == 0); |
| 100 |
assert_se(same_fd(b, a) == 0); |
| 101 |
|
| 102 |
assert_se(same_fd(a, d) == 0); |
| 103 |
assert_se(same_fd(d, a) == 0); |
| 104 |
assert_se(same_fd(d, d) > 0); |
| 105 |
assert_se(same_fd(d, e) > 0); |
| 106 |
assert_se(same_fd(e, d) > 0); |
| 107 |
|
| 108 |
/* Let's now compare with a valid fd nr, that is definitely closed, and verify it returns the right error code */ |
| 109 |
safe_close(d); |
| 110 |
assert_se(same_fd(d, d) == -EBADF); |
| 111 |
assert_se(same_fd(e, d) == -EBADF); |
| 112 |
assert_se(same_fd(d, e) == -EBADF); |
| 113 |
assert_se(same_fd(e, e) > 0); |
| 114 |
TAKE_FD(d); |
| 115 |
} |
| 116 |
|
| 117 |
TEST(open_serialization_fd) { |
| 118 |
_cleanup_close_ int fd = -EBADF; |
| 119 |
|
| 120 |
fd = open_serialization_fd("test"); |
| 121 |
assert_se(fd >= 0); |
| 122 |
|
| 123 |
assert_se(write(fd, "test\n", 5) == 5); |
| 124 |
|
| 125 |
assert_se(finish_serialization_fd(fd) >= 0); |
| 126 |
} |
| 127 |
|
| 128 |
TEST(open_serialization_file) { |
| 129 |
_cleanup_fclose_ FILE *f = NULL; |
| 130 |
int r; |
| 131 |
|
| 132 |
r = open_serialization_file("test", &f); |
| 133 |
assert_se(r >= 0); |
| 134 |
assert_se(f); |
| 135 |
|
| 136 |
assert_se(fwrite("test\n", 1, 5, f) == 5); |
| 137 |
|
| 138 |
assert_se(finish_serialization_file(f) >= 0); |
| 139 |
} |
| 140 |
|
| 141 |
TEST(fd_move_above_stdio) { |
| 142 |
int original_stdin, new_fd; |
| 143 |
|
| 144 |
original_stdin = fcntl(0, F_DUPFD, 3); |
| 145 |
assert_se(original_stdin >= 3); |
| 146 |
assert_se(close_nointr(0) != EBADF); |
| 147 |
|
| 148 |
new_fd = open("/dev/null", O_RDONLY); |
| 149 |
assert_se(new_fd == 0); |
| 150 |
|
| 151 |
new_fd = fd_move_above_stdio(new_fd); |
| 152 |
assert_se(new_fd >= 3); |
| 153 |
|
| 154 |
assert_se(fcntl(original_stdin, F_DUPFD, 0) == 0); |
| 155 |
assert_se(close_nointr(original_stdin) != EBADF); |
| 156 |
assert_se(close_nointr(new_fd) != EBADF); |
| 157 |
} |
| 158 |
|
| 159 |
TEST(rearrange_stdio) { |
| 160 |
int r; |
| 161 |
|
| 162 |
r = pidref_safe_fork("rearrange", FORK_WAIT|FORK_LOG, NULL); |
| 163 |
assert_se(r >= 0); |
| 164 |
|
| 165 |
if (r == 0) { |
| 166 |
_cleanup_free_ char *path = NULL; |
| 167 |
int pipe_read_fd, pair[2]; |
| 168 |
char buffer[10]; |
| 169 |
|
| 170 |
/* Child */ |
| 171 |
|
| 172 |
safe_close(STDERR_FILENO); /* Let's close an fd < 2, to make it more interesting */ |
| 173 |
|
| 174 |
assert_se(rearrange_stdio(-EBADF, -EBADF, -EBADF) >= 0); |
| 175 |
/* Reconfigure logging after rearranging stdout/stderr, so we still log to somewhere if the |
| 176 |
* following tests fail, making it slightly less annoying to debug */ |
| 177 |
log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); |
| 178 |
log_open(); |
| 179 |
|
| 180 |
assert_se(fd_get_path(STDIN_FILENO, &path) >= 0); |
| 181 |
assert_se(path_equal(path, "/dev/null")); |
| 182 |
path = mfree(path); |
| 183 |
|
| 184 |
assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0); |
| 185 |
assert_se(path_equal(path, "/dev/null")); |
| 186 |
path = mfree(path); |
| 187 |
|
| 188 |
assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0); |
| 189 |
assert_se(path_equal(path, "/dev/null")); |
| 190 |
path = mfree(path); |
| 191 |
|
| 192 |
safe_close(STDIN_FILENO); |
| 193 |
safe_close(STDOUT_FILENO); |
| 194 |
safe_close(STDERR_FILENO); |
| 195 |
|
| 196 |
assert_se(pipe(pair) >= 0); |
| 197 |
assert_se(pair[0] == 0); |
| 198 |
assert_se(pair[1] == 1); |
| 199 |
pipe_read_fd = fd_move_above_stdio(0); |
| 200 |
assert_se(pipe_read_fd >= 3); |
| 201 |
|
| 202 |
assert_se(open("/dev/full", O_WRONLY|O_CLOEXEC) == 0); |
| 203 |
assert_se(memfd_new_and_seal_string("data", "foobar") == 2); |
| 204 |
|
| 205 |
assert_se(rearrange_stdio(2, 0, 1) >= 0); |
| 206 |
|
| 207 |
assert_se(write(1, "x", 1) < 0 && errno == ENOSPC); |
| 208 |
assert_se(write(2, "z", 1) == 1); |
| 209 |
assert_se(read(pipe_read_fd, buffer, sizeof(buffer)) == 1); |
| 210 |
assert_se(buffer[0] == 'z'); |
| 211 |
assert_se(read(0, buffer, sizeof(buffer)) == 6); |
| 212 |
assert_se(memcmp(buffer, "foobar", 6) == 0); |
| 213 |
|
| 214 |
assert_se(rearrange_stdio(-EBADF, 1, 2) >= 0); |
| 215 |
assert_se(write(1, "a", 1) < 0 && errno == ENOSPC); |
| 216 |
assert_se(write(2, "y", 1) == 1); |
| 217 |
assert_se(read(pipe_read_fd, buffer, sizeof(buffer)) == 1); |
| 218 |
assert_se(buffer[0] == 'y'); |
| 219 |
|
| 220 |
assert_se(fd_get_path(0, &path) >= 0); |
| 221 |
assert_se(path_equal(path, "/dev/null")); |
| 222 |
path = mfree(path); |
| 223 |
|
| 224 |
_exit(EXIT_SUCCESS); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
TEST(read_nr_open) { |
| 229 |
log_info("nr-open: %u", read_nr_open()); |
| 230 |
} |
| 231 |
|
| 232 |
static size_t validate_fds( |
| 233 |
bool opened, |
| 234 |
const int *fds, |
| 235 |
size_t n_fds) { |
| 236 |
|
| 237 |
size_t c = 0; |
| 238 |
|
| 239 |
/* Validates that fds in the specified array are one of the following three: |
| 240 |
* |
| 241 |
* 1. < 0 (test is skipped) or |
| 242 |
* 2. opened (if 'opened' param is true) or |
| 243 |
* 3. closed (if 'opened' param is false) |
| 244 |
*/ |
| 245 |
|
| 246 |
for (size_t i = 0; i < n_fds; i++) { |
| 247 |
if (fds[i] < 0) |
| 248 |
continue; |
| 249 |
|
| 250 |
if (opened) |
| 251 |
assert_se(fd_validate(fds[i]) >= 0); |
| 252 |
else |
| 253 |
assert_se(fd_validate(fds[i]) == -EBADF); |
| 254 |
|
| 255 |
c++; |
| 256 |
} |
| 257 |
|
| 258 |
return c; /* Return number of fds >= 0 in the array */ |
| 259 |
} |
| 260 |
|
| 261 |
static void test_close_all_fds_inner(int (*func)(const int except[], size_t n_except)) { |
| 262 |
_cleanup_free_ int *fds = NULL, *keep = NULL; |
| 263 |
size_t n_fds, n_keep; |
| 264 |
int max_fd; |
| 265 |
|
| 266 |
log_info("/* %s */", __func__); |
| 267 |
|
| 268 |
rlimit_nofile_bump(-1); |
| 269 |
|
| 270 |
max_fd = get_max_fd(); |
| 271 |
assert_se(max_fd > 10); |
| 272 |
|
| 273 |
if (max_fd > 7000) { |
| 274 |
/* If the worst fallback is activated we need to iterate through all possible fds, hence, |
| 275 |
* let's lower the limit a small bit, so that we don't run for too long. Yes, this undoes the |
| 276 |
* rlimit_nofile_bump() call above partially. */ |
| 277 |
|
| 278 |
(void) setrlimit_closest(RLIMIT_NOFILE, &(struct rlimit) { 7000, 7000 }); |
| 279 |
max_fd = 7000; |
| 280 |
} |
| 281 |
|
| 282 |
/* Try to use 5000 fds, but when we can't bump the rlimit to make that happen use the whole limit minus 10 */ |
| 283 |
n_fds = MIN(((size_t) max_fd & ~1U) - 10U, 5000U); |
| 284 |
assert_se((n_fds & 1U) == 0U); /* make sure even number of fds */ |
| 285 |
|
| 286 |
/* Allocate the determined number of fds, always two at a time */ |
| 287 |
assert_se(fds = new(int, n_fds)); |
| 288 |
for (size_t i = 0; i < n_fds; i += 2) |
| 289 |
assert_se(pipe2(fds + i, O_CLOEXEC) >= 0); |
| 290 |
|
| 291 |
/* Validate this worked */ |
| 292 |
assert_se(validate_fds(true, fds, n_fds) == n_fds); |
| 293 |
|
| 294 |
/* Randomized number of fds to keep, but at most every second */ |
| 295 |
n_keep = (random_u64() % (n_fds / 2)); |
| 296 |
|
| 297 |
/* Now randomly select a number of fds from the array above to keep */ |
| 298 |
assert_se(keep = new(int, n_keep)); |
| 299 |
for (size_t k = 0; k < n_keep; k++) { |
| 300 |
for (;;) { |
| 301 |
size_t p; |
| 302 |
|
| 303 |
p = random_u64() % n_fds; |
| 304 |
if (fds[p] >= 0) { |
| 305 |
keep[k] = TAKE_FD(fds[p]); |
| 306 |
break; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/* Check that all fds from both arrays are still open, and test how many in each are >= 0 */ |
| 312 |
assert_se(validate_fds(true, fds, n_fds) == n_fds - n_keep); |
| 313 |
assert_se(validate_fds(true, keep, n_keep) == n_keep); |
| 314 |
|
| 315 |
/* Close logging fd first, so that we don't confuse it by closing its fd */ |
| 316 |
log_close(); |
| 317 |
log_set_open_when_needed(true); |
| 318 |
log_settle_target(); |
| 319 |
|
| 320 |
/* Close all but the ones to keep */ |
| 321 |
ASSERT_OK(func(keep, n_keep)); |
| 322 |
|
| 323 |
assert_se(validate_fds(false, fds, n_fds) == n_fds - n_keep); |
| 324 |
assert_se(validate_fds(true, keep, n_keep) == n_keep); |
| 325 |
|
| 326 |
/* Close everything else too! */ |
| 327 |
ASSERT_OK(func(NULL, 0)); |
| 328 |
|
| 329 |
assert_se(validate_fds(false, fds, n_fds) == n_fds - n_keep); |
| 330 |
assert_se(validate_fds(false, keep, n_keep) == n_keep); |
| 331 |
|
| 332 |
log_set_open_when_needed(false); |
| 333 |
log_open(); |
| 334 |
} |
| 335 |
|
| 336 |
TEST(close_all_fds) { |
| 337 |
ForkFlags flags = FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT; |
| 338 |
int r; |
| 339 |
|
| 340 |
r = ASSERT_OK(pidref_safe_fork("(caf-plain)", flags, NULL)); |
| 341 |
if (r == 0) { |
| 342 |
test_close_all_fds_inner(close_all_fds); |
| 343 |
_exit(EXIT_SUCCESS); |
| 344 |
} |
| 345 |
|
| 346 |
r = ASSERT_OK(pidref_safe_fork("(caf-nomalloc)", flags, NULL)); |
| 347 |
if (r == 0) { |
| 348 |
test_close_all_fds_inner(close_all_fds_without_malloc); |
| 349 |
_exit(EXIT_SUCCESS); |
| 350 |
} |
| 351 |
|
| 352 |
r = ASSERT_OK(pidref_safe_fork("(caf-frugal)", flags, NULL)); |
| 353 |
if (r == 0) { |
| 354 |
test_close_all_fds_inner(close_all_fds_frugal); |
| 355 |
_exit(EXIT_SUCCESS); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
TEST(format_proc_fd_path) { |
| 360 |
ASSERT_STREQ(FORMAT_PROC_FD_PATH(0), "/proc/self/fd/0"); |
| 361 |
ASSERT_STREQ(FORMAT_PROC_FD_PATH(1), "/proc/self/fd/1"); |
| 362 |
ASSERT_STREQ(FORMAT_PROC_FD_PATH(2), "/proc/self/fd/2"); |
| 363 |
ASSERT_STREQ(FORMAT_PROC_FD_PATH(3), "/proc/self/fd/3"); |
| 364 |
ASSERT_STREQ(FORMAT_PROC_FD_PATH(2147483647), "/proc/self/fd/2147483647"); |
| 365 |
} |
| 366 |
|
| 367 |
TEST(fd_reopen) { |
| 368 |
_cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF; |
| 369 |
struct stat st1, st2; |
| 370 |
int fl; |
| 371 |
|
| 372 |
/* Test this with a directory */ |
| 373 |
fd1 = open("/proc", O_DIRECTORY|O_PATH|O_CLOEXEC); |
| 374 |
assert_se(fd1 >= 0); |
| 375 |
|
| 376 |
ASSERT_OK_ERRNO(fstat(fd1, &st1)); |
| 377 |
assert_se(S_ISDIR(st1.st_mode)); |
| 378 |
|
| 379 |
fl = fcntl(fd1, F_GETFL); |
| 380 |
assert_se(fl >= 0); |
| 381 |
assert_se(FLAGS_SET(fl, O_DIRECTORY)); |
| 382 |
assert_se(FLAGS_SET(fl, O_PATH)); |
| 383 |
|
| 384 |
/* fd_reopen() with O_NOFOLLOW will systematically fail, since it is implemented via a symlink in /proc/self/fd/ */ |
| 385 |
assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC|O_NOFOLLOW) == -ELOOP); |
| 386 |
assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW) == -ELOOP); |
| 387 |
|
| 388 |
fd2 = fd_reopen(fd1, O_RDONLY|O_DIRECTORY|O_CLOEXEC); /* drop the O_PATH */ |
| 389 |
assert_se(fd2 >= 0); |
| 390 |
|
| 391 |
ASSERT_OK_ERRNO(fstat(fd2, &st2)); |
| 392 |
assert_se(S_ISDIR(st2.st_mode)); |
| 393 |
assert_se(stat_inode_same(&st1, &st2)); |
| 394 |
|
| 395 |
fl = fcntl(fd2, F_GETFL); |
| 396 |
assert_se(fl >= 0); |
| 397 |
assert_se(FLAGS_SET(fl, O_DIRECTORY)); |
| 398 |
assert_se(!FLAGS_SET(fl, O_PATH)); |
| 399 |
|
| 400 |
safe_close(fd1); |
| 401 |
|
| 402 |
fd1 = fd_reopen(fd2, O_DIRECTORY|O_PATH|O_CLOEXEC); /* reacquire the O_PATH */ |
| 403 |
assert_se(fd1 >= 0); |
| 404 |
|
| 405 |
ASSERT_OK_ERRNO(fstat(fd1, &st1)); |
| 406 |
assert_se(S_ISDIR(st1.st_mode)); |
| 407 |
assert_se(stat_inode_same(&st1, &st2)); |
| 408 |
|
| 409 |
fl = fcntl(fd1, F_GETFL); |
| 410 |
assert_se(fl >= 0); |
| 411 |
assert_se(FLAGS_SET(fl, O_DIRECTORY)); |
| 412 |
assert_se(FLAGS_SET(fl, O_PATH)); |
| 413 |
|
| 414 |
safe_close(fd1); |
| 415 |
|
| 416 |
/* And now, test this with a file. */ |
| 417 |
fd1 = open("/proc/version", O_PATH|O_CLOEXEC); |
| 418 |
assert_se(fd1 >= 0); |
| 419 |
|
| 420 |
ASSERT_OK_ERRNO(fstat(fd1, &st1)); |
| 421 |
assert_se(S_ISREG(st1.st_mode)); |
| 422 |
|
| 423 |
fl = fcntl(fd1, F_GETFL); |
| 424 |
assert_se(fl >= 0); |
| 425 |
assert_se(!FLAGS_SET(fl, O_DIRECTORY)); |
| 426 |
assert_se(FLAGS_SET(fl, O_PATH)); |
| 427 |
|
| 428 |
assert_se(fd_reopen(fd1, O_RDONLY|O_DIRECTORY|O_CLOEXEC) == -ENOTDIR); |
| 429 |
fd2 = fd_reopen(fd1, O_RDONLY|O_CLOEXEC); /* drop the O_PATH */ |
| 430 |
assert_se(fd2 >= 0); |
| 431 |
|
| 432 |
ASSERT_OK_ERRNO(fstat(fd2, &st2)); |
| 433 |
assert_se(S_ISREG(st2.st_mode)); |
| 434 |
assert_se(stat_inode_same(&st1, &st2)); |
| 435 |
|
| 436 |
fl = fcntl(fd2, F_GETFL); |
| 437 |
assert_se(fl >= 0); |
| 438 |
assert_se(!FLAGS_SET(fl, O_DIRECTORY)); |
| 439 |
assert_se(!FLAGS_SET(fl, O_PATH)); |
| 440 |
|
| 441 |
safe_close(fd1); |
| 442 |
|
| 443 |
assert_se(fd_reopen(fd2, O_DIRECTORY|O_PATH|O_CLOEXEC) == -ENOTDIR); |
| 444 |
fd1 = fd_reopen(fd2, O_PATH|O_CLOEXEC); /* reacquire the O_PATH */ |
| 445 |
assert_se(fd1 >= 0); |
| 446 |
|
| 447 |
ASSERT_OK_ERRNO(fstat(fd1, &st1)); |
| 448 |
assert_se(S_ISREG(st1.st_mode)); |
| 449 |
assert_se(stat_inode_same(&st1, &st2)); |
| 450 |
|
| 451 |
fl = fcntl(fd1, F_GETFL); |
| 452 |
assert_se(fl >= 0); |
| 453 |
assert_se(!FLAGS_SET(fl, O_DIRECTORY)); |
| 454 |
assert_se(FLAGS_SET(fl, O_PATH)); |
| 455 |
|
| 456 |
/* Also check the right error is generated if the fd is already closed */ |
| 457 |
safe_close(fd1); |
| 458 |
assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC) == -EBADF); |
| 459 |
fd1 = -EBADF; |
| 460 |
|
| 461 |
/* Validate what happens if we reopen a symlink */ |
| 462 |
fd1 = open("/proc/self", O_PATH|O_CLOEXEC|O_NOFOLLOW); |
| 463 |
assert_se(fd1 >= 0); |
| 464 |
ASSERT_OK_ERRNO(fstat(fd1, &st1)); |
| 465 |
assert_se(S_ISLNK(st1.st_mode)); |
| 466 |
|
| 467 |
fd2 = fd_reopen(fd1, O_PATH|O_CLOEXEC); |
| 468 |
assert_se(fd2 >= 0); |
| 469 |
ASSERT_OK_ERRNO(fstat(fd2, &st2)); |
| 470 |
assert_se(S_ISLNK(st2.st_mode)); |
| 471 |
assert_se(stat_inode_same(&st1, &st2)); |
| 472 |
fd2 = safe_close(fd2); |
| 473 |
|
| 474 |
/* So here's the thing: if we have an O_PATH fd to a symlink, we *cannot* convert it to a regular fd |
| 475 |
* with that. i.e. you cannot have the VFS follow a symlink pinned via an O_PATH fd. */ |
| 476 |
assert_se(fd_reopen(fd1, O_RDONLY|O_CLOEXEC) == -ELOOP); |
| 477 |
} |
| 478 |
|
| 479 |
TEST(fd_reopen_condition) { |
| 480 |
_cleanup_close_ int fd1 = -EBADF, fd3 = -EBADF; |
| 481 |
int fd2, fl; |
| 482 |
|
| 483 |
/* Open without O_PATH */ |
| 484 |
fd1 = open("/usr/", O_RDONLY|O_DIRECTORY|O_CLOEXEC); |
| 485 |
assert_se(fd1 >= 0); |
| 486 |
|
| 487 |
fl = fcntl(fd1, F_GETFL); |
| 488 |
assert_se(FLAGS_SET(fl, O_DIRECTORY)); |
| 489 |
assert_se(!FLAGS_SET(fl, O_PATH)); |
| 490 |
|
| 491 |
fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3); |
| 492 |
assert_se(fd2 == fd1); |
| 493 |
assert_se(fd3 < 0); |
| 494 |
|
| 495 |
/* Switch on O_PATH */ |
| 496 |
fd2 = fd_reopen_condition(fd1, O_DIRECTORY|O_PATH, O_DIRECTORY|O_PATH, &fd3); |
| 497 |
assert_se(fd2 != fd1); |
| 498 |
assert_se(fd3 == fd2); |
| 499 |
|
| 500 |
fl = fcntl(fd2, F_GETFL); |
| 501 |
assert_se(FLAGS_SET(fl, O_DIRECTORY)); |
| 502 |
assert_se(FLAGS_SET(fl, O_PATH)); |
| 503 |
|
| 504 |
close_and_replace(fd1, fd3); |
| 505 |
|
| 506 |
fd2 = fd_reopen_condition(fd1, O_DIRECTORY|O_PATH, O_DIRECTORY|O_PATH, &fd3); |
| 507 |
assert_se(fd2 == fd1); |
| 508 |
assert_se(fd3 < 0); |
| 509 |
|
| 510 |
/* Switch off O_PATH again */ |
| 511 |
fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3); |
| 512 |
assert_se(fd2 != fd1); |
| 513 |
assert_se(fd3 == fd2); |
| 514 |
|
| 515 |
fl = fcntl(fd2, F_GETFL); |
| 516 |
assert_se(FLAGS_SET(fl, O_DIRECTORY)); |
| 517 |
assert_se(!FLAGS_SET(fl, O_PATH)); |
| 518 |
|
| 519 |
close_and_replace(fd1, fd3); |
| 520 |
|
| 521 |
fd2 = fd_reopen_condition(fd1, O_DIRECTORY, O_DIRECTORY|O_PATH, &fd3); |
| 522 |
assert_se(fd2 == fd1); |
| 523 |
assert_se(fd3 < 0); |
| 524 |
} |
| 525 |
|
| 526 |
TEST(take_fd) { |
| 527 |
_cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF; |
| 528 |
int array[2] = EBADF_PAIR, i = 0; |
| 529 |
|
| 530 |
assert_se(fd1 == -EBADF); |
| 531 |
assert_se(fd2 == -EBADF); |
| 532 |
|
| 533 |
fd1 = eventfd(0, EFD_CLOEXEC); |
| 534 |
assert_se(fd1 >= 0); |
| 535 |
|
| 536 |
fd2 = TAKE_FD(fd1); |
| 537 |
assert_se(fd1 == -EBADF); |
| 538 |
assert_se(fd2 >= 0); |
| 539 |
|
| 540 |
assert_se(array[0] == -EBADF); |
| 541 |
assert_se(array[1] == -EBADF); |
| 542 |
|
| 543 |
array[0] = TAKE_FD(fd2); |
| 544 |
assert_se(fd1 == -EBADF); |
| 545 |
assert_se(fd2 == -EBADF); |
| 546 |
assert_se(array[0] >= 0); |
| 547 |
assert_se(array[1] == -EBADF); |
| 548 |
|
| 549 |
array[1] = TAKE_FD(array[i]); |
| 550 |
assert_se(array[0] == -EBADF); |
| 551 |
assert_se(array[1] >= 0); |
| 552 |
|
| 553 |
i = 1 - i; |
| 554 |
array[0] = TAKE_FD(*(array + i)); |
| 555 |
assert_se(array[0] >= 0); |
| 556 |
assert_se(array[1] == -EBADF); |
| 557 |
|
| 558 |
i = 1 - i; |
| 559 |
fd1 = TAKE_FD(array[i]); |
| 560 |
assert_se(fd1 >= 0); |
| 561 |
assert_se(array[0] == -EBADF); |
| 562 |
assert_se(array[1] == -EBADF); |
| 563 |
} |
| 564 |
|
| 565 |
TEST(dir_fd_is_root) { |
| 566 |
_cleanup_close_ int fd = -EBADF; |
| 567 |
int r; |
| 568 |
|
| 569 |
assert_se(dir_fd_is_root_or_cwd(AT_FDCWD) > 0); |
| 570 |
|
| 571 |
assert_se((fd = open("/", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0); |
| 572 |
assert_se(dir_fd_is_root(fd) > 0); |
| 573 |
assert_se(dir_fd_is_root_or_cwd(fd) > 0); |
| 574 |
|
| 575 |
fd = safe_close(fd); |
| 576 |
|
| 577 |
assert_se((fd = open("/usr", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0); |
| 578 |
assert_se(dir_fd_is_root(fd) == 0); |
| 579 |
assert_se(dir_fd_is_root_or_cwd(fd) == 0); |
| 580 |
|
| 581 |
r = detach_mount_namespace(); |
| 582 |
if (r < 0) |
| 583 |
return (void) log_tests_skipped_errno(r, "Failed to detach mount namespace"); |
| 584 |
|
| 585 |
_cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; |
| 586 |
_cleanup_free_ char *x = NULL, *y = NULL; |
| 587 |
|
| 588 |
assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0); |
| 589 |
assert_se(x = path_join(tmp, "x")); |
| 590 |
assert_se(y = path_join(tmp, "x/y")); |
| 591 |
assert_se(mkdir_p(y, 0755) >= 0); |
| 592 |
assert_se(mount_nofollow_verbose(LOG_DEBUG, x, y, NULL, MS_BIND, NULL) >= 0); |
| 593 |
|
| 594 |
fd = safe_close(fd); |
| 595 |
|
| 596 |
assert_se((fd = open(tmp, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0); |
| 597 |
assert_se(dir_fd_is_root(fd) == 0); |
| 598 |
assert_se(dir_fd_is_root_or_cwd(fd) == 0); |
| 599 |
|
| 600 |
fd = safe_close(fd); |
| 601 |
|
| 602 |
assert_se((fd = open(x, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0); |
| 603 |
assert_se(dir_fd_is_root(fd) == 0); |
| 604 |
assert_se(dir_fd_is_root_or_cwd(fd) == 0); |
| 605 |
|
| 606 |
fd = safe_close(fd); |
| 607 |
|
| 608 |
assert_se((fd = open(y, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)) >= 0); |
| 609 |
assert_se(dir_fd_is_root(fd) == 0); |
| 610 |
assert_se(dir_fd_is_root_or_cwd(fd) == 0); |
| 611 |
} |
| 612 |
|
| 613 |
static void test_path_is_root_at_one(bool expected) { |
| 614 |
ASSERT_OK_POSITIVE(path_is_root("/")); |
| 615 |
ASSERT_OK_POSITIVE(path_is_root("/.")); |
| 616 |
ASSERT_OK_EQ(path_is_root("/./.."), expected); |
| 617 |
ASSERT_OK_EQ(path_is_root("/.."), expected); |
| 618 |
ASSERT_OK_EQ(path_is_root("/../"), expected); |
| 619 |
ASSERT_OK_EQ(path_is_root("/../."), expected); |
| 620 |
ASSERT_OK_EQ(path_is_root("/../.."), expected); |
| 621 |
|
| 622 |
ASSERT_OK_ZERO(path_is_root("/usr")); |
| 623 |
ASSERT_OK_ZERO(path_is_root("/./usr")); |
| 624 |
ASSERT_OK_ZERO(path_is_root("/../usr")); |
| 625 |
ASSERT_OK_ZERO(path_is_root("/.././usr")); |
| 626 |
ASSERT_OK_ZERO(path_is_root("/../../usr")); |
| 627 |
|
| 628 |
_cleanup_close_ int fd = -EBADF; |
| 629 |
ASSERT_OK_ERRNO(fd = open("/", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)); |
| 630 |
|
| 631 |
ASSERT_OK_POSITIVE(path_is_root_at(fd, NULL)); |
| 632 |
ASSERT_OK_POSITIVE(path_is_root_at(fd, "")); |
| 633 |
ASSERT_OK_POSITIVE(path_is_root_at(fd, ".")); |
| 634 |
ASSERT_OK_EQ(path_is_root_at(fd, "./../"), expected); |
| 635 |
ASSERT_OK_EQ(path_is_root_at(fd, "../"), expected); |
| 636 |
ASSERT_OK_POSITIVE(path_is_root_at(fd, "/")); |
| 637 |
ASSERT_OK_POSITIVE(path_is_root_at(fd, "/.")); |
| 638 |
ASSERT_OK_EQ(path_is_root_at(fd, "/./.."), expected); |
| 639 |
ASSERT_OK_EQ(path_is_root_at(fd, "/.."), expected); |
| 640 |
ASSERT_OK_EQ(path_is_root_at(fd, "/../"), expected); |
| 641 |
ASSERT_OK_EQ(path_is_root_at(fd, "/../."), expected); |
| 642 |
ASSERT_OK_EQ(path_is_root_at(fd, "/../.."), expected); |
| 643 |
|
| 644 |
ASSERT_OK_ZERO(path_is_root_at(fd, "usr")); |
| 645 |
ASSERT_OK_ZERO(path_is_root_at(fd, "./usr")); |
| 646 |
ASSERT_OK_ZERO(path_is_root_at(fd, "../usr")); |
| 647 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/usr")); |
| 648 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/./usr")); |
| 649 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/../usr")); |
| 650 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/.././usr")); |
| 651 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/../../usr")); |
| 652 |
|
| 653 |
safe_close(fd); |
| 654 |
ASSERT_OK_ERRNO(fd = open("/../", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW)); |
| 655 |
|
| 656 |
ASSERT_OK_EQ(path_is_root_at(fd, NULL), expected); |
| 657 |
ASSERT_OK_EQ(path_is_root_at(fd, ""), expected); |
| 658 |
ASSERT_OK_EQ(path_is_root_at(fd, "."), expected); |
| 659 |
ASSERT_OK_EQ(path_is_root_at(fd, "./.."), expected); |
| 660 |
ASSERT_OK_EQ(path_is_root_at(fd, "../"), expected); |
| 661 |
ASSERT_OK_POSITIVE(path_is_root_at(fd, "/")); |
| 662 |
ASSERT_OK_POSITIVE(path_is_root_at(fd, "/.")); |
| 663 |
ASSERT_OK_EQ(path_is_root_at(fd, "/./.."), expected); |
| 664 |
ASSERT_OK_EQ(path_is_root_at(fd, "/.."), expected); |
| 665 |
ASSERT_OK_EQ(path_is_root_at(fd, "/../"), expected); |
| 666 |
ASSERT_OK_EQ(path_is_root_at(fd, "/../."), expected); |
| 667 |
ASSERT_OK_EQ(path_is_root_at(fd, "/../.."), expected); |
| 668 |
|
| 669 |
ASSERT_OK_ZERO(path_is_root_at(fd, "usr")); |
| 670 |
ASSERT_OK_ZERO(path_is_root_at(fd, "./usr")); |
| 671 |
ASSERT_OK_ZERO(path_is_root_at(fd, "../usr")); |
| 672 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/usr")); |
| 673 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/./usr")); |
| 674 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/../usr")); |
| 675 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/.././usr")); |
| 676 |
ASSERT_OK_ZERO(path_is_root_at(fd, "/../../usr")); |
| 677 |
} |
| 678 |
|
| 679 |
TEST(path_is_root_at) { |
| 680 |
int r; |
| 681 |
|
| 682 |
test_path_is_root_at_one(true); |
| 683 |
|
| 684 |
r = detach_mount_namespace(); |
| 685 |
if (r < 0) |
| 686 |
return (void) log_tests_skipped_errno(r, "Failed to detach mount namespace"); |
| 687 |
|
| 688 |
/* Interestingly, even after bind mount a path on "/", still "/" points to the previous root |
| 689 |
* directory, but "/../" points to the new root directory. Hence, path_is_root("/") is true but |
| 690 |
* path_is_root("/../") is false. Such spurious situation is resolved after chroot()ing to the new |
| 691 |
* root directory. */ |
| 692 |
ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "/", "/", NULL, MS_BIND|MS_REC, NULL)); |
| 693 |
log_debug("/* %s: bind mount(\"/\", \"/\") */", __func__); |
| 694 |
test_path_is_root_at_one(false); |
| 695 |
|
| 696 |
/* chroot("/") does not change anything. */ |
| 697 |
ASSERT_OK_ERRNO(chroot("/")); |
| 698 |
log_debug("/* %s: chroot(\"/\") */", __func__); |
| 699 |
test_path_is_root_at_one(false); |
| 700 |
|
| 701 |
/* chdir("/") neither change anything. */ |
| 702 |
ASSERT_OK_ERRNO(chdir("/")); |
| 703 |
log_debug("/* %s: chdir(\"/\") */", __func__); |
| 704 |
test_path_is_root_at_one(false); |
| 705 |
|
| 706 |
/* chdir("/../") neither change anything. */ |
| 707 |
ASSERT_OK_ERRNO(chdir("/../")); |
| 708 |
log_debug("/* %s: chdir(\"/../\") */", __func__); |
| 709 |
test_path_is_root_at_one(false); |
| 710 |
|
| 711 |
/* After chroot("/../"), both "/" and "/../" point to the new root directory. */ |
| 712 |
ASSERT_OK_ERRNO(chroot("/../")); |
| 713 |
log_debug("/* %s: chroot(\"/../\") */", __func__); |
| 714 |
test_path_is_root_at_one(true); |
| 715 |
|
| 716 |
/* chdir("/../") does not change anything. */ |
| 717 |
ASSERT_OK_ERRNO(chdir("/../")); |
| 718 |
log_debug("/* %s: chdir(\"/../\") again */", __func__); |
| 719 |
test_path_is_root_at_one(true); |
| 720 |
|
| 721 |
/* bind mounting to non-root directory has no problem, of course. */ |
| 722 |
_cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; |
| 723 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-path_is_root-XXXXXX", &tmp)); |
| 724 |
ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "/", tmp, NULL, MS_BIND|MS_REC, NULL)); |
| 725 |
log_debug("/* %s: bind mount(\"/\", \"%s\") */", __func__, tmp); |
| 726 |
test_path_is_root_at_one(true); |
| 727 |
|
| 728 |
ASSERT_OK_ERRNO(chdir(tmp)); |
| 729 |
log_debug("/* %s: chdir(\"%s\") */", __func__, tmp); |
| 730 |
test_path_is_root_at_one(true); |
| 731 |
|
| 732 |
ASSERT_OK_ERRNO(chroot(tmp)); |
| 733 |
log_debug("/* %s: chroot(\"%s\") */", __func__, tmp); |
| 734 |
test_path_is_root_at_one(true); |
| 735 |
|
| 736 |
ASSERT_OK_ERRNO(chdir(tmp)); |
| 737 |
log_debug("/* %s: chdir(\"%s\") again */", __func__, tmp); |
| 738 |
test_path_is_root_at_one(true); |
| 739 |
} |
| 740 |
|
| 741 |
TEST(fds_inode_and_mount_same) { |
| 742 |
_cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF, fd3 = -EBADF, fd4 = -EBADF; |
| 743 |
|
| 744 |
fd1 = open("/sys", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW); |
| 745 |
fd2 = open("/proc", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW); |
| 746 |
fd3 = open("/proc", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW); |
| 747 |
fd4 = open("/", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW); |
| 748 |
|
| 749 |
if (fd1 < 0 || fd2 < 0 || fd3 < 0 || fd4 < 0) |
| 750 |
return (void) log_tests_skipped_errno(errno, "Failed to open /sys or /proc or /"); |
| 751 |
|
| 752 |
assert_se(fds_inode_and_mount_same(fd1, fd2) == 0); |
| 753 |
assert_se(fds_inode_and_mount_same(fd2, fd3) > 0); |
| 754 |
} |
| 755 |
|
| 756 |
TEST(fd_get_path) { |
| 757 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 758 |
_cleanup_close_ int tfd = -EBADF, fd = -EBADF; |
| 759 |
_cleanup_free_ char *p = NULL, *q = NULL, *saved_cwd = NULL; |
| 760 |
|
| 761 |
tfd = mkdtemp_open(NULL, O_PATH, &t); |
| 762 |
assert_se(tfd >= 0); |
| 763 |
assert_se(fd_get_path(tfd, &p) >= 0); |
| 764 |
ASSERT_STREQ(p, t); |
| 765 |
|
| 766 |
p = mfree(p); |
| 767 |
|
| 768 |
assert_se(safe_getcwd(&saved_cwd) >= 0); |
| 769 |
assert_se(chdir(t) >= 0); |
| 770 |
|
| 771 |
assert_se(fd_get_path(AT_FDCWD, &p) >= 0); |
| 772 |
ASSERT_STREQ(p, t); |
| 773 |
|
| 774 |
p = mfree(p); |
| 775 |
|
| 776 |
assert_se(q = path_join(t, "regular")); |
| 777 |
assert_se(touch(q) >= 0); |
| 778 |
assert_se(mkdirat_parents(tfd, "subdir/symlink", 0755) >= 0); |
| 779 |
assert_se(symlinkat("../regular", tfd, "subdir/symlink") >= 0); |
| 780 |
assert_se(symlinkat("subdir", tfd, "symdir") >= 0); |
| 781 |
|
| 782 |
fd = openat(tfd, "regular", O_CLOEXEC|O_PATH); |
| 783 |
assert_se(fd >= 0); |
| 784 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 785 |
ASSERT_STREQ(p, q); |
| 786 |
|
| 787 |
p = mfree(p); |
| 788 |
fd = safe_close(fd); |
| 789 |
|
| 790 |
fd = openat(AT_FDCWD, "regular", O_CLOEXEC|O_PATH); |
| 791 |
assert_se(fd >= 0); |
| 792 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 793 |
ASSERT_STREQ(p, q); |
| 794 |
|
| 795 |
p = mfree(p); |
| 796 |
fd = safe_close(fd); |
| 797 |
|
| 798 |
fd = openat(tfd, "subdir/symlink", O_CLOEXEC|O_PATH); |
| 799 |
assert_se(fd >= 0); |
| 800 |
assert_se(fd_verify_regular(fd) >= 0); |
| 801 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 802 |
ASSERT_STREQ(p, q); |
| 803 |
|
| 804 |
p = mfree(p); |
| 805 |
fd = safe_close(fd); |
| 806 |
|
| 807 |
fd = openat(AT_FDCWD, "subdir/symlink", O_CLOEXEC|O_PATH); |
| 808 |
assert_se(fd >= 0); |
| 809 |
assert_se(fd_verify_regular(fd) >= 0); |
| 810 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 811 |
ASSERT_STREQ(p, q); |
| 812 |
|
| 813 |
p = mfree(p); |
| 814 |
fd = safe_close(fd); |
| 815 |
|
| 816 |
fd = openat(tfd, "symdir//./symlink", O_CLOEXEC|O_PATH); |
| 817 |
assert_se(fd >= 0); |
| 818 |
assert_se(fd_verify_regular(fd) >= 0); |
| 819 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 820 |
ASSERT_STREQ(p, q); |
| 821 |
|
| 822 |
p = mfree(p); |
| 823 |
fd = safe_close(fd); |
| 824 |
|
| 825 |
fd = openat(AT_FDCWD, "symdir//./symlink", O_CLOEXEC|O_PATH); |
| 826 |
assert_se(fd >= 0); |
| 827 |
assert_se(fd_verify_regular(fd) >= 0); |
| 828 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 829 |
ASSERT_STREQ(p, q); |
| 830 |
|
| 831 |
p = mfree(p); |
| 832 |
q = mfree(q); |
| 833 |
fd = safe_close(fd); |
| 834 |
|
| 835 |
assert_se(q = path_join(t, "subdir/symlink")); |
| 836 |
fd = openat(tfd, "subdir/symlink", O_CLOEXEC|O_PATH|O_NOFOLLOW); |
| 837 |
assert_se(fd >= 0); |
| 838 |
assert_se(fd_verify_regular(fd) == -ELOOP); |
| 839 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 840 |
ASSERT_STREQ(p, q); |
| 841 |
|
| 842 |
p = mfree(p); |
| 843 |
fd = safe_close(fd); |
| 844 |
|
| 845 |
fd = openat(AT_FDCWD, "subdir/symlink", O_CLOEXEC|O_PATH|O_NOFOLLOW); |
| 846 |
assert_se(fd >= 0); |
| 847 |
assert_se(fd_verify_regular(fd) == -ELOOP); |
| 848 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 849 |
ASSERT_STREQ(p, q); |
| 850 |
|
| 851 |
p = mfree(p); |
| 852 |
fd = safe_close(fd); |
| 853 |
|
| 854 |
fd = openat(tfd, "symdir//./symlink", O_CLOEXEC|O_PATH|O_NOFOLLOW); |
| 855 |
assert_se(fd >= 0); |
| 856 |
assert_se(fd_verify_regular(fd) == -ELOOP); |
| 857 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 858 |
ASSERT_STREQ(p, q); |
| 859 |
|
| 860 |
p = mfree(p); |
| 861 |
fd = safe_close(fd); |
| 862 |
|
| 863 |
fd = openat(AT_FDCWD, "symdir//./symlink", O_CLOEXEC|O_PATH|O_NOFOLLOW); |
| 864 |
assert_se(fd >= 0); |
| 865 |
assert_se(fd_verify_regular(fd) == -ELOOP); |
| 866 |
assert_se(fd_get_path(fd, &p) >= 0); |
| 867 |
ASSERT_STREQ(p, q); |
| 868 |
|
| 869 |
assert_se(chdir(saved_cwd) >= 0); |
| 870 |
} |
| 871 |
|
| 872 |
TEST(fd_vet_accmode) { |
| 873 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fd-accmode.XXXXXX"; |
| 874 |
_cleanup_close_ int fd_rw = -EBADF, fd_ro = -EBADF, fd_wo = -EBADF, fd_opath = -EBADF; |
| 875 |
|
| 876 |
ASSERT_OK(fd_rw = mkostemp_safe(name)); |
| 877 |
ASSERT_OK_ZERO(fd_vet_accmode(fd_rw, O_RDONLY)); |
| 878 |
ASSERT_OK_ZERO(fd_vet_accmode(fd_rw, O_WRONLY)); |
| 879 |
ASSERT_OK_POSITIVE(fd_vet_accmode(fd_rw, O_RDWR)); |
| 880 |
|
| 881 |
ASSERT_OK_ERRNO(fd_ro = open(name, O_RDONLY | O_CLOEXEC)); |
| 882 |
ASSERT_OK_POSITIVE(fd_vet_accmode(fd_ro, O_RDONLY)); |
| 883 |
ASSERT_ERROR(fd_vet_accmode(fd_ro, O_WRONLY), EPROTOTYPE); |
| 884 |
ASSERT_ERROR(fd_vet_accmode(fd_ro, O_RDWR), EPROTOTYPE); |
| 885 |
|
| 886 |
ASSERT_OK_ERRNO(fd_wo = open(name, O_WRONLY | O_CLOEXEC)); |
| 887 |
ASSERT_ERROR(fd_vet_accmode(fd_wo, O_RDONLY), EPROTOTYPE); |
| 888 |
ASSERT_OK_POSITIVE(fd_vet_accmode(fd_wo, O_WRONLY)); |
| 889 |
ASSERT_ERROR(fd_vet_accmode(fd_wo, O_RDWR), EPROTOTYPE); |
| 890 |
|
| 891 |
ASSERT_OK_ERRNO(fd_opath = open(name, O_PATH | O_CLOEXEC)); |
| 892 |
ASSERT_ERROR(fd_vet_accmode(fd_opath, O_RDONLY), EBADFD); |
| 893 |
ASSERT_ERROR(fd_vet_accmode(fd_opath, O_WRONLY), EBADFD); |
| 894 |
ASSERT_ERROR(fd_vet_accmode(fd_opath, O_RDWR), EBADFD); |
| 895 |
} |
| 896 |
|
| 897 |
TEST(fd_is_writable) { |
| 898 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fd-writable.XXXXXX"; |
| 899 |
_cleanup_close_ int fd_ro = -EBADF, fd_wo = -EBADF, fd_rw = -EBADF, fd_path = -EBADF; |
| 900 |
|
| 901 |
ASSERT_OK(fd_rw = mkostemp_safe(name)); |
| 902 |
ASSERT_OK_POSITIVE(fd_is_writable(fd_rw)); |
| 903 |
|
| 904 |
ASSERT_OK(fd_ro = open(name, O_RDONLY | O_CLOEXEC)); |
| 905 |
ASSERT_OK_ZERO(fd_is_writable(fd_ro)); |
| 906 |
|
| 907 |
ASSERT_OK(fd_wo = open(name, O_WRONLY | O_CLOEXEC)); |
| 908 |
ASSERT_OK_POSITIVE(fd_is_writable(fd_wo)); |
| 909 |
|
| 910 |
ASSERT_OK(fd_path = open(name, O_PATH | O_CLOEXEC)); |
| 911 |
ASSERT_OK_ZERO(fd_is_writable(fd_path)); |
| 912 |
|
| 913 |
ASSERT_SIGNAL(fd_is_writable(-1), SIGABRT); |
| 914 |
|
| 915 |
safe_close(fd_ro); |
| 916 |
ASSERT_ERROR(fd_is_writable(fd_ro), EBADF); |
| 917 |
TAKE_FD(fd_ro); |
| 918 |
} |
| 919 |
|
| 920 |
DEFINE_TEST_MAIN(LOG_DEBUG); |