1 /* SPDX-License-Identifier: GPL-2.0 */
3 * This is <linux/capability.h>
5 * Andrew G. Morgan <morgan@kernel.org>
6 * Alexander Kjeldaas <astor@guardian.no>
7 * with help from Aleph1, Roland Buresund and Andrew Main.
9 * See here for the libcap library ("POSIX draft" compliance):
11 * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
13 #ifndef _LINUX_CAPABILITY_H
14 #define _LINUX_CAPABILITY_H
16 #include <uapi/linux/capability.h>
17 #include <linux/uidgid.h>
19 #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
20 #define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
22 extern int file_caps_enabled;
24 typedef struct kernel_cap_struct {
25 __u32 cap[_KERNEL_CAPABILITY_U32S];
28 /* same as vfs_ns_cap_data but in cpu endian and always filled completely */
29 struct cpu_vfs_cap_data {
31 kernel_cap_t permitted;
32 kernel_cap_t inheritable;
36 #define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
37 #define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
44 struct user_namespace;
47 extern const kernel_cap_t __cap_empty_set;
48 extern const kernel_cap_t __cap_init_eff_set;
51 * Internal kernel functions only
54 #define CAP_FOR_EACH_U32(__capi) \
55 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
58 * CAP_FS_MASK and CAP_NFSD_MASKS:
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.
63 * It has never meant setting security.* and trusted.* xattrs.
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
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))
77 # define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
79 #if _KERNEL_CAPABILITY_U32S != 2
80 # error Fix up hand-coded capability macro initializers
81 #else /* HAND-CODED capability initializers */
83 #define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1)
84 #define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
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), \
91 # define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
92 | CAP_TO_MASK(CAP_SYS_RESOURCE), \
95 #endif /* _KERNEL_CAPABILITY_U32S != 2 */
97 # define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
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))
103 #define CAP_BOP_ALL(c, a, b, OP) \
106 CAP_FOR_EACH_U32(__capi) { \
107 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
111 #define CAP_UOP_ALL(c, a, OP) \
114 CAP_FOR_EACH_U32(__capi) { \
115 c.cap[__capi] = OP a.cap[__capi]; \
119 static inline kernel_cap_t cap_combine(const kernel_cap_t a,
120 const kernel_cap_t b)
123 CAP_BOP_ALL(dest, a, b, |);
127 static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
128 const kernel_cap_t b)
131 CAP_BOP_ALL(dest, a, b, &);
135 static inline kernel_cap_t cap_drop(const kernel_cap_t a,
136 const kernel_cap_t drop)
139 CAP_BOP_ALL(dest, a, drop, &~);
143 static inline kernel_cap_t cap_invert(const kernel_cap_t c)
146 CAP_UOP_ALL(dest, c, ~);
150 static inline bool cap_isclear(const kernel_cap_t a)
153 CAP_FOR_EACH_U32(__capi) {
154 if (a.cap[__capi] != 0)
161 * Check if "a" is a subset of "set".
162 * return true if ALL of the capabilities in "a" are also in "set"
163 * cap_issubset(0101, 1111) will return true
164 * return false if ANY of the capabilities in "a" are not in "set"
165 * cap_issubset(1111, 0101) will return false
167 static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
170 dest = cap_drop(a, set);
171 return cap_isclear(dest);
174 /* Used to decide between falling back on the old suser() or fsuser(). */
176 static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
178 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
179 return cap_drop(a, __cap_fs_set);
182 static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
183 const kernel_cap_t permitted)
185 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
186 return cap_combine(a,
187 cap_intersect(permitted, __cap_fs_set));
190 static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
192 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
193 return cap_drop(a, __cap_fs_set);
196 static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
197 const kernel_cap_t permitted)
199 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
200 return cap_combine(a,
201 cap_intersect(permitted, __cap_nfsd_set));
204 #ifdef CONFIG_MULTIUSER
205 extern bool has_capability(struct task_struct *t, int cap);
206 extern bool has_ns_capability(struct task_struct *t,
207 struct user_namespace *ns, int cap);
208 extern bool has_capability_noaudit(struct task_struct *t, int cap);
209 extern bool has_ns_capability_noaudit(struct task_struct *t,
210 struct user_namespace *ns, int cap);
211 extern bool capable(int cap);
212 extern bool ns_capable(struct user_namespace *ns, int cap);
213 extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
214 extern bool ns_capable_setid(struct user_namespace *ns, int cap);
216 static inline bool has_capability(struct task_struct *t, int cap)
220 static inline bool has_ns_capability(struct task_struct *t,
221 struct user_namespace *ns, int cap)
225 static inline bool has_capability_noaudit(struct task_struct *t, int cap)
229 static inline bool has_ns_capability_noaudit(struct task_struct *t,
230 struct user_namespace *ns, int cap)
234 static inline bool capable(int cap)
238 static inline bool ns_capable(struct user_namespace *ns, int cap)
242 static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
246 static inline bool ns_capable_setid(struct user_namespace *ns, int cap)
250 #endif /* CONFIG_MULTIUSER */
251 bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
252 struct mnt_idmap *idmap,
253 const struct inode *inode);
254 bool capable_wrt_inode_uidgid(struct mnt_idmap *idmap,
255 const struct inode *inode, int cap);
256 extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
257 extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
258 static inline bool perfmon_capable(void)
260 return capable(CAP_PERFMON) || capable(CAP_SYS_ADMIN);
263 static inline bool bpf_capable(void)
265 return capable(CAP_BPF) || capable(CAP_SYS_ADMIN);
268 static inline bool checkpoint_restore_ns_capable(struct user_namespace *ns)
270 return ns_capable(ns, CAP_CHECKPOINT_RESTORE) ||
271 ns_capable(ns, CAP_SYS_ADMIN);
274 /* audit system wants to get cap info from files as well */
275 int get_vfs_caps_from_disk(struct mnt_idmap *idmap,
276 const struct dentry *dentry,
277 struct cpu_vfs_cap_data *cpu_caps);
279 int cap_convert_nscap(struct mnt_idmap *idmap, struct dentry *dentry,
280 const void **ivalue, size_t size);
282 #endif /* !_LINUX_CAPABILITY_H */