revert "capabilities: clean up file capability reading"
[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 /* XXX - Note, cap_t, is defined by POSIX 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  0x19980330
33
34 typedef struct __user_cap_header_struct {
35         __u32 version;
36         int pid;
37 } __user *cap_user_header_t;
38
39 typedef struct __user_cap_data_struct {
40         __u32 effective;
41         __u32 permitted;
42         __u32 inheritable;
43 } __user *cap_user_data_t;
44
45 #define XATTR_CAPS_SUFFIX "capability"
46 #define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX
47
48 #define XATTR_CAPS_SZ (3*sizeof(__le32))
49 #define VFS_CAP_REVISION_MASK   0xFF000000
50 #define VFS_CAP_REVISION_1      0x01000000
51
52 #define VFS_CAP_REVISION        VFS_CAP_REVISION_1
53
54 #define VFS_CAP_FLAGS_MASK      ~VFS_CAP_REVISION_MASK
55 #define VFS_CAP_FLAGS_EFFECTIVE 0x000001
56
57 struct vfs_cap_data {
58         __u32 magic_etc;  /* Little endian */
59         struct {
60                 __u32 permitted;    /* Little endian */
61                 __u32 inheritable;  /* Little endian */
62         } data[1];
63 };
64
65 #ifdef __KERNEL__
66
67 /* #define STRICT_CAP_T_TYPECHECKS */
68
69 #ifdef STRICT_CAP_T_TYPECHECKS
70
71 typedef struct kernel_cap_struct {
72         __u32 cap;
73 } kernel_cap_t;
74
75 #else
76
77 typedef __u32 kernel_cap_t;
78
79 #endif
80
81 #define _USER_CAP_HEADER_SIZE  (2*sizeof(__u32))
82 #define _KERNEL_CAP_T_SIZE     (sizeof(kernel_cap_t))
83
84 #endif
85
86
87 /**
88  ** POSIX-draft defined capabilities.
89  **/
90
91 /* In a system with the [_POSIX_CHOWN_RESTRICTED] option defined, this
92    overrides the restriction of changing file ownership and group
93    ownership. */
94
95 #define CAP_CHOWN            0
96
97 /* Override all DAC access, including ACL execute access if
98    [_POSIX_ACL] is defined. Excluding DAC access covered by
99    CAP_LINUX_IMMUTABLE. */
100
101 #define CAP_DAC_OVERRIDE     1
102
103 /* Overrides all DAC restrictions regarding read and search on files
104    and directories, including ACL restrictions if [_POSIX_ACL] is
105    defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */
106
107 #define CAP_DAC_READ_SEARCH  2
108
109 /* Overrides all restrictions about allowed operations on files, where
110    file owner ID must be equal to the user ID, except where CAP_FSETID
111    is applicable. It doesn't override MAC and DAC restrictions. */
112
113 #define CAP_FOWNER           3
114
115 /* Overrides the following restrictions that the effective user ID
116    shall match the file owner ID when setting the S_ISUID and S_ISGID
117    bits on that file; that the effective group ID (or one of the
118    supplementary group IDs) shall match the file owner ID when setting
119    the S_ISGID bit on that file; that the S_ISUID and S_ISGID bits are
120    cleared on successful return from chown(2) (not implemented). */
121
122 #define CAP_FSETID           4
123
124 /* Used to decide between falling back on the old suser() or fsuser(). */
125
126 #define CAP_FS_MASK          0x1f
127
128 /* Overrides the restriction that the real or effective user ID of a
129    process sending a signal must match the real or effective user ID
130    of the process receiving the signal. */
131
132 #define CAP_KILL             5
133
134 /* Allows setgid(2) manipulation */
135 /* Allows setgroups(2) */
136 /* Allows forged gids on socket credentials passing. */
137
138 #define CAP_SETGID           6
139
140 /* Allows set*uid(2) manipulation (including fsuid). */
141 /* Allows forged pids on socket credentials passing. */
142
143 #define CAP_SETUID           7
144
145
146 /**
147  ** Linux-specific capabilities
148  **/
149
150 /* Transfer any capability in your permitted set to any pid,
151    remove any capability in your permitted set from any pid */
152
153 #define CAP_SETPCAP          8
154
155 /* Allow modification of S_IMMUTABLE and S_APPEND file attributes */
156
157 #define CAP_LINUX_IMMUTABLE  9
158
159 /* Allows binding to TCP/UDP sockets below 1024 */
160 /* Allows binding to ATM VCIs below 32 */
161
162 #define CAP_NET_BIND_SERVICE 10
163
164 /* Allow broadcasting, listen to multicast */
165
166 #define CAP_NET_BROADCAST    11
167
168 /* Allow interface configuration */
169 /* Allow administration of IP firewall, masquerading and accounting */
170 /* Allow setting debug option on sockets */
171 /* Allow modification of routing tables */
172 /* Allow setting arbitrary process / process group ownership on
173    sockets */
174 /* Allow binding to any address for transparent proxying */
175 /* Allow setting TOS (type of service) */
176 /* Allow setting promiscuous mode */
177 /* Allow clearing driver statistics */
178 /* Allow multicasting */
179 /* Allow read/write of device-specific registers */
180 /* Allow activation of ATM control sockets */
181
182 #define CAP_NET_ADMIN        12
183
184 /* Allow use of RAW sockets */
185 /* Allow use of PACKET sockets */
186
187 #define CAP_NET_RAW          13
188
189 /* Allow locking of shared memory segments */
190 /* Allow mlock and mlockall (which doesn't really have anything to do
191    with IPC) */
192
193 #define CAP_IPC_LOCK         14
194
195 /* Override IPC ownership checks */
196
197 #define CAP_IPC_OWNER        15
198
199 /* Insert and remove kernel modules - modify kernel without limit */
200 /* Modify cap_bset */
201 #define CAP_SYS_MODULE       16
202
203 /* Allow ioperm/iopl access */
204 /* Allow sending USB messages to any device via /proc/bus/usb */
205
206 #define CAP_SYS_RAWIO        17
207
208 /* Allow use of chroot() */
209
210 #define CAP_SYS_CHROOT       18
211
212 /* Allow ptrace() of any process */
213
214 #define CAP_SYS_PTRACE       19
215
216 /* Allow configuration of process accounting */
217
218 #define CAP_SYS_PACCT        20
219
220 /* Allow configuration of the secure attention key */
221 /* Allow administration of the random device */
222 /* Allow examination and configuration of disk quotas */
223 /* Allow configuring the kernel's syslog (printk behaviour) */
224 /* Allow setting the domainname */
225 /* Allow setting the hostname */
226 /* Allow calling bdflush() */
227 /* Allow mount() and umount(), setting up new smb connection */
228 /* Allow some autofs root ioctls */
229 /* Allow nfsservctl */
230 /* Allow VM86_REQUEST_IRQ */
231 /* Allow to read/write pci config on alpha */
232 /* Allow irix_prctl on mips (setstacksize) */
233 /* Allow flushing all cache on m68k (sys_cacheflush) */
234 /* Allow removing semaphores */
235 /* Used instead of CAP_CHOWN to "chown" IPC message queues, semaphores
236    and shared memory */
237 /* Allow locking/unlocking of shared memory segment */
238 /* Allow turning swap on/off */
239 /* Allow forged pids on socket credentials passing */
240 /* Allow setting readahead and flushing buffers on block devices */
241 /* Allow setting geometry in floppy driver */
242 /* Allow turning DMA on/off in xd driver */
243 /* Allow administration of md devices (mostly the above, but some
244    extra ioctls) */
245 /* Allow tuning the ide driver */
246 /* Allow access to the nvram device */
247 /* Allow administration of apm_bios, serial and bttv (TV) device */
248 /* Allow manufacturer commands in isdn CAPI support driver */
249 /* Allow reading non-standardized portions of pci configuration space */
250 /* Allow DDI debug ioctl on sbpcd driver */
251 /* Allow setting up serial ports */
252 /* Allow sending raw qic-117 commands */
253 /* Allow enabling/disabling tagged queuing on SCSI controllers and sending
254    arbitrary SCSI commands */
255 /* Allow setting encryption key on loopback filesystem */
256 /* Allow setting zone reclaim policy */
257
258 #define CAP_SYS_ADMIN        21
259
260 /* Allow use of reboot() */
261
262 #define CAP_SYS_BOOT         22
263
264 /* Allow raising priority and setting priority on other (different
265    UID) processes */
266 /* Allow use of FIFO and round-robin (realtime) scheduling on own
267    processes and setting the scheduling algorithm used by another
268    process. */
269 /* Allow setting cpu affinity on other processes */
270
271 #define CAP_SYS_NICE         23
272
273 /* Override resource limits. Set resource limits. */
274 /* Override quota limits. */
275 /* Override reserved space on ext2 filesystem */
276 /* Modify data journaling mode on ext3 filesystem (uses journaling
277    resources) */
278 /* NOTE: ext2 honors fsuid when checking for resource overrides, so
279    you can override using fsuid too */
280 /* Override size restrictions on IPC message queues */
281 /* Allow more than 64hz interrupts from the real-time clock */
282 /* Override max number of consoles on console allocation */
283 /* Override max number of keymaps */
284
285 #define CAP_SYS_RESOURCE     24
286
287 /* Allow manipulation of system clock */
288 /* Allow irix_stime on mips */
289 /* Allow setting the real-time clock */
290
291 #define CAP_SYS_TIME         25
292
293 /* Allow configuration of tty devices */
294 /* Allow vhangup() of tty */
295
296 #define CAP_SYS_TTY_CONFIG   26
297
298 /* Allow the privileged aspects of mknod() */
299
300 #define CAP_MKNOD            27
301
302 /* Allow taking of leases on files */
303
304 #define CAP_LEASE            28
305
306 #define CAP_AUDIT_WRITE      29
307
308 #define CAP_AUDIT_CONTROL    30
309
310 #define CAP_SETFCAP          31
311
312 #ifdef __KERNEL__
313
314 /*
315  * Internal kernel functions only
316  */
317
318 #ifdef STRICT_CAP_T_TYPECHECKS
319
320 #define to_cap_t(x) { x }
321 #define cap_t(x) (x).cap
322
323 #else
324
325 #define to_cap_t(x) (x)
326 #define cap_t(x) (x)
327
328 #endif
329
330 #define CAP_EMPTY_SET       to_cap_t(0)
331 #define CAP_FULL_SET        to_cap_t(~0)
332 #define CAP_INIT_EFF_SET    to_cap_t(~0 & ~CAP_TO_MASK(CAP_SETPCAP))
333 #define CAP_INIT_INH_SET    to_cap_t(0)
334
335 #define CAP_TO_MASK(x) (1 << (x))
336 #define cap_raise(c, flag)   (cap_t(c) |=  CAP_TO_MASK(flag))
337 #define cap_lower(c, flag)   (cap_t(c) &= ~CAP_TO_MASK(flag))
338 #define cap_raised(c, flag)  (cap_t(c) & CAP_TO_MASK(flag))
339
340 static inline kernel_cap_t cap_combine(kernel_cap_t a, kernel_cap_t b)
341 {
342      kernel_cap_t dest;
343      cap_t(dest) = cap_t(a) | cap_t(b);
344      return dest;
345 }
346
347 static inline kernel_cap_t cap_intersect(kernel_cap_t a, kernel_cap_t b)
348 {
349      kernel_cap_t dest;
350      cap_t(dest) = cap_t(a) & cap_t(b);
351      return dest;
352 }
353
354 static inline kernel_cap_t cap_drop(kernel_cap_t a, kernel_cap_t drop)
355 {
356      kernel_cap_t dest;
357      cap_t(dest) = cap_t(a) & ~cap_t(drop);
358      return dest;
359 }
360
361 static inline kernel_cap_t cap_invert(kernel_cap_t c)
362 {
363      kernel_cap_t dest;
364      cap_t(dest) = ~cap_t(c);
365      return dest;
366 }
367
368 #define cap_isclear(c)       (!cap_t(c))
369 #define cap_issubset(a,set)  (!(cap_t(a) & ~cap_t(set)))
370
371 #define cap_clear(c)         do { cap_t(c) =  0; } while(0)
372 #define cap_set_full(c)      do { cap_t(c) = ~0; } while(0)
373 #define cap_mask(c,mask)     do { cap_t(c) &= cap_t(mask); } while(0)
374
375 #define cap_is_fs_cap(c)     (CAP_TO_MASK(c) & CAP_FS_MASK)
376
377 int capable(int cap);
378 int __capable(struct task_struct *t, int cap);
379
380 #endif /* __KERNEL__ */
381
382 #endif /* !_LINUX_CAPABILITY_H */