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