blob: 7d50ff6d269fc6eb75d298e36140dfc012a8daf5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This is <linux/capability.h>
3 *
Serge E. Hallynb5376772007-10-16 23:31:36 -07004 * Andrew G. Morgan <morgan@kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 *
Serge E. Hallynb5376772007-10-16 23:31:36 -070010 * ftp://linux.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#ifndef _LINUX_CAPABILITY_H
14#define _LINUX_CAPABILITY_H
15
16#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Andrew Mortonb7add022007-05-23 13:57:39 -070018struct task_struct;
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* 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
Andrew Morgane338d262008-02-04 22:29:42 -080025/* Note, cap_t, is defined by POSIX (draft) to be an "opaque" pointer to
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 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.. */
Serge E. Hallynb5376772007-10-16 23:31:36 -070030
Andrew Morgane338d262008-02-04 22:29:42 -080031#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40typedef struct __user_cap_header_struct {
41 __u32 version;
42 int pid;
43} __user *cap_user_header_t;
Serge E. Hallynb5376772007-10-16 23:31:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045typedef struct __user_cap_data_struct {
46 __u32 effective;
47 __u32 permitted;
48 __u32 inheritable;
49} __user *cap_user_data_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andrew Morgane338d262008-02-04 22:29:42 -080051
Serge E. Hallynb5376772007-10-16 23:31:36 -070052#define XATTR_CAPS_SUFFIX "capability"
53#define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX
54
Serge E. Hallynb5376772007-10-16 23:31:36 -070055#define VFS_CAP_REVISION_MASK 0xFF000000
Serge E. Hallynb5376772007-10-16 23:31:36 -070056#define VFS_CAP_FLAGS_MASK ~VFS_CAP_REVISION_MASK
57#define VFS_CAP_FLAGS_EFFECTIVE 0x000001
58
Andrew Morgane338d262008-02-04 22:29:42 -080059#define VFS_CAP_REVISION_1 0x01000000
60#define VFS_CAP_U32_1 1
61#define XATTR_CAPS_SZ_1 (sizeof(__le32)*(1 + 2*VFS_CAP_U32_1))
62
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
70
71
Serge E. Hallynb5376772007-10-16 23:31:36 -070072struct vfs_cap_data {
Andrew Morgane338d262008-02-04 22:29:42 -080073 __le32 magic_etc; /* Little endian */
Andrew Morton8f6936f2008-02-04 22:29:41 -080074 struct {
Andrew Morgane338d262008-02-04 22:29:42 -080075 __le32 permitted; /* Little endian */
76 __le32 inheritable; /* Little endian */
77 } data[VFS_CAP_U32];
Serge E. Hallynb5376772007-10-16 23:31:36 -070078};
79
80#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082typedef struct kernel_cap_struct {
Andrew Morgane338d262008-02-04 22:29:42 -080083 __u32 cap[_LINUX_CAPABILITY_U32S];
Linus Torvalds1da177e2005-04-16 15:20:36 -070084} kernel_cap_t;
85
Andrew Morgane338d262008-02-04 22:29:42 -080086#define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
88
89#endif
90
91
92/**
Serge E. Hallynb5376772007-10-16 23:31:36 -070093 ** POSIX-draft defined capabilities.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 **/
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
Serge E. Hallynb5376772007-10-16 23:31:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* 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
Andrew Morgane338d262008-02-04 22:29:42 -0800151/* 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)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800155 * Add any capability from current's capability bounding set
156 * to the current process' inheritable set
157 * Allow taking bits out of capability bounding set
Andrew Morgane338d262008-02-04 22:29:42 -0800158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160#define CAP_SETPCAP 8
161
162/* Allow modification of S_IMMUTABLE and S_APPEND file attributes */
163
164#define CAP_LINUX_IMMUTABLE 9
165
166/* Allows binding to TCP/UDP sockets below 1024 */
167/* Allows binding to ATM VCIs below 32 */
168
169#define CAP_NET_BIND_SERVICE 10
170
171/* Allow broadcasting, listen to multicast */
172
173#define CAP_NET_BROADCAST 11
174
175/* Allow interface configuration */
176/* Allow administration of IP firewall, masquerading and accounting */
177/* Allow setting debug option on sockets */
178/* Allow modification of routing tables */
179/* Allow setting arbitrary process / process group ownership on
180 sockets */
181/* Allow binding to any address for transparent proxying */
182/* Allow setting TOS (type of service) */
183/* Allow setting promiscuous mode */
184/* Allow clearing driver statistics */
185/* Allow multicasting */
186/* Allow read/write of device-specific registers */
187/* Allow activation of ATM control sockets */
188
189#define CAP_NET_ADMIN 12
190
191/* Allow use of RAW sockets */
192/* Allow use of PACKET sockets */
193
194#define CAP_NET_RAW 13
195
196/* Allow locking of shared memory segments */
197/* Allow mlock and mlockall (which doesn't really have anything to do
198 with IPC) */
199
200#define CAP_IPC_LOCK 14
201
202/* Override IPC ownership checks */
203
204#define CAP_IPC_OWNER 15
205
206/* Insert and remove kernel modules - modify kernel without limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#define CAP_SYS_MODULE 16
208
209/* Allow ioperm/iopl access */
210/* Allow sending USB messages to any device via /proc/bus/usb */
211
212#define CAP_SYS_RAWIO 17
213
214/* Allow use of chroot() */
215
216#define CAP_SYS_CHROOT 18
217
218/* Allow ptrace() of any process */
219
220#define CAP_SYS_PTRACE 19
221
222/* Allow configuration of process accounting */
223
224#define CAP_SYS_PACCT 20
225
226/* Allow configuration of the secure attention key */
227/* Allow administration of the random device */
228/* Allow examination and configuration of disk quotas */
229/* Allow configuring the kernel's syslog (printk behaviour) */
230/* Allow setting the domainname */
231/* Allow setting the hostname */
232/* Allow calling bdflush() */
233/* Allow mount() and umount(), setting up new smb connection */
234/* Allow some autofs root ioctls */
235/* Allow nfsservctl */
236/* Allow VM86_REQUEST_IRQ */
237/* Allow to read/write pci config on alpha */
238/* Allow irix_prctl on mips (setstacksize) */
239/* Allow flushing all cache on m68k (sys_cacheflush) */
240/* Allow removing semaphores */
241/* Used instead of CAP_CHOWN to "chown" IPC message queues, semaphores
242 and shared memory */
243/* Allow locking/unlocking of shared memory segment */
244/* Allow turning swap on/off */
245/* Allow forged pids on socket credentials passing */
246/* Allow setting readahead and flushing buffers on block devices */
247/* Allow setting geometry in floppy driver */
248/* Allow turning DMA on/off in xd driver */
249/* Allow administration of md devices (mostly the above, but some
250 extra ioctls) */
251/* Allow tuning the ide driver */
252/* Allow access to the nvram device */
253/* Allow administration of apm_bios, serial and bttv (TV) device */
254/* Allow manufacturer commands in isdn CAPI support driver */
255/* Allow reading non-standardized portions of pci configuration space */
256/* Allow DDI debug ioctl on sbpcd driver */
257/* Allow setting up serial ports */
258/* Allow sending raw qic-117 commands */
259/* Allow enabling/disabling tagged queuing on SCSI controllers and sending
260 arbitrary SCSI commands */
261/* Allow setting encryption key on loopback filesystem */
Martin Hicksbce5f6ba2005-09-03 15:54:50 -0700262/* Allow setting zone reclaim policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264#define CAP_SYS_ADMIN 21
265
266/* Allow use of reboot() */
267
268#define CAP_SYS_BOOT 22
269
270/* Allow raising priority and setting priority on other (different
271 UID) processes */
272/* Allow use of FIFO and round-robin (realtime) scheduling on own
273 processes and setting the scheduling algorithm used by another
274 process. */
275/* Allow setting cpu affinity on other processes */
276
277#define CAP_SYS_NICE 23
278
279/* Override resource limits. Set resource limits. */
280/* Override quota limits. */
281/* Override reserved space on ext2 filesystem */
282/* Modify data journaling mode on ext3 filesystem (uses journaling
283 resources) */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700284/* NOTE: ext2 honors fsuid when checking for resource overrides, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 you can override using fsuid too */
286/* Override size restrictions on IPC message queues */
287/* Allow more than 64hz interrupts from the real-time clock */
288/* Override max number of consoles on console allocation */
289/* Override max number of keymaps */
290
291#define CAP_SYS_RESOURCE 24
292
293/* Allow manipulation of system clock */
294/* Allow irix_stime on mips */
295/* Allow setting the real-time clock */
296
297#define CAP_SYS_TIME 25
298
299/* Allow configuration of tty devices */
300/* Allow vhangup() of tty */
301
302#define CAP_SYS_TTY_CONFIG 26
303
304/* Allow the privileged aspects of mknod() */
305
306#define CAP_MKNOD 27
307
308/* Allow taking of leases on files */
309
310#define CAP_LEASE 28
311
312#define CAP_AUDIT_WRITE 29
313
314#define CAP_AUDIT_CONTROL 30
315
Serge E. Hallynb5376772007-10-16 23:31:36 -0700316#define CAP_SETFCAP 31
317
Casey Schauflere114e472008-02-04 22:29:50 -0800318/* Override MAC access.
319 The base kernel enforces no MAC policy.
320 An LSM may enforce a MAC policy, and if it does and it chooses
321 to implement capability based overrides of that policy, this is
322 the capability it should use to do so. */
323
324#define CAP_MAC_OVERRIDE 32
325
326/* Allow MAC configuration or state changes.
327 The base kernel requires no MAC configuration.
328 An LSM may enforce a MAC policy, and if it does and it chooses
329 to implement capability based checks on modifications to that
330 policy or the data required to maintain it, this is the
331 capability it should use to do so. */
332
333#define CAP_MAC_ADMIN 33
334
335#define CAP_LAST_CAP CAP_MAC_ADMIN
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800336
337#define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP)
338
Andrew Morgane338d262008-02-04 22:29:42 -0800339/*
340 * Bit location of each capability (used by user-space library and kernel)
341 */
342
343#define CAP_TO_INDEX(x) ((x) >> 5) /* 1 << 5 == bits in __u32 */
344#define CAP_TO_MASK(x) (1 << ((x) & 31)) /* mask for indexed __u32 */
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348/*
349 * Internal kernel functions only
350 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700351
Andrew Morgane338d262008-02-04 22:29:42 -0800352#define CAP_FOR_EACH_U32(__capi) \
353 for (__capi = 0; __capi < _LINUX_CAPABILITY_U32S; ++__capi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Andrew Morgane338d262008-02-04 22:29:42 -0800355# define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
356 | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
357 | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
358 | CAP_TO_MASK(CAP_FOWNER) \
359 | CAP_TO_MASK(CAP_FSETID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Casey Schauflere114e472008-02-04 22:29:50 -0800361# define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
362
Andrew Morgane338d262008-02-04 22:29:42 -0800363#if _LINUX_CAPABILITY_U32S != 2
364# error Fix up hand-coded capability macro initializers
365#else /* HAND-CODED capability initializers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Andrew Morgane338d262008-02-04 22:29:42 -0800367# define CAP_EMPTY_SET {{ 0, 0 }}
368# define CAP_FULL_SET {{ ~0, ~0 }}
369# define CAP_INIT_EFF_SET {{ ~CAP_TO_MASK(CAP_SETPCAP), ~0 }}
Casey Schauflere114e472008-02-04 22:29:50 -0800370# define CAP_FS_SET {{ CAP_FS_MASK_B0, CAP_FS_MASK_B1 } }
371# define CAP_NFSD_SET {{ CAP_FS_MASK_B0|CAP_TO_MASK(CAP_SYS_RESOURCE), \
372 CAP_FS_MASK_B1 } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Andrew Morgane338d262008-02-04 22:29:42 -0800374#endif /* _LINUX_CAPABILITY_U32S != 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Andrew Morgane338d262008-02-04 22:29:42 -0800376#define CAP_INIT_INH_SET CAP_EMPTY_SET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Andrew Morgane338d262008-02-04 22:29:42 -0800378# define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
379# define cap_set_full(c) do { (c) = __cap_full_set; } while (0)
380# define cap_set_init_eff(c) do { (c) = __cap_init_eff_set; } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Andrew Morgane338d262008-02-04 22:29:42 -0800382#define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
383#define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
384#define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
385
386#define CAP_BOP_ALL(c, a, b, OP) \
387do { \
388 unsigned __capi; \
389 CAP_FOR_EACH_U32(__capi) { \
390 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
391 } \
392} while (0)
393
394#define CAP_UOP_ALL(c, a, OP) \
395do { \
396 unsigned __capi; \
397 CAP_FOR_EACH_U32(__capi) { \
398 c.cap[__capi] = OP a.cap[__capi]; \
399 } \
400} while (0)
401
402static inline kernel_cap_t cap_combine(const kernel_cap_t a,
403 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Andrew Morgane338d262008-02-04 22:29:42 -0800405 kernel_cap_t dest;
406 CAP_BOP_ALL(dest, a, b, |);
407 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Andrew Morgane338d262008-02-04 22:29:42 -0800410static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
411 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Andrew Morgane338d262008-02-04 22:29:42 -0800413 kernel_cap_t dest;
414 CAP_BOP_ALL(dest, a, b, &);
415 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Andrew Morgane338d262008-02-04 22:29:42 -0800418static inline kernel_cap_t cap_drop(const kernel_cap_t a,
419 const kernel_cap_t drop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Andrew Morgane338d262008-02-04 22:29:42 -0800421 kernel_cap_t dest;
422 CAP_BOP_ALL(dest, a, drop, &~);
423 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Andrew Morgane338d262008-02-04 22:29:42 -0800426static inline kernel_cap_t cap_invert(const kernel_cap_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Andrew Morgane338d262008-02-04 22:29:42 -0800428 kernel_cap_t dest;
429 CAP_UOP_ALL(dest, c, ~);
430 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Andrew Morgane338d262008-02-04 22:29:42 -0800433static inline int cap_isclear(const kernel_cap_t a)
434{
435 unsigned __capi;
436 CAP_FOR_EACH_U32(__capi) {
437 if (a.cap[__capi] != 0)
438 return 0;
439 }
440 return 1;
441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Andrew Morgane338d262008-02-04 22:29:42 -0800443static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
444{
445 kernel_cap_t dest;
446 dest = cap_drop(a, set);
447 return cap_isclear(dest);
448}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Andrew Morgane338d262008-02-04 22:29:42 -0800450/* Used to decide between falling back on the old suser() or fsuser(). */
451
452static inline int cap_is_fs_cap(int cap)
453{
454 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
455 return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
456}
457
458static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
459{
460 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
461 return cap_drop(a, __cap_fs_set);
462}
463
464static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
465 const kernel_cap_t permitted)
466{
467 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
468 return cap_combine(a,
469 cap_intersect(permitted, __cap_fs_set));
470}
471
472static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
473{
474 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
475 return cap_drop(a, __cap_fs_set);
476}
477
478static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
479 const kernel_cap_t permitted)
480{
481 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
482 return cap_combine(a,
483 cap_intersect(permitted, __cap_nfsd_set));
484}
485
486extern const kernel_cap_t __cap_empty_set;
487extern const kernel_cap_t __cap_full_set;
488extern const kernel_cap_t __cap_init_eff_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Chris Wright12b59892006-03-25 03:07:41 -0800490int capable(int cap);
491int __capable(struct task_struct *t, int cap);
Randy.Dunlapc59ede72006-01-11 12:17:46 -0800492
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800493extern long cap_prctl_drop(unsigned long cap);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#endif /* __KERNEL__ */
496
497#endif /* !_LINUX_CAPABILITY_H */