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