2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
6 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org>
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>
18 * This lock protects task->cap_* for all tasks including current.
19 * Locking rule: acquire this prior to tasklist_lock.
21 static DEFINE_SPINLOCK(task_capability_lock);
24 * For sys_getproccap() and sys_setproccap(), any of the three
25 * capability set pointers may be NULL -- indicating that that set is
26 * uninteresting and/or not to be changed.
30 * sys_capget - get the capabilities of a given process.
31 * @header: pointer to struct that contains capability version and
33 * @dataptr: pointer to struct that contains the effective, permitted,
34 * and inheritable capabilities that are returned
36 * Returns 0 on success and < 0 on error.
38 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
43 struct task_struct *target;
44 struct __user_cap_data_struct data;
46 if (get_user(version, &header->version))
49 if (version != _LINUX_CAPABILITY_VERSION) {
50 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
55 if (get_user(pid, &header->pid))
61 spin_lock(&task_capability_lock);
62 read_lock(&tasklist_lock);
64 if (pid && pid != current->pid) {
65 target = find_task_by_pid(pid);
73 ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
76 read_unlock(&tasklist_lock);
77 spin_unlock(&task_capability_lock);
79 if (!ret && copy_to_user(dataptr, &data, sizeof data))
86 * cap_set_pg - set capabilities for all processes in a given process
87 * group. We call this holding task_capability_lock and tasklist_lock.
89 static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
90 kernel_cap_t *inheritable,
91 kernel_cap_t *permitted)
93 struct task_struct *g, *target;
98 pgrp = find_pid(pgrp_nr);
99 do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
101 while_each_thread(g, target) {
102 if (!security_capset_check(target, effective,
105 security_capset_set(target, effective,
112 } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
120 * cap_set_all - set capabilities for all processes other than init
121 * and self. We call this holding task_capability_lock and tasklist_lock.
123 static inline int cap_set_all(kernel_cap_t *effective,
124 kernel_cap_t *inheritable,
125 kernel_cap_t *permitted)
127 struct task_struct *g, *target;
131 do_each_thread(g, target) {
132 if (target == current || is_init(target))
135 if (security_capset_check(target, effective, inheritable,
139 security_capset_set(target, effective, inheritable, permitted);
140 } while_each_thread(g, target);
148 * sys_capset - set capabilities for a process or a group of processes
149 * @header: pointer to struct that contains capability version and
151 * @data: pointer to struct that contains the effective, permitted,
152 * and inheritable capabilities
154 * Set capabilities for a given process, all processes, or all
155 * processes in a given process group.
157 * The restrictions on setting capabilities are specified as:
159 * [pid is for the 'target' task. 'current' is the calling task.]
161 * I: any raised capabilities must be a subset of the (old current) permitted
162 * P: any raised capabilities must be a subset of the (old current) permitted
163 * E: must be set to a subset of (new target) permitted
165 * Returns 0 on success and < 0 on error.
167 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
169 kernel_cap_t inheritable, permitted, effective;
171 struct task_struct *target;
175 if (get_user(version, &header->version))
178 if (version != _LINUX_CAPABILITY_VERSION) {
179 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
184 if (get_user(pid, &header->pid))
187 if (pid && pid != current->pid && !capable(CAP_SETPCAP))
190 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
191 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
192 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
195 spin_lock(&task_capability_lock);
196 read_lock(&tasklist_lock);
198 if (pid > 0 && pid != current->pid) {
199 target = find_task_by_pid(pid);
209 /* having verified that the proposed changes are legal,
210 we now put them into effect. */
212 if (pid == -1) /* all procs other than current and init */
213 ret = cap_set_all(&effective, &inheritable, &permitted);
215 else /* all procs in process group */
216 ret = cap_set_pg(-pid, &effective, &inheritable,
219 ret = security_capset_check(target, &effective, &inheritable,
222 security_capset_set(target, &effective, &inheritable,
227 read_unlock(&tasklist_lock);
228 spin_unlock(&task_capability_lock);
233 int __capable(struct task_struct *t, int cap)
235 if (security_capable(t, cap) == 0) {
236 t->flags |= PF_SUPERPRIV;
244 return __capable(current, cap);
246 EXPORT_SYMBOL(capable);