Debian Codegraph @

security/landlock/access.h

raw
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Landlock - Access types and helpers
4 *
5 * Copyright © 2016-2020 Mickaël Salaün <[email protected]>
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 /* clang-format off */
32 #define _LANDLOCK_ACCESS_FS_OPTIONAL ( \
33 LANDLOCK_ACCESS_FS_TRUNCATE | \
34 LANDLOCK_ACCESS_FS_IOCTL_DEV)
35 /* clang-format on */
36
37 typedef u16 access_mask_t;
38
39 /* Makes sure all filesystem access rights can be stored. */
40 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
41 /* Makes sure all network access rights can be stored. */
42 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
43 /* Makes sure all scoped rights can be stored. */
44 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);
45 /* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
46 static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
47
48 /* Ruleset access masks. */
49 struct access_masks {
50 access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
51 access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
52 access_mask_t scope : LANDLOCK_NUM_SCOPE;
53 };
54
55 union access_masks_all {
56 struct access_masks masks;
57 u32 all;
58 };
59
60 /* Makes sure all fields are covered. */
61 static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
62 sizeof(typeof_member(union access_masks_all, all)));
63
64 /**
65 * struct layer_access_masks - A boolean matrix of layers and access rights
66 *
67 * This has a bit for each combination of layer numbers and access rights.
68 * During access checks, it is used to represent the access rights for each
69 * layer which still need to be fulfilled. When all bits are 0, the access
70 * request is considered to be fulfilled.
71 */
72 struct layer_access_masks {
73 /**
74 * @access: The unfulfilled access rights for each layer.
75 */
76 access_mask_t access[LANDLOCK_MAX_NUM_LAYERS];
77 };
78
79 /*
80 * Tracks domains responsible of a denied access. This avoids storing in each
81 * object the full matrix of per-layer unfulfilled access rights, which is
82 * required by update_request().
83 *
84 * Each nibble represents the layer index of the newest layer which denied a
85 * certain access right. For file system access rights, the upper four bits are
86 * the index of the layer which denies LANDLOCK_ACCESS_FS_IOCTL_DEV and the
87 * lower nibble represents LANDLOCK_ACCESS_FS_TRUNCATE.
88 */
89 typedef u8 deny_masks_t;
90
91 /*
92 * Makes sure all optional access rights can be tied to a layer index (cf.
93 * get_deny_mask).
94 */
95 static_assert(BITS_PER_TYPE(deny_masks_t) >=
96 (HWEIGHT(LANDLOCK_MAX_NUM_LAYERS - 1) *
97 HWEIGHT(_LANDLOCK_ACCESS_FS_OPTIONAL)));
98
99 /* LANDLOCK_MAX_NUM_LAYERS must be a power of two (cf. deny_masks_t assert). */
100 static_assert(HWEIGHT(LANDLOCK_MAX_NUM_LAYERS) == 1);
101
102 /* Upgrades with all initially denied by default access rights. */
103 static inline struct access_masks
104 landlock_upgrade_handled_access_masks(struct access_masks access_masks)
105 {
106 /*
107 * All access rights that are denied by default whether they are
108 * explicitly handled or not.
109 */
110 if (access_masks.fs)
111 access_masks.fs |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
112
113 return access_masks;
114 }
115
116 /* Checks the subset relation between access masks. */
117 static inline bool access_mask_subset(access_mask_t subset,
118 access_mask_t superset)
119 {
120 return (subset | superset) == superset;
121 }
122
123 #endif /* _SECURITY_LANDLOCK_ACCESS_H */