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