1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Landlock - Credential hooks 4 * 5 * Copyright © 2019-2020 Mickaël Salaün <mic@digikod.net> 6 * Copyright © 2019-2020 ANSSI 7 * Copyright © 2021-2025 Microsoft Corporation 8 */ 9 10 #ifndef _SECURITY_LANDLOCK_CRED_H 11 #define _SECURITY_LANDLOCK_CRED_H 12 13 #include <linux/container_of.h> 14 #include <linux/cred.h> 15 #include <linux/init.h> 16 #include <linux/rcupdate.h> 17 18 #include "access.h" 19 #include "limits.h" 20 #include "ruleset.h" 21 #include "setup.h" 22 23 /** 24 * struct landlock_cred_security - Credential security blob 25 * 26 * This structure is packed to minimize the size of struct 27 * landlock_file_security. However, it is always aligned in the LSM cred blob, 28 * see lsm_set_blob_size(). 29 */ 30 struct landlock_cred_security { 31 /** 32 * @domain: Immutable ruleset enforced on a task. 33 */ 34 struct landlock_ruleset *domain; 35 36 #ifdef CONFIG_AUDIT 37 /** 38 * @domain_exec: Bitmask identifying the domain layers that were enforced by 39 * the current task's executed file (i.e. no new execve(2) since 40 * landlock_restrict_self(2)). 41 */ 42 u16 domain_exec; 43 /** 44 * @log_subdomains_off: Set if the domain descendants's log_status should be 45 * set to %LANDLOCK_LOG_DISABLED. This is not a landlock_hierarchy 46 * configuration because it applies to future descendant domains and it does 47 * not require a current domain. 48 */ 49 u8 log_subdomains_off : 1; 50 #endif /* CONFIG_AUDIT */ 51 } __packed; 52 53 #ifdef CONFIG_AUDIT 54 55 /* Makes sure all layer executions can be stored. */ 56 static_assert(BITS_PER_TYPE(typeof_member(struct landlock_cred_security, 57 domain_exec)) >= 58 LANDLOCK_MAX_NUM_LAYERS); 59 60 #endif /* CONFIG_AUDIT */ 61 62 static inline struct landlock_cred_security * 63 landlock_cred(const struct cred *cred) 64 { 65 return cred->security + landlock_blob_sizes.lbs_cred; 66 } 67 68 static inline struct landlock_ruleset *landlock_get_current_domain(void) 69 { 70 return landlock_cred(current_cred())->domain; 71 } 72 73 /* 74 * The call needs to come from an RCU read-side critical section. 75 */ 76 static inline const struct landlock_ruleset * 77 landlock_get_task_domain(const struct task_struct *const task) 78 { 79 return landlock_cred(__task_cred(task))->domain; 80 } 81 82 static inline bool landlocked(const struct task_struct *const task) 83 { 84 bool has_dom; 85 86 if (task == current) 87 return !!landlock_get_current_domain(); 88 89 rcu_read_lock(); 90 has_dom = !!landlock_get_task_domain(task); 91 rcu_read_unlock(); 92 return has_dom; 93 } 94 95 /** 96 * landlock_get_applicable_subject - Return the subject's Landlock credential 97 * if its enforced domain applies to (i.e. 98 * handles) at least one of the access rights 99 * specified in @masks 100 * 101 * @cred: credential 102 * @masks: access masks 103 * @handle_layer: returned youngest layer handling a subset of @masks. Not set 104 * if the function returns NULL. 105 * 106 * Returns: landlock_cred(@cred) if any access rights specified in @masks is 107 * handled, or NULL otherwise. 108 */ 109 static inline const struct landlock_cred_security * 110 landlock_get_applicable_subject(const struct cred *const cred, 111 const struct access_masks masks, 112 size_t *const handle_layer) 113 { 114 const union access_masks_all masks_all = { 115 .masks = masks, 116 }; 117 const struct landlock_ruleset *domain; 118 ssize_t layer_level; 119 120 if (!cred) 121 return NULL; 122 123 domain = landlock_cred(cred)->domain; 124 if (!domain) 125 return NULL; 126 127 for (layer_level = domain->num_layers - 1; layer_level >= 0; 128 layer_level--) { 129 union access_masks_all layer = { 130 .masks = domain->access_masks[layer_level], 131 }; 132 133 if (layer.all & masks_all.all) { 134 if (handle_layer) 135 *handle_layer = layer_level; 136 137 return landlock_cred(cred); 138 } 139 } 140 141 return NULL; 142 } 143 144 __init void landlock_add_cred_hooks(void); 145 146 #endif /* _SECURITY_LANDLOCK_CRED_H */ 147