capability.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This is <linux/capability.h>
  4. *
  5. * Andrew G. Morgan <[email protected]>
  6. * Alexander Kjeldaas <[email protected]>
  7. * with help from Aleph1, Roland Buresund and Andrew Main.
  8. *
  9. * See here for the libcap library ("POSIX draft" compliance):
  10. *
  11. * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
  12. */
  13. #ifndef _LINUX_CAPABILITY_H
  14. #define _LINUX_CAPABILITY_H
  15. #include <uapi/linux/capability.h>
  16. #include <linux/uidgid.h>
  17. #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
  18. #define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
  19. extern int file_caps_enabled;
  20. typedef struct kernel_cap_struct {
  21. __u32 cap[_KERNEL_CAPABILITY_U32S];
  22. } kernel_cap_t;
  23. /* same as vfs_ns_cap_data but in cpu endian and always filled completely */
  24. struct cpu_vfs_cap_data {
  25. __u32 magic_etc;
  26. kernel_cap_t permitted;
  27. kernel_cap_t inheritable;
  28. kuid_t rootid;
  29. };
  30. #define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
  31. #define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
  32. struct file;
  33. struct inode;
  34. struct dentry;
  35. struct task_struct;
  36. struct user_namespace;
  37. extern const kernel_cap_t __cap_empty_set;
  38. extern const kernel_cap_t __cap_init_eff_set;
  39. /*
  40. * Internal kernel functions only
  41. */
  42. #define CAP_FOR_EACH_U32(__capi) \
  43. for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
  44. /*
  45. * CAP_FS_MASK and CAP_NFSD_MASKS:
  46. *
  47. * The fs mask is all the privileges that fsuid==0 historically meant.
  48. * At one time in the past, that included CAP_MKNOD and CAP_LINUX_IMMUTABLE.
  49. *
  50. * It has never meant setting security.* and trusted.* xattrs.
  51. *
  52. * We could also define fsmask as follows:
  53. * 1. CAP_FS_MASK is the privilege to bypass all fs-related DAC permissions
  54. * 2. The security.* and trusted.* xattrs are fs-related MAC permissions
  55. */
  56. # define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
  57. | CAP_TO_MASK(CAP_MKNOD) \
  58. | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
  59. | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
  60. | CAP_TO_MASK(CAP_FOWNER) \
  61. | CAP_TO_MASK(CAP_FSETID))
  62. # define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
  63. #if _KERNEL_CAPABILITY_U32S != 2
  64. # error Fix up hand-coded capability macro initializers
  65. #else /* HAND-CODED capability initializers */
  66. #define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1)
  67. #define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
  68. # define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
  69. # define CAP_FULL_SET ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
  70. # define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
  71. | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
  72. CAP_FS_MASK_B1 } })
  73. # define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
  74. | CAP_TO_MASK(CAP_SYS_RESOURCE), \
  75. CAP_FS_MASK_B1 } })
  76. #endif /* _KERNEL_CAPABILITY_U32S != 2 */
  77. # define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
  78. #define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
  79. #define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
  80. #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
  81. #define CAP_BOP_ALL(c, a, b, OP) \
  82. do { \
  83. unsigned __capi; \
  84. CAP_FOR_EACH_U32(__capi) { \
  85. c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
  86. } \
  87. } while (0)
  88. #define CAP_UOP_ALL(c, a, OP) \
  89. do { \
  90. unsigned __capi; \
  91. CAP_FOR_EACH_U32(__capi) { \
  92. c.cap[__capi] = OP a.cap[__capi]; \
  93. } \
  94. } while (0)
  95. static inline kernel_cap_t cap_combine(const kernel_cap_t a,
  96. const kernel_cap_t b)
  97. {
  98. kernel_cap_t dest;
  99. CAP_BOP_ALL(dest, a, b, |);
  100. return dest;
  101. }
  102. static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
  103. const kernel_cap_t b)
  104. {
  105. kernel_cap_t dest;
  106. CAP_BOP_ALL(dest, a, b, &);
  107. return dest;
  108. }
  109. static inline kernel_cap_t cap_drop(const kernel_cap_t a,
  110. const kernel_cap_t drop)
  111. {
  112. kernel_cap_t dest;
  113. CAP_BOP_ALL(dest, a, drop, &~);
  114. return dest;
  115. }
  116. static inline kernel_cap_t cap_invert(const kernel_cap_t c)
  117. {
  118. kernel_cap_t dest;
  119. CAP_UOP_ALL(dest, c, ~);
  120. return dest;
  121. }
  122. static inline bool cap_isclear(const kernel_cap_t a)
  123. {
  124. unsigned __capi;
  125. CAP_FOR_EACH_U32(__capi) {
  126. if (a.cap[__capi] != 0)
  127. return false;
  128. }
  129. return true;
  130. }
  131. /*
  132. * Check if "a" is a subset of "set".
  133. * return true if ALL of the capabilities in "a" are also in "set"
  134. * cap_issubset(0101, 1111) will return true
  135. * return false if ANY of the capabilities in "a" are not in "set"
  136. * cap_issubset(1111, 0101) will return false
  137. */
  138. static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
  139. {
  140. kernel_cap_t dest;
  141. dest = cap_drop(a, set);
  142. return cap_isclear(dest);
  143. }
  144. /* Used to decide between falling back on the old suser() or fsuser(). */
  145. static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
  146. {
  147. const kernel_cap_t __cap_fs_set = CAP_FS_SET;
  148. return cap_drop(a, __cap_fs_set);
  149. }
  150. static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
  151. const kernel_cap_t permitted)
  152. {
  153. const kernel_cap_t __cap_fs_set = CAP_FS_SET;
  154. return cap_combine(a,
  155. cap_intersect(permitted, __cap_fs_set));
  156. }
  157. static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
  158. {
  159. const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
  160. return cap_drop(a, __cap_fs_set);
  161. }
  162. static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
  163. const kernel_cap_t permitted)
  164. {
  165. const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
  166. return cap_combine(a,
  167. cap_intersect(permitted, __cap_nfsd_set));
  168. }
  169. #ifdef CONFIG_MULTIUSER
  170. extern bool has_capability(struct task_struct *t, int cap);
  171. extern bool has_ns_capability(struct task_struct *t,
  172. struct user_namespace *ns, int cap);
  173. extern bool has_capability_noaudit(struct task_struct *t, int cap);
  174. extern bool has_ns_capability_noaudit(struct task_struct *t,
  175. struct user_namespace *ns, int cap);
  176. extern bool capable(int cap);
  177. extern bool ns_capable(struct user_namespace *ns, int cap);
  178. extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
  179. extern bool ns_capable_setid(struct user_namespace *ns, int cap);
  180. #else
  181. static inline bool has_capability(struct task_struct *t, int cap)
  182. {
  183. return true;
  184. }
  185. static inline bool has_ns_capability(struct task_struct *t,
  186. struct user_namespace *ns, int cap)
  187. {
  188. return true;
  189. }
  190. static inline bool has_capability_noaudit(struct task_struct *t, int cap)
  191. {
  192. return true;
  193. }
  194. static inline bool has_ns_capability_noaudit(struct task_struct *t,
  195. struct user_namespace *ns, int cap)
  196. {
  197. return true;
  198. }
  199. static inline bool capable(int cap)
  200. {
  201. return true;
  202. }
  203. static inline bool ns_capable(struct user_namespace *ns, int cap)
  204. {
  205. return true;
  206. }
  207. static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
  208. {
  209. return true;
  210. }
  211. static inline bool ns_capable_setid(struct user_namespace *ns, int cap)
  212. {
  213. return true;
  214. }
  215. #endif /* CONFIG_MULTIUSER */
  216. bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
  217. struct user_namespace *mnt_userns,
  218. const struct inode *inode);
  219. bool capable_wrt_inode_uidgid(struct user_namespace *mnt_userns,
  220. const struct inode *inode, int cap);
  221. extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
  222. extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
  223. static inline bool perfmon_capable(void)
  224. {
  225. return capable(CAP_PERFMON) || capable(CAP_SYS_ADMIN);
  226. }
  227. static inline bool bpf_capable(void)
  228. {
  229. return capable(CAP_BPF) || capable(CAP_SYS_ADMIN);
  230. }
  231. static inline bool checkpoint_restore_ns_capable(struct user_namespace *ns)
  232. {
  233. return ns_capable(ns, CAP_CHECKPOINT_RESTORE) ||
  234. ns_capable(ns, CAP_SYS_ADMIN);
  235. }
  236. /* audit system wants to get cap info from files as well */
  237. int get_vfs_caps_from_disk(struct user_namespace *mnt_userns,
  238. const struct dentry *dentry,
  239. struct cpu_vfs_cap_data *cpu_caps);
  240. int cap_convert_nscap(struct user_namespace *mnt_userns, struct dentry *dentry,
  241. const void **ivalue, size_t size);
  242. #endif /* !_LINUX_CAPABILITY_H */