Commit | Line | Data |
---|---|---|
3e1c2515 | 1 | /* Common capabilities, needed by capability.o. |
1da177e4 LT |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2 of the License, or | |
6 | * (at your option) any later version. | |
7 | * | |
8 | */ | |
9 | ||
c59ede7b | 10 | #include <linux/capability.h> |
3fc689e9 | 11 | #include <linux/audit.h> |
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/init.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/security.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/mm.h> | |
18 | #include <linux/mman.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/swap.h> | |
1da177e4 LT |
21 | #include <linux/skbuff.h> |
22 | #include <linux/netlink.h> | |
23 | #include <linux/ptrace.h> | |
24 | #include <linux/xattr.h> | |
25 | #include <linux/hugetlb.h> | |
b5376771 | 26 | #include <linux/mount.h> |
b460cbc5 | 27 | #include <linux/sched.h> |
3898b1b4 AM |
28 | #include <linux/prctl.h> |
29 | #include <linux/securebits.h> | |
3486740a | 30 | #include <linux/user_namespace.h> |
72c2d582 | 31 | |
b5f22a59 SH |
32 | /* |
33 | * If a non-root user executes a setuid-root binary in | |
34 | * !secure(SECURE_NOROOT) mode, then we raise capabilities. | |
35 | * However if fE is also set, then the intent is for only | |
36 | * the file capabilities to be applied, and the setuid-root | |
37 | * bit is left on either to change the uid (plausible) or | |
38 | * to get full privilege on a kernel without file capabilities | |
39 | * support. So in that case we do not raise capabilities. | |
40 | * | |
41 | * Warn if that happens, once per boot. | |
42 | */ | |
d7627467 | 43 | static void warn_setuid_and_fcaps_mixed(const char *fname) |
b5f22a59 SH |
44 | { |
45 | static int warned; | |
46 | if (!warned) { | |
47 | printk(KERN_INFO "warning: `%s' has both setuid-root and" | |
48 | " effective capabilities. Therefore not raising all" | |
49 | " capabilities.\n", fname); | |
50 | warned = 1; | |
51 | } | |
52 | } | |
53 | ||
1da177e4 LT |
54 | int cap_netlink_send(struct sock *sk, struct sk_buff *skb) |
55 | { | |
1da177e4 LT |
56 | return 0; |
57 | } | |
58 | ||
1d045980 DH |
59 | /** |
60 | * cap_capable - Determine whether a task has a particular effective capability | |
3699c53c | 61 | * @cred: The credentials to use |
3486740a | 62 | * @ns: The user namespace in which we need the capability |
1d045980 DH |
63 | * @cap: The capability to check for |
64 | * @audit: Whether to write an audit message or not | |
65 | * | |
66 | * Determine whether the nominated task has the specified capability amongst | |
67 | * its effective set, returning 0 if it does, -ve if it does not. | |
68 | * | |
3699c53c DH |
69 | * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable() |
70 | * and has_capability() functions. That is, it has the reverse semantics: | |
71 | * cap_has_capability() returns 0 when a task has a capability, but the | |
72 | * kernel's capable() and has_capability() returns 1 for this case. | |
a6dbb1ef | 73 | */ |
6a9de491 EP |
74 | int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, |
75 | int cap, int audit) | |
1da177e4 | 76 | { |
3486740a SH |
77 | for (;;) { |
78 | /* The creator of the user namespace has all caps. */ | |
79 | if (targ_ns != &init_user_ns && targ_ns->creator == cred->user) | |
80 | return 0; | |
81 | ||
82 | /* Do we have the necessary capabilities? */ | |
83 | if (targ_ns == cred->user->user_ns) | |
84 | return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM; | |
85 | ||
86 | /* Have we tried all of the parent namespaces? */ | |
87 | if (targ_ns == &init_user_ns) | |
88 | return -EPERM; | |
89 | ||
90 | /* | |
91 | *If you have a capability in a parent user ns, then you have | |
92 | * it over all children user namespaces as well. | |
93 | */ | |
94 | targ_ns = targ_ns->creator->user_ns; | |
95 | } | |
96 | ||
97 | /* We never get here */ | |
1da177e4 LT |
98 | } |
99 | ||
1d045980 DH |
100 | /** |
101 | * cap_settime - Determine whether the current process may set the system clock | |
102 | * @ts: The time to set | |
103 | * @tz: The timezone to set | |
104 | * | |
105 | * Determine whether the current process may set the system clock and timezone | |
106 | * information, returning 0 if permission granted, -ve if denied. | |
107 | */ | |
1e6d7679 | 108 | int cap_settime(const struct timespec *ts, const struct timezone *tz) |
1da177e4 LT |
109 | { |
110 | if (!capable(CAP_SYS_TIME)) | |
111 | return -EPERM; | |
112 | return 0; | |
113 | } | |
114 | ||
1d045980 | 115 | /** |
9e48858f | 116 | * cap_ptrace_access_check - Determine whether the current process may access |
1d045980 DH |
117 | * another |
118 | * @child: The process to be accessed | |
119 | * @mode: The mode of attachment. | |
120 | * | |
8409cca7 SH |
121 | * If we are in the same or an ancestor user_ns and have all the target |
122 | * task's capabilities, then ptrace access is allowed. | |
123 | * If we have the ptrace capability to the target user_ns, then ptrace | |
124 | * access is allowed. | |
125 | * Else denied. | |
126 | * | |
1d045980 DH |
127 | * Determine whether a process may access another, returning 0 if permission |
128 | * granted, -ve if denied. | |
129 | */ | |
9e48858f | 130 | int cap_ptrace_access_check(struct task_struct *child, unsigned int mode) |
1da177e4 | 131 | { |
c69e8d9c | 132 | int ret = 0; |
8409cca7 | 133 | const struct cred *cred, *child_cred; |
c69e8d9c DH |
134 | |
135 | rcu_read_lock(); | |
8409cca7 SH |
136 | cred = current_cred(); |
137 | child_cred = __task_cred(child); | |
138 | if (cred->user->user_ns == child_cred->user->user_ns && | |
139 | cap_issubset(child_cred->cap_permitted, cred->cap_permitted)) | |
140 | goto out; | |
141 | if (ns_capable(child_cred->user->user_ns, CAP_SYS_PTRACE)) | |
142 | goto out; | |
143 | ret = -EPERM; | |
144 | out: | |
c69e8d9c DH |
145 | rcu_read_unlock(); |
146 | return ret; | |
5cd9c58f DH |
147 | } |
148 | ||
1d045980 DH |
149 | /** |
150 | * cap_ptrace_traceme - Determine whether another process may trace the current | |
151 | * @parent: The task proposed to be the tracer | |
152 | * | |
8409cca7 SH |
153 | * If parent is in the same or an ancestor user_ns and has all current's |
154 | * capabilities, then ptrace access is allowed. | |
155 | * If parent has the ptrace capability to current's user_ns, then ptrace | |
156 | * access is allowed. | |
157 | * Else denied. | |
158 | * | |
1d045980 DH |
159 | * Determine whether the nominated task is permitted to trace the current |
160 | * process, returning 0 if permission is granted, -ve if denied. | |
161 | */ | |
5cd9c58f DH |
162 | int cap_ptrace_traceme(struct task_struct *parent) |
163 | { | |
c69e8d9c | 164 | int ret = 0; |
8409cca7 | 165 | const struct cred *cred, *child_cred; |
c69e8d9c DH |
166 | |
167 | rcu_read_lock(); | |
8409cca7 SH |
168 | cred = __task_cred(parent); |
169 | child_cred = current_cred(); | |
170 | if (cred->user->user_ns == child_cred->user->user_ns && | |
171 | cap_issubset(child_cred->cap_permitted, cred->cap_permitted)) | |
172 | goto out; | |
173 | if (has_ns_capability(parent, child_cred->user->user_ns, CAP_SYS_PTRACE)) | |
174 | goto out; | |
175 | ret = -EPERM; | |
176 | out: | |
c69e8d9c DH |
177 | rcu_read_unlock(); |
178 | return ret; | |
1da177e4 LT |
179 | } |
180 | ||
1d045980 DH |
181 | /** |
182 | * cap_capget - Retrieve a task's capability sets | |
183 | * @target: The task from which to retrieve the capability sets | |
184 | * @effective: The place to record the effective set | |
185 | * @inheritable: The place to record the inheritable set | |
186 | * @permitted: The place to record the permitted set | |
187 | * | |
188 | * This function retrieves the capabilities of the nominated task and returns | |
189 | * them to the caller. | |
190 | */ | |
191 | int cap_capget(struct task_struct *target, kernel_cap_t *effective, | |
192 | kernel_cap_t *inheritable, kernel_cap_t *permitted) | |
1da177e4 | 193 | { |
c69e8d9c | 194 | const struct cred *cred; |
b6dff3ec | 195 | |
1da177e4 | 196 | /* Derived from kernel/capability.c:sys_capget. */ |
c69e8d9c DH |
197 | rcu_read_lock(); |
198 | cred = __task_cred(target); | |
b6dff3ec DH |
199 | *effective = cred->cap_effective; |
200 | *inheritable = cred->cap_inheritable; | |
201 | *permitted = cred->cap_permitted; | |
c69e8d9c | 202 | rcu_read_unlock(); |
1da177e4 LT |
203 | return 0; |
204 | } | |
205 | ||
1d045980 DH |
206 | /* |
207 | * Determine whether the inheritable capabilities are limited to the old | |
208 | * permitted set. Returns 1 if they are limited, 0 if they are not. | |
209 | */ | |
72c2d582 AM |
210 | static inline int cap_inh_is_capped(void) |
211 | { | |
72c2d582 | 212 | |
1d045980 DH |
213 | /* they are so limited unless the current task has the CAP_SETPCAP |
214 | * capability | |
215 | */ | |
6a9de491 EP |
216 | if (cap_capable(current_cred(), current_cred()->user->user_ns, |
217 | CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0) | |
1d045980 | 218 | return 0; |
1d045980 | 219 | return 1; |
1209726c | 220 | } |
72c2d582 | 221 | |
1d045980 DH |
222 | /** |
223 | * cap_capset - Validate and apply proposed changes to current's capabilities | |
224 | * @new: The proposed new credentials; alterations should be made here | |
225 | * @old: The current task's current credentials | |
226 | * @effective: A pointer to the proposed new effective capabilities set | |
227 | * @inheritable: A pointer to the proposed new inheritable capabilities set | |
228 | * @permitted: A pointer to the proposed new permitted capabilities set | |
229 | * | |
230 | * This function validates and applies a proposed mass change to the current | |
231 | * process's capability sets. The changes are made to the proposed new | |
232 | * credentials, and assuming no error, will be committed by the caller of LSM. | |
233 | */ | |
d84f4f99 DH |
234 | int cap_capset(struct cred *new, |
235 | const struct cred *old, | |
236 | const kernel_cap_t *effective, | |
237 | const kernel_cap_t *inheritable, | |
238 | const kernel_cap_t *permitted) | |
1da177e4 | 239 | { |
d84f4f99 DH |
240 | if (cap_inh_is_capped() && |
241 | !cap_issubset(*inheritable, | |
242 | cap_combine(old->cap_inheritable, | |
243 | old->cap_permitted))) | |
72c2d582 | 244 | /* incapable of using this inheritable set */ |
1da177e4 | 245 | return -EPERM; |
d84f4f99 | 246 | |
3b7391de | 247 | if (!cap_issubset(*inheritable, |
d84f4f99 DH |
248 | cap_combine(old->cap_inheritable, |
249 | old->cap_bset))) | |
3b7391de SH |
250 | /* no new pI capabilities outside bounding set */ |
251 | return -EPERM; | |
1da177e4 LT |
252 | |
253 | /* verify restrictions on target's new Permitted set */ | |
d84f4f99 | 254 | if (!cap_issubset(*permitted, old->cap_permitted)) |
1da177e4 | 255 | return -EPERM; |
1da177e4 LT |
256 | |
257 | /* verify the _new_Effective_ is a subset of the _new_Permitted_ */ | |
d84f4f99 | 258 | if (!cap_issubset(*effective, *permitted)) |
1da177e4 | 259 | return -EPERM; |
1da177e4 | 260 | |
d84f4f99 DH |
261 | new->cap_effective = *effective; |
262 | new->cap_inheritable = *inheritable; | |
263 | new->cap_permitted = *permitted; | |
1da177e4 LT |
264 | return 0; |
265 | } | |
266 | ||
1d045980 DH |
267 | /* |
268 | * Clear proposed capability sets for execve(). | |
269 | */ | |
b5376771 SH |
270 | static inline void bprm_clear_caps(struct linux_binprm *bprm) |
271 | { | |
a6f76f23 | 272 | cap_clear(bprm->cred->cap_permitted); |
b5376771 SH |
273 | bprm->cap_effective = false; |
274 | } | |
275 | ||
1d045980 DH |
276 | /** |
277 | * cap_inode_need_killpriv - Determine if inode change affects privileges | |
278 | * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV | |
279 | * | |
280 | * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV | |
281 | * affects the security markings on that inode, and if it is, should | |
282 | * inode_killpriv() be invoked or the change rejected? | |
283 | * | |
284 | * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and | |
285 | * -ve to deny the change. | |
286 | */ | |
b5376771 SH |
287 | int cap_inode_need_killpriv(struct dentry *dentry) |
288 | { | |
289 | struct inode *inode = dentry->d_inode; | |
290 | int error; | |
291 | ||
acfa4380 | 292 | if (!inode->i_op->getxattr) |
b5376771 SH |
293 | return 0; |
294 | ||
295 | error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0); | |
296 | if (error <= 0) | |
297 | return 0; | |
298 | return 1; | |
299 | } | |
300 | ||
1d045980 DH |
301 | /** |
302 | * cap_inode_killpriv - Erase the security markings on an inode | |
303 | * @dentry: The inode/dentry to alter | |
304 | * | |
305 | * Erase the privilege-enhancing security markings on an inode. | |
306 | * | |
307 | * Returns 0 if successful, -ve on error. | |
308 | */ | |
b5376771 SH |
309 | int cap_inode_killpriv(struct dentry *dentry) |
310 | { | |
311 | struct inode *inode = dentry->d_inode; | |
312 | ||
acfa4380 | 313 | if (!inode->i_op->removexattr) |
b5376771 SH |
314 | return 0; |
315 | ||
316 | return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS); | |
317 | } | |
318 | ||
1d045980 DH |
319 | /* |
320 | * Calculate the new process capability sets from the capability sets attached | |
321 | * to a file. | |
322 | */ | |
c0b00441 | 323 | static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps, |
a6f76f23 DH |
324 | struct linux_binprm *bprm, |
325 | bool *effective) | |
b5376771 | 326 | { |
a6f76f23 | 327 | struct cred *new = bprm->cred; |
c0b00441 EP |
328 | unsigned i; |
329 | int ret = 0; | |
330 | ||
331 | if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE) | |
a6f76f23 | 332 | *effective = true; |
c0b00441 EP |
333 | |
334 | CAP_FOR_EACH_U32(i) { | |
335 | __u32 permitted = caps->permitted.cap[i]; | |
336 | __u32 inheritable = caps->inheritable.cap[i]; | |
337 | ||
338 | /* | |
339 | * pP' = (X & fP) | (pI & fI) | |
340 | */ | |
a6f76f23 DH |
341 | new->cap_permitted.cap[i] = |
342 | (new->cap_bset.cap[i] & permitted) | | |
343 | (new->cap_inheritable.cap[i] & inheritable); | |
c0b00441 | 344 | |
a6f76f23 DH |
345 | if (permitted & ~new->cap_permitted.cap[i]) |
346 | /* insufficient to execute correctly */ | |
c0b00441 | 347 | ret = -EPERM; |
c0b00441 EP |
348 | } |
349 | ||
350 | /* | |
351 | * For legacy apps, with no internal support for recognizing they | |
352 | * do not have enough capabilities, we return an error if they are | |
353 | * missing some "forced" (aka file-permitted) capabilities. | |
354 | */ | |
a6f76f23 | 355 | return *effective ? ret : 0; |
c0b00441 EP |
356 | } |
357 | ||
1d045980 DH |
358 | /* |
359 | * Extract the on-exec-apply capability sets for an executable file. | |
360 | */ | |
c0b00441 EP |
361 | int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps) |
362 | { | |
363 | struct inode *inode = dentry->d_inode; | |
b5376771 | 364 | __u32 magic_etc; |
e338d263 | 365 | unsigned tocopy, i; |
c0b00441 EP |
366 | int size; |
367 | struct vfs_cap_data caps; | |
368 | ||
369 | memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data)); | |
370 | ||
acfa4380 | 371 | if (!inode || !inode->i_op->getxattr) |
c0b00441 EP |
372 | return -ENODATA; |
373 | ||
374 | size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps, | |
375 | XATTR_CAPS_SZ); | |
a6f76f23 | 376 | if (size == -ENODATA || size == -EOPNOTSUPP) |
c0b00441 EP |
377 | /* no data, that's ok */ |
378 | return -ENODATA; | |
c0b00441 EP |
379 | if (size < 0) |
380 | return size; | |
b5376771 | 381 | |
e338d263 | 382 | if (size < sizeof(magic_etc)) |
b5376771 SH |
383 | return -EINVAL; |
384 | ||
c0b00441 | 385 | cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc); |
b5376771 | 386 | |
a6f76f23 | 387 | switch (magic_etc & VFS_CAP_REVISION_MASK) { |
e338d263 AM |
388 | case VFS_CAP_REVISION_1: |
389 | if (size != XATTR_CAPS_SZ_1) | |
390 | return -EINVAL; | |
391 | tocopy = VFS_CAP_U32_1; | |
392 | break; | |
393 | case VFS_CAP_REVISION_2: | |
394 | if (size != XATTR_CAPS_SZ_2) | |
395 | return -EINVAL; | |
396 | tocopy = VFS_CAP_U32_2; | |
397 | break; | |
b5376771 SH |
398 | default: |
399 | return -EINVAL; | |
400 | } | |
e338d263 | 401 | |
5459c164 | 402 | CAP_FOR_EACH_U32(i) { |
c0b00441 EP |
403 | if (i >= tocopy) |
404 | break; | |
405 | cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted); | |
406 | cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable); | |
e338d263 | 407 | } |
a6f76f23 | 408 | |
c0b00441 | 409 | return 0; |
b5376771 SH |
410 | } |
411 | ||
1d045980 DH |
412 | /* |
413 | * Attempt to get the on-exec apply capability sets for an executable file from | |
414 | * its xattrs and, if present, apply them to the proposed credentials being | |
415 | * constructed by execve(). | |
416 | */ | |
a6f76f23 | 417 | static int get_file_caps(struct linux_binprm *bprm, bool *effective) |
b5376771 SH |
418 | { |
419 | struct dentry *dentry; | |
420 | int rc = 0; | |
c0b00441 | 421 | struct cpu_vfs_cap_data vcaps; |
b5376771 | 422 | |
3318a386 SH |
423 | bprm_clear_caps(bprm); |
424 | ||
1f29fae2 SH |
425 | if (!file_caps_enabled) |
426 | return 0; | |
427 | ||
3318a386 | 428 | if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) |
b5376771 | 429 | return 0; |
b5376771 SH |
430 | |
431 | dentry = dget(bprm->file->f_dentry); | |
b5376771 | 432 | |
c0b00441 EP |
433 | rc = get_vfs_caps_from_disk(dentry, &vcaps); |
434 | if (rc < 0) { | |
435 | if (rc == -EINVAL) | |
436 | printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n", | |
437 | __func__, rc, bprm->filename); | |
438 | else if (rc == -ENODATA) | |
439 | rc = 0; | |
b5376771 SH |
440 | goto out; |
441 | } | |
b5376771 | 442 | |
a6f76f23 DH |
443 | rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective); |
444 | if (rc == -EINVAL) | |
445 | printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n", | |
446 | __func__, rc, bprm->filename); | |
b5376771 SH |
447 | |
448 | out: | |
449 | dput(dentry); | |
450 | if (rc) | |
451 | bprm_clear_caps(bprm); | |
452 | ||
453 | return rc; | |
454 | } | |
455 | ||
1d045980 DH |
456 | /** |
457 | * cap_bprm_set_creds - Set up the proposed credentials for execve(). | |
458 | * @bprm: The execution parameters, including the proposed creds | |
459 | * | |
460 | * Set up the proposed credentials for a new execution context being | |
461 | * constructed by execve(). The proposed creds in @bprm->cred is altered, | |
462 | * which won't take effect immediately. Returns 0 if successful, -ve on error. | |
a6f76f23 DH |
463 | */ |
464 | int cap_bprm_set_creds(struct linux_binprm *bprm) | |
1da177e4 | 465 | { |
a6f76f23 DH |
466 | const struct cred *old = current_cred(); |
467 | struct cred *new = bprm->cred; | |
468 | bool effective; | |
b5376771 | 469 | int ret; |
1da177e4 | 470 | |
a6f76f23 DH |
471 | effective = false; |
472 | ret = get_file_caps(bprm, &effective); | |
473 | if (ret < 0) | |
474 | return ret; | |
1da177e4 | 475 | |
5459c164 | 476 | if (!issecure(SECURE_NOROOT)) { |
b5f22a59 SH |
477 | /* |
478 | * If the legacy file capability is set, then don't set privs | |
479 | * for a setuid root binary run by a non-root user. Do set it | |
480 | * for a root user just to cause least surprise to an admin. | |
481 | */ | |
482 | if (effective && new->uid != 0 && new->euid == 0) { | |
483 | warn_setuid_and_fcaps_mixed(bprm->filename); | |
484 | goto skip; | |
485 | } | |
5459c164 AM |
486 | /* |
487 | * To support inheritance of root-permissions and suid-root | |
488 | * executables under compatibility mode, we override the | |
489 | * capability sets for the file. | |
490 | * | |
a6f76f23 | 491 | * If only the real uid is 0, we do not set the effective bit. |
5459c164 | 492 | */ |
a6f76f23 | 493 | if (new->euid == 0 || new->uid == 0) { |
5459c164 | 494 | /* pP' = (cap_bset & ~0) | (pI & ~0) */ |
a6f76f23 DH |
495 | new->cap_permitted = cap_combine(old->cap_bset, |
496 | old->cap_inheritable); | |
1da177e4 | 497 | } |
a6f76f23 DH |
498 | if (new->euid == 0) |
499 | effective = true; | |
1da177e4 | 500 | } |
b5f22a59 | 501 | skip: |
b5376771 | 502 | |
a6f76f23 DH |
503 | /* Don't let someone trace a set[ug]id/setpcap binary with the revised |
504 | * credentials unless they have the appropriate permit | |
505 | */ | |
506 | if ((new->euid != old->uid || | |
507 | new->egid != old->gid || | |
508 | !cap_issubset(new->cap_permitted, old->cap_permitted)) && | |
509 | bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) { | |
510 | /* downgrade; they get no more than they had, and maybe less */ | |
511 | if (!capable(CAP_SETUID)) { | |
512 | new->euid = new->uid; | |
513 | new->egid = new->gid; | |
1da177e4 | 514 | } |
b3a222e5 SH |
515 | new->cap_permitted = cap_intersect(new->cap_permitted, |
516 | old->cap_permitted); | |
1da177e4 LT |
517 | } |
518 | ||
a6f76f23 DH |
519 | new->suid = new->fsuid = new->euid; |
520 | new->sgid = new->fsgid = new->egid; | |
1da177e4 | 521 | |
4bf2ea77 EP |
522 | if (effective) |
523 | new->cap_effective = new->cap_permitted; | |
524 | else | |
525 | cap_clear(new->cap_effective); | |
a6f76f23 | 526 | bprm->cap_effective = effective; |
1da177e4 | 527 | |
3fc689e9 EP |
528 | /* |
529 | * Audit candidate if current->cap_effective is set | |
530 | * | |
531 | * We do not bother to audit if 3 things are true: | |
532 | * 1) cap_effective has all caps | |
533 | * 2) we are root | |
534 | * 3) root is supposed to have all caps (SECURE_NOROOT) | |
535 | * Since this is just a normal root execing a process. | |
536 | * | |
537 | * Number 1 above might fail if you don't have a full bset, but I think | |
538 | * that is interesting information to audit. | |
539 | */ | |
d84f4f99 DH |
540 | if (!cap_isclear(new->cap_effective)) { |
541 | if (!cap_issubset(CAP_FULL_SET, new->cap_effective) || | |
a6f76f23 DH |
542 | new->euid != 0 || new->uid != 0 || |
543 | issecure(SECURE_NOROOT)) { | |
544 | ret = audit_log_bprm_fcaps(bprm, new, old); | |
545 | if (ret < 0) | |
546 | return ret; | |
547 | } | |
3fc689e9 | 548 | } |
1da177e4 | 549 | |
d84f4f99 | 550 | new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); |
a6f76f23 | 551 | return 0; |
1da177e4 LT |
552 | } |
553 | ||
1d045980 DH |
554 | /** |
555 | * cap_bprm_secureexec - Determine whether a secure execution is required | |
556 | * @bprm: The execution parameters | |
557 | * | |
558 | * Determine whether a secure execution is required, return 1 if it is, and 0 | |
559 | * if it is not. | |
560 | * | |
561 | * The credentials have been committed by this point, and so are no longer | |
562 | * available through @bprm->cred. | |
a6f76f23 DH |
563 | */ |
564 | int cap_bprm_secureexec(struct linux_binprm *bprm) | |
1da177e4 | 565 | { |
c69e8d9c | 566 | const struct cred *cred = current_cred(); |
b6dff3ec DH |
567 | |
568 | if (cred->uid != 0) { | |
b5376771 SH |
569 | if (bprm->cap_effective) |
570 | return 1; | |
a6f76f23 | 571 | if (!cap_isclear(cred->cap_permitted)) |
b5376771 SH |
572 | return 1; |
573 | } | |
574 | ||
b6dff3ec DH |
575 | return (cred->euid != cred->uid || |
576 | cred->egid != cred->gid); | |
1da177e4 LT |
577 | } |
578 | ||
1d045980 DH |
579 | /** |
580 | * cap_inode_setxattr - Determine whether an xattr may be altered | |
581 | * @dentry: The inode/dentry being altered | |
582 | * @name: The name of the xattr to be changed | |
583 | * @value: The value that the xattr will be changed to | |
584 | * @size: The size of value | |
585 | * @flags: The replacement flag | |
586 | * | |
587 | * Determine whether an xattr may be altered or set on an inode, returning 0 if | |
588 | * permission is granted, -ve if denied. | |
589 | * | |
590 | * This is used to make sure security xattrs don't get updated or set by those | |
591 | * who aren't privileged to do so. | |
592 | */ | |
8f0cfa52 DH |
593 | int cap_inode_setxattr(struct dentry *dentry, const char *name, |
594 | const void *value, size_t size, int flags) | |
1da177e4 | 595 | { |
b5376771 SH |
596 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
597 | if (!capable(CAP_SETFCAP)) | |
598 | return -EPERM; | |
599 | return 0; | |
1d045980 DH |
600 | } |
601 | ||
602 | if (!strncmp(name, XATTR_SECURITY_PREFIX, | |
c5b60b5e | 603 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
1da177e4 LT |
604 | !capable(CAP_SYS_ADMIN)) |
605 | return -EPERM; | |
606 | return 0; | |
607 | } | |
608 | ||
1d045980 DH |
609 | /** |
610 | * cap_inode_removexattr - Determine whether an xattr may be removed | |
611 | * @dentry: The inode/dentry being altered | |
612 | * @name: The name of the xattr to be changed | |
613 | * | |
614 | * Determine whether an xattr may be removed from an inode, returning 0 if | |
615 | * permission is granted, -ve if denied. | |
616 | * | |
617 | * This is used to make sure security xattrs don't get removed by those who | |
618 | * aren't privileged to remove them. | |
619 | */ | |
8f0cfa52 | 620 | int cap_inode_removexattr(struct dentry *dentry, const char *name) |
1da177e4 | 621 | { |
b5376771 SH |
622 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
623 | if (!capable(CAP_SETFCAP)) | |
624 | return -EPERM; | |
625 | return 0; | |
1d045980 DH |
626 | } |
627 | ||
628 | if (!strncmp(name, XATTR_SECURITY_PREFIX, | |
c5b60b5e | 629 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
1da177e4 LT |
630 | !capable(CAP_SYS_ADMIN)) |
631 | return -EPERM; | |
632 | return 0; | |
633 | } | |
634 | ||
a6f76f23 | 635 | /* |
1da177e4 LT |
636 | * cap_emulate_setxuid() fixes the effective / permitted capabilities of |
637 | * a process after a call to setuid, setreuid, or setresuid. | |
638 | * | |
639 | * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of | |
640 | * {r,e,s}uid != 0, the permitted and effective capabilities are | |
641 | * cleared. | |
642 | * | |
643 | * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective | |
644 | * capabilities of the process are cleared. | |
645 | * | |
646 | * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective | |
647 | * capabilities are set to the permitted capabilities. | |
648 | * | |
a6f76f23 | 649 | * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should |
1da177e4 LT |
650 | * never happen. |
651 | * | |
a6f76f23 | 652 | * -astor |
1da177e4 LT |
653 | * |
654 | * cevans - New behaviour, Oct '99 | |
655 | * A process may, via prctl(), elect to keep its capabilities when it | |
656 | * calls setuid() and switches away from uid==0. Both permitted and | |
657 | * effective sets will be retained. | |
658 | * Without this change, it was impossible for a daemon to drop only some | |
659 | * of its privilege. The call to setuid(!=0) would drop all privileges! | |
660 | * Keeping uid 0 is not an option because uid 0 owns too many vital | |
661 | * files.. | |
662 | * Thanks to Olaf Kirch and Peter Benie for spotting this. | |
663 | */ | |
d84f4f99 | 664 | static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old) |
1da177e4 | 665 | { |
d84f4f99 DH |
666 | if ((old->uid == 0 || old->euid == 0 || old->suid == 0) && |
667 | (new->uid != 0 && new->euid != 0 && new->suid != 0) && | |
3898b1b4 | 668 | !issecure(SECURE_KEEP_CAPS)) { |
d84f4f99 DH |
669 | cap_clear(new->cap_permitted); |
670 | cap_clear(new->cap_effective); | |
1da177e4 | 671 | } |
d84f4f99 DH |
672 | if (old->euid == 0 && new->euid != 0) |
673 | cap_clear(new->cap_effective); | |
674 | if (old->euid != 0 && new->euid == 0) | |
675 | new->cap_effective = new->cap_permitted; | |
1da177e4 LT |
676 | } |
677 | ||
1d045980 DH |
678 | /** |
679 | * cap_task_fix_setuid - Fix up the results of setuid() call | |
680 | * @new: The proposed credentials | |
681 | * @old: The current task's current credentials | |
682 | * @flags: Indications of what has changed | |
683 | * | |
684 | * Fix up the results of setuid() call before the credential changes are | |
685 | * actually applied, returning 0 to grant the changes, -ve to deny them. | |
686 | */ | |
d84f4f99 | 687 | int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags) |
1da177e4 LT |
688 | { |
689 | switch (flags) { | |
690 | case LSM_SETID_RE: | |
691 | case LSM_SETID_ID: | |
692 | case LSM_SETID_RES: | |
1d045980 DH |
693 | /* juggle the capabilities to follow [RES]UID changes unless |
694 | * otherwise suppressed */ | |
d84f4f99 DH |
695 | if (!issecure(SECURE_NO_SETUID_FIXUP)) |
696 | cap_emulate_setxuid(new, old); | |
1da177e4 | 697 | break; |
1da177e4 | 698 | |
1d045980 DH |
699 | case LSM_SETID_FS: |
700 | /* juggle the capabilties to follow FSUID changes, unless | |
701 | * otherwise suppressed | |
702 | * | |
d84f4f99 DH |
703 | * FIXME - is fsuser used for all CAP_FS_MASK capabilities? |
704 | * if not, we might be a bit too harsh here. | |
705 | */ | |
706 | if (!issecure(SECURE_NO_SETUID_FIXUP)) { | |
1d045980 | 707 | if (old->fsuid == 0 && new->fsuid != 0) |
d84f4f99 DH |
708 | new->cap_effective = |
709 | cap_drop_fs_set(new->cap_effective); | |
1d045980 DH |
710 | |
711 | if (old->fsuid != 0 && new->fsuid == 0) | |
d84f4f99 DH |
712 | new->cap_effective = |
713 | cap_raise_fs_set(new->cap_effective, | |
714 | new->cap_permitted); | |
1da177e4 | 715 | } |
d84f4f99 | 716 | break; |
1d045980 | 717 | |
1da177e4 LT |
718 | default: |
719 | return -EINVAL; | |
720 | } | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
b5376771 SH |
725 | /* |
726 | * Rationale: code calling task_setscheduler, task_setioprio, and | |
727 | * task_setnice, assumes that | |
728 | * . if capable(cap_sys_nice), then those actions should be allowed | |
729 | * . if not capable(cap_sys_nice), but acting on your own processes, | |
730 | * then those actions should be allowed | |
731 | * This is insufficient now since you can call code without suid, but | |
732 | * yet with increased caps. | |
733 | * So we check for increased caps on the target process. | |
734 | */ | |
de45e806 | 735 | static int cap_safe_nice(struct task_struct *p) |
b5376771 | 736 | { |
c69e8d9c DH |
737 | int is_subset; |
738 | ||
739 | rcu_read_lock(); | |
740 | is_subset = cap_issubset(__task_cred(p)->cap_permitted, | |
741 | current_cred()->cap_permitted); | |
742 | rcu_read_unlock(); | |
743 | ||
744 | if (!is_subset && !capable(CAP_SYS_NICE)) | |
b5376771 SH |
745 | return -EPERM; |
746 | return 0; | |
747 | } | |
748 | ||
1d045980 DH |
749 | /** |
750 | * cap_task_setscheduler - Detemine if scheduler policy change is permitted | |
751 | * @p: The task to affect | |
1d045980 DH |
752 | * |
753 | * Detemine if the requested scheduler policy change is permitted for the | |
754 | * specified task, returning 0 if permission is granted, -ve if denied. | |
755 | */ | |
b0ae1981 | 756 | int cap_task_setscheduler(struct task_struct *p) |
b5376771 SH |
757 | { |
758 | return cap_safe_nice(p); | |
759 | } | |
760 | ||
1d045980 DH |
761 | /** |
762 | * cap_task_ioprio - Detemine if I/O priority change is permitted | |
763 | * @p: The task to affect | |
764 | * @ioprio: The I/O priority to set | |
765 | * | |
766 | * Detemine if the requested I/O priority change is permitted for the specified | |
767 | * task, returning 0 if permission is granted, -ve if denied. | |
768 | */ | |
769 | int cap_task_setioprio(struct task_struct *p, int ioprio) | |
b5376771 SH |
770 | { |
771 | return cap_safe_nice(p); | |
772 | } | |
773 | ||
1d045980 DH |
774 | /** |
775 | * cap_task_ioprio - Detemine if task priority change is permitted | |
776 | * @p: The task to affect | |
777 | * @nice: The nice value to set | |
778 | * | |
779 | * Detemine if the requested task priority change is permitted for the | |
780 | * specified task, returning 0 if permission is granted, -ve if denied. | |
781 | */ | |
782 | int cap_task_setnice(struct task_struct *p, int nice) | |
b5376771 SH |
783 | { |
784 | return cap_safe_nice(p); | |
785 | } | |
786 | ||
3b7391de | 787 | /* |
1d045980 DH |
788 | * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from |
789 | * the current task's bounding set. Returns 0 on success, -ve on error. | |
3b7391de | 790 | */ |
d84f4f99 | 791 | static long cap_prctl_drop(struct cred *new, unsigned long cap) |
3b7391de SH |
792 | { |
793 | if (!capable(CAP_SETPCAP)) | |
794 | return -EPERM; | |
795 | if (!cap_valid(cap)) | |
796 | return -EINVAL; | |
d84f4f99 DH |
797 | |
798 | cap_lower(new->cap_bset, cap); | |
3b7391de SH |
799 | return 0; |
800 | } | |
3898b1b4 | 801 | |
1d045980 DH |
802 | /** |
803 | * cap_task_prctl - Implement process control functions for this security module | |
804 | * @option: The process control function requested | |
805 | * @arg2, @arg3, @arg4, @arg5: The argument data for this function | |
806 | * | |
807 | * Allow process control functions (sys_prctl()) to alter capabilities; may | |
808 | * also deny access to other functions not otherwise implemented here. | |
809 | * | |
810 | * Returns 0 or +ve on success, -ENOSYS if this function is not implemented | |
811 | * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM | |
812 | * modules will consider performing the function. | |
813 | */ | |
3898b1b4 | 814 | int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, |
d84f4f99 | 815 | unsigned long arg4, unsigned long arg5) |
3898b1b4 | 816 | { |
d84f4f99 | 817 | struct cred *new; |
3898b1b4 AM |
818 | long error = 0; |
819 | ||
d84f4f99 DH |
820 | new = prepare_creds(); |
821 | if (!new) | |
822 | return -ENOMEM; | |
823 | ||
3898b1b4 AM |
824 | switch (option) { |
825 | case PR_CAPBSET_READ: | |
d84f4f99 | 826 | error = -EINVAL; |
3898b1b4 | 827 | if (!cap_valid(arg2)) |
d84f4f99 DH |
828 | goto error; |
829 | error = !!cap_raised(new->cap_bset, arg2); | |
830 | goto no_change; | |
831 | ||
3898b1b4 | 832 | case PR_CAPBSET_DROP: |
d84f4f99 DH |
833 | error = cap_prctl_drop(new, arg2); |
834 | if (error < 0) | |
835 | goto error; | |
836 | goto changed; | |
3898b1b4 AM |
837 | |
838 | /* | |
839 | * The next four prctl's remain to assist with transitioning a | |
840 | * system from legacy UID=0 based privilege (when filesystem | |
841 | * capabilities are not in use) to a system using filesystem | |
842 | * capabilities only - as the POSIX.1e draft intended. | |
843 | * | |
844 | * Note: | |
845 | * | |
846 | * PR_SET_SECUREBITS = | |
847 | * issecure_mask(SECURE_KEEP_CAPS_LOCKED) | |
848 | * | issecure_mask(SECURE_NOROOT) | |
849 | * | issecure_mask(SECURE_NOROOT_LOCKED) | |
850 | * | issecure_mask(SECURE_NO_SETUID_FIXUP) | |
851 | * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED) | |
852 | * | |
853 | * will ensure that the current process and all of its | |
854 | * children will be locked into a pure | |
855 | * capability-based-privilege environment. | |
856 | */ | |
857 | case PR_SET_SECUREBITS: | |
d84f4f99 DH |
858 | error = -EPERM; |
859 | if ((((new->securebits & SECURE_ALL_LOCKS) >> 1) | |
860 | & (new->securebits ^ arg2)) /*[1]*/ | |
861 | || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/ | |
862 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ | |
6a9de491 | 863 | || (cap_capable(current_cred(), |
3486740a | 864 | current_cred()->user->user_ns, CAP_SETPCAP, |
3699c53c | 865 | SECURITY_CAP_AUDIT) != 0) /*[4]*/ |
3898b1b4 AM |
866 | /* |
867 | * [1] no changing of bits that are locked | |
868 | * [2] no unlocking of locks | |
869 | * [3] no setting of unsupported bits | |
870 | * [4] doing anything requires privilege (go read about | |
871 | * the "sendmail capabilities bug") | |
872 | */ | |
d84f4f99 DH |
873 | ) |
874 | /* cannot change a locked bit */ | |
875 | goto error; | |
876 | new->securebits = arg2; | |
877 | goto changed; | |
878 | ||
3898b1b4 | 879 | case PR_GET_SECUREBITS: |
d84f4f99 DH |
880 | error = new->securebits; |
881 | goto no_change; | |
3898b1b4 | 882 | |
3898b1b4 AM |
883 | case PR_GET_KEEPCAPS: |
884 | if (issecure(SECURE_KEEP_CAPS)) | |
885 | error = 1; | |
d84f4f99 DH |
886 | goto no_change; |
887 | ||
3898b1b4 | 888 | case PR_SET_KEEPCAPS: |
d84f4f99 | 889 | error = -EINVAL; |
3898b1b4 | 890 | if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */ |
d84f4f99 DH |
891 | goto error; |
892 | error = -EPERM; | |
893 | if (issecure(SECURE_KEEP_CAPS_LOCKED)) | |
894 | goto error; | |
895 | if (arg2) | |
896 | new->securebits |= issecure_mask(SECURE_KEEP_CAPS); | |
3898b1b4 | 897 | else |
d84f4f99 DH |
898 | new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); |
899 | goto changed; | |
3898b1b4 AM |
900 | |
901 | default: | |
902 | /* No functionality available - continue with default */ | |
d84f4f99 DH |
903 | error = -ENOSYS; |
904 | goto error; | |
3898b1b4 AM |
905 | } |
906 | ||
907 | /* Functionality provided */ | |
d84f4f99 DH |
908 | changed: |
909 | return commit_creds(new); | |
910 | ||
911 | no_change: | |
d84f4f99 DH |
912 | error: |
913 | abort_creds(new); | |
914 | return error; | |
1da177e4 LT |
915 | } |
916 | ||
1d045980 DH |
917 | /** |
918 | * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted | |
919 | * @mm: The VM space in which the new mapping is to be made | |
920 | * @pages: The size of the mapping | |
921 | * | |
922 | * Determine whether the allocation of a new virtual mapping by the current | |
923 | * task is permitted, returning 0 if permission is granted, -ve if not. | |
924 | */ | |
34b4e4aa | 925 | int cap_vm_enough_memory(struct mm_struct *mm, long pages) |
1da177e4 LT |
926 | { |
927 | int cap_sys_admin = 0; | |
928 | ||
6a9de491 | 929 | if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN, |
3699c53c | 930 | SECURITY_CAP_NOAUDIT) == 0) |
1da177e4 | 931 | cap_sys_admin = 1; |
34b4e4aa | 932 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
1da177e4 | 933 | } |
7c73875e EP |
934 | |
935 | /* | |
936 | * cap_file_mmap - check if able to map given addr | |
937 | * @file: unused | |
938 | * @reqprot: unused | |
939 | * @prot: unused | |
940 | * @flags: unused | |
941 | * @addr: address attempting to be mapped | |
942 | * @addr_only: unused | |
943 | * | |
6f262d8e | 944 | * If the process is attempting to map memory below dac_mmap_min_addr they need |
7c73875e EP |
945 | * CAP_SYS_RAWIO. The other parameters to this function are unused by the |
946 | * capability security module. Returns 0 if this mapping should be allowed | |
947 | * -EPERM if not. | |
948 | */ | |
949 | int cap_file_mmap(struct file *file, unsigned long reqprot, | |
950 | unsigned long prot, unsigned long flags, | |
951 | unsigned long addr, unsigned long addr_only) | |
952 | { | |
953 | int ret = 0; | |
954 | ||
a2551df7 | 955 | if (addr < dac_mmap_min_addr) { |
6a9de491 | 956 | ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO, |
7c73875e EP |
957 | SECURITY_CAP_AUDIT); |
958 | /* set PF_SUPERPRIV if it turns out we allow the low mmap */ | |
959 | if (ret == 0) | |
960 | current->flags |= PF_SUPERPRIV; | |
961 | } | |
962 | return ret; | |
963 | } |