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