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_cvirt.h>
16 #include <asm/uaccess.h>
17 #include <linux/vs_base.h>
19 unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
20 kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
22 EXPORT_SYMBOL(securebits);
23 EXPORT_SYMBOL(cap_bset);
26 * This lock protects task->cap_* for all tasks including current.
27 * Locking rule: acquire this prior to tasklist_lock.
29 static DEFINE_SPINLOCK(task_capability_lock);
32 * For sys_getproccap() and sys_setproccap(), any of the three
33 * capability set pointers may be NULL -- indicating that that set is
34 * uninteresting and/or not to be changed.
38 * sys_capget - get the capabilities of a given process.
39 * @header: pointer to struct that contains capability version and
41 * @dataptr: pointer to struct that contains the effective, permitted,
42 * and inheritable capabilities that are returned
44 * Returns 0 on success and < 0 on error.
46 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
52 struct __user_cap_data_struct data;
54 if (get_user(version, &header->version))
57 if (version != _LINUX_CAPABILITY_VERSION) {
58 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
63 if (get_user(pid, &header->pid))
69 spin_lock(&task_capability_lock);
70 read_lock(&tasklist_lock);
72 if (pid && pid != current->pid) {
73 target = find_task_by_pid(pid);
81 ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
84 read_unlock(&tasklist_lock);
85 spin_unlock(&task_capability_lock);
87 if (!ret && copy_to_user(dataptr, &data, sizeof data))
94 * cap_set_pg - set capabilities for all processes in a given process
95 * group. We call this holding task_capability_lock and tasklist_lock.
97 static inline int cap_set_pg(int pgrp, kernel_cap_t *effective,
98 kernel_cap_t *inheritable,
99 kernel_cap_t *permitted)
105 do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
107 while_each_thread(g, target) {
108 if (!security_capset_check(target, effective,
111 security_capset_set(target, effective,
118 } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
126 * cap_set_all - set capabilities for all processes other than init
127 * and self. We call this holding task_capability_lock and tasklist_lock.
129 static inline int cap_set_all(kernel_cap_t *effective,
130 kernel_cap_t *inheritable,
131 kernel_cap_t *permitted)
137 do_each_thread(g, target) {
138 if (target == current || target->pid == 1)
141 if (security_capset_check(target, effective, inheritable,
145 security_capset_set(target, effective, inheritable, permitted);
146 } while_each_thread(g, target);
154 * sys_capset - set capabilities for a process or a group of processes
155 * @header: pointer to struct that contains capability version and
157 * @data: pointer to struct that contains the effective, permitted,
158 * and inheritable capabilities
160 * Set capabilities for a given process, all processes, or all
161 * processes in a given process group.
163 * The restrictions on setting capabilities are specified as:
165 * [pid is for the 'target' task. 'current' is the calling task.]
167 * I: any raised capabilities must be a subset of the (old current) permitted
168 * P: any raised capabilities must be a subset of the (old current) permitted
169 * E: must be set to a subset of (new target) permitted
171 * Returns 0 on success and < 0 on error.
173 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
175 kernel_cap_t inheritable, permitted, effective;
181 if (get_user(version, &header->version))
184 if (version != _LINUX_CAPABILITY_VERSION) {
185 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
190 if (get_user(pid, &header->pid))
193 if (pid && pid != current->pid && !capable(CAP_SETPCAP))
196 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
197 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
198 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
201 spin_lock(&task_capability_lock);
202 read_lock(&tasklist_lock);
204 if (pid > 0 && pid != current->pid) {
205 target = find_task_by_pid(pid);
215 /* having verified that the proposed changes are legal,
216 we now put them into effect. */
218 if (pid == -1) /* all procs other than current and init */
219 ret = cap_set_all(&effective, &inheritable, &permitted);
221 else /* all procs in process group */
222 ret = cap_set_pg(-pid, &effective, &inheritable,
225 ret = security_capset_check(target, &effective, &inheritable,
228 security_capset_set(target, &effective, &inheritable,
233 read_unlock(&tasklist_lock);
234 spin_unlock(&task_capability_lock);
239 int __capable(struct task_struct *t, int cap)
241 if (security_capable(t, cap) == 0) {
242 t->flags |= PF_SUPERPRIV;
247 EXPORT_SYMBOL(__capable);
251 /* here for now so we don't require task locking */
252 if (vx_check_bit(VXC_CAP_MASK, cap) && !vx_mcaps(1L << cap))
254 return __capable(current, cap);
256 EXPORT_SYMBOL(capable);