1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Landlock - Filesystem management and hooks 4 * 5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> 6 * Copyright © 2018-2020 ANSSI 7 * Copyright © 2021-2025 Microsoft Corporation 8 * Copyright © 2022 Günther Noack <gnoack3000@gmail.com> 9 * Copyright © 2023-2024 Google LLC 10 */ 11 12 #include <asm/ioctls.h> 13 #include <kunit/test.h> 14 #include <linux/atomic.h> 15 #include <linux/bitops.h> 16 #include <linux/bits.h> 17 #include <linux/compiler_types.h> 18 #include <linux/dcache.h> 19 #include <linux/err.h> 20 #include <linux/falloc.h> 21 #include <linux/fs.h> 22 #include <linux/init.h> 23 #include <linux/kernel.h> 24 #include <linux/limits.h> 25 #include <linux/list.h> 26 #include <linux/lsm_audit.h> 27 #include <linux/lsm_hooks.h> 28 #include <linux/mount.h> 29 #include <linux/namei.h> 30 #include <linux/net.h> 31 #include <linux/path.h> 32 #include <linux/pid.h> 33 #include <linux/rcupdate.h> 34 #include <linux/sched/signal.h> 35 #include <linux/spinlock.h> 36 #include <linux/stat.h> 37 #include <linux/types.h> 38 #include <linux/wait_bit.h> 39 #include <linux/workqueue.h> 40 #include <net/af_unix.h> 41 #include <uapi/linux/fiemap.h> 42 #include <uapi/linux/landlock.h> 43 44 #include "access.h" 45 #include "audit.h" 46 #include "common.h" 47 #include "cred.h" 48 #include "domain.h" 49 #include "fs.h" 50 #include "limits.h" 51 #include "object.h" 52 #include "ruleset.h" 53 #include "setup.h" 54 55 /* Underlying object management */ 56 57 static void release_inode(struct landlock_object *const object) 58 __releases(object->lock) 59 { 60 struct inode *const inode = object->underobj; 61 struct super_block *sb; 62 63 if (!inode) { 64 spin_unlock(&object->lock); 65 return; 66 } 67 68 /* 69 * Protects against concurrent use by hook_sb_delete() of the reference 70 * to the underlying inode. 71 */ 72 object->underobj = NULL; 73 /* 74 * Makes sure that if the filesystem is concurrently unmounted, 75 * hook_sb_delete() will wait for us to finish iput(). 76 */ 77 sb = inode->i_sb; 78 atomic_long_inc(&landlock_superblock(sb)->inode_refs); 79 spin_unlock(&object->lock); 80 /* 81 * Because object->underobj was not NULL, hook_sb_delete() and 82 * get_inode_object() guarantee that it is safe to reset 83 * landlock_inode(inode)->object while it is not NULL. It is therefore 84 * not necessary to lock inode->i_lock. 85 */ 86 rcu_assign_pointer(landlock_inode(inode)->object, NULL); 87 /* 88 * Now, new rules can safely be tied to @inode with get_inode_object(). 89 */ 90 91 iput(inode); 92 if (atomic_long_dec_and_test(&landlock_superblock(sb)->inode_refs)) 93 wake_up_var(&landlock_superblock(sb)->inode_refs); 94 } 95 96 static const struct landlock_object_underops landlock_fs_underops = { 97 .release = release_inode 98 }; 99 100 /* IOCTL helpers */ 101 102 /** 103 * is_masked_device_ioctl - Determine whether an IOCTL command is always 104 * permitted with Landlock for device files. These commands can not be 105 * restricted on device files by enforcing a Landlock policy. 106 * 107 * @cmd: The IOCTL command that is supposed to be run. 108 * 109 * By default, any IOCTL on a device file requires the 110 * LANDLOCK_ACCESS_FS_IOCTL_DEV right. However, we blanket-permit some 111 * commands, if: 112 * 113 * 1. The command is implemented in fs/ioctl.c's do_vfs_ioctl(), 114 * not in f_ops->unlocked_ioctl() or f_ops->compat_ioctl(). 115 * 116 * 2. The command is harmless when invoked on devices. 117 * 118 * We also permit commands that do not make sense for devices, but where the 119 * do_vfs_ioctl() implementation returns a more conventional error code. 120 * 121 * Any new IOCTL commands that are implemented in fs/ioctl.c's do_vfs_ioctl() 122 * should be considered for inclusion here. 123 * 124 * Return: True if the IOCTL @cmd can not be restricted with Landlock for 125 * device files, false otherwise. 126 */ 127 static __attribute_const__ bool is_masked_device_ioctl(const unsigned int cmd) 128 { 129 switch (cmd) { 130 /* 131 * FIOCLEX, FIONCLEX, FIONBIO and FIOASYNC manipulate the FD's 132 * close-on-exec and the file's buffered-IO and async flags. These 133 * operations are also available through fcntl(2), and are 134 * unconditionally permitted in Landlock. 135 */ 136 case FIOCLEX: 137 case FIONCLEX: 138 case FIONBIO: 139 case FIOASYNC: 140 /* 141 * FIOQSIZE queries the size of a regular file, directory, or link. 142 * 143 * We still permit it, because it always returns -ENOTTY for 144 * other file types. 145 */ 146 case FIOQSIZE: 147 /* 148 * FIFREEZE and FITHAW freeze and thaw the file system which the 149 * given file belongs to. Requires CAP_SYS_ADMIN. 150 * 151 * These commands operate on the file system's superblock rather 152 * than on the file itself. The same operations can also be 153 * done through any other file or directory on the same file 154 * system, so it is safe to permit these. 155 */ 156 case FIFREEZE: 157 case FITHAW: 158 /* 159 * FS_IOC_FIEMAP queries information about the allocation of 160 * blocks within a file. 161 * 162 * This IOCTL command only makes sense for regular files and is 163 * not implemented by devices. It is harmless to permit. 164 */ 165 case FS_IOC_FIEMAP: 166 /* 167 * FIGETBSZ queries the file system's block size for a file or 168 * directory. 169 * 170 * This command operates on the file system's superblock rather 171 * than on the file itself. The same operation can also be done 172 * through any other file or directory on the same file system, 173 * so it is safe to permit it. 174 */ 175 case FIGETBSZ: 176 /* 177 * FICLONE, FICLONERANGE and FIDEDUPERANGE make files share 178 * their underlying storage ("reflink") between source and 179 * destination FDs, on file systems which support that. 180 * 181 * These IOCTL commands only apply to regular files 182 * and are harmless to permit for device files. 183 */ 184 case FICLONE: 185 case FICLONERANGE: 186 case FIDEDUPERANGE: 187 /* 188 * FS_IOC_GETFSUUID and FS_IOC_GETFSSYSFSPATH both operate on 189 * the file system superblock, not on the specific file, so 190 * these operations are available through any other file on the 191 * same file system as well. 192 */ 193 case FS_IOC_GETFSUUID: 194 case FS_IOC_GETFSSYSFSPATH: 195 return true; 196 197 /* 198 * FIONREAD, FS_IOC_GETFLAGS, FS_IOC_SETFLAGS, FS_IOC_FSGETXATTR and 199 * FS_IOC_FSSETXATTR are forwarded to device implementations. 200 */ 201 202 /* 203 * file_ioctl() commands (FIBMAP, FS_IOC_RESVSP, FS_IOC_RESVSP64, 204 * FS_IOC_UNRESVSP, FS_IOC_UNRESVSP64 and FS_IOC_ZERO_RANGE) are 205 * forwarded to device implementations, so not permitted. 206 */ 207 208 /* Other commands are guarded by the access right. */ 209 default: 210 return false; 211 } 212 } 213 214 /* 215 * is_masked_device_ioctl_compat - same as the helper above, but checking the 216 * "compat" IOCTL commands. 217 * 218 * The IOCTL commands with special handling in compat-mode should behave the 219 * same as their non-compat counterparts. 220 */ 221 static __attribute_const__ bool 222 is_masked_device_ioctl_compat(const unsigned int cmd) 223 { 224 switch (cmd) { 225 /* FICLONE is permitted, same as in the non-compat variant. */ 226 case FICLONE: 227 return true; 228 229 #if defined(CONFIG_X86_64) 230 /* 231 * FS_IOC_RESVSP_32, FS_IOC_RESVSP64_32, FS_IOC_UNRESVSP_32, 232 * FS_IOC_UNRESVSP64_32, FS_IOC_ZERO_RANGE_32: not blanket-permitted, 233 * for consistency with their non-compat variants. 234 */ 235 case FS_IOC_RESVSP_32: 236 case FS_IOC_RESVSP64_32: 237 case FS_IOC_UNRESVSP_32: 238 case FS_IOC_UNRESVSP64_32: 239 case FS_IOC_ZERO_RANGE_32: 240 #endif 241 242 /* 243 * FS_IOC32_GETFLAGS, FS_IOC32_SETFLAGS are forwarded to their device 244 * implementations. 245 */ 246 case FS_IOC32_GETFLAGS: 247 case FS_IOC32_SETFLAGS: 248 return false; 249 default: 250 return is_masked_device_ioctl(cmd); 251 } 252 } 253 254 /* Ruleset management */ 255 256 static struct landlock_object *get_inode_object(struct inode *const inode) 257 { 258 struct landlock_object *object, *new_object; 259 struct landlock_inode_security *inode_sec = landlock_inode(inode); 260 261 rcu_read_lock(); 262 retry: 263 object = rcu_dereference(inode_sec->object); 264 if (object) { 265 if (likely(refcount_inc_not_zero(&object->usage))) { 266 rcu_read_unlock(); 267 return object; 268 } 269 /* 270 * We are racing with release_inode(), the object is going 271 * away. Wait for release_inode(), then retry. 272 */ 273 spin_lock(&object->lock); 274 spin_unlock(&object->lock); 275 goto retry; 276 } 277 rcu_read_unlock(); 278 279 /* 280 * If there is no object tied to @inode, then create a new one (without 281 * holding any locks). 282 */ 283 new_object = landlock_create_object(&landlock_fs_underops, inode); 284 if (IS_ERR(new_object)) 285 return new_object; 286 287 /* 288 * Protects against concurrent calls to get_inode_object() or 289 * hook_sb_delete(). 290 */ 291 spin_lock(&inode->i_lock); 292 if (unlikely(rcu_access_pointer(inode_sec->object))) { 293 /* Someone else just created the object, bail out and retry. */ 294 spin_unlock(&inode->i_lock); 295 kfree(new_object); 296 297 rcu_read_lock(); 298 goto retry; 299 } 300 301 /* 302 * @inode will be released by hook_sb_delete() on its superblock 303 * shutdown, or by release_inode() when no more ruleset references the 304 * related object. 305 */ 306 ihold(inode); 307 rcu_assign_pointer(inode_sec->object, new_object); 308 spin_unlock(&inode->i_lock); 309 return new_object; 310 } 311 312 /* All access rights that can be tied to files. */ 313 /* clang-format off */ 314 #define ACCESS_FILE ( \ 315 LANDLOCK_ACCESS_FS_EXECUTE | \ 316 LANDLOCK_ACCESS_FS_WRITE_FILE | \ 317 LANDLOCK_ACCESS_FS_READ_FILE | \ 318 LANDLOCK_ACCESS_FS_TRUNCATE | \ 319 LANDLOCK_ACCESS_FS_IOCTL_DEV | \ 320 LANDLOCK_ACCESS_FS_RESOLVE_UNIX) 321 /* clang-format on */ 322 323 /* 324 * @path: Should have been checked by get_path_from_fd(). 325 */ 326 int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, 327 const struct path *const path, 328 access_mask_t access_rights) 329 { 330 int err; 331 struct landlock_id id = { 332 .type = LANDLOCK_KEY_INODE, 333 }; 334 335 /* Files only get access rights that make sense. */ 336 if (!d_is_dir(path->dentry) && 337 !access_mask_subset(access_rights, ACCESS_FILE)) 338 return -EINVAL; 339 if (WARN_ON_ONCE(ruleset->num_layers != 1)) 340 return -EINVAL; 341 342 /* Transforms relative access rights to absolute ones. */ 343 access_rights |= LANDLOCK_MASK_ACCESS_FS & 344 ~landlock_get_fs_access_mask(ruleset, 0); 345 id.key.object = get_inode_object(d_backing_inode(path->dentry)); 346 if (IS_ERR(id.key.object)) 347 return PTR_ERR(id.key.object); 348 mutex_lock(&ruleset->lock); 349 err = landlock_insert_rule(ruleset, id, access_rights); 350 mutex_unlock(&ruleset->lock); 351 /* 352 * No need to check for an error because landlock_insert_rule() 353 * increments the refcount for the new object if needed. 354 */ 355 landlock_put_object(id.key.object); 356 return err; 357 } 358 359 /* Access-control management */ 360 361 /* 362 * The lifetime of the returned rule is tied to @domain. 363 * 364 * Returns NULL if no rule is found or if @dentry is negative. 365 */ 366 static const struct landlock_rule * 367 find_rule(const struct landlock_ruleset *const domain, 368 const struct dentry *const dentry) 369 { 370 const struct landlock_rule *rule; 371 const struct inode *inode; 372 struct landlock_id id = { 373 .type = LANDLOCK_KEY_INODE, 374 }; 375 376 /* Ignores nonexistent leafs. */ 377 if (d_is_negative(dentry)) 378 return NULL; 379 380 inode = d_backing_inode(dentry); 381 rcu_read_lock(); 382 id.key.object = rcu_dereference(landlock_inode(inode)->object); 383 rule = landlock_find_rule(domain, id); 384 rcu_read_unlock(); 385 return rule; 386 } 387 388 /* 389 * Allows access to pseudo filesystems that will never be mountable (e.g. 390 * sockfs, pipefs), but can still be reachable through 391 * /proc/<pid>/fd/<file-descriptor> 392 */ 393 static bool is_nouser_or_private(const struct dentry *dentry) 394 { 395 return (dentry->d_sb->s_flags & SB_NOUSER) || 396 (d_is_positive(dentry) && 397 unlikely(IS_PRIVATE(d_backing_inode(dentry)))); 398 } 399 400 static const struct access_masks any_fs = { 401 .fs = ~0, 402 }; 403 404 /* 405 * Returns true iff the child file with the given src_child access rights under 406 * src_parent would result in having the same or fewer access rights if it were 407 * moved under new_parent. 408 */ 409 static bool may_refer(const struct layer_access_masks *const src_parent, 410 const struct layer_access_masks *const src_child, 411 const struct layer_access_masks *const new_parent, 412 const bool child_is_dir) 413 { 414 for (size_t i = 0; i < ARRAY_SIZE(new_parent->access); i++) { 415 access_mask_t child_access = src_parent->access[i] & 416 src_child->access[i]; 417 access_mask_t parent_access = new_parent->access[i]; 418 419 if (!child_is_dir) { 420 child_access &= ACCESS_FILE; 421 parent_access &= ACCESS_FILE; 422 } 423 424 if (!access_mask_subset(child_access, parent_access)) 425 return false; 426 } 427 return true; 428 } 429 430 /* 431 * Check that a destination file hierarchy has more restrictions than a source 432 * file hierarchy. This is only used for link and rename actions. 433 * 434 * Return: True if child1 may be moved from parent1 to parent2 without 435 * increasing its access rights (if child2 is set, an additional condition is 436 * that child2 may be used from parent2 to parent1 without increasing its access 437 * rights), false otherwise. 438 */ 439 static bool no_more_access(const struct layer_access_masks *const parent1, 440 const struct layer_access_masks *const child1, 441 const bool child1_is_dir, 442 const struct layer_access_masks *const parent2, 443 const struct layer_access_masks *const child2, 444 const bool child2_is_dir) 445 { 446 if (!may_refer(parent1, child1, parent2, child1_is_dir)) 447 return false; 448 449 if (!child2) 450 return true; 451 452 return may_refer(parent2, child2, parent1, child2_is_dir); 453 } 454 455 #define NMA_TRUE(...) KUNIT_EXPECT_TRUE(test, no_more_access(__VA_ARGS__)) 456 #define NMA_FALSE(...) KUNIT_EXPECT_FALSE(test, no_more_access(__VA_ARGS__)) 457 458 #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST 459 460 static void test_no_more_access(struct kunit *const test) 461 { 462 const struct layer_access_masks rx0 = { 463 .access[0] = LANDLOCK_ACCESS_FS_EXECUTE | 464 LANDLOCK_ACCESS_FS_READ_FILE, 465 }; 466 const struct layer_access_masks mx0 = { 467 .access[0] = LANDLOCK_ACCESS_FS_EXECUTE | 468 LANDLOCK_ACCESS_FS_MAKE_REG, 469 }; 470 const struct layer_access_masks x0 = { 471 .access[0] = LANDLOCK_ACCESS_FS_EXECUTE, 472 }; 473 const struct layer_access_masks x1 = { 474 .access[1] = LANDLOCK_ACCESS_FS_EXECUTE, 475 }; 476 const struct layer_access_masks x01 = { 477 .access[0] = LANDLOCK_ACCESS_FS_EXECUTE, 478 .access[1] = LANDLOCK_ACCESS_FS_EXECUTE, 479 }; 480 const struct layer_access_masks allows_all = {}; 481 482 /* Checks without restriction. */ 483 NMA_TRUE(&x0, &allows_all, false, &allows_all, NULL, false); 484 NMA_TRUE(&allows_all, &x0, false, &allows_all, NULL, false); 485 NMA_FALSE(&x0, &x0, false, &allows_all, NULL, false); 486 487 /* 488 * Checks that we can only refer a file if no more access could be 489 * inherited. 490 */ 491 NMA_TRUE(&x0, &x0, false, &rx0, NULL, false); 492 NMA_TRUE(&rx0, &rx0, false, &rx0, NULL, false); 493 NMA_FALSE(&rx0, &rx0, false, &x0, NULL, false); 494 NMA_FALSE(&rx0, &rx0, false, &x1, NULL, false); 495 496 /* Checks allowed referring with different nested domains. */ 497 NMA_TRUE(&x0, &x1, false, &x0, NULL, false); 498 NMA_TRUE(&x1, &x0, false, &x0, NULL, false); 499 NMA_TRUE(&x0, &x01, false, &x0, NULL, false); 500 NMA_TRUE(&x0, &x01, false, &rx0, NULL, false); 501 NMA_TRUE(&x01, &x0, false, &x0, NULL, false); 502 NMA_TRUE(&x01, &x0, false, &rx0, NULL, false); 503 NMA_FALSE(&x01, &x01, false, &x0, NULL, false); 504 505 /* Checks that file access rights are also enforced for a directory. */ 506 NMA_FALSE(&rx0, &rx0, true, &x0, NULL, false); 507 508 /* Checks that directory access rights don't impact file referring... */ 509 NMA_TRUE(&mx0, &mx0, false, &x0, NULL, false); 510 /* ...but only directory referring. */ 511 NMA_FALSE(&mx0, &mx0, true, &x0, NULL, false); 512 513 /* Checks directory exchange. */ 514 NMA_TRUE(&mx0, &mx0, true, &mx0, &mx0, true); 515 NMA_TRUE(&mx0, &mx0, true, &mx0, &x0, true); 516 NMA_FALSE(&mx0, &mx0, true, &x0, &mx0, true); 517 NMA_FALSE(&mx0, &mx0, true, &x0, &x0, true); 518 NMA_FALSE(&mx0, &mx0, true, &x1, &x1, true); 519 520 /* Checks file exchange with directory access rights... */ 521 NMA_TRUE(&mx0, &mx0, false, &mx0, &mx0, false); 522 NMA_TRUE(&mx0, &mx0, false, &mx0, &x0, false); 523 NMA_TRUE(&mx0, &mx0, false, &x0, &mx0, false); 524 NMA_TRUE(&mx0, &mx0, false, &x0, &x0, false); 525 /* ...and with file access rights. */ 526 NMA_TRUE(&rx0, &rx0, false, &rx0, &rx0, false); 527 NMA_TRUE(&rx0, &rx0, false, &rx0, &x0, false); 528 NMA_FALSE(&rx0, &rx0, false, &x0, &rx0, false); 529 NMA_FALSE(&rx0, &rx0, false, &x0, &x0, false); 530 NMA_FALSE(&rx0, &rx0, false, &x1, &x1, false); 531 532 /* 533 * Allowing the following requests should not be a security risk 534 * because domain 0 denies execute access, and domain 1 is always 535 * nested with domain 0. However, adding an exception for this case 536 * would mean to check all nested domains to make sure none can get 537 * more privileges (e.g. processes only sandboxed by domain 0). 538 * Moreover, this behavior (i.e. composition of N domains) could then 539 * be inconsistent compared to domain 1's ruleset alone (e.g. it might 540 * be denied to link/rename with domain 1's ruleset, whereas it would 541 * be allowed if nested on top of domain 0). Another drawback would be 542 * to create a cover channel that could enable sandboxed processes to 543 * infer most of the filesystem restrictions from their domain. To 544 * make it simple, efficient, safe, and more consistent, this case is 545 * always denied. 546 */ 547 NMA_FALSE(&x1, &x1, false, &x0, NULL, false); 548 NMA_FALSE(&x1, &x1, false, &rx0, NULL, false); 549 NMA_FALSE(&x1, &x1, true, &x0, NULL, false); 550 NMA_FALSE(&x1, &x1, true, &rx0, NULL, false); 551 552 /* Checks the same case of exclusive domains with a file... */ 553 NMA_TRUE(&x1, &x1, false, &x01, NULL, false); 554 NMA_FALSE(&x1, &x1, false, &x01, &x0, false); 555 NMA_FALSE(&x1, &x1, false, &x01, &x01, false); 556 NMA_FALSE(&x1, &x1, false, &x0, &x0, false); 557 /* ...and with a directory. */ 558 NMA_FALSE(&x1, &x1, false, &x0, &x0, true); 559 NMA_FALSE(&x1, &x1, true, &x0, &x0, false); 560 NMA_FALSE(&x1, &x1, true, &x0, &x0, true); 561 } 562 563 #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ 564 565 #undef NMA_TRUE 566 #undef NMA_FALSE 567 568 static bool is_layer_masks_allowed(const struct layer_access_masks *masks) 569 { 570 return mem_is_zero(&masks->access, sizeof(masks->access)); 571 } 572 573 /* 574 * Removes @masks accesses that are not requested. 575 * 576 * Returns true if the request is allowed, false otherwise. 577 */ 578 static bool scope_to_request(const access_mask_t access_request, 579 struct layer_access_masks *masks) 580 { 581 bool saw_unfulfilled_access = false; 582 583 if (WARN_ON_ONCE(!masks)) 584 return true; 585 586 for (size_t i = 0; i < ARRAY_SIZE(masks->access); i++) { 587 masks->access[i] &= access_request; 588 if (masks->access[i]) 589 saw_unfulfilled_access = true; 590 } 591 return !saw_unfulfilled_access; 592 } 593 594 #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST 595 596 static void test_scope_to_request_with_exec_none(struct kunit *const test) 597 { 598 /* Allows everything. */ 599 struct layer_access_masks masks = {}; 600 601 /* Checks and scopes with execute. */ 602 KUNIT_EXPECT_TRUE(test, 603 scope_to_request(LANDLOCK_ACCESS_FS_EXECUTE, &masks)); 604 KUNIT_EXPECT_EQ(test, 0, masks.access[0]); 605 } 606 607 static void test_scope_to_request_with_exec_some(struct kunit *const test) 608 { 609 /* Denies execute and write. */ 610 struct layer_access_masks masks = { 611 .access[0] = LANDLOCK_ACCESS_FS_EXECUTE, 612 .access[1] = LANDLOCK_ACCESS_FS_WRITE_FILE, 613 }; 614 615 /* Checks and scopes with execute. */ 616 KUNIT_EXPECT_FALSE(test, scope_to_request(LANDLOCK_ACCESS_FS_EXECUTE, 617 &masks)); 618 KUNIT_EXPECT_EQ(test, LANDLOCK_ACCESS_FS_EXECUTE, masks.access[0]); 619 KUNIT_EXPECT_EQ(test, 0, masks.access[1]); 620 } 621 622 static void test_scope_to_request_without_access(struct kunit *const test) 623 { 624 /* Denies execute and write. */ 625 struct layer_access_masks masks = { 626 .access[0] = LANDLOCK_ACCESS_FS_EXECUTE, 627 .access[1] = LANDLOCK_ACCESS_FS_WRITE_FILE, 628 }; 629 630 /* Checks and scopes without access request. */ 631 KUNIT_EXPECT_TRUE(test, scope_to_request(0, &masks)); 632 KUNIT_EXPECT_EQ(test, 0, masks.access[0]); 633 KUNIT_EXPECT_EQ(test, 0, masks.access[1]); 634 } 635 636 #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ 637 638 /* 639 * Returns true if there is at least one access right different than 640 * LANDLOCK_ACCESS_FS_REFER. 641 */ 642 static bool is_eacces(const struct layer_access_masks *masks, 643 const access_mask_t access_request) 644 { 645 if (!masks) 646 return false; 647 648 for (size_t i = 0; i < ARRAY_SIZE(masks->access); i++) { 649 /* LANDLOCK_ACCESS_FS_REFER alone must return -EXDEV. */ 650 if (masks->access[i] & access_request & 651 ~LANDLOCK_ACCESS_FS_REFER) 652 return true; 653 } 654 return false; 655 } 656 657 #define IE_TRUE(...) KUNIT_EXPECT_TRUE(test, is_eacces(__VA_ARGS__)) 658 #define IE_FALSE(...) KUNIT_EXPECT_FALSE(test, is_eacces(__VA_ARGS__)) 659 660 #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST 661 662 static void test_is_eacces_with_none(struct kunit *const test) 663 { 664 const struct layer_access_masks masks = {}; 665 666 IE_FALSE(&masks, 0); 667 IE_FALSE(&masks, LANDLOCK_ACCESS_FS_REFER); 668 IE_FALSE(&masks, LANDLOCK_ACCESS_FS_EXECUTE); 669 IE_FALSE(&masks, LANDLOCK_ACCESS_FS_WRITE_FILE); 670 } 671 672 static void test_is_eacces_with_refer(struct kunit *const test) 673 { 674 const struct layer_access_masks masks = { 675 .access[0] = LANDLOCK_ACCESS_FS_REFER, 676 }; 677 678 IE_FALSE(&masks, 0); 679 IE_FALSE(&masks, LANDLOCK_ACCESS_FS_REFER); 680 IE_FALSE(&masks, LANDLOCK_ACCESS_FS_EXECUTE); 681 IE_FALSE(&masks, LANDLOCK_ACCESS_FS_WRITE_FILE); 682 } 683 684 static void test_is_eacces_with_write(struct kunit *const test) 685 { 686 const struct layer_access_masks masks = { 687 .access[0] = LANDLOCK_ACCESS_FS_WRITE_FILE, 688 }; 689 690 IE_FALSE(&masks, 0); 691 IE_FALSE(&masks, LANDLOCK_ACCESS_FS_REFER); 692 IE_FALSE(&masks, LANDLOCK_ACCESS_FS_EXECUTE); 693 694 IE_TRUE(&masks, LANDLOCK_ACCESS_FS_WRITE_FILE); 695 } 696 697 #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ 698 699 #undef IE_TRUE 700 #undef IE_FALSE 701 702 /** 703 * is_access_to_paths_allowed - Check accesses for requests with a common path 704 * 705 * @domain: Domain to check against. 706 * @path: File hierarchy to walk through. For refer checks, this would be 707 * the common mountpoint. 708 * @access_request_parent1: Accesses to check, once @layer_masks_parent1 is 709 * equal to @layer_masks_parent2 (if any). This is tied to the unique 710 * requested path for most actions, or the source in case of a refer action 711 * (i.e. rename or link), or the source and destination in case of 712 * RENAME_EXCHANGE. 713 * @layer_masks_parent1: Pointer to a matrix of layer masks per access 714 * masks, identifying the layers that forbid a specific access. Bits from 715 * this matrix can be unset according to the @path walk. An empty matrix 716 * means that @domain allows all possible Landlock accesses (i.e. not only 717 * those identified by @access_request_parent1). This matrix can 718 * initially refer to domain layer masks and, when the accesses for the 719 * destination and source are the same, to requested layer masks. 720 * @log_request_parent1: Audit request to fill if the related access is denied. 721 * @dentry_child1: Dentry to the initial child of the parent1 path. This 722 * pointer must be NULL for non-refer actions (i.e. not link nor rename). 723 * @access_request_parent2: Similar to @access_request_parent1 but for a 724 * request involving a source and a destination. This refers to the 725 * destination, except in case of RENAME_EXCHANGE where it also refers to 726 * the source. Must be set to 0 when using a simple path request. 727 * @layer_masks_parent2: Similar to @layer_masks_parent1 but for a refer 728 * action. This must be NULL otherwise. 729 * @log_request_parent2: Audit request to fill if the related access is denied. 730 * @dentry_child2: Dentry to the initial child of the parent2 path. This 731 * pointer is only set for RENAME_EXCHANGE actions and must be NULL 732 * otherwise. 733 * 734 * This helper first checks that the destination has a superset of restrictions 735 * compared to the source (if any) for a common path. Because of 736 * RENAME_EXCHANGE actions, source and destinations may be swapped. It then 737 * checks that the collected accesses and the remaining ones are enough to 738 * allow the request. 739 * 740 * Return: True if the access request is granted, false otherwise. 741 */ 742 static bool 743 is_access_to_paths_allowed(const struct landlock_ruleset *const domain, 744 const struct path *const path, 745 const access_mask_t access_request_parent1, 746 struct layer_access_masks *layer_masks_parent1, 747 struct landlock_request *const log_request_parent1, 748 struct dentry *const dentry_child1, 749 const access_mask_t access_request_parent2, 750 struct layer_access_masks *layer_masks_parent2, 751 struct landlock_request *const log_request_parent2, 752 struct dentry *const dentry_child2) 753 { 754 bool allowed_parent1 = false, allowed_parent2 = false, is_dom_check, 755 child1_is_directory = true, child2_is_directory = true; 756 struct path walker_path; 757 access_mask_t access_masked_parent1, access_masked_parent2; 758 struct layer_access_masks _layer_masks_child1, _layer_masks_child2; 759 struct layer_access_masks *layer_masks_child1 = NULL, 760 *layer_masks_child2 = NULL; 761 762 if (!access_request_parent1 && !access_request_parent2) 763 return true; 764 765 if (WARN_ON_ONCE(!path)) 766 return true; 767 768 if (is_nouser_or_private(path->dentry)) 769 return true; 770 771 if (WARN_ON_ONCE(!layer_masks_parent1)) 772 return false; 773 774 allowed_parent1 = is_layer_masks_allowed(layer_masks_parent1); 775 776 if (unlikely(layer_masks_parent2)) { 777 if (WARN_ON_ONCE(!dentry_child1)) 778 return false; 779 780 allowed_parent2 = is_layer_masks_allowed(layer_masks_parent2); 781 782 /* 783 * For a double request, first check for potential privilege 784 * escalation by looking at domain handled accesses (which are 785 * a superset of the meaningful requested accesses). 786 */ 787 access_masked_parent1 = access_masked_parent2 = 788 landlock_union_access_masks(domain).fs; 789 is_dom_check = true; 790 } else { 791 if (WARN_ON_ONCE(dentry_child1 || dentry_child2)) 792 return false; 793 /* For a simple request, only check for requested accesses. */ 794 access_masked_parent1 = access_request_parent1; 795 access_masked_parent2 = access_request_parent2; 796 is_dom_check = false; 797 } 798 799 if (unlikely(dentry_child1)) { 800 if (landlock_init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS, 801 &_layer_masks_child1, 802 LANDLOCK_KEY_INODE)) 803 landlock_unmask_layers(find_rule(domain, dentry_child1), 804 &_layer_masks_child1); 805 layer_masks_child1 = &_layer_masks_child1; 806 child1_is_directory = d_is_dir(dentry_child1); 807 } 808 if (unlikely(dentry_child2)) { 809 if (landlock_init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS, 810 &_layer_masks_child2, 811 LANDLOCK_KEY_INODE)) 812 landlock_unmask_layers(find_rule(domain, dentry_child2), 813 &_layer_masks_child2); 814 layer_masks_child2 = &_layer_masks_child2; 815 child2_is_directory = d_is_dir(dentry_child2); 816 } 817 818 walker_path = *path; 819 path_get(&walker_path); 820 /* 821 * We need to walk through all the hierarchy to not miss any relevant 822 * restriction. 823 */ 824 while (true) { 825 const struct landlock_rule *rule; 826 827 /* 828 * If at least all accesses allowed on the destination are 829 * already allowed on the source, respectively if there is at 830 * least as much as restrictions on the destination than on the 831 * source, then we can safely refer files from the source to 832 * the destination without risking a privilege escalation. 833 * This also applies in the case of RENAME_EXCHANGE, which 834 * implies checks on both direction. This is crucial for 835 * standalone multilayered security policies. Furthermore, 836 * this helps avoid policy writers to shoot themselves in the 837 * foot. 838 */ 839 if (unlikely(is_dom_check && 840 no_more_access( 841 layer_masks_parent1, layer_masks_child1, 842 child1_is_directory, layer_masks_parent2, 843 layer_masks_child2, 844 child2_is_directory))) { 845 /* 846 * Now, downgrades the remaining checks from domain 847 * handled accesses to requested accesses. 848 */ 849 is_dom_check = false; 850 access_masked_parent1 = access_request_parent1; 851 access_masked_parent2 = access_request_parent2; 852 853 allowed_parent1 = 854 allowed_parent1 || 855 scope_to_request(access_masked_parent1, 856 layer_masks_parent1); 857 allowed_parent2 = 858 allowed_parent2 || 859 scope_to_request(access_masked_parent2, 860 layer_masks_parent2); 861 862 /* Stops when all accesses are granted. */ 863 if (allowed_parent1 && allowed_parent2) 864 break; 865 } 866 867 rule = find_rule(domain, walker_path.dentry); 868 allowed_parent1 = 869 allowed_parent1 || 870 landlock_unmask_layers(rule, layer_masks_parent1); 871 allowed_parent2 = 872 allowed_parent2 || 873 landlock_unmask_layers(rule, layer_masks_parent2); 874 875 /* Stops when a rule from each layer grants access. */ 876 if (allowed_parent1 && allowed_parent2) 877 break; 878 879 jump_up: 880 if (walker_path.dentry == walker_path.mnt->mnt_root) { 881 if (follow_up(&walker_path)) { 882 /* Ignores hidden mount points. */ 883 goto jump_up; 884 } else { 885 /* 886 * Stops at the real root. Denies access 887 * because not all layers have granted access. 888 */ 889 break; 890 } 891 } 892 893 if (unlikely(IS_ROOT(walker_path.dentry))) { 894 if (likely(walker_path.mnt->mnt_flags & MNT_INTERNAL)) { 895 /* 896 * Stops and allows access when reaching disconnected root 897 * directories that are part of internal filesystems (e.g. nsfs, 898 * which is reachable through /proc/<pid>/ns/<namespace>). 899 */ 900 allowed_parent1 = true; 901 allowed_parent2 = true; 902 break; 903 } 904 905 /* 906 * We reached a disconnected root directory from a bind mount. 907 * Let's continue the walk with the mount point we missed. 908 */ 909 dput(walker_path.dentry); 910 walker_path.dentry = walker_path.mnt->mnt_root; 911 dget(walker_path.dentry); 912 } else { 913 struct dentry *const parent_dentry = 914 dget_parent(walker_path.dentry); 915 916 dput(walker_path.dentry); 917 walker_path.dentry = parent_dentry; 918 } 919 } 920 path_put(&walker_path); 921 922 /* 923 * Check CONFIG_AUDIT to enable elision of log_request_parent* and 924 * associated caller's stack variables thanks to dead code elimination. 925 */ 926 #ifdef CONFIG_AUDIT 927 if (!allowed_parent1 && log_request_parent1) { 928 log_request_parent1->type = LANDLOCK_REQUEST_FS_ACCESS; 929 log_request_parent1->audit.type = LSM_AUDIT_DATA_PATH; 930 log_request_parent1->audit.u.path = *path; 931 log_request_parent1->access = access_masked_parent1; 932 log_request_parent1->layer_masks = layer_masks_parent1; 933 } 934 935 if (!allowed_parent2 && log_request_parent2) { 936 log_request_parent2->type = LANDLOCK_REQUEST_FS_ACCESS; 937 log_request_parent2->audit.type = LSM_AUDIT_DATA_PATH; 938 log_request_parent2->audit.u.path = *path; 939 log_request_parent2->access = access_masked_parent2; 940 log_request_parent2->layer_masks = layer_masks_parent2; 941 } 942 #endif /* CONFIG_AUDIT */ 943 944 return allowed_parent1 && allowed_parent2; 945 } 946 947 static int current_check_access_path(const struct path *const path, 948 access_mask_t access_request) 949 { 950 const struct access_masks masks = { 951 .fs = access_request, 952 }; 953 const struct landlock_cred_security *const subject = 954 landlock_get_applicable_subject(current_cred(), masks, NULL); 955 struct layer_access_masks layer_masks; 956 struct landlock_request request = {}; 957 958 if (!subject) 959 return 0; 960 961 access_request = landlock_init_layer_masks(subject->domain, 962 access_request, &layer_masks, 963 LANDLOCK_KEY_INODE); 964 if (is_access_to_paths_allowed(subject->domain, path, access_request, 965 &layer_masks, &request, NULL, 0, NULL, 966 NULL, NULL)) 967 return 0; 968 969 landlock_log_denial(subject, &request); 970 return -EACCES; 971 } 972 973 static __attribute_const__ access_mask_t get_mode_access(const umode_t mode) 974 { 975 switch (mode & S_IFMT) { 976 case S_IFLNK: 977 return LANDLOCK_ACCESS_FS_MAKE_SYM; 978 case S_IFDIR: 979 return LANDLOCK_ACCESS_FS_MAKE_DIR; 980 case S_IFCHR: 981 return LANDLOCK_ACCESS_FS_MAKE_CHAR; 982 case S_IFBLK: 983 return LANDLOCK_ACCESS_FS_MAKE_BLOCK; 984 case S_IFIFO: 985 return LANDLOCK_ACCESS_FS_MAKE_FIFO; 986 case S_IFSOCK: 987 return LANDLOCK_ACCESS_FS_MAKE_SOCK; 988 case S_IFREG: 989 case 0: 990 /* A zero mode translates to S_IFREG. */ 991 default: 992 /* Treats weird files as regular files. */ 993 return LANDLOCK_ACCESS_FS_MAKE_REG; 994 } 995 } 996 997 static access_mask_t maybe_remove(const struct dentry *const dentry) 998 { 999 if (d_is_negative(dentry)) 1000 return 0; 1001 return d_is_dir(dentry) ? LANDLOCK_ACCESS_FS_REMOVE_DIR : 1002 LANDLOCK_ACCESS_FS_REMOVE_FILE; 1003 } 1004 1005 /** 1006 * collect_domain_accesses - Walk through a file path and collect accesses 1007 * 1008 * @domain: Domain to check against. 1009 * @mnt_root: Last directory to check. 1010 * @dir: Directory to start the walk from. 1011 * @layer_masks_dom: Where to store the collected accesses. 1012 * 1013 * This helper is useful to begin a path walk from the @dir directory to a 1014 * @mnt_root directory used as a mount point. This mount point is the common 1015 * ancestor between the source and the destination of a renamed and linked 1016 * file. While walking from @dir to @mnt_root, we record all the domain's 1017 * allowed accesses in @layer_masks_dom. 1018 * 1019 * Because of disconnected directories, this walk may not reach @mnt_dir. In 1020 * this case, the walk will continue to @mnt_dir after this call. 1021 * 1022 * This is similar to is_access_to_paths_allowed() but much simpler because it 1023 * only handles walking on the same mount point and only checks one set of 1024 * accesses. 1025 * 1026 * Return: True if all the domain access rights are allowed for @dir, false if 1027 * the walk reached @mnt_root. 1028 */ 1029 static bool collect_domain_accesses(const struct landlock_ruleset *const domain, 1030 const struct dentry *const mnt_root, 1031 struct dentry *dir, 1032 struct layer_access_masks *layer_masks_dom) 1033 { 1034 bool ret = false; 1035 1036 if (WARN_ON_ONCE(!domain || !mnt_root || !dir || !layer_masks_dom)) 1037 return true; 1038 if (is_nouser_or_private(dir)) 1039 return true; 1040 1041 if (!landlock_init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS, 1042 layer_masks_dom, LANDLOCK_KEY_INODE)) 1043 return true; 1044 1045 dget(dir); 1046 while (true) { 1047 struct dentry *parent_dentry; 1048 1049 /* Gets all layers allowing all domain accesses. */ 1050 if (landlock_unmask_layers(find_rule(domain, dir), 1051 layer_masks_dom)) { 1052 /* 1053 * Stops when all handled accesses are allowed by at 1054 * least one rule in each layer. 1055 */ 1056 ret = true; 1057 break; 1058 } 1059 1060 /* 1061 * Stops at the mount point or the filesystem root for a disconnected 1062 * directory. 1063 */ 1064 if (dir == mnt_root || unlikely(IS_ROOT(dir))) 1065 break; 1066 1067 parent_dentry = dget_parent(dir); 1068 dput(dir); 1069 dir = parent_dentry; 1070 } 1071 dput(dir); 1072 return ret; 1073 } 1074 1075 /** 1076 * current_check_refer_path - Check if a rename or link action is allowed 1077 * 1078 * @old_dentry: File or directory requested to be moved or linked. 1079 * @new_dir: Destination parent directory. 1080 * @new_dentry: Destination file or directory. 1081 * @removable: Sets to true if it is a rename operation. 1082 * @exchange: Sets to true if it is a rename operation with RENAME_EXCHANGE. 1083 * 1084 * Because of its unprivileged constraints, Landlock relies on file hierarchies 1085 * (and not only inodes) to tie access rights to files. Being able to link or 1086 * rename a file hierarchy brings some challenges. Indeed, moving or linking a 1087 * file (i.e. creating a new reference to an inode) can have an impact on the 1088 * actions allowed for a set of files if it would change its parent directory 1089 * (i.e. reparenting). 1090 * 1091 * To avoid trivial access right bypasses, Landlock first checks if the file or 1092 * directory requested to be moved would gain new access rights inherited from 1093 * its new hierarchy. Before returning any error, Landlock then checks that 1094 * the parent source hierarchy and the destination hierarchy would allow the 1095 * link or rename action. If it is not the case, an error with EACCES is 1096 * returned to inform user space that there is no way to remove or create the 1097 * requested source file type. If it should be allowed but the new inherited 1098 * access rights would be greater than the source access rights, then the 1099 * kernel returns an error with EXDEV. Prioritizing EACCES over EXDEV enables 1100 * user space to abort the whole operation if there is no way to do it, or to 1101 * manually copy the source to the destination if this remains allowed, e.g. 1102 * because file creation is allowed on the destination directory but not direct 1103 * linking. 1104 * 1105 * To achieve this goal, the kernel needs to compare two file hierarchies: the 1106 * one identifying the source file or directory (including itself), and the 1107 * destination one. This can be seen as a multilayer partial ordering problem. 1108 * The kernel walks through these paths and collects in a matrix the access 1109 * rights that are denied per layer. These matrices are then compared to see 1110 * if the destination one has more (or the same) restrictions as the source 1111 * one. If this is the case, the requested action will not return EXDEV, which 1112 * doesn't mean the action is allowed. The parent hierarchy of the source 1113 * (i.e. parent directory), and the destination hierarchy must also be checked 1114 * to verify that they explicitly allow such action (i.e. referencing, 1115 * creation and potentially removal rights). The kernel implementation is then 1116 * required to rely on potentially four matrices of access rights: one for the 1117 * source file or directory (i.e. the child), a potentially other one for the 1118 * other source/destination (in case of RENAME_EXCHANGE), one for the source 1119 * parent hierarchy and a last one for the destination hierarchy. These 1120 * ephemeral matrices take some space on the stack, which limits the number of 1121 * layers to a deemed reasonable number: 16. 1122 * 1123 * Return: 0 if access is allowed, -EXDEV if @old_dentry would inherit new 1124 * access rights from @new_dir, or -EACCES if file removal or creation is 1125 * denied. 1126 */ 1127 static int current_check_refer_path(struct dentry *const old_dentry, 1128 const struct path *const new_dir, 1129 struct dentry *const new_dentry, 1130 const bool removable, const bool exchange) 1131 { 1132 const struct landlock_cred_security *const subject = 1133 landlock_get_applicable_subject(current_cred(), any_fs, NULL); 1134 bool allow_parent1, allow_parent2; 1135 access_mask_t access_request_parent1, access_request_parent2; 1136 struct path mnt_dir; 1137 struct dentry *old_parent; 1138 struct layer_access_masks layer_masks_parent1 = {}, 1139 layer_masks_parent2 = {}; 1140 struct landlock_request request1 = {}, request2 = {}; 1141 1142 if (!subject) 1143 return 0; 1144 1145 if (unlikely(d_is_negative(old_dentry))) 1146 return -ENOENT; 1147 if (exchange) { 1148 if (unlikely(d_is_negative(new_dentry))) 1149 return -ENOENT; 1150 access_request_parent1 = 1151 get_mode_access(d_backing_inode(new_dentry)->i_mode); 1152 } else { 1153 access_request_parent1 = 0; 1154 } 1155 access_request_parent2 = 1156 get_mode_access(d_backing_inode(old_dentry)->i_mode); 1157 if (removable) { 1158 access_request_parent1 |= maybe_remove(old_dentry); 1159 access_request_parent2 |= maybe_remove(new_dentry); 1160 } 1161 1162 /* The mount points are the same for old and new paths, cf. EXDEV. */ 1163 if (old_dentry->d_parent == new_dir->dentry) { 1164 /* 1165 * The LANDLOCK_ACCESS_FS_REFER access right is not required 1166 * for same-directory referer (i.e. no reparenting). 1167 */ 1168 access_request_parent1 = landlock_init_layer_masks( 1169 subject->domain, 1170 access_request_parent1 | access_request_parent2, 1171 &layer_masks_parent1, LANDLOCK_KEY_INODE); 1172 if (is_access_to_paths_allowed(subject->domain, new_dir, 1173 access_request_parent1, 1174 &layer_masks_parent1, &request1, 1175 NULL, 0, NULL, NULL, NULL)) 1176 return 0; 1177 1178 landlock_log_denial(subject, &request1); 1179 return -EACCES; 1180 } 1181 1182 access_request_parent1 |= LANDLOCK_ACCESS_FS_REFER; 1183 access_request_parent2 |= LANDLOCK_ACCESS_FS_REFER; 1184 1185 /* Saves the common mount point. */ 1186 mnt_dir.mnt = new_dir->mnt; 1187 mnt_dir.dentry = new_dir->mnt->mnt_root; 1188 1189 /* 1190 * old_dentry may be the root of the common mount point and 1191 * !IS_ROOT(old_dentry) at the same time (e.g. with open_tree() and 1192 * OPEN_TREE_CLONE). We do not need to call dget(old_parent) because 1193 * we keep a reference to old_dentry. 1194 */ 1195 old_parent = (old_dentry == mnt_dir.dentry) ? old_dentry : 1196 old_dentry->d_parent; 1197 1198 /* new_dir->dentry is equal to new_dentry->d_parent */ 1199 allow_parent1 = collect_domain_accesses(subject->domain, mnt_dir.dentry, 1200 old_parent, 1201 &layer_masks_parent1); 1202 allow_parent2 = collect_domain_accesses(subject->domain, mnt_dir.dentry, 1203 new_dir->dentry, 1204 &layer_masks_parent2); 1205 1206 if (allow_parent1 && allow_parent2) 1207 return 0; 1208 1209 /* 1210 * To be able to compare source and destination domain access rights, 1211 * take into account the @old_dentry access rights aggregated with its 1212 * parent access rights. This will be useful to compare with the 1213 * destination parent access rights. 1214 */ 1215 if (is_access_to_paths_allowed( 1216 subject->domain, &mnt_dir, access_request_parent1, 1217 &layer_masks_parent1, &request1, old_dentry, 1218 access_request_parent2, &layer_masks_parent2, &request2, 1219 exchange ? new_dentry : NULL)) 1220 return 0; 1221 1222 if (request1.access) { 1223 request1.audit.u.path.dentry = old_parent; 1224 landlock_log_denial(subject, &request1); 1225 } 1226 if (request2.access) { 1227 request2.audit.u.path.dentry = new_dir->dentry; 1228 landlock_log_denial(subject, &request2); 1229 } 1230 1231 /* 1232 * This prioritizes EACCES over EXDEV for all actions, including 1233 * renames with RENAME_EXCHANGE. 1234 */ 1235 if (likely(is_eacces(&layer_masks_parent1, access_request_parent1) || 1236 is_eacces(&layer_masks_parent2, access_request_parent2))) 1237 return -EACCES; 1238 1239 /* 1240 * Gracefully forbids reparenting if the destination directory 1241 * hierarchy is not a superset of restrictions of the source directory 1242 * hierarchy, or if LANDLOCK_ACCESS_FS_REFER is not allowed by the 1243 * source or the destination. 1244 */ 1245 return -EXDEV; 1246 } 1247 1248 /* Inode hooks */ 1249 1250 static void hook_inode_free_security_rcu(void *inode_security) 1251 { 1252 struct landlock_inode_security *inode_sec; 1253 1254 /* 1255 * All inodes must already have been untied from their object by 1256 * release_inode() or hook_sb_delete(). 1257 */ 1258 inode_sec = inode_security + landlock_blob_sizes.lbs_inode; 1259 WARN_ON_ONCE(inode_sec->object); 1260 } 1261 1262 /* Super-block hooks */ 1263 1264 /* 1265 * Release the inodes used in a security policy. 1266 * 1267 * Cf. fsnotify_unmount_inodes() and evict_inodes() 1268 */ 1269 static void hook_sb_delete(struct super_block *const sb) 1270 { 1271 struct inode *inode, *prev_inode = NULL; 1272 1273 if (!landlock_initialized) 1274 return; 1275 1276 spin_lock(&sb->s_inode_list_lock); 1277 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 1278 struct landlock_object *object; 1279 1280 /* Only handles referenced inodes. */ 1281 if (!icount_read(inode)) 1282 continue; 1283 1284 /* 1285 * Protects against concurrent modification of inode (e.g. 1286 * from get_inode_object()). 1287 */ 1288 spin_lock(&inode->i_lock); 1289 /* 1290 * Checks I_FREEING and I_WILL_FREE to protect against a race 1291 * condition when release_inode() just called iput(), which 1292 * could lead to a NULL dereference of inode->security or a 1293 * second call to iput() for the same Landlock object. Also 1294 * checks I_NEW because such inode cannot be tied to an object. 1295 */ 1296 if (inode_state_read(inode) & 1297 (I_FREEING | I_WILL_FREE | I_NEW)) { 1298 spin_unlock(&inode->i_lock); 1299 continue; 1300 } 1301 1302 rcu_read_lock(); 1303 object = rcu_dereference(landlock_inode(inode)->object); 1304 if (!object) { 1305 rcu_read_unlock(); 1306 spin_unlock(&inode->i_lock); 1307 continue; 1308 } 1309 /* Keeps a reference to this inode until the next loop walk. */ 1310 __iget(inode); 1311 spin_unlock(&inode->i_lock); 1312 1313 /* 1314 * If there is no concurrent release_inode() ongoing, then we 1315 * are in charge of calling iput() on this inode, otherwise we 1316 * will just wait for it to finish. 1317 */ 1318 spin_lock(&object->lock); 1319 if (object->underobj == inode) { 1320 object->underobj = NULL; 1321 spin_unlock(&object->lock); 1322 rcu_read_unlock(); 1323 1324 /* 1325 * Because object->underobj was not NULL, 1326 * release_inode() and get_inode_object() guarantee 1327 * that it is safe to reset 1328 * landlock_inode(inode)->object while it is not NULL. 1329 * It is therefore not necessary to lock inode->i_lock. 1330 */ 1331 rcu_assign_pointer(landlock_inode(inode)->object, NULL); 1332 /* 1333 * At this point, we own the ihold() reference that was 1334 * originally set up by get_inode_object() and the 1335 * __iget() reference that we just set in this loop 1336 * walk. Therefore there are at least two references 1337 * on the inode. 1338 */ 1339 iput_not_last(inode); 1340 } else { 1341 spin_unlock(&object->lock); 1342 rcu_read_unlock(); 1343 } 1344 1345 if (prev_inode) { 1346 /* 1347 * At this point, we still own the __iget() reference 1348 * that we just set in this loop walk. Therefore we 1349 * can drop the list lock and know that the inode won't 1350 * disappear from under us until the next loop walk. 1351 */ 1352 spin_unlock(&sb->s_inode_list_lock); 1353 /* 1354 * We can now actually put the inode reference from the 1355 * previous loop walk, which is not needed anymore. 1356 */ 1357 iput(prev_inode); 1358 cond_resched(); 1359 spin_lock(&sb->s_inode_list_lock); 1360 } 1361 prev_inode = inode; 1362 } 1363 spin_unlock(&sb->s_inode_list_lock); 1364 1365 /* Puts the inode reference from the last loop walk, if any. */ 1366 if (prev_inode) 1367 iput(prev_inode); 1368 /* Waits for pending iput() in release_inode(). */ 1369 wait_var_event(&landlock_superblock(sb)->inode_refs, 1370 !atomic_long_read(&landlock_superblock(sb)->inode_refs)); 1371 } 1372 1373 static void 1374 log_fs_change_topology_path(const struct landlock_cred_security *const subject, 1375 size_t handle_layer, const struct path *const path) 1376 { 1377 landlock_log_denial(subject, &(struct landlock_request) { 1378 .type = LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY, 1379 .audit = { 1380 .type = LSM_AUDIT_DATA_PATH, 1381 .u.path = *path, 1382 }, 1383 .layer_plus_one = handle_layer + 1, 1384 }); 1385 } 1386 1387 static void log_fs_change_topology_dentry( 1388 const struct landlock_cred_security *const subject, size_t handle_layer, 1389 struct dentry *const dentry) 1390 { 1391 landlock_log_denial(subject, &(struct landlock_request) { 1392 .type = LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY, 1393 .audit = { 1394 .type = LSM_AUDIT_DATA_DENTRY, 1395 .u.dentry = dentry, 1396 }, 1397 .layer_plus_one = handle_layer + 1, 1398 }); 1399 } 1400 1401 /* 1402 * Because a Landlock security policy is defined according to the filesystem 1403 * topology (i.e. the mount namespace), changing it may grant access to files 1404 * not previously allowed. 1405 * 1406 * To make it simple, deny any filesystem topology modification by landlocked 1407 * processes. Non-landlocked processes may still change the namespace of a 1408 * landlocked process, but this kind of threat must be handled by a system-wide 1409 * access-control security policy. 1410 * 1411 * This could be lifted in the future if Landlock can safely handle mount 1412 * namespace updates requested by a landlocked process. Indeed, we could 1413 * update the current domain (which is currently read-only) by taking into 1414 * account the accesses of the source and the destination of a new mount point. 1415 * However, it would also require to make all the child domains dynamically 1416 * inherit these new constraints. Anyway, for backward compatibility reasons, 1417 * a dedicated user space option would be required (e.g. as a ruleset flag). 1418 */ 1419 static int hook_sb_mount(const char *const dev_name, 1420 const struct path *const path, const char *const type, 1421 const unsigned long flags, void *const data) 1422 { 1423 size_t handle_layer; 1424 const struct landlock_cred_security *const subject = 1425 landlock_get_applicable_subject(current_cred(), any_fs, 1426 &handle_layer); 1427 1428 if (!subject) 1429 return 0; 1430 1431 log_fs_change_topology_path(subject, handle_layer, path); 1432 return -EPERM; 1433 } 1434 1435 static int hook_move_mount(const struct path *const from_path, 1436 const struct path *const to_path) 1437 { 1438 size_t handle_layer; 1439 const struct landlock_cred_security *const subject = 1440 landlock_get_applicable_subject(current_cred(), any_fs, 1441 &handle_layer); 1442 1443 if (!subject) 1444 return 0; 1445 1446 log_fs_change_topology_path(subject, handle_layer, to_path); 1447 return -EPERM; 1448 } 1449 1450 /* 1451 * Removing a mount point may reveal a previously hidden file hierarchy, which 1452 * may then grant access to files, which may have previously been forbidden. 1453 */ 1454 static int hook_sb_umount(struct vfsmount *const mnt, const int flags) 1455 { 1456 size_t handle_layer; 1457 const struct landlock_cred_security *const subject = 1458 landlock_get_applicable_subject(current_cred(), any_fs, 1459 &handle_layer); 1460 1461 if (!subject) 1462 return 0; 1463 1464 log_fs_change_topology_dentry(subject, handle_layer, mnt->mnt_root); 1465 return -EPERM; 1466 } 1467 1468 static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts) 1469 { 1470 size_t handle_layer; 1471 const struct landlock_cred_security *const subject = 1472 landlock_get_applicable_subject(current_cred(), any_fs, 1473 &handle_layer); 1474 1475 if (!subject) 1476 return 0; 1477 1478 log_fs_change_topology_dentry(subject, handle_layer, sb->s_root); 1479 return -EPERM; 1480 } 1481 1482 /* 1483 * pivot_root(2), like mount(2), changes the current mount namespace. It must 1484 * then be forbidden for a landlocked process. 1485 * 1486 * However, chroot(2) may be allowed because it only changes the relative root 1487 * directory of the current process. Moreover, it can be used to restrict the 1488 * view of the filesystem. 1489 */ 1490 static int hook_sb_pivotroot(const struct path *const old_path, 1491 const struct path *const new_path) 1492 { 1493 size_t handle_layer; 1494 const struct landlock_cred_security *const subject = 1495 landlock_get_applicable_subject(current_cred(), any_fs, 1496 &handle_layer); 1497 1498 if (!subject) 1499 return 0; 1500 1501 log_fs_change_topology_path(subject, handle_layer, new_path); 1502 return -EPERM; 1503 } 1504 1505 /* Path hooks */ 1506 1507 static int hook_path_link(struct dentry *const old_dentry, 1508 const struct path *const new_dir, 1509 struct dentry *const new_dentry) 1510 { 1511 return current_check_refer_path(old_dentry, new_dir, new_dentry, false, 1512 false); 1513 } 1514 1515 static int hook_path_rename(const struct path *const old_dir, 1516 struct dentry *const old_dentry, 1517 const struct path *const new_dir, 1518 struct dentry *const new_dentry, 1519 const unsigned int flags) 1520 { 1521 /* old_dir refers to old_dentry->d_parent and new_dir->mnt */ 1522 return current_check_refer_path(old_dentry, new_dir, new_dentry, true, 1523 !!(flags & RENAME_EXCHANGE)); 1524 } 1525 1526 static int hook_path_mkdir(const struct path *const dir, 1527 struct dentry *const dentry, const umode_t mode) 1528 { 1529 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_DIR); 1530 } 1531 1532 static int hook_path_mknod(const struct path *const dir, 1533 struct dentry *const dentry, const umode_t mode, 1534 const unsigned int dev) 1535 { 1536 return current_check_access_path(dir, get_mode_access(mode)); 1537 } 1538 1539 static int hook_path_symlink(const struct path *const dir, 1540 struct dentry *const dentry, 1541 const char *const old_name) 1542 { 1543 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_SYM); 1544 } 1545 1546 static int hook_path_unlink(const struct path *const dir, 1547 struct dentry *const dentry) 1548 { 1549 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_FILE); 1550 } 1551 1552 static int hook_path_rmdir(const struct path *const dir, 1553 struct dentry *const dentry) 1554 { 1555 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_DIR); 1556 } 1557 1558 static int hook_path_truncate(const struct path *const path) 1559 { 1560 return current_check_access_path(path, LANDLOCK_ACCESS_FS_TRUNCATE); 1561 } 1562 1563 /** 1564 * unmask_scoped_access - Remove access right bits in @masks in all layers 1565 * where @client and @server have the same domain 1566 * 1567 * This does the same as domain_is_scoped(), but unmasks bits in @masks. 1568 * It can not return early as domain_is_scoped() does. 1569 * 1570 * A scoped access for a given access right bit is allowed iff, for all layer 1571 * depths where the access bit is set, the client and server domain are the 1572 * same. This function clears the access rights @access in @masks at all layer 1573 * depths where the client and server domain are the same, so that, when they 1574 * are all cleared, the access is allowed. 1575 * 1576 * @client: Client domain 1577 * @server: Server domain 1578 * @masks: Layer access masks to unmask 1579 * @access: Access bits that control scoping 1580 */ 1581 static void unmask_scoped_access(const struct landlock_ruleset *const client, 1582 const struct landlock_ruleset *const server, 1583 struct layer_access_masks *const masks, 1584 const access_mask_t access) 1585 { 1586 int client_layer, server_layer; 1587 const struct landlock_hierarchy *client_walker, *server_walker; 1588 1589 /* This should not happen. */ 1590 if (WARN_ON_ONCE(!client)) 1591 return; 1592 1593 /* Server has no Landlock domain; nothing to clear. */ 1594 if (!server) 1595 return; 1596 1597 /* 1598 * client_layer must be able to represent all numbers from 1599 * LANDLOCK_MAX_NUM_LAYERS - 1 to -1 for the loop below to terminate. 1600 * (It must be large enough, and it must be signed.) 1601 */ 1602 BUILD_BUG_ON(!is_signed_type(typeof(client_layer))); 1603 BUILD_BUG_ON(LANDLOCK_MAX_NUM_LAYERS - 1 > 1604 type_max(typeof(client_layer))); 1605 1606 client_layer = client->num_layers - 1; 1607 client_walker = client->hierarchy; 1608 server_layer = server->num_layers - 1; 1609 server_walker = server->hierarchy; 1610 1611 /* 1612 * Clears the access bits at all layers where the client domain is the 1613 * same as the server domain. We start the walk at min(client_layer, 1614 * server_layer). The layer bits until there can not be cleared because 1615 * either the client or the server domain is missing. 1616 */ 1617 for (; client_layer > server_layer; client_layer--) 1618 client_walker = client_walker->parent; 1619 1620 for (; server_layer > client_layer; server_layer--) 1621 server_walker = server_walker->parent; 1622 1623 for (; client_layer >= 0; client_layer--) { 1624 if (masks->access[client_layer] & access && 1625 client_walker == server_walker) 1626 masks->access[client_layer] &= ~access; 1627 1628 client_walker = client_walker->parent; 1629 server_walker = server_walker->parent; 1630 } 1631 } 1632 1633 static int hook_unix_find(const struct path *const path, struct sock *other, 1634 int flags) 1635 { 1636 const struct landlock_ruleset *dom_other; 1637 const struct landlock_cred_security *subject; 1638 struct layer_access_masks layer_masks; 1639 struct landlock_request request = {}; 1640 static const struct access_masks fs_resolve_unix = { 1641 .fs = LANDLOCK_ACCESS_FS_RESOLVE_UNIX, 1642 }; 1643 1644 /* Lookup for the purpose of saving coredumps is OK. */ 1645 if (unlikely(flags & SOCK_COREDUMP)) 1646 return 0; 1647 1648 subject = landlock_get_applicable_subject(current_cred(), 1649 fs_resolve_unix, NULL); 1650 1651 if (!subject) 1652 return 0; 1653 1654 /* 1655 * Ignoring return value: that the domains apply was already checked in 1656 * landlock_get_applicable_subject() above. 1657 */ 1658 landlock_init_layer_masks(subject->domain, fs_resolve_unix.fs, 1659 &layer_masks, LANDLOCK_KEY_INODE); 1660 1661 /* Checks the layers in which we are connecting within the same domain. */ 1662 unix_state_lock(other); 1663 if (unlikely(sock_flag(other, SOCK_DEAD) || !other->sk_socket || 1664 !other->sk_socket->file)) { 1665 unix_state_unlock(other); 1666 /* 1667 * We rely on the caller to catch the (non-reversible) SOCK_DEAD 1668 * condition and retry the lookup. If we returned an error 1669 * here, the lookup would not get retried. 1670 */ 1671 return 0; 1672 } 1673 dom_other = landlock_cred(other->sk_socket->file->f_cred)->domain; 1674 1675 /* Access to the same (or a lower) domain is always allowed. */ 1676 unmask_scoped_access(subject->domain, dom_other, &layer_masks, 1677 fs_resolve_unix.fs); 1678 unix_state_unlock(other); 1679 1680 /* Checks the connections to allow-listed paths. */ 1681 if (is_access_to_paths_allowed(subject->domain, path, 1682 fs_resolve_unix.fs, &layer_masks, 1683 &request, NULL, 0, NULL, NULL, NULL)) 1684 return 0; 1685 1686 landlock_log_denial(subject, &request); 1687 return -EACCES; 1688 } 1689 1690 /* File hooks */ 1691 1692 /** 1693 * get_required_file_open_access - Get access needed to open a file 1694 * 1695 * @file: File being opened. 1696 * 1697 * Return: The access rights that are required for opening the given file, 1698 * depending on the file type and open mode. 1699 */ 1700 static access_mask_t 1701 get_required_file_open_access(const struct file *const file) 1702 { 1703 access_mask_t access = 0; 1704 1705 if (file->f_mode & FMODE_READ) { 1706 /* A directory can only be opened in read mode. */ 1707 if (S_ISDIR(file_inode(file)->i_mode)) 1708 return LANDLOCK_ACCESS_FS_READ_DIR; 1709 access = LANDLOCK_ACCESS_FS_READ_FILE; 1710 } 1711 if (file->f_mode & FMODE_WRITE) 1712 access |= LANDLOCK_ACCESS_FS_WRITE_FILE; 1713 /* __FMODE_EXEC is indeed part of f_flags, not f_mode. */ 1714 if (file->f_flags & __FMODE_EXEC) 1715 access |= LANDLOCK_ACCESS_FS_EXECUTE; 1716 return access; 1717 } 1718 1719 static int hook_file_alloc_security(struct file *const file) 1720 { 1721 /* 1722 * Grants all access rights, even if most of them are not checked later 1723 * on. It is more consistent. 1724 * 1725 * Notably, file descriptors for regular files can also be acquired 1726 * without going through the file_open hook, for example when using 1727 * memfd_create(2). 1728 */ 1729 landlock_file(file)->allowed_access = LANDLOCK_MASK_ACCESS_FS; 1730 return 0; 1731 } 1732 1733 static bool is_device(const struct file *const file) 1734 { 1735 const struct inode *inode = file_inode(file); 1736 1737 return S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode); 1738 } 1739 1740 static int hook_file_open(struct file *const file) 1741 { 1742 struct layer_access_masks layer_masks = {}; 1743 access_mask_t open_access_request, full_access_request, allowed_access, 1744 optional_access; 1745 const struct landlock_cred_security *const subject = 1746 landlock_get_applicable_subject(file->f_cred, any_fs, NULL); 1747 struct landlock_request request = {}; 1748 1749 if (!subject) 1750 return 0; 1751 1752 /* 1753 * Because a file may be opened with O_PATH, get_required_file_open_access() 1754 * may return 0. This case will be handled with a future Landlock 1755 * evolution. 1756 */ 1757 open_access_request = get_required_file_open_access(file); 1758 1759 /* 1760 * We look up more access than what we immediately need for open(), so 1761 * that we can later authorize operations on opened files. 1762 */ 1763 optional_access = LANDLOCK_ACCESS_FS_TRUNCATE; 1764 if (is_device(file)) 1765 optional_access |= LANDLOCK_ACCESS_FS_IOCTL_DEV; 1766 1767 full_access_request = open_access_request | optional_access; 1768 1769 if (is_access_to_paths_allowed( 1770 subject->domain, &file->f_path, 1771 landlock_init_layer_masks(subject->domain, 1772 full_access_request, &layer_masks, 1773 LANDLOCK_KEY_INODE), 1774 &layer_masks, &request, NULL, 0, NULL, NULL, NULL)) { 1775 allowed_access = full_access_request; 1776 } else { 1777 /* 1778 * Calculate the actual allowed access rights from layer_masks. 1779 * Remove the access rights from the full access request which 1780 * are still unfulfilled in any of the layers. 1781 */ 1782 allowed_access = full_access_request; 1783 for (size_t i = 0; i < ARRAY_SIZE(layer_masks.access); i++) 1784 allowed_access &= ~layer_masks.access[i]; 1785 } 1786 1787 /* 1788 * For operations on already opened files (i.e. ftruncate()), it is the 1789 * access rights at the time of open() which decide whether the 1790 * operation is permitted. Therefore, we record the relevant subset of 1791 * file access rights in the opened struct file. 1792 */ 1793 landlock_file(file)->allowed_access = allowed_access; 1794 #ifdef CONFIG_AUDIT 1795 landlock_file(file)->deny_masks = landlock_get_deny_masks( 1796 _LANDLOCK_ACCESS_FS_OPTIONAL, optional_access, &layer_masks); 1797 #endif /* CONFIG_AUDIT */ 1798 1799 if (access_mask_subset(open_access_request, allowed_access)) 1800 return 0; 1801 1802 /* Sets access to reflect the actual request. */ 1803 request.access = open_access_request; 1804 landlock_log_denial(subject, &request); 1805 return -EACCES; 1806 } 1807 1808 static int hook_file_truncate(struct file *const file) 1809 { 1810 /* 1811 * Allows truncation if the truncate right was available at the time of 1812 * opening the file, to get a consistent access check as for read, write 1813 * and execute operations. 1814 * 1815 * Note: For checks done based on the file's Landlock allowed access, we 1816 * enforce them independently of whether the current thread is in a 1817 * Landlock domain, so that open files passed between independent 1818 * processes retain their behaviour. 1819 */ 1820 if (landlock_file(file)->allowed_access & LANDLOCK_ACCESS_FS_TRUNCATE) 1821 return 0; 1822 1823 landlock_log_denial(landlock_cred(file->f_cred), &(struct landlock_request) { 1824 .type = LANDLOCK_REQUEST_FS_ACCESS, 1825 .audit = { 1826 .type = LSM_AUDIT_DATA_FILE, 1827 .u.file = file, 1828 }, 1829 .all_existing_optional_access = _LANDLOCK_ACCESS_FS_OPTIONAL, 1830 .access = LANDLOCK_ACCESS_FS_TRUNCATE, 1831 #ifdef CONFIG_AUDIT 1832 .deny_masks = landlock_file(file)->deny_masks, 1833 #endif /* CONFIG_AUDIT */ 1834 }); 1835 return -EACCES; 1836 } 1837 1838 static int hook_file_ioctl_common(const struct file *const file, 1839 const unsigned int cmd, const bool is_compat) 1840 { 1841 access_mask_t allowed_access = landlock_file(file)->allowed_access; 1842 1843 /* 1844 * It is the access rights at the time of opening the file which 1845 * determine whether IOCTL can be used on the opened file later. 1846 * 1847 * The access right is attached to the opened file in hook_file_open(). 1848 */ 1849 if (allowed_access & LANDLOCK_ACCESS_FS_IOCTL_DEV) 1850 return 0; 1851 1852 if (!is_device(file)) 1853 return 0; 1854 1855 if (unlikely(is_compat) ? is_masked_device_ioctl_compat(cmd) : 1856 is_masked_device_ioctl(cmd)) 1857 return 0; 1858 1859 landlock_log_denial(landlock_cred(file->f_cred), &(struct landlock_request) { 1860 .type = LANDLOCK_REQUEST_FS_ACCESS, 1861 .audit = { 1862 .type = LSM_AUDIT_DATA_IOCTL_OP, 1863 .u.op = &(struct lsm_ioctlop_audit) { 1864 .path = file->f_path, 1865 .cmd = cmd, 1866 }, 1867 }, 1868 .all_existing_optional_access = _LANDLOCK_ACCESS_FS_OPTIONAL, 1869 .access = LANDLOCK_ACCESS_FS_IOCTL_DEV, 1870 #ifdef CONFIG_AUDIT 1871 .deny_masks = landlock_file(file)->deny_masks, 1872 #endif /* CONFIG_AUDIT */ 1873 }); 1874 return -EACCES; 1875 } 1876 1877 static int hook_file_ioctl(struct file *file, unsigned int cmd, 1878 unsigned long arg) 1879 { 1880 return hook_file_ioctl_common(file, cmd, false); 1881 } 1882 1883 static int hook_file_ioctl_compat(struct file *file, unsigned int cmd, 1884 unsigned long arg) 1885 { 1886 return hook_file_ioctl_common(file, cmd, true); 1887 } 1888 1889 /* 1890 * Always allow sending signals between threads of the same process. This 1891 * ensures consistency with hook_task_kill(). 1892 */ 1893 static bool control_current_fowner(struct fown_struct *const fown) 1894 { 1895 struct task_struct *p; 1896 1897 /* 1898 * Lock already held by __f_setown(), see commit 26f204380a3c ("fs: Fix 1899 * file_set_fowner LSM hook inconsistencies"). 1900 */ 1901 lockdep_assert_held(&fown->lock); 1902 1903 /* 1904 * Some callers (e.g. fcntl_dirnotify) may not be in an RCU read-side 1905 * critical section. 1906 */ 1907 guard(rcu)(); 1908 p = pid_task(fown->pid, fown->pid_type); 1909 if (!p) 1910 return true; 1911 1912 return !same_thread_group(p, current); 1913 } 1914 1915 static void hook_file_set_fowner(struct file *file) 1916 { 1917 struct landlock_ruleset *prev_dom; 1918 struct landlock_cred_security fown_subject = {}; 1919 size_t fown_layer = 0; 1920 1921 if (control_current_fowner(file_f_owner(file))) { 1922 static const struct access_masks signal_scope = { 1923 .scope = LANDLOCK_SCOPE_SIGNAL, 1924 }; 1925 const struct landlock_cred_security *new_subject = 1926 landlock_get_applicable_subject( 1927 current_cred(), signal_scope, &fown_layer); 1928 if (new_subject) { 1929 landlock_get_ruleset(new_subject->domain); 1930 fown_subject = *new_subject; 1931 } 1932 } 1933 1934 prev_dom = landlock_file(file)->fown_subject.domain; 1935 landlock_file(file)->fown_subject = fown_subject; 1936 #ifdef CONFIG_AUDIT 1937 landlock_file(file)->fown_layer = fown_layer; 1938 #endif /* CONFIG_AUDIT*/ 1939 1940 /* May be called in an RCU read-side critical section. */ 1941 landlock_put_ruleset_deferred(prev_dom); 1942 } 1943 1944 static void hook_file_free_security(struct file *file) 1945 { 1946 landlock_put_ruleset_deferred(landlock_file(file)->fown_subject.domain); 1947 } 1948 1949 static struct security_hook_list landlock_hooks[] __ro_after_init = { 1950 LSM_HOOK_INIT(inode_free_security_rcu, hook_inode_free_security_rcu), 1951 1952 LSM_HOOK_INIT(sb_delete, hook_sb_delete), 1953 LSM_HOOK_INIT(sb_mount, hook_sb_mount), 1954 LSM_HOOK_INIT(move_mount, hook_move_mount), 1955 LSM_HOOK_INIT(sb_umount, hook_sb_umount), 1956 LSM_HOOK_INIT(sb_remount, hook_sb_remount), 1957 LSM_HOOK_INIT(sb_pivotroot, hook_sb_pivotroot), 1958 1959 LSM_HOOK_INIT(path_link, hook_path_link), 1960 LSM_HOOK_INIT(path_rename, hook_path_rename), 1961 LSM_HOOK_INIT(path_mkdir, hook_path_mkdir), 1962 LSM_HOOK_INIT(path_mknod, hook_path_mknod), 1963 LSM_HOOK_INIT(path_symlink, hook_path_symlink), 1964 LSM_HOOK_INIT(path_unlink, hook_path_unlink), 1965 LSM_HOOK_INIT(path_rmdir, hook_path_rmdir), 1966 LSM_HOOK_INIT(path_truncate, hook_path_truncate), 1967 LSM_HOOK_INIT(unix_find, hook_unix_find), 1968 1969 LSM_HOOK_INIT(file_alloc_security, hook_file_alloc_security), 1970 LSM_HOOK_INIT(file_open, hook_file_open), 1971 LSM_HOOK_INIT(file_truncate, hook_file_truncate), 1972 LSM_HOOK_INIT(file_ioctl, hook_file_ioctl), 1973 LSM_HOOK_INIT(file_ioctl_compat, hook_file_ioctl_compat), 1974 LSM_HOOK_INIT(file_set_fowner, hook_file_set_fowner), 1975 LSM_HOOK_INIT(file_free_security, hook_file_free_security), 1976 }; 1977 1978 __init void landlock_add_fs_hooks(void) 1979 { 1980 security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks), 1981 &landlock_lsmid); 1982 } 1983 1984 #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST 1985 1986 /* clang-format off */ 1987 static struct kunit_case test_cases[] = { 1988 KUNIT_CASE(test_no_more_access), 1989 KUNIT_CASE(test_scope_to_request_with_exec_none), 1990 KUNIT_CASE(test_scope_to_request_with_exec_some), 1991 KUNIT_CASE(test_scope_to_request_without_access), 1992 KUNIT_CASE(test_is_eacces_with_none), 1993 KUNIT_CASE(test_is_eacces_with_refer), 1994 KUNIT_CASE(test_is_eacces_with_write), 1995 {} 1996 }; 1997 /* clang-format on */ 1998 1999 static struct kunit_suite test_suite = { 2000 .name = "landlock_fs", 2001 .test_cases = test_cases, 2002 }; 2003 2004 kunit_test_suite(test_suite); 2005 2006 #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ 2007