blob: cdc06546401bc0045453f9f7ea4980c8ba1c7219 [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)
198 goto free_map;
199
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:
208 map->ops->map_free(map);
209 return err;
210}
211
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700212/* if error is returned, fd is released.
213 * On success caller should complete fd access with matching fdput()
214 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100215struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700216{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700217 if (!f.file)
218 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700219 if (f.file->f_op != &bpf_map_fops) {
220 fdput(f);
221 return ERR_PTR(-EINVAL);
222 }
223
Daniel Borkmannc2101292015-10-29 14:58:07 +0100224 return f.file->private_data;
225}
226
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700227/* prog's and map's refcnt limit */
228#define BPF_MAX_REFCNT 32768
229
230struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100231{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700232 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
233 atomic_dec(&map->refcnt);
234 return ERR_PTR(-EBUSY);
235 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100236 if (uref)
237 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700238 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100239}
240
241struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100242{
243 struct fd f = fdget(ufd);
244 struct bpf_map *map;
245
246 map = __bpf_map_get(f);
247 if (IS_ERR(map))
248 return map;
249
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700250 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100251 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700252
253 return map;
254}
255
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800256int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
257{
258 return -ENOTSUPP;
259}
260
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700261/* last field in 'union bpf_attr' used by this command */
262#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
263
264static int map_lookup_elem(union bpf_attr *attr)
265{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100266 void __user *ukey = u64_to_user_ptr(attr->key);
267 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700268 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700269 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800270 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800271 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200272 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700273 int err;
274
275 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
276 return -EINVAL;
277
Daniel Borkmann592867b2015-09-08 18:00:09 +0200278 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100279 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700280 if (IS_ERR(map))
281 return PTR_ERR(map);
282
283 err = -ENOMEM;
284 key = kmalloc(map->key_size, GFP_USER);
285 if (!key)
286 goto err_put;
287
288 err = -EFAULT;
289 if (copy_from_user(key, ukey, map->key_size) != 0)
290 goto free_key;
291
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800292 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
293 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
294 value_size = round_up(map->value_size, 8) * num_possible_cpus();
295 else
296 value_size = map->value_size;
297
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800298 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800299 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700300 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800301 goto free_key;
302
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800303 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
304 err = bpf_percpu_hash_copy(map, key, value);
305 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
306 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800307 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
308 err = bpf_stackmap_copy(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800309 } else {
310 rcu_read_lock();
311 ptr = map->ops->map_lookup_elem(map, key);
312 if (ptr)
313 memcpy(value, ptr, value_size);
314 rcu_read_unlock();
315 err = ptr ? 0 : -ENOENT;
316 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800317
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800318 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800319 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700320
321 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800322 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800323 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700324
325 err = 0;
326
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800327free_value:
328 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700329free_key:
330 kfree(key);
331err_put:
332 fdput(f);
333 return err;
334}
335
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800336#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700337
338static int map_update_elem(union bpf_attr *attr)
339{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100340 void __user *ukey = u64_to_user_ptr(attr->key);
341 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700342 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700343 struct bpf_map *map;
344 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800345 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200346 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700347 int err;
348
349 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
350 return -EINVAL;
351
Daniel Borkmann592867b2015-09-08 18:00:09 +0200352 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100353 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700354 if (IS_ERR(map))
355 return PTR_ERR(map);
356
357 err = -ENOMEM;
358 key = kmalloc(map->key_size, GFP_USER);
359 if (!key)
360 goto err_put;
361
362 err = -EFAULT;
363 if (copy_from_user(key, ukey, map->key_size) != 0)
364 goto free_key;
365
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800366 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
367 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
368 value_size = round_up(map->value_size, 8) * num_possible_cpus();
369 else
370 value_size = map->value_size;
371
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700372 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800373 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700374 if (!value)
375 goto free_key;
376
377 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800378 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700379 goto free_value;
380
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800381 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
382 * inside bpf map update or delete otherwise deadlocks are possible
383 */
384 preempt_disable();
385 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800386 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
387 err = bpf_percpu_hash_update(map, key, value, attr->flags);
388 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
389 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200390 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700391 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
392 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200393 rcu_read_lock();
394 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
395 attr->flags);
396 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800397 } else {
398 rcu_read_lock();
399 err = map->ops->map_update_elem(map, key, value, attr->flags);
400 rcu_read_unlock();
401 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800402 __this_cpu_dec(bpf_prog_active);
403 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700404
405free_value:
406 kfree(value);
407free_key:
408 kfree(key);
409err_put:
410 fdput(f);
411 return err;
412}
413
414#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
415
416static int map_delete_elem(union bpf_attr *attr)
417{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100418 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700419 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700420 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200421 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700422 void *key;
423 int err;
424
425 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
426 return -EINVAL;
427
Daniel Borkmann592867b2015-09-08 18:00:09 +0200428 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100429 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700430 if (IS_ERR(map))
431 return PTR_ERR(map);
432
433 err = -ENOMEM;
434 key = kmalloc(map->key_size, GFP_USER);
435 if (!key)
436 goto err_put;
437
438 err = -EFAULT;
439 if (copy_from_user(key, ukey, map->key_size) != 0)
440 goto free_key;
441
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800442 preempt_disable();
443 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700444 rcu_read_lock();
445 err = map->ops->map_delete_elem(map, key);
446 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800447 __this_cpu_dec(bpf_prog_active);
448 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700449
450free_key:
451 kfree(key);
452err_put:
453 fdput(f);
454 return err;
455}
456
457/* last field in 'union bpf_attr' used by this command */
458#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
459
460static int map_get_next_key(union bpf_attr *attr)
461{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100462 void __user *ukey = u64_to_user_ptr(attr->key);
463 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700464 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700465 struct bpf_map *map;
466 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200467 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700468 int err;
469
470 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
471 return -EINVAL;
472
Daniel Borkmann592867b2015-09-08 18:00:09 +0200473 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100474 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700475 if (IS_ERR(map))
476 return PTR_ERR(map);
477
478 err = -ENOMEM;
479 key = kmalloc(map->key_size, GFP_USER);
480 if (!key)
481 goto err_put;
482
483 err = -EFAULT;
484 if (copy_from_user(key, ukey, map->key_size) != 0)
485 goto free_key;
486
487 err = -ENOMEM;
488 next_key = kmalloc(map->key_size, GFP_USER);
489 if (!next_key)
490 goto free_key;
491
492 rcu_read_lock();
493 err = map->ops->map_get_next_key(map, key, next_key);
494 rcu_read_unlock();
495 if (err)
496 goto free_next_key;
497
498 err = -EFAULT;
499 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
500 goto free_next_key;
501
502 err = 0;
503
504free_next_key:
505 kfree(next_key);
506free_key:
507 kfree(key);
508err_put:
509 fdput(f);
510 return err;
511}
512
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700513static LIST_HEAD(bpf_prog_types);
514
515static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
516{
517 struct bpf_prog_type_list *tl;
518
519 list_for_each_entry(tl, &bpf_prog_types, list_node) {
520 if (tl->type == type) {
521 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100522 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700523 return 0;
524 }
525 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100526
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700527 return -EINVAL;
528}
529
530void bpf_register_prog_type(struct bpf_prog_type_list *tl)
531{
532 list_add(&tl->list_node, &bpf_prog_types);
533}
534
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700535/* fixup insn->imm field of bpf_call instructions:
536 * if (insn->imm == BPF_FUNC_map_lookup_elem)
537 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
538 * else if (insn->imm == BPF_FUNC_map_update_elem)
539 * insn->imm = bpf_map_update_elem - __bpf_call_base;
540 * else ...
541 *
542 * this function is called after eBPF program passed verification
543 */
544static void fixup_bpf_calls(struct bpf_prog *prog)
545{
546 const struct bpf_func_proto *fn;
547 int i;
548
549 for (i = 0; i < prog->len; i++) {
550 struct bpf_insn *insn = &prog->insnsi[i];
551
552 if (insn->code == (BPF_JMP | BPF_CALL)) {
553 /* we reach here when program has bpf_call instructions
554 * and it passed bpf_check(), means that
555 * ops->get_func_proto must have been supplied, check it
556 */
557 BUG_ON(!prog->aux->ops->get_func_proto);
558
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200559 if (insn->imm == BPF_FUNC_get_route_realm)
560 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200561 if (insn->imm == BPF_FUNC_get_prandom_u32)
562 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700563 if (insn->imm == BPF_FUNC_tail_call) {
564 /* mark bpf_tail_call as different opcode
565 * to avoid conditional branch in
566 * interpeter for every normal call
567 * and to prevent accidental JITing by
568 * JIT compiler that doesn't support
569 * bpf_tail_call yet
570 */
571 insn->imm = 0;
572 insn->code |= BPF_X;
573 continue;
574 }
575
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700576 fn = prog->aux->ops->get_func_proto(insn->imm);
577 /* all functions that have prototype and verifier allowed
578 * programs to call them, must be real in-kernel functions
579 */
580 BUG_ON(!fn->func);
581 insn->imm = fn->func - __bpf_call_base;
582 }
583 }
584}
585
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700586/* drop refcnt on maps used by eBPF program and free auxilary data */
587static void free_used_maps(struct bpf_prog_aux *aux)
588{
589 int i;
590
591 for (i = 0; i < aux->used_map_cnt; i++)
592 bpf_map_put(aux->used_maps[i]);
593
594 kfree(aux->used_maps);
595}
596
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700597static int bpf_prog_charge_memlock(struct bpf_prog *prog)
598{
599 struct user_struct *user = get_current_user();
600 unsigned long memlock_limit;
601
602 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
603
604 atomic_long_add(prog->pages, &user->locked_vm);
605 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
606 atomic_long_sub(prog->pages, &user->locked_vm);
607 free_uid(user);
608 return -EPERM;
609 }
610 prog->aux->user = user;
611 return 0;
612}
613
614static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
615{
616 struct user_struct *user = prog->aux->user;
617
618 atomic_long_sub(prog->pages, &user->locked_vm);
619 free_uid(user);
620}
621
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200622static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700623{
624 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
625
626 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700627 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700628 bpf_prog_free(aux->prog);
629}
630
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700631void bpf_prog_put(struct bpf_prog *prog)
632{
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100633 if (atomic_dec_and_test(&prog->aux->refcnt))
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200634 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700635}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100636EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700637
638static int bpf_prog_release(struct inode *inode, struct file *filp)
639{
640 struct bpf_prog *prog = filp->private_data;
641
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200642 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700643 return 0;
644}
645
646static const struct file_operations bpf_prog_fops = {
647 .release = bpf_prog_release,
648};
649
Daniel Borkmannb2197752015-10-29 14:58:09 +0100650int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100651{
652 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
653 O_RDWR | O_CLOEXEC);
654}
655
Daniel Borkmann113214b2016-06-30 17:24:44 +0200656static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700657{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700658 if (!f.file)
659 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700660 if (f.file->f_op != &bpf_prog_fops) {
661 fdput(f);
662 return ERR_PTR(-EINVAL);
663 }
664
Daniel Borkmannc2101292015-10-29 14:58:07 +0100665 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700666}
667
Brenden Blanco59d36562016-07-19 12:16:46 -0700668struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700669{
Brenden Blanco59d36562016-07-19 12:16:46 -0700670 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
671 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700672 return ERR_PTR(-EBUSY);
673 }
674 return prog;
675}
Brenden Blanco59d36562016-07-19 12:16:46 -0700676EXPORT_SYMBOL_GPL(bpf_prog_add);
677
Daniel Borkmannc5405942016-11-09 22:02:34 +0100678void bpf_prog_sub(struct bpf_prog *prog, int i)
679{
680 /* Only to be used for undoing previous bpf_prog_add() in some
681 * error path. We still know that another entity in our call
682 * path holds a reference to the program, thus atomic_sub() can
683 * be safely used in such cases!
684 */
685 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
686}
687EXPORT_SYMBOL_GPL(bpf_prog_sub);
688
Brenden Blanco59d36562016-07-19 12:16:46 -0700689struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
690{
691 return bpf_prog_add(prog, 1);
692}
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700693
Daniel Borkmann113214b2016-06-30 17:24:44 +0200694static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700695{
696 struct fd f = fdget(ufd);
697 struct bpf_prog *prog;
698
Daniel Borkmann113214b2016-06-30 17:24:44 +0200699 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700700 if (IS_ERR(prog))
701 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200702 if (type && prog->type != *type) {
703 prog = ERR_PTR(-EINVAL);
704 goto out;
705 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700706
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700707 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200708out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700709 fdput(f);
710 return prog;
711}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200712
713struct bpf_prog *bpf_prog_get(u32 ufd)
714{
715 return __bpf_prog_get(ufd, NULL);
716}
717
718struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
719{
720 return __bpf_prog_get(ufd, &type);
721}
722EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700723
724/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700725#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700726
727static int bpf_prog_load(union bpf_attr *attr)
728{
729 enum bpf_prog_type type = attr->prog_type;
730 struct bpf_prog *prog;
731 int err;
732 char license[128];
733 bool is_gpl;
734
735 if (CHECK_ATTR(BPF_PROG_LOAD))
736 return -EINVAL;
737
738 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100739 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700740 sizeof(license) - 1) < 0)
741 return -EFAULT;
742 license[sizeof(license) - 1] = 0;
743
744 /* eBPF programs must be GPL compatible to use GPL-ed functions */
745 is_gpl = license_is_gpl_compatible(license);
746
747 if (attr->insn_cnt >= BPF_MAXINSNS)
748 return -EINVAL;
749
Alexei Starovoitov25415172015-03-25 12:49:20 -0700750 if (type == BPF_PROG_TYPE_KPROBE &&
751 attr->kern_version != LINUX_VERSION_CODE)
752 return -EINVAL;
753
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700754 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
755 return -EPERM;
756
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700757 /* plain bpf_prog allocation */
758 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
759 if (!prog)
760 return -ENOMEM;
761
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700762 err = bpf_prog_charge_memlock(prog);
763 if (err)
764 goto free_prog_nouncharge;
765
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700766 prog->len = attr->insn_cnt;
767
768 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100769 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700770 prog->len * sizeof(struct bpf_insn)) != 0)
771 goto free_prog;
772
773 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200774 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700775
776 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200777 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700778
779 /* find program type: socket_filter vs tracing_filter */
780 err = find_prog_type(type, prog);
781 if (err < 0)
782 goto free_prog;
783
784 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700785 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700786 if (err < 0)
787 goto free_used_maps;
788
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700789 /* fixup BPF_CALL->imm field */
790 fixup_bpf_calls(prog);
791
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700792 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200793 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700794 if (err < 0)
795 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700796
Daniel Borkmannaa797812015-10-29 14:58:06 +0100797 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700798 if (err < 0)
799 /* failed to allocate fd */
800 goto free_used_maps;
801
802 return err;
803
804free_used_maps:
805 free_used_maps(prog->aux);
806free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700807 bpf_prog_uncharge_memlock(prog);
808free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700809 bpf_prog_free(prog);
810 return err;
811}
812
Daniel Borkmannb2197752015-10-29 14:58:09 +0100813#define BPF_OBJ_LAST_FIELD bpf_fd
814
815static int bpf_obj_pin(const union bpf_attr *attr)
816{
817 if (CHECK_ATTR(BPF_OBJ))
818 return -EINVAL;
819
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100820 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100821}
822
823static int bpf_obj_get(const union bpf_attr *attr)
824{
825 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
826 return -EINVAL;
827
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100828 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100829}
830
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700831SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
832{
833 union bpf_attr attr = {};
834 int err;
835
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700836 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700837 return -EPERM;
838
839 if (!access_ok(VERIFY_READ, uattr, 1))
840 return -EFAULT;
841
842 if (size > PAGE_SIZE) /* silly large */
843 return -E2BIG;
844
845 /* If we're handed a bigger struct than we know of,
846 * ensure all the unknown bits are 0 - i.e. new
847 * user-space does not rely on any kernel feature
848 * extensions we dont know about yet.
849 */
850 if (size > sizeof(attr)) {
851 unsigned char __user *addr;
852 unsigned char __user *end;
853 unsigned char val;
854
855 addr = (void __user *)uattr + sizeof(attr);
856 end = (void __user *)uattr + size;
857
858 for (; addr < end; addr++) {
859 err = get_user(val, addr);
860 if (err)
861 return err;
862 if (val)
863 return -E2BIG;
864 }
865 size = sizeof(attr);
866 }
867
868 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
869 if (copy_from_user(&attr, uattr, size) != 0)
870 return -EFAULT;
871
872 switch (cmd) {
873 case BPF_MAP_CREATE:
874 err = map_create(&attr);
875 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700876 case BPF_MAP_LOOKUP_ELEM:
877 err = map_lookup_elem(&attr);
878 break;
879 case BPF_MAP_UPDATE_ELEM:
880 err = map_update_elem(&attr);
881 break;
882 case BPF_MAP_DELETE_ELEM:
883 err = map_delete_elem(&attr);
884 break;
885 case BPF_MAP_GET_NEXT_KEY:
886 err = map_get_next_key(&attr);
887 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700888 case BPF_PROG_LOAD:
889 err = bpf_prog_load(&attr);
890 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100891 case BPF_OBJ_PIN:
892 err = bpf_obj_pin(&attr);
893 break;
894 case BPF_OBJ_GET:
895 err = bpf_obj_get(&attr);
896 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700897 default:
898 err = -EINVAL;
899 break;
900 }
901
902 return err;
903}