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