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