2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
6 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@transmeta.com>
7 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
10 #include <linux/capability.h>
12 #include <linux/module.h>
13 #include <linux/security.h>
14 #include <linux/syscalls.h>
15 #include <linux/vs_context.h>
16 #include <asm/uaccess.h>
18 unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
19 kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
21 EXPORT_SYMBOL(securebits);
22 EXPORT_SYMBOL(cap_bset);
25 * This lock protects task->cap_* for all tasks including current.
26 * Locking rule: acquire this prior to tasklist_lock.
28 static DEFINE_SPINLOCK(task_capability_lock);
31 * For sys_getproccap() and sys_setproccap(), any of the three
32 * capability set pointers may be NULL -- indicating that that set is
33 * uninteresting and/or not to be changed.
37 * sys_capget - get the capabilities of a given process.
38 * @header: pointer to struct that contains capability version and
40 * @dataptr: pointer to struct that contains the effective, permitted,
41 * and inheritable capabilities that are returned
43 * Returns 0 on success and < 0 on error.
45 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
50 struct task_struct *target;
51 struct __user_cap_data_struct data;
53 if (get_user(version, &header->version))
56 if (version != _LINUX_CAPABILITY_VERSION) {
57 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
62 if (get_user(pid, &header->pid))
68 spin_lock(&task_capability_lock);
69 read_lock(&tasklist_lock);
71 if (pid && pid != current->pid) {
72 target = find_task_by_pid(pid);
80 ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
83 read_unlock(&tasklist_lock);
84 spin_unlock(&task_capability_lock);
86 if (!ret && copy_to_user(dataptr, &data, sizeof data))
93 * cap_set_pg - set capabilities for all processes in a given process
94 * group. We call this holding task_capability_lock and tasklist_lock.
96 static inline int cap_set_pg(int pgrp, kernel_cap_t *effective,
97 kernel_cap_t *inheritable,
98 kernel_cap_t *permitted)
100 struct task_struct *g, *target;
104 do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
105 if (!vx_check(g->xid, VS_ADMIN_P | VS_IDENT))
108 while_each_thread(g, target) {
109 if (!security_capset_check(target, effective,
112 security_capset_set(target, effective,
119 } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
127 * cap_set_all - set capabilities for all processes other than init
128 * and self. We call this holding task_capability_lock and tasklist_lock.
130 static inline int cap_set_all(kernel_cap_t *effective,
131 kernel_cap_t *inheritable,
132 kernel_cap_t *permitted)
134 struct task_struct *g, *target;
138 do_each_thread(g, target) {
139 if (target == current || is_init(target))
142 if (security_capset_check(target, effective, inheritable,
146 security_capset_set(target, effective, inheritable, permitted);
147 } while_each_thread(g, target);
155 * sys_capset - set capabilities for a process or a group of processes
156 * @header: pointer to struct that contains capability version and
158 * @data: pointer to struct that contains the effective, permitted,
159 * and inheritable capabilities
161 * Set capabilities for a given process, all processes, or all
162 * processes in a given process group.
164 * The restrictions on setting capabilities are specified as:
166 * [pid is for the 'target' task. 'current' is the calling task.]
168 * I: any raised capabilities must be a subset of the (old current) permitted
169 * P: any raised capabilities must be a subset of the (old current) permitted
170 * E: must be set to a subset of (new target) permitted
172 * Returns 0 on success and < 0 on error.
174 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
176 kernel_cap_t inheritable, permitted, effective;
178 struct task_struct *target;
182 if (get_user(version, &header->version))
185 if (version != _LINUX_CAPABILITY_VERSION) {
186 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
191 if (get_user(pid, &header->pid))
194 if (pid && pid != current->pid && !capable(CAP_SETPCAP))
197 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
198 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
199 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
202 spin_lock(&task_capability_lock);
203 read_lock(&tasklist_lock);
205 if (pid > 0 && pid != current->pid) {
206 target = find_task_by_pid(pid);
216 /* having verified that the proposed changes are legal,
217 we now put them into effect. */
219 if (pid == -1) /* all procs other than current and init */
220 ret = cap_set_all(&effective, &inheritable, &permitted);
222 else /* all procs in process group */
223 ret = cap_set_pg(-pid, &effective, &inheritable,
226 ret = security_capset_check(target, &effective, &inheritable,
229 security_capset_set(target, &effective, &inheritable,
234 read_unlock(&tasklist_lock);
235 spin_unlock(&task_capability_lock);
240 int __capable(struct task_struct *t, int cap)
242 if (security_capable(t, cap) == 0) {
243 t->flags |= PF_SUPERPRIV;
248 EXPORT_SYMBOL(__capable);
250 #include <linux/vserver/base.h>
253 /* here for now so we don't require task locking */
254 if (vs_check_bit(VXC_CAP_MASK, cap) && !vx_mcaps(1L << cap))
256 return __capable(current, cap);
258 EXPORT_SYMBOL(capable);