Import 2.3.9
[davej-history.git] / kernel / capability.c
bloba4a1a3d030e710e06460521f591e7f76eed53be4
1 /*
2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
5 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@transmeta.com>
6 */
8 #include <linux/mm.h>
9 #include <asm/uaccess.h>
11 /* Note: never hold tasklist_lock while spinning for this one */
12 spinlock_t task_capability_lock;
15 * For sys_getproccap() and sys_setproccap(), any of the three
16 * capability set pointers may be NULL -- indicating that that set is
17 * uninteresting and/or not to be changed.
20 asmlinkage int sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
22 int error, pid;
23 __u32 version;
24 struct task_struct *target;
25 struct __user_cap_data_struct data;
27 if (get_user(version, &header->version))
28 return -EFAULT;
30 error = -EINVAL;
31 if (version != _LINUX_CAPABILITY_VERSION) {
32 version = _LINUX_CAPABILITY_VERSION;
33 if (put_user(version, &header->version))
34 error = -EFAULT;
35 return error;
38 if (get_user(pid, &header->pid))
39 return -EFAULT;
41 if (pid < 0)
42 return -EINVAL;
44 error = 0;
46 spin_lock(&task_capability_lock);
48 if (pid && pid != current->pid) {
49 read_lock(&tasklist_lock);
50 target = find_task_by_pid(pid); /* identify target of query */
51 if (!target)
52 error = -ESRCH;
53 } else {
54 target = current;
57 if (!error) {
58 data.permitted = cap_t(target->cap_permitted);
59 data.inheritable = cap_t(target->cap_inheritable);
60 data.effective = cap_t(target->cap_effective);
63 if (target != current)
64 read_unlock(&tasklist_lock);
65 spin_unlock(&task_capability_lock);
67 if (!error) {
68 if (copy_to_user(dataptr, &data, sizeof data))
69 return -EFAULT;
72 return error;
75 /* set capabilities for all processes in a given process group */
77 static void cap_set_pg(int pgrp,
78 kernel_cap_t *effective,
79 kernel_cap_t *inheritable,
80 kernel_cap_t *permitted)
82 struct task_struct *target;
84 /* FIXME: do we need to have a write lock here..? */
85 read_lock(&tasklist_lock);
86 for_each_task(target) {
87 if (target->pgrp != pgrp)
88 continue;
89 target->cap_effective = *effective;
90 target->cap_inheritable = *inheritable;
91 target->cap_permitted = *permitted;
93 read_unlock(&tasklist_lock);
96 /* set capabilities for all processes other than 1 and self */
98 static void cap_set_all(kernel_cap_t *effective,
99 kernel_cap_t *inheritable,
100 kernel_cap_t *permitted)
102 struct task_struct *target;
104 /* FIXME: do we need to have a write lock here..? */
105 read_lock(&tasklist_lock);
106 /* ALL means everyone other than self or 'init' */
107 for_each_task(target) {
108 if (target == current || target->pid == 1)
109 continue;
110 target->cap_effective = *effective;
111 target->cap_inheritable = *inheritable;
112 target->cap_permitted = *permitted;
114 read_unlock(&tasklist_lock);
118 * The restrictions on setting capabilities are specified as:
120 * [pid is for the 'target' task. 'current' is the calling task.]
122 * I: any raised capabilities must be a subset of the (old current) Permitted
123 * P: any raised capabilities must be a subset of the (old current) permitted
124 * E: must be set to a subset of (new target) Permitted
127 asmlinkage int sys_capset(cap_user_header_t header, const cap_user_data_t data)
129 kernel_cap_t inheritable, permitted, effective;
130 __u32 version;
131 struct task_struct *target;
132 int error, pid;
134 if (get_user(version, &header->version))
135 return -EFAULT;
137 if (version != _LINUX_CAPABILITY_VERSION) {
138 version = _LINUX_CAPABILITY_VERSION;
139 if (put_user(version, &header->version))
140 return -EFAULT;
141 return -EINVAL;
144 if (get_user(pid, &header->pid))
145 return -EFAULT;
147 if (pid && !capable(CAP_SETPCAP))
148 return -EPERM;
150 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
151 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
152 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
153 return -EFAULT;
155 error = -EPERM;
156 spin_lock(&task_capability_lock);
158 if (pid > 0 && pid != current->pid) {
159 read_lock(&tasklist_lock);
160 target = find_task_by_pid(pid); /* identify target of query */
161 if (!target) {
162 error = -ESRCH;
163 goto out;
165 } else {
166 target = current;
170 /* verify restrictions on target's new Inheritable set */
171 if (!cap_issubset(inheritable,
172 cap_combine(target->cap_inheritable,
173 current->cap_permitted))) {
174 goto out;
177 /* verify restrictions on target's new Permitted set */
178 if (!cap_issubset(permitted,
179 cap_combine(target->cap_permitted,
180 current->cap_permitted))) {
181 goto out;
184 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
185 if (!cap_issubset(effective, permitted)) {
186 goto out;
189 /* having verified that the proposed changes are legal,
190 we now put them into effect. */
191 error = 0;
193 if (pid < 0) {
194 if (pid == -1) /* all procs other than current and init */
195 cap_set_all(&effective, &inheritable, &permitted);
197 else /* all procs in process group */
198 cap_set_pg(-pid, &effective, &inheritable, &permitted);
199 goto spin_out;
200 } else {
201 /* FIXME: do we need to have a write lock here..? */
202 target->cap_effective = effective;
203 target->cap_inheritable = inheritable;
204 target->cap_permitted = permitted;
207 out:
208 if (target != current) {
209 read_unlock(&tasklist_lock);
211 spin_out:
212 spin_unlock(&task_capability_lock);
213 return error;