]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/capability.h
SELinux: Use unknown perm handling to handle unknown netlink msg types
[mirror_ubuntu-bionic-kernel.git] / include / linux / capability.h
CommitLineData
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
18struct 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
ca05a99a 34#define _LINUX_CAPABILITY_VERSION_2 0x20071026 /* deprecated - use v3 */
e338d263
AM
35#define _LINUX_CAPABILITY_U32S_2 2
36
ca05a99a
AM
37#define _LINUX_CAPABILITY_VERSION_3 0x20080522
38#define _LINUX_CAPABILITY_U32S_3 2
1da177e4
LT
39
40typedef struct __user_cap_header_struct {
41 __u32 version;
42 int pid;
43} __user *cap_user_header_t;
b5376771 44
1da177e4
LT
45typedef 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
1f29fae2
SH
71#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
72extern int file_caps_enabled;
73#endif
b5376771
SH
74
75struct vfs_cap_data {
e338d263 76 __le32 magic_etc; /* Little endian */
8f6936f4 77 struct {
e338d263
AM
78 __le32 permitted; /* Little endian */
79 __le32 inheritable; /* Little endian */
80 } data[VFS_CAP_U32];
b5376771
SH
81};
82
ca05a99a
AM
83#ifndef __KERNEL__
84
85/*
86 * Backwardly compatible definition for source code - trapped in a
87 * 32-bit world. If you find you need this, please consider using
88 * libcap to untrap yourself...
89 */
90#define _LINUX_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_1
91#define _LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_1
92
93#else
94
95#define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
96#define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
1da177e4 97
1da177e4 98typedef struct kernel_cap_struct {
ca05a99a 99 __u32 cap[_KERNEL_CAPABILITY_U32S];
1da177e4
LT
100} kernel_cap_t;
101
e338d263 102#define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
1da177e4
LT
103#define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
104
105#endif
106
107
108/**
b5376771 109 ** POSIX-draft defined capabilities.
1da177e4
LT
110 **/
111
112/* In a system with the [_POSIX_CHOWN_RESTRICTED] option defined, this
113 overrides the restriction of changing file ownership and group
114 ownership. */
115
116#define CAP_CHOWN 0
117
118/* Override all DAC access, including ACL execute access if
119 [_POSIX_ACL] is defined. Excluding DAC access covered by
120 CAP_LINUX_IMMUTABLE. */
121
122#define CAP_DAC_OVERRIDE 1
123
124/* Overrides all DAC restrictions regarding read and search on files
125 and directories, including ACL restrictions if [_POSIX_ACL] is
126 defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */
127
128#define CAP_DAC_READ_SEARCH 2
b5376771 129
1da177e4
LT
130/* Overrides all restrictions about allowed operations on files, where
131 file owner ID must be equal to the user ID, except where CAP_FSETID
132 is applicable. It doesn't override MAC and DAC restrictions. */
133
134#define CAP_FOWNER 3
135
136/* Overrides the following restrictions that the effective user ID
137 shall match the file owner ID when setting the S_ISUID and S_ISGID
138 bits on that file; that the effective group ID (or one of the
139 supplementary group IDs) shall match the file owner ID when setting
140 the S_ISGID bit on that file; that the S_ISUID and S_ISGID bits are
141 cleared on successful return from chown(2) (not implemented). */
142
143#define CAP_FSETID 4
144
1da177e4
LT
145/* Overrides the restriction that the real or effective user ID of a
146 process sending a signal must match the real or effective user ID
147 of the process receiving the signal. */
148
149#define CAP_KILL 5
150
151/* Allows setgid(2) manipulation */
152/* Allows setgroups(2) */
153/* Allows forged gids on socket credentials passing. */
154
155#define CAP_SETGID 6
156
157/* Allows set*uid(2) manipulation (including fsuid). */
158/* Allows forged pids on socket credentials passing. */
159
160#define CAP_SETUID 7
161
162
163/**
164 ** Linux-specific capabilities
165 **/
166
e338d263
AM
167/* Without VFS support for capabilities:
168 * Transfer any capability in your permitted set to any pid,
169 * remove any capability in your permitted set from any pid
170 * With VFS support for capabilities (neither of above, but)
3b7391de
SH
171 * Add any capability from current's capability bounding set
172 * to the current process' inheritable set
173 * Allow taking bits out of capability bounding set
3898b1b4 174 * Allow modification of the securebits for a process
e338d263 175 */
1da177e4
LT
176
177#define CAP_SETPCAP 8
178
179/* Allow modification of S_IMMUTABLE and S_APPEND file attributes */
180
181#define CAP_LINUX_IMMUTABLE 9
182
183/* Allows binding to TCP/UDP sockets below 1024 */
184/* Allows binding to ATM VCIs below 32 */
185
186#define CAP_NET_BIND_SERVICE 10
187
188/* Allow broadcasting, listen to multicast */
189
190#define CAP_NET_BROADCAST 11
191
192/* Allow interface configuration */
193/* Allow administration of IP firewall, masquerading and accounting */
194/* Allow setting debug option on sockets */
195/* Allow modification of routing tables */
196/* Allow setting arbitrary process / process group ownership on
197 sockets */
198/* Allow binding to any address for transparent proxying */
199/* Allow setting TOS (type of service) */
200/* Allow setting promiscuous mode */
201/* Allow clearing driver statistics */
202/* Allow multicasting */
203/* Allow read/write of device-specific registers */
204/* Allow activation of ATM control sockets */
205
206#define CAP_NET_ADMIN 12
207
208/* Allow use of RAW sockets */
209/* Allow use of PACKET sockets */
210
211#define CAP_NET_RAW 13
212
213/* Allow locking of shared memory segments */
214/* Allow mlock and mlockall (which doesn't really have anything to do
215 with IPC) */
216
217#define CAP_IPC_LOCK 14
218
219/* Override IPC ownership checks */
220
221#define CAP_IPC_OWNER 15
222
223/* Insert and remove kernel modules - modify kernel without limit */
1da177e4
LT
224#define CAP_SYS_MODULE 16
225
226/* Allow ioperm/iopl access */
227/* Allow sending USB messages to any device via /proc/bus/usb */
228
229#define CAP_SYS_RAWIO 17
230
231/* Allow use of chroot() */
232
233#define CAP_SYS_CHROOT 18
234
235/* Allow ptrace() of any process */
236
237#define CAP_SYS_PTRACE 19
238
239/* Allow configuration of process accounting */
240
241#define CAP_SYS_PACCT 20
242
243/* Allow configuration of the secure attention key */
244/* Allow administration of the random device */
245/* Allow examination and configuration of disk quotas */
246/* Allow configuring the kernel's syslog (printk behaviour) */
247/* Allow setting the domainname */
248/* Allow setting the hostname */
249/* Allow calling bdflush() */
250/* Allow mount() and umount(), setting up new smb connection */
251/* Allow some autofs root ioctls */
252/* Allow nfsservctl */
253/* Allow VM86_REQUEST_IRQ */
254/* Allow to read/write pci config on alpha */
255/* Allow irix_prctl on mips (setstacksize) */
256/* Allow flushing all cache on m68k (sys_cacheflush) */
257/* Allow removing semaphores */
258/* Used instead of CAP_CHOWN to "chown" IPC message queues, semaphores
259 and shared memory */
260/* Allow locking/unlocking of shared memory segment */
261/* Allow turning swap on/off */
262/* Allow forged pids on socket credentials passing */
263/* Allow setting readahead and flushing buffers on block devices */
264/* Allow setting geometry in floppy driver */
265/* Allow turning DMA on/off in xd driver */
266/* Allow administration of md devices (mostly the above, but some
267 extra ioctls) */
268/* Allow tuning the ide driver */
269/* Allow access to the nvram device */
270/* Allow administration of apm_bios, serial and bttv (TV) device */
271/* Allow manufacturer commands in isdn CAPI support driver */
272/* Allow reading non-standardized portions of pci configuration space */
273/* Allow DDI debug ioctl on sbpcd driver */
274/* Allow setting up serial ports */
275/* Allow sending raw qic-117 commands */
276/* Allow enabling/disabling tagged queuing on SCSI controllers and sending
277 arbitrary SCSI commands */
278/* Allow setting encryption key on loopback filesystem */
bce5f6ba 279/* Allow setting zone reclaim policy */
1da177e4
LT
280
281#define CAP_SYS_ADMIN 21
282
283/* Allow use of reboot() */
284
285#define CAP_SYS_BOOT 22
286
287/* Allow raising priority and setting priority on other (different
288 UID) processes */
289/* Allow use of FIFO and round-robin (realtime) scheduling on own
290 processes and setting the scheduling algorithm used by another
291 process. */
292/* Allow setting cpu affinity on other processes */
293
294#define CAP_SYS_NICE 23
295
296/* Override resource limits. Set resource limits. */
297/* Override quota limits. */
298/* Override reserved space on ext2 filesystem */
299/* Modify data journaling mode on ext3 filesystem (uses journaling
300 resources) */
b5376771 301/* NOTE: ext2 honors fsuid when checking for resource overrides, so
1da177e4
LT
302 you can override using fsuid too */
303/* Override size restrictions on IPC message queues */
304/* Allow more than 64hz interrupts from the real-time clock */
305/* Override max number of consoles on console allocation */
306/* Override max number of keymaps */
307
308#define CAP_SYS_RESOURCE 24
309
310/* Allow manipulation of system clock */
311/* Allow irix_stime on mips */
312/* Allow setting the real-time clock */
313
314#define CAP_SYS_TIME 25
315
316/* Allow configuration of tty devices */
317/* Allow vhangup() of tty */
318
319#define CAP_SYS_TTY_CONFIG 26
320
321/* Allow the privileged aspects of mknod() */
322
323#define CAP_MKNOD 27
324
325/* Allow taking of leases on files */
326
327#define CAP_LEASE 28
328
329#define CAP_AUDIT_WRITE 29
330
331#define CAP_AUDIT_CONTROL 30
332
b5376771
SH
333#define CAP_SETFCAP 31
334
e114e473
CS
335/* Override MAC access.
336 The base kernel enforces no MAC policy.
337 An LSM may enforce a MAC policy, and if it does and it chooses
338 to implement capability based overrides of that policy, this is
339 the capability it should use to do so. */
340
341#define CAP_MAC_OVERRIDE 32
342
343/* Allow MAC configuration or state changes.
344 The base kernel requires no MAC configuration.
345 An LSM may enforce a MAC policy, and if it does and it chooses
346 to implement capability based checks on modifications to that
347 policy or the data required to maintain it, this is the
348 capability it should use to do so. */
349
350#define CAP_MAC_ADMIN 33
351
352#define CAP_LAST_CAP CAP_MAC_ADMIN
3b7391de
SH
353
354#define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP)
355
e338d263
AM
356/*
357 * Bit location of each capability (used by user-space library and kernel)
358 */
359
360#define CAP_TO_INDEX(x) ((x) >> 5) /* 1 << 5 == bits in __u32 */
361#define CAP_TO_MASK(x) (1 << ((x) & 31)) /* mask for indexed __u32 */
362
1da177e4 363#ifdef __KERNEL__
1da177e4
LT
364
365/*
366 * Internal kernel functions only
367 */
b5376771 368
e338d263 369#define CAP_FOR_EACH_U32(__capi) \
ca05a99a 370 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
e338d263
AM
371
372# define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
373 | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
374 | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
375 | CAP_TO_MASK(CAP_FOWNER) \
376 | CAP_TO_MASK(CAP_FSETID))
377
e114e473
CS
378# define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
379
ca05a99a 380#if _KERNEL_CAPABILITY_U32S != 2
e338d263
AM
381# error Fix up hand-coded capability macro initializers
382#else /* HAND-CODED capability initializers */
383
25f2ea9f
DH
384# define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
385# define CAP_FULL_SET ((kernel_cap_t){{ ~0, ~0 }})
386# define CAP_INIT_EFF_SET ((kernel_cap_t){{ ~CAP_TO_MASK(CAP_SETPCAP), ~0 }})
387# define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0, CAP_FS_MASK_B1 } })
388# define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0|CAP_TO_MASK(CAP_SYS_RESOURCE), \
389 CAP_FS_MASK_B1 } })
e338d263 390
ca05a99a 391#endif /* _KERNEL_CAPABILITY_U32S != 2 */
e338d263
AM
392
393#define CAP_INIT_INH_SET CAP_EMPTY_SET
394
395# define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
396# define cap_set_full(c) do { (c) = __cap_full_set; } while (0)
397# define cap_set_init_eff(c) do { (c) = __cap_init_eff_set; } while (0)
398
399#define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
400#define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
401#define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
402
403#define CAP_BOP_ALL(c, a, b, OP) \
404do { \
405 unsigned __capi; \
406 CAP_FOR_EACH_U32(__capi) { \
407 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
408 } \
409} while (0)
410
411#define CAP_UOP_ALL(c, a, OP) \
412do { \
413 unsigned __capi; \
414 CAP_FOR_EACH_U32(__capi) { \
415 c.cap[__capi] = OP a.cap[__capi]; \
416 } \
417} while (0)
418
419static inline kernel_cap_t cap_combine(const kernel_cap_t a,
420 const kernel_cap_t b)
421{
422 kernel_cap_t dest;
423 CAP_BOP_ALL(dest, a, b, |);
424 return dest;
425}
1da177e4 426
e338d263
AM
427static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
428 const kernel_cap_t b)
429{
430 kernel_cap_t dest;
431 CAP_BOP_ALL(dest, a, b, &);
432 return dest;
433}
1da177e4 434
e338d263
AM
435static inline kernel_cap_t cap_drop(const kernel_cap_t a,
436 const kernel_cap_t drop)
437{
438 kernel_cap_t dest;
439 CAP_BOP_ALL(dest, a, drop, &~);
440 return dest;
441}
1da177e4 442
e338d263
AM
443static inline kernel_cap_t cap_invert(const kernel_cap_t c)
444{
445 kernel_cap_t dest;
446 CAP_UOP_ALL(dest, c, ~);
447 return dest;
448}
1da177e4 449
e338d263
AM
450static inline int cap_isclear(const kernel_cap_t a)
451{
452 unsigned __capi;
453 CAP_FOR_EACH_U32(__capi) {
454 if (a.cap[__capi] != 0)
455 return 0;
456 }
457 return 1;
458}
1da177e4 459
e338d263
AM
460static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
461{
462 kernel_cap_t dest;
463 dest = cap_drop(a, set);
464 return cap_isclear(dest);
465}
1da177e4 466
e338d263 467/* Used to decide between falling back on the old suser() or fsuser(). */
1da177e4 468
e338d263 469static inline int cap_is_fs_cap(int cap)
1da177e4 470{
e338d263
AM
471 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
472 return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
1da177e4
LT
473}
474
e338d263 475static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
1da177e4 476{
e338d263
AM
477 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
478 return cap_drop(a, __cap_fs_set);
1da177e4
LT
479}
480
e338d263
AM
481static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
482 const kernel_cap_t permitted)
1da177e4 483{
e338d263
AM
484 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
485 return cap_combine(a,
486 cap_intersect(permitted, __cap_fs_set));
1da177e4
LT
487}
488
e338d263 489static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
1da177e4 490{
e338d263
AM
491 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
492 return cap_drop(a, __cap_fs_set);
1da177e4
LT
493}
494
e338d263
AM
495static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
496 const kernel_cap_t permitted)
497{
498 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
499 return cap_combine(a,
500 cap_intersect(permitted, __cap_nfsd_set));
501}
1da177e4 502
e338d263
AM
503extern const kernel_cap_t __cap_empty_set;
504extern const kernel_cap_t __cap_full_set;
505extern const kernel_cap_t __cap_init_eff_set;
1da177e4 506
086f7316
AM
507kernel_cap_t cap_set_effective(const kernel_cap_t pE_new);
508
5cd9c58f
DH
509/**
510 * has_capability - Determine if a task has a superior capability available
511 * @t: The task in question
512 * @cap: The capability to be tested for
513 *
514 * Return true if the specified task has the given superior capability
515 * currently in effect, false if not.
516 *
517 * Note that this does not set PF_SUPERPRIV on the task.
518 */
519#define has_capability(t, cap) (security_capable((t), (cap)) == 0)
520
521extern int capable(int cap);
c59ede7b 522
1da177e4
LT
523#endif /* __KERNEL__ */
524
525#endif /* !_LINUX_CAPABILITY_H */