revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / kernel / capability.c
blob8cba9b27dffebc7be7a4b3e4aa730417c6575d14
1 /*
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>
8 */
10 #include <linux/capability.h>
11 #include <linux/mm.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 * Leveraged for setting/resetting capabilities
28 const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
29 const kernel_cap_t __cap_full_set = CAP_FULL_SET;
30 const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
33 * For sys_getproccap() and sys_setproccap(), any of the three
34 * capability set pointers may be NULL -- indicating that that set is
35 * uninteresting and/or not to be changed.
38 /**
39 * sys_capget - get the capabilities of a given process.
40 * @header: pointer to struct that contains capability version and
41 * target pid data
42 * @dataptr: pointer to struct that contains the effective, permitted,
43 * and inheritable capabilities that are returned
45 * Returns 0 on success and < 0 on error.
47 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
49 static int warned = 0;
50 int ret = 0;
51 pid_t pid;
52 __u32 version;
53 struct task_struct *target;
54 unsigned tocopy;
55 kernel_cap_t pE, pI, pP;
57 if (get_user(version, &header->version))
58 return -EFAULT;
60 switch (version) {
61 case _LINUX_CAPABILITY_VERSION_1:
62 if (warned < 5) {
63 warned++;
64 printk(KERN_INFO
65 "warning: process `%s' gets w/ old libcap\n",
66 current->comm);
68 tocopy = _LINUX_CAPABILITY_U32S_1;
69 break;
70 case _LINUX_CAPABILITY_VERSION_2:
71 tocopy = _LINUX_CAPABILITY_U32S_2;
72 break;
73 default:
74 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
75 return -EFAULT;
76 return -EINVAL;
79 if (get_user(pid, &header->pid))
80 return -EFAULT;
82 if (pid < 0)
83 return -EINVAL;
85 spin_lock(&task_capability_lock);
86 read_lock(&tasklist_lock);
88 if (pid && pid != task_pid_vnr(current)) {
89 target = find_task_by_vpid(pid);
90 if (!target) {
91 ret = -ESRCH;
92 goto out;
94 } else
95 target = current;
97 ret = security_capget(target, &pE, &pI, &pP);
99 out:
100 read_unlock(&tasklist_lock);
101 spin_unlock(&task_capability_lock);
103 if (!ret) {
104 struct __user_cap_data_struct kdata[_LINUX_CAPABILITY_U32S];
105 unsigned i;
107 for (i = 0; i < tocopy; i++) {
108 kdata[i].effective = pE.cap[i];
109 kdata[i].permitted = pP.cap[i];
110 kdata[i].inheritable = pI.cap[i];
112 while (i < _LINUX_CAPABILITY_U32S) {
113 if (pE.cap[i] || pP.cap[i] || pP.cap[i]) {
114 /* Cannot represent w/ legacy structure */
115 return -ERANGE;
117 i++;
120 if (copy_to_user(dataptr, kdata, tocopy
121 * sizeof(struct __user_cap_data_struct))) {
122 return -EFAULT;
126 return ret;
130 * cap_set_pg - set capabilities for all processes in a given process
131 * group. We call this holding task_capability_lock and tasklist_lock.
133 static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
134 kernel_cap_t *inheritable,
135 kernel_cap_t *permitted)
137 struct task_struct *g, *target;
138 int ret = -EPERM;
139 int found = 0;
140 struct pid *pgrp;
142 pgrp = find_vpid(pgrp_nr);
143 do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
144 target = g;
145 while_each_thread(g, target) {
146 if (!security_capset_check(target, effective,
147 inheritable,
148 permitted)) {
149 security_capset_set(target, effective,
150 inheritable,
151 permitted);
152 ret = 0;
154 found = 1;
156 } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
158 if (!found)
159 ret = 0;
160 return ret;
164 * cap_set_all - set capabilities for all processes other than init
165 * and self. We call this holding task_capability_lock and tasklist_lock.
167 static inline int cap_set_all(kernel_cap_t *effective,
168 kernel_cap_t *inheritable,
169 kernel_cap_t *permitted)
171 struct task_struct *g, *target;
172 int ret = -EPERM;
173 int found = 0;
175 do_each_thread(g, target) {
176 if (target == current || is_container_init(target->group_leader))
177 continue;
178 found = 1;
179 if (security_capset_check(target, effective, inheritable,
180 permitted))
181 continue;
182 ret = 0;
183 security_capset_set(target, effective, inheritable, permitted);
184 } while_each_thread(g, target);
186 if (!found)
187 ret = 0;
188 return ret;
192 * sys_capset - set capabilities for a process or a group of processes
193 * @header: pointer to struct that contains capability version and
194 * target pid data
195 * @data: pointer to struct that contains the effective, permitted,
196 * and inheritable capabilities
198 * Set capabilities for a given process, all processes, or all
199 * processes in a given process group.
201 * The restrictions on setting capabilities are specified as:
203 * [pid is for the 'target' task. 'current' is the calling task.]
205 * I: any raised capabilities must be a subset of the (old current) permitted
206 * P: any raised capabilities must be a subset of the (old current) permitted
207 * E: must be set to a subset of (new target) permitted
209 * Returns 0 on success and < 0 on error.
211 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
213 static int warned;
214 struct __user_cap_data_struct kdata[_LINUX_CAPABILITY_U32S];
215 unsigned i, tocopy;
216 kernel_cap_t inheritable, permitted, effective;
217 __u32 version;
218 struct task_struct *target;
219 int ret;
220 pid_t pid;
222 if (get_user(version, &header->version))
223 return -EFAULT;
225 switch (version) {
226 case _LINUX_CAPABILITY_VERSION_1:
227 if (warned < 5) {
228 char name[sizeof(current->comm)];
229 warned++;
230 printk(KERN_INFO
231 "warning: process `%s' sets w/ old libcap\n",
232 get_task_comm(name, current));
234 tocopy = _LINUX_CAPABILITY_U32S_1;
235 break;
236 case _LINUX_CAPABILITY_VERSION_2:
237 tocopy = _LINUX_CAPABILITY_U32S_2;
238 break;
239 default:
240 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
241 return -EFAULT;
242 return -EINVAL;
245 if (get_user(pid, &header->pid))
246 return -EFAULT;
248 if (pid && pid != task_pid_vnr(current) && !capable(CAP_SETPCAP))
249 return -EPERM;
251 if (copy_from_user(&kdata, data, tocopy
252 * sizeof(struct __user_cap_data_struct))) {
253 return -EFAULT;
256 for (i = 0; i < tocopy; i++) {
257 effective.cap[i] = kdata[i].effective;
258 permitted.cap[i] = kdata[i].permitted;
259 inheritable.cap[i] = kdata[i].inheritable;
261 while (i < _LINUX_CAPABILITY_U32S) {
262 effective.cap[i] = 0;
263 permitted.cap[i] = 0;
264 inheritable.cap[i] = 0;
265 i++;
268 spin_lock(&task_capability_lock);
269 read_lock(&tasklist_lock);
271 if (pid > 0 && pid != task_pid_vnr(current)) {
272 target = find_task_by_vpid(pid);
273 if (!target) {
274 ret = -ESRCH;
275 goto out;
277 } else
278 target = current;
280 ret = 0;
282 /* having verified that the proposed changes are legal,
283 we now put them into effect. */
284 if (pid < 0) {
285 if (pid == -1) /* all procs other than current and init */
286 ret = cap_set_all(&effective, &inheritable, &permitted);
288 else /* all procs in process group */
289 ret = cap_set_pg(-pid, &effective, &inheritable,
290 &permitted);
291 } else {
292 ret = security_capset_check(target, &effective, &inheritable,
293 &permitted);
294 if (!ret)
295 security_capset_set(target, &effective, &inheritable,
296 &permitted);
299 out:
300 read_unlock(&tasklist_lock);
301 spin_unlock(&task_capability_lock);
303 return ret;
306 int __capable(struct task_struct *t, int cap)
308 if (security_capable(t, cap) == 0) {
309 t->flags |= PF_SUPERPRIV;
310 return 1;
312 return 0;
315 int capable(int cap)
317 return __capable(current, cap);
319 EXPORT_SYMBOL(capable);