]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * This is <linux/capability.h> | |
3 | * | |
b5376771 | 4 | * Andrew G. Morgan <morgan@kernel.org> |
1da177e4 LT |
5 | * Alexander Kjeldaas <astor@guardian.no> |
6 | * with help from Aleph1, Roland Buresund and Andrew Main. | |
7 | * | |
8 | * See here for the libcap library ("POSIX draft" compliance): | |
9 | * | |
b5376771 SH |
10 | * ftp://linux.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/ |
11 | */ | |
1da177e4 LT |
12 | |
13 | #ifndef _LINUX_CAPABILITY_H | |
14 | #define _LINUX_CAPABILITY_H | |
15 | ||
16 | #include <linux/types.h> | |
1da177e4 | 17 | |
b7add02d AM |
18 | struct task_struct; |
19 | ||
1da177e4 LT |
20 | /* User-level do most of the mapping between kernel and user |
21 | capabilities based on the version tag given by the kernel. The | |
22 | kernel might be somewhat backwards compatible, but don't bet on | |
23 | it. */ | |
24 | ||
e338d263 | 25 | /* Note, cap_t, is defined by POSIX (draft) to be an "opaque" pointer to |
1da177e4 LT |
26 | a set of three capability sets. The transposition of 3*the |
27 | following structure to such a composite is better handled in a user | |
28 | library since the draft standard requires the use of malloc/free | |
29 | etc.. */ | |
b5376771 | 30 | |
e338d263 AM |
31 | #define _LINUX_CAPABILITY_VERSION_1 0x19980330 |
32 | #define _LINUX_CAPABILITY_U32S_1 1 | |
33 | ||
34 | #define _LINUX_CAPABILITY_VERSION_2 0x20071026 | |
35 | #define _LINUX_CAPABILITY_U32S_2 2 | |
36 | ||
37 | #define _LINUX_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_2 | |
38 | #define _LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_2 | |
1da177e4 LT |
39 | |
40 | typedef struct __user_cap_header_struct { | |
41 | __u32 version; | |
42 | int pid; | |
43 | } __user *cap_user_header_t; | |
b5376771 | 44 | |
1da177e4 LT |
45 | typedef struct __user_cap_data_struct { |
46 | __u32 effective; | |
47 | __u32 permitted; | |
48 | __u32 inheritable; | |
49 | } __user *cap_user_data_t; | |
1da177e4 | 50 | |
e338d263 | 51 | |
b5376771 SH |
52 | #define XATTR_CAPS_SUFFIX "capability" |
53 | #define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX | |
54 | ||
b5376771 | 55 | #define VFS_CAP_REVISION_MASK 0xFF000000 |
e338d263 AM |
56 | #define VFS_CAP_FLAGS_MASK ~VFS_CAP_REVISION_MASK |
57 | #define VFS_CAP_FLAGS_EFFECTIVE 0x000001 | |
58 | ||
b5376771 | 59 | #define VFS_CAP_REVISION_1 0x01000000 |
e338d263 AM |
60 | #define VFS_CAP_U32_1 1 |
61 | #define XATTR_CAPS_SZ_1 (sizeof(__le32)*(1 + 2*VFS_CAP_U32_1)) | |
b5376771 | 62 | |
e338d263 AM |
63 | #define VFS_CAP_REVISION_2 0x02000000 |
64 | #define VFS_CAP_U32_2 2 | |
65 | #define XATTR_CAPS_SZ_2 (sizeof(__le32)*(1 + 2*VFS_CAP_U32_2)) | |
66 | ||
67 | #define XATTR_CAPS_SZ XATTR_CAPS_SZ_2 | |
68 | #define VFS_CAP_U32 VFS_CAP_U32_2 | |
69 | #define VFS_CAP_REVISION VFS_CAP_REVISION_2 | |
b5376771 | 70 | |
b5376771 SH |
71 | |
72 | struct vfs_cap_data { | |
e338d263 | 73 | __le32 magic_etc; /* Little endian */ |
8f6936f4 | 74 | struct { |
e338d263 AM |
75 | __le32 permitted; /* Little endian */ |
76 | __le32 inheritable; /* Little endian */ | |
77 | } data[VFS_CAP_U32]; | |
b5376771 SH |
78 | }; |
79 | ||
80 | #ifdef __KERNEL__ | |
1da177e4 | 81 | |
1da177e4 | 82 | typedef struct kernel_cap_struct { |
e338d263 | 83 | __u32 cap[_LINUX_CAPABILITY_U32S]; |
1da177e4 LT |
84 | } kernel_cap_t; |
85 | ||
e338d263 | 86 | #define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct)) |
1da177e4 LT |
87 | #define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t)) |
88 | ||
89 | #endif | |
90 | ||
91 | ||
92 | /** | |
b5376771 | 93 | ** POSIX-draft defined capabilities. |
1da177e4 LT |
94 | **/ |
95 | ||
96 | /* In a system with the [_POSIX_CHOWN_RESTRICTED] option defined, this | |
97 | overrides the restriction of changing file ownership and group | |
98 | ownership. */ | |
99 | ||
100 | #define CAP_CHOWN 0 | |
101 | ||
102 | /* Override all DAC access, including ACL execute access if | |
103 | [_POSIX_ACL] is defined. Excluding DAC access covered by | |
104 | CAP_LINUX_IMMUTABLE. */ | |
105 | ||
106 | #define CAP_DAC_OVERRIDE 1 | |
107 | ||
108 | /* Overrides all DAC restrictions regarding read and search on files | |
109 | and directories, including ACL restrictions if [_POSIX_ACL] is | |
110 | defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */ | |
111 | ||
112 | #define CAP_DAC_READ_SEARCH 2 | |
b5376771 | 113 | |
1da177e4 LT |
114 | /* Overrides all restrictions about allowed operations on files, where |
115 | file owner ID must be equal to the user ID, except where CAP_FSETID | |
116 | is applicable. It doesn't override MAC and DAC restrictions. */ | |
117 | ||
118 | #define CAP_FOWNER 3 | |
119 | ||
120 | /* Overrides the following restrictions that the effective user ID | |
121 | shall match the file owner ID when setting the S_ISUID and S_ISGID | |
122 | bits on that file; that the effective group ID (or one of the | |
123 | supplementary group IDs) shall match the file owner ID when setting | |
124 | the S_ISGID bit on that file; that the S_ISUID and S_ISGID bits are | |
125 | cleared on successful return from chown(2) (not implemented). */ | |
126 | ||
127 | #define CAP_FSETID 4 | |
128 | ||
1da177e4 LT |
129 | /* Overrides the restriction that the real or effective user ID of a |
130 | process sending a signal must match the real or effective user ID | |
131 | of the process receiving the signal. */ | |
132 | ||
133 | #define CAP_KILL 5 | |
134 | ||
135 | /* Allows setgid(2) manipulation */ | |
136 | /* Allows setgroups(2) */ | |
137 | /* Allows forged gids on socket credentials passing. */ | |
138 | ||
139 | #define CAP_SETGID 6 | |
140 | ||
141 | /* Allows set*uid(2) manipulation (including fsuid). */ | |
142 | /* Allows forged pids on socket credentials passing. */ | |
143 | ||
144 | #define CAP_SETUID 7 | |
145 | ||
146 | ||
147 | /** | |
148 | ** Linux-specific capabilities | |
149 | **/ | |
150 | ||
e338d263 AM |
151 | /* Without VFS support for capabilities: |
152 | * Transfer any capability in your permitted set to any pid, | |
153 | * remove any capability in your permitted set from any pid | |
154 | * With VFS support for capabilities (neither of above, but) | |
155 | * Add any capability to the current process' inheritable set | |
156 | */ | |
1da177e4 LT |
157 | |
158 | #define CAP_SETPCAP 8 | |
159 | ||
160 | /* Allow modification of S_IMMUTABLE and S_APPEND file attributes */ | |
161 | ||
162 | #define CAP_LINUX_IMMUTABLE 9 | |
163 | ||
164 | /* Allows binding to TCP/UDP sockets below 1024 */ | |
165 | /* Allows binding to ATM VCIs below 32 */ | |
166 | ||
167 | #define CAP_NET_BIND_SERVICE 10 | |
168 | ||
169 | /* Allow broadcasting, listen to multicast */ | |
170 | ||
171 | #define CAP_NET_BROADCAST 11 | |
172 | ||
173 | /* Allow interface configuration */ | |
174 | /* Allow administration of IP firewall, masquerading and accounting */ | |
175 | /* Allow setting debug option on sockets */ | |
176 | /* Allow modification of routing tables */ | |
177 | /* Allow setting arbitrary process / process group ownership on | |
178 | sockets */ | |
179 | /* Allow binding to any address for transparent proxying */ | |
180 | /* Allow setting TOS (type of service) */ | |
181 | /* Allow setting promiscuous mode */ | |
182 | /* Allow clearing driver statistics */ | |
183 | /* Allow multicasting */ | |
184 | /* Allow read/write of device-specific registers */ | |
185 | /* Allow activation of ATM control sockets */ | |
186 | ||
187 | #define CAP_NET_ADMIN 12 | |
188 | ||
189 | /* Allow use of RAW sockets */ | |
190 | /* Allow use of PACKET sockets */ | |
191 | ||
192 | #define CAP_NET_RAW 13 | |
193 | ||
194 | /* Allow locking of shared memory segments */ | |
195 | /* Allow mlock and mlockall (which doesn't really have anything to do | |
196 | with IPC) */ | |
197 | ||
198 | #define CAP_IPC_LOCK 14 | |
199 | ||
200 | /* Override IPC ownership checks */ | |
201 | ||
202 | #define CAP_IPC_OWNER 15 | |
203 | ||
204 | /* Insert and remove kernel modules - modify kernel without limit */ | |
205 | /* Modify cap_bset */ | |
206 | #define CAP_SYS_MODULE 16 | |
207 | ||
208 | /* Allow ioperm/iopl access */ | |
209 | /* Allow sending USB messages to any device via /proc/bus/usb */ | |
210 | ||
211 | #define CAP_SYS_RAWIO 17 | |
212 | ||
213 | /* Allow use of chroot() */ | |
214 | ||
215 | #define CAP_SYS_CHROOT 18 | |
216 | ||
217 | /* Allow ptrace() of any process */ | |
218 | ||
219 | #define CAP_SYS_PTRACE 19 | |
220 | ||
221 | /* Allow configuration of process accounting */ | |
222 | ||
223 | #define CAP_SYS_PACCT 20 | |
224 | ||
225 | /* Allow configuration of the secure attention key */ | |
226 | /* Allow administration of the random device */ | |
227 | /* Allow examination and configuration of disk quotas */ | |
228 | /* Allow configuring the kernel's syslog (printk behaviour) */ | |
229 | /* Allow setting the domainname */ | |
230 | /* Allow setting the hostname */ | |
231 | /* Allow calling bdflush() */ | |
232 | /* Allow mount() and umount(), setting up new smb connection */ | |
233 | /* Allow some autofs root ioctls */ | |
234 | /* Allow nfsservctl */ | |
235 | /* Allow VM86_REQUEST_IRQ */ | |
236 | /* Allow to read/write pci config on alpha */ | |
237 | /* Allow irix_prctl on mips (setstacksize) */ | |
238 | /* Allow flushing all cache on m68k (sys_cacheflush) */ | |
239 | /* Allow removing semaphores */ | |
240 | /* Used instead of CAP_CHOWN to "chown" IPC message queues, semaphores | |
241 | and shared memory */ | |
242 | /* Allow locking/unlocking of shared memory segment */ | |
243 | /* Allow turning swap on/off */ | |
244 | /* Allow forged pids on socket credentials passing */ | |
245 | /* Allow setting readahead and flushing buffers on block devices */ | |
246 | /* Allow setting geometry in floppy driver */ | |
247 | /* Allow turning DMA on/off in xd driver */ | |
248 | /* Allow administration of md devices (mostly the above, but some | |
249 | extra ioctls) */ | |
250 | /* Allow tuning the ide driver */ | |
251 | /* Allow access to the nvram device */ | |
252 | /* Allow administration of apm_bios, serial and bttv (TV) device */ | |
253 | /* Allow manufacturer commands in isdn CAPI support driver */ | |
254 | /* Allow reading non-standardized portions of pci configuration space */ | |
255 | /* Allow DDI debug ioctl on sbpcd driver */ | |
256 | /* Allow setting up serial ports */ | |
257 | /* Allow sending raw qic-117 commands */ | |
258 | /* Allow enabling/disabling tagged queuing on SCSI controllers and sending | |
259 | arbitrary SCSI commands */ | |
260 | /* Allow setting encryption key on loopback filesystem */ | |
bce5f6ba | 261 | /* Allow setting zone reclaim policy */ |
1da177e4 LT |
262 | |
263 | #define CAP_SYS_ADMIN 21 | |
264 | ||
265 | /* Allow use of reboot() */ | |
266 | ||
267 | #define CAP_SYS_BOOT 22 | |
268 | ||
269 | /* Allow raising priority and setting priority on other (different | |
270 | UID) processes */ | |
271 | /* Allow use of FIFO and round-robin (realtime) scheduling on own | |
272 | processes and setting the scheduling algorithm used by another | |
273 | process. */ | |
274 | /* Allow setting cpu affinity on other processes */ | |
275 | ||
276 | #define CAP_SYS_NICE 23 | |
277 | ||
278 | /* Override resource limits. Set resource limits. */ | |
279 | /* Override quota limits. */ | |
280 | /* Override reserved space on ext2 filesystem */ | |
281 | /* Modify data journaling mode on ext3 filesystem (uses journaling | |
282 | resources) */ | |
b5376771 | 283 | /* NOTE: ext2 honors fsuid when checking for resource overrides, so |
1da177e4 LT |
284 | you can override using fsuid too */ |
285 | /* Override size restrictions on IPC message queues */ | |
286 | /* Allow more than 64hz interrupts from the real-time clock */ | |
287 | /* Override max number of consoles on console allocation */ | |
288 | /* Override max number of keymaps */ | |
289 | ||
290 | #define CAP_SYS_RESOURCE 24 | |
291 | ||
292 | /* Allow manipulation of system clock */ | |
293 | /* Allow irix_stime on mips */ | |
294 | /* Allow setting the real-time clock */ | |
295 | ||
296 | #define CAP_SYS_TIME 25 | |
297 | ||
298 | /* Allow configuration of tty devices */ | |
299 | /* Allow vhangup() of tty */ | |
300 | ||
301 | #define CAP_SYS_TTY_CONFIG 26 | |
302 | ||
303 | /* Allow the privileged aspects of mknod() */ | |
304 | ||
305 | #define CAP_MKNOD 27 | |
306 | ||
307 | /* Allow taking of leases on files */ | |
308 | ||
309 | #define CAP_LEASE 28 | |
310 | ||
311 | #define CAP_AUDIT_WRITE 29 | |
312 | ||
313 | #define CAP_AUDIT_CONTROL 30 | |
314 | ||
b5376771 SH |
315 | #define CAP_SETFCAP 31 |
316 | ||
e338d263 AM |
317 | /* |
318 | * Bit location of each capability (used by user-space library and kernel) | |
319 | */ | |
320 | ||
321 | #define CAP_TO_INDEX(x) ((x) >> 5) /* 1 << 5 == bits in __u32 */ | |
322 | #define CAP_TO_MASK(x) (1 << ((x) & 31)) /* mask for indexed __u32 */ | |
323 | ||
1da177e4 | 324 | #ifdef __KERNEL__ |
1da177e4 LT |
325 | |
326 | /* | |
327 | * Internal kernel functions only | |
328 | */ | |
b5376771 | 329 | |
e338d263 AM |
330 | #define CAP_FOR_EACH_U32(__capi) \ |
331 | for (__capi = 0; __capi < _LINUX_CAPABILITY_U32S; ++__capi) | |
332 | ||
333 | # define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \ | |
334 | | CAP_TO_MASK(CAP_DAC_OVERRIDE) \ | |
335 | | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \ | |
336 | | CAP_TO_MASK(CAP_FOWNER) \ | |
337 | | CAP_TO_MASK(CAP_FSETID)) | |
338 | ||
339 | #if _LINUX_CAPABILITY_U32S != 2 | |
340 | # error Fix up hand-coded capability macro initializers | |
341 | #else /* HAND-CODED capability initializers */ | |
342 | ||
343 | # define CAP_EMPTY_SET {{ 0, 0 }} | |
344 | # define CAP_FULL_SET {{ ~0, ~0 }} | |
345 | # define CAP_INIT_EFF_SET {{ ~CAP_TO_MASK(CAP_SETPCAP), ~0 }} | |
346 | # define CAP_FS_SET {{ CAP_FS_MASK_B0, 0 }} | |
347 | # define CAP_NFSD_SET {{ CAP_FS_MASK_B0|CAP_TO_MASK(CAP_SYS_RESOURCE), 0 }} | |
348 | ||
349 | #endif /* _LINUX_CAPABILITY_U32S != 2 */ | |
350 | ||
351 | #define CAP_INIT_INH_SET CAP_EMPTY_SET | |
352 | ||
353 | # define cap_clear(c) do { (c) = __cap_empty_set; } while (0) | |
354 | # define cap_set_full(c) do { (c) = __cap_full_set; } while (0) | |
355 | # define cap_set_init_eff(c) do { (c) = __cap_init_eff_set; } while (0) | |
356 | ||
357 | #define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag)) | |
358 | #define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag)) | |
359 | #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag)) | |
360 | ||
361 | #define CAP_BOP_ALL(c, a, b, OP) \ | |
362 | do { \ | |
363 | unsigned __capi; \ | |
364 | CAP_FOR_EACH_U32(__capi) { \ | |
365 | c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \ | |
366 | } \ | |
367 | } while (0) | |
368 | ||
369 | #define CAP_UOP_ALL(c, a, OP) \ | |
370 | do { \ | |
371 | unsigned __capi; \ | |
372 | CAP_FOR_EACH_U32(__capi) { \ | |
373 | c.cap[__capi] = OP a.cap[__capi]; \ | |
374 | } \ | |
375 | } while (0) | |
376 | ||
377 | static inline kernel_cap_t cap_combine(const kernel_cap_t a, | |
378 | const kernel_cap_t b) | |
379 | { | |
380 | kernel_cap_t dest; | |
381 | CAP_BOP_ALL(dest, a, b, |); | |
382 | return dest; | |
383 | } | |
1da177e4 | 384 | |
e338d263 AM |
385 | static inline kernel_cap_t cap_intersect(const kernel_cap_t a, |
386 | const kernel_cap_t b) | |
387 | { | |
388 | kernel_cap_t dest; | |
389 | CAP_BOP_ALL(dest, a, b, &); | |
390 | return dest; | |
391 | } | |
1da177e4 | 392 | |
e338d263 AM |
393 | static inline kernel_cap_t cap_drop(const kernel_cap_t a, |
394 | const kernel_cap_t drop) | |
395 | { | |
396 | kernel_cap_t dest; | |
397 | CAP_BOP_ALL(dest, a, drop, &~); | |
398 | return dest; | |
399 | } | |
1da177e4 | 400 | |
e338d263 AM |
401 | static inline kernel_cap_t cap_invert(const kernel_cap_t c) |
402 | { | |
403 | kernel_cap_t dest; | |
404 | CAP_UOP_ALL(dest, c, ~); | |
405 | return dest; | |
406 | } | |
1da177e4 | 407 | |
e338d263 AM |
408 | static inline int cap_isclear(const kernel_cap_t a) |
409 | { | |
410 | unsigned __capi; | |
411 | CAP_FOR_EACH_U32(__capi) { | |
412 | if (a.cap[__capi] != 0) | |
413 | return 0; | |
414 | } | |
415 | return 1; | |
416 | } | |
1da177e4 | 417 | |
e338d263 AM |
418 | static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set) |
419 | { | |
420 | kernel_cap_t dest; | |
421 | dest = cap_drop(a, set); | |
422 | return cap_isclear(dest); | |
423 | } | |
1da177e4 | 424 | |
e338d263 | 425 | /* Used to decide between falling back on the old suser() or fsuser(). */ |
1da177e4 | 426 | |
e338d263 | 427 | static inline int cap_is_fs_cap(int cap) |
1da177e4 | 428 | { |
e338d263 AM |
429 | const kernel_cap_t __cap_fs_set = CAP_FS_SET; |
430 | return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]); | |
1da177e4 LT |
431 | } |
432 | ||
e338d263 | 433 | static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a) |
1da177e4 | 434 | { |
e338d263 AM |
435 | const kernel_cap_t __cap_fs_set = CAP_FS_SET; |
436 | return cap_drop(a, __cap_fs_set); | |
1da177e4 LT |
437 | } |
438 | ||
e338d263 AM |
439 | static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a, |
440 | const kernel_cap_t permitted) | |
1da177e4 | 441 | { |
e338d263 AM |
442 | const kernel_cap_t __cap_fs_set = CAP_FS_SET; |
443 | return cap_combine(a, | |
444 | cap_intersect(permitted, __cap_fs_set)); | |
1da177e4 LT |
445 | } |
446 | ||
e338d263 | 447 | static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a) |
1da177e4 | 448 | { |
e338d263 AM |
449 | const kernel_cap_t __cap_fs_set = CAP_NFSD_SET; |
450 | return cap_drop(a, __cap_fs_set); | |
1da177e4 LT |
451 | } |
452 | ||
e338d263 AM |
453 | static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a, |
454 | const kernel_cap_t permitted) | |
455 | { | |
456 | const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET; | |
457 | return cap_combine(a, | |
458 | cap_intersect(permitted, __cap_nfsd_set)); | |
459 | } | |
1da177e4 | 460 | |
e338d263 AM |
461 | extern const kernel_cap_t __cap_empty_set; |
462 | extern const kernel_cap_t __cap_full_set; | |
463 | extern const kernel_cap_t __cap_init_eff_set; | |
1da177e4 | 464 | |
12b5989b CW |
465 | int capable(int cap); |
466 | int __capable(struct task_struct *t, int cap); | |
c59ede7b | 467 | |
1da177e4 LT |
468 | #endif /* __KERNEL__ */ |
469 | ||
470 | #endif /* !_LINUX_CAPABILITY_H */ |