blob: 58b00519624a3533800eb0ffe4dcc4d60b523c6d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/capability.c
3 *
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
5 *
Andrew Morgan72c2d582007-10-18 03:05:59 -07006 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
Daniel Walker314f70f2007-10-18 03:06:08 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Eric Parise68b75a02008-11-11 21:48:22 +110010#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080011#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/security.h>
15#include <linux/syscalls.h>
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070016#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/uaccess.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/*
20 * This lock protects task->cap_* for all tasks including current.
21 * Locking rule: acquire this prior to tasklist_lock.
22 */
23static DEFINE_SPINLOCK(task_capability_lock);
24
25/*
Andrew Morgane338d262008-02-04 22:29:42 -080026 * Leveraged for setting/resetting capabilities
27 */
28
29const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
30const kernel_cap_t __cap_full_set = CAP_FULL_SET;
31const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
32
33EXPORT_SYMBOL(__cap_empty_set);
34EXPORT_SYMBOL(__cap_full_set);
35EXPORT_SYMBOL(__cap_init_eff_set);
36
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -060037#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
38int file_caps_enabled = 1;
39
40static int __init file_caps_disable(char *str)
41{
42 file_caps_enabled = 0;
43 return 1;
44}
45__setup("no_file_caps", file_caps_disable);
46#endif
47
Andrew Morgane338d262008-02-04 22:29:42 -080048/*
49 * More recent versions of libcap are available from:
50 *
51 * http://www.kernel.org/pub/linux/libs/security/linux-privs/
52 */
53
54static 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/*
Andrew G. Morganca05a992008-05-27 22:05:17 -070068 * 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
83static 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 */
101static 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
Andrew G. Morganab763c72008-07-23 21:28:25 -0700130/*
131 * If we have configured with filesystem capability support, then the
132 * only thing that can change the capabilities of the current process
133 * is the current process. As such, we can't be in this code at the
134 * same time as we are in the process of setting capabilities in this
135 * process. The net result is that we can limit our use of locks to
136 * when we are reading the caps of another process.
137 */
138static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
139 kernel_cap_t *pIp, kernel_cap_t *pPp)
140{
141 int ret;
142
143 if (pid && (pid != task_pid_vnr(current))) {
144 struct task_struct *target;
145
146 spin_lock(&task_capability_lock);
147 read_lock(&tasklist_lock);
148
149 target = find_task_by_vpid(pid);
150 if (!target)
151 ret = -ESRCH;
152 else
153 ret = security_capget(target, pEp, pIp, pPp);
154
155 read_unlock(&tasklist_lock);
156 spin_unlock(&task_capability_lock);
157 } else
158 ret = security_capget(current, pEp, pIp, pPp);
159
160 return ret;
161}
162
163/*
Andrew G. Morgan086f7312008-07-04 09:59:58 -0700164 * Atomically modify the effective capabilities returning the original
165 * value. No permission check is performed here - it is assumed that the
166 * caller is permitted to set the desired effective capabilities.
167 */
168kernel_cap_t cap_set_effective(const kernel_cap_t pE_new)
169{
170 kernel_cap_t pE_old;
171
172 spin_lock(&task_capability_lock);
173
174 pE_old = current->cap_effective;
175 current->cap_effective = pE_new;
176
177 spin_unlock(&task_capability_lock);
178
179 return pE_old;
180}
181
182EXPORT_SYMBOL(cap_set_effective);
183
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700184/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * sys_capget - get the capabilities of a given process.
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700186 * @header: pointer to struct that contains capability version and
187 * target pid data
188 * @dataptr: pointer to struct that contains the effective, permitted,
189 * and inheritable capabilities that are returned
190 *
191 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
194{
Daniel Walker314f70f2007-10-18 03:06:08 -0700195 int ret = 0;
196 pid_t pid;
Andrew Morgane338d262008-02-04 22:29:42 -0800197 unsigned tocopy;
198 kernel_cap_t pE, pI, pP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Andrew G. Morganca05a992008-05-27 22:05:17 -0700200 ret = cap_validate_magic(header, &tocopy);
201 if (ret != 0)
202 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Daniel Walker314f70f2007-10-18 03:06:08 -0700204 if (get_user(pid, &header->pid))
205 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Daniel Walker314f70f2007-10-18 03:06:08 -0700207 if (pid < 0)
208 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Andrew G. Morganab763c72008-07-23 21:28:25 -0700210 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Andrew Morgane338d262008-02-04 22:29:42 -0800212 if (!ret) {
Andrew G. Morganca05a992008-05-27 22:05:17 -0700213 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
Andrew Morgane338d262008-02-04 22:29:42 -0800214 unsigned i;
215
216 for (i = 0; i < tocopy; i++) {
217 kdata[i].effective = pE.cap[i];
218 kdata[i].permitted = pP.cap[i];
219 kdata[i].inheritable = pI.cap[i];
220 }
221
222 /*
Andrew G. Morganca05a992008-05-27 22:05:17 -0700223 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
Andrew Morgane338d262008-02-04 22:29:42 -0800224 * we silently drop the upper capabilities here. This
225 * has the effect of making older libcap
226 * implementations implicitly drop upper capability
227 * bits when they perform a: capget/modify/capset
228 * sequence.
229 *
230 * This behavior is considered fail-safe
231 * behavior. Upgrading the application to a newer
232 * version of libcap will enable access to the newer
233 * capabilities.
234 *
235 * An alternative would be to return an error here
236 * (-ERANGE), but that causes legacy applications to
237 * unexpectidly fail; the capget/modify/capset aborts
238 * before modification is attempted and the application
239 * fails.
240 */
Andrew Morgane338d262008-02-04 22:29:42 -0800241 if (copy_to_user(dataptr, kdata, tocopy
242 * sizeof(struct __user_cap_data_struct))) {
243 return -EFAULT;
244 }
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Daniel Walker314f70f2007-10-18 03:06:08 -0700247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700250/**
Andrew G. Morganab763c72008-07-23 21:28:25 -0700251 * sys_capset - set capabilities for a process or (*) a group of processes
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700252 * @header: pointer to struct that contains capability version and
253 * target pid data
254 * @data: pointer to struct that contains the effective, permitted,
255 * and inheritable capabilities
256 *
David Howells1cdcbec2008-11-14 10:39:14 +1100257 * Set capabilities for the current process only. The ability to any other
258 * process(es) has been deprecated and removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
260 * The restrictions on setting capabilities are specified as:
261 *
David Howells1cdcbec2008-11-14 10:39:14 +1100262 * I: any raised capabilities must be a subset of the old permitted
263 * P: any raised capabilities must be a subset of the old permitted
264 * E: must be set to a subset of new permitted
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700265 *
266 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
268asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
269{
Andrew G. Morganca05a992008-05-27 22:05:17 -0700270 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
Andrew Morgane338d262008-02-04 22:29:42 -0800271 unsigned i, tocopy;
Daniel Walker314f70f2007-10-18 03:06:08 -0700272 kernel_cap_t inheritable, permitted, effective;
Daniel Walker314f70f2007-10-18 03:06:08 -0700273 int ret;
274 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Andrew G. Morganca05a992008-05-27 22:05:17 -0700276 ret = cap_validate_magic(header, &tocopy);
277 if (ret != 0)
278 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Daniel Walker314f70f2007-10-18 03:06:08 -0700280 if (get_user(pid, &header->pid))
281 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David Howells1cdcbec2008-11-14 10:39:14 +1100283 /* may only affect current now */
284 if (pid != 0 && pid != task_pid_vnr(current))
285 return -EPERM;
286
Andrew Morgane338d262008-02-04 22:29:42 -0800287 if (copy_from_user(&kdata, data, tocopy
David Howells1cdcbec2008-11-14 10:39:14 +1100288 * sizeof(struct __user_cap_data_struct)))
Daniel Walker314f70f2007-10-18 03:06:08 -0700289 return -EFAULT;
Andrew Morgane338d262008-02-04 22:29:42 -0800290
291 for (i = 0; i < tocopy; i++) {
292 effective.cap[i] = kdata[i].effective;
293 permitted.cap[i] = kdata[i].permitted;
294 inheritable.cap[i] = kdata[i].inheritable;
295 }
Andrew G. Morganca05a992008-05-27 22:05:17 -0700296 while (i < _KERNEL_CAPABILITY_U32S) {
Andrew Morgane338d262008-02-04 22:29:42 -0800297 effective.cap[i] = 0;
298 permitted.cap[i] = 0;
299 inheritable.cap[i] = 0;
300 i++;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Eric Parise68b75a02008-11-11 21:48:22 +1100303 ret = audit_log_capset(pid, &effective, &inheritable, &permitted);
304 if (ret)
305 return ret;
306
David Howells1cdcbec2008-11-14 10:39:14 +1100307 /* This lock is required even when filesystem capability support is
308 * configured - it protects the sys_capget() call from returning
309 * incorrect data in the case that the targeted process is not the
310 * current one.
311 */
312 spin_lock(&task_capability_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
David Howells1cdcbec2008-11-14 10:39:14 +1100314 ret = security_capset_check(&effective, &inheritable, &permitted);
315 /* Having verified that the proposed changes are legal, we now put them
316 * into effect.
317 */
318 if (!ret)
319 security_capset_set(&effective, &inheritable, &permitted);
320 spin_unlock(&task_capability_lock);
Daniel Walker314f70f2007-10-18 03:06:08 -0700321 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
Chris Wright12b59892006-03-25 03:07:41 -0800323
David Howells5cd9c582008-08-14 11:37:28 +0100324/**
325 * capable - Determine if the current task has a superior capability in effect
326 * @cap: The capability to be tested for
327 *
328 * Return true if the current task has the given superior capability currently
329 * available for use, false if not.
330 *
331 * This sets PF_SUPERPRIV on the task if the capability is available on the
332 * assumption that it's about to be used.
333 */
334int capable(int cap)
Chris Wright12b59892006-03-25 03:07:41 -0800335{
Eric Paris637d32d2008-10-29 15:42:12 +1100336 if (unlikely(!cap_valid(cap))) {
337 printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
338 BUG();
339 }
340
David Howells5cd9c582008-08-14 11:37:28 +0100341 if (has_capability(current, cap)) {
342 current->flags |= PF_SUPERPRIV;
Chris Wright12b59892006-03-25 03:07:41 -0800343 return 1;
344 }
345 return 0;
346}
Chris Wright12b59892006-03-25 03:07:41 -0800347EXPORT_SYMBOL(capable);