capability.h 6.5 KB

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