blob: 233e3ac836a6689e7aefee822cbd38a3f4d00216 [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>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010020#include <linux/kernel.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070021
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080022DEFINE_PER_CPU(int, bpf_prog_active);
23
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070024int sysctl_unprivileged_bpf_disabled __read_mostly;
25
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070026static LIST_HEAD(bpf_map_types);
27
28static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
29{
30 struct bpf_map_type_list *tl;
31 struct bpf_map *map;
32
33 list_for_each_entry(tl, &bpf_map_types, list_node) {
34 if (tl->type == attr->map_type) {
35 map = tl->ops->map_alloc(attr);
36 if (IS_ERR(map))
37 return map;
38 map->ops = tl->ops;
39 map->map_type = attr->map_type;
40 return map;
41 }
42 }
43 return ERR_PTR(-EINVAL);
44}
45
46/* boot time registration of different map implementations */
47void bpf_register_map_type(struct bpf_map_type_list *tl)
48{
49 list_add(&tl->list_node, &bpf_map_types);
50}
51
Alexei Starovoitov6c905982016-03-07 21:57:15 -080052int bpf_map_precharge_memlock(u32 pages)
53{
54 struct user_struct *user = get_current_user();
55 unsigned long memlock_limit, cur;
56
57 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
58 cur = atomic_long_read(&user->locked_vm);
59 free_uid(user);
60 if (cur + pages > memlock_limit)
61 return -EPERM;
62 return 0;
63}
64
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070065static int bpf_map_charge_memlock(struct bpf_map *map)
66{
67 struct user_struct *user = get_current_user();
68 unsigned long memlock_limit;
69
70 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
71
72 atomic_long_add(map->pages, &user->locked_vm);
73
74 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
75 atomic_long_sub(map->pages, &user->locked_vm);
76 free_uid(user);
77 return -EPERM;
78 }
79 map->user = user;
80 return 0;
81}
82
83static void bpf_map_uncharge_memlock(struct bpf_map *map)
84{
85 struct user_struct *user = map->user;
86
87 atomic_long_sub(map->pages, &user->locked_vm);
88 free_uid(user);
89}
90
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070091/* called from workqueue */
92static void bpf_map_free_deferred(struct work_struct *work)
93{
94 struct bpf_map *map = container_of(work, struct bpf_map, work);
95
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070096 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070097 /* implementation dependent freeing */
98 map->ops->map_free(map);
99}
100
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100101static void bpf_map_put_uref(struct bpf_map *map)
102{
103 if (atomic_dec_and_test(&map->usercnt)) {
104 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
105 bpf_fd_array_map_clear(map);
106 }
107}
108
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700109/* decrement map refcnt and schedule it for freeing via workqueue
110 * (unrelying map implementation ops->map_free() might sleep)
111 */
112void bpf_map_put(struct bpf_map *map)
113{
114 if (atomic_dec_and_test(&map->refcnt)) {
115 INIT_WORK(&map->work, bpf_map_free_deferred);
116 schedule_work(&map->work);
117 }
118}
119
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100120void bpf_map_put_with_uref(struct bpf_map *map)
121{
122 bpf_map_put_uref(map);
123 bpf_map_put(map);
124}
125
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700126static int bpf_map_release(struct inode *inode, struct file *filp)
127{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200128 struct bpf_map *map = filp->private_data;
129
130 if (map->ops->map_release)
131 map->ops->map_release(map, filp);
132
133 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700134 return 0;
135}
136
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100137#ifdef CONFIG_PROC_FS
138static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
139{
140 const struct bpf_map *map = filp->private_data;
141
142 seq_printf(m,
143 "map_type:\t%u\n"
144 "key_size:\t%u\n"
145 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100146 "max_entries:\t%u\n"
147 "map_flags:\t%#x\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100148 map->map_type,
149 map->key_size,
150 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100151 map->max_entries,
152 map->map_flags);
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100153}
154#endif
155
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700156static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100157#ifdef CONFIG_PROC_FS
158 .show_fdinfo = bpf_map_show_fdinfo,
159#endif
160 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700161};
162
Daniel Borkmannb2197752015-10-29 14:58:09 +0100163int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100164{
165 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
166 O_RDWR | O_CLOEXEC);
167}
168
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700169/* helper macro to check that unused fields 'union bpf_attr' are zero */
170#define CHECK_ATTR(CMD) \
171 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
172 sizeof(attr->CMD##_LAST_FIELD), 0, \
173 sizeof(*attr) - \
174 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
175 sizeof(attr->CMD##_LAST_FIELD)) != NULL
176
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800177#define BPF_MAP_CREATE_LAST_FIELD map_flags
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700178/* called via syscall */
179static int map_create(union bpf_attr *attr)
180{
181 struct bpf_map *map;
182 int err;
183
184 err = CHECK_ATTR(BPF_MAP_CREATE);
185 if (err)
186 return -EINVAL;
187
188 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
189 map = find_and_alloc_map(attr);
190 if (IS_ERR(map))
191 return PTR_ERR(map);
192
193 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100194 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700195
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700196 err = bpf_map_charge_memlock(map);
197 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100198 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700199
Daniel Borkmannaa797812015-10-29 14:58:06 +0100200 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700201 if (err < 0)
202 /* failed to allocate fd */
203 goto free_map;
204
205 return err;
206
207free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100208 bpf_map_uncharge_memlock(map);
209free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700210 map->ops->map_free(map);
211 return err;
212}
213
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700214/* if error is returned, fd is released.
215 * On success caller should complete fd access with matching fdput()
216 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100217struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700218{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700219 if (!f.file)
220 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700221 if (f.file->f_op != &bpf_map_fops) {
222 fdput(f);
223 return ERR_PTR(-EINVAL);
224 }
225
Daniel Borkmannc2101292015-10-29 14:58:07 +0100226 return f.file->private_data;
227}
228
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700229/* prog's and map's refcnt limit */
230#define BPF_MAX_REFCNT 32768
231
232struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100233{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700234 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
235 atomic_dec(&map->refcnt);
236 return ERR_PTR(-EBUSY);
237 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100238 if (uref)
239 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700240 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100241}
242
243struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100244{
245 struct fd f = fdget(ufd);
246 struct bpf_map *map;
247
248 map = __bpf_map_get(f);
249 if (IS_ERR(map))
250 return map;
251
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700252 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100253 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700254
255 return map;
256}
257
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800258int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
259{
260 return -ENOTSUPP;
261}
262
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700263/* last field in 'union bpf_attr' used by this command */
264#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
265
266static int map_lookup_elem(union bpf_attr *attr)
267{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100268 void __user *ukey = u64_to_user_ptr(attr->key);
269 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700270 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700271 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800272 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800273 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200274 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700275 int err;
276
277 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
278 return -EINVAL;
279
Daniel Borkmann592867b2015-09-08 18:00:09 +0200280 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100281 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700282 if (IS_ERR(map))
283 return PTR_ERR(map);
284
285 err = -ENOMEM;
286 key = kmalloc(map->key_size, GFP_USER);
287 if (!key)
288 goto err_put;
289
290 err = -EFAULT;
291 if (copy_from_user(key, ukey, map->key_size) != 0)
292 goto free_key;
293
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800294 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
295 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
296 value_size = round_up(map->value_size, 8) * num_possible_cpus();
297 else
298 value_size = map->value_size;
299
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800300 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800301 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700302 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800303 goto free_key;
304
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800305 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
306 err = bpf_percpu_hash_copy(map, key, value);
307 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
308 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800309 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
310 err = bpf_stackmap_copy(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800311 } else {
312 rcu_read_lock();
313 ptr = map->ops->map_lookup_elem(map, key);
314 if (ptr)
315 memcpy(value, ptr, value_size);
316 rcu_read_unlock();
317 err = ptr ? 0 : -ENOENT;
318 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800319
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800320 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800321 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700322
323 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800324 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800325 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700326
327 err = 0;
328
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800329free_value:
330 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700331free_key:
332 kfree(key);
333err_put:
334 fdput(f);
335 return err;
336}
337
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800338#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700339
340static int map_update_elem(union bpf_attr *attr)
341{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100342 void __user *ukey = u64_to_user_ptr(attr->key);
343 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700344 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700345 struct bpf_map *map;
346 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800347 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200348 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700349 int err;
350
351 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
352 return -EINVAL;
353
Daniel Borkmann592867b2015-09-08 18:00:09 +0200354 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100355 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700356 if (IS_ERR(map))
357 return PTR_ERR(map);
358
359 err = -ENOMEM;
360 key = kmalloc(map->key_size, GFP_USER);
361 if (!key)
362 goto err_put;
363
364 err = -EFAULT;
365 if (copy_from_user(key, ukey, map->key_size) != 0)
366 goto free_key;
367
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800368 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
369 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
370 value_size = round_up(map->value_size, 8) * num_possible_cpus();
371 else
372 value_size = map->value_size;
373
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700374 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800375 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700376 if (!value)
377 goto free_key;
378
379 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800380 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700381 goto free_value;
382
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800383 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
384 * inside bpf map update or delete otherwise deadlocks are possible
385 */
386 preempt_disable();
387 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800388 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
389 err = bpf_percpu_hash_update(map, key, value, attr->flags);
390 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
391 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200392 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700393 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
394 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200395 rcu_read_lock();
396 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
397 attr->flags);
398 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800399 } else {
400 rcu_read_lock();
401 err = map->ops->map_update_elem(map, key, value, attr->flags);
402 rcu_read_unlock();
403 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800404 __this_cpu_dec(bpf_prog_active);
405 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700406
407free_value:
408 kfree(value);
409free_key:
410 kfree(key);
411err_put:
412 fdput(f);
413 return err;
414}
415
416#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
417
418static int map_delete_elem(union bpf_attr *attr)
419{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100420 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700421 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700422 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200423 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700424 void *key;
425 int err;
426
427 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
428 return -EINVAL;
429
Daniel Borkmann592867b2015-09-08 18:00:09 +0200430 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100431 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700432 if (IS_ERR(map))
433 return PTR_ERR(map);
434
435 err = -ENOMEM;
436 key = kmalloc(map->key_size, GFP_USER);
437 if (!key)
438 goto err_put;
439
440 err = -EFAULT;
441 if (copy_from_user(key, ukey, map->key_size) != 0)
442 goto free_key;
443
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800444 preempt_disable();
445 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700446 rcu_read_lock();
447 err = map->ops->map_delete_elem(map, key);
448 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800449 __this_cpu_dec(bpf_prog_active);
450 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700451
452free_key:
453 kfree(key);
454err_put:
455 fdput(f);
456 return err;
457}
458
459/* last field in 'union bpf_attr' used by this command */
460#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
461
462static int map_get_next_key(union bpf_attr *attr)
463{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100464 void __user *ukey = u64_to_user_ptr(attr->key);
465 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700466 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700467 struct bpf_map *map;
468 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200469 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700470 int err;
471
472 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
473 return -EINVAL;
474
Daniel Borkmann592867b2015-09-08 18:00:09 +0200475 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100476 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700477 if (IS_ERR(map))
478 return PTR_ERR(map);
479
480 err = -ENOMEM;
481 key = kmalloc(map->key_size, GFP_USER);
482 if (!key)
483 goto err_put;
484
485 err = -EFAULT;
486 if (copy_from_user(key, ukey, map->key_size) != 0)
487 goto free_key;
488
489 err = -ENOMEM;
490 next_key = kmalloc(map->key_size, GFP_USER);
491 if (!next_key)
492 goto free_key;
493
494 rcu_read_lock();
495 err = map->ops->map_get_next_key(map, key, next_key);
496 rcu_read_unlock();
497 if (err)
498 goto free_next_key;
499
500 err = -EFAULT;
501 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
502 goto free_next_key;
503
504 err = 0;
505
506free_next_key:
507 kfree(next_key);
508free_key:
509 kfree(key);
510err_put:
511 fdput(f);
512 return err;
513}
514
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700515static LIST_HEAD(bpf_prog_types);
516
517static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
518{
519 struct bpf_prog_type_list *tl;
520
521 list_for_each_entry(tl, &bpf_prog_types, list_node) {
522 if (tl->type == type) {
523 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100524 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700525 return 0;
526 }
527 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100528
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700529 return -EINVAL;
530}
531
532void bpf_register_prog_type(struct bpf_prog_type_list *tl)
533{
534 list_add(&tl->list_node, &bpf_prog_types);
535}
536
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700537/* fixup insn->imm field of bpf_call instructions:
538 * if (insn->imm == BPF_FUNC_map_lookup_elem)
539 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
540 * else if (insn->imm == BPF_FUNC_map_update_elem)
541 * insn->imm = bpf_map_update_elem - __bpf_call_base;
542 * else ...
543 *
544 * this function is called after eBPF program passed verification
545 */
546static void fixup_bpf_calls(struct bpf_prog *prog)
547{
548 const struct bpf_func_proto *fn;
549 int i;
550
551 for (i = 0; i < prog->len; i++) {
552 struct bpf_insn *insn = &prog->insnsi[i];
553
554 if (insn->code == (BPF_JMP | BPF_CALL)) {
555 /* we reach here when program has bpf_call instructions
556 * and it passed bpf_check(), means that
557 * ops->get_func_proto must have been supplied, check it
558 */
559 BUG_ON(!prog->aux->ops->get_func_proto);
560
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200561 if (insn->imm == BPF_FUNC_get_route_realm)
562 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200563 if (insn->imm == BPF_FUNC_get_prandom_u32)
564 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700565 if (insn->imm == BPF_FUNC_tail_call) {
566 /* mark bpf_tail_call as different opcode
567 * to avoid conditional branch in
568 * interpeter for every normal call
569 * and to prevent accidental JITing by
570 * JIT compiler that doesn't support
571 * bpf_tail_call yet
572 */
573 insn->imm = 0;
574 insn->code |= BPF_X;
575 continue;
576 }
577
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700578 fn = prog->aux->ops->get_func_proto(insn->imm);
579 /* all functions that have prototype and verifier allowed
580 * programs to call them, must be real in-kernel functions
581 */
582 BUG_ON(!fn->func);
583 insn->imm = fn->func - __bpf_call_base;
584 }
585 }
586}
587
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700588/* drop refcnt on maps used by eBPF program and free auxilary data */
589static void free_used_maps(struct bpf_prog_aux *aux)
590{
591 int i;
592
593 for (i = 0; i < aux->used_map_cnt; i++)
594 bpf_map_put(aux->used_maps[i]);
595
596 kfree(aux->used_maps);
597}
598
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700599static int bpf_prog_charge_memlock(struct bpf_prog *prog)
600{
601 struct user_struct *user = get_current_user();
602 unsigned long memlock_limit;
603
604 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
605
606 atomic_long_add(prog->pages, &user->locked_vm);
607 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
608 atomic_long_sub(prog->pages, &user->locked_vm);
609 free_uid(user);
610 return -EPERM;
611 }
612 prog->aux->user = user;
613 return 0;
614}
615
616static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
617{
618 struct user_struct *user = prog->aux->user;
619
620 atomic_long_sub(prog->pages, &user->locked_vm);
621 free_uid(user);
622}
623
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200624static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700625{
626 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
627
628 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700629 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700630 bpf_prog_free(aux->prog);
631}
632
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700633void bpf_prog_put(struct bpf_prog *prog)
634{
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100635 if (atomic_dec_and_test(&prog->aux->refcnt))
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200636 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700637}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100638EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700639
640static int bpf_prog_release(struct inode *inode, struct file *filp)
641{
642 struct bpf_prog *prog = filp->private_data;
643
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200644 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700645 return 0;
646}
647
648static const struct file_operations bpf_prog_fops = {
649 .release = bpf_prog_release,
650};
651
Daniel Borkmannb2197752015-10-29 14:58:09 +0100652int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100653{
654 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
655 O_RDWR | O_CLOEXEC);
656}
657
Daniel Borkmann113214b2016-06-30 17:24:44 +0200658static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700659{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700660 if (!f.file)
661 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700662 if (f.file->f_op != &bpf_prog_fops) {
663 fdput(f);
664 return ERR_PTR(-EINVAL);
665 }
666
Daniel Borkmannc2101292015-10-29 14:58:07 +0100667 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700668}
669
Brenden Blanco59d36562016-07-19 12:16:46 -0700670struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700671{
Brenden Blanco59d36562016-07-19 12:16:46 -0700672 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
673 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700674 return ERR_PTR(-EBUSY);
675 }
676 return prog;
677}
Brenden Blanco59d36562016-07-19 12:16:46 -0700678EXPORT_SYMBOL_GPL(bpf_prog_add);
679
Daniel Borkmannc5405942016-11-09 22:02:34 +0100680void bpf_prog_sub(struct bpf_prog *prog, int i)
681{
682 /* Only to be used for undoing previous bpf_prog_add() in some
683 * error path. We still know that another entity in our call
684 * path holds a reference to the program, thus atomic_sub() can
685 * be safely used in such cases!
686 */
687 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
688}
689EXPORT_SYMBOL_GPL(bpf_prog_sub);
690
Brenden Blanco59d36562016-07-19 12:16:46 -0700691struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
692{
693 return bpf_prog_add(prog, 1);
694}
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700695
Daniel Borkmann113214b2016-06-30 17:24:44 +0200696static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700697{
698 struct fd f = fdget(ufd);
699 struct bpf_prog *prog;
700
Daniel Borkmann113214b2016-06-30 17:24:44 +0200701 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700702 if (IS_ERR(prog))
703 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200704 if (type && prog->type != *type) {
705 prog = ERR_PTR(-EINVAL);
706 goto out;
707 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700708
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700709 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200710out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700711 fdput(f);
712 return prog;
713}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200714
715struct bpf_prog *bpf_prog_get(u32 ufd)
716{
717 return __bpf_prog_get(ufd, NULL);
718}
719
720struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
721{
722 return __bpf_prog_get(ufd, &type);
723}
724EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700725
726/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700727#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700728
729static int bpf_prog_load(union bpf_attr *attr)
730{
731 enum bpf_prog_type type = attr->prog_type;
732 struct bpf_prog *prog;
733 int err;
734 char license[128];
735 bool is_gpl;
736
737 if (CHECK_ATTR(BPF_PROG_LOAD))
738 return -EINVAL;
739
740 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100741 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700742 sizeof(license) - 1) < 0)
743 return -EFAULT;
744 license[sizeof(license) - 1] = 0;
745
746 /* eBPF programs must be GPL compatible to use GPL-ed functions */
747 is_gpl = license_is_gpl_compatible(license);
748
749 if (attr->insn_cnt >= BPF_MAXINSNS)
750 return -EINVAL;
751
Alexei Starovoitov25415172015-03-25 12:49:20 -0700752 if (type == BPF_PROG_TYPE_KPROBE &&
753 attr->kern_version != LINUX_VERSION_CODE)
754 return -EINVAL;
755
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700756 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
757 return -EPERM;
758
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700759 /* plain bpf_prog allocation */
760 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
761 if (!prog)
762 return -ENOMEM;
763
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700764 err = bpf_prog_charge_memlock(prog);
765 if (err)
766 goto free_prog_nouncharge;
767
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700768 prog->len = attr->insn_cnt;
769
770 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100771 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700772 prog->len * sizeof(struct bpf_insn)) != 0)
773 goto free_prog;
774
775 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200776 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700777
778 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200779 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700780
781 /* find program type: socket_filter vs tracing_filter */
782 err = find_prog_type(type, prog);
783 if (err < 0)
784 goto free_prog;
785
786 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700787 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700788 if (err < 0)
789 goto free_used_maps;
790
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700791 /* fixup BPF_CALL->imm field */
792 fixup_bpf_calls(prog);
793
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700794 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200795 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700796 if (err < 0)
797 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700798
Daniel Borkmannaa797812015-10-29 14:58:06 +0100799 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700800 if (err < 0)
801 /* failed to allocate fd */
802 goto free_used_maps;
803
804 return err;
805
806free_used_maps:
807 free_used_maps(prog->aux);
808free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700809 bpf_prog_uncharge_memlock(prog);
810free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700811 bpf_prog_free(prog);
812 return err;
813}
814
Daniel Borkmannb2197752015-10-29 14:58:09 +0100815#define BPF_OBJ_LAST_FIELD bpf_fd
816
817static int bpf_obj_pin(const union bpf_attr *attr)
818{
819 if (CHECK_ATTR(BPF_OBJ))
820 return -EINVAL;
821
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100822 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100823}
824
825static int bpf_obj_get(const union bpf_attr *attr)
826{
827 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
828 return -EINVAL;
829
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100830 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100831}
832
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700833SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
834{
835 union bpf_attr attr = {};
836 int err;
837
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700838 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700839 return -EPERM;
840
841 if (!access_ok(VERIFY_READ, uattr, 1))
842 return -EFAULT;
843
844 if (size > PAGE_SIZE) /* silly large */
845 return -E2BIG;
846
847 /* If we're handed a bigger struct than we know of,
848 * ensure all the unknown bits are 0 - i.e. new
849 * user-space does not rely on any kernel feature
850 * extensions we dont know about yet.
851 */
852 if (size > sizeof(attr)) {
853 unsigned char __user *addr;
854 unsigned char __user *end;
855 unsigned char val;
856
857 addr = (void __user *)uattr + sizeof(attr);
858 end = (void __user *)uattr + size;
859
860 for (; addr < end; addr++) {
861 err = get_user(val, addr);
862 if (err)
863 return err;
864 if (val)
865 return -E2BIG;
866 }
867 size = sizeof(attr);
868 }
869
870 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
871 if (copy_from_user(&attr, uattr, size) != 0)
872 return -EFAULT;
873
874 switch (cmd) {
875 case BPF_MAP_CREATE:
876 err = map_create(&attr);
877 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700878 case BPF_MAP_LOOKUP_ELEM:
879 err = map_lookup_elem(&attr);
880 break;
881 case BPF_MAP_UPDATE_ELEM:
882 err = map_update_elem(&attr);
883 break;
884 case BPF_MAP_DELETE_ELEM:
885 err = map_delete_elem(&attr);
886 break;
887 case BPF_MAP_GET_NEXT_KEY:
888 err = map_get_next_key(&attr);
889 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700890 case BPF_PROG_LOAD:
891 err = bpf_prog_load(&attr);
892 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100893 case BPF_OBJ_PIN:
894 err = bpf_obj_pin(&attr);
895 break;
896 case BPF_OBJ_GET:
897 err = bpf_obj_get(&attr);
898 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700899 default:
900 err = -EINVAL;
901 break;
902 }
903
904 return err;
905}