xref: /linux/security/landlock/cred.h (revision 67f8bc848ee31831336bd478e57d2f993551902e)
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 "domain.h"
20 #include "limits.h"
21 #include "ruleset.h"
22 #include "setup.h"
23 
24 /**
25  * struct landlock_cred_security - Credential security blob
26  *
27  * This structure is packed to minimize the size of struct
28  * landlock_file_security.  However, it is always aligned in the LSM cred blob,
29  * see lsm_set_blob_size().
30  *
31  * When updating this, also update landlock_cred_copy() if needed.
32  */
33 struct landlock_cred_security {
34 	/**
35 	 * @domain: Immutable domain enforced on a task.
36 	 */
37 	struct landlock_domain *domain;
38 
39 #ifdef CONFIG_SECURITY_LANDLOCK_LOG
40 	/**
41 	 * @domain_exec: Bitmask identifying the domain layers that were enforced by
42 	 * the current task's executed file (i.e. no new execve(2) since
43 	 * landlock_restrict_self(2)).
44 	 */
45 	u16 domain_exec;
46 	/**
47 	 * @log_subdomains_off: Set if the domain descendants's log_status should be
48 	 * set to %LANDLOCK_LOG_DISABLED.  This is not a landlock_hierarchy
49 	 * configuration because it applies to future descendant domains and it does
50 	 * not require a current domain.
51 	 */
52 	u8 log_subdomains_off : 1;
53 #endif /* CONFIG_SECURITY_LANDLOCK_LOG */
54 } __packed;
55 
56 #ifdef CONFIG_SECURITY_LANDLOCK_LOG
57 
58 /* Makes sure all layer executions can be stored. */
59 static_assert(BITS_PER_TYPE(typeof_member(struct landlock_cred_security,
60 					  domain_exec)) >=
61 	      LANDLOCK_MAX_NUM_LAYERS);
62 
63 #endif /* CONFIG_SECURITY_LANDLOCK_LOG */
64 
65 static inline struct landlock_cred_security *
66 landlock_cred(const struct cred *cred)
67 {
68 	return cred->security + landlock_blob_sizes.lbs_cred;
69 }
70 
71 static inline void landlock_cred_copy(struct landlock_cred_security *dst,
72 				      const struct landlock_cred_security *src)
73 {
74 	landlock_put_domain(dst->domain);
75 
76 	*dst = *src;
77 
78 	landlock_get_domain(src->domain);
79 }
80 
81 static inline struct landlock_domain *landlock_get_current_domain(void)
82 {
83 	return landlock_cred(current_cred())->domain;
84 }
85 
86 /* The call needs to come from an RCU read-side critical section. */
87 static inline const struct landlock_domain *
88 landlock_get_task_domain(const struct task_struct *const task)
89 {
90 	return landlock_cred(__task_cred(task))->domain;
91 }
92 
93 static inline bool landlocked(const struct task_struct *const task)
94 {
95 	bool has_dom;
96 
97 	if (task == current)
98 		return !!landlock_get_current_domain();
99 
100 	rcu_read_lock();
101 	has_dom = !!landlock_get_task_domain(task);
102 	rcu_read_unlock();
103 	return has_dom;
104 }
105 
106 /**
107  * landlock_get_applicable_subject - Return the subject's Landlock credential
108  *                                   if its enforced domain applies to (i.e.
109  *                                   handles) at least one of the access rights
110  *                                   specified in @masks
111  *
112  * @cred: credential
113  * @masks: access masks
114  * @handle_layer: returned youngest layer handling a subset of @masks.  Not set
115  *                if the function returns NULL.
116  *
117  * Return: landlock_cred(@cred) if any access rights specified in @masks is
118  * handled, or NULL otherwise.
119  */
120 static inline const struct landlock_cred_security *
121 landlock_get_applicable_subject(const struct cred *const cred,
122 				const struct access_masks masks,
123 				size_t *const handle_layer)
124 {
125 	const union access_masks_all masks_all = {
126 		.masks = masks,
127 	};
128 	const struct landlock_domain *domain;
129 	ssize_t layer_level;
130 
131 	if (!cred)
132 		return NULL;
133 
134 	domain = landlock_cred(cred)->domain;
135 	if (!domain)
136 		return NULL;
137 
138 	for (layer_level = domain->num_layers - 1; layer_level >= 0;
139 	     layer_level--) {
140 		union access_masks_all layer = {
141 			.masks = domain->handled_masks[layer_level],
142 		};
143 
144 		if (layer.all & masks_all.all) {
145 			if (handle_layer)
146 				*handle_layer = layer_level;
147 
148 			return landlock_cred(cred);
149 		}
150 	}
151 
152 	return NULL;
153 }
154 
155 __init void landlock_add_cred_hooks(void);
156 
157 #endif /* _SECURITY_LANDLOCK_CRED_H */
158