Merge tag 'landlock-6.11-rc1-houdini-fix' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux

Pull landlock fix from Mickaël Salaün:
"Jann Horn reported a sandbox bypass for Landlock. This includes the
fix and new tests. This should be backported"

* tag 'landlock-6.11-rc1-houdini-fix' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux:
selftests/landlock: Add cred_transfer test
landlock: Don't lose track of restrictions on cred_transfer

+84 -2
+9 -2
security/landlock/cred.c
··· 14 14 #include "ruleset.h" 15 15 #include "setup.h" 16 16 17 - static int hook_cred_prepare(struct cred *const new, 18 - const struct cred *const old, const gfp_t gfp) 17 + static void hook_cred_transfer(struct cred *const new, 18 + const struct cred *const old) 19 19 { 20 20 struct landlock_ruleset *const old_dom = landlock_cred(old)->domain; 21 21 ··· 23 23 landlock_get_ruleset(old_dom); 24 24 landlock_cred(new)->domain = old_dom; 25 25 } 26 + } 27 + 28 + static int hook_cred_prepare(struct cred *const new, 29 + const struct cred *const old, const gfp_t gfp) 30 + { 31 + hook_cred_transfer(new, old); 26 32 return 0; 27 33 } 28 34 ··· 42 36 43 37 static struct security_hook_list landlock_hooks[] __ro_after_init = { 44 38 LSM_HOOK_INIT(cred_prepare, hook_cred_prepare), 39 + LSM_HOOK_INIT(cred_transfer, hook_cred_transfer), 45 40 LSM_HOOK_INIT(cred_free, hook_cred_free), 46 41 }; 47 42
+74
tools/testing/selftests/landlock/base_test.c
··· 9 9 #define _GNU_SOURCE 10 10 #include <errno.h> 11 11 #include <fcntl.h> 12 + #include <linux/keyctl.h> 12 13 #include <linux/landlock.h> 13 14 #include <string.h> 14 15 #include <sys/prctl.h> ··· 325 324 ASSERT_EQ(child, waitpid(child, &status, 0)); 326 325 ASSERT_EQ(1, WIFEXITED(status)); 327 326 ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 327 + } 328 + 329 + TEST(cred_transfer) 330 + { 331 + struct landlock_ruleset_attr ruleset_attr = { 332 + .handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR, 333 + }; 334 + int ruleset_fd, dir_fd; 335 + pid_t child; 336 + int status; 337 + 338 + drop_caps(_metadata); 339 + 340 + dir_fd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC); 341 + EXPECT_LE(0, dir_fd); 342 + EXPECT_EQ(0, close(dir_fd)); 343 + 344 + /* Denies opening directories. */ 345 + ruleset_fd = 346 + landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 347 + ASSERT_LE(0, ruleset_fd); 348 + EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)); 349 + ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0)); 350 + EXPECT_EQ(0, close(ruleset_fd)); 351 + 352 + /* Checks ruleset enforcement. */ 353 + EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); 354 + EXPECT_EQ(EACCES, errno); 355 + 356 + /* Needed for KEYCTL_SESSION_TO_PARENT permission checks */ 357 + EXPECT_NE(-1, syscall(__NR_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL, 0, 358 + 0, 0)) 359 + { 360 + TH_LOG("Failed to join session keyring: %s", strerror(errno)); 361 + } 362 + 363 + child = fork(); 364 + ASSERT_LE(0, child); 365 + if (child == 0) { 366 + /* Checks ruleset enforcement. */ 367 + EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); 368 + EXPECT_EQ(EACCES, errno); 369 + 370 + /* 371 + * KEYCTL_SESSION_TO_PARENT is a no-op unless we have a 372 + * different session keyring in the child, so make that happen. 373 + */ 374 + EXPECT_NE(-1, syscall(__NR_keyctl, KEYCTL_JOIN_SESSION_KEYRING, 375 + NULL, 0, 0, 0)); 376 + 377 + /* 378 + * KEYCTL_SESSION_TO_PARENT installs credentials on the parent 379 + * that never go through the cred_prepare hook, this path uses 380 + * cred_transfer instead. 381 + */ 382 + EXPECT_EQ(0, syscall(__NR_keyctl, KEYCTL_SESSION_TO_PARENT, 0, 383 + 0, 0, 0)); 384 + 385 + /* Re-checks ruleset enforcement. */ 386 + EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); 387 + EXPECT_EQ(EACCES, errno); 388 + 389 + _exit(_metadata->exit_code); 390 + return; 391 + } 392 + 393 + EXPECT_EQ(child, waitpid(child, &status, 0)); 394 + EXPECT_EQ(1, WIFEXITED(status)); 395 + EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); 396 + 397 + /* Re-checks ruleset enforcement. */ 398 + EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); 399 + EXPECT_EQ(EACCES, errno); 328 400 } 329 401 330 402 TEST_HARNESS_MAIN
+1
tools/testing/selftests/landlock/config
··· 2 2 CONFIG_CGROUP_SCHED=y 3 3 CONFIG_INET=y 4 4 CONFIG_IPV6=y 5 + CONFIG_KEYS=y 5 6 CONFIG_NET=y 6 7 CONFIG_NET_NS=y 7 8 CONFIG_OVERLAY_FS=y