Security: Typecast CAP_*_SET macros
[safe/jmp/linux-2.6] / include / linux / capability.h
1 /*
2  * This is <linux/capability.h>
3  *
4  * Andrew G. Morgan <morgan@kernel.org>
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  *
10  * ftp://linux.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
11  */
12
13 #ifndef _LINUX_CAPABILITY_H
14 #define _LINUX_CAPABILITY_H
15
16 #include <linux/types.h>
17
18 struct task_struct;
19
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
25 /* Note, cap_t, is defined by POSIX (draft) to be an "opaque" pointer to
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.. */
30
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
39
40 typedef struct __user_cap_header_struct {
41         __u32 version;
42         int pid;
43 } __user *cap_user_header_t;
44
45 typedef struct __user_cap_data_struct {
46         __u32 effective;
47         __u32 permitted;
48         __u32 inheritable;
49 } __user *cap_user_data_t;
50
51
52 #define XATTR_CAPS_SUFFIX "capability"
53 #define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX
54
55 #define VFS_CAP_REVISION_MASK   0xFF000000
56 #define VFS_CAP_FLAGS_MASK      ~VFS_CAP_REVISION_MASK
57 #define VFS_CAP_FLAGS_EFFECTIVE 0x000001
58
59 #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
72 struct vfs_cap_data {
73         __le32 magic_etc;            /* Little endian */
74         struct {
75                 __le32 permitted;    /* Little endian */
76                 __le32 inheritable;  /* Little endian */
77         } data[VFS_CAP_U32];
78 };
79
80 #ifdef __KERNEL__
81
82 typedef struct kernel_cap_struct {
83         __u32 cap[_LINUX_CAPABILITY_U32S];
84 } kernel_cap_t;
85
86 #define _USER_CAP_HEADER_SIZE  (sizeof(struct __user_cap_header_struct))
87 #define _KERNEL_CAP_T_SIZE     (sizeof(kernel_cap_t))
88
89 #endif
90
91
92 /**
93  ** POSIX-draft defined capabilities.
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
113
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
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
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 from current's capability bounding set
156  *       to the current process' inheritable set
157  *   Allow taking bits out of capability bounding set
158  *   Allow modification of the securebits for a process
159  */
160
161 #define CAP_SETPCAP          8
162
163 /* Allow modification of S_IMMUTABLE and S_APPEND file attributes */
164
165 #define CAP_LINUX_IMMUTABLE  9
166
167 /* Allows binding to TCP/UDP sockets below 1024 */
168 /* Allows binding to ATM VCIs below 32 */
169
170 #define CAP_NET_BIND_SERVICE 10
171
172 /* Allow broadcasting, listen to multicast */
173
174 #define CAP_NET_BROADCAST    11
175
176 /* Allow interface configuration */
177 /* Allow administration of IP firewall, masquerading and accounting */
178 /* Allow setting debug option on sockets */
179 /* Allow modification of routing tables */
180 /* Allow setting arbitrary process / process group ownership on
181    sockets */
182 /* Allow binding to any address for transparent proxying */
183 /* Allow setting TOS (type of service) */
184 /* Allow setting promiscuous mode */
185 /* Allow clearing driver statistics */
186 /* Allow multicasting */
187 /* Allow read/write of device-specific registers */
188 /* Allow activation of ATM control sockets */
189
190 #define CAP_NET_ADMIN        12
191
192 /* Allow use of RAW sockets */
193 /* Allow use of PACKET sockets */
194
195 #define CAP_NET_RAW          13
196
197 /* Allow locking of shared memory segments */
198 /* Allow mlock and mlockall (which doesn't really have anything to do
199    with IPC) */
200
201 #define CAP_IPC_LOCK         14
202
203 /* Override IPC ownership checks */
204
205 #define CAP_IPC_OWNER        15
206
207 /* Insert and remove kernel modules - modify kernel without limit */
208 #define CAP_SYS_MODULE       16
209
210 /* Allow ioperm/iopl access */
211 /* Allow sending USB messages to any device via /proc/bus/usb */
212
213 #define CAP_SYS_RAWIO        17
214
215 /* Allow use of chroot() */
216
217 #define CAP_SYS_CHROOT       18
218
219 /* Allow ptrace() of any process */
220
221 #define CAP_SYS_PTRACE       19
222
223 /* Allow configuration of process accounting */
224
225 #define CAP_SYS_PACCT        20
226
227 /* Allow configuration of the secure attention key */
228 /* Allow administration of the random device */
229 /* Allow examination and configuration of disk quotas */
230 /* Allow configuring the kernel's syslog (printk behaviour) */
231 /* Allow setting the domainname */
232 /* Allow setting the hostname */
233 /* Allow calling bdflush() */
234 /* Allow mount() and umount(), setting up new smb connection */
235 /* Allow some autofs root ioctls */
236 /* Allow nfsservctl */
237 /* Allow VM86_REQUEST_IRQ */
238 /* Allow to read/write pci config on alpha */
239 /* Allow irix_prctl on mips (setstacksize) */
240 /* Allow flushing all cache on m68k (sys_cacheflush) */
241 /* Allow removing semaphores */
242 /* Used instead of CAP_CHOWN to "chown" IPC message queues, semaphores
243    and shared memory */
244 /* Allow locking/unlocking of shared memory segment */
245 /* Allow turning swap on/off */
246 /* Allow forged pids on socket credentials passing */
247 /* Allow setting readahead and flushing buffers on block devices */
248 /* Allow setting geometry in floppy driver */
249 /* Allow turning DMA on/off in xd driver */
250 /* Allow administration of md devices (mostly the above, but some
251    extra ioctls) */
252 /* Allow tuning the ide driver */
253 /* Allow access to the nvram device */
254 /* Allow administration of apm_bios, serial and bttv (TV) device */
255 /* Allow manufacturer commands in isdn CAPI support driver */
256 /* Allow reading non-standardized portions of pci configuration space */
257 /* Allow DDI debug ioctl on sbpcd driver */
258 /* Allow setting up serial ports */
259 /* Allow sending raw qic-117 commands */
260 /* Allow enabling/disabling tagged queuing on SCSI controllers and sending
261    arbitrary SCSI commands */
262 /* Allow setting encryption key on loopback filesystem */
263 /* Allow setting zone reclaim policy */
264
265 #define CAP_SYS_ADMIN        21
266
267 /* Allow use of reboot() */
268
269 #define CAP_SYS_BOOT         22
270
271 /* Allow raising priority and setting priority on other (different
272    UID) processes */
273 /* Allow use of FIFO and round-robin (realtime) scheduling on own
274    processes and setting the scheduling algorithm used by another
275    process. */
276 /* Allow setting cpu affinity on other processes */
277
278 #define CAP_SYS_NICE         23
279
280 /* Override resource limits. Set resource limits. */
281 /* Override quota limits. */
282 /* Override reserved space on ext2 filesystem */
283 /* Modify data journaling mode on ext3 filesystem (uses journaling
284    resources) */
285 /* NOTE: ext2 honors fsuid when checking for resource overrides, so
286    you can override using fsuid too */
287 /* Override size restrictions on IPC message queues */
288 /* Allow more than 64hz interrupts from the real-time clock */
289 /* Override max number of consoles on console allocation */
290 /* Override max number of keymaps */
291
292 #define CAP_SYS_RESOURCE     24
293
294 /* Allow manipulation of system clock */
295 /* Allow irix_stime on mips */
296 /* Allow setting the real-time clock */
297
298 #define CAP_SYS_TIME         25
299
300 /* Allow configuration of tty devices */
301 /* Allow vhangup() of tty */
302
303 #define CAP_SYS_TTY_CONFIG   26
304
305 /* Allow the privileged aspects of mknod() */
306
307 #define CAP_MKNOD            27
308
309 /* Allow taking of leases on files */
310
311 #define CAP_LEASE            28
312
313 #define CAP_AUDIT_WRITE      29
314
315 #define CAP_AUDIT_CONTROL    30
316
317 #define CAP_SETFCAP          31
318
319 /* Override MAC access.
320    The base kernel enforces no MAC policy.
321    An LSM may enforce a MAC policy, and if it does and it chooses
322    to implement capability based overrides of that policy, this is
323    the capability it should use to do so. */
324
325 #define CAP_MAC_OVERRIDE     32
326
327 /* Allow MAC configuration or state changes.
328    The base kernel requires no MAC configuration.
329    An LSM may enforce a MAC policy, and if it does and it chooses
330    to implement capability based checks on modifications to that
331    policy or the data required to maintain it, this is the
332    capability it should use to do so. */
333
334 #define CAP_MAC_ADMIN        33
335
336 #define CAP_LAST_CAP         CAP_MAC_ADMIN
337
338 #define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP)
339
340 /*
341  * Bit location of each capability (used by user-space library and kernel)
342  */
343
344 #define CAP_TO_INDEX(x)     ((x) >> 5)        /* 1 << 5 == bits in __u32 */
345 #define CAP_TO_MASK(x)      (1 << ((x) & 31)) /* mask for indexed __u32 */
346
347 #ifdef __KERNEL__
348
349 /*
350  * Internal kernel functions only
351  */
352
353 #define CAP_FOR_EACH_U32(__capi)  \
354         for (__capi = 0; __capi < _LINUX_CAPABILITY_U32S; ++__capi)
355
356 # define CAP_FS_MASK_B0     (CAP_TO_MASK(CAP_CHOWN)             \
357                             | CAP_TO_MASK(CAP_DAC_OVERRIDE)     \
358                             | CAP_TO_MASK(CAP_DAC_READ_SEARCH)  \
359                             | CAP_TO_MASK(CAP_FOWNER)           \
360                             | CAP_TO_MASK(CAP_FSETID))
361
362 # define CAP_FS_MASK_B1     (CAP_TO_MASK(CAP_MAC_OVERRIDE))
363
364 #if _LINUX_CAPABILITY_U32S != 2
365 # error Fix up hand-coded capability macro initializers
366 #else /* HAND-CODED capability initializers */
367
368 # define CAP_EMPTY_SET    ((kernel_cap_t){{ 0, 0 }})
369 # define CAP_FULL_SET     ((kernel_cap_t){{ ~0, ~0 }})
370 # define CAP_INIT_EFF_SET ((kernel_cap_t){{ ~CAP_TO_MASK(CAP_SETPCAP), ~0 }})
371 # define CAP_FS_SET       ((kernel_cap_t){{ CAP_FS_MASK_B0, CAP_FS_MASK_B1 } })
372 # define CAP_NFSD_SET     ((kernel_cap_t){{ CAP_FS_MASK_B0|CAP_TO_MASK(CAP_SYS_RESOURCE), \
373                                         CAP_FS_MASK_B1 } })
374
375 #endif /* _LINUX_CAPABILITY_U32S != 2 */
376
377 #define CAP_INIT_INH_SET    CAP_EMPTY_SET
378
379 # define cap_clear(c)         do { (c) = __cap_empty_set; } while (0)
380 # define cap_set_full(c)      do { (c) = __cap_full_set; } while (0)
381 # define cap_set_init_eff(c)  do { (c) = __cap_init_eff_set; } while (0)
382
383 #define cap_raise(c, flag)  ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
384 #define cap_lower(c, flag)  ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
385 #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
386
387 #define CAP_BOP_ALL(c, a, b, OP)                                    \
388 do {                                                                \
389         unsigned __capi;                                            \
390         CAP_FOR_EACH_U32(__capi) {                                  \
391                 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi];     \
392         }                                                           \
393 } while (0)
394
395 #define CAP_UOP_ALL(c, a, OP)                                       \
396 do {                                                                \
397         unsigned __capi;                                            \
398         CAP_FOR_EACH_U32(__capi) {                                  \
399                 c.cap[__capi] = OP a.cap[__capi];                   \
400         }                                                           \
401 } while (0)
402
403 static inline kernel_cap_t cap_combine(const kernel_cap_t a,
404                                        const kernel_cap_t b)
405 {
406         kernel_cap_t dest;
407         CAP_BOP_ALL(dest, a, b, |);
408         return dest;
409 }
410
411 static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
412                                          const kernel_cap_t b)
413 {
414         kernel_cap_t dest;
415         CAP_BOP_ALL(dest, a, b, &);
416         return dest;
417 }
418
419 static inline kernel_cap_t cap_drop(const kernel_cap_t a,
420                                     const kernel_cap_t drop)
421 {
422         kernel_cap_t dest;
423         CAP_BOP_ALL(dest, a, drop, &~);
424         return dest;
425 }
426
427 static inline kernel_cap_t cap_invert(const kernel_cap_t c)
428 {
429         kernel_cap_t dest;
430         CAP_UOP_ALL(dest, c, ~);
431         return dest;
432 }
433
434 static inline int cap_isclear(const kernel_cap_t a)
435 {
436         unsigned __capi;
437         CAP_FOR_EACH_U32(__capi) {
438                 if (a.cap[__capi] != 0)
439                         return 0;
440         }
441         return 1;
442 }
443
444 static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
445 {
446         kernel_cap_t dest;
447         dest = cap_drop(a, set);
448         return cap_isclear(dest);
449 }
450
451 /* Used to decide between falling back on the old suser() or fsuser(). */
452
453 static inline int cap_is_fs_cap(int cap)
454 {
455         const kernel_cap_t __cap_fs_set = CAP_FS_SET;
456         return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
457 }
458
459 static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
460 {
461         const kernel_cap_t __cap_fs_set = CAP_FS_SET;
462         return cap_drop(a, __cap_fs_set);
463 }
464
465 static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
466                                             const kernel_cap_t permitted)
467 {
468         const kernel_cap_t __cap_fs_set = CAP_FS_SET;
469         return cap_combine(a,
470                            cap_intersect(permitted, __cap_fs_set));
471 }
472
473 static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
474 {
475         const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
476         return cap_drop(a, __cap_fs_set);
477 }
478
479 static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
480                                               const kernel_cap_t permitted)
481 {
482         const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
483         return cap_combine(a,
484                            cap_intersect(permitted, __cap_nfsd_set));
485 }
486
487 extern const kernel_cap_t __cap_empty_set;
488 extern const kernel_cap_t __cap_full_set;
489 extern const kernel_cap_t __cap_init_eff_set;
490
491 int capable(int cap);
492 int __capable(struct task_struct *t, int cap);
493
494 #endif /* __KERNEL__ */
495
496 #endif /* !_LINUX_CAPABILITY_H */