1 // SPDX-License-Identifier: GPL-2.0-only
3 * Landlock LSM - Filesystem management and hooks
5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6 * Copyright © 2018-2020 ANSSI
7 * Copyright © 2021-2022 Microsoft Corporation
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/bits.h>
13 #include <linux/compiler_types.h>
14 #include <linux/dcache.h>
15 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/limits.h>
20 #include <linux/list.h>
21 #include <linux/lsm_hooks.h>
22 #include <linux/mount.h>
23 #include <linux/namei.h>
24 #include <linux/path.h>
25 #include <linux/rcupdate.h>
26 #include <linux/spinlock.h>
27 #include <linux/stat.h>
28 #include <linux/types.h>
29 #include <linux/wait_bit.h>
30 #include <linux/workqueue.h>
31 #include <uapi/linux/landlock.h>
41 /* Underlying object management */
43 static void release_inode(struct landlock_object *const object)
44 __releases(object->lock)
46 struct inode *const inode = object->underobj;
47 struct super_block *sb;
50 spin_unlock(&object->lock);
55 * Protects against concurrent use by hook_sb_delete() of the reference
56 * to the underlying inode.
58 object->underobj = NULL;
60 * Makes sure that if the filesystem is concurrently unmounted,
61 * hook_sb_delete() will wait for us to finish iput().
64 atomic_long_inc(&landlock_superblock(sb)->inode_refs);
65 spin_unlock(&object->lock);
67 * Because object->underobj was not NULL, hook_sb_delete() and
68 * get_inode_object() guarantee that it is safe to reset
69 * landlock_inode(inode)->object while it is not NULL. It is therefore
70 * not necessary to lock inode->i_lock.
72 rcu_assign_pointer(landlock_inode(inode)->object, NULL);
74 * Now, new rules can safely be tied to @inode with get_inode_object().
78 if (atomic_long_dec_and_test(&landlock_superblock(sb)->inode_refs))
79 wake_up_var(&landlock_superblock(sb)->inode_refs);
82 static const struct landlock_object_underops landlock_fs_underops = {
83 .release = release_inode
86 /* Ruleset management */
88 static struct landlock_object *get_inode_object(struct inode *const inode)
90 struct landlock_object *object, *new_object;
91 struct landlock_inode_security *inode_sec = landlock_inode(inode);
95 object = rcu_dereference(inode_sec->object);
97 if (likely(refcount_inc_not_zero(&object->usage))) {
102 * We are racing with release_inode(), the object is going
103 * away. Wait for release_inode(), then retry.
105 spin_lock(&object->lock);
106 spin_unlock(&object->lock);
112 * If there is no object tied to @inode, then create a new one (without
113 * holding any locks).
115 new_object = landlock_create_object(&landlock_fs_underops, inode);
116 if (IS_ERR(new_object))
120 * Protects against concurrent calls to get_inode_object() or
123 spin_lock(&inode->i_lock);
124 if (unlikely(rcu_access_pointer(inode_sec->object))) {
125 /* Someone else just created the object, bail out and retry. */
126 spin_unlock(&inode->i_lock);
134 * @inode will be released by hook_sb_delete() on its superblock
135 * shutdown, or by release_inode() when no more ruleset references the
139 rcu_assign_pointer(inode_sec->object, new_object);
140 spin_unlock(&inode->i_lock);
144 /* All access rights that can be tied to files. */
145 /* clang-format off */
146 #define ACCESS_FILE ( \
147 LANDLOCK_ACCESS_FS_EXECUTE | \
148 LANDLOCK_ACCESS_FS_WRITE_FILE | \
149 LANDLOCK_ACCESS_FS_READ_FILE | \
150 LANDLOCK_ACCESS_FS_TRUNCATE)
151 /* clang-format on */
154 * @path: Should have been checked by get_path_from_fd().
156 int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
157 const struct path *const path,
158 access_mask_t access_rights)
161 struct landlock_id id = {
162 .type = LANDLOCK_KEY_INODE,
165 /* Files only get access rights that make sense. */
166 if (!d_is_dir(path->dentry) &&
167 (access_rights | ACCESS_FILE) != ACCESS_FILE)
169 if (WARN_ON_ONCE(ruleset->num_layers != 1))
172 /* Transforms relative access rights to absolute ones. */
173 access_rights |= LANDLOCK_MASK_ACCESS_FS &
174 ~landlock_get_fs_access_mask(ruleset, 0);
175 id.key.object = get_inode_object(d_backing_inode(path->dentry));
176 if (IS_ERR(id.key.object))
177 return PTR_ERR(id.key.object);
178 mutex_lock(&ruleset->lock);
179 err = landlock_insert_rule(ruleset, id, access_rights);
180 mutex_unlock(&ruleset->lock);
182 * No need to check for an error because landlock_insert_rule()
183 * increments the refcount for the new object if needed.
185 landlock_put_object(id.key.object);
189 /* Access-control management */
192 * The lifetime of the returned rule is tied to @domain.
194 * Returns NULL if no rule is found or if @dentry is negative.
196 static const struct landlock_rule *
197 find_rule(const struct landlock_ruleset *const domain,
198 const struct dentry *const dentry)
200 const struct landlock_rule *rule;
201 const struct inode *inode;
202 struct landlock_id id = {
203 .type = LANDLOCK_KEY_INODE,
206 /* Ignores nonexistent leafs. */
207 if (d_is_negative(dentry))
210 inode = d_backing_inode(dentry);
212 id.key.object = rcu_dereference(landlock_inode(inode)->object);
213 rule = landlock_find_rule(domain, id);
219 * Allows access to pseudo filesystems that will never be mountable (e.g.
220 * sockfs, pipefs), but can still be reachable through
221 * /proc/<pid>/fd/<file-descriptor>
223 static bool is_nouser_or_private(const struct dentry *dentry)
225 return (dentry->d_sb->s_flags & SB_NOUSER) ||
226 (d_is_positive(dentry) &&
227 unlikely(IS_PRIVATE(d_backing_inode(dentry))));
231 get_raw_handled_fs_accesses(const struct landlock_ruleset *const domain)
233 access_mask_t access_dom = 0;
236 for (layer_level = 0; layer_level < domain->num_layers; layer_level++)
238 landlock_get_raw_fs_access_mask(domain, layer_level);
243 get_handled_fs_accesses(const struct landlock_ruleset *const domain)
245 /* Handles all initially denied by default access rights. */
246 return get_raw_handled_fs_accesses(domain) |
247 LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
250 static const struct landlock_ruleset *get_current_fs_domain(void)
252 const struct landlock_ruleset *const dom =
253 landlock_get_current_domain();
255 if (!dom || !get_raw_handled_fs_accesses(dom))
262 * Check that a destination file hierarchy has more restrictions than a source
263 * file hierarchy. This is only used for link and rename actions.
265 * @layer_masks_child2: Optional child masks.
267 static bool no_more_access(
268 const layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS],
269 const layer_mask_t (*const layer_masks_child1)[LANDLOCK_NUM_ACCESS_FS],
270 const bool child1_is_directory,
271 const layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS],
272 const layer_mask_t (*const layer_masks_child2)[LANDLOCK_NUM_ACCESS_FS],
273 const bool child2_is_directory)
275 unsigned long access_bit;
277 for (access_bit = 0; access_bit < ARRAY_SIZE(*layer_masks_parent2);
279 /* Ignores accesses that only make sense for directories. */
280 const bool is_file_access =
281 !!(BIT_ULL(access_bit) & ACCESS_FILE);
283 if (child1_is_directory || is_file_access) {
285 * Checks if the destination restrictions are a
286 * superset of the source ones (i.e. inherited access
287 * rights without child exceptions):
288 * restrictions(parent2) >= restrictions(child1)
290 if ((((*layer_masks_parent1)[access_bit] &
291 (*layer_masks_child1)[access_bit]) |
292 (*layer_masks_parent2)[access_bit]) !=
293 (*layer_masks_parent2)[access_bit])
297 if (!layer_masks_child2)
299 if (child2_is_directory || is_file_access) {
301 * Checks inverted restrictions for RENAME_EXCHANGE:
302 * restrictions(parent1) >= restrictions(child2)
304 if ((((*layer_masks_parent2)[access_bit] &
305 (*layer_masks_child2)[access_bit]) |
306 (*layer_masks_parent1)[access_bit]) !=
307 (*layer_masks_parent1)[access_bit])
315 * Removes @layer_masks accesses that are not requested.
317 * Returns true if the request is allowed, false otherwise.
320 scope_to_request(const access_mask_t access_request,
321 layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS])
323 const unsigned long access_req = access_request;
324 unsigned long access_bit;
326 if (WARN_ON_ONCE(!layer_masks))
329 for_each_clear_bit(access_bit, &access_req, ARRAY_SIZE(*layer_masks))
330 (*layer_masks)[access_bit] = 0;
331 return !memchr_inv(layer_masks, 0, sizeof(*layer_masks));
335 * Returns true if there is at least one access right different than
336 * LANDLOCK_ACCESS_FS_REFER.
339 is_eacces(const layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS],
340 const access_mask_t access_request)
342 unsigned long access_bit;
343 /* LANDLOCK_ACCESS_FS_REFER alone must return -EXDEV. */
344 const unsigned long access_check = access_request &
345 ~LANDLOCK_ACCESS_FS_REFER;
350 for_each_set_bit(access_bit, &access_check, ARRAY_SIZE(*layer_masks)) {
351 if ((*layer_masks)[access_bit])
358 * is_access_to_paths_allowed - Check accesses for requests with a common path
360 * @domain: Domain to check against.
361 * @path: File hierarchy to walk through.
362 * @access_request_parent1: Accesses to check, once @layer_masks_parent1 is
363 * equal to @layer_masks_parent2 (if any). This is tied to the unique
364 * requested path for most actions, or the source in case of a refer action
365 * (i.e. rename or link), or the source and destination in case of
367 * @layer_masks_parent1: Pointer to a matrix of layer masks per access
368 * masks, identifying the layers that forbid a specific access. Bits from
369 * this matrix can be unset according to the @path walk. An empty matrix
370 * means that @domain allows all possible Landlock accesses (i.e. not only
371 * those identified by @access_request_parent1). This matrix can
372 * initially refer to domain layer masks and, when the accesses for the
373 * destination and source are the same, to requested layer masks.
374 * @dentry_child1: Dentry to the initial child of the parent1 path. This
375 * pointer must be NULL for non-refer actions (i.e. not link nor rename).
376 * @access_request_parent2: Similar to @access_request_parent1 but for a
377 * request involving a source and a destination. This refers to the
378 * destination, except in case of RENAME_EXCHANGE where it also refers to
379 * the source. Must be set to 0 when using a simple path request.
380 * @layer_masks_parent2: Similar to @layer_masks_parent1 but for a refer
381 * action. This must be NULL otherwise.
382 * @dentry_child2: Dentry to the initial child of the parent2 path. This
383 * pointer is only set for RENAME_EXCHANGE actions and must be NULL
386 * This helper first checks that the destination has a superset of restrictions
387 * compared to the source (if any) for a common path. Because of
388 * RENAME_EXCHANGE actions, source and destinations may be swapped. It then
389 * checks that the collected accesses and the remaining ones are enough to
393 * - true if the access request is granted;
396 static bool is_access_to_paths_allowed(
397 const struct landlock_ruleset *const domain,
398 const struct path *const path,
399 const access_mask_t access_request_parent1,
400 layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS],
401 const struct dentry *const dentry_child1,
402 const access_mask_t access_request_parent2,
403 layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS],
404 const struct dentry *const dentry_child2)
406 bool allowed_parent1 = false, allowed_parent2 = false, is_dom_check,
407 child1_is_directory = true, child2_is_directory = true;
408 struct path walker_path;
409 access_mask_t access_masked_parent1, access_masked_parent2;
410 layer_mask_t _layer_masks_child1[LANDLOCK_NUM_ACCESS_FS],
411 _layer_masks_child2[LANDLOCK_NUM_ACCESS_FS];
412 layer_mask_t(*layer_masks_child1)[LANDLOCK_NUM_ACCESS_FS] = NULL,
413 (*layer_masks_child2)[LANDLOCK_NUM_ACCESS_FS] = NULL;
415 if (!access_request_parent1 && !access_request_parent2)
417 if (WARN_ON_ONCE(!domain || !path))
419 if (is_nouser_or_private(path->dentry))
421 if (WARN_ON_ONCE(domain->num_layers < 1 || !layer_masks_parent1))
424 if (unlikely(layer_masks_parent2)) {
425 if (WARN_ON_ONCE(!dentry_child1))
428 * For a double request, first check for potential privilege
429 * escalation by looking at domain handled accesses (which are
430 * a superset of the meaningful requested accesses).
432 access_masked_parent1 = access_masked_parent2 =
433 get_handled_fs_accesses(domain);
436 if (WARN_ON_ONCE(dentry_child1 || dentry_child2))
438 /* For a simple request, only check for requested accesses. */
439 access_masked_parent1 = access_request_parent1;
440 access_masked_parent2 = access_request_parent2;
441 is_dom_check = false;
444 if (unlikely(dentry_child1)) {
445 landlock_unmask_layers(
446 find_rule(domain, dentry_child1),
447 landlock_init_layer_masks(
448 domain, LANDLOCK_MASK_ACCESS_FS,
449 &_layer_masks_child1, LANDLOCK_KEY_INODE),
450 &_layer_masks_child1, ARRAY_SIZE(_layer_masks_child1));
451 layer_masks_child1 = &_layer_masks_child1;
452 child1_is_directory = d_is_dir(dentry_child1);
454 if (unlikely(dentry_child2)) {
455 landlock_unmask_layers(
456 find_rule(domain, dentry_child2),
457 landlock_init_layer_masks(
458 domain, LANDLOCK_MASK_ACCESS_FS,
459 &_layer_masks_child2, LANDLOCK_KEY_INODE),
460 &_layer_masks_child2, ARRAY_SIZE(_layer_masks_child2));
461 layer_masks_child2 = &_layer_masks_child2;
462 child2_is_directory = d_is_dir(dentry_child2);
466 path_get(&walker_path);
468 * We need to walk through all the hierarchy to not miss any relevant
472 struct dentry *parent_dentry;
473 const struct landlock_rule *rule;
476 * If at least all accesses allowed on the destination are
477 * already allowed on the source, respectively if there is at
478 * least as much as restrictions on the destination than on the
479 * source, then we can safely refer files from the source to
480 * the destination without risking a privilege escalation.
481 * This also applies in the case of RENAME_EXCHANGE, which
482 * implies checks on both direction. This is crucial for
483 * standalone multilayered security policies. Furthermore,
484 * this helps avoid policy writers to shoot themselves in the
487 if (unlikely(is_dom_check &&
489 layer_masks_parent1, layer_masks_child1,
490 child1_is_directory, layer_masks_parent2,
492 child2_is_directory))) {
493 allowed_parent1 = scope_to_request(
494 access_request_parent1, layer_masks_parent1);
495 allowed_parent2 = scope_to_request(
496 access_request_parent2, layer_masks_parent2);
498 /* Stops when all accesses are granted. */
499 if (allowed_parent1 && allowed_parent2)
503 * Now, downgrades the remaining checks from domain
504 * handled accesses to requested accesses.
506 is_dom_check = false;
507 access_masked_parent1 = access_request_parent1;
508 access_masked_parent2 = access_request_parent2;
511 rule = find_rule(domain, walker_path.dentry);
512 allowed_parent1 = landlock_unmask_layers(
513 rule, access_masked_parent1, layer_masks_parent1,
514 ARRAY_SIZE(*layer_masks_parent1));
515 allowed_parent2 = landlock_unmask_layers(
516 rule, access_masked_parent2, layer_masks_parent2,
517 ARRAY_SIZE(*layer_masks_parent2));
519 /* Stops when a rule from each layer grants access. */
520 if (allowed_parent1 && allowed_parent2)
523 if (walker_path.dentry == walker_path.mnt->mnt_root) {
524 if (follow_up(&walker_path)) {
525 /* Ignores hidden mount points. */
529 * Stops at the real root. Denies access
530 * because not all layers have granted access.
535 if (unlikely(IS_ROOT(walker_path.dentry))) {
537 * Stops at disconnected root directories. Only allows
538 * access to internal filesystems (e.g. nsfs, which is
539 * reachable through /proc/<pid>/ns/<namespace>).
541 allowed_parent1 = allowed_parent2 =
542 !!(walker_path.mnt->mnt_flags & MNT_INTERNAL);
545 parent_dentry = dget_parent(walker_path.dentry);
546 dput(walker_path.dentry);
547 walker_path.dentry = parent_dentry;
549 path_put(&walker_path);
551 return allowed_parent1 && allowed_parent2;
554 static int check_access_path(const struct landlock_ruleset *const domain,
555 const struct path *const path,
556 access_mask_t access_request)
558 layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
560 access_request = landlock_init_layer_masks(
561 domain, access_request, &layer_masks, LANDLOCK_KEY_INODE);
562 if (is_access_to_paths_allowed(domain, path, access_request,
563 &layer_masks, NULL, 0, NULL, NULL))
568 static int current_check_access_path(const struct path *const path,
569 const access_mask_t access_request)
571 const struct landlock_ruleset *const dom = get_current_fs_domain();
575 return check_access_path(dom, path, access_request);
578 static access_mask_t get_mode_access(const umode_t mode)
580 switch (mode & S_IFMT) {
582 return LANDLOCK_ACCESS_FS_MAKE_SYM;
584 /* A zero mode translates to S_IFREG. */
586 return LANDLOCK_ACCESS_FS_MAKE_REG;
588 return LANDLOCK_ACCESS_FS_MAKE_DIR;
590 return LANDLOCK_ACCESS_FS_MAKE_CHAR;
592 return LANDLOCK_ACCESS_FS_MAKE_BLOCK;
594 return LANDLOCK_ACCESS_FS_MAKE_FIFO;
596 return LANDLOCK_ACCESS_FS_MAKE_SOCK;
603 static access_mask_t maybe_remove(const struct dentry *const dentry)
605 if (d_is_negative(dentry))
607 return d_is_dir(dentry) ? LANDLOCK_ACCESS_FS_REMOVE_DIR :
608 LANDLOCK_ACCESS_FS_REMOVE_FILE;
612 * collect_domain_accesses - Walk through a file path and collect accesses
614 * @domain: Domain to check against.
615 * @mnt_root: Last directory to check.
616 * @dir: Directory to start the walk from.
617 * @layer_masks_dom: Where to store the collected accesses.
619 * This helper is useful to begin a path walk from the @dir directory to a
620 * @mnt_root directory used as a mount point. This mount point is the common
621 * ancestor between the source and the destination of a renamed and linked
622 * file. While walking from @dir to @mnt_root, we record all the domain's
623 * allowed accesses in @layer_masks_dom.
625 * This is similar to is_access_to_paths_allowed() but much simpler because it
626 * only handles walking on the same mount point and only checks one set of
630 * - true if all the domain access rights are allowed for @dir;
631 * - false if the walk reached @mnt_root.
633 static bool collect_domain_accesses(
634 const struct landlock_ruleset *const domain,
635 const struct dentry *const mnt_root, struct dentry *dir,
636 layer_mask_t (*const layer_masks_dom)[LANDLOCK_NUM_ACCESS_FS])
638 unsigned long access_dom;
641 if (WARN_ON_ONCE(!domain || !mnt_root || !dir || !layer_masks_dom))
643 if (is_nouser_or_private(dir))
646 access_dom = landlock_init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS,
652 struct dentry *parent_dentry;
654 /* Gets all layers allowing all domain accesses. */
655 if (landlock_unmask_layers(find_rule(domain, dir), access_dom,
657 ARRAY_SIZE(*layer_masks_dom))) {
659 * Stops when all handled accesses are allowed by at
660 * least one rule in each layer.
666 /* We should not reach a root other than @mnt_root. */
667 if (dir == mnt_root || WARN_ON_ONCE(IS_ROOT(dir)))
670 parent_dentry = dget_parent(dir);
679 * current_check_refer_path - Check if a rename or link action is allowed
681 * @old_dentry: File or directory requested to be moved or linked.
682 * @new_dir: Destination parent directory.
683 * @new_dentry: Destination file or directory.
684 * @removable: Sets to true if it is a rename operation.
685 * @exchange: Sets to true if it is a rename operation with RENAME_EXCHANGE.
687 * Because of its unprivileged constraints, Landlock relies on file hierarchies
688 * (and not only inodes) to tie access rights to files. Being able to link or
689 * rename a file hierarchy brings some challenges. Indeed, moving or linking a
690 * file (i.e. creating a new reference to an inode) can have an impact on the
691 * actions allowed for a set of files if it would change its parent directory
692 * (i.e. reparenting).
694 * To avoid trivial access right bypasses, Landlock first checks if the file or
695 * directory requested to be moved would gain new access rights inherited from
696 * its new hierarchy. Before returning any error, Landlock then checks that
697 * the parent source hierarchy and the destination hierarchy would allow the
698 * link or rename action. If it is not the case, an error with EACCES is
699 * returned to inform user space that there is no way to remove or create the
700 * requested source file type. If it should be allowed but the new inherited
701 * access rights would be greater than the source access rights, then the
702 * kernel returns an error with EXDEV. Prioritizing EACCES over EXDEV enables
703 * user space to abort the whole operation if there is no way to do it, or to
704 * manually copy the source to the destination if this remains allowed, e.g.
705 * because file creation is allowed on the destination directory but not direct
708 * To achieve this goal, the kernel needs to compare two file hierarchies: the
709 * one identifying the source file or directory (including itself), and the
710 * destination one. This can be seen as a multilayer partial ordering problem.
711 * The kernel walks through these paths and collects in a matrix the access
712 * rights that are denied per layer. These matrices are then compared to see
713 * if the destination one has more (or the same) restrictions as the source
714 * one. If this is the case, the requested action will not return EXDEV, which
715 * doesn't mean the action is allowed. The parent hierarchy of the source
716 * (i.e. parent directory), and the destination hierarchy must also be checked
717 * to verify that they explicitly allow such action (i.e. referencing,
718 * creation and potentially removal rights). The kernel implementation is then
719 * required to rely on potentially four matrices of access rights: one for the
720 * source file or directory (i.e. the child), a potentially other one for the
721 * other source/destination (in case of RENAME_EXCHANGE), one for the source
722 * parent hierarchy and a last one for the destination hierarchy. These
723 * ephemeral matrices take some space on the stack, which limits the number of
724 * layers to a deemed reasonable number: 16.
727 * - 0 if access is allowed;
728 * - -EXDEV if @old_dentry would inherit new access rights from @new_dir;
729 * - -EACCES if file removal or creation is denied.
731 static int current_check_refer_path(struct dentry *const old_dentry,
732 const struct path *const new_dir,
733 struct dentry *const new_dentry,
734 const bool removable, const bool exchange)
736 const struct landlock_ruleset *const dom = get_current_fs_domain();
737 bool allow_parent1, allow_parent2;
738 access_mask_t access_request_parent1, access_request_parent2;
740 layer_mask_t layer_masks_parent1[LANDLOCK_NUM_ACCESS_FS] = {},
741 layer_masks_parent2[LANDLOCK_NUM_ACCESS_FS] = {};
745 if (WARN_ON_ONCE(dom->num_layers < 1))
747 if (unlikely(d_is_negative(old_dentry)))
750 if (unlikely(d_is_negative(new_dentry)))
752 access_request_parent1 =
753 get_mode_access(d_backing_inode(new_dentry)->i_mode);
755 access_request_parent1 = 0;
757 access_request_parent2 =
758 get_mode_access(d_backing_inode(old_dentry)->i_mode);
760 access_request_parent1 |= maybe_remove(old_dentry);
761 access_request_parent2 |= maybe_remove(new_dentry);
764 /* The mount points are the same for old and new paths, cf. EXDEV. */
765 if (old_dentry->d_parent == new_dir->dentry) {
767 * The LANDLOCK_ACCESS_FS_REFER access right is not required
768 * for same-directory referer (i.e. no reparenting).
770 access_request_parent1 = landlock_init_layer_masks(
771 dom, access_request_parent1 | access_request_parent2,
772 &layer_masks_parent1, LANDLOCK_KEY_INODE);
773 if (is_access_to_paths_allowed(
774 dom, new_dir, access_request_parent1,
775 &layer_masks_parent1, NULL, 0, NULL, NULL))
780 access_request_parent1 |= LANDLOCK_ACCESS_FS_REFER;
781 access_request_parent2 |= LANDLOCK_ACCESS_FS_REFER;
783 /* Saves the common mount point. */
784 mnt_dir.mnt = new_dir->mnt;
785 mnt_dir.dentry = new_dir->mnt->mnt_root;
787 /* new_dir->dentry is equal to new_dentry->d_parent */
788 allow_parent1 = collect_domain_accesses(dom, mnt_dir.dentry,
789 old_dentry->d_parent,
790 &layer_masks_parent1);
791 allow_parent2 = collect_domain_accesses(
792 dom, mnt_dir.dentry, new_dir->dentry, &layer_masks_parent2);
794 if (allow_parent1 && allow_parent2)
798 * To be able to compare source and destination domain access rights,
799 * take into account the @old_dentry access rights aggregated with its
800 * parent access rights. This will be useful to compare with the
801 * destination parent access rights.
803 if (is_access_to_paths_allowed(
804 dom, &mnt_dir, access_request_parent1, &layer_masks_parent1,
805 old_dentry, access_request_parent2, &layer_masks_parent2,
806 exchange ? new_dentry : NULL))
810 * This prioritizes EACCES over EXDEV for all actions, including
811 * renames with RENAME_EXCHANGE.
813 if (likely(is_eacces(&layer_masks_parent1, access_request_parent1) ||
814 is_eacces(&layer_masks_parent2, access_request_parent2)))
818 * Gracefully forbids reparenting if the destination directory
819 * hierarchy is not a superset of restrictions of the source directory
820 * hierarchy, or if LANDLOCK_ACCESS_FS_REFER is not allowed by the
821 * source or the destination.
828 static void hook_inode_free_security(struct inode *const inode)
831 * All inodes must already have been untied from their object by
832 * release_inode() or hook_sb_delete().
834 WARN_ON_ONCE(landlock_inode(inode)->object);
837 /* Super-block hooks */
840 * Release the inodes used in a security policy.
842 * Cf. fsnotify_unmount_inodes() and invalidate_inodes()
844 static void hook_sb_delete(struct super_block *const sb)
846 struct inode *inode, *prev_inode = NULL;
848 if (!landlock_initialized)
851 spin_lock(&sb->s_inode_list_lock);
852 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
853 struct landlock_object *object;
855 /* Only handles referenced inodes. */
856 if (!atomic_read(&inode->i_count))
860 * Protects against concurrent modification of inode (e.g.
861 * from get_inode_object()).
863 spin_lock(&inode->i_lock);
865 * Checks I_FREEING and I_WILL_FREE to protect against a race
866 * condition when release_inode() just called iput(), which
867 * could lead to a NULL dereference of inode->security or a
868 * second call to iput() for the same Landlock object. Also
869 * checks I_NEW because such inode cannot be tied to an object.
871 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
872 spin_unlock(&inode->i_lock);
877 object = rcu_dereference(landlock_inode(inode)->object);
880 spin_unlock(&inode->i_lock);
883 /* Keeps a reference to this inode until the next loop walk. */
885 spin_unlock(&inode->i_lock);
888 * If there is no concurrent release_inode() ongoing, then we
889 * are in charge of calling iput() on this inode, otherwise we
890 * will just wait for it to finish.
892 spin_lock(&object->lock);
893 if (object->underobj == inode) {
894 object->underobj = NULL;
895 spin_unlock(&object->lock);
899 * Because object->underobj was not NULL,
900 * release_inode() and get_inode_object() guarantee
901 * that it is safe to reset
902 * landlock_inode(inode)->object while it is not NULL.
903 * It is therefore not necessary to lock inode->i_lock.
905 rcu_assign_pointer(landlock_inode(inode)->object, NULL);
907 * At this point, we own the ihold() reference that was
908 * originally set up by get_inode_object() and the
909 * __iget() reference that we just set in this loop
910 * walk. Therefore the following call to iput() will
911 * not sleep nor drop the inode because there is now at
912 * least two references to it.
916 spin_unlock(&object->lock);
922 * At this point, we still own the __iget() reference
923 * that we just set in this loop walk. Therefore we
924 * can drop the list lock and know that the inode won't
925 * disappear from under us until the next loop walk.
927 spin_unlock(&sb->s_inode_list_lock);
929 * We can now actually put the inode reference from the
930 * previous loop walk, which is not needed anymore.
934 spin_lock(&sb->s_inode_list_lock);
938 spin_unlock(&sb->s_inode_list_lock);
940 /* Puts the inode reference from the last loop walk, if any. */
943 /* Waits for pending iput() in release_inode(). */
944 wait_var_event(&landlock_superblock(sb)->inode_refs,
945 !atomic_long_read(&landlock_superblock(sb)->inode_refs));
949 * Because a Landlock security policy is defined according to the filesystem
950 * topology (i.e. the mount namespace), changing it may grant access to files
951 * not previously allowed.
953 * To make it simple, deny any filesystem topology modification by landlocked
954 * processes. Non-landlocked processes may still change the namespace of a
955 * landlocked process, but this kind of threat must be handled by a system-wide
956 * access-control security policy.
958 * This could be lifted in the future if Landlock can safely handle mount
959 * namespace updates requested by a landlocked process. Indeed, we could
960 * update the current domain (which is currently read-only) by taking into
961 * account the accesses of the source and the destination of a new mount point.
962 * However, it would also require to make all the child domains dynamically
963 * inherit these new constraints. Anyway, for backward compatibility reasons,
964 * a dedicated user space option would be required (e.g. as a ruleset flag).
966 static int hook_sb_mount(const char *const dev_name,
967 const struct path *const path, const char *const type,
968 const unsigned long flags, void *const data)
970 if (!get_current_fs_domain())
975 static int hook_move_mount(const struct path *const from_path,
976 const struct path *const to_path)
978 if (!get_current_fs_domain())
984 * Removing a mount point may reveal a previously hidden file hierarchy, which
985 * may then grant access to files, which may have previously been forbidden.
987 static int hook_sb_umount(struct vfsmount *const mnt, const int flags)
989 if (!get_current_fs_domain())
994 static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts)
996 if (!get_current_fs_domain())
1002 * pivot_root(2), like mount(2), changes the current mount namespace. It must
1003 * then be forbidden for a landlocked process.
1005 * However, chroot(2) may be allowed because it only changes the relative root
1006 * directory of the current process. Moreover, it can be used to restrict the
1007 * view of the filesystem.
1009 static int hook_sb_pivotroot(const struct path *const old_path,
1010 const struct path *const new_path)
1012 if (!get_current_fs_domain())
1019 static int hook_path_link(struct dentry *const old_dentry,
1020 const struct path *const new_dir,
1021 struct dentry *const new_dentry)
1023 return current_check_refer_path(old_dentry, new_dir, new_dentry, false,
1027 static int hook_path_rename(const struct path *const old_dir,
1028 struct dentry *const old_dentry,
1029 const struct path *const new_dir,
1030 struct dentry *const new_dentry,
1031 const unsigned int flags)
1033 /* old_dir refers to old_dentry->d_parent and new_dir->mnt */
1034 return current_check_refer_path(old_dentry, new_dir, new_dentry, true,
1035 !!(flags & RENAME_EXCHANGE));
1038 static int hook_path_mkdir(const struct path *const dir,
1039 struct dentry *const dentry, const umode_t mode)
1041 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_DIR);
1044 static int hook_path_mknod(const struct path *const dir,
1045 struct dentry *const dentry, const umode_t mode,
1046 const unsigned int dev)
1048 const struct landlock_ruleset *const dom = get_current_fs_domain();
1052 return check_access_path(dom, dir, get_mode_access(mode));
1055 static int hook_path_symlink(const struct path *const dir,
1056 struct dentry *const dentry,
1057 const char *const old_name)
1059 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_SYM);
1062 static int hook_path_unlink(const struct path *const dir,
1063 struct dentry *const dentry)
1065 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_FILE);
1068 static int hook_path_rmdir(const struct path *const dir,
1069 struct dentry *const dentry)
1071 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_DIR);
1074 static int hook_path_truncate(const struct path *const path)
1076 return current_check_access_path(path, LANDLOCK_ACCESS_FS_TRUNCATE);
1082 * get_required_file_open_access - Get access needed to open a file
1084 * @file: File being opened.
1086 * Returns the access rights that are required for opening the given file,
1087 * depending on the file type and open mode.
1089 static access_mask_t
1090 get_required_file_open_access(const struct file *const file)
1092 access_mask_t access = 0;
1094 if (file->f_mode & FMODE_READ) {
1095 /* A directory can only be opened in read mode. */
1096 if (S_ISDIR(file_inode(file)->i_mode))
1097 return LANDLOCK_ACCESS_FS_READ_DIR;
1098 access = LANDLOCK_ACCESS_FS_READ_FILE;
1100 if (file->f_mode & FMODE_WRITE)
1101 access |= LANDLOCK_ACCESS_FS_WRITE_FILE;
1102 /* __FMODE_EXEC is indeed part of f_flags, not f_mode. */
1103 if (file->f_flags & __FMODE_EXEC)
1104 access |= LANDLOCK_ACCESS_FS_EXECUTE;
1108 static int hook_file_alloc_security(struct file *const file)
1111 * Grants all access rights, even if most of them are not checked later
1112 * on. It is more consistent.
1114 * Notably, file descriptors for regular files can also be acquired
1115 * without going through the file_open hook, for example when using
1118 landlock_file(file)->allowed_access = LANDLOCK_MASK_ACCESS_FS;
1122 static int hook_file_open(struct file *const file)
1124 layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
1125 access_mask_t open_access_request, full_access_request, allowed_access;
1126 const access_mask_t optional_access = LANDLOCK_ACCESS_FS_TRUNCATE;
1127 const struct landlock_ruleset *const dom = get_current_fs_domain();
1133 * Because a file may be opened with O_PATH, get_required_file_open_access()
1134 * may return 0. This case will be handled with a future Landlock
1137 open_access_request = get_required_file_open_access(file);
1140 * We look up more access than what we immediately need for open(), so
1141 * that we can later authorize operations on opened files.
1143 full_access_request = open_access_request | optional_access;
1145 if (is_access_to_paths_allowed(
1147 landlock_init_layer_masks(dom, full_access_request,
1148 &layer_masks, LANDLOCK_KEY_INODE),
1149 &layer_masks, NULL, 0, NULL, NULL)) {
1150 allowed_access = full_access_request;
1152 unsigned long access_bit;
1153 const unsigned long access_req = full_access_request;
1156 * Calculate the actual allowed access rights from layer_masks.
1157 * Add each access right to allowed_access which has not been
1158 * vetoed by any layer.
1161 for_each_set_bit(access_bit, &access_req,
1162 ARRAY_SIZE(layer_masks)) {
1163 if (!layer_masks[access_bit])
1164 allowed_access |= BIT_ULL(access_bit);
1169 * For operations on already opened files (i.e. ftruncate()), it is the
1170 * access rights at the time of open() which decide whether the
1171 * operation is permitted. Therefore, we record the relevant subset of
1172 * file access rights in the opened struct file.
1174 landlock_file(file)->allowed_access = allowed_access;
1176 if ((open_access_request & allowed_access) == open_access_request)
1182 static int hook_file_truncate(struct file *const file)
1185 * Allows truncation if the truncate right was available at the time of
1186 * opening the file, to get a consistent access check as for read, write
1187 * and execute operations.
1189 * Note: For checks done based on the file's Landlock allowed access, we
1190 * enforce them independently of whether the current thread is in a
1191 * Landlock domain, so that open files passed between independent
1192 * processes retain their behaviour.
1194 if (landlock_file(file)->allowed_access & LANDLOCK_ACCESS_FS_TRUNCATE)
1199 static struct security_hook_list landlock_hooks[] __ro_after_init = {
1200 LSM_HOOK_INIT(inode_free_security, hook_inode_free_security),
1202 LSM_HOOK_INIT(sb_delete, hook_sb_delete),
1203 LSM_HOOK_INIT(sb_mount, hook_sb_mount),
1204 LSM_HOOK_INIT(move_mount, hook_move_mount),
1205 LSM_HOOK_INIT(sb_umount, hook_sb_umount),
1206 LSM_HOOK_INIT(sb_remount, hook_sb_remount),
1207 LSM_HOOK_INIT(sb_pivotroot, hook_sb_pivotroot),
1209 LSM_HOOK_INIT(path_link, hook_path_link),
1210 LSM_HOOK_INIT(path_rename, hook_path_rename),
1211 LSM_HOOK_INIT(path_mkdir, hook_path_mkdir),
1212 LSM_HOOK_INIT(path_mknod, hook_path_mknod),
1213 LSM_HOOK_INIT(path_symlink, hook_path_symlink),
1214 LSM_HOOK_INIT(path_unlink, hook_path_unlink),
1215 LSM_HOOK_INIT(path_rmdir, hook_path_rmdir),
1216 LSM_HOOK_INIT(path_truncate, hook_path_truncate),
1218 LSM_HOOK_INIT(file_alloc_security, hook_file_alloc_security),
1219 LSM_HOOK_INIT(file_open, hook_file_open),
1220 LSM_HOOK_INIT(file_truncate, hook_file_truncate),
1223 __init void landlock_add_fs_hooks(void)
1225 security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),