1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Landlock tests - Namespace restriction 4 * 5 * Copyright © 2026 Cloudflare, Inc. 6 */ 7 8 #define _GNU_SOURCE 9 #include <errno.h> 10 #include <fcntl.h> 11 #include <linux/capability.h> 12 #include <linux/landlock.h> 13 #include <linux/mount.h> 14 #include <linux/nsfs.h> 15 #include <sched.h> 16 #include <stdio.h> 17 #include <sys/ioctl.h> 18 #include <sys/prctl.h> 19 #include <sys/wait.h> 20 #include <syscall.h> 21 #include <unistd.h> 22 23 /* Defines AT_RECURSIVE without type conflicts. */ 24 #define _ASM_GENERIC_FCNTL_H 25 #include <linux/fcntl.h> 26 27 #include "audit.h" 28 #include "common.h" 29 #include "trace.h" 30 31 #define TRACE_TASK "ns_test" 32 33 /* 34 * Max length for /proc/self/ns/<name> paths (longest: "/proc/self/ns/cgroup"). 35 */ 36 #define NS_PROC_PATH_MAX 32 37 38 static int create_ns_ruleset(void) 39 { 40 const struct landlock_ruleset_attr attr = { 41 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 42 }; 43 44 return landlock_create_ruleset(&attr, sizeof(attr), 0); 45 } 46 47 static int add_ns_rule(int ruleset_fd, __u64 ns_type) 48 { 49 const struct landlock_namespace_attr attr = { 50 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 51 .allowed_namespace_types = ns_type, 52 }; 53 54 return landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, &attr, 0); 55 } 56 57 static int add_ns_rule_full(int ruleset_fd, __u64 allowed, __u64 quiet) 58 { 59 const struct landlock_namespace_attr attr = { 60 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 61 .allowed_namespace_types = allowed, 62 .quiet_namespace_types = quiet, 63 }; 64 65 return landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, &attr, 0); 66 } 67 68 /* 69 * Returns the /proc/self/NS entry name for a given CLONE_NEW* type, or NULL if 70 * unknown. Used to check kernel support without side effects. 71 */ 72 static const char *ns_proc_name(__u64 ns_type) 73 { 74 switch (ns_type) { 75 case CLONE_NEWNS: 76 return "mnt"; 77 case CLONE_NEWCGROUP: 78 return "cgroup"; 79 case CLONE_NEWUTS: 80 return "uts"; 81 case CLONE_NEWIPC: 82 return "ipc"; 83 case CLONE_NEWUSER: 84 return "user"; 85 case CLONE_NEWPID: 86 return "pid"; 87 case CLONE_NEWNET: 88 return "net"; 89 case CLONE_NEWTIME: 90 return "time"; 91 default: 92 return NULL; 93 } 94 } 95 96 static bool ns_is_supported(__u64 ns_type, char *proc_path, size_t size) 97 { 98 const char *ns_name; 99 100 ns_name = ns_proc_name(ns_type); 101 if (!ns_name) 102 return false; 103 104 snprintf(proc_path, size, "/proc/self/ns/%s", ns_name); 105 return access(proc_path, F_OK) == 0; 106 } 107 108 /* Rule validation tests */ 109 110 TEST(add_rule_bad_attr) 111 { 112 const struct landlock_ruleset_attr cap_only_attr = { 113 .handled_permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 114 }; 115 int ruleset_fd; 116 struct landlock_namespace_attr attr = {}; 117 118 ruleset_fd = create_ns_ruleset(); 119 ASSERT_LE(0, ruleset_fd); 120 121 /* Invalid rule pointer returns EFAULT. */ 122 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 123 NULL, 0)); 124 ASSERT_EQ(EFAULT, errno); 125 126 /* Empty permissions selector returns ENOMSG. */ 127 attr.permissions = 0; 128 attr.allowed_namespace_types = CLONE_NEWUTS; 129 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 130 &attr, 0)); 131 ASSERT_EQ(ENOMSG, errno); 132 133 /* Valid namespace selector plus an extra unhandled selector bit. */ 134 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | 135 LANDLOCK_PERMISSION_CAPABILITY_USE; 136 attr.allowed_namespace_types = CLONE_NEWUTS; 137 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 138 &attr, 0)); 139 ASSERT_EQ(EINVAL, errno); 140 141 /* permissions with wrong type. */ 142 attr.permissions = LANDLOCK_PERMISSION_CAPABILITY_USE; 143 attr.allowed_namespace_types = CLONE_NEWUTS; 144 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 145 &attr, 0)); 146 ASSERT_EQ(EINVAL, errno); 147 148 /* 149 * Unknown namespace bits (e.g. bit 63) are silently accepted for 150 * forward compatibility. Only known CLONE_NEW* bits are stored. 151 */ 152 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE; 153 attr.allowed_namespace_types = 1ULL << 63; 154 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 155 &attr, 0)); 156 157 /* Useless rule: neither allowed nor quiet types set returns ENOMSG. */ 158 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE; 159 attr.allowed_namespace_types = 0; 160 attr.quiet_namespace_types = 0; 161 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 162 &attr, 0)); 163 ASSERT_EQ(ENOMSG, errno); 164 165 /* Quiet-only rule (empty allowed set) is legal. */ 166 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE; 167 attr.allowed_namespace_types = 0; 168 attr.quiet_namespace_types = CLONE_NEWUTS; 169 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 170 &attr, 0)); 171 172 /* Allow and quiet in the same rule. */ 173 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE; 174 attr.allowed_namespace_types = CLONE_NEWNET; 175 attr.quiet_namespace_types = CLONE_NEWUTS; 176 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 177 &attr, 0)); 178 attr.quiet_namespace_types = 0; 179 180 /* 181 * Bit 1 is not a CLONE_NEW* value but is silently accepted for forward 182 * compatibility (no hole rejection). 183 */ 184 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE; 185 attr.allowed_namespace_types = (1ULL << 1); 186 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 187 &attr, 0)); 188 189 /* Multi-bit values are valid (bitmask allows multiple types). */ 190 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE; 191 attr.allowed_namespace_types = CLONE_NEWUTS | CLONE_NEWNET; 192 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 193 &attr, 0)); 194 195 /* 196 * LANDLOCK_ADD_RULE_QUIET (and any other flag) is filesystem/network 197 * only and must be rejected for namespace rules, even when every attr 198 * field is otherwise valid. 199 */ 200 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE; 201 attr.allowed_namespace_types = CLONE_NEWUTS; 202 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 203 &attr, LANDLOCK_ADD_RULE_QUIET)); 204 ASSERT_EQ(EINVAL, errno); 205 206 EXPECT_EQ(0, close(ruleset_fd)); 207 208 /* 209 * Ruleset handles LANDLOCK_PERMISSION_CAPABILITY_USE but not 210 * LANDLOCK_PERMISSION_NAMESPACE_USE: adding a namespace rule must be 211 * rejected. 212 */ 213 ruleset_fd = landlock_create_ruleset(&cap_only_attr, 214 sizeof(cap_only_attr), 0); 215 ASSERT_LE(0, ruleset_fd); 216 attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE; 217 attr.allowed_namespace_types = CLONE_NEWUTS; 218 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 219 &attr, 0)); 220 ASSERT_EQ(EINVAL, errno); 221 EXPECT_EQ(0, close(ruleset_fd)); 222 } 223 224 /* 225 * Unknown namespace types in the upper range are silently accepted (allow-list: 226 * they have no effect since the kernel never checks them). 227 */ 228 TEST(add_rule_unknown) 229 { 230 int ruleset_fd; 231 struct landlock_namespace_attr attr = { 232 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 233 }; 234 235 ruleset_fd = create_ns_ruleset(); 236 ASSERT_LE(0, ruleset_fd); 237 238 /* 239 * Bit 31 is in the lower 32 bits but not a CLONE_NEW* value. Silently 240 * accepted for forward compatibility (no hole rejection). 241 */ 242 attr.allowed_namespace_types = 1ULL << 31; 243 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 244 &attr, 0)); 245 246 /* Bit 32 is in the unknown upper range: silently accepted. */ 247 attr.allowed_namespace_types = 1ULL << 32; 248 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 249 &attr, 0)); 250 251 EXPECT_EQ(0, close(ruleset_fd)); 252 } 253 254 /* 255 * A rule that lists only namespace bits unknown to the running kernel is 256 * accepted by landlock_add_rule() but has no runtime effect: once the domain is 257 * enforced, any actual CLONE_NEW* operation is still denied by the per-category 258 * deny-by-default behaviour. This documents the forward-compatibility 259 * contract: unknown bits are silently accepted so the same policy can be loaded 260 * across kernels, but they never grant a permission that the running kernel 261 * knows nothing about. 262 */ 263 TEST(add_rule_unknown_no_runtime_effect) 264 { 265 const struct landlock_ruleset_attr ruleset_attr = { 266 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 267 }; 268 struct landlock_namespace_attr attr = { 269 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 270 /* Only unknown bits: bit 31 (in lower 32) and bit 32. */ 271 .allowed_namespace_types = (1ULL << 31) | (1ULL << 32), 272 }; 273 int ruleset_fd; 274 275 disable_caps(_metadata); 276 277 ruleset_fd = 278 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 279 ASSERT_LE(0, ruleset_fd); 280 281 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 282 &attr, 0)); 283 284 /* 285 * Baseline: absent the domain, creating CLONE_NEWUTS succeeds. This 286 * ensures the EPERM below is attributable to Landlock rather than to a 287 * missing capability, and fails if the hook always allows. 288 */ 289 set_cap(_metadata, CAP_SYS_ADMIN); 290 ASSERT_EQ(0, unshare(CLONE_NEWUTS)); 291 292 enforce_ruleset(_metadata, ruleset_fd); 293 EXPECT_EQ(0, close(ruleset_fd)); 294 295 /* 296 * CLONE_NEWUTS is a real, known CLONE_NEW* type but was not authorised 297 * by the rule above; deny-by-default applies. 298 */ 299 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 300 EXPECT_EQ(EPERM, errno); 301 clear_cap(_metadata, CAP_SYS_ADMIN); 302 } 303 304 /* 305 * The install-hook mirror of add_rule_unknown_no_runtime_effect: a rule listing 306 * only unknown namespace bits has no runtime effect on setns(2) either. Once 307 * enforced, entering a known namespace type is still denied by the per-category 308 * deny-by-default behaviour. 309 */ 310 TEST(add_rule_unknown_no_runtime_effect_setns) 311 { 312 const struct landlock_ruleset_attr ruleset_attr = { 313 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 314 }; 315 struct landlock_namespace_attr attr = { 316 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 317 /* Only unknown bits: bit 31 (in lower 32) and bit 32. */ 318 .allowed_namespace_types = (1ULL << 31) | (1ULL << 32), 319 }; 320 int ruleset_fd, ns_fd; 321 322 disable_caps(_metadata); 323 324 /* Open the NS FD before enforcing the domain. */ 325 ns_fd = open("/proc/self/ns/uts", O_RDONLY); 326 ASSERT_LE(0, ns_fd); 327 328 ruleset_fd = 329 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 330 ASSERT_LE(0, ruleset_fd); 331 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 332 &attr, 0)); 333 334 /* 335 * Baseline: absent the domain, entering the process's own UTS namespace 336 * succeeds, so the later EPERM is attributable to Landlock rather than 337 * to a missing capability, and fails if the hook always allows. 338 */ 339 set_cap(_metadata, CAP_SYS_ADMIN); 340 ASSERT_EQ(0, setns(ns_fd, CLONE_NEWUTS)); 341 342 enforce_ruleset(_metadata, ruleset_fd); 343 EXPECT_EQ(0, close(ruleset_fd)); 344 345 /* 346 * CLONE_NEWUTS is a real, known CLONE_NEW* type but was not authorised 347 * by the rule above; deny-by-default applies at the install hook. 348 */ 349 EXPECT_EQ(-1, setns(ns_fd, CLONE_NEWUTS)); 350 EXPECT_EQ(EPERM, errno); 351 clear_cap(_metadata, CAP_SYS_ADMIN); 352 EXPECT_EQ(0, close(ns_fd)); 353 } 354 355 /* Namespace creation tests (variant-based positive/negative) */ 356 357 /* clang-format off */ 358 FIXTURE(ns_create) { 359 /* clang-format on */ 360 char proc_path[NS_PROC_PATH_MAX]; 361 }; 362 363 FIXTURE_VARIANT(ns_create) 364 { 365 const __u64 namespace_types; 366 const bool is_sandboxed; 367 const bool has_rule; 368 const bool drop_all_caps; 369 const int expected_result; 370 }; 371 372 /* 373 * Unsandboxed baseline: no Landlock domain is enforced. User namespace creation 374 * should succeed without any restriction. 375 */ 376 /* clang-format off */ 377 FIXTURE_VARIANT_ADD(ns_create, user_unsandboxed) { 378 /* clang-format on */ 379 .namespace_types = CLONE_NEWUSER, 380 .is_sandboxed = false, 381 .has_rule = false, 382 .drop_all_caps = false, 383 .expected_result = 0, 384 }; 385 386 /* 387 * User namespace creation denied: handled by Landlock but no rule allows 388 * CLONE_NEWUSER. 389 */ 390 /* clang-format off */ 391 FIXTURE_VARIANT_ADD(ns_create, user_denied) { 392 /* clang-format on */ 393 .namespace_types = CLONE_NEWUSER, 394 .is_sandboxed = true, 395 .has_rule = false, 396 .drop_all_caps = false, 397 .expected_result = EPERM, 398 }; 399 400 /* User namespace creation allowed: Landlock rule permits CLONE_NEWUSER. */ 401 /* clang-format off */ 402 FIXTURE_VARIANT_ADD(ns_create, user_allowed) { 403 /* clang-format on */ 404 .namespace_types = CLONE_NEWUSER, 405 .is_sandboxed = true, 406 .has_rule = true, 407 .drop_all_caps = false, 408 .expected_result = 0, 409 }; 410 411 /* 412 * User namespace creation while unprivileged: the process has no capabilities 413 * but unshare(CLONE_NEWUSER) is an unprivileged operation so it still succeeds. 414 * The Landlock rule allows it. For setns, the capability check (CAP_SYS_ADMIN) 415 * fails first since the process has no capabilities, yielding EPERM. 416 */ 417 /* clang-format off */ 418 FIXTURE_VARIANT_ADD(ns_create, user_unprivileged) { 419 /* clang-format on */ 420 .namespace_types = CLONE_NEWUSER, 421 .is_sandboxed = true, 422 .has_rule = true, 423 .drop_all_caps = true, 424 .expected_result = 0, 425 }; 426 427 /* 428 * Unsandboxed baseline for non-user namespace: no Landlock domain, process has 429 * CAP_SYS_ADMIN. UTS creation should succeed. 430 */ 431 /* clang-format off */ 432 FIXTURE_VARIANT_ADD(ns_create, uts_unsandboxed) { 433 /* clang-format on */ 434 .namespace_types = CLONE_NEWUTS, 435 .is_sandboxed = false, 436 .has_rule = false, 437 .drop_all_caps = false, 438 .expected_result = 0, 439 }; 440 441 /* 442 * Non-user namespace denied: process has CAP_SYS_ADMIN (passes ns_capable), but 443 * Landlock denies (no rule). 444 */ 445 /* clang-format off */ 446 FIXTURE_VARIANT_ADD(ns_create, uts_denied) { 447 /* clang-format on */ 448 .namespace_types = CLONE_NEWUTS, 449 .is_sandboxed = true, 450 .has_rule = false, 451 .drop_all_caps = false, 452 .expected_result = EPERM, 453 }; 454 455 /* 456 * Non-user namespace allowed: process has CAP_SYS_ADMIN and Landlock rule 457 * permits CLONE_NEWUTS. 458 */ 459 /* clang-format off */ 460 FIXTURE_VARIANT_ADD(ns_create, uts_allowed) { 461 .namespace_types = CLONE_NEWUTS, 462 .is_sandboxed = true, 463 .has_rule = true, 464 .drop_all_caps = false, 465 .expected_result = 0, 466 }; 467 /* clang-format on */ 468 469 /* 470 * Unprivileged namespace creation: process lacks CAP_SYS_ADMIN, so the kernel 471 * denies creation regardless of Landlock rules. Landlock cannot authorize what 472 * the kernel denied (LSM hooks are restriction-only). The rule is present to 473 * verify Landlock does not change the error code. 474 */ 475 /* clang-format off */ 476 FIXTURE_VARIANT_ADD(ns_create, uts_unprivileged) { 477 /* clang-format on */ 478 .namespace_types = CLONE_NEWUTS, 479 .is_sandboxed = true, 480 .has_rule = true, 481 .drop_all_caps = true, 482 .expected_result = EPERM, 483 }; 484 485 /* clang-format off */ 486 FIXTURE_VARIANT_ADD(ns_create, ipc_denied) { 487 /* clang-format on */ 488 .namespace_types = CLONE_NEWIPC, 489 .is_sandboxed = true, 490 .has_rule = false, 491 .drop_all_caps = false, 492 .expected_result = EPERM, 493 }; 494 495 /* clang-format off */ 496 FIXTURE_VARIANT_ADD(ns_create, ipc_allowed) { 497 .namespace_types = CLONE_NEWIPC, 498 .is_sandboxed = true, 499 .has_rule = true, 500 .drop_all_caps = false, 501 .expected_result = 0, 502 }; 503 /* clang-format on */ 504 505 /* clang-format off */ 506 FIXTURE_VARIANT_ADD(ns_create, ipc_unprivileged) { 507 /* clang-format on */ 508 .namespace_types = CLONE_NEWIPC, 509 .is_sandboxed = true, 510 .has_rule = true, 511 .drop_all_caps = true, 512 .expected_result = EPERM, 513 }; 514 515 /* clang-format off */ 516 FIXTURE_VARIANT_ADD(ns_create, mnt_denied) { 517 /* clang-format on */ 518 .namespace_types = CLONE_NEWNS, 519 .is_sandboxed = true, 520 .has_rule = false, 521 .drop_all_caps = false, 522 .expected_result = EPERM, 523 }; 524 525 /* clang-format off */ 526 FIXTURE_VARIANT_ADD(ns_create, mnt_allowed) { 527 .namespace_types = CLONE_NEWNS, 528 .is_sandboxed = true, 529 .has_rule = true, 530 .drop_all_caps = false, 531 .expected_result = 0, 532 }; 533 /* clang-format on */ 534 535 /* clang-format off */ 536 FIXTURE_VARIANT_ADD(ns_create, mnt_unprivileged) { 537 /* clang-format on */ 538 .namespace_types = CLONE_NEWNS, 539 .is_sandboxed = true, 540 .has_rule = true, 541 .drop_all_caps = true, 542 .expected_result = EPERM, 543 }; 544 545 /* clang-format off */ 546 FIXTURE_VARIANT_ADD(ns_create, cgroup_denied) { 547 /* clang-format on */ 548 .namespace_types = CLONE_NEWCGROUP, 549 .is_sandboxed = true, 550 .has_rule = false, 551 .drop_all_caps = false, 552 .expected_result = EPERM, 553 }; 554 555 /* clang-format off */ 556 FIXTURE_VARIANT_ADD(ns_create, cgroup_allowed) { 557 /* clang-format on */ 558 .namespace_types = CLONE_NEWCGROUP, 559 .is_sandboxed = true, 560 .has_rule = true, 561 .drop_all_caps = false, 562 .expected_result = 0, 563 }; 564 565 /* clang-format off */ 566 FIXTURE_VARIANT_ADD(ns_create, cgroup_unprivileged) { 567 /* clang-format on */ 568 .namespace_types = CLONE_NEWCGROUP, 569 .is_sandboxed = true, 570 .has_rule = true, 571 .drop_all_caps = true, 572 .expected_result = EPERM, 573 }; 574 575 /* clang-format off */ 576 FIXTURE_VARIANT_ADD(ns_create, pid_denied) { 577 /* clang-format on */ 578 .namespace_types = CLONE_NEWPID, 579 .is_sandboxed = true, 580 .has_rule = false, 581 .drop_all_caps = false, 582 .expected_result = EPERM, 583 }; 584 585 /* clang-format off */ 586 FIXTURE_VARIANT_ADD(ns_create, pid_allowed) { 587 .namespace_types = CLONE_NEWPID, 588 .is_sandboxed = true, 589 .has_rule = true, 590 .drop_all_caps = false, 591 .expected_result = 0, 592 }; 593 /* clang-format on */ 594 595 /* clang-format off */ 596 FIXTURE_VARIANT_ADD(ns_create, pid_unprivileged) { 597 /* clang-format on */ 598 .namespace_types = CLONE_NEWPID, 599 .is_sandboxed = true, 600 .has_rule = true, 601 .drop_all_caps = true, 602 .expected_result = EPERM, 603 }; 604 605 /* clang-format off */ 606 FIXTURE_VARIANT_ADD(ns_create, net_denied) { 607 /* clang-format on */ 608 .namespace_types = CLONE_NEWNET, 609 .is_sandboxed = true, 610 .has_rule = false, 611 .drop_all_caps = false, 612 .expected_result = EPERM, 613 }; 614 615 /* clang-format off */ 616 FIXTURE_VARIANT_ADD(ns_create, net_allowed) { 617 .namespace_types = CLONE_NEWNET, 618 .is_sandboxed = true, 619 .has_rule = true, 620 .drop_all_caps = false, 621 .expected_result = 0, 622 }; 623 /* clang-format on */ 624 625 /* clang-format off */ 626 FIXTURE_VARIANT_ADD(ns_create, net_unprivileged) { 627 /* clang-format on */ 628 .namespace_types = CLONE_NEWNET, 629 .is_sandboxed = true, 630 .has_rule = true, 631 .drop_all_caps = true, 632 .expected_result = EPERM, 633 }; 634 635 /* clang-format off */ 636 FIXTURE_VARIANT_ADD(ns_create, time_denied) { 637 /* clang-format on */ 638 .namespace_types = CLONE_NEWTIME, 639 .is_sandboxed = true, 640 .has_rule = false, 641 .drop_all_caps = false, 642 .expected_result = EPERM, 643 }; 644 645 /* clang-format off */ 646 FIXTURE_VARIANT_ADD(ns_create, time_allowed) { 647 /* clang-format on */ 648 .namespace_types = CLONE_NEWTIME, 649 .is_sandboxed = true, 650 .has_rule = true, 651 .drop_all_caps = false, 652 .expected_result = 0, 653 }; 654 655 /* clang-format off */ 656 FIXTURE_VARIANT_ADD(ns_create, time_unprivileged) { 657 /* clang-format on */ 658 .namespace_types = CLONE_NEWTIME, 659 .is_sandboxed = true, 660 .has_rule = true, 661 .drop_all_caps = true, 662 .expected_result = EPERM, 663 }; 664 665 FIXTURE_SETUP(ns_create) 666 { 667 ASSERT_TRUE(ns_is_supported(variant->namespace_types, self->proc_path, 668 sizeof(self->proc_path))) 669 { 670 TH_LOG("Namespace type 0x%llx not supported", 671 (unsigned long long)variant->namespace_types); 672 } 673 674 if (variant->drop_all_caps) 675 drop_caps(_metadata); 676 else 677 disable_caps(_metadata); 678 } 679 680 FIXTURE_TEARDOWN(ns_create) 681 { 682 } 683 684 TEST_F(ns_create, unshare) 685 { 686 int ruleset_fd, err; 687 688 if (variant->is_sandboxed) { 689 ruleset_fd = create_ns_ruleset(); 690 ASSERT_LE(0, ruleset_fd); 691 692 if (variant->has_rule) 693 ASSERT_EQ(0, add_ns_rule(ruleset_fd, 694 variant->namespace_types)); 695 696 enforce_ruleset(_metadata, ruleset_fd); 697 EXPECT_EQ(0, close(ruleset_fd)); 698 } 699 700 /* 701 * Non-user namespaces need CAP_SYS_ADMIN for the privileged path. User 702 * namespaces and unprivileged tests skip this. 703 */ 704 if (!variant->drop_all_caps && 705 variant->namespace_types != CLONE_NEWUSER) 706 set_cap(_metadata, CAP_SYS_ADMIN); 707 708 err = unshare(variant->namespace_types); 709 if (variant->expected_result) { 710 EXPECT_EQ(-1, err); 711 EXPECT_EQ(variant->expected_result, errno); 712 } else { 713 EXPECT_EQ(0, err); 714 } 715 716 if (!variant->drop_all_caps && 717 variant->namespace_types != CLONE_NEWUSER) 718 clear_cap(_metadata, CAP_SYS_ADMIN); 719 } 720 721 /* 722 * clone3 exercises a different kernel entry point than unshare: it goes through 723 * kernel_clone() -> copy_process() -> copy_namespaces() -> 724 * create_new_namespaces(). Both paths converge at __ns_common_init() -> 725 * security_namespace_init(), but the entry point and argument handling differ. 726 */ 727 TEST_F(ns_create, clone3) 728 { 729 int ruleset_fd, status; 730 pid_t pid; 731 struct clone_args args = {}; 732 733 if (variant->is_sandboxed) { 734 ruleset_fd = create_ns_ruleset(); 735 ASSERT_LE(0, ruleset_fd); 736 737 if (variant->has_rule) 738 ASSERT_EQ(0, add_ns_rule(ruleset_fd, 739 variant->namespace_types)); 740 741 enforce_ruleset(_metadata, ruleset_fd); 742 EXPECT_EQ(0, close(ruleset_fd)); 743 } 744 745 if (!variant->drop_all_caps && 746 variant->namespace_types != CLONE_NEWUSER) 747 set_cap(_metadata, CAP_SYS_ADMIN); 748 749 args.flags = variant->namespace_types; 750 args.exit_signal = SIGCHLD; 751 pid = sys_clone3(&args, sizeof(args)); 752 if (pid == 0) 753 _exit(EXIT_SUCCESS); 754 755 if (variant->expected_result) { 756 EXPECT_EQ(-1, pid); 757 EXPECT_EQ(variant->expected_result, errno); 758 } else { 759 ASSERT_LE(0, pid); 760 ASSERT_EQ(pid, waitpid(pid, &status, 0)); 761 ASSERT_EQ(1, WIFEXITED(status)); 762 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 763 } 764 765 if (!variant->drop_all_caps && 766 variant->namespace_types != CLONE_NEWUSER) 767 clear_cap(_metadata, CAP_SYS_ADMIN); 768 } 769 770 /* 771 * setns exercises the namespace install path: validate_ns() -> 772 * security_namespace_install() -> hook_namespace_install(). This is a 773 * different LSM hook than creation, so it must be tested separately for each 774 * type. 775 * 776 * Mount namespace setns requires both CAP_SYS_ADMIN and CAP_SYS_CHROOT (checked 777 * by mntns_install), so the allowed variant sets both. 778 */ 779 TEST_F(ns_create, setns) 780 { 781 int ruleset_fd, ns_fd, err, expected; 782 783 /* 784 * setns into the process's own user NS returns EINVAL from 785 * userns_install() (rejects re-entry), but when Landlock denies the 786 * operation, security_namespace_install() returns EPERM before 787 * userns_install() runs. 788 */ 789 if (variant->namespace_types == CLONE_NEWUSER && 790 !variant->expected_result) { 791 expected = EINVAL; 792 } else { 793 expected = variant->expected_result; 794 } 795 796 /* Open the NS FD before enforcing the domain. */ 797 ns_fd = open(self->proc_path, O_RDONLY); 798 ASSERT_LE(0, ns_fd); 799 800 if (variant->is_sandboxed) { 801 ruleset_fd = create_ns_ruleset(); 802 ASSERT_LE(0, ruleset_fd); 803 804 if (variant->has_rule) 805 ASSERT_EQ(0, add_ns_rule(ruleset_fd, 806 variant->namespace_types)); 807 808 enforce_ruleset(_metadata, ruleset_fd); 809 EXPECT_EQ(0, close(ruleset_fd)); 810 } 811 812 if (!variant->drop_all_caps) { 813 set_cap(_metadata, CAP_SYS_ADMIN); 814 /* 815 * mntns_install() requires CAP_SYS_CHROOT in addition to 816 * CAP_SYS_ADMIN. 817 */ 818 if (variant->namespace_types == CLONE_NEWNS) 819 set_cap(_metadata, CAP_SYS_CHROOT); 820 } 821 822 err = setns(ns_fd, variant->namespace_types); 823 if (expected) { 824 EXPECT_EQ(-1, err); 825 EXPECT_EQ(expected, errno); 826 } else { 827 EXPECT_EQ(0, err); 828 } 829 830 if (!variant->drop_all_caps) { 831 clear_cap(_metadata, CAP_SYS_ADMIN); 832 if (variant->namespace_types == CLONE_NEWNS) 833 clear_cap(_metadata, CAP_SYS_CHROOT); 834 } 835 836 EXPECT_EQ(0, close(ns_fd)); 837 } 838 839 /* Additional namespace creation tests */ 840 841 /* 842 * When LANDLOCK_PERMISSION_NAMESPACE_USE is not handled by any domain, 843 * namespace creation must produce the same result as without Landlock. Unlike 844 * the unsandboxed variants of ns_create (which have no domain at all), this 845 * test verifies that a domain handling only FS access does not interfere with 846 * namespace operations. 847 */ 848 TEST(ns_create_unhandled) 849 { 850 const struct landlock_ruleset_attr attr = { 851 .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE, 852 }; 853 int ruleset_fd; 854 855 disable_caps(_metadata); 856 857 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 858 ASSERT_LE(0, ruleset_fd); 859 860 enforce_ruleset(_metadata, ruleset_fd); 861 EXPECT_EQ(0, close(ruleset_fd)); 862 863 /* User namespace creation should still work (unhandled). */ 864 EXPECT_EQ(0, unshare(CLONE_NEWUSER)); 865 } 866 867 /* 868 * Layer stacking: both layers must allow CLONE_NEWUSER for the operation to 869 * succeed. Variants exercise the three combinations of per-layer allow/deny 870 * that exercise distinct semantics; the (deny, deny) combination is omitted 871 * because it is covered by every other "deny" test in this file. 872 */ 873 /* clang-format off */ 874 FIXTURE(ns_stacking) {}; 875 /* clang-format on */ 876 877 FIXTURE_VARIANT(ns_stacking) 878 { 879 bool first_layer_allows; 880 bool second_layer_allows; 881 }; 882 883 /* Layer 1 allows, layer 2 denies -> child denies. */ 884 /* clang-format off */ 885 FIXTURE_VARIANT_ADD(ns_stacking, deny) { 886 /* clang-format on */ 887 .first_layer_allows = true, 888 .second_layer_allows = false, 889 }; 890 891 /* Both layers allow CLONE_NEWUSER -> operation succeeds. */ 892 /* clang-format off */ 893 FIXTURE_VARIANT_ADD(ns_stacking, allow) { 894 /* clang-format on */ 895 .first_layer_allows = true, 896 .second_layer_allows = true, 897 }; 898 899 /* 900 * Layer 1 denies, layer 2 allows -> still denied: a child layer cannot grant 901 * what an ancestor layer withheld. This complements the 902 * parent-allows/child-denies variant above; together they verify the walker 903 * checks both layers and accepts only the (allow, allow) cell. 904 */ 905 /* clang-format off */ 906 FIXTURE_VARIANT_ADD(ns_stacking, parent_denies) { 907 /* clang-format on */ 908 .first_layer_allows = false, 909 .second_layer_allows = true, 910 }; 911 912 FIXTURE_SETUP(ns_stacking) 913 { 914 disable_caps(_metadata); 915 } 916 917 FIXTURE_TEARDOWN(ns_stacking) 918 { 919 } 920 921 /* 922 * Verify that any layer can deny an operation: enforcement requires all layers 923 * to allow. Variants exercise the three combinations that exercise distinct 924 * walker paths (allow/deny, allow/allow, deny/allow); only allow/allow lets the 925 * operation through. 926 */ 927 TEST_F(ns_stacking, two_layers) 928 { 929 int ruleset_fd; 930 const bool expect_success = variant->first_layer_allows && 931 variant->second_layer_allows; 932 933 /* First layer: allow or deny depending on variant. */ 934 ruleset_fd = create_ns_ruleset(); 935 ASSERT_LE(0, ruleset_fd); 936 if (variant->first_layer_allows) 937 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWUSER)); 938 enforce_ruleset(_metadata, ruleset_fd); 939 EXPECT_EQ(0, close(ruleset_fd)); 940 941 /* Second layer: allow or deny depending on variant. */ 942 ruleset_fd = create_ns_ruleset(); 943 ASSERT_LE(0, ruleset_fd); 944 if (variant->second_layer_allows) 945 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWUSER)); 946 enforce_ruleset(_metadata, ruleset_fd); 947 EXPECT_EQ(0, close(ruleset_fd)); 948 949 if (expect_success) { 950 EXPECT_EQ(0, unshare(CLONE_NEWUSER)); 951 } else { 952 EXPECT_EQ(-1, unshare(CLONE_NEWUSER)); 953 EXPECT_EQ(EPERM, errno); 954 } 955 } 956 957 /* 958 * Combined capability and namespace permissions in a single domain. Verifies 959 * that both permission types can coexist and are enforced independently. 960 */ 961 TEST(combined_cap_ns) 962 { 963 const struct landlock_ruleset_attr attr = { 964 .handled_permissions = LANDLOCK_PERMISSION_CAPABILITY_USE | 965 LANDLOCK_PERMISSION_NAMESPACE_USE, 966 }; 967 const struct landlock_capability_attr cap_attr = { 968 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 969 .allowed_capabilities = (1ULL << CAP_SYS_ADMIN), 970 }; 971 const struct landlock_namespace_attr ns_attr = { 972 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 973 .allowed_namespace_types = CLONE_NEWUSER, 974 }; 975 int ruleset_fd; 976 977 /* Isolate hostname changes from other tests. */ 978 ASSERT_EQ(0, unshare(CLONE_NEWUTS)); 979 980 disable_caps(_metadata); 981 982 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 983 ASSERT_LE(0, ruleset_fd); 984 985 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY, 986 &cap_attr, 0)); 987 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 988 &ns_attr, 0)); 989 990 enforce_ruleset(_metadata, ruleset_fd); 991 EXPECT_EQ(0, close(ruleset_fd)); 992 993 /* CAP_SYS_ADMIN use allowed by capability rule. */ 994 set_cap(_metadata, CAP_SYS_ADMIN); 995 EXPECT_EQ(0, sethostname("test", 4)); 996 clear_cap(_metadata, CAP_SYS_ADMIN); 997 998 /* CAP_SYS_CHROOT denied (not in allowed capability rules). */ 999 set_cap(_metadata, CAP_SYS_CHROOT); 1000 EXPECT_EQ(-1, chroot("/")); 1001 EXPECT_EQ(EPERM, errno); 1002 clear_cap(_metadata, CAP_SYS_CHROOT); 1003 1004 /* 1005 * UTS namespace creation denied by Landlock (not in allowed namespace 1006 * rules). CAP_SYS_ADMIN is needed for the kernel's ns_capable() check 1007 * to pass, so that Landlock's hook is actually reached. 1008 */ 1009 set_cap(_metadata, CAP_SYS_ADMIN); 1010 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 1011 EXPECT_EQ(EPERM, errno); 1012 clear_cap(_metadata, CAP_SYS_ADMIN); 1013 1014 /* User namespace creation allowed by namespace rule. */ 1015 EXPECT_EQ(0, unshare(CLONE_NEWUSER)); 1016 } 1017 1018 /* 1019 * Partial allow: one namespace type is allowed, another is denied. Verifies 1020 * that rules are per-type. 1021 */ 1022 TEST(ns_create_partial) 1023 { 1024 int ruleset_fd; 1025 1026 disable_caps(_metadata); 1027 1028 ruleset_fd = create_ns_ruleset(); 1029 ASSERT_LE(0, ruleset_fd); 1030 1031 /* Only allow UTS namespace creation. */ 1032 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWUTS)); 1033 1034 enforce_ruleset(_metadata, ruleset_fd); 1035 EXPECT_EQ(0, close(ruleset_fd)); 1036 1037 /* UTS namespace should be allowed. */ 1038 set_cap(_metadata, CAP_SYS_ADMIN); 1039 EXPECT_EQ(0, unshare(CLONE_NEWUTS)); 1040 clear_cap(_metadata, CAP_SYS_ADMIN); 1041 1042 /* User namespace should be denied (no rule). */ 1043 EXPECT_EQ(-1, unshare(CLONE_NEWUSER)); 1044 EXPECT_EQ(EPERM, errno); 1045 } 1046 1047 /* 1048 * open_tree(2) and fsmount(2) acquire a file descriptor referring to a 1049 * newly-created mount namespace. Both call paths funnel into 1050 * security_namespace_init() with CLONE_NEWNS, gated by 1051 * LANDLOCK_PERMISSION_NAMESPACE_USE. Without coverage here, regressions in 1052 * those paths would slip past the suite. 1053 */ 1054 /* clang-format off */ 1055 FIXTURE(ns_mount_fd) {}; 1056 /* clang-format on */ 1057 1058 FIXTURE_VARIANT(ns_mount_fd) 1059 { 1060 bool sandboxed; 1061 bool has_rule; 1062 int expected_errno; 1063 }; 1064 1065 /* clang-format off */ 1066 FIXTURE_VARIANT_ADD(ns_mount_fd, denied) { 1067 /* clang-format on */ 1068 .sandboxed = true, 1069 .has_rule = false, 1070 .expected_errno = EPERM, 1071 }; 1072 1073 /* clang-format off */ 1074 FIXTURE_VARIANT_ADD(ns_mount_fd, allowed) { 1075 /* clang-format on */ 1076 .sandboxed = true, 1077 .has_rule = true, 1078 .expected_errno = 0, 1079 }; 1080 1081 /* clang-format off */ 1082 FIXTURE_VARIANT_ADD(ns_mount_fd, unsandboxed) { 1083 /* clang-format on */ 1084 .sandboxed = false, 1085 .has_rule = false, 1086 .expected_errno = 0, 1087 }; 1088 1089 FIXTURE_SETUP(ns_mount_fd) 1090 { 1091 } 1092 1093 FIXTURE_TEARDOWN(ns_mount_fd) 1094 { 1095 } 1096 1097 /* 1098 * open_tree(OPEN_TREE_CLONE) creates an anonymous mount namespace to hold the 1099 * cloned mount tree. hook_namespace_init() fires with CLONE_NEWNS. 1100 */ 1101 TEST_F(ns_mount_fd, open_tree_clone) 1102 { 1103 int ruleset_fd, fd; 1104 1105 disable_caps(_metadata); 1106 1107 if (variant->sandboxed) { 1108 ruleset_fd = create_ns_ruleset(); 1109 ASSERT_LE(0, ruleset_fd); 1110 if (variant->has_rule) 1111 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWNS)); 1112 enforce_ruleset(_metadata, ruleset_fd); 1113 EXPECT_EQ(0, close(ruleset_fd)); 1114 } 1115 1116 set_cap(_metadata, CAP_SYS_ADMIN); 1117 fd = sys_open_tree(AT_FDCWD, "/", 1118 OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_RECURSIVE); 1119 if (variant->expected_errno) { 1120 EXPECT_EQ(-1, fd); 1121 EXPECT_EQ(variant->expected_errno, errno); 1122 } else { 1123 ASSERT_LE(0, fd); 1124 EXPECT_EQ(0, close(fd)); 1125 } 1126 clear_cap(_metadata, CAP_SYS_ADMIN); 1127 } 1128 1129 /* 1130 * open_tree(OPEN_TREE_NAMESPACE) clones the mount tree into a new 1131 * (non-anonymous) mount namespace. Same hook (CLONE_NEWNS) but a different 1132 * code path inside fs/namespace.c (open_new_namespace -> alloc_mnt_ns). 1133 * OPEN_TREE_NAMESPACE and OPEN_TREE_CLONE are mutually exclusive. 1134 */ 1135 TEST_F(ns_mount_fd, open_tree_namespace) 1136 { 1137 int ruleset_fd, fd; 1138 1139 disable_caps(_metadata); 1140 1141 if (variant->sandboxed) { 1142 ruleset_fd = create_ns_ruleset(); 1143 ASSERT_LE(0, ruleset_fd); 1144 if (variant->has_rule) 1145 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWNS)); 1146 enforce_ruleset(_metadata, ruleset_fd); 1147 EXPECT_EQ(0, close(ruleset_fd)); 1148 } 1149 1150 set_cap(_metadata, CAP_SYS_ADMIN); 1151 fd = sys_open_tree(AT_FDCWD, "/", 1152 OPEN_TREE_NAMESPACE | OPEN_TREE_CLOEXEC | 1153 AT_RECURSIVE); 1154 if (variant->expected_errno) { 1155 EXPECT_EQ(-1, fd); 1156 EXPECT_EQ(variant->expected_errno, errno); 1157 } else { 1158 ASSERT_LE(0, fd); 1159 EXPECT_EQ(0, close(fd)); 1160 } 1161 clear_cap(_metadata, CAP_SYS_ADMIN); 1162 } 1163 1164 /* 1165 * fsmount(2) without FSMOUNT_NAMESPACE creates an anonymous mount namespace to 1166 * attach the new superblock. hook_namespace_init() fires with CLONE_NEWNS. 1167 * The fs context (fsopen + fsconfig) is set up before sandboxing because 1168 * Landlock here only handles the namespace permission. 1169 */ 1170 TEST_F(ns_mount_fd, fsmount_default) 1171 { 1172 int ruleset_fd, fs_fd, mnt_fd; 1173 1174 disable_caps(_metadata); 1175 1176 set_cap(_metadata, CAP_SYS_ADMIN); 1177 fs_fd = sys_fsopen("tmpfs", 0); 1178 ASSERT_LE(0, fs_fd); 1179 ASSERT_EQ(0, sys_fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); 1180 1181 if (variant->sandboxed) { 1182 ruleset_fd = create_ns_ruleset(); 1183 ASSERT_LE(0, ruleset_fd); 1184 if (variant->has_rule) 1185 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWNS)); 1186 enforce_ruleset(_metadata, ruleset_fd); 1187 EXPECT_EQ(0, close(ruleset_fd)); 1188 } 1189 1190 mnt_fd = sys_fsmount(fs_fd, FSMOUNT_CLOEXEC, 0); 1191 if (variant->expected_errno) { 1192 EXPECT_EQ(-1, mnt_fd); 1193 EXPECT_EQ(variant->expected_errno, errno); 1194 } else { 1195 ASSERT_LE(0, mnt_fd); 1196 EXPECT_EQ(0, close(mnt_fd)); 1197 } 1198 EXPECT_EQ(0, close(fs_fd)); 1199 clear_cap(_metadata, CAP_SYS_ADMIN); 1200 } 1201 1202 /* 1203 * fsmount(2) with FSMOUNT_NAMESPACE creates a (non-anonymous) mount namespace 1204 * for the new mount. Same hook as the default path, different code path. 1205 */ 1206 TEST_F(ns_mount_fd, fsmount_namespace) 1207 { 1208 int ruleset_fd, fs_fd, mnt_fd; 1209 1210 disable_caps(_metadata); 1211 1212 set_cap(_metadata, CAP_SYS_ADMIN); 1213 fs_fd = sys_fsopen("tmpfs", 0); 1214 ASSERT_LE(0, fs_fd); 1215 ASSERT_EQ(0, sys_fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); 1216 1217 if (variant->sandboxed) { 1218 ruleset_fd = create_ns_ruleset(); 1219 ASSERT_LE(0, ruleset_fd); 1220 if (variant->has_rule) 1221 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWNS)); 1222 enforce_ruleset(_metadata, ruleset_fd); 1223 EXPECT_EQ(0, close(ruleset_fd)); 1224 } 1225 1226 mnt_fd = sys_fsmount(fs_fd, FSMOUNT_CLOEXEC | FSMOUNT_NAMESPACE, 0); 1227 if (variant->expected_errno) { 1228 EXPECT_EQ(-1, mnt_fd); 1229 EXPECT_EQ(variant->expected_errno, errno); 1230 } else { 1231 ASSERT_LE(0, mnt_fd); 1232 EXPECT_EQ(0, close(mnt_fd)); 1233 } 1234 EXPECT_EQ(0, close(fs_fd)); 1235 clear_cap(_metadata, CAP_SYS_ADMIN); 1236 } 1237 1238 /* 1239 * unshare(2) with multiple CLONE_NEW* flags: when 1240 * LANDLOCK_PERMISSION_NAMESPACE_USE denies any of the requested types, the 1241 * entire syscall fails with EPERM. This documents the kernel's atomic 1242 * behavior: create_new_namespaces(), called by copy_namespaces(), creates 1243 * namespaces sequentially, and the first Landlock denial rolls back the whole 1244 * operation. Mixing CLONE_NEWUSER (no capability check) with another 1245 * CLONE_NEW* type is the typical container-runtime bootstrap pattern. 1246 */ 1247 /* clang-format off */ 1248 FIXTURE(ns_create_multi_flag) {}; 1249 /* clang-format on */ 1250 1251 FIXTURE_VARIANT(ns_create_multi_flag) 1252 { 1253 __u64 allowed_types; 1254 int expected_errno; 1255 }; 1256 1257 /* clang-format off */ 1258 FIXTURE_VARIANT_ADD(ns_create_multi_flag, partial_denied) { 1259 /* clang-format on */ 1260 /* User namespace allowed; UTS namespace denied. */ 1261 .allowed_types = CLONE_NEWUSER, 1262 .expected_errno = EPERM, 1263 }; 1264 1265 /* clang-format off */ 1266 FIXTURE_VARIANT_ADD(ns_create_multi_flag, both_allowed) { 1267 /* clang-format on */ 1268 .allowed_types = CLONE_NEWUSER | CLONE_NEWUTS, 1269 .expected_errno = 0, 1270 }; 1271 1272 FIXTURE_SETUP(ns_create_multi_flag) 1273 { 1274 } 1275 1276 FIXTURE_TEARDOWN(ns_create_multi_flag) 1277 { 1278 } 1279 1280 TEST_F(ns_create_multi_flag, unshare) 1281 { 1282 int ruleset_fd, status, err; 1283 pid_t child; 1284 1285 disable_caps(_metadata); 1286 1287 /* Run unshare(2) in a child to avoid polluting the test process. */ 1288 child = fork(); 1289 ASSERT_LE(0, child); 1290 1291 if (child == 0) { 1292 ruleset_fd = create_ns_ruleset(); 1293 ASSERT_LE(0, ruleset_fd); 1294 ASSERT_EQ(0, add_ns_rule(ruleset_fd, variant->allowed_types)); 1295 enforce_ruleset(_metadata, ruleset_fd); 1296 EXPECT_EQ(0, close(ruleset_fd)); 1297 1298 err = unshare(CLONE_NEWUSER | CLONE_NEWUTS); 1299 if (variant->expected_errno) { 1300 EXPECT_EQ(-1, err); 1301 EXPECT_EQ(variant->expected_errno, errno); 1302 } else { 1303 EXPECT_EQ(0, err); 1304 } 1305 _exit(_metadata->exit_code); 1306 } 1307 1308 ASSERT_EQ(child, waitpid(child, &status, 0)); 1309 ASSERT_EQ(1, WIFEXITED(status)); 1310 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 1311 } 1312 1313 /* clang-format off */ 1314 FIXTURE(setns_cross_process) {}; 1315 /* clang-format on */ 1316 1317 FIXTURE_VARIANT(setns_cross_process) 1318 { 1319 bool is_sandboxed; 1320 bool has_rule; 1321 int expected_setns; 1322 }; 1323 1324 /* clang-format off */ 1325 FIXTURE_VARIANT_ADD(setns_cross_process, denied) { 1326 /* clang-format on */ 1327 .is_sandboxed = true, 1328 .has_rule = false, 1329 .expected_setns = EPERM, 1330 }; 1331 1332 /* clang-format off */ 1333 FIXTURE_VARIANT_ADD(setns_cross_process, sandboxed_allowed) { 1334 /* clang-format on */ 1335 .is_sandboxed = true, 1336 .has_rule = true, 1337 .expected_setns = 0, 1338 }; 1339 1340 /* clang-format off */ 1341 FIXTURE_VARIANT_ADD(setns_cross_process, unsandboxed) { 1342 /* clang-format on */ 1343 .is_sandboxed = false, 1344 .has_rule = false, 1345 .expected_setns = 0, 1346 }; 1347 1348 FIXTURE_SETUP(setns_cross_process) 1349 { 1350 } 1351 1352 FIXTURE_TEARDOWN(setns_cross_process) 1353 { 1354 } 1355 1356 /* 1357 * setns into a child's UTS namespace: when sandboxed with 1358 * LANDLOCK_PERMISSION_NAMESPACE_USE denying UTS, the rule-based check applies 1359 * regardless of which process created the namespace. This fixture exercises 1360 * only CLONE_NEWUTS; the same enforcement applies to every namespace type (see 1361 * hook_namespace_install() in security/landlock/ns.c), so per-type variants 1362 * would not exercise different code paths. 1363 */ 1364 TEST_F(setns_cross_process, setns) 1365 { 1366 int ruleset_fd, ns_fd, status; 1367 pid_t child; 1368 int pipe_parent[2], pipe_child[2]; 1369 char buf, path[64]; 1370 1371 disable_caps(_metadata); 1372 1373 /* 1374 * Enable dumpable so the parent can read /proc/<child>/ns/uts. Without 1375 * this, ptrace access checks (PTRACE_MODE_READ) prevent opening another 1376 * process's namespace entries. 1377 */ 1378 ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)); 1379 1380 ASSERT_EQ(0, pipe2(pipe_parent, O_CLOEXEC)); 1381 ASSERT_EQ(0, pipe2(pipe_child, O_CLOEXEC)); 1382 1383 child = fork(); 1384 ASSERT_LE(0, child); 1385 1386 if (child == 0) { 1387 EXPECT_EQ(0, close(pipe_parent[1])); 1388 EXPECT_EQ(0, close(pipe_child[0])); 1389 1390 /* Child: create a UTS namespace. */ 1391 set_cap(_metadata, CAP_SYS_ADMIN); 1392 ASSERT_EQ(0, unshare(CLONE_NEWUTS)); 1393 1394 drop_caps(_metadata); 1395 ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)); 1396 1397 /* Signal parent that the namespace is ready. */ 1398 ASSERT_EQ(1, write(pipe_child[1], ".", 1)); 1399 1400 /* Wait for parent to finish testing. */ 1401 ASSERT_EQ(1, read(pipe_parent[0], &buf, 1)); 1402 _exit(_metadata->exit_code); 1403 } 1404 1405 EXPECT_EQ(0, close(pipe_parent[0])); 1406 EXPECT_EQ(0, close(pipe_child[1])); 1407 1408 /* Wait for child namespace. */ 1409 ASSERT_EQ(1, read(pipe_child[0], &buf, 1)); 1410 EXPECT_EQ(0, close(pipe_child[0])); 1411 1412 /* Open the child's NS FD BEFORE creating the domain. */ 1413 snprintf(path, sizeof(path), "/proc/%d/ns/uts", child); 1414 ns_fd = open(path, O_RDONLY); 1415 ASSERT_LE(0, ns_fd); 1416 1417 if (variant->is_sandboxed) { 1418 ruleset_fd = create_ns_ruleset(); 1419 ASSERT_LE(0, ruleset_fd); 1420 if (variant->has_rule) 1421 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWUTS)); 1422 enforce_ruleset(_metadata, ruleset_fd); 1423 EXPECT_EQ(0, close(ruleset_fd)); 1424 } 1425 1426 set_cap(_metadata, CAP_SYS_ADMIN); 1427 if (variant->expected_setns) { 1428 EXPECT_EQ(-1, setns(ns_fd, CLONE_NEWUTS)); 1429 EXPECT_EQ(variant->expected_setns, errno); 1430 } else { 1431 EXPECT_EQ(0, setns(ns_fd, CLONE_NEWUTS)); 1432 } 1433 clear_cap(_metadata, CAP_SYS_ADMIN); 1434 EXPECT_EQ(0, close(ns_fd)); 1435 1436 /* Release child. */ 1437 ASSERT_EQ(1, write(pipe_parent[1], ".", 1)); 1438 EXPECT_EQ(0, close(pipe_parent[1])); 1439 ASSERT_EQ(child, waitpid(child, &status, 0)); 1440 ASSERT_EQ(1, WIFEXITED(status)); 1441 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 1442 } 1443 1444 /* 1445 * Verify that LANDLOCK_PERMISSION_NAMESPACE_USE and 1446 * LANDLOCK_PERMISSION_CAPABILITY_USE apply simultaneously: creating/entering a 1447 * non-user namespace requires both the namespace type to be allowed AND 1448 * CAP_SYS_ADMIN to be allowed. User namespace creation is the exception (no 1449 * capable() call from the kernel). 1450 */ 1451 TEST(setns_and_create) 1452 { 1453 int ruleset_fd, ns_fd; 1454 const struct landlock_ruleset_attr attr = { 1455 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | 1456 LANDLOCK_PERMISSION_CAPABILITY_USE, 1457 }; 1458 const struct landlock_namespace_attr ns_attr = { 1459 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 1460 .allowed_namespace_types = CLONE_NEWUTS, 1461 }; 1462 const struct landlock_capability_attr cap_attr = { 1463 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 1464 .allowed_capabilities = (1ULL << CAP_SYS_ADMIN), 1465 }; 1466 1467 disable_caps(_metadata); 1468 1469 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 1470 ASSERT_LE(0, ruleset_fd); 1471 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 1472 &ns_attr, 0)); 1473 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY, 1474 &cap_attr, 0)); 1475 enforce_ruleset(_metadata, ruleset_fd); 1476 EXPECT_EQ(0, close(ruleset_fd)); 1477 1478 /* UTS unshare: allowed by NS rule + CAP_SYS_ADMIN allowed. */ 1479 set_cap(_metadata, CAP_SYS_ADMIN); 1480 ASSERT_EQ(0, unshare(CLONE_NEWUTS)); 1481 1482 /* IPC unshare: denied by NS rule (type not allowed). */ 1483 EXPECT_EQ(-1, unshare(CLONE_NEWIPC)); 1484 EXPECT_EQ(EPERM, errno); 1485 1486 /* setns into current UTS: allowed by NS rule. */ 1487 ns_fd = open("/proc/self/ns/uts", O_RDONLY); 1488 ASSERT_LE(0, ns_fd); 1489 EXPECT_EQ(0, setns(ns_fd, CLONE_NEWUTS)); 1490 clear_cap(_metadata, CAP_SYS_ADMIN); 1491 EXPECT_EQ(0, close(ns_fd)); 1492 1493 /* 1494 * User namespace creation: only LANDLOCK_PERMISSION_NAMESPACE_USE 1495 * needed (no capable() call from the kernel for user NS). Denied 1496 * because CLONE_NEWUSER is not in the allowed namespace types. 1497 */ 1498 EXPECT_EQ(-1, unshare(CLONE_NEWUSER)); 1499 EXPECT_EQ(EPERM, errno); 1500 } 1501 1502 /* 1503 * Verify that LANDLOCK_PERMISSION_CAPABILITY_USE can deny the CAP_SYS_ADMIN 1504 * check that the kernel performs before the Landlock namespace hook is reached. 1505 * The NS type is allowed but the required capability is not, so the operation 1506 * fails on the capability check. 1507 * 1508 * User namespace creation is the exception: no capable() call, so the operation 1509 * succeeds with just LANDLOCK_PERMISSION_NAMESPACE_USE. 1510 */ 1511 TEST(two_permissions_cap_denied) 1512 { 1513 const struct landlock_ruleset_attr attr = { 1514 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | 1515 LANDLOCK_PERMISSION_CAPABILITY_USE, 1516 }; 1517 const struct landlock_namespace_attr ns_attr = { 1518 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 1519 .allowed_namespace_types = CLONE_NEWUTS | CLONE_NEWUSER, 1520 }; 1521 /* CAP_SYS_ADMIN is NOT allowed. */ 1522 const struct landlock_capability_attr cap_attr = { 1523 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 1524 .allowed_capabilities = (1ULL << CAP_SYS_CHROOT), 1525 }; 1526 int ruleset_fd, ns_fd; 1527 1528 disable_caps(_metadata); 1529 1530 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 1531 ASSERT_LE(0, ruleset_fd); 1532 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 1533 &ns_attr, 0)); 1534 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY, 1535 &cap_attr, 0)); 1536 enforce_ruleset(_metadata, ruleset_fd); 1537 EXPECT_EQ(0, close(ruleset_fd)); 1538 1539 /* 1540 * UTS creation: the process holds CAP_SYS_ADMIN but Landlock denies it 1541 * (not in the cap rule), so the kernel's ns_capable(CAP_SYS_ADMIN) gate 1542 * fails before the namespace hook is reached. 1543 */ 1544 set_cap(_metadata, CAP_SYS_ADMIN); 1545 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 1546 EXPECT_EQ(EPERM, errno); 1547 1548 /* 1549 * setns into the current UTS namespace: the type is allowed but 1550 * CAP_SYS_ADMIN is denied, so the kernel's ns_capable(CAP_SYS_ADMIN) 1551 * gate fails before the namespace hook is reached (the setns capability 1552 * leg, complementing the unshare/create leg above). 1553 */ 1554 ns_fd = open("/proc/self/ns/uts", O_RDONLY); 1555 ASSERT_LE(0, ns_fd); 1556 EXPECT_EQ(-1, setns(ns_fd, CLONE_NEWUTS)); 1557 EXPECT_EQ(EPERM, errno); 1558 EXPECT_EQ(0, close(ns_fd)); 1559 clear_cap(_metadata, CAP_SYS_ADMIN); 1560 1561 /* 1562 * User NS creation: no capable() call from the kernel, so only 1563 * LANDLOCK_PERMISSION_NAMESPACE_USE applies. CLONE_NEWUSER is in the 1564 * allowed set, so this succeeds. 1565 */ 1566 EXPECT_EQ(0, unshare(CLONE_NEWUSER)); 1567 } 1568 1569 /* 1570 * Combining CLONE_NEWUSER with another namespace type in a single unshare(2) 1571 * call does not exempt the non-user namespace from the capability check: the 1572 * kernel checks ns_capable(new_user_ns, CAP_SYS_ADMIN) for it, and the 1573 * namespace-agnostic capability hook denies it when the domain handles 1574 * LANDLOCK_PERMISSION_CAPABILITY_USE without allowing CAP_SYS_ADMIN. Both 1575 * namespace types are allowed here, yet the combined call fails atomically, 1576 * exactly like the equivalent two-call sequence. 1577 */ 1578 TEST(two_permissions_combined_unshare_denied) 1579 { 1580 const struct landlock_ruleset_attr attr = { 1581 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | 1582 LANDLOCK_PERMISSION_CAPABILITY_USE, 1583 }; 1584 /* Both namespace types are allowed; no capability is allowed. */ 1585 const struct landlock_namespace_attr ns_attr = { 1586 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 1587 .allowed_namespace_types = CLONE_NEWUSER | CLONE_NEWUTS, 1588 }; 1589 int ruleset_fd; 1590 1591 disable_caps(_metadata); 1592 1593 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 1594 ASSERT_LE(0, ruleset_fd); 1595 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 1596 &ns_attr, 0)); 1597 enforce_ruleset(_metadata, ruleset_fd); 1598 EXPECT_EQ(0, close(ruleset_fd)); 1599 1600 /* 1601 * CAP_SYS_ADMIN is held so the kernel's commoncap check passes (via 1602 * ownership of the new user namespace) and the denial is attributable 1603 * to Landlock. The UTS leg needs CAP_SYS_ADMIN, which the capability 1604 * hook denies, so the atomic unshare(2) fails and no namespace is 1605 * created. 1606 */ 1607 set_cap(_metadata, CAP_SYS_ADMIN); 1608 EXPECT_EQ(-1, unshare(CLONE_NEWUSER | CLONE_NEWUTS)); 1609 EXPECT_EQ(EPERM, errno); 1610 clear_cap(_metadata, CAP_SYS_ADMIN); 1611 1612 /* The user namespace alone has no capability check, so it succeeds. */ 1613 EXPECT_EQ(0, unshare(CLONE_NEWUSER)); 1614 } 1615 1616 /* 1617 * Positive complement to two_permissions_combined_unshare_denied: allowing 1618 * CAP_SYS_ADMIN lets the same combined unshare(CLONE_NEWUSER | CLONE_NEWUTS) 1619 * succeed, proving the denial above comes from the missing capability allowance 1620 * and not the namespace rule. Run in a child because a successful unshare(2) 1621 * mutates the caller. 1622 */ 1623 TEST(two_permissions_combined_unshare_allowed) 1624 { 1625 const struct landlock_ruleset_attr attr = { 1626 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | 1627 LANDLOCK_PERMISSION_CAPABILITY_USE, 1628 }; 1629 const struct landlock_namespace_attr ns_attr = { 1630 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 1631 .allowed_namespace_types = CLONE_NEWUSER | CLONE_NEWUTS, 1632 }; 1633 const struct landlock_capability_attr cap_attr = { 1634 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 1635 .allowed_capabilities = (1ULL << CAP_SYS_ADMIN), 1636 }; 1637 int ruleset_fd, status; 1638 pid_t child; 1639 1640 disable_caps(_metadata); 1641 1642 child = fork(); 1643 ASSERT_LE(0, child); 1644 if (child == 0) { 1645 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 1646 ASSERT_LE(0, ruleset_fd); 1647 ASSERT_EQ(0, 1648 landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 1649 &ns_attr, 0)); 1650 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, 1651 LANDLOCK_RULE_CAPABILITY, 1652 &cap_attr, 0)); 1653 enforce_ruleset(_metadata, ruleset_fd); 1654 EXPECT_EQ(0, close(ruleset_fd)); 1655 1656 set_cap(_metadata, CAP_SYS_ADMIN); 1657 EXPECT_EQ(0, unshare(CLONE_NEWUSER | CLONE_NEWUTS)); 1658 clear_cap(_metadata, CAP_SYS_ADMIN); 1659 _exit(_metadata->exit_code); 1660 } 1661 ASSERT_EQ(child, waitpid(child, &status, 0)); 1662 ASSERT_EQ(1, WIFEXITED(status)); 1663 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 1664 } 1665 1666 /* 1667 * Inverse of two_permissions_cap_denied: the required capability is allowed but 1668 * the namespace type is not, so the Landlock namespace hook denies the 1669 * operation (the capability leg passes). A positive control (an allowed type 1670 * creates successfully with the same allowed capability) proves CAP_SYS_ADMIN 1671 * is genuinely exercisable, so the UTS denial is attributable to the namespace 1672 * hook rather than the capability hook. 1673 */ 1674 TEST(two_permissions_ns_denied) 1675 { 1676 const struct landlock_ruleset_attr attr = { 1677 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | 1678 LANDLOCK_PERMISSION_CAPABILITY_USE, 1679 }; 1680 /* CLONE_NEWUTS is NOT allowed; CLONE_NEWNET is. */ 1681 const struct landlock_namespace_attr ns_attr = { 1682 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 1683 .allowed_namespace_types = CLONE_NEWNET, 1684 }; 1685 const struct landlock_capability_attr cap_attr = { 1686 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 1687 .allowed_capabilities = (1ULL << CAP_SYS_ADMIN), 1688 }; 1689 int ruleset_fd; 1690 1691 disable_caps(_metadata); 1692 1693 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 1694 ASSERT_LE(0, ruleset_fd); 1695 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 1696 &ns_attr, 0)); 1697 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY, 1698 &cap_attr, 0)); 1699 enforce_ruleset(_metadata, ruleset_fd); 1700 EXPECT_EQ(0, close(ruleset_fd)); 1701 1702 /* 1703 * Positive control: CLONE_NEWNET is allowed and CAP_SYS_ADMIN is 1704 * allowed, so the kernel's ns_capable() gate passes and the namespace 1705 * hook allows it. 1706 */ 1707 set_cap(_metadata, CAP_SYS_ADMIN); 1708 EXPECT_EQ(0, unshare(CLONE_NEWNET)); 1709 1710 /* 1711 * CAP_SYS_ADMIN is allowed (ns_capable passes), but CLONE_NEWUTS is not 1712 * in the allowed namespace types, so the Landlock namespace hook denies 1713 * it. 1714 */ 1715 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 1716 EXPECT_EQ(EPERM, errno); 1717 clear_cap(_metadata, CAP_SYS_ADMIN); 1718 } 1719 1720 /* 1721 * Mount namespace setns is unique: the kernel checks both CAP_SYS_ADMIN and 1722 * CAP_SYS_CHROOT in mntns_install(). Verify that allowing CAP_SYS_ADMIN alone 1723 * is not sufficient. 1724 */ 1725 TEST(two_permissions_mnt_setns) 1726 { 1727 const struct landlock_ruleset_attr attr = { 1728 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | 1729 LANDLOCK_PERMISSION_CAPABILITY_USE, 1730 }; 1731 const struct landlock_namespace_attr ns_attr = { 1732 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 1733 .allowed_namespace_types = CLONE_NEWNS, 1734 }; 1735 const struct landlock_capability_attr cap_admin = { 1736 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 1737 .allowed_capabilities = (1ULL << CAP_SYS_ADMIN), 1738 }; 1739 const struct landlock_capability_attr cap_admin_chroot = { 1740 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 1741 .allowed_capabilities = (1ULL << CAP_SYS_ADMIN) | 1742 (1ULL << CAP_SYS_CHROOT), 1743 }; 1744 int ruleset_fd, ns_fd; 1745 1746 disable_caps(_metadata); 1747 1748 /* Layer 1: allow mount NS + CAP_SYS_ADMIN only (no CAP_SYS_CHROOT). */ 1749 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 1750 ASSERT_LE(0, ruleset_fd); 1751 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 1752 &ns_attr, 0)); 1753 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY, 1754 &cap_admin, 0)); 1755 enforce_ruleset(_metadata, ruleset_fd); 1756 EXPECT_EQ(0, close(ruleset_fd)); 1757 1758 ns_fd = open("/proc/self/ns/mnt", O_RDONLY); 1759 ASSERT_LE(0, ns_fd); 1760 1761 /* 1762 * Fails: mntns_install() checks CAP_SYS_ADMIN (allowed) then 1763 * CAP_SYS_CHROOT (denied by LANDLOCK_PERMISSION_CAPABILITY_USE). 1764 */ 1765 set_cap(_metadata, CAP_SYS_ADMIN); 1766 set_cap(_metadata, CAP_SYS_CHROOT); 1767 EXPECT_EQ(-1, setns(ns_fd, CLONE_NEWNS)); 1768 EXPECT_EQ(EPERM, errno); 1769 clear_cap(_metadata, CAP_SYS_ADMIN); 1770 clear_cap(_metadata, CAP_SYS_CHROOT); 1771 1772 /* Layer 2: also allows CAP_SYS_CHROOT. */ 1773 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 1774 ASSERT_LE(0, ruleset_fd); 1775 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 1776 &ns_attr, 0)); 1777 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY, 1778 &cap_admin_chroot, 0)); 1779 enforce_ruleset(_metadata, ruleset_fd); 1780 EXPECT_EQ(0, close(ruleset_fd)); 1781 1782 /* 1783 * Still fails: layer 1 still denies CAP_SYS_CHROOT. Landlock layer 1784 * stacking means the most restrictive layer wins. 1785 */ 1786 set_cap(_metadata, CAP_SYS_ADMIN); 1787 set_cap(_metadata, CAP_SYS_CHROOT); 1788 EXPECT_EQ(-1, setns(ns_fd, CLONE_NEWNS)); 1789 EXPECT_EQ(EPERM, errno); 1790 clear_cap(_metadata, CAP_SYS_ADMIN); 1791 clear_cap(_metadata, CAP_SYS_CHROOT); 1792 EXPECT_EQ(0, close(ns_fd)); 1793 } 1794 1795 /* 1796 * Positive complement to two_permissions_mnt_setns: a single layer that allows 1797 * the mount namespace type AND both CAP_SYS_ADMIN and CAP_SYS_CHROOT lets 1798 * setns(CLONE_NEWNS) into the process's own mount namespace succeed. Proves 1799 * the denial legs above are caused by the withheld CAP_SYS_CHROOT, not an 1800 * unconditional block of mount setns. 1801 */ 1802 TEST(two_permissions_mnt_setns_allowed) 1803 { 1804 const struct landlock_ruleset_attr attr = { 1805 .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | 1806 LANDLOCK_PERMISSION_CAPABILITY_USE, 1807 }; 1808 const struct landlock_namespace_attr ns_attr = { 1809 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 1810 .allowed_namespace_types = CLONE_NEWNS, 1811 }; 1812 const struct landlock_capability_attr cap_attr = { 1813 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 1814 .allowed_capabilities = (1ULL << CAP_SYS_ADMIN) | 1815 (1ULL << CAP_SYS_CHROOT), 1816 }; 1817 int ruleset_fd, ns_fd; 1818 1819 disable_caps(_metadata); 1820 1821 /* Open the NS FD before enforcing the domain. */ 1822 ns_fd = open("/proc/self/ns/mnt", O_RDONLY); 1823 ASSERT_LE(0, ns_fd); 1824 1825 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); 1826 ASSERT_LE(0, ruleset_fd); 1827 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 1828 &ns_attr, 0)); 1829 ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY, 1830 &cap_attr, 0)); 1831 enforce_ruleset(_metadata, ruleset_fd); 1832 EXPECT_EQ(0, close(ruleset_fd)); 1833 1834 /* 1835 * mntns_install() checks CAP_SYS_ADMIN then CAP_SYS_CHROOT (both 1836 * allowed by the rule) and the mount NS type is allowed, so setns into 1837 * the process's own mount namespace succeeds. 1838 */ 1839 set_cap(_metadata, CAP_SYS_ADMIN); 1840 set_cap(_metadata, CAP_SYS_CHROOT); 1841 EXPECT_EQ(0, setns(ns_fd, CLONE_NEWNS)); 1842 clear_cap(_metadata, CAP_SYS_ADMIN); 1843 clear_cap(_metadata, CAP_SYS_CHROOT); 1844 EXPECT_EQ(0, close(ns_fd)); 1845 } 1846 1847 /* 1848 * setns() through a namespace fd inherited across fork() is still governed by 1849 * the child's own Landlock domain: the child enforces a domain denying 1850 * CLONE_NEWUTS, then joining the inherited /proc/self/ns/uts fd is denied. 1851 */ 1852 TEST(setns_inherited_fd) 1853 { 1854 int ns_fd, status; 1855 pid_t child; 1856 1857 disable_caps(_metadata); 1858 1859 /* Open the UTS ns fd before fork() so the child inherits it. */ 1860 ns_fd = open("/proc/self/ns/uts", O_RDONLY); 1861 ASSERT_LE(0, ns_fd); 1862 1863 child = fork(); 1864 ASSERT_LE(0, child); 1865 if (child == 0) { 1866 int ruleset_fd; 1867 1868 /* 1869 * Baseline: joining the inherited fd works before the domain, 1870 * so the later EPERM is attributable to Landlock. 1871 */ 1872 set_cap(_metadata, CAP_SYS_ADMIN); 1873 ASSERT_EQ(0, setns(ns_fd, CLONE_NEWUTS)); 1874 clear_cap(_metadata, CAP_SYS_ADMIN); 1875 1876 ruleset_fd = create_ns_ruleset(); 1877 ASSERT_LE(0, ruleset_fd); 1878 /* No rule allows UTS. */ 1879 enforce_ruleset(_metadata, ruleset_fd); 1880 EXPECT_EQ(0, close(ruleset_fd)); 1881 1882 set_cap(_metadata, CAP_SYS_ADMIN); 1883 EXPECT_EQ(-1, setns(ns_fd, CLONE_NEWUTS)); 1884 EXPECT_EQ(EPERM, errno); 1885 clear_cap(_metadata, CAP_SYS_ADMIN); 1886 1887 _exit(_metadata->exit_code); 1888 return; 1889 } 1890 1891 EXPECT_EQ(0, close(ns_fd)); 1892 ASSERT_EQ(child, waitpid(child, &status, 0)); 1893 if (WIFSIGNALED(status) || !WIFEXITED(status) || 1894 WEXITSTATUS(status) != EXIT_SUCCESS) 1895 _metadata->exit_code = KSFT_FAIL; 1896 } 1897 1898 /* Audit tests */ 1899 1900 #define REGEX_LANDLOCK_NS_CREATE \ 1901 REGEX_LANDLOCK_PREFIX " blockers=namespace\\.use namespace_op=create" 1902 1903 static int matches_log_ns_create(int audit_fd, __u64 ns_type) 1904 { 1905 /* A denied creation logs the ID allocated for the new namespace. */ 1906 static const char log_template[] = REGEX_LANDLOCK_NS_CREATE 1907 " namespace_type=0x%x namespace_id=[1-9][0-9]*$"; 1908 char log_match[sizeof(log_template) + 10]; 1909 int log_match_len; 1910 1911 log_match_len = snprintf(log_match, sizeof(log_match), log_template, 1912 (unsigned int)ns_type); 1913 if (log_match_len >= sizeof(log_match)) 1914 return -E2BIG; 1915 1916 return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match, 1917 NULL); 1918 } 1919 1920 static int matches_log_ns_setns(int audit_fd, __u64 ns_type, __u64 ns_id) 1921 { 1922 static const char log_template[] = REGEX_LANDLOCK_PREFIX 1923 " blockers=namespace\\.use namespace_op=%s namespace_type=0x%x namespace_id=%llu$"; 1924 char log_match[sizeof(log_template) + 32]; 1925 int log_match_len; 1926 1927 log_match_len = snprintf(log_match, sizeof(log_match), log_template, 1928 "enter", (unsigned int)ns_type, 1929 (unsigned long long)ns_id); 1930 if (log_match_len >= sizeof(log_match)) 1931 return -E2BIG; 1932 1933 return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match, 1934 NULL); 1935 } 1936 1937 FIXTURE(ns_audit) 1938 { 1939 struct audit_filter audit_filter; 1940 int audit_fd; 1941 }; 1942 1943 FIXTURE_SETUP(ns_audit) 1944 { 1945 ASSERT_TRUE(is_in_init_user_ns()); 1946 1947 disable_caps(_metadata); 1948 1949 set_cap(_metadata, CAP_AUDIT_CONTROL); 1950 self->audit_fd = audit_init_with_exe_filter(&self->audit_filter); 1951 EXPECT_LE(0, self->audit_fd); 1952 clear_cap(_metadata, CAP_AUDIT_CONTROL); 1953 } 1954 1955 FIXTURE_TEARDOWN(ns_audit) 1956 { 1957 set_cap(_metadata, CAP_AUDIT_CONTROL); 1958 EXPECT_EQ(0, audit_cleanup(self->audit_fd, &self->audit_filter)); 1959 clear_cap(_metadata, CAP_AUDIT_CONTROL); 1960 } 1961 1962 /* 1963 * Verifies that a denied namespace creation produces the expected audit record 1964 * with the namespace.use blocker string, namespace type, and create operation. 1965 */ 1966 TEST_F(ns_audit, create_denied) 1967 { 1968 struct audit_records records = {}; 1969 int ruleset_fd; 1970 __u64 domain_id; 1971 1972 ruleset_fd = create_ns_ruleset(); 1973 ASSERT_LE(0, ruleset_fd); 1974 enforce_ruleset(_metadata, ruleset_fd); 1975 EXPECT_EQ(0, close(ruleset_fd)); 1976 1977 set_cap(_metadata, CAP_SYS_ADMIN); 1978 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 1979 EXPECT_EQ(EPERM, errno); 1980 clear_cap(_metadata, CAP_SYS_ADMIN); 1981 1982 EXPECT_EQ(0, matches_log_ns_create(self->audit_fd, CLONE_NEWUTS)); 1983 1984 /* 1985 * The domain allocation record is emitted in the same event as the 1986 * first denial; anchor its status=allocated and enforcing pid. Both 1987 * access and domain records are now consumed, so none remain. 1988 */ 1989 EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(), 1990 &domain_id)); 1991 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 1992 EXPECT_EQ(0, records.access); 1993 EXPECT_EQ(0, records.domain); 1994 } 1995 1996 TEST_F(ns_audit, create_allowed) 1997 { 1998 struct audit_records records = {}; 1999 int ruleset_fd; 2000 2001 ruleset_fd = create_ns_ruleset(); 2002 ASSERT_LE(0, ruleset_fd); 2003 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWUTS)); 2004 enforce_ruleset(_metadata, ruleset_fd); 2005 EXPECT_EQ(0, close(ruleset_fd)); 2006 2007 set_cap(_metadata, CAP_SYS_ADMIN); 2008 EXPECT_EQ(0, unshare(CLONE_NEWUTS)); 2009 clear_cap(_metadata, CAP_SYS_ADMIN); 2010 2011 /* No records: allowed operations never trigger audit logging. */ 2012 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2013 EXPECT_EQ(0, records.access); 2014 } 2015 2016 TEST_F(ns_audit, setns_allowed) 2017 { 2018 struct audit_records records = {}; 2019 int ruleset_fd, ns_fd; 2020 2021 ruleset_fd = create_ns_ruleset(); 2022 ASSERT_LE(0, ruleset_fd); 2023 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWUTS)); 2024 enforce_ruleset(_metadata, ruleset_fd); 2025 EXPECT_EQ(0, close(ruleset_fd)); 2026 2027 ns_fd = open("/proc/self/ns/uts", O_RDONLY); 2028 ASSERT_LE(0, ns_fd); 2029 2030 /* Allowed: should succeed with no audit record. */ 2031 set_cap(_metadata, CAP_SYS_ADMIN); 2032 EXPECT_EQ(0, setns(ns_fd, CLONE_NEWUTS)); 2033 clear_cap(_metadata, CAP_SYS_ADMIN); 2034 EXPECT_EQ(0, close(ns_fd)); 2035 2036 /* No records: allowed setns never triggers audit logging. */ 2037 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2038 EXPECT_EQ(0, records.access); 2039 } 2040 2041 TEST_F(ns_audit, setns_denied) 2042 { 2043 struct audit_records records = {}; 2044 int ruleset_fd, ns_fd; 2045 __u64 ns_id, domain_id; 2046 2047 ruleset_fd = create_ns_ruleset(); 2048 ASSERT_LE(0, ruleset_fd); 2049 /* No rule allows UTS -> denied. */ 2050 enforce_ruleset(_metadata, ruleset_fd); 2051 EXPECT_EQ(0, close(ruleset_fd)); 2052 2053 ns_fd = open("/proc/self/ns/uts", O_RDONLY); 2054 ASSERT_LE(0, ns_fd); 2055 2056 /* The audit record logs the target namespace's exact ns_id. */ 2057 ASSERT_EQ(0, ioctl(ns_fd, NS_GET_ID, &ns_id)); 2058 2059 set_cap(_metadata, CAP_SYS_ADMIN); 2060 EXPECT_EQ(-1, setns(ns_fd, CLONE_NEWUTS)); 2061 EXPECT_EQ(EPERM, errno); 2062 clear_cap(_metadata, CAP_SYS_ADMIN); 2063 EXPECT_EQ(0, close(ns_fd)); 2064 2065 /* Verify the audit record for setns denial, anchored to the ns_id. */ 2066 EXPECT_EQ(0, matches_log_ns_setns(self->audit_fd, CLONE_NEWUTS, ns_id)); 2067 2068 /* 2069 * The domain allocation record is emitted in the same event as the 2070 * first denial; anchor its status=allocated and enforcing pid. Both 2071 * access and domain records are now consumed, so none remain. 2072 */ 2073 EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(), 2074 &domain_id)); 2075 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2076 EXPECT_EQ(0, records.access); 2077 EXPECT_EQ(0, records.domain); 2078 } 2079 2080 /* 2081 * A quieted denial is still denied (EPERM); only its audit record is 2082 * suppressed. Quiet-only rule (empty allowed set). 2083 */ 2084 TEST_F(ns_audit, create_quieted) 2085 { 2086 struct audit_records records = {}; 2087 int ruleset_fd; 2088 2089 ruleset_fd = create_ns_ruleset(); 2090 ASSERT_LE(0, ruleset_fd); 2091 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, 0, CLONE_NEWUTS)); 2092 enforce_ruleset(_metadata, ruleset_fd); 2093 EXPECT_EQ(0, close(ruleset_fd)); 2094 2095 set_cap(_metadata, CAP_SYS_ADMIN); 2096 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 2097 EXPECT_EQ(EPERM, errno); 2098 clear_cap(_metadata, CAP_SYS_ADMIN); 2099 2100 /* The denial is suppressed: no access record. */ 2101 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2102 EXPECT_EQ(0, records.access); 2103 } 2104 2105 /* 2106 * A quieted setns denial is still denied (EPERM); only its audit record is 2107 * suppressed. The install-hook mirror of create_quieted. 2108 */ 2109 TEST_F(ns_audit, setns_quieted) 2110 { 2111 struct audit_records records = {}; 2112 int ruleset_fd, ns_fd; 2113 2114 ruleset_fd = create_ns_ruleset(); 2115 ASSERT_LE(0, ruleset_fd); 2116 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, 0, CLONE_NEWUTS)); 2117 enforce_ruleset(_metadata, ruleset_fd); 2118 EXPECT_EQ(0, close(ruleset_fd)); 2119 2120 ns_fd = open("/proc/self/ns/uts", O_RDONLY); 2121 ASSERT_LE(0, ns_fd); 2122 2123 set_cap(_metadata, CAP_SYS_ADMIN); 2124 EXPECT_EQ(-1, setns(ns_fd, CLONE_NEWUTS)); 2125 EXPECT_EQ(EPERM, errno); 2126 clear_cap(_metadata, CAP_SYS_ADMIN); 2127 EXPECT_EQ(0, close(ns_fd)); 2128 2129 /* The denial is suppressed: no access record. */ 2130 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2131 EXPECT_EQ(0, records.access); 2132 } 2133 2134 /* 2135 * Quiet is per-member: quieting one namespace type does not silence the denial 2136 * of another type denied by the same layer. 2137 */ 2138 TEST_F(ns_audit, quiet_is_per_member) 2139 { 2140 struct audit_records records = {}; 2141 int ruleset_fd; 2142 __u64 domain_id; 2143 2144 ruleset_fd = create_ns_ruleset(); 2145 ASSERT_LE(0, ruleset_fd); 2146 /* 2147 * Quiet CLONE_NEWNET only; CLONE_NEWUTS is neither allowed nor quiet. 2148 */ 2149 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, 0, CLONE_NEWNET)); 2150 enforce_ruleset(_metadata, ruleset_fd); 2151 EXPECT_EQ(0, close(ruleset_fd)); 2152 2153 set_cap(_metadata, CAP_SYS_ADMIN); 2154 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 2155 EXPECT_EQ(EPERM, errno); 2156 clear_cap(_metadata, CAP_SYS_ADMIN); 2157 2158 /* The unquieted CLONE_NEWUTS denial is still logged. */ 2159 EXPECT_EQ(0, matches_log_ns_create(self->audit_fd, CLONE_NEWUTS)); 2160 EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(), 2161 &domain_id)); 2162 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2163 EXPECT_EQ(0, records.access); 2164 EXPECT_EQ(0, records.domain); 2165 } 2166 2167 /* 2168 * Quieting an unknown namespace bit has no effect: a rule that quiets only a 2169 * bit the running kernel does not know still logs the denial of a known type. 2170 * Mirrors the forward-compatibility contract for the quiet mask. 2171 */ 2172 TEST_F(ns_audit, quiet_unknown_bit_no_effect) 2173 { 2174 struct audit_records records = {}; 2175 int ruleset_fd; 2176 __u64 domain_id; 2177 2178 ruleset_fd = create_ns_ruleset(); 2179 ASSERT_LE(0, ruleset_fd); 2180 /* Quiet only an unknown bit (bit 31 is not a CLONE_NEW* value). */ 2181 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, 0, (1ULL << 31))); 2182 enforce_ruleset(_metadata, ruleset_fd); 2183 EXPECT_EQ(0, close(ruleset_fd)); 2184 2185 set_cap(_metadata, CAP_SYS_ADMIN); 2186 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 2187 EXPECT_EQ(EPERM, errno); 2188 clear_cap(_metadata, CAP_SYS_ADMIN); 2189 2190 /* The unknown quiet bit does not suppress the known denial. */ 2191 EXPECT_EQ(0, matches_log_ns_create(self->audit_fd, CLONE_NEWUTS)); 2192 EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(), 2193 &domain_id)); 2194 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2195 EXPECT_EQ(0, records.access); 2196 EXPECT_EQ(0, records.domain); 2197 } 2198 2199 /* A single rule can both allow one member and quiet the denial of another. */ 2200 TEST_F(ns_audit, allow_and_quiet) 2201 { 2202 struct audit_records records = {}; 2203 int ruleset_fd; 2204 2205 ruleset_fd = create_ns_ruleset(); 2206 ASSERT_LE(0, ruleset_fd); 2207 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, CLONE_NEWNET, CLONE_NEWUTS)); 2208 enforce_ruleset(_metadata, ruleset_fd); 2209 EXPECT_EQ(0, close(ruleset_fd)); 2210 2211 /* Allowed member: succeeds. */ 2212 set_cap(_metadata, CAP_SYS_ADMIN); 2213 EXPECT_EQ(0, unshare(CLONE_NEWNET)); 2214 /* Quieted member: denied but not logged. */ 2215 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 2216 EXPECT_EQ(EPERM, errno); 2217 clear_cap(_metadata, CAP_SYS_ADMIN); 2218 2219 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2220 EXPECT_EQ(0, records.access); 2221 } 2222 2223 /* 2224 * Quieting a member that is also allowed is inert: an allowed member is never 2225 * denied, so its quiet bit has no effect. 2226 */ 2227 TEST_F(ns_audit, allow_and_quiet_same_member) 2228 { 2229 struct audit_records records = {}; 2230 int ruleset_fd; 2231 2232 ruleset_fd = create_ns_ruleset(); 2233 ASSERT_LE(0, ruleset_fd); 2234 /* Same member both allowed and quieted. */ 2235 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, CLONE_NEWNET, CLONE_NEWNET)); 2236 enforce_ruleset(_metadata, ruleset_fd); 2237 EXPECT_EQ(0, close(ruleset_fd)); 2238 2239 /* The member is allowed (quiet is inert), so it succeeds. */ 2240 set_cap(_metadata, CAP_SYS_ADMIN); 2241 EXPECT_EQ(0, unshare(CLONE_NEWNET)); 2242 clear_cap(_metadata, CAP_SYS_ADMIN); 2243 2244 /* Nothing denied, nothing logged. */ 2245 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2246 EXPECT_EQ(0, records.access); 2247 } 2248 2249 /* 2250 * Only the youngest denying layer decides quieting: a parent that quiets a 2251 * member cannot silence a denial by a deeper layer that handles but does not 2252 * quiet it. 2253 */ 2254 TEST_F(ns_audit, quiet_youngest_layer_wins) 2255 { 2256 struct audit_records records = {}; 2257 int ruleset_fd; 2258 2259 /* Parent layer: handles namespaces and quiets CLONE_NEWUTS. */ 2260 ruleset_fd = create_ns_ruleset(); 2261 ASSERT_LE(0, ruleset_fd); 2262 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, 0, CLONE_NEWUTS)); 2263 enforce_ruleset(_metadata, ruleset_fd); 2264 EXPECT_EQ(0, close(ruleset_fd)); 2265 2266 /* Child layer: handles namespaces but does not quiet CLONE_NEWUTS. */ 2267 ruleset_fd = create_ns_ruleset(); 2268 ASSERT_LE(0, ruleset_fd); 2269 enforce_ruleset(_metadata, ruleset_fd); 2270 EXPECT_EQ(0, close(ruleset_fd)); 2271 2272 set_cap(_metadata, CAP_SYS_ADMIN); 2273 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 2274 EXPECT_EQ(EPERM, errno); 2275 clear_cap(_metadata, CAP_SYS_ADMIN); 2276 2277 /* The child layer denies and does not quiet: logged. */ 2278 EXPECT_EQ(0, matches_log_ns_create(self->audit_fd, CLONE_NEWUTS)); 2279 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2280 EXPECT_EQ(0, records.access); 2281 } 2282 2283 /* 2284 * A parent layer that quiets and denies a member suppresses its own denial even 2285 * when it is not the youngest layer: a child layer allows the member, so the 2286 * parent is the youngest (and only) denying layer and its stored quiet mask 2287 * fires. Complements quiet_youngest_layer_wins. 2288 */ 2289 TEST_F(ns_audit, quiet_parent_only_denying_layer) 2290 { 2291 struct audit_records records = {}; 2292 int ruleset_fd; 2293 2294 /* Parent layer: quiet-only, so it denies CLONE_NEWUTS. */ 2295 ruleset_fd = create_ns_ruleset(); 2296 ASSERT_LE(0, ruleset_fd); 2297 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, 0, CLONE_NEWUTS)); 2298 enforce_ruleset(_metadata, ruleset_fd); 2299 EXPECT_EQ(0, close(ruleset_fd)); 2300 2301 /* Child layer: allows CLONE_NEWUTS, so only the parent denies it. */ 2302 ruleset_fd = create_ns_ruleset(); 2303 ASSERT_LE(0, ruleset_fd); 2304 ASSERT_EQ(0, add_ns_rule(ruleset_fd, CLONE_NEWUTS)); 2305 enforce_ruleset(_metadata, ruleset_fd); 2306 EXPECT_EQ(0, close(ruleset_fd)); 2307 2308 set_cap(_metadata, CAP_SYS_ADMIN); 2309 EXPECT_EQ(-1, unshare(CLONE_NEWUTS)); 2310 EXPECT_EQ(EPERM, errno); 2311 clear_cap(_metadata, CAP_SYS_ADMIN); 2312 2313 /* The parent quiets and is the youngest denying layer: suppressed. */ 2314 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2315 EXPECT_EQ(0, records.access); 2316 } 2317 2318 /* Trace tests */ 2319 2320 /* clang-format off */ 2321 FIXTURE(ns_trace) { 2322 /* clang-format on */ 2323 int tracefs_ok; 2324 }; 2325 2326 FIXTURE_SETUP(ns_trace) 2327 { 2328 int ret; 2329 2330 set_cap(_metadata, CAP_SYS_ADMIN); 2331 ASSERT_EQ(0, unshare(CLONE_NEWNS)); 2332 ASSERT_EQ(0, mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)); 2333 2334 ret = tracefs_fixture_setup(); 2335 if (ret) { 2336 clear_cap(_metadata, CAP_SYS_ADMIN); 2337 self->tracefs_ok = 0; 2338 SKIP(return, "tracefs not available"); 2339 } 2340 self->tracefs_ok = 1; 2341 2342 ASSERT_EQ(0, tracefs_enable_event(TRACEFS_CREATE_RULESET_ENABLE, true)); 2343 ASSERT_EQ(0, tracefs_enable_event(TRACEFS_ADD_RULE_NAMESPACE_ENABLE, 2344 true)); 2345 ASSERT_EQ(0, tracefs_enable_event( 2346 TRACEFS_DENY_PERMISSION_NAMESPACE_ENABLE, true)); 2347 ASSERT_EQ(0, tracefs_clear()); 2348 clear_cap(_metadata, CAP_SYS_ADMIN); 2349 } 2350 2351 FIXTURE_TEARDOWN(ns_trace) 2352 { 2353 if (!self->tracefs_ok) 2354 return; 2355 2356 set_cap(_metadata, CAP_SYS_ADMIN); 2357 tracefs_enable_event(TRACEFS_CREATE_RULESET_ENABLE, false); 2358 tracefs_enable_event(TRACEFS_ADD_RULE_NAMESPACE_ENABLE, false); 2359 tracefs_enable_event(TRACEFS_DENY_PERMISSION_NAMESPACE_ENABLE, false); 2360 tracefs_fixture_teardown(); 2361 clear_cap(_metadata, CAP_SYS_ADMIN); 2362 } 2363 2364 /* 2365 * Check the permission fields and version history, including two rejected calls 2366 * and two successful calls that do not extend the effective policy: a repeated 2367 * rule and an unknown-only rule. 2368 */ 2369 TEST_F(ns_trace, rule_events) 2370 { 2371 const char *const version_1 = 2372 REGEX_ADD_RULE_NAMESPACE_VERSION(TRACE_TASK, "1"); 2373 const char *const version_2 = 2374 REGEX_ADD_RULE_NAMESPACE_VERSION(TRACE_TASK, "2"); 2375 const char *const version_3 = 2376 REGEX_ADD_RULE_NAMESPACE_VERSION(TRACE_TASK, "3"); 2377 const struct landlock_namespace_attr valid_attr = { 2378 .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, 2379 .allowed_namespace_types = CLONE_NEWUTS, 2380 .quiet_namespace_types = CLONE_NEWNET, 2381 }; 2382 const struct landlock_namespace_attr invalid_attr = { 2383 .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, 2384 .allowed_namespace_types = CLONE_NEWUTS, 2385 }; 2386 char expected[32], field[64]; 2387 char *buf; 2388 int ruleset_fd; 2389 2390 if (!self->tracefs_ok) 2391 SKIP(return, "tracefs not available"); 2392 2393 ASSERT_EQ(0, tracefs_clear_buf()); 2394 2395 ruleset_fd = create_ns_ruleset(); 2396 ASSERT_LE(0, ruleset_fd); 2397 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, CLONE_NEWUTS, CLONE_NEWNET)); 2398 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 2399 &invalid_attr, 0)); 2400 ASSERT_EQ(EINVAL, errno); 2401 ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, 2402 &valid_attr, LANDLOCK_ADD_RULE_QUIET)); 2403 ASSERT_EQ(EINVAL, errno); 2404 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, CLONE_NEWUTS, CLONE_NEWNET)); 2405 ASSERT_EQ(0, add_ns_rule(ruleset_fd, 1ULL << 63)); 2406 EXPECT_EQ(0, close(ruleset_fd)); 2407 2408 buf = tracefs_read_buf(); 2409 ASSERT_NE(NULL, buf); 2410 2411 ASSERT_EQ(1, 2412 tracefs_count_matches(buf, REGEX_CREATE_RULESET(TRACE_TASK))); 2413 ASSERT_EQ(0, tracefs_extract_field( 2414 buf, REGEX_CREATE_RULESET(TRACE_TASK), 2415 "handled_permissions", field, sizeof(field))); 2416 EXPECT_STREQ("namespace.use", field); 2417 2418 ASSERT_EQ(3, 2419 tracefs_count_matches(buf, "landlock_add_rule_namespace: ")); 2420 ASSERT_EQ(3, tracefs_count_matches( 2421 buf, REGEX_ADD_RULE_NAMESPACE(TRACE_TASK))); 2422 EXPECT_EQ(1, tracefs_count_matches(buf, version_1)); 2423 EXPECT_EQ(1, tracefs_count_matches(buf, version_2)); 2424 EXPECT_EQ(1, tracefs_count_matches(buf, version_3)); 2425 2426 ASSERT_EQ(0, tracefs_extract_field(buf, version_1, "permissions", field, 2427 sizeof(field))); 2428 EXPECT_STREQ("namespace.use", field); 2429 ASSERT_EQ(0, tracefs_extract_field(buf, version_1, 2430 "allowed_namespace_types", field, 2431 sizeof(field))); 2432 /* Trace fields use raw CLONE_NEW* values, not internal bit indexes. */ 2433 snprintf(expected, sizeof(expected), "0x%llx", 2434 (unsigned long long)CLONE_NEWUTS); 2435 EXPECT_STREQ(expected, field); 2436 ASSERT_EQ(0, 2437 tracefs_extract_field(buf, version_1, "quiet_namespace_types", 2438 field, sizeof(field))); 2439 snprintf(expected, sizeof(expected), "0x%llx", 2440 (unsigned long long)CLONE_NEWNET); 2441 EXPECT_STREQ(expected, field); 2442 2443 /* The rejected calls emit no event and do not consume version 2. */ 2444 ASSERT_EQ(0, tracefs_extract_field(buf, version_2, "permissions", field, 2445 sizeof(field))); 2446 EXPECT_STREQ("namespace.use", field); 2447 ASSERT_EQ(0, tracefs_extract_field(buf, version_2, 2448 "allowed_namespace_types", field, 2449 sizeof(field))); 2450 snprintf(expected, sizeof(expected), "0x%llx", 2451 (unsigned long long)CLONE_NEWUTS); 2452 EXPECT_STREQ(expected, field); 2453 ASSERT_EQ(0, 2454 tracefs_extract_field(buf, version_2, "quiet_namespace_types", 2455 field, sizeof(field))); 2456 snprintf(expected, sizeof(expected), "0x%llx", 2457 (unsigned long long)CLONE_NEWNET); 2458 EXPECT_STREQ(expected, field); 2459 2460 /* The unknown-only call succeeds but contributes no effective bits. */ 2461 ASSERT_EQ(0, tracefs_extract_field(buf, version_3, "permissions", field, 2462 sizeof(field))); 2463 EXPECT_STREQ("namespace.use", field); 2464 ASSERT_EQ(0, tracefs_extract_field(buf, version_3, 2465 "allowed_namespace_types", field, 2466 sizeof(field))); 2467 EXPECT_STREQ("0x0", field); 2468 ASSERT_EQ(0, 2469 tracefs_extract_field(buf, version_3, "quiet_namespace_types", 2470 field, sizeof(field))); 2471 EXPECT_STREQ("0x0", field); 2472 2473 free(buf); 2474 } 2475 2476 static void test_ns_trace_denial(struct __test_metadata *const _metadata, 2477 const bool use_setns, const bool quiet) 2478 { 2479 char expected[32], field[64]; 2480 int ruleset_fd, ns_fd = -1, ret; 2481 __u64 expected_ns_id = 0; 2482 char *buf; 2483 2484 ASSERT_EQ(0, tracefs_clear_buf()); 2485 2486 if (use_setns) { 2487 ns_fd = open("/proc/self/ns/uts", O_RDONLY | O_CLOEXEC); 2488 ASSERT_LE(0, ns_fd); 2489 ASSERT_EQ(0, ioctl(ns_fd, NS_GET_ID, &expected_ns_id)); 2490 } 2491 2492 ruleset_fd = create_ns_ruleset(); 2493 ASSERT_LE(0, ruleset_fd); 2494 if (quiet) 2495 ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, 0, CLONE_NEWUTS)); 2496 2497 set_cap(_metadata, CAP_SYS_ADMIN); 2498 enforce_ruleset(_metadata, ruleset_fd); 2499 EXPECT_EQ(0, close(ruleset_fd)); 2500 2501 if (use_setns) 2502 ret = setns(ns_fd, CLONE_NEWUTS); 2503 else 2504 ret = unshare(CLONE_NEWUTS); 2505 EXPECT_EQ(-1, ret); 2506 EXPECT_EQ(EPERM, errno); 2507 2508 clear_cap(_metadata, CAP_SYS_ADMIN); 2509 if (ns_fd >= 0) 2510 EXPECT_EQ(0, close(ns_fd)); 2511 2512 buf = tracefs_read_buf(); 2513 ASSERT_NE(NULL, buf); 2514 ASSERT_EQ(1, tracefs_count_matches( 2515 buf, REGEX_DENY_PERMISSION_NAMESPACE(TRACE_TASK))) 2516 { 2517 TH_LOG("Expected one namespace denial event\n%s", buf); 2518 } 2519 2520 ASSERT_EQ(0, tracefs_extract_field( 2521 buf, REGEX_DENY_PERMISSION_NAMESPACE(TRACE_TASK), 2522 "domain", field, sizeof(field))); 2523 EXPECT_STRNE("0", field); 2524 ASSERT_EQ(0, tracefs_extract_field( 2525 buf, REGEX_DENY_PERMISSION_NAMESPACE(TRACE_TASK), 2526 "same_exec", field, sizeof(field))); 2527 EXPECT_STREQ("1", field); 2528 ASSERT_EQ(0, tracefs_extract_field( 2529 buf, REGEX_DENY_PERMISSION_NAMESPACE(TRACE_TASK), 2530 "logged", field, sizeof(field))); 2531 EXPECT_STREQ(quiet ? "0" : "1", field); 2532 ASSERT_EQ(0, tracefs_extract_field( 2533 buf, REGEX_DENY_PERMISSION_NAMESPACE(TRACE_TASK), 2534 "blockers", field, sizeof(field))); 2535 EXPECT_STREQ("use", field); 2536 ASSERT_EQ(0, tracefs_extract_field( 2537 buf, REGEX_DENY_PERMISSION_NAMESPACE(TRACE_TASK), 2538 "namespace_type", field, sizeof(field))); 2539 snprintf(expected, sizeof(expected), "0x%llx", 2540 (unsigned long long)CLONE_NEWUTS); 2541 EXPECT_STREQ(expected, field); 2542 ASSERT_EQ(0, tracefs_extract_field( 2543 buf, REGEX_DENY_PERMISSION_NAMESPACE(TRACE_TASK), 2544 "namespace_id", field, sizeof(field))); 2545 EXPECT_STRNE("0", field); 2546 if (use_setns) 2547 EXPECT_EQ(expected_ns_id, strtoull(field, NULL, 10)); 2548 ret = tracefs_extract_field(buf, 2549 REGEX_DENY_PERMISSION_NAMESPACE(TRACE_TASK), 2550 "namespace_op", field, sizeof(field)); 2551 ASSERT_EQ(0, ret); 2552 EXPECT_STREQ(use_setns ? "enter" : "create", field); 2553 2554 free(buf); 2555 } 2556 2557 TEST_F(ns_trace, deny_create) 2558 { 2559 if (!self->tracefs_ok) 2560 SKIP(return, "tracefs not available"); 2561 test_ns_trace_denial(_metadata, false, false); 2562 } 2563 2564 TEST_F(ns_trace, deny_create_quiet) 2565 { 2566 if (!self->tracefs_ok) 2567 SKIP(return, "tracefs not available"); 2568 test_ns_trace_denial(_metadata, false, true); 2569 } 2570 2571 TEST_F(ns_trace, deny_setns) 2572 { 2573 if (!self->tracefs_ok) 2574 SKIP(return, "tracefs not available"); 2575 test_ns_trace_denial(_metadata, true, false); 2576 } 2577 2578 TEST_F(ns_trace, deny_setns_quiet) 2579 { 2580 if (!self->tracefs_ok) 2581 SKIP(return, "tracefs not available"); 2582 test_ns_trace_denial(_metadata, true, true); 2583 } 2584 2585 /* clang-format off */ 2586 FIXTURE(ns_proc_open) { 2587 /* clang-format on */ 2588 struct audit_filter audit_filter; 2589 int audit_fd; 2590 }; 2591 2592 FIXTURE_VARIANT(ns_proc_open) 2593 { 2594 __u64 ns_type; 2595 }; 2596 2597 /* clang-format off */ 2598 FIXTURE_VARIANT_ADD(ns_proc_open, mnt) { 2599 /* clang-format on */ 2600 .ns_type = CLONE_NEWNS, 2601 }; 2602 2603 /* clang-format off */ 2604 FIXTURE_VARIANT_ADD(ns_proc_open, user) { 2605 /* clang-format on */ 2606 .ns_type = CLONE_NEWUSER, 2607 }; 2608 2609 FIXTURE_SETUP(ns_proc_open) 2610 { 2611 ASSERT_TRUE(is_in_init_user_ns()); 2612 2613 disable_caps(_metadata); 2614 2615 set_cap(_metadata, CAP_AUDIT_CONTROL); 2616 self->audit_fd = audit_init_with_exe_filter(&self->audit_filter); 2617 EXPECT_LE(0, self->audit_fd); 2618 clear_cap(_metadata, CAP_AUDIT_CONTROL); 2619 } 2620 2621 FIXTURE_TEARDOWN(ns_proc_open) 2622 { 2623 set_cap(_metadata, CAP_AUDIT_CONTROL); 2624 EXPECT_EQ(0, audit_cleanup(self->audit_fd, &self->audit_filter)); 2625 clear_cap(_metadata, CAP_AUDIT_CONTROL); 2626 } 2627 2628 /* 2629 * Opening /proc/self/ns/<type> only acquires a procfs reference, not membership 2630 * or an acquired fd of the kind LANDLOCK_PERMISSION_NAMESPACE_USE gates. 2631 * Verify the open is unrestricted even when the permission is handled with no 2632 * rules. 2633 */ 2634 TEST_F(ns_proc_open, open_unrestricted) 2635 { 2636 char proc_path[NS_PROC_PATH_MAX]; 2637 struct audit_records records = {}; 2638 int ruleset_fd, fd; 2639 2640 ASSERT_TRUE( 2641 ns_is_supported(variant->ns_type, proc_path, sizeof(proc_path))) 2642 { 2643 TH_LOG("Namespace type 0x%llx not supported", 2644 (unsigned long long)variant->ns_type); 2645 } 2646 2647 ruleset_fd = create_ns_ruleset(); 2648 ASSERT_LE(0, ruleset_fd); 2649 enforce_ruleset(_metadata, ruleset_fd); 2650 EXPECT_EQ(0, close(ruleset_fd)); 2651 2652 fd = open(proc_path, O_RDONLY); 2653 ASSERT_LE(0, fd) 2654 { 2655 TH_LOG("open(%s) failed: %s", proc_path, strerror(errno)); 2656 } 2657 2658 /* No Landlock denial. */ 2659 EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); 2660 EXPECT_EQ(0, records.access); 2661 2662 EXPECT_EQ(0, close(fd)); 2663 } 2664 2665 TEST_HARNESS_MAIN 2666