blob: fc3adcd064b1a8fe8d121abe768888799d8844c8 [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 Starovoitovb121d1e2016-03-07 21:57:13 -080021DEFINE_PER_CPU(int, bpf_prog_active);
22
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070023int sysctl_unprivileged_bpf_disabled __read_mostly;
24
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070025static LIST_HEAD(bpf_map_types);
26
27static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
28{
29 struct bpf_map_type_list *tl;
30 struct bpf_map *map;
31
32 list_for_each_entry(tl, &bpf_map_types, list_node) {
33 if (tl->type == attr->map_type) {
34 map = tl->ops->map_alloc(attr);
35 if (IS_ERR(map))
36 return map;
37 map->ops = tl->ops;
38 map->map_type = attr->map_type;
39 return map;
40 }
41 }
42 return ERR_PTR(-EINVAL);
43}
44
45/* boot time registration of different map implementations */
46void bpf_register_map_type(struct bpf_map_type_list *tl)
47{
48 list_add(&tl->list_node, &bpf_map_types);
49}
50
Alexei Starovoitov6c905982016-03-07 21:57:15 -080051int bpf_map_precharge_memlock(u32 pages)
52{
53 struct user_struct *user = get_current_user();
54 unsigned long memlock_limit, cur;
55
56 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
57 cur = atomic_long_read(&user->locked_vm);
58 free_uid(user);
59 if (cur + pages > memlock_limit)
60 return -EPERM;
61 return 0;
62}
63
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070064static int bpf_map_charge_memlock(struct bpf_map *map)
65{
66 struct user_struct *user = get_current_user();
67 unsigned long memlock_limit;
68
69 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
70
71 atomic_long_add(map->pages, &user->locked_vm);
72
73 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
74 atomic_long_sub(map->pages, &user->locked_vm);
75 free_uid(user);
76 return -EPERM;
77 }
78 map->user = user;
79 return 0;
80}
81
82static void bpf_map_uncharge_memlock(struct bpf_map *map)
83{
84 struct user_struct *user = map->user;
85
86 atomic_long_sub(map->pages, &user->locked_vm);
87 free_uid(user);
88}
89
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070090/* called from workqueue */
91static void bpf_map_free_deferred(struct work_struct *work)
92{
93 struct bpf_map *map = container_of(work, struct bpf_map, work);
94
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070095 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070096 /* implementation dependent freeing */
97 map->ops->map_free(map);
98}
99
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100100static void bpf_map_put_uref(struct bpf_map *map)
101{
102 if (atomic_dec_and_test(&map->usercnt)) {
103 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
104 bpf_fd_array_map_clear(map);
105 }
106}
107
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700108/* decrement map refcnt and schedule it for freeing via workqueue
109 * (unrelying map implementation ops->map_free() might sleep)
110 */
111void bpf_map_put(struct bpf_map *map)
112{
113 if (atomic_dec_and_test(&map->refcnt)) {
114 INIT_WORK(&map->work, bpf_map_free_deferred);
115 schedule_work(&map->work);
116 }
117}
118
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100119void bpf_map_put_with_uref(struct bpf_map *map)
120{
121 bpf_map_put_uref(map);
122 bpf_map_put(map);
123}
124
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700125static int bpf_map_release(struct inode *inode, struct file *filp)
126{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200127 struct bpf_map *map = filp->private_data;
128
129 if (map->ops->map_release)
130 map->ops->map_release(map, filp);
131
132 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700133 return 0;
134}
135
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100136#ifdef CONFIG_PROC_FS
137static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
138{
139 const struct bpf_map *map = filp->private_data;
140
141 seq_printf(m,
142 "map_type:\t%u\n"
143 "key_size:\t%u\n"
144 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100145 "max_entries:\t%u\n"
146 "map_flags:\t%#x\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100147 map->map_type,
148 map->key_size,
149 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100150 map->max_entries,
151 map->map_flags);
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100152}
153#endif
154
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700155static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100156#ifdef CONFIG_PROC_FS
157 .show_fdinfo = bpf_map_show_fdinfo,
158#endif
159 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700160};
161
Daniel Borkmannb2197752015-10-29 14:58:09 +0100162int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100163{
164 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
165 O_RDWR | O_CLOEXEC);
166}
167
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700168/* helper macro to check that unused fields 'union bpf_attr' are zero */
169#define CHECK_ATTR(CMD) \
170 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
171 sizeof(attr->CMD##_LAST_FIELD), 0, \
172 sizeof(*attr) - \
173 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
174 sizeof(attr->CMD##_LAST_FIELD)) != NULL
175
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800176#define BPF_MAP_CREATE_LAST_FIELD map_flags
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700177/* called via syscall */
178static int map_create(union bpf_attr *attr)
179{
180 struct bpf_map *map;
181 int err;
182
183 err = CHECK_ATTR(BPF_MAP_CREATE);
184 if (err)
185 return -EINVAL;
186
187 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
188 map = find_and_alloc_map(attr);
189 if (IS_ERR(map))
190 return PTR_ERR(map);
191
192 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100193 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700194
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700195 err = bpf_map_charge_memlock(map);
196 if (err)
197 goto free_map;
198
Daniel Borkmannaa797812015-10-29 14:58:06 +0100199 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700200 if (err < 0)
201 /* failed to allocate fd */
202 goto free_map;
203
204 return err;
205
206free_map:
207 map->ops->map_free(map);
208 return err;
209}
210
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700211/* if error is returned, fd is released.
212 * On success caller should complete fd access with matching fdput()
213 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100214struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700215{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700216 if (!f.file)
217 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700218 if (f.file->f_op != &bpf_map_fops) {
219 fdput(f);
220 return ERR_PTR(-EINVAL);
221 }
222
Daniel Borkmannc2101292015-10-29 14:58:07 +0100223 return f.file->private_data;
224}
225
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700226/* prog's and map's refcnt limit */
227#define BPF_MAX_REFCNT 32768
228
229struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100230{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700231 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
232 atomic_dec(&map->refcnt);
233 return ERR_PTR(-EBUSY);
234 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100235 if (uref)
236 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700237 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100238}
239
240struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100241{
242 struct fd f = fdget(ufd);
243 struct bpf_map *map;
244
245 map = __bpf_map_get(f);
246 if (IS_ERR(map))
247 return map;
248
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700249 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100250 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700251
252 return map;
253}
254
255/* helper to convert user pointers passed inside __aligned_u64 fields */
256static void __user *u64_to_ptr(__u64 val)
257{
258 return (void __user *) (unsigned long) val;
259}
260
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800261int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
262{
263 return -ENOTSUPP;
264}
265
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700266/* last field in 'union bpf_attr' used by this command */
267#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
268
269static int map_lookup_elem(union bpf_attr *attr)
270{
271 void __user *ukey = u64_to_ptr(attr->key);
272 void __user *uvalue = u64_to_ptr(attr->value);
273 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700274 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800275 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800276 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200277 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700278 int err;
279
280 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
281 return -EINVAL;
282
Daniel Borkmann592867b2015-09-08 18:00:09 +0200283 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100284 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700285 if (IS_ERR(map))
286 return PTR_ERR(map);
287
288 err = -ENOMEM;
289 key = kmalloc(map->key_size, GFP_USER);
290 if (!key)
291 goto err_put;
292
293 err = -EFAULT;
294 if (copy_from_user(key, ukey, map->key_size) != 0)
295 goto free_key;
296
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800297 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
298 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
299 value_size = round_up(map->value_size, 8) * num_possible_cpus();
300 else
301 value_size = map->value_size;
302
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800303 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800304 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700305 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800306 goto free_key;
307
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800308 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
309 err = bpf_percpu_hash_copy(map, key, value);
310 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
311 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800312 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
313 err = bpf_stackmap_copy(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800314 } else {
315 rcu_read_lock();
316 ptr = map->ops->map_lookup_elem(map, key);
317 if (ptr)
318 memcpy(value, ptr, value_size);
319 rcu_read_unlock();
320 err = ptr ? 0 : -ENOENT;
321 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800322
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800323 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800324 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700325
326 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800327 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800328 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700329
330 err = 0;
331
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800332free_value:
333 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700334free_key:
335 kfree(key);
336err_put:
337 fdput(f);
338 return err;
339}
340
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800341#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700342
343static int map_update_elem(union bpf_attr *attr)
344{
345 void __user *ukey = u64_to_ptr(attr->key);
346 void __user *uvalue = u64_to_ptr(attr->value);
347 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700348 struct bpf_map *map;
349 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800350 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200351 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700352 int err;
353
354 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
355 return -EINVAL;
356
Daniel Borkmann592867b2015-09-08 18:00:09 +0200357 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100358 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700359 if (IS_ERR(map))
360 return PTR_ERR(map);
361
362 err = -ENOMEM;
363 key = kmalloc(map->key_size, GFP_USER);
364 if (!key)
365 goto err_put;
366
367 err = -EFAULT;
368 if (copy_from_user(key, ukey, map->key_size) != 0)
369 goto free_key;
370
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800371 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
372 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
373 value_size = round_up(map->value_size, 8) * num_possible_cpus();
374 else
375 value_size = map->value_size;
376
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700377 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800378 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700379 if (!value)
380 goto free_key;
381
382 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800383 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700384 goto free_value;
385
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800386 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
387 * inside bpf map update or delete otherwise deadlocks are possible
388 */
389 preempt_disable();
390 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800391 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
392 err = bpf_percpu_hash_update(map, key, value, attr->flags);
393 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
394 err = bpf_percpu_array_update(map, key, value, attr->flags);
395 } else {
396 rcu_read_lock();
397 err = map->ops->map_update_elem(map, key, value, attr->flags);
398 rcu_read_unlock();
399 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800400 __this_cpu_dec(bpf_prog_active);
401 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700402
403free_value:
404 kfree(value);
405free_key:
406 kfree(key);
407err_put:
408 fdput(f);
409 return err;
410}
411
412#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
413
414static int map_delete_elem(union bpf_attr *attr)
415{
416 void __user *ukey = u64_to_ptr(attr->key);
417 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700418 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200419 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700420 void *key;
421 int err;
422
423 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
424 return -EINVAL;
425
Daniel Borkmann592867b2015-09-08 18:00:09 +0200426 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100427 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700428 if (IS_ERR(map))
429 return PTR_ERR(map);
430
431 err = -ENOMEM;
432 key = kmalloc(map->key_size, GFP_USER);
433 if (!key)
434 goto err_put;
435
436 err = -EFAULT;
437 if (copy_from_user(key, ukey, map->key_size) != 0)
438 goto free_key;
439
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800440 preempt_disable();
441 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700442 rcu_read_lock();
443 err = map->ops->map_delete_elem(map, key);
444 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800445 __this_cpu_dec(bpf_prog_active);
446 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700447
448free_key:
449 kfree(key);
450err_put:
451 fdput(f);
452 return err;
453}
454
455/* last field in 'union bpf_attr' used by this command */
456#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
457
458static int map_get_next_key(union bpf_attr *attr)
459{
460 void __user *ukey = u64_to_ptr(attr->key);
461 void __user *unext_key = u64_to_ptr(attr->next_key);
462 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700463 struct bpf_map *map;
464 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200465 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700466 int err;
467
468 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
469 return -EINVAL;
470
Daniel Borkmann592867b2015-09-08 18:00:09 +0200471 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100472 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700473 if (IS_ERR(map))
474 return PTR_ERR(map);
475
476 err = -ENOMEM;
477 key = kmalloc(map->key_size, GFP_USER);
478 if (!key)
479 goto err_put;
480
481 err = -EFAULT;
482 if (copy_from_user(key, ukey, map->key_size) != 0)
483 goto free_key;
484
485 err = -ENOMEM;
486 next_key = kmalloc(map->key_size, GFP_USER);
487 if (!next_key)
488 goto free_key;
489
490 rcu_read_lock();
491 err = map->ops->map_get_next_key(map, key, next_key);
492 rcu_read_unlock();
493 if (err)
494 goto free_next_key;
495
496 err = -EFAULT;
497 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
498 goto free_next_key;
499
500 err = 0;
501
502free_next_key:
503 kfree(next_key);
504free_key:
505 kfree(key);
506err_put:
507 fdput(f);
508 return err;
509}
510
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700511static LIST_HEAD(bpf_prog_types);
512
513static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
514{
515 struct bpf_prog_type_list *tl;
516
517 list_for_each_entry(tl, &bpf_prog_types, list_node) {
518 if (tl->type == type) {
519 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100520 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700521 return 0;
522 }
523 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100524
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700525 return -EINVAL;
526}
527
528void bpf_register_prog_type(struct bpf_prog_type_list *tl)
529{
530 list_add(&tl->list_node, &bpf_prog_types);
531}
532
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700533/* fixup insn->imm field of bpf_call instructions:
534 * if (insn->imm == BPF_FUNC_map_lookup_elem)
535 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
536 * else if (insn->imm == BPF_FUNC_map_update_elem)
537 * insn->imm = bpf_map_update_elem - __bpf_call_base;
538 * else ...
539 *
540 * this function is called after eBPF program passed verification
541 */
542static void fixup_bpf_calls(struct bpf_prog *prog)
543{
544 const struct bpf_func_proto *fn;
545 int i;
546
547 for (i = 0; i < prog->len; i++) {
548 struct bpf_insn *insn = &prog->insnsi[i];
549
550 if (insn->code == (BPF_JMP | BPF_CALL)) {
551 /* we reach here when program has bpf_call instructions
552 * and it passed bpf_check(), means that
553 * ops->get_func_proto must have been supplied, check it
554 */
555 BUG_ON(!prog->aux->ops->get_func_proto);
556
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200557 if (insn->imm == BPF_FUNC_get_route_realm)
558 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200559 if (insn->imm == BPF_FUNC_get_prandom_u32)
560 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700561 if (insn->imm == BPF_FUNC_tail_call) {
562 /* mark bpf_tail_call as different opcode
563 * to avoid conditional branch in
564 * interpeter for every normal call
565 * and to prevent accidental JITing by
566 * JIT compiler that doesn't support
567 * bpf_tail_call yet
568 */
569 insn->imm = 0;
570 insn->code |= BPF_X;
571 continue;
572 }
573
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700574 fn = prog->aux->ops->get_func_proto(insn->imm);
575 /* all functions that have prototype and verifier allowed
576 * programs to call them, must be real in-kernel functions
577 */
578 BUG_ON(!fn->func);
579 insn->imm = fn->func - __bpf_call_base;
580 }
581 }
582}
583
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700584/* drop refcnt on maps used by eBPF program and free auxilary data */
585static void free_used_maps(struct bpf_prog_aux *aux)
586{
587 int i;
588
589 for (i = 0; i < aux->used_map_cnt; i++)
590 bpf_map_put(aux->used_maps[i]);
591
592 kfree(aux->used_maps);
593}
594
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700595static int bpf_prog_charge_memlock(struct bpf_prog *prog)
596{
597 struct user_struct *user = get_current_user();
598 unsigned long memlock_limit;
599
600 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
601
602 atomic_long_add(prog->pages, &user->locked_vm);
603 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
604 atomic_long_sub(prog->pages, &user->locked_vm);
605 free_uid(user);
606 return -EPERM;
607 }
608 prog->aux->user = user;
609 return 0;
610}
611
612static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
613{
614 struct user_struct *user = prog->aux->user;
615
616 atomic_long_sub(prog->pages, &user->locked_vm);
617 free_uid(user);
618}
619
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100620static void __prog_put_common(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700621{
622 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
623
624 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700625 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700626 bpf_prog_free(aux->prog);
627}
628
629/* version of bpf_prog_put() that is called after a grace period */
630void bpf_prog_put_rcu(struct bpf_prog *prog)
631{
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100632 if (atomic_dec_and_test(&prog->aux->refcnt))
633 call_rcu(&prog->aux->rcu, __prog_put_common);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700634}
635
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700636void bpf_prog_put(struct bpf_prog *prog)
637{
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100638 if (atomic_dec_and_test(&prog->aux->refcnt))
639 __prog_put_common(&prog->aux->rcu);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700640}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100641EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700642
643static int bpf_prog_release(struct inode *inode, struct file *filp)
644{
645 struct bpf_prog *prog = filp->private_data;
646
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700647 bpf_prog_put_rcu(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700648 return 0;
649}
650
651static const struct file_operations bpf_prog_fops = {
652 .release = bpf_prog_release,
653};
654
Daniel Borkmannb2197752015-10-29 14:58:09 +0100655int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100656{
657 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
658 O_RDWR | O_CLOEXEC);
659}
660
Daniel Borkmannc2101292015-10-29 14:58:07 +0100661static struct bpf_prog *__bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700662{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700663 if (!f.file)
664 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700665 if (f.file->f_op != &bpf_prog_fops) {
666 fdput(f);
667 return ERR_PTR(-EINVAL);
668 }
669
Daniel Borkmannc2101292015-10-29 14:58:07 +0100670 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700671}
672
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700673struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
674{
675 if (atomic_inc_return(&prog->aux->refcnt) > BPF_MAX_REFCNT) {
676 atomic_dec(&prog->aux->refcnt);
677 return ERR_PTR(-EBUSY);
678 }
679 return prog;
680}
681
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700682/* called by sockets/tracing/seccomp before attaching program to an event
683 * pairs with bpf_prog_put()
684 */
685struct bpf_prog *bpf_prog_get(u32 ufd)
686{
687 struct fd f = fdget(ufd);
688 struct bpf_prog *prog;
689
Daniel Borkmannc2101292015-10-29 14:58:07 +0100690 prog = __bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700691 if (IS_ERR(prog))
692 return prog;
693
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700694 prog = bpf_prog_inc(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700695 fdput(f);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100696
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700697 return prog;
698}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100699EXPORT_SYMBOL_GPL(bpf_prog_get);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700700
701/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700702#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700703
704static int bpf_prog_load(union bpf_attr *attr)
705{
706 enum bpf_prog_type type = attr->prog_type;
707 struct bpf_prog *prog;
708 int err;
709 char license[128];
710 bool is_gpl;
711
712 if (CHECK_ATTR(BPF_PROG_LOAD))
713 return -EINVAL;
714
715 /* copy eBPF program license from user space */
716 if (strncpy_from_user(license, u64_to_ptr(attr->license),
717 sizeof(license) - 1) < 0)
718 return -EFAULT;
719 license[sizeof(license) - 1] = 0;
720
721 /* eBPF programs must be GPL compatible to use GPL-ed functions */
722 is_gpl = license_is_gpl_compatible(license);
723
724 if (attr->insn_cnt >= BPF_MAXINSNS)
725 return -EINVAL;
726
Alexei Starovoitov25415172015-03-25 12:49:20 -0700727 if (type == BPF_PROG_TYPE_KPROBE &&
728 attr->kern_version != LINUX_VERSION_CODE)
729 return -EINVAL;
730
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700731 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
732 return -EPERM;
733
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700734 /* plain bpf_prog allocation */
735 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
736 if (!prog)
737 return -ENOMEM;
738
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700739 err = bpf_prog_charge_memlock(prog);
740 if (err)
741 goto free_prog_nouncharge;
742
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700743 prog->len = attr->insn_cnt;
744
745 err = -EFAULT;
746 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
747 prog->len * sizeof(struct bpf_insn)) != 0)
748 goto free_prog;
749
750 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200751 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700752
753 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200754 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700755
756 /* find program type: socket_filter vs tracing_filter */
757 err = find_prog_type(type, prog);
758 if (err < 0)
759 goto free_prog;
760
761 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700762 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700763 if (err < 0)
764 goto free_used_maps;
765
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700766 /* fixup BPF_CALL->imm field */
767 fixup_bpf_calls(prog);
768
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700769 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200770 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700771 if (err < 0)
772 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700773
Daniel Borkmannaa797812015-10-29 14:58:06 +0100774 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700775 if (err < 0)
776 /* failed to allocate fd */
777 goto free_used_maps;
778
779 return err;
780
781free_used_maps:
782 free_used_maps(prog->aux);
783free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700784 bpf_prog_uncharge_memlock(prog);
785free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700786 bpf_prog_free(prog);
787 return err;
788}
789
Daniel Borkmannb2197752015-10-29 14:58:09 +0100790#define BPF_OBJ_LAST_FIELD bpf_fd
791
792static int bpf_obj_pin(const union bpf_attr *attr)
793{
794 if (CHECK_ATTR(BPF_OBJ))
795 return -EINVAL;
796
797 return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
798}
799
800static int bpf_obj_get(const union bpf_attr *attr)
801{
802 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
803 return -EINVAL;
804
805 return bpf_obj_get_user(u64_to_ptr(attr->pathname));
806}
807
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700808SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
809{
810 union bpf_attr attr = {};
811 int err;
812
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700813 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700814 return -EPERM;
815
816 if (!access_ok(VERIFY_READ, uattr, 1))
817 return -EFAULT;
818
819 if (size > PAGE_SIZE) /* silly large */
820 return -E2BIG;
821
822 /* If we're handed a bigger struct than we know of,
823 * ensure all the unknown bits are 0 - i.e. new
824 * user-space does not rely on any kernel feature
825 * extensions we dont know about yet.
826 */
827 if (size > sizeof(attr)) {
828 unsigned char __user *addr;
829 unsigned char __user *end;
830 unsigned char val;
831
832 addr = (void __user *)uattr + sizeof(attr);
833 end = (void __user *)uattr + size;
834
835 for (; addr < end; addr++) {
836 err = get_user(val, addr);
837 if (err)
838 return err;
839 if (val)
840 return -E2BIG;
841 }
842 size = sizeof(attr);
843 }
844
845 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
846 if (copy_from_user(&attr, uattr, size) != 0)
847 return -EFAULT;
848
849 switch (cmd) {
850 case BPF_MAP_CREATE:
851 err = map_create(&attr);
852 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700853 case BPF_MAP_LOOKUP_ELEM:
854 err = map_lookup_elem(&attr);
855 break;
856 case BPF_MAP_UPDATE_ELEM:
857 err = map_update_elem(&attr);
858 break;
859 case BPF_MAP_DELETE_ELEM:
860 err = map_delete_elem(&attr);
861 break;
862 case BPF_MAP_GET_NEXT_KEY:
863 err = map_get_next_key(&attr);
864 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700865 case BPF_PROG_LOAD:
866 err = bpf_prog_load(&attr);
867 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100868 case BPF_OBJ_PIN:
869 err = bpf_obj_pin(&attr);
870 break;
871 case BPF_OBJ_GET:
872 err = bpf_obj_get(&attr);
873 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700874 default:
875 err = -EINVAL;
876 break;
877 }
878
879 return err;
880}