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