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