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