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>
11 #include <linux/module.h>
12 #include <linux/security.h>
13 #include <linux/vs_cvirt.h>
14 #include <linux/syscalls.h>
15 #include <linux/vs_cvirt.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.
39 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
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 != current->pid) {
66 target = find_task_by_pid(pid);
74 ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
77 read_unlock(&tasklist_lock);
78 spin_unlock(&task_capability_lock);
80 if (!ret && copy_to_user(dataptr, &data, sizeof data))
87 * cap_set_pg - set capabilities for all processes in a given process
88 * group. We call this holding task_capability_lock and tasklist_lock.
90 static inline int cap_set_pg(int pgrp, kernel_cap_t *effective,
91 kernel_cap_t *inheritable,
92 kernel_cap_t *permitted)
98 do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
100 while_each_thread(g, target) {
101 if (!security_capset_check(target, effective,
104 security_capset_set(target, effective,
111 } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
119 * cap_set_all - set capabilities for all processes other than init
120 * and self. We call this holding task_capability_lock and tasklist_lock.
122 static inline int cap_set_all(kernel_cap_t *effective,
123 kernel_cap_t *inheritable,
124 kernel_cap_t *permitted)
130 do_each_thread(g, target) {
131 if (target == current || target->pid == 1)
134 if (security_capset_check(target, effective, inheritable,
138 security_capset_set(target, effective, inheritable, permitted);
139 } while_each_thread(g, target);
147 * sys_capset - set capabilities for a given process, all processes, or all
148 * processes in a given process group.
150 * The restrictions on setting capabilities are specified as:
152 * [pid is for the 'target' task. 'current' is the calling task.]
154 * I: any raised capabilities must be a subset of the (old current) permitted
155 * P: any raised capabilities must be a subset of the (old current) permitted
156 * E: must be set to a subset of (new target) permitted
158 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
160 kernel_cap_t inheritable, permitted, effective;
166 if (get_user(version, &header->version))
169 if (version != _LINUX_CAPABILITY_VERSION) {
170 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
175 if (get_user(pid, &header->pid))
178 if (pid && pid != current->pid && !capable(CAP_SETPCAP))
181 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
182 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
183 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
186 spin_lock(&task_capability_lock);
187 read_lock(&tasklist_lock);
189 if (pid > 0 && pid != current->pid) {
190 target = find_task_by_pid(pid);
200 /* having verified that the proposed changes are legal,
201 we now put them into effect. */
203 if (pid == -1) /* all procs other than current and init */
204 ret = cap_set_all(&effective, &inheritable, &permitted);
206 else /* all procs in process group */
207 ret = cap_set_pg(-pid, &effective, &inheritable,
210 ret = security_capset_check(target, &effective, &inheritable,
213 security_capset_set(target, &effective, &inheritable,
218 read_unlock(&tasklist_lock);
219 spin_unlock(&task_capability_lock);