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