x86: trivial clean up for irq_vectors.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / commoncap.c
blobbeac0258c2a8f3a0cbad52ec9adf12e019264626
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>
31 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
33 NETLINK_CB(skb).eff_cap = current_cap();
34 return 0;
37 int cap_netlink_recv(struct sk_buff *skb, int cap)
39 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
40 return -EPERM;
41 return 0;
43 EXPORT_SYMBOL(cap_netlink_recv);
45 /**
46 * cap_capable - Determine whether a task has a particular effective capability
47 * @tsk: The task to query
48 * @cred: The credentials to use
49 * @cap: The capability to check for
50 * @audit: Whether to write an audit message or not
52 * Determine whether the nominated task has the specified capability amongst
53 * its effective set, returning 0 if it does, -ve if it does not.
55 * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
56 * and has_capability() functions. That is, it has the reverse semantics:
57 * cap_has_capability() returns 0 when a task has a capability, but the
58 * kernel's capable() and has_capability() returns 1 for this case.
60 int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
61 int audit)
63 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
66 /**
67 * cap_settime - Determine whether the current process may set the system clock
68 * @ts: The time to set
69 * @tz: The timezone to set
71 * Determine whether the current process may set the system clock and timezone
72 * information, returning 0 if permission granted, -ve if denied.
74 int cap_settime(struct timespec *ts, struct timezone *tz)
76 if (!capable(CAP_SYS_TIME))
77 return -EPERM;
78 return 0;
81 /**
82 * cap_ptrace_may_access - Determine whether the current process may access
83 * another
84 * @child: The process to be accessed
85 * @mode: The mode of attachment.
87 * Determine whether a process may access another, returning 0 if permission
88 * granted, -ve if denied.
90 int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
92 int ret = 0;
94 rcu_read_lock();
95 if (!cap_issubset(__task_cred(child)->cap_permitted,
96 current_cred()->cap_permitted) &&
97 !capable(CAP_SYS_PTRACE))
98 ret = -EPERM;
99 rcu_read_unlock();
100 return ret;
104 * cap_ptrace_traceme - Determine whether another process may trace the current
105 * @parent: The task proposed to be the tracer
107 * Determine whether the nominated task is permitted to trace the current
108 * process, returning 0 if permission is granted, -ve if denied.
110 int cap_ptrace_traceme(struct task_struct *parent)
112 int ret = 0;
114 rcu_read_lock();
115 if (!cap_issubset(current_cred()->cap_permitted,
116 __task_cred(parent)->cap_permitted) &&
117 !has_capability(parent, CAP_SYS_PTRACE))
118 ret = -EPERM;
119 rcu_read_unlock();
120 return ret;
124 * cap_capget - Retrieve a task's capability sets
125 * @target: The task from which to retrieve the capability sets
126 * @effective: The place to record the effective set
127 * @inheritable: The place to record the inheritable set
128 * @permitted: The place to record the permitted set
130 * This function retrieves the capabilities of the nominated task and returns
131 * them to the caller.
133 int cap_capget(struct task_struct *target, kernel_cap_t *effective,
134 kernel_cap_t *inheritable, kernel_cap_t *permitted)
136 const struct cred *cred;
138 /* Derived from kernel/capability.c:sys_capget. */
139 rcu_read_lock();
140 cred = __task_cred(target);
141 *effective = cred->cap_effective;
142 *inheritable = cred->cap_inheritable;
143 *permitted = cred->cap_permitted;
144 rcu_read_unlock();
145 return 0;
149 * Determine whether the inheritable capabilities are limited to the old
150 * permitted set. Returns 1 if they are limited, 0 if they are not.
152 static inline int cap_inh_is_capped(void)
154 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
156 /* they are so limited unless the current task has the CAP_SETPCAP
157 * capability
159 if (cap_capable(current, current_cred(), CAP_SETPCAP,
160 SECURITY_CAP_AUDIT) == 0)
161 return 0;
162 #endif
163 return 1;
167 * cap_capset - Validate and apply proposed changes to current's capabilities
168 * @new: The proposed new credentials; alterations should be made here
169 * @old: The current task's current credentials
170 * @effective: A pointer to the proposed new effective capabilities set
171 * @inheritable: A pointer to the proposed new inheritable capabilities set
172 * @permitted: A pointer to the proposed new permitted capabilities set
174 * This function validates and applies a proposed mass change to the current
175 * process's capability sets. The changes are made to the proposed new
176 * credentials, and assuming no error, will be committed by the caller of LSM.
178 int cap_capset(struct cred *new,
179 const struct cred *old,
180 const kernel_cap_t *effective,
181 const kernel_cap_t *inheritable,
182 const kernel_cap_t *permitted)
184 if (cap_inh_is_capped() &&
185 !cap_issubset(*inheritable,
186 cap_combine(old->cap_inheritable,
187 old->cap_permitted)))
188 /* incapable of using this inheritable set */
189 return -EPERM;
191 if (!cap_issubset(*inheritable,
192 cap_combine(old->cap_inheritable,
193 old->cap_bset)))
194 /* no new pI capabilities outside bounding set */
195 return -EPERM;
197 /* verify restrictions on target's new Permitted set */
198 if (!cap_issubset(*permitted, old->cap_permitted))
199 return -EPERM;
201 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
202 if (!cap_issubset(*effective, *permitted))
203 return -EPERM;
205 new->cap_effective = *effective;
206 new->cap_inheritable = *inheritable;
207 new->cap_permitted = *permitted;
208 return 0;
212 * Clear proposed capability sets for execve().
214 static inline void bprm_clear_caps(struct linux_binprm *bprm)
216 cap_clear(bprm->cred->cap_permitted);
217 bprm->cap_effective = false;
220 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
223 * cap_inode_need_killpriv - Determine if inode change affects privileges
224 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
226 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
227 * affects the security markings on that inode, and if it is, should
228 * inode_killpriv() be invoked or the change rejected?
230 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
231 * -ve to deny the change.
233 int cap_inode_need_killpriv(struct dentry *dentry)
235 struct inode *inode = dentry->d_inode;
236 int error;
238 if (!inode->i_op->getxattr)
239 return 0;
241 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
242 if (error <= 0)
243 return 0;
244 return 1;
248 * cap_inode_killpriv - Erase the security markings on an inode
249 * @dentry: The inode/dentry to alter
251 * Erase the privilege-enhancing security markings on an inode.
253 * Returns 0 if successful, -ve on error.
255 int cap_inode_killpriv(struct dentry *dentry)
257 struct inode *inode = dentry->d_inode;
259 if (!inode->i_op->removexattr)
260 return 0;
262 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
266 * Calculate the new process capability sets from the capability sets attached
267 * to a file.
269 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
270 struct linux_binprm *bprm,
271 bool *effective)
273 struct cred *new = bprm->cred;
274 unsigned i;
275 int ret = 0;
277 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
278 *effective = true;
280 CAP_FOR_EACH_U32(i) {
281 __u32 permitted = caps->permitted.cap[i];
282 __u32 inheritable = caps->inheritable.cap[i];
285 * pP' = (X & fP) | (pI & fI)
287 new->cap_permitted.cap[i] =
288 (new->cap_bset.cap[i] & permitted) |
289 (new->cap_inheritable.cap[i] & inheritable);
291 if (permitted & ~new->cap_permitted.cap[i])
292 /* insufficient to execute correctly */
293 ret = -EPERM;
297 * For legacy apps, with no internal support for recognizing they
298 * do not have enough capabilities, we return an error if they are
299 * missing some "forced" (aka file-permitted) capabilities.
301 return *effective ? ret : 0;
305 * Extract the on-exec-apply capability sets for an executable file.
307 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
309 struct inode *inode = dentry->d_inode;
310 __u32 magic_etc;
311 unsigned tocopy, i;
312 int size;
313 struct vfs_cap_data caps;
315 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
317 if (!inode || !inode->i_op->getxattr)
318 return -ENODATA;
320 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
321 XATTR_CAPS_SZ);
322 if (size == -ENODATA || size == -EOPNOTSUPP)
323 /* no data, that's ok */
324 return -ENODATA;
325 if (size < 0)
326 return size;
328 if (size < sizeof(magic_etc))
329 return -EINVAL;
331 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
333 switch (magic_etc & VFS_CAP_REVISION_MASK) {
334 case VFS_CAP_REVISION_1:
335 if (size != XATTR_CAPS_SZ_1)
336 return -EINVAL;
337 tocopy = VFS_CAP_U32_1;
338 break;
339 case VFS_CAP_REVISION_2:
340 if (size != XATTR_CAPS_SZ_2)
341 return -EINVAL;
342 tocopy = VFS_CAP_U32_2;
343 break;
344 default:
345 return -EINVAL;
348 CAP_FOR_EACH_U32(i) {
349 if (i >= tocopy)
350 break;
351 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
352 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
355 return 0;
359 * Attempt to get the on-exec apply capability sets for an executable file from
360 * its xattrs and, if present, apply them to the proposed credentials being
361 * constructed by execve().
363 static int get_file_caps(struct linux_binprm *bprm, bool *effective)
365 struct dentry *dentry;
366 int rc = 0;
367 struct cpu_vfs_cap_data vcaps;
369 bprm_clear_caps(bprm);
371 if (!file_caps_enabled)
372 return 0;
374 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
375 return 0;
377 dentry = dget(bprm->file->f_dentry);
379 rc = get_vfs_caps_from_disk(dentry, &vcaps);
380 if (rc < 0) {
381 if (rc == -EINVAL)
382 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
383 __func__, rc, bprm->filename);
384 else if (rc == -ENODATA)
385 rc = 0;
386 goto out;
389 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
390 if (rc == -EINVAL)
391 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
392 __func__, rc, bprm->filename);
394 out:
395 dput(dentry);
396 if (rc)
397 bprm_clear_caps(bprm);
399 return rc;
402 #else
403 int cap_inode_need_killpriv(struct dentry *dentry)
405 return 0;
408 int cap_inode_killpriv(struct dentry *dentry)
410 return 0;
413 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
415 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
416 return -ENODATA;
419 static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
421 bprm_clear_caps(bprm);
422 return 0;
424 #endif
427 * Determine whether a exec'ing process's new permitted capabilities should be
428 * limited to just what it already has.
430 * This prevents processes that are being ptraced from gaining access to
431 * CAP_SETPCAP, unless the process they're tracing already has it, and the
432 * binary they're executing has filecaps that elevate it.
434 * Returns 1 if they should be limited, 0 if they are not.
436 static inline int cap_limit_ptraced_target(void)
438 #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
439 if (capable(CAP_SETPCAP))
440 return 0;
441 #endif
442 return 1;
446 * cap_bprm_set_creds - Set up the proposed credentials for execve().
447 * @bprm: The execution parameters, including the proposed creds
449 * Set up the proposed credentials for a new execution context being
450 * constructed by execve(). The proposed creds in @bprm->cred is altered,
451 * which won't take effect immediately. Returns 0 if successful, -ve on error.
453 int cap_bprm_set_creds(struct linux_binprm *bprm)
455 const struct cred *old = current_cred();
456 struct cred *new = bprm->cred;
457 bool effective;
458 int ret;
460 effective = false;
461 ret = get_file_caps(bprm, &effective);
462 if (ret < 0)
463 return ret;
465 if (!issecure(SECURE_NOROOT)) {
467 * To support inheritance of root-permissions and suid-root
468 * executables under compatibility mode, we override the
469 * capability sets for the file.
471 * If only the real uid is 0, we do not set the effective bit.
473 if (new->euid == 0 || new->uid == 0) {
474 /* pP' = (cap_bset & ~0) | (pI & ~0) */
475 new->cap_permitted = cap_combine(old->cap_bset,
476 old->cap_inheritable);
478 if (new->euid == 0)
479 effective = true;
482 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
483 * credentials unless they have the appropriate permit
485 if ((new->euid != old->uid ||
486 new->egid != old->gid ||
487 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
488 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
489 /* downgrade; they get no more than they had, and maybe less */
490 if (!capable(CAP_SETUID)) {
491 new->euid = new->uid;
492 new->egid = new->gid;
494 if (cap_limit_ptraced_target())
495 new->cap_permitted = cap_intersect(new->cap_permitted,
496 old->cap_permitted);
499 new->suid = new->fsuid = new->euid;
500 new->sgid = new->fsgid = new->egid;
502 /* For init, we want to retain the capabilities set in the initial
503 * task. Thus we skip the usual capability rules
505 if (!is_global_init(current)) {
506 if (effective)
507 new->cap_effective = new->cap_permitted;
508 else
509 cap_clear(new->cap_effective);
511 bprm->cap_effective = effective;
514 * Audit candidate if current->cap_effective is set
516 * We do not bother to audit if 3 things are true:
517 * 1) cap_effective has all caps
518 * 2) we are root
519 * 3) root is supposed to have all caps (SECURE_NOROOT)
520 * Since this is just a normal root execing a process.
522 * Number 1 above might fail if you don't have a full bset, but I think
523 * that is interesting information to audit.
525 if (!cap_isclear(new->cap_effective)) {
526 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
527 new->euid != 0 || new->uid != 0 ||
528 issecure(SECURE_NOROOT)) {
529 ret = audit_log_bprm_fcaps(bprm, new, old);
530 if (ret < 0)
531 return ret;
535 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
536 return 0;
540 * cap_bprm_secureexec - Determine whether a secure execution is required
541 * @bprm: The execution parameters
543 * Determine whether a secure execution is required, return 1 if it is, and 0
544 * if it is not.
546 * The credentials have been committed by this point, and so are no longer
547 * available through @bprm->cred.
549 int cap_bprm_secureexec(struct linux_binprm *bprm)
551 const struct cred *cred = current_cred();
553 if (cred->uid != 0) {
554 if (bprm->cap_effective)
555 return 1;
556 if (!cap_isclear(cred->cap_permitted))
557 return 1;
560 return (cred->euid != cred->uid ||
561 cred->egid != cred->gid);
565 * cap_inode_setxattr - Determine whether an xattr may be altered
566 * @dentry: The inode/dentry being altered
567 * @name: The name of the xattr to be changed
568 * @value: The value that the xattr will be changed to
569 * @size: The size of value
570 * @flags: The replacement flag
572 * Determine whether an xattr may be altered or set on an inode, returning 0 if
573 * permission is granted, -ve if denied.
575 * This is used to make sure security xattrs don't get updated or set by those
576 * who aren't privileged to do so.
578 int cap_inode_setxattr(struct dentry *dentry, const char *name,
579 const void *value, size_t size, int flags)
581 if (!strcmp(name, XATTR_NAME_CAPS)) {
582 if (!capable(CAP_SETFCAP))
583 return -EPERM;
584 return 0;
587 if (!strncmp(name, XATTR_SECURITY_PREFIX,
588 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
589 !capable(CAP_SYS_ADMIN))
590 return -EPERM;
591 return 0;
595 * cap_inode_removexattr - Determine whether an xattr may be removed
596 * @dentry: The inode/dentry being altered
597 * @name: The name of the xattr to be changed
599 * Determine whether an xattr may be removed from an inode, returning 0 if
600 * permission is granted, -ve if denied.
602 * This is used to make sure security xattrs don't get removed by those who
603 * aren't privileged to remove them.
605 int cap_inode_removexattr(struct dentry *dentry, const char *name)
607 if (!strcmp(name, XATTR_NAME_CAPS)) {
608 if (!capable(CAP_SETFCAP))
609 return -EPERM;
610 return 0;
613 if (!strncmp(name, XATTR_SECURITY_PREFIX,
614 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
615 !capable(CAP_SYS_ADMIN))
616 return -EPERM;
617 return 0;
621 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
622 * a process after a call to setuid, setreuid, or setresuid.
624 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
625 * {r,e,s}uid != 0, the permitted and effective capabilities are
626 * cleared.
628 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
629 * capabilities of the process are cleared.
631 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
632 * capabilities are set to the permitted capabilities.
634 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
635 * never happen.
637 * -astor
639 * cevans - New behaviour, Oct '99
640 * A process may, via prctl(), elect to keep its capabilities when it
641 * calls setuid() and switches away from uid==0. Both permitted and
642 * effective sets will be retained.
643 * Without this change, it was impossible for a daemon to drop only some
644 * of its privilege. The call to setuid(!=0) would drop all privileges!
645 * Keeping uid 0 is not an option because uid 0 owns too many vital
646 * files..
647 * Thanks to Olaf Kirch and Peter Benie for spotting this.
649 static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
651 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
652 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
653 !issecure(SECURE_KEEP_CAPS)) {
654 cap_clear(new->cap_permitted);
655 cap_clear(new->cap_effective);
657 if (old->euid == 0 && new->euid != 0)
658 cap_clear(new->cap_effective);
659 if (old->euid != 0 && new->euid == 0)
660 new->cap_effective = new->cap_permitted;
664 * cap_task_fix_setuid - Fix up the results of setuid() call
665 * @new: The proposed credentials
666 * @old: The current task's current credentials
667 * @flags: Indications of what has changed
669 * Fix up the results of setuid() call before the credential changes are
670 * actually applied, returning 0 to grant the changes, -ve to deny them.
672 int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
674 switch (flags) {
675 case LSM_SETID_RE:
676 case LSM_SETID_ID:
677 case LSM_SETID_RES:
678 /* juggle the capabilities to follow [RES]UID changes unless
679 * otherwise suppressed */
680 if (!issecure(SECURE_NO_SETUID_FIXUP))
681 cap_emulate_setxuid(new, old);
682 break;
684 case LSM_SETID_FS:
685 /* juggle the capabilties to follow FSUID changes, unless
686 * otherwise suppressed
688 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
689 * if not, we might be a bit too harsh here.
691 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
692 if (old->fsuid == 0 && new->fsuid != 0)
693 new->cap_effective =
694 cap_drop_fs_set(new->cap_effective);
696 if (old->fsuid != 0 && new->fsuid == 0)
697 new->cap_effective =
698 cap_raise_fs_set(new->cap_effective,
699 new->cap_permitted);
701 break;
703 default:
704 return -EINVAL;
707 return 0;
710 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
712 * Rationale: code calling task_setscheduler, task_setioprio, and
713 * task_setnice, assumes that
714 * . if capable(cap_sys_nice), then those actions should be allowed
715 * . if not capable(cap_sys_nice), but acting on your own processes,
716 * then those actions should be allowed
717 * This is insufficient now since you can call code without suid, but
718 * yet with increased caps.
719 * So we check for increased caps on the target process.
721 static int cap_safe_nice(struct task_struct *p)
723 int is_subset;
725 rcu_read_lock();
726 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
727 current_cred()->cap_permitted);
728 rcu_read_unlock();
730 if (!is_subset && !capable(CAP_SYS_NICE))
731 return -EPERM;
732 return 0;
736 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
737 * @p: The task to affect
738 * @policy: The policy to effect
739 * @lp: The parameters to the scheduling policy
741 * Detemine if the requested scheduler policy change is permitted for the
742 * specified task, returning 0 if permission is granted, -ve if denied.
744 int cap_task_setscheduler(struct task_struct *p, int policy,
745 struct sched_param *lp)
747 return cap_safe_nice(p);
751 * cap_task_ioprio - Detemine if I/O priority change is permitted
752 * @p: The task to affect
753 * @ioprio: The I/O priority to set
755 * Detemine if the requested I/O priority change is permitted for the specified
756 * task, returning 0 if permission is granted, -ve if denied.
758 int cap_task_setioprio(struct task_struct *p, int ioprio)
760 return cap_safe_nice(p);
764 * cap_task_ioprio - Detemine if task priority change is permitted
765 * @p: The task to affect
766 * @nice: The nice value to set
768 * Detemine if the requested task priority change is permitted for the
769 * specified task, returning 0 if permission is granted, -ve if denied.
771 int cap_task_setnice(struct task_struct *p, int nice)
773 return cap_safe_nice(p);
777 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
778 * the current task's bounding set. Returns 0 on success, -ve on error.
780 static long cap_prctl_drop(struct cred *new, unsigned long cap)
782 if (!capable(CAP_SETPCAP))
783 return -EPERM;
784 if (!cap_valid(cap))
785 return -EINVAL;
787 cap_lower(new->cap_bset, cap);
788 return 0;
791 #else
792 int cap_task_setscheduler (struct task_struct *p, int policy,
793 struct sched_param *lp)
795 return 0;
797 int cap_task_setioprio (struct task_struct *p, int ioprio)
799 return 0;
801 int cap_task_setnice (struct task_struct *p, int nice)
803 return 0;
805 #endif
808 * cap_task_prctl - Implement process control functions for this security module
809 * @option: The process control function requested
810 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
812 * Allow process control functions (sys_prctl()) to alter capabilities; may
813 * also deny access to other functions not otherwise implemented here.
815 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
816 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
817 * modules will consider performing the function.
819 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
820 unsigned long arg4, unsigned long arg5)
822 struct cred *new;
823 long error = 0;
825 new = prepare_creds();
826 if (!new)
827 return -ENOMEM;
829 switch (option) {
830 case PR_CAPBSET_READ:
831 error = -EINVAL;
832 if (!cap_valid(arg2))
833 goto error;
834 error = !!cap_raised(new->cap_bset, arg2);
835 goto no_change;
837 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
838 case PR_CAPBSET_DROP:
839 error = cap_prctl_drop(new, arg2);
840 if (error < 0)
841 goto error;
842 goto changed;
845 * The next four prctl's remain to assist with transitioning a
846 * system from legacy UID=0 based privilege (when filesystem
847 * capabilities are not in use) to a system using filesystem
848 * capabilities only - as the POSIX.1e draft intended.
850 * Note:
852 * PR_SET_SECUREBITS =
853 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
854 * | issecure_mask(SECURE_NOROOT)
855 * | issecure_mask(SECURE_NOROOT_LOCKED)
856 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
857 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
859 * will ensure that the current process and all of its
860 * children will be locked into a pure
861 * capability-based-privilege environment.
863 case PR_SET_SECUREBITS:
864 error = -EPERM;
865 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
866 & (new->securebits ^ arg2)) /*[1]*/
867 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
868 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
869 || (cap_capable(current, current_cred(), CAP_SETPCAP,
870 SECURITY_CAP_AUDIT) != 0) /*[4]*/
872 * [1] no changing of bits that are locked
873 * [2] no unlocking of locks
874 * [3] no setting of unsupported bits
875 * [4] doing anything requires privilege (go read about
876 * the "sendmail capabilities bug")
879 /* cannot change a locked bit */
880 goto error;
881 new->securebits = arg2;
882 goto changed;
884 case PR_GET_SECUREBITS:
885 error = new->securebits;
886 goto no_change;
888 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
890 case PR_GET_KEEPCAPS:
891 if (issecure(SECURE_KEEP_CAPS))
892 error = 1;
893 goto no_change;
895 case PR_SET_KEEPCAPS:
896 error = -EINVAL;
897 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
898 goto error;
899 error = -EPERM;
900 if (issecure(SECURE_KEEP_CAPS_LOCKED))
901 goto error;
902 if (arg2)
903 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
904 else
905 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
906 goto changed;
908 default:
909 /* No functionality available - continue with default */
910 error = -ENOSYS;
911 goto error;
914 /* Functionality provided */
915 changed:
916 return commit_creds(new);
918 no_change:
919 error:
920 abort_creds(new);
921 return error;
925 * cap_syslog - Determine whether syslog function is permitted
926 * @type: Function requested
928 * Determine whether the current process is permitted to use a particular
929 * syslog function, returning 0 if permission is granted, -ve if not.
931 int cap_syslog(int type)
933 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
934 return -EPERM;
935 return 0;
939 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
940 * @mm: The VM space in which the new mapping is to be made
941 * @pages: The size of the mapping
943 * Determine whether the allocation of a new virtual mapping by the current
944 * task is permitted, returning 0 if permission is granted, -ve if not.
946 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
948 int cap_sys_admin = 0;
950 if (cap_capable(current, current_cred(), CAP_SYS_ADMIN,
951 SECURITY_CAP_NOAUDIT) == 0)
952 cap_sys_admin = 1;
953 return __vm_enough_memory(mm, pages, cap_sys_admin);