blob: 90012b9ddbf3e5f999983bf7d2f6e388a99d25a1 [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 *
GeunSik Limbcf56442009-06-16 10:26:25 +020010 * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
Serge E. Hallynb5376772007-10-16 23:31:36 -070011 */
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
Andrew G. Morganca05a992008-05-27 22:05:17 -070034#define _LINUX_CAPABILITY_VERSION_2 0x20071026 /* deprecated - use v3 */
Andrew Morgane338d262008-02-04 22:29:42 -080035#define _LINUX_CAPABILITY_U32S_2 2
36
Andrew G. Morganca05a992008-05-27 22:05:17 -070037#define _LINUX_CAPABILITY_VERSION_3 0x20080522
38#define _LINUX_CAPABILITY_U32S_3 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 VFS_CAP_REVISION_MASK 0xFF000000
Eric Paris851f7ff2008-11-11 21:48:14 +110053#define VFS_CAP_REVISION_SHIFT 24
Serge E. Hallynb5376772007-10-16 23:31:36 -070054#define VFS_CAP_FLAGS_MASK ~VFS_CAP_REVISION_MASK
55#define VFS_CAP_FLAGS_EFFECTIVE 0x000001
56
Andrew Morgane338d262008-02-04 22:29:42 -080057#define VFS_CAP_REVISION_1 0x01000000
58#define VFS_CAP_U32_1 1
59#define XATTR_CAPS_SZ_1 (sizeof(__le32)*(1 + 2*VFS_CAP_U32_1))
60
61#define VFS_CAP_REVISION_2 0x02000000
62#define VFS_CAP_U32_2 2
63#define XATTR_CAPS_SZ_2 (sizeof(__le32)*(1 + 2*VFS_CAP_U32_2))
64
65#define XATTR_CAPS_SZ XATTR_CAPS_SZ_2
66#define VFS_CAP_U32 VFS_CAP_U32_2
67#define VFS_CAP_REVISION VFS_CAP_REVISION_2
68
Serge E. Hallynb5376772007-10-16 23:31:36 -070069struct vfs_cap_data {
Andrew Morgane338d262008-02-04 22:29:42 -080070 __le32 magic_etc; /* Little endian */
Andrew Morton8f6936f2008-02-04 22:29:41 -080071 struct {
Andrew Morgane338d262008-02-04 22:29:42 -080072 __le32 permitted; /* Little endian */
73 __le32 inheritable; /* Little endian */
74 } data[VFS_CAP_U32];
Serge E. Hallynb5376772007-10-16 23:31:36 -070075};
76
Andrew G. Morganca05a992008-05-27 22:05:17 -070077#ifndef __KERNEL__
78
79/*
80 * Backwardly compatible definition for source code - trapped in a
81 * 32-bit world. If you find you need this, please consider using
82 * libcap to untrap yourself...
83 */
84#define _LINUX_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_1
85#define _LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_1
86
87#else
88
89#define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
90#define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jaswinder Singh Rajput9fa91d92009-01-30 20:39:30 +053092extern int file_caps_enabled;
Jaswinder Singh Rajput9fa91d92009-01-30 20:39:30 +053093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094typedef struct kernel_cap_struct {
Andrew G. Morganca05a992008-05-27 22:05:17 -070095 __u32 cap[_KERNEL_CAPABILITY_U32S];
Linus Torvalds1da177e2005-04-16 15:20:36 -070096} kernel_cap_t;
97
Eric Parisc0b00442008-11-11 21:48:10 +110098/* exact same as vfs_cap_data but in cpu endian and always filled completely */
99struct cpu_vfs_cap_data {
100 __u32 magic_etc;
101 kernel_cap_t permitted;
102 kernel_cap_t inheritable;
103};
104
Andrew Morgane338d262008-02-04 22:29:42 -0800105#define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
107
108#endif
109
110
111/**
Serge E. Hallynb5376772007-10-16 23:31:36 -0700112 ** POSIX-draft defined capabilities.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 **/
114
115/* In a system with the [_POSIX_CHOWN_RESTRICTED] option defined, this
116 overrides the restriction of changing file ownership and group
117 ownership. */
118
119#define CAP_CHOWN 0
120
121/* Override all DAC access, including ACL execute access if
122 [_POSIX_ACL] is defined. Excluding DAC access covered by
123 CAP_LINUX_IMMUTABLE. */
124
125#define CAP_DAC_OVERRIDE 1
126
127/* Overrides all DAC restrictions regarding read and search on files
128 and directories, including ACL restrictions if [_POSIX_ACL] is
129 defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */
130
131#define CAP_DAC_READ_SEARCH 2
Serge E. Hallynb5376772007-10-16 23:31:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* Overrides all restrictions about allowed operations on files, where
134 file owner ID must be equal to the user ID, except where CAP_FSETID
135 is applicable. It doesn't override MAC and DAC restrictions. */
136
137#define CAP_FOWNER 3
138
139/* Overrides the following restrictions that the effective user ID
140 shall match the file owner ID when setting the S_ISUID and S_ISGID
141 bits on that file; that the effective group ID (or one of the
142 supplementary group IDs) shall match the file owner ID when setting
143 the S_ISGID bit on that file; that the S_ISUID and S_ISGID bits are
144 cleared on successful return from chown(2) (not implemented). */
145
146#define CAP_FSETID 4
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* Overrides the restriction that the real or effective user ID of a
149 process sending a signal must match the real or effective user ID
150 of the process receiving the signal. */
151
152#define CAP_KILL 5
153
154/* Allows setgid(2) manipulation */
155/* Allows setgroups(2) */
156/* Allows forged gids on socket credentials passing. */
157
158#define CAP_SETGID 6
159
160/* Allows set*uid(2) manipulation (including fsuid). */
161/* Allows forged pids on socket credentials passing. */
162
163#define CAP_SETUID 7
164
165
166/**
167 ** Linux-specific capabilities
168 **/
169
Andrew Morgane338d262008-02-04 22:29:42 -0800170/* Without VFS support for capabilities:
171 * Transfer any capability in your permitted set to any pid,
172 * remove any capability in your permitted set from any pid
173 * With VFS support for capabilities (neither of above, but)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800174 * Add any capability from current's capability bounding set
175 * to the current process' inheritable set
176 * Allow taking bits out of capability bounding set
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700177 * Allow modification of the securebits for a process
Andrew Morgane338d262008-02-04 22:29:42 -0800178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180#define CAP_SETPCAP 8
181
182/* Allow modification of S_IMMUTABLE and S_APPEND file attributes */
183
184#define CAP_LINUX_IMMUTABLE 9
185
186/* Allows binding to TCP/UDP sockets below 1024 */
187/* Allows binding to ATM VCIs below 32 */
188
189#define CAP_NET_BIND_SERVICE 10
190
191/* Allow broadcasting, listen to multicast */
192
193#define CAP_NET_BROADCAST 11
194
195/* Allow interface configuration */
196/* Allow administration of IP firewall, masquerading and accounting */
197/* Allow setting debug option on sockets */
198/* Allow modification of routing tables */
199/* Allow setting arbitrary process / process group ownership on
200 sockets */
201/* Allow binding to any address for transparent proxying */
202/* Allow setting TOS (type of service) */
203/* Allow setting promiscuous mode */
204/* Allow clearing driver statistics */
205/* Allow multicasting */
206/* Allow read/write of device-specific registers */
207/* Allow activation of ATM control sockets */
208
209#define CAP_NET_ADMIN 12
210
211/* Allow use of RAW sockets */
212/* Allow use of PACKET sockets */
213
214#define CAP_NET_RAW 13
215
216/* Allow locking of shared memory segments */
217/* Allow mlock and mlockall (which doesn't really have anything to do
218 with IPC) */
219
220#define CAP_IPC_LOCK 14
221
222/* Override IPC ownership checks */
223
224#define CAP_IPC_OWNER 15
225
226/* Insert and remove kernel modules - modify kernel without limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#define CAP_SYS_MODULE 16
228
229/* Allow ioperm/iopl access */
230/* Allow sending USB messages to any device via /proc/bus/usb */
231
232#define CAP_SYS_RAWIO 17
233
234/* Allow use of chroot() */
235
236#define CAP_SYS_CHROOT 18
237
238/* Allow ptrace() of any process */
239
240#define CAP_SYS_PTRACE 19
241
242/* Allow configuration of process accounting */
243
244#define CAP_SYS_PACCT 20
245
246/* Allow configuration of the secure attention key */
247/* Allow administration of the random device */
248/* Allow examination and configuration of disk quotas */
249/* Allow configuring the kernel's syslog (printk behaviour) */
250/* Allow setting the domainname */
251/* Allow setting the hostname */
252/* Allow calling bdflush() */
253/* Allow mount() and umount(), setting up new smb connection */
254/* Allow some autofs root ioctls */
255/* Allow nfsservctl */
256/* Allow VM86_REQUEST_IRQ */
257/* Allow to read/write pci config on alpha */
258/* Allow irix_prctl on mips (setstacksize) */
259/* Allow flushing all cache on m68k (sys_cacheflush) */
260/* Allow removing semaphores */
261/* Used instead of CAP_CHOWN to "chown" IPC message queues, semaphores
262 and shared memory */
263/* Allow locking/unlocking of shared memory segment */
264/* Allow turning swap on/off */
265/* Allow forged pids on socket credentials passing */
266/* Allow setting readahead and flushing buffers on block devices */
267/* Allow setting geometry in floppy driver */
268/* Allow turning DMA on/off in xd driver */
269/* Allow administration of md devices (mostly the above, but some
270 extra ioctls) */
271/* Allow tuning the ide driver */
272/* Allow access to the nvram device */
273/* Allow administration of apm_bios, serial and bttv (TV) device */
274/* Allow manufacturer commands in isdn CAPI support driver */
275/* Allow reading non-standardized portions of pci configuration space */
276/* Allow DDI debug ioctl on sbpcd driver */
277/* Allow setting up serial ports */
278/* Allow sending raw qic-117 commands */
279/* Allow enabling/disabling tagged queuing on SCSI controllers and sending
280 arbitrary SCSI commands */
281/* Allow setting encryption key on loopback filesystem */
Martin Hicksbce5f6ba2005-09-03 15:54:50 -0700282/* Allow setting zone reclaim policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284#define CAP_SYS_ADMIN 21
285
286/* Allow use of reboot() */
287
288#define CAP_SYS_BOOT 22
289
290/* Allow raising priority and setting priority on other (different
291 UID) processes */
292/* Allow use of FIFO and round-robin (realtime) scheduling on own
293 processes and setting the scheduling algorithm used by another
294 process. */
295/* Allow setting cpu affinity on other processes */
296
297#define CAP_SYS_NICE 23
298
299/* Override resource limits. Set resource limits. */
300/* Override quota limits. */
301/* Override reserved space on ext2 filesystem */
302/* Modify data journaling mode on ext3 filesystem (uses journaling
303 resources) */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700304/* NOTE: ext2 honors fsuid when checking for resource overrides, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 you can override using fsuid too */
306/* Override size restrictions on IPC message queues */
307/* Allow more than 64hz interrupts from the real-time clock */
308/* Override max number of consoles on console allocation */
309/* Override max number of keymaps */
310
311#define CAP_SYS_RESOURCE 24
312
313/* Allow manipulation of system clock */
314/* Allow irix_stime on mips */
315/* Allow setting the real-time clock */
316
317#define CAP_SYS_TIME 25
318
319/* Allow configuration of tty devices */
320/* Allow vhangup() of tty */
321
322#define CAP_SYS_TTY_CONFIG 26
323
324/* Allow the privileged aspects of mknod() */
325
326#define CAP_MKNOD 27
327
328/* Allow taking of leases on files */
329
330#define CAP_LEASE 28
331
332#define CAP_AUDIT_WRITE 29
333
334#define CAP_AUDIT_CONTROL 30
335
Serge E. Hallynb5376772007-10-16 23:31:36 -0700336#define CAP_SETFCAP 31
337
Casey Schauflere114e472008-02-04 22:29:50 -0800338/* Override MAC access.
339 The base kernel enforces no MAC policy.
340 An LSM may enforce a MAC policy, and if it does and it chooses
341 to implement capability based overrides of that policy, this is
342 the capability it should use to do so. */
343
344#define CAP_MAC_OVERRIDE 32
345
346/* Allow MAC configuration or state changes.
347 The base kernel requires no MAC configuration.
348 An LSM may enforce a MAC policy, and if it does and it chooses
349 to implement capability based checks on modifications to that
350 policy or the data required to maintain it, this is the
351 capability it should use to do so. */
352
353#define CAP_MAC_ADMIN 33
354
355#define CAP_LAST_CAP CAP_MAC_ADMIN
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800356
357#define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP)
358
Andrew Morgane338d262008-02-04 22:29:42 -0800359/*
360 * Bit location of each capability (used by user-space library and kernel)
361 */
362
363#define CAP_TO_INDEX(x) ((x) >> 5) /* 1 << 5 == bits in __u32 */
364#define CAP_TO_MASK(x) (1 << ((x) & 31)) /* mask for indexed __u32 */
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368/*
369 * Internal kernel functions only
370 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700371
Andrew Morgane338d262008-02-04 22:29:42 -0800372#define CAP_FOR_EACH_U32(__capi) \
Andrew G. Morganca05a992008-05-27 22:05:17 -0700373 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -0500375/*
376 * CAP_FS_MASK and CAP_NFSD_MASKS:
377 *
378 * The fs mask is all the privileges that fsuid==0 historically meant.
379 * At one time in the past, that included CAP_MKNOD and CAP_LINUX_IMMUTABLE.
380 *
381 * It has never meant setting security.* and trusted.* xattrs.
382 *
383 * We could also define fsmask as follows:
384 * 1. CAP_FS_MASK is the privilege to bypass all fs-related DAC permissions
385 * 2. The security.* and trusted.* xattrs are fs-related MAC permissions
386 */
387
Andrew Morgane338d262008-02-04 22:29:42 -0800388# define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -0500389 | CAP_TO_MASK(CAP_MKNOD) \
Andrew Morgane338d262008-02-04 22:29:42 -0800390 | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
391 | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
392 | CAP_TO_MASK(CAP_FOWNER) \
393 | CAP_TO_MASK(CAP_FSETID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Casey Schauflere114e472008-02-04 22:29:50 -0800395# define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
396
Andrew G. Morganca05a992008-05-27 22:05:17 -0700397#if _KERNEL_CAPABILITY_U32S != 2
Andrew Morgane338d262008-02-04 22:29:42 -0800398# error Fix up hand-coded capability macro initializers
399#else /* HAND-CODED capability initializers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Howells25f2ea92008-04-29 20:54:28 +0100401# define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
402# define CAP_FULL_SET ((kernel_cap_t){{ ~0, ~0 }})
403# define CAP_INIT_EFF_SET ((kernel_cap_t){{ ~CAP_TO_MASK(CAP_SETPCAP), ~0 }})
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -0500404# define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
405 | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
406 CAP_FS_MASK_B1 } })
J. Bruce Fields76a67ec2009-03-16 18:34:20 -0400407# define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -0500408 | CAP_TO_MASK(CAP_SYS_RESOURCE), \
409 CAP_FS_MASK_B1 } })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Andrew G. Morganca05a992008-05-27 22:05:17 -0700411#endif /* _KERNEL_CAPABILITY_U32S != 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Andrew Morgane338d262008-02-04 22:29:42 -0800413#define CAP_INIT_INH_SET CAP_EMPTY_SET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Andrew Morgane338d262008-02-04 22:29:42 -0800415# define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
416# define cap_set_full(c) do { (c) = __cap_full_set; } while (0)
417# define cap_set_init_eff(c) do { (c) = __cap_init_eff_set; } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Andrew Morgane338d262008-02-04 22:29:42 -0800419#define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
420#define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
421#define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
422
423#define CAP_BOP_ALL(c, a, b, OP) \
424do { \
425 unsigned __capi; \
426 CAP_FOR_EACH_U32(__capi) { \
427 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
428 } \
429} while (0)
430
431#define CAP_UOP_ALL(c, a, OP) \
432do { \
433 unsigned __capi; \
434 CAP_FOR_EACH_U32(__capi) { \
435 c.cap[__capi] = OP a.cap[__capi]; \
436 } \
437} while (0)
438
439static inline kernel_cap_t cap_combine(const kernel_cap_t a,
440 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Andrew Morgane338d262008-02-04 22:29:42 -0800442 kernel_cap_t dest;
443 CAP_BOP_ALL(dest, a, b, |);
444 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Andrew Morgane338d262008-02-04 22:29:42 -0800447static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
448 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Andrew Morgane338d262008-02-04 22:29:42 -0800450 kernel_cap_t dest;
451 CAP_BOP_ALL(dest, a, b, &);
452 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Andrew Morgane338d262008-02-04 22:29:42 -0800455static inline kernel_cap_t cap_drop(const kernel_cap_t a,
456 const kernel_cap_t drop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Andrew Morgane338d262008-02-04 22:29:42 -0800458 kernel_cap_t dest;
459 CAP_BOP_ALL(dest, a, drop, &~);
460 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Andrew Morgane338d262008-02-04 22:29:42 -0800463static inline kernel_cap_t cap_invert(const kernel_cap_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Andrew Morgane338d262008-02-04 22:29:42 -0800465 kernel_cap_t dest;
466 CAP_UOP_ALL(dest, c, ~);
467 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Andrew Morgane338d262008-02-04 22:29:42 -0800470static inline int cap_isclear(const kernel_cap_t a)
471{
472 unsigned __capi;
473 CAP_FOR_EACH_U32(__capi) {
474 if (a.cap[__capi] != 0)
475 return 0;
476 }
477 return 1;
478}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Eric Paris9d36be72008-11-11 21:48:07 +1100480/*
481 * Check if "a" is a subset of "set".
482 * return 1 if ALL of the capabilities in "a" are also in "set"
483 * cap_issubset(0101, 1111) will return 1
484 * return 0 if ANY of the capabilities in "a" are not in "set"
485 * cap_issubset(1111, 0101) will return 0
486 */
Andrew Morgane338d262008-02-04 22:29:42 -0800487static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
488{
489 kernel_cap_t dest;
490 dest = cap_drop(a, set);
491 return cap_isclear(dest);
492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Andrew Morgane338d262008-02-04 22:29:42 -0800494/* Used to decide between falling back on the old suser() or fsuser(). */
495
496static inline int cap_is_fs_cap(int cap)
497{
498 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
499 return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
500}
501
502static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
503{
504 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
505 return cap_drop(a, __cap_fs_set);
506}
507
508static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
509 const kernel_cap_t permitted)
510{
511 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
512 return cap_combine(a,
513 cap_intersect(permitted, __cap_fs_set));
514}
515
516static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
517{
518 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
519 return cap_drop(a, __cap_fs_set);
520}
521
522static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
523 const kernel_cap_t permitted)
524{
525 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
526 return cap_combine(a,
527 cap_intersect(permitted, __cap_nfsd_set));
528}
529
530extern const kernel_cap_t __cap_empty_set;
531extern const kernel_cap_t __cap_full_set;
532extern const kernel_cap_t __cap_init_eff_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
David Howells5cd9c582008-08-14 11:37:28 +0100534/**
535 * has_capability - Determine if a task has a superior capability available
536 * @t: The task in question
537 * @cap: The capability to be tested for
538 *
539 * Return true if the specified task has the given superior capability
540 * currently in effect, false if not.
541 *
542 * Note that this does not set PF_SUPERPRIV on the task.
543 */
David Howells3699c532009-01-06 22:27:01 +0000544#define has_capability(t, cap) (security_real_capable((t), (cap)) == 0)
545
546/**
547 * has_capability_noaudit - Determine if a task has a superior capability available (unaudited)
548 * @t: The task in question
549 * @cap: The capability to be tested for
550 *
551 * Return true if the specified task has the given superior capability
552 * currently in effect, false if not, but don't write an audit message for the
553 * check.
554 *
555 * Note that this does not set PF_SUPERPRIV on the task.
556 */
557#define has_capability_noaudit(t, cap) \
558 (security_real_capable_noaudit((t), (cap)) == 0)
David Howells5cd9c582008-08-14 11:37:28 +0100559
560extern int capable(int cap);
Randy.Dunlapc59ede72006-01-11 12:17:46 -0800561
Eric Paris851f7ff2008-11-11 21:48:14 +1100562/* audit system wants to get cap info from files as well */
563struct dentry;
564extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#endif /* __KERNEL__ */
567
568#endif /* !_LINUX_CAPABILITY_H */