blob: 6d1407bc15310de92c7f5e63d359309c5b7d5830 [file] [log] [blame]
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
13#include <linux/syscalls.h>
14#include <linux/slab.h>
15#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070016#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070017#include <linux/license.h>
18#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070019#include <linux/version.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070020
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070021int sysctl_unprivileged_bpf_disabled __read_mostly;
22
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070023static LIST_HEAD(bpf_map_types);
24
25static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
26{
27 struct bpf_map_type_list *tl;
28 struct bpf_map *map;
29
30 list_for_each_entry(tl, &bpf_map_types, list_node) {
31 if (tl->type == attr->map_type) {
32 map = tl->ops->map_alloc(attr);
33 if (IS_ERR(map))
34 return map;
35 map->ops = tl->ops;
36 map->map_type = attr->map_type;
37 return map;
38 }
39 }
40 return ERR_PTR(-EINVAL);
41}
42
43/* boot time registration of different map implementations */
44void bpf_register_map_type(struct bpf_map_type_list *tl)
45{
46 list_add(&tl->list_node, &bpf_map_types);
47}
48
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070049static int bpf_map_charge_memlock(struct bpf_map *map)
50{
51 struct user_struct *user = get_current_user();
52 unsigned long memlock_limit;
53
54 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
55
56 atomic_long_add(map->pages, &user->locked_vm);
57
58 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
59 atomic_long_sub(map->pages, &user->locked_vm);
60 free_uid(user);
61 return -EPERM;
62 }
63 map->user = user;
64 return 0;
65}
66
67static void bpf_map_uncharge_memlock(struct bpf_map *map)
68{
69 struct user_struct *user = map->user;
70
71 atomic_long_sub(map->pages, &user->locked_vm);
72 free_uid(user);
73}
74
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070075/* called from workqueue */
76static void bpf_map_free_deferred(struct work_struct *work)
77{
78 struct bpf_map *map = container_of(work, struct bpf_map, work);
79
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070080 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070081 /* implementation dependent freeing */
82 map->ops->map_free(map);
83}
84
85/* decrement map refcnt and schedule it for freeing via workqueue
86 * (unrelying map implementation ops->map_free() might sleep)
87 */
88void bpf_map_put(struct bpf_map *map)
89{
90 if (atomic_dec_and_test(&map->refcnt)) {
91 INIT_WORK(&map->work, bpf_map_free_deferred);
92 schedule_work(&map->work);
93 }
94}
95
Daniel Borkmannf99bf202015-11-19 11:56:22 +010096#ifdef CONFIG_PROC_FS
97static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
98{
99 const struct bpf_map *map = filp->private_data;
100
101 seq_printf(m,
102 "map_type:\t%u\n"
103 "key_size:\t%u\n"
104 "value_size:\t%u\n"
105 "max_entries:\t%u\n",
106 map->map_type,
107 map->key_size,
108 map->value_size,
109 map->max_entries);
110}
111#endif
112
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700113static int bpf_map_release(struct inode *inode, struct file *filp)
114{
115 struct bpf_map *map = filp->private_data;
116
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700117 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
118 /* prog_array stores refcnt-ed bpf_prog pointers
119 * release them all when user space closes prog_array_fd
120 */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000121 bpf_fd_array_map_clear(map);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700122
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700123 bpf_map_put(map);
124 return 0;
125}
126
127static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100128#ifdef CONFIG_PROC_FS
129 .show_fdinfo = bpf_map_show_fdinfo,
130#endif
131 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700132};
133
Daniel Borkmannb2197752015-10-29 14:58:09 +0100134int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100135{
136 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
137 O_RDWR | O_CLOEXEC);
138}
139
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700140/* helper macro to check that unused fields 'union bpf_attr' are zero */
141#define CHECK_ATTR(CMD) \
142 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
143 sizeof(attr->CMD##_LAST_FIELD), 0, \
144 sizeof(*attr) - \
145 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
146 sizeof(attr->CMD##_LAST_FIELD)) != NULL
147
148#define BPF_MAP_CREATE_LAST_FIELD max_entries
149/* called via syscall */
150static int map_create(union bpf_attr *attr)
151{
152 struct bpf_map *map;
153 int err;
154
155 err = CHECK_ATTR(BPF_MAP_CREATE);
156 if (err)
157 return -EINVAL;
158
159 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
160 map = find_and_alloc_map(attr);
161 if (IS_ERR(map))
162 return PTR_ERR(map);
163
164 atomic_set(&map->refcnt, 1);
165
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700166 err = bpf_map_charge_memlock(map);
167 if (err)
168 goto free_map;
169
Daniel Borkmannaa797812015-10-29 14:58:06 +0100170 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700171 if (err < 0)
172 /* failed to allocate fd */
173 goto free_map;
174
175 return err;
176
177free_map:
178 map->ops->map_free(map);
179 return err;
180}
181
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700182/* if error is returned, fd is released.
183 * On success caller should complete fd access with matching fdput()
184 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100185struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700186{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700187 if (!f.file)
188 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700189 if (f.file->f_op != &bpf_map_fops) {
190 fdput(f);
191 return ERR_PTR(-EINVAL);
192 }
193
Daniel Borkmannc2101292015-10-29 14:58:07 +0100194 return f.file->private_data;
195}
196
Daniel Borkmannb2197752015-10-29 14:58:09 +0100197struct bpf_map *bpf_map_get(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100198{
199 struct fd f = fdget(ufd);
200 struct bpf_map *map;
201
202 map = __bpf_map_get(f);
203 if (IS_ERR(map))
204 return map;
205
206 atomic_inc(&map->refcnt);
207 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700208
209 return map;
210}
211
212/* helper to convert user pointers passed inside __aligned_u64 fields */
213static void __user *u64_to_ptr(__u64 val)
214{
215 return (void __user *) (unsigned long) val;
216}
217
218/* last field in 'union bpf_attr' used by this command */
219#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
220
221static int map_lookup_elem(union bpf_attr *attr)
222{
223 void __user *ukey = u64_to_ptr(attr->key);
224 void __user *uvalue = u64_to_ptr(attr->value);
225 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700226 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800227 void *key, *value, *ptr;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200228 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700229 int err;
230
231 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
232 return -EINVAL;
233
Daniel Borkmann592867b2015-09-08 18:00:09 +0200234 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100235 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700236 if (IS_ERR(map))
237 return PTR_ERR(map);
238
239 err = -ENOMEM;
240 key = kmalloc(map->key_size, GFP_USER);
241 if (!key)
242 goto err_put;
243
244 err = -EFAULT;
245 if (copy_from_user(key, ukey, map->key_size) != 0)
246 goto free_key;
247
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800248 err = -ENOMEM;
249 value = kmalloc(map->value_size, GFP_USER);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700250 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800251 goto free_key;
252
253 rcu_read_lock();
254 ptr = map->ops->map_lookup_elem(map, key);
255 if (ptr)
256 memcpy(value, ptr, map->value_size);
257 rcu_read_unlock();
258
259 err = -ENOENT;
260 if (!ptr)
261 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700262
263 err = -EFAULT;
264 if (copy_to_user(uvalue, value, map->value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800265 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700266
267 err = 0;
268
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800269free_value:
270 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700271free_key:
272 kfree(key);
273err_put:
274 fdput(f);
275 return err;
276}
277
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800278#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700279
280static int map_update_elem(union bpf_attr *attr)
281{
282 void __user *ukey = u64_to_ptr(attr->key);
283 void __user *uvalue = u64_to_ptr(attr->value);
284 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700285 struct bpf_map *map;
286 void *key, *value;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200287 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700288 int err;
289
290 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
291 return -EINVAL;
292
Daniel Borkmann592867b2015-09-08 18:00:09 +0200293 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100294 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700295 if (IS_ERR(map))
296 return PTR_ERR(map);
297
298 err = -ENOMEM;
299 key = kmalloc(map->key_size, GFP_USER);
300 if (!key)
301 goto err_put;
302
303 err = -EFAULT;
304 if (copy_from_user(key, ukey, map->key_size) != 0)
305 goto free_key;
306
307 err = -ENOMEM;
308 value = kmalloc(map->value_size, GFP_USER);
309 if (!value)
310 goto free_key;
311
312 err = -EFAULT;
313 if (copy_from_user(value, uvalue, map->value_size) != 0)
314 goto free_value;
315
316 /* eBPF program that use maps are running under rcu_read_lock(),
317 * therefore all map accessors rely on this fact, so do the same here
318 */
319 rcu_read_lock();
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800320 err = map->ops->map_update_elem(map, key, value, attr->flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700321 rcu_read_unlock();
322
323free_value:
324 kfree(value);
325free_key:
326 kfree(key);
327err_put:
328 fdput(f);
329 return err;
330}
331
332#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
333
334static int map_delete_elem(union bpf_attr *attr)
335{
336 void __user *ukey = u64_to_ptr(attr->key);
337 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700338 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200339 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700340 void *key;
341 int err;
342
343 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
344 return -EINVAL;
345
Daniel Borkmann592867b2015-09-08 18:00:09 +0200346 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100347 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700348 if (IS_ERR(map))
349 return PTR_ERR(map);
350
351 err = -ENOMEM;
352 key = kmalloc(map->key_size, GFP_USER);
353 if (!key)
354 goto err_put;
355
356 err = -EFAULT;
357 if (copy_from_user(key, ukey, map->key_size) != 0)
358 goto free_key;
359
360 rcu_read_lock();
361 err = map->ops->map_delete_elem(map, key);
362 rcu_read_unlock();
363
364free_key:
365 kfree(key);
366err_put:
367 fdput(f);
368 return err;
369}
370
371/* last field in 'union bpf_attr' used by this command */
372#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
373
374static int map_get_next_key(union bpf_attr *attr)
375{
376 void __user *ukey = u64_to_ptr(attr->key);
377 void __user *unext_key = u64_to_ptr(attr->next_key);
378 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700379 struct bpf_map *map;
380 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200381 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700382 int err;
383
384 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
385 return -EINVAL;
386
Daniel Borkmann592867b2015-09-08 18:00:09 +0200387 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100388 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700389 if (IS_ERR(map))
390 return PTR_ERR(map);
391
392 err = -ENOMEM;
393 key = kmalloc(map->key_size, GFP_USER);
394 if (!key)
395 goto err_put;
396
397 err = -EFAULT;
398 if (copy_from_user(key, ukey, map->key_size) != 0)
399 goto free_key;
400
401 err = -ENOMEM;
402 next_key = kmalloc(map->key_size, GFP_USER);
403 if (!next_key)
404 goto free_key;
405
406 rcu_read_lock();
407 err = map->ops->map_get_next_key(map, key, next_key);
408 rcu_read_unlock();
409 if (err)
410 goto free_next_key;
411
412 err = -EFAULT;
413 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
414 goto free_next_key;
415
416 err = 0;
417
418free_next_key:
419 kfree(next_key);
420free_key:
421 kfree(key);
422err_put:
423 fdput(f);
424 return err;
425}
426
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700427static LIST_HEAD(bpf_prog_types);
428
429static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
430{
431 struct bpf_prog_type_list *tl;
432
433 list_for_each_entry(tl, &bpf_prog_types, list_node) {
434 if (tl->type == type) {
435 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100436 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700437 return 0;
438 }
439 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100440
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700441 return -EINVAL;
442}
443
444void bpf_register_prog_type(struct bpf_prog_type_list *tl)
445{
446 list_add(&tl->list_node, &bpf_prog_types);
447}
448
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700449/* fixup insn->imm field of bpf_call instructions:
450 * if (insn->imm == BPF_FUNC_map_lookup_elem)
451 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
452 * else if (insn->imm == BPF_FUNC_map_update_elem)
453 * insn->imm = bpf_map_update_elem - __bpf_call_base;
454 * else ...
455 *
456 * this function is called after eBPF program passed verification
457 */
458static void fixup_bpf_calls(struct bpf_prog *prog)
459{
460 const struct bpf_func_proto *fn;
461 int i;
462
463 for (i = 0; i < prog->len; i++) {
464 struct bpf_insn *insn = &prog->insnsi[i];
465
466 if (insn->code == (BPF_JMP | BPF_CALL)) {
467 /* we reach here when program has bpf_call instructions
468 * and it passed bpf_check(), means that
469 * ops->get_func_proto must have been supplied, check it
470 */
471 BUG_ON(!prog->aux->ops->get_func_proto);
472
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200473 if (insn->imm == BPF_FUNC_get_route_realm)
474 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200475 if (insn->imm == BPF_FUNC_get_prandom_u32)
476 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700477 if (insn->imm == BPF_FUNC_tail_call) {
478 /* mark bpf_tail_call as different opcode
479 * to avoid conditional branch in
480 * interpeter for every normal call
481 * and to prevent accidental JITing by
482 * JIT compiler that doesn't support
483 * bpf_tail_call yet
484 */
485 insn->imm = 0;
486 insn->code |= BPF_X;
487 continue;
488 }
489
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700490 fn = prog->aux->ops->get_func_proto(insn->imm);
491 /* all functions that have prototype and verifier allowed
492 * programs to call them, must be real in-kernel functions
493 */
494 BUG_ON(!fn->func);
495 insn->imm = fn->func - __bpf_call_base;
496 }
497 }
498}
499
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700500/* drop refcnt on maps used by eBPF program and free auxilary data */
501static void free_used_maps(struct bpf_prog_aux *aux)
502{
503 int i;
504
505 for (i = 0; i < aux->used_map_cnt; i++)
506 bpf_map_put(aux->used_maps[i]);
507
508 kfree(aux->used_maps);
509}
510
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700511static int bpf_prog_charge_memlock(struct bpf_prog *prog)
512{
513 struct user_struct *user = get_current_user();
514 unsigned long memlock_limit;
515
516 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
517
518 atomic_long_add(prog->pages, &user->locked_vm);
519 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
520 atomic_long_sub(prog->pages, &user->locked_vm);
521 free_uid(user);
522 return -EPERM;
523 }
524 prog->aux->user = user;
525 return 0;
526}
527
528static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
529{
530 struct user_struct *user = prog->aux->user;
531
532 atomic_long_sub(prog->pages, &user->locked_vm);
533 free_uid(user);
534}
535
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100536static void __prog_put_common(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700537{
538 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
539
540 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700541 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700542 bpf_prog_free(aux->prog);
543}
544
545/* version of bpf_prog_put() that is called after a grace period */
546void bpf_prog_put_rcu(struct bpf_prog *prog)
547{
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100548 if (atomic_dec_and_test(&prog->aux->refcnt))
549 call_rcu(&prog->aux->rcu, __prog_put_common);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700550}
551
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700552void bpf_prog_put(struct bpf_prog *prog)
553{
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100554 if (atomic_dec_and_test(&prog->aux->refcnt))
555 __prog_put_common(&prog->aux->rcu);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700556}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100557EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700558
559static int bpf_prog_release(struct inode *inode, struct file *filp)
560{
561 struct bpf_prog *prog = filp->private_data;
562
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700563 bpf_prog_put_rcu(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700564 return 0;
565}
566
567static const struct file_operations bpf_prog_fops = {
568 .release = bpf_prog_release,
569};
570
Daniel Borkmannb2197752015-10-29 14:58:09 +0100571int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100572{
573 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
574 O_RDWR | O_CLOEXEC);
575}
576
Daniel Borkmannc2101292015-10-29 14:58:07 +0100577static struct bpf_prog *__bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700578{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700579 if (!f.file)
580 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700581 if (f.file->f_op != &bpf_prog_fops) {
582 fdput(f);
583 return ERR_PTR(-EINVAL);
584 }
585
Daniel Borkmannc2101292015-10-29 14:58:07 +0100586 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700587}
588
589/* called by sockets/tracing/seccomp before attaching program to an event
590 * pairs with bpf_prog_put()
591 */
592struct bpf_prog *bpf_prog_get(u32 ufd)
593{
594 struct fd f = fdget(ufd);
595 struct bpf_prog *prog;
596
Daniel Borkmannc2101292015-10-29 14:58:07 +0100597 prog = __bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700598 if (IS_ERR(prog))
599 return prog;
600
601 atomic_inc(&prog->aux->refcnt);
602 fdput(f);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100603
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700604 return prog;
605}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100606EXPORT_SYMBOL_GPL(bpf_prog_get);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700607
608/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700609#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700610
611static int bpf_prog_load(union bpf_attr *attr)
612{
613 enum bpf_prog_type type = attr->prog_type;
614 struct bpf_prog *prog;
615 int err;
616 char license[128];
617 bool is_gpl;
618
619 if (CHECK_ATTR(BPF_PROG_LOAD))
620 return -EINVAL;
621
622 /* copy eBPF program license from user space */
623 if (strncpy_from_user(license, u64_to_ptr(attr->license),
624 sizeof(license) - 1) < 0)
625 return -EFAULT;
626 license[sizeof(license) - 1] = 0;
627
628 /* eBPF programs must be GPL compatible to use GPL-ed functions */
629 is_gpl = license_is_gpl_compatible(license);
630
631 if (attr->insn_cnt >= BPF_MAXINSNS)
632 return -EINVAL;
633
Alexei Starovoitov25415172015-03-25 12:49:20 -0700634 if (type == BPF_PROG_TYPE_KPROBE &&
635 attr->kern_version != LINUX_VERSION_CODE)
636 return -EINVAL;
637
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700638 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
639 return -EPERM;
640
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700641 /* plain bpf_prog allocation */
642 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
643 if (!prog)
644 return -ENOMEM;
645
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700646 err = bpf_prog_charge_memlock(prog);
647 if (err)
648 goto free_prog_nouncharge;
649
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700650 prog->len = attr->insn_cnt;
651
652 err = -EFAULT;
653 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
654 prog->len * sizeof(struct bpf_insn)) != 0)
655 goto free_prog;
656
657 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200658 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700659
660 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200661 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700662
663 /* find program type: socket_filter vs tracing_filter */
664 err = find_prog_type(type, prog);
665 if (err < 0)
666 goto free_prog;
667
668 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700669 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700670 if (err < 0)
671 goto free_used_maps;
672
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700673 /* fixup BPF_CALL->imm field */
674 fixup_bpf_calls(prog);
675
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700676 /* eBPF program is ready to be JITed */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700677 err = bpf_prog_select_runtime(prog);
678 if (err < 0)
679 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700680
Daniel Borkmannaa797812015-10-29 14:58:06 +0100681 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700682 if (err < 0)
683 /* failed to allocate fd */
684 goto free_used_maps;
685
686 return err;
687
688free_used_maps:
689 free_used_maps(prog->aux);
690free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700691 bpf_prog_uncharge_memlock(prog);
692free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700693 bpf_prog_free(prog);
694 return err;
695}
696
Daniel Borkmannb2197752015-10-29 14:58:09 +0100697#define BPF_OBJ_LAST_FIELD bpf_fd
698
699static int bpf_obj_pin(const union bpf_attr *attr)
700{
701 if (CHECK_ATTR(BPF_OBJ))
702 return -EINVAL;
703
704 return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
705}
706
707static int bpf_obj_get(const union bpf_attr *attr)
708{
709 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
710 return -EINVAL;
711
712 return bpf_obj_get_user(u64_to_ptr(attr->pathname));
713}
714
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700715SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
716{
717 union bpf_attr attr = {};
718 int err;
719
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700720 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700721 return -EPERM;
722
723 if (!access_ok(VERIFY_READ, uattr, 1))
724 return -EFAULT;
725
726 if (size > PAGE_SIZE) /* silly large */
727 return -E2BIG;
728
729 /* If we're handed a bigger struct than we know of,
730 * ensure all the unknown bits are 0 - i.e. new
731 * user-space does not rely on any kernel feature
732 * extensions we dont know about yet.
733 */
734 if (size > sizeof(attr)) {
735 unsigned char __user *addr;
736 unsigned char __user *end;
737 unsigned char val;
738
739 addr = (void __user *)uattr + sizeof(attr);
740 end = (void __user *)uattr + size;
741
742 for (; addr < end; addr++) {
743 err = get_user(val, addr);
744 if (err)
745 return err;
746 if (val)
747 return -E2BIG;
748 }
749 size = sizeof(attr);
750 }
751
752 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
753 if (copy_from_user(&attr, uattr, size) != 0)
754 return -EFAULT;
755
756 switch (cmd) {
757 case BPF_MAP_CREATE:
758 err = map_create(&attr);
759 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700760 case BPF_MAP_LOOKUP_ELEM:
761 err = map_lookup_elem(&attr);
762 break;
763 case BPF_MAP_UPDATE_ELEM:
764 err = map_update_elem(&attr);
765 break;
766 case BPF_MAP_DELETE_ELEM:
767 err = map_delete_elem(&attr);
768 break;
769 case BPF_MAP_GET_NEXT_KEY:
770 err = map_get_next_key(&attr);
771 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700772 case BPF_PROG_LOAD:
773 err = bpf_prog_load(&attr);
774 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100775 case BPF_OBJ_PIN:
776 err = bpf_obj_pin(&attr);
777 break;
778 case BPF_OBJ_GET:
779 err = bpf_obj_get(&attr);
780 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700781 default:
782 err = -EINVAL;
783 break;
784 }
785
786 return err;
787}