blob: e13157fd425396d43247b3cd8291a573f9098a98 [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)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100197 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700198
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:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100207 bpf_map_uncharge_memlock(map);
208free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700209 map->ops->map_free(map);
210 return err;
211}
212
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700213/* if error is returned, fd is released.
214 * On success caller should complete fd access with matching fdput()
215 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100216struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700217{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700218 if (!f.file)
219 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700220 if (f.file->f_op != &bpf_map_fops) {
221 fdput(f);
222 return ERR_PTR(-EINVAL);
223 }
224
Daniel Borkmannc2101292015-10-29 14:58:07 +0100225 return f.file->private_data;
226}
227
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700228/* prog's and map's refcnt limit */
229#define BPF_MAX_REFCNT 32768
230
231struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100232{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700233 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
234 atomic_dec(&map->refcnt);
235 return ERR_PTR(-EBUSY);
236 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100237 if (uref)
238 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700239 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100240}
241
242struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100243{
244 struct fd f = fdget(ufd);
245 struct bpf_map *map;
246
247 map = __bpf_map_get(f);
248 if (IS_ERR(map))
249 return map;
250
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700251 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100252 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700253
254 return map;
255}
256
257/* helper to convert user pointers passed inside __aligned_u64 fields */
258static void __user *u64_to_ptr(__u64 val)
259{
260 return (void __user *) (unsigned long) val;
261}
262
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800263int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
264{
265 return -ENOTSUPP;
266}
267
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700268/* last field in 'union bpf_attr' used by this command */
269#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
270
271static int map_lookup_elem(union bpf_attr *attr)
272{
273 void __user *ukey = u64_to_ptr(attr->key);
274 void __user *uvalue = u64_to_ptr(attr->value);
275 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700276 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800277 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800278 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200279 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700280 int err;
281
282 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
283 return -EINVAL;
284
Daniel Borkmann592867b2015-09-08 18:00:09 +0200285 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100286 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700287 if (IS_ERR(map))
288 return PTR_ERR(map);
289
290 err = -ENOMEM;
291 key = kmalloc(map->key_size, GFP_USER);
292 if (!key)
293 goto err_put;
294
295 err = -EFAULT;
296 if (copy_from_user(key, ukey, map->key_size) != 0)
297 goto free_key;
298
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800299 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
300 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
301 value_size = round_up(map->value_size, 8) * num_possible_cpus();
302 else
303 value_size = map->value_size;
304
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800305 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800306 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700307 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800308 goto free_key;
309
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800310 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
311 err = bpf_percpu_hash_copy(map, key, value);
312 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
313 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800314 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
315 err = bpf_stackmap_copy(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800316 } else {
317 rcu_read_lock();
318 ptr = map->ops->map_lookup_elem(map, key);
319 if (ptr)
320 memcpy(value, ptr, value_size);
321 rcu_read_unlock();
322 err = ptr ? 0 : -ENOENT;
323 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800324
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800325 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800326 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700327
328 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800329 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800330 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700331
332 err = 0;
333
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800334free_value:
335 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700336free_key:
337 kfree(key);
338err_put:
339 fdput(f);
340 return err;
341}
342
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800343#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700344
345static int map_update_elem(union bpf_attr *attr)
346{
347 void __user *ukey = u64_to_ptr(attr->key);
348 void __user *uvalue = u64_to_ptr(attr->value);
349 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700350 struct bpf_map *map;
351 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800352 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200353 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700354 int err;
355
356 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
357 return -EINVAL;
358
Daniel Borkmann592867b2015-09-08 18:00:09 +0200359 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100360 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700361 if (IS_ERR(map))
362 return PTR_ERR(map);
363
364 err = -ENOMEM;
365 key = kmalloc(map->key_size, GFP_USER);
366 if (!key)
367 goto err_put;
368
369 err = -EFAULT;
370 if (copy_from_user(key, ukey, map->key_size) != 0)
371 goto free_key;
372
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800373 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
374 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
375 value_size = round_up(map->value_size, 8) * num_possible_cpus();
376 else
377 value_size = map->value_size;
378
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700379 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800380 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700381 if (!value)
382 goto free_key;
383
384 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800385 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700386 goto free_value;
387
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800388 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
389 * inside bpf map update or delete otherwise deadlocks are possible
390 */
391 preempt_disable();
392 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800393 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
394 err = bpf_percpu_hash_update(map, key, value, attr->flags);
395 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
396 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200397 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700398 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
399 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200400 rcu_read_lock();
401 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
402 attr->flags);
403 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800404 } else {
405 rcu_read_lock();
406 err = map->ops->map_update_elem(map, key, value, attr->flags);
407 rcu_read_unlock();
408 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800409 __this_cpu_dec(bpf_prog_active);
410 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700411
412free_value:
413 kfree(value);
414free_key:
415 kfree(key);
416err_put:
417 fdput(f);
418 return err;
419}
420
421#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
422
423static int map_delete_elem(union bpf_attr *attr)
424{
425 void __user *ukey = u64_to_ptr(attr->key);
426 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700427 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200428 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700429 void *key;
430 int err;
431
432 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
433 return -EINVAL;
434
Daniel Borkmann592867b2015-09-08 18:00:09 +0200435 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100436 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700437 if (IS_ERR(map))
438 return PTR_ERR(map);
439
440 err = -ENOMEM;
441 key = kmalloc(map->key_size, GFP_USER);
442 if (!key)
443 goto err_put;
444
445 err = -EFAULT;
446 if (copy_from_user(key, ukey, map->key_size) != 0)
447 goto free_key;
448
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800449 preempt_disable();
450 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700451 rcu_read_lock();
452 err = map->ops->map_delete_elem(map, key);
453 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800454 __this_cpu_dec(bpf_prog_active);
455 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700456
457free_key:
458 kfree(key);
459err_put:
460 fdput(f);
461 return err;
462}
463
464/* last field in 'union bpf_attr' used by this command */
465#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
466
467static int map_get_next_key(union bpf_attr *attr)
468{
469 void __user *ukey = u64_to_ptr(attr->key);
470 void __user *unext_key = u64_to_ptr(attr->next_key);
471 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700472 struct bpf_map *map;
473 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200474 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700475 int err;
476
477 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
478 return -EINVAL;
479
Daniel Borkmann592867b2015-09-08 18:00:09 +0200480 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100481 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700482 if (IS_ERR(map))
483 return PTR_ERR(map);
484
485 err = -ENOMEM;
486 key = kmalloc(map->key_size, GFP_USER);
487 if (!key)
488 goto err_put;
489
490 err = -EFAULT;
491 if (copy_from_user(key, ukey, map->key_size) != 0)
492 goto free_key;
493
494 err = -ENOMEM;
495 next_key = kmalloc(map->key_size, GFP_USER);
496 if (!next_key)
497 goto free_key;
498
499 rcu_read_lock();
500 err = map->ops->map_get_next_key(map, key, next_key);
501 rcu_read_unlock();
502 if (err)
503 goto free_next_key;
504
505 err = -EFAULT;
506 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
507 goto free_next_key;
508
509 err = 0;
510
511free_next_key:
512 kfree(next_key);
513free_key:
514 kfree(key);
515err_put:
516 fdput(f);
517 return err;
518}
519
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700520static LIST_HEAD(bpf_prog_types);
521
522static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
523{
524 struct bpf_prog_type_list *tl;
525
526 list_for_each_entry(tl, &bpf_prog_types, list_node) {
527 if (tl->type == type) {
528 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100529 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700530 return 0;
531 }
532 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100533
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700534 return -EINVAL;
535}
536
537void bpf_register_prog_type(struct bpf_prog_type_list *tl)
538{
539 list_add(&tl->list_node, &bpf_prog_types);
540}
541
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700542/* fixup insn->imm field of bpf_call instructions:
543 * if (insn->imm == BPF_FUNC_map_lookup_elem)
544 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
545 * else if (insn->imm == BPF_FUNC_map_update_elem)
546 * insn->imm = bpf_map_update_elem - __bpf_call_base;
547 * else ...
548 *
549 * this function is called after eBPF program passed verification
550 */
551static void fixup_bpf_calls(struct bpf_prog *prog)
552{
553 const struct bpf_func_proto *fn;
554 int i;
555
556 for (i = 0; i < prog->len; i++) {
557 struct bpf_insn *insn = &prog->insnsi[i];
558
559 if (insn->code == (BPF_JMP | BPF_CALL)) {
560 /* we reach here when program has bpf_call instructions
561 * and it passed bpf_check(), means that
562 * ops->get_func_proto must have been supplied, check it
563 */
564 BUG_ON(!prog->aux->ops->get_func_proto);
565
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200566 if (insn->imm == BPF_FUNC_get_route_realm)
567 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200568 if (insn->imm == BPF_FUNC_get_prandom_u32)
569 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700570 if (insn->imm == BPF_FUNC_tail_call) {
571 /* mark bpf_tail_call as different opcode
572 * to avoid conditional branch in
573 * interpeter for every normal call
574 * and to prevent accidental JITing by
575 * JIT compiler that doesn't support
576 * bpf_tail_call yet
577 */
578 insn->imm = 0;
579 insn->code |= BPF_X;
580 continue;
581 }
582
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700583 fn = prog->aux->ops->get_func_proto(insn->imm);
584 /* all functions that have prototype and verifier allowed
585 * programs to call them, must be real in-kernel functions
586 */
587 BUG_ON(!fn->func);
588 insn->imm = fn->func - __bpf_call_base;
589 }
590 }
591}
592
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700593/* drop refcnt on maps used by eBPF program and free auxilary data */
594static void free_used_maps(struct bpf_prog_aux *aux)
595{
596 int i;
597
598 for (i = 0; i < aux->used_map_cnt; i++)
599 bpf_map_put(aux->used_maps[i]);
600
601 kfree(aux->used_maps);
602}
603
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700604static int bpf_prog_charge_memlock(struct bpf_prog *prog)
605{
606 struct user_struct *user = get_current_user();
607 unsigned long memlock_limit;
608
609 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
610
611 atomic_long_add(prog->pages, &user->locked_vm);
612 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
613 atomic_long_sub(prog->pages, &user->locked_vm);
614 free_uid(user);
615 return -EPERM;
616 }
617 prog->aux->user = user;
618 return 0;
619}
620
621static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
622{
623 struct user_struct *user = prog->aux->user;
624
625 atomic_long_sub(prog->pages, &user->locked_vm);
626 free_uid(user);
627}
628
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200629static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700630{
631 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
632
633 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700634 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700635 bpf_prog_free(aux->prog);
636}
637
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700638void bpf_prog_put(struct bpf_prog *prog)
639{
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100640 if (atomic_dec_and_test(&prog->aux->refcnt))
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200641 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700642}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100643EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700644
645static int bpf_prog_release(struct inode *inode, struct file *filp)
646{
647 struct bpf_prog *prog = filp->private_data;
648
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200649 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700650 return 0;
651}
652
653static const struct file_operations bpf_prog_fops = {
654 .release = bpf_prog_release,
655};
656
Daniel Borkmannb2197752015-10-29 14:58:09 +0100657int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100658{
659 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
660 O_RDWR | O_CLOEXEC);
661}
662
Daniel Borkmann113214b2016-06-30 17:24:44 +0200663static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700664{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700665 if (!f.file)
666 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700667 if (f.file->f_op != &bpf_prog_fops) {
668 fdput(f);
669 return ERR_PTR(-EINVAL);
670 }
671
Daniel Borkmannc2101292015-10-29 14:58:07 +0100672 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700673}
674
Brenden Blanco59d36562016-07-19 12:16:46 -0700675struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700676{
Brenden Blanco59d36562016-07-19 12:16:46 -0700677 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
678 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700679 return ERR_PTR(-EBUSY);
680 }
681 return prog;
682}
Brenden Blanco59d36562016-07-19 12:16:46 -0700683EXPORT_SYMBOL_GPL(bpf_prog_add);
684
685struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
686{
687 return bpf_prog_add(prog, 1);
688}
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700689
Daniel Borkmann113214b2016-06-30 17:24:44 +0200690static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700691{
692 struct fd f = fdget(ufd);
693 struct bpf_prog *prog;
694
Daniel Borkmann113214b2016-06-30 17:24:44 +0200695 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700696 if (IS_ERR(prog))
697 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200698 if (type && prog->type != *type) {
699 prog = ERR_PTR(-EINVAL);
700 goto out;
701 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700702
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700703 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200704out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700705 fdput(f);
706 return prog;
707}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200708
709struct bpf_prog *bpf_prog_get(u32 ufd)
710{
711 return __bpf_prog_get(ufd, NULL);
712}
713
714struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
715{
716 return __bpf_prog_get(ufd, &type);
717}
718EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700719
720/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700721#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700722
723static int bpf_prog_load(union bpf_attr *attr)
724{
725 enum bpf_prog_type type = attr->prog_type;
726 struct bpf_prog *prog;
727 int err;
728 char license[128];
729 bool is_gpl;
730
731 if (CHECK_ATTR(BPF_PROG_LOAD))
732 return -EINVAL;
733
734 /* copy eBPF program license from user space */
735 if (strncpy_from_user(license, u64_to_ptr(attr->license),
736 sizeof(license) - 1) < 0)
737 return -EFAULT;
738 license[sizeof(license) - 1] = 0;
739
740 /* eBPF programs must be GPL compatible to use GPL-ed functions */
741 is_gpl = license_is_gpl_compatible(license);
742
743 if (attr->insn_cnt >= BPF_MAXINSNS)
744 return -EINVAL;
745
Alexei Starovoitov25415172015-03-25 12:49:20 -0700746 if (type == BPF_PROG_TYPE_KPROBE &&
747 attr->kern_version != LINUX_VERSION_CODE)
748 return -EINVAL;
749
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700750 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
751 return -EPERM;
752
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700753 /* plain bpf_prog allocation */
754 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
755 if (!prog)
756 return -ENOMEM;
757
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700758 err = bpf_prog_charge_memlock(prog);
759 if (err)
760 goto free_prog_nouncharge;
761
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700762 prog->len = attr->insn_cnt;
763
764 err = -EFAULT;
765 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
766 prog->len * sizeof(struct bpf_insn)) != 0)
767 goto free_prog;
768
769 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200770 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700771
772 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200773 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700774
775 /* find program type: socket_filter vs tracing_filter */
776 err = find_prog_type(type, prog);
777 if (err < 0)
778 goto free_prog;
779
780 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700781 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700782 if (err < 0)
783 goto free_used_maps;
784
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700785 /* fixup BPF_CALL->imm field */
786 fixup_bpf_calls(prog);
787
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700788 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200789 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700790 if (err < 0)
791 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700792
Daniel Borkmannaa797812015-10-29 14:58:06 +0100793 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700794 if (err < 0)
795 /* failed to allocate fd */
796 goto free_used_maps;
797
798 return err;
799
800free_used_maps:
801 free_used_maps(prog->aux);
802free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700803 bpf_prog_uncharge_memlock(prog);
804free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700805 bpf_prog_free(prog);
806 return err;
807}
808
Daniel Borkmannb2197752015-10-29 14:58:09 +0100809#define BPF_OBJ_LAST_FIELD bpf_fd
810
811static int bpf_obj_pin(const union bpf_attr *attr)
812{
813 if (CHECK_ATTR(BPF_OBJ))
814 return -EINVAL;
815
816 return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
817}
818
819static int bpf_obj_get(const union bpf_attr *attr)
820{
821 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
822 return -EINVAL;
823
824 return bpf_obj_get_user(u64_to_ptr(attr->pathname));
825}
826
Daniel Mack00615df2016-11-23 16:52:27 +0100827#ifdef CONFIG_CGROUP_BPF
828
829#define BPF_PROG_ATTACH_LAST_FIELD attach_type
830
831static int bpf_prog_attach(const union bpf_attr *attr)
832{
833 struct bpf_prog *prog;
834 struct cgroup *cgrp;
835
836 if (!capable(CAP_NET_ADMIN))
837 return -EPERM;
838
839 if (CHECK_ATTR(BPF_PROG_ATTACH))
840 return -EINVAL;
841
842 switch (attr->attach_type) {
843 case BPF_CGROUP_INET_INGRESS:
844 case BPF_CGROUP_INET_EGRESS:
845 prog = bpf_prog_get_type(attr->attach_bpf_fd,
846 BPF_PROG_TYPE_CGROUP_SKB);
847 if (IS_ERR(prog))
848 return PTR_ERR(prog);
849
850 cgrp = cgroup_get_from_fd(attr->target_fd);
851 if (IS_ERR(cgrp)) {
852 bpf_prog_put(prog);
853 return PTR_ERR(cgrp);
854 }
855
856 cgroup_bpf_update(cgrp, prog, attr->attach_type);
857 cgroup_put(cgrp);
858 break;
859
860 default:
861 return -EINVAL;
862 }
863
864 return 0;
865}
866
867#define BPF_PROG_DETACH_LAST_FIELD attach_type
868
869static int bpf_prog_detach(const union bpf_attr *attr)
870{
871 struct cgroup *cgrp;
872
873 if (!capable(CAP_NET_ADMIN))
874 return -EPERM;
875
876 if (CHECK_ATTR(BPF_PROG_DETACH))
877 return -EINVAL;
878
879 switch (attr->attach_type) {
880 case BPF_CGROUP_INET_INGRESS:
881 case BPF_CGROUP_INET_EGRESS:
882 cgrp = cgroup_get_from_fd(attr->target_fd);
883 if (IS_ERR(cgrp))
884 return PTR_ERR(cgrp);
885
886 cgroup_bpf_update(cgrp, NULL, attr->attach_type);
887 cgroup_put(cgrp);
888 break;
889
890 default:
891 return -EINVAL;
892 }
893
894 return 0;
895}
896#endif /* CONFIG_CGROUP_BPF */
897
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700898SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
899{
900 union bpf_attr attr = {};
901 int err;
902
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700903 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700904 return -EPERM;
905
906 if (!access_ok(VERIFY_READ, uattr, 1))
907 return -EFAULT;
908
909 if (size > PAGE_SIZE) /* silly large */
910 return -E2BIG;
911
912 /* If we're handed a bigger struct than we know of,
913 * ensure all the unknown bits are 0 - i.e. new
914 * user-space does not rely on any kernel feature
915 * extensions we dont know about yet.
916 */
917 if (size > sizeof(attr)) {
918 unsigned char __user *addr;
919 unsigned char __user *end;
920 unsigned char val;
921
922 addr = (void __user *)uattr + sizeof(attr);
923 end = (void __user *)uattr + size;
924
925 for (; addr < end; addr++) {
926 err = get_user(val, addr);
927 if (err)
928 return err;
929 if (val)
930 return -E2BIG;
931 }
932 size = sizeof(attr);
933 }
934
935 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
936 if (copy_from_user(&attr, uattr, size) != 0)
937 return -EFAULT;
938
939 switch (cmd) {
940 case BPF_MAP_CREATE:
941 err = map_create(&attr);
942 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700943 case BPF_MAP_LOOKUP_ELEM:
944 err = map_lookup_elem(&attr);
945 break;
946 case BPF_MAP_UPDATE_ELEM:
947 err = map_update_elem(&attr);
948 break;
949 case BPF_MAP_DELETE_ELEM:
950 err = map_delete_elem(&attr);
951 break;
952 case BPF_MAP_GET_NEXT_KEY:
953 err = map_get_next_key(&attr);
954 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700955 case BPF_PROG_LOAD:
956 err = bpf_prog_load(&attr);
957 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100958 case BPF_OBJ_PIN:
959 err = bpf_obj_pin(&attr);
960 break;
961 case BPF_OBJ_GET:
962 err = bpf_obj_get(&attr);
963 break;
Daniel Mack00615df2016-11-23 16:52:27 +0100964
965#ifdef CONFIG_CGROUP_BPF
966 case BPF_PROG_ATTACH:
967 err = bpf_prog_attach(&attr);
968 break;
969 case BPF_PROG_DETACH:
970 err = bpf_prog_detach(&attr);
971 break;
972#endif
973
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700974 default:
975 err = -EINVAL;
976 break;
977 }
978
979 return err;
980}