1
2
3
4
5
6
7
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
22
23
24const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
25const kernel_cap_t __cap_full_set = CAP_FULL_SET;
26const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
27
28EXPORT_SYMBOL(__cap_empty_set);
29EXPORT_SYMBOL(__cap_full_set);
30EXPORT_SYMBOL(__cap_init_eff_set);
31
32int file_caps_enabled = 1;
33
34static 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
42
43
44
45
46
47static void warn_legacy_capability_use(void)
48{
49 static int warned;
50 if (!warned) {
51 char name[sizeof(current->comm)];
52
53 printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
54 " (legacy support in use)\n",
55 get_task_comm(name, current));
56 warned = 1;
57 }
58}
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76static void warn_deprecated_v2(void)
77{
78 static int warned;
79
80 if (!warned) {
81 char name[sizeof(current->comm)];
82
83 printk(KERN_INFO "warning: `%s' uses deprecated v2"
84 " capabilities in a way that may be insecure.\n",
85 get_task_comm(name, current));
86 warned = 1;
87 }
88}
89
90
91
92
93
94static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
95{
96 __u32 version;
97
98 if (get_user(version, &header->version))
99 return -EFAULT;
100
101 switch (version) {
102 case _LINUX_CAPABILITY_VERSION_1:
103 warn_legacy_capability_use();
104 *tocopy = _LINUX_CAPABILITY_U32S_1;
105 break;
106 case _LINUX_CAPABILITY_VERSION_2:
107 warn_deprecated_v2();
108
109
110
111 case _LINUX_CAPABILITY_VERSION_3:
112 *tocopy = _LINUX_CAPABILITY_U32S_3;
113 break;
114 default:
115 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
116 return -EFAULT;
117 return -EINVAL;
118 }
119
120 return 0;
121}
122
123
124
125
126
127
128
129
130static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
131 kernel_cap_t *pIp, kernel_cap_t *pPp)
132{
133 int ret;
134
135 if (pid && (pid != task_pid_vnr(current))) {
136 struct task_struct *target;
137
138 rcu_read_lock();
139
140 target = find_task_by_vpid(pid);
141 if (!target)
142 ret = -ESRCH;
143 else
144 ret = security_capget(target, pEp, pIp, pPp);
145
146 rcu_read_unlock();
147 } else
148 ret = security_capget(current, pEp, pIp, pPp);
149
150 return ret;
151}
152
153
154
155
156
157
158
159
160
161
162SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
163{
164 int ret = 0;
165 pid_t pid;
166 unsigned tocopy;
167 kernel_cap_t pE, pI, pP;
168
169 ret = cap_validate_magic(header, &tocopy);
170 if ((dataptr == NULL) || (ret != 0))
171 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
172
173 if (get_user(pid, &header->pid))
174 return -EFAULT;
175
176 if (pid < 0)
177 return -EINVAL;
178
179 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
180 if (!ret) {
181 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
182 unsigned i;
183
184 for (i = 0; i < tocopy; i++) {
185 kdata[i].effective = pE.cap[i];
186 kdata[i].permitted = pP.cap[i];
187 kdata[i].inheritable = pI.cap[i];
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209 if (copy_to_user(dataptr, kdata, tocopy
210 * sizeof(struct __user_cap_data_struct))) {
211 return -EFAULT;
212 }
213 }
214
215 return ret;
216}
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
237{
238 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
239 unsigned i, tocopy, copybytes;
240 kernel_cap_t inheritable, permitted, effective;
241 struct cred *new;
242 int ret;
243 pid_t pid;
244
245 ret = cap_validate_magic(header, &tocopy);
246 if (ret != 0)
247 return ret;
248
249 if (get_user(pid, &header->pid))
250 return -EFAULT;
251
252
253 if (pid != 0 && pid != task_pid_vnr(current))
254 return -EPERM;
255
256 copybytes = tocopy * sizeof(struct __user_cap_data_struct);
257 if (copybytes > sizeof(kdata))
258 return -EFAULT;
259
260 if (copy_from_user(&kdata, data, copybytes))
261 return -EFAULT;
262
263 for (i = 0; i < tocopy; i++) {
264 effective.cap[i] = kdata[i].effective;
265 permitted.cap[i] = kdata[i].permitted;
266 inheritable.cap[i] = kdata[i].inheritable;
267 }
268 while (i < _KERNEL_CAPABILITY_U32S) {
269 effective.cap[i] = 0;
270 permitted.cap[i] = 0;
271 inheritable.cap[i] = 0;
272 i++;
273 }
274
275 new = prepare_creds();
276 if (!new)
277 return -ENOMEM;
278
279 ret = security_capset(new, current_cred(),
280 &effective, &inheritable, &permitted);
281 if (ret < 0)
282 goto error;
283
284 audit_log_capset(pid, new, current_cred());
285
286 return commit_creds(new);
287
288error:
289 abort_creds(new);
290 return ret;
291}
292
293
294
295
296
297
298
299
300
301
302
303bool has_capability(struct task_struct *t, int cap)
304{
305 int ret = security_real_capable(t, &init_user_ns, cap);
306
307 return (ret == 0);
308}
309
310
311
312
313
314
315
316
317
318
319
320
321bool has_ns_capability(struct task_struct *t,
322 struct user_namespace *ns, int cap)
323{
324 int ret = security_real_capable(t, ns, cap);
325
326 return (ret == 0);
327}
328
329
330
331
332
333
334
335
336
337
338
339
340bool has_capability_noaudit(struct task_struct *t, int cap)
341{
342 int ret = security_real_capable_noaudit(t, &init_user_ns, cap);
343
344 return (ret == 0);
345}
346
347
348
349
350
351
352
353
354
355
356
357bool capable(int cap)
358{
359 return ns_capable(&init_user_ns, cap);
360}
361EXPORT_SYMBOL(capable);
362
363
364
365
366
367
368
369
370
371
372
373
374bool ns_capable(struct user_namespace *ns, int cap)
375{
376 if (unlikely(!cap_valid(cap))) {
377 printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
378 BUG();
379 }
380
381 if (security_capable(ns, current_cred(), cap) == 0) {
382 current->flags |= PF_SUPERPRIV;
383 return true;
384 }
385 return false;
386}
387EXPORT_SYMBOL(ns_capable);
388
389
390
391
392
393
394
395
396
397bool task_ns_capable(struct task_struct *t, int cap)
398{
399 return ns_capable(task_cred_xxx(t, user)->user_ns, cap);
400}
401EXPORT_SYMBOL(task_ns_capable);
402
403
404
405
406
407
408
409
410bool nsown_capable(int cap)
411{
412 return ns_capable(current_user_ns(), cap);
413}
414