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