Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This is <linux/capability.h> |
| 3 | * |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 4 | * Andrew G. Morgan <morgan@kernel.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
GeunSik Lim | bcf5644 | 2009-06-16 10:26:25 +0200 | [diff] [blame] | 10 | * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/ |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 11 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #ifndef _LINUX_CAPABILITY_H |
| 13 | #define _LINUX_CAPABILITY_H |
| 14 | |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 15 | #include <uapi/linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Andrew G. Morgan | ca05a99 | 2008-05-27 22:05:17 -0700 | [diff] [blame] | 17 | |
| 18 | #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3 |
| 19 | #define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Jaswinder Singh Rajput | 9fa91d9 | 2009-01-30 20:39:30 +0530 | [diff] [blame] | 21 | extern int file_caps_enabled; |
Jaswinder Singh Rajput | 9fa91d9 | 2009-01-30 20:39:30 +0530 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | typedef struct kernel_cap_struct { |
Andrew G. Morgan | ca05a99 | 2008-05-27 22:05:17 -0700 | [diff] [blame] | 24 | __u32 cap[_KERNEL_CAPABILITY_U32S]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | } kernel_cap_t; |
| 26 | |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 27 | /* exact same as vfs_cap_data but in cpu endian and always filled completely */ |
| 28 | struct cpu_vfs_cap_data { |
| 29 | __u32 magic_etc; |
| 30 | kernel_cap_t permitted; |
| 31 | kernel_cap_t inheritable; |
| 32 | }; |
| 33 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 34 | #define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t)) |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 935d8aa | 2013-04-14 10:06:31 -0700 | [diff] [blame] | 38 | struct file; |
Eric W. Biederman | 1a48e2a | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 39 | struct inode; |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 40 | struct dentry; |
| 41 | struct user_namespace; |
| 42 | |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 43 | struct user_namespace *current_user_ns(void); |
| 44 | |
| 45 | extern const kernel_cap_t __cap_empty_set; |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 46 | extern const kernel_cap_t __cap_init_eff_set; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* |
| 49 | * Internal kernel functions only |
| 50 | */ |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 51 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 52 | #define CAP_FOR_EACH_U32(__capi) \ |
Andrew G. Morgan | ca05a99 | 2008-05-27 22:05:17 -0700 | [diff] [blame] | 53 | for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Serge E. Hallyn | 0ad30b8 | 2009-04-13 09:56:14 -0500 | [diff] [blame] | 55 | /* |
| 56 | * CAP_FS_MASK and CAP_NFSD_MASKS: |
| 57 | * |
| 58 | * The fs mask is all the privileges that fsuid==0 historically meant. |
| 59 | * At one time in the past, that included CAP_MKNOD and CAP_LINUX_IMMUTABLE. |
| 60 | * |
| 61 | * It has never meant setting security.* and trusted.* xattrs. |
| 62 | * |
| 63 | * We could also define fsmask as follows: |
| 64 | * 1. CAP_FS_MASK is the privilege to bypass all fs-related DAC permissions |
| 65 | * 2. The security.* and trusted.* xattrs are fs-related MAC permissions |
| 66 | */ |
| 67 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 68 | # define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \ |
Serge E. Hallyn | 0ad30b8 | 2009-04-13 09:56:14 -0500 | [diff] [blame] | 69 | | CAP_TO_MASK(CAP_MKNOD) \ |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 70 | | CAP_TO_MASK(CAP_DAC_OVERRIDE) \ |
| 71 | | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \ |
| 72 | | CAP_TO_MASK(CAP_FOWNER) \ |
| 73 | | CAP_TO_MASK(CAP_FSETID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 75 | # define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE)) |
| 76 | |
Andrew G. Morgan | ca05a99 | 2008-05-27 22:05:17 -0700 | [diff] [blame] | 77 | #if _KERNEL_CAPABILITY_U32S != 2 |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 78 | # error Fix up hand-coded capability macro initializers |
| 79 | #else /* HAND-CODED capability initializers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Eric Paris | 7d8b6c6 | 2014-07-23 15:36:26 -0400 | [diff] [blame] | 81 | #define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1) |
| 82 | #define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1) |
| 83 | |
David Howells | 25f2ea9 | 2008-04-29 20:54:28 +0100 | [diff] [blame] | 84 | # define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }}) |
Eric Paris | 7d8b6c6 | 2014-07-23 15:36:26 -0400 | [diff] [blame] | 85 | # define CAP_FULL_SET ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }}) |
Serge E. Hallyn | 0ad30b8 | 2009-04-13 09:56:14 -0500 | [diff] [blame] | 86 | # define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \ |
| 87 | | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \ |
| 88 | CAP_FS_MASK_B1 } }) |
J. Bruce Fields | 76a67ec | 2009-03-16 18:34:20 -0400 | [diff] [blame] | 89 | # define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \ |
Serge E. Hallyn | 0ad30b8 | 2009-04-13 09:56:14 -0500 | [diff] [blame] | 90 | | CAP_TO_MASK(CAP_SYS_RESOURCE), \ |
| 91 | CAP_FS_MASK_B1 } }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Andrew G. Morgan | ca05a99 | 2008-05-27 22:05:17 -0700 | [diff] [blame] | 93 | #endif /* _KERNEL_CAPABILITY_U32S != 2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 95 | # define cap_clear(c) do { (c) = __cap_empty_set; } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 97 | #define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag)) |
| 98 | #define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag)) |
| 99 | #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag)) |
| 100 | |
| 101 | #define CAP_BOP_ALL(c, a, b, OP) \ |
| 102 | do { \ |
| 103 | unsigned __capi; \ |
| 104 | CAP_FOR_EACH_U32(__capi) { \ |
| 105 | c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \ |
| 106 | } \ |
| 107 | } while (0) |
| 108 | |
| 109 | #define CAP_UOP_ALL(c, a, OP) \ |
| 110 | do { \ |
| 111 | unsigned __capi; \ |
| 112 | CAP_FOR_EACH_U32(__capi) { \ |
| 113 | c.cap[__capi] = OP a.cap[__capi]; \ |
| 114 | } \ |
| 115 | } while (0) |
| 116 | |
| 117 | static inline kernel_cap_t cap_combine(const kernel_cap_t a, |
| 118 | const kernel_cap_t b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 120 | kernel_cap_t dest; |
| 121 | CAP_BOP_ALL(dest, a, b, |); |
| 122 | return dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 125 | static inline kernel_cap_t cap_intersect(const kernel_cap_t a, |
| 126 | const kernel_cap_t b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 128 | kernel_cap_t dest; |
| 129 | CAP_BOP_ALL(dest, a, b, &); |
| 130 | return dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 133 | static inline kernel_cap_t cap_drop(const kernel_cap_t a, |
| 134 | const kernel_cap_t drop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 136 | kernel_cap_t dest; |
| 137 | CAP_BOP_ALL(dest, a, drop, &~); |
| 138 | return dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 141 | static inline kernel_cap_t cap_invert(const kernel_cap_t c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 143 | kernel_cap_t dest; |
| 144 | CAP_UOP_ALL(dest, c, ~); |
| 145 | return dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Yaowei Bai | e42852bf | 2015-11-17 15:25:24 +0800 | [diff] [blame] | 148 | static inline bool cap_isclear(const kernel_cap_t a) |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 149 | { |
| 150 | unsigned __capi; |
| 151 | CAP_FOR_EACH_U32(__capi) { |
| 152 | if (a.cap[__capi] != 0) |
Yaowei Bai | e42852bf | 2015-11-17 15:25:24 +0800 | [diff] [blame] | 153 | return false; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 154 | } |
Yaowei Bai | e42852bf | 2015-11-17 15:25:24 +0800 | [diff] [blame] | 155 | return true; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Eric Paris | 9d36be7 | 2008-11-11 21:48:07 +1100 | [diff] [blame] | 158 | /* |
| 159 | * Check if "a" is a subset of "set". |
Yaowei Bai | e42852bf | 2015-11-17 15:25:24 +0800 | [diff] [blame] | 160 | * return true if ALL of the capabilities in "a" are also in "set" |
| 161 | * cap_issubset(0101, 1111) will return true |
| 162 | * return false if ANY of the capabilities in "a" are not in "set" |
| 163 | * cap_issubset(1111, 0101) will return false |
Eric Paris | 9d36be7 | 2008-11-11 21:48:07 +1100 | [diff] [blame] | 164 | */ |
Yaowei Bai | e42852bf | 2015-11-17 15:25:24 +0800 | [diff] [blame] | 165 | static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set) |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 166 | { |
| 167 | kernel_cap_t dest; |
| 168 | dest = cap_drop(a, set); |
| 169 | return cap_isclear(dest); |
| 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 172 | /* Used to decide between falling back on the old suser() or fsuser(). */ |
| 173 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 174 | static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a) |
| 175 | { |
| 176 | const kernel_cap_t __cap_fs_set = CAP_FS_SET; |
| 177 | return cap_drop(a, __cap_fs_set); |
| 178 | } |
| 179 | |
| 180 | static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a, |
| 181 | const kernel_cap_t permitted) |
| 182 | { |
| 183 | const kernel_cap_t __cap_fs_set = CAP_FS_SET; |
| 184 | return cap_combine(a, |
| 185 | cap_intersect(permitted, __cap_fs_set)); |
| 186 | } |
| 187 | |
| 188 | static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a) |
| 189 | { |
| 190 | const kernel_cap_t __cap_fs_set = CAP_NFSD_SET; |
| 191 | return cap_drop(a, __cap_fs_set); |
| 192 | } |
| 193 | |
| 194 | static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a, |
| 195 | const kernel_cap_t permitted) |
| 196 | { |
| 197 | const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET; |
| 198 | return cap_combine(a, |
| 199 | cap_intersect(permitted, __cap_nfsd_set)); |
| 200 | } |
| 201 | |
Iulia Manda | 2813893 | 2015-04-15 16:16:41 -0700 | [diff] [blame] | 202 | #ifdef CONFIG_MULTIUSER |
Serge E. Hallyn | 3263245 | 2011-03-23 16:43:21 -0700 | [diff] [blame] | 203 | extern bool has_capability(struct task_struct *t, int cap); |
| 204 | extern bool has_ns_capability(struct task_struct *t, |
| 205 | struct user_namespace *ns, int cap); |
| 206 | extern bool has_capability_noaudit(struct task_struct *t, int cap); |
Eric Paris | 7b61d64 | 2012-01-03 12:25:15 -0500 | [diff] [blame] | 207 | extern bool has_ns_capability_noaudit(struct task_struct *t, |
| 208 | struct user_namespace *ns, int cap); |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 209 | extern bool capable(int cap); |
| 210 | extern bool ns_capable(struct user_namespace *ns, int cap); |
Iulia Manda | 2813893 | 2015-04-15 16:16:41 -0700 | [diff] [blame] | 211 | #else |
| 212 | static inline bool has_capability(struct task_struct *t, int cap) |
| 213 | { |
| 214 | return true; |
| 215 | } |
| 216 | static inline bool has_ns_capability(struct task_struct *t, |
| 217 | struct user_namespace *ns, int cap) |
| 218 | { |
| 219 | return true; |
| 220 | } |
| 221 | static inline bool has_capability_noaudit(struct task_struct *t, int cap) |
| 222 | { |
| 223 | return true; |
| 224 | } |
| 225 | static inline bool has_ns_capability_noaudit(struct task_struct *t, |
| 226 | struct user_namespace *ns, int cap) |
| 227 | { |
| 228 | return true; |
| 229 | } |
| 230 | static inline bool capable(int cap) |
| 231 | { |
| 232 | return true; |
| 233 | } |
| 234 | static inline bool ns_capable(struct user_namespace *ns, int cap) |
| 235 | { |
| 236 | return true; |
| 237 | } |
| 238 | #endif /* CONFIG_MULTIUSER */ |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 239 | extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap); |
Linus Torvalds | 935d8aa | 2013-04-14 10:06:31 -0700 | [diff] [blame] | 240 | extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap); |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 241 | |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 242 | /* audit system wants to get cap info from files as well */ |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 243 | extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps); |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | #endif /* !_LINUX_CAPABILITY_H */ |