capability.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. #ifndef _LINUX_CAPABILITY_H
  13. #define _LINUX_CAPABILITY_H
  14. #include <linux/types.h>
  15. struct task_struct;
  16. /* User-level do most of the mapping between kernel and user
  17. capabilities based on the version tag given by the kernel. The
  18. kernel might be somewhat backwards compatible, but don't bet on
  19. it. */
  20. /* Note, cap_t, is defined by POSIX (draft) to be an "opaque" pointer to
  21. a set of three capability sets. The transposition of 3*the
  22. following structure to such a composite is better handled in a user
  23. library since the draft standard requires the use of malloc/free
  24. etc.. */
  25. #define _LINUX_CAPABILITY_VERSION_1 0x19980330
  26. #define _LINUX_CAPABILITY_U32S_1 1
  27. #define _LINUX_CAPABILITY_VERSION_2 0x20071026 /* deprecated - use v3 */
  28. #define _LINUX_CAPABILITY_U32S_2 2
  29. #define _LINUX_CAPABILITY_VERSION_3 0x20080522
  30. #define _LINUX_CAPABILITY_U32S_3 2
  31. typedef struct __user_cap_header_struct {
  32. __u32 version;
  33. int pid;
  34. } __user *cap_user_header_t;
  35. typedef struct __user_cap_data_struct {
  36. __u32 effective;
  37. __u32 permitted;
  38. __u32 inheritable;
  39. } __user *cap_user_data_t;
  40. #define XATTR_CAPS_SUFFIX "capability"
  41. #define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX
  42. #define VFS_CAP_REVISION_MASK 0xFF000000
  43. #define VFS_CAP_REVISION_SHIFT 24
  44. #define VFS_CAP_FLAGS_MASK ~VFS_CAP_REVISION_MASK
  45. #define VFS_CAP_FLAGS_EFFECTIVE 0x000001
  46. #define VFS_CAP_REVISION_1 0x01000000
  47. #define VFS_CAP_U32_1 1
  48. #define XATTR_CAPS_SZ_1 (sizeof(__le32)*(1 + 2*VFS_CAP_U32_1))
  49. #define VFS_CAP_REVISION_2 0x02000000
  50. #define VFS_CAP_U32_2 2
  51. #define XATTR_CAPS_SZ_2 (sizeof(__le32)*(1 + 2*VFS_CAP_U32_2))
  52. #define XATTR_CAPS_SZ XATTR_CAPS_SZ_2
  53. #define VFS_CAP_U32 VFS_CAP_U32_2
  54. #define VFS_CAP_REVISION VFS_CAP_REVISION_2
  55. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  56. extern int file_caps_enabled;
  57. #endif
  58. struct vfs_cap_data {
  59. __le32 magic_etc; /* Little endian */
  60. struct {
  61. __le32 permitted; /* Little endian */
  62. __le32 inheritable; /* Little endian */
  63. } data[VFS_CAP_U32];
  64. };
  65. #ifndef __KERNEL__
  66. /*
  67. * Backwardly compatible definition for source code - trapped in a
  68. * 32-bit world. If you find you need this, please consider using
  69. * libcap to untrap yourself...
  70. */
  71. #define _LINUX_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_1
  72. #define _LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_1
  73. #else
  74. #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
  75. #define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
  76. typedef struct kernel_cap_struct {
  77. __u32 cap[_KERNEL_CAPABILITY_U32S];
  78. } kernel_cap_t;
  79. /* exact same as vfs_cap_data but in cpu endian and always filled completely */
  80. struct cpu_vfs_cap_data {
  81. __u32 magic_etc;
  82. kernel_cap_t permitted;
  83. kernel_cap_t inheritable;
  84. };
  85. #define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
  86. #define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
  87. #endif
  88. /**
  89. ** POSIX-draft defined capabilities.
  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. #define CAP_CHOWN 0
  95. /* Override all DAC access, including ACL execute access if
  96. [_POSIX_ACL] is defined. Excluding DAC access covered by
  97. CAP_LINUX_IMMUTABLE. */
  98. #define CAP_DAC_OVERRIDE 1
  99. /* Overrides all DAC restrictions regarding read and search on files
  100. and directories, including ACL restrictions if [_POSIX_ACL] is
  101. defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */
  102. #define CAP_DAC_READ_SEARCH 2
  103. /* Overrides all restrictions about allowed operations on files, where
  104. file owner ID must be equal to the user ID, except where CAP_FSETID
  105. is applicable. It doesn't override MAC and DAC restrictions. */
  106. #define CAP_FOWNER 3
  107. /* Overrides the following restrictions that the effective user ID
  108. shall match the file owner ID when setting the S_ISUID and S_ISGID
  109. bits on that file; that the effective group ID (or one of the
  110. supplementary group IDs) shall match the file owner ID when setting
  111. the S_ISGID bit on that file; that the S_ISUID and S_ISGID bits are
  112. cleared on successful return from chown(2) (not implemented). */
  113. #define CAP_FSETID 4
  114. /* Overrides the restriction that the real or effective user ID of a
  115. process sending a signal must match the real or effective user ID
  116. of the process receiving the signal. */
  117. #define CAP_KILL 5
  118. /* Allows setgid(2) manipulation */
  119. /* Allows setgroups(2) */
  120. /* Allows forged gids on socket credentials passing. */
  121. #define CAP_SETGID 6
  122. /* Allows set*uid(2) manipulation (including fsuid). */
  123. /* Allows forged pids on socket credentials passing. */
  124. #define CAP_SETUID 7
  125. /**
  126. ** Linux-specific capabilities
  127. **/
  128. /* Without VFS support for capabilities:
  129. * Transfer any capability in your permitted set to any pid,
  130. * remove any capability in your permitted set from any pid
  131. * With VFS support for capabilities (neither of above, but)
  132. * Add any capability from current's capability bounding set
  133. * to the current process' inheritable set
  134. * Allow taking bits out of capability bounding set
  135. * Allow modification of the securebits for a process
  136. */
  137. #define CAP_SETPCAP 8
  138. /* Allow modification of S_IMMUTABLE and S_APPEND file attributes */
  139. #define CAP_LINUX_IMMUTABLE 9
  140. /* Allows binding to TCP/UDP sockets below 1024 */
  141. /* Allows binding to ATM VCIs below 32 */
  142. #define CAP_NET_BIND_SERVICE 10
  143. /* Allow broadcasting, listen to multicast */
  144. #define CAP_NET_BROADCAST 11
  145. /* Allow interface configuration */
  146. /* Allow administration of IP firewall, masquerading and accounting */
  147. /* Allow setting debug option on sockets */
  148. /* Allow modification of routing tables */
  149. /* Allow setting arbitrary process / process group ownership on
  150. sockets */
  151. /* Allow binding to any address for transparent proxying */
  152. /* Allow setting TOS (type of service) */
  153. /* Allow setting promiscuous mode */
  154. /* Allow clearing driver statistics */
  155. /* Allow multicasting */
  156. /* Allow read/write of device-specific registers */
  157. /* Allow activation of ATM control sockets */
  158. #define CAP_NET_ADMIN 12
  159. /* Allow use of RAW sockets */
  160. /* Allow use of PACKET sockets */
  161. #define CAP_NET_RAW 13
  162. /* Allow locking of shared memory segments */
  163. /* Allow mlock and mlockall (which doesn't really have anything to do
  164. with IPC) */
  165. #define CAP_IPC_LOCK 14
  166. /* Override IPC ownership checks */
  167. #define CAP_IPC_OWNER 15
  168. /* Insert and remove kernel modules - modify kernel without limit */
  169. #define CAP_SYS_MODULE 16
  170. /* Allow ioperm/iopl access */
  171. /* Allow sending USB messages to any device via /proc/bus/usb */
  172. #define CAP_SYS_RAWIO 17
  173. /* Allow use of chroot() */
  174. #define CAP_SYS_CHROOT 18
  175. /* Allow ptrace() of any process */
  176. #define CAP_SYS_PTRACE 19
  177. /* Allow configuration of process accounting */
  178. #define CAP_SYS_PACCT 20
  179. /* Allow configuration of the secure attention key */
  180. /* Allow administration of the random device */
  181. /* Allow examination and configuration of disk quotas */
  182. /* Allow configuring the kernel's syslog (printk behaviour) */
  183. /* Allow setting the domainname */
  184. /* Allow setting the hostname */
  185. /* Allow calling bdflush() */
  186. /* Allow mount() and umount(), setting up new smb connection */
  187. /* Allow some autofs root ioctls */
  188. /* Allow nfsservctl */
  189. /* Allow VM86_REQUEST_IRQ */
  190. /* Allow to read/write pci config on alpha */
  191. /* Allow irix_prctl on mips (setstacksize) */
  192. /* Allow flushing all cache on m68k (sys_cacheflush) */
  193. /* Allow removing semaphores */
  194. /* Used instead of CAP_CHOWN to "chown" IPC message queues, semaphores
  195. and shared memory */
  196. /* Allow locking/unlocking of shared memory segment */
  197. /* Allow turning swap on/off */
  198. /* Allow forged pids on socket credentials passing */
  199. /* Allow setting readahead and flushing buffers on block devices */
  200. /* Allow setting geometry in floppy driver */
  201. /* Allow turning DMA on/off in xd driver */
  202. /* Allow administration of md devices (mostly the above, but some
  203. extra ioctls) */
  204. /* Allow tuning the ide driver */
  205. /* Allow access to the nvram device */
  206. /* Allow administration of apm_bios, serial and bttv (TV) device */
  207. /* Allow manufacturer commands in isdn CAPI support driver */
  208. /* Allow reading non-standardized portions of pci configuration space */
  209. /* Allow DDI debug ioctl on sbpcd driver */
  210. /* Allow setting up serial ports */
  211. /* Allow sending raw qic-117 commands */
  212. /* Allow enabling/disabling tagged queuing on SCSI controllers and sending
  213. arbitrary SCSI commands */
  214. /* Allow setting encryption key on loopback filesystem */
  215. /* Allow setting zone reclaim policy */
  216. #define CAP_SYS_ADMIN 21
  217. /* Allow use of reboot() */
  218. #define CAP_SYS_BOOT 22
  219. /* Allow raising priority and setting priority on other (different
  220. UID) processes */
  221. /* Allow use of FIFO and round-robin (realtime) scheduling on own
  222. processes and setting the scheduling algorithm used by another
  223. process. */
  224. /* Allow setting cpu affinity on other processes */
  225. #define CAP_SYS_NICE 23
  226. /* Override resource limits. Set resource limits. */
  227. /* Override quota limits. */
  228. /* Override reserved space on ext2 filesystem */
  229. /* Modify data journaling mode on ext3 filesystem (uses journaling
  230. resources) */
  231. /* NOTE: ext2 honors fsuid when checking for resource overrides, so
  232. you can override using fsuid too */
  233. /* Override size restrictions on IPC message queues */
  234. /* Allow more than 64hz interrupts from the real-time clock */
  235. /* Override max number of consoles on console allocation */
  236. /* Override max number of keymaps */
  237. #define CAP_SYS_RESOURCE 24
  238. /* Allow manipulation of system clock */
  239. /* Allow irix_stime on mips */
  240. /* Allow setting the real-time clock */
  241. #define CAP_SYS_TIME 25
  242. /* Allow configuration of tty devices */
  243. /* Allow vhangup() of tty */
  244. #define CAP_SYS_TTY_CONFIG 26
  245. /* Allow the privileged aspects of mknod() */
  246. #define CAP_MKNOD 27
  247. /* Allow taking of leases on files */
  248. #define CAP_LEASE 28
  249. #define CAP_AUDIT_WRITE 29
  250. #define CAP_AUDIT_CONTROL 30
  251. #define CAP_SETFCAP 31
  252. /* Override MAC access.
  253. The base kernel enforces no MAC policy.
  254. An LSM may enforce a MAC policy, and if it does and it chooses
  255. to implement capability based overrides of that policy, this is
  256. the capability it should use to do so. */
  257. #define CAP_MAC_OVERRIDE 32
  258. /* Allow MAC configuration or state changes.
  259. The base kernel requires no MAC configuration.
  260. An LSM may enforce a MAC policy, and if it does and it chooses
  261. to implement capability based checks on modifications to that
  262. policy or the data required to maintain it, this is the
  263. capability it should use to do so. */
  264. #define CAP_MAC_ADMIN 33
  265. #define CAP_LAST_CAP CAP_MAC_ADMIN
  266. #define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP)
  267. /*
  268. * Bit location of each capability (used by user-space library and kernel)
  269. */
  270. #define CAP_TO_INDEX(x) ((x) >> 5) /* 1 << 5 == bits in __u32 */
  271. #define CAP_TO_MASK(x) (1 << ((x) & 31)) /* mask for indexed __u32 */
  272. #ifdef __KERNEL__
  273. /*
  274. * Internal kernel functions only
  275. */
  276. #define CAP_FOR_EACH_U32(__capi) \
  277. for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
  278. # define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
  279. | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
  280. | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
  281. | CAP_TO_MASK(CAP_FOWNER) \
  282. | CAP_TO_MASK(CAP_FSETID))
  283. # define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
  284. #if _KERNEL_CAPABILITY_U32S != 2
  285. # error Fix up hand-coded capability macro initializers
  286. #else /* HAND-CODED capability initializers */
  287. # define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
  288. # define CAP_FULL_SET ((kernel_cap_t){{ ~0, ~0 }})
  289. # define CAP_INIT_EFF_SET ((kernel_cap_t){{ ~CAP_TO_MASK(CAP_SETPCAP), ~0 }})
  290. # define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0, CAP_FS_MASK_B1 } })
  291. # define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0|CAP_TO_MASK(CAP_SYS_RESOURCE), \
  292. CAP_FS_MASK_B1 } })
  293. #endif /* _KERNEL_CAPABILITY_U32S != 2 */
  294. #define CAP_INIT_INH_SET CAP_EMPTY_SET
  295. # define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
  296. # define cap_set_full(c) do { (c) = __cap_full_set; } while (0)
  297. # define cap_set_init_eff(c) do { (c) = __cap_init_eff_set; } while (0)
  298. #define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
  299. #define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
  300. #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
  301. #define CAP_BOP_ALL(c, a, b, OP) \
  302. do { \
  303. unsigned __capi; \
  304. CAP_FOR_EACH_U32(__capi) { \
  305. c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
  306. } \
  307. } while (0)
  308. #define CAP_UOP_ALL(c, a, OP) \
  309. do { \
  310. unsigned __capi; \
  311. CAP_FOR_EACH_U32(__capi) { \
  312. c.cap[__capi] = OP a.cap[__capi]; \
  313. } \
  314. } while (0)
  315. static inline kernel_cap_t cap_combine(const kernel_cap_t a,
  316. const kernel_cap_t b)
  317. {
  318. kernel_cap_t dest;
  319. CAP_BOP_ALL(dest, a, b, |);
  320. return dest;
  321. }
  322. static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
  323. const kernel_cap_t b)
  324. {
  325. kernel_cap_t dest;
  326. CAP_BOP_ALL(dest, a, b, &);
  327. return dest;
  328. }
  329. static inline kernel_cap_t cap_drop(const kernel_cap_t a,
  330. const kernel_cap_t drop)
  331. {
  332. kernel_cap_t dest;
  333. CAP_BOP_ALL(dest, a, drop, &~);
  334. return dest;
  335. }
  336. static inline kernel_cap_t cap_invert(const kernel_cap_t c)
  337. {
  338. kernel_cap_t dest;
  339. CAP_UOP_ALL(dest, c, ~);
  340. return dest;
  341. }
  342. static inline int cap_isclear(const kernel_cap_t a)
  343. {
  344. unsigned __capi;
  345. CAP_FOR_EACH_U32(__capi) {
  346. if (a.cap[__capi] != 0)
  347. return 0;
  348. }
  349. return 1;
  350. }
  351. /*
  352. * Check if "a" is a subset of "set".
  353. * return 1 if ALL of the capabilities in "a" are also in "set"
  354. * cap_issubset(0101, 1111) will return 1
  355. * return 0 if ANY of the capabilities in "a" are not in "set"
  356. * cap_issubset(1111, 0101) will return 0
  357. */
  358. static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
  359. {
  360. kernel_cap_t dest;
  361. dest = cap_drop(a, set);
  362. return cap_isclear(dest);
  363. }
  364. /* Used to decide between falling back on the old suser() or fsuser(). */
  365. static inline int cap_is_fs_cap(int cap)
  366. {
  367. const kernel_cap_t __cap_fs_set = CAP_FS_SET;
  368. return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
  369. }
  370. static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
  371. {
  372. const kernel_cap_t __cap_fs_set = CAP_FS_SET;
  373. return cap_drop(a, __cap_fs_set);
  374. }
  375. static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
  376. const kernel_cap_t permitted)
  377. {
  378. const kernel_cap_t __cap_fs_set = CAP_FS_SET;
  379. return cap_combine(a,
  380. cap_intersect(permitted, __cap_fs_set));
  381. }
  382. static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
  383. {
  384. const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
  385. return cap_drop(a, __cap_fs_set);
  386. }
  387. static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
  388. const kernel_cap_t permitted)
  389. {
  390. const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
  391. return cap_combine(a,
  392. cap_intersect(permitted, __cap_nfsd_set));
  393. }
  394. extern const kernel_cap_t __cap_empty_set;
  395. extern const kernel_cap_t __cap_full_set;
  396. extern const kernel_cap_t __cap_init_eff_set;
  397. /**
  398. * has_capability - Determine if a task has a superior capability available
  399. * @t: The task in question
  400. * @cap: The capability to be tested for
  401. *
  402. * Return true if the specified task has the given superior capability
  403. * currently in effect, false if not.
  404. *
  405. * Note that this does not set PF_SUPERPRIV on the task.
  406. */
  407. #define has_capability(t, cap) (security_capable((t), (cap)) == 0)
  408. #define has_capability_noaudit(t, cap) (security_capable_noaudit((t), (cap)) == 0)
  409. extern int capable(int cap);
  410. /* audit system wants to get cap info from files as well */
  411. struct dentry;
  412. extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
  413. #endif /* __KERNEL__ */
  414. #endif /* !_LINUX_CAPABILITY_H */