xref: /linux-6.15/include/linux/capability.h (revision a4eecbae)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * This is <linux/capability.h>
4  *
5  * Andrew G. Morgan <[email protected]>
6  * Alexander Kjeldaas <[email protected]>
7  * with help from Aleph1, Roland Buresund and Andrew Main.
8  *
9  * See here for the libcap library ("POSIX draft" compliance):
10  *
11  * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
12  */
13 #ifndef _LINUX_CAPABILITY_H
14 #define _LINUX_CAPABILITY_H
15 
16 #include <uapi/linux/capability.h>
17 #include <linux/uidgid.h>
18 
19 #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
20 #define _KERNEL_CAPABILITY_U32S    _LINUX_CAPABILITY_U32S_3
21 
22 extern int file_caps_enabled;
23 
24 typedef struct kernel_cap_struct {
25 	__u32 cap[_KERNEL_CAPABILITY_U32S];
26 } kernel_cap_t;
27 
28 /* same as vfs_ns_cap_data but in cpu endian and always filled completely */
29 struct cpu_vfs_cap_data {
30 	__u32 magic_etc;
31 	kernel_cap_t permitted;
32 	kernel_cap_t inheritable;
33 	kuid_t rootid;
34 };
35 
36 #define _USER_CAP_HEADER_SIZE  (sizeof(struct __user_cap_header_struct))
37 #define _KERNEL_CAP_T_SIZE     (sizeof(kernel_cap_t))
38 
39 
40 struct file;
41 struct inode;
42 struct dentry;
43 struct task_struct;
44 struct user_namespace;
45 struct mnt_idmap;
46 
47 extern const kernel_cap_t __cap_empty_set;
48 extern const kernel_cap_t __cap_init_eff_set;
49 
50 /*
51  * Internal kernel functions only
52  */
53 
54 #define CAP_FOR_EACH_U32(__capi)  \
55 	for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
56 
57 /*
58  * CAP_FS_MASK and CAP_NFSD_MASKS:
59  *
60  * The fs mask is all the privileges that fsuid==0 historically meant.
61  * At one time in the past, that included CAP_MKNOD and CAP_LINUX_IMMUTABLE.
62  *
63  * It has never meant setting security.* and trusted.* xattrs.
64  *
65  * We could also define fsmask as follows:
66  *   1. CAP_FS_MASK is the privilege to bypass all fs-related DAC permissions
67  *   2. The security.* and trusted.* xattrs are fs-related MAC permissions
68  */
69 
70 # define CAP_FS_MASK_B0     (CAP_TO_MASK(CAP_CHOWN)		\
71 			    | CAP_TO_MASK(CAP_MKNOD)		\
72 			    | CAP_TO_MASK(CAP_DAC_OVERRIDE)	\
73 			    | CAP_TO_MASK(CAP_DAC_READ_SEARCH)	\
74 			    | CAP_TO_MASK(CAP_FOWNER)		\
75 			    | CAP_TO_MASK(CAP_FSETID))
76 
77 # define CAP_FS_MASK_B1     (CAP_TO_MASK(CAP_MAC_OVERRIDE))
78 
79 #if _KERNEL_CAPABILITY_U32S != 2
80 # error Fix up hand-coded capability macro initializers
81 #else /* HAND-CODED capability initializers */
82 
83 #define CAP_LAST_U32			((_KERNEL_CAPABILITY_U32S) - 1)
84 #define CAP_LAST_U32_VALID_MASK		(CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
85 
86 # define CAP_EMPTY_SET    ((kernel_cap_t){{ 0, 0 }})
87 # define CAP_FULL_SET     ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
88 # define CAP_FS_SET       ((kernel_cap_t){{ CAP_FS_MASK_B0 \
89 				    | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
90 				    CAP_FS_MASK_B1 } })
91 # define CAP_NFSD_SET     ((kernel_cap_t){{ CAP_FS_MASK_B0 \
92 				    | CAP_TO_MASK(CAP_SYS_RESOURCE), \
93 				    CAP_FS_MASK_B1 } })
94 
95 #endif /* _KERNEL_CAPABILITY_U32S != 2 */
96 
97 # define cap_clear(c)         do { (c) = __cap_empty_set; } while (0)
98 
99 #define cap_raise(c, flag)  ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
100 #define cap_lower(c, flag)  ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
101 #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
102 
103 #define CAP_BOP_ALL(c, a, b, OP)                                    \
104 do {                                                                \
105 	unsigned __capi;                                            \
106 	CAP_FOR_EACH_U32(__capi) {                                  \
107 		c.cap[__capi] = a.cap[__capi] OP b.cap[__capi];     \
108 	}                                                           \
109 } while (0)
110 
111 #define CAP_UOP_ALL(c, a, OP)                                       \
112 do {                                                                \
113 	unsigned __capi;                                            \
114 	CAP_FOR_EACH_U32(__capi) {                                  \
115 		c.cap[__capi] = OP a.cap[__capi];                   \
116 	}                                                           \
117 } while (0)
118 
119 static inline kernel_cap_t cap_combine(const kernel_cap_t a,
120 				       const kernel_cap_t b)
121 {
122 	kernel_cap_t dest;
123 	CAP_BOP_ALL(dest, a, b, |);
124 	return dest;
125 }
126 
127 static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
128 					 const kernel_cap_t b)
129 {
130 	kernel_cap_t dest;
131 	CAP_BOP_ALL(dest, a, b, &);
132 	return dest;
133 }
134 
135 static inline kernel_cap_t cap_drop(const kernel_cap_t a,
136 				    const kernel_cap_t drop)
137 {
138 	kernel_cap_t dest;
139 	CAP_BOP_ALL(dest, a, drop, &~);
140 	return dest;
141 }
142 
143 static inline kernel_cap_t cap_invert(const kernel_cap_t c)
144 {
145 	kernel_cap_t dest;
146 	CAP_UOP_ALL(dest, c, ~);
147 	return dest;
148 }
149 
150 static inline bool cap_isclear(const kernel_cap_t a)
151 {
152 	unsigned __capi;
153 	CAP_FOR_EACH_U32(__capi) {
154 		if (a.cap[__capi] != 0)
155 			return false;
156 	}
157 	return true;
158 }
159 
160 static inline bool cap_isidentical(const kernel_cap_t a, const kernel_cap_t b)
161 {
162 	unsigned __capi;
163 	CAP_FOR_EACH_U32(__capi) {
164 		if (a.cap[__capi] != b.cap[__capi])
165 			return false;
166 	}
167 	return true;
168 }
169 
170 /*
171  * Check if "a" is a subset of "set".
172  * return true if ALL of the capabilities in "a" are also in "set"
173  *	cap_issubset(0101, 1111) will return true
174  * return false if ANY of the capabilities in "a" are not in "set"
175  *	cap_issubset(1111, 0101) will return false
176  */
177 static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
178 {
179 	kernel_cap_t dest;
180 	dest = cap_drop(a, set);
181 	return cap_isclear(dest);
182 }
183 
184 /* Used to decide between falling back on the old suser() or fsuser(). */
185 
186 static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
187 {
188 	const kernel_cap_t __cap_fs_set = CAP_FS_SET;
189 	return cap_drop(a, __cap_fs_set);
190 }
191 
192 static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
193 					    const kernel_cap_t permitted)
194 {
195 	const kernel_cap_t __cap_fs_set = CAP_FS_SET;
196 	return cap_combine(a,
197 			   cap_intersect(permitted, __cap_fs_set));
198 }
199 
200 static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
201 {
202 	const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
203 	return cap_drop(a, __cap_fs_set);
204 }
205 
206 static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
207 					      const kernel_cap_t permitted)
208 {
209 	const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
210 	return cap_combine(a,
211 			   cap_intersect(permitted, __cap_nfsd_set));
212 }
213 
214 #ifdef CONFIG_MULTIUSER
215 extern bool has_capability(struct task_struct *t, int cap);
216 extern bool has_ns_capability(struct task_struct *t,
217 			      struct user_namespace *ns, int cap);
218 extern bool has_capability_noaudit(struct task_struct *t, int cap);
219 extern bool has_ns_capability_noaudit(struct task_struct *t,
220 				      struct user_namespace *ns, int cap);
221 extern bool capable(int cap);
222 extern bool ns_capable(struct user_namespace *ns, int cap);
223 extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
224 extern bool ns_capable_setid(struct user_namespace *ns, int cap);
225 #else
226 static inline bool has_capability(struct task_struct *t, int cap)
227 {
228 	return true;
229 }
230 static inline bool has_ns_capability(struct task_struct *t,
231 			      struct user_namespace *ns, int cap)
232 {
233 	return true;
234 }
235 static inline bool has_capability_noaudit(struct task_struct *t, int cap)
236 {
237 	return true;
238 }
239 static inline bool has_ns_capability_noaudit(struct task_struct *t,
240 				      struct user_namespace *ns, int cap)
241 {
242 	return true;
243 }
244 static inline bool capable(int cap)
245 {
246 	return true;
247 }
248 static inline bool ns_capable(struct user_namespace *ns, int cap)
249 {
250 	return true;
251 }
252 static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
253 {
254 	return true;
255 }
256 static inline bool ns_capable_setid(struct user_namespace *ns, int cap)
257 {
258 	return true;
259 }
260 #endif /* CONFIG_MULTIUSER */
261 bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
262 				 struct mnt_idmap *idmap,
263 				 const struct inode *inode);
264 bool capable_wrt_inode_uidgid(struct mnt_idmap *idmap,
265 			      const struct inode *inode, int cap);
266 extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
267 extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
268 static inline bool perfmon_capable(void)
269 {
270 	return capable(CAP_PERFMON) || capable(CAP_SYS_ADMIN);
271 }
272 
273 static inline bool bpf_capable(void)
274 {
275 	return capable(CAP_BPF) || capable(CAP_SYS_ADMIN);
276 }
277 
278 static inline bool checkpoint_restore_ns_capable(struct user_namespace *ns)
279 {
280 	return ns_capable(ns, CAP_CHECKPOINT_RESTORE) ||
281 		ns_capable(ns, CAP_SYS_ADMIN);
282 }
283 
284 /* audit system wants to get cap info from files as well */
285 int get_vfs_caps_from_disk(struct mnt_idmap *idmap,
286 			   const struct dentry *dentry,
287 			   struct cpu_vfs_cap_data *cpu_caps);
288 
289 int cap_convert_nscap(struct mnt_idmap *idmap, struct dentry *dentry,
290 		      const void **ivalue, size_t size);
291 
292 #endif /* !_LINUX_CAPABILITY_H */
293