capability.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. struct user_namespace *current_user_ns(void);
  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_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
  64. # define CAP_FULL_SET ((kernel_cap_t){{ ~0, ~0 }})
  65. # define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
  66. | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
  67. CAP_FS_MASK_B1 } })
  68. # define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
  69. | CAP_TO_MASK(CAP_SYS_RESOURCE), \
  70. CAP_FS_MASK_B1 } })
  71. #endif /* _KERNEL_CAPABILITY_U32S != 2 */
  72. # define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
  73. #define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
  74. #define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
  75. #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
  76. #define CAP_BOP_ALL(c, a, b, OP) \
  77. do { \
  78. unsigned __capi; \
  79. CAP_FOR_EACH_U32(__capi) { \
  80. c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
  81. } \
  82. } while (0)
  83. #define CAP_UOP_ALL(c, a, OP) \
  84. do { \
  85. unsigned __capi; \
  86. CAP_FOR_EACH_U32(__capi) { \
  87. c.cap[__capi] = OP a.cap[__capi]; \
  88. } \
  89. } while (0)
  90. static inline kernel_cap_t cap_combine(const kernel_cap_t a,
  91. const kernel_cap_t b)
  92. {
  93. kernel_cap_t dest;
  94. CAP_BOP_ALL(dest, a, b, |);
  95. return dest;
  96. }
  97. static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
  98. const kernel_cap_t b)
  99. {
  100. kernel_cap_t dest;
  101. CAP_BOP_ALL(dest, a, b, &);
  102. return dest;
  103. }
  104. static inline kernel_cap_t cap_drop(const kernel_cap_t a,
  105. const kernel_cap_t drop)
  106. {
  107. kernel_cap_t dest;
  108. CAP_BOP_ALL(dest, a, drop, &~);
  109. return dest;
  110. }
  111. static inline kernel_cap_t cap_invert(const kernel_cap_t c)
  112. {
  113. kernel_cap_t dest;
  114. CAP_UOP_ALL(dest, c, ~);
  115. return dest;
  116. }
  117. static inline int cap_isclear(const kernel_cap_t a)
  118. {
  119. unsigned __capi;
  120. CAP_FOR_EACH_U32(__capi) {
  121. if (a.cap[__capi] != 0)
  122. return 0;
  123. }
  124. return 1;
  125. }
  126. /*
  127. * Check if "a" is a subset of "set".
  128. * return 1 if ALL of the capabilities in "a" are also in "set"
  129. * cap_issubset(0101, 1111) will return 1
  130. * return 0 if ANY of the capabilities in "a" are not in "set"
  131. * cap_issubset(1111, 0101) will return 0
  132. */
  133. static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
  134. {
  135. kernel_cap_t dest;
  136. dest = cap_drop(a, set);
  137. return cap_isclear(dest);
  138. }
  139. /* Used to decide between falling back on the old suser() or fsuser(). */
  140. static inline int cap_is_fs_cap(int cap)
  141. {
  142. const kernel_cap_t __cap_fs_set = CAP_FS_SET;
  143. return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
  144. }
  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. extern bool has_capability(struct task_struct *t, int cap);
  170. extern bool has_ns_capability(struct task_struct *t,
  171. struct user_namespace *ns, int cap);
  172. extern bool has_capability_noaudit(struct task_struct *t, int cap);
  173. extern bool has_ns_capability_noaudit(struct task_struct *t,
  174. struct user_namespace *ns, int cap);
  175. extern bool capable(int cap);
  176. extern bool ns_capable(struct user_namespace *ns, int cap);
  177. extern bool nsown_capable(int cap);
  178. extern bool inode_capable(const struct inode *inode, int cap);
  179. extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
  180. /* audit system wants to get cap info from files as well */
  181. extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
  182. #endif /* !_LINUX_CAPABILITY_H */