modified PMU_LP8725 delete set buck1(core) voltage
[firefly-linux-kernel-4.4.55.git] / security / commoncap.c
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
35 /*
36  * If a non-root user executes a setuid-root binary in
37  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
38  * However if fE is also set, then the intent is for only
39  * the file capabilities to be applied, and the setuid-root
40  * bit is left on either to change the uid (plausible) or
41  * to get full privilege on a kernel without file capabilities
42  * support.  So in that case we do not raise capabilities.
43  *
44  * Warn if that happens, once per boot.
45  */
46 static void warn_setuid_and_fcaps_mixed(char *fname)
47 {
48         static int warned;
49         if (!warned) {
50                 printk(KERN_INFO "warning: `%s' has both setuid-root and"
51                         " effective capabilities. Therefore not raising all"
52                         " capabilities.\n", fname);
53                 warned = 1;
54         }
55 }
56
57 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
58 {
59         NETLINK_CB(skb).eff_cap = current_cap();
60         return 0;
61 }
62
63 int cap_netlink_recv(struct sk_buff *skb, int cap)
64 {
65         if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
66                 return -EPERM;
67         return 0;
68 }
69 EXPORT_SYMBOL(cap_netlink_recv);
70
71 /**
72  * cap_capable - Determine whether a task has a particular effective capability
73  * @tsk: The task to query
74  * @cred: The credentials to use
75  * @cap: The capability to check for
76  * @audit: Whether to write an audit message or not
77  *
78  * Determine whether the nominated task has the specified capability amongst
79  * its effective set, returning 0 if it does, -ve if it does not.
80  *
81  * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
82  * and has_capability() functions.  That is, it has the reverse semantics:
83  * cap_has_capability() returns 0 when a task has a capability, but the
84  * kernel's capable() and has_capability() returns 1 for this case.
85  */
86 int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
87                 int audit)
88 {
89 #ifdef CONFIG_ANDROID_PARANOID_NETWORK
90         if (cap == CAP_NET_RAW && in_egroup_p(AID_NET_RAW))
91                 return 0;
92         if (cap == CAP_NET_ADMIN && in_egroup_p(AID_NET_ADMIN))
93                 return 0;
94 #endif
95         return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
96 }
97
98 /**
99  * cap_settime - Determine whether the current process may set the system clock
100  * @ts: The time to set
101  * @tz: The timezone to set
102  *
103  * Determine whether the current process may set the system clock and timezone
104  * information, returning 0 if permission granted, -ve if denied.
105  */
106 int cap_settime(struct timespec *ts, struct timezone *tz)
107 {
108         if (!capable(CAP_SYS_TIME))
109                 return -EPERM;
110         return 0;
111 }
112
113 /**
114  * cap_ptrace_access_check - Determine whether the current process may access
115  *                         another
116  * @child: The process to be accessed
117  * @mode: The mode of attachment.
118  *
119  * Determine whether a process may access another, returning 0 if permission
120  * granted, -ve if denied.
121  */
122 int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
123 {
124         int ret = 0;
125
126         rcu_read_lock();
127         if (!cap_issubset(__task_cred(child)->cap_permitted,
128                           current_cred()->cap_permitted) &&
129             !capable(CAP_SYS_PTRACE))
130                 ret = -EPERM;
131         rcu_read_unlock();
132         return ret;
133 }
134
135 /**
136  * cap_ptrace_traceme - Determine whether another process may trace the current
137  * @parent: The task proposed to be the tracer
138  *
139  * Determine whether the nominated task is permitted to trace the current
140  * process, returning 0 if permission is granted, -ve if denied.
141  */
142 int cap_ptrace_traceme(struct task_struct *parent)
143 {
144         int ret = 0;
145
146         rcu_read_lock();
147         if (!cap_issubset(current_cred()->cap_permitted,
148                           __task_cred(parent)->cap_permitted) &&
149             !has_capability(parent, CAP_SYS_PTRACE))
150                 ret = -EPERM;
151         rcu_read_unlock();
152         return ret;
153 }
154
155 /**
156  * cap_capget - Retrieve a task's capability sets
157  * @target: The task from which to retrieve the capability sets
158  * @effective: The place to record the effective set
159  * @inheritable: The place to record the inheritable set
160  * @permitted: The place to record the permitted set
161  *
162  * This function retrieves the capabilities of the nominated task and returns
163  * them to the caller.
164  */
165 int cap_capget(struct task_struct *target, kernel_cap_t *effective,
166                kernel_cap_t *inheritable, kernel_cap_t *permitted)
167 {
168         const struct cred *cred;
169
170         /* Derived from kernel/capability.c:sys_capget. */
171         rcu_read_lock();
172         cred = __task_cred(target);
173         *effective   = cred->cap_effective;
174         *inheritable = cred->cap_inheritable;
175         *permitted   = cred->cap_permitted;
176         rcu_read_unlock();
177         return 0;
178 }
179
180 /*
181  * Determine whether the inheritable capabilities are limited to the old
182  * permitted set.  Returns 1 if they are limited, 0 if they are not.
183  */
184 static inline int cap_inh_is_capped(void)
185 {
186 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
187
188         /* they are so limited unless the current task has the CAP_SETPCAP
189          * capability
190          */
191         if (cap_capable(current, current_cred(), CAP_SETPCAP,
192                         SECURITY_CAP_AUDIT) == 0)
193                 return 0;
194 #endif
195         return 1;
196 }
197
198 /**
199  * cap_capset - Validate and apply proposed changes to current's capabilities
200  * @new: The proposed new credentials; alterations should be made here
201  * @old: The current task's current credentials
202  * @effective: A pointer to the proposed new effective capabilities set
203  * @inheritable: A pointer to the proposed new inheritable capabilities set
204  * @permitted: A pointer to the proposed new permitted capabilities set
205  *
206  * This function validates and applies a proposed mass change to the current
207  * process's capability sets.  The changes are made to the proposed new
208  * credentials, and assuming no error, will be committed by the caller of LSM.
209  */
210 int cap_capset(struct cred *new,
211                const struct cred *old,
212                const kernel_cap_t *effective,
213                const kernel_cap_t *inheritable,
214                const kernel_cap_t *permitted)
215 {
216         if (cap_inh_is_capped() &&
217             !cap_issubset(*inheritable,
218                           cap_combine(old->cap_inheritable,
219                                       old->cap_permitted)))
220                 /* incapable of using this inheritable set */
221                 return -EPERM;
222
223         if (!cap_issubset(*inheritable,
224                           cap_combine(old->cap_inheritable,
225                                       old->cap_bset)))
226                 /* no new pI capabilities outside bounding set */
227                 return -EPERM;
228
229         /* verify restrictions on target's new Permitted set */
230         if (!cap_issubset(*permitted, old->cap_permitted))
231                 return -EPERM;
232
233         /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
234         if (!cap_issubset(*effective, *permitted))
235                 return -EPERM;
236
237         new->cap_effective   = *effective;
238         new->cap_inheritable = *inheritable;
239         new->cap_permitted   = *permitted;
240         return 0;
241 }
242
243 /*
244  * Clear proposed capability sets for execve().
245  */
246 static inline void bprm_clear_caps(struct linux_binprm *bprm)
247 {
248         cap_clear(bprm->cred->cap_permitted);
249         bprm->cap_effective = false;
250 }
251
252 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
253
254 /**
255  * cap_inode_need_killpriv - Determine if inode change affects privileges
256  * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
257  *
258  * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
259  * affects the security markings on that inode, and if it is, should
260  * inode_killpriv() be invoked or the change rejected?
261  *
262  * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
263  * -ve to deny the change.
264  */
265 int cap_inode_need_killpriv(struct dentry *dentry)
266 {
267         struct inode *inode = dentry->d_inode;
268         int error;
269
270         if (!inode->i_op->getxattr)
271                return 0;
272
273         error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
274         if (error <= 0)
275                 return 0;
276         return 1;
277 }
278
279 /**
280  * cap_inode_killpriv - Erase the security markings on an inode
281  * @dentry: The inode/dentry to alter
282  *
283  * Erase the privilege-enhancing security markings on an inode.
284  *
285  * Returns 0 if successful, -ve on error.
286  */
287 int cap_inode_killpriv(struct dentry *dentry)
288 {
289         struct inode *inode = dentry->d_inode;
290
291         if (!inode->i_op->removexattr)
292                return 0;
293
294         return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
295 }
296
297 /*
298  * Calculate the new process capability sets from the capability sets attached
299  * to a file.
300  */
301 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
302                                           struct linux_binprm *bprm,
303                                           bool *effective)
304 {
305         struct cred *new = bprm->cred;
306         unsigned i;
307         int ret = 0;
308
309         if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
310                 *effective = true;
311
312         CAP_FOR_EACH_U32(i) {
313                 __u32 permitted = caps->permitted.cap[i];
314                 __u32 inheritable = caps->inheritable.cap[i];
315
316                 /*
317                  * pP' = (X & fP) | (pI & fI)
318                  */
319                 new->cap_permitted.cap[i] =
320                         (new->cap_bset.cap[i] & permitted) |
321                         (new->cap_inheritable.cap[i] & inheritable);
322
323                 if (permitted & ~new->cap_permitted.cap[i])
324                         /* insufficient to execute correctly */
325                         ret = -EPERM;
326         }
327
328         /*
329          * For legacy apps, with no internal support for recognizing they
330          * do not have enough capabilities, we return an error if they are
331          * missing some "forced" (aka file-permitted) capabilities.
332          */
333         return *effective ? ret : 0;
334 }
335
336 /*
337  * Extract the on-exec-apply capability sets for an executable file.
338  */
339 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
340 {
341         struct inode *inode = dentry->d_inode;
342         __u32 magic_etc;
343         unsigned tocopy, i;
344         int size;
345         struct vfs_cap_data caps;
346
347         memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
348
349         if (!inode || !inode->i_op->getxattr)
350                 return -ENODATA;
351
352         size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
353                                    XATTR_CAPS_SZ);
354         if (size == -ENODATA || size == -EOPNOTSUPP)
355                 /* no data, that's ok */
356                 return -ENODATA;
357         if (size < 0)
358                 return size;
359
360         if (size < sizeof(magic_etc))
361                 return -EINVAL;
362
363         cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
364
365         switch (magic_etc & VFS_CAP_REVISION_MASK) {
366         case VFS_CAP_REVISION_1:
367                 if (size != XATTR_CAPS_SZ_1)
368                         return -EINVAL;
369                 tocopy = VFS_CAP_U32_1;
370                 break;
371         case VFS_CAP_REVISION_2:
372                 if (size != XATTR_CAPS_SZ_2)
373                         return -EINVAL;
374                 tocopy = VFS_CAP_U32_2;
375                 break;
376         default:
377                 return -EINVAL;
378         }
379
380         CAP_FOR_EACH_U32(i) {
381                 if (i >= tocopy)
382                         break;
383                 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
384                 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
385         }
386
387         return 0;
388 }
389
390 /*
391  * Attempt to get the on-exec apply capability sets for an executable file from
392  * its xattrs and, if present, apply them to the proposed credentials being
393  * constructed by execve().
394  */
395 static int get_file_caps(struct linux_binprm *bprm, bool *effective)
396 {
397         struct dentry *dentry;
398         int rc = 0;
399         struct cpu_vfs_cap_data vcaps;
400
401         bprm_clear_caps(bprm);
402
403         if (!file_caps_enabled)
404                 return 0;
405
406         if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
407                 return 0;
408
409         dentry = dget(bprm->file->f_dentry);
410
411         rc = get_vfs_caps_from_disk(dentry, &vcaps);
412         if (rc < 0) {
413                 if (rc == -EINVAL)
414                         printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
415                                 __func__, rc, bprm->filename);
416                 else if (rc == -ENODATA)
417                         rc = 0;
418                 goto out;
419         }
420
421         rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
422         if (rc == -EINVAL)
423                 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
424                        __func__, rc, bprm->filename);
425
426 out:
427         dput(dentry);
428         if (rc)
429                 bprm_clear_caps(bprm);
430
431         return rc;
432 }
433
434 #else
435 int cap_inode_need_killpriv(struct dentry *dentry)
436 {
437         return 0;
438 }
439
440 int cap_inode_killpriv(struct dentry *dentry)
441 {
442         return 0;
443 }
444
445 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
446 {
447         memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
448         return -ENODATA;
449 }
450
451 static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
452 {
453         bprm_clear_caps(bprm);
454         return 0;
455 }
456 #endif
457
458 /*
459  * Determine whether a exec'ing process's new permitted capabilities should be
460  * limited to just what it already has.
461  *
462  * This prevents processes that are being ptraced from gaining access to
463  * CAP_SETPCAP, unless the process they're tracing already has it, and the
464  * binary they're executing has filecaps that elevate it.
465  *
466  *  Returns 1 if they should be limited, 0 if they are not.
467  */
468 static inline int cap_limit_ptraced_target(void)
469 {
470 #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
471         if (capable(CAP_SETPCAP))
472                 return 0;
473 #endif
474         return 1;
475 }
476
477 /**
478  * cap_bprm_set_creds - Set up the proposed credentials for execve().
479  * @bprm: The execution parameters, including the proposed creds
480  *
481  * Set up the proposed credentials for a new execution context being
482  * constructed by execve().  The proposed creds in @bprm->cred is altered,
483  * which won't take effect immediately.  Returns 0 if successful, -ve on error.
484  */
485 int cap_bprm_set_creds(struct linux_binprm *bprm)
486 {
487         const struct cred *old = current_cred();
488         struct cred *new = bprm->cred;
489         bool effective;
490         int ret;
491
492         effective = false;
493         ret = get_file_caps(bprm, &effective);
494         if (ret < 0)
495                 return ret;
496
497         if (!issecure(SECURE_NOROOT)) {
498                 /*
499                  * If the legacy file capability is set, then don't set privs
500                  * for a setuid root binary run by a non-root user.  Do set it
501                  * for a root user just to cause least surprise to an admin.
502                  */
503                 if (effective && new->uid != 0 && new->euid == 0) {
504                         warn_setuid_and_fcaps_mixed(bprm->filename);
505                         goto skip;
506                 }
507                 /*
508                  * To support inheritance of root-permissions and suid-root
509                  * executables under compatibility mode, we override the
510                  * capability sets for the file.
511                  *
512                  * If only the real uid is 0, we do not set the effective bit.
513                  */
514                 if (new->euid == 0 || new->uid == 0) {
515                         /* pP' = (cap_bset & ~0) | (pI & ~0) */
516                         new->cap_permitted = cap_combine(old->cap_bset,
517                                                          old->cap_inheritable);
518                 }
519                 if (new->euid == 0)
520                         effective = true;
521         }
522 skip:
523
524         /* Don't let someone trace a set[ug]id/setpcap binary with the revised
525          * credentials unless they have the appropriate permit
526          */
527         if ((new->euid != old->uid ||
528              new->egid != old->gid ||
529              !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
530             bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
531                 /* downgrade; they get no more than they had, and maybe less */
532                 if (!capable(CAP_SETUID)) {
533                         new->euid = new->uid;
534                         new->egid = new->gid;
535                 }
536                 if (cap_limit_ptraced_target())
537                         new->cap_permitted = cap_intersect(new->cap_permitted,
538                                                            old->cap_permitted);
539         }
540
541         new->suid = new->fsuid = new->euid;
542         new->sgid = new->fsgid = new->egid;
543
544         /* For init, we want to retain the capabilities set in the initial
545          * task.  Thus we skip the usual capability rules
546          */
547         if (!is_global_init(current)) {
548                 if (effective)
549                         new->cap_effective = new->cap_permitted;
550                 else
551                         cap_clear(new->cap_effective);
552         }
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 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
753 /*
754  * Rationale: code calling task_setscheduler, task_setioprio, and
755  * task_setnice, assumes that
756  *   . if capable(cap_sys_nice), then those actions should be allowed
757  *   . if not capable(cap_sys_nice), but acting on your own processes,
758  *      then those actions should be allowed
759  * This is insufficient now since you can call code without suid, but
760  * yet with increased caps.
761  * So we check for increased caps on the target process.
762  */
763 static int cap_safe_nice(struct task_struct *p)
764 {
765         int is_subset;
766
767         rcu_read_lock();
768         is_subset = cap_issubset(__task_cred(p)->cap_permitted,
769                                  current_cred()->cap_permitted);
770         rcu_read_unlock();
771
772         if (!is_subset && !capable(CAP_SYS_NICE))
773                 return -EPERM;
774         return 0;
775 }
776
777 /**
778  * cap_task_setscheduler - Detemine if scheduler policy change is permitted
779  * @p: The task to affect
780  * @policy: The policy to effect
781  * @lp: The parameters to the scheduling policy
782  *
783  * Detemine if the requested scheduler policy change is permitted for the
784  * specified task, returning 0 if permission is granted, -ve if denied.
785  */
786 int cap_task_setscheduler(struct task_struct *p, int policy,
787                            struct sched_param *lp)
788 {
789         return cap_safe_nice(p);
790 }
791
792 /**
793  * cap_task_ioprio - Detemine if I/O priority change is permitted
794  * @p: The task to affect
795  * @ioprio: The I/O priority to set
796  *
797  * Detemine if the requested I/O priority change is permitted for the specified
798  * task, returning 0 if permission is granted, -ve if denied.
799  */
800 int cap_task_setioprio(struct task_struct *p, int ioprio)
801 {
802         return cap_safe_nice(p);
803 }
804
805 /**
806  * cap_task_ioprio - Detemine if task priority change is permitted
807  * @p: The task to affect
808  * @nice: The nice value to set
809  *
810  * Detemine if the requested task priority change is permitted for the
811  * specified task, returning 0 if permission is granted, -ve if denied.
812  */
813 int cap_task_setnice(struct task_struct *p, int nice)
814 {
815         return cap_safe_nice(p);
816 }
817
818 /*
819  * Implement PR_CAPBSET_DROP.  Attempt to remove the specified capability from
820  * the current task's bounding set.  Returns 0 on success, -ve on error.
821  */
822 static long cap_prctl_drop(struct cred *new, unsigned long cap)
823 {
824         if (!capable(CAP_SETPCAP))
825                 return -EPERM;
826         if (!cap_valid(cap))
827                 return -EINVAL;
828
829         cap_lower(new->cap_bset, cap);
830         return 0;
831 }
832
833 #else
834 int cap_task_setscheduler (struct task_struct *p, int policy,
835                            struct sched_param *lp)
836 {
837         return 0;
838 }
839 int cap_task_setioprio (struct task_struct *p, int ioprio)
840 {
841         return 0;
842 }
843 int cap_task_setnice (struct task_struct *p, int nice)
844 {
845         return 0;
846 }
847 #endif
848
849 /**
850  * cap_task_prctl - Implement process control functions for this security module
851  * @option: The process control function requested
852  * @arg2, @arg3, @arg4, @arg5: The argument data for this function
853  *
854  * Allow process control functions (sys_prctl()) to alter capabilities; may
855  * also deny access to other functions not otherwise implemented here.
856  *
857  * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
858  * here, other -ve on error.  If -ENOSYS is returned, sys_prctl() and other LSM
859  * modules will consider performing the function.
860  */
861 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
862                    unsigned long arg4, unsigned long arg5)
863 {
864         struct cred *new;
865         long error = 0;
866
867         new = prepare_creds();
868         if (!new)
869                 return -ENOMEM;
870
871         switch (option) {
872         case PR_CAPBSET_READ:
873                 error = -EINVAL;
874                 if (!cap_valid(arg2))
875                         goto error;
876                 error = !!cap_raised(new->cap_bset, arg2);
877                 goto no_change;
878
879 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
880         case PR_CAPBSET_DROP:
881                 error = cap_prctl_drop(new, arg2);
882                 if (error < 0)
883                         goto error;
884                 goto changed;
885
886         /*
887          * The next four prctl's remain to assist with transitioning a
888          * system from legacy UID=0 based privilege (when filesystem
889          * capabilities are not in use) to a system using filesystem
890          * capabilities only - as the POSIX.1e draft intended.
891          *
892          * Note:
893          *
894          *  PR_SET_SECUREBITS =
895          *      issecure_mask(SECURE_KEEP_CAPS_LOCKED)
896          *    | issecure_mask(SECURE_NOROOT)
897          *    | issecure_mask(SECURE_NOROOT_LOCKED)
898          *    | issecure_mask(SECURE_NO_SETUID_FIXUP)
899          *    | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
900          *
901          * will ensure that the current process and all of its
902          * children will be locked into a pure
903          * capability-based-privilege environment.
904          */
905         case PR_SET_SECUREBITS:
906                 error = -EPERM;
907                 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
908                      & (new->securebits ^ arg2))                        /*[1]*/
909                     || ((new->securebits & SECURE_ALL_LOCKS & ~arg2))   /*[2]*/
910                     || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS))   /*[3]*/
911                     || (cap_capable(current, current_cred(), CAP_SETPCAP,
912                                     SECURITY_CAP_AUDIT) != 0)           /*[4]*/
913                         /*
914                          * [1] no changing of bits that are locked
915                          * [2] no unlocking of locks
916                          * [3] no setting of unsupported bits
917                          * [4] doing anything requires privilege (go read about
918                          *     the "sendmail capabilities bug")
919                          */
920                     )
921                         /* cannot change a locked bit */
922                         goto error;
923                 new->securebits = arg2;
924                 goto changed;
925
926         case PR_GET_SECUREBITS:
927                 error = new->securebits;
928                 goto no_change;
929
930 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
931
932         case PR_GET_KEEPCAPS:
933                 if (issecure(SECURE_KEEP_CAPS))
934                         error = 1;
935                 goto no_change;
936
937         case PR_SET_KEEPCAPS:
938                 error = -EINVAL;
939                 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
940                         goto error;
941                 error = -EPERM;
942                 if (issecure(SECURE_KEEP_CAPS_LOCKED))
943                         goto error;
944                 if (arg2)
945                         new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
946                 else
947                         new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
948                 goto changed;
949
950         default:
951                 /* No functionality available - continue with default */
952                 error = -ENOSYS;
953                 goto error;
954         }
955
956         /* Functionality provided */
957 changed:
958         return commit_creds(new);
959
960 no_change:
961 error:
962         abort_creds(new);
963         return error;
964 }
965
966 /**
967  * cap_syslog - Determine whether syslog function is permitted
968  * @type: Function requested
969  *
970  * Determine whether the current process is permitted to use a particular
971  * syslog function, returning 0 if permission is granted, -ve if not.
972  */
973 int cap_syslog(int type)
974 {
975         if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
976                 return -EPERM;
977         return 0;
978 }
979
980 /**
981  * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
982  * @mm: The VM space in which the new mapping is to be made
983  * @pages: The size of the mapping
984  *
985  * Determine whether the allocation of a new virtual mapping by the current
986  * task is permitted, returning 0 if permission is granted, -ve if not.
987  */
988 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
989 {
990         int cap_sys_admin = 0;
991
992         if (cap_capable(current, current_cred(), CAP_SYS_ADMIN,
993                         SECURITY_CAP_NOAUDIT) == 0)
994                 cap_sys_admin = 1;
995         return __vm_enough_memory(mm, pages, cap_sys_admin);
996 }
997
998 /*
999  * cap_file_mmap - check if able to map given addr
1000  * @file: unused
1001  * @reqprot: unused
1002  * @prot: unused
1003  * @flags: unused
1004  * @addr: address attempting to be mapped
1005  * @addr_only: unused
1006  *
1007  * If the process is attempting to map memory below mmap_min_addr they need
1008  * CAP_SYS_RAWIO.  The other parameters to this function are unused by the
1009  * capability security module.  Returns 0 if this mapping should be allowed
1010  * -EPERM if not.
1011  */
1012 int cap_file_mmap(struct file *file, unsigned long reqprot,
1013                   unsigned long prot, unsigned long flags,
1014                   unsigned long addr, unsigned long addr_only)
1015 {
1016         int ret = 0;
1017
1018         if (addr < dac_mmap_min_addr) {
1019                 ret = cap_capable(current, current_cred(), CAP_SYS_RAWIO,
1020                                   SECURITY_CAP_AUDIT);
1021                 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
1022                 if (ret == 0)
1023                         current->flags |= PF_SUPERPRIV;
1024         }
1025         return ret;
1026 }