- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.git] / kernel / capability.c
1 /*
2  * linux/kernel/capability.c
3  *
4  * Copyright (C) 1997  Andrew Main <zefram@fysh.org>
5  *
6  * Integrated into 2.1.97+,  Andrew G. Morgan <morgan@kernel.org>
7  * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
8  */
9
10 #include <linux/audit.h>
11 #include <linux/capability.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/security.h>
15 #include <linux/syscalls.h>
16 #include <linux/pid_namespace.h>
17 #include <asm/uaccess.h>
18 #include "cred-internals.h"
19
20 /*
21  * Leveraged for setting/resetting capabilities
22  */
23
24 const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
25 const kernel_cap_t __cap_full_set = CAP_FULL_SET;
26 const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
27
28 EXPORT_SYMBOL(__cap_empty_set);
29 EXPORT_SYMBOL(__cap_full_set);
30 EXPORT_SYMBOL(__cap_init_eff_set);
31
32 int file_caps_enabled;
33
34 static int __init file_caps_disable(char *str)
35 {
36         file_caps_enabled = 0;
37         return 1;
38 }
39 __setup("no_file_caps", file_caps_disable);
40
41 static int __init file_caps_enable(char *str)
42 {
43         file_caps_enabled = 1;
44         return 1;
45 }
46 __setup("file_caps", file_caps_enable);
47
48 /*
49  * More recent versions of libcap are available from:
50  *
51  *   http://www.kernel.org/pub/linux/libs/security/linux-privs/
52  */
53
54 static void warn_legacy_capability_use(void)
55 {
56         static int warned;
57         if (!warned) {
58                 char name[sizeof(current->comm)];
59
60                 printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
61                        " (legacy support in use)\n",
62                        get_task_comm(name, current));
63                 warned = 1;
64         }
65 }
66
67 /*
68  * Version 2 capabilities worked fine, but the linux/capability.h file
69  * that accompanied their introduction encouraged their use without
70  * the necessary user-space source code changes. As such, we have
71  * created a version 3 with equivalent functionality to version 2, but
72  * with a header change to protect legacy source code from using
73  * version 2 when it wanted to use version 1. If your system has code
74  * that trips the following warning, it is using version 2 specific
75  * capabilities and may be doing so insecurely.
76  *
77  * The remedy is to either upgrade your version of libcap (to 2.10+,
78  * if the application is linked against it), or recompile your
79  * application with modern kernel headers and this warning will go
80  * away.
81  */
82
83 static void warn_deprecated_v2(void)
84 {
85         static int warned;
86
87         if (!warned) {
88                 char name[sizeof(current->comm)];
89
90                 printk(KERN_INFO "warning: `%s' uses deprecated v2"
91                        " capabilities in a way that may be insecure.\n",
92                        get_task_comm(name, current));
93                 warned = 1;
94         }
95 }
96
97 /*
98  * Version check. Return the number of u32s in each capability flag
99  * array, or a negative value on error.
100  */
101 static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
102 {
103         __u32 version;
104
105         if (get_user(version, &header->version))
106                 return -EFAULT;
107
108         switch (version) {
109         case _LINUX_CAPABILITY_VERSION_1:
110                 warn_legacy_capability_use();
111                 *tocopy = _LINUX_CAPABILITY_U32S_1;
112                 break;
113         case _LINUX_CAPABILITY_VERSION_2:
114                 warn_deprecated_v2();
115                 /*
116                  * fall through - v3 is otherwise equivalent to v2.
117                  */
118         case _LINUX_CAPABILITY_VERSION_3:
119                 *tocopy = _LINUX_CAPABILITY_U32S_3;
120                 break;
121         default:
122                 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
123                         return -EFAULT;
124                 return -EINVAL;
125         }
126
127         return 0;
128 }
129
130 /*
131  * The only thing that can change the capabilities of the current
132  * process is the current process. As such, we can't be in this code
133  * at the same time as we are in the process of setting capabilities
134  * in this process. The net result is that we can limit our use of
135  * locks to when we are reading the caps of another process.
136  */
137 static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
138                                      kernel_cap_t *pIp, kernel_cap_t *pPp)
139 {
140         int ret;
141
142         if (pid && (pid != task_pid_vnr(current))) {
143                 struct task_struct *target;
144
145                 read_lock(&tasklist_lock);
146
147                 target = find_task_by_vpid(pid);
148                 if (!target)
149                         ret = -ESRCH;
150                 else
151                         ret = security_capget(target, pEp, pIp, pPp);
152
153                 read_unlock(&tasklist_lock);
154         } else
155                 ret = security_capget(current, pEp, pIp, pPp);
156
157         return ret;
158 }
159
160 /**
161  * sys_capget - get the capabilities of a given process.
162  * @header: pointer to struct that contains capability version and
163  *      target pid data
164  * @dataptr: pointer to struct that contains the effective, permitted,
165  *      and inheritable capabilities that are returned
166  *
167  * Returns 0 on success and < 0 on error.
168  */
169 SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
170 {
171         int ret = 0;
172         pid_t pid;
173         unsigned tocopy;
174         kernel_cap_t pE, pI, pP;
175
176         ret = cap_validate_magic(header, &tocopy);
177         if ((dataptr == NULL) || (ret != 0))
178                 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
179
180         if (get_user(pid, &header->pid))
181                 return -EFAULT;
182
183         if (pid < 0)
184                 return -EINVAL;
185
186         ret = cap_get_target_pid(pid, &pE, &pI, &pP);
187         if (!ret) {
188                 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
189                 unsigned i;
190
191                 for (i = 0; i < tocopy; i++) {
192                         kdata[i].effective = pE.cap[i];
193                         kdata[i].permitted = pP.cap[i];
194                         kdata[i].inheritable = pI.cap[i];
195                 }
196
197                 /*
198                  * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
199                  * we silently drop the upper capabilities here. This
200                  * has the effect of making older libcap
201                  * implementations implicitly drop upper capability
202                  * bits when they perform a: capget/modify/capset
203                  * sequence.
204                  *
205                  * This behavior is considered fail-safe
206                  * behavior. Upgrading the application to a newer
207                  * version of libcap will enable access to the newer
208                  * capabilities.
209                  *
210                  * An alternative would be to return an error here
211                  * (-ERANGE), but that causes legacy applications to
212                  * unexpectidly fail; the capget/modify/capset aborts
213                  * before modification is attempted and the application
214                  * fails.
215                  */
216                 if (copy_to_user(dataptr, kdata, tocopy
217                                  * sizeof(struct __user_cap_data_struct))) {
218                         return -EFAULT;
219                 }
220         }
221
222         return ret;
223 }
224
225 /**
226  * sys_capset - set capabilities for a process or (*) a group of processes
227  * @header: pointer to struct that contains capability version and
228  *      target pid data
229  * @data: pointer to struct that contains the effective, permitted,
230  *      and inheritable capabilities
231  *
232  * Set capabilities for the current process only.  The ability to any other
233  * process(es) has been deprecated and removed.
234  *
235  * The restrictions on setting capabilities are specified as:
236  *
237  * I: any raised capabilities must be a subset of the old permitted
238  * P: any raised capabilities must be a subset of the old permitted
239  * E: must be set to a subset of new permitted
240  *
241  * Returns 0 on success and < 0 on error.
242  */
243 SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
244 {
245         struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
246         unsigned i, tocopy, copybytes;
247         kernel_cap_t inheritable, permitted, effective;
248         struct cred *new;
249         int ret;
250         pid_t pid;
251
252         ret = cap_validate_magic(header, &tocopy);
253         if (ret != 0)
254                 return ret;
255
256         if (get_user(pid, &header->pid))
257                 return -EFAULT;
258
259         /* may only affect current now */
260         if (pid != 0 && pid != task_pid_vnr(current))
261                 return -EPERM;
262
263         copybytes = tocopy * sizeof(struct __user_cap_data_struct);
264         if (copybytes > sizeof(kdata))
265                 return -EFAULT;
266
267         if (copy_from_user(&kdata, data, copybytes))
268                 return -EFAULT;
269
270         for (i = 0; i < tocopy; i++) {
271                 effective.cap[i] = kdata[i].effective;
272                 permitted.cap[i] = kdata[i].permitted;
273                 inheritable.cap[i] = kdata[i].inheritable;
274         }
275         while (i < _KERNEL_CAPABILITY_U32S) {
276                 effective.cap[i] = 0;
277                 permitted.cap[i] = 0;
278                 inheritable.cap[i] = 0;
279                 i++;
280         }
281
282         new = prepare_creds();
283         if (!new)
284                 return -ENOMEM;
285
286         ret = security_capset(new, current_cred(),
287                               &effective, &inheritable, &permitted);
288         if (ret < 0)
289                 goto error;
290
291         audit_log_capset(pid, new, current_cred());
292
293         return commit_creds(new);
294
295 error:
296         abort_creds(new);
297         return ret;
298 }
299
300 /**
301  * capable - Determine if the current task has a superior capability in effect
302  * @cap: The capability to be tested for
303  *
304  * Return true if the current task has the given superior capability currently
305  * available for use, false if not.
306  *
307  * This sets PF_SUPERPRIV on the task if the capability is available on the
308  * assumption that it's about to be used.
309  */
310 int capable(int cap)
311 {
312         if (unlikely(!cap_valid(cap))) {
313                 printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
314                 BUG();
315         }
316
317         if (security_capable(cap) == 0) {
318                 current->flags |= PF_SUPERPRIV;
319                 return 1;
320         }
321         return 0;
322 }
323 EXPORT_SYMBOL(capable);