xref: /linux/security/landlock/access.h (revision 622e2f5954763385c4fa1f9a11a11366952a9b60)
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.
24  */
25 /* clang-format off */
26 #define _LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \
27 	LANDLOCK_ACCESS_FS_REFER)
28 /* clang-format on */
29 
30 typedef u16 access_mask_t;
31 
32 /* Makes sure all filesystem access rights can be stored. */
33 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
34 /* Makes sure all network access rights can be stored. */
35 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
36 /* Makes sure all scoped rights can be stored. */
37 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);
38 /* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
39 static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
40 
41 /* Ruleset access masks. */
42 struct access_masks {
43 	access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
44 	access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
45 	access_mask_t scope : LANDLOCK_NUM_SCOPE;
46 };
47 
48 union access_masks_all {
49 	struct access_masks masks;
50 	u32 all;
51 };
52 
53 /* Makes sure all fields are covered. */
54 static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
55 	      sizeof(typeof_member(union access_masks_all, all)));
56 
57 typedef u16 layer_mask_t;
58 
59 /* Makes sure all layers can be checked. */
60 static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
61 
62 #endif /* _SECURITY_LANDLOCK_ACCESS_H */
63