xref: /linux/security/landlock/access.h (revision de5817bbfb569f22406970f81360ac3f694ba6e8)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Landlock LSM - Access types and helpers
4  *
5  * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6  * Copyright © 2018-2020 ANSSI
7  * Copyright © 2024-2025 Microsoft Corporation
8  */
9 
10 #ifndef _SECURITY_LANDLOCK_ACCESS_H
11 #define _SECURITY_LANDLOCK_ACCESS_H
12 
13 #include <linux/bitops.h>
14 #include <linux/build_bug.h>
15 #include <linux/kernel.h>
16 #include <uapi/linux/landlock.h>
17 
18 #include "limits.h"
19 
20 /*
21  * All access rights that are denied by default whether they are handled or not
22  * by a ruleset/layer.  This must be ORed with all ruleset->access_masks[]
23  * entries when we need to get the absolute handled access masks, see
24  * landlock_upgrade_handled_access_masks().
25  */
26 /* clang-format off */
27 #define _LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \
28 	LANDLOCK_ACCESS_FS_REFER)
29 /* clang-format on */
30 
31 typedef u16 access_mask_t;
32 
33 /* Makes sure all filesystem access rights can be stored. */
34 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
35 /* Makes sure all network access rights can be stored. */
36 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
37 /* Makes sure all scoped rights can be stored. */
38 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);
39 /* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
40 static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
41 
42 /* Ruleset access masks. */
43 struct access_masks {
44 	access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
45 	access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
46 	access_mask_t scope : LANDLOCK_NUM_SCOPE;
47 };
48 
49 union access_masks_all {
50 	struct access_masks masks;
51 	u32 all;
52 };
53 
54 /* Makes sure all fields are covered. */
55 static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
56 	      sizeof(typeof_member(union access_masks_all, all)));
57 
58 typedef u16 layer_mask_t;
59 
60 /* Makes sure all layers can be checked. */
61 static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
62 
63 /* Upgrades with all initially denied by default access rights. */
64 static inline struct access_masks
landlock_upgrade_handled_access_masks(struct access_masks access_masks)65 landlock_upgrade_handled_access_masks(struct access_masks access_masks)
66 {
67 	/*
68 	 * All access rights that are denied by default whether they are
69 	 * explicitly handled or not.
70 	 */
71 	if (access_masks.fs)
72 		access_masks.fs |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
73 
74 	return access_masks;
75 }
76 
77 #endif /* _SECURITY_LANDLOCK_ACCESS_H */
78