1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Landlock LSM - Ruleset management
5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6 * Copyright © 2018-2020 ANSSI
9 #ifndef _SECURITY_LANDLOCK_RULESET_H
10 #define _SECURITY_LANDLOCK_RULESET_H
12 #include <linux/cleanup.h>
13 #include <linux/err.h>
14 #include <linux/mutex.h>
15 #include <linux/rbtree.h>
16 #include <linux/refcount.h>
17 #include <linux/workqueue.h>
24 * struct landlock_layer - Access rights for a given layer
26 struct landlock_layer
{
28 * @level: Position of this layer in the layer stack.
32 * @access: Bitfield of allowed actions on the kernel object. They are
33 * relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ).
39 * union landlock_key - Key of a ruleset's red-black tree
43 * @object: Pointer to identify a kernel object (e.g. an inode).
45 struct landlock_object
*object
;
47 * @data: Raw data to identify an arbitrary 32-bit value
54 * enum landlock_key_type - Type of &union landlock_key
56 enum landlock_key_type
{
58 * @LANDLOCK_KEY_INODE: Type of &landlock_ruleset.root_inode's node
61 LANDLOCK_KEY_INODE
= 1,
63 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
66 LANDLOCK_KEY_NET_PORT
,
70 * struct landlock_id - Unique rule identifier for a ruleset
74 * @key: Identifies either a kernel object (e.g. an inode) or
75 * a raw value (e.g. a TCP port).
77 union landlock_key key
;
79 * @type: Type of a landlock_ruleset's root tree.
81 const enum landlock_key_type type
;
85 * struct landlock_rule - Access rights tied to an object
87 struct landlock_rule
{
89 * @node: Node in the ruleset's red-black tree.
93 * @key: A union to identify either a kernel object (e.g. an inode) or
94 * a raw data value (e.g. a network socket port). This is used as a key
95 * for this ruleset element. The pointer is set once and never
96 * modified. It always points to an allocated object because each rule
97 * increments the refcount of its object.
99 union landlock_key key
;
101 * @num_layers: Number of entries in @layers.
105 * @layers: Stack of layers, from the latest to the newest, implemented
106 * as a flexible array member (FAM).
108 struct landlock_layer layers
[] __counted_by(num_layers
);
112 * struct landlock_hierarchy - Node in a ruleset hierarchy
114 struct landlock_hierarchy
{
116 * @parent: Pointer to the parent node, or NULL if it is a root
119 struct landlock_hierarchy
*parent
;
121 * @usage: Number of potential children domains plus their parent
128 * struct landlock_ruleset - Landlock ruleset
130 * This data structure must contain unique entries, be updatable, and quick to
133 struct landlock_ruleset
{
135 * @root_inode: Root of a red-black tree containing &struct
136 * landlock_rule nodes with inode object. Once a ruleset is tied to a
137 * process (i.e. as a domain), this tree is immutable until @usage
140 struct rb_root root_inode
;
142 #if IS_ENABLED(CONFIG_INET)
144 * @root_net_port: Root of a red-black tree containing &struct
145 * landlock_rule nodes with network port. Once a ruleset is tied to a
146 * process (i.e. as a domain), this tree is immutable until @usage
149 struct rb_root root_net_port
;
150 #endif /* IS_ENABLED(CONFIG_INET) */
153 * @hierarchy: Enables hierarchy identification even when a parent
154 * domain vanishes. This is needed for the ptrace protection.
156 struct landlock_hierarchy
*hierarchy
;
159 * @work_free: Enables to free a ruleset within a lockless
160 * section. This is only used by
161 * landlock_put_ruleset_deferred() when @usage reaches zero.
162 * The fields @lock, @usage, @num_rules, @num_layers and
163 * @access_masks are then unused.
165 struct work_struct work_free
;
168 * @lock: Protects against concurrent modifications of
169 * @root, if @usage is greater than zero.
173 * @usage: Number of processes (i.e. domains) or file
174 * descriptors referencing this ruleset.
178 * @num_rules: Number of non-overlapping (i.e. not for
179 * the same object) rules in this ruleset.
183 * @num_layers: Number of layers that are used in this
184 * ruleset. This enables to check that all the layers
185 * allow an access request. A value of 0 identifies a
186 * non-merged ruleset (i.e. not a domain).
190 * @access_masks: Contains the subset of filesystem and
191 * network actions that are restricted by a ruleset.
192 * A domain saves all layers of merged rulesets in a
193 * stack (FAM), starting from the first layer to the
194 * last one. These layers are used when merging
195 * rulesets, for user space backward compatibility
196 * (i.e. future-proof), and to properly handle merged
197 * rulesets without overlapping access rights. These
198 * layers are set once and never changed for the
199 * lifetime of the ruleset.
201 struct access_masks access_masks
[];
206 struct landlock_ruleset
*
207 landlock_create_ruleset(const access_mask_t access_mask_fs
,
208 const access_mask_t access_mask_net
,
209 const access_mask_t scope_mask
);
211 void landlock_put_ruleset(struct landlock_ruleset
*const ruleset
);
212 void landlock_put_ruleset_deferred(struct landlock_ruleset
*const ruleset
);
214 DEFINE_FREE(landlock_put_ruleset
, struct landlock_ruleset
*,
215 if (!IS_ERR_OR_NULL(_T
)) landlock_put_ruleset(_T
))
217 int landlock_insert_rule(struct landlock_ruleset
*const ruleset
,
218 const struct landlock_id id
,
219 const access_mask_t access
);
221 struct landlock_ruleset
*
222 landlock_merge_ruleset(struct landlock_ruleset
*const parent
,
223 struct landlock_ruleset
*const ruleset
);
225 const struct landlock_rule
*
226 landlock_find_rule(const struct landlock_ruleset
*const ruleset
,
227 const struct landlock_id id
);
229 static inline void landlock_get_ruleset(struct landlock_ruleset
*const ruleset
)
232 refcount_inc(&ruleset
->usage
);
236 * landlock_union_access_masks - Return all access rights handled in the
239 * @domain: Landlock ruleset (used as a domain)
241 * Returns: an access_masks result of the OR of all the domain's access masks.
243 static inline struct access_masks
244 landlock_union_access_masks(const struct landlock_ruleset
*const domain
)
246 union access_masks_all matches
= {};
249 for (layer_level
= 0; layer_level
< domain
->num_layers
; layer_level
++) {
250 union access_masks_all layer
= {
251 .masks
= domain
->access_masks
[layer_level
],
254 matches
.all
|= layer
.all
;
257 return matches
.masks
;
261 * landlock_get_applicable_domain - Return @domain if it applies to (handles)
262 * at least one of the access rights specified
265 * @domain: Landlock ruleset (used as a domain)
266 * @masks: access masks
268 * Returns: @domain if any access rights specified in @masks is handled, or
271 static inline const struct landlock_ruleset
*
272 landlock_get_applicable_domain(const struct landlock_ruleset
*const domain
,
273 const struct access_masks masks
)
275 const union access_masks_all masks_all
= {
278 union access_masks_all merge
= {};
283 merge
.masks
= landlock_union_access_masks(domain
);
284 if (merge
.all
& masks_all
.all
)
291 landlock_add_fs_access_mask(struct landlock_ruleset
*const ruleset
,
292 const access_mask_t fs_access_mask
,
293 const u16 layer_level
)
295 access_mask_t fs_mask
= fs_access_mask
& LANDLOCK_MASK_ACCESS_FS
;
297 /* Should already be checked in sys_landlock_create_ruleset(). */
298 WARN_ON_ONCE(fs_access_mask
!= fs_mask
);
299 ruleset
->access_masks
[layer_level
].fs
|= fs_mask
;
303 landlock_add_net_access_mask(struct landlock_ruleset
*const ruleset
,
304 const access_mask_t net_access_mask
,
305 const u16 layer_level
)
307 access_mask_t net_mask
= net_access_mask
& LANDLOCK_MASK_ACCESS_NET
;
309 /* Should already be checked in sys_landlock_create_ruleset(). */
310 WARN_ON_ONCE(net_access_mask
!= net_mask
);
311 ruleset
->access_masks
[layer_level
].net
|= net_mask
;
315 landlock_add_scope_mask(struct landlock_ruleset
*const ruleset
,
316 const access_mask_t scope_mask
, const u16 layer_level
)
318 access_mask_t mask
= scope_mask
& LANDLOCK_MASK_SCOPE
;
320 /* Should already be checked in sys_landlock_create_ruleset(). */
321 WARN_ON_ONCE(scope_mask
!= mask
);
322 ruleset
->access_masks
[layer_level
].scope
|= mask
;
325 static inline access_mask_t
326 landlock_get_fs_access_mask(const struct landlock_ruleset
*const ruleset
,
327 const u16 layer_level
)
329 /* Handles all initially denied by default access rights. */
330 return ruleset
->access_masks
[layer_level
].fs
|
331 _LANDLOCK_ACCESS_FS_INITIALLY_DENIED
;
334 static inline access_mask_t
335 landlock_get_net_access_mask(const struct landlock_ruleset
*const ruleset
,
336 const u16 layer_level
)
338 return ruleset
->access_masks
[layer_level
].net
;
341 static inline access_mask_t
342 landlock_get_scope_mask(const struct landlock_ruleset
*const ruleset
,
343 const u16 layer_level
)
345 return ruleset
->access_masks
[layer_level
].scope
;
348 bool landlock_unmask_layers(const struct landlock_rule
*const rule
,
349 const access_mask_t access_request
,
350 layer_mask_t (*const layer_masks
)[],
351 const size_t masks_array_size
);
354 landlock_init_layer_masks(const struct landlock_ruleset
*const domain
,
355 const access_mask_t access_request
,
356 layer_mask_t (*const layer_masks
)[],
357 const enum landlock_key_type key_type
);
359 #endif /* _SECURITY_LANDLOCK_RULESET_H */