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 <asm/uaccess.h>
16 #include <linux/vs_base.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) {
106 while_each_thread(g, target) {
107 if (!security_capset_check(target, effective,
110 security_capset_set(target, effective,
117 } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
125 * cap_set_all - set capabilities for all processes other than init
126 * and self. We call this holding task_capability_lock and tasklist_lock.
128 static inline int cap_set_all(kernel_cap_t *effective,
129 kernel_cap_t *inheritable,
130 kernel_cap_t *permitted)
132 struct task_struct *g, *target;
136 do_each_thread(g, target) {
137 if (target == current || target->pid == 1)
140 if (security_capset_check(target, effective, inheritable,
144 security_capset_set(target, effective, inheritable, permitted);
145 } while_each_thread(g, target);
153 * sys_capset - set capabilities for a process or a group of processes
154 * @header: pointer to struct that contains capability version and
156 * @data: pointer to struct that contains the effective, permitted,
157 * and inheritable capabilities
159 * Set capabilities for a given process, all processes, or all
160 * processes in a given process group.
162 * The restrictions on setting capabilities are specified as:
164 * [pid is for the 'target' task. 'current' is the calling task.]
166 * I: any raised capabilities must be a subset of the (old current) permitted
167 * P: any raised capabilities must be a subset of the (old current) permitted
168 * E: must be set to a subset of (new target) permitted
170 * Returns 0 on success and < 0 on error.
172 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
174 kernel_cap_t inheritable, permitted, effective;
176 struct task_struct *target;
180 if (get_user(version, &header->version))
183 if (version != _LINUX_CAPABILITY_VERSION) {
184 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
189 if (get_user(pid, &header->pid))
192 if (pid && pid != current->pid && !capable(CAP_SETPCAP))
195 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
196 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
197 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
200 spin_lock(&task_capability_lock);
201 read_lock(&tasklist_lock);
203 if (pid > 0 && pid != current->pid) {
204 target = find_task_by_pid(pid);
214 /* having verified that the proposed changes are legal,
215 we now put them into effect. */
217 if (pid == -1) /* all procs other than current and init */
218 ret = cap_set_all(&effective, &inheritable, &permitted);
220 else /* all procs in process group */
221 ret = cap_set_pg(-pid, &effective, &inheritable,
224 ret = security_capset_check(target, &effective, &inheritable,
227 security_capset_set(target, &effective, &inheritable,
232 read_unlock(&tasklist_lock);
233 spin_unlock(&task_capability_lock);
238 int __capable(struct task_struct *t, int cap)
240 if (security_capable(t, cap) == 0) {
241 t->flags |= PF_SUPERPRIV;
246 EXPORT_SYMBOL(__capable);
250 /* here for now so we don't require task locking */
251 if (vx_check_bit(VXC_CAP_MASK, cap) && !vx_mcaps(1L << cap))
253 return __capable(current, cap);
255 EXPORT_SYMBOL(capable);