1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Landlock - Domain management 4 * 5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> 6 * Copyright © 2018-2020 ANSSI 7 * Copyright © 2024-2025 Microsoft Corporation 8 * Copyright © 2026 Cloudflare, Inc. 9 */ 10 11 #ifndef _SECURITY_LANDLOCK_DOMAIN_H 12 #define _SECURITY_LANDLOCK_DOMAIN_H 13 14 #include <linux/cleanup.h> 15 #include <linux/limits.h> 16 #include <linux/mm.h> 17 #include <linux/path.h> 18 #include <linux/pid.h> 19 #include <linux/refcount.h> 20 #include <linux/sched.h> 21 #include <linux/slab.h> 22 #include <linux/workqueue.h> 23 24 #include "access.h" 25 #include "log.h" 26 #include "ruleset.h" 27 28 enum landlock_log_status { 29 /* 30 * Hierarchy whose creation event has not been emitted, so it is not yet 31 * observable from user space. A hierarchy is born in this state (the 32 * zero value, so a partially initialized hierarchy defaults to "not 33 * observable") and leaves it when landlock_restrict_self() emits its 34 * creation event, right after the merge and before the thread-sync 35 * wait. No trace free_domain event (and no audit deallocation record) 36 * fires while a hierarchy is in this state, so a hierarchy that never 37 * became observable (e.g. its initialization failed) is freed silently. 38 * A domain aborted by a thread-sync failure already emitted its 39 * creation event, so it is no longer UNCOMMITTED and does fire 40 * free_domain. 41 */ 42 LANDLOCK_LOG_UNCOMMITTED = 0, 43 LANDLOCK_LOG_PENDING, 44 LANDLOCK_LOG_RECORDED, 45 LANDLOCK_LOG_DISABLED, 46 }; 47 48 /** 49 * struct landlock_details - Domain's creation information 50 * 51 * Rarely accessed, mainly when logging the first domain's denial. 52 * 53 * The contained pointers are initialized at the domain creation time and never 54 * changed again. 55 */ 56 struct landlock_details { 57 /** 58 * @pid: PID of the task that initially restricted itself. It still 59 * identifies the same task. Keeping a reference to this PID ensures that 60 * it will not be recycled. 61 */ 62 struct pid *pid; 63 /** 64 * @uid: UID of the task that initially restricted itself, at creation time. 65 */ 66 uid_t uid; 67 /** 68 * @comm: Command line of the task that initially restricted itself, at 69 * creation time. Always NULL terminated. 70 */ 71 char comm[TASK_COMM_LEN]; 72 /** 73 * @exe_path: Executable path of the task that initially restricted 74 * itself, at creation time. Always NULL terminated, and never greater 75 * than LANDLOCK_PATH_MAX_SIZE. 76 */ 77 char exe_path[]; 78 }; 79 80 /* Adds 11 extra characters for the potential " (deleted)" suffix. */ 81 #define LANDLOCK_PATH_MAX_SIZE (PATH_MAX + 11) 82 83 /* Makes sure the greatest landlock_details can be allocated. */ 84 static_assert(struct_size_t(struct landlock_details, exe_path, 85 LANDLOCK_PATH_MAX_SIZE) <= KMALLOC_MAX_SIZE); 86 87 /** 88 * struct landlock_hierarchy - Node in a domain hierarchy 89 */ 90 struct landlock_hierarchy { 91 /** 92 * @parent: Pointer to the parent node, or NULL if it is a root 93 * Landlock domain. 94 */ 95 struct landlock_hierarchy *parent; 96 /** 97 * @usage: Number of potential children domains plus their parent 98 * domain. 99 */ 100 refcount_t usage; 101 102 #ifdef CONFIG_SECURITY_LANDLOCK_LOG 103 /** 104 * @log_status: Whether this domain should be logged or not. Because 105 * concurrent log entries may be created at the same time, it is still 106 * possible to have several domain records of the same domain. 107 */ 108 enum landlock_log_status log_status; 109 /** 110 * @num_denials: Number of access requests denied by this domain. 111 * Masked (i.e. never logged) denials are still counted. 112 */ 113 atomic64_t num_denials; 114 /** 115 * @id: Landlock domain ID, set once at domain creation time. 116 */ 117 u64 id; 118 /** 119 * @details: Information about the related domain. 120 */ 121 const struct landlock_details *details; 122 /** 123 * @log_same_exec: Set if the domain is *not* configured with 124 * %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF. Set to true by default. 125 */ 126 u32 log_same_exec : 1, 127 /** 128 * @log_new_exec: Set if the domain is configured with 129 * %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON. Set to false by default. 130 */ 131 log_new_exec : 1; 132 /** 133 * @quiet_access: Bitmasks of access that should be quieted (i.e. not 134 * logged) if the related object is marked as quiet. 135 */ 136 struct access_masks quiet_access; 137 /** 138 * @quiet_permission: Per-member quiet bitmasks for permission types in 139 * this layer. 140 */ 141 struct permission_masks quiet_permission; 142 #endif /* CONFIG_SECURITY_LANDLOCK_LOG */ 143 }; 144 145 #ifdef CONFIG_SECURITY_LANDLOCK_LOG 146 147 deny_masks_t 148 landlock_get_deny_masks(const access_mask_t all_existing_optional_access, 149 const access_mask_t optional_access, 150 const struct layer_masks *const masks); 151 152 optional_access_t landlock_get_quiet_optional_accesses( 153 const access_mask_t all_existing_optional_access, 154 const deny_masks_t deny_masks, const struct layer_masks *const masks); 155 156 int landlock_init_hierarchy_log(struct landlock_hierarchy *const hierarchy); 157 158 static inline void 159 landlock_free_hierarchy_details(struct landlock_hierarchy *const hierarchy) 160 { 161 if (!hierarchy || !hierarchy->details) 162 return; 163 164 put_pid(hierarchy->details->pid); 165 kfree(hierarchy->details); 166 } 167 168 #else /* CONFIG_SECURITY_LANDLOCK_LOG */ 169 170 static inline int 171 landlock_init_hierarchy_log(struct landlock_hierarchy *const hierarchy) 172 { 173 return 0; 174 } 175 176 static inline void 177 landlock_free_hierarchy_details(struct landlock_hierarchy *const hierarchy) 178 { 179 } 180 181 #endif /* CONFIG_SECURITY_LANDLOCK_LOG */ 182 183 static inline void 184 landlock_get_hierarchy(struct landlock_hierarchy *const hierarchy) 185 { 186 if (hierarchy) 187 refcount_inc(&hierarchy->usage); 188 } 189 190 static inline void landlock_put_hierarchy(struct landlock_hierarchy *hierarchy) 191 { 192 while (hierarchy && refcount_dec_and_test(&hierarchy->usage)) { 193 const struct landlock_hierarchy *const freeme = hierarchy; 194 195 landlock_log_free_domain(hierarchy); 196 landlock_free_hierarchy_details(hierarchy); 197 hierarchy = hierarchy->parent; 198 kfree(freeme); 199 } 200 } 201 202 /** 203 * struct landlock_domain - Immutable Landlock domain 204 * 205 * A domain is created from a ruleset by landlock_merge_ruleset() and enforced 206 * on a task. Once created, its rules and access masks are immutable. Unlike 207 * &struct landlock_ruleset, a domain has no lock field. 208 */ 209 struct landlock_domain { 210 /** 211 * @rules: Red-black tree storage for rules. 212 */ 213 struct landlock_rules rules; 214 /** 215 * @hierarchy: Enables hierarchy identification even when a parent 216 * domain vanishes. This is needed for the ptrace and scope 217 * restrictions. 218 */ 219 struct landlock_hierarchy *hierarchy; 220 union { 221 /** 222 * @work_free: Enables to free a domain within a lockless 223 * section. This is only used by landlock_put_domain_deferred() 224 * when @usage reaches zero. The fields @usage, @num_layers and 225 * @layers are then unused. 226 */ 227 struct work_struct work_free; 228 struct { 229 /** 230 * @usage: Number of credentials referencing this 231 * domain. 232 */ 233 refcount_t usage; 234 /** 235 * @num_layers: Number of layers that are used in this 236 * domain. This enables to check that all the layers 237 * allow an access request. 238 */ 239 u32 num_layers; 240 /** 241 * @layers: Per-layer access configuration. A domain 242 * saves all layers of merged rulesets in a stack (FAM), 243 * starting from the first layer to the last one. These 244 * layers are used when merging rulesets, for user space 245 * backward compatibility (i.e. future-proof), and to 246 * properly handle merged rulesets without overlapping 247 * access rights. These layers are set once and never 248 * changed for the lifetime of the domain. 249 * 250 * Not annotated with __counted_by(num_layers): clang 251 * miscomputes the counter address for a FAM nested in 252 * an anonymous struct within a union, which trips 253 * FORTIFY: 254 * https://github.com/llvm/llvm-project/pull/228309 255 */ 256 struct layer_config layers[]; 257 }; 258 }; 259 }; 260 261 static inline access_mask_t 262 landlock_get_fs_access_mask(const struct landlock_domain *const domain, 263 const u16 layer_level) 264 { 265 /* Handles all initially denied by default access rights. */ 266 return domain->layers[layer_level].handled.fs | 267 _LANDLOCK_ACCESS_FS_INITIALLY_DENIED; 268 } 269 270 static inline access_mask_t 271 landlock_get_net_access_mask(const struct landlock_domain *const domain, 272 const u16 layer_level) 273 { 274 return domain->layers[layer_level].handled.net; 275 } 276 277 static inline access_mask_t 278 landlock_get_scope_mask(const struct landlock_domain *const domain, 279 const u16 layer_level) 280 { 281 return domain->layers[layer_level].handled.scope; 282 } 283 284 /** 285 * landlock_union_access_masks - Return all access rights handled in the 286 * domain 287 * 288 * @domain: Landlock domain 289 * 290 * Return: An access_masks result of the OR of all the domain's access masks. 291 */ 292 static inline struct access_masks 293 landlock_union_access_masks(const struct landlock_domain *const domain) 294 { 295 union access_masks_all matches = {}; 296 size_t layer_level; 297 298 for (layer_level = 0; layer_level < domain->num_layers; layer_level++) { 299 union access_masks_all layer = { 300 .masks = domain->layers[layer_level].handled, 301 }; 302 303 matches.all |= layer.all; 304 } 305 306 return matches.masks; 307 } 308 309 void landlock_put_domain(struct landlock_domain *const domain); 310 void landlock_put_domain_deferred(struct landlock_domain *const domain); 311 312 DEFINE_FREE(landlock_put_domain, struct landlock_domain *, 313 if (!IS_ERR_OR_NULL(_T)) landlock_put_domain(_T)) 314 315 struct landlock_domain * 316 landlock_merge_ruleset(struct landlock_domain *const parent, 317 struct landlock_ruleset *const ruleset); 318 319 bool landlock_unmask_layers(const struct landlock_domain *const domain, 320 const struct landlock_id id, 321 struct layer_masks *masks, 322 const struct landlock_rule **matched_rule); 323 324 access_mask_t 325 landlock_init_layer_masks(const struct landlock_domain *const domain, 326 const access_mask_t access_request, 327 struct layer_masks *masks, 328 const enum landlock_key_type key_type); 329 330 static inline void landlock_get_domain(struct landlock_domain *const domain) 331 { 332 if (domain) 333 refcount_inc(&domain->usage); 334 } 335 336 /** 337 * landlock_permission_is_denied - Check if a permission request is denied 338 * 339 * @domain: The enforced domain. 340 * @permission_bit: The LANDLOCK_PERMISSION_* flag to check. Must have 341 * exactly one bit set. 342 * @request_value: Compact bitmask to look for (e.g. the result of 343 * landlock_ns_type_to_bit()). Must have exactly one bit set. 344 * 345 * Iterate from the youngest layer to the oldest. For each layer that handles 346 * @permission_bit, check whether @request_value is present in the layer's 347 * allowed bitmask. Return on the first (youngest) denying layer. 348 * 349 * Return: The youngest denying layer + 1, or 0 if allowed. 350 */ 351 static inline size_t 352 landlock_permission_is_denied(const struct landlock_domain *const domain, 353 const access_mask_t permission_bit, 354 const u64 request_value) 355 { 356 ssize_t layer; 357 358 BUILD_BUG_ON(sizeof(permission_bit) > sizeof(u32)); 359 360 if (WARN_ON_ONCE(hweight32(permission_bit) != 1) || 361 WARN_ON_ONCE(hweight64(request_value) != 1)) 362 return domain->num_layers; 363 364 for (layer = domain->num_layers - 1; layer >= 0; layer--) { 365 u64 allowed; 366 367 if (!(domain->layers[layer].handled.permissions & 368 permission_bit)) 369 continue; 370 371 /* 372 * Current callers pass only permission types with an explicit 373 * case below. The default catches a new caller missing its 374 * member mask. 375 */ 376 switch (permission_bit) { 377 case LANDLOCK_PERMISSION_NAMESPACE_USE: 378 allowed = domain->layers[layer].allowed.ns_types; 379 break; 380 case LANDLOCK_PERMISSION_CAPABILITY_USE: 381 allowed = domain->layers[layer].allowed.caps; 382 break; 383 default: 384 WARN_ONCE(1, "Unknown permission %u\n", 385 (unsigned int)permission_bit); 386 return layer + 1; 387 } 388 389 if (!(allowed & request_value)) 390 return layer + 1; 391 } 392 return 0; 393 } 394 395 #endif /* _SECURITY_LANDLOCK_DOMAIN_H */ 396