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 /* clang-format on */
153 * All access rights that are denied by default whether they are handled or not
154 * by a ruleset/layer. This must be ORed with all ruleset->fs_access_masks[]
155 * entries when we need to get the absolute handled access masks.
157 /* clang-format off */
158 #define ACCESS_INITIALLY_DENIED ( \
159 LANDLOCK_ACCESS_FS_REFER)
160 /* clang-format on */
163 * @path: Should have been checked by get_path_from_fd().
165 int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
166 const struct path *const path,
167 access_mask_t access_rights)
170 struct landlock_object *object;
172 /* Files only get access rights that make sense. */
173 if (!d_is_dir(path->dentry) &&
174 (access_rights | ACCESS_FILE) != ACCESS_FILE)
176 if (WARN_ON_ONCE(ruleset->num_layers != 1))
179 /* Transforms relative access rights to absolute ones. */
181 LANDLOCK_MASK_ACCESS_FS &
182 ~(ruleset->fs_access_masks[0] | ACCESS_INITIALLY_DENIED);
183 object = get_inode_object(d_backing_inode(path->dentry));
185 return PTR_ERR(object);
186 mutex_lock(&ruleset->lock);
187 err = landlock_insert_rule(ruleset, object, access_rights);
188 mutex_unlock(&ruleset->lock);
190 * No need to check for an error because landlock_insert_rule()
191 * increments the refcount for the new object if needed.
193 landlock_put_object(object);
197 /* Access-control management */
200 * The lifetime of the returned rule is tied to @domain.
202 * Returns NULL if no rule is found or if @dentry is negative.
204 static inline const struct landlock_rule *
205 find_rule(const struct landlock_ruleset *const domain,
206 const struct dentry *const dentry)
208 const struct landlock_rule *rule;
209 const struct inode *inode;
211 /* Ignores nonexistent leafs. */
212 if (d_is_negative(dentry))
215 inode = d_backing_inode(dentry);
217 rule = landlock_find_rule(
218 domain, rcu_dereference(landlock_inode(inode)->object));
224 * @layer_masks is read and may be updated according to the access request and
227 * Returns true if the request is allowed (i.e. relevant layer masks for the
228 * request are empty).
231 unmask_layers(const struct landlock_rule *const rule,
232 const access_mask_t access_request,
233 layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS])
237 if (!access_request || !layer_masks)
243 * An access is granted if, for each policy layer, at least one rule
244 * encountered on the pathwalk grants the requested access,
245 * regardless of its position in the layer stack. We must then check
246 * the remaining layers for each inode, from the first added layer to
247 * the last one. When there is multiple requested accesses, for each
248 * policy layer, the full set of requested accesses may not be granted
249 * by only one rule, but by the union (binary OR) of multiple rules.
250 * E.g. /a/b <execute> + /a <read> => /a/b <execute + read>
252 for (layer_level = 0; layer_level < rule->num_layers; layer_level++) {
253 const struct landlock_layer *const layer =
254 &rule->layers[layer_level];
255 const layer_mask_t layer_bit = BIT_ULL(layer->level - 1);
256 const unsigned long access_req = access_request;
257 unsigned long access_bit;
261 * Records in @layer_masks which layer grants access to each
265 for_each_set_bit(access_bit, &access_req,
266 ARRAY_SIZE(*layer_masks)) {
267 if (layer->access & BIT_ULL(access_bit))
268 (*layer_masks)[access_bit] &= ~layer_bit;
269 is_empty = is_empty && !(*layer_masks)[access_bit];
278 * Allows access to pseudo filesystems that will never be mountable (e.g.
279 * sockfs, pipefs), but can still be reachable through
280 * /proc/<pid>/fd/<file-descriptor>
282 static inline bool is_nouser_or_private(const struct dentry *dentry)
284 return (dentry->d_sb->s_flags & SB_NOUSER) ||
285 (d_is_positive(dentry) &&
286 unlikely(IS_PRIVATE(d_backing_inode(dentry))));
289 static inline access_mask_t
290 get_handled_accesses(const struct landlock_ruleset *const domain)
292 access_mask_t access_dom = ACCESS_INITIALLY_DENIED;
295 for (layer_level = 0; layer_level < domain->num_layers; layer_level++)
296 access_dom |= domain->fs_access_masks[layer_level];
297 return access_dom & LANDLOCK_MASK_ACCESS_FS;
301 * init_layer_masks - Initialize layer masks from an access request
303 * Populates @layer_masks such that for each access right in @access_request,
304 * the bits for all the layers are set where this access right is handled.
306 * @domain: The domain that defines the current restrictions.
307 * @access_request: The requested access rights to check.
308 * @layer_masks: The layer masks to populate.
310 * Returns: An access mask where each access right bit is set which is handled
311 * in any of the active layers in @domain.
313 static inline access_mask_t
314 init_layer_masks(const struct landlock_ruleset *const domain,
315 const access_mask_t access_request,
316 layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS])
318 access_mask_t handled_accesses = 0;
321 memset(layer_masks, 0, sizeof(*layer_masks));
322 /* An empty access request can happen because of O_WRONLY | O_RDWR. */
326 /* Saves all handled accesses per layer. */
327 for (layer_level = 0; layer_level < domain->num_layers; layer_level++) {
328 const unsigned long access_req = access_request;
329 unsigned long access_bit;
331 for_each_set_bit(access_bit, &access_req,
332 ARRAY_SIZE(*layer_masks)) {
334 * Artificially handles all initially denied by default
337 if (BIT_ULL(access_bit) &
338 (domain->fs_access_masks[layer_level] |
339 ACCESS_INITIALLY_DENIED)) {
340 (*layer_masks)[access_bit] |=
341 BIT_ULL(layer_level);
342 handled_accesses |= BIT_ULL(access_bit);
346 return handled_accesses;
350 * Check that a destination file hierarchy has more restrictions than a source
351 * file hierarchy. This is only used for link and rename actions.
353 * @layer_masks_child2: Optional child masks.
355 static inline bool no_more_access(
356 const layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS],
357 const layer_mask_t (*const layer_masks_child1)[LANDLOCK_NUM_ACCESS_FS],
358 const bool child1_is_directory,
359 const layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS],
360 const layer_mask_t (*const layer_masks_child2)[LANDLOCK_NUM_ACCESS_FS],
361 const bool child2_is_directory)
363 unsigned long access_bit;
365 for (access_bit = 0; access_bit < ARRAY_SIZE(*layer_masks_parent2);
367 /* Ignores accesses that only make sense for directories. */
368 const bool is_file_access =
369 !!(BIT_ULL(access_bit) & ACCESS_FILE);
371 if (child1_is_directory || is_file_access) {
373 * Checks if the destination restrictions are a
374 * superset of the source ones (i.e. inherited access
375 * rights without child exceptions):
376 * restrictions(parent2) >= restrictions(child1)
378 if ((((*layer_masks_parent1)[access_bit] &
379 (*layer_masks_child1)[access_bit]) |
380 (*layer_masks_parent2)[access_bit]) !=
381 (*layer_masks_parent2)[access_bit])
385 if (!layer_masks_child2)
387 if (child2_is_directory || is_file_access) {
389 * Checks inverted restrictions for RENAME_EXCHANGE:
390 * restrictions(parent1) >= restrictions(child2)
392 if ((((*layer_masks_parent2)[access_bit] &
393 (*layer_masks_child2)[access_bit]) |
394 (*layer_masks_parent1)[access_bit]) !=
395 (*layer_masks_parent1)[access_bit])
403 * Removes @layer_masks accesses that are not requested.
405 * Returns true if the request is allowed, false otherwise.
408 scope_to_request(const access_mask_t access_request,
409 layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS])
411 const unsigned long access_req = access_request;
412 unsigned long access_bit;
414 if (WARN_ON_ONCE(!layer_masks))
417 for_each_clear_bit(access_bit, &access_req, ARRAY_SIZE(*layer_masks))
418 (*layer_masks)[access_bit] = 0;
419 return !memchr_inv(layer_masks, 0, sizeof(*layer_masks));
423 * Returns true if there is at least one access right different than
424 * LANDLOCK_ACCESS_FS_REFER.
427 is_eacces(const layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS],
428 const access_mask_t access_request)
430 unsigned long access_bit;
431 /* LANDLOCK_ACCESS_FS_REFER alone must return -EXDEV. */
432 const unsigned long access_check = access_request &
433 ~LANDLOCK_ACCESS_FS_REFER;
438 for_each_set_bit(access_bit, &access_check, ARRAY_SIZE(*layer_masks)) {
439 if ((*layer_masks)[access_bit])
446 * is_access_to_paths_allowed - Check accesses for requests with a common path
448 * @domain: Domain to check against.
449 * @path: File hierarchy to walk through.
450 * @access_request_parent1: Accesses to check, once @layer_masks_parent1 is
451 * equal to @layer_masks_parent2 (if any). This is tied to the unique
452 * requested path for most actions, or the source in case of a refer action
453 * (i.e. rename or link), or the source and destination in case of
455 * @layer_masks_parent1: Pointer to a matrix of layer masks per access
456 * masks, identifying the layers that forbid a specific access. Bits from
457 * this matrix can be unset according to the @path walk. An empty matrix
458 * means that @domain allows all possible Landlock accesses (i.e. not only
459 * those identified by @access_request_parent1). This matrix can
460 * initially refer to domain layer masks and, when the accesses for the
461 * destination and source are the same, to requested layer masks.
462 * @dentry_child1: Dentry to the initial child of the parent1 path. This
463 * pointer must be NULL for non-refer actions (i.e. not link nor rename).
464 * @access_request_parent2: Similar to @access_request_parent1 but for a
465 * request involving a source and a destination. This refers to the
466 * destination, except in case of RENAME_EXCHANGE where it also refers to
467 * the source. Must be set to 0 when using a simple path request.
468 * @layer_masks_parent2: Similar to @layer_masks_parent1 but for a refer
469 * action. This must be NULL otherwise.
470 * @dentry_child2: Dentry to the initial child of the parent2 path. This
471 * pointer is only set for RENAME_EXCHANGE actions and must be NULL
474 * This helper first checks that the destination has a superset of restrictions
475 * compared to the source (if any) for a common path. Because of
476 * RENAME_EXCHANGE actions, source and destinations may be swapped. It then
477 * checks that the collected accesses and the remaining ones are enough to
481 * - true if the access request is granted;
484 static bool is_access_to_paths_allowed(
485 const struct landlock_ruleset *const domain,
486 const struct path *const path,
487 const access_mask_t access_request_parent1,
488 layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS],
489 const struct dentry *const dentry_child1,
490 const access_mask_t access_request_parent2,
491 layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS],
492 const struct dentry *const dentry_child2)
494 bool allowed_parent1 = false, allowed_parent2 = false, is_dom_check,
495 child1_is_directory = true, child2_is_directory = true;
496 struct path walker_path;
497 access_mask_t access_masked_parent1, access_masked_parent2;
498 layer_mask_t _layer_masks_child1[LANDLOCK_NUM_ACCESS_FS],
499 _layer_masks_child2[LANDLOCK_NUM_ACCESS_FS];
500 layer_mask_t(*layer_masks_child1)[LANDLOCK_NUM_ACCESS_FS] = NULL,
501 (*layer_masks_child2)[LANDLOCK_NUM_ACCESS_FS] = NULL;
503 if (!access_request_parent1 && !access_request_parent2)
505 if (WARN_ON_ONCE(!domain || !path))
507 if (is_nouser_or_private(path->dentry))
509 if (WARN_ON_ONCE(domain->num_layers < 1 || !layer_masks_parent1))
512 if (unlikely(layer_masks_parent2)) {
513 if (WARN_ON_ONCE(!dentry_child1))
516 * For a double request, first check for potential privilege
517 * escalation by looking at domain handled accesses (which are
518 * a superset of the meaningful requested accesses).
520 access_masked_parent1 = access_masked_parent2 =
521 get_handled_accesses(domain);
524 if (WARN_ON_ONCE(dentry_child1 || dentry_child2))
526 /* For a simple request, only check for requested accesses. */
527 access_masked_parent1 = access_request_parent1;
528 access_masked_parent2 = access_request_parent2;
529 is_dom_check = false;
532 if (unlikely(dentry_child1)) {
533 unmask_layers(find_rule(domain, dentry_child1),
534 init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS,
535 &_layer_masks_child1),
536 &_layer_masks_child1);
537 layer_masks_child1 = &_layer_masks_child1;
538 child1_is_directory = d_is_dir(dentry_child1);
540 if (unlikely(dentry_child2)) {
541 unmask_layers(find_rule(domain, dentry_child2),
542 init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS,
543 &_layer_masks_child2),
544 &_layer_masks_child2);
545 layer_masks_child2 = &_layer_masks_child2;
546 child2_is_directory = d_is_dir(dentry_child2);
550 path_get(&walker_path);
552 * We need to walk through all the hierarchy to not miss any relevant
556 struct dentry *parent_dentry;
557 const struct landlock_rule *rule;
560 * If at least all accesses allowed on the destination are
561 * already allowed on the source, respectively if there is at
562 * least as much as restrictions on the destination than on the
563 * source, then we can safely refer files from the source to
564 * the destination without risking a privilege escalation.
565 * This also applies in the case of RENAME_EXCHANGE, which
566 * implies checks on both direction. This is crucial for
567 * standalone multilayered security policies. Furthermore,
568 * this helps avoid policy writers to shoot themselves in the
571 if (unlikely(is_dom_check &&
573 layer_masks_parent1, layer_masks_child1,
574 child1_is_directory, layer_masks_parent2,
576 child2_is_directory))) {
577 allowed_parent1 = scope_to_request(
578 access_request_parent1, layer_masks_parent1);
579 allowed_parent2 = scope_to_request(
580 access_request_parent2, layer_masks_parent2);
582 /* Stops when all accesses are granted. */
583 if (allowed_parent1 && allowed_parent2)
587 * Now, downgrades the remaining checks from domain
588 * handled accesses to requested accesses.
590 is_dom_check = false;
591 access_masked_parent1 = access_request_parent1;
592 access_masked_parent2 = access_request_parent2;
595 rule = find_rule(domain, walker_path.dentry);
596 allowed_parent1 = unmask_layers(rule, access_masked_parent1,
597 layer_masks_parent1);
598 allowed_parent2 = unmask_layers(rule, access_masked_parent2,
599 layer_masks_parent2);
601 /* Stops when a rule from each layer grants access. */
602 if (allowed_parent1 && allowed_parent2)
606 if (walker_path.dentry == walker_path.mnt->mnt_root) {
607 if (follow_up(&walker_path)) {
608 /* Ignores hidden mount points. */
612 * Stops at the real root. Denies access
613 * because not all layers have granted access.
618 if (unlikely(IS_ROOT(walker_path.dentry))) {
620 * Stops at disconnected root directories. Only allows
621 * access to internal filesystems (e.g. nsfs, which is
622 * reachable through /proc/<pid>/ns/<namespace>).
624 allowed_parent1 = allowed_parent2 =
625 !!(walker_path.mnt->mnt_flags & MNT_INTERNAL);
628 parent_dentry = dget_parent(walker_path.dentry);
629 dput(walker_path.dentry);
630 walker_path.dentry = parent_dentry;
632 path_put(&walker_path);
634 return allowed_parent1 && allowed_parent2;
637 static inline int check_access_path(const struct landlock_ruleset *const domain,
638 const struct path *const path,
639 access_mask_t access_request)
641 layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
643 access_request = init_layer_masks(domain, access_request, &layer_masks);
644 if (is_access_to_paths_allowed(domain, path, access_request,
645 &layer_masks, NULL, 0, NULL, NULL))
650 static inline int current_check_access_path(const struct path *const path,
651 const access_mask_t access_request)
653 const struct landlock_ruleset *const dom =
654 landlock_get_current_domain();
658 return check_access_path(dom, path, access_request);
661 static inline access_mask_t get_mode_access(const umode_t mode)
663 switch (mode & S_IFMT) {
665 return LANDLOCK_ACCESS_FS_MAKE_SYM;
667 /* A zero mode translates to S_IFREG. */
669 return LANDLOCK_ACCESS_FS_MAKE_REG;
671 return LANDLOCK_ACCESS_FS_MAKE_DIR;
673 return LANDLOCK_ACCESS_FS_MAKE_CHAR;
675 return LANDLOCK_ACCESS_FS_MAKE_BLOCK;
677 return LANDLOCK_ACCESS_FS_MAKE_FIFO;
679 return LANDLOCK_ACCESS_FS_MAKE_SOCK;
686 static inline access_mask_t maybe_remove(const struct dentry *const dentry)
688 if (d_is_negative(dentry))
690 return d_is_dir(dentry) ? LANDLOCK_ACCESS_FS_REMOVE_DIR :
691 LANDLOCK_ACCESS_FS_REMOVE_FILE;
695 * collect_domain_accesses - Walk through a file path and collect accesses
697 * @domain: Domain to check against.
698 * @mnt_root: Last directory to check.
699 * @dir: Directory to start the walk from.
700 * @layer_masks_dom: Where to store the collected accesses.
702 * This helper is useful to begin a path walk from the @dir directory to a
703 * @mnt_root directory used as a mount point. This mount point is the common
704 * ancestor between the source and the destination of a renamed and linked
705 * file. While walking from @dir to @mnt_root, we record all the domain's
706 * allowed accesses in @layer_masks_dom.
708 * This is similar to is_access_to_paths_allowed() but much simpler because it
709 * only handles walking on the same mount point and only checks one set of
713 * - true if all the domain access rights are allowed for @dir;
714 * - false if the walk reached @mnt_root.
716 static bool collect_domain_accesses(
717 const struct landlock_ruleset *const domain,
718 const struct dentry *const mnt_root, struct dentry *dir,
719 layer_mask_t (*const layer_masks_dom)[LANDLOCK_NUM_ACCESS_FS])
721 unsigned long access_dom;
724 if (WARN_ON_ONCE(!domain || !mnt_root || !dir || !layer_masks_dom))
726 if (is_nouser_or_private(dir))
729 access_dom = init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS,
734 struct dentry *parent_dentry;
736 /* Gets all layers allowing all domain accesses. */
737 if (unmask_layers(find_rule(domain, dir), access_dom,
740 * Stops when all handled accesses are allowed by at
741 * least one rule in each layer.
747 /* We should not reach a root other than @mnt_root. */
748 if (dir == mnt_root || WARN_ON_ONCE(IS_ROOT(dir)))
751 parent_dentry = dget_parent(dir);
760 * current_check_refer_path - Check if a rename or link action is allowed
762 * @old_dentry: File or directory requested to be moved or linked.
763 * @new_dir: Destination parent directory.
764 * @new_dentry: Destination file or directory.
765 * @removable: Sets to true if it is a rename operation.
766 * @exchange: Sets to true if it is a rename operation with RENAME_EXCHANGE.
768 * Because of its unprivileged constraints, Landlock relies on file hierarchies
769 * (and not only inodes) to tie access rights to files. Being able to link or
770 * rename a file hierarchy brings some challenges. Indeed, moving or linking a
771 * file (i.e. creating a new reference to an inode) can have an impact on the
772 * actions allowed for a set of files if it would change its parent directory
773 * (i.e. reparenting).
775 * To avoid trivial access right bypasses, Landlock first checks if the file or
776 * directory requested to be moved would gain new access rights inherited from
777 * its new hierarchy. Before returning any error, Landlock then checks that
778 * the parent source hierarchy and the destination hierarchy would allow the
779 * link or rename action. If it is not the case, an error with EACCES is
780 * returned to inform user space that there is no way to remove or create the
781 * requested source file type. If it should be allowed but the new inherited
782 * access rights would be greater than the source access rights, then the
783 * kernel returns an error with EXDEV. Prioritizing EACCES over EXDEV enables
784 * user space to abort the whole operation if there is no way to do it, or to
785 * manually copy the source to the destination if this remains allowed, e.g.
786 * because file creation is allowed on the destination directory but not direct
789 * To achieve this goal, the kernel needs to compare two file hierarchies: the
790 * one identifying the source file or directory (including itself), and the
791 * destination one. This can be seen as a multilayer partial ordering problem.
792 * The kernel walks through these paths and collects in a matrix the access
793 * rights that are denied per layer. These matrices are then compared to see
794 * if the destination one has more (or the same) restrictions as the source
795 * one. If this is the case, the requested action will not return EXDEV, which
796 * doesn't mean the action is allowed. The parent hierarchy of the source
797 * (i.e. parent directory), and the destination hierarchy must also be checked
798 * to verify that they explicitly allow such action (i.e. referencing,
799 * creation and potentially removal rights). The kernel implementation is then
800 * required to rely on potentially four matrices of access rights: one for the
801 * source file or directory (i.e. the child), a potentially other one for the
802 * other source/destination (in case of RENAME_EXCHANGE), one for the source
803 * parent hierarchy and a last one for the destination hierarchy. These
804 * ephemeral matrices take some space on the stack, which limits the number of
805 * layers to a deemed reasonable number: 16.
808 * - 0 if access is allowed;
809 * - -EXDEV if @old_dentry would inherit new access rights from @new_dir;
810 * - -EACCES if file removal or creation is denied.
812 static int current_check_refer_path(struct dentry *const old_dentry,
813 const struct path *const new_dir,
814 struct dentry *const new_dentry,
815 const bool removable, const bool exchange)
817 const struct landlock_ruleset *const dom =
818 landlock_get_current_domain();
819 bool allow_parent1, allow_parent2;
820 access_mask_t access_request_parent1, access_request_parent2;
822 layer_mask_t layer_masks_parent1[LANDLOCK_NUM_ACCESS_FS],
823 layer_masks_parent2[LANDLOCK_NUM_ACCESS_FS];
827 if (WARN_ON_ONCE(dom->num_layers < 1))
829 if (unlikely(d_is_negative(old_dentry)))
832 if (unlikely(d_is_negative(new_dentry)))
834 access_request_parent1 =
835 get_mode_access(d_backing_inode(new_dentry)->i_mode);
837 access_request_parent1 = 0;
839 access_request_parent2 =
840 get_mode_access(d_backing_inode(old_dentry)->i_mode);
842 access_request_parent1 |= maybe_remove(old_dentry);
843 access_request_parent2 |= maybe_remove(new_dentry);
846 /* The mount points are the same for old and new paths, cf. EXDEV. */
847 if (old_dentry->d_parent == new_dir->dentry) {
849 * The LANDLOCK_ACCESS_FS_REFER access right is not required
850 * for same-directory referer (i.e. no reparenting).
852 access_request_parent1 = init_layer_masks(
853 dom, access_request_parent1 | access_request_parent2,
854 &layer_masks_parent1);
855 if (is_access_to_paths_allowed(
856 dom, new_dir, access_request_parent1,
857 &layer_masks_parent1, NULL, 0, NULL, NULL))
862 access_request_parent1 |= LANDLOCK_ACCESS_FS_REFER;
863 access_request_parent2 |= LANDLOCK_ACCESS_FS_REFER;
865 /* Saves the common mount point. */
866 mnt_dir.mnt = new_dir->mnt;
867 mnt_dir.dentry = new_dir->mnt->mnt_root;
869 /* new_dir->dentry is equal to new_dentry->d_parent */
870 allow_parent1 = collect_domain_accesses(dom, mnt_dir.dentry,
871 old_dentry->d_parent,
872 &layer_masks_parent1);
873 allow_parent2 = collect_domain_accesses(
874 dom, mnt_dir.dentry, new_dir->dentry, &layer_masks_parent2);
876 if (allow_parent1 && allow_parent2)
880 * To be able to compare source and destination domain access rights,
881 * take into account the @old_dentry access rights aggregated with its
882 * parent access rights. This will be useful to compare with the
883 * destination parent access rights.
885 if (is_access_to_paths_allowed(
886 dom, &mnt_dir, access_request_parent1, &layer_masks_parent1,
887 old_dentry, access_request_parent2, &layer_masks_parent2,
888 exchange ? new_dentry : NULL))
892 * This prioritizes EACCES over EXDEV for all actions, including
893 * renames with RENAME_EXCHANGE.
895 if (likely(is_eacces(&layer_masks_parent1, access_request_parent1) ||
896 is_eacces(&layer_masks_parent2, access_request_parent2)))
900 * Gracefully forbids reparenting if the destination directory
901 * hierarchy is not a superset of restrictions of the source directory
902 * hierarchy, or if LANDLOCK_ACCESS_FS_REFER is not allowed by the
903 * source or the destination.
910 static void hook_inode_free_security(struct inode *const inode)
913 * All inodes must already have been untied from their object by
914 * release_inode() or hook_sb_delete().
916 WARN_ON_ONCE(landlock_inode(inode)->object);
919 /* Super-block hooks */
922 * Release the inodes used in a security policy.
924 * Cf. fsnotify_unmount_inodes() and invalidate_inodes()
926 static void hook_sb_delete(struct super_block *const sb)
928 struct inode *inode, *prev_inode = NULL;
930 if (!landlock_initialized)
933 spin_lock(&sb->s_inode_list_lock);
934 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
935 struct landlock_object *object;
937 /* Only handles referenced inodes. */
938 if (!atomic_read(&inode->i_count))
942 * Protects against concurrent modification of inode (e.g.
943 * from get_inode_object()).
945 spin_lock(&inode->i_lock);
947 * Checks I_FREEING and I_WILL_FREE to protect against a race
948 * condition when release_inode() just called iput(), which
949 * could lead to a NULL dereference of inode->security or a
950 * second call to iput() for the same Landlock object. Also
951 * checks I_NEW because such inode cannot be tied to an object.
953 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
954 spin_unlock(&inode->i_lock);
959 object = rcu_dereference(landlock_inode(inode)->object);
962 spin_unlock(&inode->i_lock);
965 /* Keeps a reference to this inode until the next loop walk. */
967 spin_unlock(&inode->i_lock);
970 * If there is no concurrent release_inode() ongoing, then we
971 * are in charge of calling iput() on this inode, otherwise we
972 * will just wait for it to finish.
974 spin_lock(&object->lock);
975 if (object->underobj == inode) {
976 object->underobj = NULL;
977 spin_unlock(&object->lock);
981 * Because object->underobj was not NULL,
982 * release_inode() and get_inode_object() guarantee
983 * that it is safe to reset
984 * landlock_inode(inode)->object while it is not NULL.
985 * It is therefore not necessary to lock inode->i_lock.
987 rcu_assign_pointer(landlock_inode(inode)->object, NULL);
989 * At this point, we own the ihold() reference that was
990 * originally set up by get_inode_object() and the
991 * __iget() reference that we just set in this loop
992 * walk. Therefore the following call to iput() will
993 * not sleep nor drop the inode because there is now at
994 * least two references to it.
998 spin_unlock(&object->lock);
1004 * At this point, we still own the __iget() reference
1005 * that we just set in this loop walk. Therefore we
1006 * can drop the list lock and know that the inode won't
1007 * disappear from under us until the next loop walk.
1009 spin_unlock(&sb->s_inode_list_lock);
1011 * We can now actually put the inode reference from the
1012 * previous loop walk, which is not needed anymore.
1016 spin_lock(&sb->s_inode_list_lock);
1020 spin_unlock(&sb->s_inode_list_lock);
1022 /* Puts the inode reference from the last loop walk, if any. */
1025 /* Waits for pending iput() in release_inode(). */
1026 wait_var_event(&landlock_superblock(sb)->inode_refs,
1027 !atomic_long_read(&landlock_superblock(sb)->inode_refs));
1031 * Because a Landlock security policy is defined according to the filesystem
1032 * topology (i.e. the mount namespace), changing it may grant access to files
1033 * not previously allowed.
1035 * To make it simple, deny any filesystem topology modification by landlocked
1036 * processes. Non-landlocked processes may still change the namespace of a
1037 * landlocked process, but this kind of threat must be handled by a system-wide
1038 * access-control security policy.
1040 * This could be lifted in the future if Landlock can safely handle mount
1041 * namespace updates requested by a landlocked process. Indeed, we could
1042 * update the current domain (which is currently read-only) by taking into
1043 * account the accesses of the source and the destination of a new mount point.
1044 * However, it would also require to make all the child domains dynamically
1045 * inherit these new constraints. Anyway, for backward compatibility reasons,
1046 * a dedicated user space option would be required (e.g. as a ruleset flag).
1048 static int hook_sb_mount(const char *const dev_name,
1049 const struct path *const path, const char *const type,
1050 const unsigned long flags, void *const data)
1052 if (!landlock_get_current_domain())
1057 static int hook_move_mount(const struct path *const from_path,
1058 const struct path *const to_path)
1060 if (!landlock_get_current_domain())
1066 * Removing a mount point may reveal a previously hidden file hierarchy, which
1067 * may then grant access to files, which may have previously been forbidden.
1069 static int hook_sb_umount(struct vfsmount *const mnt, const int flags)
1071 if (!landlock_get_current_domain())
1076 static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts)
1078 if (!landlock_get_current_domain())
1084 * pivot_root(2), like mount(2), changes the current mount namespace. It must
1085 * then be forbidden for a landlocked process.
1087 * However, chroot(2) may be allowed because it only changes the relative root
1088 * directory of the current process. Moreover, it can be used to restrict the
1089 * view of the filesystem.
1091 static int hook_sb_pivotroot(const struct path *const old_path,
1092 const struct path *const new_path)
1094 if (!landlock_get_current_domain())
1101 static int hook_path_link(struct dentry *const old_dentry,
1102 const struct path *const new_dir,
1103 struct dentry *const new_dentry)
1105 return current_check_refer_path(old_dentry, new_dir, new_dentry, false,
1109 static int hook_path_rename(const struct path *const old_dir,
1110 struct dentry *const old_dentry,
1111 const struct path *const new_dir,
1112 struct dentry *const new_dentry,
1113 const unsigned int flags)
1115 /* old_dir refers to old_dentry->d_parent and new_dir->mnt */
1116 return current_check_refer_path(old_dentry, new_dir, new_dentry, true,
1117 !!(flags & RENAME_EXCHANGE));
1120 static int hook_path_mkdir(const struct path *const dir,
1121 struct dentry *const dentry, const umode_t mode)
1123 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_DIR);
1126 static int hook_path_mknod(const struct path *const dir,
1127 struct dentry *const dentry, const umode_t mode,
1128 const unsigned int dev)
1130 const struct landlock_ruleset *const dom =
1131 landlock_get_current_domain();
1135 return check_access_path(dom, dir, get_mode_access(mode));
1138 static int hook_path_symlink(const struct path *const dir,
1139 struct dentry *const dentry,
1140 const char *const old_name)
1142 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_SYM);
1145 static int hook_path_unlink(const struct path *const dir,
1146 struct dentry *const dentry)
1148 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_FILE);
1151 static int hook_path_rmdir(const struct path *const dir,
1152 struct dentry *const dentry)
1154 return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_DIR);
1159 static inline access_mask_t get_file_access(const struct file *const file)
1161 access_mask_t access = 0;
1163 if (file->f_mode & FMODE_READ) {
1164 /* A directory can only be opened in read mode. */
1165 if (S_ISDIR(file_inode(file)->i_mode))
1166 return LANDLOCK_ACCESS_FS_READ_DIR;
1167 access = LANDLOCK_ACCESS_FS_READ_FILE;
1169 if (file->f_mode & FMODE_WRITE)
1170 access |= LANDLOCK_ACCESS_FS_WRITE_FILE;
1171 /* __FMODE_EXEC is indeed part of f_flags, not f_mode. */
1172 if (file->f_flags & __FMODE_EXEC)
1173 access |= LANDLOCK_ACCESS_FS_EXECUTE;
1177 static int hook_file_open(struct file *const file)
1179 const struct landlock_ruleset *const dom =
1180 landlock_get_current_domain();
1185 * Because a file may be opened with O_PATH, get_file_access() may
1186 * return 0. This case will be handled with a future Landlock
1189 return check_access_path(dom, &file->f_path, get_file_access(file));
1192 static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = {
1193 LSM_HOOK_INIT(inode_free_security, hook_inode_free_security),
1195 LSM_HOOK_INIT(sb_delete, hook_sb_delete),
1196 LSM_HOOK_INIT(sb_mount, hook_sb_mount),
1197 LSM_HOOK_INIT(move_mount, hook_move_mount),
1198 LSM_HOOK_INIT(sb_umount, hook_sb_umount),
1199 LSM_HOOK_INIT(sb_remount, hook_sb_remount),
1200 LSM_HOOK_INIT(sb_pivotroot, hook_sb_pivotroot),
1202 LSM_HOOK_INIT(path_link, hook_path_link),
1203 LSM_HOOK_INIT(path_rename, hook_path_rename),
1204 LSM_HOOK_INIT(path_mkdir, hook_path_mkdir),
1205 LSM_HOOK_INIT(path_mknod, hook_path_mknod),
1206 LSM_HOOK_INIT(path_symlink, hook_path_symlink),
1207 LSM_HOOK_INIT(path_unlink, hook_path_unlink),
1208 LSM_HOOK_INIT(path_rmdir, hook_path_rmdir),
1210 LSM_HOOK_INIT(file_open, hook_file_open),
1213 __init void landlock_add_fs_hooks(void)
1215 security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),