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 <linux/pid_namespace.h>
16 #include <asm/uaccess.h>
19 * This lock protects task->cap_* for all tasks including current.
20 * Locking rule: acquire this prior to tasklist_lock.
22 static DEFINE_SPINLOCK(task_capability_lock);
25 * For sys_getproccap() and sys_setproccap(), any of the three
26 * capability set pointers may be NULL -- indicating that that set is
27 * uninteresting and/or not to be changed.
31 * sys_capget - get the capabilities of a given process.
32 * @header: pointer to struct that contains capability version and
34 * @dataptr: pointer to struct that contains the effective, permitted,
35 * and inheritable capabilities that are returned
37 * Returns 0 on success and < 0 on error.
39 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
44 struct task_struct *target;
45 struct __user_cap_data_struct data;
47 if (get_user(version, &header->version))
50 if (version != _LINUX_CAPABILITY_VERSION) {
51 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
56 if (get_user(pid, &header->pid))
62 spin_lock(&task_capability_lock);
63 read_lock(&tasklist_lock);
65 if (pid && pid != task_pid_vnr(current)) {
66 target = find_task_by_pid_ns(pid,
67 current->nsproxy->pid_ns);
75 ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
78 read_unlock(&tasklist_lock);
79 spin_unlock(&task_capability_lock);
81 if (!ret && copy_to_user(dataptr, &data, sizeof data))
88 * cap_set_pg - set capabilities for all processes in a given process
89 * group. We call this holding task_capability_lock and tasklist_lock.
91 static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
92 kernel_cap_t *inheritable,
93 kernel_cap_t *permitted)
95 struct task_struct *g, *target;
100 pgrp = find_pid_ns(pgrp_nr, current->nsproxy->pid_ns);
101 do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
103 while_each_thread(g, target) {
104 if (!security_capset_check(target, effective,
107 security_capset_set(target, effective,
114 } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
122 * cap_set_all - set capabilities for all processes other than init
123 * and self. We call this holding task_capability_lock and tasklist_lock.
125 static inline int cap_set_all(kernel_cap_t *effective,
126 kernel_cap_t *inheritable,
127 kernel_cap_t *permitted)
129 struct task_struct *g, *target;
133 do_each_thread(g, target) {
134 if (target == current || is_container_init(target->group_leader))
137 if (security_capset_check(target, effective, inheritable,
141 security_capset_set(target, effective, inheritable, permitted);
142 } while_each_thread(g, target);
150 * sys_capset - set capabilities for a process or a group of processes
151 * @header: pointer to struct that contains capability version and
153 * @data: pointer to struct that contains the effective, permitted,
154 * and inheritable capabilities
156 * Set capabilities for a given process, all processes, or all
157 * processes in a given process group.
159 * The restrictions on setting capabilities are specified as:
161 * [pid is for the 'target' task. 'current' is the calling task.]
163 * I: any raised capabilities must be a subset of the (old current) permitted
164 * P: any raised capabilities must be a subset of the (old current) permitted
165 * E: must be set to a subset of (new target) permitted
167 * Returns 0 on success and < 0 on error.
169 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
171 kernel_cap_t inheritable, permitted, effective;
173 struct task_struct *target;
177 if (get_user(version, &header->version))
180 if (version != _LINUX_CAPABILITY_VERSION) {
181 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
186 if (get_user(pid, &header->pid))
189 if (pid && pid != task_pid_vnr(current) && !capable(CAP_SETPCAP))
192 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
193 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
194 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
197 spin_lock(&task_capability_lock);
198 read_lock(&tasklist_lock);
200 if (pid > 0 && pid != task_pid_vnr(current)) {
201 target = find_task_by_pid_ns(pid,
202 current->nsproxy->pid_ns);
212 /* having verified that the proposed changes are legal,
213 we now put them into effect. */
215 if (pid == -1) /* all procs other than current and init */
216 ret = cap_set_all(&effective, &inheritable, &permitted);
218 else /* all procs in process group */
219 ret = cap_set_pg(-pid, &effective, &inheritable,
222 ret = security_capset_check(target, &effective, &inheritable,
225 security_capset_set(target, &effective, &inheritable,
230 read_unlock(&tasklist_lock);
231 spin_unlock(&task_capability_lock);
236 int __capable(struct task_struct *t, int cap)
238 if (security_capable(t, cap) == 0) {
239 t->flags |= PF_SUPERPRIV;
247 return __capable(current, cap);
249 EXPORT_SYMBOL(capable);