|
7 | 7 |
|
8 | 8 | #define _GNU_SOURCE
|
9 | 9 | #include <errno.h>
|
| 10 | +#include <limits.h> |
10 | 11 | #include <linux/landlock.h>
|
| 12 | +#include <stdlib.h> |
11 | 13 | #include <sys/mount.h>
|
12 | 14 | #include <sys/prctl.h>
|
13 | 15 | #include <sys/types.h>
|
@@ -329,4 +331,221 @@ TEST_F(audit_flags, signal)
|
329 | 331 | }
|
330 | 332 | }
|
331 | 333 |
|
| 334 | +static int matches_log_fs_read_root(int audit_fd) |
| 335 | +{ |
| 336 | + return audit_match_record( |
| 337 | + audit_fd, AUDIT_LANDLOCK_ACCESS, |
| 338 | + REGEX_LANDLOCK_PREFIX |
| 339 | + " blockers=fs\\.read_dir path=\"/\" dev=\"[^\"]\\+\" ino=[0-9]\\+$", |
| 340 | + NULL); |
| 341 | +} |
| 342 | + |
| 343 | +FIXTURE(audit_exec) |
| 344 | +{ |
| 345 | + struct audit_filter audit_filter; |
| 346 | + int audit_fd; |
| 347 | +}; |
| 348 | + |
| 349 | +FIXTURE_VARIANT(audit_exec) |
| 350 | +{ |
| 351 | + const int restrict_flags; |
| 352 | +}; |
| 353 | + |
| 354 | +/* clang-format off */ |
| 355 | +FIXTURE_VARIANT_ADD(audit_exec, default) { |
| 356 | + /* clang-format on */ |
| 357 | + .restrict_flags = 0, |
| 358 | +}; |
| 359 | + |
| 360 | +/* clang-format off */ |
| 361 | +FIXTURE_VARIANT_ADD(audit_exec, same_exec_off) { |
| 362 | + /* clang-format on */ |
| 363 | + .restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF, |
| 364 | +}; |
| 365 | + |
| 366 | +/* clang-format off */ |
| 367 | +FIXTURE_VARIANT_ADD(audit_exec, subdomains_off) { |
| 368 | + /* clang-format on */ |
| 369 | + .restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF, |
| 370 | +}; |
| 371 | + |
| 372 | +/* clang-format off */ |
| 373 | +FIXTURE_VARIANT_ADD(audit_exec, cross_exec_on) { |
| 374 | + /* clang-format on */ |
| 375 | + .restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON, |
| 376 | +}; |
| 377 | + |
| 378 | +/* clang-format off */ |
| 379 | +FIXTURE_VARIANT_ADD(audit_exec, subdomains_off_and_cross_exec_on) { |
| 380 | + /* clang-format on */ |
| 381 | + .restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF | |
| 382 | + LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON, |
| 383 | +}; |
| 384 | + |
| 385 | +FIXTURE_SETUP(audit_exec) |
| 386 | +{ |
| 387 | + disable_caps(_metadata); |
| 388 | + set_cap(_metadata, CAP_AUDIT_CONTROL); |
| 389 | + |
| 390 | + self->audit_fd = audit_init(); |
| 391 | + EXPECT_LE(0, self->audit_fd) |
| 392 | + { |
| 393 | + const char *error_msg; |
| 394 | + |
| 395 | + /* kill "$(auditctl -s | sed -ne 's/^pid \([0-9]\+\)$/\1/p')" */ |
| 396 | + if (self->audit_fd == -EEXIST) |
| 397 | + error_msg = "socket already in use (e.g. auditd)"; |
| 398 | + else |
| 399 | + error_msg = strerror(-self->audit_fd); |
| 400 | + TH_LOG("Failed to initialize audit: %s", error_msg); |
| 401 | + } |
| 402 | + |
| 403 | + /* Applies test filter for the bin_wait_pipe_sandbox program. */ |
| 404 | + EXPECT_EQ(0, audit_init_filter_exe(&self->audit_filter, |
| 405 | + bin_wait_pipe_sandbox)); |
| 406 | + EXPECT_EQ(0, audit_filter_exe(self->audit_fd, &self->audit_filter, |
| 407 | + AUDIT_ADD_RULE)); |
| 408 | + |
| 409 | + clear_cap(_metadata, CAP_AUDIT_CONTROL); |
| 410 | +} |
| 411 | + |
| 412 | +FIXTURE_TEARDOWN(audit_exec) |
| 413 | +{ |
| 414 | + set_cap(_metadata, CAP_AUDIT_CONTROL); |
| 415 | + EXPECT_EQ(0, audit_filter_exe(self->audit_fd, &self->audit_filter, |
| 416 | + AUDIT_DEL_RULE)); |
| 417 | + clear_cap(_metadata, CAP_AUDIT_CONTROL); |
| 418 | + EXPECT_EQ(0, close(self->audit_fd)); |
| 419 | +} |
| 420 | + |
| 421 | +TEST_F(audit_exec, signal_and_open) |
| 422 | +{ |
| 423 | + struct audit_records records; |
| 424 | + int pipe_child[2], pipe_parent[2]; |
| 425 | + char buf_parent; |
| 426 | + pid_t child; |
| 427 | + int status; |
| 428 | + |
| 429 | + ASSERT_EQ(0, pipe2(pipe_child, 0)); |
| 430 | + ASSERT_EQ(0, pipe2(pipe_parent, 0)); |
| 431 | + |
| 432 | + child = fork(); |
| 433 | + ASSERT_LE(0, child); |
| 434 | + if (child == 0) { |
| 435 | + const struct landlock_ruleset_attr layer1 = { |
| 436 | + .scoped = LANDLOCK_SCOPE_SIGNAL, |
| 437 | + }; |
| 438 | + char pipe_child_str[12], pipe_parent_str[12]; |
| 439 | + char *const argv[] = { (char *)bin_wait_pipe_sandbox, |
| 440 | + pipe_child_str, pipe_parent_str, NULL }; |
| 441 | + int ruleset_fd; |
| 442 | + |
| 443 | + /* Passes the pipe FDs to the executed binary. */ |
| 444 | + EXPECT_EQ(0, close(pipe_child[0])); |
| 445 | + EXPECT_EQ(0, close(pipe_parent[1])); |
| 446 | + snprintf(pipe_child_str, sizeof(pipe_child_str), "%d", |
| 447 | + pipe_child[1]); |
| 448 | + snprintf(pipe_parent_str, sizeof(pipe_parent_str), "%d", |
| 449 | + pipe_parent[0]); |
| 450 | + |
| 451 | + ruleset_fd = |
| 452 | + landlock_create_ruleset(&layer1, sizeof(layer1), 0); |
| 453 | + if (ruleset_fd < 0) { |
| 454 | + perror("Failed to create a ruleset"); |
| 455 | + _exit(1); |
| 456 | + } |
| 457 | + prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 458 | + if (landlock_restrict_self(ruleset_fd, |
| 459 | + variant->restrict_flags)) { |
| 460 | + perror("Failed to restrict self"); |
| 461 | + _exit(1); |
| 462 | + } |
| 463 | + close(ruleset_fd); |
| 464 | + |
| 465 | + ASSERT_EQ(0, execve(argv[0], argv, NULL)) |
| 466 | + { |
| 467 | + TH_LOG("Failed to execute \"%s\": %s", argv[0], |
| 468 | + strerror(errno)); |
| 469 | + }; |
| 470 | + _exit(1); |
| 471 | + return; |
| 472 | + } |
| 473 | + |
| 474 | + EXPECT_EQ(0, close(pipe_child[1])); |
| 475 | + EXPECT_EQ(0, close(pipe_parent[0])); |
| 476 | + |
| 477 | + /* Waits for the child. */ |
| 478 | + EXPECT_EQ(1, read(pipe_child[0], &buf_parent, 1)); |
| 479 | + |
| 480 | + /* Tests that there was no denial until now. */ |
| 481 | + EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); |
| 482 | + EXPECT_EQ(0, records.access); |
| 483 | + EXPECT_EQ(0, records.domain); |
| 484 | + |
| 485 | + /* |
| 486 | + * Wait for the child to do a first denied action by layer1 and |
| 487 | + * sandbox itself with layer2. |
| 488 | + */ |
| 489 | + EXPECT_EQ(1, write(pipe_parent[1], ".", 1)); |
| 490 | + EXPECT_EQ(1, read(pipe_child[0], &buf_parent, 1)); |
| 491 | + |
| 492 | + /* Tests that the audit record only matches the child. */ |
| 493 | + if (variant->restrict_flags & LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON) { |
| 494 | + /* Matches the current domain. */ |
| 495 | + EXPECT_EQ(0, matches_log_signal(_metadata, self->audit_fd, |
| 496 | + getpid(), NULL)); |
| 497 | + } |
| 498 | + |
| 499 | + /* Checks that we didn't miss anything. */ |
| 500 | + EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); |
| 501 | + EXPECT_EQ(0, records.access); |
| 502 | + |
| 503 | + /* |
| 504 | + * Wait for the child to do a second denied action by layer1 and |
| 505 | + * layer2, and sandbox itself with layer3. |
| 506 | + */ |
| 507 | + EXPECT_EQ(1, write(pipe_parent[1], ".", 1)); |
| 508 | + EXPECT_EQ(1, read(pipe_child[0], &buf_parent, 1)); |
| 509 | + |
| 510 | + /* Tests that the audit record only matches the child. */ |
| 511 | + if (variant->restrict_flags & LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON) { |
| 512 | + /* Matches the current domain. */ |
| 513 | + EXPECT_EQ(0, matches_log_signal(_metadata, self->audit_fd, |
| 514 | + getpid(), NULL)); |
| 515 | + } |
| 516 | + |
| 517 | + if (!(variant->restrict_flags & |
| 518 | + LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF)) { |
| 519 | + /* Matches the child domain. */ |
| 520 | + EXPECT_EQ(0, matches_log_fs_read_root(self->audit_fd)); |
| 521 | + } |
| 522 | + |
| 523 | + /* Checks that we didn't miss anything. */ |
| 524 | + EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); |
| 525 | + EXPECT_EQ(0, records.access); |
| 526 | + |
| 527 | + /* Waits for the child to terminate. */ |
| 528 | + EXPECT_EQ(1, write(pipe_parent[1], ".", 1)); |
| 529 | + ASSERT_EQ(child, waitpid(child, &status, 0)); |
| 530 | + ASSERT_EQ(1, WIFEXITED(status)); |
| 531 | + ASSERT_EQ(0, WEXITSTATUS(status)); |
| 532 | + |
| 533 | + /* Tests that the audit record only matches the child. */ |
| 534 | + if (!(variant->restrict_flags & |
| 535 | + LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF)) { |
| 536 | + /* |
| 537 | + * Matches the child domains, which tests that the |
| 538 | + * llcred->domain_exec bitmask is correctly updated with a new |
| 539 | + * domain. |
| 540 | + */ |
| 541 | + EXPECT_EQ(0, matches_log_fs_read_root(self->audit_fd)); |
| 542 | + EXPECT_EQ(0, matches_log_signal(_metadata, self->audit_fd, |
| 543 | + getpid(), NULL)); |
| 544 | + } |
| 545 | + |
| 546 | + /* Checks that we didn't miss anything. */ |
| 547 | + EXPECT_EQ(0, audit_count_records(self->audit_fd, &records)); |
| 548 | + EXPECT_EQ(0, records.access); |
| 549 | +} |
| 550 | + |
332 | 551 | TEST_HARNESS_MAIN
|
0 commit comments