1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Landlock tests - Filesystem 4 * 5 * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net> 6 * Copyright © 2020 ANSSI 7 * Copyright © 2020-2022 Microsoft Corporation 8 */ 9 10 #define _GNU_SOURCE 11 #include <asm/termbits.h> 12 #include <fcntl.h> 13 #include <libgen.h> 14 #include <linux/fiemap.h> 15 #include <linux/landlock.h> 16 #include <linux/magic.h> 17 #include <sched.h> 18 #include <stddef.h> 19 #include <stdio.h> 20 #include <string.h> 21 #include <sys/capability.h> 22 #include <sys/ioctl.h> 23 #include <sys/mount.h> 24 #include <sys/prctl.h> 25 #include <sys/resource.h> 26 #include <sys/sendfile.h> 27 #include <sys/socket.h> 28 #include <sys/stat.h> 29 #include <sys/sysmacros.h> 30 #include <sys/un.h> 31 #include <sys/vfs.h> 32 #include <unistd.h> 33 34 /* 35 * Intentionally included last to work around header conflict. 36 * See https://sourceware.org/glibc/wiki/Synchronizing_Headers. 37 */ 38 #include <linux/fs.h> 39 #include <linux/mount.h> 40 41 /* Defines AT_EXECVE_CHECK without type conflicts. */ 42 #define _ASM_GENERIC_FCNTL_H 43 #include <linux/fcntl.h> 44 45 #include "audit.h" 46 #include "common.h" 47 #include "trace.h" 48 49 #define TRACE_TASK "fs_test" 50 51 #ifndef renameat2 52 int renameat2(int olddirfd, const char *oldpath, int newdirfd, 53 const char *newpath, unsigned int flags) 54 { 55 return syscall(__NR_renameat2, olddirfd, oldpath, newdirfd, newpath, 56 flags); 57 } 58 #endif 59 60 static int sys_execveat(int dirfd, const char *pathname, char *const argv[], 61 char *const envp[], int flags) 62 { 63 return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags); 64 } 65 66 #ifndef RENAME_EXCHANGE 67 #define RENAME_EXCHANGE (1 << 1) 68 #endif 69 70 static const char bin_true[] = "./true"; 71 72 /* Paths (sibling number and depth) */ 73 static const char dir_s1d1[] = TMP_DIR "/s1d1"; 74 static const char file1_s1d1[] = TMP_DIR "/s1d1/f1"; 75 static const char file2_s1d1[] = TMP_DIR "/s1d1/f2"; 76 static const char dir_s1d2[] = TMP_DIR "/s1d1/s1d2"; 77 static const char file1_s1d2[] = TMP_DIR "/s1d1/s1d2/f1"; 78 static const char file2_s1d2[] = TMP_DIR "/s1d1/s1d2/f2"; 79 static const char dir_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3"; 80 static const char file1_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f1"; 81 static const char file2_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f2"; 82 83 static const char dir_s2d1[] = TMP_DIR "/s2d1"; 84 static const char file1_s2d1[] = TMP_DIR "/s2d1/f1"; 85 static const char dir_s2d2[] = TMP_DIR "/s2d1/s2d2"; 86 static const char file1_s2d2[] = TMP_DIR "/s2d1/s2d2/f1"; 87 static const char dir_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3"; 88 static const char file1_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f1"; 89 static const char file2_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f2"; 90 91 static const char dir_s3d1[] = TMP_DIR "/s3d1"; 92 static const char file1_s3d1[] = TMP_DIR "/s3d1/f1"; 93 /* dir_s3d2 is a mount point. */ 94 static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2"; 95 static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3"; 96 static const char file1_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3/f1"; 97 static const char dir_s3d4[] = TMP_DIR "/s3d1/s3d2/s3d4"; 98 static const char file1_s3d4[] = TMP_DIR "/s3d1/s3d2/s3d4/f1"; 99 100 /* 101 * layout1 hierarchy: 102 * 103 * tmp 104 * ├── s1d1 105 * │ ├── f1 106 * │ ├── f2 107 * │ └── s1d2 108 * │ ├── f1 109 * │ ├── f2 110 * │ └── s1d3 111 * │ ├── f1 112 * │ └── f2 113 * ├── s2d1 114 * │ ├── f1 115 * │ └── s2d2 116 * │ ├── f1 117 * │ └── s2d3 118 * │ ├── f1 119 * │ └── f2 120 * └── s3d1 121 * ├── f1 122 * └── s3d2 [mount point] 123 * ├── s3d3 124 * │ └── f1 125 * └── s3d4 126 * └── f1 127 */ 128 129 static bool fgrep(FILE *const inf, const char *const str) 130 { 131 char line[32]; 132 const int slen = strlen(str); 133 134 while (!feof(inf)) { 135 if (!fgets(line, sizeof(line), inf)) 136 break; 137 if (strncmp(line, str, slen)) 138 continue; 139 140 return true; 141 } 142 143 return false; 144 } 145 146 static bool supports_filesystem(const char *const filesystem) 147 { 148 char str[32]; 149 int len; 150 bool res = true; 151 FILE *const inf = fopen("/proc/filesystems", "r"); 152 153 /* 154 * Consider that the filesystem is supported if we cannot get the 155 * supported ones. 156 */ 157 if (!inf) 158 return true; 159 160 /* filesystem can be null for bind mounts. */ 161 if (!filesystem) 162 goto out; 163 164 len = snprintf(str, sizeof(str), "nodev\t%s\n", filesystem); 165 if (len >= sizeof(str)) 166 /* Ignores too-long filesystem names. */ 167 goto out; 168 169 res = fgrep(inf, str); 170 171 out: 172 fclose(inf); 173 return res; 174 } 175 176 static bool cwd_matches_fs(unsigned int fs_magic) 177 { 178 struct statfs statfs_buf; 179 180 if (!fs_magic) 181 return true; 182 183 if (statfs(".", &statfs_buf)) 184 return true; 185 186 return statfs_buf.f_type == fs_magic; 187 } 188 189 static void mkdir_parents(struct __test_metadata *const _metadata, 190 const char *const path) 191 { 192 char *walker; 193 const char *parent; 194 int i, err; 195 196 ASSERT_NE(path[0], '\0'); 197 walker = strdup(path); 198 ASSERT_NE(NULL, walker); 199 parent = walker; 200 for (i = 1; walker[i]; i++) { 201 if (walker[i] != '/') 202 continue; 203 walker[i] = '\0'; 204 err = mkdir(parent, 0700); 205 ASSERT_FALSE(err && errno != EEXIST) 206 { 207 TH_LOG("Failed to create directory \"%s\": %s", parent, 208 strerror(errno)); 209 } 210 walker[i] = '/'; 211 } 212 free(walker); 213 } 214 215 static void create_directory(struct __test_metadata *const _metadata, 216 const char *const path) 217 { 218 mkdir_parents(_metadata, path); 219 ASSERT_EQ(0, mkdir(path, 0700)) 220 { 221 TH_LOG("Failed to create directory \"%s\": %s", path, 222 strerror(errno)); 223 } 224 } 225 226 static void create_file(struct __test_metadata *const _metadata, 227 const char *const path) 228 { 229 mkdir_parents(_metadata, path); 230 ASSERT_EQ(0, mknod(path, S_IFREG | 0700, 0)) 231 { 232 TH_LOG("Failed to create file \"%s\": %s", path, 233 strerror(errno)); 234 } 235 } 236 237 static int remove_path(const char *const path) 238 { 239 char *walker; 240 int i, ret, err = 0; 241 242 walker = strdup(path); 243 if (!walker) { 244 err = ENOMEM; 245 goto out; 246 } 247 if (unlink(path) && rmdir(path)) { 248 if (errno != ENOENT && errno != ENOTDIR) 249 err = errno; 250 goto out; 251 } 252 for (i = strlen(walker); i > 0; i--) { 253 if (walker[i] != '/') 254 continue; 255 walker[i] = '\0'; 256 ret = rmdir(walker); 257 if (ret) { 258 if (errno != ENOTEMPTY && errno != EBUSY) 259 err = errno; 260 goto out; 261 } 262 if (strcmp(walker, TMP_DIR) == 0) 263 goto out; 264 } 265 266 out: 267 free(walker); 268 return err; 269 } 270 271 struct mnt_opt { 272 const char *const source; 273 const char *const type; 274 const unsigned long flags; 275 const char *const data; 276 }; 277 278 #define MNT_TMP_DATA "size=4m,mode=700" 279 280 static const struct mnt_opt mnt_tmp = { 281 .type = "tmpfs", 282 .data = MNT_TMP_DATA, 283 }; 284 285 static int mount_opt(const struct mnt_opt *const mnt, const char *const target) 286 { 287 return mount(mnt->source ?: mnt->type, target, mnt->type, mnt->flags, 288 mnt->data); 289 } 290 291 static void prepare_layout_opt(struct __test_metadata *const _metadata, 292 const struct mnt_opt *const mnt) 293 { 294 disable_caps(_metadata); 295 umask(0077); 296 create_directory(_metadata, TMP_DIR); 297 298 /* 299 * Do not pollute the rest of the system: creates a private mount point 300 * for tests relying on pivot_root(2) and move_mount(2). 301 */ 302 set_cap(_metadata, CAP_SYS_ADMIN); 303 ASSERT_EQ(0, unshare(CLONE_NEWNS | CLONE_NEWCGROUP)); 304 ASSERT_EQ(0, mount_opt(mnt, TMP_DIR)) 305 { 306 TH_LOG("Failed to mount the %s filesystem: %s", mnt->type, 307 strerror(errno)); 308 /* 309 * FIXTURE_TEARDOWN() is not called when FIXTURE_SETUP() 310 * failed, so we need to explicitly do a minimal cleanup to 311 * avoid cascading errors with other tests that don't depend on 312 * the same filesystem. 313 */ 314 remove_path(TMP_DIR); 315 } 316 ASSERT_EQ(0, mount(NULL, TMP_DIR, NULL, MS_PRIVATE | MS_REC, NULL)); 317 clear_cap(_metadata, CAP_SYS_ADMIN); 318 } 319 320 static void prepare_layout(struct __test_metadata *const _metadata) 321 { 322 prepare_layout_opt(_metadata, &mnt_tmp); 323 } 324 325 static void cleanup_layout(struct __test_metadata *const _metadata) 326 { 327 set_cap(_metadata, CAP_SYS_ADMIN); 328 if (umount(TMP_DIR)) { 329 /* 330 * According to the test environment, the mount point of the 331 * current directory may be shared or not, which changes the 332 * visibility of the nested TMP_DIR mount point for the test's 333 * parent process doing this cleanup. 334 */ 335 ASSERT_EQ(EINVAL, errno); 336 } 337 clear_cap(_metadata, CAP_SYS_ADMIN); 338 EXPECT_EQ(0, remove_path(TMP_DIR)); 339 } 340 341 /* clang-format off */ 342 FIXTURE(layout0) {}; 343 /* clang-format on */ 344 345 FIXTURE_SETUP(layout0) 346 { 347 prepare_layout(_metadata); 348 } 349 350 FIXTURE_TEARDOWN_PARENT(layout0) 351 { 352 cleanup_layout(_metadata); 353 } 354 355 static void create_layout1(struct __test_metadata *const _metadata) 356 { 357 create_file(_metadata, file1_s1d1); 358 create_file(_metadata, file1_s1d2); 359 create_file(_metadata, file1_s1d3); 360 create_file(_metadata, file2_s1d1); 361 create_file(_metadata, file2_s1d2); 362 create_file(_metadata, file2_s1d3); 363 364 create_file(_metadata, file1_s2d1); 365 create_file(_metadata, file1_s2d2); 366 create_file(_metadata, file1_s2d3); 367 create_file(_metadata, file2_s2d3); 368 369 create_file(_metadata, file1_s3d1); 370 create_directory(_metadata, dir_s3d2); 371 set_cap(_metadata, CAP_SYS_ADMIN); 372 ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s3d2)); 373 clear_cap(_metadata, CAP_SYS_ADMIN); 374 375 create_file(_metadata, file1_s3d3); 376 create_file(_metadata, file1_s3d4); 377 } 378 379 static void remove_layout1(struct __test_metadata *const _metadata) 380 { 381 EXPECT_EQ(0, remove_path(file2_s1d3)); 382 EXPECT_EQ(0, remove_path(file2_s1d2)); 383 EXPECT_EQ(0, remove_path(file2_s1d1)); 384 EXPECT_EQ(0, remove_path(file1_s1d3)); 385 EXPECT_EQ(0, remove_path(file1_s1d2)); 386 EXPECT_EQ(0, remove_path(file1_s1d1)); 387 EXPECT_EQ(0, remove_path(dir_s1d3)); 388 389 EXPECT_EQ(0, remove_path(file2_s2d3)); 390 EXPECT_EQ(0, remove_path(file1_s2d3)); 391 EXPECT_EQ(0, remove_path(file1_s2d2)); 392 EXPECT_EQ(0, remove_path(file1_s2d1)); 393 EXPECT_EQ(0, remove_path(dir_s2d2)); 394 395 EXPECT_EQ(0, remove_path(file1_s3d1)); 396 EXPECT_EQ(0, remove_path(file1_s3d3)); 397 EXPECT_EQ(0, remove_path(file1_s3d4)); 398 set_cap(_metadata, CAP_SYS_ADMIN); 399 umount(dir_s3d2); 400 clear_cap(_metadata, CAP_SYS_ADMIN); 401 EXPECT_EQ(0, remove_path(dir_s3d2)); 402 } 403 404 /* clang-format off */ 405 FIXTURE(layout1) {}; 406 /* clang-format on */ 407 408 FIXTURE_SETUP(layout1) 409 { 410 prepare_layout(_metadata); 411 412 create_layout1(_metadata); 413 } 414 415 FIXTURE_TEARDOWN_PARENT(layout1) 416 { 417 remove_layout1(_metadata); 418 419 cleanup_layout(_metadata); 420 } 421 422 /* 423 * This helper enables to use the ASSERT_* macros and print the line number 424 * pointing to the test caller. 425 */ 426 static int test_open_rel(const int dirfd, const char *const path, 427 const int flags) 428 { 429 int fd; 430 431 /* Works with file and directories. */ 432 fd = openat(dirfd, path, flags | O_CLOEXEC); 433 if (fd < 0) 434 return errno; 435 /* 436 * Mixing error codes from close(2) and open(2) should not lead to any 437 * (access type) confusion for this test. 438 */ 439 if (close(fd) != 0) 440 return errno; 441 return 0; 442 } 443 444 static int test_open(const char *const path, const int flags) 445 { 446 return test_open_rel(AT_FDCWD, path, flags); 447 } 448 449 TEST_F_FORK(layout1, no_restriction) 450 { 451 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 452 ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 453 ASSERT_EQ(0, test_open(file2_s1d1, O_RDONLY)); 454 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 455 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 456 ASSERT_EQ(0, test_open(file2_s1d2, O_RDONLY)); 457 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 458 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 459 460 ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY)); 461 ASSERT_EQ(0, test_open(file1_s2d1, O_RDONLY)); 462 ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY)); 463 ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY)); 464 ASSERT_EQ(0, test_open(dir_s2d3, O_RDONLY)); 465 ASSERT_EQ(0, test_open(file1_s2d3, O_RDONLY)); 466 467 ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY)); 468 ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY)); 469 ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY)); 470 } 471 472 TEST_F_FORK(layout1, inval) 473 { 474 struct landlock_path_beneath_attr path_beneath = { 475 .allowed_access = LANDLOCK_ACCESS_FS_READ_FILE | 476 LANDLOCK_ACCESS_FS_WRITE_FILE, 477 .parent_fd = -1, 478 }; 479 struct landlock_ruleset_attr ruleset_attr = { 480 .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE | 481 LANDLOCK_ACCESS_FS_WRITE_FILE, 482 }; 483 int ruleset_fd; 484 485 path_beneath.parent_fd = 486 open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC); 487 ASSERT_LE(0, path_beneath.parent_fd); 488 489 ruleset_fd = open(dir_s1d1, O_PATH | O_DIRECTORY | O_CLOEXEC); 490 ASSERT_LE(0, ruleset_fd); 491 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 492 &path_beneath, 0)); 493 /* Returns EBADF because ruleset_fd is not a landlock-ruleset FD. */ 494 ASSERT_EQ(EBADF, errno); 495 ASSERT_EQ(0, close(ruleset_fd)); 496 497 ruleset_fd = open(dir_s1d1, O_DIRECTORY | O_CLOEXEC); 498 ASSERT_LE(0, ruleset_fd); 499 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 500 &path_beneath, 0)); 501 /* Returns EBADFD because ruleset_fd is not a valid ruleset. */ 502 ASSERT_EQ(EBADFD, errno); 503 ASSERT_EQ(0, close(ruleset_fd)); 504 505 /* Gets a real ruleset. */ 506 ruleset_fd = 507 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 508 ASSERT_LE(0, ruleset_fd); 509 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 510 &path_beneath, 0)); 511 ASSERT_EQ(0, close(path_beneath.parent_fd)); 512 513 /* Tests without O_PATH. */ 514 path_beneath.parent_fd = open(dir_s1d2, O_DIRECTORY | O_CLOEXEC); 515 ASSERT_LE(0, path_beneath.parent_fd); 516 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 517 &path_beneath, 0)); 518 ASSERT_EQ(0, close(path_beneath.parent_fd)); 519 520 /* Tests with a ruleset FD. */ 521 path_beneath.parent_fd = ruleset_fd; 522 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 523 &path_beneath, 0)); 524 ASSERT_EQ(EBADFD, errno); 525 526 /* Checks unhandled allowed_access. */ 527 path_beneath.parent_fd = 528 open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC); 529 ASSERT_LE(0, path_beneath.parent_fd); 530 531 /* Test with legitimate values. */ 532 path_beneath.allowed_access |= LANDLOCK_ACCESS_FS_EXECUTE; 533 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 534 &path_beneath, 0)); 535 ASSERT_EQ(EINVAL, errno); 536 path_beneath.allowed_access &= ~LANDLOCK_ACCESS_FS_EXECUTE; 537 538 /* Tests with denied-by-default access right. */ 539 path_beneath.allowed_access |= LANDLOCK_ACCESS_FS_REFER; 540 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 541 &path_beneath, 0)); 542 ASSERT_EQ(EINVAL, errno); 543 path_beneath.allowed_access &= ~LANDLOCK_ACCESS_FS_REFER; 544 545 /* Test with unknown (64-bits) value. */ 546 path_beneath.allowed_access |= (1ULL << 60); 547 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 548 &path_beneath, 0)); 549 ASSERT_EQ(EINVAL, errno); 550 path_beneath.allowed_access &= ~(1ULL << 60); 551 552 /* Test with no access. */ 553 path_beneath.allowed_access = 0; 554 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 555 &path_beneath, 0)); 556 ASSERT_EQ(ENOMSG, errno); 557 path_beneath.allowed_access &= ~(1ULL << 60); 558 559 ASSERT_EQ(0, close(path_beneath.parent_fd)); 560 561 /* Enforces the ruleset. */ 562 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)); 563 ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0)); 564 565 ASSERT_EQ(0, close(ruleset_fd)); 566 } 567 568 /* clang-format off */ 569 570 #define ACCESS_FILE ( \ 571 LANDLOCK_ACCESS_FS_EXECUTE | \ 572 LANDLOCK_ACCESS_FS_WRITE_FILE | \ 573 LANDLOCK_ACCESS_FS_READ_FILE | \ 574 LANDLOCK_ACCESS_FS_TRUNCATE | \ 575 LANDLOCK_ACCESS_FS_IOCTL_DEV | \ 576 LANDLOCK_ACCESS_FS_RESOLVE_UNIX) 577 578 #define ACCESS_LAST LANDLOCK_ACCESS_FS_RESOLVE_UNIX 579 580 #define ACCESS_ALL ( \ 581 ACCESS_FILE | \ 582 LANDLOCK_ACCESS_FS_READ_DIR | \ 583 LANDLOCK_ACCESS_FS_REMOVE_DIR | \ 584 LANDLOCK_ACCESS_FS_REMOVE_FILE | \ 585 LANDLOCK_ACCESS_FS_MAKE_CHAR | \ 586 LANDLOCK_ACCESS_FS_MAKE_DIR | \ 587 LANDLOCK_ACCESS_FS_MAKE_REG | \ 588 LANDLOCK_ACCESS_FS_MAKE_SOCK | \ 589 LANDLOCK_ACCESS_FS_MAKE_FIFO | \ 590 LANDLOCK_ACCESS_FS_MAKE_BLOCK | \ 591 LANDLOCK_ACCESS_FS_MAKE_SYM | \ 592 LANDLOCK_ACCESS_FS_REFER) 593 594 /* clang-format on */ 595 596 TEST_F_FORK(layout1, file_and_dir_access_rights) 597 { 598 __u64 access; 599 int err; 600 struct landlock_path_beneath_attr path_beneath_file = {}, 601 path_beneath_dir = {}; 602 struct landlock_ruleset_attr ruleset_attr = { 603 .handled_access_fs = ACCESS_ALL, 604 }; 605 const int ruleset_fd = 606 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 607 608 ASSERT_LE(0, ruleset_fd); 609 610 /* Tests access rights for files. */ 611 path_beneath_file.parent_fd = open(file1_s1d2, O_PATH | O_CLOEXEC); 612 ASSERT_LE(0, path_beneath_file.parent_fd); 613 614 /* Tests access rights for directories. */ 615 path_beneath_dir.parent_fd = 616 open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC); 617 ASSERT_LE(0, path_beneath_dir.parent_fd); 618 619 for (access = 1; access <= ACCESS_LAST; access <<= 1) { 620 path_beneath_dir.allowed_access = access; 621 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, 622 LANDLOCK_RULE_PATH_BENEATH, 623 &path_beneath_dir, 0)); 624 625 path_beneath_file.allowed_access = access; 626 err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 627 &path_beneath_file, 0); 628 if (access & ACCESS_FILE) { 629 ASSERT_EQ(0, err); 630 } else { 631 ASSERT_EQ(-1, err); 632 ASSERT_EQ(EINVAL, errno); 633 } 634 } 635 ASSERT_EQ(0, close(path_beneath_file.parent_fd)); 636 ASSERT_EQ(0, close(path_beneath_dir.parent_fd)); 637 ASSERT_EQ(0, close(ruleset_fd)); 638 } 639 640 TEST_F_FORK(layout0, ruleset_with_unknown_access) 641 { 642 __u64 access_mask; 643 644 for (access_mask = 1ULL << 63; access_mask != ACCESS_LAST; 645 access_mask >>= 1) { 646 struct landlock_ruleset_attr ruleset_attr = { 647 .handled_access_fs = access_mask, 648 }; 649 650 ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 651 sizeof(ruleset_attr), 0)); 652 ASSERT_EQ(EINVAL, errno); 653 } 654 } 655 656 TEST_F_FORK(layout0, rule_with_unknown_access) 657 { 658 __u64 access; 659 struct landlock_path_beneath_attr path_beneath = {}; 660 const struct landlock_ruleset_attr ruleset_attr = { 661 .handled_access_fs = ACCESS_ALL, 662 }; 663 const int ruleset_fd = 664 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 665 666 ASSERT_LE(0, ruleset_fd); 667 668 path_beneath.parent_fd = 669 open(TMP_DIR, O_PATH | O_DIRECTORY | O_CLOEXEC); 670 ASSERT_LE(0, path_beneath.parent_fd); 671 672 for (access = 1ULL << 63; access != ACCESS_LAST; access >>= 1) { 673 path_beneath.allowed_access = access; 674 EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, 675 LANDLOCK_RULE_PATH_BENEATH, 676 &path_beneath, 0)); 677 EXPECT_EQ(EINVAL, errno); 678 } 679 ASSERT_EQ(0, close(path_beneath.parent_fd)); 680 ASSERT_EQ(0, close(ruleset_fd)); 681 } 682 683 TEST_F_FORK(layout1, rule_with_unhandled_access) 684 { 685 struct landlock_ruleset_attr ruleset_attr = { 686 .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE, 687 }; 688 struct landlock_path_beneath_attr path_beneath = {}; 689 int ruleset_fd; 690 __u64 access; 691 692 ruleset_fd = 693 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 694 ASSERT_LE(0, ruleset_fd); 695 696 path_beneath.parent_fd = open(file1_s1d2, O_PATH | O_CLOEXEC); 697 ASSERT_LE(0, path_beneath.parent_fd); 698 699 for (access = 1; access > 0; access <<= 1) { 700 int err; 701 702 path_beneath.allowed_access = access; 703 err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 704 &path_beneath, 0); 705 if (access == ruleset_attr.handled_access_fs) { 706 EXPECT_EQ(0, err); 707 } else { 708 EXPECT_EQ(-1, err); 709 EXPECT_EQ(EINVAL, errno); 710 } 711 } 712 713 EXPECT_EQ(0, close(path_beneath.parent_fd)); 714 EXPECT_EQ(0, close(ruleset_fd)); 715 } 716 717 static void add_path_beneath(struct __test_metadata *const _metadata, 718 const int ruleset_fd, const __u64 allowed_access, 719 const char *const path, __u32 flags) 720 { 721 struct landlock_path_beneath_attr path_beneath = { 722 .allowed_access = allowed_access, 723 }; 724 725 path_beneath.parent_fd = open(path, O_PATH | O_CLOEXEC); 726 ASSERT_LE(0, path_beneath.parent_fd) 727 { 728 TH_LOG("Failed to open directory \"%s\": %s", path, 729 strerror(errno)); 730 } 731 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 732 &path_beneath, flags)) 733 { 734 TH_LOG("Failed to update the ruleset with \"%s\": %s", path, 735 strerror(errno)); 736 } 737 ASSERT_EQ(0, close(path_beneath.parent_fd)); 738 } 739 740 struct rule { 741 const char *path; 742 __u64 access; 743 }; 744 745 /* clang-format off */ 746 747 #define ACCESS_RO ( \ 748 LANDLOCK_ACCESS_FS_READ_FILE | \ 749 LANDLOCK_ACCESS_FS_READ_DIR) 750 751 #define ACCESS_RW ( \ 752 ACCESS_RO | \ 753 LANDLOCK_ACCESS_FS_WRITE_FILE) 754 755 /* clang-format on */ 756 757 static int create_ruleset(struct __test_metadata *const _metadata, 758 const __u64 handled_access_fs, 759 const struct rule rules[]) 760 { 761 int ruleset_fd, i; 762 struct landlock_ruleset_attr ruleset_attr = { 763 .handled_access_fs = handled_access_fs, 764 }; 765 766 ruleset_fd = 767 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 768 ASSERT_LE(0, ruleset_fd) 769 { 770 TH_LOG("Failed to create a ruleset: %s", strerror(errno)); 771 } 772 773 if (rules) 774 for (i = 0; rules[i].path; i++) { 775 if (!rules[i].access) 776 continue; 777 778 add_path_beneath(_metadata, ruleset_fd, rules[i].access, 779 rules[i].path, 0); 780 } 781 return ruleset_fd; 782 } 783 784 static void enforce_fs(struct __test_metadata *const _metadata, 785 const __u64 access_fs, const struct rule rules[]) 786 { 787 const int ruleset_fd = create_ruleset(_metadata, access_fs, rules); 788 789 enforce_ruleset(_metadata, ruleset_fd); 790 EXPECT_EQ(0, close(ruleset_fd)); 791 } 792 793 TEST_F_FORK(layout0, proc_nsfs) 794 { 795 const struct rule rules[] = { 796 { 797 .path = "/dev/null", 798 .access = LANDLOCK_ACCESS_FS_READ_FILE | 799 LANDLOCK_ACCESS_FS_WRITE_FILE, 800 }, 801 {}, 802 }; 803 struct landlock_path_beneath_attr path_beneath; 804 const int ruleset_fd = create_ruleset( 805 _metadata, rules[0].access | LANDLOCK_ACCESS_FS_READ_DIR, 806 rules); 807 808 ASSERT_LE(0, ruleset_fd); 809 ASSERT_EQ(0, test_open("/proc/self/ns/mnt", O_RDONLY)); 810 811 enforce_ruleset(_metadata, ruleset_fd); 812 813 ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 814 ASSERT_EQ(EACCES, test_open("/dev", O_RDONLY)); 815 ASSERT_EQ(0, test_open("/dev/null", O_RDONLY)); 816 ASSERT_EQ(EACCES, test_open("/dev/full", O_RDONLY)); 817 818 ASSERT_EQ(EACCES, test_open("/proc", O_RDONLY)); 819 ASSERT_EQ(EACCES, test_open("/proc/self", O_RDONLY)); 820 ASSERT_EQ(EACCES, test_open("/proc/self/ns", O_RDONLY)); 821 /* 822 * Because nsfs is an internal filesystem, /proc/self/ns/mnt is a 823 * disconnected path. Such path cannot be identified and must then be 824 * allowed. 825 */ 826 ASSERT_EQ(0, test_open("/proc/self/ns/mnt", O_RDONLY)); 827 828 /* 829 * Checks that it is not possible to add nsfs-like filesystem 830 * references to a ruleset. 831 */ 832 path_beneath.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE | 833 LANDLOCK_ACCESS_FS_WRITE_FILE, 834 path_beneath.parent_fd = open("/proc/self/ns/mnt", O_PATH | O_CLOEXEC); 835 ASSERT_LE(0, path_beneath.parent_fd); 836 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 837 &path_beneath, 0)); 838 ASSERT_EQ(EBADFD, errno); 839 ASSERT_EQ(0, close(path_beneath.parent_fd)); 840 } 841 842 TEST_F_FORK(layout0, unpriv) 843 { 844 const struct rule rules[] = { 845 { 846 .path = TMP_DIR, 847 .access = ACCESS_RO, 848 }, 849 {}, 850 }; 851 int ruleset_fd; 852 853 drop_caps(_metadata); 854 855 ruleset_fd = create_ruleset(_metadata, ACCESS_RO, rules); 856 ASSERT_LE(0, ruleset_fd); 857 ASSERT_EQ(-1, landlock_restrict_self(ruleset_fd, 0)); 858 ASSERT_EQ(EPERM, errno); 859 860 /* enforce_ruleset() calls prctl(no_new_privs). */ 861 enforce_ruleset(_metadata, ruleset_fd); 862 ASSERT_EQ(0, close(ruleset_fd)); 863 } 864 865 TEST_F_FORK(layout1, effective_access) 866 { 867 const struct rule rules[] = { 868 { 869 .path = dir_s1d2, 870 .access = ACCESS_RO, 871 }, 872 { 873 .path = file1_s2d2, 874 .access = LANDLOCK_ACCESS_FS_READ_FILE | 875 LANDLOCK_ACCESS_FS_WRITE_FILE, 876 }, 877 {}, 878 }; 879 char buf; 880 int reg_fd; 881 882 enforce_fs(_metadata, ACCESS_RW, rules); 883 884 /* Tests on a directory (with or without O_PATH). */ 885 ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 886 ASSERT_EQ(0, test_open("/", O_RDONLY | O_PATH)); 887 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY)); 888 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY | O_PATH)); 889 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 890 ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY | O_PATH)); 891 892 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 893 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 894 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 895 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 896 897 /* Tests on a file (with or without O_PATH). */ 898 ASSERT_EQ(EACCES, test_open(dir_s2d2, O_RDONLY)); 899 ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_PATH)); 900 901 ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY)); 902 903 /* Checks effective read and write actions. */ 904 reg_fd = open(file1_s2d2, O_RDWR | O_CLOEXEC); 905 ASSERT_LE(0, reg_fd); 906 ASSERT_EQ(1, write(reg_fd, ".", 1)); 907 ASSERT_LE(0, lseek(reg_fd, 0, SEEK_SET)); 908 ASSERT_EQ(1, read(reg_fd, &buf, 1)); 909 ASSERT_EQ('.', buf); 910 ASSERT_EQ(0, close(reg_fd)); 911 912 /* Just in case, double-checks effective actions. */ 913 reg_fd = open(file1_s2d2, O_RDONLY | O_CLOEXEC); 914 ASSERT_LE(0, reg_fd); 915 ASSERT_EQ(-1, write(reg_fd, &buf, 1)); 916 ASSERT_EQ(EBADF, errno); 917 ASSERT_EQ(0, close(reg_fd)); 918 } 919 920 TEST_F_FORK(layout1, unhandled_access) 921 { 922 const struct rule rules[] = { 923 { 924 .path = dir_s1d2, 925 .access = ACCESS_RO, 926 }, 927 {}, 928 }; 929 930 /* Here, we only handle read accesses, not write accesses. */ 931 enforce_fs(_metadata, ACCESS_RO, rules); 932 933 /* 934 * Because the policy does not handle LANDLOCK_ACCESS_FS_WRITE_FILE, 935 * opening for write-only should be allowed, but not read-write. 936 */ 937 ASSERT_EQ(0, test_open(file1_s1d1, O_WRONLY)); 938 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 939 940 ASSERT_EQ(0, test_open(file1_s1d2, O_WRONLY)); 941 ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR)); 942 } 943 944 TEST_F_FORK(layout1, ruleset_overlap) 945 { 946 const struct rule rules[] = { 947 /* These rules should be ORed among them. */ 948 { 949 .path = dir_s1d2, 950 .access = LANDLOCK_ACCESS_FS_READ_FILE | 951 LANDLOCK_ACCESS_FS_WRITE_FILE, 952 }, 953 { 954 .path = dir_s1d2, 955 .access = LANDLOCK_ACCESS_FS_READ_FILE | 956 LANDLOCK_ACCESS_FS_READ_DIR, 957 }, 958 {}, 959 }; 960 961 enforce_fs(_metadata, ACCESS_RW, rules); 962 963 /* Checks s1d1 hierarchy. */ 964 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 965 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 966 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 967 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 968 969 /* Checks s1d2 hierarchy. */ 970 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 971 ASSERT_EQ(0, test_open(file1_s1d2, O_WRONLY)); 972 ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR)); 973 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 974 975 /* Checks s1d3 hierarchy. */ 976 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 977 ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY)); 978 ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 979 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 980 } 981 982 TEST_F_FORK(layout1, layer_rule_unions) 983 { 984 const struct rule layer1[] = { 985 { 986 .path = dir_s1d2, 987 .access = LANDLOCK_ACCESS_FS_READ_FILE, 988 }, 989 /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */ 990 { 991 .path = dir_s1d3, 992 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 993 }, 994 {}, 995 }; 996 const struct rule layer2[] = { 997 /* Doesn't change anything from layer1. */ 998 { 999 .path = dir_s1d2, 1000 .access = LANDLOCK_ACCESS_FS_READ_FILE | 1001 LANDLOCK_ACCESS_FS_WRITE_FILE, 1002 }, 1003 {}, 1004 }; 1005 const struct rule layer3[] = { 1006 /* Only allows write (but not read) to dir_s1d3. */ 1007 { 1008 .path = dir_s1d2, 1009 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 1010 }, 1011 {}, 1012 }; 1013 1014 enforce_fs(_metadata, ACCESS_RW, layer1); 1015 1016 /* Checks s1d1 hierarchy with layer1. */ 1017 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 1018 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1019 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 1020 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1021 1022 /* Checks s1d2 hierarchy with layer1. */ 1023 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 1024 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1025 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR)); 1026 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1027 1028 /* Checks s1d3 hierarchy with layer1. */ 1029 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1030 ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY)); 1031 /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */ 1032 ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 1033 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1034 1035 /* Doesn't change anything from layer1. */ 1036 enforce_fs(_metadata, ACCESS_RW, layer2); 1037 1038 /* Checks s1d1 hierarchy with layer2. */ 1039 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 1040 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1041 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 1042 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1043 1044 /* Checks s1d2 hierarchy with layer2. */ 1045 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 1046 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1047 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR)); 1048 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1049 1050 /* Checks s1d3 hierarchy with layer2. */ 1051 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1052 ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY)); 1053 /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */ 1054 ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 1055 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1056 1057 /* Only allows write (but not read) to dir_s1d3. */ 1058 enforce_fs(_metadata, ACCESS_RW, layer3); 1059 1060 /* Checks s1d1 hierarchy with layer3. */ 1061 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 1062 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1063 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 1064 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1065 1066 /* Checks s1d2 hierarchy with layer3. */ 1067 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDONLY)); 1068 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1069 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR)); 1070 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1071 1072 /* Checks s1d3 hierarchy with layer3. */ 1073 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY)); 1074 ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY)); 1075 /* dir_s1d3 should now deny READ_FILE and WRITE_FILE (O_RDWR). */ 1076 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDWR)); 1077 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1078 } 1079 1080 TEST_F_FORK(layout1, non_overlapping_accesses) 1081 { 1082 const struct rule layer1[] = { 1083 { 1084 .path = dir_s1d2, 1085 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 1086 }, 1087 {}, 1088 }; 1089 const struct rule layer2[] = { 1090 { 1091 .path = dir_s1d3, 1092 .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 1093 }, 1094 {}, 1095 }; 1096 1097 ASSERT_EQ(0, unlink(file1_s1d1)); 1098 ASSERT_EQ(0, unlink(file1_s1d2)); 1099 1100 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, layer1); 1101 1102 ASSERT_EQ(-1, mknod(file1_s1d1, S_IFREG | 0700, 0)); 1103 ASSERT_EQ(EACCES, errno); 1104 ASSERT_EQ(0, mknod(file1_s1d2, S_IFREG | 0700, 0)); 1105 ASSERT_EQ(0, unlink(file1_s1d2)); 1106 1107 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_REMOVE_FILE, layer2); 1108 1109 /* Unchanged accesses for file creation. */ 1110 ASSERT_EQ(-1, mknod(file1_s1d1, S_IFREG | 0700, 0)); 1111 ASSERT_EQ(EACCES, errno); 1112 ASSERT_EQ(0, mknod(file1_s1d2, S_IFREG | 0700, 0)); 1113 1114 /* Checks file removing. */ 1115 ASSERT_EQ(-1, unlink(file1_s1d2)); 1116 ASSERT_EQ(EACCES, errno); 1117 ASSERT_EQ(0, unlink(file1_s1d3)); 1118 } 1119 1120 TEST_F_FORK(layout1, interleaved_masked_accesses) 1121 { 1122 /* 1123 * Checks overly restrictive rules: 1124 * layer 1: allows R s1d1/s1d2/s1d3/file1 1125 * layer 2: allows RW s1d1/s1d2/s1d3 1126 * allows W s1d1/s1d2 1127 * denies R s1d1/s1d2 1128 * layer 3: allows R s1d1 1129 * layer 4: allows R s1d1/s1d2 1130 * denies W s1d1/s1d2 1131 * layer 5: allows R s1d1/s1d2 1132 * layer 6: allows X ---- 1133 * layer 7: allows W s1d1/s1d2 1134 * denies R s1d1/s1d2 1135 */ 1136 const struct rule layer1_read[] = { 1137 /* Allows read access to file1_s1d3 with the first layer. */ 1138 { 1139 .path = file1_s1d3, 1140 .access = LANDLOCK_ACCESS_FS_READ_FILE, 1141 }, 1142 {}, 1143 }; 1144 /* First rule with write restrictions. */ 1145 const struct rule layer2_read_write[] = { 1146 /* Start by granting read-write access via its parent directory... */ 1147 { 1148 .path = dir_s1d3, 1149 .access = LANDLOCK_ACCESS_FS_READ_FILE | 1150 LANDLOCK_ACCESS_FS_WRITE_FILE, 1151 }, 1152 /* ...but also denies read access via its grandparent directory. */ 1153 { 1154 .path = dir_s1d2, 1155 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 1156 }, 1157 {}, 1158 }; 1159 const struct rule layer3_read[] = { 1160 /* Allows read access via its great-grandparent directory. */ 1161 { 1162 .path = dir_s1d1, 1163 .access = LANDLOCK_ACCESS_FS_READ_FILE, 1164 }, 1165 {}, 1166 }; 1167 const struct rule layer4_read_write[] = { 1168 /* 1169 * Try to confuse the deny access by denying write (but not 1170 * read) access via its grandparent directory. 1171 */ 1172 { 1173 .path = dir_s1d2, 1174 .access = LANDLOCK_ACCESS_FS_READ_FILE, 1175 }, 1176 {}, 1177 }; 1178 const struct rule layer5_read[] = { 1179 /* 1180 * Try to override layer2's deny read access by explicitly 1181 * allowing read access via file1_s1d3's grandparent. 1182 */ 1183 { 1184 .path = dir_s1d2, 1185 .access = LANDLOCK_ACCESS_FS_READ_FILE, 1186 }, 1187 {}, 1188 }; 1189 const struct rule layer6_execute[] = { 1190 /* 1191 * Restricts an unrelated file hierarchy with a new access 1192 * (non-overlapping) type. 1193 */ 1194 { 1195 .path = dir_s2d1, 1196 .access = LANDLOCK_ACCESS_FS_EXECUTE, 1197 }, 1198 {}, 1199 }; 1200 const struct rule layer7_read_write[] = { 1201 /* 1202 * Finally, denies read access to file1_s1d3 via its 1203 * grandparent. 1204 */ 1205 { 1206 .path = dir_s1d2, 1207 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 1208 }, 1209 {}, 1210 }; 1211 1212 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, layer1_read); 1213 1214 /* Checks that read access is granted for file1_s1d3 with layer 1. */ 1215 ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 1216 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 1217 ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY)); 1218 1219 enforce_fs(_metadata, 1220 LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE, 1221 layer2_read_write); 1222 1223 /* Checks that previous access rights are unchanged with layer 2. */ 1224 ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 1225 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 1226 ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY)); 1227 1228 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, layer3_read); 1229 1230 /* Checks that previous access rights are unchanged with layer 3. */ 1231 ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 1232 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 1233 ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY)); 1234 1235 /* This time, denies write access for the file hierarchy. */ 1236 enforce_fs(_metadata, 1237 LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE, 1238 layer4_read_write); 1239 1240 /* 1241 * Checks that the only change with layer 4 is that write access is 1242 * denied. 1243 */ 1244 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1245 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1246 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 1247 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY)); 1248 1249 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, layer5_read); 1250 1251 /* Checks that previous access rights are unchanged with layer 5. */ 1252 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1253 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1254 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY)); 1255 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 1256 1257 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_EXECUTE, layer6_execute); 1258 1259 /* Checks that previous access rights are unchanged with layer 6. */ 1260 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1261 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1262 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY)); 1263 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 1264 1265 enforce_fs(_metadata, 1266 LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE, 1267 layer7_read_write); 1268 1269 /* Checks read access is now denied with layer 7. */ 1270 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY)); 1271 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1272 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY)); 1273 ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 1274 } 1275 1276 TEST_F_FORK(layout1, inherit_subset) 1277 { 1278 const struct rule rules[] = { 1279 { 1280 .path = dir_s1d2, 1281 .access = LANDLOCK_ACCESS_FS_READ_FILE | 1282 LANDLOCK_ACCESS_FS_READ_DIR, 1283 }, 1284 {}, 1285 }; 1286 const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1287 1288 enforce_ruleset(_metadata, ruleset_fd); 1289 1290 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1291 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1292 1293 /* Write access is forbidden. */ 1294 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1295 /* Readdir access is allowed. */ 1296 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1297 1298 /* Write access is forbidden. */ 1299 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1300 /* Readdir access is allowed. */ 1301 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1302 1303 /* 1304 * Tests shared rule extension: the following rules should not grant 1305 * any new access, only remove some. Once enforced, these rules are 1306 * ANDed with the previous ones. 1307 */ 1308 add_path_beneath(_metadata, ruleset_fd, LANDLOCK_ACCESS_FS_WRITE_FILE, 1309 dir_s1d2, 0); 1310 /* 1311 * According to ruleset_fd, dir_s1d2 should now have the 1312 * LANDLOCK_ACCESS_FS_READ_FILE and LANDLOCK_ACCESS_FS_WRITE_FILE 1313 * access rights (even if this directory is opened a second time). 1314 * However, when enforcing this updated ruleset, the ruleset tied to 1315 * the current process (i.e. its domain) will still only have the 1316 * dir_s1d2 with LANDLOCK_ACCESS_FS_READ_FILE and 1317 * LANDLOCK_ACCESS_FS_READ_DIR accesses, but 1318 * LANDLOCK_ACCESS_FS_WRITE_FILE must not be allowed because it would 1319 * be a privilege escalation. 1320 */ 1321 enforce_ruleset(_metadata, ruleset_fd); 1322 1323 /* Same tests and results as above. */ 1324 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1325 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1326 1327 /* It is still forbidden to write in file1_s1d2. */ 1328 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1329 /* Readdir access is still allowed. */ 1330 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1331 1332 /* It is still forbidden to write in file1_s1d3. */ 1333 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1334 /* Readdir access is still allowed. */ 1335 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1336 1337 /* 1338 * Try to get more privileges by adding new access rights to the parent 1339 * directory: dir_s1d1. 1340 */ 1341 add_path_beneath(_metadata, ruleset_fd, ACCESS_RW, dir_s1d1, 0); 1342 enforce_ruleset(_metadata, ruleset_fd); 1343 1344 /* Same tests and results as above. */ 1345 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1346 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1347 1348 /* It is still forbidden to write in file1_s1d2. */ 1349 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1350 /* Readdir access is still allowed. */ 1351 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1352 1353 /* It is still forbidden to write in file1_s1d3. */ 1354 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1355 /* Readdir access is still allowed. */ 1356 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1357 1358 /* 1359 * Now, dir_s1d3 get a new rule tied to it, only allowing 1360 * LANDLOCK_ACCESS_FS_WRITE_FILE. The (kernel internal) difference is 1361 * that there was no rule tied to it before. 1362 */ 1363 add_path_beneath(_metadata, ruleset_fd, LANDLOCK_ACCESS_FS_WRITE_FILE, 1364 dir_s1d3, 0); 1365 enforce_ruleset(_metadata, ruleset_fd); 1366 ASSERT_EQ(0, close(ruleset_fd)); 1367 1368 /* 1369 * Same tests and results as above, except for open(dir_s1d3) which is 1370 * now denied because the new rule mask the rule previously inherited 1371 * from dir_s1d2. 1372 */ 1373 1374 /* Same tests and results as above. */ 1375 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1376 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1377 1378 /* It is still forbidden to write in file1_s1d2. */ 1379 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1380 /* Readdir access is still allowed. */ 1381 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1382 1383 /* It is still forbidden to write in file1_s1d3. */ 1384 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1385 /* 1386 * Readdir of dir_s1d3 is still allowed because of the OR policy inside 1387 * the same layer. 1388 */ 1389 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1390 } 1391 1392 TEST_F_FORK(layout1, inherit_superset) 1393 { 1394 const struct rule rules[] = { 1395 { 1396 .path = dir_s1d3, 1397 .access = ACCESS_RO, 1398 }, 1399 {}, 1400 }; 1401 const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1402 1403 enforce_ruleset(_metadata, ruleset_fd); 1404 1405 /* Readdir access is denied for dir_s1d2. */ 1406 ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1407 /* Readdir access is allowed for dir_s1d3. */ 1408 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1409 /* File access is allowed for file1_s1d3. */ 1410 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1411 1412 /* Now dir_s1d2, parent of dir_s1d3, gets a new rule tied to it. */ 1413 add_path_beneath(_metadata, ruleset_fd, 1414 LANDLOCK_ACCESS_FS_READ_FILE | 1415 LANDLOCK_ACCESS_FS_READ_DIR, 1416 dir_s1d2, 0); 1417 enforce_ruleset(_metadata, ruleset_fd); 1418 EXPECT_EQ(0, close(ruleset_fd)); 1419 1420 /* Readdir access is still denied for dir_s1d2. */ 1421 ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1422 /* Readdir access is still allowed for dir_s1d3. */ 1423 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1424 /* File access is still allowed for file1_s1d3. */ 1425 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1426 } 1427 1428 TEST_F_FORK(layout0, max_layers) 1429 { 1430 int i, err; 1431 const struct rule rules[] = { 1432 { 1433 .path = TMP_DIR, 1434 .access = ACCESS_RO, 1435 }, 1436 {}, 1437 }; 1438 const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1439 1440 for (i = 0; i < LANDLOCK_MAX_NUM_LAYERS; i++) 1441 enforce_ruleset(_metadata, ruleset_fd); 1442 1443 for (i = 0; i < 2; i++) { 1444 err = landlock_restrict_self(ruleset_fd, 0); 1445 ASSERT_EQ(-1, err); 1446 ASSERT_EQ(E2BIG, errno); 1447 } 1448 EXPECT_EQ(0, close(ruleset_fd)); 1449 } 1450 1451 TEST_F_FORK(layout1, empty_or_same_ruleset) 1452 { 1453 struct landlock_ruleset_attr ruleset_attr = {}; 1454 int ruleset_fd; 1455 1456 /* Tests empty handled_access_fs. */ 1457 ruleset_fd = 1458 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 1459 ASSERT_LE(-1, ruleset_fd); 1460 ASSERT_EQ(ENOMSG, errno); 1461 1462 /* Enforces policy which denies read access to all files. */ 1463 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, NULL); 1464 1465 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 1466 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1467 1468 /* Nests a policy which denies read access to all directories. */ 1469 ruleset_fd = 1470 create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_DIR, NULL); 1471 enforce_ruleset(_metadata, ruleset_fd); 1472 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 1473 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY)); 1474 1475 /* Enforces a second time with the same ruleset. */ 1476 enforce_ruleset(_metadata, ruleset_fd); 1477 ASSERT_EQ(0, close(ruleset_fd)); 1478 } 1479 1480 TEST_F_FORK(layout1, rule_on_mountpoint) 1481 { 1482 const struct rule rules[] = { 1483 { 1484 .path = dir_s1d1, 1485 .access = ACCESS_RO, 1486 }, 1487 { 1488 /* dir_s3d2 is a mount point. */ 1489 .path = dir_s3d2, 1490 .access = ACCESS_RO, 1491 }, 1492 {}, 1493 }; 1494 1495 enforce_fs(_metadata, ACCESS_RW, rules); 1496 1497 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1498 1499 ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY)); 1500 1501 ASSERT_EQ(EACCES, test_open(dir_s3d1, O_RDONLY)); 1502 ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY)); 1503 ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY)); 1504 } 1505 1506 TEST_F_FORK(layout1, rule_over_mountpoint) 1507 { 1508 const struct rule rules[] = { 1509 { 1510 .path = dir_s1d1, 1511 .access = ACCESS_RO, 1512 }, 1513 { 1514 /* dir_s3d2 is a mount point. */ 1515 .path = dir_s3d1, 1516 .access = ACCESS_RO, 1517 }, 1518 {}, 1519 }; 1520 1521 enforce_fs(_metadata, ACCESS_RW, rules); 1522 1523 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1524 1525 ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY)); 1526 1527 ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY)); 1528 ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY)); 1529 ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY)); 1530 } 1531 1532 /* 1533 * This test verifies that we can apply a landlock rule on the root directory 1534 * (which might require special handling). 1535 */ 1536 TEST_F_FORK(layout1, rule_over_root_allow_then_deny) 1537 { 1538 struct rule rules[] = { 1539 { 1540 .path = "/", 1541 .access = ACCESS_RO, 1542 }, 1543 {}, 1544 }; 1545 1546 enforce_fs(_metadata, ACCESS_RW, rules); 1547 1548 /* Checks allowed access. */ 1549 ASSERT_EQ(0, test_open("/", O_RDONLY)); 1550 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1551 1552 rules[0].access = LANDLOCK_ACCESS_FS_READ_FILE; 1553 enforce_fs(_metadata, ACCESS_RW, rules); 1554 1555 /* Checks denied access (on a directory). */ 1556 ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 1557 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY)); 1558 } 1559 1560 TEST_F_FORK(layout1, rule_over_root_deny) 1561 { 1562 const struct rule rules[] = { 1563 { 1564 .path = "/", 1565 .access = LANDLOCK_ACCESS_FS_READ_FILE, 1566 }, 1567 {}, 1568 }; 1569 1570 enforce_fs(_metadata, ACCESS_RW, rules); 1571 1572 /* Checks denied access (on a directory). */ 1573 ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 1574 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY)); 1575 } 1576 1577 TEST_F_FORK(layout1, rule_inside_mount_ns) 1578 { 1579 const struct rule rules[] = { 1580 { 1581 .path = "s3d3", 1582 .access = ACCESS_RO, 1583 }, 1584 {}, 1585 }; 1586 1587 set_cap(_metadata, CAP_SYS_ADMIN); 1588 ASSERT_EQ(0, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3)) 1589 { 1590 TH_LOG("Failed to pivot root: %s", strerror(errno)); 1591 }; 1592 ASSERT_EQ(0, chdir("/")); 1593 clear_cap(_metadata, CAP_SYS_ADMIN); 1594 1595 enforce_fs(_metadata, ACCESS_RW, rules); 1596 1597 ASSERT_EQ(0, test_open("s3d3", O_RDONLY)); 1598 ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 1599 } 1600 1601 TEST_F_FORK(layout1, mount_and_pivot) 1602 { 1603 const struct rule rules[] = { 1604 { 1605 .path = dir_s3d2, 1606 .access = ACCESS_RO, 1607 }, 1608 {}, 1609 }; 1610 1611 enforce_fs(_metadata, ACCESS_RW, rules); 1612 1613 set_cap(_metadata, CAP_SYS_ADMIN); 1614 ASSERT_EQ(-1, mount(NULL, dir_s3d2, NULL, MS_RDONLY, NULL)); 1615 ASSERT_EQ(EPERM, errno); 1616 ASSERT_EQ(-1, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3)); 1617 ASSERT_EQ(EPERM, errno); 1618 clear_cap(_metadata, CAP_SYS_ADMIN); 1619 } 1620 1621 TEST_F_FORK(layout1, move_mount) 1622 { 1623 const struct rule rules[] = { 1624 { 1625 .path = dir_s3d2, 1626 .access = ACCESS_RO, 1627 }, 1628 {}, 1629 }; 1630 1631 set_cap(_metadata, CAP_SYS_ADMIN); 1632 ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD, 1633 dir_s1d2, 0)) 1634 { 1635 TH_LOG("Failed to move mount: %s", strerror(errno)); 1636 } 1637 1638 ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s1d2, AT_FDCWD, 1639 dir_s3d2, 0)); 1640 clear_cap(_metadata, CAP_SYS_ADMIN); 1641 1642 enforce_fs(_metadata, ACCESS_RW, rules); 1643 1644 set_cap(_metadata, CAP_SYS_ADMIN); 1645 ASSERT_EQ(-1, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD, 1646 dir_s1d2, 0)); 1647 ASSERT_EQ(EPERM, errno); 1648 clear_cap(_metadata, CAP_SYS_ADMIN); 1649 } 1650 1651 TEST_F_FORK(layout1, topology_changes_with_net_only) 1652 { 1653 const struct landlock_ruleset_attr ruleset_net = { 1654 .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP | 1655 LANDLOCK_ACCESS_NET_CONNECT_TCP, 1656 }; 1657 1658 /* Add network restrictions. */ 1659 drop_access_rights(_metadata, &ruleset_net); 1660 1661 /* Mount, remount, move_mount, umount, and pivot_root checks. */ 1662 set_cap(_metadata, CAP_SYS_ADMIN); 1663 ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s1d2)); 1664 ASSERT_EQ(0, mount(NULL, dir_s1d2, NULL, MS_PRIVATE | MS_REC, NULL)); 1665 ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s1d2, AT_FDCWD, 1666 dir_s2d2, 0)); 1667 ASSERT_EQ(0, umount(dir_s2d2)); 1668 ASSERT_EQ(0, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3)); 1669 ASSERT_EQ(0, chdir("/")); 1670 clear_cap(_metadata, CAP_SYS_ADMIN); 1671 } 1672 1673 TEST_F_FORK(layout1, topology_changes_with_net_and_fs) 1674 { 1675 const struct landlock_ruleset_attr ruleset_net_fs = { 1676 .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP | 1677 LANDLOCK_ACCESS_NET_CONNECT_TCP, 1678 .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE, 1679 }; 1680 1681 /* Add network and filesystem restrictions. */ 1682 drop_access_rights(_metadata, &ruleset_net_fs); 1683 1684 /* Mount, remount, move_mount, umount, and pivot_root checks. */ 1685 set_cap(_metadata, CAP_SYS_ADMIN); 1686 ASSERT_EQ(-1, mount_opt(&mnt_tmp, dir_s1d2)); 1687 ASSERT_EQ(EPERM, errno); 1688 ASSERT_EQ(-1, mount(NULL, dir_s3d2, NULL, MS_PRIVATE | MS_REC, NULL)); 1689 ASSERT_EQ(EPERM, errno); 1690 ASSERT_EQ(-1, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD, 1691 dir_s2d2, 0)); 1692 ASSERT_EQ(EPERM, errno); 1693 ASSERT_EQ(-1, umount(dir_s3d2)); 1694 ASSERT_EQ(EPERM, errno); 1695 ASSERT_EQ(-1, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3)); 1696 ASSERT_EQ(EPERM, errno); 1697 clear_cap(_metadata, CAP_SYS_ADMIN); 1698 } 1699 1700 TEST_F_FORK(layout1, release_inodes) 1701 { 1702 const struct rule rules[] = { 1703 { 1704 .path = dir_s1d1, 1705 .access = ACCESS_RO, 1706 }, 1707 { 1708 .path = dir_s3d2, 1709 .access = ACCESS_RO, 1710 }, 1711 { 1712 .path = dir_s3d3, 1713 .access = ACCESS_RO, 1714 }, 1715 {}, 1716 }; 1717 const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1718 1719 /* Unmount a file hierarchy while it is being used by a ruleset. */ 1720 set_cap(_metadata, CAP_SYS_ADMIN); 1721 ASSERT_EQ(0, umount(dir_s3d2)); 1722 clear_cap(_metadata, CAP_SYS_ADMIN); 1723 1724 enforce_ruleset(_metadata, ruleset_fd); 1725 EXPECT_EQ(0, close(ruleset_fd)); 1726 1727 ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 1728 ASSERT_EQ(EACCES, test_open(dir_s3d2, O_RDONLY)); 1729 /* This dir_s3d3 would not be allowed and does not exist anyway. */ 1730 ASSERT_EQ(ENOENT, test_open(dir_s3d3, O_RDONLY)); 1731 } 1732 1733 /* 1734 * This test checks that a rule on a directory used as a mount point does not 1735 * grant access to the mount covering it. It is a generalization of the bind 1736 * mount case in layout3_fs.hostfs.release_inodes that tests hidden mount points. 1737 */ 1738 TEST_F_FORK(layout1, covered_rule) 1739 { 1740 const struct rule layer1[] = { 1741 { 1742 .path = dir_s3d2, 1743 .access = LANDLOCK_ACCESS_FS_READ_DIR, 1744 }, 1745 {}, 1746 }; 1747 int ruleset_fd; 1748 1749 /* Unmount to simplify FIXTURE_TEARDOWN. */ 1750 set_cap(_metadata, CAP_SYS_ADMIN); 1751 ASSERT_EQ(0, umount(dir_s3d2)); 1752 clear_cap(_metadata, CAP_SYS_ADMIN); 1753 1754 /* Creates a ruleset with the future hidden directory. */ 1755 ruleset_fd = 1756 create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_DIR, layer1); 1757 1758 /* Covers with a new mount point. */ 1759 set_cap(_metadata, CAP_SYS_ADMIN); 1760 ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s3d2)); 1761 clear_cap(_metadata, CAP_SYS_ADMIN); 1762 1763 ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY)); 1764 1765 enforce_ruleset(_metadata, ruleset_fd); 1766 ASSERT_EQ(0, close(ruleset_fd)); 1767 1768 /* Checks that access to the new mount point is denied. */ 1769 ASSERT_EQ(EACCES, test_open(dir_s3d2, O_RDONLY)); 1770 } 1771 1772 enum relative_access { 1773 REL_OPEN, 1774 REL_CHDIR, 1775 REL_CHROOT_ONLY, 1776 REL_CHROOT_CHDIR, 1777 }; 1778 1779 static void test_relative_path(struct __test_metadata *const _metadata, 1780 const enum relative_access rel) 1781 { 1782 /* 1783 * Common layer to check that chroot doesn't ignore it (i.e. a chroot 1784 * is not a disconnected root directory). 1785 */ 1786 const struct rule layer1_base[] = { 1787 { 1788 .path = TMP_DIR, 1789 .access = ACCESS_RO, 1790 }, 1791 {}, 1792 }; 1793 const struct rule layer2_subs[] = { 1794 { 1795 .path = dir_s1d2, 1796 .access = ACCESS_RO, 1797 }, 1798 { 1799 .path = dir_s2d2, 1800 .access = ACCESS_RO, 1801 }, 1802 {}, 1803 }; 1804 int dirfd, ruleset_fd; 1805 1806 enforce_fs(_metadata, ACCESS_RW, layer1_base); 1807 1808 ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_subs); 1809 1810 ASSERT_LE(0, ruleset_fd); 1811 switch (rel) { 1812 case REL_OPEN: 1813 case REL_CHDIR: 1814 break; 1815 case REL_CHROOT_ONLY: 1816 ASSERT_EQ(0, chdir(dir_s2d2)); 1817 break; 1818 case REL_CHROOT_CHDIR: 1819 ASSERT_EQ(0, chdir(dir_s1d2)); 1820 break; 1821 default: 1822 ASSERT_TRUE(false); 1823 return; 1824 } 1825 1826 set_cap(_metadata, CAP_SYS_CHROOT); 1827 enforce_ruleset(_metadata, ruleset_fd); 1828 1829 switch (rel) { 1830 case REL_OPEN: 1831 dirfd = open(dir_s1d2, O_DIRECTORY); 1832 ASSERT_LE(0, dirfd); 1833 break; 1834 case REL_CHDIR: 1835 ASSERT_EQ(0, chdir(dir_s1d2)); 1836 dirfd = AT_FDCWD; 1837 break; 1838 case REL_CHROOT_ONLY: 1839 /* Do chroot into dir_s1d2 (relative to dir_s2d2). */ 1840 ASSERT_EQ(0, chroot("../../s1d1/s1d2")) 1841 { 1842 TH_LOG("Failed to chroot: %s", strerror(errno)); 1843 } 1844 dirfd = AT_FDCWD; 1845 break; 1846 case REL_CHROOT_CHDIR: 1847 /* Do chroot into dir_s1d2. */ 1848 ASSERT_EQ(0, chroot(".")) 1849 { 1850 TH_LOG("Failed to chroot: %s", strerror(errno)); 1851 } 1852 dirfd = AT_FDCWD; 1853 break; 1854 } 1855 1856 ASSERT_EQ((rel == REL_CHROOT_CHDIR) ? 0 : EACCES, 1857 test_open_rel(dirfd, "..", O_RDONLY)); 1858 ASSERT_EQ(0, test_open_rel(dirfd, ".", O_RDONLY)); 1859 1860 if (rel == REL_CHROOT_ONLY) { 1861 /* The current directory is dir_s2d2. */ 1862 ASSERT_EQ(0, test_open_rel(dirfd, "./s2d3", O_RDONLY)); 1863 } else { 1864 /* The current directory is dir_s1d2. */ 1865 ASSERT_EQ(0, test_open_rel(dirfd, "./s1d3", O_RDONLY)); 1866 } 1867 1868 if (rel == REL_CHROOT_ONLY || rel == REL_CHROOT_CHDIR) { 1869 /* Checks the root dir_s1d2. */ 1870 ASSERT_EQ(0, test_open_rel(dirfd, "/..", O_RDONLY)); 1871 ASSERT_EQ(0, test_open_rel(dirfd, "/", O_RDONLY)); 1872 ASSERT_EQ(0, test_open_rel(dirfd, "/f1", O_RDONLY)); 1873 ASSERT_EQ(0, test_open_rel(dirfd, "/s1d3", O_RDONLY)); 1874 } 1875 1876 if (rel != REL_CHROOT_CHDIR) { 1877 ASSERT_EQ(EACCES, test_open_rel(dirfd, "../../s1d1", O_RDONLY)); 1878 ASSERT_EQ(0, test_open_rel(dirfd, "../../s1d1/s1d2", O_RDONLY)); 1879 ASSERT_EQ(0, test_open_rel(dirfd, "../../s1d1/s1d2/s1d3", 1880 O_RDONLY)); 1881 1882 ASSERT_EQ(EACCES, test_open_rel(dirfd, "../../s2d1", O_RDONLY)); 1883 ASSERT_EQ(0, test_open_rel(dirfd, "../../s2d1/s2d2", O_RDONLY)); 1884 ASSERT_EQ(0, test_open_rel(dirfd, "../../s2d1/s2d2/s2d3", 1885 O_RDONLY)); 1886 } 1887 1888 if (rel == REL_OPEN) 1889 ASSERT_EQ(0, close(dirfd)); 1890 ASSERT_EQ(0, close(ruleset_fd)); 1891 } 1892 1893 TEST_F_FORK(layout1, relative_open) 1894 { 1895 test_relative_path(_metadata, REL_OPEN); 1896 } 1897 1898 TEST_F_FORK(layout1, relative_chdir) 1899 { 1900 test_relative_path(_metadata, REL_CHDIR); 1901 } 1902 1903 TEST_F_FORK(layout1, relative_chroot_only) 1904 { 1905 test_relative_path(_metadata, REL_CHROOT_ONLY); 1906 } 1907 1908 TEST_F_FORK(layout1, relative_chroot_chdir) 1909 { 1910 test_relative_path(_metadata, REL_CHROOT_CHDIR); 1911 } 1912 1913 static void copy_file(struct __test_metadata *const _metadata, 1914 const char *const src_path, const char *const dst_path) 1915 { 1916 int dst_fd, src_fd; 1917 struct stat statbuf; 1918 1919 dst_fd = open(dst_path, O_WRONLY | O_TRUNC | O_CLOEXEC); 1920 ASSERT_LE(0, dst_fd) 1921 { 1922 TH_LOG("Failed to open \"%s\": %s", dst_path, strerror(errno)); 1923 } 1924 src_fd = open(src_path, O_RDONLY | O_CLOEXEC); 1925 ASSERT_LE(0, src_fd) 1926 { 1927 TH_LOG("Failed to open \"%s\": %s", src_path, strerror(errno)); 1928 } 1929 ASSERT_EQ(0, fstat(src_fd, &statbuf)); 1930 ASSERT_EQ(statbuf.st_size, 1931 sendfile(dst_fd, src_fd, 0, statbuf.st_size)); 1932 ASSERT_EQ(0, close(src_fd)); 1933 ASSERT_EQ(0, close(dst_fd)); 1934 } 1935 1936 static void test_execute(struct __test_metadata *const _metadata, const int err, 1937 const char *const path) 1938 { 1939 int status; 1940 char *const argv[] = { (char *)path, NULL }; 1941 const pid_t child = fork(); 1942 1943 ASSERT_LE(0, child); 1944 if (child == 0) { 1945 ASSERT_EQ(err ? -1 : 0, execve(path, argv, NULL)) 1946 { 1947 TH_LOG("Failed to execute \"%s\": %s", path, 1948 strerror(errno)); 1949 }; 1950 ASSERT_EQ(err, errno); 1951 _exit(__test_passed(_metadata) ? 2 : 1); 1952 return; 1953 } 1954 ASSERT_EQ(child, waitpid(child, &status, 0)); 1955 ASSERT_EQ(1, WIFEXITED(status)); 1956 ASSERT_EQ(err ? 2 : 0, WEXITSTATUS(status)) 1957 { 1958 TH_LOG("Unexpected return code for \"%s\"", path); 1959 }; 1960 } 1961 1962 static void test_check_exec(struct __test_metadata *const _metadata, 1963 const int err, const char *const path) 1964 { 1965 int ret; 1966 char *const argv[] = { (char *)path, NULL }; 1967 1968 ret = sys_execveat(AT_FDCWD, path, argv, NULL, 1969 AT_EMPTY_PATH | AT_EXECVE_CHECK); 1970 if (err) { 1971 EXPECT_EQ(-1, ret); 1972 EXPECT_EQ(errno, err); 1973 } else { 1974 EXPECT_EQ(0, ret); 1975 } 1976 } 1977 1978 TEST_F_FORK(layout1, execute) 1979 { 1980 const struct rule rules[] = { 1981 { 1982 .path = dir_s1d2, 1983 .access = LANDLOCK_ACCESS_FS_EXECUTE, 1984 }, 1985 {}, 1986 }; 1987 1988 copy_file(_metadata, bin_true, file1_s1d1); 1989 copy_file(_metadata, bin_true, file1_s1d2); 1990 copy_file(_metadata, bin_true, file1_s1d3); 1991 1992 /* Checks before file1_s1d1 being denied. */ 1993 test_execute(_metadata, 0, file1_s1d1); 1994 test_check_exec(_metadata, 0, file1_s1d1); 1995 1996 enforce_fs(_metadata, rules[0].access, rules); 1997 1998 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1999 ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 2000 test_execute(_metadata, EACCES, file1_s1d1); 2001 test_check_exec(_metadata, EACCES, file1_s1d1); 2002 2003 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 2004 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 2005 test_execute(_metadata, 0, file1_s1d2); 2006 test_check_exec(_metadata, 0, file1_s1d2); 2007 2008 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 2009 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 2010 test_execute(_metadata, 0, file1_s1d3); 2011 test_check_exec(_metadata, 0, file1_s1d3); 2012 } 2013 2014 TEST_F_FORK(layout1, umount_sandboxer) 2015 { 2016 int pipe_child[2], pipe_parent[2]; 2017 char buf_parent; 2018 pid_t child; 2019 int status; 2020 2021 copy_file(_metadata, bin_sandbox_and_launch, file1_s3d3); 2022 ASSERT_EQ(0, pipe2(pipe_child, 0)); 2023 ASSERT_EQ(0, pipe2(pipe_parent, 0)); 2024 2025 child = fork(); 2026 ASSERT_LE(0, child); 2027 if (child == 0) { 2028 char pipe_child_str[12], pipe_parent_str[12]; 2029 char *const argv[] = { (char *)file1_s3d3, 2030 (char *)bin_wait_pipe, pipe_child_str, 2031 pipe_parent_str, NULL }; 2032 2033 /* Passes the pipe FDs to the executed binary and its child. */ 2034 EXPECT_EQ(0, close(pipe_child[0])); 2035 EXPECT_EQ(0, close(pipe_parent[1])); 2036 snprintf(pipe_child_str, sizeof(pipe_child_str), "%d", 2037 pipe_child[1]); 2038 snprintf(pipe_parent_str, sizeof(pipe_parent_str), "%d", 2039 pipe_parent[0]); 2040 2041 /* 2042 * We need bin_sandbox_and_launch (copied inside the mount as 2043 * file1_s3d3) to execute bin_wait_pipe (outside the mount) to 2044 * make sure the mount point will not be EBUSY because of 2045 * file1_s3d3 being in use. This avoids a potential race 2046 * condition between the following read() and umount() calls. 2047 */ 2048 ASSERT_EQ(0, execve(argv[0], argv, NULL)) 2049 { 2050 TH_LOG("Failed to execute \"%s\": %s", argv[0], 2051 strerror(errno)); 2052 }; 2053 _exit(1); 2054 return; 2055 } 2056 2057 EXPECT_EQ(0, close(pipe_child[1])); 2058 EXPECT_EQ(0, close(pipe_parent[0])); 2059 2060 /* Waits for the child to sandbox itself. */ 2061 EXPECT_EQ(1, read(pipe_child[0], &buf_parent, 1)); 2062 2063 /* Tests that the sandboxer is tied to its mount point. */ 2064 set_cap(_metadata, CAP_SYS_ADMIN); 2065 EXPECT_EQ(-1, umount(dir_s3d2)); 2066 EXPECT_EQ(EBUSY, errno); 2067 clear_cap(_metadata, CAP_SYS_ADMIN); 2068 2069 /* Signals the child to launch a grandchild. */ 2070 EXPECT_EQ(1, write(pipe_parent[1], ".", 1)); 2071 2072 /* Waits for the grandchild. */ 2073 EXPECT_EQ(1, read(pipe_child[0], &buf_parent, 1)); 2074 2075 /* Tests that the domain's sandboxer is not tied to its mount point. */ 2076 set_cap(_metadata, CAP_SYS_ADMIN); 2077 EXPECT_EQ(0, umount(dir_s3d2)) 2078 { 2079 TH_LOG("Failed to umount \"%s\": %s", dir_s3d2, 2080 strerror(errno)); 2081 }; 2082 clear_cap(_metadata, CAP_SYS_ADMIN); 2083 2084 /* Signals the grandchild to terminate. */ 2085 EXPECT_EQ(1, write(pipe_parent[1], ".", 1)); 2086 ASSERT_EQ(child, waitpid(child, &status, 0)); 2087 ASSERT_EQ(1, WIFEXITED(status)); 2088 ASSERT_EQ(0, WEXITSTATUS(status)); 2089 } 2090 2091 TEST_F_FORK(layout1, link) 2092 { 2093 const struct rule layer1[] = { 2094 { 2095 .path = dir_s1d2, 2096 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2097 }, 2098 {}, 2099 }; 2100 const struct rule layer2[] = { 2101 { 2102 .path = dir_s1d3, 2103 .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 2104 }, 2105 {}, 2106 }; 2107 2108 ASSERT_EQ(0, unlink(file1_s1d1)); 2109 ASSERT_EQ(0, unlink(file1_s1d2)); 2110 ASSERT_EQ(0, unlink(file1_s1d3)); 2111 2112 enforce_fs(_metadata, layer1[0].access, layer1); 2113 2114 ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1)); 2115 ASSERT_EQ(EACCES, errno); 2116 2117 /* Denies linking because of reparenting. */ 2118 ASSERT_EQ(-1, link(file1_s2d1, file1_s1d2)); 2119 ASSERT_EQ(EXDEV, errno); 2120 ASSERT_EQ(-1, link(file2_s1d2, file1_s1d3)); 2121 ASSERT_EQ(EXDEV, errno); 2122 ASSERT_EQ(-1, link(file2_s1d3, file1_s1d2)); 2123 ASSERT_EQ(EXDEV, errno); 2124 2125 ASSERT_EQ(0, link(file2_s1d2, file1_s1d2)); 2126 ASSERT_EQ(0, link(file2_s1d3, file1_s1d3)); 2127 2128 /* Prepares for next unlinks. */ 2129 ASSERT_EQ(0, unlink(file2_s1d2)); 2130 ASSERT_EQ(0, unlink(file2_s1d3)); 2131 2132 enforce_fs(_metadata, layer2[0].access, layer2); 2133 2134 /* Checks that linkind doesn't require the ability to delete a file. */ 2135 ASSERT_EQ(0, link(file1_s1d2, file2_s1d2)); 2136 ASSERT_EQ(0, link(file1_s1d3, file2_s1d3)); 2137 } 2138 2139 static int test_rename(const char *const oldpath, const char *const newpath) 2140 { 2141 if (rename(oldpath, newpath)) 2142 return errno; 2143 return 0; 2144 } 2145 2146 static int test_exchange(const char *const oldpath, const char *const newpath) 2147 { 2148 if (renameat2(AT_FDCWD, oldpath, AT_FDCWD, newpath, RENAME_EXCHANGE)) 2149 return errno; 2150 return 0; 2151 } 2152 2153 static int test_renameat(int olddirfd, const char *oldpath, int newdirfd, 2154 const char *newpath) 2155 { 2156 if (renameat2(olddirfd, oldpath, newdirfd, newpath, 0)) 2157 return errno; 2158 return 0; 2159 } 2160 2161 static int test_exchangeat(int olddirfd, const char *oldpath, int newdirfd, 2162 const char *newpath) 2163 { 2164 if (renameat2(olddirfd, oldpath, newdirfd, newpath, RENAME_EXCHANGE)) 2165 return errno; 2166 return 0; 2167 } 2168 2169 TEST_F_FORK(layout1, rename_file) 2170 { 2171 const struct rule rules[] = { 2172 { 2173 .path = dir_s1d3, 2174 .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 2175 }, 2176 { 2177 .path = dir_s2d2, 2178 .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 2179 }, 2180 {}, 2181 }; 2182 2183 ASSERT_EQ(0, unlink(file1_s1d2)); 2184 2185 enforce_fs(_metadata, rules[0].access, rules); 2186 2187 /* 2188 * Tries to replace a file, from a directory that allows file removal, 2189 * but to a different directory (which also allows file removal). 2190 */ 2191 ASSERT_EQ(-1, rename(file1_s2d3, file1_s1d3)); 2192 ASSERT_EQ(EXDEV, errno); 2193 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d3, 2194 RENAME_EXCHANGE)); 2195 ASSERT_EQ(EXDEV, errno); 2196 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, dir_s1d3, 2197 RENAME_EXCHANGE)); 2198 ASSERT_EQ(EXDEV, errno); 2199 2200 /* 2201 * Tries to replace a file, from a directory that denies file removal, 2202 * to a different directory (which allows file removal). 2203 */ 2204 ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3)); 2205 ASSERT_EQ(EACCES, errno); 2206 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file1_s1d3, 2207 RENAME_EXCHANGE)); 2208 ASSERT_EQ(EACCES, errno); 2209 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d2, AT_FDCWD, file1_s1d3, 2210 RENAME_EXCHANGE)); 2211 ASSERT_EQ(EXDEV, errno); 2212 2213 /* Exchanges files and directories that partially allow removal. */ 2214 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d2, AT_FDCWD, file1_s2d1, 2215 RENAME_EXCHANGE)); 2216 ASSERT_EQ(EACCES, errno); 2217 /* Checks that file1_s2d1 cannot be removed (instead of ENOTDIR). */ 2218 ASSERT_EQ(-1, rename(dir_s2d2, file1_s2d1)); 2219 ASSERT_EQ(EACCES, errno); 2220 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, dir_s2d2, 2221 RENAME_EXCHANGE)); 2222 ASSERT_EQ(EACCES, errno); 2223 /* Checks that file1_s1d1 cannot be removed (instead of EISDIR). */ 2224 ASSERT_EQ(-1, rename(file1_s1d1, dir_s1d2)); 2225 ASSERT_EQ(EACCES, errno); 2226 2227 /* Renames files with different parents. */ 2228 ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d2)); 2229 ASSERT_EQ(EXDEV, errno); 2230 ASSERT_EQ(0, unlink(file1_s1d3)); 2231 ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3)); 2232 ASSERT_EQ(EACCES, errno); 2233 2234 /* Exchanges and renames files with same parent. */ 2235 ASSERT_EQ(0, renameat2(AT_FDCWD, file2_s2d3, AT_FDCWD, file1_s2d3, 2236 RENAME_EXCHANGE)); 2237 ASSERT_EQ(0, rename(file2_s2d3, file1_s2d3)); 2238 2239 /* Exchanges files and directories with same parent, twice. */ 2240 ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s2d3, 2241 RENAME_EXCHANGE)); 2242 ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s2d3, 2243 RENAME_EXCHANGE)); 2244 } 2245 2246 TEST_F_FORK(layout1, rename_whiteout_denied) 2247 { 2248 /* The affected file is a FIFO. */ 2249 ASSERT_EQ(0, unlink(file1_s3d3)); 2250 ASSERT_EQ(0, mknod(file1_s3d3, S_IFIFO | 0600, 0)); 2251 2252 /* Deny MAKE_REG, but allow MAKE_FIFO. */ 2253 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, NULL); 2254 2255 /* 2256 * Try to rename a file with RENAME_WHITEOUT. 2257 * file1_s3d3 is in dir_s3d2 (tmpfs), so it supports RENAME_WHITEOUT. 2258 * Denied, because whiteout creation is guarded with MAKE_REG. 2259 */ 2260 EXPECT_EQ(-1, renameat2(AT_FDCWD, file1_s3d3, AT_FDCWD, 2261 TMP_DIR "/s3d1/s3d2/s3d3/f2", RENAME_WHITEOUT)); 2262 EXPECT_EQ(EACCES, errno); 2263 } 2264 2265 static bool is_whiteout(const char *const path) 2266 { 2267 struct stat st; 2268 2269 if (stat(path, &st) == -1) 2270 return false; 2271 2272 return S_ISCHR(st.st_mode) && st.st_rdev == makedev(0, 0); 2273 } 2274 2275 static bool is_fifo(const char *const path) 2276 { 2277 struct stat st; 2278 2279 return stat(path, &st) == 0 && S_ISFIFO(st.st_mode); 2280 } 2281 2282 static bool is_missing(const char *const path) 2283 { 2284 struct stat st; 2285 2286 return stat(path, &st) == -1 && errno == ENOENT; 2287 } 2288 2289 TEST_F_FORK(layout1, rename_whiteout_allowed) 2290 { 2291 const struct rule rules[] = { 2292 { 2293 .path = dir_s3d3, 2294 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2295 }, 2296 {}, 2297 }; 2298 2299 /* The affected file is a FIFO. */ 2300 ASSERT_EQ(0, unlink(file1_s3d3)); 2301 ASSERT_EQ(0, mknod(file1_s3d3, S_IFIFO | 0600, 0)); 2302 2303 /* Allow MAKE_REG below dir_s3d3. */ 2304 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, rules); 2305 2306 /* 2307 * Rename a file with RENAME_WHITEOUT within the same directory. 2308 * Allowed, because MAKE_REG is granted for the whiteout object which 2309 * gets created in the source location. 2310 */ 2311 EXPECT_EQ(0, renameat2(AT_FDCWD, file1_s3d3, AT_FDCWD, 2312 TMP_DIR "/s3d1/s3d2/s3d3/f2", RENAME_WHITEOUT)); 2313 2314 /* A whiteout object took the place of the moved FIFO. */ 2315 EXPECT_TRUE(is_whiteout(file1_s3d3)); 2316 EXPECT_TRUE(is_fifo(TMP_DIR "/s3d1/s3d2/s3d3/f2")); 2317 } 2318 2319 TEST_F_FORK(layout1, rename_whiteout_reparenting) 2320 { 2321 const struct rule rules[] = { 2322 { 2323 .path = dir_s3d2, 2324 .access = LANDLOCK_ACCESS_FS_REFER, 2325 }, 2326 { 2327 .path = dir_s3d3, 2328 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2329 }, 2330 {}, 2331 }; 2332 2333 /* The moved files are FIFOs. */ 2334 ASSERT_EQ(0, unlink(file1_s3d3)); 2335 ASSERT_EQ(0, mknod(file1_s3d3, S_IFIFO | 0600, 0)); 2336 ASSERT_EQ(0, unlink(file1_s3d4)); 2337 ASSERT_EQ(0, mknod(file1_s3d4, S_IFIFO | 0600, 0)); 2338 2339 /* Allow REFER below dir_s3d2, but MAKE_REG only below dir_s3d3. */ 2340 enforce_fs(_metadata, 2341 LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, 2342 rules); 2343 2344 /* 2345 * The whiteout object is created in the source directory: Moving the 2346 * FIFO out of dir_s3d4 is denied because MAKE_REG is not granted 2347 * there, even though it is granted in the destination directory 2348 * dir_s3d3. 2349 */ 2350 EXPECT_EQ(-1, renameat2(AT_FDCWD, file1_s3d4, AT_FDCWD, 2351 TMP_DIR "/s3d1/s3d2/s3d3/f2", RENAME_WHITEOUT)); 2352 EXPECT_EQ(EACCES, errno); 2353 2354 /* 2355 * Moving the FIFO out of dir_s3d3 is allowed, because MAKE_REG is 2356 * granted there for the created whiteout object. 2357 */ 2358 EXPECT_EQ(0, renameat2(AT_FDCWD, file1_s3d3, AT_FDCWD, 2359 TMP_DIR "/s3d1/s3d2/s3d4/f2", RENAME_WHITEOUT)); 2360 2361 /* A whiteout object took the place of the moved FIFO. */ 2362 EXPECT_TRUE(is_whiteout(file1_s3d3)); 2363 EXPECT_TRUE(is_fifo(TMP_DIR "/s3d1/s3d2/s3d4/f2")); 2364 } 2365 2366 TEST_F_FORK(layout1, rename_whiteout_exchange) 2367 { 2368 const char *const whiteout_s3d3 = TMP_DIR "/s3d1/s3d2/s3d3/f2"; 2369 const struct rule rules[] = { 2370 { 2371 .path = dir_s3d2, 2372 .access = LANDLOCK_ACCESS_FS_REFER, 2373 }, 2374 { 2375 .path = dir_s3d3, 2376 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2377 }, 2378 {}, 2379 }; 2380 2381 /* The exchanged files are FIFOs and an existing whiteout object. */ 2382 ASSERT_EQ(0, unlink(file1_s3d3)); 2383 ASSERT_EQ(0, mknod(file1_s3d3, S_IFIFO | 0600, 0)); 2384 ASSERT_EQ(0, mknod(whiteout_s3d3, S_IFCHR | 0600, makedev(0, 0))); 2385 ASSERT_EQ(0, unlink(file1_s3d4)); 2386 ASSERT_EQ(0, mknod(file1_s3d4, S_IFIFO | 0600, 0)); 2387 2388 /* Allow REFER below dir_s3d2, but MAKE_REG only below dir_s3d3. */ 2389 enforce_fs(_metadata, 2390 LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, 2391 rules); 2392 2393 /* 2394 * With RENAME_EXCHANGE, the whiteout object moves into the source 2395 * directory of the rename: Exchanging the FIFO in dir_s3d4 with the 2396 * whiteout object is denied because MAKE_REG is not granted in 2397 * dir_s3d4, even though it is granted in the whiteout object's own 2398 * directory dir_s3d3. 2399 */ 2400 EXPECT_EQ(-1, renameat2(AT_FDCWD, file1_s3d4, AT_FDCWD, whiteout_s3d3, 2401 RENAME_EXCHANGE)); 2402 EXPECT_EQ(EACCES, errno); 2403 2404 /* 2405 * Exchanging the FIFO in dir_s3d3 with the whiteout object is 2406 * allowed, because MAKE_REG is granted in the directory into which 2407 * the whiteout object moves. 2408 */ 2409 EXPECT_EQ(0, renameat2(AT_FDCWD, file1_s3d3, AT_FDCWD, whiteout_s3d3, 2410 RENAME_EXCHANGE)); 2411 2412 /* The FIFO and the whiteout object swapped places. */ 2413 EXPECT_TRUE(is_whiteout(file1_s3d3)); 2414 EXPECT_TRUE(is_fifo(whiteout_s3d3)); 2415 } 2416 2417 TEST_F_FORK(layout1, rename_dir) 2418 { 2419 const struct rule rules[] = { 2420 { 2421 .path = dir_s1d2, 2422 .access = LANDLOCK_ACCESS_FS_REMOVE_DIR, 2423 }, 2424 { 2425 .path = dir_s2d1, 2426 .access = LANDLOCK_ACCESS_FS_REMOVE_DIR, 2427 }, 2428 {}, 2429 }; 2430 2431 /* Empties dir_s1d3 to allow renaming. */ 2432 ASSERT_EQ(0, unlink(file1_s1d3)); 2433 ASSERT_EQ(0, unlink(file2_s1d3)); 2434 2435 enforce_fs(_metadata, rules[0].access, rules); 2436 2437 /* Exchanges and renames directory to a different parent. */ 2438 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3, 2439 RENAME_EXCHANGE)); 2440 ASSERT_EQ(EXDEV, errno); 2441 ASSERT_EQ(-1, rename(dir_s2d3, dir_s1d3)); 2442 ASSERT_EQ(EXDEV, errno); 2443 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3, 2444 RENAME_EXCHANGE)); 2445 ASSERT_EQ(EXDEV, errno); 2446 2447 /* 2448 * Exchanges directory to the same parent, which doesn't allow 2449 * directory removal. 2450 */ 2451 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d1, AT_FDCWD, dir_s2d1, 2452 RENAME_EXCHANGE)); 2453 ASSERT_EQ(EACCES, errno); 2454 /* Checks that dir_s1d2 cannot be removed (instead of ENOTDIR). */ 2455 ASSERT_EQ(-1, rename(dir_s1d2, file1_s1d1)); 2456 ASSERT_EQ(EACCES, errno); 2457 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s1d2, 2458 RENAME_EXCHANGE)); 2459 ASSERT_EQ(EACCES, errno); 2460 /* Checks that dir_s1d2 cannot be removed (instead of EISDIR). */ 2461 ASSERT_EQ(-1, rename(file1_s1d1, dir_s1d2)); 2462 ASSERT_EQ(EACCES, errno); 2463 2464 /* 2465 * Exchanges and renames directory to the same parent, which allows 2466 * directory removal. 2467 */ 2468 ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, file1_s1d2, 2469 RENAME_EXCHANGE)); 2470 ASSERT_EQ(0, unlink(dir_s1d3)); 2471 ASSERT_EQ(0, mkdir(dir_s1d3, 0700)); 2472 ASSERT_EQ(0, rename(file1_s1d2, dir_s1d3)); 2473 ASSERT_EQ(0, rmdir(dir_s1d3)); 2474 } 2475 2476 TEST_F_FORK(layout1, reparent_refer) 2477 { 2478 const struct rule layer1[] = { 2479 { 2480 .path = dir_s1d2, 2481 .access = LANDLOCK_ACCESS_FS_REFER, 2482 }, 2483 { 2484 .path = dir_s2d2, 2485 .access = LANDLOCK_ACCESS_FS_REFER, 2486 }, 2487 {}, 2488 }; 2489 2490 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_REFER, layer1); 2491 2492 ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d1)); 2493 ASSERT_EQ(EXDEV, errno); 2494 ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d2)); 2495 ASSERT_EQ(EXDEV, errno); 2496 ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d3)); 2497 ASSERT_EQ(EXDEV, errno); 2498 2499 ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d1)); 2500 ASSERT_EQ(EXDEV, errno); 2501 ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d2)); 2502 ASSERT_EQ(EXDEV, errno); 2503 /* 2504 * Moving should only be allowed when the source and the destination 2505 * parent directory have REFER. 2506 */ 2507 ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d3)); 2508 ASSERT_EQ(ENOTEMPTY, errno); 2509 ASSERT_EQ(0, unlink(file1_s2d3)); 2510 ASSERT_EQ(0, unlink(file2_s2d3)); 2511 ASSERT_EQ(0, rename(dir_s1d3, dir_s2d3)); 2512 } 2513 2514 /* Checks renames beneath dir_s1d1. */ 2515 static void refer_denied_by_default(struct __test_metadata *const _metadata, 2516 const struct rule layer1[], 2517 const int layer1_err, 2518 const struct rule layer2[]) 2519 { 2520 ASSERT_EQ(0, unlink(file1_s1d2)); 2521 2522 enforce_fs(_metadata, layer1[0].access, layer1); 2523 2524 /* 2525 * If the first layer handles LANDLOCK_ACCESS_FS_REFER (according to 2526 * layer1_err), then it allows some different-parent renames and links. 2527 */ 2528 ASSERT_EQ(layer1_err, test_rename(file1_s1d1, file1_s1d2)); 2529 if (layer1_err == 0) 2530 ASSERT_EQ(layer1_err, test_rename(file1_s1d2, file1_s1d1)); 2531 ASSERT_EQ(layer1_err, test_exchange(file2_s1d1, file2_s1d2)); 2532 ASSERT_EQ(layer1_err, test_exchange(file2_s1d2, file2_s1d1)); 2533 2534 enforce_fs(_metadata, layer2[0].access, layer2); 2535 2536 /* 2537 * Now, either the first or the second layer does not handle 2538 * LANDLOCK_ACCESS_FS_REFER, which means that any different-parent 2539 * renames and links are denied, thus making the layer handling 2540 * LANDLOCK_ACCESS_FS_REFER null and void. 2541 */ 2542 ASSERT_EQ(EXDEV, test_rename(file1_s1d1, file1_s1d2)); 2543 ASSERT_EQ(EXDEV, test_exchange(file2_s1d1, file2_s1d2)); 2544 ASSERT_EQ(EXDEV, test_exchange(file2_s1d2, file2_s1d1)); 2545 } 2546 2547 const struct rule layer_dir_s1d1_refer[] = { 2548 { 2549 .path = dir_s1d1, 2550 .access = LANDLOCK_ACCESS_FS_REFER, 2551 }, 2552 {}, 2553 }; 2554 2555 const struct rule layer_dir_s1d1_execute[] = { 2556 { 2557 /* Matches a parent directory. */ 2558 .path = dir_s1d1, 2559 .access = LANDLOCK_ACCESS_FS_EXECUTE, 2560 }, 2561 {}, 2562 }; 2563 2564 const struct rule layer_dir_s2d1_execute[] = { 2565 { 2566 /* Does not match a parent directory. */ 2567 .path = dir_s2d1, 2568 .access = LANDLOCK_ACCESS_FS_EXECUTE, 2569 }, 2570 {}, 2571 }; 2572 2573 /* 2574 * Tests precedence over renames: denied by default for different parent 2575 * directories, *with* a rule matching a parent directory, but not directly 2576 * denying access (with MAKE_REG nor REMOVE). 2577 */ 2578 TEST_F_FORK(layout1, refer_denied_by_default1) 2579 { 2580 refer_denied_by_default(_metadata, layer_dir_s1d1_refer, 0, 2581 layer_dir_s1d1_execute); 2582 } 2583 2584 /* 2585 * Same test but this time turning around the ABI version order: the first 2586 * layer does not handle LANDLOCK_ACCESS_FS_REFER. 2587 */ 2588 TEST_F_FORK(layout1, refer_denied_by_default2) 2589 { 2590 refer_denied_by_default(_metadata, layer_dir_s1d1_execute, EXDEV, 2591 layer_dir_s1d1_refer); 2592 } 2593 2594 /* 2595 * Tests precedence over renames: denied by default for different parent 2596 * directories, *without* a rule matching a parent directory, but not directly 2597 * denying access (with MAKE_REG nor REMOVE). 2598 */ 2599 TEST_F_FORK(layout1, refer_denied_by_default3) 2600 { 2601 refer_denied_by_default(_metadata, layer_dir_s1d1_refer, 0, 2602 layer_dir_s2d1_execute); 2603 } 2604 2605 /* 2606 * Same test but this time turning around the ABI version order: the first 2607 * layer does not handle LANDLOCK_ACCESS_FS_REFER. 2608 */ 2609 TEST_F_FORK(layout1, refer_denied_by_default4) 2610 { 2611 refer_denied_by_default(_metadata, layer_dir_s2d1_execute, EXDEV, 2612 layer_dir_s1d1_refer); 2613 } 2614 2615 /* 2616 * Regression test for layered domains: The two enforced Landlock layers both do 2617 * not restrict the tested "refer" operations on their own, so they should also 2618 * not restrict them when they are layered, regardless of the layer order. 2619 */ 2620 static void test_layering_does_not_break_refer( 2621 struct __test_metadata *const _metadata, 2622 const struct landlock_ruleset_attr *const non_fs_attr, 2623 const bool non_fs_first) 2624 { 2625 ASSERT_EQ(0, unlink(file1_s1d2)); 2626 2627 if (non_fs_first) 2628 drop_access_rights(_metadata, non_fs_attr); 2629 2630 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_REFER, layer_dir_s1d1_refer); 2631 2632 if (!non_fs_first) 2633 drop_access_rights(_metadata, non_fs_attr); 2634 2635 EXPECT_EQ(0, test_rename(file1_s1d1, file1_s1d2)); 2636 EXPECT_EQ(0, test_rename(file1_s1d2, file1_s1d1)); 2637 EXPECT_EQ(0, test_exchange(file2_s1d1, file2_s1d2)); 2638 EXPECT_EQ(0, test_exchange(file2_s1d2, file2_s1d1)); 2639 } 2640 2641 static const struct landlock_ruleset_attr net_attr = { 2642 .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP, 2643 }; 2644 2645 static const struct landlock_ruleset_attr scoped_attr = { 2646 .scoped = LANDLOCK_SCOPE_SIGNAL, 2647 }; 2648 2649 TEST_F_FORK(layout1, layering_fs_then_net_does_not_break_refer) 2650 { 2651 test_layering_does_not_break_refer(_metadata, &net_attr, false); 2652 } 2653 2654 TEST_F_FORK(layout1, layering_net_then_fs_does_not_break_refer) 2655 { 2656 test_layering_does_not_break_refer(_metadata, &net_attr, true); 2657 } 2658 2659 TEST_F_FORK(layout1, layering_fs_then_scoped_does_not_break_refer) 2660 { 2661 test_layering_does_not_break_refer(_metadata, &scoped_attr, false); 2662 } 2663 2664 TEST_F_FORK(layout1, layering_scoped_then_fs_does_not_break_refer) 2665 { 2666 test_layering_does_not_break_refer(_metadata, &scoped_attr, true); 2667 } 2668 2669 /* 2670 * Tests walking through a denied root mount. 2671 */ 2672 TEST_F_FORK(layout1, refer_mount_root_deny) 2673 { 2674 int root_fd; 2675 2676 /* Creates a mount object from a non-mount point. */ 2677 set_cap(_metadata, CAP_SYS_ADMIN); 2678 root_fd = sys_open_tree(AT_FDCWD, dir_s1d1, 2679 AT_EMPTY_PATH | OPEN_TREE_CLONE | 2680 OPEN_TREE_CLOEXEC); 2681 clear_cap(_metadata, CAP_SYS_ADMIN); 2682 ASSERT_LE(0, root_fd); 2683 2684 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_MAKE_DIR, NULL); 2685 2686 /* Link denied by Landlock: EACCES. */ 2687 EXPECT_EQ(-1, linkat(root_fd, ".", root_fd, "does_not_exist", 0)); 2688 EXPECT_EQ(EACCES, errno); 2689 2690 /* renameat2() always returns EBUSY. */ 2691 EXPECT_EQ(-1, renameat2(root_fd, ".", root_fd, "does_not_exist", 0)); 2692 EXPECT_EQ(EBUSY, errno); 2693 2694 EXPECT_EQ(0, close(root_fd)); 2695 } 2696 2697 TEST_F_FORK(layout1, refer_part_mount_tree_is_allowed) 2698 { 2699 const struct rule layer1[] = { 2700 { 2701 /* Parent mount point. */ 2702 .path = dir_s3d1, 2703 .access = LANDLOCK_ACCESS_FS_REFER | 2704 LANDLOCK_ACCESS_FS_MAKE_REG, 2705 }, 2706 { 2707 /* 2708 * Removing the source file is allowed because its 2709 * access rights are already a superset of the 2710 * destination. 2711 */ 2712 .path = dir_s3d4, 2713 .access = LANDLOCK_ACCESS_FS_REFER | 2714 LANDLOCK_ACCESS_FS_MAKE_REG | 2715 LANDLOCK_ACCESS_FS_REMOVE_FILE, 2716 }, 2717 {}, 2718 }; 2719 2720 ASSERT_EQ(0, unlink(file1_s3d3)); 2721 enforce_fs(_metadata, 2722 LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG | 2723 LANDLOCK_ACCESS_FS_REMOVE_FILE, 2724 layer1); 2725 2726 ASSERT_EQ(0, rename(file1_s3d4, file1_s3d3)); 2727 } 2728 2729 TEST_F_FORK(layout1, reparent_link) 2730 { 2731 const struct rule layer1[] = { 2732 { 2733 .path = dir_s1d2, 2734 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2735 }, 2736 { 2737 .path = dir_s1d3, 2738 .access = LANDLOCK_ACCESS_FS_REFER, 2739 }, 2740 { 2741 .path = dir_s2d2, 2742 .access = LANDLOCK_ACCESS_FS_REFER, 2743 }, 2744 { 2745 .path = dir_s2d3, 2746 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2747 }, 2748 {}, 2749 }; 2750 2751 enforce_fs(_metadata, 2752 LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, 2753 layer1); 2754 2755 ASSERT_EQ(0, unlink(file1_s1d1)); 2756 ASSERT_EQ(0, unlink(file1_s1d2)); 2757 ASSERT_EQ(0, unlink(file1_s1d3)); 2758 2759 /* Denies linking because of missing MAKE_REG. */ 2760 ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1)); 2761 ASSERT_EQ(EACCES, errno); 2762 /* Denies linking because of missing source and destination REFER. */ 2763 ASSERT_EQ(-1, link(file1_s2d1, file1_s1d2)); 2764 ASSERT_EQ(EXDEV, errno); 2765 /* Denies linking because of missing source REFER. */ 2766 ASSERT_EQ(-1, link(file1_s2d1, file1_s1d3)); 2767 ASSERT_EQ(EXDEV, errno); 2768 2769 /* Denies linking because of missing MAKE_REG. */ 2770 ASSERT_EQ(-1, link(file1_s2d2, file1_s1d1)); 2771 ASSERT_EQ(EACCES, errno); 2772 /* Denies linking because of missing destination REFER. */ 2773 ASSERT_EQ(-1, link(file1_s2d2, file1_s1d2)); 2774 ASSERT_EQ(EXDEV, errno); 2775 2776 /* Allows linking because of REFER and MAKE_REG. */ 2777 ASSERT_EQ(0, link(file1_s2d2, file1_s1d3)); 2778 ASSERT_EQ(0, unlink(file1_s2d2)); 2779 /* Reverse linking denied because of missing MAKE_REG. */ 2780 ASSERT_EQ(-1, link(file1_s1d3, file1_s2d2)); 2781 ASSERT_EQ(EACCES, errno); 2782 ASSERT_EQ(0, unlink(file1_s2d3)); 2783 /* Checks reverse linking. */ 2784 ASSERT_EQ(0, link(file1_s1d3, file1_s2d3)); 2785 ASSERT_EQ(0, unlink(file1_s1d3)); 2786 2787 /* 2788 * This is OK for a file link, but it should not be allowed for a 2789 * directory rename (because of the superset of access rights. 2790 */ 2791 ASSERT_EQ(0, link(file1_s2d3, file1_s1d3)); 2792 ASSERT_EQ(0, unlink(file1_s1d3)); 2793 2794 ASSERT_EQ(-1, link(file2_s1d2, file1_s1d3)); 2795 ASSERT_EQ(EXDEV, errno); 2796 ASSERT_EQ(-1, link(file2_s1d3, file1_s1d2)); 2797 ASSERT_EQ(EXDEV, errno); 2798 2799 ASSERT_EQ(0, link(file2_s1d2, file1_s1d2)); 2800 ASSERT_EQ(0, link(file2_s1d3, file1_s1d3)); 2801 } 2802 2803 TEST_F_FORK(layout1, reparent_rename) 2804 { 2805 /* Same rules as for reparent_link. */ 2806 const struct rule layer1[] = { 2807 { 2808 .path = dir_s1d2, 2809 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2810 }, 2811 { 2812 .path = dir_s1d3, 2813 .access = LANDLOCK_ACCESS_FS_REFER, 2814 }, 2815 { 2816 .path = dir_s2d2, 2817 .access = LANDLOCK_ACCESS_FS_REFER, 2818 }, 2819 { 2820 .path = dir_s2d3, 2821 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2822 }, 2823 {}, 2824 }; 2825 2826 enforce_fs(_metadata, 2827 LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, 2828 layer1); 2829 2830 ASSERT_EQ(0, unlink(file1_s1d2)); 2831 ASSERT_EQ(0, unlink(file1_s1d3)); 2832 2833 /* Denies renaming because of missing MAKE_REG. */ 2834 ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file1_s1d1, 2835 RENAME_EXCHANGE)); 2836 ASSERT_EQ(EACCES, errno); 2837 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file2_s1d1, 2838 RENAME_EXCHANGE)); 2839 ASSERT_EQ(EACCES, errno); 2840 ASSERT_EQ(0, unlink(file1_s1d1)); 2841 ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1)); 2842 ASSERT_EQ(EACCES, errno); 2843 /* Even denies same file exchange. */ 2844 ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file2_s1d1, 2845 RENAME_EXCHANGE)); 2846 ASSERT_EQ(EACCES, errno); 2847 2848 /* Denies renaming because of missing source and destination REFER. */ 2849 ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d2)); 2850 ASSERT_EQ(EXDEV, errno); 2851 /* 2852 * Denies renaming because of missing MAKE_REG, source and destination 2853 * REFER. 2854 */ 2855 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file2_s1d1, 2856 RENAME_EXCHANGE)); 2857 ASSERT_EQ(EACCES, errno); 2858 ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file1_s2d1, 2859 RENAME_EXCHANGE)); 2860 ASSERT_EQ(EACCES, errno); 2861 2862 /* Denies renaming because of missing source REFER. */ 2863 ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3)); 2864 ASSERT_EQ(EXDEV, errno); 2865 /* Denies renaming because of missing MAKE_REG. */ 2866 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file2_s1d3, 2867 RENAME_EXCHANGE)); 2868 ASSERT_EQ(EACCES, errno); 2869 2870 /* Denies renaming because of missing MAKE_REG. */ 2871 ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d1)); 2872 ASSERT_EQ(EACCES, errno); 2873 /* Denies renaming because of missing destination REFER*/ 2874 ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d2)); 2875 ASSERT_EQ(EXDEV, errno); 2876 2877 /* Denies exchange because of one missing MAKE_REG. */ 2878 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, file2_s1d3, 2879 RENAME_EXCHANGE)); 2880 ASSERT_EQ(EACCES, errno); 2881 /* Allows renaming because of REFER and MAKE_REG. */ 2882 ASSERT_EQ(0, rename(file1_s2d2, file1_s1d3)); 2883 2884 /* Reverse renaming denied because of missing MAKE_REG. */ 2885 ASSERT_EQ(-1, rename(file1_s1d3, file1_s2d2)); 2886 ASSERT_EQ(EACCES, errno); 2887 ASSERT_EQ(0, unlink(file1_s2d3)); 2888 ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3)); 2889 2890 /* Tests reverse renaming. */ 2891 ASSERT_EQ(0, rename(file1_s2d3, file1_s1d3)); 2892 ASSERT_EQ(0, renameat2(AT_FDCWD, file2_s2d3, AT_FDCWD, file1_s1d3, 2893 RENAME_EXCHANGE)); 2894 ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3)); 2895 2896 /* 2897 * This is OK for a file rename, but it should not be allowed for a 2898 * directory rename (because of the superset of access rights). 2899 */ 2900 ASSERT_EQ(0, rename(file1_s2d3, file1_s1d3)); 2901 ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3)); 2902 2903 /* 2904 * Tests superset restrictions applied to directories. Not only the 2905 * dir_s2d3's parent (dir_s2d2) should be taken into account but also 2906 * access rights tied to dir_s2d3. dir_s2d2 is missing one access right 2907 * compared to dir_s1d3/file1_s1d3 (MAKE_REG) but it is provided 2908 * directly by the moved dir_s2d3. 2909 */ 2910 ASSERT_EQ(0, rename(dir_s2d3, file1_s1d3)); 2911 ASSERT_EQ(0, rename(file1_s1d3, dir_s2d3)); 2912 /* 2913 * The first rename is allowed but not the exchange because dir_s1d3's 2914 * parent (dir_s1d2) doesn't have REFER. 2915 */ 2916 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, dir_s1d3, 2917 RENAME_EXCHANGE)); 2918 ASSERT_EQ(EXDEV, errno); 2919 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, file1_s2d3, 2920 RENAME_EXCHANGE)); 2921 ASSERT_EQ(EXDEV, errno); 2922 ASSERT_EQ(-1, rename(file1_s2d3, dir_s1d3)); 2923 ASSERT_EQ(EXDEV, errno); 2924 2925 ASSERT_EQ(-1, rename(file2_s1d2, file1_s1d3)); 2926 ASSERT_EQ(EXDEV, errno); 2927 ASSERT_EQ(-1, rename(file2_s1d3, file1_s1d2)); 2928 ASSERT_EQ(EXDEV, errno); 2929 2930 /* Renaming in the same directory is always allowed. */ 2931 ASSERT_EQ(0, rename(file2_s1d2, file1_s1d2)); 2932 ASSERT_EQ(0, rename(file2_s1d3, file1_s1d3)); 2933 2934 ASSERT_EQ(0, unlink(file1_s1d2)); 2935 /* Denies because of missing source MAKE_REG and destination REFER. */ 2936 ASSERT_EQ(-1, rename(dir_s2d3, file1_s1d2)); 2937 ASSERT_EQ(EXDEV, errno); 2938 2939 ASSERT_EQ(0, unlink(file1_s1d3)); 2940 /* Denies because of missing source MAKE_REG and REFER. */ 2941 ASSERT_EQ(-1, rename(dir_s2d2, file1_s1d3)); 2942 ASSERT_EQ(EXDEV, errno); 2943 } 2944 2945 static void 2946 reparent_exdev_layers_enforce1(struct __test_metadata *const _metadata) 2947 { 2948 const struct rule layer1[] = { 2949 { 2950 .path = dir_s1d2, 2951 .access = LANDLOCK_ACCESS_FS_REFER, 2952 }, 2953 { 2954 /* Interesting for the layer2 tests. */ 2955 .path = dir_s1d3, 2956 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2957 }, 2958 { 2959 .path = dir_s2d2, 2960 .access = LANDLOCK_ACCESS_FS_REFER, 2961 }, 2962 { 2963 .path = dir_s2d3, 2964 .access = LANDLOCK_ACCESS_FS_MAKE_REG, 2965 }, 2966 {}, 2967 }; 2968 enforce_fs(_metadata, 2969 LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, 2970 layer1); 2971 } 2972 2973 static void 2974 reparent_exdev_layers_enforce2(struct __test_metadata *const _metadata) 2975 { 2976 const struct rule layer2[] = { 2977 { 2978 .path = dir_s2d3, 2979 .access = LANDLOCK_ACCESS_FS_MAKE_DIR, 2980 }, 2981 {}, 2982 }; 2983 /* 2984 * Same checks as before but with a second layer and a new MAKE_DIR 2985 * rule (and no explicit handling of REFER). 2986 */ 2987 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_MAKE_DIR, layer2); 2988 } 2989 2990 TEST_F_FORK(layout1, reparent_exdev_layers_rename1) 2991 { 2992 ASSERT_EQ(0, unlink(file1_s2d2)); 2993 ASSERT_EQ(0, unlink(file1_s2d3)); 2994 2995 reparent_exdev_layers_enforce1(_metadata); 2996 2997 /* 2998 * Moving the dir_s1d3 directory below dir_s2d2 is allowed by Landlock 2999 * because it doesn't inherit new access rights. 3000 */ 3001 ASSERT_EQ(0, rename(dir_s1d3, file1_s2d2)); 3002 ASSERT_EQ(0, rename(file1_s2d2, dir_s1d3)); 3003 3004 /* 3005 * Moving the dir_s1d3 directory below dir_s2d3 is allowed, even if it 3006 * gets a new inherited access rights (MAKE_REG), because MAKE_REG is 3007 * already allowed for dir_s1d3. 3008 */ 3009 ASSERT_EQ(0, rename(dir_s1d3, file1_s2d3)); 3010 ASSERT_EQ(0, rename(file1_s2d3, dir_s1d3)); 3011 3012 /* 3013 * However, moving the file1_s1d3 file below dir_s2d3 is allowed 3014 * because it cannot inherit MAKE_REG right (which is dedicated to 3015 * directories). 3016 */ 3017 ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3)); 3018 3019 reparent_exdev_layers_enforce2(_metadata); 3020 3021 /* 3022 * Moving the dir_s1d3 directory below dir_s2d2 is now denied because 3023 * MAKE_DIR is not tied to dir_s2d2. 3024 */ 3025 ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d2)); 3026 ASSERT_EQ(EACCES, errno); 3027 3028 /* 3029 * Moving the dir_s1d3 directory below dir_s2d3 is forbidden because it 3030 * would grants MAKE_REG and MAKE_DIR rights to it. 3031 */ 3032 ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d3)); 3033 ASSERT_EQ(EXDEV, errno); 3034 3035 /* 3036 * Moving the file2_s1d3 file below dir_s2d3 is denied because the 3037 * second layer does not handle REFER, which is always denied as soon as 3038 * any other filesystem access right is handled. 3039 */ 3040 ASSERT_EQ(-1, rename(file2_s1d3, file1_s2d3)); 3041 ASSERT_EQ(EXDEV, errno); 3042 } 3043 3044 TEST_F_FORK(layout1, reparent_exdev_layers_rename2) 3045 { 3046 reparent_exdev_layers_enforce1(_metadata); 3047 3048 /* Checks EACCES predominance over EXDEV. */ 3049 ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d2)); 3050 ASSERT_EQ(EACCES, errno); 3051 ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d2)); 3052 ASSERT_EQ(EACCES, errno); 3053 ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d3)); 3054 ASSERT_EQ(EXDEV, errno); 3055 /* Modify layout! */ 3056 ASSERT_EQ(0, rename(file1_s1d2, file1_s2d3)); 3057 3058 /* Without REFER source. */ 3059 ASSERT_EQ(-1, rename(dir_s1d1, file1_s2d2)); 3060 ASSERT_EQ(EXDEV, errno); 3061 ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d2)); 3062 ASSERT_EQ(EXDEV, errno); 3063 3064 reparent_exdev_layers_enforce2(_metadata); 3065 3066 /* Checks EACCES predominance over EXDEV. */ 3067 ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d2)); 3068 ASSERT_EQ(EACCES, errno); 3069 /* Checks with actual file2_s1d2. */ 3070 ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d2)); 3071 ASSERT_EQ(EACCES, errno); 3072 ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d3)); 3073 ASSERT_EQ(EXDEV, errno); 3074 /* 3075 * Modifying the layout is now denied because the second layer does not 3076 * handle REFER, which is always denied as soon as any other filesystem 3077 * access right is handled. 3078 */ 3079 ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d3)); 3080 ASSERT_EQ(EXDEV, errno); 3081 3082 /* Without REFER source, EACCES wins over EXDEV. */ 3083 ASSERT_EQ(-1, rename(dir_s1d1, file1_s2d2)); 3084 ASSERT_EQ(EACCES, errno); 3085 ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d2)); 3086 ASSERT_EQ(EACCES, errno); 3087 } 3088 3089 TEST_F_FORK(layout1, reparent_exdev_layers_exchange1) 3090 { 3091 const char *const dir_file1_s1d2 = file1_s1d2, *const dir_file2_s2d3 = 3092 file2_s2d3; 3093 3094 ASSERT_EQ(0, unlink(file1_s1d2)); 3095 ASSERT_EQ(0, mkdir(file1_s1d2, 0700)); 3096 ASSERT_EQ(0, unlink(file2_s2d3)); 3097 ASSERT_EQ(0, mkdir(file2_s2d3, 0700)); 3098 3099 reparent_exdev_layers_enforce1(_metadata); 3100 3101 /* Error predominance with file exchange: returns EXDEV and EACCES. */ 3102 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d3, 3103 RENAME_EXCHANGE)); 3104 ASSERT_EQ(EACCES, errno); 3105 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d1, 3106 RENAME_EXCHANGE)); 3107 ASSERT_EQ(EACCES, errno); 3108 3109 /* 3110 * Checks with directories which creation could be allowed, but denied 3111 * because of access rights that would be inherited. 3112 */ 3113 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD, 3114 dir_file2_s2d3, RENAME_EXCHANGE)); 3115 ASSERT_EQ(EXDEV, errno); 3116 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, 3117 dir_file1_s1d2, RENAME_EXCHANGE)); 3118 ASSERT_EQ(EXDEV, errno); 3119 3120 /* Checks with same access rights. */ 3121 ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, dir_s2d3, 3122 RENAME_EXCHANGE)); 3123 ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3, 3124 RENAME_EXCHANGE)); 3125 3126 /* Checks with different (child-only) access rights. */ 3127 ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_file1_s1d2, 3128 RENAME_EXCHANGE)); 3129 ASSERT_EQ(0, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD, dir_s2d3, 3130 RENAME_EXCHANGE)); 3131 3132 /* 3133 * Checks that exchange between file and directory are consistent. 3134 * 3135 * Moving a file (file1_s2d2) to a directory which only grants more 3136 * directory-related access rights is allowed, and at the same time 3137 * moving a directory (dir_file2_s2d3) to another directory which 3138 * grants less access rights is allowed too. 3139 * 3140 * See layout1.reparent_exdev_layers_exchange3 for inverted arguments. 3141 */ 3142 ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3, 3143 RENAME_EXCHANGE)); 3144 /* 3145 * However, moving back the directory is denied because it would get 3146 * more access rights than the current state and because file creation 3147 * is forbidden (in dir_s2d2). 3148 */ 3149 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2, 3150 RENAME_EXCHANGE)); 3151 ASSERT_EQ(EACCES, errno); 3152 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3, 3153 RENAME_EXCHANGE)); 3154 ASSERT_EQ(EACCES, errno); 3155 3156 reparent_exdev_layers_enforce2(_metadata); 3157 3158 /* Error predominance with file exchange: returns EXDEV and EACCES. */ 3159 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d3, 3160 RENAME_EXCHANGE)); 3161 ASSERT_EQ(EACCES, errno); 3162 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d1, 3163 RENAME_EXCHANGE)); 3164 ASSERT_EQ(EACCES, errno); 3165 3166 /* Checks with directories which creation is now denied. */ 3167 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD, 3168 dir_file2_s2d3, RENAME_EXCHANGE)); 3169 ASSERT_EQ(EACCES, errno); 3170 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, 3171 dir_file1_s1d2, RENAME_EXCHANGE)); 3172 ASSERT_EQ(EACCES, errno); 3173 3174 /* Checks with different (child-only) access rights. */ 3175 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, dir_s2d3, 3176 RENAME_EXCHANGE)); 3177 /* Denied because of MAKE_DIR. */ 3178 ASSERT_EQ(EACCES, errno); 3179 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3, 3180 RENAME_EXCHANGE)); 3181 ASSERT_EQ(EACCES, errno); 3182 3183 /* Checks with different (child-only) access rights. */ 3184 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_file1_s1d2, 3185 RENAME_EXCHANGE)); 3186 /* Denied because of MAKE_DIR. */ 3187 ASSERT_EQ(EACCES, errno); 3188 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD, dir_s2d3, 3189 RENAME_EXCHANGE)); 3190 ASSERT_EQ(EACCES, errno); 3191 3192 /* See layout1.reparent_exdev_layers_exchange2 for complement. */ 3193 } 3194 3195 TEST_F_FORK(layout1, reparent_exdev_layers_exchange2) 3196 { 3197 const char *const dir_file2_s2d3 = file2_s2d3; 3198 3199 ASSERT_EQ(0, unlink(file2_s2d3)); 3200 ASSERT_EQ(0, mkdir(file2_s2d3, 0700)); 3201 3202 reparent_exdev_layers_enforce1(_metadata); 3203 reparent_exdev_layers_enforce2(_metadata); 3204 3205 /* Checks that exchange between file and directory are consistent. */ 3206 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3, 3207 RENAME_EXCHANGE)); 3208 ASSERT_EQ(EACCES, errno); 3209 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2, 3210 RENAME_EXCHANGE)); 3211 ASSERT_EQ(EACCES, errno); 3212 } 3213 3214 TEST_F_FORK(layout1, reparent_exdev_layers_exchange3) 3215 { 3216 const char *const dir_file2_s2d3 = file2_s2d3; 3217 3218 ASSERT_EQ(0, unlink(file2_s2d3)); 3219 ASSERT_EQ(0, mkdir(file2_s2d3, 0700)); 3220 3221 reparent_exdev_layers_enforce1(_metadata); 3222 3223 /* 3224 * Checks that exchange between file and directory are consistent, 3225 * including with inverted arguments (see 3226 * layout1.reparent_exdev_layers_exchange1). 3227 */ 3228 ASSERT_EQ(0, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2, 3229 RENAME_EXCHANGE)); 3230 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3, 3231 RENAME_EXCHANGE)); 3232 ASSERT_EQ(EACCES, errno); 3233 ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2, 3234 RENAME_EXCHANGE)); 3235 ASSERT_EQ(EACCES, errno); 3236 } 3237 3238 TEST_F_FORK(layout1, reparent_remove) 3239 { 3240 const struct rule layer1[] = { 3241 { 3242 .path = dir_s1d1, 3243 .access = LANDLOCK_ACCESS_FS_REFER | 3244 LANDLOCK_ACCESS_FS_REMOVE_DIR, 3245 }, 3246 { 3247 .path = dir_s1d2, 3248 .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 3249 }, 3250 { 3251 .path = dir_s2d1, 3252 .access = LANDLOCK_ACCESS_FS_REFER | 3253 LANDLOCK_ACCESS_FS_REMOVE_FILE, 3254 }, 3255 {}, 3256 }; 3257 3258 enforce_fs(_metadata, 3259 LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_REMOVE_DIR | 3260 LANDLOCK_ACCESS_FS_REMOVE_FILE, 3261 layer1); 3262 3263 /* Access denied because of wrong/swapped remove file/dir. */ 3264 ASSERT_EQ(-1, rename(file1_s1d1, dir_s2d2)); 3265 ASSERT_EQ(EACCES, errno); 3266 ASSERT_EQ(-1, rename(dir_s2d2, file1_s1d1)); 3267 ASSERT_EQ(EACCES, errno); 3268 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s2d2, 3269 RENAME_EXCHANGE)); 3270 ASSERT_EQ(EACCES, errno); 3271 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s2d3, 3272 RENAME_EXCHANGE)); 3273 ASSERT_EQ(EACCES, errno); 3274 3275 /* Access allowed thanks to the matching rights. */ 3276 ASSERT_EQ(-1, rename(file1_s2d1, dir_s1d2)); 3277 ASSERT_EQ(EISDIR, errno); 3278 ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d1)); 3279 ASSERT_EQ(ENOTDIR, errno); 3280 ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d1)); 3281 ASSERT_EQ(ENOTDIR, errno); 3282 ASSERT_EQ(0, unlink(file1_s2d1)); 3283 ASSERT_EQ(0, unlink(file1_s1d3)); 3284 ASSERT_EQ(0, unlink(file2_s1d3)); 3285 ASSERT_EQ(0, rename(dir_s1d3, file1_s2d1)); 3286 3287 /* Effectively removes a file and a directory by exchanging them. */ 3288 ASSERT_EQ(0, mkdir(dir_s1d3, 0700)); 3289 ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3, 3290 RENAME_EXCHANGE)); 3291 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3, 3292 RENAME_EXCHANGE)); 3293 ASSERT_EQ(EACCES, errno); 3294 } 3295 3296 TEST_F_FORK(layout1, reparent_dom_superset) 3297 { 3298 const struct rule layer1[] = { 3299 { 3300 .path = dir_s1d2, 3301 .access = LANDLOCK_ACCESS_FS_REFER, 3302 }, 3303 { 3304 .path = file1_s1d2, 3305 .access = LANDLOCK_ACCESS_FS_EXECUTE, 3306 }, 3307 { 3308 .path = dir_s1d3, 3309 .access = LANDLOCK_ACCESS_FS_MAKE_SOCK | 3310 LANDLOCK_ACCESS_FS_EXECUTE, 3311 }, 3312 { 3313 .path = dir_s2d2, 3314 .access = LANDLOCK_ACCESS_FS_REFER | 3315 LANDLOCK_ACCESS_FS_EXECUTE | 3316 LANDLOCK_ACCESS_FS_MAKE_SOCK, 3317 }, 3318 { 3319 .path = dir_s2d3, 3320 .access = LANDLOCK_ACCESS_FS_READ_FILE | 3321 LANDLOCK_ACCESS_FS_MAKE_FIFO, 3322 }, 3323 {}, 3324 }; 3325 3326 enforce_fs(_metadata, 3327 LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_EXECUTE | 3328 LANDLOCK_ACCESS_FS_MAKE_SOCK | 3329 LANDLOCK_ACCESS_FS_READ_FILE | 3330 LANDLOCK_ACCESS_FS_MAKE_FIFO, 3331 layer1); 3332 3333 ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d1)); 3334 ASSERT_EQ(EXDEV, errno); 3335 /* 3336 * Moving file1_s1d2 beneath dir_s2d3 would grant it the READ_FILE 3337 * access right. 3338 */ 3339 ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d3)); 3340 ASSERT_EQ(EXDEV, errno); 3341 /* 3342 * Moving file1_s1d2 should be allowed even if dir_s2d2 grants a 3343 * superset of access rights compared to dir_s1d2, because file1_s1d2 3344 * already has these access rights anyway. 3345 */ 3346 ASSERT_EQ(0, rename(file1_s1d2, file1_s2d2)); 3347 ASSERT_EQ(0, rename(file1_s2d2, file1_s1d2)); 3348 3349 ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d1)); 3350 ASSERT_EQ(EXDEV, errno); 3351 /* 3352 * Moving dir_s1d3 beneath dir_s2d3 would grant it the MAKE_FIFO access 3353 * right. 3354 */ 3355 ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d3)); 3356 ASSERT_EQ(EXDEV, errno); 3357 /* 3358 * Moving dir_s1d3 should be allowed even if dir_s2d2 grants a superset 3359 * of access rights compared to dir_s1d2, because dir_s1d3 already has 3360 * these access rights anyway. 3361 */ 3362 ASSERT_EQ(0, rename(dir_s1d3, file1_s2d2)); 3363 ASSERT_EQ(0, rename(file1_s2d2, dir_s1d3)); 3364 3365 /* 3366 * Moving file1_s2d3 beneath dir_s1d2 is allowed, but moving it back 3367 * will be denied because the new inherited access rights from dir_s1d2 3368 * will be less than the destination (original) dir_s2d3. This is a 3369 * sinkhole scenario where we cannot move back files or directories. 3370 */ 3371 ASSERT_EQ(0, rename(file1_s2d3, file2_s1d2)); 3372 ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d3)); 3373 ASSERT_EQ(EXDEV, errno); 3374 ASSERT_EQ(0, unlink(file2_s1d2)); 3375 ASSERT_EQ(0, unlink(file2_s2d3)); 3376 /* 3377 * Checks similar directory one-way move: dir_s2d3 loses EXECUTE and 3378 * MAKE_SOCK which were inherited from dir_s1d3. 3379 */ 3380 ASSERT_EQ(0, rename(dir_s2d3, file2_s1d2)); 3381 ASSERT_EQ(-1, rename(file2_s1d2, dir_s2d3)); 3382 ASSERT_EQ(EXDEV, errno); 3383 } 3384 3385 TEST_F_FORK(layout1, remove_dir) 3386 { 3387 const struct rule rules[] = { 3388 { 3389 .path = dir_s1d2, 3390 .access = LANDLOCK_ACCESS_FS_REMOVE_DIR, 3391 }, 3392 {}, 3393 }; 3394 3395 ASSERT_EQ(0, unlink(file1_s1d1)); 3396 ASSERT_EQ(0, unlink(file1_s1d2)); 3397 ASSERT_EQ(0, unlink(file1_s1d3)); 3398 ASSERT_EQ(0, unlink(file2_s1d3)); 3399 3400 enforce_fs(_metadata, rules[0].access, rules); 3401 3402 ASSERT_EQ(0, rmdir(dir_s1d3)); 3403 ASSERT_EQ(0, mkdir(dir_s1d3, 0700)); 3404 ASSERT_EQ(0, unlinkat(AT_FDCWD, dir_s1d3, AT_REMOVEDIR)); 3405 3406 /* dir_s1d2 itself cannot be removed. */ 3407 ASSERT_EQ(-1, rmdir(dir_s1d2)); 3408 ASSERT_EQ(EACCES, errno); 3409 ASSERT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d2, AT_REMOVEDIR)); 3410 ASSERT_EQ(EACCES, errno); 3411 ASSERT_EQ(-1, rmdir(dir_s1d1)); 3412 ASSERT_EQ(EACCES, errno); 3413 ASSERT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d1, AT_REMOVEDIR)); 3414 ASSERT_EQ(EACCES, errno); 3415 } 3416 3417 TEST_F_FORK(layout1, remove_file) 3418 { 3419 const struct rule rules[] = { 3420 { 3421 .path = dir_s1d2, 3422 .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 3423 }, 3424 {}, 3425 }; 3426 3427 enforce_fs(_metadata, rules[0].access, rules); 3428 3429 ASSERT_EQ(-1, unlink(file1_s1d1)); 3430 ASSERT_EQ(EACCES, errno); 3431 ASSERT_EQ(-1, unlinkat(AT_FDCWD, file1_s1d1, 0)); 3432 ASSERT_EQ(EACCES, errno); 3433 ASSERT_EQ(0, unlink(file1_s1d2)); 3434 ASSERT_EQ(0, unlinkat(AT_FDCWD, file1_s1d3, 0)); 3435 } 3436 3437 static void test_make_file(struct __test_metadata *const _metadata, 3438 const __u64 access, const mode_t mode, 3439 const dev_t dev) 3440 { 3441 const struct rule rules[] = { 3442 { 3443 .path = dir_s1d2, 3444 .access = access, 3445 }, 3446 {}, 3447 }; 3448 3449 ASSERT_EQ(0, unlink(file1_s1d1)); 3450 ASSERT_EQ(0, unlink(file2_s1d1)); 3451 ASSERT_EQ(0, mknod(file2_s1d1, mode | 0400, dev)) 3452 { 3453 TH_LOG("Failed to make file \"%s\": %s", file2_s1d1, 3454 strerror(errno)); 3455 }; 3456 3457 ASSERT_EQ(0, unlink(file1_s1d2)); 3458 ASSERT_EQ(0, unlink(file2_s1d2)); 3459 3460 ASSERT_EQ(0, unlink(file1_s1d3)); 3461 ASSERT_EQ(0, unlink(file2_s1d3)); 3462 3463 enforce_fs(_metadata, access, rules); 3464 3465 ASSERT_EQ(-1, mknod(file1_s1d1, mode | 0400, dev)); 3466 ASSERT_EQ(EACCES, errno); 3467 ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1)); 3468 ASSERT_EQ(EACCES, errno); 3469 ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1)); 3470 ASSERT_EQ(EACCES, errno); 3471 3472 ASSERT_EQ(0, mknod(file1_s1d2, mode | 0400, dev)) 3473 { 3474 TH_LOG("Failed to make file \"%s\": %s", file1_s1d2, 3475 strerror(errno)); 3476 }; 3477 ASSERT_EQ(0, link(file1_s1d2, file2_s1d2)); 3478 ASSERT_EQ(0, unlink(file2_s1d2)); 3479 ASSERT_EQ(0, rename(file1_s1d2, file2_s1d2)); 3480 3481 ASSERT_EQ(0, mknod(file1_s1d3, mode | 0400, dev)); 3482 ASSERT_EQ(0, link(file1_s1d3, file2_s1d3)); 3483 ASSERT_EQ(0, unlink(file2_s1d3)); 3484 ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3)); 3485 } 3486 3487 TEST_F_FORK(layout1, make_char) 3488 { 3489 /* Creates a /dev/null device. */ 3490 set_cap(_metadata, CAP_MKNOD); 3491 test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_CHAR, S_IFCHR, 3492 makedev(1, 3)); 3493 } 3494 3495 TEST_F_FORK(layout1, make_whiteout) 3496 { 3497 /* 3498 * Creates a whiteout object (creation guarded by MAKE_REG). 3499 * 3500 * Contrary to the other character devices, this does not require 3501 * CAP_MKNOD, cf. vfs_mknod(). 3502 */ 3503 test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, S_IFCHR, 3504 makedev(0, 0)); 3505 } 3506 3507 TEST_F_FORK(layout1, make_block) 3508 { 3509 /* Creates a /dev/loop0 device. */ 3510 set_cap(_metadata, CAP_MKNOD); 3511 test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_BLOCK, S_IFBLK, 3512 makedev(7, 0)); 3513 } 3514 3515 TEST_F_FORK(layout1, make_reg_1) 3516 { 3517 test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, S_IFREG, 0); 3518 } 3519 3520 TEST_F_FORK(layout1, make_reg_2) 3521 { 3522 test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, 0, 0); 3523 } 3524 3525 TEST_F_FORK(layout1, make_sock) 3526 { 3527 test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_SOCK, S_IFSOCK, 0); 3528 } 3529 3530 TEST_F_FORK(layout1, make_fifo) 3531 { 3532 test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_FIFO, S_IFIFO, 0); 3533 } 3534 3535 TEST_F_FORK(layout1, make_sym) 3536 { 3537 const struct rule rules[] = { 3538 { 3539 .path = dir_s1d2, 3540 .access = LANDLOCK_ACCESS_FS_MAKE_SYM, 3541 }, 3542 {}, 3543 }; 3544 3545 ASSERT_EQ(0, unlink(file1_s1d1)); 3546 ASSERT_EQ(0, unlink(file2_s1d1)); 3547 ASSERT_EQ(0, symlink("none", file2_s1d1)); 3548 3549 ASSERT_EQ(0, unlink(file1_s1d2)); 3550 ASSERT_EQ(0, unlink(file2_s1d2)); 3551 3552 ASSERT_EQ(0, unlink(file1_s1d3)); 3553 ASSERT_EQ(0, unlink(file2_s1d3)); 3554 3555 enforce_fs(_metadata, rules[0].access, rules); 3556 3557 ASSERT_EQ(-1, symlink("none", file1_s1d1)); 3558 ASSERT_EQ(EACCES, errno); 3559 ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1)); 3560 ASSERT_EQ(EACCES, errno); 3561 ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1)); 3562 ASSERT_EQ(EACCES, errno); 3563 3564 ASSERT_EQ(0, symlink("none", file1_s1d2)); 3565 ASSERT_EQ(0, link(file1_s1d2, file2_s1d2)); 3566 ASSERT_EQ(0, unlink(file2_s1d2)); 3567 ASSERT_EQ(0, rename(file1_s1d2, file2_s1d2)); 3568 3569 ASSERT_EQ(0, symlink("none", file1_s1d3)); 3570 ASSERT_EQ(0, link(file1_s1d3, file2_s1d3)); 3571 ASSERT_EQ(0, unlink(file2_s1d3)); 3572 ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3)); 3573 } 3574 3575 TEST_F_FORK(layout1, make_dir) 3576 { 3577 const struct rule rules[] = { 3578 { 3579 .path = dir_s1d2, 3580 .access = LANDLOCK_ACCESS_FS_MAKE_DIR, 3581 }, 3582 {}, 3583 }; 3584 3585 ASSERT_EQ(0, unlink(file1_s1d1)); 3586 ASSERT_EQ(0, unlink(file1_s1d2)); 3587 ASSERT_EQ(0, unlink(file1_s1d3)); 3588 3589 enforce_fs(_metadata, rules[0].access, rules); 3590 3591 /* Uses file_* as directory names. */ 3592 ASSERT_EQ(-1, mkdir(file1_s1d1, 0700)); 3593 ASSERT_EQ(EACCES, errno); 3594 ASSERT_EQ(0, mkdir(file1_s1d2, 0700)); 3595 ASSERT_EQ(0, mkdir(file1_s1d3, 0700)); 3596 } 3597 3598 static int open_proc_fd(struct __test_metadata *const _metadata, const int fd, 3599 const int open_flags) 3600 { 3601 static const char path_template[] = "/proc/self/fd/%d"; 3602 char procfd_path[sizeof(path_template) + 10]; 3603 const int procfd_path_size = 3604 snprintf(procfd_path, sizeof(procfd_path), path_template, fd); 3605 3606 ASSERT_LT(procfd_path_size, sizeof(procfd_path)); 3607 return open(procfd_path, open_flags); 3608 } 3609 3610 TEST_F_FORK(layout1, proc_unlinked_file) 3611 { 3612 const struct rule rules[] = { 3613 { 3614 .path = file1_s1d2, 3615 .access = LANDLOCK_ACCESS_FS_READ_FILE, 3616 }, 3617 {}, 3618 }; 3619 int reg_fd, proc_fd; 3620 3621 enforce_fs(_metadata, 3622 LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE, 3623 rules); 3624 3625 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR)); 3626 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 3627 reg_fd = open(file1_s1d2, O_RDONLY | O_CLOEXEC); 3628 ASSERT_LE(0, reg_fd); 3629 ASSERT_EQ(0, unlink(file1_s1d2)); 3630 3631 proc_fd = open_proc_fd(_metadata, reg_fd, O_RDONLY | O_CLOEXEC); 3632 ASSERT_LE(0, proc_fd); 3633 ASSERT_EQ(0, close(proc_fd)); 3634 3635 proc_fd = open_proc_fd(_metadata, reg_fd, O_RDWR | O_CLOEXEC); 3636 ASSERT_EQ(-1, proc_fd) 3637 { 3638 TH_LOG("Successfully opened /proc/self/fd/%d: %s", reg_fd, 3639 strerror(errno)); 3640 } 3641 ASSERT_EQ(EACCES, errno); 3642 3643 ASSERT_EQ(0, close(reg_fd)); 3644 } 3645 3646 TEST_F_FORK(layout1, proc_pipe) 3647 { 3648 int proc_fd; 3649 int pipe_fds[2]; 3650 char buf = '\0'; 3651 const struct rule rules[] = { 3652 { 3653 .path = dir_s1d2, 3654 .access = LANDLOCK_ACCESS_FS_READ_FILE | 3655 LANDLOCK_ACCESS_FS_WRITE_FILE, 3656 }, 3657 {}, 3658 }; 3659 3660 /* Limits read and write access to files tied to the filesystem. */ 3661 enforce_fs(_metadata, rules[0].access, rules); 3662 3663 /* Checks enforcement for normal files. */ 3664 ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR)); 3665 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 3666 3667 /* Checks access to pipes through FD. */ 3668 ASSERT_EQ(0, pipe2(pipe_fds, O_CLOEXEC)); 3669 ASSERT_EQ(1, write(pipe_fds[1], ".", 1)) 3670 { 3671 TH_LOG("Failed to write in pipe: %s", strerror(errno)); 3672 } 3673 ASSERT_EQ(1, read(pipe_fds[0], &buf, 1)); 3674 ASSERT_EQ('.', buf); 3675 3676 /* Checks write access to pipe through /proc/self/fd . */ 3677 proc_fd = open_proc_fd(_metadata, pipe_fds[1], O_WRONLY | O_CLOEXEC); 3678 ASSERT_LE(0, proc_fd); 3679 ASSERT_EQ(1, write(proc_fd, ".", 1)) 3680 { 3681 TH_LOG("Failed to write through /proc/self/fd/%d: %s", 3682 pipe_fds[1], strerror(errno)); 3683 } 3684 ASSERT_EQ(0, close(proc_fd)); 3685 3686 /* Checks read access to pipe through /proc/self/fd . */ 3687 proc_fd = open_proc_fd(_metadata, pipe_fds[0], O_RDONLY | O_CLOEXEC); 3688 ASSERT_LE(0, proc_fd); 3689 buf = '\0'; 3690 ASSERT_EQ(1, read(proc_fd, &buf, 1)) 3691 { 3692 TH_LOG("Failed to read through /proc/self/fd/%d: %s", 3693 pipe_fds[1], strerror(errno)); 3694 } 3695 ASSERT_EQ(0, close(proc_fd)); 3696 3697 ASSERT_EQ(0, close(pipe_fds[0])); 3698 ASSERT_EQ(0, close(pipe_fds[1])); 3699 } 3700 3701 /* Invokes truncate(2) and returns its errno or 0. */ 3702 static int test_truncate(const char *const path) 3703 { 3704 if (truncate(path, 10) < 0) 3705 return errno; 3706 return 0; 3707 } 3708 3709 /* 3710 * Invokes creat(2) and returns its errno or 0. 3711 * Closes the opened file descriptor on success. 3712 */ 3713 static int test_creat(const char *const path) 3714 { 3715 int fd = creat(path, 0600); 3716 3717 if (fd < 0) 3718 return errno; 3719 3720 /* 3721 * Mixing error codes from close(2) and creat(2) should not lead to any 3722 * (access type) confusion for this test. 3723 */ 3724 if (close(fd) < 0) 3725 return errno; 3726 return 0; 3727 } 3728 3729 /* 3730 * Exercises file truncation when it's not restricted, 3731 * as it was the case before LANDLOCK_ACCESS_FS_TRUNCATE existed. 3732 */ 3733 TEST_F_FORK(layout1, truncate_unhandled) 3734 { 3735 const char *const file_r = file1_s1d1; 3736 const char *const file_w = file2_s1d1; 3737 const char *const file_none = file1_s1d2; 3738 const struct rule rules[] = { 3739 { 3740 .path = file_r, 3741 .access = LANDLOCK_ACCESS_FS_READ_FILE, 3742 }, 3743 { 3744 .path = file_w, 3745 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 3746 }, 3747 /* Implicitly: No rights for file_none. */ 3748 {}, 3749 }; 3750 3751 /* Enables Landlock. */ 3752 enforce_fs(_metadata, 3753 LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE, 3754 rules); 3755 3756 /* 3757 * Checks read right: truncate and open with O_TRUNC work, unless the 3758 * file is attempted to be opened for writing. 3759 */ 3760 EXPECT_EQ(0, test_truncate(file_r)); 3761 EXPECT_EQ(0, test_open(file_r, O_RDONLY | O_TRUNC)); 3762 EXPECT_EQ(EACCES, test_open(file_r, O_WRONLY | O_TRUNC)); 3763 EXPECT_EQ(EACCES, test_creat(file_r)); 3764 3765 /* 3766 * Checks write right: truncate and open with O_TRUNC work, unless the 3767 * file is attempted to be opened for reading. 3768 */ 3769 EXPECT_EQ(0, test_truncate(file_w)); 3770 EXPECT_EQ(EACCES, test_open(file_w, O_RDONLY | O_TRUNC)); 3771 EXPECT_EQ(0, test_open(file_w, O_WRONLY | O_TRUNC)); 3772 EXPECT_EQ(0, test_creat(file_w)); 3773 3774 /* 3775 * Checks "no rights" case: truncate works but all open attempts fail, 3776 * including creat. 3777 */ 3778 EXPECT_EQ(0, test_truncate(file_none)); 3779 EXPECT_EQ(EACCES, test_open(file_none, O_RDONLY | O_TRUNC)); 3780 EXPECT_EQ(EACCES, test_open(file_none, O_WRONLY | O_TRUNC)); 3781 EXPECT_EQ(EACCES, test_creat(file_none)); 3782 } 3783 3784 TEST_F_FORK(layout1, truncate) 3785 { 3786 const char *const file_rwt = file1_s1d1; 3787 const char *const file_rw = file2_s1d1; 3788 const char *const file_rt = file1_s1d2; 3789 const char *const file_t = file2_s1d2; 3790 const char *const file_none = file1_s1d3; 3791 const char *const dir_t = dir_s2d1; 3792 const char *const file_in_dir_t = file1_s2d1; 3793 const char *const dir_w = dir_s3d1; 3794 const char *const file_in_dir_w = file1_s3d1; 3795 const struct rule rules[] = { 3796 { 3797 .path = file_rwt, 3798 .access = LANDLOCK_ACCESS_FS_READ_FILE | 3799 LANDLOCK_ACCESS_FS_WRITE_FILE | 3800 LANDLOCK_ACCESS_FS_TRUNCATE, 3801 }, 3802 { 3803 .path = file_rw, 3804 .access = LANDLOCK_ACCESS_FS_READ_FILE | 3805 LANDLOCK_ACCESS_FS_WRITE_FILE, 3806 }, 3807 { 3808 .path = file_rt, 3809 .access = LANDLOCK_ACCESS_FS_READ_FILE | 3810 LANDLOCK_ACCESS_FS_TRUNCATE, 3811 }, 3812 { 3813 .path = file_t, 3814 .access = LANDLOCK_ACCESS_FS_TRUNCATE, 3815 }, 3816 /* Implicitly: No access rights for file_none. */ 3817 { 3818 .path = dir_t, 3819 .access = LANDLOCK_ACCESS_FS_TRUNCATE, 3820 }, 3821 { 3822 .path = dir_w, 3823 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 3824 }, 3825 {}, 3826 }; 3827 3828 /* Enables Landlock. */ 3829 enforce_fs(_metadata, 3830 LANDLOCK_ACCESS_FS_READ_FILE | 3831 LANDLOCK_ACCESS_FS_WRITE_FILE | 3832 LANDLOCK_ACCESS_FS_TRUNCATE, 3833 rules); 3834 3835 /* Checks read, write and truncate rights: truncation works. */ 3836 EXPECT_EQ(0, test_truncate(file_rwt)); 3837 EXPECT_EQ(0, test_open(file_rwt, O_RDONLY | O_TRUNC)); 3838 EXPECT_EQ(0, test_open(file_rwt, O_WRONLY | O_TRUNC)); 3839 3840 /* Checks read and write rights: no truncate variant works. */ 3841 EXPECT_EQ(EACCES, test_truncate(file_rw)); 3842 EXPECT_EQ(EACCES, test_open(file_rw, O_RDONLY | O_TRUNC)); 3843 EXPECT_EQ(EACCES, test_open(file_rw, O_WRONLY | O_TRUNC)); 3844 3845 /* 3846 * Checks read and truncate rights: truncation works. 3847 * 3848 * Note: Files can get truncated using open() even with O_RDONLY. 3849 */ 3850 EXPECT_EQ(0, test_truncate(file_rt)); 3851 EXPECT_EQ(0, test_open(file_rt, O_RDONLY | O_TRUNC)); 3852 EXPECT_EQ(EACCES, test_open(file_rt, O_WRONLY | O_TRUNC)); 3853 3854 /* Checks truncate right: truncate works, but can't open file. */ 3855 EXPECT_EQ(0, test_truncate(file_t)); 3856 EXPECT_EQ(EACCES, test_open(file_t, O_RDONLY | O_TRUNC)); 3857 EXPECT_EQ(EACCES, test_open(file_t, O_WRONLY | O_TRUNC)); 3858 3859 /* Checks "no rights" case: No form of truncation works. */ 3860 EXPECT_EQ(EACCES, test_truncate(file_none)); 3861 EXPECT_EQ(EACCES, test_open(file_none, O_RDONLY | O_TRUNC)); 3862 EXPECT_EQ(EACCES, test_open(file_none, O_WRONLY | O_TRUNC)); 3863 3864 /* 3865 * Checks truncate right on directory: truncate works on contained 3866 * files. 3867 */ 3868 EXPECT_EQ(0, test_truncate(file_in_dir_t)); 3869 EXPECT_EQ(EACCES, test_open(file_in_dir_t, O_RDONLY | O_TRUNC)); 3870 EXPECT_EQ(EACCES, test_open(file_in_dir_t, O_WRONLY | O_TRUNC)); 3871 3872 /* 3873 * Checks creat in dir_w: This requires the truncate right when 3874 * overwriting an existing file, but does not require it when the file 3875 * is new. 3876 */ 3877 EXPECT_EQ(EACCES, test_creat(file_in_dir_w)); 3878 3879 ASSERT_EQ(0, unlink(file_in_dir_w)); 3880 EXPECT_EQ(0, test_creat(file_in_dir_w)); 3881 } 3882 3883 /* Invokes ftruncate(2) and returns its errno or 0. */ 3884 static int test_ftruncate(int fd) 3885 { 3886 if (ftruncate(fd, 10) < 0) 3887 return errno; 3888 return 0; 3889 } 3890 3891 TEST_F_FORK(layout1, ftruncate) 3892 { 3893 /* 3894 * This test opens a new file descriptor at different stages of 3895 * Landlock restriction: 3896 * 3897 * without restriction: ftruncate works 3898 * something else but truncate restricted: ftruncate works 3899 * truncate restricted and permitted: ftruncate works 3900 * truncate restricted and not permitted: ftruncate fails 3901 * 3902 * Whether this works or not is expected to depend on the time when the 3903 * FD was opened, not to depend on the time when ftruncate() was 3904 * called. 3905 */ 3906 const char *const path = file1_s1d1; 3907 const __u64 handled1 = LANDLOCK_ACCESS_FS_READ_FILE | 3908 LANDLOCK_ACCESS_FS_WRITE_FILE; 3909 const struct rule layer1[] = { 3910 { 3911 .path = path, 3912 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 3913 }, 3914 {}, 3915 }; 3916 const __u64 handled2 = LANDLOCK_ACCESS_FS_TRUNCATE; 3917 const struct rule layer2[] = { 3918 { 3919 .path = path, 3920 .access = LANDLOCK_ACCESS_FS_TRUNCATE, 3921 }, 3922 {}, 3923 }; 3924 const __u64 handled3 = LANDLOCK_ACCESS_FS_TRUNCATE | 3925 LANDLOCK_ACCESS_FS_WRITE_FILE; 3926 const struct rule layer3[] = { 3927 { 3928 .path = path, 3929 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 3930 }, 3931 {}, 3932 }; 3933 int fd_layer0, fd_layer1, fd_layer2, fd_layer3; 3934 3935 fd_layer0 = open(path, O_WRONLY); 3936 EXPECT_EQ(0, test_ftruncate(fd_layer0)); 3937 3938 enforce_fs(_metadata, handled1, layer1); 3939 3940 fd_layer1 = open(path, O_WRONLY); 3941 EXPECT_EQ(0, test_ftruncate(fd_layer0)); 3942 EXPECT_EQ(0, test_ftruncate(fd_layer1)); 3943 3944 enforce_fs(_metadata, handled2, layer2); 3945 3946 fd_layer2 = open(path, O_WRONLY); 3947 EXPECT_EQ(0, test_ftruncate(fd_layer0)); 3948 EXPECT_EQ(0, test_ftruncate(fd_layer1)); 3949 EXPECT_EQ(0, test_ftruncate(fd_layer2)); 3950 3951 enforce_fs(_metadata, handled3, layer3); 3952 3953 fd_layer3 = open(path, O_WRONLY); 3954 EXPECT_EQ(0, test_ftruncate(fd_layer0)); 3955 EXPECT_EQ(0, test_ftruncate(fd_layer1)); 3956 EXPECT_EQ(0, test_ftruncate(fd_layer2)); 3957 EXPECT_EQ(EACCES, test_ftruncate(fd_layer3)); 3958 3959 ASSERT_EQ(0, close(fd_layer0)); 3960 ASSERT_EQ(0, close(fd_layer1)); 3961 ASSERT_EQ(0, close(fd_layer2)); 3962 ASSERT_EQ(0, close(fd_layer3)); 3963 } 3964 3965 /* clang-format off */ 3966 FIXTURE(ftruncate) {}; 3967 /* clang-format on */ 3968 3969 FIXTURE_SETUP(ftruncate) 3970 { 3971 prepare_layout(_metadata); 3972 create_file(_metadata, file1_s1d1); 3973 } 3974 3975 FIXTURE_TEARDOWN_PARENT(ftruncate) 3976 { 3977 EXPECT_EQ(0, remove_path(file1_s1d1)); 3978 cleanup_layout(_metadata); 3979 } 3980 3981 FIXTURE_VARIANT(ftruncate) 3982 { 3983 const __u64 handled; 3984 const __u64 allowed; 3985 const int expected_open_result; 3986 const int expected_ftruncate_result; 3987 }; 3988 3989 /* clang-format off */ 3990 FIXTURE_VARIANT_ADD(ftruncate, w_w) { 3991 /* clang-format on */ 3992 .handled = LANDLOCK_ACCESS_FS_WRITE_FILE, 3993 .allowed = LANDLOCK_ACCESS_FS_WRITE_FILE, 3994 .expected_open_result = 0, 3995 .expected_ftruncate_result = 0, 3996 }; 3997 3998 /* clang-format off */ 3999 FIXTURE_VARIANT_ADD(ftruncate, t_t) { 4000 /* clang-format on */ 4001 .handled = LANDLOCK_ACCESS_FS_TRUNCATE, 4002 .allowed = LANDLOCK_ACCESS_FS_TRUNCATE, 4003 .expected_open_result = 0, 4004 .expected_ftruncate_result = 0, 4005 }; 4006 4007 /* clang-format off */ 4008 FIXTURE_VARIANT_ADD(ftruncate, wt_w) { 4009 /* clang-format on */ 4010 .handled = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_TRUNCATE, 4011 .allowed = LANDLOCK_ACCESS_FS_WRITE_FILE, 4012 .expected_open_result = 0, 4013 .expected_ftruncate_result = EACCES, 4014 }; 4015 4016 /* clang-format off */ 4017 FIXTURE_VARIANT_ADD(ftruncate, wt_wt) { 4018 /* clang-format on */ 4019 .handled = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_TRUNCATE, 4020 .allowed = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_TRUNCATE, 4021 .expected_open_result = 0, 4022 .expected_ftruncate_result = 0, 4023 }; 4024 4025 /* clang-format off */ 4026 FIXTURE_VARIANT_ADD(ftruncate, wt_t) { 4027 /* clang-format on */ 4028 .handled = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_TRUNCATE, 4029 .allowed = LANDLOCK_ACCESS_FS_TRUNCATE, 4030 .expected_open_result = EACCES, 4031 }; 4032 4033 TEST_F_FORK(ftruncate, open_and_ftruncate) 4034 { 4035 const char *const path = file1_s1d1; 4036 const struct rule rules[] = { 4037 { 4038 .path = path, 4039 .access = variant->allowed, 4040 }, 4041 {}, 4042 }; 4043 int fd; 4044 4045 /* Enables Landlock. */ 4046 enforce_fs(_metadata, variant->handled, rules); 4047 4048 fd = open(path, O_WRONLY); 4049 EXPECT_EQ(variant->expected_open_result, (fd < 0 ? errno : 0)); 4050 if (fd >= 0) { 4051 EXPECT_EQ(variant->expected_ftruncate_result, 4052 test_ftruncate(fd)); 4053 ASSERT_EQ(0, close(fd)); 4054 } 4055 } 4056 4057 TEST_F_FORK(ftruncate, open_and_ftruncate_in_different_processes) 4058 { 4059 int child, fd, status; 4060 int socket_fds[2]; 4061 4062 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, 4063 socket_fds)); 4064 4065 child = fork(); 4066 ASSERT_LE(0, child); 4067 if (child == 0) { 4068 /* 4069 * Enables Landlock in the child process, open a file descriptor 4070 * where truncation is forbidden and send it to the 4071 * non-landlocked parent process. 4072 */ 4073 const char *const path = file1_s1d1; 4074 const struct rule rules[] = { 4075 { 4076 .path = path, 4077 .access = variant->allowed, 4078 }, 4079 {}, 4080 }; 4081 int fd; 4082 4083 enforce_fs(_metadata, variant->handled, rules); 4084 4085 fd = open(path, O_WRONLY); 4086 ASSERT_EQ(variant->expected_open_result, (fd < 0 ? errno : 0)); 4087 4088 if (fd >= 0) { 4089 ASSERT_EQ(0, send_fd(socket_fds[0], fd)); 4090 ASSERT_EQ(0, close(fd)); 4091 } 4092 4093 ASSERT_EQ(0, close(socket_fds[0])); 4094 4095 _exit(_metadata->exit_code); 4096 return; 4097 } 4098 4099 if (variant->expected_open_result == 0) { 4100 fd = recv_fd(socket_fds[1]); 4101 ASSERT_LE(0, fd); 4102 4103 EXPECT_EQ(variant->expected_ftruncate_result, 4104 test_ftruncate(fd)); 4105 ASSERT_EQ(0, close(fd)); 4106 } 4107 4108 ASSERT_EQ(child, waitpid(child, &status, 0)); 4109 ASSERT_EQ(1, WIFEXITED(status)); 4110 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 4111 4112 ASSERT_EQ(0, close(socket_fds[0])); 4113 ASSERT_EQ(0, close(socket_fds[1])); 4114 } 4115 4116 /* Invokes the FS_IOC_GETFLAGS IOCTL and returns its errno or 0. */ 4117 static int test_fs_ioc_getflags_ioctl(int fd) 4118 { 4119 uint32_t flags; 4120 4121 if (ioctl(fd, FS_IOC_GETFLAGS, &flags) < 0) 4122 return errno; 4123 return 0; 4124 } 4125 4126 TEST(memfd_ftruncate_and_ioctl) 4127 { 4128 int fd, i; 4129 4130 /* 4131 * We exercise the same test both with and without Landlock enabled, to 4132 * ensure that it behaves the same in both cases. 4133 */ 4134 for (i = 0; i < 2; i++) { 4135 /* Creates a new memfd. */ 4136 fd = memfd_create("name", MFD_CLOEXEC); 4137 ASSERT_LE(0, fd); 4138 4139 /* 4140 * Checks that operations associated with the opened file 4141 * (ftruncate, ioctl) are permitted on file descriptors that are 4142 * created in ways other than open(2). 4143 */ 4144 EXPECT_EQ(0, test_ftruncate(fd)); 4145 EXPECT_EQ(0, test_fs_ioc_getflags_ioctl(fd)); 4146 4147 ASSERT_EQ(0, close(fd)); 4148 4149 /* Enables Landlock. */ 4150 enforce_fs(_metadata, ACCESS_ALL, NULL); 4151 } 4152 } 4153 4154 static int test_fionread_ioctl(int fd) 4155 { 4156 size_t sz = 0; 4157 4158 if (ioctl(fd, FIONREAD, &sz) < 0 && errno == EACCES) 4159 return errno; 4160 return 0; 4161 } 4162 4163 TEST_F_FORK(layout1, o_path_ftruncate_and_ioctl) 4164 { 4165 int fd; 4166 4167 /* 4168 * Checks that for files opened with O_PATH, both ioctl(2) and 4169 * ftruncate(2) yield EBADF, as it is documented in open(2) for the 4170 * O_PATH flag. 4171 */ 4172 fd = open(dir_s1d1, O_PATH | O_CLOEXEC); 4173 ASSERT_LE(0, fd); 4174 4175 EXPECT_EQ(EBADF, test_ftruncate(fd)); 4176 EXPECT_EQ(EBADF, test_fs_ioc_getflags_ioctl(fd)); 4177 4178 ASSERT_EQ(0, close(fd)); 4179 4180 /* Enables Landlock. */ 4181 enforce_fs(_metadata, ACCESS_ALL, NULL); 4182 4183 /* 4184 * Checks that after enabling Landlock, 4185 * - the file can still be opened with O_PATH 4186 * - both ioctl and truncate still yield EBADF (not EACCES). 4187 */ 4188 fd = open(dir_s1d1, O_PATH | O_CLOEXEC); 4189 ASSERT_LE(0, fd); 4190 4191 EXPECT_EQ(EBADF, test_ftruncate(fd)); 4192 EXPECT_EQ(EBADF, test_fs_ioc_getflags_ioctl(fd)); 4193 4194 ASSERT_EQ(0, close(fd)); 4195 } 4196 4197 /* Arbitrary command with nonzero bits in both 16-bit halves. */ 4198 static const unsigned int unknown_ioctl_cmd = 0xc00ffeee; 4199 4200 /* 4201 * ioctl_error - generically call the given ioctl with a pointer to a 4202 * sufficiently large zeroed-out memory region. 4203 * 4204 * Returns the IOCTLs error, or 0. 4205 */ 4206 static int ioctl_error(struct __test_metadata *const _metadata, int fd, 4207 unsigned int cmd) 4208 { 4209 char buf[128]; /* sufficiently large */ 4210 int res, stdinbak_fd, err; 4211 4212 /* 4213 * Depending on the IOCTL command, parts of the zeroed-out buffer might 4214 * be interpreted as file descriptor numbers. We do not want to 4215 * accidentally operate on file descriptor 0 (stdin), so we temporarily 4216 * move stdin to a different FD and close FD 0 for the IOCTL call. 4217 */ 4218 stdinbak_fd = dup(0); 4219 ASSERT_LT(0, stdinbak_fd); 4220 ASSERT_EQ(0, close(0)); 4221 4222 /* Invokes the IOCTL with a zeroed-out buffer. */ 4223 bzero(&buf, sizeof(buf)); 4224 res = ioctl(fd, cmd, &buf); 4225 err = errno; 4226 4227 /* Restores the old FD 0 and closes the backup FD. */ 4228 ASSERT_EQ(0, dup2(stdinbak_fd, 0)); 4229 ASSERT_EQ(0, close(stdinbak_fd)); 4230 4231 if (res < 0) 4232 return err; 4233 4234 return 0; 4235 } 4236 4237 /* Define some linux/falloc.h IOCTL commands which are not available in uapi headers. */ 4238 struct space_resv { 4239 __s16 l_type; 4240 __s16 l_whence; 4241 __s64 l_start; 4242 __s64 l_len; /* len == 0 means until end of file */ 4243 __s32 l_sysid; 4244 __u32 l_pid; 4245 __s32 l_pad[4]; /* reserved area */ 4246 }; 4247 4248 #define FS_IOC_RESVSP _IOW('X', 40, struct space_resv) 4249 #define FS_IOC_UNRESVSP _IOW('X', 41, struct space_resv) 4250 #define FS_IOC_RESVSP64 _IOW('X', 42, struct space_resv) 4251 #define FS_IOC_UNRESVSP64 _IOW('X', 43, struct space_resv) 4252 #define FS_IOC_ZERO_RANGE _IOW('X', 57, struct space_resv) 4253 4254 /* 4255 * Tests a series of blanket-permitted and denied IOCTLs. 4256 */ 4257 TEST_F_FORK(layout1, blanket_permitted_ioctls) 4258 { 4259 int fd; 4260 4261 /* Enables Landlock. */ 4262 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_IOCTL_DEV, NULL); 4263 4264 fd = open("/dev/null", O_RDWR | O_CLOEXEC); 4265 ASSERT_LE(0, fd); 4266 4267 /* 4268 * Checks permitted commands. 4269 * These ones may return errors, but should not be blocked by Landlock. 4270 */ 4271 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIOCLEX)); 4272 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIONCLEX)); 4273 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIONBIO)); 4274 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIOASYNC)); 4275 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIOQSIZE)); 4276 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIFREEZE)); 4277 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FITHAW)); 4278 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FS_IOC_FIEMAP)); 4279 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIGETBSZ)); 4280 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FICLONE)); 4281 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FICLONERANGE)); 4282 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIDEDUPERANGE)); 4283 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FS_IOC_GETFSUUID)); 4284 EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FS_IOC_GETFSSYSFSPATH)); 4285 4286 /* 4287 * Checks blocked commands. 4288 * A call to a blocked IOCTL command always returns EACCES. 4289 */ 4290 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FIONREAD)); 4291 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_GETFLAGS)); 4292 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_SETFLAGS)); 4293 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_FSGETXATTR)); 4294 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_FSSETXATTR)); 4295 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FIBMAP)); 4296 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_RESVSP)); 4297 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_RESVSP64)); 4298 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_UNRESVSP)); 4299 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_UNRESVSP64)); 4300 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_ZERO_RANGE)); 4301 4302 /* Default case is also blocked. */ 4303 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, unknown_ioctl_cmd)); 4304 4305 ASSERT_EQ(0, close(fd)); 4306 } 4307 4308 /* 4309 * Named pipes are not governed by the LANDLOCK_ACCESS_FS_IOCTL_DEV right, 4310 * because they are not character or block devices. 4311 */ 4312 TEST_F_FORK(layout1, named_pipe_ioctl) 4313 { 4314 pid_t child_pid; 4315 int fd; 4316 const char *const path = file1_s1d1; 4317 4318 ASSERT_EQ(0, unlink(path)); 4319 ASSERT_EQ(0, mkfifo(path, 0600)); 4320 4321 /* Enables Landlock. */ 4322 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_IOCTL_DEV, NULL); 4323 4324 /* The child process opens the pipe for writing. */ 4325 child_pid = fork(); 4326 ASSERT_NE(-1, child_pid); 4327 if (child_pid == 0) { 4328 fd = open(path, O_WRONLY); 4329 close(fd); 4330 exit(0); 4331 } 4332 4333 fd = open(path, O_RDONLY); 4334 ASSERT_LE(0, fd); 4335 4336 /* FIONREAD is implemented by pipefifo_fops. */ 4337 EXPECT_EQ(0, test_fionread_ioctl(fd)); 4338 4339 ASSERT_EQ(0, close(fd)); 4340 ASSERT_EQ(0, unlink(path)); 4341 4342 ASSERT_EQ(child_pid, waitpid(child_pid, NULL, 0)); 4343 } 4344 4345 /* 4346 * set_up_named_unix_server - Create a pathname unix socket 4347 * 4348 * If the socket type is not SOCK_DGRAM, also invoke listen(2). 4349 * 4350 * Return: The listening FD - it is the caller responsibility to close it. 4351 */ 4352 static int set_up_named_unix_server(struct __test_metadata *const _metadata, 4353 int type, const char *const path) 4354 { 4355 int fd; 4356 struct sockaddr_un addr = { 4357 .sun_family = AF_UNIX, 4358 }; 4359 4360 fd = socket(AF_UNIX, type, 0); 4361 ASSERT_LE(0, fd); 4362 4363 ASSERT_LT(strlen(path), sizeof(addr.sun_path)); 4364 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); 4365 4366 ASSERT_EQ(0, bind(fd, (struct sockaddr *)&addr, sizeof(addr))); 4367 4368 if (type != SOCK_DGRAM) 4369 ASSERT_EQ(0, listen(fd, 10 /* qlen */)); 4370 return fd; 4371 } 4372 4373 /* 4374 * test_connect_named_unix - connect to the given named UNIX socket 4375 * 4376 * Return: The errno from connect(), or 0 4377 */ 4378 static int test_connect_named_unix(struct __test_metadata *const _metadata, 4379 int fd, const char *const path) 4380 { 4381 struct sockaddr_un addr = { 4382 .sun_family = AF_UNIX, 4383 }; 4384 4385 ASSERT_LT(strlen(path), sizeof(addr.sun_path)); 4386 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); 4387 4388 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) 4389 return errno; 4390 return 0; 4391 } 4392 4393 /* For named UNIX domain sockets, no IOCTL restrictions apply. */ 4394 TEST_F_FORK(layout1, named_unix_domain_socket_ioctl) 4395 { 4396 const char *const path = file1_s1d1; 4397 int srv_fd, cli_fd; 4398 4399 /* Sets up a server */ 4400 ASSERT_EQ(0, unlink(path)); 4401 srv_fd = set_up_named_unix_server(_metadata, SOCK_STREAM, path); 4402 4403 /* Enables Landlock. */ 4404 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_IOCTL_DEV, NULL); 4405 4406 /* Sets up a client connection to it */ 4407 cli_fd = socket(AF_UNIX, SOCK_STREAM, 0); 4408 ASSERT_LE(0, cli_fd); 4409 4410 ASSERT_EQ(0, test_connect_named_unix(_metadata, cli_fd, path)); 4411 4412 /* FIONREAD and other IOCTLs should not be forbidden. */ 4413 EXPECT_EQ(0, test_fionread_ioctl(cli_fd)); 4414 4415 EXPECT_EQ(0, close(cli_fd)); 4416 EXPECT_EQ(0, close(srv_fd)); 4417 } 4418 4419 /* clang-format off */ 4420 FIXTURE(ioctl) {}; 4421 4422 FIXTURE_SETUP(ioctl) {}; 4423 4424 FIXTURE_TEARDOWN(ioctl) {}; 4425 /* clang-format on */ 4426 4427 FIXTURE_VARIANT(ioctl) 4428 { 4429 const __u64 handled; 4430 const __u64 allowed; 4431 const mode_t open_mode; 4432 /* 4433 * FIONREAD is used as a characteristic device-specific IOCTL command. 4434 * It is implemented in fs/ioctl.c for regular files, 4435 * but we do not blanket-permit it for devices. 4436 */ 4437 const int expected_fionread_result; 4438 }; 4439 4440 /* clang-format off */ 4441 FIXTURE_VARIANT_ADD(ioctl, handled_i_allowed_none) { 4442 /* clang-format on */ 4443 .handled = LANDLOCK_ACCESS_FS_IOCTL_DEV, 4444 .allowed = 0, 4445 .open_mode = O_RDWR, 4446 .expected_fionread_result = EACCES, 4447 }; 4448 4449 /* clang-format off */ 4450 FIXTURE_VARIANT_ADD(ioctl, handled_i_allowed_i) { 4451 /* clang-format on */ 4452 .handled = LANDLOCK_ACCESS_FS_IOCTL_DEV, 4453 .allowed = LANDLOCK_ACCESS_FS_IOCTL_DEV, 4454 .open_mode = O_RDWR, 4455 .expected_fionread_result = 0, 4456 }; 4457 4458 /* clang-format off */ 4459 FIXTURE_VARIANT_ADD(ioctl, unhandled) { 4460 /* clang-format on */ 4461 .handled = LANDLOCK_ACCESS_FS_EXECUTE, 4462 .allowed = LANDLOCK_ACCESS_FS_EXECUTE, 4463 .open_mode = O_RDWR, 4464 .expected_fionread_result = 0, 4465 }; 4466 4467 TEST_F_FORK(ioctl, handle_dir_access_file) 4468 { 4469 const int flag = 0; 4470 const struct rule rules[] = { 4471 { 4472 .path = "/dev", 4473 .access = variant->allowed, 4474 }, 4475 {}, 4476 }; 4477 int fd; 4478 4479 /* Enables Landlock. */ 4480 enforce_fs(_metadata, variant->handled, rules); 4481 4482 fd = open("/dev/zero", variant->open_mode); 4483 ASSERT_LE(0, fd); 4484 4485 /* Checks that IOCTL commands return the expected errors. */ 4486 EXPECT_EQ(variant->expected_fionread_result, test_fionread_ioctl(fd)); 4487 4488 /* Checks that unrestrictable commands are unrestricted. */ 4489 EXPECT_EQ(0, ioctl(fd, FIOCLEX)); 4490 EXPECT_EQ(0, ioctl(fd, FIONCLEX)); 4491 EXPECT_EQ(0, ioctl(fd, FIONBIO, &flag)); 4492 EXPECT_EQ(0, ioctl(fd, FIOASYNC, &flag)); 4493 EXPECT_EQ(0, ioctl(fd, FIGETBSZ, &flag)); 4494 4495 ASSERT_EQ(0, close(fd)); 4496 } 4497 4498 TEST_F_FORK(ioctl, handle_dir_access_dir) 4499 { 4500 const int flag = 0; 4501 const struct rule rules[] = { 4502 { 4503 .path = "/dev", 4504 .access = variant->allowed, 4505 }, 4506 {}, 4507 }; 4508 int dir_fd; 4509 4510 /* Enables Landlock. */ 4511 enforce_fs(_metadata, variant->handled, rules); 4512 4513 /* 4514 * Ignore variant->open_mode for this test, as we intend to open a 4515 * directory. If the directory can not be opened, the variant is 4516 * infeasible to test with an opened directory. 4517 */ 4518 dir_fd = open("/dev", O_RDONLY); 4519 if (dir_fd < 0) 4520 return; 4521 4522 /* 4523 * Checks that IOCTL commands return the expected errors. 4524 * We do not use the expected values from the fixture here. 4525 * 4526 * When using IOCTL on a directory, no Landlock restrictions apply. 4527 */ 4528 EXPECT_EQ(0, test_fionread_ioctl(dir_fd)); 4529 4530 /* Checks that unrestrictable commands are unrestricted. */ 4531 EXPECT_EQ(0, ioctl(dir_fd, FIOCLEX)); 4532 EXPECT_EQ(0, ioctl(dir_fd, FIONCLEX)); 4533 EXPECT_EQ(0, ioctl(dir_fd, FIONBIO, &flag)); 4534 EXPECT_EQ(0, ioctl(dir_fd, FIOASYNC, &flag)); 4535 EXPECT_EQ(0, ioctl(dir_fd, FIGETBSZ, &flag)); 4536 4537 ASSERT_EQ(0, close(dir_fd)); 4538 } 4539 4540 TEST_F_FORK(ioctl, handle_file_access_file) 4541 { 4542 const int flag = 0; 4543 const struct rule rules[] = { 4544 { 4545 .path = "/dev/zero", 4546 .access = variant->allowed, 4547 }, 4548 {}, 4549 }; 4550 int fd; 4551 4552 /* Enables Landlock. */ 4553 enforce_fs(_metadata, variant->handled, rules); 4554 4555 fd = open("/dev/zero", variant->open_mode); 4556 ASSERT_LE(0, fd) 4557 { 4558 TH_LOG("Failed to open /dev/zero: %s", strerror(errno)); 4559 } 4560 4561 /* Checks that IOCTL commands return the expected errors. */ 4562 EXPECT_EQ(variant->expected_fionread_result, test_fionread_ioctl(fd)); 4563 4564 /* Checks that unrestrictable commands are unrestricted. */ 4565 EXPECT_EQ(0, ioctl(fd, FIOCLEX)); 4566 EXPECT_EQ(0, ioctl(fd, FIONCLEX)); 4567 EXPECT_EQ(0, ioctl(fd, FIONBIO, &flag)); 4568 EXPECT_EQ(0, ioctl(fd, FIOASYNC, &flag)); 4569 EXPECT_EQ(0, ioctl(fd, FIGETBSZ, &flag)); 4570 4571 ASSERT_EQ(0, close(fd)); 4572 } 4573 4574 /* 4575 * test_sendto_named_unix - sendto to the given named UNIX socket 4576 * 4577 * sendto() is equivalent to sendmsg() in this respect. 4578 * 4579 * Return: The errno from sendto(), or 0 4580 */ 4581 static int test_sendto_named_unix(struct __test_metadata *const _metadata, 4582 int fd, const char *const path) 4583 { 4584 static const char buf[] = "dummy"; 4585 struct sockaddr_un addr = { 4586 .sun_family = AF_UNIX, 4587 }; 4588 4589 ASSERT_LT(strlen(path), sizeof(addr.sun_path)); 4590 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); 4591 4592 if (sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, 4593 sizeof(addr)) == -1) 4594 return errno; 4595 return 0; 4596 } 4597 4598 /* clang-format off */ 4599 FIXTURE(scoped_domains) {}; 4600 /* clang-format on */ 4601 4602 #include "scoped_base_variants.h" 4603 4604 FIXTURE_SETUP(scoped_domains) 4605 { 4606 drop_caps(_metadata); 4607 }; 4608 4609 FIXTURE_TEARDOWN(scoped_domains) 4610 { 4611 } 4612 4613 /* 4614 * Flags for test_connect_to_parent and test_connect_to_child: 4615 * 4616 * USE_SENDTO: Use sendto() instead of connect() (for SOCK_DGRAM only) 4617 * ENFORCE_ALL: Enforce a Landlock domain even when the variant says 4618 * we shouldn't. We enforce a domain where the path is allow-listed, 4619 * and expect the behavior to be the same as if none was used. 4620 */ 4621 #define USE_SENDTO (1 << 0) 4622 #define ENFORCE_ALL (1 << 1) 4623 4624 static void test_connect_to_parent(struct __test_metadata *const _metadata, 4625 const FIXTURE_VARIANT(scoped_domains) * 4626 variant, 4627 int sock_type, int flags) 4628 { 4629 const char *const path = "sock"; 4630 const struct rule rules[] = { 4631 { 4632 .path = ".", 4633 .access = LANDLOCK_ACCESS_FS_RESOLVE_UNIX, 4634 }, 4635 {}, 4636 }; 4637 int cli_fd, srv_fd, res, status; 4638 pid_t child_pid; 4639 int readiness_pipe[2]; 4640 char buf[1]; 4641 4642 if (variant->domain_both) 4643 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, NULL); 4644 else if (flags & ENFORCE_ALL) 4645 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, rules); 4646 4647 unlink(path); 4648 ASSERT_EQ(0, pipe2(readiness_pipe, O_CLOEXEC)); 4649 4650 child_pid = fork(); 4651 ASSERT_LE(0, child_pid); 4652 4653 if (child_pid == 0) { 4654 if (variant->domain_child) 4655 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, 4656 NULL); 4657 else if (flags & ENFORCE_ALL) 4658 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, 4659 rules); 4660 4661 /* Wait for server to be available. */ 4662 EXPECT_EQ(0, close(readiness_pipe[1])); 4663 EXPECT_EQ(1, read(readiness_pipe[0], &buf, 1)); 4664 EXPECT_EQ(0, close(readiness_pipe[0])); 4665 4666 /* Talk to server. */ 4667 cli_fd = socket(AF_UNIX, sock_type, 0); 4668 ASSERT_LE(0, cli_fd); 4669 4670 if (flags & USE_SENDTO) 4671 res = test_sendto_named_unix(_metadata, cli_fd, path); 4672 else 4673 res = test_connect_named_unix(_metadata, cli_fd, path); 4674 4675 EXPECT_EQ(variant->domain_child ? EACCES : 0, res); 4676 4677 /* Clean up. */ 4678 EXPECT_EQ(0, close(cli_fd)); 4679 4680 _exit(_metadata->exit_code); 4681 return; 4682 } 4683 4684 if (variant->domain_parent) 4685 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, NULL); 4686 else if (flags & ENFORCE_ALL) 4687 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, rules); 4688 4689 srv_fd = set_up_named_unix_server(_metadata, sock_type, path); 4690 4691 /* Tell the child that it can connect. */ 4692 EXPECT_EQ(0, close(readiness_pipe[0])); 4693 EXPECT_EQ(sizeof(buf), write(readiness_pipe[1], buf, sizeof(buf))); 4694 EXPECT_EQ(0, close(readiness_pipe[1])); 4695 4696 /* Wait for child. */ 4697 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 4698 EXPECT_EQ(1, WIFEXITED(status)); 4699 EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 4700 4701 /* Clean up. */ 4702 EXPECT_EQ(0, close(srv_fd)); 4703 EXPECT_EQ(0, unlink(path)); 4704 } 4705 4706 static void test_connect_to_child(struct __test_metadata *const _metadata, 4707 const FIXTURE_VARIANT(scoped_domains) * 4708 variant, 4709 int sock_type, int flags) 4710 { 4711 const char *const path = "sock"; 4712 const struct rule rules[] = { 4713 { 4714 .path = ".", 4715 .access = LANDLOCK_ACCESS_FS_RESOLVE_UNIX, 4716 }, 4717 {}, 4718 }; 4719 int readiness_pipe[2]; 4720 int shutdown_pipe[2]; 4721 int cli_fd, srv_fd, res, status; 4722 pid_t child_pid; 4723 char buf[1]; 4724 4725 if (variant->domain_both) 4726 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, NULL); 4727 else if (flags & ENFORCE_ALL) 4728 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, rules); 4729 4730 unlink(path); 4731 ASSERT_EQ(0, pipe2(readiness_pipe, O_CLOEXEC)); 4732 ASSERT_EQ(0, pipe2(shutdown_pipe, O_CLOEXEC)); 4733 4734 child_pid = fork(); 4735 ASSERT_LE(0, child_pid); 4736 4737 if (child_pid == 0) { 4738 if (variant->domain_child) 4739 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, 4740 NULL); 4741 else if (flags & ENFORCE_ALL) 4742 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, 4743 rules); 4744 4745 srv_fd = set_up_named_unix_server(_metadata, sock_type, path); 4746 4747 /* Tell the parent that it can connect. */ 4748 EXPECT_EQ(0, close(readiness_pipe[0])); 4749 EXPECT_EQ(sizeof(buf), 4750 write(readiness_pipe[1], buf, sizeof(buf))); 4751 EXPECT_EQ(0, close(readiness_pipe[1])); 4752 4753 /* Wait until it is time to shut down. */ 4754 EXPECT_EQ(0, close(shutdown_pipe[1])); 4755 EXPECT_EQ(1, read(shutdown_pipe[0], &buf, 1)); 4756 EXPECT_EQ(0, close(shutdown_pipe[0])); 4757 4758 /* Cleanup */ 4759 EXPECT_EQ(0, close(srv_fd)); 4760 EXPECT_EQ(0, unlink(path)); 4761 4762 _exit(_metadata->exit_code); 4763 return; 4764 } 4765 4766 if (variant->domain_parent) 4767 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, NULL); 4768 else if (flags & ENFORCE_ALL) 4769 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, rules); 4770 4771 /* Wait for server to be available. */ 4772 EXPECT_EQ(0, close(readiness_pipe[1])); 4773 EXPECT_EQ(1, read(readiness_pipe[0], &buf, 1)); 4774 EXPECT_EQ(0, close(readiness_pipe[0])); 4775 4776 /* Talk to server. */ 4777 cli_fd = socket(AF_UNIX, sock_type, 0); 4778 ASSERT_LE(0, cli_fd); 4779 4780 if (flags & USE_SENDTO) 4781 res = test_sendto_named_unix(_metadata, cli_fd, path); 4782 else 4783 res = test_connect_named_unix(_metadata, cli_fd, path); 4784 4785 EXPECT_EQ(variant->domain_parent ? EACCES : 0, res); 4786 4787 /* Clean up. */ 4788 EXPECT_EQ(0, close(cli_fd)); 4789 4790 /* Tell the server to shut down. */ 4791 EXPECT_EQ(0, close(shutdown_pipe[0])); 4792 EXPECT_EQ(sizeof(buf), write(shutdown_pipe[1], buf, sizeof(buf))); 4793 EXPECT_EQ(0, close(shutdown_pipe[1])); 4794 4795 /* Wait for child. */ 4796 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 4797 EXPECT_EQ(1, WIFEXITED(status)); 4798 EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 4799 } 4800 4801 TEST_F(scoped_domains, unix_stream_connect_to_parent) 4802 { 4803 test_connect_to_parent(_metadata, variant, SOCK_STREAM, 0); 4804 } 4805 4806 TEST_F(scoped_domains, unix_dgram_connect_to_parent) 4807 { 4808 test_connect_to_parent(_metadata, variant, SOCK_DGRAM, 0); 4809 } 4810 4811 TEST_F(scoped_domains, unix_dgram_sendmsg_to_parent) 4812 { 4813 test_connect_to_parent(_metadata, variant, SOCK_DGRAM, USE_SENDTO); 4814 } 4815 4816 TEST_F(scoped_domains, unix_seqpacket_connect_to_parent) 4817 { 4818 test_connect_to_parent(_metadata, variant, SOCK_SEQPACKET, 0); 4819 } 4820 4821 TEST_F(scoped_domains, unix_stream_connect_to_parent_full) 4822 { 4823 test_connect_to_parent(_metadata, variant, SOCK_STREAM, ENFORCE_ALL); 4824 } 4825 4826 TEST_F(scoped_domains, unix_dgram_connect_to_parent_full) 4827 { 4828 test_connect_to_parent(_metadata, variant, SOCK_DGRAM, ENFORCE_ALL); 4829 } 4830 4831 TEST_F(scoped_domains, unix_dgram_sendmsg_to_parent_full) 4832 { 4833 test_connect_to_parent(_metadata, variant, SOCK_DGRAM, 4834 USE_SENDTO | ENFORCE_ALL); 4835 } 4836 4837 TEST_F(scoped_domains, unix_seqpacket_connect_to_parent_full) 4838 { 4839 test_connect_to_parent(_metadata, variant, SOCK_SEQPACKET, ENFORCE_ALL); 4840 } 4841 4842 TEST_F(scoped_domains, unix_stream_connect_to_child) 4843 { 4844 test_connect_to_child(_metadata, variant, SOCK_STREAM, 0); 4845 } 4846 4847 TEST_F(scoped_domains, unix_dgram_connect_to_child) 4848 { 4849 test_connect_to_child(_metadata, variant, SOCK_DGRAM, 0); 4850 } 4851 4852 TEST_F(scoped_domains, unix_dgram_sendmsg_to_child) 4853 { 4854 test_connect_to_child(_metadata, variant, SOCK_DGRAM, USE_SENDTO); 4855 } 4856 4857 TEST_F(scoped_domains, unix_seqpacket_connect_to_child) 4858 { 4859 test_connect_to_child(_metadata, variant, SOCK_SEQPACKET, 0); 4860 } 4861 4862 TEST_F(scoped_domains, unix_stream_connect_to_child_full) 4863 { 4864 test_connect_to_child(_metadata, variant, SOCK_STREAM, ENFORCE_ALL); 4865 } 4866 4867 TEST_F(scoped_domains, unix_dgram_connect_to_child_full) 4868 { 4869 test_connect_to_child(_metadata, variant, SOCK_DGRAM, ENFORCE_ALL); 4870 } 4871 4872 TEST_F(scoped_domains, unix_dgram_sendmsg_to_child_full) 4873 { 4874 test_connect_to_child(_metadata, variant, SOCK_DGRAM, 4875 USE_SENDTO | ENFORCE_ALL); 4876 } 4877 4878 TEST_F(scoped_domains, unix_seqpacket_connect_to_child_full) 4879 { 4880 test_connect_to_child(_metadata, variant, SOCK_SEQPACKET, ENFORCE_ALL); 4881 } 4882 4883 #undef USE_SENDTO 4884 #undef ENFORCE_ALL 4885 4886 static void read_core_pattern(struct __test_metadata *const _metadata, 4887 char *buf, size_t buf_size) 4888 { 4889 int fd; 4890 ssize_t ret; 4891 4892 fd = open("/proc/sys/kernel/core_pattern", O_RDONLY | O_CLOEXEC); 4893 ASSERT_LE(0, fd); 4894 4895 ret = read(fd, buf, buf_size - 1); 4896 ASSERT_LE(0, ret); 4897 EXPECT_EQ(0, close(fd)); 4898 4899 buf[ret] = '\0'; 4900 } 4901 4902 static void set_core_pattern(struct __test_metadata *const _metadata, 4903 const char *pattern) 4904 { 4905 int fd; 4906 size_t len = strlen(pattern); 4907 4908 /* 4909 * Writing to /proc/sys/kernel/core_pattern requires EUID 0 because 4910 * sysctl_perm() checks that, ignoring capabilities like 4911 * CAP_SYS_ADMIN or CAP_DAC_OVERRIDE. 4912 * 4913 * Switching EUID clears the dumpable flag, which must be restored 4914 * afterwards to allow coredumps. 4915 */ 4916 set_cap(_metadata, CAP_SETUID); 4917 ASSERT_EQ(0, seteuid(0)); 4918 clear_cap(_metadata, CAP_SETUID); 4919 4920 fd = open("/proc/sys/kernel/core_pattern", O_WRONLY | O_CLOEXEC); 4921 ASSERT_LE(0, fd) 4922 { 4923 TH_LOG("Failed to open core_pattern for writing: %s", 4924 strerror(errno)); 4925 } 4926 4927 ASSERT_EQ(len, write(fd, pattern, len)); 4928 EXPECT_EQ(0, close(fd)); 4929 4930 set_cap(_metadata, CAP_SETUID); 4931 ASSERT_EQ(0, seteuid(getuid())); 4932 clear_cap(_metadata, CAP_SETUID); 4933 4934 /* Restore dumpable flag cleared by seteuid(). */ 4935 ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)); 4936 } 4937 4938 FIXTURE(coredump) 4939 { 4940 char original_core_pattern[256]; 4941 }; 4942 4943 FIXTURE_SETUP(coredump) 4944 { 4945 disable_caps(_metadata); 4946 read_core_pattern(_metadata, self->original_core_pattern, 4947 sizeof(self->original_core_pattern)); 4948 } 4949 4950 FIXTURE_TEARDOWN_PARENT(coredump) 4951 { 4952 set_core_pattern(_metadata, self->original_core_pattern); 4953 } 4954 4955 /* 4956 * Test that even when a process is restricted with 4957 * LANDLOCK_ACCESS_FS_RESOLVE_UNIX, the kernel can still initiate a connection 4958 * to the coredump socket on the processes' behalf. 4959 */ 4960 TEST_F_FORK(coredump, socket_not_restricted) 4961 { 4962 static const char core_pattern[] = "@/tmp/landlock_coredump_test.sock"; 4963 const char *const sock_path = core_pattern + 1; 4964 int srv_fd, conn_fd, status; 4965 pid_t child_pid; 4966 struct ucred cred; 4967 socklen_t cred_len = sizeof(cred); 4968 char buf[4096]; 4969 4970 /* Set up the coredump server socket. */ 4971 unlink(sock_path); 4972 srv_fd = set_up_named_unix_server(_metadata, SOCK_STREAM, sock_path); 4973 4974 /* Point coredumps at our socket. */ 4975 set_core_pattern(_metadata, core_pattern); 4976 4977 /* Restrict LANDLOCK_ACCESS_FS_RESOLVE_UNIX. */ 4978 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_RESOLVE_UNIX, NULL); 4979 4980 /* Fork a child that crashes. */ 4981 child_pid = fork(); 4982 ASSERT_LE(0, child_pid); 4983 if (child_pid == 0) { 4984 struct rlimit rl = { 4985 .rlim_cur = RLIM_INFINITY, 4986 .rlim_max = RLIM_INFINITY, 4987 }; 4988 4989 ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &rl)); 4990 4991 /* Crash on purpose. */ 4992 kill(getpid(), SIGSEGV); 4993 _exit(1); 4994 } 4995 4996 /* 4997 * Accept the coredump connection. If Landlock incorrectly denies the 4998 * kernel's coredump connect, accept() will block forever, so the test 4999 * would time out. 5000 */ 5001 conn_fd = accept(srv_fd, NULL, NULL); 5002 ASSERT_LE(0, conn_fd); 5003 5004 /* Check that the connection came from the crashing child. */ 5005 ASSERT_EQ(0, getsockopt(conn_fd, SOL_SOCKET, SO_PEERCRED, &cred, 5006 &cred_len)); 5007 EXPECT_EQ(child_pid, cred.pid); 5008 5009 /* Drain the coredump data so the kernel can finish. */ 5010 while (read(conn_fd, buf, sizeof(buf)) > 0) 5011 ; 5012 5013 EXPECT_EQ(0, close(conn_fd)); 5014 5015 /* Wait for the child and verify it coredumped. */ 5016 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 5017 ASSERT_TRUE(WIFSIGNALED(status)); 5018 ASSERT_TRUE(WCOREDUMP(status)); 5019 5020 EXPECT_EQ(0, close(srv_fd)); 5021 EXPECT_EQ(0, unlink(sock_path)); 5022 } 5023 5024 /* clang-format off */ 5025 FIXTURE(layout1_bind) {}; 5026 /* clang-format on */ 5027 5028 static const char bind_dir_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3"; 5029 static const char bind_file1_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3/f1"; 5030 static const char bind_file2_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3/f2"; 5031 5032 /* Move targets for disconnected path tests. */ 5033 static const char dir_s4d1[] = TMP_DIR "/s4d1"; 5034 static const char file1_s4d1[] = TMP_DIR "/s4d1/f1"; 5035 static const char file2_s4d1[] = TMP_DIR "/s4d1/f2"; 5036 static const char dir_s4d2[] = TMP_DIR "/s4d1/s4d2"; 5037 static const char file1_s4d2[] = TMP_DIR "/s4d1/s4d2/f1"; 5038 static const char file1_name[] = "f1"; 5039 static const char file2_name[] = "f2"; 5040 5041 FIXTURE_SETUP(layout1_bind) 5042 { 5043 prepare_layout(_metadata); 5044 5045 create_layout1(_metadata); 5046 5047 set_cap(_metadata, CAP_SYS_ADMIN); 5048 ASSERT_EQ(0, mount(dir_s1d2, dir_s2d2, NULL, MS_BIND, NULL)); 5049 clear_cap(_metadata, CAP_SYS_ADMIN); 5050 } 5051 5052 FIXTURE_TEARDOWN_PARENT(layout1_bind) 5053 { 5054 /* umount(dir_s2d2)) is handled by namespace lifetime. */ 5055 5056 remove_path(file1_s4d1); 5057 remove_path(file2_s4d1); 5058 5059 remove_layout1(_metadata); 5060 5061 cleanup_layout(_metadata); 5062 } 5063 5064 /* 5065 * layout1_bind hierarchy: 5066 * 5067 * tmp 5068 * ├── s1d1 5069 * │ ├── f1 5070 * │ ├── f2 5071 * │ └── s1d2 5072 * │ ├── f1 5073 * │ ├── f2 5074 * │ └── s1d3 [disconnected by path_disconnected] 5075 * │ ├── f1 5076 * │ └── f2 5077 * ├── s2d1 5078 * │ ├── f1 5079 * │ └── s2d2 [bind mount from s1d2] 5080 * │ ├── f1 5081 * │ ├── f2 5082 * │ └── s1d3 5083 * │ ├── f1 5084 * │ └── f2 5085 * ├── s3d1 5086 * │ └── s3d2 5087 * │ └── s3d3 5088 * └── s4d1 [renamed from s1d3 by path_disconnected] 5089 * ├── f1 5090 * ├── f2 5091 * └── s4d2 5092 * └── f1 5093 */ 5094 5095 TEST_F_FORK(layout1_bind, no_restriction) 5096 { 5097 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 5098 ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 5099 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 5100 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 5101 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 5102 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 5103 5104 ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY)); 5105 ASSERT_EQ(0, test_open(file1_s2d1, O_RDONLY)); 5106 ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY)); 5107 ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY)); 5108 ASSERT_EQ(ENOENT, test_open(dir_s2d3, O_RDONLY)); 5109 ASSERT_EQ(ENOENT, test_open(file1_s2d3, O_RDONLY)); 5110 5111 ASSERT_EQ(0, test_open(bind_dir_s1d3, O_RDONLY)); 5112 ASSERT_EQ(0, test_open(bind_file1_s1d3, O_RDONLY)); 5113 5114 ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY)); 5115 } 5116 5117 TEST_F_FORK(layout1_bind, same_content_same_file) 5118 { 5119 /* 5120 * Sets access right on parent directories of both source and 5121 * destination mount points. 5122 */ 5123 const struct rule layer1_parent[] = { 5124 { 5125 .path = dir_s1d1, 5126 .access = ACCESS_RO, 5127 }, 5128 { 5129 .path = dir_s2d1, 5130 .access = ACCESS_RW, 5131 }, 5132 {}, 5133 }; 5134 /* 5135 * Sets access rights on the same bind-mounted directories. The result 5136 * should be ACCESS_RW for both directories, but not both hierarchies 5137 * because of the first layer. 5138 */ 5139 const struct rule layer2_mount_point[] = { 5140 { 5141 .path = dir_s1d2, 5142 .access = LANDLOCK_ACCESS_FS_READ_FILE, 5143 }, 5144 { 5145 .path = dir_s2d2, 5146 .access = ACCESS_RW, 5147 }, 5148 {}, 5149 }; 5150 /* Only allow read-access to the s1d3 hierarchies. */ 5151 const struct rule layer3_source[] = { 5152 { 5153 .path = dir_s1d3, 5154 .access = LANDLOCK_ACCESS_FS_READ_FILE, 5155 }, 5156 {}, 5157 }; 5158 /* Removes all access rights. */ 5159 const struct rule layer4_destination[] = { 5160 { 5161 .path = bind_file1_s1d3, 5162 .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 5163 }, 5164 {}, 5165 }; 5166 5167 /* Sets rules for the parent directories. */ 5168 enforce_fs(_metadata, ACCESS_RW, layer1_parent); 5169 5170 /* Checks source hierarchy. */ 5171 ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 5172 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 5173 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 5174 5175 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 5176 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 5177 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 5178 5179 /* Checks destination hierarchy. */ 5180 ASSERT_EQ(0, test_open(file1_s2d1, O_RDWR)); 5181 ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY | O_DIRECTORY)); 5182 5183 ASSERT_EQ(0, test_open(file1_s2d2, O_RDWR)); 5184 ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY)); 5185 5186 /* Sets rules for the mount points. */ 5187 enforce_fs(_metadata, ACCESS_RW, layer2_mount_point); 5188 5189 /* Checks source hierarchy. */ 5190 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 5191 ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 5192 ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 5193 5194 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 5195 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 5196 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 5197 5198 /* Checks destination hierarchy. */ 5199 ASSERT_EQ(EACCES, test_open(file1_s2d1, O_RDONLY)); 5200 ASSERT_EQ(EACCES, test_open(file1_s2d1, O_WRONLY)); 5201 ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY | O_DIRECTORY)); 5202 5203 ASSERT_EQ(0, test_open(file1_s2d2, O_RDWR)); 5204 ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY)); 5205 ASSERT_EQ(0, test_open(bind_dir_s1d3, O_RDONLY | O_DIRECTORY)); 5206 5207 /* Sets a (shared) rule only on the source. */ 5208 enforce_fs(_metadata, ACCESS_RW, layer3_source); 5209 5210 /* Checks source hierarchy. */ 5211 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDONLY)); 5212 ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 5213 ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 5214 5215 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 5216 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 5217 ASSERT_EQ(EACCES, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 5218 5219 /* Checks destination hierarchy. */ 5220 ASSERT_EQ(EACCES, test_open(file1_s2d2, O_RDONLY)); 5221 ASSERT_EQ(EACCES, test_open(file1_s2d2, O_WRONLY)); 5222 ASSERT_EQ(EACCES, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY)); 5223 5224 ASSERT_EQ(0, test_open(bind_file1_s1d3, O_RDONLY)); 5225 ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY)); 5226 ASSERT_EQ(EACCES, test_open(bind_dir_s1d3, O_RDONLY | O_DIRECTORY)); 5227 5228 /* Sets a (shared) rule only on the destination. */ 5229 enforce_fs(_metadata, ACCESS_RW, layer4_destination); 5230 5231 /* Checks source hierarchy. */ 5232 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY)); 5233 ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 5234 5235 /* Checks destination hierarchy. */ 5236 ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_RDONLY)); 5237 ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY)); 5238 } 5239 5240 TEST_F_FORK(layout1_bind, reparent_cross_mount) 5241 { 5242 const struct rule layer1[] = { 5243 { 5244 /* dir_s2d1 is beneath the dir_s2d2 mount point. */ 5245 .path = dir_s2d1, 5246 .access = LANDLOCK_ACCESS_FS_REFER, 5247 }, 5248 { 5249 .path = bind_dir_s1d3, 5250 .access = LANDLOCK_ACCESS_FS_EXECUTE, 5251 }, 5252 {}, 5253 }; 5254 5255 enforce_fs(_metadata, 5256 LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_EXECUTE, 5257 layer1); 5258 5259 /* Checks basic denied move. */ 5260 ASSERT_EQ(-1, rename(file1_s1d1, file1_s1d2)); 5261 ASSERT_EQ(EXDEV, errno); 5262 5263 /* Checks real cross-mount move (Landlock is not involved). */ 5264 ASSERT_EQ(-1, rename(file1_s2d1, file1_s2d2)); 5265 ASSERT_EQ(EXDEV, errno); 5266 5267 /* Checks move that will give more accesses. */ 5268 ASSERT_EQ(-1, rename(file1_s2d2, bind_file1_s1d3)); 5269 ASSERT_EQ(EXDEV, errno); 5270 5271 /* Checks legitimate downgrade move. */ 5272 ASSERT_EQ(0, rename(bind_file1_s1d3, file1_s2d2)); 5273 } 5274 5275 /* 5276 * Make sure access to file through a disconnected path works as expected. 5277 * This test moves s1d3 to s4d1. 5278 */ 5279 TEST_F_FORK(layout1_bind, path_disconnected) 5280 { 5281 const struct rule layer1_allow_all[] = { 5282 { 5283 .path = TMP_DIR, 5284 .access = ACCESS_ALL, 5285 }, 5286 {}, 5287 }; 5288 const struct rule layer2_allow_just_f1[] = { 5289 { 5290 .path = file1_s1d3, 5291 .access = LANDLOCK_ACCESS_FS_READ_FILE, 5292 }, 5293 {}, 5294 }; 5295 const struct rule layer3_only_s1d2[] = { 5296 { 5297 .path = dir_s1d2, 5298 .access = LANDLOCK_ACCESS_FS_READ_FILE, 5299 }, 5300 {}, 5301 }; 5302 5303 /* Landlock should not deny access just because it is disconnected. */ 5304 int ruleset_fd_l1 = 5305 create_ruleset(_metadata, ACCESS_ALL, layer1_allow_all); 5306 5307 /* Creates the new ruleset now before we move the dir containing the file. */ 5308 int ruleset_fd_l2 = 5309 create_ruleset(_metadata, ACCESS_RW, layer2_allow_just_f1); 5310 int ruleset_fd_l3 = 5311 create_ruleset(_metadata, ACCESS_RW, layer3_only_s1d2); 5312 int bind_s1d3_fd; 5313 5314 enforce_ruleset(_metadata, ruleset_fd_l1); 5315 EXPECT_EQ(0, close(ruleset_fd_l1)); 5316 5317 bind_s1d3_fd = open(bind_dir_s1d3, O_PATH | O_CLOEXEC); 5318 ASSERT_LE(0, bind_s1d3_fd); 5319 5320 /* Tests access is possible before we move. */ 5321 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5322 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file2_name, O_RDONLY)); 5323 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, "..", O_RDONLY | O_DIRECTORY)); 5324 5325 /* Makes it disconnected. */ 5326 ASSERT_EQ(0, rename(dir_s1d3, dir_s4d1)) 5327 { 5328 TH_LOG("Failed to rename %s to %s: %s", dir_s1d3, dir_s4d1, 5329 strerror(errno)); 5330 } 5331 5332 /* Tests that access is still possible. */ 5333 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5334 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file2_name, O_RDONLY)); 5335 5336 /* 5337 * Tests that ".." is not possible (not because of Landlock, but just 5338 * because it's disconnected). 5339 */ 5340 EXPECT_EQ(ENOENT, 5341 test_open_rel(bind_s1d3_fd, "..", O_RDONLY | O_DIRECTORY)); 5342 5343 /* This should still work with a narrower rule. */ 5344 enforce_ruleset(_metadata, ruleset_fd_l2); 5345 EXPECT_EQ(0, close(ruleset_fd_l2)); 5346 5347 EXPECT_EQ(0, test_open(file1_s4d1, O_RDONLY)); 5348 /* 5349 * Accessing a file through a disconnected file descriptor can still be 5350 * allowed by a rule tied to this file, even if it is no longer visible in 5351 * its mount point. 5352 */ 5353 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5354 EXPECT_EQ(EACCES, test_open_rel(bind_s1d3_fd, file2_name, O_RDONLY)); 5355 5356 enforce_ruleset(_metadata, ruleset_fd_l3); 5357 EXPECT_EQ(0, close(ruleset_fd_l3)); 5358 5359 EXPECT_EQ(EACCES, test_open(file1_s4d1, O_RDONLY)); 5360 /* 5361 * Accessing a file through a disconnected file descriptor can still be 5362 * allowed by a rule tied to the original mount point, even if it is no 5363 * longer visible in its mount point. 5364 */ 5365 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5366 EXPECT_EQ(EACCES, test_open_rel(bind_s1d3_fd, file2_name, O_RDONLY)); 5367 } 5368 5369 /* 5370 * Test that renameat with disconnected paths works under Landlock. This test 5371 * moves s1d3 to s4d2, so that we can have a rule allowing refers on the move 5372 * target's immediate parent. 5373 */ 5374 TEST_F_FORK(layout1_bind, path_disconnected_rename) 5375 { 5376 const struct rule layer1[] = { 5377 { 5378 .path = dir_s1d2, 5379 .access = LANDLOCK_ACCESS_FS_REFER | 5380 LANDLOCK_ACCESS_FS_MAKE_DIR | 5381 LANDLOCK_ACCESS_FS_REMOVE_DIR | 5382 LANDLOCK_ACCESS_FS_MAKE_REG | 5383 LANDLOCK_ACCESS_FS_REMOVE_FILE | 5384 LANDLOCK_ACCESS_FS_READ_FILE, 5385 }, 5386 { 5387 .path = dir_s4d1, 5388 .access = LANDLOCK_ACCESS_FS_REFER | 5389 LANDLOCK_ACCESS_FS_MAKE_DIR | 5390 LANDLOCK_ACCESS_FS_REMOVE_DIR | 5391 LANDLOCK_ACCESS_FS_MAKE_REG | 5392 LANDLOCK_ACCESS_FS_REMOVE_FILE | 5393 LANDLOCK_ACCESS_FS_READ_FILE, 5394 }, 5395 {} 5396 }; 5397 5398 /* This layer only handles LANDLOCK_ACCESS_FS_READ_FILE. */ 5399 const struct rule layer2_only_s1d2[] = { 5400 { 5401 .path = dir_s1d2, 5402 .access = LANDLOCK_ACCESS_FS_READ_FILE, 5403 }, 5404 {}, 5405 }; 5406 int ruleset_fd_l1, ruleset_fd_l2; 5407 pid_t child_pid; 5408 int bind_s1d3_fd, status; 5409 5410 ASSERT_EQ(0, mkdir(dir_s4d1, 0755)) 5411 { 5412 TH_LOG("Failed to create %s: %s", dir_s4d1, strerror(errno)); 5413 } 5414 ruleset_fd_l1 = create_ruleset(_metadata, ACCESS_ALL, layer1); 5415 ruleset_fd_l2 = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, 5416 layer2_only_s1d2); 5417 5418 enforce_ruleset(_metadata, ruleset_fd_l1); 5419 EXPECT_EQ(0, close(ruleset_fd_l1)); 5420 5421 bind_s1d3_fd = open(bind_dir_s1d3, O_PATH | O_CLOEXEC); 5422 ASSERT_LE(0, bind_s1d3_fd); 5423 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5424 5425 /* Tests ENOENT priority over EACCES for disconnected directory. */ 5426 EXPECT_EQ(EACCES, test_open_rel(bind_s1d3_fd, "..", O_DIRECTORY)); 5427 ASSERT_EQ(0, rename(dir_s1d3, dir_s4d2)) 5428 { 5429 TH_LOG("Failed to rename %s to %s: %s", dir_s1d3, dir_s4d2, 5430 strerror(errno)); 5431 } 5432 EXPECT_EQ(ENOENT, test_open_rel(bind_s1d3_fd, "..", O_DIRECTORY)); 5433 5434 /* 5435 * The file is no longer under s1d2 but we should still be able to access it 5436 * with layer 2 because its mount point is evaluated as the first valid 5437 * directory because it was initially a parent. Do a fork to test this so 5438 * we don't prevent ourselves from renaming it back later. 5439 */ 5440 child_pid = fork(); 5441 ASSERT_LE(0, child_pid); 5442 if (child_pid == 0) { 5443 enforce_ruleset(_metadata, ruleset_fd_l2); 5444 EXPECT_EQ(0, close(ruleset_fd_l2)); 5445 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5446 EXPECT_EQ(EACCES, test_open(file1_s4d2, O_RDONLY)); 5447 5448 /* 5449 * Tests that access widening checks indeed prevents us from renaming it 5450 * back. 5451 */ 5452 EXPECT_EQ(-1, rename(dir_s4d2, dir_s1d3)); 5453 EXPECT_EQ(EXDEV, errno); 5454 5455 /* 5456 * Including through the now disconnected fd (but it should return 5457 * EXDEV). 5458 */ 5459 EXPECT_EQ(-1, renameat(bind_s1d3_fd, file1_name, AT_FDCWD, 5460 file1_s2d2)); 5461 EXPECT_EQ(EXDEV, errno); 5462 _exit(_metadata->exit_code); 5463 return; 5464 } 5465 5466 EXPECT_EQ(child_pid, waitpid(child_pid, &status, 0)); 5467 EXPECT_EQ(1, WIFEXITED(status)); 5468 EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 5469 5470 ASSERT_EQ(0, rename(dir_s4d2, dir_s1d3)) 5471 { 5472 TH_LOG("Failed to rename %s back to %s: %s", dir_s4d1, dir_s1d3, 5473 strerror(errno)); 5474 } 5475 5476 /* Now checks that we can access it under l2. */ 5477 child_pid = fork(); 5478 ASSERT_LE(0, child_pid); 5479 if (child_pid == 0) { 5480 enforce_ruleset(_metadata, ruleset_fd_l2); 5481 EXPECT_EQ(0, close(ruleset_fd_l2)); 5482 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5483 EXPECT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 5484 _exit(_metadata->exit_code); 5485 return; 5486 } 5487 5488 EXPECT_EQ(child_pid, waitpid(child_pid, &status, 0)); 5489 EXPECT_EQ(1, WIFEXITED(status)); 5490 EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 5491 5492 /* 5493 * Also test that we can rename via a disconnected path. We move the 5494 * dir back to the disconnected place first, then we rename file1 to 5495 * file2 through our dir fd. 5496 */ 5497 ASSERT_EQ(0, rename(dir_s1d3, dir_s4d2)) 5498 { 5499 TH_LOG("Failed to rename %s to %s: %s", dir_s1d3, dir_s4d2, 5500 strerror(errno)); 5501 } 5502 ASSERT_EQ(0, 5503 renameat(bind_s1d3_fd, file1_name, bind_s1d3_fd, file2_name)) 5504 { 5505 TH_LOG("Failed to rename %s to %s within disconnected %s: %s", 5506 file1_name, file2_name, bind_dir_s1d3, strerror(errno)); 5507 } 5508 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file2_name, O_RDONLY)); 5509 ASSERT_EQ(0, renameat(bind_s1d3_fd, file2_name, AT_FDCWD, file1_s2d2)) 5510 { 5511 TH_LOG("Failed to rename %s to %s through disconnected %s: %s", 5512 file2_name, file1_s2d2, bind_dir_s1d3, strerror(errno)); 5513 } 5514 EXPECT_EQ(0, test_open(file1_s2d2, O_RDONLY)); 5515 EXPECT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 5516 5517 /* Move it back using the disconnected path as the target. */ 5518 ASSERT_EQ(0, renameat(AT_FDCWD, file1_s2d2, bind_s1d3_fd, file1_name)) 5519 { 5520 TH_LOG("Failed to rename %s to %s through disconnected %s: %s", 5521 file1_s1d2, file1_name, bind_dir_s1d3, strerror(errno)); 5522 } 5523 5524 /* Now make it connected again. */ 5525 ASSERT_EQ(0, rename(dir_s4d2, dir_s1d3)) 5526 { 5527 TH_LOG("Failed to rename %s back to %s: %s", dir_s4d2, dir_s1d3, 5528 strerror(errno)); 5529 } 5530 5531 /* Checks again that we can access it under l2. */ 5532 enforce_ruleset(_metadata, ruleset_fd_l2); 5533 EXPECT_EQ(0, close(ruleset_fd_l2)); 5534 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5535 EXPECT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 5536 } 5537 5538 /* 5539 * Test that linkat(2) with disconnected paths works under Landlock. This 5540 * test moves s1d3 to s4d1. 5541 */ 5542 TEST_F_FORK(layout1_bind, path_disconnected_link) 5543 { 5544 /* Ruleset to be applied after renaming s1d3 to s4d1. */ 5545 const struct rule layer1[] = { 5546 { 5547 .path = dir_s4d1, 5548 .access = LANDLOCK_ACCESS_FS_REFER | 5549 LANDLOCK_ACCESS_FS_READ_FILE | 5550 LANDLOCK_ACCESS_FS_MAKE_REG | 5551 LANDLOCK_ACCESS_FS_REMOVE_FILE, 5552 }, 5553 { 5554 .path = dir_s2d2, 5555 .access = LANDLOCK_ACCESS_FS_REFER | 5556 LANDLOCK_ACCESS_FS_READ_FILE | 5557 LANDLOCK_ACCESS_FS_MAKE_REG | 5558 LANDLOCK_ACCESS_FS_REMOVE_FILE, 5559 }, 5560 {} 5561 }; 5562 int bind_s1d3_fd; 5563 5564 /* Removes unneeded files created by layout1, otherwise it will EEXIST. */ 5565 ASSERT_EQ(0, unlink(file1_s1d2)); 5566 ASSERT_EQ(0, unlink(file2_s1d3)); 5567 5568 bind_s1d3_fd = open(bind_dir_s1d3, O_PATH | O_CLOEXEC); 5569 ASSERT_LE(0, bind_s1d3_fd); 5570 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)); 5571 5572 /* Disconnects bind_s1d3_fd. */ 5573 ASSERT_EQ(0, rename(dir_s1d3, dir_s4d1)) 5574 { 5575 TH_LOG("Failed to rename %s to %s: %s", dir_s1d3, dir_s4d1, 5576 strerror(errno)); 5577 } 5578 5579 /* Need this later to test different parent link. */ 5580 ASSERT_EQ(0, mkdir(dir_s4d2, 0755)) 5581 { 5582 TH_LOG("Failed to create %s: %s", dir_s4d2, strerror(errno)); 5583 } 5584 5585 enforce_fs(_metadata, ACCESS_ALL, layer1); 5586 5587 /* From disconnected to connected. */ 5588 ASSERT_EQ(0, linkat(bind_s1d3_fd, file1_name, AT_FDCWD, file1_s2d2, 0)) 5589 { 5590 TH_LOG("Failed to link %s to %s via disconnected %s: %s", 5591 file1_name, file1_s2d2, bind_dir_s1d3, strerror(errno)); 5592 } 5593 5594 /* Tests that we can access via the new link... */ 5595 EXPECT_EQ(0, test_open(file1_s2d2, O_RDONLY)) 5596 { 5597 TH_LOG("Failed to open newly linked %s: %s", file1_s2d2, 5598 strerror(errno)); 5599 } 5600 5601 /* ...as well as the old one. */ 5602 EXPECT_EQ(0, test_open(file1_s4d1, O_RDONLY)) 5603 { 5604 TH_LOG("Failed to open original %s: %s", file1_s4d1, 5605 strerror(errno)); 5606 } 5607 5608 /* From connected to disconnected. */ 5609 ASSERT_EQ(0, unlink(file1_s4d1)); 5610 ASSERT_EQ(0, linkat(AT_FDCWD, file1_s2d2, bind_s1d3_fd, file2_name, 0)) 5611 { 5612 TH_LOG("Failed to link %s to %s via disconnected %s: %s", 5613 file1_s2d2, file2_name, bind_dir_s1d3, strerror(errno)); 5614 } 5615 EXPECT_EQ(0, test_open(file2_s4d1, O_RDONLY)); 5616 ASSERT_EQ(0, unlink(file1_s2d2)); 5617 5618 /* From disconnected to disconnected (same parent). */ 5619 ASSERT_EQ(0, 5620 linkat(bind_s1d3_fd, file2_name, bind_s1d3_fd, file1_name, 0)) 5621 { 5622 TH_LOG("Failed to link %s to %s within disconnected %s: %s", 5623 file2_name, file1_name, bind_dir_s1d3, strerror(errno)); 5624 } 5625 EXPECT_EQ(0, test_open(file1_s4d1, O_RDONLY)) 5626 { 5627 TH_LOG("Failed to open newly linked %s: %s", file1_s4d1, 5628 strerror(errno)); 5629 } 5630 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, file1_name, O_RDONLY)) 5631 { 5632 TH_LOG("Failed to open %s through newly created link under disconnected path: %s", 5633 file1_name, strerror(errno)); 5634 } 5635 ASSERT_EQ(0, unlink(file2_s4d1)); 5636 5637 /* From disconnected to disconnected (different parent). */ 5638 ASSERT_EQ(0, 5639 linkat(bind_s1d3_fd, file1_name, bind_s1d3_fd, "s4d2/f1", 0)) 5640 { 5641 TH_LOG("Failed to link %s to %s within disconnected %s: %s", 5642 file1_name, "s4d2/f1", bind_dir_s1d3, strerror(errno)); 5643 } 5644 EXPECT_EQ(0, test_open(file1_s4d2, O_RDONLY)) 5645 { 5646 TH_LOG("Failed to open %s after link: %s", file1_s4d2, 5647 strerror(errno)); 5648 } 5649 EXPECT_EQ(0, test_open_rel(bind_s1d3_fd, "s4d2/f1", O_RDONLY)) 5650 { 5651 TH_LOG("Failed to open %s through disconnected path after link: %s", 5652 "s4d2/f1", strerror(errno)); 5653 } 5654 } 5655 5656 /* 5657 * layout4_disconnected_leafs with bind mount and renames: 5658 * 5659 * tmp 5660 * ├── s1d1 5661 * │ └── s1d2 [source of the bind mount] 5662 * │ ├── s1d31 5663 * │ │ └── s1d41 [now renamed beneath s3d1] 5664 * │ │ ├── f1 5665 * │ │ └── f2 5666 * │ └── s1d32 5667 * │ └── s1d42 [now renamed beneath s4d1] 5668 * │ ├── f3 5669 * │ └── f4 5670 * ├── s2d1 5671 * │ └── s2d2 [bind mount of s1d2] 5672 * │ ├── s1d31 5673 * │ │ └── s1d41 [opened FD, now renamed beneath s3d1] 5674 * │ │ ├── f1 5675 * │ │ └── f2 5676 * │ └── s1d32 5677 * │ └── s1d42 [opened FD, now renamed beneath s4d1] 5678 * │ ├── f3 5679 * │ └── f4 5680 * ├── s3d1 5681 * │ └── s1d41 [renamed here] 5682 * │ ├── f1 5683 * │ └── f2 5684 * └── s4d1 5685 * └── s1d42 [renamed here] 5686 * ├── f3 5687 * └── f4 5688 */ 5689 /* clang-format off */ 5690 FIXTURE(layout4_disconnected_leafs) { 5691 int s2d2_fd; 5692 }; 5693 /* clang-format on */ 5694 5695 FIXTURE_SETUP(layout4_disconnected_leafs) 5696 { 5697 prepare_layout(_metadata); 5698 5699 create_file(_metadata, TMP_DIR "/s1d1/s1d2/s1d31/s1d41/f1"); 5700 create_file(_metadata, TMP_DIR "/s1d1/s1d2/s1d31/s1d41/f2"); 5701 create_file(_metadata, TMP_DIR "/s1d1/s1d2/s1d32/s1d42/f3"); 5702 create_file(_metadata, TMP_DIR "/s1d1/s1d2/s1d32/s1d42/f4"); 5703 create_directory(_metadata, TMP_DIR "/s2d1/s2d2"); 5704 create_directory(_metadata, TMP_DIR "/s3d1"); 5705 create_directory(_metadata, TMP_DIR "/s4d1"); 5706 5707 self->s2d2_fd = 5708 open(TMP_DIR "/s2d1/s2d2", O_DIRECTORY | O_PATH | O_CLOEXEC); 5709 ASSERT_LE(0, self->s2d2_fd); 5710 5711 set_cap(_metadata, CAP_SYS_ADMIN); 5712 ASSERT_EQ(0, mount(TMP_DIR "/s1d1/s1d2", TMP_DIR "/s2d1/s2d2", NULL, 5713 MS_BIND, NULL)); 5714 clear_cap(_metadata, CAP_SYS_ADMIN); 5715 } 5716 5717 FIXTURE_TEARDOWN_PARENT(layout4_disconnected_leafs) 5718 { 5719 /* umount(TMP_DIR "/s2d1") is handled by namespace lifetime. */ 5720 5721 /* Removes files after renames. */ 5722 remove_path(TMP_DIR "/s3d1/s1d41/f1"); 5723 remove_path(TMP_DIR "/s3d1/s1d41/f2"); 5724 remove_path(TMP_DIR "/s4d1/s1d42/f1"); 5725 remove_path(TMP_DIR "/s4d1/s1d42/f3"); 5726 remove_path(TMP_DIR "/s4d1/s1d42/f4"); 5727 remove_path(TMP_DIR "/s4d1/s1d42/f5"); 5728 5729 cleanup_layout(_metadata); 5730 } 5731 5732 FIXTURE_VARIANT(layout4_disconnected_leafs) 5733 { 5734 /* 5735 * Parent of the bind mount source. It should always be ignored when 5736 * testing against files under the s1d41 or s1d42 disconnected directories. 5737 */ 5738 const __u64 allowed_s1d1; 5739 /* 5740 * Source of bind mount (to s2d2). It should always be enforced when 5741 * testing against files under the s1d41 or s1d42 disconnected directories. 5742 */ 5743 const __u64 allowed_s1d2; 5744 /* 5745 * Original parent of s1d41. It should always be ignored when testing 5746 * against files under the s1d41 disconnected directory. 5747 */ 5748 const __u64 allowed_s1d31; 5749 /* 5750 * Original parent of s1d42. It should always be ignored when testing 5751 * against files under the s1d42 disconnected directory. 5752 */ 5753 const __u64 allowed_s1d32; 5754 /* 5755 * Opened and disconnected source directory. It should always be enforced 5756 * when testing against files under the s1d41 disconnected directory. 5757 */ 5758 const __u64 allowed_s1d41; 5759 /* 5760 * Opened and disconnected source directory. It should always be enforced 5761 * when testing against files under the s1d42 disconnected directory. 5762 */ 5763 const __u64 allowed_s1d42; 5764 /* 5765 * File in the s1d41 disconnected directory. It should always be enforced 5766 * when testing against itself under the s1d41 disconnected directory. 5767 */ 5768 const __u64 allowed_f1; 5769 /* 5770 * File in the s1d41 disconnected directory. It should always be enforced 5771 * when testing against itself under the s1d41 disconnected directory. 5772 */ 5773 const __u64 allowed_f2; 5774 /* 5775 * File in the s1d42 disconnected directory. It should always be enforced 5776 * when testing against itself under the s1d42 disconnected directory. 5777 */ 5778 const __u64 allowed_f3; 5779 /* 5780 * Parent of the bind mount destination. It should always be enforced when 5781 * testing against files under the s1d41 or s1d42 disconnected directories. 5782 */ 5783 const __u64 allowed_s2d1; 5784 /* 5785 * Directory covered by the bind mount. It should always be ignored when 5786 * testing against files under the s1d41 or s1d42 disconnected directories. 5787 */ 5788 const __u64 allowed_s2d2; 5789 /* 5790 * New parent of the renamed s1d41. It should always be ignored when 5791 * testing against files under the s1d41 disconnected directory. 5792 */ 5793 const __u64 allowed_s3d1; 5794 /* 5795 * New parent of the renamed s1d42. It should always be ignored when 5796 * testing against files under the s1d42 disconnected directory. 5797 */ 5798 const __u64 allowed_s4d1; 5799 5800 /* Expected result of the call to open([fd:s1d41]/f1, O_RDONLY). */ 5801 const int expected_read_result; 5802 /* Expected result of the call to renameat([fd:s1d41]/f1, [fd:s1d42]/f1). */ 5803 const int expected_rename_result; 5804 /* 5805 * Expected result of the call to renameat([fd:s1d41]/f2, [fd:s1d42]/f3, 5806 * RENAME_EXCHANGE). 5807 */ 5808 const int expected_exchange_result; 5809 /* Expected result of the call to renameat([fd:s1d42]/f4, [fd:s1d42]/f5). */ 5810 const int expected_same_dir_rename_result; 5811 }; 5812 5813 /* clang-format off */ 5814 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d1_mount_src_parent) { 5815 /* clang-format on */ 5816 .allowed_s1d1 = LANDLOCK_ACCESS_FS_REFER | 5817 LANDLOCK_ACCESS_FS_READ_FILE | 5818 LANDLOCK_ACCESS_FS_EXECUTE | 5819 LANDLOCK_ACCESS_FS_MAKE_REG, 5820 .expected_read_result = EACCES, 5821 .expected_same_dir_rename_result = EACCES, 5822 .expected_rename_result = EACCES, 5823 .expected_exchange_result = EACCES, 5824 }; 5825 5826 /* clang-format off */ 5827 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d2_mount_src_refer) { 5828 /* clang-format on */ 5829 .allowed_s1d2 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 5830 .expected_read_result = 0, 5831 .expected_same_dir_rename_result = EACCES, 5832 .expected_rename_result = EACCES, 5833 .expected_exchange_result = EACCES, 5834 }; 5835 5836 /* clang-format off */ 5837 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d2_mount_src_create) { 5838 /* clang-format on */ 5839 .allowed_s1d2 = LANDLOCK_ACCESS_FS_READ_FILE | 5840 LANDLOCK_ACCESS_FS_MAKE_REG, 5841 .expected_read_result = 0, 5842 .expected_same_dir_rename_result = 0, 5843 .expected_rename_result = EXDEV, 5844 .expected_exchange_result = EXDEV, 5845 }; 5846 5847 /* clang-format off */ 5848 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d2_mount_src_rename) { 5849 /* clang-format on */ 5850 .allowed_s1d2 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 5851 .expected_read_result = EACCES, 5852 .expected_same_dir_rename_result = 0, 5853 .expected_rename_result = 0, 5854 .expected_exchange_result = 0, 5855 }; 5856 5857 /* clang-format off */ 5858 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d31_s1d32_old_parent) { 5859 /* clang-format on */ 5860 .allowed_s1d31 = LANDLOCK_ACCESS_FS_REFER | 5861 LANDLOCK_ACCESS_FS_READ_FILE | 5862 LANDLOCK_ACCESS_FS_EXECUTE | 5863 LANDLOCK_ACCESS_FS_MAKE_REG, 5864 .allowed_s1d32 = LANDLOCK_ACCESS_FS_REFER | 5865 LANDLOCK_ACCESS_FS_READ_FILE | 5866 LANDLOCK_ACCESS_FS_EXECUTE | 5867 LANDLOCK_ACCESS_FS_MAKE_REG, 5868 .expected_read_result = EACCES, 5869 .expected_same_dir_rename_result = EACCES, 5870 .expected_rename_result = EACCES, 5871 .expected_exchange_result = EACCES, 5872 }; 5873 5874 /* clang-format off */ 5875 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d41_s1d42_disconnected_refer) { 5876 /* clang-format on */ 5877 .allowed_s1d41 = LANDLOCK_ACCESS_FS_REFER | 5878 LANDLOCK_ACCESS_FS_READ_FILE, 5879 .allowed_s1d42 = LANDLOCK_ACCESS_FS_REFER | 5880 LANDLOCK_ACCESS_FS_READ_FILE, 5881 .expected_read_result = 0, 5882 .expected_same_dir_rename_result = EACCES, 5883 .expected_rename_result = EACCES, 5884 .expected_exchange_result = EACCES, 5885 }; 5886 5887 /* clang-format off */ 5888 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d41_s1d42_disconnected_create) { 5889 /* clang-format on */ 5890 .allowed_s1d41 = LANDLOCK_ACCESS_FS_READ_FILE | 5891 LANDLOCK_ACCESS_FS_MAKE_REG, 5892 .allowed_s1d42 = LANDLOCK_ACCESS_FS_READ_FILE | 5893 LANDLOCK_ACCESS_FS_MAKE_REG, 5894 .expected_read_result = 0, 5895 .expected_same_dir_rename_result = 0, 5896 .expected_rename_result = EXDEV, 5897 .expected_exchange_result = EXDEV, 5898 }; 5899 5900 /* clang-format off */ 5901 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d41_s1d42_disconnected_rename_even) { 5902 /* clang-format on */ 5903 .allowed_s1d41 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 5904 .allowed_s1d42 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 5905 .expected_read_result = EACCES, 5906 .expected_same_dir_rename_result = 0, 5907 .expected_rename_result = 0, 5908 .expected_exchange_result = 0, 5909 }; 5910 5911 /* The destination directory has more access right. */ 5912 /* clang-format off */ 5913 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d41_s1d42_disconnected_rename_more) { 5914 /* clang-format on */ 5915 .allowed_s1d41 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 5916 .allowed_s1d42 = LANDLOCK_ACCESS_FS_REFER | 5917 LANDLOCK_ACCESS_FS_MAKE_REG | 5918 LANDLOCK_ACCESS_FS_EXECUTE, 5919 .expected_read_result = EACCES, 5920 .expected_same_dir_rename_result = 0, 5921 /* Access denied. */ 5922 .expected_rename_result = EXDEV, 5923 .expected_exchange_result = EXDEV, 5924 }; 5925 5926 /* The destination directory has less access right. */ 5927 /* clang-format off */ 5928 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s1d41_s1d42_disconnected_rename_less) { 5929 /* clang-format on */ 5930 .allowed_s1d41 = LANDLOCK_ACCESS_FS_REFER | 5931 LANDLOCK_ACCESS_FS_MAKE_REG | 5932 LANDLOCK_ACCESS_FS_EXECUTE, 5933 .allowed_s1d42 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 5934 .expected_read_result = EACCES, 5935 .expected_same_dir_rename_result = 0, 5936 /* Access allowed. */ 5937 .expected_rename_result = 0, 5938 .expected_exchange_result = EXDEV, 5939 }; 5940 5941 /* clang-format off */ 5942 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s2d1_mount_dst_parent_create) { 5943 /* clang-format on */ 5944 .allowed_s2d1 = LANDLOCK_ACCESS_FS_READ_FILE | 5945 LANDLOCK_ACCESS_FS_MAKE_REG, 5946 .expected_read_result = 0, 5947 .expected_same_dir_rename_result = 0, 5948 .expected_rename_result = EXDEV, 5949 .expected_exchange_result = EXDEV, 5950 }; 5951 5952 /* clang-format off */ 5953 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s2d1_mount_dst_parent_refer) { 5954 /* clang-format on */ 5955 .allowed_s2d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 5956 .expected_read_result = 0, 5957 .expected_same_dir_rename_result = EACCES, 5958 .expected_rename_result = EACCES, 5959 .expected_exchange_result = EACCES, 5960 }; 5961 5962 /* clang-format off */ 5963 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s2d1_mount_dst_parent_mini) { 5964 /* clang-format on */ 5965 .allowed_s2d1 = LANDLOCK_ACCESS_FS_REFER | 5966 LANDLOCK_ACCESS_FS_READ_FILE | 5967 LANDLOCK_ACCESS_FS_MAKE_REG, 5968 .expected_read_result = 0, 5969 .expected_same_dir_rename_result = 0, 5970 .expected_rename_result = 0, 5971 .expected_exchange_result = 0, 5972 }; 5973 5974 /* clang-format off */ 5975 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s2d2_covered_by_mount) { 5976 /* clang-format on */ 5977 .allowed_s2d2 = LANDLOCK_ACCESS_FS_REFER | 5978 LANDLOCK_ACCESS_FS_READ_FILE | 5979 LANDLOCK_ACCESS_FS_EXECUTE | 5980 LANDLOCK_ACCESS_FS_MAKE_REG, 5981 .expected_read_result = EACCES, 5982 .expected_same_dir_rename_result = EACCES, 5983 .expected_rename_result = EACCES, 5984 .expected_exchange_result = EACCES, 5985 }; 5986 5987 /* Tests collect_domain_accesses(). */ 5988 /* clang-format off */ 5989 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s3d1_s4d1_new_parent_refer) { 5990 /* clang-format on */ 5991 .allowed_s3d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 5992 .allowed_s4d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 5993 .expected_read_result = 0, 5994 .expected_same_dir_rename_result = EACCES, 5995 .expected_rename_result = EACCES, 5996 .expected_exchange_result = EACCES, 5997 }; 5998 5999 /* clang-format off */ 6000 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s3d1_s4d1_new_parent_create) { 6001 /* clang-format on */ 6002 .allowed_s3d1 = LANDLOCK_ACCESS_FS_READ_FILE | 6003 LANDLOCK_ACCESS_FS_MAKE_REG, 6004 .allowed_s4d1 = LANDLOCK_ACCESS_FS_READ_FILE | 6005 LANDLOCK_ACCESS_FS_MAKE_REG, 6006 .expected_read_result = 0, 6007 .expected_same_dir_rename_result = 0, 6008 .expected_rename_result = EXDEV, 6009 .expected_exchange_result = EXDEV, 6010 }; 6011 6012 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, 6013 s3d1_s4d1_disconnected_rename_even){ 6014 /* clang-format on */ 6015 .allowed_s3d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6016 .allowed_s4d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6017 .expected_read_result = EACCES, 6018 .expected_same_dir_rename_result = 0, 6019 .expected_rename_result = 0, 6020 .expected_exchange_result = 0, 6021 }; 6022 6023 /* The destination directory has more access right. */ 6024 /* clang-format off */ 6025 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s3d1_s4d1_disconnected_rename_more) { 6026 /* clang-format on */ 6027 .allowed_s3d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6028 .allowed_s4d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG | 6029 LANDLOCK_ACCESS_FS_EXECUTE, 6030 .expected_read_result = EACCES, 6031 .expected_same_dir_rename_result = 0, 6032 /* Access denied. */ 6033 .expected_rename_result = EXDEV, 6034 .expected_exchange_result = EXDEV, 6035 }; 6036 6037 /* The destination directory has less access right. */ 6038 /* clang-format off */ 6039 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, s3d1_s4d1_disconnected_rename_less) { 6040 /* clang-format on */ 6041 .allowed_s3d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG | 6042 LANDLOCK_ACCESS_FS_EXECUTE, 6043 .allowed_s4d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6044 .expected_read_result = EACCES, 6045 .expected_same_dir_rename_result = 0, 6046 /* Access allowed. */ 6047 .expected_rename_result = 0, 6048 .expected_exchange_result = EXDEV, 6049 }; 6050 6051 /* clang-format off */ 6052 FIXTURE_VARIANT_ADD(layout4_disconnected_leafs, f1_f2_f3) { 6053 /* clang-format on */ 6054 .allowed_f1 = LANDLOCK_ACCESS_FS_READ_FILE, 6055 .allowed_f2 = LANDLOCK_ACCESS_FS_READ_FILE, 6056 .allowed_f3 = LANDLOCK_ACCESS_FS_READ_FILE, 6057 .expected_read_result = 0, 6058 .expected_same_dir_rename_result = EACCES, 6059 .expected_rename_result = EACCES, 6060 .expected_exchange_result = EACCES, 6061 }; 6062 6063 TEST_F_FORK(layout4_disconnected_leafs, read_rename_exchange) 6064 { 6065 const __u64 handled_access = 6066 LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE | 6067 LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_MAKE_REG; 6068 const struct rule rules[] = { 6069 { 6070 .path = TMP_DIR "/s1d1", 6071 .access = variant->allowed_s1d1, 6072 }, 6073 { 6074 .path = TMP_DIR "/s1d1/s1d2", 6075 .access = variant->allowed_s1d2, 6076 }, 6077 { 6078 .path = TMP_DIR "/s1d1/s1d2/s1d31", 6079 .access = variant->allowed_s1d31, 6080 }, 6081 { 6082 .path = TMP_DIR "/s1d1/s1d2/s1d32", 6083 .access = variant->allowed_s1d32, 6084 }, 6085 { 6086 .path = TMP_DIR "/s1d1/s1d2/s1d31/s1d41", 6087 .access = variant->allowed_s1d41, 6088 }, 6089 { 6090 .path = TMP_DIR "/s1d1/s1d2/s1d32/s1d42", 6091 .access = variant->allowed_s1d42, 6092 }, 6093 { 6094 .path = TMP_DIR "/s1d1/s1d2/s1d31/s1d41/f1", 6095 .access = variant->allowed_f1, 6096 }, 6097 { 6098 .path = TMP_DIR "/s1d1/s1d2/s1d31/s1d41/f2", 6099 .access = variant->allowed_f2, 6100 }, 6101 { 6102 .path = TMP_DIR "/s1d1/s1d2/s1d32/s1d42/f3", 6103 .access = variant->allowed_f3, 6104 }, 6105 { 6106 .path = TMP_DIR "/s2d1", 6107 .access = variant->allowed_s2d1, 6108 }, 6109 /* s2d2_fd */ 6110 { 6111 .path = TMP_DIR "/s3d1", 6112 .access = variant->allowed_s3d1, 6113 }, 6114 { 6115 .path = TMP_DIR "/s4d1", 6116 .access = variant->allowed_s4d1, 6117 }, 6118 {}, 6119 }; 6120 int ruleset_fd, s1d41_bind_fd, s1d42_bind_fd; 6121 6122 ruleset_fd = create_ruleset(_metadata, handled_access, rules); 6123 6124 /* Adds rule for the covered directory. */ 6125 if (variant->allowed_s2d2) { 6126 ASSERT_EQ(0, landlock_add_rule( 6127 ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 6128 &(struct landlock_path_beneath_attr){ 6129 .parent_fd = self->s2d2_fd, 6130 .allowed_access = 6131 variant->allowed_s2d2, 6132 }, 6133 0)); 6134 } 6135 EXPECT_EQ(0, close(self->s2d2_fd)); 6136 6137 s1d41_bind_fd = open(TMP_DIR "/s2d1/s2d2/s1d31/s1d41", 6138 O_DIRECTORY | O_PATH | O_CLOEXEC); 6139 ASSERT_LE(0, s1d41_bind_fd); 6140 s1d42_bind_fd = open(TMP_DIR "/s2d1/s2d2/s1d32/s1d42", 6141 O_DIRECTORY | O_PATH | O_CLOEXEC); 6142 ASSERT_LE(0, s1d42_bind_fd); 6143 6144 /* Disconnects and checks source and destination directories. */ 6145 EXPECT_EQ(0, test_open_rel(s1d41_bind_fd, "..", O_DIRECTORY)); 6146 EXPECT_EQ(0, test_open_rel(s1d42_bind_fd, "..", O_DIRECTORY)); 6147 /* Renames to make it accessible through s3d1/s1d41 */ 6148 ASSERT_EQ(0, test_renameat(AT_FDCWD, TMP_DIR "/s1d1/s1d2/s1d31/s1d41", 6149 AT_FDCWD, TMP_DIR "/s3d1/s1d41")); 6150 /* Renames to make it accessible through s4d1/s1d42 */ 6151 ASSERT_EQ(0, test_renameat(AT_FDCWD, TMP_DIR "/s1d1/s1d2/s1d32/s1d42", 6152 AT_FDCWD, TMP_DIR "/s4d1/s1d42")); 6153 EXPECT_EQ(ENOENT, test_open_rel(s1d41_bind_fd, "..", O_DIRECTORY)); 6154 EXPECT_EQ(ENOENT, test_open_rel(s1d42_bind_fd, "..", O_DIRECTORY)); 6155 6156 enforce_ruleset(_metadata, ruleset_fd); 6157 EXPECT_EQ(0, close(ruleset_fd)); 6158 6159 EXPECT_EQ(variant->expected_read_result, 6160 test_open_rel(s1d41_bind_fd, "f1", O_RDONLY)); 6161 6162 EXPECT_EQ(variant->expected_rename_result, 6163 test_renameat(s1d41_bind_fd, "f1", s1d42_bind_fd, "f1")); 6164 EXPECT_EQ(variant->expected_exchange_result, 6165 test_exchangeat(s1d41_bind_fd, "f2", s1d42_bind_fd, "f3")); 6166 6167 EXPECT_EQ(variant->expected_same_dir_rename_result, 6168 test_renameat(s1d42_bind_fd, "f4", s1d42_bind_fd, "f5")); 6169 } 6170 6171 /* 6172 * layout5_disconnected_branch before rename: 6173 * 6174 * tmp 6175 * ├── s1d1 6176 * │ └── s1d2 [source of the first bind mount] 6177 * │ └── s1d3 6178 * │ ├── s1d41 6179 * │ │ ├── f1 6180 * │ │ └── f2 6181 * │ └── s1d42 6182 * │ ├── f3 6183 * │ └── f4 6184 * ├── s2d1 6185 * │ └── s2d2 [source of the second bind mount] 6186 * │ └── s2d3 6187 * │ └── s2d4 [first s1d2 bind mount] 6188 * │ └── s1d3 6189 * │ ├── s1d41 6190 * │ │ ├── f1 6191 * │ │ └── f2 6192 * │ └── s1d42 6193 * │ ├── f3 6194 * │ └── f4 6195 * ├── s3d1 6196 * │ └── s3d2 [second s2d2 bind mount] 6197 * │ └── s2d3 6198 * │ └── s2d4 [first s1d2 bind mount] 6199 * │ └── s1d3 6200 * │ ├── s1d41 6201 * │ │ ├── f1 6202 * │ │ └── f2 6203 * │ └── s1d42 6204 * │ ├── f3 6205 * │ └── f4 6206 * └── s4d1 6207 * 6208 * After rename: 6209 * 6210 * tmp 6211 * ├── s1d1 6212 * │ └── s1d2 [source of the first bind mount] 6213 * │ └── s1d3 6214 * │ ├── s1d41 6215 * │ │ ├── f1 6216 * │ │ └── f2 6217 * │ └── s1d42 6218 * │ ├── f3 6219 * │ └── f4 6220 * ├── s2d1 6221 * │ └── s2d2 [source of the second bind mount] 6222 * ├── s3d1 6223 * │ └── s3d2 [second s2d2 bind mount] 6224 * └── s4d1 6225 * └── s2d3 [renamed here] 6226 * └── s2d4 [first s1d2 bind mount] 6227 * └── s1d3 6228 * ├── s1d41 6229 * │ ├── f1 6230 * │ └── f2 6231 * └── s1d42 6232 * ├── f3 6233 * └── f4 6234 * 6235 * Decision path for access from the s3d1/s3d2/s2d3/s2d4/s1d3 file descriptor: 6236 * 1. first bind mount: s1d3 -> s1d2 6237 * 2. second bind mount: s2d3 6238 * 3. tmp mount: s4d1 -> tmp [disconnected branch] 6239 * 4. second bind mount: s2d2 6240 * 5. tmp mount: s3d1 -> tmp 6241 * 6. parent mounts: [...] -> / 6242 * 6243 * The s4d1 directory is evaluated even if it is not in the s2d2 mount. 6244 */ 6245 6246 /* clang-format off */ 6247 FIXTURE(layout5_disconnected_branch) { 6248 int s2d4_fd, s3d2_fd; 6249 }; 6250 /* clang-format on */ 6251 6252 FIXTURE_SETUP(layout5_disconnected_branch) 6253 { 6254 prepare_layout(_metadata); 6255 6256 create_file(_metadata, TMP_DIR "/s1d1/s1d2/s1d3/s1d41/f1"); 6257 create_file(_metadata, TMP_DIR "/s1d1/s1d2/s1d3/s1d41/f2"); 6258 create_file(_metadata, TMP_DIR "/s1d1/s1d2/s1d3/s1d42/f3"); 6259 create_file(_metadata, TMP_DIR "/s1d1/s1d2/s1d3/s1d42/f4"); 6260 create_directory(_metadata, TMP_DIR "/s2d1/s2d2/s2d3/s2d4"); 6261 create_directory(_metadata, TMP_DIR "/s3d1/s3d2"); 6262 create_directory(_metadata, TMP_DIR "/s4d1"); 6263 6264 self->s2d4_fd = open(TMP_DIR "/s2d1/s2d2/s2d3/s2d4", 6265 O_DIRECTORY | O_PATH | O_CLOEXEC); 6266 ASSERT_LE(0, self->s2d4_fd); 6267 6268 self->s3d2_fd = 6269 open(TMP_DIR "/s3d1/s3d2", O_DIRECTORY | O_PATH | O_CLOEXEC); 6270 ASSERT_LE(0, self->s3d2_fd); 6271 6272 set_cap(_metadata, CAP_SYS_ADMIN); 6273 ASSERT_EQ(0, mount(TMP_DIR "/s1d1/s1d2", TMP_DIR "/s2d1/s2d2/s2d3/s2d4", 6274 NULL, MS_BIND, NULL)); 6275 ASSERT_EQ(0, mount(TMP_DIR "/s2d1/s2d2", TMP_DIR "/s3d1/s3d2", NULL, 6276 MS_BIND | MS_REC, NULL)); 6277 clear_cap(_metadata, CAP_SYS_ADMIN); 6278 } 6279 6280 FIXTURE_TEARDOWN_PARENT(layout5_disconnected_branch) 6281 { 6282 /* Bind mounts are handled by namespace lifetime. */ 6283 6284 /* Removes files after renames. */ 6285 remove_path(TMP_DIR "/s1d1/s1d2/s1d3/s1d41/f1"); 6286 remove_path(TMP_DIR "/s1d1/s1d2/s1d3/s1d41/f2"); 6287 remove_path(TMP_DIR "/s1d1/s1d2/s1d3/s1d42/f1"); 6288 remove_path(TMP_DIR "/s1d1/s1d2/s1d3/s1d42/f3"); 6289 remove_path(TMP_DIR "/s1d1/s1d2/s1d3/s1d42/f4"); 6290 remove_path(TMP_DIR "/s1d1/s1d2/s1d3/s1d42/f5"); 6291 6292 cleanup_layout(_metadata); 6293 } 6294 6295 FIXTURE_VARIANT(layout5_disconnected_branch) 6296 { 6297 /* 6298 * Parent of all files. It should always be enforced when testing against 6299 * files under the s1d41 or s1d42 disconnected directories. 6300 */ 6301 const __u64 allowed_base; 6302 /* 6303 * Parent of the first bind mount source. It should always be ignored when 6304 * testing against files under the s1d41 or s1d42 disconnected directories. 6305 */ 6306 const __u64 allowed_s1d1; 6307 const __u64 allowed_s1d2; 6308 const __u64 allowed_s1d3; 6309 const __u64 allowed_s2d1; 6310 const __u64 allowed_s2d2; 6311 const __u64 allowed_s2d3; 6312 const __u64 allowed_s2d4; 6313 const __u64 allowed_s3d1; 6314 const __u64 allowed_s3d2; 6315 const __u64 allowed_s4d1; 6316 6317 /* Expected result of the call to open([fd:s1d3]/s1d41/f1, O_RDONLY). */ 6318 const int expected_read_result; 6319 /* 6320 * Expected result of the call to renameat([fd:s1d3]/s1d41/f1, 6321 * [fd:s1d3]/s1d42/f1). 6322 */ 6323 const int expected_rename_result; 6324 /* 6325 * Expected result of the call to renameat([fd:s1d3]/s1d41/f2, 6326 * [fd:s1d3]/s1d42/f3, RENAME_EXCHANGE). 6327 */ 6328 const int expected_exchange_result; 6329 /* 6330 * Expected result of the call to renameat([fd:s1d3]/s1d42/f4, 6331 * [fd:s1d3]/s1d42/f5). 6332 */ 6333 const int expected_same_dir_rename_result; 6334 }; 6335 6336 /* clang-format off */ 6337 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s1d1_mount1_src_parent) { 6338 /* clang-format on */ 6339 .allowed_s1d1 = LANDLOCK_ACCESS_FS_REFER | 6340 LANDLOCK_ACCESS_FS_READ_FILE | 6341 LANDLOCK_ACCESS_FS_EXECUTE | 6342 LANDLOCK_ACCESS_FS_MAKE_REG, 6343 .expected_read_result = EACCES, 6344 .expected_same_dir_rename_result = EACCES, 6345 .expected_rename_result = EACCES, 6346 .expected_exchange_result = EACCES, 6347 }; 6348 6349 /* clang-format off */ 6350 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s1d2_mount1_src_refer) { 6351 /* clang-format on */ 6352 .allowed_s1d2 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 6353 .expected_read_result = 0, 6354 .expected_same_dir_rename_result = EACCES, 6355 .expected_rename_result = EACCES, 6356 .expected_exchange_result = EACCES, 6357 }; 6358 6359 /* clang-format off */ 6360 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s1d2_mount1_src_create) { 6361 /* clang-format on */ 6362 .allowed_s1d2 = LANDLOCK_ACCESS_FS_READ_FILE | 6363 LANDLOCK_ACCESS_FS_MAKE_REG, 6364 .expected_read_result = 0, 6365 .expected_same_dir_rename_result = 0, 6366 .expected_rename_result = EXDEV, 6367 .expected_exchange_result = EXDEV, 6368 }; 6369 6370 /* clang-format off */ 6371 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s1d2_mount1_src_rename) { 6372 /* clang-format on */ 6373 .allowed_s1d2 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6374 .expected_read_result = EACCES, 6375 .expected_same_dir_rename_result = 0, 6376 .expected_rename_result = 0, 6377 .expected_exchange_result = 0, 6378 }; 6379 6380 /* clang-format off */ 6381 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s1d3_fd_refer) { 6382 /* clang-format on */ 6383 .allowed_s1d3 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 6384 .expected_read_result = 0, 6385 .expected_same_dir_rename_result = EACCES, 6386 .expected_rename_result = EACCES, 6387 .expected_exchange_result = EACCES, 6388 }; 6389 6390 /* clang-format off */ 6391 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s1d3_fd_create) { 6392 /* clang-format on */ 6393 .allowed_s1d3 = LANDLOCK_ACCESS_FS_READ_FILE | 6394 LANDLOCK_ACCESS_FS_MAKE_REG, 6395 .expected_read_result = 0, 6396 .expected_same_dir_rename_result = 0, 6397 .expected_rename_result = EXDEV, 6398 .expected_exchange_result = EXDEV, 6399 }; 6400 6401 /* clang-format off */ 6402 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s1d3_fd_rename) { 6403 /* clang-format on */ 6404 .allowed_s1d3 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6405 .expected_read_result = EACCES, 6406 .expected_same_dir_rename_result = 0, 6407 .expected_rename_result = 0, 6408 .expected_exchange_result = 0, 6409 }; 6410 6411 /* clang-format off */ 6412 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s1d3_fd_full) { 6413 /* clang-format on */ 6414 .allowed_s1d3 = LANDLOCK_ACCESS_FS_REFER | 6415 LANDLOCK_ACCESS_FS_READ_FILE | 6416 LANDLOCK_ACCESS_FS_EXECUTE | 6417 LANDLOCK_ACCESS_FS_MAKE_REG, 6418 .expected_read_result = 0, 6419 .expected_same_dir_rename_result = 0, 6420 .expected_rename_result = 0, 6421 .expected_exchange_result = 0, 6422 }; 6423 6424 /* clang-format off */ 6425 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s2d1_mount2_src_parent) { 6426 /* clang-format on */ 6427 .allowed_s2d1 = LANDLOCK_ACCESS_FS_REFER | 6428 LANDLOCK_ACCESS_FS_READ_FILE | 6429 LANDLOCK_ACCESS_FS_EXECUTE | 6430 LANDLOCK_ACCESS_FS_MAKE_REG, 6431 .expected_read_result = EACCES, 6432 .expected_same_dir_rename_result = EACCES, 6433 .expected_rename_result = EACCES, 6434 .expected_exchange_result = EACCES, 6435 }; 6436 6437 /* clang-format off */ 6438 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s2d2_mount2_src_refer) { 6439 /* clang-format on */ 6440 .allowed_s2d2 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 6441 .expected_read_result = 0, 6442 .expected_same_dir_rename_result = EACCES, 6443 .expected_rename_result = EACCES, 6444 .expected_exchange_result = EACCES, 6445 }; 6446 6447 /* clang-format off */ 6448 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s2d2_mount2_src_create) { 6449 /* clang-format on */ 6450 .allowed_s2d2 = LANDLOCK_ACCESS_FS_READ_FILE | 6451 LANDLOCK_ACCESS_FS_MAKE_REG, 6452 .expected_read_result = 0, 6453 .expected_same_dir_rename_result = 0, 6454 .expected_rename_result = EXDEV, 6455 .expected_exchange_result = EXDEV, 6456 }; 6457 6458 /* clang-format off */ 6459 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s2d2_mount2_src_rename) { 6460 /* clang-format on */ 6461 .allowed_s2d2 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6462 .expected_read_result = EACCES, 6463 .expected_same_dir_rename_result = 0, 6464 .expected_rename_result = 0, 6465 .expected_exchange_result = 0, 6466 }; 6467 6468 /* clang-format off */ 6469 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s2d3_mount1_dst_parent_refer) { 6470 /* clang-format on */ 6471 .allowed_s2d3 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 6472 .expected_read_result = 0, 6473 .expected_same_dir_rename_result = EACCES, 6474 .expected_rename_result = EACCES, 6475 .expected_exchange_result = EACCES, 6476 }; 6477 6478 /* clang-format off */ 6479 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s2d3_mount1_dst_parent_create) { 6480 /* clang-format on */ 6481 .allowed_s2d3 = LANDLOCK_ACCESS_FS_READ_FILE | 6482 LANDLOCK_ACCESS_FS_MAKE_REG, 6483 .expected_read_result = 0, 6484 .expected_same_dir_rename_result = 0, 6485 .expected_rename_result = EXDEV, 6486 .expected_exchange_result = EXDEV, 6487 }; 6488 6489 /* clang-format off */ 6490 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s2d3_mount1_dst_parent_rename) { 6491 /* clang-format on */ 6492 .allowed_s2d3 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6493 .expected_read_result = EACCES, 6494 .expected_same_dir_rename_result = 0, 6495 .expected_rename_result = 0, 6496 .expected_exchange_result = 0, 6497 }; 6498 6499 /* clang-format off */ 6500 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s2d4_mount1_dst) { 6501 /* clang-format on */ 6502 .allowed_s2d4 = LANDLOCK_ACCESS_FS_REFER | 6503 LANDLOCK_ACCESS_FS_READ_FILE | 6504 LANDLOCK_ACCESS_FS_EXECUTE | 6505 LANDLOCK_ACCESS_FS_MAKE_REG, 6506 .expected_read_result = EACCES, 6507 .expected_same_dir_rename_result = EACCES, 6508 .expected_rename_result = EACCES, 6509 .expected_exchange_result = EACCES, 6510 }; 6511 6512 /* clang-format off */ 6513 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s3d1_mount2_dst_parent_refer) { 6514 /* clang-format on */ 6515 .allowed_s3d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 6516 .expected_read_result = 0, 6517 .expected_same_dir_rename_result = EACCES, 6518 .expected_rename_result = EACCES, 6519 .expected_exchange_result = EACCES, 6520 }; 6521 6522 /* clang-format off */ 6523 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s3d1_mount2_dst_parent_create) { 6524 /* clang-format on */ 6525 .allowed_s3d1 = LANDLOCK_ACCESS_FS_READ_FILE | 6526 LANDLOCK_ACCESS_FS_MAKE_REG, 6527 .expected_read_result = 0, 6528 .expected_same_dir_rename_result = 0, 6529 .expected_rename_result = EXDEV, 6530 .expected_exchange_result = EXDEV, 6531 }; 6532 6533 /* clang-format off */ 6534 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s3d1_mount2_dst_parent_rename) { 6535 /* clang-format on */ 6536 .allowed_s3d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6537 .expected_read_result = EACCES, 6538 .expected_same_dir_rename_result = 0, 6539 .expected_rename_result = 0, 6540 .expected_exchange_result = 0, 6541 }; 6542 6543 /* clang-format off */ 6544 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s3d2_mount1_dst) { 6545 /* clang-format on */ 6546 .allowed_s3d2 = LANDLOCK_ACCESS_FS_REFER | 6547 LANDLOCK_ACCESS_FS_READ_FILE | 6548 LANDLOCK_ACCESS_FS_EXECUTE | 6549 LANDLOCK_ACCESS_FS_MAKE_REG, 6550 .expected_read_result = EACCES, 6551 .expected_same_dir_rename_result = EACCES, 6552 .expected_rename_result = EACCES, 6553 .expected_exchange_result = EACCES, 6554 }; 6555 6556 /* clang-format off */ 6557 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s4d1_rename_parent_refer) { 6558 /* clang-format on */ 6559 .allowed_s4d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE, 6560 .expected_read_result = 0, 6561 .expected_same_dir_rename_result = EACCES, 6562 .expected_rename_result = EACCES, 6563 .expected_exchange_result = EACCES, 6564 }; 6565 6566 /* clang-format off */ 6567 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s4d1_rename_parent_create) { 6568 /* clang-format on */ 6569 .allowed_s4d1 = LANDLOCK_ACCESS_FS_READ_FILE | 6570 LANDLOCK_ACCESS_FS_MAKE_REG, 6571 .expected_read_result = 0, 6572 .expected_same_dir_rename_result = 0, 6573 .expected_rename_result = EXDEV, 6574 .expected_exchange_result = EXDEV, 6575 }; 6576 6577 /* clang-format off */ 6578 FIXTURE_VARIANT_ADD(layout5_disconnected_branch, s4d1_rename_parent_rename) { 6579 /* clang-format on */ 6580 .allowed_s4d1 = LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_MAKE_REG, 6581 .expected_read_result = EACCES, 6582 .expected_same_dir_rename_result = 0, 6583 .expected_rename_result = 0, 6584 .expected_exchange_result = 0, 6585 }; 6586 6587 TEST_F_FORK(layout5_disconnected_branch, read_rename_exchange) 6588 { 6589 const __u64 handled_access = 6590 LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_READ_FILE | 6591 LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_MAKE_REG; 6592 const struct rule rules[] = { 6593 { 6594 .path = TMP_DIR "/s1d1", 6595 .access = variant->allowed_s1d1, 6596 }, 6597 { 6598 .path = TMP_DIR "/s1d1/s1d2", 6599 .access = variant->allowed_s1d2, 6600 }, 6601 { 6602 .path = TMP_DIR "/s1d1/s1d2/s1d3", 6603 .access = variant->allowed_s1d3, 6604 }, 6605 { 6606 .path = TMP_DIR "/s2d1", 6607 .access = variant->allowed_s2d1, 6608 }, 6609 { 6610 .path = TMP_DIR "/s2d1/s2d2", 6611 .access = variant->allowed_s2d2, 6612 }, 6613 { 6614 .path = TMP_DIR "/s2d1/s2d2/s2d3", 6615 .access = variant->allowed_s2d3, 6616 }, 6617 /* s2d4_fd */ 6618 { 6619 .path = TMP_DIR "/s3d1", 6620 .access = variant->allowed_s3d1, 6621 }, 6622 /* s3d2_fd */ 6623 { 6624 .path = TMP_DIR "/s4d1", 6625 .access = variant->allowed_s4d1, 6626 }, 6627 {}, 6628 }; 6629 int ruleset_fd, s1d3_bind_fd; 6630 6631 ruleset_fd = create_ruleset(_metadata, handled_access, rules); 6632 ASSERT_LE(0, ruleset_fd); 6633 6634 /* Adds rules for the covered directories. */ 6635 if (variant->allowed_s2d4) { 6636 ASSERT_EQ(0, landlock_add_rule( 6637 ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 6638 &(struct landlock_path_beneath_attr){ 6639 .parent_fd = self->s2d4_fd, 6640 .allowed_access = 6641 variant->allowed_s2d4, 6642 }, 6643 0)); 6644 } 6645 EXPECT_EQ(0, close(self->s2d4_fd)); 6646 6647 if (variant->allowed_s3d2) { 6648 ASSERT_EQ(0, landlock_add_rule( 6649 ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 6650 &(struct landlock_path_beneath_attr){ 6651 .parent_fd = self->s3d2_fd, 6652 .allowed_access = 6653 variant->allowed_s3d2, 6654 }, 6655 0)); 6656 } 6657 EXPECT_EQ(0, close(self->s3d2_fd)); 6658 6659 s1d3_bind_fd = open(TMP_DIR "/s3d1/s3d2/s2d3/s2d4/s1d3", 6660 O_DIRECTORY | O_PATH | O_CLOEXEC); 6661 ASSERT_LE(0, s1d3_bind_fd); 6662 6663 /* Disconnects and checks source and destination directories. */ 6664 EXPECT_EQ(0, test_open_rel(s1d3_bind_fd, "..", O_DIRECTORY)); 6665 EXPECT_EQ(0, test_open_rel(s1d3_bind_fd, "../..", O_DIRECTORY)); 6666 /* Renames to make it accessible through s3d1/s1d41 */ 6667 ASSERT_EQ(0, test_renameat(AT_FDCWD, TMP_DIR "/s2d1/s2d2/s2d3", 6668 AT_FDCWD, TMP_DIR "/s4d1/s2d3")); 6669 EXPECT_EQ(0, test_open_rel(s1d3_bind_fd, "..", O_DIRECTORY)); 6670 EXPECT_EQ(ENOENT, test_open_rel(s1d3_bind_fd, "../..", O_DIRECTORY)); 6671 6672 enforce_ruleset(_metadata, ruleset_fd); 6673 EXPECT_EQ(0, close(ruleset_fd)); 6674 6675 EXPECT_EQ(variant->expected_read_result, 6676 test_open_rel(s1d3_bind_fd, "s1d41/f1", O_RDONLY)); 6677 6678 EXPECT_EQ(variant->expected_rename_result, 6679 test_renameat(s1d3_bind_fd, "s1d41/f1", s1d3_bind_fd, 6680 "s1d42/f1")); 6681 EXPECT_EQ(variant->expected_exchange_result, 6682 test_exchangeat(s1d3_bind_fd, "s1d41/f2", s1d3_bind_fd, 6683 "s1d42/f3")); 6684 6685 EXPECT_EQ(variant->expected_same_dir_rename_result, 6686 test_renameat(s1d3_bind_fd, "s1d42/f4", s1d3_bind_fd, 6687 "s1d42/f5")); 6688 } 6689 6690 #define LOWER_BASE TMP_DIR "/lower" 6691 #define LOWER_DATA LOWER_BASE "/data" 6692 static const char lower_fl1[] = LOWER_DATA "/fl1"; 6693 static const char lower_dl1[] = LOWER_DATA "/dl1"; 6694 static const char lower_dl1_fl2[] = LOWER_DATA "/dl1/fl2"; 6695 static const char lower_fo1[] = LOWER_DATA "/fo1"; 6696 static const char lower_do1[] = LOWER_DATA "/do1"; 6697 static const char lower_do1_fo2[] = LOWER_DATA "/do1/fo2"; 6698 static const char lower_do1_fl3[] = LOWER_DATA "/do1/fl3"; 6699 /* lower_pl1 is a FIFO and is deliberately not in the lists below. */ 6700 static const char lower_pl1[] = LOWER_DATA "/pl1"; 6701 6702 static const char (*lower_base_files[])[] = { 6703 &lower_fl1, 6704 &lower_fo1, 6705 NULL, 6706 }; 6707 static const char (*lower_base_directories[])[] = { 6708 &lower_dl1, 6709 &lower_do1, 6710 NULL, 6711 }; 6712 static const char (*lower_sub_files[])[] = { 6713 &lower_dl1_fl2, 6714 &lower_do1_fo2, 6715 &lower_do1_fl3, 6716 NULL, 6717 }; 6718 6719 #define UPPER_BASE TMP_DIR "/upper" 6720 #define UPPER_DATA UPPER_BASE "/data" 6721 #define UPPER_WORK UPPER_BASE "/work" 6722 static const char upper_fu1[] = UPPER_DATA "/fu1"; 6723 static const char upper_du1[] = UPPER_DATA "/du1"; 6724 static const char upper_du1_fu2[] = UPPER_DATA "/du1/fu2"; 6725 static const char upper_fo1[] = UPPER_DATA "/fo1"; 6726 static const char upper_do1[] = UPPER_DATA "/do1"; 6727 static const char upper_do1_fo2[] = UPPER_DATA "/do1/fo2"; 6728 static const char upper_do1_fu3[] = UPPER_DATA "/do1/fu3"; 6729 6730 static const char (*upper_base_files[])[] = { 6731 &upper_fu1, 6732 &upper_fo1, 6733 NULL, 6734 }; 6735 static const char (*upper_base_directories[])[] = { 6736 &upper_du1, 6737 &upper_do1, 6738 NULL, 6739 }; 6740 static const char (*upper_sub_files[])[] = { 6741 &upper_du1_fu2, 6742 &upper_do1_fo2, 6743 &upper_do1_fu3, 6744 NULL, 6745 }; 6746 6747 #define MERGE_BASE TMP_DIR "/merge" 6748 #define MERGE_DATA MERGE_BASE "/data" 6749 static const char merge_fl1[] = MERGE_DATA "/fl1"; 6750 /* merge_pl1 is a FIFO and is deliberately not in the lists below. */ 6751 static const char merge_pl1[] = MERGE_DATA "/pl1"; 6752 static const char merge_dl1[] = MERGE_DATA "/dl1"; 6753 static const char merge_dl1_fl2[] = MERGE_DATA "/dl1/fl2"; 6754 static const char merge_fu1[] = MERGE_DATA "/fu1"; 6755 static const char merge_du1[] = MERGE_DATA "/du1"; 6756 static const char merge_du1_fu2[] = MERGE_DATA "/du1/fu2"; 6757 static const char merge_fo1[] = MERGE_DATA "/fo1"; 6758 static const char merge_do1[] = MERGE_DATA "/do1"; 6759 static const char merge_do1_fo2[] = MERGE_DATA "/do1/fo2"; 6760 static const char merge_do1_fl3[] = MERGE_DATA "/do1/fl3"; 6761 static const char merge_do1_fu3[] = MERGE_DATA "/do1/fu3"; 6762 6763 static const char (*merge_base_files[])[] = { 6764 &merge_fl1, 6765 &merge_fu1, 6766 &merge_fo1, 6767 NULL, 6768 }; 6769 static const char (*merge_base_directories[])[] = { 6770 &merge_dl1, 6771 &merge_du1, 6772 &merge_do1, 6773 NULL, 6774 }; 6775 static const char (*merge_sub_files[])[] = { 6776 &merge_dl1_fl2, &merge_du1_fu2, &merge_do1_fo2, 6777 &merge_do1_fl3, &merge_do1_fu3, NULL, 6778 }; 6779 6780 /* 6781 * layout2_overlay hierarchy: 6782 * 6783 * tmp 6784 * ├── lower 6785 * │ └── data 6786 * │ ├── dl1 6787 * │ │ └── fl2 6788 * │ ├── do1 6789 * │ │ ├── fl3 6790 * │ │ └── fo2 6791 * │ ├── fl1 6792 * │ ├── fo1 6793 * │ └── pl1 [FIFO] 6794 * ├── merge 6795 * │ └── data 6796 * │ ├── dl1 6797 * │ │ └── fl2 6798 * │ ├── do1 6799 * │ │ ├── fl3 6800 * │ │ ├── fo2 6801 * │ │ └── fu3 6802 * │ ├── du1 6803 * │ │ └── fu2 6804 * │ ├── fl1 6805 * │ ├── fo1 6806 * │ ├── fu1 6807 * │ └── pl1 [FIFO] 6808 * └── upper 6809 * ├── data 6810 * │ ├── do1 6811 * │ │ ├── fo2 6812 * │ │ └── fu3 6813 * │ ├── du1 6814 * │ │ └── fu2 6815 * │ ├── fo1 6816 * │ └── fu1 6817 * └── work 6818 * └── work 6819 */ 6820 6821 FIXTURE(layout2_overlay) 6822 { 6823 bool skip_test; 6824 }; 6825 6826 FIXTURE_SETUP(layout2_overlay) 6827 { 6828 if (!supports_filesystem("overlay")) { 6829 self->skip_test = true; 6830 SKIP(return, "overlayfs is not supported (setup)"); 6831 } 6832 6833 prepare_layout(_metadata); 6834 6835 create_directory(_metadata, LOWER_BASE); 6836 set_cap(_metadata, CAP_SYS_ADMIN); 6837 /* Creates tmpfs mount points to get deterministic overlayfs. */ 6838 ASSERT_EQ(0, mount_opt(&mnt_tmp, LOWER_BASE)); 6839 clear_cap(_metadata, CAP_SYS_ADMIN); 6840 create_file(_metadata, lower_fl1); 6841 create_file(_metadata, lower_dl1_fl2); 6842 create_file(_metadata, lower_fo1); 6843 create_file(_metadata, lower_do1_fo2); 6844 create_file(_metadata, lower_do1_fl3); 6845 ASSERT_EQ(0, mknod(lower_pl1, S_IFIFO | 0600, 0)); 6846 6847 create_directory(_metadata, UPPER_BASE); 6848 set_cap(_metadata, CAP_SYS_ADMIN); 6849 ASSERT_EQ(0, mount_opt(&mnt_tmp, UPPER_BASE)); 6850 clear_cap(_metadata, CAP_SYS_ADMIN); 6851 create_file(_metadata, upper_fu1); 6852 create_file(_metadata, upper_du1_fu2); 6853 create_file(_metadata, upper_fo1); 6854 create_file(_metadata, upper_do1_fo2); 6855 create_file(_metadata, upper_do1_fu3); 6856 ASSERT_EQ(0, mkdir(UPPER_WORK, 0700)); 6857 6858 create_directory(_metadata, MERGE_DATA); 6859 set_cap(_metadata, CAP_SYS_ADMIN); 6860 set_cap(_metadata, CAP_DAC_OVERRIDE); 6861 ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0, 6862 "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA 6863 ",workdir=" UPPER_WORK)); 6864 clear_cap(_metadata, CAP_DAC_OVERRIDE); 6865 clear_cap(_metadata, CAP_SYS_ADMIN); 6866 } 6867 6868 FIXTURE_TEARDOWN_PARENT(layout2_overlay) 6869 { 6870 if (self->skip_test) 6871 SKIP(return, "overlayfs is not supported (teardown)"); 6872 6873 EXPECT_EQ(0, remove_path(lower_do1_fl3)); 6874 EXPECT_EQ(0, remove_path(lower_dl1_fl2)); 6875 EXPECT_EQ(0, remove_path(lower_fl1)); 6876 EXPECT_EQ(0, remove_path(lower_do1_fo2)); 6877 EXPECT_EQ(0, remove_path(lower_fo1)); 6878 EXPECT_EQ(0, remove_path(lower_pl1)); 6879 6880 /* umount(LOWER_BASE)) is handled by namespace lifetime. */ 6881 EXPECT_EQ(0, remove_path(LOWER_BASE)); 6882 6883 EXPECT_EQ(0, remove_path(upper_do1_fu3)); 6884 EXPECT_EQ(0, remove_path(upper_du1_fu2)); 6885 EXPECT_EQ(0, remove_path(upper_fu1)); 6886 EXPECT_EQ(0, remove_path(upper_do1_fo2)); 6887 EXPECT_EQ(0, remove_path(upper_fo1)); 6888 EXPECT_EQ(0, remove_path(UPPER_WORK "/work")); 6889 6890 /* umount(UPPER_BASE)) is handled by namespace lifetime. */ 6891 EXPECT_EQ(0, remove_path(UPPER_BASE)); 6892 6893 /* umount(MERGE_DATA)) is handled by namespace lifetime. */ 6894 EXPECT_EQ(0, remove_path(MERGE_DATA)); 6895 6896 cleanup_layout(_metadata); 6897 } 6898 6899 TEST_F_FORK(layout2_overlay, no_restriction) 6900 { 6901 if (self->skip_test) 6902 SKIP(return, "overlayfs is not supported (test)"); 6903 6904 ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY)); 6905 ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY)); 6906 ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY)); 6907 ASSERT_EQ(0, test_open(lower_fo1, O_RDONLY)); 6908 ASSERT_EQ(0, test_open(lower_do1, O_RDONLY)); 6909 ASSERT_EQ(0, test_open(lower_do1_fo2, O_RDONLY)); 6910 ASSERT_EQ(0, test_open(lower_do1_fl3, O_RDONLY)); 6911 6912 ASSERT_EQ(0, test_open(upper_fu1, O_RDONLY)); 6913 ASSERT_EQ(0, test_open(upper_du1, O_RDONLY)); 6914 ASSERT_EQ(0, test_open(upper_du1_fu2, O_RDONLY)); 6915 ASSERT_EQ(0, test_open(upper_fo1, O_RDONLY)); 6916 ASSERT_EQ(0, test_open(upper_do1, O_RDONLY)); 6917 ASSERT_EQ(0, test_open(upper_do1_fo2, O_RDONLY)); 6918 ASSERT_EQ(0, test_open(upper_do1_fu3, O_RDONLY)); 6919 6920 ASSERT_EQ(0, test_open(merge_fl1, O_RDONLY)); 6921 ASSERT_EQ(0, test_open(merge_dl1, O_RDONLY)); 6922 ASSERT_EQ(0, test_open(merge_dl1_fl2, O_RDONLY)); 6923 ASSERT_EQ(0, test_open(merge_fu1, O_RDONLY)); 6924 ASSERT_EQ(0, test_open(merge_du1, O_RDONLY)); 6925 ASSERT_EQ(0, test_open(merge_du1_fu2, O_RDONLY)); 6926 ASSERT_EQ(0, test_open(merge_fo1, O_RDONLY)); 6927 ASSERT_EQ(0, test_open(merge_do1, O_RDONLY)); 6928 ASSERT_EQ(0, test_open(merge_do1_fo2, O_RDONLY)); 6929 ASSERT_EQ(0, test_open(merge_do1_fl3, O_RDONLY)); 6930 ASSERT_EQ(0, test_open(merge_do1_fu3, O_RDONLY)); 6931 } 6932 6933 #define for_each_path(path_list, path_entry, i) \ 6934 for (i = 0, path_entry = *path_list[i]; path_list[i]; \ 6935 path_entry = *path_list[++i]) 6936 6937 TEST_F_FORK(layout2_overlay, same_content_different_file) 6938 { 6939 /* Sets access right on parent directories of both layers. */ 6940 const struct rule layer1_base[] = { 6941 { 6942 .path = LOWER_BASE, 6943 .access = LANDLOCK_ACCESS_FS_READ_FILE, 6944 }, 6945 { 6946 .path = UPPER_BASE, 6947 .access = LANDLOCK_ACCESS_FS_READ_FILE, 6948 }, 6949 { 6950 .path = MERGE_BASE, 6951 .access = ACCESS_RW, 6952 }, 6953 {}, 6954 }; 6955 const struct rule layer2_data[] = { 6956 { 6957 .path = LOWER_DATA, 6958 .access = LANDLOCK_ACCESS_FS_READ_FILE, 6959 }, 6960 { 6961 .path = UPPER_DATA, 6962 .access = LANDLOCK_ACCESS_FS_READ_FILE, 6963 }, 6964 { 6965 .path = MERGE_DATA, 6966 .access = ACCESS_RW, 6967 }, 6968 {}, 6969 }; 6970 /* Sets access right on directories inside both layers. */ 6971 const struct rule layer3_subdirs[] = { 6972 { 6973 .path = lower_dl1, 6974 .access = LANDLOCK_ACCESS_FS_READ_FILE, 6975 }, 6976 { 6977 .path = lower_do1, 6978 .access = LANDLOCK_ACCESS_FS_READ_FILE, 6979 }, 6980 { 6981 .path = upper_du1, 6982 .access = LANDLOCK_ACCESS_FS_READ_FILE, 6983 }, 6984 { 6985 .path = upper_do1, 6986 .access = LANDLOCK_ACCESS_FS_READ_FILE, 6987 }, 6988 { 6989 .path = merge_dl1, 6990 .access = ACCESS_RW, 6991 }, 6992 { 6993 .path = merge_du1, 6994 .access = ACCESS_RW, 6995 }, 6996 { 6997 .path = merge_do1, 6998 .access = ACCESS_RW, 6999 }, 7000 {}, 7001 }; 7002 /* Tighten access rights to the files. */ 7003 const struct rule layer4_files[] = { 7004 { 7005 .path = lower_dl1_fl2, 7006 .access = LANDLOCK_ACCESS_FS_READ_FILE, 7007 }, 7008 { 7009 .path = lower_do1_fo2, 7010 .access = LANDLOCK_ACCESS_FS_READ_FILE, 7011 }, 7012 { 7013 .path = lower_do1_fl3, 7014 .access = LANDLOCK_ACCESS_FS_READ_FILE, 7015 }, 7016 { 7017 .path = upper_du1_fu2, 7018 .access = LANDLOCK_ACCESS_FS_READ_FILE, 7019 }, 7020 { 7021 .path = upper_do1_fo2, 7022 .access = LANDLOCK_ACCESS_FS_READ_FILE, 7023 }, 7024 { 7025 .path = upper_do1_fu3, 7026 .access = LANDLOCK_ACCESS_FS_READ_FILE, 7027 }, 7028 { 7029 .path = merge_dl1_fl2, 7030 .access = LANDLOCK_ACCESS_FS_READ_FILE | 7031 LANDLOCK_ACCESS_FS_WRITE_FILE, 7032 }, 7033 { 7034 .path = merge_du1_fu2, 7035 .access = LANDLOCK_ACCESS_FS_READ_FILE | 7036 LANDLOCK_ACCESS_FS_WRITE_FILE, 7037 }, 7038 { 7039 .path = merge_do1_fo2, 7040 .access = LANDLOCK_ACCESS_FS_READ_FILE | 7041 LANDLOCK_ACCESS_FS_WRITE_FILE, 7042 }, 7043 { 7044 .path = merge_do1_fl3, 7045 .access = LANDLOCK_ACCESS_FS_READ_FILE | 7046 LANDLOCK_ACCESS_FS_WRITE_FILE, 7047 }, 7048 { 7049 .path = merge_do1_fu3, 7050 .access = LANDLOCK_ACCESS_FS_READ_FILE | 7051 LANDLOCK_ACCESS_FS_WRITE_FILE, 7052 }, 7053 {}, 7054 }; 7055 const struct rule layer5_merge_only[] = { 7056 { 7057 .path = MERGE_DATA, 7058 .access = LANDLOCK_ACCESS_FS_READ_FILE | 7059 LANDLOCK_ACCESS_FS_WRITE_FILE, 7060 }, 7061 {}, 7062 }; 7063 size_t i; 7064 const char *path_entry; 7065 7066 if (self->skip_test) 7067 SKIP(return, "overlayfs is not supported (test)"); 7068 7069 /* Sets rules on base directories (i.e. outside overlay scope). */ 7070 enforce_fs(_metadata, ACCESS_RW, layer1_base); 7071 7072 /* Checks lower layer. */ 7073 for_each_path(lower_base_files, path_entry, i) { 7074 ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 7075 ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 7076 } 7077 for_each_path(lower_base_directories, path_entry, i) { 7078 ASSERT_EQ(EACCES, 7079 test_open(path_entry, O_RDONLY | O_DIRECTORY)); 7080 } 7081 for_each_path(lower_sub_files, path_entry, i) { 7082 ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 7083 ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 7084 } 7085 /* Checks upper layer. */ 7086 for_each_path(upper_base_files, path_entry, i) { 7087 ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 7088 ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 7089 } 7090 for_each_path(upper_base_directories, path_entry, i) { 7091 ASSERT_EQ(EACCES, 7092 test_open(path_entry, O_RDONLY | O_DIRECTORY)); 7093 } 7094 for_each_path(upper_sub_files, path_entry, i) { 7095 ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 7096 ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 7097 } 7098 /* 7099 * Checks that access rights are independent from the lower and upper 7100 * layers: write access to upper files viewed through the merge point 7101 * is still allowed, and write access to lower file viewed (and copied) 7102 * through the merge point is still allowed. 7103 */ 7104 for_each_path(merge_base_files, path_entry, i) { 7105 ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 7106 } 7107 for_each_path(merge_base_directories, path_entry, i) { 7108 ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY)); 7109 } 7110 for_each_path(merge_sub_files, path_entry, i) { 7111 ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 7112 } 7113 7114 /* Sets rules on data directories (i.e. inside overlay scope). */ 7115 enforce_fs(_metadata, ACCESS_RW, layer2_data); 7116 7117 /* Checks merge. */ 7118 for_each_path(merge_base_files, path_entry, i) { 7119 ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 7120 } 7121 for_each_path(merge_base_directories, path_entry, i) { 7122 ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY)); 7123 } 7124 for_each_path(merge_sub_files, path_entry, i) { 7125 ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 7126 } 7127 7128 /* Same checks with tighter rules. */ 7129 enforce_fs(_metadata, ACCESS_RW, layer3_subdirs); 7130 7131 /* Checks changes for lower layer. */ 7132 for_each_path(lower_base_files, path_entry, i) { 7133 ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY)); 7134 } 7135 /* Checks changes for upper layer. */ 7136 for_each_path(upper_base_files, path_entry, i) { 7137 ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY)); 7138 } 7139 /* Checks all merge accesses. */ 7140 for_each_path(merge_base_files, path_entry, i) { 7141 ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR)); 7142 } 7143 for_each_path(merge_base_directories, path_entry, i) { 7144 ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY)); 7145 } 7146 for_each_path(merge_sub_files, path_entry, i) { 7147 ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 7148 } 7149 7150 /* Sets rules directly on overlayed files. */ 7151 enforce_fs(_metadata, ACCESS_RW, layer4_files); 7152 7153 /* Checks unchanged accesses on lower layer. */ 7154 for_each_path(lower_sub_files, path_entry, i) { 7155 ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 7156 ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 7157 } 7158 /* Checks unchanged accesses on upper layer. */ 7159 for_each_path(upper_sub_files, path_entry, i) { 7160 ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 7161 ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 7162 } 7163 /* Checks all merge accesses. */ 7164 for_each_path(merge_base_files, path_entry, i) { 7165 ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR)); 7166 } 7167 for_each_path(merge_base_directories, path_entry, i) { 7168 ASSERT_EQ(EACCES, 7169 test_open(path_entry, O_RDONLY | O_DIRECTORY)); 7170 } 7171 for_each_path(merge_sub_files, path_entry, i) { 7172 ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 7173 } 7174 7175 /* Only allows access to the merge hierarchy. */ 7176 enforce_fs(_metadata, ACCESS_RW, layer5_merge_only); 7177 7178 /* Checks new accesses on lower layer. */ 7179 for_each_path(lower_sub_files, path_entry, i) { 7180 ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY)); 7181 } 7182 /* Checks new accesses on upper layer. */ 7183 for_each_path(upper_sub_files, path_entry, i) { 7184 ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY)); 7185 } 7186 /* Checks all merge accesses. */ 7187 for_each_path(merge_base_files, path_entry, i) { 7188 ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR)); 7189 } 7190 for_each_path(merge_base_directories, path_entry, i) { 7191 ASSERT_EQ(EACCES, 7192 test_open(path_entry, O_RDONLY | O_DIRECTORY)); 7193 } 7194 for_each_path(merge_sub_files, path_entry, i) { 7195 ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 7196 } 7197 } 7198 7199 TEST_F_FORK(layout2_overlay, rename_in_overlay_without_make_reg) 7200 { 7201 const char *const merge_pl1_renamed = MERGE_DATA "/pl1_renamed"; 7202 7203 if (self->skip_test) 7204 SKIP(return, "overlayfs is not supported (test)"); 7205 7206 /* 7207 * merge_pl1 is a FIFO which only exists in the lower layer. Before 7208 * the rename, the upper layer has no entry under this name. 7209 */ 7210 ASSERT_TRUE(is_fifo(merge_pl1)); 7211 ASSERT_TRUE(is_missing(UPPER_DATA "/pl1")); 7212 7213 /* MAKE_REG is restricted, but MAKE_FIFO is not. */ 7214 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, NULL); 7215 7216 /* 7217 * Rename the FIFO through OverlayFS. merge_pl1 originates from the 7218 * lower layer, so this triggers a copy-up and creates the whiteout in 7219 * the upper layer to hide the lower layer FIFO file. Even though 7220 * MAKE_REG is restricted, the rename on the OverlayFS works. 7221 */ 7222 EXPECT_EQ(0, rename(merge_pl1, merge_pl1_renamed)); 7223 7224 /* Check that the rename worked. */ 7225 EXPECT_TRUE(is_fifo(merge_pl1_renamed)); 7226 EXPECT_TRUE(is_missing(merge_pl1)); 7227 7228 /* 7229 * Check that the whiteout object was created on the underlying "upper" 7230 * filesystem during the rename. This is OK because the whiteout object 7231 * was created by OverlayFS, not by the calling task. 7232 */ 7233 EXPECT_TRUE(is_whiteout(UPPER_DATA "/pl1")); 7234 } 7235 7236 FIXTURE(layout3_fs) 7237 { 7238 bool has_created_dir; 7239 bool has_created_file; 7240 bool skip_test; 7241 }; 7242 7243 FIXTURE_VARIANT(layout3_fs) 7244 { 7245 const struct mnt_opt mnt; 7246 const char *const file_path; 7247 unsigned int cwd_fs_magic; 7248 }; 7249 7250 /* clang-format off */ 7251 FIXTURE_VARIANT_ADD(layout3_fs, tmpfs) { 7252 /* clang-format on */ 7253 .mnt = { 7254 .type = "tmpfs", 7255 .data = MNT_TMP_DATA, 7256 }, 7257 .file_path = file1_s1d1, 7258 }; 7259 7260 FIXTURE_VARIANT_ADD(layout3_fs, ramfs) { 7261 .mnt = { 7262 .type = "ramfs", 7263 .data = "mode=700", 7264 }, 7265 .file_path = TMP_DIR "/dir/file", 7266 }; 7267 7268 FIXTURE_VARIANT_ADD(layout3_fs, cgroup2) { 7269 .mnt = { 7270 .type = "cgroup2", 7271 }, 7272 .file_path = TMP_DIR "/test/cgroup.procs", 7273 }; 7274 7275 FIXTURE_VARIANT_ADD(layout3_fs, proc) { 7276 .mnt = { 7277 .type = "proc", 7278 }, 7279 .file_path = TMP_DIR "/self/status", 7280 }; 7281 7282 FIXTURE_VARIANT_ADD(layout3_fs, sysfs) { 7283 .mnt = { 7284 .type = "sysfs", 7285 }, 7286 .file_path = TMP_DIR "/kernel/notes", 7287 }; 7288 7289 FIXTURE_VARIANT_ADD(layout3_fs, hostfs) { 7290 .mnt = { 7291 .source = TMP_DIR, 7292 .flags = MS_BIND, 7293 }, 7294 .file_path = TMP_DIR "/dir/file", 7295 .cwd_fs_magic = HOSTFS_SUPER_MAGIC, 7296 }; 7297 7298 static char *dirname_alloc(const char *path) 7299 { 7300 char *dup; 7301 7302 if (!path) 7303 return NULL; 7304 7305 dup = strdup(path); 7306 if (!dup) 7307 return NULL; 7308 7309 return dirname(dup); 7310 } 7311 7312 FIXTURE_SETUP(layout3_fs) 7313 { 7314 struct stat statbuf; 7315 char *dir_path = dirname_alloc(variant->file_path); 7316 7317 if (!supports_filesystem(variant->mnt.type) || 7318 !cwd_matches_fs(variant->cwd_fs_magic)) { 7319 self->skip_test = true; 7320 SKIP(return, "this filesystem is not supported (setup)"); 7321 } 7322 7323 prepare_layout_opt(_metadata, &variant->mnt); 7324 7325 /* Creates directory when required. */ 7326 if (stat(dir_path, &statbuf)) { 7327 set_cap(_metadata, CAP_DAC_OVERRIDE); 7328 EXPECT_EQ(0, mkdir(dir_path, 0700)) 7329 { 7330 TH_LOG("Failed to create directory \"%s\": %s", 7331 dir_path, strerror(errno)); 7332 } 7333 self->has_created_dir = true; 7334 clear_cap(_metadata, CAP_DAC_OVERRIDE); 7335 } 7336 7337 /* Creates file when required. */ 7338 if (stat(variant->file_path, &statbuf)) { 7339 int fd; 7340 7341 set_cap(_metadata, CAP_DAC_OVERRIDE); 7342 fd = creat(variant->file_path, 0600); 7343 EXPECT_LE(0, fd) 7344 { 7345 TH_LOG("Failed to create file \"%s\": %s", 7346 variant->file_path, strerror(errno)); 7347 } 7348 EXPECT_EQ(0, close(fd)); 7349 self->has_created_file = true; 7350 clear_cap(_metadata, CAP_DAC_OVERRIDE); 7351 } 7352 7353 free(dir_path); 7354 } 7355 7356 FIXTURE_TEARDOWN_PARENT(layout3_fs) 7357 { 7358 if (self->skip_test) 7359 SKIP(return, "this filesystem is not supported (teardown)"); 7360 7361 if (self->has_created_file) { 7362 set_cap(_metadata, CAP_DAC_OVERRIDE); 7363 /* 7364 * Don't check for error because the file might already 7365 * have been removed (cf. release_inode test). 7366 */ 7367 unlink(variant->file_path); 7368 clear_cap(_metadata, CAP_DAC_OVERRIDE); 7369 } 7370 7371 if (self->has_created_dir) { 7372 char *dir_path = dirname_alloc(variant->file_path); 7373 7374 set_cap(_metadata, CAP_DAC_OVERRIDE); 7375 /* 7376 * Don't check for error because the directory might already 7377 * have been removed (cf. release_inode test). 7378 */ 7379 rmdir(dir_path); 7380 clear_cap(_metadata, CAP_DAC_OVERRIDE); 7381 free(dir_path); 7382 } 7383 7384 cleanup_layout(_metadata); 7385 } 7386 7387 static void layer3_fs_tag_inode(struct __test_metadata *const _metadata, 7388 FIXTURE_DATA(layout3_fs) * self, 7389 const FIXTURE_VARIANT(layout3_fs) * variant, 7390 const char *const rule_path) 7391 { 7392 const struct rule layer1_allow_read_file[] = { 7393 { 7394 .path = rule_path, 7395 .access = LANDLOCK_ACCESS_FS_READ_FILE, 7396 }, 7397 {}, 7398 }; 7399 const char *const dev_null_path = "/dev/null"; 7400 7401 if (self->skip_test) 7402 SKIP(return, "this filesystem is not supported (test)"); 7403 7404 /* Checks without Landlock. */ 7405 EXPECT_EQ(0, test_open(dev_null_path, O_RDONLY | O_CLOEXEC)); 7406 EXPECT_EQ(0, test_open(variant->file_path, O_RDONLY | O_CLOEXEC)); 7407 7408 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, 7409 layer1_allow_read_file); 7410 7411 EXPECT_EQ(EACCES, test_open(dev_null_path, O_RDONLY | O_CLOEXEC)); 7412 EXPECT_EQ(0, test_open(variant->file_path, O_RDONLY | O_CLOEXEC)); 7413 7414 /* Forbids directory reading. */ 7415 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, NULL); 7416 7417 /* Checks with Landlock and forbidden access. */ 7418 EXPECT_EQ(EACCES, test_open(dev_null_path, O_RDONLY | O_CLOEXEC)); 7419 EXPECT_EQ(EACCES, test_open(variant->file_path, O_RDONLY | O_CLOEXEC)); 7420 } 7421 7422 /* Matrix of tests to check file hierarchy evaluation. */ 7423 7424 TEST_F_FORK(layout3_fs, tag_inode_dir_parent) 7425 { 7426 /* The current directory must not be the root for this test. */ 7427 layer3_fs_tag_inode(_metadata, self, variant, "."); 7428 } 7429 7430 TEST_F_FORK(layout3_fs, tag_inode_dir_mnt) 7431 { 7432 layer3_fs_tag_inode(_metadata, self, variant, TMP_DIR); 7433 } 7434 7435 TEST_F_FORK(layout3_fs, tag_inode_dir_child) 7436 { 7437 char *dir_path = dirname_alloc(variant->file_path); 7438 7439 layer3_fs_tag_inode(_metadata, self, variant, dir_path); 7440 free(dir_path); 7441 } 7442 7443 TEST_F_FORK(layout3_fs, tag_inode_file) 7444 { 7445 layer3_fs_tag_inode(_metadata, self, variant, variant->file_path); 7446 } 7447 7448 /* Light version of layout1.release_inodes */ 7449 TEST_F_FORK(layout3_fs, release_inodes) 7450 { 7451 const struct rule layer1[] = { 7452 { 7453 .path = TMP_DIR, 7454 .access = LANDLOCK_ACCESS_FS_READ_DIR, 7455 }, 7456 {}, 7457 }; 7458 int ruleset_fd; 7459 7460 if (self->skip_test) 7461 SKIP(return, "this filesystem is not supported (test)"); 7462 7463 /* Clean up for the teardown to not fail. */ 7464 if (self->has_created_file) 7465 EXPECT_EQ(0, remove_path(variant->file_path)); 7466 7467 if (self->has_created_dir) { 7468 char *dir_path = dirname_alloc(variant->file_path); 7469 7470 /* Don't check for error because of cgroup specificities. */ 7471 remove_path(dir_path); 7472 free(dir_path); 7473 } 7474 7475 ruleset_fd = 7476 create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_DIR, layer1); 7477 7478 /* Unmount the filesystem while it is being used by a ruleset. */ 7479 set_cap(_metadata, CAP_SYS_ADMIN); 7480 ASSERT_EQ(0, umount(TMP_DIR)); 7481 clear_cap(_metadata, CAP_SYS_ADMIN); 7482 7483 /* Replaces with a new mount point to simplify FIXTURE_TEARDOWN. */ 7484 set_cap(_metadata, CAP_SYS_ADMIN); 7485 ASSERT_EQ(0, mount_opt(&mnt_tmp, TMP_DIR)); 7486 clear_cap(_metadata, CAP_SYS_ADMIN); 7487 7488 enforce_ruleset(_metadata, ruleset_fd); 7489 ASSERT_EQ(0, close(ruleset_fd)); 7490 7491 /* Checks that access to the new mount point is denied. */ 7492 ASSERT_EQ(EACCES, test_open(TMP_DIR, O_RDONLY)); 7493 } 7494 7495 static int matches_log_fs_extra(struct __test_metadata *const _metadata, 7496 int audit_fd, const char *const blockers, 7497 const char *const path, const char *const extra) 7498 { 7499 static const char log_template[] = REGEX_LANDLOCK_PREFIX 7500 " blockers=fs\\.%s path=\"%s\" dev=\"[^\"]\\+\" ino=[0-9]\\+$"; 7501 char *absolute_path = NULL; 7502 size_t log_match_remaining = sizeof(log_template) + strlen(blockers) + 7503 PATH_MAX * 2 + 7504 (extra ? strlen(extra) : 0) + 1; 7505 char log_match[log_match_remaining]; 7506 char *log_match_cursor = log_match; 7507 size_t chunk_len; 7508 7509 chunk_len = snprintf(log_match_cursor, log_match_remaining, 7510 REGEX_LANDLOCK_PREFIX " blockers=%s path=\"", 7511 blockers); 7512 if (chunk_len < 0 || chunk_len >= log_match_remaining) 7513 return -E2BIG; 7514 7515 /* 7516 * It is assumed that absolute_path does not contain control 7517 * characters nor spaces, see audit_string_contains_control(). 7518 */ 7519 absolute_path = realpath(path, NULL); 7520 if (!absolute_path) 7521 return -errno; 7522 7523 log_match_remaining -= chunk_len; 7524 log_match_cursor += chunk_len; 7525 log_match_cursor = regex_escape(absolute_path, log_match_cursor, 7526 log_match_remaining); 7527 free(absolute_path); 7528 if (log_match_cursor < 0) 7529 return (long long)log_match_cursor; 7530 7531 log_match_remaining -= log_match_cursor - log_match; 7532 chunk_len = snprintf(log_match_cursor, log_match_remaining, 7533 "\" dev=\"[^\"]\\+\" ino=[0-9]\\+%s$", 7534 extra ?: ""); 7535 if (chunk_len < 0 || chunk_len >= log_match_remaining) 7536 return -E2BIG; 7537 7538 return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match, 7539 NULL); 7540 } 7541 7542 static int matches_log_fs(struct __test_metadata *const _metadata, int audit_fd, 7543 const char *const blockers, const char *const path) 7544 { 7545 return matches_log_fs_extra(_metadata, audit_fd, blockers, path, NULL); 7546 } 7547 7548 FIXTURE(audit_layout1) 7549 { 7550 struct audit_filter audit_filter; 7551 int audit_fd; 7552 }; 7553 7554 FIXTURE_SETUP(audit_layout1) 7555 { 7556 prepare_layout(_metadata); 7557 7558 create_layout1(_metadata); 7559 7560 set_cap(_metadata, CAP_AUDIT_CONTROL); 7561 self->audit_fd = audit_init_with_exe_filter(&self->audit_filter); 7562 EXPECT_LE(0, self->audit_fd); 7563 disable_caps(_metadata); 7564 } 7565 7566 FIXTURE_TEARDOWN_PARENT(audit_layout1) 7567 { 7568 remove_layout1(_metadata); 7569 7570 cleanup_layout(_metadata); 7571 7572 EXPECT_EQ(0, audit_cleanup(-1, NULL)); 7573 } 7574 7575 TEST_F(audit_layout1, execute_make) 7576 { 7577 struct audit_records records; 7578 7579 copy_file(_metadata, bin_true, file1_s1d1); 7580 test_execute(_metadata, 0, file1_s1d1); 7581 test_check_exec(_metadata, 0, file1_s1d1); 7582 7583 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_EXECUTE, NULL); 7584 7585 test_execute(_metadata, EACCES, file1_s1d1); 7586 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.execute", 7587 file1_s1d1)); 7588 test_check_exec(_metadata, EACCES, file1_s1d1); 7589 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.execute", 7590 file1_s1d1)); 7591 7592 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7593 EXPECT_EQ(0, records.access); 7594 EXPECT_EQ(0, records.domain); 7595 } 7596 7597 /* 7598 * Using a set of handled/denied access rights make it possible to check that 7599 * only the blocked ones are logged. 7600 */ 7601 7602 TEST_F(audit_layout1, execute_read) 7603 { 7604 struct audit_records records; 7605 7606 copy_file(_metadata, bin_true, file1_s1d1); 7607 test_execute(_metadata, 0, file1_s1d1); 7608 test_check_exec(_metadata, 0, file1_s1d1); 7609 7610 enforce_fs(_metadata, ACCESS_ALL, NULL); 7611 7612 /* 7613 * The only difference with the previous audit_layout1.execute_read test is 7614 * the extra ",fs\\.read_file" blocked by the executable file. 7615 */ 7616 test_execute(_metadata, EACCES, file1_s1d1); 7617 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7618 "fs\\.execute,fs\\.read_file", file1_s1d1)); 7619 test_check_exec(_metadata, EACCES, file1_s1d1); 7620 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7621 "fs\\.execute,fs\\.read_file", file1_s1d1)); 7622 7623 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7624 EXPECT_EQ(0, records.access); 7625 EXPECT_EQ(0, records.domain); 7626 } 7627 7628 TEST_F(audit_layout1, write_file) 7629 { 7630 struct audit_records records; 7631 7632 enforce_fs(_metadata, ACCESS_ALL, NULL); 7633 7634 EXPECT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 7635 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7636 "fs\\.write_file", file1_s1d1)); 7637 7638 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7639 EXPECT_EQ(0, records.access); 7640 EXPECT_EQ(1, records.domain); 7641 } 7642 7643 TEST_F(audit_layout1, read_file) 7644 { 7645 struct audit_records records; 7646 7647 enforce_fs(_metadata, ACCESS_ALL, NULL); 7648 7649 EXPECT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 7650 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.read_file", 7651 file1_s1d1)); 7652 7653 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7654 EXPECT_EQ(0, records.access); 7655 EXPECT_EQ(1, records.domain); 7656 } 7657 7658 TEST_F(audit_layout1, read_dir) 7659 { 7660 struct audit_records records; 7661 7662 enforce_fs(_metadata, ACCESS_ALL, NULL); 7663 7664 EXPECT_EQ(EACCES, test_open(dir_s1d1, O_DIRECTORY)); 7665 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.read_dir", 7666 dir_s1d1)); 7667 7668 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7669 EXPECT_EQ(0, records.access); 7670 EXPECT_EQ(1, records.domain); 7671 } 7672 7673 TEST_F(audit_layout1, remove_dir) 7674 { 7675 struct audit_records records; 7676 7677 EXPECT_EQ(0, unlink(file1_s1d3)); 7678 EXPECT_EQ(0, unlink(file2_s1d3)); 7679 7680 enforce_fs(_metadata, ACCESS_ALL, NULL); 7681 7682 EXPECT_EQ(-1, rmdir(dir_s1d3)); 7683 EXPECT_EQ(EACCES, errno); 7684 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7685 "fs\\.remove_dir", dir_s1d2)); 7686 7687 EXPECT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d3, AT_REMOVEDIR)); 7688 EXPECT_EQ(EACCES, errno); 7689 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7690 "fs\\.remove_dir", dir_s1d2)); 7691 7692 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7693 EXPECT_EQ(0, records.access); 7694 EXPECT_EQ(0, records.domain); 7695 } 7696 7697 TEST_F(audit_layout1, remove_file) 7698 { 7699 struct audit_records records; 7700 7701 enforce_fs(_metadata, ACCESS_ALL, NULL); 7702 7703 EXPECT_EQ(-1, unlink(file1_s1d3)); 7704 EXPECT_EQ(EACCES, errno); 7705 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7706 "fs\\.remove_file", dir_s1d3)); 7707 7708 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7709 EXPECT_EQ(0, records.access); 7710 EXPECT_EQ(1, records.domain); 7711 } 7712 7713 TEST_F(audit_layout1, make_char) 7714 { 7715 struct audit_records records; 7716 7717 EXPECT_EQ(0, unlink(file1_s1d3)); 7718 7719 enforce_fs(_metadata, ACCESS_ALL, NULL); 7720 7721 EXPECT_EQ(-1, mknod(file1_s1d3, S_IFCHR | 0644, makedev(7, 0))); 7722 EXPECT_EQ(EACCES, errno); 7723 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.make_char", 7724 dir_s1d3)); 7725 7726 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7727 EXPECT_EQ(0, records.access); 7728 EXPECT_EQ(1, records.domain); 7729 } 7730 7731 TEST_F(audit_layout1, make_whiteout) 7732 { 7733 struct audit_records records; 7734 7735 EXPECT_EQ(0, unlink(file1_s1d3)); 7736 7737 enforce_fs(_metadata, ACCESS_ALL, NULL); 7738 7739 /* Whiteout creation is denied and logged as fs.make_reg. */ 7740 EXPECT_EQ(-1, mknod(file1_s1d3, S_IFCHR | 0644, makedev(0, 0))); 7741 EXPECT_EQ(EACCES, errno); 7742 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.make_reg", 7743 dir_s1d3)); 7744 7745 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7746 EXPECT_EQ(0, records.access); 7747 EXPECT_EQ(1, records.domain); 7748 } 7749 7750 TEST_F(audit_layout1, make_dir) 7751 { 7752 struct audit_records records; 7753 7754 EXPECT_EQ(0, unlink(file1_s1d3)); 7755 7756 enforce_fs(_metadata, ACCESS_ALL, NULL); 7757 7758 EXPECT_EQ(-1, mkdir(file1_s1d3, 0755)); 7759 EXPECT_EQ(EACCES, errno); 7760 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.make_dir", 7761 dir_s1d3)); 7762 7763 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7764 EXPECT_EQ(0, records.access); 7765 EXPECT_EQ(1, records.domain); 7766 } 7767 7768 TEST_F(audit_layout1, make_reg) 7769 { 7770 struct audit_records records; 7771 7772 EXPECT_EQ(0, unlink(file1_s1d3)); 7773 7774 enforce_fs(_metadata, ACCESS_ALL, NULL); 7775 7776 EXPECT_EQ(-1, mknod(file1_s1d3, S_IFREG | 0644, 0)); 7777 EXPECT_EQ(EACCES, errno); 7778 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.make_reg", 7779 dir_s1d3)); 7780 7781 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7782 EXPECT_EQ(0, records.access); 7783 EXPECT_EQ(1, records.domain); 7784 } 7785 7786 TEST_F(audit_layout1, make_sock) 7787 { 7788 struct audit_records records; 7789 7790 EXPECT_EQ(0, unlink(file1_s1d3)); 7791 7792 enforce_fs(_metadata, ACCESS_ALL, NULL); 7793 7794 EXPECT_EQ(-1, mknod(file1_s1d3, S_IFSOCK | 0644, 0)); 7795 EXPECT_EQ(EACCES, errno); 7796 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.make_sock", 7797 dir_s1d3)); 7798 7799 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7800 EXPECT_EQ(0, records.access); 7801 EXPECT_EQ(1, records.domain); 7802 } 7803 7804 TEST_F(audit_layout1, make_fifo) 7805 { 7806 struct audit_records records; 7807 7808 EXPECT_EQ(0, unlink(file1_s1d3)); 7809 7810 enforce_fs(_metadata, ACCESS_ALL, NULL); 7811 7812 EXPECT_EQ(-1, mknod(file1_s1d3, S_IFIFO | 0644, 0)); 7813 EXPECT_EQ(EACCES, errno); 7814 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.make_fifo", 7815 dir_s1d3)); 7816 7817 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7818 EXPECT_EQ(0, records.access); 7819 EXPECT_EQ(1, records.domain); 7820 } 7821 7822 TEST_F(audit_layout1, make_block) 7823 { 7824 struct audit_records records; 7825 7826 EXPECT_EQ(0, unlink(file1_s1d3)); 7827 7828 enforce_fs(_metadata, ACCESS_ALL, NULL); 7829 7830 EXPECT_EQ(-1, mknod(file1_s1d3, S_IFBLK | 0644, 0)); 7831 EXPECT_EQ(EACCES, errno); 7832 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7833 "fs\\.make_block", dir_s1d3)); 7834 7835 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7836 EXPECT_EQ(0, records.access); 7837 EXPECT_EQ(1, records.domain); 7838 } 7839 7840 TEST_F(audit_layout1, make_sym) 7841 { 7842 struct audit_records records; 7843 7844 EXPECT_EQ(0, unlink(file1_s1d3)); 7845 7846 enforce_fs(_metadata, ACCESS_ALL, NULL); 7847 7848 EXPECT_EQ(-1, symlink("target", file1_s1d3)); 7849 EXPECT_EQ(EACCES, errno); 7850 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.make_sym", 7851 dir_s1d3)); 7852 7853 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7854 EXPECT_EQ(0, records.access); 7855 EXPECT_EQ(1, records.domain); 7856 } 7857 7858 TEST_F(audit_layout1, refer_handled) 7859 { 7860 struct audit_records records; 7861 7862 EXPECT_EQ(0, unlink(file1_s1d3)); 7863 7864 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_REFER, NULL); 7865 7866 EXPECT_EQ(-1, link(file1_s1d1, file1_s1d3)); 7867 EXPECT_EQ(EXDEV, errno); 7868 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer", 7869 dir_s1d1)); 7870 EXPECT_EQ(0, 7871 matches_log_domain_allocated(self->audit_fd, getpid(), NULL)); 7872 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer", 7873 dir_s1d3)); 7874 7875 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7876 EXPECT_EQ(0, records.access); 7877 EXPECT_EQ(0, records.domain); 7878 } 7879 7880 TEST_F(audit_layout1, refer_make) 7881 { 7882 struct audit_records records; 7883 7884 EXPECT_EQ(0, unlink(file1_s1d3)); 7885 7886 enforce_fs(_metadata, 7887 LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, 7888 NULL); 7889 7890 EXPECT_EQ(-1, link(file1_s1d1, file1_s1d3)); 7891 EXPECT_EQ(EACCES, errno); 7892 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer", 7893 dir_s1d1)); 7894 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7895 "fs\\.make_reg,fs\\.refer", dir_s1d3)); 7896 7897 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7898 EXPECT_EQ(0, records.access); 7899 EXPECT_EQ(0, records.domain); 7900 } 7901 7902 TEST_F(audit_layout1, refer_rename) 7903 { 7904 struct audit_records records; 7905 7906 EXPECT_EQ(0, unlink(file1_s1d3)); 7907 7908 enforce_fs(_metadata, ACCESS_ALL, NULL); 7909 7910 EXPECT_EQ(EACCES, test_rename(file1_s1d2, file1_s2d3)); 7911 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7912 "fs\\.remove_file,fs\\.refer", dir_s1d2)); 7913 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7914 "fs\\.remove_file,fs\\.make_reg,fs\\.refer", 7915 dir_s2d3)); 7916 7917 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7918 EXPECT_EQ(0, records.access); 7919 EXPECT_EQ(0, records.domain); 7920 } 7921 7922 TEST_F(audit_layout1, refer_exchange) 7923 { 7924 struct audit_records records; 7925 7926 EXPECT_EQ(0, unlink(file1_s1d3)); 7927 7928 enforce_fs(_metadata, ACCESS_ALL, NULL); 7929 7930 /* 7931 * The only difference with the previous audit_layout1.refer_rename test is 7932 * the extra ",fs\\.make_reg" blocked by the source directory. 7933 */ 7934 EXPECT_EQ(EACCES, test_exchange(file1_s1d2, file1_s2d3)); 7935 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7936 "fs\\.remove_file,fs\\.make_reg,fs\\.refer", 7937 dir_s1d2)); 7938 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 7939 "fs\\.remove_file,fs\\.make_reg,fs\\.refer", 7940 dir_s2d3)); 7941 7942 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7943 EXPECT_EQ(0, records.access); 7944 EXPECT_EQ(0, records.domain); 7945 } 7946 7947 /* 7948 * This test checks that the audit record is correctly generated when the 7949 * operation is only partially denied. This is the case for rename(2) when the 7950 * source file is allowed to be referenced but the destination directory is not. 7951 * 7952 * This is also a regression test for commit d617f0d72d80 ("landlock: Optimize 7953 * file path walks and prepare for audit support") and commit 058518c20920 7954 * ("landlock: Align partial refer access checks with final ones"). 7955 */ 7956 TEST_F(audit_layout1, refer_rename_half) 7957 { 7958 struct audit_records records; 7959 const struct rule layer1[] = { 7960 { 7961 .path = dir_s2d2, 7962 .access = LANDLOCK_ACCESS_FS_REFER, 7963 }, 7964 {}, 7965 }; 7966 7967 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_REFER, layer1); 7968 7969 ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d3)); 7970 ASSERT_EQ(EXDEV, errno); 7971 7972 /* Only half of the request is denied. */ 7973 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer", 7974 dir_s1d1)); 7975 7976 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7977 EXPECT_EQ(0, records.access); 7978 EXPECT_EQ(1, records.domain); 7979 } 7980 7981 TEST_F(audit_layout1, truncate) 7982 { 7983 struct audit_records records; 7984 7985 enforce_fs(_metadata, ACCESS_ALL, NULL); 7986 7987 EXPECT_EQ(-1, truncate(file1_s1d3, 0)); 7988 EXPECT_EQ(EACCES, errno); 7989 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.truncate", 7990 file1_s1d3)); 7991 7992 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 7993 EXPECT_EQ(0, records.access); 7994 EXPECT_EQ(1, records.domain); 7995 } 7996 7997 /* Checks that audit records preserve every ioctl command bit. */ 7998 TEST_F(audit_layout1, ioctl_dev) 7999 { 8000 struct audit_records records; 8001 int fd; 8002 8003 enforce_fs(_metadata, ACCESS_ALL & ~LANDLOCK_ACCESS_FS_READ_FILE, NULL); 8004 8005 fd = open("/dev/null", O_RDONLY | O_CLOEXEC); 8006 ASSERT_LE(0, fd); 8007 EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, unknown_ioctl_cmd)); 8008 EXPECT_EQ(0, matches_log_fs_extra(_metadata, self->audit_fd, 8009 "fs\\.ioctl_dev", "/dev/null", 8010 " ioctlcmd=0xc00ffeee")); 8011 8012 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 8013 EXPECT_EQ(0, records.access); 8014 EXPECT_EQ(1, records.domain); 8015 } 8016 8017 TEST_F(audit_layout1, resolve_unix) 8018 { 8019 struct audit_records records; 8020 const char *const path = "sock"; 8021 int srv_fd, cli_fd, status; 8022 pid_t child_pid; 8023 8024 srv_fd = set_up_named_unix_server(_metadata, SOCK_STREAM, path); 8025 8026 child_pid = fork(); 8027 ASSERT_LE(0, child_pid); 8028 if (!child_pid) { 8029 enforce_fs(_metadata, ACCESS_ALL, NULL); 8030 8031 cli_fd = socket(AF_UNIX, SOCK_STREAM, 0); 8032 ASSERT_LE(0, cli_fd); 8033 EXPECT_EQ(EACCES, 8034 test_connect_named_unix(_metadata, cli_fd, path)); 8035 8036 EXPECT_EQ(0, close(cli_fd)); 8037 _exit(_metadata->exit_code); 8038 } 8039 8040 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 8041 EXPECT_EQ(1, WIFEXITED(status)); 8042 EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 8043 8044 EXPECT_EQ(0, matches_log_fs_extra(_metadata, self->audit_fd, 8045 "fs\\.resolve_unix", path, NULL)); 8046 8047 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 8048 EXPECT_EQ(0, records.access); 8049 EXPECT_EQ(1, records.domain); 8050 8051 EXPECT_EQ(0, close(srv_fd)); 8052 } 8053 8054 TEST_F(audit_layout1, mount) 8055 { 8056 struct audit_records records; 8057 8058 enforce_fs(_metadata, LANDLOCK_ACCESS_FS_EXECUTE, NULL); 8059 8060 set_cap(_metadata, CAP_SYS_ADMIN); 8061 EXPECT_EQ(-1, mount(NULL, dir_s3d2, NULL, MS_RDONLY, NULL)); 8062 EXPECT_EQ(EPERM, errno); 8063 clear_cap(_metadata, CAP_SYS_ADMIN); 8064 EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 8065 "fs\\.change_topology", dir_s3d2)); 8066 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 8067 EXPECT_EQ(0, records.access); 8068 EXPECT_EQ(1, records.domain); 8069 } 8070 8071 static bool debug_quiet_tests; 8072 8073 FIXTURE(audit_quiet_layout1) 8074 { 8075 struct audit_filter audit_filter; 8076 int audit_fd; 8077 }; 8078 8079 FIXTURE_SETUP(audit_quiet_layout1) 8080 { 8081 prepare_layout(_metadata); 8082 create_layout1(_metadata); 8083 8084 set_cap(_metadata, CAP_AUDIT_CONTROL); 8085 self->audit_fd = audit_init_with_exe_filter(&self->audit_filter); 8086 EXPECT_LE(0, self->audit_fd); 8087 clear_cap(_metadata, CAP_AUDIT_CONTROL); 8088 8089 if (getenv("DEBUG_QUIET_TESTS")) 8090 debug_quiet_tests = true; 8091 } 8092 8093 FIXTURE_TEARDOWN_PARENT(audit_quiet_layout1) 8094 { 8095 remove_layout1(_metadata); 8096 cleanup_layout(_metadata); 8097 8098 set_cap(_metadata, CAP_AUDIT_CONTROL); 8099 EXPECT_EQ(0, audit_cleanup(-1, NULL)); 8100 clear_cap(_metadata, CAP_AUDIT_CONTROL); 8101 } 8102 8103 struct a_rule { 8104 const char *path; 8105 __u64 access; 8106 bool quiet; 8107 }; 8108 8109 struct a_layer { 8110 __u64 handled_access_fs; 8111 __u64 quiet_access_fs; 8112 struct a_rule rules[6]; 8113 __u64 restrict_flags; 8114 }; 8115 8116 struct a_target { 8117 /* File/dir to try open. */ 8118 const char *target; 8119 /* Open mode (one of O_RDONLY, O_WRONLY, or O_RDWR). */ 8120 int open_mode; 8121 /* Should open succeed? */ 8122 bool expect_open_success; 8123 /* If open fails, whether to expect an audit log for read. */ 8124 bool audit_read_blocked; 8125 /* If open fails, whether to expect an audit log for write. */ 8126 bool audit_write_blocked; 8127 /* If ftruncate() is expected to be allowed. */ 8128 bool expect_truncate_success; 8129 /* If ftruncate fails, whether to expect an audit log. */ 8130 bool audit_truncate; 8131 /* 8132 * If ioctl() is expected to be allowed (ioctl not attempted if neither 8133 * this nor expect_ioctl_denied is set). 8134 */ 8135 bool expect_ioctl_allowed; 8136 /* If ioctl() is expected to be denied. */ 8137 bool expect_ioctl_denied; 8138 /* If ioctl fails, whether to expect an audit log. */ 8139 bool audit_ioctl; 8140 }; 8141 8142 #define AUDIT_QUIET_MAX_TARGETS 10 8143 8144 FIXTURE_VARIANT(audit_quiet_layout1) 8145 { 8146 struct a_layer layers[3]; 8147 struct a_target targets[AUDIT_QUIET_MAX_TARGETS]; 8148 }; 8149 8150 #define FS_R LANDLOCK_ACCESS_FS_READ_FILE 8151 #define FS_W LANDLOCK_ACCESS_FS_WRITE_FILE 8152 #define FS_TRUNC LANDLOCK_ACCESS_FS_TRUNCATE 8153 #define FS_IOCTL LANDLOCK_ACCESS_FS_IOCTL_DEV 8154 8155 static int sprint_access_bits(char *buf, size_t buflen, __u64 access) 8156 { 8157 size_t offset = 0; 8158 8159 if (buflen < strlen("rwti make_reg remove_file refer") + 1) 8160 abort(); 8161 8162 buf[0] = '\0'; 8163 if (access & FS_R) 8164 offset += snprintf(buf + offset, buflen - offset, "r"); 8165 if (access & FS_W) 8166 offset += snprintf(buf + offset, buflen - offset, "w"); 8167 if (access & FS_TRUNC) 8168 offset += snprintf(buf + offset, buflen - offset, "t"); 8169 if (access & FS_IOCTL) 8170 offset += snprintf(buf + offset, buflen - offset, "i"); 8171 if (access & LANDLOCK_ACCESS_FS_MAKE_REG) 8172 offset += snprintf(buf + offset, buflen - offset, ",make_reg"); 8173 if (access & LANDLOCK_ACCESS_FS_REMOVE_FILE) 8174 offset += 8175 snprintf(buf + offset, buflen - offset, ",remove_file"); 8176 if (access & LANDLOCK_ACCESS_FS_REFER) 8177 offset += snprintf(buf + offset, buflen - offset, ",refer"); 8178 8179 if (buf[0] == ',') { 8180 offset--; 8181 memmove(buf, buf + 1, offset); 8182 buf[offset] = '\0'; 8183 } 8184 8185 return offset; 8186 } 8187 8188 static int apply_a_layer(struct __test_metadata *const _metadata, 8189 const struct a_layer *l) 8190 { 8191 struct landlock_ruleset_attr rs_attr = { 8192 .handled_access_fs = l->handled_access_fs, 8193 .quiet_access_fs = l->quiet_access_fs, 8194 }; 8195 int rs_fd; 8196 int i; 8197 const struct a_rule *r; 8198 char handled_access_s[33], quiet_access_s[33], rule_access_s[33]; 8199 8200 if (!l->handled_access_fs) 8201 return 0; 8202 8203 rs_fd = landlock_create_ruleset(&rs_attr, sizeof(rs_attr), 0); 8204 ASSERT_LE(0, rs_fd); 8205 8206 for (i = 0; i < ARRAY_SIZE(l->rules); i++) { 8207 r = &l->rules[i]; 8208 if (!r->path) 8209 continue; 8210 8211 add_path_beneath(_metadata, rs_fd, r->access, r->path, 8212 r->quiet ? LANDLOCK_ADD_RULE_QUIET : 0); 8213 } 8214 8215 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)); 8216 ASSERT_EQ(0, landlock_restrict_self(rs_fd, l->restrict_flags)) 8217 { 8218 TH_LOG("Failed to enforce ruleset: %s", strerror(errno)); 8219 } 8220 ASSERT_EQ(0, close(rs_fd)); 8221 8222 if (debug_quiet_tests) { 8223 sprint_access_bits(handled_access_s, sizeof(handled_access_s), 8224 l->handled_access_fs); 8225 sprint_access_bits(quiet_access_s, sizeof(quiet_access_s), 8226 l->quiet_access_fs); 8227 TH_LOG("applied layer: handled=%s quiet=%s restrict_flags=0x%llx", 8228 handled_access_s, quiet_access_s, 8229 (unsigned long long)l->restrict_flags); 8230 for (i = 0; i < ARRAY_SIZE(l->rules); i++) { 8231 r = &l->rules[i]; 8232 if (!r->path) 8233 continue; 8234 8235 sprint_access_bits(rule_access_s, sizeof(rule_access_s), 8236 r->access); 8237 TH_LOG(" rule[%d]: path=%s access=%s quiet=%d", i, 8238 r->path, rule_access_s, r->quiet); 8239 } 8240 } 8241 return 0; 8242 } 8243 8244 void audit_quiet_layout1_test_body(struct __test_metadata *const _metadata, 8245 FIXTURE_DATA(audit_quiet_layout1) * self, 8246 const struct a_target *targets) 8247 { 8248 struct audit_records records = {}; 8249 int i; 8250 const struct a_target *target; 8251 int fd = -1; 8252 int open_mode; 8253 int ret; 8254 bool expect_audit; 8255 const char *blocker; 8256 8257 for (i = 0; i < AUDIT_QUIET_MAX_TARGETS; i++) { 8258 target = &targets[i]; 8259 if (!target->target) 8260 continue; 8261 8262 open_mode = target->open_mode & (O_RDONLY | O_WRONLY | O_RDWR); 8263 8264 EXPECT_TRUE(open_mode == O_RDONLY || open_mode == O_WRONLY || 8265 open_mode == O_RDWR); 8266 8267 if (target->expect_open_success) { 8268 EXPECT_FALSE(target->audit_read_blocked); 8269 EXPECT_FALSE(target->audit_write_blocked); 8270 } 8271 if (target->expect_truncate_success) 8272 EXPECT_TRUE(target->expect_open_success && 8273 !target->audit_truncate); 8274 8275 if (debug_quiet_tests) 8276 TH_LOG("Try open \"%s\" with %s%s", target->target, 8277 open_mode != O_WRONLY ? "r" : "", 8278 open_mode != O_RDONLY ? "w" : ""); 8279 8280 fd = openat(AT_FDCWD, target->target, open_mode | O_CLOEXEC); 8281 if (target->expect_open_success) { 8282 ASSERT_LE(0, fd) 8283 { 8284 TH_LOG("Failed to open \"%s\": %s", 8285 target->target, strerror(errno)); 8286 }; 8287 } else { 8288 ASSERT_EQ(-1, fd); 8289 ASSERT_EQ(EACCES, errno); 8290 } 8291 8292 expect_audit = true; 8293 8294 if (target->audit_read_blocked && target->audit_write_blocked) 8295 blocker = "fs\\.write_file,fs\\.read_file"; 8296 else if (target->audit_read_blocked) 8297 blocker = "fs\\.read_file"; 8298 else if (target->audit_write_blocked) 8299 blocker = "fs\\.write_file"; 8300 else 8301 expect_audit = false; 8302 8303 if (expect_audit) 8304 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 8305 blocker, target->target)); 8306 8307 /* Check that we see no (other) logs. */ 8308 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 8309 ASSERT_EQ(0, records.access); 8310 8311 if (target->expect_open_success && fd >= 0) { 8312 if (debug_quiet_tests) 8313 TH_LOG("Try ftruncate \"%s\"", target->target); 8314 8315 ret = ftruncate(fd, 0); 8316 if (target->expect_truncate_success) { 8317 ASSERT_EQ(0, ret); 8318 } else { 8319 ASSERT_EQ(-1, ret); 8320 if (open_mode != O_RDONLY) 8321 ASSERT_EQ(EACCES, errno); 8322 } 8323 8324 if (target->audit_truncate) 8325 ASSERT_EQ(0, matches_log_fs(_metadata, 8326 self->audit_fd, 8327 "fs\\.truncate", 8328 target->target)); 8329 8330 if (target->expect_ioctl_allowed || 8331 target->expect_ioctl_denied) { 8332 if (debug_quiet_tests) 8333 TH_LOG("Try ioctl FIONREAD on \"%s\"", 8334 target->target); 8335 8336 ret = ioctl_error(_metadata, fd, FIONREAD); 8337 if (target->expect_ioctl_allowed) { 8338 ASSERT_NE(EACCES, ret); 8339 } else { 8340 ASSERT_EQ(EACCES, ret); 8341 } 8342 } 8343 8344 if (target->audit_ioctl) 8345 ASSERT_EQ(0, matches_log_fs_extra( 8346 _metadata, self->audit_fd, 8347 "fs\\.ioctl_dev", 8348 target->target, 8349 " ioctlcmd=0x541b\\+")); 8350 8351 /* Check that we see no other logs. */ 8352 EXPECT_EQ(0, audit_count_records(self->audit_fd, 8353 &records)); 8354 ASSERT_EQ(0, records.access); 8355 ASSERT_EQ(0, close(fd)); 8356 } 8357 } 8358 } 8359 8360 TEST_F(audit_quiet_layout1, base) 8361 { 8362 int i; 8363 8364 for (i = 0; i < ARRAY_SIZE(variant->layers); i++) 8365 ASSERT_EQ(0, apply_a_layer(_metadata, &variant->layers[i])); 8366 8367 audit_quiet_layout1_test_body(_metadata, self, variant->targets); 8368 } 8369 8370 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_simple) { 8371 .layers = { 8372 { 8373 .handled_access_fs = FS_R | FS_W | FS_TRUNC, 8374 .quiet_access_fs = FS_R, 8375 .rules = { 8376 { .path = dir_s1d1, .access = 0, .quiet = true }, 8377 }, 8378 }, 8379 }, 8380 .targets = { 8381 { 8382 .target = file1_s1d1, 8383 .open_mode = O_RDONLY, 8384 }, 8385 /* Not covered by quiet */ 8386 { 8387 .target = file1_s2d1, 8388 .open_mode = O_RDONLY, 8389 .audit_read_blocked = true, 8390 }, 8391 /* Access not quieted */ 8392 { 8393 .target = file1_s1d1, 8394 .open_mode = O_WRONLY, 8395 .audit_write_blocked = true, 8396 }, 8397 /* 8398 * Quiet flag only takes effect if all blocked access bits are 8399 * quieted, otherwise audit log emitted as normal (with all 8400 * blockers) 8401 */ 8402 { 8403 .target = file1_s1d1, 8404 .open_mode = O_RDWR, 8405 .audit_read_blocked = true, 8406 .audit_write_blocked = true, 8407 }, 8408 }, 8409 }; 8410 8411 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_allow_read) { 8412 .layers = { 8413 { 8414 .handled_access_fs = FS_R | FS_W | FS_TRUNC, 8415 .quiet_access_fs = FS_W, 8416 .rules = { 8417 { .path = dir_s1d1, .access = FS_R, .quiet = true }, 8418 /* Quiet flags inherit down and are not overridden */ 8419 { .path = file1_s1d1, .access = FS_R, .quiet = false }, 8420 { .path = file1_s2d3, .access = 0, .quiet = true }, 8421 }, 8422 }, 8423 }, 8424 .targets = { 8425 /* Read ok */ 8426 { 8427 .target = file1_s1d1, 8428 .open_mode = O_RDONLY, 8429 .expect_open_success = true, 8430 }, 8431 /* Write quieted */ 8432 { 8433 .target = file1_s1d1, 8434 .open_mode = O_WRONLY, 8435 }, 8436 /* Read allowed, write quieted so no audit */ 8437 { 8438 .target = file1_s1d1, 8439 .open_mode = O_RDWR, 8440 }, 8441 /* Not covered by quiet */ 8442 { 8443 .target = file1_s2d2, 8444 .open_mode = O_WRONLY, 8445 .audit_write_blocked = true, 8446 }, 8447 { 8448 .target = file1_s2d2, 8449 .open_mode = O_RDWR, 8450 .audit_read_blocked = true, 8451 .audit_write_blocked = true, 8452 }, 8453 /* Single file quiet */ 8454 { 8455 .target = file1_s2d3, 8456 .open_mode = O_WRONLY, 8457 }, 8458 /* Wrong file */ 8459 { 8460 .target = file2_s2d3, 8461 .open_mode = O_WRONLY, 8462 .audit_write_blocked = true, 8463 }, 8464 /* Access not quieted */ 8465 { 8466 .target = file1_s2d3, 8467 .open_mode = O_RDONLY, 8468 .audit_read_blocked = true, 8469 }, 8470 /* Some access not quieted */ 8471 { 8472 .target = file1_s2d3, 8473 .open_mode = O_RDWR, 8474 .audit_read_blocked = true, 8475 .audit_write_blocked = true, 8476 }, 8477 }, 8478 }; 8479 8480 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_allow_write) { 8481 .layers = { 8482 { 8483 .handled_access_fs = FS_R | FS_W | FS_TRUNC, 8484 .quiet_access_fs = FS_R, 8485 .rules = { 8486 { .path = dir_s1d1, .access = FS_W, .quiet = true }, 8487 }, 8488 }, 8489 }, 8490 .targets = { 8491 /* Read quieted */ 8492 { 8493 .target = file1_s1d1, 8494 .open_mode = O_RDONLY, 8495 }, 8496 /* Truncate not quieted */ 8497 { 8498 .target = file1_s1d1, 8499 .open_mode = O_WRONLY, 8500 .expect_open_success = true, 8501 .audit_truncate = true, 8502 }, 8503 /* Not covered by quiet */ 8504 { 8505 .target = file1_s2d1, 8506 .open_mode = O_RDONLY, 8507 .audit_read_blocked = true, 8508 }, 8509 /* Write allowed, read quieted so no audit */ 8510 { 8511 .target = file1_s1d1, 8512 .open_mode = O_RDWR, 8513 }, 8514 }, 8515 }; 8516 8517 FIXTURE_VARIANT_ADD(audit_quiet_layout1, allow_write_quiet_trunc) { 8518 .layers = { 8519 { 8520 .handled_access_fs = FS_R | FS_W | FS_TRUNC, 8521 .quiet_access_fs = FS_TRUNC, 8522 .rules = { 8523 { .path = dir_s1d1, .access = FS_W, .quiet = true }, 8524 { .path = dir_s2d1, .access = FS_W, .quiet = false }, 8525 }, 8526 }, 8527 }, 8528 .targets = { 8529 /* Read not allowed and not quieted */ 8530 { 8531 .target = file1_s1d1, 8532 .open_mode = O_RDONLY, 8533 .audit_read_blocked = true, 8534 }, 8535 /* Truncate quieted */ 8536 { 8537 .target = file1_s1d1, 8538 .open_mode = O_WRONLY, 8539 .expect_open_success = true, 8540 }, 8541 /* Not covered by quiet (truncate) */ 8542 { 8543 .target = file1_s2d1, 8544 .open_mode = O_WRONLY, 8545 .expect_open_success = true, 8546 .audit_truncate = true, 8547 }, 8548 /* Not covered by quiet (read/write) */ 8549 { 8550 .target = file1_s3d1, 8551 .open_mode = O_RDWR, 8552 .audit_read_blocked = true, 8553 .audit_write_blocked = true, 8554 }, 8555 }, 8556 }; 8557 8558 FIXTURE_VARIANT_ADD(audit_quiet_layout1, allow_rw_quiet_trunc) { 8559 .layers = { 8560 { 8561 .handled_access_fs = FS_R | FS_W | FS_TRUNC, 8562 .quiet_access_fs = FS_TRUNC, 8563 .rules = { 8564 { .path = dir_s1d1, .access = FS_R | FS_W, .quiet = true }, 8565 { .path = dir_s2d1, .access = FS_R | FS_W, .quiet = false }, 8566 }, 8567 }, 8568 }, 8569 .targets = { 8570 { 8571 .target = file1_s1d1, 8572 .open_mode = O_RDWR, 8573 .expect_open_success = true, 8574 }, 8575 { 8576 .target = file1_s2d1, 8577 .open_mode = O_RDWR, 8578 .expect_open_success = true, 8579 .audit_truncate = true, 8580 }, 8581 }, 8582 }; 8583 8584 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_all) { 8585 .layers = { 8586 { 8587 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8588 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8589 .rules = { 8590 { .path = dir_s1d1, .access = 0, .quiet = true }, 8591 { .path = file1_s2d1, .access = FS_R | FS_W, .quiet = true }, 8592 { .path = file1_s2d3, .access = 0, .quiet = true }, 8593 { .path = dir_s3d1, .access = FS_W, .quiet = false }, 8594 { .path = "/dev/zero", .access = FS_R, .quiet = false }, 8595 { .path = "/dev/null", .access = FS_R, .quiet = true }, 8596 }, 8597 }, 8598 }, 8599 .targets = { 8600 /* No logs */ 8601 { 8602 .target = file1_s1d1, 8603 .open_mode = O_RDONLY, 8604 }, 8605 { 8606 .target = file1_s1d1, 8607 .open_mode = O_WRONLY, 8608 }, 8609 { 8610 .target = file1_s1d1, 8611 .open_mode = O_RDWR, 8612 }, 8613 /* Truncate quieted - no log */ 8614 { 8615 .target = file1_s2d1, 8616 .open_mode = O_RDWR, 8617 .expect_open_success = true, 8618 }, 8619 /* Truncate not covered by quiet */ 8620 { 8621 .target = file1_s3d1, 8622 .open_mode = O_WRONLY, 8623 .expect_open_success = true, 8624 .audit_truncate = true, 8625 }, 8626 /* Not covered by quiet */ 8627 { 8628 .target = file1_s3d1, 8629 .open_mode = O_RDONLY, 8630 .audit_read_blocked = true, 8631 }, 8632 /* Single file quiet */ 8633 { 8634 .target = file1_s2d3, 8635 .open_mode = O_RDWR, 8636 }, 8637 /* Wrong file */ 8638 { 8639 .target = file2_s2d3, 8640 .open_mode = O_RDWR, 8641 .audit_read_blocked = true, 8642 .audit_write_blocked = true, 8643 }, 8644 /* Ioctl quieted */ 8645 { 8646 .target = "/dev/null", 8647 .open_mode = O_RDONLY, 8648 .expect_open_success = true, 8649 .expect_ioctl_denied = true, 8650 }, 8651 /* Ioctl not quieted */ 8652 { 8653 .target = "/dev/zero", 8654 .open_mode = O_RDONLY, 8655 .expect_open_success = true, 8656 .expect_ioctl_denied = true, 8657 .audit_ioctl = true, 8658 }, 8659 }, 8660 }; 8661 8662 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_across_mountpoint) { 8663 .layers = { 8664 { 8665 .handled_access_fs = FS_R | FS_W | FS_TRUNC, 8666 .quiet_access_fs = FS_R, 8667 .rules = { 8668 { .path = dir_s3d1, .access = 0, .quiet = true }, 8669 }, 8670 }, 8671 }, 8672 .targets = { 8673 { 8674 .target = file1_s3d3, 8675 .open_mode = O_RDONLY, 8676 }, 8677 /* Not covered by quiet */ 8678 { 8679 .target = file1_s1d1, 8680 .open_mode = O_RDONLY, 8681 .audit_read_blocked = true, 8682 }, 8683 { 8684 .target = file1_s1d1, 8685 .open_mode = O_RDWR, 8686 .audit_read_blocked = true, 8687 .audit_write_blocked = true, 8688 }, 8689 /* Access not quieted */ 8690 { 8691 .target = file1_s3d3, 8692 .open_mode = O_WRONLY, 8693 .audit_write_blocked = true, 8694 }, 8695 }, 8696 }; 8697 8698 FIXTURE_VARIANT_ADD(audit_quiet_layout1, allow_all_quiet) { 8699 .layers = { 8700 { 8701 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8702 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8703 .rules = { 8704 { 8705 .path = dir_s1d1, 8706 .access = FS_R | FS_W | FS_TRUNC, 8707 .quiet = true 8708 }, 8709 { 8710 .path = "/dev/null", 8711 .access = FS_R | FS_W | FS_IOCTL, 8712 .quiet = true 8713 }, 8714 }, 8715 }, 8716 }, 8717 .targets = { 8718 { 8719 .target = file1_s1d1, 8720 .open_mode = O_RDWR, 8721 .expect_open_success = true, 8722 .expect_truncate_success = true, 8723 }, 8724 { 8725 .target = "/dev/null", 8726 .open_mode = O_RDONLY, 8727 .expect_open_success = true, 8728 .expect_ioctl_allowed = true, 8729 }, 8730 }, 8731 }; 8732 8733 /* 8734 * With LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF, it doesn't matter what the 8735 * quiet flags below the layer say. 8736 */ 8737 FIXTURE_VARIANT_ADD(audit_quiet_layout1, subdomains_off) { 8738 .layers = { 8739 { 8740 .handled_access_fs = FS_R, 8741 .restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF, 8742 .rules = { 8743 { .path = "/", .access = FS_R, .quiet = false }, 8744 } 8745 }, 8746 { 8747 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8748 .quiet_access_fs = FS_R, 8749 .rules = { 8750 { .path = dir_s1d1, .access = 0, .quiet = true }, 8751 { .path = file1_s2d2, .access = FS_R | FS_W, .quiet = true }, 8752 { .path = file1_s2d3, .access = FS_R | FS_W, .quiet = false }, 8753 { .path = "/dev/null", .access = FS_R | FS_W, .quiet = true }, 8754 { .path = "/dev/zero", .access = FS_R | FS_W, .quiet = false }, 8755 }, 8756 }, 8757 }, 8758 .targets = { 8759 { 8760 .target = file1_s1d1, 8761 .open_mode = O_RDWR, 8762 }, 8763 { 8764 .target = file1_s2d1, 8765 .open_mode = O_RDWR, 8766 }, 8767 { 8768 .target = file1_s2d2, 8769 .open_mode = O_RDWR, 8770 .expect_open_success = true, 8771 /* No audit_truncate */ 8772 }, 8773 { 8774 .target = file1_s2d3, 8775 .open_mode = O_RDWR, 8776 .expect_open_success = true, 8777 /* No audit_truncate */ 8778 }, 8779 { 8780 .target = "/dev/null", 8781 .open_mode = O_RDONLY, 8782 .expect_open_success = true, 8783 .expect_ioctl_denied = true, 8784 /* No audit_ioctl */ 8785 }, 8786 { 8787 .target = "/dev/zero", 8788 .open_mode = O_RDONLY, 8789 .expect_open_success = true, 8790 .expect_ioctl_denied = true, 8791 /* No audit_ioctl */ 8792 }, 8793 }, 8794 }; 8795 8796 /* 8797 * With LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF, it doesn't matter what the 8798 * quiet flags on the layer say. 8799 */ 8800 FIXTURE_VARIANT_ADD(audit_quiet_layout1, same_exec_off) { 8801 .layers = { 8802 { 8803 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8804 .quiet_access_fs = FS_R, 8805 .restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF, 8806 .rules = { 8807 { .path = dir_s1d1, .access = 0, .quiet = true }, 8808 { .path = file1_s2d2, .access = FS_R | FS_W, .quiet = true }, 8809 { .path = file1_s2d3, .access = FS_R | FS_W, .quiet = false }, 8810 { .path = "/dev/null", .access = FS_R | FS_W, .quiet = true }, 8811 { .path = "/dev/zero", .access = FS_R | FS_W, .quiet = false }, 8812 }, 8813 }, 8814 }, 8815 .targets = { 8816 { 8817 .target = file1_s1d1, 8818 .open_mode = O_RDWR, 8819 }, 8820 { 8821 .target = file1_s2d1, 8822 .open_mode = O_RDWR, 8823 }, 8824 { 8825 .target = file1_s2d2, 8826 .open_mode = O_RDWR, 8827 .expect_open_success = true, 8828 /* No audit_truncate */ 8829 }, 8830 { 8831 .target = file1_s2d3, 8832 .open_mode = O_RDWR, 8833 .expect_open_success = true, 8834 /* No audit_truncate */ 8835 }, 8836 { 8837 .target = "/dev/null", 8838 .open_mode = O_RDONLY, 8839 .expect_open_success = true, 8840 .expect_ioctl_denied = true, 8841 /* No audit_ioctl */ 8842 }, 8843 { 8844 .target = "/dev/zero", 8845 .open_mode = O_RDONLY, 8846 .expect_open_success = true, 8847 .expect_ioctl_denied = true, 8848 /* No audit_ioctl */ 8849 }, 8850 }, 8851 }; 8852 8853 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_two_layers_1) { 8854 /* Here, rules that deny access are always quiet. */ 8855 .layers = { 8856 { 8857 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8858 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8859 .rules = { 8860 { 8861 .path = dir_s1d1, 8862 .access = FS_W, 8863 .quiet = true, 8864 }, 8865 { 8866 .path = dir_s2d1, 8867 .access = FS_R | FS_W | FS_TRUNC, 8868 .quiet = false, 8869 }, 8870 { 8871 .path = "/dev/null", 8872 .access = FS_R, 8873 .quiet = true, 8874 }, 8875 { 8876 .path = "/dev/zero", 8877 .access = FS_R | FS_W | FS_IOCTL, 8878 .quiet = false, 8879 }, 8880 }, 8881 }, 8882 { 8883 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8884 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8885 .rules = { 8886 { 8887 .path = dir_s1d1, 8888 .access = FS_R | FS_W | FS_TRUNC, 8889 .quiet = false, 8890 }, 8891 { 8892 .path = dir_s2d1, 8893 .access = FS_W, 8894 .quiet = true, 8895 }, 8896 { 8897 .path = "/dev/null", 8898 .access = FS_R | FS_W | FS_IOCTL, 8899 .quiet = false, 8900 }, 8901 { 8902 .path = "/dev/zero", 8903 .access = FS_R, 8904 .quiet = true, 8905 }, 8906 }, 8907 }, 8908 }, 8909 .targets = { 8910 { 8911 .target = file1_s1d1, 8912 .open_mode = O_RDONLY, 8913 }, 8914 { 8915 .target = file1_s1d1, 8916 .open_mode = O_WRONLY, 8917 .expect_open_success = true, 8918 }, 8919 { 8920 .target = file1_s2d1, 8921 .open_mode = O_RDONLY, 8922 }, 8923 { 8924 .target = file1_s2d1, 8925 .open_mode = O_WRONLY, 8926 .expect_open_success = true, 8927 }, 8928 { 8929 .target = "/dev/null", 8930 .open_mode = O_RDONLY, 8931 .expect_open_success = true, 8932 .expect_ioctl_denied = true, 8933 }, 8934 { 8935 .target = "/dev/zero", 8936 .open_mode = O_RDONLY, 8937 .expect_open_success = true, 8938 .expect_ioctl_denied = true, 8939 }, 8940 }, 8941 }; 8942 8943 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_two_layers_2) { 8944 /* Here, rules that deny access are never quiet. */ 8945 .layers = { 8946 { 8947 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8948 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8949 .rules = { 8950 { 8951 .path = dir_s1d1, 8952 .access = FS_W, 8953 .quiet = false 8954 }, 8955 { 8956 .path = dir_s2d1, 8957 .access = FS_R | FS_W | FS_TRUNC, 8958 .quiet = true 8959 }, 8960 { 8961 .path = "/dev/null", 8962 .access = FS_R, 8963 .quiet = false 8964 }, 8965 { 8966 .path = "/dev/zero", 8967 .access = FS_R | FS_W | FS_IOCTL, 8968 .quiet = true 8969 }, 8970 }, 8971 }, 8972 { 8973 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8974 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 8975 .rules = { 8976 { 8977 .path = dir_s1d1, 8978 .access = FS_R | FS_W | FS_TRUNC, 8979 .quiet = true 8980 }, 8981 { 8982 .path = dir_s2d1, 8983 .access = FS_W, 8984 .quiet = false 8985 }, 8986 { 8987 .path = "/dev/null", 8988 .access = FS_R | FS_W | FS_IOCTL, 8989 .quiet = true 8990 }, 8991 { 8992 .path = "/dev/zero", 8993 .access = FS_R, 8994 .quiet = false 8995 }, 8996 }, 8997 }, 8998 }, 8999 .targets = { 9000 { 9001 .target = file1_s1d1, 9002 .open_mode = O_RDONLY, 9003 .audit_read_blocked = true, 9004 }, 9005 { 9006 .target = file1_s1d1, 9007 .open_mode = O_WRONLY, 9008 .expect_open_success = true, 9009 .audit_truncate = true, 9010 }, 9011 { 9012 .target = file1_s2d1, 9013 .open_mode = O_RDONLY, 9014 .audit_read_blocked = true, 9015 }, 9016 { 9017 .target = file1_s2d1, 9018 .open_mode = O_WRONLY, 9019 .expect_open_success = true, 9020 .audit_truncate = true, 9021 }, 9022 { 9023 .target = "/dev/null", 9024 .open_mode = O_RDONLY, 9025 .expect_open_success = true, 9026 .expect_ioctl_denied = true, 9027 .audit_ioctl = true, 9028 }, 9029 { 9030 .target = "/dev/zero", 9031 .open_mode = O_RDONLY, 9032 .expect_open_success = true, 9033 .expect_ioctl_denied = true, 9034 .audit_ioctl = true, 9035 }, 9036 }, 9037 }; 9038 9039 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_two_layers_3) { 9040 /* This time only the second layer quiets things. */ 9041 .layers = { 9042 { 9043 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9044 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9045 .rules = { 9046 { 9047 .path = dir_s1d1, 9048 .access = FS_W, 9049 .quiet = false, 9050 }, 9051 { 9052 .path = dir_s2d1, 9053 .access = FS_R | FS_W | FS_TRUNC, 9054 .quiet = false, 9055 }, 9056 { 9057 .path = "/dev/null", 9058 .access = FS_R, 9059 .quiet = false, 9060 }, 9061 { 9062 .path = "/dev/zero", 9063 .access = FS_R | FS_W | FS_IOCTL, 9064 .quiet = false, 9065 }, 9066 }, 9067 }, 9068 { 9069 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9070 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9071 .rules = { 9072 { 9073 .path = dir_s1d1, 9074 .access = FS_R | FS_W | FS_TRUNC, 9075 .quiet = false, 9076 }, 9077 { 9078 .path = dir_s2d1, 9079 .access = FS_W, 9080 .quiet = true, 9081 }, 9082 { 9083 .path = "/dev/null", 9084 .access = FS_R | FS_W | FS_IOCTL, 9085 .quiet = false, 9086 }, 9087 { 9088 .path = "/dev/zero", 9089 .access = FS_R, 9090 .quiet = true, 9091 }, 9092 }, 9093 }, 9094 }, 9095 .targets = { 9096 { 9097 .target = file1_s1d1, 9098 .open_mode = O_RDONLY, 9099 .audit_read_blocked = true, 9100 }, 9101 { 9102 .target = file1_s1d1, 9103 .open_mode = O_WRONLY, 9104 .expect_open_success = true, 9105 .audit_truncate = true, 9106 }, 9107 { 9108 .target = file1_s2d1, 9109 .open_mode = O_RDONLY, 9110 }, 9111 { 9112 .target = file1_s2d1, 9113 .open_mode = O_WRONLY, 9114 .expect_open_success = true, 9115 }, 9116 { 9117 .target = "/dev/null", 9118 .open_mode = O_RDONLY, 9119 .expect_open_success = true, 9120 .expect_ioctl_denied = true, 9121 .audit_ioctl = true, 9122 }, 9123 { 9124 .target = "/dev/zero", 9125 .open_mode = O_RDONLY, 9126 .expect_open_success = true, 9127 .expect_ioctl_denied = true, 9128 }, 9129 }, 9130 }; 9131 9132 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_two_layers_different_quiet_access) { 9133 /* Here, rules that deny access are always quiet. */ 9134 .layers = { 9135 { 9136 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9137 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9138 .rules = { 9139 { 9140 .path = dir_s1d1, 9141 .access = FS_W, 9142 .quiet = true, 9143 }, 9144 { 9145 .path = dir_s2d1, 9146 .access = FS_R | FS_W | FS_TRUNC, 9147 .quiet = false, 9148 }, 9149 { 9150 .path = "/dev/null", 9151 .access = FS_R, 9152 .quiet = true, 9153 }, 9154 { 9155 .path = "/dev/zero", 9156 .access = FS_R | FS_W | FS_IOCTL, 9157 .quiet = false, 9158 }, 9159 }, 9160 }, 9161 { 9162 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9163 .quiet_access_fs = FS_IOCTL, 9164 .rules = { 9165 { 9166 .path = dir_s1d1, 9167 .access = FS_R | FS_W | FS_TRUNC, 9168 .quiet = false, 9169 }, 9170 { 9171 .path = dir_s2d1, 9172 .access = FS_W, 9173 .quiet = true, 9174 }, 9175 { 9176 .path = "/dev/null", 9177 .access = FS_R | FS_W | FS_IOCTL, 9178 .quiet = false, 9179 }, 9180 { 9181 .path = "/dev/zero", 9182 .access = FS_R, 9183 .quiet = true, 9184 }, 9185 }, 9186 }, 9187 }, 9188 .targets = { 9189 { 9190 .target = file1_s1d1, 9191 .open_mode = O_RDONLY, 9192 }, 9193 { 9194 .target = file1_s1d1, 9195 .open_mode = O_WRONLY, 9196 .expect_open_success = true, 9197 }, 9198 { 9199 .target = file1_s2d1, 9200 .open_mode = O_RDONLY, 9201 .audit_read_blocked = true, 9202 }, 9203 { 9204 .target = file1_s2d1, 9205 .open_mode = O_WRONLY, 9206 .expect_open_success = true, 9207 .audit_truncate = true, 9208 }, 9209 { 9210 .target = "/dev/null", 9211 .open_mode = O_RDONLY, 9212 .expect_open_success = true, 9213 .expect_ioctl_denied = true, 9214 }, 9215 { 9216 .target = "/dev/zero", 9217 .open_mode = O_RDONLY, 9218 .expect_open_success = true, 9219 .expect_ioctl_denied = true, 9220 }, 9221 }, 9222 }; 9223 9224 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_two_layers_different_handled_1) { 9225 /* Quiet from layer 1 */ 9226 .layers = { 9227 { 9228 .handled_access_fs = FS_R, 9229 .quiet_access_fs = FS_R, 9230 .rules = { 9231 { 9232 .path = file1_s1d1, 9233 .access = FS_R, 9234 .quiet = true, 9235 }, 9236 { 9237 .path = file2_s1d1, 9238 .access = 0, 9239 .quiet = true, 9240 }, 9241 { 9242 .path = file1_s1d2, 9243 .access = 0, 9244 .quiet = true, 9245 }, 9246 { 9247 .path = file2_s1d2, 9248 .access = FS_R, 9249 .quiet = true, 9250 }, 9251 }, 9252 }, 9253 { 9254 .handled_access_fs = FS_W, 9255 .quiet_access_fs = FS_W, 9256 .rules = { 9257 { 9258 .path = file1_s1d1, 9259 .access = FS_W, 9260 .quiet = false, 9261 }, 9262 /* Nothing for file2_s1d1 */ 9263 { 9264 .path = file1_s1d2, 9265 .access = FS_W, 9266 .quiet = false, 9267 }, 9268 /* Nothing for file2_s1d2 */ 9269 }, 9270 }, 9271 }, 9272 .targets = { 9273 { 9274 .target = file1_s1d1, 9275 .open_mode = O_RDWR, 9276 .expect_open_success = true, 9277 .expect_truncate_success = true, 9278 }, 9279 /* Missing both, youngest layer denies write, not quiet */ 9280 { 9281 .target = file2_s1d1, 9282 .open_mode = O_RDWR, 9283 .audit_write_blocked = true, 9284 }, 9285 /* Missing read, denied and quieted by layer 1 */ 9286 { 9287 .target = file1_s1d2, 9288 .open_mode = O_RDWR, 9289 }, 9290 /* Missing write, denied and not quieted by layer 2 */ 9291 { 9292 .target = file2_s1d2, 9293 .open_mode = O_RDWR, 9294 .audit_write_blocked = true, 9295 }, 9296 }, 9297 }; 9298 9299 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_two_layers_different_handled_2) { 9300 /* Quiet from layer 2 */ 9301 .layers = { 9302 { 9303 .handled_access_fs = FS_R, 9304 .quiet_access_fs = FS_R, 9305 .rules = { 9306 { 9307 .path = file1_s1d1, 9308 .access = FS_R, 9309 .quiet = false, 9310 }, 9311 /* Nothing for file2_s1d1 and file1_s1d2 */ 9312 { 9313 .path = file2_s1d2, 9314 .access = FS_R, 9315 .quiet = false, 9316 }, 9317 }, 9318 }, 9319 { 9320 .handled_access_fs = FS_W, 9321 .quiet_access_fs = FS_W, 9322 .rules = { 9323 { 9324 .path = file1_s1d1, 9325 .access = FS_W, 9326 .quiet = true, 9327 }, 9328 { 9329 .path = file2_s1d1, 9330 .access = 0, 9331 .quiet = true, 9332 }, 9333 { 9334 .path = file1_s1d2, 9335 .access = FS_W, 9336 .quiet = true, 9337 }, 9338 { 9339 .path = file2_s1d2, 9340 .access = 0, 9341 .quiet = true, 9342 }, 9343 }, 9344 }, 9345 }, 9346 .targets = { 9347 { 9348 .target = file1_s1d1, 9349 .open_mode = O_RDWR, 9350 .expect_open_success = true, 9351 .expect_truncate_success = true, 9352 }, 9353 /* Missing both, youngest layer denies write, quiet */ 9354 { 9355 .target = file2_s1d1, 9356 .open_mode = O_RDWR, 9357 }, 9358 /* Missing read, denied and not quieted by layer 1 */ 9359 { 9360 .target = file1_s1d2, 9361 .open_mode = O_RDWR, 9362 .audit_read_blocked = true, 9363 }, 9364 /* Missing write, denied and quieted by layer 2 */ 9365 { 9366 .target = file2_s1d2, 9367 .open_mode = O_RDWR, 9368 }, 9369 }, 9370 }; 9371 9372 FIXTURE_VARIANT_ADD(audit_quiet_layout1, quiet_two_layers_different_handled_3) { 9373 /* Quiet from both layers */ 9374 .layers = { 9375 { 9376 .handled_access_fs = FS_R, 9377 .quiet_access_fs = FS_R, 9378 .rules = { 9379 { 9380 .path = file1_s1d1, 9381 .access = FS_R, 9382 .quiet = true, 9383 }, 9384 { 9385 .path = file2_s1d1, 9386 .access = 0, 9387 .quiet = true, 9388 }, 9389 { 9390 .path = file1_s1d2, 9391 .access = 0, 9392 .quiet = true, 9393 }, 9394 { 9395 .path = file2_s1d2, 9396 .access = FS_R, 9397 .quiet = true, 9398 }, 9399 }, 9400 }, 9401 { 9402 .handled_access_fs = FS_W, 9403 .quiet_access_fs = FS_W, 9404 .rules = { 9405 { 9406 .path = file1_s1d1, 9407 .access = FS_W, 9408 .quiet = true, 9409 }, 9410 { 9411 .path = file2_s1d1, 9412 .access = 0, 9413 .quiet = true, 9414 }, 9415 { 9416 .path = file1_s1d2, 9417 .access = FS_W, 9418 .quiet = true, 9419 }, 9420 { 9421 .path = file2_s1d2, 9422 .access = 0, 9423 .quiet = true, 9424 }, 9425 }, 9426 }, 9427 }, 9428 .targets = { 9429 { 9430 .target = file1_s1d1, 9431 .open_mode = O_RDWR, 9432 .expect_open_success = true, 9433 .expect_truncate_success = true, 9434 }, 9435 { 9436 .target = file2_s1d1, 9437 .open_mode = O_RDWR, 9438 }, 9439 { 9440 .target = file1_s1d2, 9441 .open_mode = O_RDWR, 9442 }, 9443 { 9444 .target = file2_s1d2, 9445 .open_mode = O_RDWR, 9446 }, 9447 }, 9448 }; 9449 9450 FIXTURE_VARIANT_ADD(audit_quiet_layout1, without_quiet_then_with_quiet) { 9451 .layers = { 9452 { 9453 .handled_access_fs = FS_R | FS_W, 9454 .quiet_access_fs = FS_R, 9455 .rules = { 9456 { .path = dir_s1d1, .access = FS_W, .quiet = false }, 9457 { .path = dir_s1d1, .access = 0, .quiet = true }, 9458 }, 9459 }, 9460 }, 9461 .targets = { 9462 /* Read denied and quieted */ 9463 { 9464 .target = file1_s1d1, 9465 .open_mode = O_RDONLY, 9466 }, 9467 /* Write ok */ 9468 { 9469 .target = file1_s1d1, 9470 .open_mode = O_WRONLY, 9471 .expect_open_success = true, 9472 .expect_truncate_success = true, 9473 }, 9474 /* Write ok, read denied and quieted */ 9475 { 9476 .target = file1_s1d1, 9477 .open_mode = O_RDWR, 9478 }, 9479 /* Not covered by quiet */ 9480 { 9481 .target = file1_s2d1, 9482 .open_mode = O_RDONLY, 9483 .audit_read_blocked = true, 9484 }, 9485 }, 9486 }; 9487 9488 /* 9489 * The following TEST_F extend the above test cases to test more layers, with 9490 * the inserted layers having varying configurations. 9491 */ 9492 9493 /* Extra allow all layers, quiet or not, does not change any behaviour. */ 9494 TEST_F(audit_quiet_layout1, allow_all_layer) 9495 { 9496 struct a_layer allow_all_layer = { 9497 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9498 .quiet_access_fs = 0, 9499 .rules = { 9500 { 9501 .path = "/", 9502 .access = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9503 .quiet = false, 9504 }, 9505 }, 9506 }; 9507 int i; 9508 9509 ASSERT_EQ(0, apply_a_layer(_metadata, &allow_all_layer)); 9510 for (i = 0; i < ARRAY_SIZE(variant->layers); i++) 9511 ASSERT_EQ(0, apply_a_layer(_metadata, &variant->layers[i])); 9512 9513 audit_quiet_layout1_test_body(_metadata, self, variant->targets); 9514 9515 ASSERT_EQ(0, apply_a_layer(_metadata, &allow_all_layer)); 9516 9517 audit_quiet_layout1_test_body(_metadata, self, variant->targets); 9518 9519 /* 9520 * SELF_LOG flags or quiet bits from inner allowing layers should not 9521 * affect behaviour. 9522 */ 9523 allow_all_layer.quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL; 9524 allow_all_layer.rules[0].quiet = true; 9525 /* 9526 * Note: this only works because we're not checking counts of domain 9527 * alloc/dealloc logs 9528 */ 9529 allow_all_layer.restrict_flags = 9530 LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF | 9531 LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF; 9532 ASSERT_EQ(0, apply_a_layer(_metadata, &allow_all_layer)); 9533 9534 audit_quiet_layout1_test_body(_metadata, self, variant->targets); 9535 } 9536 9537 /* 9538 * Add useless outer layers until we reach the layer limit. Should not change 9539 * anything. 9540 */ 9541 TEST_F(audit_quiet_layout1, many_outer_layers) 9542 { 9543 struct a_layer useless_layer = { 9544 .handled_access_fs = FS_R | FS_W | FS_TRUNC, 9545 .quiet_access_fs = FS_R | FS_W | FS_TRUNC, 9546 .rules = { 9547 { .path = "/", .access = FS_R | FS_W | FS_TRUNC, .quiet = true }, 9548 }, 9549 }; 9550 int i; 9551 9552 for (i = 0; i < ARRAY_SIZE(variant->layers); i++) { 9553 if (variant->layers[i].handled_access_fs == 0) 9554 break; 9555 } 9556 9557 for (; i < LANDLOCK_MAX_NUM_LAYERS; i++) 9558 ASSERT_EQ(0, apply_a_layer(_metadata, &useless_layer)); 9559 9560 for (i = 0; i < ARRAY_SIZE(variant->layers); i++) 9561 ASSERT_EQ(0, apply_a_layer(_metadata, &variant->layers[i])); 9562 9563 audit_quiet_layout1_test_body(_metadata, self, variant->targets); 9564 } 9565 9566 /* An inner layer that denies and quiets everything should result in no logs. */ 9567 TEST_F(audit_quiet_layout1, deny_all_quiet_layer) 9568 { 9569 struct a_layer deny_all_layer = { 9570 .handled_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9571 .quiet_access_fs = FS_R | FS_W | FS_TRUNC | FS_IOCTL, 9572 .rules = { 9573 { .path = "/", .access = 0, .quiet = true }, 9574 }, 9575 }; 9576 int i; 9577 FIXTURE_VARIANT(audit_quiet_layout1) variant_2 = {}; 9578 9579 /* Any open should fail with no logs. */ 9580 for (i = 0; i < ARRAY_SIZE(variant->targets); i++) { 9581 const struct a_target *target = &variant->targets[i]; 9582 9583 variant_2.targets[i] = (struct a_target){ 9584 .target = target->target, 9585 .open_mode = target->open_mode, 9586 /* We denied everything, open should always fail. */ 9587 .expect_open_success = false, 9588 }; 9589 } 9590 9591 for (i = 0; i < ARRAY_SIZE(variant->layers); i++) 9592 ASSERT_EQ(0, apply_a_layer(_metadata, &variant->layers[i])); 9593 ASSERT_EQ(0, apply_a_layer(_metadata, &deny_all_layer)); 9594 9595 audit_quiet_layout1_test_body(_metadata, self, variant_2.targets); 9596 } 9597 9598 /* 9599 * An inner layer that denies everything without quiet should produce logs for 9600 * all access. 9601 */ 9602 TEST_F(audit_quiet_layout1, deny_all_layer) 9603 { 9604 struct a_layer deny_all_layer = { 9605 .handled_access_fs = FS_R | FS_W, 9606 .quiet_access_fs = FS_R | FS_W, 9607 }; 9608 int i; 9609 FIXTURE_VARIANT(audit_quiet_layout1) variant_2 = {}; 9610 bool test_has_subdomains_off = false; 9611 9612 for (i = 0; i < ARRAY_SIZE(variant->layers); i++) { 9613 if (variant->layers[i].restrict_flags & 9614 LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF) { 9615 test_has_subdomains_off = true; 9616 break; 9617 } 9618 } 9619 9620 for (i = 0; i < ARRAY_SIZE(variant->targets); i++) { 9621 const struct a_target *target = &variant->targets[i]; 9622 9623 variant_2.targets[i] = (struct a_target){ 9624 .target = target->target, 9625 .open_mode = target->open_mode, 9626 9627 /* We denied everything, open should always fail. */ 9628 .expect_open_success = false, 9629 /* Audit should always happen as long as open request contains read. */ 9630 .audit_read_blocked = !test_has_subdomains_off && 9631 target->open_mode != O_WRONLY, 9632 /* Audit should always happen as long as open request contains write. */ 9633 .audit_write_blocked = !test_has_subdomains_off && 9634 target->open_mode != O_RDONLY, 9635 }; 9636 } 9637 9638 for (i = 0; i < ARRAY_SIZE(variant->layers); i++) 9639 ASSERT_EQ(0, apply_a_layer(_metadata, &variant->layers[i])); 9640 ASSERT_EQ(0, apply_a_layer(_metadata, &deny_all_layer)); 9641 9642 audit_quiet_layout1_test_body(_metadata, self, variant_2.targets); 9643 } 9644 9645 /* Uses layout1_bind hierarchy */ 9646 FIXTURE(audit_quiet_rename) 9647 { 9648 struct audit_filter audit_filter; 9649 int audit_fd; 9650 }; 9651 9652 FIXTURE_SETUP(audit_quiet_rename) 9653 { 9654 prepare_layout(_metadata); 9655 create_layout1(_metadata); 9656 9657 set_cap(_metadata, CAP_SYS_ADMIN); 9658 ASSERT_EQ(0, mount(dir_s1d2, dir_s2d2, NULL, MS_BIND, NULL)); 9659 clear_cap(_metadata, CAP_SYS_ADMIN); 9660 9661 set_cap(_metadata, CAP_AUDIT_CONTROL); 9662 self->audit_fd = audit_init_with_exe_filter(&self->audit_filter); 9663 EXPECT_LE(0, self->audit_fd); 9664 clear_cap(_metadata, CAP_AUDIT_CONTROL); 9665 9666 if (getenv("DEBUG_QUIET_TESTS")) 9667 debug_quiet_tests = true; 9668 } 9669 9670 FIXTURE_TEARDOWN_PARENT(audit_quiet_rename) 9671 { 9672 remove_layout1(_metadata); 9673 cleanup_layout(_metadata); 9674 9675 /* umount(dir_s2d2)) is handled by namespace lifetime. */ 9676 9677 remove_path(file1_s4d1); 9678 remove_path(file2_s4d1); 9679 9680 set_cap(_metadata, CAP_AUDIT_CONTROL); 9681 EXPECT_EQ(0, audit_cleanup(-1, NULL)); 9682 clear_cap(_metadata, CAP_AUDIT_CONTROL); 9683 } 9684 9685 static void simple_quiet_rename(struct __test_metadata *const _metadata, 9686 FIXTURE_DATA(audit_quiet_rename) *const self, 9687 __u64 handled_access, __u64 quiet_access, 9688 bool source_allow, bool dest_allow, 9689 bool source_quiet, bool dest_quiet, 9690 const char *source_blockers, 9691 const char *dest_blockers) 9692 { 9693 /* We will move file1_s1d1 to file1_s2d1 */ 9694 struct a_layer layer = { 9695 .handled_access_fs = handled_access, 9696 .quiet_access_fs = quiet_access, 9697 .rules = { 9698 { 9699 .path = dir_s1d1, 9700 .access = source_allow ? handled_access : 0, 9701 .quiet = source_quiet, 9702 }, 9703 { 9704 .path = dir_s2d1, 9705 .access = dest_allow ? handled_access : 0, 9706 .quiet = dest_quiet, 9707 }, 9708 }, 9709 }; 9710 struct audit_records records = {}; 9711 int ret, err; 9712 9713 /* Skip landlock_add_rule for useless rules. */ 9714 if (!source_allow && !source_quiet) 9715 layer.rules[0].path = NULL; 9716 if (!dest_allow && !dest_quiet) 9717 layer.rules[1].path = NULL; 9718 9719 EXPECT_EQ(0, unlink(file1_s2d1)); 9720 EXPECT_EQ(0, apply_a_layer(_metadata, &layer)); 9721 9722 if (debug_quiet_tests) 9723 TH_LOG("Try renameat \"%s\" to \"%s\"", file1_s1d1, file1_s2d1); 9724 ret = renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1); 9725 err = errno; 9726 if (ret != 0 && debug_quiet_tests) { 9727 TH_LOG("renameat error: %s", err == EXDEV ? "EXDEV" : 9728 err == EACCES ? "EACCES" : 9729 strerror(err)); 9730 } 9731 if (source_allow && dest_allow) { 9732 ASSERT_EQ(0, ret); 9733 } else { 9734 ASSERT_EQ(-1, ret); 9735 if (handled_access & (LANDLOCK_ACCESS_FS_MAKE_REG | 9736 LANDLOCK_ACCESS_FS_REMOVE_FILE)) { 9737 ASSERT_EQ(EACCES, err); 9738 } else { 9739 ASSERT_EQ(EXDEV, err); 9740 } 9741 9742 if (source_blockers) 9743 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 9744 source_blockers, dir_s1d1)); 9745 if (dest_blockers) 9746 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 9747 dest_blockers, dir_s2d1)); 9748 } 9749 /* 9750 * No other logs. records.domain not checked per reasoning in 9751 * audit_quiet_layout1_test_body. 9752 */ 9753 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 9754 ASSERT_EQ(0, records.access); 9755 } 9756 9757 TEST_F(audit_quiet_rename, rename_ok) 9758 { 9759 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9760 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9761 LANDLOCK_ACCESS_FS_REFER; 9762 9763 simple_quiet_rename(_metadata, self, access, access, true, true, false, 9764 false, NULL, NULL); 9765 } 9766 9767 TEST_F(audit_quiet_rename, no_quiet) 9768 { 9769 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9770 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9771 LANDLOCK_ACCESS_FS_REFER; 9772 9773 simple_quiet_rename(_metadata, self, access, access, false, false, 9774 false, false, "fs\\.remove_file,fs\\.refer", 9775 "fs\\.make_reg,fs\\.refer"); 9776 } 9777 9778 TEST_F(audit_quiet_rename, quiet) 9779 { 9780 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9781 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9782 LANDLOCK_ACCESS_FS_REFER; 9783 9784 simple_quiet_rename(_metadata, self, access, access, false, false, true, 9785 true, NULL, NULL); 9786 } 9787 9788 TEST_F(audit_quiet_rename, source_no_quiet_dest_quiet) 9789 { 9790 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9791 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9792 LANDLOCK_ACCESS_FS_REFER; 9793 9794 simple_quiet_rename(_metadata, self, access, access, false, false, 9795 false, true, "fs\\.remove_file,fs\\.refer", NULL); 9796 } 9797 9798 TEST_F(audit_quiet_rename, source_quiet_dest_no_quiet) 9799 { 9800 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9801 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9802 LANDLOCK_ACCESS_FS_REFER; 9803 9804 simple_quiet_rename(_metadata, self, access, access, false, false, true, 9805 false, NULL, "fs\\.make_reg,fs\\.refer"); 9806 } 9807 9808 TEST_F(audit_quiet_rename, only_quiet_refer) 9809 { 9810 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9811 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9812 LANDLOCK_ACCESS_FS_REFER; 9813 9814 simple_quiet_rename(_metadata, self, access, LANDLOCK_ACCESS_FS_REFER, 9815 false, false, true, true, 9816 "fs\\.remove_file,fs\\.refer", 9817 "fs\\.make_reg,fs\\.refer"); 9818 } 9819 9820 TEST_F(audit_quiet_rename, source_allow_dest_quiet) 9821 { 9822 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9823 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9824 LANDLOCK_ACCESS_FS_REFER; 9825 9826 simple_quiet_rename(_metadata, self, access, access, true, false, false, 9827 true, NULL, NULL); 9828 } 9829 9830 TEST_F(audit_quiet_rename, source_quiet_dest_allow) 9831 { 9832 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9833 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9834 LANDLOCK_ACCESS_FS_REFER; 9835 9836 simple_quiet_rename(_metadata, self, access, access, false, true, true, 9837 false, NULL, NULL); 9838 } 9839 9840 TEST_F(audit_quiet_rename, handle_all_deny_quiet_refer) 9841 { 9842 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9843 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9844 LANDLOCK_ACCESS_FS_REFER; 9845 struct a_layer layer = { 9846 .handled_access_fs = access, 9847 .quiet_access_fs = LANDLOCK_ACCESS_FS_REFER, 9848 .rules = { 9849 { 9850 .path = dir_s1d1, 9851 .access = LANDLOCK_ACCESS_FS_MAKE_REG | 9852 LANDLOCK_ACCESS_FS_REMOVE_FILE, 9853 .quiet = true, 9854 }, 9855 { 9856 .path = dir_s2d1, 9857 .access = LANDLOCK_ACCESS_FS_MAKE_REG | 9858 LANDLOCK_ACCESS_FS_REMOVE_FILE, 9859 .quiet = true, 9860 }, 9861 }, 9862 }; 9863 struct audit_records records = {}; 9864 9865 EXPECT_EQ(0, unlink(file1_s2d1)); 9866 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 9867 9868 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 9869 ASSERT_EQ(EXDEV, errno); 9870 9871 /* No logs */ 9872 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 9873 ASSERT_EQ(0, records.access); 9874 } 9875 9876 TEST_F(audit_quiet_rename, handle_all_deny_not_quiet_refer) 9877 { 9878 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9879 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9880 LANDLOCK_ACCESS_FS_REFER; 9881 struct a_layer layer = { 9882 .handled_access_fs = access, 9883 .quiet_access_fs = 0, 9884 .rules = { 9885 { 9886 .path = dir_s1d1, 9887 .access = LANDLOCK_ACCESS_FS_MAKE_REG | 9888 LANDLOCK_ACCESS_FS_REMOVE_FILE, 9889 .quiet = false, 9890 }, 9891 { 9892 .path = dir_s2d1, 9893 .access = LANDLOCK_ACCESS_FS_MAKE_REG | 9894 LANDLOCK_ACCESS_FS_REMOVE_FILE, 9895 .quiet = false, 9896 }, 9897 }, 9898 }; 9899 struct audit_records records = {}; 9900 9901 EXPECT_EQ(0, unlink(file1_s2d1)); 9902 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 9903 9904 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 9905 ASSERT_EQ(EXDEV, errno); 9906 9907 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer", 9908 dir_s1d1)); 9909 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer", 9910 dir_s2d1)); 9911 9912 /* No other logs */ 9913 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 9914 ASSERT_EQ(0, records.access); 9915 } 9916 9917 TEST_F(audit_quiet_rename, handle_all_deny_refer_quiet_source_not_quiet_dest) 9918 { 9919 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9920 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9921 LANDLOCK_ACCESS_FS_REFER; 9922 struct a_layer layer = { 9923 .handled_access_fs = access, 9924 .quiet_access_fs = LANDLOCK_ACCESS_FS_REFER, 9925 .rules = { 9926 { 9927 .path = dir_s1d1, 9928 .access = LANDLOCK_ACCESS_FS_MAKE_REG | 9929 LANDLOCK_ACCESS_FS_REMOVE_FILE, 9930 .quiet = true, 9931 }, 9932 { 9933 .path = dir_s2d1, 9934 .access = LANDLOCK_ACCESS_FS_MAKE_REG | 9935 LANDLOCK_ACCESS_FS_REMOVE_FILE, 9936 .quiet = false, 9937 }, 9938 }, 9939 }; 9940 struct audit_records records = {}; 9941 9942 EXPECT_EQ(0, unlink(file1_s2d1)); 9943 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 9944 9945 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 9946 ASSERT_EQ(EXDEV, errno); 9947 9948 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer", 9949 dir_s2d1)); 9950 9951 /* No other logs */ 9952 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 9953 ASSERT_EQ(0, records.access); 9954 } 9955 9956 TEST_F(audit_quiet_rename, quiet_same_dir) 9957 { 9958 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9959 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9960 LANDLOCK_ACCESS_FS_REFER; 9961 struct a_layer layer = { 9962 .handled_access_fs = access, 9963 .quiet_access_fs = access, 9964 .rules = { 9965 { 9966 .path = dir_s1d1, 9967 .access = 0, 9968 .quiet = true, 9969 }, 9970 }, 9971 }; 9972 struct audit_records records = {}; 9973 9974 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 9975 9976 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file2_s1d1)); 9977 ASSERT_EQ(EACCES, errno); 9978 9979 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 9980 ASSERT_EQ(0, records.access); 9981 } 9982 9983 TEST_F(audit_quiet_rename, quiet_flag_on_file_ignored) 9984 { 9985 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 9986 LANDLOCK_ACCESS_FS_REMOVE_FILE | 9987 LANDLOCK_ACCESS_FS_REFER; 9988 struct a_layer layer = { 9989 .handled_access_fs = access, 9990 .quiet_access_fs = access, 9991 .rules = { 9992 { 9993 .path = file1_s1d1, 9994 .access = 0, 9995 .quiet = true, 9996 }, 9997 { 9998 .path = file1_s2d1, 9999 .access = 0, 10000 .quiet = true, 10001 }, 10002 }, 10003 }; 10004 struct audit_records records = {}; 10005 10006 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 10007 10008 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 10009 ASSERT_EQ(EACCES, errno); 10010 10011 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 10012 "fs\\.remove_file,fs\\.refer", dir_s1d1)); 10013 /* We didn't unlink destination file */ 10014 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 10015 "fs\\.remove_file,fs\\.make_reg,fs\\.refer", 10016 dir_s2d1)); 10017 10018 /* No other logs */ 10019 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10020 ASSERT_EQ(0, records.access); 10021 } 10022 10023 TEST_F(audit_quiet_rename, quiet_flag_on_file_ignored_same_dir) 10024 { 10025 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10026 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10027 LANDLOCK_ACCESS_FS_REFER; 10028 struct a_layer layer = { 10029 .handled_access_fs = access, 10030 .quiet_access_fs = access, 10031 .rules = { 10032 { 10033 .path = file1_s1d1, 10034 .access = 0, 10035 .quiet = true, 10036 }, 10037 { 10038 .path = file2_s1d1, 10039 .access = 0, 10040 .quiet = true, 10041 }, 10042 }, 10043 }; 10044 struct audit_records records = {}; 10045 10046 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 10047 10048 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file2_s1d1)); 10049 ASSERT_EQ(EACCES, errno); 10050 10051 ASSERT_EQ(0, 10052 matches_log_fs(_metadata, self->audit_fd, 10053 "fs\\.remove_file,fs\\.make_reg", dir_s1d1)); 10054 10055 /* No other logs */ 10056 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10057 ASSERT_EQ(0, records.access); 10058 } 10059 10060 TEST_F(audit_quiet_rename, two_layers_different_quiet1) 10061 { 10062 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10063 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10064 LANDLOCK_ACCESS_FS_REFER; 10065 struct a_layer layer1 = { 10066 .handled_access_fs = access, 10067 .quiet_access_fs = access, 10068 .rules = { 10069 { 10070 .path = dir_s1d1, 10071 .access = access, 10072 .quiet = false, 10073 }, 10074 { 10075 .path = dir_s2d1, 10076 .access = 0, 10077 .quiet = true, 10078 }, 10079 }, 10080 }; 10081 struct a_layer layer2 = { 10082 .handled_access_fs = access, 10083 .quiet_access_fs = LANDLOCK_ACCESS_FS_REFER, 10084 .rules = { 10085 { 10086 .path = dir_s1d1, 10087 .access = 0, 10088 .quiet = true, 10089 }, 10090 { 10091 .path = dir_s2d1, 10092 .access = access, 10093 .quiet = false, 10094 }, 10095 }, 10096 }; 10097 struct audit_records records = {}; 10098 10099 EXPECT_EQ(0, unlink(file1_s2d1)); 10100 10101 ASSERT_EQ(0, apply_a_layer(_metadata, &layer1)); 10102 ASSERT_EQ(0, apply_a_layer(_metadata, &layer2)); 10103 10104 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 10105 ASSERT_EQ(EACCES, errno); 10106 10107 /* 10108 * The youngest denial will be layer 2. Refer is quieted but we are 10109 * also missing remove_file on source. 10110 */ 10111 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 10112 "fs\\.remove_file,fs\\.refer", dir_s1d1)); 10113 /* No other logs */ 10114 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10115 ASSERT_EQ(0, records.access); 10116 } 10117 10118 TEST_F(audit_quiet_rename, two_layers_different_quiet2) 10119 { 10120 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10121 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10122 LANDLOCK_ACCESS_FS_REFER; 10123 struct a_layer layer1 = { 10124 .handled_access_fs = access, 10125 .quiet_access_fs = access, 10126 .rules = { 10127 { 10128 .path = dir_s1d1, 10129 .access = access, 10130 .quiet = false, 10131 }, 10132 { 10133 .path = dir_s2d1, 10134 .access = 0, 10135 .quiet = true, 10136 }, 10137 }, 10138 }; 10139 struct a_layer layer2 = { 10140 .handled_access_fs = LANDLOCK_ACCESS_FS_REFER, 10141 .quiet_access_fs = LANDLOCK_ACCESS_FS_REFER, 10142 .rules = { 10143 { 10144 .path = dir_s1d1, 10145 .access = 0, 10146 .quiet = true, 10147 }, 10148 { 10149 .path = dir_s2d1, 10150 .access = LANDLOCK_ACCESS_FS_REFER, 10151 .quiet = false, 10152 }, 10153 }, 10154 }; 10155 struct audit_records records = {}; 10156 10157 EXPECT_EQ(0, unlink(file1_s2d1)); 10158 10159 ASSERT_EQ(0, apply_a_layer(_metadata, &layer1)); 10160 ASSERT_EQ(0, apply_a_layer(_metadata, &layer2)); 10161 10162 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 10163 ASSERT_EQ(EACCES, errno); 10164 10165 /* 10166 * The youngest denial will be layer 2, but refer is quieted (and that 10167 * layer does not handle any other accesses). 10168 */ 10169 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10170 ASSERT_EQ(0, records.access); 10171 } 10172 10173 TEST_F(audit_quiet_rename, two_layers_different_quiet3) 10174 { 10175 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10176 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10177 LANDLOCK_ACCESS_FS_REFER; 10178 struct a_layer layer1 = { 10179 .handled_access_fs = access, 10180 .quiet_access_fs = access, 10181 .rules = { 10182 { 10183 .path = dir_s1d1, 10184 .access = access, 10185 .quiet = false, 10186 }, 10187 { 10188 .path = dir_s2d1, 10189 .access = 0, 10190 .quiet = true, 10191 }, 10192 }, 10193 }; 10194 struct a_layer layer2 = { 10195 .handled_access_fs = access, 10196 .quiet_access_fs = access, 10197 .rules = { 10198 { 10199 .path = dir_s1d1, 10200 .access = 0, 10201 .quiet = true, 10202 }, 10203 { 10204 .path = dir_s2d1, 10205 .access = access, 10206 .quiet = false, 10207 }, 10208 }, 10209 }; 10210 struct audit_records records = {}; 10211 10212 EXPECT_EQ(0, unlink(file1_s2d1)); 10213 10214 ASSERT_EQ(0, apply_a_layer(_metadata, &layer1)); 10215 ASSERT_EQ(0, apply_a_layer(_metadata, &layer2)); 10216 10217 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 10218 ASSERT_EQ(EACCES, errno); 10219 10220 /* 10221 * The youngest denial will be layer 2, in which everything is quieted. 10222 */ 10223 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10224 ASSERT_EQ(0, records.access); 10225 } 10226 10227 TEST_F(audit_quiet_rename, 10228 first_layer_quiet_deny_all_second_layer_not_quiet_deny_all) 10229 { 10230 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10231 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10232 LANDLOCK_ACCESS_FS_REFER; 10233 struct a_layer layer1 = { 10234 .handled_access_fs = access, 10235 .quiet_access_fs = access, 10236 .rules = { 10237 { 10238 .path = dir_s1d1, 10239 .access = 0, 10240 .quiet = true, 10241 }, 10242 { 10243 .path = dir_s2d1, 10244 .access = 0, 10245 .quiet = true, 10246 }, 10247 }, 10248 }; 10249 struct a_layer layer2 = { 10250 .handled_access_fs = access, 10251 .quiet_access_fs = access, 10252 .rules = {}, 10253 }; 10254 struct audit_records records = {}; 10255 10256 EXPECT_EQ(0, unlink(file1_s2d1)); 10257 10258 ASSERT_EQ(0, apply_a_layer(_metadata, &layer1)); 10259 ASSERT_EQ(0, apply_a_layer(_metadata, &layer2)); 10260 10261 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 10262 ASSERT_EQ(EACCES, errno); 10263 10264 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 10265 "fs\\.remove_file,fs\\.refer", dir_s1d1)); 10266 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 10267 "fs\\.make_reg,fs\\.refer", dir_s2d1)); 10268 /* No other logs. */ 10269 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10270 ASSERT_EQ(0, records.access); 10271 } 10272 10273 TEST_F(audit_quiet_rename, 10274 first_layer_quiet_deny_all_second_layer_dest_not_quiet) 10275 { 10276 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10277 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10278 LANDLOCK_ACCESS_FS_REFER; 10279 struct a_layer layer1 = { 10280 .handled_access_fs = access, 10281 .quiet_access_fs = access, 10282 .rules = { 10283 { 10284 .path = dir_s1d1, 10285 .access = 0, 10286 .quiet = true, 10287 }, 10288 { 10289 .path = dir_s2d1, 10290 .access = 0, 10291 .quiet = true, 10292 }, 10293 }, 10294 }; 10295 struct a_layer layer2 = { 10296 .handled_access_fs = access, 10297 .quiet_access_fs = access, 10298 .rules = { 10299 { 10300 .path = dir_s1d1, 10301 .access = 0, 10302 .quiet = true, 10303 }, 10304 }, 10305 }; 10306 struct audit_records records = {}; 10307 10308 EXPECT_EQ(0, unlink(file1_s2d1)); 10309 10310 ASSERT_EQ(0, apply_a_layer(_metadata, &layer1)); 10311 ASSERT_EQ(0, apply_a_layer(_metadata, &layer2)); 10312 10313 ASSERT_EQ(-1, renameat(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1)); 10314 ASSERT_EQ(EACCES, errno); 10315 10316 /* Source is quieted but destination is not. */ 10317 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 10318 "fs\\.make_reg,fs\\.refer", dir_s2d1)); 10319 /* No other logs. */ 10320 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10321 ASSERT_EQ(0, records.access); 10322 } 10323 10324 TEST_F(audit_quiet_rename, rename_xchg) 10325 { 10326 struct a_layer layer = { 10327 .handled_access_fs = LANDLOCK_ACCESS_FS_MAKE_REG | 10328 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10329 LANDLOCK_ACCESS_FS_REFER, 10330 .quiet_access_fs = LANDLOCK_ACCESS_FS_MAKE_REG, 10331 .rules = { { 10332 .path = dir_s1d1, 10333 .access = LANDLOCK_ACCESS_FS_REMOVE_FILE | 10334 LANDLOCK_ACCESS_FS_REFER, 10335 .quiet = true, 10336 }, 10337 { 10338 .path = dir_s2d1, 10339 .access = LANDLOCK_ACCESS_FS_MAKE_REG | 10340 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10341 LANDLOCK_ACCESS_FS_REFER, 10342 .quiet = false, 10343 } }, 10344 }; 10345 struct audit_records records = {}; 10346 10347 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 10348 10349 ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d1, 10350 RENAME_EXCHANGE)); 10351 ASSERT_EQ(EACCES, errno); 10352 10353 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10354 ASSERT_EQ(0, records.access); 10355 } 10356 10357 TEST_F(audit_quiet_rename, quiet_on_parent_mount) 10358 { 10359 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10360 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10361 LANDLOCK_ACCESS_FS_REFER; 10362 struct a_layer layer = { 10363 .handled_access_fs = access, 10364 .quiet_access_fs = access, 10365 .rules = { 10366 { 10367 .path = dir_s2d1, 10368 .access = 0, 10369 .quiet = true, 10370 }, 10371 }, 10372 }; 10373 struct audit_records records = {}; 10374 10375 EXPECT_EQ(0, unlink(file2_s1d3)); 10376 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 10377 10378 ASSERT_EQ(-1, renameat(AT_FDCWD, bind_file1_s1d3, AT_FDCWD, 10379 bind_file2_s1d3)); 10380 ASSERT_EQ(EACCES, errno); 10381 10382 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10383 ASSERT_EQ(0, records.access); 10384 } 10385 10386 TEST_F(audit_quiet_rename, quiet_behind_mountpoint_ignored) 10387 { 10388 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10389 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10390 LANDLOCK_ACCESS_FS_REFER; 10391 struct a_layer layer = { 10392 .handled_access_fs = access, 10393 .quiet_access_fs = access, 10394 .rules = { 10395 { 10396 .path = dir_s1d1, 10397 .access = 0, 10398 .quiet = true, 10399 }, 10400 }, 10401 }; 10402 struct audit_records records = {}; 10403 10404 EXPECT_EQ(0, unlink(file2_s1d3)); 10405 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 10406 10407 ASSERT_EQ(-1, renameat(AT_FDCWD, bind_file1_s1d3, AT_FDCWD, 10408 bind_file2_s1d3)); 10409 ASSERT_EQ(EACCES, errno); 10410 ASSERT_EQ(0, matches_log_fs(_metadata, self->audit_fd, 10411 "fs\\.remove_file,fs\\.make_reg", 10412 bind_dir_s1d3)); 10413 10414 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10415 ASSERT_EQ(0, records.access); 10416 } 10417 10418 TEST_F(audit_quiet_rename, quiet_on_parent_mount_disconnected) 10419 { 10420 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10421 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10422 LANDLOCK_ACCESS_FS_REFER; 10423 struct a_layer layer = { 10424 .handled_access_fs = access, 10425 .quiet_access_fs = access, 10426 .rules = { 10427 { 10428 .path = dir_s2d1, 10429 .access = 0, 10430 .quiet = true, 10431 }, 10432 }, 10433 }; 10434 struct audit_records records = {}; 10435 int bind_s1d3_fd; 10436 10437 EXPECT_EQ(0, unlink(file2_s1d3)); 10438 10439 bind_s1d3_fd = open(bind_dir_s1d3, O_PATH | O_DIRECTORY); 10440 ASSERT_GE(bind_s1d3_fd, 0); 10441 10442 /* Make s1d3 disconnected. */ 10443 create_directory(_metadata, dir_s4d1); 10444 ASSERT_EQ(0, renameat(AT_FDCWD, dir_s1d3, AT_FDCWD, dir_s4d2)); 10445 10446 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 10447 10448 ASSERT_EQ(-1, 10449 renameat(bind_s1d3_fd, file1_name, bind_s1d3_fd, file2_name)); 10450 ASSERT_EQ(EACCES, errno); 10451 10452 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10453 ASSERT_EQ(0, records.access); 10454 } 10455 10456 TEST_F(audit_quiet_rename, quiet_behind_mountpoint_disconnected) 10457 { 10458 __u64 access = LANDLOCK_ACCESS_FS_MAKE_REG | 10459 LANDLOCK_ACCESS_FS_REMOVE_FILE | 10460 LANDLOCK_ACCESS_FS_REFER; 10461 struct a_layer layer = { 10462 .handled_access_fs = access, 10463 .quiet_access_fs = access, 10464 .rules = { 10465 { 10466 .path = dir_s4d1, 10467 .access = 0, 10468 .quiet = true, 10469 }, 10470 }, 10471 }; 10472 struct audit_records records = {}; 10473 int bind_s1d3_fd; 10474 10475 EXPECT_EQ(0, unlink(file2_s1d3)); 10476 10477 bind_s1d3_fd = open(bind_dir_s1d3, O_PATH | O_DIRECTORY); 10478 ASSERT_GE(bind_s1d3_fd, 0); 10479 10480 /* Make s1d3 disconnected. */ 10481 create_directory(_metadata, dir_s4d1); 10482 ASSERT_EQ(0, renameat(AT_FDCWD, dir_s1d3, AT_FDCWD, dir_s4d2)); 10483 10484 ASSERT_EQ(0, apply_a_layer(_metadata, &layer)); 10485 10486 ASSERT_EQ(-1, 10487 renameat(bind_s1d3_fd, file1_name, bind_s1d3_fd, file2_name)); 10488 ASSERT_EQ(EACCES, errno); 10489 10490 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 10491 ASSERT_EQ(0, records.access); 10492 } 10493 10494 /* clang-format off */ 10495 FIXTURE(trace_layout1) { 10496 /* clang-format on */ 10497 int tracefs_ok; 10498 }; 10499 10500 FIXTURE_SETUP(trace_layout1) 10501 { 10502 struct stat st; 10503 10504 /* 10505 * Check tracefs availability before creating the layout, following the 10506 * layout3_fs pattern: skip before any layout creation to avoid leaving 10507 * stale TMP_DIR on skip. 10508 */ 10509 if (stat(TRACEFS_LANDLOCK_DIR, &st)) { 10510 self->tracefs_ok = 0; 10511 SKIP(return, "tracefs not available"); 10512 } 10513 self->tracefs_ok = 1; 10514 10515 /* Isolate tracefs state (PID filter, event enables). */ 10516 set_cap(_metadata, CAP_SYS_ADMIN); 10517 ASSERT_EQ(0, unshare(CLONE_NEWNS)); 10518 ASSERT_EQ(0, mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)); 10519 clear_cap(_metadata, CAP_SYS_ADMIN); 10520 10521 prepare_layout(_metadata); 10522 create_layout1(_metadata); 10523 10524 set_cap(_metadata, CAP_DAC_OVERRIDE); 10525 ASSERT_EQ(0, tracefs_fixture_setup()); 10526 ASSERT_EQ(0, tracefs_enable_event(TRACEFS_CHECK_RULE_FS_ENABLE, true)); 10527 ASSERT_EQ(0, tracefs_clear()); 10528 ASSERT_EQ(0, tracefs_set_pid_filter(getpid())); 10529 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10530 } 10531 10532 FIXTURE_TEARDOWN_PARENT(trace_layout1) 10533 { 10534 if (!self->tracefs_ok) 10535 return; 10536 10537 set_cap(_metadata, CAP_DAC_OVERRIDE); 10538 tracefs_enable_event(TRACEFS_CHECK_RULE_FS_ENABLE, false); 10539 tracefs_clear_pid_filter(); 10540 tracefs_fixture_teardown(); 10541 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10542 10543 remove_layout1(_metadata); 10544 cleanup_layout(_metadata); 10545 } 10546 10547 /* 10548 * Verifies that check_rule_inode events include correct field values: domain, 10549 * dev, ino, access_request, and grants. All values are verified against stat() 10550 * of the rule path on a deterministic tmpfs layout. 10551 */ 10552 TEST_F(trace_layout1, check_rule_fs_fields) 10553 { 10554 struct stat dir_stat; 10555 char expected_dev[32]; 10556 char expected_ino[32]; 10557 char *buf; 10558 char field[64]; 10559 10560 if (!self->tracefs_ok) 10561 SKIP(return, "tracefs not available"); 10562 10563 ASSERT_EQ(0, stat(dir_s1d1, &dir_stat)); 10564 snprintf(expected_dev, sizeof(expected_dev), "%u:%u", 10565 major(dir_stat.st_dev), minor(dir_stat.st_dev)); 10566 snprintf(expected_ino, sizeof(expected_ino), "%lu", dir_stat.st_ino); 10567 10568 set_cap(_metadata, CAP_DAC_OVERRIDE); 10569 ASSERT_EQ(0, tracefs_clear()); 10570 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10571 10572 sandbox_child_fs_access(_metadata, dir_s1d1, 10573 LANDLOCK_ACCESS_FS_READ_DIR, 10574 LANDLOCK_ACCESS_FS_READ_DIR, dir_s1d1); 10575 10576 set_cap(_metadata, CAP_DAC_OVERRIDE); 10577 buf = tracefs_read_trace(); 10578 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10579 ASSERT_NE(NULL, buf); 10580 10581 EXPECT_EQ(1, 10582 tracefs_count_matches(buf, REGEX_CHECK_RULE_FS(TRACE_TASK))) 10583 { 10584 TH_LOG("Expected 1 check_rule_inode event\n%s", buf); 10585 } 10586 10587 ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK), 10588 "dev", field, sizeof(field))); 10589 EXPECT_STREQ(expected_dev, field) 10590 { 10591 TH_LOG("Expected dev=%s, got %s", expected_dev, field); 10592 } 10593 10594 ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK), 10595 "ino", field, sizeof(field))); 10596 EXPECT_STREQ(expected_ino, field) 10597 { 10598 TH_LOG("Expected ino=%s, got %s", expected_ino, field); 10599 } 10600 10601 ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK), 10602 "access_request", field, 10603 sizeof(field))); 10604 EXPECT_STREQ("read_dir", field) 10605 { 10606 TH_LOG("Expected access_request=read_dir, got %s", field); 10607 } 10608 10609 /* 10610 * The domain handles only READ_DIR, so the rule carries the 10611 * unhandled-rights padding; intersecting with the request leaves just 10612 * the requested read_dir (no padding, no hex). 10613 */ 10614 ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK), 10615 "grants", field, sizeof(field))); 10616 EXPECT_STREQ("{read_dir}", field) 10617 { 10618 TH_LOG("Expected grants={read_dir}, got %s", field); 10619 } 10620 10621 free(buf); 10622 } 10623 10624 /* 10625 * Verifies check_rule_inode behavior with multiple rules. With rules at s1d1 10626 * and s1d2 (a child of s1d1), accessing s1d2 produces only 1 event because the 10627 * pathwalk short-circuits after the first rule fully unmasks the single layer. 10628 */ 10629 TEST_F(trace_layout1, check_rule_fs_multiple_rules) 10630 { 10631 pid_t pid; 10632 int status; 10633 char *buf; 10634 int count; 10635 10636 if (!self->tracefs_ok) 10637 SKIP(return, "tracefs not available"); 10638 10639 set_cap(_metadata, CAP_DAC_OVERRIDE); 10640 ASSERT_EQ(0, tracefs_clear()); 10641 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10642 10643 pid = fork(); 10644 ASSERT_LE(0, pid); 10645 10646 if (pid == 0) { 10647 struct landlock_ruleset_attr attr = { 10648 .handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR, 10649 }; 10650 struct landlock_path_beneath_attr path_beneath = { 10651 .allowed_access = LANDLOCK_ACCESS_FS_READ_DIR, 10652 }; 10653 int ruleset_fd, fd; 10654 10655 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 10656 if (ruleset_fd < 0) 10657 _exit(1); 10658 10659 path_beneath.parent_fd = 10660 open(dir_s1d1, O_PATH | O_DIRECTORY | O_CLOEXEC); 10661 if (path_beneath.parent_fd < 0) 10662 _exit(1); 10663 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 10664 &path_beneath, 0)) 10665 _exit(1); 10666 close(path_beneath.parent_fd); 10667 10668 path_beneath.parent_fd = 10669 open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC); 10670 if (path_beneath.parent_fd < 0) 10671 _exit(1); 10672 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 10673 &path_beneath, 0)) 10674 _exit(1); 10675 close(path_beneath.parent_fd); 10676 10677 prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 10678 if (landlock_restrict_self(ruleset_fd, 0)) 10679 _exit(1); 10680 close(ruleset_fd); 10681 10682 fd = open(dir_s1d2, O_RDONLY | O_DIRECTORY | O_CLOEXEC); 10683 if (fd >= 0) 10684 close(fd); 10685 _exit(0); 10686 } 10687 10688 ASSERT_EQ(pid, waitpid(pid, &status, 0)); 10689 ASSERT_TRUE(WIFEXITED(status)); 10690 EXPECT_EQ(0, WEXITSTATUS(status)); 10691 10692 set_cap(_metadata, CAP_DAC_OVERRIDE); 10693 buf = tracefs_read_trace(); 10694 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10695 ASSERT_NE(NULL, buf); 10696 10697 /* 10698 * Only one check_rule_inode event: the rule on dir_s1d2 fully unmasks 10699 * the single layer, so the pathwalk short-circuits before reaching the 10700 * dir_s1d1 rule. 10701 */ 10702 count = tracefs_count_matches(buf, REGEX_CHECK_RULE_FS(TRACE_TASK)); 10703 EXPECT_EQ(1, count) 10704 { 10705 TH_LOG("Expected 1 check_rule_inode event, got %d\n%s", count, 10706 buf); 10707 } 10708 10709 free(buf); 10710 } 10711 10712 /* 10713 * Verifies the grants array is intersected with the request: a handled, 10714 * granted, but unrequested right (execute) is filtered out, leaving only the 10715 * requested read_dir. 10716 */ 10717 TEST_F(trace_layout1, check_rule_fs_request_subset) 10718 { 10719 char *buf; 10720 char field[64]; 10721 10722 if (!self->tracefs_ok) 10723 SKIP(return, "tracefs not available"); 10724 10725 set_cap(_metadata, CAP_DAC_OVERRIDE); 10726 ASSERT_EQ(0, tracefs_clear()); 10727 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10728 10729 /* 10730 * Handle and grant READ_DIR|EXECUTE; the open only requests read_dir. 10731 */ 10732 sandbox_child_fs_access( 10733 _metadata, dir_s1d1, 10734 LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE, 10735 LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE, 10736 dir_s1d1); 10737 10738 set_cap(_metadata, CAP_DAC_OVERRIDE); 10739 buf = tracefs_read_trace(); 10740 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10741 ASSERT_NE(NULL, buf); 10742 10743 ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK), 10744 "access_request", field, 10745 sizeof(field))); 10746 EXPECT_STREQ("read_dir", field); 10747 10748 ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK), 10749 "grants", field, sizeof(field))); 10750 EXPECT_STREQ("{read_dir}", field); 10751 10752 free(buf); 10753 } 10754 10755 /* 10756 * Verifies that the optional TRUNCATE access right, which hook_file_open() 10757 * speculatively evaluates on every open, appears in the access_request= and 10758 * grants= fields. Opening file1_s1d1 read-only needs only read_file, but the 10759 * open hook also evaluates truncate; the domain handles and the rule grants 10760 * both, so the event reports access_request=read_file|truncate and 10761 * grants={read_file|truncate}, and the open is allowed. 10762 */ 10763 TEST_F(trace_layout1, check_rule_fs_optional_access) 10764 { 10765 pid_t pid; 10766 int status; 10767 char *buf; 10768 char field[64]; 10769 int count; 10770 10771 if (!self->tracefs_ok) 10772 SKIP(return, "tracefs not available"); 10773 10774 set_cap(_metadata, CAP_DAC_OVERRIDE); 10775 ASSERT_EQ(0, tracefs_clear()); 10776 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10777 10778 pid = fork(); 10779 ASSERT_LE(0, pid); 10780 10781 if (pid == 0) { 10782 struct landlock_ruleset_attr attr = { 10783 .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE | 10784 LANDLOCK_ACCESS_FS_TRUNCATE, 10785 }; 10786 struct landlock_path_beneath_attr path_beneath = { 10787 .allowed_access = LANDLOCK_ACCESS_FS_READ_FILE | 10788 LANDLOCK_ACCESS_FS_TRUNCATE, 10789 }; 10790 int ruleset_fd, fd; 10791 10792 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 10793 if (ruleset_fd < 0) 10794 _exit(1); 10795 10796 path_beneath.parent_fd = 10797 open(dir_s1d1, O_PATH | O_DIRECTORY | O_CLOEXEC); 10798 if (path_beneath.parent_fd < 0) 10799 _exit(1); 10800 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 10801 &path_beneath, 0)) 10802 _exit(1); 10803 close(path_beneath.parent_fd); 10804 10805 prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 10806 if (landlock_restrict_self(ruleset_fd, 0)) 10807 _exit(1); 10808 close(ruleset_fd); 10809 10810 /* Read-only open needs only read_file; truncate is optional. */ 10811 fd = open(file1_s1d1, O_RDONLY | O_CLOEXEC); 10812 if (fd < 0) 10813 _exit(1); 10814 close(fd); 10815 _exit(0); 10816 } 10817 10818 ASSERT_EQ(pid, waitpid(pid, &status, 0)); 10819 ASSERT_TRUE(WIFEXITED(status)); 10820 /* The open is allowed: the required read_file is granted. */ 10821 EXPECT_EQ(0, WEXITSTATUS(status)); 10822 10823 set_cap(_metadata, CAP_DAC_OVERRIDE); 10824 buf = tracefs_read_trace(); 10825 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10826 ASSERT_NE(NULL, buf); 10827 10828 /* The rule at dir_s1d1 matches when opening file1_s1d1. */ 10829 count = tracefs_count_matches(buf, REGEX_CHECK_RULE_FS(TRACE_TASK)); 10830 EXPECT_EQ(1, count) 10831 { 10832 TH_LOG("Expected 1 check_rule_inode event, got %d\n%s", count, 10833 buf); 10834 } 10835 10836 /* The open hook adds the optional truncate to the request. */ 10837 ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK), 10838 "access_request", field, 10839 sizeof(field))); 10840 EXPECT_STREQ("read_file|truncate", field); 10841 10842 /* The rule grants both, so truncate appears in the grants array. */ 10843 ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK), 10844 "grants", field, sizeof(field))); 10845 EXPECT_STREQ("{read_file|truncate}", field); 10846 10847 free(buf); 10848 } 10849 10850 /* 10851 * Verifies that check_rule_inode fires for a rule matching the inode even when 10852 * it grants none of the requested rights, so the grants set is empty. Landlock 10853 * cannot know a rule ignores the request before reading it, so the event is 10854 * still emitted (grants={}), which lets a tracer see that the rule matched. 10855 * The domain handles READ_DIR|EXECUTE, dir_s1d2 grants only EXECUTE and its 10856 * parent dir_s1d1 grants only READ_DIR. Reading dir_s1d2 (requesting read_dir) 10857 * first matches the dir_s1d2 rule, which grants nothing requested (grants={}); 10858 * walking up to dir_s1d1 then grants read_dir (grants={read_dir}) and allows 10859 * the access. 10860 */ 10861 TEST_F(trace_layout1, check_rule_fs_empty_grant) 10862 { 10863 pid_t pid; 10864 int status; 10865 char *buf; 10866 int count; 10867 10868 if (!self->tracefs_ok) 10869 SKIP(return, "tracefs not available"); 10870 10871 set_cap(_metadata, CAP_DAC_OVERRIDE); 10872 ASSERT_EQ(0, tracefs_clear()); 10873 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10874 10875 pid = fork(); 10876 ASSERT_LE(0, pid); 10877 10878 if (pid == 0) { 10879 struct landlock_ruleset_attr attr = { 10880 .handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR | 10881 LANDLOCK_ACCESS_FS_EXECUTE, 10882 }; 10883 struct landlock_path_beneath_attr path_beneath = {}; 10884 int ruleset_fd, fd; 10885 10886 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 10887 if (ruleset_fd < 0) 10888 _exit(1); 10889 10890 /* Parent dir_s1d1 grants only READ_DIR. */ 10891 path_beneath.allowed_access = LANDLOCK_ACCESS_FS_READ_DIR; 10892 path_beneath.parent_fd = 10893 open(dir_s1d1, O_PATH | O_DIRECTORY | O_CLOEXEC); 10894 if (path_beneath.parent_fd < 0) 10895 _exit(1); 10896 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 10897 &path_beneath, 0)) 10898 _exit(1); 10899 close(path_beneath.parent_fd); 10900 10901 /* Child dir_s1d2 grants only EXECUTE. */ 10902 path_beneath.allowed_access = LANDLOCK_ACCESS_FS_EXECUTE; 10903 path_beneath.parent_fd = 10904 open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC); 10905 if (path_beneath.parent_fd < 0) 10906 _exit(1); 10907 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 10908 &path_beneath, 0)) 10909 _exit(1); 10910 close(path_beneath.parent_fd); 10911 10912 prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 10913 if (landlock_restrict_self(ruleset_fd, 0)) 10914 _exit(1); 10915 close(ruleset_fd); 10916 10917 fd = open(dir_s1d2, O_RDONLY | O_DIRECTORY | O_CLOEXEC); 10918 if (fd < 0) 10919 _exit(1); 10920 close(fd); 10921 _exit(0); 10922 } 10923 10924 ASSERT_EQ(pid, waitpid(pid, &status, 0)); 10925 ASSERT_TRUE(WIFEXITED(status)); 10926 EXPECT_EQ(0, WEXITSTATUS(status)); 10927 10928 set_cap(_metadata, CAP_DAC_OVERRIDE); 10929 buf = tracefs_read_trace(); 10930 clear_cap(_metadata, CAP_DAC_OVERRIDE); 10931 ASSERT_NE(NULL, buf); 10932 10933 /* 10934 * dir_s1d2 (grants nothing requested) then dir_s1d1 (grants read_dir). 10935 */ 10936 count = tracefs_count_matches(buf, REGEX_CHECK_RULE_FS(TRACE_TASK)); 10937 EXPECT_EQ(2, count) 10938 { 10939 TH_LOG("Expected 2 check_rule_inode events, got %d\n%s", count, 10940 buf); 10941 } 10942 10943 /* The dir_s1d2 rule matches the inode but grants none of read_dir. */ 10944 EXPECT_EQ( 10945 1, 10946 tracefs_count_matches( 10947 buf, 10948 TRACE_PREFIX( 10949 TRACE_TASK) "landlock_check_rule_inode: domain=[0-9a-f]\\+ " 10950 "access_request=read_dir " 10951 "dev=[0-9]\\+:[0-9]\\+ ino=[0-9]\\+ " 10952 "grants={}$")) 10953 { 10954 TH_LOG("Expected a grants={} event\n%s", buf); 10955 } 10956 10957 /* Walking up to dir_s1d1 grants the requested read_dir. */ 10958 EXPECT_EQ( 10959 1, 10960 tracefs_count_matches( 10961 buf, 10962 TRACE_PREFIX( 10963 TRACE_TASK) "landlock_check_rule_inode: domain=[0-9a-f]\\+ " 10964 "access_request=read_dir " 10965 "dev=[0-9]\\+:[0-9]\\+ ino=[0-9]\\+ " 10966 "grants={read_dir}$")) 10967 { 10968 TH_LOG("Expected a grants={read_dir} event\n%s", buf); 10969 } 10970 10971 free(buf); 10972 } 10973 10974 TEST_HARNESS_MAIN 10975