capability.h 7.3 KB

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