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