capability.h 16 KB

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