initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / security / commoncap.c
blobee9d623608a7ed80c2ff9ac6622bf00bfdf80ed9
1 /* Common capabilities, needed by capability.o and root_plug.o
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.
8 */
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/personality.h>
33 * If a non-root user executes a setuid-root binary in
34 * !secure(SECURE_NOROOT) mode, then we raise capabilities.
35 * However if fE is also set, then the intent is for only
36 * the file capabilities to be applied, and the setuid-root
37 * bit is left on either to change the uid (plausible) or
38 * to get full privilege on a kernel without file capabilities
39 * support. So in that case we do not raise capabilities.
41 * Warn if that happens, once per boot.
43 static void warn_setuid_and_fcaps_mixed(char *fname)
45 static int warned;
46 if (!warned) {
47 printk(KERN_INFO "warning: `%s' has both setuid-root and"
48 " effective capabilities. Therefore not raising all"
49 " capabilities.\n", fname);
50 warned = 1;
54 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
56 NETLINK_CB(skb).eff_cap = current_cap();
57 return 0;
60 int cap_netlink_recv(struct sk_buff *skb, int cap)
62 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
63 return -EPERM;
64 return 0;
66 EXPORT_SYMBOL(cap_netlink_recv);
68 /**
69 * cap_capable - Determine whether a task has a particular effective capability
70 * @tsk: The task to query
71 * @cred: The credentials to use
72 * @cap: The capability to check for
73 * @audit: Whether to write an audit message or not
75 * Determine whether the nominated task has the specified capability amongst
76 * its effective set, returning 0 if it does, -ve if it does not.
78 * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
79 * and has_capability() functions. That is, it has the reverse semantics:
80 * cap_has_capability() returns 0 when a task has a capability, but the
81 * kernel's capable() and has_capability() returns 1 for this case.
83 int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
84 int audit)
86 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
89 /**
90 * cap_settime - Determine whether the current process may set the system clock
91 * @ts: The time to set
92 * @tz: The timezone to set
94 * Determine whether the current process may set the system clock and timezone
95 * information, returning 0 if permission granted, -ve if denied.
97 int cap_settime(struct timespec *ts, struct timezone *tz)
99 if (!capable(CAP_SYS_TIME))
100 return -EPERM;
101 return 0;
105 * cap_ptrace_access_check - Determine whether the current process may access
106 * another
107 * @child: The process to be accessed
108 * @mode: The mode of attachment.
110 * Determine whether a process may access another, returning 0 if permission
111 * granted, -ve if denied.
113 int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
115 int ret = 0;
117 rcu_read_lock();
118 if (!cap_issubset(__task_cred(child)->cap_permitted,
119 current_cred()->cap_permitted) &&
120 !capable(CAP_SYS_PTRACE))
121 ret = -EPERM;
122 rcu_read_unlock();
123 return ret;
127 * cap_ptrace_traceme - Determine whether another process may trace the current
128 * @parent: The task proposed to be the tracer
130 * Determine whether the nominated task is permitted to trace the current
131 * process, returning 0 if permission is granted, -ve if denied.
133 int cap_ptrace_traceme(struct task_struct *parent)
135 int ret = 0;
137 rcu_read_lock();
138 if (!cap_issubset(current_cred()->cap_permitted,
139 __task_cred(parent)->cap_permitted) &&
140 !has_capability(parent, CAP_SYS_PTRACE))
141 ret = -EPERM;
142 rcu_read_unlock();
143 return ret;
147 * cap_capget - Retrieve a task's capability sets
148 * @target: The task from which to retrieve the capability sets
149 * @effective: The place to record the effective set
150 * @inheritable: The place to record the inheritable set
151 * @permitted: The place to record the permitted set
153 * This function retrieves the capabilities of the nominated task and returns
154 * them to the caller.
156 int cap_capget(struct task_struct *target, kernel_cap_t *effective,
157 kernel_cap_t *inheritable, kernel_cap_t *permitted)
159 const struct cred *cred;
161 /* Derived from kernel/capability.c:sys_capget. */
162 rcu_read_lock();
163 cred = __task_cred(target);
164 *effective = cred->cap_effective;
165 *inheritable = cred->cap_inheritable;
166 *permitted = cred->cap_permitted;
167 rcu_read_unlock();
168 return 0;
172 * Determine whether the inheritable capabilities are limited to the old
173 * permitted set. Returns 1 if they are limited, 0 if they are not.
175 static inline int cap_inh_is_capped(void)
177 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
179 /* they are so limited unless the current task has the CAP_SETPCAP
180 * capability
182 if (cap_capable(current, current_cred(), CAP_SETPCAP,
183 SECURITY_CAP_AUDIT) == 0)
184 return 0;
185 #endif
186 return 1;
190 * cap_capset - Validate and apply proposed changes to current's capabilities
191 * @new: The proposed new credentials; alterations should be made here
192 * @old: The current task's current credentials
193 * @effective: A pointer to the proposed new effective capabilities set
194 * @inheritable: A pointer to the proposed new inheritable capabilities set
195 * @permitted: A pointer to the proposed new permitted capabilities set
197 * This function validates and applies a proposed mass change to the current
198 * process's capability sets. The changes are made to the proposed new
199 * credentials, and assuming no error, will be committed by the caller of LSM.
201 int cap_capset(struct cred *new,
202 const struct cred *old,
203 const kernel_cap_t *effective,
204 const kernel_cap_t *inheritable,
205 const kernel_cap_t *permitted)
207 if (cap_inh_is_capped() &&
208 !cap_issubset(*inheritable,
209 cap_combine(old->cap_inheritable,
210 old->cap_permitted)))
211 /* incapable of using this inheritable set */
212 return -EPERM;
214 if (!cap_issubset(*inheritable,
215 cap_combine(old->cap_inheritable,
216 old->cap_bset)))
217 /* no new pI capabilities outside bounding set */
218 return -EPERM;
220 /* verify restrictions on target's new Permitted set */
221 if (!cap_issubset(*permitted, old->cap_permitted))
222 return -EPERM;
224 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
225 if (!cap_issubset(*effective, *permitted))
226 return -EPERM;
228 new->cap_effective = *effective;
229 new->cap_inheritable = *inheritable;
230 new->cap_permitted = *permitted;
231 return 0;
235 * Clear proposed capability sets for execve().
237 static inline void bprm_clear_caps(struct linux_binprm *bprm)
239 cap_clear(bprm->cred->cap_permitted);
240 bprm->cap_effective = false;
243 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
246 * cap_inode_need_killpriv - Determine if inode change affects privileges
247 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
249 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
250 * affects the security markings on that inode, and if it is, should
251 * inode_killpriv() be invoked or the change rejected?
253 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
254 * -ve to deny the change.
256 int cap_inode_need_killpriv(struct dentry *dentry)
258 struct inode *inode = dentry->d_inode;
259 int error;
261 if (!inode->i_op->getxattr)
262 return 0;
264 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
265 if (error <= 0)
266 return 0;
267 return 1;
271 * cap_inode_killpriv - Erase the security markings on an inode
272 * @dentry: The inode/dentry to alter
274 * Erase the privilege-enhancing security markings on an inode.
276 * Returns 0 if successful, -ve on error.
278 int cap_inode_killpriv(struct dentry *dentry)
280 struct inode *inode = dentry->d_inode;
282 if (!inode->i_op->removexattr)
283 return 0;
285 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
289 * Calculate the new process capability sets from the capability sets attached
290 * to a file.
292 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
293 struct linux_binprm *bprm,
294 bool *effective)
296 struct cred *new = bprm->cred;
297 unsigned i;
298 int ret = 0;
300 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
301 *effective = true;
303 CAP_FOR_EACH_U32(i) {
304 __u32 permitted = caps->permitted.cap[i];
305 __u32 inheritable = caps->inheritable.cap[i];
308 * pP' = (X & fP) | (pI & fI)
310 new->cap_permitted.cap[i] =
311 (new->cap_bset.cap[i] & permitted) |
312 (new->cap_inheritable.cap[i] & inheritable);
314 if (permitted & ~new->cap_permitted.cap[i])
315 /* insufficient to execute correctly */
316 ret = -EPERM;
320 * For legacy apps, with no internal support for recognizing they
321 * do not have enough capabilities, we return an error if they are
322 * missing some "forced" (aka file-permitted) capabilities.
324 return *effective ? ret : 0;
328 * Extract the on-exec-apply capability sets for an executable file.
330 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
332 struct inode *inode = dentry->d_inode;
333 __u32 magic_etc;
334 unsigned tocopy, i;
335 int size;
336 struct vfs_cap_data caps;
338 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
340 if (!inode || !inode->i_op->getxattr)
341 return -ENODATA;
343 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
344 XATTR_CAPS_SZ);
345 if (size == -ENODATA || size == -EOPNOTSUPP)
346 /* no data, that's ok */
347 return -ENODATA;
348 if (size < 0)
349 return size;
351 if (size < sizeof(magic_etc))
352 return -EINVAL;
354 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
356 switch (magic_etc & VFS_CAP_REVISION_MASK) {
357 case VFS_CAP_REVISION_1:
358 if (size != XATTR_CAPS_SZ_1)
359 return -EINVAL;
360 tocopy = VFS_CAP_U32_1;
361 break;
362 case VFS_CAP_REVISION_2:
363 if (size != XATTR_CAPS_SZ_2)
364 return -EINVAL;
365 tocopy = VFS_CAP_U32_2;
366 break;
367 default:
368 return -EINVAL;
371 CAP_FOR_EACH_U32(i) {
372 if (i >= tocopy)
373 break;
374 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
375 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
378 return 0;
382 * Attempt to get the on-exec apply capability sets for an executable file from
383 * its xattrs and, if present, apply them to the proposed credentials being
384 * constructed by execve().
386 static int get_file_caps(struct linux_binprm *bprm, bool *effective)
388 struct dentry *dentry;
389 int rc = 0;
390 struct cpu_vfs_cap_data vcaps;
392 bprm_clear_caps(bprm);
394 if (!file_caps_enabled)
395 return 0;
397 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
398 return 0;
400 dentry = dget(bprm->file->f_dentry);
402 rc = get_vfs_caps_from_disk(dentry, &vcaps);
403 if (rc < 0) {
404 if (rc == -EINVAL)
405 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
406 __func__, rc, bprm->filename);
407 else if (rc == -ENODATA)
408 rc = 0;
409 goto out;
412 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
413 if (rc == -EINVAL)
414 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
415 __func__, rc, bprm->filename);
417 out:
418 dput(dentry);
419 if (rc)
420 bprm_clear_caps(bprm);
422 return rc;
425 #else
426 int cap_inode_need_killpriv(struct dentry *dentry)
428 return 0;
431 int cap_inode_killpriv(struct dentry *dentry)
433 return 0;
436 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
438 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
439 return -ENODATA;
442 static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
444 bprm_clear_caps(bprm);
445 return 0;
447 #endif
450 * Determine whether a exec'ing process's new permitted capabilities should be
451 * limited to just what it already has.
453 * This prevents processes that are being ptraced from gaining access to
454 * CAP_SETPCAP, unless the process they're tracing already has it, and the
455 * binary they're executing has filecaps that elevate it.
457 * Returns 1 if they should be limited, 0 if they are not.
459 static inline int cap_limit_ptraced_target(void)
461 #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
462 if (capable(CAP_SETPCAP))
463 return 0;
464 #endif
465 return 1;
469 * cap_bprm_set_creds - Set up the proposed credentials for execve().
470 * @bprm: The execution parameters, including the proposed creds
472 * Set up the proposed credentials for a new execution context being
473 * constructed by execve(). The proposed creds in @bprm->cred is altered,
474 * which won't take effect immediately. Returns 0 if successful, -ve on error.
476 int cap_bprm_set_creds(struct linux_binprm *bprm)
478 const struct cred *old = current_cred();
479 struct cred *new = bprm->cred;
480 bool effective;
481 int ret;
483 effective = false;
484 ret = get_file_caps(bprm, &effective);
485 if (ret < 0)
486 return ret;
488 if (!issecure(SECURE_NOROOT)) {
490 * If the legacy file capability is set, then don't set privs
491 * for a setuid root binary run by a non-root user. Do set it
492 * for a root user just to cause least surprise to an admin.
494 if (effective && new->uid != 0 && new->euid == 0) {
495 warn_setuid_and_fcaps_mixed(bprm->filename);
496 goto skip;
499 * To support inheritance of root-permissions and suid-root
500 * executables under compatibility mode, we override the
501 * capability sets for the file.
503 * If only the real uid is 0, we do not set the effective bit.
505 if (new->euid == 0 || new->uid == 0) {
506 /* pP' = (cap_bset & ~0) | (pI & ~0) */
507 new->cap_permitted = cap_combine(old->cap_bset,
508 old->cap_inheritable);
510 if (new->euid == 0)
511 effective = true;
513 skip:
515 /* if we have fs caps, clear dangerous personality flags */
516 if (!cap_issubset(new->cap_permitted, old->cap_permitted))
517 bprm->per_clear |= PER_CLEAR_ON_SETID;
520 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
521 * credentials unless they have the appropriate permit
523 if ((new->euid != old->uid ||
524 new->egid != old->gid ||
525 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
526 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
527 /* downgrade; they get no more than they had, and maybe less */
528 if (!capable(CAP_SETUID)) {
529 new->euid = new->uid;
530 new->egid = new->gid;
532 if (cap_limit_ptraced_target())
533 new->cap_permitted = cap_intersect(new->cap_permitted,
534 old->cap_permitted);
537 new->suid = new->fsuid = new->euid;
538 new->sgid = new->fsgid = new->egid;
540 /* For init, we want to retain the capabilities set in the initial
541 * task. Thus we skip the usual capability rules
543 if (!is_global_init(current)) {
544 if (effective)
545 new->cap_effective = new->cap_permitted;
546 else
547 cap_clear(new->cap_effective);
549 bprm->cap_effective = effective;
552 * Audit candidate if current->cap_effective is set
554 * We do not bother to audit if 3 things are true:
555 * 1) cap_effective has all caps
556 * 2) we are root
557 * 3) root is supposed to have all caps (SECURE_NOROOT)
558 * Since this is just a normal root execing a process.
560 * Number 1 above might fail if you don't have a full bset, but I think
561 * that is interesting information to audit.
563 if (!cap_isclear(new->cap_effective)) {
564 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
565 new->euid != 0 || new->uid != 0 ||
566 issecure(SECURE_NOROOT)) {
567 ret = audit_log_bprm_fcaps(bprm, new, old);
568 if (ret < 0)
569 return ret;
573 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
574 return 0;
578 * cap_bprm_secureexec - Determine whether a secure execution is required
579 * @bprm: The execution parameters
581 * Determine whether a secure execution is required, return 1 if it is, and 0
582 * if it is not.
584 * The credentials have been committed by this point, and so are no longer
585 * available through @bprm->cred.
587 int cap_bprm_secureexec(struct linux_binprm *bprm)
589 const struct cred *cred = current_cred();
591 if (cred->uid != 0) {
592 if (bprm->cap_effective)
593 return 1;
594 if (!cap_isclear(cred->cap_permitted))
595 return 1;
598 return (cred->euid != cred->uid ||
599 cred->egid != cred->gid);
603 * cap_inode_setxattr - Determine whether an xattr may be altered
604 * @dentry: The inode/dentry being altered
605 * @name: The name of the xattr to be changed
606 * @value: The value that the xattr will be changed to
607 * @size: The size of value
608 * @flags: The replacement flag
610 * Determine whether an xattr may be altered or set on an inode, returning 0 if
611 * permission is granted, -ve if denied.
613 * This is used to make sure security xattrs don't get updated or set by those
614 * who aren't privileged to do so.
616 int cap_inode_setxattr(struct dentry *dentry, const char *name,
617 const void *value, size_t size, int flags)
619 if (!strcmp(name, XATTR_NAME_CAPS)) {
620 if (!capable(CAP_SETFCAP))
621 return -EPERM;
622 return 0;
625 if (!strncmp(name, XATTR_SECURITY_PREFIX,
626 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
627 !capable(CAP_SYS_ADMIN))
628 return -EPERM;
629 return 0;
633 * cap_inode_removexattr - Determine whether an xattr may be removed
634 * @dentry: The inode/dentry being altered
635 * @name: The name of the xattr to be changed
637 * Determine whether an xattr may be removed from an inode, returning 0 if
638 * permission is granted, -ve if denied.
640 * This is used to make sure security xattrs don't get removed by those who
641 * aren't privileged to remove them.
643 int cap_inode_removexattr(struct dentry *dentry, const char *name)
645 if (!strcmp(name, XATTR_NAME_CAPS)) {
646 if (!capable(CAP_SETFCAP))
647 return -EPERM;
648 return 0;
651 if (!strncmp(name, XATTR_SECURITY_PREFIX,
652 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
653 !capable(CAP_SYS_ADMIN))
654 return -EPERM;
655 return 0;
659 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
660 * a process after a call to setuid, setreuid, or setresuid.
662 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
663 * {r,e,s}uid != 0, the permitted and effective capabilities are
664 * cleared.
666 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
667 * capabilities of the process are cleared.
669 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
670 * capabilities are set to the permitted capabilities.
672 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
673 * never happen.
675 * -astor
677 * cevans - New behaviour, Oct '99
678 * A process may, via prctl(), elect to keep its capabilities when it
679 * calls setuid() and switches away from uid==0. Both permitted and
680 * effective sets will be retained.
681 * Without this change, it was impossible for a daemon to drop only some
682 * of its privilege. The call to setuid(!=0) would drop all privileges!
683 * Keeping uid 0 is not an option because uid 0 owns too many vital
684 * files..
685 * Thanks to Olaf Kirch and Peter Benie for spotting this.
687 static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
689 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
690 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
691 !issecure(SECURE_KEEP_CAPS)) {
692 cap_clear(new->cap_permitted);
693 cap_clear(new->cap_effective);
695 if (old->euid == 0 && new->euid != 0)
696 cap_clear(new->cap_effective);
697 if (old->euid != 0 && new->euid == 0)
698 new->cap_effective = new->cap_permitted;
702 * cap_task_fix_setuid - Fix up the results of setuid() call
703 * @new: The proposed credentials
704 * @old: The current task's current credentials
705 * @flags: Indications of what has changed
707 * Fix up the results of setuid() call before the credential changes are
708 * actually applied, returning 0 to grant the changes, -ve to deny them.
710 int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
712 switch (flags) {
713 case LSM_SETID_RE:
714 case LSM_SETID_ID:
715 case LSM_SETID_RES:
716 /* juggle the capabilities to follow [RES]UID changes unless
717 * otherwise suppressed */
718 if (!issecure(SECURE_NO_SETUID_FIXUP))
719 cap_emulate_setxuid(new, old);
720 break;
722 case LSM_SETID_FS:
723 /* juggle the capabilties to follow FSUID changes, unless
724 * otherwise suppressed
726 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
727 * if not, we might be a bit too harsh here.
729 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
730 if (old->fsuid == 0 && new->fsuid != 0)
731 new->cap_effective =
732 cap_drop_fs_set(new->cap_effective);
734 if (old->fsuid != 0 && new->fsuid == 0)
735 new->cap_effective =
736 cap_raise_fs_set(new->cap_effective,
737 new->cap_permitted);
739 break;
741 default:
742 return -EINVAL;
745 return 0;
748 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
750 * Rationale: code calling task_setscheduler, task_setioprio, and
751 * task_setnice, assumes that
752 * . if capable(cap_sys_nice), then those actions should be allowed
753 * . if not capable(cap_sys_nice), but acting on your own processes,
754 * then those actions should be allowed
755 * This is insufficient now since you can call code without suid, but
756 * yet with increased caps.
757 * So we check for increased caps on the target process.
759 static int cap_safe_nice(struct task_struct *p)
761 int is_subset;
763 rcu_read_lock();
764 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
765 current_cred()->cap_permitted);
766 rcu_read_unlock();
768 if (!is_subset && !capable(CAP_SYS_NICE))
769 return -EPERM;
770 return 0;
774 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
775 * @p: The task to affect
776 * @policy: The policy to effect
777 * @lp: The parameters to the scheduling policy
779 * Detemine if the requested scheduler policy change is permitted for the
780 * specified task, returning 0 if permission is granted, -ve if denied.
782 int cap_task_setscheduler(struct task_struct *p, int policy,
783 struct sched_param *lp)
785 return cap_safe_nice(p);
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
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.
796 int cap_task_setioprio(struct task_struct *p, int ioprio)
798 return cap_safe_nice(p);
802 * cap_task_ioprio - Detemine if task priority change is permitted
803 * @p: The task to affect
804 * @nice: The nice value to set
806 * Detemine if the requested task priority change is permitted for the
807 * specified task, returning 0 if permission is granted, -ve if denied.
809 int cap_task_setnice(struct task_struct *p, int nice)
811 return cap_safe_nice(p);
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.
818 static long cap_prctl_drop(struct cred *new, unsigned long cap)
820 if (!capable(CAP_SETPCAP))
821 return -EPERM;
822 if (!cap_valid(cap))
823 return -EINVAL;
825 cap_lower(new->cap_bset, cap);
826 return 0;
829 #else
830 int cap_task_setscheduler (struct task_struct *p, int policy,
831 struct sched_param *lp)
833 return 0;
835 int cap_task_setioprio (struct task_struct *p, int ioprio)
837 return 0;
839 int cap_task_setnice (struct task_struct *p, int nice)
841 return 0;
843 #endif
846 * cap_task_prctl - Implement process control functions for this security module
847 * @option: The process control function requested
848 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
850 * Allow process control functions (sys_prctl()) to alter capabilities; may
851 * also deny access to other functions not otherwise implemented here.
853 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
854 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
855 * modules will consider performing the function.
857 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
858 unsigned long arg4, unsigned long arg5)
860 struct cred *new;
861 long error = 0;
863 new = prepare_creds();
864 if (!new)
865 return -ENOMEM;
867 switch (option) {
868 case PR_CAPBSET_READ:
869 error = -EINVAL;
870 if (!cap_valid(arg2))
871 goto error;
872 error = !!cap_raised(new->cap_bset, arg2);
873 goto no_change;
875 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
876 case PR_CAPBSET_DROP:
877 error = cap_prctl_drop(new, arg2);
878 if (error < 0)
879 goto error;
880 goto changed;
883 * The next four prctl's remain to assist with transitioning a
884 * system from legacy UID=0 based privilege (when filesystem
885 * capabilities are not in use) to a system using filesystem
886 * capabilities only - as the POSIX.1e draft intended.
888 * Note:
890 * PR_SET_SECUREBITS =
891 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
892 * | issecure_mask(SECURE_NOROOT)
893 * | issecure_mask(SECURE_NOROOT_LOCKED)
894 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
895 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
897 * will ensure that the current process and all of its
898 * children will be locked into a pure
899 * capability-based-privilege environment.
901 case PR_SET_SECUREBITS:
902 error = -EPERM;
903 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
904 & (new->securebits ^ arg2)) /*[1]*/
905 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
906 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
907 || (cap_capable(current, current_cred(), CAP_SETPCAP,
908 SECURITY_CAP_AUDIT) != 0) /*[4]*/
910 * [1] no changing of bits that are locked
911 * [2] no unlocking of locks
912 * [3] no setting of unsupported bits
913 * [4] doing anything requires privilege (go read about
914 * the "sendmail capabilities bug")
917 /* cannot change a locked bit */
918 goto error;
919 new->securebits = arg2;
920 goto changed;
922 case PR_GET_SECUREBITS:
923 error = new->securebits;
924 goto no_change;
926 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
928 case PR_GET_KEEPCAPS:
929 if (issecure(SECURE_KEEP_CAPS))
930 error = 1;
931 goto no_change;
933 case PR_SET_KEEPCAPS:
934 error = -EINVAL;
935 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
936 goto error;
937 error = -EPERM;
938 if (issecure(SECURE_KEEP_CAPS_LOCKED))
939 goto error;
940 if (arg2)
941 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
942 else
943 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
944 goto changed;
946 default:
947 /* No functionality available - continue with default */
948 error = -ENOSYS;
949 goto error;
952 /* Functionality provided */
953 changed:
954 return commit_creds(new);
956 no_change:
957 error:
958 abort_creds(new);
959 return error;
963 * cap_syslog - Determine whether syslog function is permitted
964 * @type: Function requested
966 * Determine whether the current process is permitted to use a particular
967 * syslog function, returning 0 if permission is granted, -ve if not.
969 int cap_syslog(int type)
971 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
972 return -EPERM;
973 return 0;
977 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
978 * @mm: The VM space in which the new mapping is to be made
979 * @pages: The size of the mapping
981 * Determine whether the allocation of a new virtual mapping by the current
982 * task is permitted, returning 0 if permission is granted, -ve if not.
984 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
986 int cap_sys_admin = 0;
988 if (cap_capable(current, current_cred(), CAP_SYS_ADMIN,
989 SECURITY_CAP_NOAUDIT) == 0)
990 cap_sys_admin = 1;
991 return __vm_enough_memory(mm, pages, cap_sys_admin);
995 * cap_file_mmap - check if able to map given addr
996 * @file: unused
997 * @reqprot: unused
998 * @prot: unused
999 * @flags: unused
1000 * @addr: address attempting to be mapped
1001 * @addr_only: unused
1003 * If the process is attempting to map memory below mmap_min_addr they need
1004 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
1005 * capability security module. Returns 0 if this mapping should be allowed
1006 * -EPERM if not.
1008 int cap_file_mmap(struct file *file, unsigned long reqprot,
1009 unsigned long prot, unsigned long flags,
1010 unsigned long addr, unsigned long addr_only)
1012 int ret = 0;
1014 if (addr < dac_mmap_min_addr) {
1015 ret = cap_capable(current, current_cred(), CAP_SYS_RAWIO,
1016 SECURITY_CAP_AUDIT);
1017 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
1018 if (ret == 0)
1019 current->flags |= PF_SUPERPRIV;
1021 return ret;