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 <asm/uaccess.h>
16 unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
17 kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
19 EXPORT_SYMBOL(securebits);
20 EXPORT_SYMBOL(cap_bset);
23 * This global lock protects task->cap_* for all tasks including current.
24 * Locking rule: acquire this prior to tasklist_lock.
26 spinlock_t task_capability_lock = SPIN_LOCK_UNLOCKED;
29 * For sys_getproccap() and sys_setproccap(), any of the three
30 * capability set pointers may be NULL -- indicating that that set is
31 * uninteresting and/or not to be changed.
35 * sys_capget - get the capabilities of a given process.
37 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
43 struct __user_cap_data_struct data;
45 if (get_user(version, &header->version))
48 if (version != _LINUX_CAPABILITY_VERSION) {
49 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
54 if (get_user(pid, &header->pid))
60 spin_lock(&task_capability_lock);
61 read_lock(&tasklist_lock);
63 if (pid && pid != current->pid) {
64 target = find_task_by_pid(pid);
72 ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
75 read_unlock(&tasklist_lock);
76 spin_unlock(&task_capability_lock);
78 if (!ret && copy_to_user(dataptr, &data, sizeof data))
85 * cap_set_pg - set capabilities for all processes in a given process
86 * group. We call this holding task_capability_lock and tasklist_lock.
88 static inline void cap_set_pg(int pgrp, kernel_cap_t *effective,
89 kernel_cap_t *inheritable,
90 kernel_cap_t *permitted)
94 do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
96 while_each_thread(g, target)
97 security_capset_set(target, effective, inheritable, permitted);
98 } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
102 * cap_set_all - set capabilities for all processes other than init
103 * and self. We call this holding task_capability_lock and tasklist_lock.
105 static inline void cap_set_all(kernel_cap_t *effective,
106 kernel_cap_t *inheritable,
107 kernel_cap_t *permitted)
111 do_each_thread(g, target) {
112 if (target == current || target->pid == 1)
114 security_capset_set(target, effective, inheritable, permitted);
115 } while_each_thread(g, target);
119 * sys_capset - set capabilities for a given process, all processes, or all
120 * processes in a given process group.
122 * The restrictions on setting capabilities are specified as:
124 * [pid is for the 'target' task. 'current' is the calling task.]
126 * I: any raised capabilities must be a subset of the (old current) permitted
127 * P: any raised capabilities must be a subset of the (old current) permitted
128 * E: must be set to a subset of (new target) permitted
130 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
132 kernel_cap_t inheritable, permitted, effective;
138 if (get_user(version, &header->version))
141 if (version != _LINUX_CAPABILITY_VERSION) {
142 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
147 if (get_user(pid, &header->pid))
150 if (pid && !capable(CAP_SETPCAP))
153 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
154 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
155 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
158 spin_lock(&task_capability_lock);
159 read_lock(&tasklist_lock);
161 if (pid > 0 && pid != current->pid) {
162 target = find_task_by_pid(pid);
172 if (security_capset_check(target, &effective, &inheritable, &permitted))
175 if (!cap_issubset(inheritable, cap_combine(target->cap_inheritable,
176 current->cap_permitted)))
179 /* verify restrictions on target's new Permitted set */
180 if (!cap_issubset(permitted, cap_combine(target->cap_permitted,
181 current->cap_permitted)))
184 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
185 if (!cap_issubset(effective, permitted))
190 /* having verified that the proposed changes are legal,
191 we now put them into effect. */
193 if (pid == -1) /* all procs other than current and init */
194 cap_set_all(&effective, &inheritable, &permitted);
196 else /* all procs in process group */
197 cap_set_pg(-pid, &effective, &inheritable, &permitted);
199 security_capset_set(target, &effective, &inheritable, &permitted);
203 read_unlock(&tasklist_lock);
204 spin_unlock(&task_capability_lock);