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