Add 64-bit capability support to the kernel
[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 #include <linux/compiler.h>
18
19 struct task_struct;
20
21 /* User-level do most of the mapping between kernel and user
22    capabilities based on the version tag given by the kernel. The
23    kernel might be somewhat backwards compatible, but don't bet on
24    it. */
25
26 /* Note, cap_t, is defined by POSIX (draft) to be an "opaque" pointer to
27    a set of three capability sets.  The transposition of 3*the
28    following structure to such a composite is better handled in a user
29    library since the draft standard requires the use of malloc/free
30    etc.. */
31
32 #define _LINUX_CAPABILITY_VERSION_1  0x19980330
33 #define _LINUX_CAPABILITY_U32S_1     1
34
35 #define _LINUX_CAPABILITY_VERSION_2  0x20071026
36 #define _LINUX_CAPABILITY_U32S_2     2
37
38 #define _LINUX_CAPABILITY_VERSION    _LINUX_CAPABILITY_VERSION_2
39 #define _LINUX_CAPABILITY_U32S       _LINUX_CAPABILITY_U32S_2
40
41 typedef struct __user_cap_header_struct {
42         __u32 version;
43         int pid;
44 } __user *cap_user_header_t;
45
46 typedef struct __user_cap_data_struct {
47         __u32 effective;
48         __u32 permitted;
49         __u32 inheritable;
50 } __user *cap_user_data_t;
51
52
53 #define XATTR_CAPS_SUFFIX "capability"
54 #define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX
55
56 #define VFS_CAP_REVISION_MASK   0xFF000000
57 #define VFS_CAP_FLAGS_MASK      ~VFS_CAP_REVISION_MASK
58 #define VFS_CAP_FLAGS_EFFECTIVE 0x000001
59
60 #define VFS_CAP_REVISION_1      0x01000000
61 #define VFS_CAP_U32_1           1
62 #define XATTR_CAPS_SZ_1         (sizeof(__le32)*(1 + 2*VFS_CAP_U32_1))
63
64 #define VFS_CAP_REVISION_2      0x02000000
65 #define VFS_CAP_U32_2           2
66 #define XATTR_CAPS_SZ_2         (sizeof(__le32)*(1 + 2*VFS_CAP_U32_2))
67
68 #define XATTR_CAPS_SZ           XATTR_CAPS_SZ_2
69 #define VFS_CAP_U32             VFS_CAP_U32_2
70 #define VFS_CAP_REVISION        VFS_CAP_REVISION_2
71
72
73 struct vfs_cap_data {
74         __le32 magic_etc;            /* Little endian */
75         struct {
76                 __le32 permitted;    /* Little endian */
77                 __le32 inheritable;  /* Little endian */
78         } data[VFS_CAP_U32];
79 };
80
81 #ifdef __KERNEL__
82
83 typedef struct kernel_cap_struct {
84         __u32 cap[_LINUX_CAPABILITY_U32S];
85 } kernel_cap_t;
86
87 #define _USER_CAP_HEADER_SIZE  (sizeof(struct __user_cap_header_struct))
88 #define _KERNEL_CAP_T_SIZE     (sizeof(kernel_cap_t))
89
90 #endif
91
92
93 /**
94  ** POSIX-draft defined capabilities.
95  **/
96
97 /* In a system with the [_POSIX_CHOWN_RESTRICTED] option defined, this
98    overrides the restriction of changing file ownership and group
99    ownership. */
100
101 #define CAP_CHOWN            0
102
103 /* Override all DAC access, including ACL execute access if
104    [_POSIX_ACL] is defined. Excluding DAC access covered by
105    CAP_LINUX_IMMUTABLE. */
106
107 #define CAP_DAC_OVERRIDE     1
108
109 /* Overrides all DAC restrictions regarding read and search on files
110    and directories, including ACL restrictions if [_POSIX_ACL] is
111    defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */
112
113 #define CAP_DAC_READ_SEARCH  2
114
115 /* Overrides all restrictions about allowed operations on files, where
116    file owner ID must be equal to the user ID, except where CAP_FSETID
117    is applicable. It doesn't override MAC and DAC restrictions. */
118
119 #define CAP_FOWNER           3
120
121 /* Overrides the following restrictions that the effective user ID
122    shall match the file owner ID when setting the S_ISUID and S_ISGID
123    bits on that file; that the effective group ID (or one of the
124    supplementary group IDs) shall match the file owner ID when setting
125    the S_ISGID bit on that file; that the S_ISUID and S_ISGID bits are
126    cleared on successful return from chown(2) (not implemented). */
127
128 #define CAP_FSETID           4
129
130 /* Overrides the restriction that the real or effective user ID of a
131    process sending a signal must match the real or effective user ID
132    of the process receiving the signal. */
133
134 #define CAP_KILL             5
135
136 /* Allows setgid(2) manipulation */
137 /* Allows setgroups(2) */
138 /* Allows forged gids on socket credentials passing. */
139
140 #define CAP_SETGID           6
141
142 /* Allows set*uid(2) manipulation (including fsuid). */
143 /* Allows forged pids on socket credentials passing. */
144
145 #define CAP_SETUID           7
146
147
148 /**
149  ** Linux-specific capabilities
150  **/
151
152 /* Without VFS support for capabilities:
153  *   Transfer any capability in your permitted set to any pid,
154  *   remove any capability in your permitted set from any pid
155  * With VFS support for capabilities (neither of above, but)
156  *   Add any capability to the current process' inheritable set
157  */
158
159 #define CAP_SETPCAP          8
160
161 /* Allow modification of S_IMMUTABLE and S_APPEND file attributes */
162
163 #define CAP_LINUX_IMMUTABLE  9
164
165 /* Allows binding to TCP/UDP sockets below 1024 */
166 /* Allows binding to ATM VCIs below 32 */
167
168 #define CAP_NET_BIND_SERVICE 10
169
170 /* Allow broadcasting, listen to multicast */
171
172 #define CAP_NET_BROADCAST    11
173
174 /* Allow interface configuration */
175 /* Allow administration of IP firewall, masquerading and accounting */
176 /* Allow setting debug option on sockets */
177 /* Allow modification of routing tables */
178 /* Allow setting arbitrary process / process group ownership on
179    sockets */
180 /* Allow binding to any address for transparent proxying */
181 /* Allow setting TOS (type of service) */
182 /* Allow setting promiscuous mode */
183 /* Allow clearing driver statistics */
184 /* Allow multicasting */
185 /* Allow read/write of device-specific registers */
186 /* Allow activation of ATM control sockets */
187
188 #define CAP_NET_ADMIN        12
189
190 /* Allow use of RAW sockets */
191 /* Allow use of PACKET sockets */
192
193 #define CAP_NET_RAW          13
194
195 /* Allow locking of shared memory segments */
196 /* Allow mlock and mlockall (which doesn't really have anything to do
197    with IPC) */
198
199 #define CAP_IPC_LOCK         14
200
201 /* Override IPC ownership checks */
202
203 #define CAP_IPC_OWNER        15
204
205 /* Insert and remove kernel modules - modify kernel without limit */
206 /* Modify cap_bset */
207 #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 */
262 /* Allow setting zone reclaim policy */
263
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) */
284 /* NOTE: ext2 honors fsuid when checking for resource overrides, so
285    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
316 #define CAP_SETFCAP          31
317
318 /*
319  * Bit location of each capability (used by user-space library and kernel)
320  */
321
322 #define CAP_TO_INDEX(x)     ((x) >> 5)        /* 1 << 5 == bits in __u32 */
323 #define CAP_TO_MASK(x)      (1 << ((x) & 31)) /* mask for indexed __u32 */
324
325 #ifdef __KERNEL__
326
327 /*
328  * Internal kernel functions only
329  */
330
331 #define CAP_FOR_EACH_U32(__capi)  \
332         for (__capi = 0; __capi < _LINUX_CAPABILITY_U32S; ++__capi)
333
334 # define CAP_FS_MASK_B0     (CAP_TO_MASK(CAP_CHOWN)             \
335                             | CAP_TO_MASK(CAP_DAC_OVERRIDE)     \
336                             | CAP_TO_MASK(CAP_DAC_READ_SEARCH)  \
337                             | CAP_TO_MASK(CAP_FOWNER)           \
338                             | CAP_TO_MASK(CAP_FSETID))
339
340 #if _LINUX_CAPABILITY_U32S != 2
341 # error Fix up hand-coded capability macro initializers
342 #else /* HAND-CODED capability initializers */
343
344 # define CAP_EMPTY_SET    {{ 0, 0 }}
345 # define CAP_FULL_SET     {{ ~0, ~0 }}
346 # define CAP_INIT_EFF_SET {{ ~CAP_TO_MASK(CAP_SETPCAP), ~0 }}
347 # define CAP_FS_SET       {{ CAP_FS_MASK_B0, 0 }}
348 # define CAP_NFSD_SET     {{ CAP_FS_MASK_B0|CAP_TO_MASK(CAP_SYS_RESOURCE), 0 }}
349
350 #endif /* _LINUX_CAPABILITY_U32S != 2 */
351
352 #define CAP_INIT_INH_SET    CAP_EMPTY_SET
353
354 # define cap_clear(c)         do { (c) = __cap_empty_set; } while (0)
355 # define cap_set_full(c)      do { (c) = __cap_full_set; } while (0)
356 # define cap_set_init_eff(c)  do { (c) = __cap_init_eff_set; } while (0)
357
358 #define cap_raise(c, flag)  ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
359 #define cap_lower(c, flag)  ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
360 #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
361
362 #define CAP_BOP_ALL(c, a, b, OP)                                    \
363 do {                                                                \
364         unsigned __capi;                                            \
365         CAP_FOR_EACH_U32(__capi) {                                  \
366                 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi];     \
367         }                                                           \
368 } while (0)
369
370 #define CAP_UOP_ALL(c, a, OP)                                       \
371 do {                                                                \
372         unsigned __capi;                                            \
373         CAP_FOR_EACH_U32(__capi) {                                  \
374                 c.cap[__capi] = OP a.cap[__capi];                   \
375         }                                                           \
376 } while (0)
377
378 static inline kernel_cap_t cap_combine(const kernel_cap_t a,
379                                        const kernel_cap_t b)
380 {
381         kernel_cap_t dest;
382         CAP_BOP_ALL(dest, a, b, |);
383         return dest;
384 }
385
386 static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
387                                          const kernel_cap_t b)
388 {
389         kernel_cap_t dest;
390         CAP_BOP_ALL(dest, a, b, &);
391         return dest;
392 }
393
394 static inline kernel_cap_t cap_drop(const kernel_cap_t a,
395                                     const kernel_cap_t drop)
396 {
397         kernel_cap_t dest;
398         CAP_BOP_ALL(dest, a, drop, &~);
399         return dest;
400 }
401
402 static inline kernel_cap_t cap_invert(const kernel_cap_t c)
403 {
404         kernel_cap_t dest;
405         CAP_UOP_ALL(dest, c, ~);
406         return dest;
407 }
408
409 static inline int cap_isclear(const kernel_cap_t a)
410 {
411         unsigned __capi;
412         CAP_FOR_EACH_U32(__capi) {
413                 if (a.cap[__capi] != 0)
414                         return 0;
415         }
416         return 1;
417 }
418
419 static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
420 {
421         kernel_cap_t dest;
422         dest = cap_drop(a, set);
423         return cap_isclear(dest);
424 }
425
426 /* Used to decide between falling back on the old suser() or fsuser(). */
427
428 static inline int cap_is_fs_cap(int cap)
429 {
430         const kernel_cap_t __cap_fs_set = CAP_FS_SET;
431         return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
432 }
433
434 static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
435 {
436         const kernel_cap_t __cap_fs_set = CAP_FS_SET;
437         return cap_drop(a, __cap_fs_set);
438 }
439
440 static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
441                                             const kernel_cap_t permitted)
442 {
443         const kernel_cap_t __cap_fs_set = CAP_FS_SET;
444         return cap_combine(a,
445                            cap_intersect(permitted, __cap_fs_set));
446 }
447
448 static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
449 {
450         const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
451         return cap_drop(a, __cap_fs_set);
452 }
453
454 static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
455                                               const kernel_cap_t permitted)
456 {
457         const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
458         return cap_combine(a,
459                            cap_intersect(permitted, __cap_nfsd_set));
460 }
461
462 extern const kernel_cap_t __cap_empty_set;
463 extern const kernel_cap_t __cap_full_set;
464 extern const kernel_cap_t __cap_init_eff_set;
465
466 int capable(int cap);
467 int __capable(struct task_struct *t, int cap);
468
469 #endif /* __KERNEL__ */
470
471 #endif /* !_LINUX_CAPABILITY_H */