blob: ce1b7de7d72c6926ac5d4a50f812df764d25a07c [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 ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800295 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800296 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
297 value_size = round_up(map->value_size, 8) * num_possible_cpus();
298 else
299 value_size = map->value_size;
300
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800301 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800302 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700303 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800304 goto free_key;
305
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800306 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
307 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800308 err = bpf_percpu_hash_copy(map, key, value);
309 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
310 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800311 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
312 err = bpf_stackmap_copy(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800313 } else {
314 rcu_read_lock();
315 ptr = map->ops->map_lookup_elem(map, key);
316 if (ptr)
317 memcpy(value, ptr, value_size);
318 rcu_read_unlock();
319 err = ptr ? 0 : -ENOENT;
320 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800321
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800322 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800323 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700324
325 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800326 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800327 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700328
329 err = 0;
330
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800331free_value:
332 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700333free_key:
334 kfree(key);
335err_put:
336 fdput(f);
337 return err;
338}
339
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800340#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700341
342static int map_update_elem(union bpf_attr *attr)
343{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100344 void __user *ukey = u64_to_user_ptr(attr->key);
345 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700346 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700347 struct bpf_map *map;
348 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800349 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200350 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700351 int err;
352
353 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
354 return -EINVAL;
355
Daniel Borkmann592867b2015-09-08 18:00:09 +0200356 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100357 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700358 if (IS_ERR(map))
359 return PTR_ERR(map);
360
361 err = -ENOMEM;
362 key = kmalloc(map->key_size, GFP_USER);
363 if (!key)
364 goto err_put;
365
366 err = -EFAULT;
367 if (copy_from_user(key, ukey, map->key_size) != 0)
368 goto free_key;
369
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800370 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800371 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800372 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
373 value_size = round_up(map->value_size, 8) * num_possible_cpus();
374 else
375 value_size = map->value_size;
376
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700377 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800378 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700379 if (!value)
380 goto free_key;
381
382 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800383 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700384 goto free_value;
385
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800386 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
387 * inside bpf map update or delete otherwise deadlocks are possible
388 */
389 preempt_disable();
390 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800391 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
392 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800393 err = bpf_percpu_hash_update(map, key, value, attr->flags);
394 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
395 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200396 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700397 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
398 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200399 rcu_read_lock();
400 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
401 attr->flags);
402 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800403 } else {
404 rcu_read_lock();
405 err = map->ops->map_update_elem(map, key, value, attr->flags);
406 rcu_read_unlock();
407 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800408 __this_cpu_dec(bpf_prog_active);
409 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700410
411free_value:
412 kfree(value);
413free_key:
414 kfree(key);
415err_put:
416 fdput(f);
417 return err;
418}
419
420#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
421
422static int map_delete_elem(union bpf_attr *attr)
423{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100424 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700425 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700426 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200427 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700428 void *key;
429 int err;
430
431 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
432 return -EINVAL;
433
Daniel Borkmann592867b2015-09-08 18:00:09 +0200434 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100435 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700436 if (IS_ERR(map))
437 return PTR_ERR(map);
438
439 err = -ENOMEM;
440 key = kmalloc(map->key_size, GFP_USER);
441 if (!key)
442 goto err_put;
443
444 err = -EFAULT;
445 if (copy_from_user(key, ukey, map->key_size) != 0)
446 goto free_key;
447
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800448 preempt_disable();
449 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700450 rcu_read_lock();
451 err = map->ops->map_delete_elem(map, key);
452 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800453 __this_cpu_dec(bpf_prog_active);
454 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700455
456free_key:
457 kfree(key);
458err_put:
459 fdput(f);
460 return err;
461}
462
463/* last field in 'union bpf_attr' used by this command */
464#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
465
466static int map_get_next_key(union bpf_attr *attr)
467{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100468 void __user *ukey = u64_to_user_ptr(attr->key);
469 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700470 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700471 struct bpf_map *map;
472 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200473 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700474 int err;
475
476 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
477 return -EINVAL;
478
Daniel Borkmann592867b2015-09-08 18:00:09 +0200479 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100480 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700481 if (IS_ERR(map))
482 return PTR_ERR(map);
483
484 err = -ENOMEM;
485 key = kmalloc(map->key_size, GFP_USER);
486 if (!key)
487 goto err_put;
488
489 err = -EFAULT;
490 if (copy_from_user(key, ukey, map->key_size) != 0)
491 goto free_key;
492
493 err = -ENOMEM;
494 next_key = kmalloc(map->key_size, GFP_USER);
495 if (!next_key)
496 goto free_key;
497
498 rcu_read_lock();
499 err = map->ops->map_get_next_key(map, key, next_key);
500 rcu_read_unlock();
501 if (err)
502 goto free_next_key;
503
504 err = -EFAULT;
505 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
506 goto free_next_key;
507
508 err = 0;
509
510free_next_key:
511 kfree(next_key);
512free_key:
513 kfree(key);
514err_put:
515 fdput(f);
516 return err;
517}
518
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700519static LIST_HEAD(bpf_prog_types);
520
521static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
522{
523 struct bpf_prog_type_list *tl;
524
525 list_for_each_entry(tl, &bpf_prog_types, list_node) {
526 if (tl->type == type) {
527 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100528 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700529 return 0;
530 }
531 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100532
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700533 return -EINVAL;
534}
535
536void bpf_register_prog_type(struct bpf_prog_type_list *tl)
537{
538 list_add(&tl->list_node, &bpf_prog_types);
539}
540
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700541/* fixup insn->imm field of bpf_call instructions:
542 * if (insn->imm == BPF_FUNC_map_lookup_elem)
543 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
544 * else if (insn->imm == BPF_FUNC_map_update_elem)
545 * insn->imm = bpf_map_update_elem - __bpf_call_base;
546 * else ...
547 *
548 * this function is called after eBPF program passed verification
549 */
550static void fixup_bpf_calls(struct bpf_prog *prog)
551{
552 const struct bpf_func_proto *fn;
553 int i;
554
555 for (i = 0; i < prog->len; i++) {
556 struct bpf_insn *insn = &prog->insnsi[i];
557
558 if (insn->code == (BPF_JMP | BPF_CALL)) {
559 /* we reach here when program has bpf_call instructions
560 * and it passed bpf_check(), means that
561 * ops->get_func_proto must have been supplied, check it
562 */
563 BUG_ON(!prog->aux->ops->get_func_proto);
564
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200565 if (insn->imm == BPF_FUNC_get_route_realm)
566 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200567 if (insn->imm == BPF_FUNC_get_prandom_u32)
568 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700569 if (insn->imm == BPF_FUNC_tail_call) {
570 /* mark bpf_tail_call as different opcode
571 * to avoid conditional branch in
572 * interpeter for every normal call
573 * and to prevent accidental JITing by
574 * JIT compiler that doesn't support
575 * bpf_tail_call yet
576 */
577 insn->imm = 0;
578 insn->code |= BPF_X;
579 continue;
580 }
581
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700582 fn = prog->aux->ops->get_func_proto(insn->imm);
583 /* all functions that have prototype and verifier allowed
584 * programs to call them, must be real in-kernel functions
585 */
586 BUG_ON(!fn->func);
587 insn->imm = fn->func - __bpf_call_base;
588 }
589 }
590}
591
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700592/* drop refcnt on maps used by eBPF program and free auxilary data */
593static void free_used_maps(struct bpf_prog_aux *aux)
594{
595 int i;
596
597 for (i = 0; i < aux->used_map_cnt; i++)
598 bpf_map_put(aux->used_maps[i]);
599
600 kfree(aux->used_maps);
601}
602
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700603static int bpf_prog_charge_memlock(struct bpf_prog *prog)
604{
605 struct user_struct *user = get_current_user();
606 unsigned long memlock_limit;
607
608 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
609
610 atomic_long_add(prog->pages, &user->locked_vm);
611 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
612 atomic_long_sub(prog->pages, &user->locked_vm);
613 free_uid(user);
614 return -EPERM;
615 }
616 prog->aux->user = user;
617 return 0;
618}
619
620static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
621{
622 struct user_struct *user = prog->aux->user;
623
624 atomic_long_sub(prog->pages, &user->locked_vm);
625 free_uid(user);
626}
627
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200628static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700629{
630 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
631
632 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700633 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700634 bpf_prog_free(aux->prog);
635}
636
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700637void bpf_prog_put(struct bpf_prog *prog)
638{
Daniel Borkmanne9d8afa2015-10-29 14:58:08 +0100639 if (atomic_dec_and_test(&prog->aux->refcnt))
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200640 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700641}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100642EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700643
644static int bpf_prog_release(struct inode *inode, struct file *filp)
645{
646 struct bpf_prog *prog = filp->private_data;
647
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200648 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700649 return 0;
650}
651
652static const struct file_operations bpf_prog_fops = {
653 .release = bpf_prog_release,
654};
655
Daniel Borkmannb2197752015-10-29 14:58:09 +0100656int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100657{
658 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
659 O_RDWR | O_CLOEXEC);
660}
661
Daniel Borkmann113214b2016-06-30 17:24:44 +0200662static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700663{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700664 if (!f.file)
665 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700666 if (f.file->f_op != &bpf_prog_fops) {
667 fdput(f);
668 return ERR_PTR(-EINVAL);
669 }
670
Daniel Borkmannc2101292015-10-29 14:58:07 +0100671 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700672}
673
Brenden Blanco59d36562016-07-19 12:16:46 -0700674struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700675{
Brenden Blanco59d36562016-07-19 12:16:46 -0700676 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
677 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700678 return ERR_PTR(-EBUSY);
679 }
680 return prog;
681}
Brenden Blanco59d36562016-07-19 12:16:46 -0700682EXPORT_SYMBOL_GPL(bpf_prog_add);
683
Daniel Borkmannc5405942016-11-09 22:02:34 +0100684void bpf_prog_sub(struct bpf_prog *prog, int i)
685{
686 /* Only to be used for undoing previous bpf_prog_add() in some
687 * error path. We still know that another entity in our call
688 * path holds a reference to the program, thus atomic_sub() can
689 * be safely used in such cases!
690 */
691 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
692}
693EXPORT_SYMBOL_GPL(bpf_prog_sub);
694
Brenden Blanco59d36562016-07-19 12:16:46 -0700695struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
696{
697 return bpf_prog_add(prog, 1);
698}
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700699
Daniel Borkmann113214b2016-06-30 17:24:44 +0200700static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700701{
702 struct fd f = fdget(ufd);
703 struct bpf_prog *prog;
704
Daniel Borkmann113214b2016-06-30 17:24:44 +0200705 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700706 if (IS_ERR(prog))
707 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200708 if (type && prog->type != *type) {
709 prog = ERR_PTR(-EINVAL);
710 goto out;
711 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700712
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700713 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200714out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700715 fdput(f);
716 return prog;
717}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200718
719struct bpf_prog *bpf_prog_get(u32 ufd)
720{
721 return __bpf_prog_get(ufd, NULL);
722}
723
724struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
725{
726 return __bpf_prog_get(ufd, &type);
727}
728EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700729
730/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700731#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700732
733static int bpf_prog_load(union bpf_attr *attr)
734{
735 enum bpf_prog_type type = attr->prog_type;
736 struct bpf_prog *prog;
737 int err;
738 char license[128];
739 bool is_gpl;
740
741 if (CHECK_ATTR(BPF_PROG_LOAD))
742 return -EINVAL;
743
744 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100745 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700746 sizeof(license) - 1) < 0)
747 return -EFAULT;
748 license[sizeof(license) - 1] = 0;
749
750 /* eBPF programs must be GPL compatible to use GPL-ed functions */
751 is_gpl = license_is_gpl_compatible(license);
752
753 if (attr->insn_cnt >= BPF_MAXINSNS)
754 return -EINVAL;
755
Alexei Starovoitov25415172015-03-25 12:49:20 -0700756 if (type == BPF_PROG_TYPE_KPROBE &&
757 attr->kern_version != LINUX_VERSION_CODE)
758 return -EINVAL;
759
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700760 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
761 return -EPERM;
762
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700763 /* plain bpf_prog allocation */
764 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
765 if (!prog)
766 return -ENOMEM;
767
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700768 err = bpf_prog_charge_memlock(prog);
769 if (err)
770 goto free_prog_nouncharge;
771
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700772 prog->len = attr->insn_cnt;
773
774 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100775 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700776 prog->len * sizeof(struct bpf_insn)) != 0)
777 goto free_prog;
778
779 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200780 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700781
782 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200783 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700784
785 /* find program type: socket_filter vs tracing_filter */
786 err = find_prog_type(type, prog);
787 if (err < 0)
788 goto free_prog;
789
790 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700791 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700792 if (err < 0)
793 goto free_used_maps;
794
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700795 /* fixup BPF_CALL->imm field */
796 fixup_bpf_calls(prog);
797
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700798 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200799 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700800 if (err < 0)
801 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700802
Daniel Borkmannaa797812015-10-29 14:58:06 +0100803 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700804 if (err < 0)
805 /* failed to allocate fd */
806 goto free_used_maps;
807
808 return err;
809
810free_used_maps:
811 free_used_maps(prog->aux);
812free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700813 bpf_prog_uncharge_memlock(prog);
814free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700815 bpf_prog_free(prog);
816 return err;
817}
818
Daniel Borkmannb2197752015-10-29 14:58:09 +0100819#define BPF_OBJ_LAST_FIELD bpf_fd
820
821static int bpf_obj_pin(const union bpf_attr *attr)
822{
823 if (CHECK_ATTR(BPF_OBJ))
824 return -EINVAL;
825
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100826 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100827}
828
829static int bpf_obj_get(const union bpf_attr *attr)
830{
831 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
832 return -EINVAL;
833
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100834 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100835}
836
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700837SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
838{
839 union bpf_attr attr = {};
840 int err;
841
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700842 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700843 return -EPERM;
844
845 if (!access_ok(VERIFY_READ, uattr, 1))
846 return -EFAULT;
847
848 if (size > PAGE_SIZE) /* silly large */
849 return -E2BIG;
850
851 /* If we're handed a bigger struct than we know of,
852 * ensure all the unknown bits are 0 - i.e. new
853 * user-space does not rely on any kernel feature
854 * extensions we dont know about yet.
855 */
856 if (size > sizeof(attr)) {
857 unsigned char __user *addr;
858 unsigned char __user *end;
859 unsigned char val;
860
861 addr = (void __user *)uattr + sizeof(attr);
862 end = (void __user *)uattr + size;
863
864 for (; addr < end; addr++) {
865 err = get_user(val, addr);
866 if (err)
867 return err;
868 if (val)
869 return -E2BIG;
870 }
871 size = sizeof(attr);
872 }
873
874 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
875 if (copy_from_user(&attr, uattr, size) != 0)
876 return -EFAULT;
877
878 switch (cmd) {
879 case BPF_MAP_CREATE:
880 err = map_create(&attr);
881 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700882 case BPF_MAP_LOOKUP_ELEM:
883 err = map_lookup_elem(&attr);
884 break;
885 case BPF_MAP_UPDATE_ELEM:
886 err = map_update_elem(&attr);
887 break;
888 case BPF_MAP_DELETE_ELEM:
889 err = map_delete_elem(&attr);
890 break;
891 case BPF_MAP_GET_NEXT_KEY:
892 err = map_get_next_key(&attr);
893 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700894 case BPF_PROG_LOAD:
895 err = bpf_prog_load(&attr);
896 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100897 case BPF_OBJ_PIN:
898 err = bpf_obj_pin(&attr);
899 break;
900 case BPF_OBJ_GET:
901 err = bpf_obj_get(&attr);
902 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700903 default:
904 err = -EINVAL;
905 break;
906 }
907
908 return err;
909}