]> Pileus Git - ~andy/linux/blob - kernel/capability.c
capabilities: do not drop CAP_SETPCAP from the initial task
[~andy/linux] / 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 <linux/user_namespace.h>
18 #include <asm/uaccess.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
27 EXPORT_SYMBOL(__cap_empty_set);
28 EXPORT_SYMBOL(__cap_full_set);
29
30 int file_caps_enabled = 1;
31
32 static int __init file_caps_disable(char *str)
33 {
34         file_caps_enabled = 0;
35         return 1;
36 }
37 __setup("no_file_caps", file_caps_disable);
38
39 /*
40  * More recent versions of libcap are available from:
41  *
42  *   http://www.kernel.org/pub/linux/libs/security/linux-privs/
43  */
44
45 static void warn_legacy_capability_use(void)
46 {
47         static int warned;
48         if (!warned) {
49                 char name[sizeof(current->comm)];
50
51                 printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
52                        " (legacy support in use)\n",
53                        get_task_comm(name, current));
54                 warned = 1;
55         }
56 }
57
58 /*
59  * Version 2 capabilities worked fine, but the linux/capability.h file
60  * that accompanied their introduction encouraged their use without
61  * the necessary user-space source code changes. As such, we have
62  * created a version 3 with equivalent functionality to version 2, but
63  * with a header change to protect legacy source code from using
64  * version 2 when it wanted to use version 1. If your system has code
65  * that trips the following warning, it is using version 2 specific
66  * capabilities and may be doing so insecurely.
67  *
68  * The remedy is to either upgrade your version of libcap (to 2.10+,
69  * if the application is linked against it), or recompile your
70  * application with modern kernel headers and this warning will go
71  * away.
72  */
73
74 static void warn_deprecated_v2(void)
75 {
76         static int warned;
77
78         if (!warned) {
79                 char name[sizeof(current->comm)];
80
81                 printk(KERN_INFO "warning: `%s' uses deprecated v2"
82                        " capabilities in a way that may be insecure.\n",
83                        get_task_comm(name, current));
84                 warned = 1;
85         }
86 }
87
88 /*
89  * Version check. Return the number of u32s in each capability flag
90  * array, or a negative value on error.
91  */
92 static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
93 {
94         __u32 version;
95
96         if (get_user(version, &header->version))
97                 return -EFAULT;
98
99         switch (version) {
100         case _LINUX_CAPABILITY_VERSION_1:
101                 warn_legacy_capability_use();
102                 *tocopy = _LINUX_CAPABILITY_U32S_1;
103                 break;
104         case _LINUX_CAPABILITY_VERSION_2:
105                 warn_deprecated_v2();
106                 /*
107                  * fall through - v3 is otherwise equivalent to v2.
108                  */
109         case _LINUX_CAPABILITY_VERSION_3:
110                 *tocopy = _LINUX_CAPABILITY_U32S_3;
111                 break;
112         default:
113                 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
114                         return -EFAULT;
115                 return -EINVAL;
116         }
117
118         return 0;
119 }
120
121 /*
122  * The only thing that can change the capabilities of the current
123  * process is the current process. As such, we can't be in this code
124  * at the same time as we are in the process of setting capabilities
125  * in this process. The net result is that we can limit our use of
126  * locks to when we are reading the caps of another process.
127  */
128 static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
129                                      kernel_cap_t *pIp, kernel_cap_t *pPp)
130 {
131         int ret;
132
133         if (pid && (pid != task_pid_vnr(current))) {
134                 struct task_struct *target;
135
136                 rcu_read_lock();
137
138                 target = find_task_by_vpid(pid);
139                 if (!target)
140                         ret = -ESRCH;
141                 else
142                         ret = security_capget(target, pEp, pIp, pPp);
143
144                 rcu_read_unlock();
145         } else
146                 ret = security_capget(current, pEp, pIp, pPp);
147
148         return ret;
149 }
150
151 /**
152  * sys_capget - get the capabilities of a given process.
153  * @header: pointer to struct that contains capability version and
154  *      target pid data
155  * @dataptr: pointer to struct that contains the effective, permitted,
156  *      and inheritable capabilities that are returned
157  *
158  * Returns 0 on success and < 0 on error.
159  */
160 SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
161 {
162         int ret = 0;
163         pid_t pid;
164         unsigned tocopy;
165         kernel_cap_t pE, pI, pP;
166
167         ret = cap_validate_magic(header, &tocopy);
168         if ((dataptr == NULL) || (ret != 0))
169                 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
170
171         if (get_user(pid, &header->pid))
172                 return -EFAULT;
173
174         if (pid < 0)
175                 return -EINVAL;
176
177         ret = cap_get_target_pid(pid, &pE, &pI, &pP);
178         if (!ret) {
179                 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
180                 unsigned i;
181
182                 for (i = 0; i < tocopy; i++) {
183                         kdata[i].effective = pE.cap[i];
184                         kdata[i].permitted = pP.cap[i];
185                         kdata[i].inheritable = pI.cap[i];
186                 }
187
188                 /*
189                  * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
190                  * we silently drop the upper capabilities here. This
191                  * has the effect of making older libcap
192                  * implementations implicitly drop upper capability
193                  * bits when they perform a: capget/modify/capset
194                  * sequence.
195                  *
196                  * This behavior is considered fail-safe
197                  * behavior. Upgrading the application to a newer
198                  * version of libcap will enable access to the newer
199                  * capabilities.
200                  *
201                  * An alternative would be to return an error here
202                  * (-ERANGE), but that causes legacy applications to
203                  * unexpectidly fail; the capget/modify/capset aborts
204                  * before modification is attempted and the application
205                  * fails.
206                  */
207                 if (copy_to_user(dataptr, kdata, tocopy
208                                  * sizeof(struct __user_cap_data_struct))) {
209                         return -EFAULT;
210                 }
211         }
212
213         return ret;
214 }
215
216 /**
217  * sys_capset - set capabilities for a process or (*) a group of processes
218  * @header: pointer to struct that contains capability version and
219  *      target pid data
220  * @data: pointer to struct that contains the effective, permitted,
221  *      and inheritable capabilities
222  *
223  * Set capabilities for the current process only.  The ability to any other
224  * process(es) has been deprecated and removed.
225  *
226  * The restrictions on setting capabilities are specified as:
227  *
228  * I: any raised capabilities must be a subset of the old permitted
229  * P: any raised capabilities must be a subset of the old permitted
230  * E: must be set to a subset of new permitted
231  *
232  * Returns 0 on success and < 0 on error.
233  */
234 SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
235 {
236         struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
237         unsigned i, tocopy, copybytes;
238         kernel_cap_t inheritable, permitted, effective;
239         struct cred *new;
240         int ret;
241         pid_t pid;
242
243         ret = cap_validate_magic(header, &tocopy);
244         if (ret != 0)
245                 return ret;
246
247         if (get_user(pid, &header->pid))
248                 return -EFAULT;
249
250         /* may only affect current now */
251         if (pid != 0 && pid != task_pid_vnr(current))
252                 return -EPERM;
253
254         copybytes = tocopy * sizeof(struct __user_cap_data_struct);
255         if (copybytes > sizeof(kdata))
256                 return -EFAULT;
257
258         if (copy_from_user(&kdata, data, copybytes))
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  * has_capability - Does a task have a capability in init_user_ns
293  * @t: The task in question
294  * @cap: The capability to be tested for
295  *
296  * Return true if the specified task has the given superior capability
297  * currently in effect to the initial user namespace, false if not.
298  *
299  * Note that this does not set PF_SUPERPRIV on the task.
300  */
301 bool has_capability(struct task_struct *t, int cap)
302 {
303         int ret = security_real_capable(t, &init_user_ns, cap);
304
305         return (ret == 0);
306 }
307
308 /**
309  * has_capability - Does a task have a capability in a specific user ns
310  * @t: The task in question
311  * @ns: target user namespace
312  * @cap: The capability to be tested for
313  *
314  * Return true if the specified task has the given superior capability
315  * currently in effect to the specified user namespace, false if not.
316  *
317  * Note that this does not set PF_SUPERPRIV on the task.
318  */
319 bool has_ns_capability(struct task_struct *t,
320                        struct user_namespace *ns, int cap)
321 {
322         int ret = security_real_capable(t, ns, cap);
323
324         return (ret == 0);
325 }
326
327 /**
328  * has_capability_noaudit - Does a task have a capability (unaudited)
329  * @t: The task in question
330  * @cap: The capability to be tested for
331  *
332  * Return true if the specified task has the given superior capability
333  * currently in effect to init_user_ns, false if not.  Don't write an
334  * audit message for the check.
335  *
336  * Note that this does not set PF_SUPERPRIV on the task.
337  */
338 bool has_capability_noaudit(struct task_struct *t, int cap)
339 {
340         int ret = security_real_capable_noaudit(t, &init_user_ns, cap);
341
342         return (ret == 0);
343 }
344
345 /**
346  * capable - Determine if the current task has a superior capability in effect
347  * @cap: The capability to be tested for
348  *
349  * Return true if the current task has the given superior capability currently
350  * available for use, false if not.
351  *
352  * This sets PF_SUPERPRIV on the task if the capability is available on the
353  * assumption that it's about to be used.
354  */
355 bool capable(int cap)
356 {
357         return ns_capable(&init_user_ns, cap);
358 }
359 EXPORT_SYMBOL(capable);
360
361 /**
362  * ns_capable - Determine if the current task has a superior capability in effect
363  * @ns:  The usernamespace we want the capability in
364  * @cap: The capability to be tested for
365  *
366  * Return true if the current task has the given superior capability currently
367  * available for use, false if not.
368  *
369  * This sets PF_SUPERPRIV on the task if the capability is available on the
370  * assumption that it's about to be used.
371  */
372 bool ns_capable(struct user_namespace *ns, int cap)
373 {
374         if (unlikely(!cap_valid(cap))) {
375                 printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
376                 BUG();
377         }
378
379         if (security_capable(ns, current_cred(), cap) == 0) {
380                 current->flags |= PF_SUPERPRIV;
381                 return true;
382         }
383         return false;
384 }
385 EXPORT_SYMBOL(ns_capable);
386
387 /**
388  * task_ns_capable - Determine whether current task has a superior
389  * capability targeted at a specific task's user namespace.
390  * @t: The task whose user namespace is targeted.
391  * @cap: The capability in question.
392  *
393  *  Return true if it does, false otherwise.
394  */
395 bool task_ns_capable(struct task_struct *t, int cap)
396 {
397         return ns_capable(task_cred_xxx(t, user)->user_ns, cap);
398 }
399 EXPORT_SYMBOL(task_ns_capable);