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 */ 8 9 #include <linux/atomic.h> 10 #include <linux/bitops.h> 11 #include <linux/bits.h> 12 #include <linux/compiler_types.h> 13 #include <linux/dcache.h> 14 #include <linux/err.h> 15 #include <linux/fs.h> 16 #include <linux/init.h> 17 #include <linux/kernel.h> 18 #include <linux/limits.h> 19 #include <linux/list.h> 20 #include <linux/lsm_hooks.h> 21 #include <linux/mount.h> 22 #include <linux/namei.h> 23 #include <linux/path.h> 24 #include <linux/rcupdate.h> 25 #include <linux/spinlock.h> 26 #include <linux/stat.h> 27 #include <linux/types.h> 28 #include <linux/wait_bit.h> 29 #include <linux/workqueue.h> 30 #include <uapi/linux/landlock.h> 31 32 #include "common.h" 33 #include "cred.h" 34 #include "fs.h" 35 #include "limits.h" 36 #include "object.h" 37 #include "ruleset.h" 38 #include "setup.h" 39 40 /* Underlying object management */ 41 42 static void release_inode(struct landlock_object *const object) 43 __releases(object->lock) 44 { 45 struct inode *const inode = object->underobj; 46 struct super_block *sb; 47 48 if (!inode) { 49 spin_unlock(&object->lock); 50 return; 51 } 52 53 /* 54 * Protects against concurrent use by hook_sb_delete() of the reference 55 * to the underlying inode. 56 */ 57 object->underobj = NULL; 58 /* 59 * Makes sure that if the filesystem is concurrently unmounted, 60 * hook_sb_delete() will wait for us to finish iput(). 61 */ 62 sb = inode->i_sb; 63 atomic_long_inc(&landlock_superblock(sb)->inode_refs); 64 spin_unlock(&object->lock); 65 /* 66 * Because object->underobj was not NULL, hook_sb_delete() and 67 * get_inode_object() guarantee that it is safe to reset 68 * landlock_inode(inode)->object while it is not NULL. It is therefore 69 * not necessary to lock inode->i_lock. 70 */ 71 rcu_assign_pointer(landlock_inode(inode)->object, NULL); 72 /* 73 * Now, new rules can safely be tied to @inode with get_inode_object(). 74 */ 75 76 iput(inode); 77 if (atomic_long_dec_and_test(&landlock_superblock(sb)->inode_refs)) 78 wake_up_var(&landlock_superblock(sb)->inode_refs); 79 } 80 81 static const struct landlock_object_underops landlock_fs_underops = { 82 .release = release_inode 83 }; 84 85 /* Ruleset management */ 86 87 static struct landlock_object *get_inode_object(struct inode *const inode) 88 { 89 struct landlock_object *object, *new_object; 90 struct landlock_inode_security *inode_sec = landlock_inode(inode); 91 92 rcu_read_lock(); 93 retry: 94 object = rcu_dereference(inode_sec->object); 95 if (object) { 96 if (likely(refcount_inc_not_zero(&object->usage))) { 97 rcu_read_unlock(); 98 return object; 99 } 100 /* 101 * We are racing with release_inode(), the object is going 102 * away. Wait for release_inode(), then retry. 103 */ 104 spin_lock(&object->lock); 105 spin_unlock(&object->lock); 106 goto retry; 107 } 108 rcu_read_unlock(); 109 110 /* 111 * If there is no object tied to @inode, then create a new one (without 112 * holding any locks). 113 */ 114 new_object = landlock_create_object(&landlock_fs_underops, inode); 115 if (IS_ERR(new_object)) 116 return new_object; 117 118 /* 119 * Protects against concurrent calls to get_inode_object() or 120 * hook_sb_delete(). 121 */ 122 spin_lock(&inode->i_lock); 123 if (unlikely(rcu_access_pointer(inode_sec->object))) { 124 /* Someone else just created the object, bail out and retry. */ 125 spin_unlock(&inode->i_lock); 126 kfree(new_object); 127 128 rcu_read_lock(); 129 goto retry; 130 } 131 132 /* 133 * @inode will be released by hook_sb_delete() on its superblock 134 * shutdown, or by release_inode() when no more ruleset references the 135 * related object. 136 */ 137 ihold(inode); 138 rcu_assign_pointer(inode_sec->object, new_object); 139 spin_unlock(&inode->i_lock); 140 return new_object; 141 } 142 143 /* All access rights that can be tied to files. */ 144 /* clang-format off */ 145 #define ACCESS_FILE ( \ 146 LANDLOCK_ACCESS_FS_EXECUTE | \ 147 LANDLOCK_ACCESS_FS_WRITE_FILE | \ 148 LANDLOCK_ACCESS_FS_READ_FILE) 149 /* clang-format on */ 150 151 /* 152 * @path: Should have been checked by get_path_from_fd(). 153 */ 154 int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, 155 const struct path *const path, 156 access_mask_t access_rights) 157 { 158 int err; 159 struct landlock_object *object; 160 161 /* Files only get access rights that make sense. */ 162 if (!d_is_dir(path->dentry) && 163 (access_rights | ACCESS_FILE) != ACCESS_FILE) 164 return -EINVAL; 165 if (WARN_ON_ONCE(ruleset->num_layers != 1)) 166 return -EINVAL; 167 168 /* Transforms relative access rights to absolute ones. */ 169 access_rights |= LANDLOCK_MASK_ACCESS_FS & ~ruleset->fs_access_masks[0]; 170 object = get_inode_object(d_backing_inode(path->dentry)); 171 if (IS_ERR(object)) 172 return PTR_ERR(object); 173 mutex_lock(&ruleset->lock); 174 err = landlock_insert_rule(ruleset, object, access_rights); 175 mutex_unlock(&ruleset->lock); 176 /* 177 * No need to check for an error because landlock_insert_rule() 178 * increments the refcount for the new object if needed. 179 */ 180 landlock_put_object(object); 181 return err; 182 } 183 184 /* Access-control management */ 185 186 static inline u64 unmask_layers(const struct landlock_ruleset *const domain, 187 const struct path *const path, 188 const access_mask_t access_request, 189 u64 layer_mask) 190 { 191 const struct landlock_rule *rule; 192 const struct inode *inode; 193 size_t i; 194 195 if (d_is_negative(path->dentry)) 196 /* Ignore nonexistent leafs. */ 197 return layer_mask; 198 inode = d_backing_inode(path->dentry); 199 rcu_read_lock(); 200 rule = landlock_find_rule( 201 domain, rcu_dereference(landlock_inode(inode)->object)); 202 rcu_read_unlock(); 203 if (!rule) 204 return layer_mask; 205 206 /* 207 * An access is granted if, for each policy layer, at least one rule 208 * encountered on the pathwalk grants the requested accesses, 209 * regardless of their position in the layer stack. We must then check 210 * the remaining layers for each inode, from the first added layer to 211 * the last one. 212 */ 213 for (i = 0; i < rule->num_layers; i++) { 214 const struct landlock_layer *const layer = &rule->layers[i]; 215 const u64 layer_level = BIT_ULL(layer->level - 1); 216 217 /* Checks that the layer grants access to the full request. */ 218 if ((layer->access & access_request) == access_request) { 219 layer_mask &= ~layer_level; 220 221 if (layer_mask == 0) 222 return layer_mask; 223 } 224 } 225 return layer_mask; 226 } 227 228 static int check_access_path(const struct landlock_ruleset *const domain, 229 const struct path *const path, 230 const access_mask_t access_request) 231 { 232 bool allowed = false; 233 struct path walker_path; 234 u64 layer_mask; 235 size_t i; 236 237 /* Make sure all layers can be checked. */ 238 BUILD_BUG_ON(BITS_PER_TYPE(layer_mask) < LANDLOCK_MAX_NUM_LAYERS); 239 240 if (!access_request) 241 return 0; 242 if (WARN_ON_ONCE(!domain || !path)) 243 return 0; 244 /* 245 * Allows access to pseudo filesystems that will never be mountable 246 * (e.g. sockfs, pipefs), but can still be reachable through 247 * /proc/<pid>/fd/<file-descriptor> . 248 */ 249 if ((path->dentry->d_sb->s_flags & SB_NOUSER) || 250 (d_is_positive(path->dentry) && 251 unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))) 252 return 0; 253 if (WARN_ON_ONCE(domain->num_layers < 1)) 254 return -EACCES; 255 256 /* Saves all layers handling a subset of requested accesses. */ 257 layer_mask = 0; 258 for (i = 0; i < domain->num_layers; i++) { 259 if (domain->fs_access_masks[i] & access_request) 260 layer_mask |= BIT_ULL(i); 261 } 262 /* An access request not handled by the domain is allowed. */ 263 if (layer_mask == 0) 264 return 0; 265 266 walker_path = *path; 267 path_get(&walker_path); 268 /* 269 * We need to walk through all the hierarchy to not miss any relevant 270 * restriction. 271 */ 272 while (true) { 273 struct dentry *parent_dentry; 274 275 layer_mask = unmask_layers(domain, &walker_path, access_request, 276 layer_mask); 277 if (layer_mask == 0) { 278 /* Stops when a rule from each layer grants access. */ 279 allowed = true; 280 break; 281 } 282 283 jump_up: 284 if (walker_path.dentry == walker_path.mnt->mnt_root) { 285 if (follow_up(&walker_path)) { 286 /* Ignores hidden mount points. */ 287 goto jump_up; 288 } else { 289 /* 290 * Stops at the real root. Denies access 291 * because not all layers have granted access. 292 */ 293 allowed = false; 294 break; 295 } 296 } 297 if (unlikely(IS_ROOT(walker_path.dentry))) { 298 /* 299 * Stops at disconnected root directories. Only allows 300 * access to internal filesystems (e.g. nsfs, which is 301 * reachable through /proc/<pid>/ns/<namespace>). 302 */ 303 allowed = !!(walker_path.mnt->mnt_flags & MNT_INTERNAL); 304 break; 305 } 306 parent_dentry = dget_parent(walker_path.dentry); 307 dput(walker_path.dentry); 308 walker_path.dentry = parent_dentry; 309 } 310 path_put(&walker_path); 311 return allowed ? 0 : -EACCES; 312 } 313 314 static inline int current_check_access_path(const struct path *const path, 315 const access_mask_t access_request) 316 { 317 const struct landlock_ruleset *const dom = 318 landlock_get_current_domain(); 319 320 if (!dom) 321 return 0; 322 return check_access_path(dom, path, access_request); 323 } 324 325 /* Inode hooks */ 326 327 static void hook_inode_free_security(struct inode *const inode) 328 { 329 /* 330 * All inodes must already have been untied from their object by 331 * release_inode() or hook_sb_delete(). 332 */ 333 WARN_ON_ONCE(landlock_inode(inode)->object); 334 } 335 336 /* Super-block hooks */ 337 338 /* 339 * Release the inodes used in a security policy. 340 * 341 * Cf. fsnotify_unmount_inodes() and invalidate_inodes() 342 */ 343 static void hook_sb_delete(struct super_block *const sb) 344 { 345 struct inode *inode, *prev_inode = NULL; 346 347 if (!landlock_initialized) 348 return; 349 350 spin_lock(&sb->s_inode_list_lock); 351 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 352 struct landlock_object *object; 353 354 /* Only handles referenced inodes. */ 355 if (!atomic_read(&inode->i_count)) 356 continue; 357 358 /* 359 * Protects against concurrent modification of inode (e.g. 360 * from get_inode_object()). 361 */ 362 spin_lock(&inode->i_lock); 363 /* 364 * Checks I_FREEING and I_WILL_FREE to protect against a race 365 * condition when release_inode() just called iput(), which 366 * could lead to a NULL dereference of inode->security or a 367 * second call to iput() for the same Landlock object. Also 368 * checks I_NEW because such inode cannot be tied to an object. 369 */ 370 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) { 371 spin_unlock(&inode->i_lock); 372 continue; 373 } 374 375 rcu_read_lock(); 376 object = rcu_dereference(landlock_inode(inode)->object); 377 if (!object) { 378 rcu_read_unlock(); 379 spin_unlock(&inode->i_lock); 380 continue; 381 } 382 /* Keeps a reference to this inode until the next loop walk. */ 383 __iget(inode); 384 spin_unlock(&inode->i_lock); 385 386 /* 387 * If there is no concurrent release_inode() ongoing, then we 388 * are in charge of calling iput() on this inode, otherwise we 389 * will just wait for it to finish. 390 */ 391 spin_lock(&object->lock); 392 if (object->underobj == inode) { 393 object->underobj = NULL; 394 spin_unlock(&object->lock); 395 rcu_read_unlock(); 396 397 /* 398 * Because object->underobj was not NULL, 399 * release_inode() and get_inode_object() guarantee 400 * that it is safe to reset 401 * landlock_inode(inode)->object while it is not NULL. 402 * It is therefore not necessary to lock inode->i_lock. 403 */ 404 rcu_assign_pointer(landlock_inode(inode)->object, NULL); 405 /* 406 * At this point, we own the ihold() reference that was 407 * originally set up by get_inode_object() and the 408 * __iget() reference that we just set in this loop 409 * walk. Therefore the following call to iput() will 410 * not sleep nor drop the inode because there is now at 411 * least two references to it. 412 */ 413 iput(inode); 414 } else { 415 spin_unlock(&object->lock); 416 rcu_read_unlock(); 417 } 418 419 if (prev_inode) { 420 /* 421 * At this point, we still own the __iget() reference 422 * that we just set in this loop walk. Therefore we 423 * can drop the list lock and know that the inode won't 424 * disappear from under us until the next loop walk. 425 */ 426 spin_unlock(&sb->s_inode_list_lock); 427 /* 428 * We can now actually put the inode reference from the 429 * previous loop walk, which is not needed anymore. 430 */ 431 iput(prev_inode); 432 cond_resched(); 433 spin_lock(&sb->s_inode_list_lock); 434 } 435 prev_inode = inode; 436 } 437 spin_unlock(&sb->s_inode_list_lock); 438 439 /* Puts the inode reference from the last loop walk, if any. */ 440 if (prev_inode) 441 iput(prev_inode); 442 /* Waits for pending iput() in release_inode(). */ 443 wait_var_event(&landlock_superblock(sb)->inode_refs, 444 !atomic_long_read(&landlock_superblock(sb)->inode_refs)); 445 } 446 447 /* 448 * Because a Landlock security policy is defined according to the filesystem 449 * topology (i.e. the mount namespace), changing it may grant access to files 450 * not previously allowed. 451 * 452 * To make it simple, deny any filesystem topology modification by landlocked 453 * processes. Non-landlocked processes may still change the namespace of a 454 * landlocked process, but this kind of threat must be handled by a system-wide 455 * access-control security policy. 456 * 457 * This could be lifted in the future if Landlock can safely handle mount 458 * namespace updates requested by a landlocked process. Indeed, we could 459 * update the current domain (which is currently read-only) by taking into 460 * account the accesses of the source and the destination of a new mount point. 461 * However, it would also require to make all the child domains dynamically 462 * inherit these new constraints. Anyway, for backward compatibility reasons, 463 * a dedicated user space option would be required (e.g. as a ruleset flag). 464 */ 465 static int hook_sb_mount(const char *const dev_name, 466 const struct path *const path, const char *const type, 467 const unsigned long flags, void *const data) 468 { 469 if (!landlock_get_current_domain()) 470 return 0; 471 return -EPERM; 472 } 473 474 static int hook_move_mount(const struct path *const from_path, 475 const struct path *const to_path) 476 { 477 if (!landlock_get_current_domain()) 478 return 0; 479 return -EPERM; 480 } 481 482 /* 483 * Removing a mount point may reveal a previously hidden file hierarchy, which 484 * may then grant access to files, which may have previously been forbidden. 485 */ 486 static int hook_sb_umount(struct vfsmount *const mnt, const int flags) 487 { 488 if (!landlock_get_current_domain()) 489 return 0; 490 return -EPERM; 491 } 492 493 static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts) 494 { 495 if (!landlock_get_current_domain()) 496 return 0; 497 return -EPERM; 498 } 499 500 /* 501 * pivot_root(2), like mount(2), changes the current mount namespace. It must 502 * then be forbidden for a landlocked process. 503 * 504 * However, chroot(2) may be allowed because it only changes the relative root 505 * directory of the current process. Moreover, it can be used to restrict the 506 * view of the filesystem. 507 */ 508 static int hook_sb_pivotroot(const struct path *const old_path, 509 const struct path *const new_path) 510 { 511 if (!landlock_get_current_domain()) 512 return 0; 513 return -EPERM; 514 } 515 516 /* Path hooks */ 517 518 static inline access_mask_t get_mode_access(const umode_t mode) 519 { 520 switch (mode & S_IFMT) { 521 case S_IFLNK: 522 return LANDLOCK_ACCESS_FS_MAKE_SYM; 523 case 0: 524 /* A zero mode translates to S_IFREG. */ 525 case S_IFREG: 526 return LANDLOCK_ACCESS_FS_MAKE_REG; 527 case S_IFDIR: 528 return LANDLOCK_ACCESS_FS_MAKE_DIR; 529 case S_IFCHR: 530 return LANDLOCK_ACCESS_FS_MAKE_CHAR; 531 case S_IFBLK: 532 return LANDLOCK_ACCESS_FS_MAKE_BLOCK; 533 case S_IFIFO: 534 return LANDLOCK_ACCESS_FS_MAKE_FIFO; 535 case S_IFSOCK: 536 return LANDLOCK_ACCESS_FS_MAKE_SOCK; 537 default: 538 WARN_ON_ONCE(1); 539 return 0; 540 } 541 } 542 543 /* 544 * Creating multiple links or renaming may lead to privilege escalations if not 545 * handled properly. Indeed, we must be sure that the source doesn't gain more 546 * privileges by being accessible from the destination. This is getting more 547 * complex when dealing with multiple layers. The whole picture can be seen as 548 * a multilayer partial ordering problem. A future version of Landlock will 549 * deal with that. 550 */ 551 static int hook_path_link(struct dentry *const old_dentry, 552 const struct path *const new_dir, 553 struct dentry *const new_dentry) 554 { 555 const struct landlock_ruleset *const dom = 556 landlock_get_current_domain(); 557 558 if (!dom) 559 return 0; 560 /* The mount points are the same for old and new paths, cf. EXDEV. */ 561 if (old_dentry->d_parent != new_dir->dentry) 562 /* Gracefully forbids reparenting. */ 563 return -EXDEV; 564 if (unlikely(d_is_negative(old_dentry))) 565 return -ENOENT; 566 return check_access_path( 567 dom, new_dir, 568 get_mode_access(d_backing_inode(old_dentry)->i_mode)); 569 } 570 571 static inline access_mask_t maybe_remove(const struct dentry *const dentry) 572 { 573 if (d_is_negative(dentry)) 574 return 0; 575 return d_is_dir(dentry) ? LANDLOCK_ACCESS_FS_REMOVE_DIR : 576 LANDLOCK_ACCESS_FS_REMOVE_FILE; 577 } 578 579 static int hook_path_rename(const struct path *const old_dir, 580 struct dentry *const old_dentry, 581 const struct path *const new_dir, 582 struct dentry *const new_dentry) 583 { 584 const struct landlock_ruleset *const dom = 585 landlock_get_current_domain(); 586 587 if (!dom) 588 return 0; 589 /* The mount points are the same for old and new paths, cf. EXDEV. */ 590 if (old_dir->dentry != new_dir->dentry) 591 /* Gracefully forbids reparenting. */ 592 return -EXDEV; 593 if (unlikely(d_is_negative(old_dentry))) 594 return -ENOENT; 595 /* RENAME_EXCHANGE is handled because directories are the same. */ 596 return check_access_path( 597 dom, old_dir, 598 maybe_remove(old_dentry) | maybe_remove(new_dentry) | 599 get_mode_access(d_backing_inode(old_dentry)->i_mode)); 600 } 601 602 static int hook_path_mkdir(const struct path *const dir, 603 struct dentry *const dentry, const umode_t mode) 604 { 605 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_DIR); 606 } 607 608 static int hook_path_mknod(const struct path *const dir, 609 struct dentry *const dentry, const umode_t mode, 610 const unsigned int dev) 611 { 612 const struct landlock_ruleset *const dom = 613 landlock_get_current_domain(); 614 615 if (!dom) 616 return 0; 617 return check_access_path(dom, dir, get_mode_access(mode)); 618 } 619 620 static int hook_path_symlink(const struct path *const dir, 621 struct dentry *const dentry, 622 const char *const old_name) 623 { 624 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_SYM); 625 } 626 627 static int hook_path_unlink(const struct path *const dir, 628 struct dentry *const dentry) 629 { 630 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_FILE); 631 } 632 633 static int hook_path_rmdir(const struct path *const dir, 634 struct dentry *const dentry) 635 { 636 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_DIR); 637 } 638 639 /* File hooks */ 640 641 static inline access_mask_t get_file_access(const struct file *const file) 642 { 643 access_mask_t access = 0; 644 645 if (file->f_mode & FMODE_READ) { 646 /* A directory can only be opened in read mode. */ 647 if (S_ISDIR(file_inode(file)->i_mode)) 648 return LANDLOCK_ACCESS_FS_READ_DIR; 649 access = LANDLOCK_ACCESS_FS_READ_FILE; 650 } 651 if (file->f_mode & FMODE_WRITE) 652 access |= LANDLOCK_ACCESS_FS_WRITE_FILE; 653 /* __FMODE_EXEC is indeed part of f_flags, not f_mode. */ 654 if (file->f_flags & __FMODE_EXEC) 655 access |= LANDLOCK_ACCESS_FS_EXECUTE; 656 return access; 657 } 658 659 static int hook_file_open(struct file *const file) 660 { 661 const struct landlock_ruleset *const dom = 662 landlock_get_current_domain(); 663 664 if (!dom) 665 return 0; 666 /* 667 * Because a file may be opened with O_PATH, get_file_access() may 668 * return 0. This case will be handled with a future Landlock 669 * evolution. 670 */ 671 return check_access_path(dom, &file->f_path, get_file_access(file)); 672 } 673 674 static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { 675 LSM_HOOK_INIT(inode_free_security, hook_inode_free_security), 676 677 LSM_HOOK_INIT(sb_delete, hook_sb_delete), 678 LSM_HOOK_INIT(sb_mount, hook_sb_mount), 679 LSM_HOOK_INIT(move_mount, hook_move_mount), 680 LSM_HOOK_INIT(sb_umount, hook_sb_umount), 681 LSM_HOOK_INIT(sb_remount, hook_sb_remount), 682 LSM_HOOK_INIT(sb_pivotroot, hook_sb_pivotroot), 683 684 LSM_HOOK_INIT(path_link, hook_path_link), 685 LSM_HOOK_INIT(path_rename, hook_path_rename), 686 LSM_HOOK_INIT(path_mkdir, hook_path_mkdir), 687 LSM_HOOK_INIT(path_mknod, hook_path_mknod), 688 LSM_HOOK_INIT(path_symlink, hook_path_symlink), 689 LSM_HOOK_INIT(path_unlink, hook_path_unlink), 690 LSM_HOOK_INIT(path_rmdir, hook_path_rmdir), 691 692 LSM_HOOK_INIT(file_open, hook_file_open), 693 }; 694 695 __init void landlock_add_fs_hooks(void) 696 { 697 security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks), 698 LANDLOCK_NAME); 699 } 700