1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Landlock LSM - Filesystem management and hooks 4 * 5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> 6 * Copyright © 2018-2020 ANSSI 7 * Copyright © 2021-2022 Microsoft Corporation 8 */ 9 10 #include <linux/atomic.h> 11 #include <linux/bitops.h> 12 #include <linux/bits.h> 13 #include <linux/compiler_types.h> 14 #include <linux/dcache.h> 15 #include <linux/err.h> 16 #include <linux/fs.h> 17 #include <linux/init.h> 18 #include <linux/kernel.h> 19 #include <linux/limits.h> 20 #include <linux/list.h> 21 #include <linux/lsm_hooks.h> 22 #include <linux/mount.h> 23 #include <linux/namei.h> 24 #include <linux/path.h> 25 #include <linux/rcupdate.h> 26 #include <linux/spinlock.h> 27 #include <linux/stat.h> 28 #include <linux/types.h> 29 #include <linux/wait_bit.h> 30 #include <linux/workqueue.h> 31 #include <uapi/linux/landlock.h> 32 33 #include "common.h" 34 #include "cred.h" 35 #include "fs.h" 36 #include "limits.h" 37 #include "object.h" 38 #include "ruleset.h" 39 #include "setup.h" 40 41 /* Underlying object management */ 42 43 static void release_inode(struct landlock_object *const object) 44 __releases(object->lock) 45 { 46 struct inode *const inode = object->underobj; 47 struct super_block *sb; 48 49 if (!inode) { 50 spin_unlock(&object->lock); 51 return; 52 } 53 54 /* 55 * Protects against concurrent use by hook_sb_delete() of the reference 56 * to the underlying inode. 57 */ 58 object->underobj = NULL; 59 /* 60 * Makes sure that if the filesystem is concurrently unmounted, 61 * hook_sb_delete() will wait for us to finish iput(). 62 */ 63 sb = inode->i_sb; 64 atomic_long_inc(&landlock_superblock(sb)->inode_refs); 65 spin_unlock(&object->lock); 66 /* 67 * Because object->underobj was not NULL, hook_sb_delete() and 68 * get_inode_object() guarantee that it is safe to reset 69 * landlock_inode(inode)->object while it is not NULL. It is therefore 70 * not necessary to lock inode->i_lock. 71 */ 72 rcu_assign_pointer(landlock_inode(inode)->object, NULL); 73 /* 74 * Now, new rules can safely be tied to @inode with get_inode_object(). 75 */ 76 77 iput(inode); 78 if (atomic_long_dec_and_test(&landlock_superblock(sb)->inode_refs)) 79 wake_up_var(&landlock_superblock(sb)->inode_refs); 80 } 81 82 static const struct landlock_object_underops landlock_fs_underops = { 83 .release = release_inode 84 }; 85 86 /* Ruleset management */ 87 88 static struct landlock_object *get_inode_object(struct inode *const inode) 89 { 90 struct landlock_object *object, *new_object; 91 struct landlock_inode_security *inode_sec = landlock_inode(inode); 92 93 rcu_read_lock(); 94 retry: 95 object = rcu_dereference(inode_sec->object); 96 if (object) { 97 if (likely(refcount_inc_not_zero(&object->usage))) { 98 rcu_read_unlock(); 99 return object; 100 } 101 /* 102 * We are racing with release_inode(), the object is going 103 * away. Wait for release_inode(), then retry. 104 */ 105 spin_lock(&object->lock); 106 spin_unlock(&object->lock); 107 goto retry; 108 } 109 rcu_read_unlock(); 110 111 /* 112 * If there is no object tied to @inode, then create a new one (without 113 * holding any locks). 114 */ 115 new_object = landlock_create_object(&landlock_fs_underops, inode); 116 if (IS_ERR(new_object)) 117 return new_object; 118 119 /* 120 * Protects against concurrent calls to get_inode_object() or 121 * hook_sb_delete(). 122 */ 123 spin_lock(&inode->i_lock); 124 if (unlikely(rcu_access_pointer(inode_sec->object))) { 125 /* Someone else just created the object, bail out and retry. */ 126 spin_unlock(&inode->i_lock); 127 kfree(new_object); 128 129 rcu_read_lock(); 130 goto retry; 131 } 132 133 /* 134 * @inode will be released by hook_sb_delete() on its superblock 135 * shutdown, or by release_inode() when no more ruleset references the 136 * related object. 137 */ 138 ihold(inode); 139 rcu_assign_pointer(inode_sec->object, new_object); 140 spin_unlock(&inode->i_lock); 141 return new_object; 142 } 143 144 /* All access rights that can be tied to files. */ 145 /* clang-format off */ 146 #define ACCESS_FILE ( \ 147 LANDLOCK_ACCESS_FS_EXECUTE | \ 148 LANDLOCK_ACCESS_FS_WRITE_FILE | \ 149 LANDLOCK_ACCESS_FS_READ_FILE | \ 150 LANDLOCK_ACCESS_FS_TRUNCATE) 151 /* clang-format on */ 152 153 /* 154 * @path: Should have been checked by get_path_from_fd(). 155 */ 156 int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, 157 const struct path *const path, 158 access_mask_t access_rights) 159 { 160 int err; 161 struct landlock_id id = { 162 .type = LANDLOCK_KEY_INODE, 163 }; 164 165 /* Files only get access rights that make sense. */ 166 if (!d_is_dir(path->dentry) && 167 (access_rights | ACCESS_FILE) != ACCESS_FILE) 168 return -EINVAL; 169 if (WARN_ON_ONCE(ruleset->num_layers != 1)) 170 return -EINVAL; 171 172 /* Transforms relative access rights to absolute ones. */ 173 access_rights |= LANDLOCK_MASK_ACCESS_FS & 174 ~landlock_get_fs_access_mask(ruleset, 0); 175 id.key.object = get_inode_object(d_backing_inode(path->dentry)); 176 if (IS_ERR(id.key.object)) 177 return PTR_ERR(id.key.object); 178 mutex_lock(&ruleset->lock); 179 err = landlock_insert_rule(ruleset, id, access_rights); 180 mutex_unlock(&ruleset->lock); 181 /* 182 * No need to check for an error because landlock_insert_rule() 183 * increments the refcount for the new object if needed. 184 */ 185 landlock_put_object(id.key.object); 186 return err; 187 } 188 189 /* Access-control management */ 190 191 /* 192 * The lifetime of the returned rule is tied to @domain. 193 * 194 * Returns NULL if no rule is found or if @dentry is negative. 195 */ 196 static inline const struct landlock_rule * 197 find_rule(const struct landlock_ruleset *const domain, 198 const struct dentry *const dentry) 199 { 200 const struct landlock_rule *rule; 201 const struct inode *inode; 202 struct landlock_id id = { 203 .type = LANDLOCK_KEY_INODE, 204 }; 205 206 /* Ignores nonexistent leafs. */ 207 if (d_is_negative(dentry)) 208 return NULL; 209 210 inode = d_backing_inode(dentry); 211 rcu_read_lock(); 212 id.key.object = rcu_dereference(landlock_inode(inode)->object); 213 rule = landlock_find_rule(domain, id); 214 rcu_read_unlock(); 215 return rule; 216 } 217 218 /* 219 * Allows access to pseudo filesystems that will never be mountable (e.g. 220 * sockfs, pipefs), but can still be reachable through 221 * /proc/<pid>/fd/<file-descriptor> 222 */ 223 static inline bool is_nouser_or_private(const struct dentry *dentry) 224 { 225 return (dentry->d_sb->s_flags & SB_NOUSER) || 226 (d_is_positive(dentry) && 227 unlikely(IS_PRIVATE(d_backing_inode(dentry)))); 228 } 229 230 static access_mask_t 231 get_raw_handled_fs_accesses(const struct landlock_ruleset *const domain) 232 { 233 access_mask_t access_dom = 0; 234 size_t layer_level; 235 236 for (layer_level = 0; layer_level < domain->num_layers; layer_level++) 237 access_dom |= 238 landlock_get_raw_fs_access_mask(domain, layer_level); 239 return access_dom; 240 } 241 242 static access_mask_t 243 get_handled_fs_accesses(const struct landlock_ruleset *const domain) 244 { 245 /* Handles all initially denied by default access rights. */ 246 return get_raw_handled_fs_accesses(domain) | 247 LANDLOCK_ACCESS_FS_INITIALLY_DENIED; 248 } 249 250 static const struct landlock_ruleset *get_current_fs_domain(void) 251 { 252 const struct landlock_ruleset *const dom = 253 landlock_get_current_domain(); 254 255 if (!dom || !get_raw_handled_fs_accesses(dom)) 256 return NULL; 257 258 return dom; 259 } 260 261 /* 262 * Check that a destination file hierarchy has more restrictions than a source 263 * file hierarchy. This is only used for link and rename actions. 264 * 265 * @layer_masks_child2: Optional child masks. 266 */ 267 static inline bool no_more_access( 268 const layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS], 269 const layer_mask_t (*const layer_masks_child1)[LANDLOCK_NUM_ACCESS_FS], 270 const bool child1_is_directory, 271 const layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS], 272 const layer_mask_t (*const layer_masks_child2)[LANDLOCK_NUM_ACCESS_FS], 273 const bool child2_is_directory) 274 { 275 unsigned long access_bit; 276 277 for (access_bit = 0; access_bit < ARRAY_SIZE(*layer_masks_parent2); 278 access_bit++) { 279 /* Ignores accesses that only make sense for directories. */ 280 const bool is_file_access = 281 !!(BIT_ULL(access_bit) & ACCESS_FILE); 282 283 if (child1_is_directory || is_file_access) { 284 /* 285 * Checks if the destination restrictions are a 286 * superset of the source ones (i.e. inherited access 287 * rights without child exceptions): 288 * restrictions(parent2) >= restrictions(child1) 289 */ 290 if ((((*layer_masks_parent1)[access_bit] & 291 (*layer_masks_child1)[access_bit]) | 292 (*layer_masks_parent2)[access_bit]) != 293 (*layer_masks_parent2)[access_bit]) 294 return false; 295 } 296 297 if (!layer_masks_child2) 298 continue; 299 if (child2_is_directory || is_file_access) { 300 /* 301 * Checks inverted restrictions for RENAME_EXCHANGE: 302 * restrictions(parent1) >= restrictions(child2) 303 */ 304 if ((((*layer_masks_parent2)[access_bit] & 305 (*layer_masks_child2)[access_bit]) | 306 (*layer_masks_parent1)[access_bit]) != 307 (*layer_masks_parent1)[access_bit]) 308 return false; 309 } 310 } 311 return true; 312 } 313 314 /* 315 * Removes @layer_masks accesses that are not requested. 316 * 317 * Returns true if the request is allowed, false otherwise. 318 */ 319 static inline bool 320 scope_to_request(const access_mask_t access_request, 321 layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS]) 322 { 323 const unsigned long access_req = access_request; 324 unsigned long access_bit; 325 326 if (WARN_ON_ONCE(!layer_masks)) 327 return true; 328 329 for_each_clear_bit(access_bit, &access_req, ARRAY_SIZE(*layer_masks)) 330 (*layer_masks)[access_bit] = 0; 331 return !memchr_inv(layer_masks, 0, sizeof(*layer_masks)); 332 } 333 334 /* 335 * Returns true if there is at least one access right different than 336 * LANDLOCK_ACCESS_FS_REFER. 337 */ 338 static inline bool 339 is_eacces(const layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS], 340 const access_mask_t access_request) 341 { 342 unsigned long access_bit; 343 /* LANDLOCK_ACCESS_FS_REFER alone must return -EXDEV. */ 344 const unsigned long access_check = access_request & 345 ~LANDLOCK_ACCESS_FS_REFER; 346 347 if (!layer_masks) 348 return false; 349 350 for_each_set_bit(access_bit, &access_check, ARRAY_SIZE(*layer_masks)) { 351 if ((*layer_masks)[access_bit]) 352 return true; 353 } 354 return false; 355 } 356 357 /** 358 * is_access_to_paths_allowed - Check accesses for requests with a common path 359 * 360 * @domain: Domain to check against. 361 * @path: File hierarchy to walk through. 362 * @access_request_parent1: Accesses to check, once @layer_masks_parent1 is 363 * equal to @layer_masks_parent2 (if any). This is tied to the unique 364 * requested path for most actions, or the source in case of a refer action 365 * (i.e. rename or link), or the source and destination in case of 366 * RENAME_EXCHANGE. 367 * @layer_masks_parent1: Pointer to a matrix of layer masks per access 368 * masks, identifying the layers that forbid a specific access. Bits from 369 * this matrix can be unset according to the @path walk. An empty matrix 370 * means that @domain allows all possible Landlock accesses (i.e. not only 371 * those identified by @access_request_parent1). This matrix can 372 * initially refer to domain layer masks and, when the accesses for the 373 * destination and source are the same, to requested layer masks. 374 * @dentry_child1: Dentry to the initial child of the parent1 path. This 375 * pointer must be NULL for non-refer actions (i.e. not link nor rename). 376 * @access_request_parent2: Similar to @access_request_parent1 but for a 377 * request involving a source and a destination. This refers to the 378 * destination, except in case of RENAME_EXCHANGE where it also refers to 379 * the source. Must be set to 0 when using a simple path request. 380 * @layer_masks_parent2: Similar to @layer_masks_parent1 but for a refer 381 * action. This must be NULL otherwise. 382 * @dentry_child2: Dentry to the initial child of the parent2 path. This 383 * pointer is only set for RENAME_EXCHANGE actions and must be NULL 384 * otherwise. 385 * 386 * This helper first checks that the destination has a superset of restrictions 387 * compared to the source (if any) for a common path. Because of 388 * RENAME_EXCHANGE actions, source and destinations may be swapped. It then 389 * checks that the collected accesses and the remaining ones are enough to 390 * allow the request. 391 * 392 * Returns: 393 * - true if the access request is granted; 394 * - false otherwise. 395 */ 396 static bool is_access_to_paths_allowed( 397 const struct landlock_ruleset *const domain, 398 const struct path *const path, 399 const access_mask_t access_request_parent1, 400 layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS], 401 const struct dentry *const dentry_child1, 402 const access_mask_t access_request_parent2, 403 layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS], 404 const struct dentry *const dentry_child2) 405 { 406 bool allowed_parent1 = false, allowed_parent2 = false, is_dom_check, 407 child1_is_directory = true, child2_is_directory = true; 408 struct path walker_path; 409 access_mask_t access_masked_parent1, access_masked_parent2; 410 layer_mask_t _layer_masks_child1[LANDLOCK_NUM_ACCESS_FS], 411 _layer_masks_child2[LANDLOCK_NUM_ACCESS_FS]; 412 layer_mask_t(*layer_masks_child1)[LANDLOCK_NUM_ACCESS_FS] = NULL, 413 (*layer_masks_child2)[LANDLOCK_NUM_ACCESS_FS] = NULL; 414 415 if (!access_request_parent1 && !access_request_parent2) 416 return true; 417 if (WARN_ON_ONCE(!domain || !path)) 418 return true; 419 if (is_nouser_or_private(path->dentry)) 420 return true; 421 if (WARN_ON_ONCE(domain->num_layers < 1 || !layer_masks_parent1)) 422 return false; 423 424 if (unlikely(layer_masks_parent2)) { 425 if (WARN_ON_ONCE(!dentry_child1)) 426 return false; 427 /* 428 * For a double request, first check for potential privilege 429 * escalation by looking at domain handled accesses (which are 430 * a superset of the meaningful requested accesses). 431 */ 432 access_masked_parent1 = access_masked_parent2 = 433 get_handled_fs_accesses(domain); 434 is_dom_check = true; 435 } else { 436 if (WARN_ON_ONCE(dentry_child1 || dentry_child2)) 437 return false; 438 /* For a simple request, only check for requested accesses. */ 439 access_masked_parent1 = access_request_parent1; 440 access_masked_parent2 = access_request_parent2; 441 is_dom_check = false; 442 } 443 444 if (unlikely(dentry_child1)) { 445 landlock_unmask_layers(find_rule(domain, dentry_child1), 446 landlock_init_layer_masks( 447 domain, LANDLOCK_MASK_ACCESS_FS, 448 &_layer_masks_child1), 449 &_layer_masks_child1); 450 layer_masks_child1 = &_layer_masks_child1; 451 child1_is_directory = d_is_dir(dentry_child1); 452 } 453 if (unlikely(dentry_child2)) { 454 landlock_unmask_layers(find_rule(domain, dentry_child2), 455 landlock_init_layer_masks( 456 domain, LANDLOCK_MASK_ACCESS_FS, 457 &_layer_masks_child2), 458 &_layer_masks_child2); 459 layer_masks_child2 = &_layer_masks_child2; 460 child2_is_directory = d_is_dir(dentry_child2); 461 } 462 463 walker_path = *path; 464 path_get(&walker_path); 465 /* 466 * We need to walk through all the hierarchy to not miss any relevant 467 * restriction. 468 */ 469 while (true) { 470 struct dentry *parent_dentry; 471 const struct landlock_rule *rule; 472 473 /* 474 * If at least all accesses allowed on the destination are 475 * already allowed on the source, respectively if there is at 476 * least as much as restrictions on the destination than on the 477 * source, then we can safely refer files from the source to 478 * the destination without risking a privilege escalation. 479 * This also applies in the case of RENAME_EXCHANGE, which 480 * implies checks on both direction. This is crucial for 481 * standalone multilayered security policies. Furthermore, 482 * this helps avoid policy writers to shoot themselves in the 483 * foot. 484 */ 485 if (unlikely(is_dom_check && 486 no_more_access( 487 layer_masks_parent1, layer_masks_child1, 488 child1_is_directory, layer_masks_parent2, 489 layer_masks_child2, 490 child2_is_directory))) { 491 allowed_parent1 = scope_to_request( 492 access_request_parent1, layer_masks_parent1); 493 allowed_parent2 = scope_to_request( 494 access_request_parent2, layer_masks_parent2); 495 496 /* Stops when all accesses are granted. */ 497 if (allowed_parent1 && allowed_parent2) 498 break; 499 500 /* 501 * Now, downgrades the remaining checks from domain 502 * handled accesses to requested accesses. 503 */ 504 is_dom_check = false; 505 access_masked_parent1 = access_request_parent1; 506 access_masked_parent2 = access_request_parent2; 507 } 508 509 rule = find_rule(domain, walker_path.dentry); 510 allowed_parent1 = landlock_unmask_layers( 511 rule, access_masked_parent1, layer_masks_parent1); 512 allowed_parent2 = landlock_unmask_layers( 513 rule, access_masked_parent2, layer_masks_parent2); 514 515 /* Stops when a rule from each layer grants access. */ 516 if (allowed_parent1 && allowed_parent2) 517 break; 518 519 jump_up: 520 if (walker_path.dentry == walker_path.mnt->mnt_root) { 521 if (follow_up(&walker_path)) { 522 /* Ignores hidden mount points. */ 523 goto jump_up; 524 } else { 525 /* 526 * Stops at the real root. Denies access 527 * because not all layers have granted access. 528 */ 529 break; 530 } 531 } 532 if (unlikely(IS_ROOT(walker_path.dentry))) { 533 /* 534 * Stops at disconnected root directories. Only allows 535 * access to internal filesystems (e.g. nsfs, which is 536 * reachable through /proc/<pid>/ns/<namespace>). 537 */ 538 allowed_parent1 = allowed_parent2 = 539 !!(walker_path.mnt->mnt_flags & MNT_INTERNAL); 540 break; 541 } 542 parent_dentry = dget_parent(walker_path.dentry); 543 dput(walker_path.dentry); 544 walker_path.dentry = parent_dentry; 545 } 546 path_put(&walker_path); 547 548 return allowed_parent1 && allowed_parent2; 549 } 550 551 static inline int check_access_path(const struct landlock_ruleset *const domain, 552 const struct path *const path, 553 access_mask_t access_request) 554 { 555 layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {}; 556 557 access_request = 558 landlock_init_layer_masks(domain, access_request, &layer_masks); 559 if (is_access_to_paths_allowed(domain, path, access_request, 560 &layer_masks, NULL, 0, NULL, NULL)) 561 return 0; 562 return -EACCES; 563 } 564 565 static inline int current_check_access_path(const struct path *const path, 566 const access_mask_t access_request) 567 { 568 const struct landlock_ruleset *const dom = get_current_fs_domain(); 569 570 if (!dom) 571 return 0; 572 return check_access_path(dom, path, access_request); 573 } 574 575 static inline access_mask_t get_mode_access(const umode_t mode) 576 { 577 switch (mode & S_IFMT) { 578 case S_IFLNK: 579 return LANDLOCK_ACCESS_FS_MAKE_SYM; 580 case 0: 581 /* A zero mode translates to S_IFREG. */ 582 case S_IFREG: 583 return LANDLOCK_ACCESS_FS_MAKE_REG; 584 case S_IFDIR: 585 return LANDLOCK_ACCESS_FS_MAKE_DIR; 586 case S_IFCHR: 587 return LANDLOCK_ACCESS_FS_MAKE_CHAR; 588 case S_IFBLK: 589 return LANDLOCK_ACCESS_FS_MAKE_BLOCK; 590 case S_IFIFO: 591 return LANDLOCK_ACCESS_FS_MAKE_FIFO; 592 case S_IFSOCK: 593 return LANDLOCK_ACCESS_FS_MAKE_SOCK; 594 default: 595 WARN_ON_ONCE(1); 596 return 0; 597 } 598 } 599 600 static inline access_mask_t maybe_remove(const struct dentry *const dentry) 601 { 602 if (d_is_negative(dentry)) 603 return 0; 604 return d_is_dir(dentry) ? LANDLOCK_ACCESS_FS_REMOVE_DIR : 605 LANDLOCK_ACCESS_FS_REMOVE_FILE; 606 } 607 608 /** 609 * collect_domain_accesses - Walk through a file path and collect accesses 610 * 611 * @domain: Domain to check against. 612 * @mnt_root: Last directory to check. 613 * @dir: Directory to start the walk from. 614 * @layer_masks_dom: Where to store the collected accesses. 615 * 616 * This helper is useful to begin a path walk from the @dir directory to a 617 * @mnt_root directory used as a mount point. This mount point is the common 618 * ancestor between the source and the destination of a renamed and linked 619 * file. While walking from @dir to @mnt_root, we record all the domain's 620 * allowed accesses in @layer_masks_dom. 621 * 622 * This is similar to is_access_to_paths_allowed() but much simpler because it 623 * only handles walking on the same mount point and only checks one set of 624 * accesses. 625 * 626 * Returns: 627 * - true if all the domain access rights are allowed for @dir; 628 * - false if the walk reached @mnt_root. 629 */ 630 static bool collect_domain_accesses( 631 const struct landlock_ruleset *const domain, 632 const struct dentry *const mnt_root, struct dentry *dir, 633 layer_mask_t (*const layer_masks_dom)[LANDLOCK_NUM_ACCESS_FS]) 634 { 635 unsigned long access_dom; 636 bool ret = false; 637 638 if (WARN_ON_ONCE(!domain || !mnt_root || !dir || !layer_masks_dom)) 639 return true; 640 if (is_nouser_or_private(dir)) 641 return true; 642 643 access_dom = landlock_init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS, 644 layer_masks_dom); 645 646 dget(dir); 647 while (true) { 648 struct dentry *parent_dentry; 649 650 /* Gets all layers allowing all domain accesses. */ 651 if (landlock_unmask_layers(find_rule(domain, dir), access_dom, 652 layer_masks_dom)) { 653 /* 654 * Stops when all handled accesses are allowed by at 655 * least one rule in each layer. 656 */ 657 ret = true; 658 break; 659 } 660 661 /* We should not reach a root other than @mnt_root. */ 662 if (dir == mnt_root || WARN_ON_ONCE(IS_ROOT(dir))) 663 break; 664 665 parent_dentry = dget_parent(dir); 666 dput(dir); 667 dir = parent_dentry; 668 } 669 dput(dir); 670 return ret; 671 } 672 673 /** 674 * current_check_refer_path - Check if a rename or link action is allowed 675 * 676 * @old_dentry: File or directory requested to be moved or linked. 677 * @new_dir: Destination parent directory. 678 * @new_dentry: Destination file or directory. 679 * @removable: Sets to true if it is a rename operation. 680 * @exchange: Sets to true if it is a rename operation with RENAME_EXCHANGE. 681 * 682 * Because of its unprivileged constraints, Landlock relies on file hierarchies 683 * (and not only inodes) to tie access rights to files. Being able to link or 684 * rename a file hierarchy brings some challenges. Indeed, moving or linking a 685 * file (i.e. creating a new reference to an inode) can have an impact on the 686 * actions allowed for a set of files if it would change its parent directory 687 * (i.e. reparenting). 688 * 689 * To avoid trivial access right bypasses, Landlock first checks if the file or 690 * directory requested to be moved would gain new access rights inherited from 691 * its new hierarchy. Before returning any error, Landlock then checks that 692 * the parent source hierarchy and the destination hierarchy would allow the 693 * link or rename action. If it is not the case, an error with EACCES is 694 * returned to inform user space that there is no way to remove or create the 695 * requested source file type. If it should be allowed but the new inherited 696 * access rights would be greater than the source access rights, then the 697 * kernel returns an error with EXDEV. Prioritizing EACCES over EXDEV enables 698 * user space to abort the whole operation if there is no way to do it, or to 699 * manually copy the source to the destination if this remains allowed, e.g. 700 * because file creation is allowed on the destination directory but not direct 701 * linking. 702 * 703 * To achieve this goal, the kernel needs to compare two file hierarchies: the 704 * one identifying the source file or directory (including itself), and the 705 * destination one. This can be seen as a multilayer partial ordering problem. 706 * The kernel walks through these paths and collects in a matrix the access 707 * rights that are denied per layer. These matrices are then compared to see 708 * if the destination one has more (or the same) restrictions as the source 709 * one. If this is the case, the requested action will not return EXDEV, which 710 * doesn't mean the action is allowed. The parent hierarchy of the source 711 * (i.e. parent directory), and the destination hierarchy must also be checked 712 * to verify that they explicitly allow such action (i.e. referencing, 713 * creation and potentially removal rights). The kernel implementation is then 714 * required to rely on potentially four matrices of access rights: one for the 715 * source file or directory (i.e. the child), a potentially other one for the 716 * other source/destination (in case of RENAME_EXCHANGE), one for the source 717 * parent hierarchy and a last one for the destination hierarchy. These 718 * ephemeral matrices take some space on the stack, which limits the number of 719 * layers to a deemed reasonable number: 16. 720 * 721 * Returns: 722 * - 0 if access is allowed; 723 * - -EXDEV if @old_dentry would inherit new access rights from @new_dir; 724 * - -EACCES if file removal or creation is denied. 725 */ 726 static int current_check_refer_path(struct dentry *const old_dentry, 727 const struct path *const new_dir, 728 struct dentry *const new_dentry, 729 const bool removable, const bool exchange) 730 { 731 const struct landlock_ruleset *const dom = get_current_fs_domain(); 732 bool allow_parent1, allow_parent2; 733 access_mask_t access_request_parent1, access_request_parent2; 734 struct path mnt_dir; 735 layer_mask_t layer_masks_parent1[LANDLOCK_NUM_ACCESS_FS], 736 layer_masks_parent2[LANDLOCK_NUM_ACCESS_FS]; 737 738 if (!dom) 739 return 0; 740 if (WARN_ON_ONCE(dom->num_layers < 1)) 741 return -EACCES; 742 if (unlikely(d_is_negative(old_dentry))) 743 return -ENOENT; 744 if (exchange) { 745 if (unlikely(d_is_negative(new_dentry))) 746 return -ENOENT; 747 access_request_parent1 = 748 get_mode_access(d_backing_inode(new_dentry)->i_mode); 749 } else { 750 access_request_parent1 = 0; 751 } 752 access_request_parent2 = 753 get_mode_access(d_backing_inode(old_dentry)->i_mode); 754 if (removable) { 755 access_request_parent1 |= maybe_remove(old_dentry); 756 access_request_parent2 |= maybe_remove(new_dentry); 757 } 758 759 /* The mount points are the same for old and new paths, cf. EXDEV. */ 760 if (old_dentry->d_parent == new_dir->dentry) { 761 /* 762 * The LANDLOCK_ACCESS_FS_REFER access right is not required 763 * for same-directory referer (i.e. no reparenting). 764 */ 765 access_request_parent1 = landlock_init_layer_masks( 766 dom, access_request_parent1 | access_request_parent2, 767 &layer_masks_parent1); 768 if (is_access_to_paths_allowed( 769 dom, new_dir, access_request_parent1, 770 &layer_masks_parent1, NULL, 0, NULL, NULL)) 771 return 0; 772 return -EACCES; 773 } 774 775 access_request_parent1 |= LANDLOCK_ACCESS_FS_REFER; 776 access_request_parent2 |= LANDLOCK_ACCESS_FS_REFER; 777 778 /* Saves the common mount point. */ 779 mnt_dir.mnt = new_dir->mnt; 780 mnt_dir.dentry = new_dir->mnt->mnt_root; 781 782 /* new_dir->dentry is equal to new_dentry->d_parent */ 783 allow_parent1 = collect_domain_accesses(dom, mnt_dir.dentry, 784 old_dentry->d_parent, 785 &layer_masks_parent1); 786 allow_parent2 = collect_domain_accesses( 787 dom, mnt_dir.dentry, new_dir->dentry, &layer_masks_parent2); 788 789 if (allow_parent1 && allow_parent2) 790 return 0; 791 792 /* 793 * To be able to compare source and destination domain access rights, 794 * take into account the @old_dentry access rights aggregated with its 795 * parent access rights. This will be useful to compare with the 796 * destination parent access rights. 797 */ 798 if (is_access_to_paths_allowed( 799 dom, &mnt_dir, access_request_parent1, &layer_masks_parent1, 800 old_dentry, access_request_parent2, &layer_masks_parent2, 801 exchange ? new_dentry : NULL)) 802 return 0; 803 804 /* 805 * This prioritizes EACCES over EXDEV for all actions, including 806 * renames with RENAME_EXCHANGE. 807 */ 808 if (likely(is_eacces(&layer_masks_parent1, access_request_parent1) || 809 is_eacces(&layer_masks_parent2, access_request_parent2))) 810 return -EACCES; 811 812 /* 813 * Gracefully forbids reparenting if the destination directory 814 * hierarchy is not a superset of restrictions of the source directory 815 * hierarchy, or if LANDLOCK_ACCESS_FS_REFER is not allowed by the 816 * source or the destination. 817 */ 818 return -EXDEV; 819 } 820 821 /* Inode hooks */ 822 823 static void hook_inode_free_security(struct inode *const inode) 824 { 825 /* 826 * All inodes must already have been untied from their object by 827 * release_inode() or hook_sb_delete(). 828 */ 829 WARN_ON_ONCE(landlock_inode(inode)->object); 830 } 831 832 /* Super-block hooks */ 833 834 /* 835 * Release the inodes used in a security policy. 836 * 837 * Cf. fsnotify_unmount_inodes() and invalidate_inodes() 838 */ 839 static void hook_sb_delete(struct super_block *const sb) 840 { 841 struct inode *inode, *prev_inode = NULL; 842 843 if (!landlock_initialized) 844 return; 845 846 spin_lock(&sb->s_inode_list_lock); 847 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 848 struct landlock_object *object; 849 850 /* Only handles referenced inodes. */ 851 if (!atomic_read(&inode->i_count)) 852 continue; 853 854 /* 855 * Protects against concurrent modification of inode (e.g. 856 * from get_inode_object()). 857 */ 858 spin_lock(&inode->i_lock); 859 /* 860 * Checks I_FREEING and I_WILL_FREE to protect against a race 861 * condition when release_inode() just called iput(), which 862 * could lead to a NULL dereference of inode->security or a 863 * second call to iput() for the same Landlock object. Also 864 * checks I_NEW because such inode cannot be tied to an object. 865 */ 866 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) { 867 spin_unlock(&inode->i_lock); 868 continue; 869 } 870 871 rcu_read_lock(); 872 object = rcu_dereference(landlock_inode(inode)->object); 873 if (!object) { 874 rcu_read_unlock(); 875 spin_unlock(&inode->i_lock); 876 continue; 877 } 878 /* Keeps a reference to this inode until the next loop walk. */ 879 __iget(inode); 880 spin_unlock(&inode->i_lock); 881 882 /* 883 * If there is no concurrent release_inode() ongoing, then we 884 * are in charge of calling iput() on this inode, otherwise we 885 * will just wait for it to finish. 886 */ 887 spin_lock(&object->lock); 888 if (object->underobj == inode) { 889 object->underobj = NULL; 890 spin_unlock(&object->lock); 891 rcu_read_unlock(); 892 893 /* 894 * Because object->underobj was not NULL, 895 * release_inode() and get_inode_object() guarantee 896 * that it is safe to reset 897 * landlock_inode(inode)->object while it is not NULL. 898 * It is therefore not necessary to lock inode->i_lock. 899 */ 900 rcu_assign_pointer(landlock_inode(inode)->object, NULL); 901 /* 902 * At this point, we own the ihold() reference that was 903 * originally set up by get_inode_object() and the 904 * __iget() reference that we just set in this loop 905 * walk. Therefore the following call to iput() will 906 * not sleep nor drop the inode because there is now at 907 * least two references to it. 908 */ 909 iput(inode); 910 } else { 911 spin_unlock(&object->lock); 912 rcu_read_unlock(); 913 } 914 915 if (prev_inode) { 916 /* 917 * At this point, we still own the __iget() reference 918 * that we just set in this loop walk. Therefore we 919 * can drop the list lock and know that the inode won't 920 * disappear from under us until the next loop walk. 921 */ 922 spin_unlock(&sb->s_inode_list_lock); 923 /* 924 * We can now actually put the inode reference from the 925 * previous loop walk, which is not needed anymore. 926 */ 927 iput(prev_inode); 928 cond_resched(); 929 spin_lock(&sb->s_inode_list_lock); 930 } 931 prev_inode = inode; 932 } 933 spin_unlock(&sb->s_inode_list_lock); 934 935 /* Puts the inode reference from the last loop walk, if any. */ 936 if (prev_inode) 937 iput(prev_inode); 938 /* Waits for pending iput() in release_inode(). */ 939 wait_var_event(&landlock_superblock(sb)->inode_refs, 940 !atomic_long_read(&landlock_superblock(sb)->inode_refs)); 941 } 942 943 /* 944 * Because a Landlock security policy is defined according to the filesystem 945 * topology (i.e. the mount namespace), changing it may grant access to files 946 * not previously allowed. 947 * 948 * To make it simple, deny any filesystem topology modification by landlocked 949 * processes. Non-landlocked processes may still change the namespace of a 950 * landlocked process, but this kind of threat must be handled by a system-wide 951 * access-control security policy. 952 * 953 * This could be lifted in the future if Landlock can safely handle mount 954 * namespace updates requested by a landlocked process. Indeed, we could 955 * update the current domain (which is currently read-only) by taking into 956 * account the accesses of the source and the destination of a new mount point. 957 * However, it would also require to make all the child domains dynamically 958 * inherit these new constraints. Anyway, for backward compatibility reasons, 959 * a dedicated user space option would be required (e.g. as a ruleset flag). 960 */ 961 static int hook_sb_mount(const char *const dev_name, 962 const struct path *const path, const char *const type, 963 const unsigned long flags, void *const data) 964 { 965 if (!get_current_fs_domain()) 966 return 0; 967 return -EPERM; 968 } 969 970 static int hook_move_mount(const struct path *const from_path, 971 const struct path *const to_path) 972 { 973 if (!get_current_fs_domain()) 974 return 0; 975 return -EPERM; 976 } 977 978 /* 979 * Removing a mount point may reveal a previously hidden file hierarchy, which 980 * may then grant access to files, which may have previously been forbidden. 981 */ 982 static int hook_sb_umount(struct vfsmount *const mnt, const int flags) 983 { 984 if (!get_current_fs_domain()) 985 return 0; 986 return -EPERM; 987 } 988 989 static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts) 990 { 991 if (!get_current_fs_domain()) 992 return 0; 993 return -EPERM; 994 } 995 996 /* 997 * pivot_root(2), like mount(2), changes the current mount namespace. It must 998 * then be forbidden for a landlocked process. 999 * 1000 * However, chroot(2) may be allowed because it only changes the relative root 1001 * directory of the current process. Moreover, it can be used to restrict the 1002 * view of the filesystem. 1003 */ 1004 static int hook_sb_pivotroot(const struct path *const old_path, 1005 const struct path *const new_path) 1006 { 1007 if (!get_current_fs_domain()) 1008 return 0; 1009 return -EPERM; 1010 } 1011 1012 /* Path hooks */ 1013 1014 static int hook_path_link(struct dentry *const old_dentry, 1015 const struct path *const new_dir, 1016 struct dentry *const new_dentry) 1017 { 1018 return current_check_refer_path(old_dentry, new_dir, new_dentry, false, 1019 false); 1020 } 1021 1022 static int hook_path_rename(const struct path *const old_dir, 1023 struct dentry *const old_dentry, 1024 const struct path *const new_dir, 1025 struct dentry *const new_dentry, 1026 const unsigned int flags) 1027 { 1028 /* old_dir refers to old_dentry->d_parent and new_dir->mnt */ 1029 return current_check_refer_path(old_dentry, new_dir, new_dentry, true, 1030 !!(flags & RENAME_EXCHANGE)); 1031 } 1032 1033 static int hook_path_mkdir(const struct path *const dir, 1034 struct dentry *const dentry, const umode_t mode) 1035 { 1036 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_DIR); 1037 } 1038 1039 static int hook_path_mknod(const struct path *const dir, 1040 struct dentry *const dentry, const umode_t mode, 1041 const unsigned int dev) 1042 { 1043 const struct landlock_ruleset *const dom = get_current_fs_domain(); 1044 1045 if (!dom) 1046 return 0; 1047 return check_access_path(dom, dir, get_mode_access(mode)); 1048 } 1049 1050 static int hook_path_symlink(const struct path *const dir, 1051 struct dentry *const dentry, 1052 const char *const old_name) 1053 { 1054 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_SYM); 1055 } 1056 1057 static int hook_path_unlink(const struct path *const dir, 1058 struct dentry *const dentry) 1059 { 1060 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_FILE); 1061 } 1062 1063 static int hook_path_rmdir(const struct path *const dir, 1064 struct dentry *const dentry) 1065 { 1066 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_DIR); 1067 } 1068 1069 static int hook_path_truncate(const struct path *const path) 1070 { 1071 return current_check_access_path(path, LANDLOCK_ACCESS_FS_TRUNCATE); 1072 } 1073 1074 /* File hooks */ 1075 1076 /** 1077 * get_required_file_open_access - Get access needed to open a file 1078 * 1079 * @file: File being opened. 1080 * 1081 * Returns the access rights that are required for opening the given file, 1082 * depending on the file type and open mode. 1083 */ 1084 static inline access_mask_t 1085 get_required_file_open_access(const struct file *const file) 1086 { 1087 access_mask_t access = 0; 1088 1089 if (file->f_mode & FMODE_READ) { 1090 /* A directory can only be opened in read mode. */ 1091 if (S_ISDIR(file_inode(file)->i_mode)) 1092 return LANDLOCK_ACCESS_FS_READ_DIR; 1093 access = LANDLOCK_ACCESS_FS_READ_FILE; 1094 } 1095 if (file->f_mode & FMODE_WRITE) 1096 access |= LANDLOCK_ACCESS_FS_WRITE_FILE; 1097 /* __FMODE_EXEC is indeed part of f_flags, not f_mode. */ 1098 if (file->f_flags & __FMODE_EXEC) 1099 access |= LANDLOCK_ACCESS_FS_EXECUTE; 1100 return access; 1101 } 1102 1103 static int hook_file_alloc_security(struct file *const file) 1104 { 1105 /* 1106 * Grants all access rights, even if most of them are not checked later 1107 * on. It is more consistent. 1108 * 1109 * Notably, file descriptors for regular files can also be acquired 1110 * without going through the file_open hook, for example when using 1111 * memfd_create(2). 1112 */ 1113 landlock_file(file)->allowed_access = LANDLOCK_MASK_ACCESS_FS; 1114 return 0; 1115 } 1116 1117 static int hook_file_open(struct file *const file) 1118 { 1119 layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {}; 1120 access_mask_t open_access_request, full_access_request, allowed_access; 1121 const access_mask_t optional_access = LANDLOCK_ACCESS_FS_TRUNCATE; 1122 const struct landlock_ruleset *const dom = get_current_fs_domain(); 1123 1124 if (!dom) 1125 return 0; 1126 1127 /* 1128 * Because a file may be opened with O_PATH, get_required_file_open_access() 1129 * may return 0. This case will be handled with a future Landlock 1130 * evolution. 1131 */ 1132 open_access_request = get_required_file_open_access(file); 1133 1134 /* 1135 * We look up more access than what we immediately need for open(), so 1136 * that we can later authorize operations on opened files. 1137 */ 1138 full_access_request = open_access_request | optional_access; 1139 1140 if (is_access_to_paths_allowed( 1141 dom, &file->f_path, 1142 landlock_init_layer_masks(dom, full_access_request, 1143 &layer_masks), 1144 &layer_masks, NULL, 0, NULL, NULL)) { 1145 allowed_access = full_access_request; 1146 } else { 1147 unsigned long access_bit; 1148 const unsigned long access_req = full_access_request; 1149 1150 /* 1151 * Calculate the actual allowed access rights from layer_masks. 1152 * Add each access right to allowed_access which has not been 1153 * vetoed by any layer. 1154 */ 1155 allowed_access = 0; 1156 for_each_set_bit(access_bit, &access_req, 1157 ARRAY_SIZE(layer_masks)) { 1158 if (!layer_masks[access_bit]) 1159 allowed_access |= BIT_ULL(access_bit); 1160 } 1161 } 1162 1163 /* 1164 * For operations on already opened files (i.e. ftruncate()), it is the 1165 * access rights at the time of open() which decide whether the 1166 * operation is permitted. Therefore, we record the relevant subset of 1167 * file access rights in the opened struct file. 1168 */ 1169 landlock_file(file)->allowed_access = allowed_access; 1170 1171 if ((open_access_request & allowed_access) == open_access_request) 1172 return 0; 1173 1174 return -EACCES; 1175 } 1176 1177 static int hook_file_truncate(struct file *const file) 1178 { 1179 /* 1180 * Allows truncation if the truncate right was available at the time of 1181 * opening the file, to get a consistent access check as for read, write 1182 * and execute operations. 1183 * 1184 * Note: For checks done based on the file's Landlock allowed access, we 1185 * enforce them independently of whether the current thread is in a 1186 * Landlock domain, so that open files passed between independent 1187 * processes retain their behaviour. 1188 */ 1189 if (landlock_file(file)->allowed_access & LANDLOCK_ACCESS_FS_TRUNCATE) 1190 return 0; 1191 return -EACCES; 1192 } 1193 1194 static struct security_hook_list landlock_hooks[] __ro_after_init = { 1195 LSM_HOOK_INIT(inode_free_security, hook_inode_free_security), 1196 1197 LSM_HOOK_INIT(sb_delete, hook_sb_delete), 1198 LSM_HOOK_INIT(sb_mount, hook_sb_mount), 1199 LSM_HOOK_INIT(move_mount, hook_move_mount), 1200 LSM_HOOK_INIT(sb_umount, hook_sb_umount), 1201 LSM_HOOK_INIT(sb_remount, hook_sb_remount), 1202 LSM_HOOK_INIT(sb_pivotroot, hook_sb_pivotroot), 1203 1204 LSM_HOOK_INIT(path_link, hook_path_link), 1205 LSM_HOOK_INIT(path_rename, hook_path_rename), 1206 LSM_HOOK_INIT(path_mkdir, hook_path_mkdir), 1207 LSM_HOOK_INIT(path_mknod, hook_path_mknod), 1208 LSM_HOOK_INIT(path_symlink, hook_path_symlink), 1209 LSM_HOOK_INIT(path_unlink, hook_path_unlink), 1210 LSM_HOOK_INIT(path_rmdir, hook_path_rmdir), 1211 LSM_HOOK_INIT(path_truncate, hook_path_truncate), 1212 1213 LSM_HOOK_INIT(file_alloc_security, hook_file_alloc_security), 1214 LSM_HOOK_INIT(file_open, hook_file_open), 1215 LSM_HOOK_INIT(file_truncate, hook_file_truncate), 1216 }; 1217 1218 __init void landlock_add_fs_hooks(void) 1219 { 1220 security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks), 1221 LANDLOCK_NAME); 1222 } 1223