• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Common capabilities, needed by capability.o.
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 
10 #include <linux/capability.h>
11 #include <linux/audit.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/lsm_hooks.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>
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>
26 #include <linux/mount.h>
27 #include <linux/sched.h>
28 #include <linux/prctl.h>
29 #include <linux/securebits.h>
30 #include <linux/user_namespace.h>
31 #include <linux/binfmts.h>
32 #include <linux/personality.h>
33 
34 #ifdef CONFIG_ANDROID_PARANOID_NETWORK
35 #include <linux/android_aid.h>
36 #endif
37 
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  */
warn_setuid_and_fcaps_mixed(const char * fname)49 static void warn_setuid_and_fcaps_mixed(const char *fname)
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 
60 /**
61  * __cap_capable - Determine whether a task has a particular effective capability
62  * @cred: The credentials to use
63  * @ns:  The user namespace in which we need the capability
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  *
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.
74  */
__cap_capable(const struct cred * cred,struct user_namespace * targ_ns,int cap,int audit)75 int __cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
76 		int cap, int audit)
77 {
78 	struct user_namespace *ns = targ_ns;
79 
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 (;;) {
85 		/* Do we have the necessary capabilities? */
86 		if (ns == cred->user_ns)
87 			return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
88 
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)
94 			return -EPERM;
95 
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 
103 		/*
104 		 * If you have a capability in a parent user ns, then you have
105 		 * it over all children user namespaces as well.
106 		 */
107 		ns = ns->parent;
108 	}
109 
110 	/* We never get here */
111 }
112 
cap_capable(const struct cred * cred,struct user_namespace * targ_ns,int cap,int audit)113 int 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 }
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  */
cap_settime(const struct timespec64 * ts,const struct timezone * tz)142 int cap_settime(const struct timespec64 *ts, const struct timezone *tz)
143 {
144 	if (!capable(CAP_SYS_TIME))
145 		return -EPERM;
146 	return 0;
147 }
148 
149 /**
150  * cap_ptrace_access_check - Determine whether the current process may access
151  *			   another
152  * @child: The process to be accessed
153  * @mode: The mode of attachment.
154  *
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  *
161  * Determine whether a process may access another, returning 0 if permission
162  * granted, -ve if denied.
163  */
cap_ptrace_access_check(struct task_struct * child,unsigned int mode)164 int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
165 {
166 	int ret = 0;
167 	const struct cred *cred, *child_cred;
168 	const kernel_cap_t *caller_caps;
169 
170 	rcu_read_lock();
171 	cred = current_cred();
172 	child_cred = __task_cred(child);
173 	if (mode & PTRACE_MODE_FSCREDS)
174 		caller_caps = &cred->cap_effective;
175 	else
176 		caller_caps = &cred->cap_permitted;
177 	if (cred->user_ns == child_cred->user_ns &&
178 	    cap_issubset(child_cred->cap_permitted, *caller_caps))
179 		goto out;
180 	if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE))
181 		goto out;
182 	ret = -EPERM;
183 out:
184 	rcu_read_unlock();
185 	return ret;
186 }
187 
188 /**
189  * cap_ptrace_traceme - Determine whether another process may trace the current
190  * @parent: The task proposed to be the tracer
191  *
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  *
198  * Determine whether the nominated task is permitted to trace the current
199  * process, returning 0 if permission is granted, -ve if denied.
200  */
cap_ptrace_traceme(struct task_struct * parent)201 int cap_ptrace_traceme(struct task_struct *parent)
202 {
203 	int ret = 0;
204 	const struct cred *cred, *child_cred;
205 
206 	rcu_read_lock();
207 	cred = __task_cred(parent);
208 	child_cred = current_cred();
209 	if (cred->user_ns == child_cred->user_ns &&
210 	    cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
211 		goto out;
212 	if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE))
213 		goto out;
214 	ret = -EPERM;
215 out:
216 	rcu_read_unlock();
217 	return ret;
218 }
219 
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  */
cap_capget(struct task_struct * target,kernel_cap_t * effective,kernel_cap_t * inheritable,kernel_cap_t * permitted)230 int cap_capget(struct task_struct *target, kernel_cap_t *effective,
231 	       kernel_cap_t *inheritable, kernel_cap_t *permitted)
232 {
233 	const struct cred *cred;
234 
235 	/* Derived from kernel/capability.c:sys_capget. */
236 	rcu_read_lock();
237 	cred = __task_cred(target);
238 	*effective   = cred->cap_effective;
239 	*inheritable = cred->cap_inheritable;
240 	*permitted   = cred->cap_permitted;
241 	rcu_read_unlock();
242 	return 0;
243 }
244 
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  */
cap_inh_is_capped(void)249 static inline int cap_inh_is_capped(void)
250 {
251 
252 	/* they are so limited unless the current task has the CAP_SETPCAP
253 	 * capability
254 	 */
255 	if (cap_capable(current_cred(), current_cred()->user_ns,
256 			CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
257 		return 0;
258 	return 1;
259 }
260 
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  */
cap_capset(struct cred * new,const struct cred * old,const kernel_cap_t * effective,const kernel_cap_t * inheritable,const kernel_cap_t * permitted)273 int 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)
278 {
279 	if (cap_inh_is_capped() &&
280 	    !cap_issubset(*inheritable,
281 			  cap_combine(old->cap_inheritable,
282 				      old->cap_permitted)))
283 		/* incapable of using this inheritable set */
284 		return -EPERM;
285 
286 	if (!cap_issubset(*inheritable,
287 			  cap_combine(old->cap_inheritable,
288 				      old->cap_bset)))
289 		/* no new pI capabilities outside bounding set */
290 		return -EPERM;
291 
292 	/* verify restrictions on target's new Permitted set */
293 	if (!cap_issubset(*permitted, old->cap_permitted))
294 		return -EPERM;
295 
296 	/* verify the _new_Effective_ is a subset of the _new_Permitted_ */
297 	if (!cap_issubset(*effective, *permitted))
298 		return -EPERM;
299 
300 	new->cap_effective   = *effective;
301 	new->cap_inheritable = *inheritable;
302 	new->cap_permitted   = *permitted;
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;
313 	return 0;
314 }
315 
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
322  * inode_killpriv() be invoked or the change rejected.
323  *
324  * Returns 1 if security.capability has a value, meaning inode_killpriv()
325  * is required, 0 otherwise, meaning inode_killpriv() is not required.
326  */
cap_inode_need_killpriv(struct dentry * dentry)327 int cap_inode_need_killpriv(struct dentry *dentry)
328 {
329 	struct inode *inode = d_backing_inode(dentry);
330 	int error;
331 
332 	error = __vfs_getxattr(dentry, inode, XATTR_NAME_CAPS, NULL, 0);
333 	return error > 0;
334 }
335 
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  */
cap_inode_killpriv(struct dentry * dentry)344 int cap_inode_killpriv(struct dentry *dentry)
345 {
346 	int error;
347 
348 	error = __vfs_removexattr(dentry, XATTR_NAME_CAPS);
349 	if (error == -EOPNOTSUPP)
350 		error = 0;
351 	return error;
352 }
353 
rootid_owns_currentns(kuid_t kroot)354 static 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 
sansflags(__u32 m)371 static __u32 sansflags(__u32 m)
372 {
373 	return m & ~VFS_CAP_FLAGS_EFFECTIVE;
374 }
375 
is_v2header(size_t size,const struct vfs_cap_data * cap)376 static bool is_v2header(size_t size, const struct vfs_cap_data *cap)
377 {
378 	if (size != XATTR_CAPS_SZ_2)
379 		return false;
380 	return sansflags(le32_to_cpu(cap->magic_etc)) == VFS_CAP_REVISION_2;
381 }
382 
is_v3header(size_t size,const struct vfs_cap_data * cap)383 static bool is_v3header(size_t size, const struct vfs_cap_data *cap)
384 {
385 	if (size != XATTR_CAPS_SZ_3)
386 		return false;
387 	return sansflags(le32_to_cpu(cap->magic_etc)) == VFS_CAP_REVISION_3;
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  */
cap_inode_getsecurity(struct inode * inode,const char * name,void ** buffer,bool alloc)401 int 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_any_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;
430 	if (is_v2header((size_t) ret, cap)) {
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;
438 	} else if (!is_v3header((size_t) ret, cap)) {
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);
477 		} else {
478 			size = -ENOMEM;
479 		}
480 	}
481 	kfree(tmpbuf);
482 	return size;
483 }
484 
rootid_from_xattr(const void * value,size_t size,struct user_namespace * task_ns)485 static 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 
validheader(size_t size,const struct vfs_cap_data * cap)497 static bool validheader(size_t size, const struct vfs_cap_data *cap)
498 {
499 	return is_v2header(size, cap) || is_v3header(size, cap);
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  */
cap_convert_nscap(struct dentry * dentry,void ** ivalue,size_t size)508 int 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;
522 	if (!validheader(size, cap))
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 
556 /*
557  * Calculate the new process capability sets from the capability sets attached
558  * to a file.
559  */
bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data * caps,struct linux_binprm * bprm,bool * effective,bool * has_cap)560 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
561 					  struct linux_binprm *bprm,
562 					  bool *effective,
563 					  bool *has_cap)
564 {
565 	struct cred *new = bprm->cred;
566 	unsigned i;
567 	int ret = 0;
568 
569 	if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
570 		*effective = true;
571 
572 	if (caps->magic_etc & VFS_CAP_REVISION_MASK)
573 		*has_cap = true;
574 
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)
581 		 * The addition of pA' is handled later.
582 		 */
583 		new->cap_permitted.cap[i] =
584 			(new->cap_bset.cap[i] & permitted) |
585 			(new->cap_inheritable.cap[i] & inheritable);
586 
587 		if (permitted & ~new->cap_permitted.cap[i])
588 			/* insufficient to execute correctly */
589 			ret = -EPERM;
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 	 */
597 	return *effective ? ret : 0;
598 }
599 
600 /*
601  * Extract the on-exec-apply capability sets for an executable file.
602  */
get_vfs_caps_from_disk(const struct dentry * dentry,struct cpu_vfs_cap_data * cpu_caps)603 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
604 {
605 	struct inode *inode = d_backing_inode(dentry);
606 	__u32 magic_etc;
607 	unsigned tocopy, i;
608 	int size;
609 	struct vfs_ns_cap_data data, *nscaps = &data;
610 	struct vfs_cap_data *caps = (struct vfs_cap_data *) &data;
611 	kuid_t rootkuid;
612 	struct user_namespace *fs_ns;
613 
614 	memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
615 
616 	if (!inode)
617 		return -ENODATA;
618 
619 	fs_ns = inode->i_sb->s_user_ns;
620 	size = __vfs_getxattr((struct dentry *)dentry, inode,
621 			      XATTR_NAME_CAPS, &data, XATTR_CAPS_SZ);
622 	if (size == -ENODATA || size == -EOPNOTSUPP)
623 		/* no data, that's ok */
624 		return -ENODATA;
625 
626 	if (size < 0)
627 		return size;
628 
629 	if (size < sizeof(magic_etc))
630 		return -EINVAL;
631 
632 	cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps->magic_etc);
633 
634 	rootkuid = make_kuid(fs_ns, 0);
635 	switch (magic_etc & VFS_CAP_REVISION_MASK) {
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;
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 
653 	default:
654 		return -EINVAL;
655 	}
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;
661 
662 	CAP_FOR_EACH_U32(i) {
663 		if (i >= tocopy)
664 			break;
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);
667 	}
668 
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 
672 	return 0;
673 }
674 
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  */
get_file_caps(struct linux_binprm * bprm,bool * effective,bool * has_cap)680 static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
681 {
682 	int rc = 0;
683 	struct cpu_vfs_cap_data vcaps;
684 
685 	cap_clear(bprm->cred->cap_permitted);
686 
687 	if (!file_caps_enabled)
688 		return 0;
689 
690 	if (!mnt_may_suid(bprm->file->f_path.mnt))
691 		return 0;
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 	 */
698 	if (!current_in_userns(bprm->file->f_path.mnt->mnt_sb->s_user_ns))
699 		return 0;
700 
701 	rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
702 	if (rc < 0) {
703 		if (rc == -EINVAL)
704 			printk(KERN_NOTICE "Invalid argument reading file caps for %s\n",
705 					bprm->filename);
706 		else if (rc == -ENODATA)
707 			rc = 0;
708 		goto out;
709 	}
710 
711 	rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
712 	if (rc == -EINVAL)
713 		printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
714 		       __func__, rc, bprm->filename);
715 
716 out:
717 	if (rc)
718 		cap_clear(bprm->cred->cap_permitted);
719 
720 	return rc;
721 }
722 
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.
730  */
cap_bprm_set_creds(struct linux_binprm * bprm)731 int cap_bprm_set_creds(struct linux_binprm *bprm)
732 {
733 	const struct cred *old = current_cred();
734 	struct cred *new = bprm->cred;
735 	bool effective, has_cap = false, is_setid;
736 	int ret;
737 	kuid_t root_uid;
738 
739 	if (WARN_ON(!cap_ambient_invariant_ok(old)))
740 		return -EPERM;
741 
742 	effective = false;
743 	ret = get_file_caps(bprm, &effective, &has_cap);
744 	if (ret < 0)
745 		return ret;
746 
747 	root_uid = make_kuid(new->user_ns, 0);
748 
749 	if (!issecure(SECURE_NOROOT)) {
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 		 */
755 		if (has_cap && !uid_eq(new->uid, root_uid) && uid_eq(new->euid, root_uid)) {
756 			warn_setuid_and_fcaps_mixed(bprm->filename);
757 			goto skip;
758 		}
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 		 *
764 		 * If only the real uid is 0, we do not set the effective bit.
765 		 */
766 		if (uid_eq(new->euid, root_uid) || uid_eq(new->uid, root_uid)) {
767 			/* pP' = (cap_bset & ~0) | (pI & ~0) */
768 			new->cap_permitted = cap_combine(old->cap_bset,
769 							 old->cap_inheritable);
770 		}
771 		if (uid_eq(new->euid, root_uid))
772 			effective = true;
773 	}
774 skip:
775 
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 
781 	/* Don't let someone trace a set[ug]id/setpcap binary with the revised
782 	 * credentials unless they have the appropriate permit.
783 	 *
784 	 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
785 	 */
786 	is_setid = !uid_eq(new->euid, old->uid) || !gid_eq(new->egid, old->gid);
787 
788 	if ((is_setid ||
789 	     !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
790 	    ((bprm->unsafe & ~LSM_UNSAFE_PTRACE) ||
791 	     !ptracer_capable(current, new->user_ns))) {
792 		/* downgrade; they get no more than they had, and maybe less */
793 		if (!ns_capable(new->user_ns, CAP_SETUID) ||
794 		    (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
795 			new->euid = new->uid;
796 			new->egid = new->gid;
797 		}
798 		new->cap_permitted = cap_intersect(new->cap_permitted,
799 						   old->cap_permitted);
800 	}
801 
802 	new->suid = new->fsuid = new->euid;
803 	new->sgid = new->fsgid = new->egid;
804 
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 	 */
819 	if (effective)
820 		new->cap_effective = new->cap_permitted;
821 	else
822 		new->cap_effective = new->cap_ambient;
823 
824 	if (WARN_ON(!cap_ambient_invariant_ok(new)))
825 		return -EPERM;
826 
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 	 */
839 	if (!cap_issubset(new->cap_effective, new->cap_ambient)) {
840 		if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
841 		    !uid_eq(new->euid, root_uid) || !uid_eq(new->uid, root_uid) ||
842 		    issecure(SECURE_NOROOT)) {
843 			ret = audit_log_bprm_fcaps(bprm, new, old);
844 			if (ret < 0)
845 				return ret;
846 		}
847 	}
848 
849 	new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
850 
851 	if (WARN_ON(!cap_ambient_invariant_ok(new)))
852 		return -EPERM;
853 
854 	/* Check for privilege-elevated exec. */
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;
862 	}
863 
864 	return 0;
865 }
866 
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  */
cap_inode_setxattr(struct dentry * dentry,const char * name,const void * value,size_t size,int flags)881 int cap_inode_setxattr(struct dentry *dentry, const char *name,
882 		       const void *value, size_t size, int flags)
883 {
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)
894 		return 0;
895 
896 	if (!capable(CAP_SYS_ADMIN))
897 		return -EPERM;
898 	return 0;
899 }
900 
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  */
cap_inode_removexattr(struct dentry * dentry,const char * name)912 int cap_inode_removexattr(struct dentry *dentry, const char *name)
913 {
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))
925 			return -EPERM;
926 		return 0;
927 	}
928 
929 	if (!capable(CAP_SYS_ADMIN))
930 		return -EPERM;
931 	return 0;
932 }
933 
934 /*
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  *
948  *  fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
949  *  never happen.
950  *
951  *  -astor
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  */
cap_emulate_setxuid(struct cred * new,const struct cred * old)963 static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
964 {
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) &&
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);
984 	}
985 	if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
986 		cap_clear(new->cap_effective);
987 	if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
988 		new->cap_effective = new->cap_permitted;
989 }
990 
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  */
cap_task_fix_setuid(struct cred * new,const struct cred * old,int flags)1000 int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
1001 {
1002 	switch (flags) {
1003 	case LSM_SETID_RE:
1004 	case LSM_SETID_ID:
1005 	case LSM_SETID_RES:
1006 		/* juggle the capabilities to follow [RES]UID changes unless
1007 		 * otherwise suppressed */
1008 		if (!issecure(SECURE_NO_SETUID_FIXUP))
1009 			cap_emulate_setxuid(new, old);
1010 		break;
1011 
1012 	case LSM_SETID_FS:
1013 		/* juggle the capabilties to follow FSUID changes, unless
1014 		 * otherwise suppressed
1015 		 *
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)) {
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))
1022 				new->cap_effective =
1023 					cap_drop_fs_set(new->cap_effective);
1024 
1025 			if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
1026 				new->cap_effective =
1027 					cap_raise_fs_set(new->cap_effective,
1028 							 new->cap_permitted);
1029 		}
1030 		break;
1031 
1032 	default:
1033 		return -EINVAL;
1034 	}
1035 
1036 	return 0;
1037 }
1038 
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  */
cap_safe_nice(struct task_struct * p)1049 static int cap_safe_nice(struct task_struct *p)
1050 {
1051 	int is_subset, ret = 0;
1052 
1053 	rcu_read_lock();
1054 	is_subset = cap_issubset(__task_cred(p)->cap_permitted,
1055 				 current_cred()->cap_permitted);
1056 	if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
1057 		ret = -EPERM;
1058 	rcu_read_unlock();
1059 
1060 	return ret;
1061 }
1062 
1063 /**
1064  * cap_task_setscheduler - Detemine if scheduler policy change is permitted
1065  * @p: The task to affect
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  */
cap_task_setscheduler(struct task_struct * p)1070 int cap_task_setscheduler(struct task_struct *p)
1071 {
1072 	return cap_safe_nice(p);
1073 }
1074 
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  */
cap_task_setioprio(struct task_struct * p,int ioprio)1083 int cap_task_setioprio(struct task_struct *p, int ioprio)
1084 {
1085 	return cap_safe_nice(p);
1086 }
1087 
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  */
cap_task_setnice(struct task_struct * p,int nice)1096 int cap_task_setnice(struct task_struct *p, int nice)
1097 {
1098 	return cap_safe_nice(p);
1099 }
1100 
1101 /*
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.
1104  */
cap_prctl_drop(unsigned long cap)1105 static int cap_prctl_drop(unsigned long cap)
1106 {
1107 	struct cred *new;
1108 
1109 	if (!ns_capable(current_user_ns(), CAP_SETPCAP))
1110 		return -EPERM;
1111 	if (!cap_valid(cap))
1112 		return -EINVAL;
1113 
1114 	new = prepare_creds();
1115 	if (!new)
1116 		return -ENOMEM;
1117 	cap_lower(new->cap_bset, cap);
1118 	return commit_creds(new);
1119 }
1120 
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  */
cap_task_prctl(int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)1133 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
1134 		   unsigned long arg4, unsigned long arg5)
1135 {
1136 	const struct cred *old = current_cred();
1137 	struct cred *new;
1138 
1139 	switch (option) {
1140 	case PR_CAPBSET_READ:
1141 		if (!cap_valid(arg2))
1142 			return -EINVAL;
1143 		return !!cap_raised(old->cap_bset, arg2);
1144 
1145 	case PR_CAPBSET_DROP:
1146 		return cap_prctl_drop(arg2);
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:
1168 		if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
1169 		     & (old->securebits ^ arg2))			/*[1]*/
1170 		    || ((old->securebits & SECURE_ALL_LOCKS & ~arg2))	/*[2]*/
1171 		    || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS))	/*[3]*/
1172 		    || (cap_capable(current_cred(),
1173 				    current_cred()->user_ns, CAP_SETPCAP,
1174 				    SECURITY_CAP_AUDIT) != 0)		/*[4]*/
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 			 */
1182 		    )
1183 			/* cannot change a locked bit */
1184 			return -EPERM;
1185 
1186 		new = prepare_creds();
1187 		if (!new)
1188 			return -ENOMEM;
1189 		new->securebits = arg2;
1190 		return commit_creds(new);
1191 
1192 	case PR_GET_SECUREBITS:
1193 		return old->securebits;
1194 
1195 	case PR_GET_KEEPCAPS:
1196 		return !!issecure(SECURE_KEEP_CAPS);
1197 
1198 	case PR_SET_KEEPCAPS:
1199 		if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
1200 			return -EINVAL;
1201 		if (issecure(SECURE_KEEP_CAPS_LOCKED))
1202 			return -EPERM;
1203 
1204 		new = prepare_creds();
1205 		if (!new)
1206 			return -ENOMEM;
1207 		if (arg2)
1208 			new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
1209 		else
1210 			new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
1211 		return commit_creds(new);
1212 
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,
1237 					 arg3) ||
1238 			     issecure(SECURE_NO_CAP_AMBIENT_RAISE)))
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 
1251 	default:
1252 		/* No functionality available - continue with default */
1253 		return -ENOSYS;
1254 	}
1255 }
1256 
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
1263  * task is permitted, returning 1 if permission is granted, 0 if not.
1264  */
cap_vm_enough_memory(struct mm_struct * mm,long pages)1265 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
1266 {
1267 	int cap_sys_admin = 0;
1268 
1269 	if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
1270 			SECURITY_CAP_NOAUDIT) == 0)
1271 		cap_sys_admin = 1;
1272 	return cap_sys_admin;
1273 }
1274 
1275 /*
1276  * cap_mmap_addr - check if able to map given addr
1277  * @addr: address attempting to be mapped
1278  *
1279  * If the process is attempting to map memory below dac_mmap_min_addr they need
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  */
cap_mmap_addr(unsigned long addr)1284 int cap_mmap_addr(unsigned long addr)
1285 {
1286 	int ret = 0;
1287 
1288 	if (addr < dac_mmap_min_addr) {
1289 		ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
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 }
1297 
cap_mmap_file(struct file * file,unsigned long reqprot,unsigned long prot,unsigned long flags)1298 int cap_mmap_file(struct file *file, unsigned long reqprot,
1299 		  unsigned long prot, unsigned long flags)
1300 {
1301 	return 0;
1302 }
1303 
1304 #ifdef CONFIG_SECURITY
1305 
1306 struct security_hook_list capability_hooks[] __lsm_ro_after_init = {
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),
1314 	LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
1315 	LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
1316 	LSM_HOOK_INIT(inode_getsecurity, cap_inode_getsecurity),
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 
capability_add_hooks(void)1327 void __init capability_add_hooks(void)
1328 {
1329 	security_add_hooks(capability_hooks, ARRAY_SIZE(capability_hooks),
1330 				"capability");
1331 }
1332 
1333 #endif /* CONFIG_SECURITY */
1334