blob: 7af0dcc5d7555679cea6c08395ab54710e7066e6 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010017#include <linux/vmalloc.h>
18#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070019#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070020#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070021#include <linux/license.h>
22#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070023#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010024#include <linux/kernel.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070025
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080026DEFINE_PER_CPU(int, bpf_prog_active);
27
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070028int sysctl_unprivileged_bpf_disabled __read_mostly;
29
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070030static LIST_HEAD(bpf_map_types);
31
32static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
33{
34 struct bpf_map_type_list *tl;
35 struct bpf_map *map;
36
37 list_for_each_entry(tl, &bpf_map_types, list_node) {
38 if (tl->type == attr->map_type) {
39 map = tl->ops->map_alloc(attr);
40 if (IS_ERR(map))
41 return map;
42 map->ops = tl->ops;
43 map->map_type = attr->map_type;
44 return map;
45 }
46 }
47 return ERR_PTR(-EINVAL);
48}
49
50/* boot time registration of different map implementations */
51void bpf_register_map_type(struct bpf_map_type_list *tl)
52{
53 list_add(&tl->list_node, &bpf_map_types);
54}
55
Daniel Borkmannd407bd22017-01-18 15:14:17 +010056void *bpf_map_area_alloc(size_t size)
57{
58 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
59 * trigger under memory pressure as we really just want to
60 * fail instead.
61 */
62 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
63 void *area;
64
65 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
66 area = kmalloc(size, GFP_USER | flags);
67 if (area != NULL)
68 return area;
69 }
70
71 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | flags,
72 PAGE_KERNEL);
73}
74
75void bpf_map_area_free(void *area)
76{
77 kvfree(area);
78}
79
Alexei Starovoitov6c905982016-03-07 21:57:15 -080080int bpf_map_precharge_memlock(u32 pages)
81{
82 struct user_struct *user = get_current_user();
83 unsigned long memlock_limit, cur;
84
85 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
86 cur = atomic_long_read(&user->locked_vm);
87 free_uid(user);
88 if (cur + pages > memlock_limit)
89 return -EPERM;
90 return 0;
91}
92
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070093static int bpf_map_charge_memlock(struct bpf_map *map)
94{
95 struct user_struct *user = get_current_user();
96 unsigned long memlock_limit;
97
98 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
99
100 atomic_long_add(map->pages, &user->locked_vm);
101
102 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
103 atomic_long_sub(map->pages, &user->locked_vm);
104 free_uid(user);
105 return -EPERM;
106 }
107 map->user = user;
108 return 0;
109}
110
111static void bpf_map_uncharge_memlock(struct bpf_map *map)
112{
113 struct user_struct *user = map->user;
114
115 atomic_long_sub(map->pages, &user->locked_vm);
116 free_uid(user);
117}
118
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700119/* called from workqueue */
120static void bpf_map_free_deferred(struct work_struct *work)
121{
122 struct bpf_map *map = container_of(work, struct bpf_map, work);
123
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700124 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700125 /* implementation dependent freeing */
126 map->ops->map_free(map);
127}
128
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100129static void bpf_map_put_uref(struct bpf_map *map)
130{
131 if (atomic_dec_and_test(&map->usercnt)) {
132 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
133 bpf_fd_array_map_clear(map);
134 }
135}
136
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700137/* decrement map refcnt and schedule it for freeing via workqueue
138 * (unrelying map implementation ops->map_free() might sleep)
139 */
140void bpf_map_put(struct bpf_map *map)
141{
142 if (atomic_dec_and_test(&map->refcnt)) {
143 INIT_WORK(&map->work, bpf_map_free_deferred);
144 schedule_work(&map->work);
145 }
146}
147
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100148void bpf_map_put_with_uref(struct bpf_map *map)
149{
150 bpf_map_put_uref(map);
151 bpf_map_put(map);
152}
153
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700154static int bpf_map_release(struct inode *inode, struct file *filp)
155{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200156 struct bpf_map *map = filp->private_data;
157
158 if (map->ops->map_release)
159 map->ops->map_release(map, filp);
160
161 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700162 return 0;
163}
164
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100165#ifdef CONFIG_PROC_FS
166static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
167{
168 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100169 const struct bpf_array *array;
170 u32 owner_prog_type = 0;
171
172 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
173 array = container_of(map, struct bpf_array, map);
174 owner_prog_type = array->owner_prog_type;
175 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100176
177 seq_printf(m,
178 "map_type:\t%u\n"
179 "key_size:\t%u\n"
180 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100181 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100182 "map_flags:\t%#x\n"
183 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100184 map->map_type,
185 map->key_size,
186 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100187 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100188 map->map_flags,
189 map->pages * 1ULL << PAGE_SHIFT);
190
191 if (owner_prog_type)
192 seq_printf(m, "owner_prog_type:\t%u\n",
193 owner_prog_type);
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100194}
195#endif
196
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700197static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100198#ifdef CONFIG_PROC_FS
199 .show_fdinfo = bpf_map_show_fdinfo,
200#endif
201 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700202};
203
Daniel Borkmannb2197752015-10-29 14:58:09 +0100204int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100205{
206 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
207 O_RDWR | O_CLOEXEC);
208}
209
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700210/* helper macro to check that unused fields 'union bpf_attr' are zero */
211#define CHECK_ATTR(CMD) \
212 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
213 sizeof(attr->CMD##_LAST_FIELD), 0, \
214 sizeof(*attr) - \
215 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
216 sizeof(attr->CMD##_LAST_FIELD)) != NULL
217
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800218#define BPF_MAP_CREATE_LAST_FIELD map_flags
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700219/* called via syscall */
220static int map_create(union bpf_attr *attr)
221{
222 struct bpf_map *map;
223 int err;
224
225 err = CHECK_ATTR(BPF_MAP_CREATE);
226 if (err)
227 return -EINVAL;
228
229 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
230 map = find_and_alloc_map(attr);
231 if (IS_ERR(map))
232 return PTR_ERR(map);
233
234 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100235 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700236
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700237 err = bpf_map_charge_memlock(map);
238 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100239 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700240
Daniel Borkmannaa797812015-10-29 14:58:06 +0100241 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700242 if (err < 0)
243 /* failed to allocate fd */
244 goto free_map;
245
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100246 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700247 return err;
248
249free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100250 bpf_map_uncharge_memlock(map);
251free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700252 map->ops->map_free(map);
253 return err;
254}
255
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700256/* if error is returned, fd is released.
257 * On success caller should complete fd access with matching fdput()
258 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100259struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700260{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700261 if (!f.file)
262 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700263 if (f.file->f_op != &bpf_map_fops) {
264 fdput(f);
265 return ERR_PTR(-EINVAL);
266 }
267
Daniel Borkmannc2101292015-10-29 14:58:07 +0100268 return f.file->private_data;
269}
270
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700271/* prog's and map's refcnt limit */
272#define BPF_MAX_REFCNT 32768
273
274struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100275{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700276 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
277 atomic_dec(&map->refcnt);
278 return ERR_PTR(-EBUSY);
279 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100280 if (uref)
281 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700282 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100283}
284
285struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100286{
287 struct fd f = fdget(ufd);
288 struct bpf_map *map;
289
290 map = __bpf_map_get(f);
291 if (IS_ERR(map))
292 return map;
293
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700294 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100295 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700296
297 return map;
298}
299
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800300int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
301{
302 return -ENOTSUPP;
303}
304
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700305/* last field in 'union bpf_attr' used by this command */
306#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
307
308static int map_lookup_elem(union bpf_attr *attr)
309{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100310 void __user *ukey = u64_to_user_ptr(attr->key);
311 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700312 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700313 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800314 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800315 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200316 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700317 int err;
318
319 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
320 return -EINVAL;
321
Daniel Borkmann592867b2015-09-08 18:00:09 +0200322 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100323 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700324 if (IS_ERR(map))
325 return PTR_ERR(map);
326
327 err = -ENOMEM;
328 key = kmalloc(map->key_size, GFP_USER);
329 if (!key)
330 goto err_put;
331
332 err = -EFAULT;
333 if (copy_from_user(key, ukey, map->key_size) != 0)
334 goto free_key;
335
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800336 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800337 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800338 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
339 value_size = round_up(map->value_size, 8) * num_possible_cpus();
340 else
341 value_size = map->value_size;
342
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800343 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800344 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700345 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800346 goto free_key;
347
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800348 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
349 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800350 err = bpf_percpu_hash_copy(map, key, value);
351 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
352 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800353 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
354 err = bpf_stackmap_copy(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800355 } else {
356 rcu_read_lock();
357 ptr = map->ops->map_lookup_elem(map, key);
358 if (ptr)
359 memcpy(value, ptr, value_size);
360 rcu_read_unlock();
361 err = ptr ? 0 : -ENOENT;
362 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800363
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800364 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800365 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700366
367 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800368 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800369 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700370
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100371 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700372 err = 0;
373
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800374free_value:
375 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700376free_key:
377 kfree(key);
378err_put:
379 fdput(f);
380 return err;
381}
382
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800383#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700384
385static int map_update_elem(union bpf_attr *attr)
386{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100387 void __user *ukey = u64_to_user_ptr(attr->key);
388 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700389 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700390 struct bpf_map *map;
391 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800392 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200393 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700394 int err;
395
396 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
397 return -EINVAL;
398
Daniel Borkmann592867b2015-09-08 18:00:09 +0200399 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100400 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700401 if (IS_ERR(map))
402 return PTR_ERR(map);
403
404 err = -ENOMEM;
405 key = kmalloc(map->key_size, GFP_USER);
406 if (!key)
407 goto err_put;
408
409 err = -EFAULT;
410 if (copy_from_user(key, ukey, map->key_size) != 0)
411 goto free_key;
412
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800413 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800414 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800415 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
416 value_size = round_up(map->value_size, 8) * num_possible_cpus();
417 else
418 value_size = map->value_size;
419
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700420 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800421 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700422 if (!value)
423 goto free_key;
424
425 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800426 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700427 goto free_value;
428
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800429 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
430 * inside bpf map update or delete otherwise deadlocks are possible
431 */
432 preempt_disable();
433 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800434 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
435 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800436 err = bpf_percpu_hash_update(map, key, value, attr->flags);
437 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
438 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200439 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700440 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
441 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200442 rcu_read_lock();
443 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
444 attr->flags);
445 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800446 } else {
447 rcu_read_lock();
448 err = map->ops->map_update_elem(map, key, value, attr->flags);
449 rcu_read_unlock();
450 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800451 __this_cpu_dec(bpf_prog_active);
452 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700453
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100454 if (!err)
455 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700456free_value:
457 kfree(value);
458free_key:
459 kfree(key);
460err_put:
461 fdput(f);
462 return err;
463}
464
465#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
466
467static int map_delete_elem(union bpf_attr *attr)
468{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100469 void __user *ukey = u64_to_user_ptr(attr->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;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200472 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700473 void *key;
474 int err;
475
476 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
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
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800493 preempt_disable();
494 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700495 rcu_read_lock();
496 err = map->ops->map_delete_elem(map, key);
497 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800498 __this_cpu_dec(bpf_prog_active);
499 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700500
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100501 if (!err)
502 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700503free_key:
504 kfree(key);
505err_put:
506 fdput(f);
507 return err;
508}
509
510/* last field in 'union bpf_attr' used by this command */
511#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
512
513static int map_get_next_key(union bpf_attr *attr)
514{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100515 void __user *ukey = u64_to_user_ptr(attr->key);
516 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700517 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700518 struct bpf_map *map;
519 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200520 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700521 int err;
522
523 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
524 return -EINVAL;
525
Daniel Borkmann592867b2015-09-08 18:00:09 +0200526 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100527 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700528 if (IS_ERR(map))
529 return PTR_ERR(map);
530
531 err = -ENOMEM;
532 key = kmalloc(map->key_size, GFP_USER);
533 if (!key)
534 goto err_put;
535
536 err = -EFAULT;
537 if (copy_from_user(key, ukey, map->key_size) != 0)
538 goto free_key;
539
540 err = -ENOMEM;
541 next_key = kmalloc(map->key_size, GFP_USER);
542 if (!next_key)
543 goto free_key;
544
545 rcu_read_lock();
546 err = map->ops->map_get_next_key(map, key, next_key);
547 rcu_read_unlock();
548 if (err)
549 goto free_next_key;
550
551 err = -EFAULT;
552 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
553 goto free_next_key;
554
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100555 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700556 err = 0;
557
558free_next_key:
559 kfree(next_key);
560free_key:
561 kfree(key);
562err_put:
563 fdput(f);
564 return err;
565}
566
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700567static LIST_HEAD(bpf_prog_types);
568
569static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
570{
571 struct bpf_prog_type_list *tl;
572
573 list_for_each_entry(tl, &bpf_prog_types, list_node) {
574 if (tl->type == type) {
575 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100576 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700577 return 0;
578 }
579 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100580
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700581 return -EINVAL;
582}
583
584void bpf_register_prog_type(struct bpf_prog_type_list *tl)
585{
586 list_add(&tl->list_node, &bpf_prog_types);
587}
588
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700589/* fixup insn->imm field of bpf_call instructions:
590 * if (insn->imm == BPF_FUNC_map_lookup_elem)
591 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
592 * else if (insn->imm == BPF_FUNC_map_update_elem)
593 * insn->imm = bpf_map_update_elem - __bpf_call_base;
594 * else ...
595 *
596 * this function is called after eBPF program passed verification
597 */
598static void fixup_bpf_calls(struct bpf_prog *prog)
599{
600 const struct bpf_func_proto *fn;
601 int i;
602
603 for (i = 0; i < prog->len; i++) {
604 struct bpf_insn *insn = &prog->insnsi[i];
605
606 if (insn->code == (BPF_JMP | BPF_CALL)) {
607 /* we reach here when program has bpf_call instructions
608 * and it passed bpf_check(), means that
609 * ops->get_func_proto must have been supplied, check it
610 */
611 BUG_ON(!prog->aux->ops->get_func_proto);
612
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200613 if (insn->imm == BPF_FUNC_get_route_realm)
614 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200615 if (insn->imm == BPF_FUNC_get_prandom_u32)
616 bpf_user_rnd_init_once();
Martin KaFai Lau17bedab2016-12-07 15:53:11 -0800617 if (insn->imm == BPF_FUNC_xdp_adjust_head)
618 prog->xdp_adjust_head = 1;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700619 if (insn->imm == BPF_FUNC_tail_call) {
620 /* mark bpf_tail_call as different opcode
621 * to avoid conditional branch in
622 * interpeter for every normal call
623 * and to prevent accidental JITing by
624 * JIT compiler that doesn't support
625 * bpf_tail_call yet
626 */
627 insn->imm = 0;
628 insn->code |= BPF_X;
629 continue;
630 }
631
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700632 fn = prog->aux->ops->get_func_proto(insn->imm);
633 /* all functions that have prototype and verifier allowed
634 * programs to call them, must be real in-kernel functions
635 */
636 BUG_ON(!fn->func);
637 insn->imm = fn->func - __bpf_call_base;
638 }
639 }
640}
641
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700642/* drop refcnt on maps used by eBPF program and free auxilary data */
643static void free_used_maps(struct bpf_prog_aux *aux)
644{
645 int i;
646
647 for (i = 0; i < aux->used_map_cnt; i++)
648 bpf_map_put(aux->used_maps[i]);
649
650 kfree(aux->used_maps);
651}
652
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100653int __bpf_prog_charge(struct user_struct *user, u32 pages)
654{
655 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
656 unsigned long user_bufs;
657
658 if (user) {
659 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
660 if (user_bufs > memlock_limit) {
661 atomic_long_sub(pages, &user->locked_vm);
662 return -EPERM;
663 }
664 }
665
666 return 0;
667}
668
669void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
670{
671 if (user)
672 atomic_long_sub(pages, &user->locked_vm);
673}
674
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700675static int bpf_prog_charge_memlock(struct bpf_prog *prog)
676{
677 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100678 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700679
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100680 ret = __bpf_prog_charge(user, prog->pages);
681 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700682 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100683 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700684 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100685
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700686 prog->aux->user = user;
687 return 0;
688}
689
690static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
691{
692 struct user_struct *user = prog->aux->user;
693
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100694 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700695 free_uid(user);
696}
697
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200698static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700699{
700 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
701
702 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700703 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700704 bpf_prog_free(aux->prog);
705}
706
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700707void bpf_prog_put(struct bpf_prog *prog)
708{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100709 if (atomic_dec_and_test(&prog->aux->refcnt)) {
710 trace_bpf_prog_put_rcu(prog);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100711 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200712 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100713 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700714}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100715EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700716
717static int bpf_prog_release(struct inode *inode, struct file *filp)
718{
719 struct bpf_prog *prog = filp->private_data;
720
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200721 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700722 return 0;
723}
724
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100725#ifdef CONFIG_PROC_FS
726static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
727{
728 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100729 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100730
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100731 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100732 seq_printf(m,
733 "prog_type:\t%u\n"
734 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100735 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100736 "memlock:\t%llu\n",
737 prog->type,
738 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100739 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100740 prog->pages * 1ULL << PAGE_SHIFT);
741}
742#endif
743
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700744static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100745#ifdef CONFIG_PROC_FS
746 .show_fdinfo = bpf_prog_show_fdinfo,
747#endif
748 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700749};
750
Daniel Borkmannb2197752015-10-29 14:58:09 +0100751int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100752{
753 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
754 O_RDWR | O_CLOEXEC);
755}
756
Daniel Borkmann113214b2016-06-30 17:24:44 +0200757static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700758{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700759 if (!f.file)
760 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700761 if (f.file->f_op != &bpf_prog_fops) {
762 fdput(f);
763 return ERR_PTR(-EINVAL);
764 }
765
Daniel Borkmannc2101292015-10-29 14:58:07 +0100766 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700767}
768
Brenden Blanco59d36562016-07-19 12:16:46 -0700769struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700770{
Brenden Blanco59d36562016-07-19 12:16:46 -0700771 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
772 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700773 return ERR_PTR(-EBUSY);
774 }
775 return prog;
776}
Brenden Blanco59d36562016-07-19 12:16:46 -0700777EXPORT_SYMBOL_GPL(bpf_prog_add);
778
Daniel Borkmannc5405942016-11-09 22:02:34 +0100779void bpf_prog_sub(struct bpf_prog *prog, int i)
780{
781 /* Only to be used for undoing previous bpf_prog_add() in some
782 * error path. We still know that another entity in our call
783 * path holds a reference to the program, thus atomic_sub() can
784 * be safely used in such cases!
785 */
786 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
787}
788EXPORT_SYMBOL_GPL(bpf_prog_sub);
789
Brenden Blanco59d36562016-07-19 12:16:46 -0700790struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
791{
792 return bpf_prog_add(prog, 1);
793}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100794EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700795
Daniel Borkmann113214b2016-06-30 17:24:44 +0200796static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700797{
798 struct fd f = fdget(ufd);
799 struct bpf_prog *prog;
800
Daniel Borkmann113214b2016-06-30 17:24:44 +0200801 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700802 if (IS_ERR(prog))
803 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200804 if (type && prog->type != *type) {
805 prog = ERR_PTR(-EINVAL);
806 goto out;
807 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700808
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700809 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200810out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700811 fdput(f);
812 return prog;
813}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200814
815struct bpf_prog *bpf_prog_get(u32 ufd)
816{
817 return __bpf_prog_get(ufd, NULL);
818}
819
820struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
821{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100822 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
823
824 if (!IS_ERR(prog))
825 trace_bpf_prog_get_type(prog);
826 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200827}
828EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700829
830/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700831#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700832
833static int bpf_prog_load(union bpf_attr *attr)
834{
835 enum bpf_prog_type type = attr->prog_type;
836 struct bpf_prog *prog;
837 int err;
838 char license[128];
839 bool is_gpl;
840
841 if (CHECK_ATTR(BPF_PROG_LOAD))
842 return -EINVAL;
843
844 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100845 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700846 sizeof(license) - 1) < 0)
847 return -EFAULT;
848 license[sizeof(license) - 1] = 0;
849
850 /* eBPF programs must be GPL compatible to use GPL-ed functions */
851 is_gpl = license_is_gpl_compatible(license);
852
Daniel Borkmannef0915c2016-12-07 01:15:44 +0100853 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
854 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700855
Alexei Starovoitov25415172015-03-25 12:49:20 -0700856 if (type == BPF_PROG_TYPE_KPROBE &&
857 attr->kern_version != LINUX_VERSION_CODE)
858 return -EINVAL;
859
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700860 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
861 return -EPERM;
862
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700863 /* plain bpf_prog allocation */
864 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
865 if (!prog)
866 return -ENOMEM;
867
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700868 err = bpf_prog_charge_memlock(prog);
869 if (err)
870 goto free_prog_nouncharge;
871
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700872 prog->len = attr->insn_cnt;
873
874 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100875 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100876 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700877 goto free_prog;
878
879 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200880 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700881
882 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200883 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700884
885 /* find program type: socket_filter vs tracing_filter */
886 err = find_prog_type(type, prog);
887 if (err < 0)
888 goto free_prog;
889
890 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700891 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700892 if (err < 0)
893 goto free_used_maps;
894
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700895 /* fixup BPF_CALL->imm field */
896 fixup_bpf_calls(prog);
897
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700898 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200899 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700900 if (err < 0)
901 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700902
Daniel Borkmannaa797812015-10-29 14:58:06 +0100903 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700904 if (err < 0)
905 /* failed to allocate fd */
906 goto free_used_maps;
907
Daniel Borkmann74451e662017-02-16 22:24:50 +0100908 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100909 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700910 return err;
911
912free_used_maps:
913 free_used_maps(prog->aux);
914free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700915 bpf_prog_uncharge_memlock(prog);
916free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700917 bpf_prog_free(prog);
918 return err;
919}
920
Daniel Borkmannb2197752015-10-29 14:58:09 +0100921#define BPF_OBJ_LAST_FIELD bpf_fd
922
923static int bpf_obj_pin(const union bpf_attr *attr)
924{
925 if (CHECK_ATTR(BPF_OBJ))
926 return -EINVAL;
927
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100928 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100929}
930
931static int bpf_obj_get(const union bpf_attr *attr)
932{
933 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
934 return -EINVAL;
935
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100936 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100937}
938
Daniel Mackf4324552016-11-23 16:52:27 +0100939#ifdef CONFIG_CGROUP_BPF
940
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800941#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
Daniel Mackf4324552016-11-23 16:52:27 +0100942
943static int bpf_prog_attach(const union bpf_attr *attr)
944{
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800945 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +0100946 struct bpf_prog *prog;
947 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800948 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100949
950 if (!capable(CAP_NET_ADMIN))
951 return -EPERM;
952
953 if (CHECK_ATTR(BPF_PROG_ATTACH))
954 return -EINVAL;
955
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800956 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
957 return -EINVAL;
958
Daniel Mackf4324552016-11-23 16:52:27 +0100959 switch (attr->attach_type) {
960 case BPF_CGROUP_INET_INGRESS:
961 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -0800962 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +0100963 break;
David Ahern610236582016-12-01 08:48:04 -0800964 case BPF_CGROUP_INET_SOCK_CREATE:
965 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
966 break;
Daniel Mackf4324552016-11-23 16:52:27 +0100967 default:
968 return -EINVAL;
969 }
970
David Ahernb2cd1252016-12-01 08:48:03 -0800971 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
972 if (IS_ERR(prog))
973 return PTR_ERR(prog);
974
975 cgrp = cgroup_get_from_fd(attr->target_fd);
976 if (IS_ERR(cgrp)) {
977 bpf_prog_put(prog);
978 return PTR_ERR(cgrp);
979 }
980
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800981 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
982 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
983 if (ret)
984 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -0800985 cgroup_put(cgrp);
986
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800987 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100988}
989
990#define BPF_PROG_DETACH_LAST_FIELD attach_type
991
992static int bpf_prog_detach(const union bpf_attr *attr)
993{
994 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800995 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100996
997 if (!capable(CAP_NET_ADMIN))
998 return -EPERM;
999
1000 if (CHECK_ATTR(BPF_PROG_DETACH))
1001 return -EINVAL;
1002
1003 switch (attr->attach_type) {
1004 case BPF_CGROUP_INET_INGRESS:
1005 case BPF_CGROUP_INET_EGRESS:
David Ahern610236582016-12-01 08:48:04 -08001006 case BPF_CGROUP_INET_SOCK_CREATE:
Daniel Mackf4324552016-11-23 16:52:27 +01001007 cgrp = cgroup_get_from_fd(attr->target_fd);
1008 if (IS_ERR(cgrp))
1009 return PTR_ERR(cgrp);
1010
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001011 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001012 cgroup_put(cgrp);
1013 break;
1014
1015 default:
1016 return -EINVAL;
1017 }
1018
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001019 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001020}
1021#endif /* CONFIG_CGROUP_BPF */
1022
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001023SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1024{
1025 union bpf_attr attr = {};
1026 int err;
1027
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001028 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001029 return -EPERM;
1030
1031 if (!access_ok(VERIFY_READ, uattr, 1))
1032 return -EFAULT;
1033
1034 if (size > PAGE_SIZE) /* silly large */
1035 return -E2BIG;
1036
1037 /* If we're handed a bigger struct than we know of,
1038 * ensure all the unknown bits are 0 - i.e. new
1039 * user-space does not rely on any kernel feature
1040 * extensions we dont know about yet.
1041 */
1042 if (size > sizeof(attr)) {
1043 unsigned char __user *addr;
1044 unsigned char __user *end;
1045 unsigned char val;
1046
1047 addr = (void __user *)uattr + sizeof(attr);
1048 end = (void __user *)uattr + size;
1049
1050 for (; addr < end; addr++) {
1051 err = get_user(val, addr);
1052 if (err)
1053 return err;
1054 if (val)
1055 return -E2BIG;
1056 }
1057 size = sizeof(attr);
1058 }
1059
1060 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1061 if (copy_from_user(&attr, uattr, size) != 0)
1062 return -EFAULT;
1063
1064 switch (cmd) {
1065 case BPF_MAP_CREATE:
1066 err = map_create(&attr);
1067 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001068 case BPF_MAP_LOOKUP_ELEM:
1069 err = map_lookup_elem(&attr);
1070 break;
1071 case BPF_MAP_UPDATE_ELEM:
1072 err = map_update_elem(&attr);
1073 break;
1074 case BPF_MAP_DELETE_ELEM:
1075 err = map_delete_elem(&attr);
1076 break;
1077 case BPF_MAP_GET_NEXT_KEY:
1078 err = map_get_next_key(&attr);
1079 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001080 case BPF_PROG_LOAD:
1081 err = bpf_prog_load(&attr);
1082 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001083 case BPF_OBJ_PIN:
1084 err = bpf_obj_pin(&attr);
1085 break;
1086 case BPF_OBJ_GET:
1087 err = bpf_obj_get(&attr);
1088 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001089
1090#ifdef CONFIG_CGROUP_BPF
1091 case BPF_PROG_ATTACH:
1092 err = bpf_prog_attach(&attr);
1093 break;
1094 case BPF_PROG_DETACH:
1095 err = bpf_prog_detach(&attr);
1096 break;
1097#endif
1098
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001099 default:
1100 err = -EINVAL;
1101 break;
1102 }
1103
1104 return err;
1105}