blob: 461eb1e66a0fdf498557c2ff5a85354f730acc3e [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>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010013#include <linux/bpf_trace.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070014#include <linux/syscalls.h>
15#include <linux/slab.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010016#include <linux/vmalloc.h>
17#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070018#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070019#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070020#include <linux/license.h>
21#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070022#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010023#include <linux/kernel.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070024
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080025DEFINE_PER_CPU(int, bpf_prog_active);
26
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070027int sysctl_unprivileged_bpf_disabled __read_mostly;
28
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070029static LIST_HEAD(bpf_map_types);
30
31static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
32{
33 struct bpf_map_type_list *tl;
34 struct bpf_map *map;
35
36 list_for_each_entry(tl, &bpf_map_types, list_node) {
37 if (tl->type == attr->map_type) {
38 map = tl->ops->map_alloc(attr);
39 if (IS_ERR(map))
40 return map;
41 map->ops = tl->ops;
42 map->map_type = attr->map_type;
43 return map;
44 }
45 }
46 return ERR_PTR(-EINVAL);
47}
48
49/* boot time registration of different map implementations */
50void bpf_register_map_type(struct bpf_map_type_list *tl)
51{
52 list_add(&tl->list_node, &bpf_map_types);
53}
54
Daniel Borkmannd407bd22017-01-18 15:14:17 +010055void *bpf_map_area_alloc(size_t size)
56{
57 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
58 * trigger under memory pressure as we really just want to
59 * fail instead.
60 */
61 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
62 void *area;
63
64 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
65 area = kmalloc(size, GFP_USER | flags);
66 if (area != NULL)
67 return area;
68 }
69
70 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | flags,
71 PAGE_KERNEL);
72}
73
74void bpf_map_area_free(void *area)
75{
76 kvfree(area);
77}
78
Alexei Starovoitov6c905982016-03-07 21:57:15 -080079int bpf_map_precharge_memlock(u32 pages)
80{
81 struct user_struct *user = get_current_user();
82 unsigned long memlock_limit, cur;
83
84 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
85 cur = atomic_long_read(&user->locked_vm);
86 free_uid(user);
87 if (cur + pages > memlock_limit)
88 return -EPERM;
89 return 0;
90}
91
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070092static int bpf_map_charge_memlock(struct bpf_map *map)
93{
94 struct user_struct *user = get_current_user();
95 unsigned long memlock_limit;
96
97 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
98
99 atomic_long_add(map->pages, &user->locked_vm);
100
101 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
102 atomic_long_sub(map->pages, &user->locked_vm);
103 free_uid(user);
104 return -EPERM;
105 }
106 map->user = user;
107 return 0;
108}
109
110static void bpf_map_uncharge_memlock(struct bpf_map *map)
111{
112 struct user_struct *user = map->user;
113
114 atomic_long_sub(map->pages, &user->locked_vm);
115 free_uid(user);
116}
117
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700118/* called from workqueue */
119static void bpf_map_free_deferred(struct work_struct *work)
120{
121 struct bpf_map *map = container_of(work, struct bpf_map, work);
122
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700123 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700124 /* implementation dependent freeing */
125 map->ops->map_free(map);
126}
127
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100128static void bpf_map_put_uref(struct bpf_map *map)
129{
130 if (atomic_dec_and_test(&map->usercnt)) {
131 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
132 bpf_fd_array_map_clear(map);
133 }
134}
135
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700136/* decrement map refcnt and schedule it for freeing via workqueue
137 * (unrelying map implementation ops->map_free() might sleep)
138 */
139void bpf_map_put(struct bpf_map *map)
140{
141 if (atomic_dec_and_test(&map->refcnt)) {
142 INIT_WORK(&map->work, bpf_map_free_deferred);
143 schedule_work(&map->work);
144 }
145}
146
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100147void bpf_map_put_with_uref(struct bpf_map *map)
148{
149 bpf_map_put_uref(map);
150 bpf_map_put(map);
151}
152
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700153static int bpf_map_release(struct inode *inode, struct file *filp)
154{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200155 struct bpf_map *map = filp->private_data;
156
157 if (map->ops->map_release)
158 map->ops->map_release(map, filp);
159
160 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700161 return 0;
162}
163
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100164#ifdef CONFIG_PROC_FS
165static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
166{
167 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100168 const struct bpf_array *array;
169 u32 owner_prog_type = 0;
170
171 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
172 array = container_of(map, struct bpf_array, map);
173 owner_prog_type = array->owner_prog_type;
174 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100175
176 seq_printf(m,
177 "map_type:\t%u\n"
178 "key_size:\t%u\n"
179 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100180 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100181 "map_flags:\t%#x\n"
182 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100183 map->map_type,
184 map->key_size,
185 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100186 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100187 map->map_flags,
188 map->pages * 1ULL << PAGE_SHIFT);
189
190 if (owner_prog_type)
191 seq_printf(m, "owner_prog_type:\t%u\n",
192 owner_prog_type);
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100193}
194#endif
195
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700196static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100197#ifdef CONFIG_PROC_FS
198 .show_fdinfo = bpf_map_show_fdinfo,
199#endif
200 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700201};
202
Daniel Borkmannb2197752015-10-29 14:58:09 +0100203int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100204{
205 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
206 O_RDWR | O_CLOEXEC);
207}
208
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700209/* helper macro to check that unused fields 'union bpf_attr' are zero */
210#define CHECK_ATTR(CMD) \
211 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
212 sizeof(attr->CMD##_LAST_FIELD), 0, \
213 sizeof(*attr) - \
214 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
215 sizeof(attr->CMD##_LAST_FIELD)) != NULL
216
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800217#define BPF_MAP_CREATE_LAST_FIELD map_flags
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700218/* called via syscall */
219static int map_create(union bpf_attr *attr)
220{
221 struct bpf_map *map;
222 int err;
223
224 err = CHECK_ATTR(BPF_MAP_CREATE);
225 if (err)
226 return -EINVAL;
227
228 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
229 map = find_and_alloc_map(attr);
230 if (IS_ERR(map))
231 return PTR_ERR(map);
232
233 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100234 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700235
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700236 err = bpf_map_charge_memlock(map);
237 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100238 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700239
Daniel Borkmannaa797812015-10-29 14:58:06 +0100240 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700241 if (err < 0)
242 /* failed to allocate fd */
243 goto free_map;
244
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100245 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700246 return err;
247
248free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100249 bpf_map_uncharge_memlock(map);
250free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700251 map->ops->map_free(map);
252 return err;
253}
254
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700255/* if error is returned, fd is released.
256 * On success caller should complete fd access with matching fdput()
257 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100258struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700259{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700260 if (!f.file)
261 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700262 if (f.file->f_op != &bpf_map_fops) {
263 fdput(f);
264 return ERR_PTR(-EINVAL);
265 }
266
Daniel Borkmannc2101292015-10-29 14:58:07 +0100267 return f.file->private_data;
268}
269
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700270/* prog's and map's refcnt limit */
271#define BPF_MAX_REFCNT 32768
272
273struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100274{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700275 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
276 atomic_dec(&map->refcnt);
277 return ERR_PTR(-EBUSY);
278 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100279 if (uref)
280 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700281 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100282}
283
284struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100285{
286 struct fd f = fdget(ufd);
287 struct bpf_map *map;
288
289 map = __bpf_map_get(f);
290 if (IS_ERR(map))
291 return map;
292
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700293 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100294 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700295
296 return map;
297}
298
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800299int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
300{
301 return -ENOTSUPP;
302}
303
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700304/* last field in 'union bpf_attr' used by this command */
305#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
306
307static int map_lookup_elem(union bpf_attr *attr)
308{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100309 void __user *ukey = u64_to_user_ptr(attr->key);
310 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700311 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700312 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800313 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800314 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200315 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700316 int err;
317
318 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
319 return -EINVAL;
320
Daniel Borkmann592867b2015-09-08 18:00:09 +0200321 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100322 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700323 if (IS_ERR(map))
324 return PTR_ERR(map);
325
326 err = -ENOMEM;
327 key = kmalloc(map->key_size, GFP_USER);
328 if (!key)
329 goto err_put;
330
331 err = -EFAULT;
332 if (copy_from_user(key, ukey, map->key_size) != 0)
333 goto free_key;
334
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800335 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800336 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800337 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
338 value_size = round_up(map->value_size, 8) * num_possible_cpus();
339 else
340 value_size = map->value_size;
341
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800342 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800343 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700344 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800345 goto free_key;
346
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800347 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
348 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800349 err = bpf_percpu_hash_copy(map, key, value);
350 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
351 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800352 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
353 err = bpf_stackmap_copy(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800354 } else {
355 rcu_read_lock();
356 ptr = map->ops->map_lookup_elem(map, key);
357 if (ptr)
358 memcpy(value, ptr, value_size);
359 rcu_read_unlock();
360 err = ptr ? 0 : -ENOENT;
361 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800362
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800363 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800364 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700365
366 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800367 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800368 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700369
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100370 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700371 err = 0;
372
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800373free_value:
374 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700375free_key:
376 kfree(key);
377err_put:
378 fdput(f);
379 return err;
380}
381
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800382#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700383
384static int map_update_elem(union bpf_attr *attr)
385{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100386 void __user *ukey = u64_to_user_ptr(attr->key);
387 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700388 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700389 struct bpf_map *map;
390 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800391 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200392 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700393 int err;
394
395 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
396 return -EINVAL;
397
Daniel Borkmann592867b2015-09-08 18:00:09 +0200398 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100399 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700400 if (IS_ERR(map))
401 return PTR_ERR(map);
402
403 err = -ENOMEM;
404 key = kmalloc(map->key_size, GFP_USER);
405 if (!key)
406 goto err_put;
407
408 err = -EFAULT;
409 if (copy_from_user(key, ukey, map->key_size) != 0)
410 goto free_key;
411
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800412 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800413 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800414 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
415 value_size = round_up(map->value_size, 8) * num_possible_cpus();
416 else
417 value_size = map->value_size;
418
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700419 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800420 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700421 if (!value)
422 goto free_key;
423
424 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800425 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700426 goto free_value;
427
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800428 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
429 * inside bpf map update or delete otherwise deadlocks are possible
430 */
431 preempt_disable();
432 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800433 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
434 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800435 err = bpf_percpu_hash_update(map, key, value, attr->flags);
436 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
437 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200438 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700439 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
440 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200441 rcu_read_lock();
442 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
443 attr->flags);
444 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800445 } else {
446 rcu_read_lock();
447 err = map->ops->map_update_elem(map, key, value, attr->flags);
448 rcu_read_unlock();
449 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800450 __this_cpu_dec(bpf_prog_active);
451 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700452
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100453 if (!err)
454 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700455free_value:
456 kfree(value);
457free_key:
458 kfree(key);
459err_put:
460 fdput(f);
461 return err;
462}
463
464#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
465
466static int map_delete_elem(union bpf_attr *attr)
467{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100468 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700469 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700470 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200471 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700472 void *key;
473 int err;
474
475 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
476 return -EINVAL;
477
Daniel Borkmann592867b2015-09-08 18:00:09 +0200478 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100479 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700480 if (IS_ERR(map))
481 return PTR_ERR(map);
482
483 err = -ENOMEM;
484 key = kmalloc(map->key_size, GFP_USER);
485 if (!key)
486 goto err_put;
487
488 err = -EFAULT;
489 if (copy_from_user(key, ukey, map->key_size) != 0)
490 goto free_key;
491
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800492 preempt_disable();
493 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700494 rcu_read_lock();
495 err = map->ops->map_delete_elem(map, key);
496 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800497 __this_cpu_dec(bpf_prog_active);
498 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700499
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100500 if (!err)
501 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700502free_key:
503 kfree(key);
504err_put:
505 fdput(f);
506 return err;
507}
508
509/* last field in 'union bpf_attr' used by this command */
510#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
511
512static int map_get_next_key(union bpf_attr *attr)
513{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100514 void __user *ukey = u64_to_user_ptr(attr->key);
515 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700516 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700517 struct bpf_map *map;
518 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200519 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700520 int err;
521
522 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
523 return -EINVAL;
524
Daniel Borkmann592867b2015-09-08 18:00:09 +0200525 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100526 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700527 if (IS_ERR(map))
528 return PTR_ERR(map);
529
530 err = -ENOMEM;
531 key = kmalloc(map->key_size, GFP_USER);
532 if (!key)
533 goto err_put;
534
535 err = -EFAULT;
536 if (copy_from_user(key, ukey, map->key_size) != 0)
537 goto free_key;
538
539 err = -ENOMEM;
540 next_key = kmalloc(map->key_size, GFP_USER);
541 if (!next_key)
542 goto free_key;
543
544 rcu_read_lock();
545 err = map->ops->map_get_next_key(map, key, next_key);
546 rcu_read_unlock();
547 if (err)
548 goto free_next_key;
549
550 err = -EFAULT;
551 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
552 goto free_next_key;
553
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100554 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700555 err = 0;
556
557free_next_key:
558 kfree(next_key);
559free_key:
560 kfree(key);
561err_put:
562 fdput(f);
563 return err;
564}
565
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700566static LIST_HEAD(bpf_prog_types);
567
568static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
569{
570 struct bpf_prog_type_list *tl;
571
572 list_for_each_entry(tl, &bpf_prog_types, list_node) {
573 if (tl->type == type) {
574 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100575 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700576 return 0;
577 }
578 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100579
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700580 return -EINVAL;
581}
582
583void bpf_register_prog_type(struct bpf_prog_type_list *tl)
584{
585 list_add(&tl->list_node, &bpf_prog_types);
586}
587
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700588/* fixup insn->imm field of bpf_call instructions:
589 * if (insn->imm == BPF_FUNC_map_lookup_elem)
590 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
591 * else if (insn->imm == BPF_FUNC_map_update_elem)
592 * insn->imm = bpf_map_update_elem - __bpf_call_base;
593 * else ...
594 *
595 * this function is called after eBPF program passed verification
596 */
597static void fixup_bpf_calls(struct bpf_prog *prog)
598{
599 const struct bpf_func_proto *fn;
600 int i;
601
602 for (i = 0; i < prog->len; i++) {
603 struct bpf_insn *insn = &prog->insnsi[i];
604
605 if (insn->code == (BPF_JMP | BPF_CALL)) {
606 /* we reach here when program has bpf_call instructions
607 * and it passed bpf_check(), means that
608 * ops->get_func_proto must have been supplied, check it
609 */
610 BUG_ON(!prog->aux->ops->get_func_proto);
611
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200612 if (insn->imm == BPF_FUNC_get_route_realm)
613 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200614 if (insn->imm == BPF_FUNC_get_prandom_u32)
615 bpf_user_rnd_init_once();
Martin KaFai Lau17bedab2016-12-07 15:53:11 -0800616 if (insn->imm == BPF_FUNC_xdp_adjust_head)
617 prog->xdp_adjust_head = 1;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700618 if (insn->imm == BPF_FUNC_tail_call) {
619 /* mark bpf_tail_call as different opcode
620 * to avoid conditional branch in
621 * interpeter for every normal call
622 * and to prevent accidental JITing by
623 * JIT compiler that doesn't support
624 * bpf_tail_call yet
625 */
626 insn->imm = 0;
627 insn->code |= BPF_X;
628 continue;
629 }
630
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700631 fn = prog->aux->ops->get_func_proto(insn->imm);
632 /* all functions that have prototype and verifier allowed
633 * programs to call them, must be real in-kernel functions
634 */
635 BUG_ON(!fn->func);
636 insn->imm = fn->func - __bpf_call_base;
637 }
638 }
639}
640
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700641/* drop refcnt on maps used by eBPF program and free auxilary data */
642static void free_used_maps(struct bpf_prog_aux *aux)
643{
644 int i;
645
646 for (i = 0; i < aux->used_map_cnt; i++)
647 bpf_map_put(aux->used_maps[i]);
648
649 kfree(aux->used_maps);
650}
651
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100652int __bpf_prog_charge(struct user_struct *user, u32 pages)
653{
654 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
655 unsigned long user_bufs;
656
657 if (user) {
658 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
659 if (user_bufs > memlock_limit) {
660 atomic_long_sub(pages, &user->locked_vm);
661 return -EPERM;
662 }
663 }
664
665 return 0;
666}
667
668void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
669{
670 if (user)
671 atomic_long_sub(pages, &user->locked_vm);
672}
673
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700674static int bpf_prog_charge_memlock(struct bpf_prog *prog)
675{
676 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100677 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700678
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100679 ret = __bpf_prog_charge(user, prog->pages);
680 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700681 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100682 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700683 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100684
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700685 prog->aux->user = user;
686 return 0;
687}
688
689static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
690{
691 struct user_struct *user = prog->aux->user;
692
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100693 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700694 free_uid(user);
695}
696
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200697static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700698{
699 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
700
701 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700702 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700703 bpf_prog_free(aux->prog);
704}
705
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700706void bpf_prog_put(struct bpf_prog *prog)
707{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100708 if (atomic_dec_and_test(&prog->aux->refcnt)) {
709 trace_bpf_prog_put_rcu(prog);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100710 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200711 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100712 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700713}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100714EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700715
716static int bpf_prog_release(struct inode *inode, struct file *filp)
717{
718 struct bpf_prog *prog = filp->private_data;
719
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200720 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700721 return 0;
722}
723
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100724#ifdef CONFIG_PROC_FS
725static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
726{
727 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100728 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100729
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100730 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100731 seq_printf(m,
732 "prog_type:\t%u\n"
733 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100734 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100735 "memlock:\t%llu\n",
736 prog->type,
737 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100738 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100739 prog->pages * 1ULL << PAGE_SHIFT);
740}
741#endif
742
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700743static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100744#ifdef CONFIG_PROC_FS
745 .show_fdinfo = bpf_prog_show_fdinfo,
746#endif
747 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700748};
749
Daniel Borkmannb2197752015-10-29 14:58:09 +0100750int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100751{
752 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
753 O_RDWR | O_CLOEXEC);
754}
755
Daniel Borkmann113214b2016-06-30 17:24:44 +0200756static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700757{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700758 if (!f.file)
759 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700760 if (f.file->f_op != &bpf_prog_fops) {
761 fdput(f);
762 return ERR_PTR(-EINVAL);
763 }
764
Daniel Borkmannc2101292015-10-29 14:58:07 +0100765 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700766}
767
Brenden Blanco59d36562016-07-19 12:16:46 -0700768struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700769{
Brenden Blanco59d36562016-07-19 12:16:46 -0700770 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
771 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700772 return ERR_PTR(-EBUSY);
773 }
774 return prog;
775}
Brenden Blanco59d36562016-07-19 12:16:46 -0700776EXPORT_SYMBOL_GPL(bpf_prog_add);
777
Daniel Borkmannc5405942016-11-09 22:02:34 +0100778void bpf_prog_sub(struct bpf_prog *prog, int i)
779{
780 /* Only to be used for undoing previous bpf_prog_add() in some
781 * error path. We still know that another entity in our call
782 * path holds a reference to the program, thus atomic_sub() can
783 * be safely used in such cases!
784 */
785 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
786}
787EXPORT_SYMBOL_GPL(bpf_prog_sub);
788
Brenden Blanco59d36562016-07-19 12:16:46 -0700789struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
790{
791 return bpf_prog_add(prog, 1);
792}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100793EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700794
Daniel Borkmann113214b2016-06-30 17:24:44 +0200795static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700796{
797 struct fd f = fdget(ufd);
798 struct bpf_prog *prog;
799
Daniel Borkmann113214b2016-06-30 17:24:44 +0200800 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700801 if (IS_ERR(prog))
802 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200803 if (type && prog->type != *type) {
804 prog = ERR_PTR(-EINVAL);
805 goto out;
806 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700807
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700808 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200809out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700810 fdput(f);
811 return prog;
812}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200813
814struct bpf_prog *bpf_prog_get(u32 ufd)
815{
816 return __bpf_prog_get(ufd, NULL);
817}
818
819struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
820{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100821 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
822
823 if (!IS_ERR(prog))
824 trace_bpf_prog_get_type(prog);
825 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200826}
827EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700828
829/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700830#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700831
832static int bpf_prog_load(union bpf_attr *attr)
833{
834 enum bpf_prog_type type = attr->prog_type;
835 struct bpf_prog *prog;
836 int err;
837 char license[128];
838 bool is_gpl;
839
840 if (CHECK_ATTR(BPF_PROG_LOAD))
841 return -EINVAL;
842
843 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100844 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700845 sizeof(license) - 1) < 0)
846 return -EFAULT;
847 license[sizeof(license) - 1] = 0;
848
849 /* eBPF programs must be GPL compatible to use GPL-ed functions */
850 is_gpl = license_is_gpl_compatible(license);
851
Daniel Borkmannef0915c2016-12-07 01:15:44 +0100852 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
853 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700854
Alexei Starovoitov25415172015-03-25 12:49:20 -0700855 if (type == BPF_PROG_TYPE_KPROBE &&
856 attr->kern_version != LINUX_VERSION_CODE)
857 return -EINVAL;
858
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700859 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
860 return -EPERM;
861
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700862 /* plain bpf_prog allocation */
863 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
864 if (!prog)
865 return -ENOMEM;
866
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700867 err = bpf_prog_charge_memlock(prog);
868 if (err)
869 goto free_prog_nouncharge;
870
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700871 prog->len = attr->insn_cnt;
872
873 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100874 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100875 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700876 goto free_prog;
877
878 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200879 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700880
881 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200882 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700883
884 /* find program type: socket_filter vs tracing_filter */
885 err = find_prog_type(type, prog);
886 if (err < 0)
887 goto free_prog;
888
889 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700890 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700891 if (err < 0)
892 goto free_used_maps;
893
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700894 /* fixup BPF_CALL->imm field */
895 fixup_bpf_calls(prog);
896
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700897 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200898 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700899 if (err < 0)
900 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700901
Daniel Borkmannaa797812015-10-29 14:58:06 +0100902 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700903 if (err < 0)
904 /* failed to allocate fd */
905 goto free_used_maps;
906
Daniel Borkmann74451e662017-02-16 22:24:50 +0100907 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100908 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700909 return err;
910
911free_used_maps:
912 free_used_maps(prog->aux);
913free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700914 bpf_prog_uncharge_memlock(prog);
915free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700916 bpf_prog_free(prog);
917 return err;
918}
919
Daniel Borkmannb2197752015-10-29 14:58:09 +0100920#define BPF_OBJ_LAST_FIELD bpf_fd
921
922static int bpf_obj_pin(const union bpf_attr *attr)
923{
924 if (CHECK_ATTR(BPF_OBJ))
925 return -EINVAL;
926
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100927 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100928}
929
930static int bpf_obj_get(const union bpf_attr *attr)
931{
932 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
933 return -EINVAL;
934
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100935 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100936}
937
Daniel Mackf4324552016-11-23 16:52:27 +0100938#ifdef CONFIG_CGROUP_BPF
939
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800940#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
Daniel Mackf4324552016-11-23 16:52:27 +0100941
942static int bpf_prog_attach(const union bpf_attr *attr)
943{
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800944 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +0100945 struct bpf_prog *prog;
946 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800947 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100948
949 if (!capable(CAP_NET_ADMIN))
950 return -EPERM;
951
952 if (CHECK_ATTR(BPF_PROG_ATTACH))
953 return -EINVAL;
954
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800955 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
956 return -EINVAL;
957
Daniel Mackf4324552016-11-23 16:52:27 +0100958 switch (attr->attach_type) {
959 case BPF_CGROUP_INET_INGRESS:
960 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -0800961 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +0100962 break;
David Ahern610236582016-12-01 08:48:04 -0800963 case BPF_CGROUP_INET_SOCK_CREATE:
964 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
965 break;
Daniel Mackf4324552016-11-23 16:52:27 +0100966 default:
967 return -EINVAL;
968 }
969
David Ahernb2cd1252016-12-01 08:48:03 -0800970 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
971 if (IS_ERR(prog))
972 return PTR_ERR(prog);
973
974 cgrp = cgroup_get_from_fd(attr->target_fd);
975 if (IS_ERR(cgrp)) {
976 bpf_prog_put(prog);
977 return PTR_ERR(cgrp);
978 }
979
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800980 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
981 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
982 if (ret)
983 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -0800984 cgroup_put(cgrp);
985
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800986 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100987}
988
989#define BPF_PROG_DETACH_LAST_FIELD attach_type
990
991static int bpf_prog_detach(const union bpf_attr *attr)
992{
993 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800994 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100995
996 if (!capable(CAP_NET_ADMIN))
997 return -EPERM;
998
999 if (CHECK_ATTR(BPF_PROG_DETACH))
1000 return -EINVAL;
1001
1002 switch (attr->attach_type) {
1003 case BPF_CGROUP_INET_INGRESS:
1004 case BPF_CGROUP_INET_EGRESS:
David Ahern610236582016-12-01 08:48:04 -08001005 case BPF_CGROUP_INET_SOCK_CREATE:
Daniel Mackf4324552016-11-23 16:52:27 +01001006 cgrp = cgroup_get_from_fd(attr->target_fd);
1007 if (IS_ERR(cgrp))
1008 return PTR_ERR(cgrp);
1009
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001010 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001011 cgroup_put(cgrp);
1012 break;
1013
1014 default:
1015 return -EINVAL;
1016 }
1017
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001018 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001019}
1020#endif /* CONFIG_CGROUP_BPF */
1021
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001022SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1023{
1024 union bpf_attr attr = {};
1025 int err;
1026
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001027 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001028 return -EPERM;
1029
1030 if (!access_ok(VERIFY_READ, uattr, 1))
1031 return -EFAULT;
1032
1033 if (size > PAGE_SIZE) /* silly large */
1034 return -E2BIG;
1035
1036 /* If we're handed a bigger struct than we know of,
1037 * ensure all the unknown bits are 0 - i.e. new
1038 * user-space does not rely on any kernel feature
1039 * extensions we dont know about yet.
1040 */
1041 if (size > sizeof(attr)) {
1042 unsigned char __user *addr;
1043 unsigned char __user *end;
1044 unsigned char val;
1045
1046 addr = (void __user *)uattr + sizeof(attr);
1047 end = (void __user *)uattr + size;
1048
1049 for (; addr < end; addr++) {
1050 err = get_user(val, addr);
1051 if (err)
1052 return err;
1053 if (val)
1054 return -E2BIG;
1055 }
1056 size = sizeof(attr);
1057 }
1058
1059 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1060 if (copy_from_user(&attr, uattr, size) != 0)
1061 return -EFAULT;
1062
1063 switch (cmd) {
1064 case BPF_MAP_CREATE:
1065 err = map_create(&attr);
1066 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001067 case BPF_MAP_LOOKUP_ELEM:
1068 err = map_lookup_elem(&attr);
1069 break;
1070 case BPF_MAP_UPDATE_ELEM:
1071 err = map_update_elem(&attr);
1072 break;
1073 case BPF_MAP_DELETE_ELEM:
1074 err = map_delete_elem(&attr);
1075 break;
1076 case BPF_MAP_GET_NEXT_KEY:
1077 err = map_get_next_key(&attr);
1078 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001079 case BPF_PROG_LOAD:
1080 err = bpf_prog_load(&attr);
1081 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001082 case BPF_OBJ_PIN:
1083 err = bpf_obj_pin(&attr);
1084 break;
1085 case BPF_OBJ_GET:
1086 err = bpf_obj_get(&attr);
1087 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001088
1089#ifdef CONFIG_CGROUP_BPF
1090 case BPF_PROG_ATTACH:
1091 err = bpf_prog_attach(&attr);
1092 break;
1093 case BPF_PROG_DETACH:
1094 err = bpf_prog_detach(&attr);
1095 break;
1096#endif
1097
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001098 default:
1099 err = -EINVAL;
1100 break;
1101 }
1102
1103 return err;
1104}