Merge 4.14.43 into android-4.14
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / security / commoncap.c
CommitLineData
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>
b1d9e6b0 15#include <linux/lsm_hooks.h>
1da177e4
LT
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>
40401530 31#include <linux/binfmts.h>
51b79bee 32#include <linux/personality.h>
72c2d582 33
36ac63e7
CY
34#ifdef CONFIG_ANDROID_PARANOID_NETWORK
35#include <linux/android_aid.h>
36#endif
37
b5f22a59
SH
38/*
39 * If a non-root user executes a setuid-root binary in
40 * !secure(SECURE_NOROOT) mode, then we raise capabilities.
41 * However if fE is also set, then the intent is for only
42 * the file capabilities to be applied, and the setuid-root
43 * bit is left on either to change the uid (plausible) or
44 * to get full privilege on a kernel without file capabilities
45 * support. So in that case we do not raise capabilities.
46 *
47 * Warn if that happens, once per boot.
48 */
d7627467 49static void warn_setuid_and_fcaps_mixed(const char *fname)
b5f22a59
SH
50{
51 static int warned;
52 if (!warned) {
53 printk(KERN_INFO "warning: `%s' has both setuid-root and"
54 " effective capabilities. Therefore not raising all"
55 " capabilities.\n", fname);
56 warned = 1;
57 }
58}
59
1d045980 60/**
2b02b4ab 61 * __cap_capable - Determine whether a task has a particular effective capability
3699c53c 62 * @cred: The credentials to use
3486740a 63 * @ns: The user namespace in which we need the capability
1d045980
DH
64 * @cap: The capability to check for
65 * @audit: Whether to write an audit message or not
66 *
67 * Determine whether the nominated task has the specified capability amongst
68 * its effective set, returning 0 if it does, -ve if it does not.
69 *
3699c53c
DH
70 * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
71 * and has_capability() functions. That is, it has the reverse semantics:
72 * cap_has_capability() returns 0 when a task has a capability, but the
73 * kernel's capable() and has_capability() returns 1 for this case.
a6dbb1ef 74 */
2b02b4ab 75int __cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
6a9de491 76 int cap, int audit)
1da177e4 77{
520d9eab 78 struct user_namespace *ns = targ_ns;
3486740a 79
520d9eab
EB
80 /* See if cred has the capability in the target user namespace
81 * by examining the target user namespace and all of the target
82 * user namespace's parents.
83 */
84 for (;;) {
3486740a 85 /* Do we have the necessary capabilities? */
520d9eab 86 if (ns == cred->user_ns)
3486740a
SH
87 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
88
64db4c7f
KT
89 /*
90 * If we're already at a lower level than we're looking for,
91 * we're done searching.
92 */
93 if (ns->level <= cred->user_ns->level)
3486740a
SH
94 return -EPERM;
95
520d9eab
EB
96 /*
97 * The owner of the user namespace in the parent of the
98 * user namespace has all caps.
99 */
100 if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
101 return 0;
102
3486740a 103 /*
520d9eab 104 * If you have a capability in a parent user ns, then you have
3486740a
SH
105 * it over all children user namespaces as well.
106 */
520d9eab 107 ns = ns->parent;
3486740a
SH
108 }
109
110 /* We never get here */
1da177e4
LT
111}
112
2b02b4ab
JS
113int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
114 int cap, int audit)
115{
116 int ret = __cap_capable(cred, targ_ns, cap, audit);
117
118#ifdef CONFIG_ANDROID_PARANOID_NETWORK
119 if (ret != 0 && cap == CAP_NET_RAW && in_egroup_p(AID_NET_RAW)) {
120 printk("Process %s granted CAP_NET_RAW from Android group net_raw.\n", current->comm);
121 printk(" Please update the .rc file to explictly set 'capabilities NET_RAW'\n");
122 printk(" Implicit grants are deprecated and will be removed in the future.\n");
123 return 0;
124 }
125 if (ret != 0 && cap == CAP_NET_ADMIN && in_egroup_p(AID_NET_ADMIN)) {
126 printk("Process %s granted CAP_NET_ADMIN from Android group net_admin.\n", current->comm);
127 printk(" Please update the .rc file to explictly set 'capabilities NET_ADMIN'\n");
128 printk(" Implicit grants are deprecated and will be removed in the future.\n");
129 return 0;
130 }
131#endif
132 return ret;
133}
1d045980
DH
134/**
135 * cap_settime - Determine whether the current process may set the system clock
136 * @ts: The time to set
137 * @tz: The timezone to set
138 *
139 * Determine whether the current process may set the system clock and timezone
140 * information, returning 0 if permission granted, -ve if denied.
141 */
457db29b 142int cap_settime(const struct timespec64 *ts, const struct timezone *tz)
1da177e4
LT
143{
144 if (!capable(CAP_SYS_TIME))
145 return -EPERM;
146 return 0;
147}
148
1d045980 149/**
9e48858f 150 * cap_ptrace_access_check - Determine whether the current process may access
1d045980
DH
151 * another
152 * @child: The process to be accessed
153 * @mode: The mode of attachment.
154 *
8409cca7
SH
155 * If we are in the same or an ancestor user_ns and have all the target
156 * task's capabilities, then ptrace access is allowed.
157 * If we have the ptrace capability to the target user_ns, then ptrace
158 * access is allowed.
159 * Else denied.
160 *
1d045980
DH
161 * Determine whether a process may access another, returning 0 if permission
162 * granted, -ve if denied.
163 */
9e48858f 164int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
1da177e4 165{
c69e8d9c 166 int ret = 0;
8409cca7 167 const struct cred *cred, *child_cred;
caaee623 168 const kernel_cap_t *caller_caps;
c69e8d9c
DH
169
170 rcu_read_lock();
8409cca7
SH
171 cred = current_cred();
172 child_cred = __task_cred(child);
caaee623
JH
173 if (mode & PTRACE_MODE_FSCREDS)
174 caller_caps = &cred->cap_effective;
175 else
176 caller_caps = &cred->cap_permitted;
c4a4d603 177 if (cred->user_ns == child_cred->user_ns &&
caaee623 178 cap_issubset(child_cred->cap_permitted, *caller_caps))
8409cca7 179 goto out;
c4a4d603 180 if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE))
8409cca7
SH
181 goto out;
182 ret = -EPERM;
183out:
c69e8d9c
DH
184 rcu_read_unlock();
185 return ret;
5cd9c58f
DH
186}
187
1d045980
DH
188/**
189 * cap_ptrace_traceme - Determine whether another process may trace the current
190 * @parent: The task proposed to be the tracer
191 *
8409cca7
SH
192 * If parent is in the same or an ancestor user_ns and has all current's
193 * capabilities, then ptrace access is allowed.
194 * If parent has the ptrace capability to current's user_ns, then ptrace
195 * access is allowed.
196 * Else denied.
197 *
1d045980
DH
198 * Determine whether the nominated task is permitted to trace the current
199 * process, returning 0 if permission is granted, -ve if denied.
200 */
5cd9c58f
DH
201int cap_ptrace_traceme(struct task_struct *parent)
202{
c69e8d9c 203 int ret = 0;
8409cca7 204 const struct cred *cred, *child_cred;
c69e8d9c
DH
205
206 rcu_read_lock();
8409cca7
SH
207 cred = __task_cred(parent);
208 child_cred = current_cred();
c4a4d603 209 if (cred->user_ns == child_cred->user_ns &&
8409cca7
SH
210 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
211 goto out;
c4a4d603 212 if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE))
8409cca7
SH
213 goto out;
214 ret = -EPERM;
215out:
c69e8d9c
DH
216 rcu_read_unlock();
217 return ret;
1da177e4
LT
218}
219
1d045980
DH
220/**
221 * cap_capget - Retrieve a task's capability sets
222 * @target: The task from which to retrieve the capability sets
223 * @effective: The place to record the effective set
224 * @inheritable: The place to record the inheritable set
225 * @permitted: The place to record the permitted set
226 *
227 * This function retrieves the capabilities of the nominated task and returns
228 * them to the caller.
229 */
230int cap_capget(struct task_struct *target, kernel_cap_t *effective,
231 kernel_cap_t *inheritable, kernel_cap_t *permitted)
1da177e4 232{
c69e8d9c 233 const struct cred *cred;
b6dff3ec 234
1da177e4 235 /* Derived from kernel/capability.c:sys_capget. */
c69e8d9c
DH
236 rcu_read_lock();
237 cred = __task_cred(target);
b6dff3ec
DH
238 *effective = cred->cap_effective;
239 *inheritable = cred->cap_inheritable;
240 *permitted = cred->cap_permitted;
c69e8d9c 241 rcu_read_unlock();
1da177e4
LT
242 return 0;
243}
244
1d045980
DH
245/*
246 * Determine whether the inheritable capabilities are limited to the old
247 * permitted set. Returns 1 if they are limited, 0 if they are not.
248 */
72c2d582
AM
249static inline int cap_inh_is_capped(void)
250{
72c2d582 251
1d045980
DH
252 /* they are so limited unless the current task has the CAP_SETPCAP
253 * capability
254 */
c4a4d603 255 if (cap_capable(current_cred(), current_cred()->user_ns,
6a9de491 256 CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
1d045980 257 return 0;
1d045980 258 return 1;
1209726c 259}
72c2d582 260
1d045980
DH
261/**
262 * cap_capset - Validate and apply proposed changes to current's capabilities
263 * @new: The proposed new credentials; alterations should be made here
264 * @old: The current task's current credentials
265 * @effective: A pointer to the proposed new effective capabilities set
266 * @inheritable: A pointer to the proposed new inheritable capabilities set
267 * @permitted: A pointer to the proposed new permitted capabilities set
268 *
269 * This function validates and applies a proposed mass change to the current
270 * process's capability sets. The changes are made to the proposed new
271 * credentials, and assuming no error, will be committed by the caller of LSM.
272 */
d84f4f99
DH
273int cap_capset(struct cred *new,
274 const struct cred *old,
275 const kernel_cap_t *effective,
276 const kernel_cap_t *inheritable,
277 const kernel_cap_t *permitted)
1da177e4 278{
d84f4f99
DH
279 if (cap_inh_is_capped() &&
280 !cap_issubset(*inheritable,
281 cap_combine(old->cap_inheritable,
282 old->cap_permitted)))
72c2d582 283 /* incapable of using this inheritable set */
1da177e4 284 return -EPERM;
d84f4f99 285
3b7391de 286 if (!cap_issubset(*inheritable,
d84f4f99
DH
287 cap_combine(old->cap_inheritable,
288 old->cap_bset)))
3b7391de
SH
289 /* no new pI capabilities outside bounding set */
290 return -EPERM;
1da177e4
LT
291
292 /* verify restrictions on target's new Permitted set */
d84f4f99 293 if (!cap_issubset(*permitted, old->cap_permitted))
1da177e4 294 return -EPERM;
1da177e4
LT
295
296 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
d84f4f99 297 if (!cap_issubset(*effective, *permitted))
1da177e4 298 return -EPERM;
1da177e4 299
d84f4f99
DH
300 new->cap_effective = *effective;
301 new->cap_inheritable = *inheritable;
302 new->cap_permitted = *permitted;
58319057
AL
303
304 /*
305 * Mask off ambient bits that are no longer both permitted and
306 * inheritable.
307 */
308 new->cap_ambient = cap_intersect(new->cap_ambient,
309 cap_intersect(*permitted,
310 *inheritable));
311 if (WARN_ON(!cap_ambient_invariant_ok(new)))
312 return -EINVAL;
1da177e4
LT
313 return 0;
314}
315
1d045980
DH
316/**
317 * cap_inode_need_killpriv - Determine if inode change affects privileges
318 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
319 *
320 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
321 * affects the security markings on that inode, and if it is, should
ab5348c9 322 * inode_killpriv() be invoked or the change rejected.
1d045980 323 *
ab5348c9
SB
324 * Returns 1 if security.capability has a value, meaning inode_killpriv()
325 * is required, 0 otherwise, meaning inode_killpriv() is not required.
1d045980 326 */
b5376771
SH
327int cap_inode_need_killpriv(struct dentry *dentry)
328{
c6f493d6 329 struct inode *inode = d_backing_inode(dentry);
b5376771
SH
330 int error;
331
5d6c3191
AG
332 error = __vfs_getxattr(dentry, inode, XATTR_NAME_CAPS, NULL, 0);
333 return error > 0;
b5376771
SH
334}
335
1d045980
DH
336/**
337 * cap_inode_killpriv - Erase the security markings on an inode
338 * @dentry: The inode/dentry to alter
339 *
340 * Erase the privilege-enhancing security markings on an inode.
341 *
342 * Returns 0 if successful, -ve on error.
343 */
b5376771
SH
344int cap_inode_killpriv(struct dentry *dentry)
345{
5d6c3191 346 int error;
b5376771 347
5d6c3191
AG
348 error = __vfs_removexattr(dentry, XATTR_NAME_CAPS);
349 if (error == -EOPNOTSUPP)
350 error = 0;
351 return error;
b5376771
SH
352}
353
8db6c34f
SH
354static bool rootid_owns_currentns(kuid_t kroot)
355{
356 struct user_namespace *ns;
357
358 if (!uid_valid(kroot))
359 return false;
360
361 for (ns = current_user_ns(); ; ns = ns->parent) {
362 if (from_kuid(ns, kroot) == 0)
363 return true;
364 if (ns == &init_user_ns)
365 break;
366 }
367
368 return false;
369}
370
371static __u32 sansflags(__u32 m)
372{
373 return m & ~VFS_CAP_FLAGS_EFFECTIVE;
374}
375
df4373c5 376static bool is_v2header(size_t size, const struct vfs_cap_data *cap)
8db6c34f 377{
8db6c34f
SH
378 if (size != XATTR_CAPS_SZ_2)
379 return false;
df4373c5 380 return sansflags(le32_to_cpu(cap->magic_etc)) == VFS_CAP_REVISION_2;
8db6c34f
SH
381}
382
df4373c5 383static bool is_v3header(size_t size, const struct vfs_cap_data *cap)
8db6c34f 384{
8db6c34f
SH
385 if (size != XATTR_CAPS_SZ_3)
386 return false;
df4373c5 387 return sansflags(le32_to_cpu(cap->magic_etc)) == VFS_CAP_REVISION_3;
8db6c34f
SH
388}
389
390/*
391 * getsecurity: We are called for security.* before any attempt to read the
392 * xattr from the inode itself.
393 *
394 * This gives us a chance to read the on-disk value and convert it. If we
395 * return -EOPNOTSUPP, then vfs_getxattr() will call the i_op handler.
396 *
397 * Note we are not called by vfs_getxattr_alloc(), but that is only called
398 * by the integrity subsystem, which really wants the unconverted values -
399 * so that's good.
400 */
401int cap_inode_getsecurity(struct inode *inode, const char *name, void **buffer,
402 bool alloc)
403{
404 int size, ret;
405 kuid_t kroot;
406 uid_t root, mappedroot;
407 char *tmpbuf = NULL;
408 struct vfs_cap_data *cap;
409 struct vfs_ns_cap_data *nscap;
410 struct dentry *dentry;
411 struct user_namespace *fs_ns;
412
413 if (strcmp(name, "capability") != 0)
414 return -EOPNOTSUPP;
415
416 dentry = d_find_alias(inode);
417 if (!dentry)
418 return -EINVAL;
419
420 size = sizeof(struct vfs_ns_cap_data);
421 ret = (int) vfs_getxattr_alloc(dentry, XATTR_NAME_CAPS,
422 &tmpbuf, size, GFP_NOFS);
423 dput(dentry);
424
425 if (ret < 0)
426 return ret;
427
428 fs_ns = inode->i_sb->s_user_ns;
429 cap = (struct vfs_cap_data *) tmpbuf;
df4373c5 430 if (is_v2header((size_t) ret, cap)) {
8db6c34f
SH
431 /* If this is sizeof(vfs_cap_data) then we're ok with the
432 * on-disk value, so return that. */
433 if (alloc)
434 *buffer = tmpbuf;
435 else
436 kfree(tmpbuf);
437 return ret;
df4373c5 438 } else if (!is_v3header((size_t) ret, cap)) {
8db6c34f
SH
439 kfree(tmpbuf);
440 return -EINVAL;
441 }
442
443 nscap = (struct vfs_ns_cap_data *) tmpbuf;
444 root = le32_to_cpu(nscap->rootid);
445 kroot = make_kuid(fs_ns, root);
446
447 /* If the root kuid maps to a valid uid in current ns, then return
448 * this as a nscap. */
449 mappedroot = from_kuid(current_user_ns(), kroot);
450 if (mappedroot != (uid_t)-1 && mappedroot != (uid_t)0) {
451 if (alloc) {
452 *buffer = tmpbuf;
453 nscap->rootid = cpu_to_le32(mappedroot);
454 } else
455 kfree(tmpbuf);
456 return size;
457 }
458
459 if (!rootid_owns_currentns(kroot)) {
460 kfree(tmpbuf);
461 return -EOPNOTSUPP;
462 }
463
464 /* This comes from a parent namespace. Return as a v2 capability */
465 size = sizeof(struct vfs_cap_data);
466 if (alloc) {
467 *buffer = kmalloc(size, GFP_ATOMIC);
468 if (*buffer) {
469 struct vfs_cap_data *cap = *buffer;
470 __le32 nsmagic, magic;
471 magic = VFS_CAP_REVISION_2;
472 nsmagic = le32_to_cpu(nscap->magic_etc);
473 if (nsmagic & VFS_CAP_FLAGS_EFFECTIVE)
474 magic |= VFS_CAP_FLAGS_EFFECTIVE;
475 memcpy(&cap->data, &nscap->data, sizeof(__le32) * 2 * VFS_CAP_U32);
476 cap->magic_etc = cpu_to_le32(magic);
77df079b
TH
477 } else {
478 size = -ENOMEM;
8db6c34f
SH
479 }
480 }
481 kfree(tmpbuf);
482 return size;
483}
484
485static kuid_t rootid_from_xattr(const void *value, size_t size,
486 struct user_namespace *task_ns)
487{
488 const struct vfs_ns_cap_data *nscap = value;
489 uid_t rootid = 0;
490
491 if (size == XATTR_CAPS_SZ_3)
492 rootid = le32_to_cpu(nscap->rootid);
493
494 return make_kuid(task_ns, rootid);
495}
496
df4373c5 497static bool validheader(size_t size, const struct vfs_cap_data *cap)
8db6c34f 498{
df4373c5 499 return is_v2header(size, cap) || is_v3header(size, cap);
8db6c34f
SH
500}
501
502/*
503 * User requested a write of security.capability. If needed, update the
504 * xattr to change from v2 to v3, or to fixup the v3 rootid.
505 *
506 * If all is ok, we return the new size, on error return < 0.
507 */
508int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size)
509{
510 struct vfs_ns_cap_data *nscap;
511 uid_t nsrootid;
512 const struct vfs_cap_data *cap = *ivalue;
513 __u32 magic, nsmagic;
514 struct inode *inode = d_backing_inode(dentry);
515 struct user_namespace *task_ns = current_user_ns(),
516 *fs_ns = inode->i_sb->s_user_ns;
517 kuid_t rootid;
518 size_t newsize;
519
520 if (!*ivalue)
521 return -EINVAL;
df4373c5 522 if (!validheader(size, cap))
8db6c34f
SH
523 return -EINVAL;
524 if (!capable_wrt_inode_uidgid(inode, CAP_SETFCAP))
525 return -EPERM;
526 if (size == XATTR_CAPS_SZ_2)
527 if (ns_capable(inode->i_sb->s_user_ns, CAP_SETFCAP))
528 /* user is privileged, just write the v2 */
529 return size;
530
531 rootid = rootid_from_xattr(*ivalue, size, task_ns);
532 if (!uid_valid(rootid))
533 return -EINVAL;
534
535 nsrootid = from_kuid(fs_ns, rootid);
536 if (nsrootid == -1)
537 return -EINVAL;
538
539 newsize = sizeof(struct vfs_ns_cap_data);
540 nscap = kmalloc(newsize, GFP_ATOMIC);
541 if (!nscap)
542 return -ENOMEM;
543 nscap->rootid = cpu_to_le32(nsrootid);
544 nsmagic = VFS_CAP_REVISION_3;
545 magic = le32_to_cpu(cap->magic_etc);
546 if (magic & VFS_CAP_FLAGS_EFFECTIVE)
547 nsmagic |= VFS_CAP_FLAGS_EFFECTIVE;
548 nscap->magic_etc = cpu_to_le32(nsmagic);
549 memcpy(&nscap->data, &cap->data, sizeof(__le32) * 2 * VFS_CAP_U32);
550
551 kvfree(*ivalue);
552 *ivalue = nscap;
553 return newsize;
554}
555
1d045980
DH
556/*
557 * Calculate the new process capability sets from the capability sets attached
558 * to a file.
559 */
c0b00441 560static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
a6f76f23 561 struct linux_binprm *bprm,
4d49f671
ZL
562 bool *effective,
563 bool *has_cap)
b5376771 564{
a6f76f23 565 struct cred *new = bprm->cred;
c0b00441
EP
566 unsigned i;
567 int ret = 0;
568
569 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
a6f76f23 570 *effective = true;
c0b00441 571
4d49f671
ZL
572 if (caps->magic_etc & VFS_CAP_REVISION_MASK)
573 *has_cap = true;
574
c0b00441
EP
575 CAP_FOR_EACH_U32(i) {
576 __u32 permitted = caps->permitted.cap[i];
577 __u32 inheritable = caps->inheritable.cap[i];
578
579 /*
580 * pP' = (X & fP) | (pI & fI)
58319057 581 * The addition of pA' is handled later.
c0b00441 582 */
a6f76f23
DH
583 new->cap_permitted.cap[i] =
584 (new->cap_bset.cap[i] & permitted) |
585 (new->cap_inheritable.cap[i] & inheritable);
c0b00441 586
a6f76f23
DH
587 if (permitted & ~new->cap_permitted.cap[i])
588 /* insufficient to execute correctly */
c0b00441 589 ret = -EPERM;
c0b00441
EP
590 }
591
592 /*
593 * For legacy apps, with no internal support for recognizing they
594 * do not have enough capabilities, we return an error if they are
595 * missing some "forced" (aka file-permitted) capabilities.
596 */
a6f76f23 597 return *effective ? ret : 0;
c0b00441
EP
598}
599
1d045980
DH
600/*
601 * Extract the on-exec-apply capability sets for an executable file.
602 */
c0b00441
EP
603int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
604{
c6f493d6 605 struct inode *inode = d_backing_inode(dentry);
b5376771 606 __u32 magic_etc;
e338d263 607 unsigned tocopy, i;
c0b00441 608 int size;
8db6c34f
SH
609 struct vfs_ns_cap_data data, *nscaps = &data;
610 struct vfs_cap_data *caps = (struct vfs_cap_data *) &data;
611 kuid_t rootkuid;
76ba89c7 612 struct user_namespace *fs_ns;
c0b00441
EP
613
614 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
615
5d6c3191 616 if (!inode)
c0b00441
EP
617 return -ENODATA;
618
76ba89c7 619 fs_ns = inode->i_sb->s_user_ns;
5d6c3191 620 size = __vfs_getxattr((struct dentry *)dentry, inode,
8db6c34f 621 XATTR_NAME_CAPS, &data, XATTR_CAPS_SZ);
a6f76f23 622 if (size == -ENODATA || size == -EOPNOTSUPP)
c0b00441
EP
623 /* no data, that's ok */
624 return -ENODATA;
8db6c34f 625
c0b00441
EP
626 if (size < 0)
627 return size;
b5376771 628
e338d263 629 if (size < sizeof(magic_etc))
b5376771
SH
630 return -EINVAL;
631
8db6c34f 632 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps->magic_etc);
b5376771 633
8db6c34f 634 rootkuid = make_kuid(fs_ns, 0);
a6f76f23 635 switch (magic_etc & VFS_CAP_REVISION_MASK) {
e338d263
AM
636 case VFS_CAP_REVISION_1:
637 if (size != XATTR_CAPS_SZ_1)
638 return -EINVAL;
639 tocopy = VFS_CAP_U32_1;
640 break;
641 case VFS_CAP_REVISION_2:
642 if (size != XATTR_CAPS_SZ_2)
643 return -EINVAL;
644 tocopy = VFS_CAP_U32_2;
645 break;
8db6c34f
SH
646 case VFS_CAP_REVISION_3:
647 if (size != XATTR_CAPS_SZ_3)
648 return -EINVAL;
649 tocopy = VFS_CAP_U32_3;
650 rootkuid = make_kuid(fs_ns, le32_to_cpu(nscaps->rootid));
651 break;
652
b5376771
SH
653 default:
654 return -EINVAL;
655 }
8db6c34f
SH
656 /* Limit the caps to the mounter of the filesystem
657 * or the more limited uid specified in the xattr.
658 */
659 if (!rootid_owns_currentns(rootkuid))
660 return -ENODATA;
e338d263 661
5459c164 662 CAP_FOR_EACH_U32(i) {
c0b00441
EP
663 if (i >= tocopy)
664 break;
8db6c34f
SH
665 cpu_caps->permitted.cap[i] = le32_to_cpu(caps->data[i].permitted);
666 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps->data[i].inheritable);
e338d263 667 }
a6f76f23 668
7d8b6c63
EP
669 cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
670 cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
671
c0b00441 672 return 0;
b5376771
SH
673}
674
1d045980
DH
675/*
676 * Attempt to get the on-exec apply capability sets for an executable file from
677 * its xattrs and, if present, apply them to the proposed credentials being
678 * constructed by execve().
679 */
4d49f671 680static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
b5376771 681{
b5376771 682 int rc = 0;
c0b00441 683 struct cpu_vfs_cap_data vcaps;
b5376771 684
ee67ae7e 685 cap_clear(bprm->cred->cap_permitted);
3318a386 686
1f29fae2
SH
687 if (!file_caps_enabled)
688 return 0;
689
380cf5ba 690 if (!mnt_may_suid(bprm->file->f_path.mnt))
b5376771 691 return 0;
380cf5ba
AL
692
693 /*
694 * This check is redundant with mnt_may_suid() but is kept to make
695 * explicit that capability bits are limited to s_user_ns and its
696 * descendants.
697 */
d07b846f
SF
698 if (!current_in_userns(bprm->file->f_path.mnt->mnt_sb->s_user_ns))
699 return 0;
b5376771 700
f4a4a8b1 701 rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
c0b00441
EP
702 if (rc < 0) {
703 if (rc == -EINVAL)
8db6c34f
SH
704 printk(KERN_NOTICE "Invalid argument reading file caps for %s\n",
705 bprm->filename);
c0b00441
EP
706 else if (rc == -ENODATA)
707 rc = 0;
b5376771
SH
708 goto out;
709 }
b5376771 710
4d49f671 711 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
a6f76f23
DH
712 if (rc == -EINVAL)
713 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
714 __func__, rc, bprm->filename);
b5376771
SH
715
716out:
b5376771 717 if (rc)
ee67ae7e 718 cap_clear(bprm->cred->cap_permitted);
b5376771
SH
719
720 return rc;
721}
722
1d045980
DH
723/**
724 * cap_bprm_set_creds - Set up the proposed credentials for execve().
725 * @bprm: The execution parameters, including the proposed creds
726 *
727 * Set up the proposed credentials for a new execution context being
728 * constructed by execve(). The proposed creds in @bprm->cred is altered,
729 * which won't take effect immediately. Returns 0 if successful, -ve on error.
a6f76f23
DH
730 */
731int cap_bprm_set_creds(struct linux_binprm *bprm)
1da177e4 732{
a6f76f23
DH
733 const struct cred *old = current_cred();
734 struct cred *new = bprm->cred;
58319057 735 bool effective, has_cap = false, is_setid;
b5376771 736 int ret;
18815a18 737 kuid_t root_uid;
1da177e4 738
58319057
AL
739 if (WARN_ON(!cap_ambient_invariant_ok(old)))
740 return -EPERM;
741
a6f76f23 742 effective = false;
4d49f671 743 ret = get_file_caps(bprm, &effective, &has_cap);
a6f76f23
DH
744 if (ret < 0)
745 return ret;
1da177e4 746
18815a18
EB
747 root_uid = make_kuid(new->user_ns, 0);
748
5459c164 749 if (!issecure(SECURE_NOROOT)) {
b5f22a59
SH
750 /*
751 * If the legacy file capability is set, then don't set privs
752 * for a setuid root binary run by a non-root user. Do set it
753 * for a root user just to cause least surprise to an admin.
754 */
18815a18 755 if (has_cap && !uid_eq(new->uid, root_uid) && uid_eq(new->euid, root_uid)) {
b5f22a59
SH
756 warn_setuid_and_fcaps_mixed(bprm->filename);
757 goto skip;
758 }
5459c164
AM
759 /*
760 * To support inheritance of root-permissions and suid-root
761 * executables under compatibility mode, we override the
762 * capability sets for the file.
763 *
a6f76f23 764 * If only the real uid is 0, we do not set the effective bit.
5459c164 765 */
18815a18 766 if (uid_eq(new->euid, root_uid) || uid_eq(new->uid, root_uid)) {
5459c164 767 /* pP' = (cap_bset & ~0) | (pI & ~0) */
a6f76f23
DH
768 new->cap_permitted = cap_combine(old->cap_bset,
769 old->cap_inheritable);
1da177e4 770 }
18815a18 771 if (uid_eq(new->euid, root_uid))
a6f76f23 772 effective = true;
1da177e4 773 }
b5f22a59 774skip:
b5376771 775
d52fc5dd
EP
776 /* if we have fs caps, clear dangerous personality flags */
777 if (!cap_issubset(new->cap_permitted, old->cap_permitted))
778 bprm->per_clear |= PER_CLEAR_ON_SETID;
779
780
a6f76f23 781 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
259e5e6c
AL
782 * credentials unless they have the appropriate permit.
783 *
784 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
a6f76f23 785 */
58319057
AL
786 is_setid = !uid_eq(new->euid, old->uid) || !gid_eq(new->egid, old->gid);
787
788 if ((is_setid ||
a6f76f23 789 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
9227dd2a 790 ((bprm->unsafe & ~LSM_UNSAFE_PTRACE) ||
20523132 791 !ptracer_capable(current, new->user_ns))) {
a6f76f23 792 /* downgrade; they get no more than they had, and maybe less */
70169420 793 if (!ns_capable(new->user_ns, CAP_SETUID) ||
259e5e6c 794 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
a6f76f23
DH
795 new->euid = new->uid;
796 new->egid = new->gid;
1da177e4 797 }
b3a222e5
SH
798 new->cap_permitted = cap_intersect(new->cap_permitted,
799 old->cap_permitted);
1da177e4
LT
800 }
801
a6f76f23
DH
802 new->suid = new->fsuid = new->euid;
803 new->sgid = new->fsgid = new->egid;
1da177e4 804
58319057
AL
805 /* File caps or setid cancels ambient. */
806 if (has_cap || is_setid)
807 cap_clear(new->cap_ambient);
808
809 /*
810 * Now that we've computed pA', update pP' to give:
811 * pP' = (X & fP) | (pI & fI) | pA'
812 */
813 new->cap_permitted = cap_combine(new->cap_permitted, new->cap_ambient);
814
815 /*
816 * Set pE' = (fE ? pP' : pA'). Because pA' is zero if fE is set,
817 * this is the same as pE' = (fE ? pP' : 0) | pA'.
818 */
4bf2ea77
EP
819 if (effective)
820 new->cap_effective = new->cap_permitted;
821 else
58319057
AL
822 new->cap_effective = new->cap_ambient;
823
824 if (WARN_ON(!cap_ambient_invariant_ok(new)))
825 return -EPERM;
826
3fc689e9
EP
827 /*
828 * Audit candidate if current->cap_effective is set
829 *
830 * We do not bother to audit if 3 things are true:
831 * 1) cap_effective has all caps
832 * 2) we are root
833 * 3) root is supposed to have all caps (SECURE_NOROOT)
834 * Since this is just a normal root execing a process.
835 *
836 * Number 1 above might fail if you don't have a full bset, but I think
837 * that is interesting information to audit.
838 */
58319057 839 if (!cap_issubset(new->cap_effective, new->cap_ambient)) {
d84f4f99 840 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
18815a18 841 !uid_eq(new->euid, root_uid) || !uid_eq(new->uid, root_uid) ||
a6f76f23
DH
842 issecure(SECURE_NOROOT)) {
843 ret = audit_log_bprm_fcaps(bprm, new, old);
844 if (ret < 0)
845 return ret;
846 }
3fc689e9 847 }
1da177e4 848
d84f4f99 849 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
58319057
AL
850
851 if (WARN_ON(!cap_ambient_invariant_ok(new)))
852 return -EPERM;
853
46d98eb4 854 /* Check for privilege-elevated exec. */
ee67ae7e
KC
855 bprm->cap_elevated = 0;
856 if (is_setid) {
857 bprm->cap_elevated = 1;
858 } else if (!uid_eq(new->uid, root_uid)) {
859 if (effective ||
860 !cap_issubset(new->cap_permitted, new->cap_ambient))
861 bprm->cap_elevated = 1;
b5376771
SH
862 }
863
ee67ae7e 864 return 0;
1da177e4
LT
865}
866
1d045980
DH
867/**
868 * cap_inode_setxattr - Determine whether an xattr may be altered
869 * @dentry: The inode/dentry being altered
870 * @name: The name of the xattr to be changed
871 * @value: The value that the xattr will be changed to
872 * @size: The size of value
873 * @flags: The replacement flag
874 *
875 * Determine whether an xattr may be altered or set on an inode, returning 0 if
876 * permission is granted, -ve if denied.
877 *
878 * This is used to make sure security xattrs don't get updated or set by those
879 * who aren't privileged to do so.
880 */
8f0cfa52
DH
881int cap_inode_setxattr(struct dentry *dentry, const char *name,
882 const void *value, size_t size, int flags)
1da177e4 883{
8db6c34f
SH
884 /* Ignore non-security xattrs */
885 if (strncmp(name, XATTR_SECURITY_PREFIX,
886 sizeof(XATTR_SECURITY_PREFIX) - 1) != 0)
887 return 0;
888
889 /*
890 * For XATTR_NAME_CAPS the check will be done in
891 * cap_convert_nscap(), called by setxattr()
892 */
893 if (strcmp(name, XATTR_NAME_CAPS) == 0)
b5376771 894 return 0;
1d045980 895
8db6c34f 896 if (!capable(CAP_SYS_ADMIN))
1da177e4
LT
897 return -EPERM;
898 return 0;
899}
900
1d045980
DH
901/**
902 * cap_inode_removexattr - Determine whether an xattr may be removed
903 * @dentry: The inode/dentry being altered
904 * @name: The name of the xattr to be changed
905 *
906 * Determine whether an xattr may be removed from an inode, returning 0 if
907 * permission is granted, -ve if denied.
908 *
909 * This is used to make sure security xattrs don't get removed by those who
910 * aren't privileged to remove them.
911 */
8f0cfa52 912int cap_inode_removexattr(struct dentry *dentry, const char *name)
1da177e4 913{
8db6c34f
SH
914 /* Ignore non-security xattrs */
915 if (strncmp(name, XATTR_SECURITY_PREFIX,
916 sizeof(XATTR_SECURITY_PREFIX) - 1) != 0)
917 return 0;
918
919 if (strcmp(name, XATTR_NAME_CAPS) == 0) {
920 /* security.capability gets namespaced */
921 struct inode *inode = d_backing_inode(dentry);
922 if (!inode)
923 return -EINVAL;
924 if (!capable_wrt_inode_uidgid(inode, CAP_SETFCAP))
b5376771
SH
925 return -EPERM;
926 return 0;
1d045980
DH
927 }
928
8db6c34f 929 if (!capable(CAP_SYS_ADMIN))
1da177e4
LT
930 return -EPERM;
931 return 0;
932}
933
a6f76f23 934/*
1da177e4
LT
935 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
936 * a process after a call to setuid, setreuid, or setresuid.
937 *
938 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
939 * {r,e,s}uid != 0, the permitted and effective capabilities are
940 * cleared.
941 *
942 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
943 * capabilities of the process are cleared.
944 *
945 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
946 * capabilities are set to the permitted capabilities.
947 *
a6f76f23 948 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
1da177e4
LT
949 * never happen.
950 *
a6f76f23 951 * -astor
1da177e4
LT
952 *
953 * cevans - New behaviour, Oct '99
954 * A process may, via prctl(), elect to keep its capabilities when it
955 * calls setuid() and switches away from uid==0. Both permitted and
956 * effective sets will be retained.
957 * Without this change, it was impossible for a daemon to drop only some
958 * of its privilege. The call to setuid(!=0) would drop all privileges!
959 * Keeping uid 0 is not an option because uid 0 owns too many vital
960 * files..
961 * Thanks to Olaf Kirch and Peter Benie for spotting this.
962 */
d84f4f99 963static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
1da177e4 964{
18815a18
EB
965 kuid_t root_uid = make_kuid(old->user_ns, 0);
966
967 if ((uid_eq(old->uid, root_uid) ||
968 uid_eq(old->euid, root_uid) ||
969 uid_eq(old->suid, root_uid)) &&
970 (!uid_eq(new->uid, root_uid) &&
971 !uid_eq(new->euid, root_uid) &&
58319057
AL
972 !uid_eq(new->suid, root_uid))) {
973 if (!issecure(SECURE_KEEP_CAPS)) {
974 cap_clear(new->cap_permitted);
975 cap_clear(new->cap_effective);
976 }
977
978 /*
979 * Pre-ambient programs expect setresuid to nonroot followed
980 * by exec to drop capabilities. We should make sure that
981 * this remains the case.
982 */
983 cap_clear(new->cap_ambient);
1da177e4 984 }
18815a18 985 if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
d84f4f99 986 cap_clear(new->cap_effective);
18815a18 987 if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
d84f4f99 988 new->cap_effective = new->cap_permitted;
1da177e4
LT
989}
990
1d045980
DH
991/**
992 * cap_task_fix_setuid - Fix up the results of setuid() call
993 * @new: The proposed credentials
994 * @old: The current task's current credentials
995 * @flags: Indications of what has changed
996 *
997 * Fix up the results of setuid() call before the credential changes are
998 * actually applied, returning 0 to grant the changes, -ve to deny them.
999 */
d84f4f99 1000int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
1da177e4
LT
1001{
1002 switch (flags) {
1003 case LSM_SETID_RE:
1004 case LSM_SETID_ID:
1005 case LSM_SETID_RES:
1d045980
DH
1006 /* juggle the capabilities to follow [RES]UID changes unless
1007 * otherwise suppressed */
d84f4f99
DH
1008 if (!issecure(SECURE_NO_SETUID_FIXUP))
1009 cap_emulate_setxuid(new, old);
1da177e4 1010 break;
1da177e4 1011
1d045980
DH
1012 case LSM_SETID_FS:
1013 /* juggle the capabilties to follow FSUID changes, unless
1014 * otherwise suppressed
1015 *
d84f4f99
DH
1016 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
1017 * if not, we might be a bit too harsh here.
1018 */
1019 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
18815a18
EB
1020 kuid_t root_uid = make_kuid(old->user_ns, 0);
1021 if (uid_eq(old->fsuid, root_uid) && !uid_eq(new->fsuid, root_uid))
d84f4f99
DH
1022 new->cap_effective =
1023 cap_drop_fs_set(new->cap_effective);
1d045980 1024
18815a18 1025 if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
d84f4f99
DH
1026 new->cap_effective =
1027 cap_raise_fs_set(new->cap_effective,
1028 new->cap_permitted);
1da177e4 1029 }
d84f4f99 1030 break;
1d045980 1031
1da177e4
LT
1032 default:
1033 return -EINVAL;
1034 }
1035
1036 return 0;
1037}
1038
b5376771
SH
1039/*
1040 * Rationale: code calling task_setscheduler, task_setioprio, and
1041 * task_setnice, assumes that
1042 * . if capable(cap_sys_nice), then those actions should be allowed
1043 * . if not capable(cap_sys_nice), but acting on your own processes,
1044 * then those actions should be allowed
1045 * This is insufficient now since you can call code without suid, but
1046 * yet with increased caps.
1047 * So we check for increased caps on the target process.
1048 */
de45e806 1049static int cap_safe_nice(struct task_struct *p)
b5376771 1050{
f54fb863 1051 int is_subset, ret = 0;
c69e8d9c
DH
1052
1053 rcu_read_lock();
1054 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
1055 current_cred()->cap_permitted);
f54fb863
SH
1056 if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
1057 ret = -EPERM;
c69e8d9c
DH
1058 rcu_read_unlock();
1059
f54fb863 1060 return ret;
b5376771
SH
1061}
1062
1d045980
DH
1063/**
1064 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
1065 * @p: The task to affect
1d045980
DH
1066 *
1067 * Detemine if the requested scheduler policy change is permitted for the
1068 * specified task, returning 0 if permission is granted, -ve if denied.
1069 */
b0ae1981 1070int cap_task_setscheduler(struct task_struct *p)
b5376771
SH
1071{
1072 return cap_safe_nice(p);
1073}
1074
1d045980
DH
1075/**
1076 * cap_task_ioprio - Detemine if I/O priority change is permitted
1077 * @p: The task to affect
1078 * @ioprio: The I/O priority to set
1079 *
1080 * Detemine if the requested I/O priority change is permitted for the specified
1081 * task, returning 0 if permission is granted, -ve if denied.
1082 */
1083int cap_task_setioprio(struct task_struct *p, int ioprio)
b5376771
SH
1084{
1085 return cap_safe_nice(p);
1086}
1087
1d045980
DH
1088/**
1089 * cap_task_ioprio - Detemine if task priority change is permitted
1090 * @p: The task to affect
1091 * @nice: The nice value to set
1092 *
1093 * Detemine if the requested task priority change is permitted for the
1094 * specified task, returning 0 if permission is granted, -ve if denied.
1095 */
1096int cap_task_setnice(struct task_struct *p, int nice)
b5376771
SH
1097{
1098 return cap_safe_nice(p);
1099}
1100
3b7391de 1101/*
1d045980
DH
1102 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
1103 * the current task's bounding set. Returns 0 on success, -ve on error.
3b7391de 1104 */
6d6f3328 1105static int cap_prctl_drop(unsigned long cap)
3b7391de 1106{
6d6f3328
TH
1107 struct cred *new;
1108
160da84d 1109 if (!ns_capable(current_user_ns(), CAP_SETPCAP))
3b7391de
SH
1110 return -EPERM;
1111 if (!cap_valid(cap))
1112 return -EINVAL;
d84f4f99 1113
6d6f3328
TH
1114 new = prepare_creds();
1115 if (!new)
1116 return -ENOMEM;
d84f4f99 1117 cap_lower(new->cap_bset, cap);
6d6f3328 1118 return commit_creds(new);
3b7391de 1119}
3898b1b4 1120
1d045980
DH
1121/**
1122 * cap_task_prctl - Implement process control functions for this security module
1123 * @option: The process control function requested
1124 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
1125 *
1126 * Allow process control functions (sys_prctl()) to alter capabilities; may
1127 * also deny access to other functions not otherwise implemented here.
1128 *
1129 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
1130 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
1131 * modules will consider performing the function.
1132 */
3898b1b4 1133int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
d84f4f99 1134 unsigned long arg4, unsigned long arg5)
3898b1b4 1135{
6d6f3328 1136 const struct cred *old = current_cred();
d84f4f99 1137 struct cred *new;
d84f4f99 1138
3898b1b4
AM
1139 switch (option) {
1140 case PR_CAPBSET_READ:
1141 if (!cap_valid(arg2))
6d6f3328
TH
1142 return -EINVAL;
1143 return !!cap_raised(old->cap_bset, arg2);
d84f4f99 1144
3898b1b4 1145 case PR_CAPBSET_DROP:
6d6f3328 1146 return cap_prctl_drop(arg2);
3898b1b4
AM
1147
1148 /*
1149 * The next four prctl's remain to assist with transitioning a
1150 * system from legacy UID=0 based privilege (when filesystem
1151 * capabilities are not in use) to a system using filesystem
1152 * capabilities only - as the POSIX.1e draft intended.
1153 *
1154 * Note:
1155 *
1156 * PR_SET_SECUREBITS =
1157 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
1158 * | issecure_mask(SECURE_NOROOT)
1159 * | issecure_mask(SECURE_NOROOT_LOCKED)
1160 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
1161 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
1162 *
1163 * will ensure that the current process and all of its
1164 * children will be locked into a pure
1165 * capability-based-privilege environment.
1166 */
1167 case PR_SET_SECUREBITS:
6d6f3328
TH
1168 if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
1169 & (old->securebits ^ arg2)) /*[1]*/
1170 || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
d84f4f99 1171 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
6a9de491 1172 || (cap_capable(current_cred(),
c4a4d603 1173 current_cred()->user_ns, CAP_SETPCAP,
3699c53c 1174 SECURITY_CAP_AUDIT) != 0) /*[4]*/
3898b1b4
AM
1175 /*
1176 * [1] no changing of bits that are locked
1177 * [2] no unlocking of locks
1178 * [3] no setting of unsupported bits
1179 * [4] doing anything requires privilege (go read about
1180 * the "sendmail capabilities bug")
1181 */
d84f4f99
DH
1182 )
1183 /* cannot change a locked bit */
6d6f3328
TH
1184 return -EPERM;
1185
1186 new = prepare_creds();
1187 if (!new)
1188 return -ENOMEM;
d84f4f99 1189 new->securebits = arg2;
6d6f3328 1190 return commit_creds(new);
d84f4f99 1191
3898b1b4 1192 case PR_GET_SECUREBITS:
6d6f3328 1193 return old->securebits;
3898b1b4 1194
3898b1b4 1195 case PR_GET_KEEPCAPS:
6d6f3328 1196 return !!issecure(SECURE_KEEP_CAPS);
d84f4f99 1197
3898b1b4
AM
1198 case PR_SET_KEEPCAPS:
1199 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
6d6f3328 1200 return -EINVAL;
d84f4f99 1201 if (issecure(SECURE_KEEP_CAPS_LOCKED))
6d6f3328
TH
1202 return -EPERM;
1203
1204 new = prepare_creds();
1205 if (!new)
1206 return -ENOMEM;
d84f4f99
DH
1207 if (arg2)
1208 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
3898b1b4 1209 else
d84f4f99 1210 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
6d6f3328 1211 return commit_creds(new);
3898b1b4 1212
58319057
AL
1213 case PR_CAP_AMBIENT:
1214 if (arg2 == PR_CAP_AMBIENT_CLEAR_ALL) {
1215 if (arg3 | arg4 | arg5)
1216 return -EINVAL;
1217
1218 new = prepare_creds();
1219 if (!new)
1220 return -ENOMEM;
1221 cap_clear(new->cap_ambient);
1222 return commit_creds(new);
1223 }
1224
1225 if (((!cap_valid(arg3)) | arg4 | arg5))
1226 return -EINVAL;
1227
1228 if (arg2 == PR_CAP_AMBIENT_IS_SET) {
1229 return !!cap_raised(current_cred()->cap_ambient, arg3);
1230 } else if (arg2 != PR_CAP_AMBIENT_RAISE &&
1231 arg2 != PR_CAP_AMBIENT_LOWER) {
1232 return -EINVAL;
1233 } else {
1234 if (arg2 == PR_CAP_AMBIENT_RAISE &&
1235 (!cap_raised(current_cred()->cap_permitted, arg3) ||
1236 !cap_raised(current_cred()->cap_inheritable,
746bf6d6
AL
1237 arg3) ||
1238 issecure(SECURE_NO_CAP_AMBIENT_RAISE)))
58319057
AL
1239 return -EPERM;
1240
1241 new = prepare_creds();
1242 if (!new)
1243 return -ENOMEM;
1244 if (arg2 == PR_CAP_AMBIENT_RAISE)
1245 cap_raise(new->cap_ambient, arg3);
1246 else
1247 cap_lower(new->cap_ambient, arg3);
1248 return commit_creds(new);
1249 }
1250
3898b1b4
AM
1251 default:
1252 /* No functionality available - continue with default */
6d6f3328 1253 return -ENOSYS;
3898b1b4 1254 }
1da177e4
LT
1255}
1256
1d045980
DH
1257/**
1258 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
1259 * @mm: The VM space in which the new mapping is to be made
1260 * @pages: The size of the mapping
1261 *
1262 * Determine whether the allocation of a new virtual mapping by the current
b1d9e6b0 1263 * task is permitted, returning 1 if permission is granted, 0 if not.
1d045980 1264 */
34b4e4aa 1265int cap_vm_enough_memory(struct mm_struct *mm, long pages)
1da177e4
LT
1266{
1267 int cap_sys_admin = 0;
1268
6a9de491 1269 if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
3699c53c 1270 SECURITY_CAP_NOAUDIT) == 0)
1da177e4 1271 cap_sys_admin = 1;
b1d9e6b0 1272 return cap_sys_admin;
1da177e4 1273}
7c73875e
EP
1274
1275/*
d007794a 1276 * cap_mmap_addr - check if able to map given addr
7c73875e 1277 * @addr: address attempting to be mapped
7c73875e 1278 *
6f262d8e 1279 * If the process is attempting to map memory below dac_mmap_min_addr they need
7c73875e
EP
1280 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
1281 * capability security module. Returns 0 if this mapping should be allowed
1282 * -EPERM if not.
1283 */
d007794a 1284int cap_mmap_addr(unsigned long addr)
7c73875e
EP
1285{
1286 int ret = 0;
1287
a2551df7 1288 if (addr < dac_mmap_min_addr) {
6a9de491 1289 ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
7c73875e
EP
1290 SECURITY_CAP_AUDIT);
1291 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
1292 if (ret == 0)
1293 current->flags |= PF_SUPERPRIV;
1294 }
1295 return ret;
1296}
d007794a 1297
e5467859
AV
1298int cap_mmap_file(struct file *file, unsigned long reqprot,
1299 unsigned long prot, unsigned long flags)
d007794a 1300{
e5467859 1301 return 0;
d007794a 1302}
b1d9e6b0
CS
1303
1304#ifdef CONFIG_SECURITY
1305
ca97d939 1306struct security_hook_list capability_hooks[] __lsm_ro_after_init = {
b1d9e6b0
CS
1307 LSM_HOOK_INIT(capable, cap_capable),
1308 LSM_HOOK_INIT(settime, cap_settime),
1309 LSM_HOOK_INIT(ptrace_access_check, cap_ptrace_access_check),
1310 LSM_HOOK_INIT(ptrace_traceme, cap_ptrace_traceme),
1311 LSM_HOOK_INIT(capget, cap_capget),
1312 LSM_HOOK_INIT(capset, cap_capset),
1313 LSM_HOOK_INIT(bprm_set_creds, cap_bprm_set_creds),
b1d9e6b0
CS
1314 LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
1315 LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
8db6c34f 1316 LSM_HOOK_INIT(inode_getsecurity, cap_inode_getsecurity),
b1d9e6b0
CS
1317 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
1318 LSM_HOOK_INIT(mmap_file, cap_mmap_file),
1319 LSM_HOOK_INIT(task_fix_setuid, cap_task_fix_setuid),
1320 LSM_HOOK_INIT(task_prctl, cap_task_prctl),
1321 LSM_HOOK_INIT(task_setscheduler, cap_task_setscheduler),
1322 LSM_HOOK_INIT(task_setioprio, cap_task_setioprio),
1323 LSM_HOOK_INIT(task_setnice, cap_task_setnice),
1324 LSM_HOOK_INIT(vm_enough_memory, cap_vm_enough_memory),
1325};
1326
1327void __init capability_add_hooks(void)
1328{
d69dece5
CS
1329 security_add_hooks(capability_hooks, ARRAY_SIZE(capability_hooks),
1330 "capability");
b1d9e6b0
CS
1331}
1332
1333#endif /* CONFIG_SECURITY */