xref: /linux/security/landlock/access.h (revision 97c8386f1d744e9689cd3e72347f980d9af4f59f)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Landlock - 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 the .handled field of all
23  * domain->layers[] entries when we need to get the absolute handled access
24  * masks, see landlock_upgrade_handled_layer_config().
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 u32 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 all permissions can be stored. */
46 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_PERMISSION);
47 /* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
48 static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
49 
50 /* Access and permission masks (bitfields only). */
51 struct access_masks {
52 	access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
53 	access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
54 	access_mask_t scope : LANDLOCK_NUM_SCOPE;
55 	access_mask_t permissions : LANDLOCK_NUM_PERMISSION;
56 } __packed __aligned(sizeof(u32));
57 
58 union access_masks_all {
59 	struct access_masks masks;
60 	u32 all;
61 };
62 
63 /* Makes sure all fields are covered. */
64 static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
65 	      sizeof(typeof_member(union access_masks_all, all)));
66 
67 /**
68  * struct permission_masks - Per-permission member bitmasks
69  */
70 struct permission_masks {
71 	/**
72 	 * @ns_types: Namespace type member mask, indexed in FOR_EACH_NS_TYPE()
73 	 * order.
74 	 */
75 	u64 ns_types : LANDLOCK_NUM_NAMESPACE_TYPE;
76 	/**
77 	 * @caps: Capability member mask, indexed by CAP_* values.
78 	 */
79 	u64 caps : LANDLOCK_NUM_CAPABILITY;
80 } __packed __aligned(sizeof(u64));
81 
82 static_assert(sizeof(struct permission_masks) == sizeof(u64));
83 /* All permission_masks bitfields must fit in a single u64. */
84 static_assert(LANDLOCK_NUM_CAPABILITY + LANDLOCK_NUM_NAMESPACE_TYPE <=
85 	      BITS_PER_TYPE(u64));
86 
87 /**
88  * struct layer_config - Per-layer access configuration
89  *
90  * A ruleset stores one mutable layer and a domain stores a flexible array of
91  * immutable layers.  Unlike filesystem and network access rights, namespace
92  * types and capabilities use flat bitmasks because their keyspaces are small
93  * and bounded.
94  */
95 struct layer_config {
96 	/**
97 	 * @allowed: Members allowed by each handled permission.
98 	 */
99 	struct permission_masks allowed;
100 	/**
101 	 * @handled: Bitmask of access rights and permissions handled (i.e.
102 	 * restricted) by this layer.
103 	 */
104 	struct access_masks handled;
105 };
106 
107 #define _LANDLOCK_LAYER_MASK_PADDING                              \
108 	(BITS_PER_TYPE(access_mask_t) - LANDLOCK_NUM_ACCESS_MAX - \
109 	 IS_ENABLED(CONFIG_SECURITY_LANDLOCK_LOG))
110 
111 /**
112  * struct layer_mask - The access rights and rule flags for a layer.
113  *
114  * This has a bit for each access rights and rule flags.  During access checks,
115  * it is used to represent the access rights for each layer which still need to
116  * be fulfilled.  When all bits are 0, the access request is considered to be
117  * fulfilled.
118  */
119 struct layer_mask {
120 	/**
121 	 * @access: The unfulfilled access rights for this layer.
122 	 */
123 	access_mask_t access : LANDLOCK_NUM_ACCESS_MAX;
124 #ifdef CONFIG_SECURITY_LANDLOCK_LOG
125 	/**
126 	 * @quiet: Whether we have encountered a rule with the quiet flag for
127 	 * this layer.  Used to control logging.
128 	 */
129 	access_mask_t quiet : 1;
130 #endif /* CONFIG_SECURITY_LANDLOCK_LOG */
131 	/**
132 	 * @__pad: Padding for the compiler's bitfield initialization.
133 	 */
134 	access_mask_t __pad : _LANDLOCK_LAYER_MASK_PADDING;
135 } __packed __aligned(sizeof(access_mask_t));
136 
137 /*
138  * Make sure that we don't increase the size of struct layer_mask when storing
139  * rule flags.
140  */
141 static_assert(sizeof(struct layer_mask) == sizeof(access_mask_t));
142 
143 /**
144  * struct layer_masks - An array of struct layer_mask, one per layer.
145  */
146 struct layer_masks {
147 	/**
148 	 * @layers: The unfulfilled access rights for each layer.
149 	 */
150 	struct layer_mask layers[LANDLOCK_MAX_NUM_LAYERS];
151 };
152 
153 /*
154  * Tracks domains responsible of a denied access.  This avoids storing in each
155  * object the full matrix of per-layer unfulfilled access rights, which is
156  * required by update_request().
157  *
158  * Each nibble represents the layer index of the newest layer which denied a
159  * certain access right.  For file system access rights, the upper four bits are
160  * the index of the layer which denies LANDLOCK_ACCESS_FS_IOCTL_DEV and the
161  * lower nibble represents LANDLOCK_ACCESS_FS_TRUNCATE.
162  */
163 typedef u8 deny_masks_t;
164 
165 /*
166  * Makes sure all optional access rights can be tied to a layer index (cf.
167  * get_deny_mask).
168  */
169 static_assert(BITS_PER_TYPE(deny_masks_t) >=
170 	      (HWEIGHT(LANDLOCK_MAX_NUM_LAYERS - 1) *
171 	       HWEIGHT(_LANDLOCK_ACCESS_FS_OPTIONAL)));
172 
173 /* LANDLOCK_MAX_NUM_LAYERS must be a power of two (cf. deny_masks_t assert). */
174 static_assert(HWEIGHT(LANDLOCK_MAX_NUM_LAYERS) == 1);
175 
176 /* Upgrades with all initially denied by default access rights. */
177 static inline struct layer_config
178 landlock_upgrade_handled_layer_config(struct layer_config layer_config)
179 {
180 	/*
181 	 * All access rights that are denied by default whether they are
182 	 * explicitly handled or not.
183 	 */
184 	if (layer_config.handled.fs)
185 		layer_config.handled.fs |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
186 
187 	return layer_config;
188 }
189 
190 /* Checks the subset relation between access masks. */
191 static inline bool access_mask_subset(access_mask_t subset,
192 				      access_mask_t superset)
193 {
194 	return (subset | superset) == superset;
195 }
196 
197 /* A bitmask that is large enough to hold set of optional accesses. */
198 typedef u8 optional_access_t;
199 static_assert(BITS_PER_TYPE(optional_access_t) >=
200 	      HWEIGHT(_LANDLOCK_ACCESS_FS_OPTIONAL));
201 
202 #endif /* _SECURITY_LANDLOCK_ACCESS_H */
203