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>
17 #include <asm/uaccess.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 global lock protects task->cap_* for all tasks including current.
27 * Locking rule: acquire this prior to tasklist_lock.
29 spinlock_t task_capability_lock = SPIN_LOCK_UNLOCKED;
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.
40 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
46 struct __user_cap_data_struct data;
48 if (get_user(version, &header->version))
51 if (version != _LINUX_CAPABILITY_VERSION) {
52 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
57 if (get_user(pid, &header->pid))
63 spin_lock(&task_capability_lock);
64 read_lock(&tasklist_lock);
66 if (pid && pid != current->pid) {
67 target = find_task_by_pid(pid);
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 void cap_set_pg(int pgrp, kernel_cap_t *effective,
92 kernel_cap_t *inheritable,
93 kernel_cap_t *permitted)
97 do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
99 while_each_thread(g, target)
100 security_capset_set(target, effective, inheritable, permitted);
101 } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
105 * cap_set_all - set capabilities for all processes other than init
106 * and self. We call this holding task_capability_lock and tasklist_lock.
108 static inline void cap_set_all(kernel_cap_t *effective,
109 kernel_cap_t *inheritable,
110 kernel_cap_t *permitted)
114 do_each_thread(g, target) {
115 if (target == current || target->pid == 1)
117 security_capset_set(target, effective, inheritable, permitted);
118 } while_each_thread(g, target);
122 * sys_capset - set capabilities for a given process, all processes, or all
123 * processes in a given process group.
125 * The restrictions on setting capabilities are specified as:
127 * [pid is for the 'target' task. 'current' is the calling task.]
129 * I: any raised capabilities must be a subset of the (old current) permitted
130 * P: any raised capabilities must be a subset of the (old current) permitted
131 * E: must be set to a subset of (new target) permitted
133 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
135 kernel_cap_t inheritable, permitted, effective;
141 if (get_user(version, &header->version))
144 if (version != _LINUX_CAPABILITY_VERSION) {
145 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
150 if (get_user(pid, &header->pid))
153 if (pid && !capable(CAP_SETPCAP))
156 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
157 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
158 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
161 spin_lock(&task_capability_lock);
162 read_lock(&tasklist_lock);
164 if (pid > 0 && pid != current->pid) {
165 target = find_task_by_pid(pid);
175 if (security_capset_check(target, &effective, &inheritable, &permitted))
178 if (!cap_issubset(inheritable, cap_combine(target->cap_inheritable,
179 current->cap_permitted)))
182 /* verify restrictions on target's new Permitted set */
183 if (!cap_issubset(permitted, cap_combine(target->cap_permitted,
184 current->cap_permitted)))
187 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
188 if (!cap_issubset(effective, permitted))
193 /* having verified that the proposed changes are legal,
194 we now put them into effect. */
196 if (pid == -1) /* all procs other than current and init */
197 cap_set_all(&effective, &inheritable, &permitted);
199 else /* all procs in process group */
200 cap_set_pg(-pid, &effective, &inheritable, &permitted);
202 security_capset_set(target, &effective, &inheritable, &permitted);
206 read_unlock(&tasklist_lock);
207 spin_unlock(&task_capability_lock);