xref: /linux/security/landlock/ruleset.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Landlock LSM - Ruleset management
4  *
5  * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6  * Copyright © 2018-2020 ANSSI
7  */
8 
9 #include <linux/bits.h>
10 #include <linux/bug.h>
11 #include <linux/cleanup.h>
12 #include <linux/compiler_types.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/lockdep.h>
17 #include <linux/mutex.h>
18 #include <linux/overflow.h>
19 #include <linux/rbtree.h>
20 #include <linux/refcount.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/workqueue.h>
24 #include <uapi/linux/landlock.h>
25 
26 #include "access.h"
27 #include "domain.h"
28 #include "limits.h"
29 #include "object.h"
30 #include "ruleset.h"
31 
32 static struct landlock_ruleset *create_ruleset(const u32 num_layers)
33 {
34 	struct landlock_ruleset *new_ruleset;
35 
36 	new_ruleset = kzalloc_flex(*new_ruleset, access_masks, num_layers,
37 				   GFP_KERNEL_ACCOUNT);
38 	if (!new_ruleset)
39 		return ERR_PTR(-ENOMEM);
40 	refcount_set(&new_ruleset->usage, 1);
41 	mutex_init(&new_ruleset->lock);
42 	new_ruleset->root_inode = RB_ROOT;
43 
44 #if IS_ENABLED(CONFIG_INET)
45 	new_ruleset->root_net_port = RB_ROOT;
46 #endif /* IS_ENABLED(CONFIG_INET) */
47 
48 	new_ruleset->num_layers = num_layers;
49 	/*
50 	 * hierarchy = NULL
51 	 * num_rules = 0
52 	 * access_masks[] = 0
53 	 */
54 	return new_ruleset;
55 }
56 
57 struct landlock_ruleset *
58 landlock_create_ruleset(const access_mask_t fs_access_mask,
59 			const access_mask_t net_access_mask,
60 			const access_mask_t scope_mask)
61 {
62 	struct landlock_ruleset *new_ruleset;
63 
64 	/* Informs about useless ruleset. */
65 	if (!fs_access_mask && !net_access_mask && !scope_mask)
66 		return ERR_PTR(-ENOMSG);
67 	new_ruleset = create_ruleset(1);
68 	if (IS_ERR(new_ruleset))
69 		return new_ruleset;
70 	if (fs_access_mask)
71 		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
72 	if (net_access_mask)
73 		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
74 	if (scope_mask)
75 		landlock_add_scope_mask(new_ruleset, scope_mask, 0);
76 	return new_ruleset;
77 }
78 
79 static void build_check_rule(void)
80 {
81 	const struct landlock_rule rule = {
82 		.num_layers = ~0,
83 	};
84 
85 	/*
86 	 * Checks that .num_layers is large enough for at least
87 	 * LANDLOCK_MAX_NUM_LAYERS layers.
88 	 */
89 	BUILD_BUG_ON(rule.num_layers < LANDLOCK_MAX_NUM_LAYERS);
90 }
91 
92 static bool is_object_pointer(const enum landlock_key_type key_type)
93 {
94 	switch (key_type) {
95 	case LANDLOCK_KEY_INODE:
96 		return true;
97 
98 #if IS_ENABLED(CONFIG_INET)
99 	case LANDLOCK_KEY_NET_PORT:
100 		return false;
101 #endif /* IS_ENABLED(CONFIG_INET) */
102 
103 	default:
104 		WARN_ON_ONCE(1);
105 		return false;
106 	}
107 }
108 
109 static struct landlock_rule *
110 create_rule(const struct landlock_id id,
111 	    const struct landlock_layer (*layers)[], const u32 num_layers,
112 	    const struct landlock_layer *const new_layer)
113 {
114 	struct landlock_rule *new_rule;
115 	u32 new_num_layers;
116 
117 	build_check_rule();
118 	if (new_layer) {
119 		/* Should already be checked by landlock_merge_ruleset(). */
120 		if (WARN_ON_ONCE(num_layers >= LANDLOCK_MAX_NUM_LAYERS))
121 			return ERR_PTR(-E2BIG);
122 		new_num_layers = num_layers + 1;
123 	} else {
124 		new_num_layers = num_layers;
125 	}
126 	new_rule = kzalloc_flex(*new_rule, layers, new_num_layers,
127 				GFP_KERNEL_ACCOUNT);
128 	if (!new_rule)
129 		return ERR_PTR(-ENOMEM);
130 	RB_CLEAR_NODE(&new_rule->node);
131 	if (is_object_pointer(id.type)) {
132 		/* This should have been caught by insert_rule(). */
133 		WARN_ON_ONCE(!id.key.object);
134 		landlock_get_object(id.key.object);
135 	}
136 
137 	new_rule->key = id.key;
138 	new_rule->num_layers = new_num_layers;
139 	/* Copies the original layer stack. */
140 	memcpy(new_rule->layers, layers,
141 	       flex_array_size(new_rule, layers, num_layers));
142 	if (new_layer)
143 		/* Adds a copy of @new_layer on the layer stack. */
144 		new_rule->layers[new_rule->num_layers - 1] = *new_layer;
145 	return new_rule;
146 }
147 
148 static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
149 				const enum landlock_key_type key_type)
150 {
151 	switch (key_type) {
152 	case LANDLOCK_KEY_INODE:
153 		return &ruleset->root_inode;
154 
155 #if IS_ENABLED(CONFIG_INET)
156 	case LANDLOCK_KEY_NET_PORT:
157 		return &ruleset->root_net_port;
158 #endif /* IS_ENABLED(CONFIG_INET) */
159 
160 	default:
161 		WARN_ON_ONCE(1);
162 		return ERR_PTR(-EINVAL);
163 	}
164 }
165 
166 static void free_rule(struct landlock_rule *const rule,
167 		      const enum landlock_key_type key_type)
168 {
169 	might_sleep();
170 	if (!rule)
171 		return;
172 	if (is_object_pointer(key_type))
173 		landlock_put_object(rule->key.object);
174 	kfree(rule);
175 }
176 
177 static void build_check_ruleset(void)
178 {
179 	const struct landlock_ruleset ruleset = {
180 		.num_rules = ~0,
181 		.num_layers = ~0,
182 	};
183 
184 	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
185 	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
186 }
187 
188 /**
189  * insert_rule - Create and insert a rule in a ruleset
190  *
191  * @ruleset: The ruleset to be updated.
192  * @id: The ID to build the new rule with.  The underlying kernel object, if
193  *      any, must be held by the caller.
194  * @layers: One or multiple layers to be copied into the new rule.
195  * @num_layers: The number of @layers entries.
196  *
197  * When user space requests to add a new rule to a ruleset, @layers only
198  * contains one entry and this entry is not assigned to any level.  In this
199  * case, the new rule will extend @ruleset, similarly to a boolean OR between
200  * access rights.
201  *
202  * When merging a ruleset in a domain, or copying a domain, @layers will be
203  * added to @ruleset as new constraints, similarly to a boolean AND between
204  * access rights.
205  *
206  * Return: 0 on success, -errno on failure.
207  */
208 static int insert_rule(struct landlock_ruleset *const ruleset,
209 		       const struct landlock_id id,
210 		       const struct landlock_layer (*layers)[],
211 		       const size_t num_layers)
212 {
213 	struct rb_node **walker_node;
214 	struct rb_node *parent_node = NULL;
215 	struct landlock_rule *new_rule;
216 	struct rb_root *root;
217 
218 	might_sleep();
219 	lockdep_assert_held(&ruleset->lock);
220 	if (WARN_ON_ONCE(!layers))
221 		return -ENOENT;
222 
223 	if (is_object_pointer(id.type) && WARN_ON_ONCE(!id.key.object))
224 		return -ENOENT;
225 
226 	root = get_root(ruleset, id.type);
227 	if (IS_ERR(root))
228 		return PTR_ERR(root);
229 
230 	walker_node = &root->rb_node;
231 	while (*walker_node) {
232 		struct landlock_rule *const this =
233 			rb_entry(*walker_node, struct landlock_rule, node);
234 
235 		if (this->key.data != id.key.data) {
236 			parent_node = *walker_node;
237 			if (this->key.data < id.key.data)
238 				walker_node = &((*walker_node)->rb_right);
239 			else
240 				walker_node = &((*walker_node)->rb_left);
241 			continue;
242 		}
243 
244 		/* Only a single-level layer should match an existing rule. */
245 		if (WARN_ON_ONCE(num_layers != 1))
246 			return -EINVAL;
247 
248 		/* If there is a matching rule, updates it. */
249 		if ((*layers)[0].level == 0) {
250 			/*
251 			 * Extends access rights when the request comes from
252 			 * landlock_add_rule(2), i.e. @ruleset is not a domain.
253 			 */
254 			if (WARN_ON_ONCE(this->num_layers != 1))
255 				return -EINVAL;
256 			if (WARN_ON_ONCE(this->layers[0].level != 0))
257 				return -EINVAL;
258 			this->layers[0].access |= (*layers)[0].access;
259 			this->layers[0].flags.quiet |= (*layers)[0].flags.quiet;
260 			return 0;
261 		}
262 
263 		if (WARN_ON_ONCE(this->layers[0].level == 0))
264 			return -EINVAL;
265 
266 		/*
267 		 * Intersects access rights when it is a merge between a
268 		 * ruleset and a domain.
269 		 */
270 		new_rule = create_rule(id, &this->layers, this->num_layers,
271 				       &(*layers)[0]);
272 		if (IS_ERR(new_rule))
273 			return PTR_ERR(new_rule);
274 		rb_replace_node(&this->node, &new_rule->node, root);
275 		free_rule(this, id.type);
276 		return 0;
277 	}
278 
279 	/* There is no match for @id. */
280 	build_check_ruleset();
281 	if (ruleset->num_rules >= LANDLOCK_MAX_NUM_RULES)
282 		return -E2BIG;
283 	new_rule = create_rule(id, layers, num_layers, NULL);
284 	if (IS_ERR(new_rule))
285 		return PTR_ERR(new_rule);
286 	rb_link_node(&new_rule->node, parent_node, walker_node);
287 	rb_insert_color(&new_rule->node, root);
288 	ruleset->num_rules++;
289 	return 0;
290 }
291 
292 static void build_check_layer(void)
293 {
294 	const struct landlock_layer layer = {
295 		.level = ~0,
296 		.access = ~0,
297 	};
298 
299 	/*
300 	 * Checks that .level and .access are large enough to contain their expected
301 	 * maximum values.
302 	 */
303 	BUILD_BUG_ON(layer.level < LANDLOCK_MAX_NUM_LAYERS);
304 	BUILD_BUG_ON(layer.access < LANDLOCK_MASK_ACCESS_FS);
305 }
306 
307 /* @ruleset must be locked by the caller. */
308 int landlock_insert_rule(struct landlock_ruleset *const ruleset,
309 			 const struct landlock_id id,
310 			 const access_mask_t access, const u32 flags)
311 {
312 	struct landlock_layer layers[] = { {
313 		.access = access,
314 		/* When @level is zero, insert_rule() extends @ruleset. */
315 		.level = 0,
316 		.flags = {
317 			.quiet = !!(flags & LANDLOCK_ADD_RULE_QUIET),
318 		},
319 	} };
320 
321 	build_check_layer();
322 	return insert_rule(ruleset, id, &layers, ARRAY_SIZE(layers));
323 }
324 
325 static int merge_tree(struct landlock_ruleset *const dst,
326 		      struct landlock_ruleset *const src,
327 		      const enum landlock_key_type key_type)
328 {
329 	struct landlock_rule *walker_rule, *next_rule;
330 	struct rb_root *src_root;
331 	int err = 0;
332 
333 	might_sleep();
334 	lockdep_assert_held(&dst->lock);
335 	lockdep_assert_held(&src->lock);
336 
337 	src_root = get_root(src, key_type);
338 	if (IS_ERR(src_root))
339 		return PTR_ERR(src_root);
340 
341 	/* Merges the @src tree. */
342 	rbtree_postorder_for_each_entry_safe(walker_rule, next_rule, src_root,
343 					     node) {
344 		struct landlock_layer layers[] = { {
345 			.level = dst->num_layers,
346 		} };
347 		const struct landlock_id id = {
348 			.key = walker_rule->key,
349 			.type = key_type,
350 		};
351 
352 		if (WARN_ON_ONCE(walker_rule->num_layers != 1))
353 			return -EINVAL;
354 
355 		if (WARN_ON_ONCE(walker_rule->layers[0].level != 0))
356 			return -EINVAL;
357 
358 		layers[0].access = walker_rule->layers[0].access;
359 		layers[0].flags = walker_rule->layers[0].flags;
360 
361 		err = insert_rule(dst, id, &layers, ARRAY_SIZE(layers));
362 		if (err)
363 			return err;
364 	}
365 	return err;
366 }
367 
368 static int merge_ruleset(struct landlock_ruleset *const dst,
369 			 struct landlock_ruleset *const src)
370 {
371 	int err = 0;
372 
373 	might_sleep();
374 	/* Should already be checked by landlock_merge_ruleset() */
375 	if (WARN_ON_ONCE(!src))
376 		return 0;
377 	/* Only merge into a domain. */
378 	if (WARN_ON_ONCE(!dst || !dst->hierarchy))
379 		return -EINVAL;
380 
381 	/* Locks @dst first because we are its only owner. */
382 	mutex_lock(&dst->lock);
383 	mutex_lock_nested(&src->lock, SINGLE_DEPTH_NESTING);
384 
385 	/* Stacks the new layer. */
386 	if (WARN_ON_ONCE(src->num_layers != 1 || dst->num_layers < 1)) {
387 		err = -EINVAL;
388 		goto out_unlock;
389 	}
390 	dst->access_masks[dst->num_layers - 1] =
391 		landlock_upgrade_handled_access_masks(src->access_masks[0]);
392 
393 	/* Merges the @src inode tree. */
394 	err = merge_tree(dst, src, LANDLOCK_KEY_INODE);
395 	if (err)
396 		goto out_unlock;
397 
398 #if IS_ENABLED(CONFIG_INET)
399 	/* Merges the @src network port tree. */
400 	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
401 	if (err)
402 		goto out_unlock;
403 #endif /* IS_ENABLED(CONFIG_INET) */
404 
405 out_unlock:
406 	mutex_unlock(&src->lock);
407 	mutex_unlock(&dst->lock);
408 	return err;
409 }
410 
411 static int inherit_tree(struct landlock_ruleset *const parent,
412 			struct landlock_ruleset *const child,
413 			const enum landlock_key_type key_type)
414 {
415 	struct landlock_rule *walker_rule, *next_rule;
416 	struct rb_root *parent_root;
417 	int err = 0;
418 
419 	might_sleep();
420 	lockdep_assert_held(&parent->lock);
421 	lockdep_assert_held(&child->lock);
422 
423 	parent_root = get_root(parent, key_type);
424 	if (IS_ERR(parent_root))
425 		return PTR_ERR(parent_root);
426 
427 	/* Copies the @parent inode or network tree. */
428 	rbtree_postorder_for_each_entry_safe(walker_rule, next_rule,
429 					     parent_root, node) {
430 		const struct landlock_id id = {
431 			.key = walker_rule->key,
432 			.type = key_type,
433 		};
434 
435 		err = insert_rule(child, id, &walker_rule->layers,
436 				  walker_rule->num_layers);
437 		if (err)
438 			return err;
439 	}
440 	return err;
441 }
442 
443 static int inherit_ruleset(struct landlock_ruleset *const parent,
444 			   struct landlock_ruleset *const child)
445 {
446 	int err = 0;
447 
448 	might_sleep();
449 	if (!parent)
450 		return 0;
451 
452 	/* Locks @child first because we are its only owner. */
453 	mutex_lock(&child->lock);
454 	mutex_lock_nested(&parent->lock, SINGLE_DEPTH_NESTING);
455 
456 	/* Copies the @parent inode tree. */
457 	err = inherit_tree(parent, child, LANDLOCK_KEY_INODE);
458 	if (err)
459 		goto out_unlock;
460 
461 #if IS_ENABLED(CONFIG_INET)
462 	/* Copies the @parent network port tree. */
463 	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
464 	if (err)
465 		goto out_unlock;
466 #endif /* IS_ENABLED(CONFIG_INET) */
467 
468 	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
469 		err = -EINVAL;
470 		goto out_unlock;
471 	}
472 	/* Copies the parent layer stack and leaves a space for the new layer. */
473 	memcpy(child->access_masks, parent->access_masks,
474 	       flex_array_size(parent, access_masks, parent->num_layers));
475 
476 	if (WARN_ON_ONCE(!parent->hierarchy)) {
477 		err = -EINVAL;
478 		goto out_unlock;
479 	}
480 	landlock_get_hierarchy(parent->hierarchy);
481 	child->hierarchy->parent = parent->hierarchy;
482 
483 out_unlock:
484 	mutex_unlock(&parent->lock);
485 	mutex_unlock(&child->lock);
486 	return err;
487 }
488 
489 static void free_ruleset(struct landlock_ruleset *const ruleset)
490 {
491 	struct landlock_rule *freeme, *next;
492 
493 	might_sleep();
494 	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
495 					     node)
496 		free_rule(freeme, LANDLOCK_KEY_INODE);
497 
498 #if IS_ENABLED(CONFIG_INET)
499 	rbtree_postorder_for_each_entry_safe(freeme, next,
500 					     &ruleset->root_net_port, node)
501 		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
502 #endif /* IS_ENABLED(CONFIG_INET) */
503 
504 	landlock_put_hierarchy(ruleset->hierarchy);
505 	kfree(ruleset);
506 }
507 
508 void landlock_put_ruleset(struct landlock_ruleset *const ruleset)
509 {
510 	might_sleep();
511 	if (ruleset && refcount_dec_and_test(&ruleset->usage))
512 		free_ruleset(ruleset);
513 }
514 
515 static void free_ruleset_work(struct work_struct *const work)
516 {
517 	struct landlock_ruleset *ruleset;
518 
519 	ruleset = container_of(work, struct landlock_ruleset, work_free);
520 	free_ruleset(ruleset);
521 }
522 
523 /* Only called by hook_cred_free(). */
524 void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset)
525 {
526 	if (ruleset && refcount_dec_and_test(&ruleset->usage)) {
527 		INIT_WORK(&ruleset->work_free, free_ruleset_work);
528 		schedule_work(&ruleset->work_free);
529 	}
530 }
531 
532 /**
533  * landlock_merge_ruleset - Merge a ruleset with a domain
534  *
535  * @parent: Parent domain.
536  * @ruleset: New ruleset to be merged.
537  *
538  * The current task is requesting to be restricted.  The subjective credentials
539  * must not be in an overridden state. cf. landlock_init_hierarchy_log().
540  *
541  * Return: A new domain merging @parent and @ruleset on success, or ERR_PTR()
542  * on failure.  If @parent is NULL, the new domain duplicates @ruleset.
543  */
544 struct landlock_ruleset *
545 landlock_merge_ruleset(struct landlock_ruleset *const parent,
546 		       struct landlock_ruleset *const ruleset)
547 {
548 	struct landlock_ruleset *new_dom __free(landlock_put_ruleset) = NULL;
549 	u32 num_layers;
550 	int err;
551 
552 	might_sleep();
553 	if (WARN_ON_ONCE(!ruleset || parent == ruleset))
554 		return ERR_PTR(-EINVAL);
555 
556 	if (parent) {
557 		if (parent->num_layers >= LANDLOCK_MAX_NUM_LAYERS)
558 			return ERR_PTR(-E2BIG);
559 		num_layers = parent->num_layers + 1;
560 	} else {
561 		num_layers = 1;
562 	}
563 
564 	/* Creates a new domain... */
565 	new_dom = create_ruleset(num_layers);
566 	if (IS_ERR(new_dom))
567 		return new_dom;
568 
569 	new_dom->hierarchy =
570 		kzalloc_obj(*new_dom->hierarchy, GFP_KERNEL_ACCOUNT);
571 	if (!new_dom->hierarchy)
572 		return ERR_PTR(-ENOMEM);
573 
574 	refcount_set(&new_dom->hierarchy->usage, 1);
575 
576 	/* ...as a child of @parent... */
577 	err = inherit_ruleset(parent, new_dom);
578 	if (err)
579 		return ERR_PTR(err);
580 
581 	/* ...and including @ruleset. */
582 	err = merge_ruleset(new_dom, ruleset);
583 	if (err)
584 		return ERR_PTR(err);
585 
586 	err = landlock_init_hierarchy_log(new_dom->hierarchy);
587 	if (err)
588 		return ERR_PTR(err);
589 
590 #ifdef CONFIG_AUDIT
591 	new_dom->hierarchy->quiet_masks = ruleset->quiet_masks;
592 #endif /* CONFIG_AUDIT */
593 
594 	return no_free_ptr(new_dom);
595 }
596 
597 /*
598  * The returned access has the same lifetime as @ruleset.
599  */
600 const struct landlock_rule *
601 landlock_find_rule(const struct landlock_ruleset *const ruleset,
602 		   const struct landlock_id id)
603 {
604 	const struct rb_root *root;
605 	const struct rb_node *node;
606 
607 	root = get_root((struct landlock_ruleset *)ruleset, id.type);
608 	if (IS_ERR(root))
609 		return NULL;
610 	node = root->rb_node;
611 
612 	while (node) {
613 		struct landlock_rule *this =
614 			rb_entry(node, struct landlock_rule, node);
615 
616 		if (this->key.data == id.key.data)
617 			return this;
618 		if (this->key.data < id.key.data)
619 			node = node->rb_right;
620 		else
621 			node = node->rb_left;
622 	}
623 	return NULL;
624 }
625 
626 /**
627  * landlock_unmask_layers - Remove the access rights in @masks
628  *                          which are granted in @rule
629  *
630  * Updates the set of (per-layer) unfulfilled access rights @masks
631  * so that all the access rights granted in @rule are removed from it
632  * (because they are now fulfilled).
633  *
634  * @rule: A rule that grants a set of access rights for each layer
635  * @masks: A matrix of unfulfilled access rights for each layer
636  *
637  * Return: True if the request is allowed (i.e. the access rights granted all
638  * remaining unfulfilled access rights and masks has no leftover set bits).
639  */
640 bool landlock_unmask_layers(const struct landlock_rule *const rule,
641 			    struct layer_masks *masks)
642 {
643 	if (!masks)
644 		return true;
645 	if (!rule)
646 		return false;
647 
648 	/*
649 	 * An access is granted if, for each policy layer, at least one rule
650 	 * encountered on the pathwalk grants the requested access,
651 	 * regardless of its position in the layer stack.  We must then check
652 	 * the remaining layers for each inode, from the first added layer to
653 	 * the last one.  When there is multiple requested accesses, for each
654 	 * policy layer, the full set of requested accesses may not be granted
655 	 * by only one rule, but by the union (binary OR) of multiple rules.
656 	 * E.g. /a/b <execute> + /a <read> => /a/b <execute + read>
657 	 */
658 	for (size_t i = 0; i < rule->num_layers; i++) {
659 		const struct landlock_layer *const layer = &rule->layers[i];
660 
661 		/* Clear the bits where the layer in the rule grants access. */
662 		masks->layers[layer->level - 1].access &= ~layer->access;
663 
664 #ifdef CONFIG_AUDIT
665 		/* Collect rule flags for each layer. */
666 		if (layer->flags.quiet)
667 			masks->layers[layer->level - 1].quiet = true;
668 #endif /* CONFIG_AUDIT */
669 	}
670 
671 	for (size_t i = 0; i < ARRAY_SIZE(masks->layers); i++) {
672 		if (masks->layers[i].access)
673 			return false;
674 	}
675 	return true;
676 }
677 
678 typedef access_mask_t
679 get_access_mask_t(const struct landlock_ruleset *const ruleset,
680 		  const u16 layer_level);
681 
682 /**
683  * landlock_init_layer_masks - Initialize layer masks from an access request
684  *
685  * Populates @masks such that for each access right in @access_request, the bits
686  * for all the layers are set where this access right is handled.  Rule flags
687  * are also zeroed.
688  *
689  * @domain: The domain that defines the current restrictions.
690  * @access_request: The requested access rights to check.
691  * @masks: Layer access masks to populate.
692  * @key_type: The key type to switch between access masks of different types.
693  *
694  * Return: An access mask where each access right bit is set which is handled
695  * in any of the active layers in @domain.
696  */
697 access_mask_t
698 landlock_init_layer_masks(const struct landlock_ruleset *const domain,
699 			  const access_mask_t access_request,
700 			  struct layer_masks *const masks,
701 			  const enum landlock_key_type key_type)
702 {
703 	access_mask_t handled_accesses = 0;
704 	get_access_mask_t *get_access_mask;
705 
706 	switch (key_type) {
707 	case LANDLOCK_KEY_INODE:
708 		get_access_mask = landlock_get_fs_access_mask;
709 		break;
710 
711 #if IS_ENABLED(CONFIG_INET)
712 	case LANDLOCK_KEY_NET_PORT:
713 		get_access_mask = landlock_get_net_access_mask;
714 		break;
715 #endif /* IS_ENABLED(CONFIG_INET) */
716 
717 	default:
718 		WARN_ON_ONCE(1);
719 		return 0;
720 	}
721 
722 	/* An empty access request can happen because of O_WRONLY | O_RDWR. */
723 	if (!access_request)
724 		return 0;
725 
726 	for (size_t i = 0; i < domain->num_layers; i++) {
727 		const access_mask_t handled = get_access_mask(domain, i);
728 
729 		masks->layers[i].access = access_request & handled;
730 		handled_accesses |= masks->layers[i].access;
731 #ifdef CONFIG_AUDIT
732 		masks->layers[i].quiet = false;
733 #endif /* CONFIG_AUDIT */
734 	}
735 	for (size_t i = domain->num_layers; i < ARRAY_SIZE(masks->layers);
736 	     i++) {
737 		masks->layers[i].access = 0;
738 #ifdef CONFIG_AUDIT
739 		masks->layers[i].quiet = false;
740 #endif /* CONFIG_AUDIT */
741 	}
742 
743 	return handled_accesses;
744 }
745