capability.h 7.5 KB

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