blob: 2a1b32b470f186bdf18eb8e8ab0ed507ffb6754f [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>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070025#include <linux/idr.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070026
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080027DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070028static DEFINE_IDR(prog_idr);
29static DEFINE_SPINLOCK(prog_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080030
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070031int sysctl_unprivileged_bpf_disabled __read_mostly;
32
Johannes Berg40077e02017-04-11 15:34:58 +020033static const struct bpf_map_ops * const bpf_map_types[] = {
34#define BPF_PROG_TYPE(_id, _ops)
35#define BPF_MAP_TYPE(_id, _ops) \
36 [_id] = &_ops,
37#include <linux/bpf_types.h>
38#undef BPF_PROG_TYPE
39#undef BPF_MAP_TYPE
40};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070041
42static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
43{
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070044 struct bpf_map *map;
45
Johannes Berg40077e02017-04-11 15:34:58 +020046 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
47 !bpf_map_types[attr->map_type])
48 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070049
Johannes Berg40077e02017-04-11 15:34:58 +020050 map = bpf_map_types[attr->map_type]->map_alloc(attr);
51 if (IS_ERR(map))
52 return map;
53 map->ops = bpf_map_types[attr->map_type];
54 map->map_type = attr->map_type;
55 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070056}
57
Daniel Borkmannd407bd22017-01-18 15:14:17 +010058void *bpf_map_area_alloc(size_t size)
59{
60 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
61 * trigger under memory pressure as we really just want to
62 * fail instead.
63 */
64 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
65 void *area;
66
67 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
68 area = kmalloc(size, GFP_USER | flags);
69 if (area != NULL)
70 return area;
71 }
72
Michal Hocko19809c22017-05-08 15:57:44 -070073 return __vmalloc(size, GFP_KERNEL | flags, PAGE_KERNEL);
Daniel Borkmannd407bd22017-01-18 15:14:17 +010074}
75
76void bpf_map_area_free(void *area)
77{
78 kvfree(area);
79}
80
Alexei Starovoitov6c905982016-03-07 21:57:15 -080081int bpf_map_precharge_memlock(u32 pages)
82{
83 struct user_struct *user = get_current_user();
84 unsigned long memlock_limit, cur;
85
86 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
87 cur = atomic_long_read(&user->locked_vm);
88 free_uid(user);
89 if (cur + pages > memlock_limit)
90 return -EPERM;
91 return 0;
92}
93
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070094static int bpf_map_charge_memlock(struct bpf_map *map)
95{
96 struct user_struct *user = get_current_user();
97 unsigned long memlock_limit;
98
99 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
100
101 atomic_long_add(map->pages, &user->locked_vm);
102
103 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
104 atomic_long_sub(map->pages, &user->locked_vm);
105 free_uid(user);
106 return -EPERM;
107 }
108 map->user = user;
109 return 0;
110}
111
112static void bpf_map_uncharge_memlock(struct bpf_map *map)
113{
114 struct user_struct *user = map->user;
115
116 atomic_long_sub(map->pages, &user->locked_vm);
117 free_uid(user);
118}
119
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700120/* called from workqueue */
121static void bpf_map_free_deferred(struct work_struct *work)
122{
123 struct bpf_map *map = container_of(work, struct bpf_map, work);
124
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700125 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700126 /* implementation dependent freeing */
127 map->ops->map_free(map);
128}
129
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100130static void bpf_map_put_uref(struct bpf_map *map)
131{
132 if (atomic_dec_and_test(&map->usercnt)) {
133 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
134 bpf_fd_array_map_clear(map);
135 }
136}
137
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700138/* decrement map refcnt and schedule it for freeing via workqueue
139 * (unrelying map implementation ops->map_free() might sleep)
140 */
141void bpf_map_put(struct bpf_map *map)
142{
143 if (atomic_dec_and_test(&map->refcnt)) {
144 INIT_WORK(&map->work, bpf_map_free_deferred);
145 schedule_work(&map->work);
146 }
147}
148
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100149void bpf_map_put_with_uref(struct bpf_map *map)
150{
151 bpf_map_put_uref(map);
152 bpf_map_put(map);
153}
154
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700155static int bpf_map_release(struct inode *inode, struct file *filp)
156{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200157 struct bpf_map *map = filp->private_data;
158
159 if (map->ops->map_release)
160 map->ops->map_release(map, filp);
161
162 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700163 return 0;
164}
165
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100166#ifdef CONFIG_PROC_FS
167static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
168{
169 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100170 const struct bpf_array *array;
171 u32 owner_prog_type = 0;
172
173 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
174 array = container_of(map, struct bpf_array, map);
175 owner_prog_type = array->owner_prog_type;
176 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100177
178 seq_printf(m,
179 "map_type:\t%u\n"
180 "key_size:\t%u\n"
181 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100182 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100183 "map_flags:\t%#x\n"
184 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100185 map->map_type,
186 map->key_size,
187 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100188 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100189 map->map_flags,
190 map->pages * 1ULL << PAGE_SHIFT);
191
192 if (owner_prog_type)
193 seq_printf(m, "owner_prog_type:\t%u\n",
194 owner_prog_type);
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100195}
196#endif
197
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700198static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100199#ifdef CONFIG_PROC_FS
200 .show_fdinfo = bpf_map_show_fdinfo,
201#endif
202 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700203};
204
Daniel Borkmannb2197752015-10-29 14:58:09 +0100205int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100206{
207 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
208 O_RDWR | O_CLOEXEC);
209}
210
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700211/* helper macro to check that unused fields 'union bpf_attr' are zero */
212#define CHECK_ATTR(CMD) \
213 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
214 sizeof(attr->CMD##_LAST_FIELD), 0, \
215 sizeof(*attr) - \
216 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
217 sizeof(attr->CMD##_LAST_FIELD)) != NULL
218
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700219#define BPF_MAP_CREATE_LAST_FIELD inner_map_fd
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700220/* called via syscall */
221static int map_create(union bpf_attr *attr)
222{
223 struct bpf_map *map;
224 int err;
225
226 err = CHECK_ATTR(BPF_MAP_CREATE);
227 if (err)
228 return -EINVAL;
229
230 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
231 map = find_and_alloc_map(attr);
232 if (IS_ERR(map))
233 return PTR_ERR(map);
234
235 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100236 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700237
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700238 err = bpf_map_charge_memlock(map);
239 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100240 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700241
Daniel Borkmannaa797812015-10-29 14:58:06 +0100242 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700243 if (err < 0)
244 /* failed to allocate fd */
245 goto free_map;
246
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100247 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700248 return err;
249
250free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100251 bpf_map_uncharge_memlock(map);
252free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700253 map->ops->map_free(map);
254 return err;
255}
256
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700257/* if error is returned, fd is released.
258 * On success caller should complete fd access with matching fdput()
259 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100260struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700261{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700262 if (!f.file)
263 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700264 if (f.file->f_op != &bpf_map_fops) {
265 fdput(f);
266 return ERR_PTR(-EINVAL);
267 }
268
Daniel Borkmannc2101292015-10-29 14:58:07 +0100269 return f.file->private_data;
270}
271
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700272/* prog's and map's refcnt limit */
273#define BPF_MAX_REFCNT 32768
274
275struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100276{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700277 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
278 atomic_dec(&map->refcnt);
279 return ERR_PTR(-EBUSY);
280 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100281 if (uref)
282 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700283 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100284}
285
286struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100287{
288 struct fd f = fdget(ufd);
289 struct bpf_map *map;
290
291 map = __bpf_map_get(f);
292 if (IS_ERR(map))
293 return map;
294
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700295 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100296 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700297
298 return map;
299}
300
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800301int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
302{
303 return -ENOTSUPP;
304}
305
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700306/* last field in 'union bpf_attr' used by this command */
307#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
308
309static int map_lookup_elem(union bpf_attr *attr)
310{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100311 void __user *ukey = u64_to_user_ptr(attr->key);
312 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700313 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700314 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800315 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800316 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200317 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700318 int err;
319
320 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
321 return -EINVAL;
322
Daniel Borkmann592867b2015-09-08 18:00:09 +0200323 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100324 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700325 if (IS_ERR(map))
326 return PTR_ERR(map);
327
328 err = -ENOMEM;
329 key = kmalloc(map->key_size, GFP_USER);
330 if (!key)
331 goto err_put;
332
333 err = -EFAULT;
334 if (copy_from_user(key, ukey, map->key_size) != 0)
335 goto free_key;
336
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800337 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800338 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800339 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
340 value_size = round_up(map->value_size, 8) * num_possible_cpus();
341 else
342 value_size = map->value_size;
343
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800344 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800345 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700346 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800347 goto free_key;
348
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800349 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
350 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800351 err = bpf_percpu_hash_copy(map, key, value);
352 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
353 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800354 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
355 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700356 } else if (map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
357 map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700358 err = -ENOTSUPP;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800359 } else {
360 rcu_read_lock();
361 ptr = map->ops->map_lookup_elem(map, key);
362 if (ptr)
363 memcpy(value, ptr, value_size);
364 rcu_read_unlock();
365 err = ptr ? 0 : -ENOENT;
366 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800367
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800368 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800369 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700370
371 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800372 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800373 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700374
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100375 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700376 err = 0;
377
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800378free_value:
379 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700380free_key:
381 kfree(key);
382err_put:
383 fdput(f);
384 return err;
385}
386
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800387#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700388
389static int map_update_elem(union bpf_attr *attr)
390{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100391 void __user *ukey = u64_to_user_ptr(attr->key);
392 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700393 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700394 struct bpf_map *map;
395 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800396 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200397 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700398 int err;
399
400 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
401 return -EINVAL;
402
Daniel Borkmann592867b2015-09-08 18:00:09 +0200403 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100404 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700405 if (IS_ERR(map))
406 return PTR_ERR(map);
407
408 err = -ENOMEM;
409 key = kmalloc(map->key_size, GFP_USER);
410 if (!key)
411 goto err_put;
412
413 err = -EFAULT;
414 if (copy_from_user(key, ukey, map->key_size) != 0)
415 goto free_key;
416
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800417 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800418 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800419 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
420 value_size = round_up(map->value_size, 8) * num_possible_cpus();
421 else
422 value_size = map->value_size;
423
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700424 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800425 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700426 if (!value)
427 goto free_key;
428
429 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800430 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700431 goto free_value;
432
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800433 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
434 * inside bpf map update or delete otherwise deadlocks are possible
435 */
436 preempt_disable();
437 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800438 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
439 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800440 err = bpf_percpu_hash_update(map, key, value, attr->flags);
441 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
442 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200443 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700444 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700445 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
446 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200447 rcu_read_lock();
448 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
449 attr->flags);
450 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700451 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
452 rcu_read_lock();
453 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
454 attr->flags);
455 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800456 } else {
457 rcu_read_lock();
458 err = map->ops->map_update_elem(map, key, value, attr->flags);
459 rcu_read_unlock();
460 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800461 __this_cpu_dec(bpf_prog_active);
462 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700463
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100464 if (!err)
465 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700466free_value:
467 kfree(value);
468free_key:
469 kfree(key);
470err_put:
471 fdput(f);
472 return err;
473}
474
475#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
476
477static int map_delete_elem(union bpf_attr *attr)
478{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100479 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700480 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700481 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200482 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700483 void *key;
484 int err;
485
486 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
487 return -EINVAL;
488
Daniel Borkmann592867b2015-09-08 18:00:09 +0200489 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100490 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700491 if (IS_ERR(map))
492 return PTR_ERR(map);
493
494 err = -ENOMEM;
495 key = kmalloc(map->key_size, GFP_USER);
496 if (!key)
497 goto err_put;
498
499 err = -EFAULT;
500 if (copy_from_user(key, ukey, map->key_size) != 0)
501 goto free_key;
502
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800503 preempt_disable();
504 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700505 rcu_read_lock();
506 err = map->ops->map_delete_elem(map, key);
507 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800508 __this_cpu_dec(bpf_prog_active);
509 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700510
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100511 if (!err)
512 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700513free_key:
514 kfree(key);
515err_put:
516 fdput(f);
517 return err;
518}
519
520/* last field in 'union bpf_attr' used by this command */
521#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
522
523static int map_get_next_key(union bpf_attr *attr)
524{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100525 void __user *ukey = u64_to_user_ptr(attr->key);
526 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700527 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700528 struct bpf_map *map;
529 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200530 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700531 int err;
532
533 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
534 return -EINVAL;
535
Daniel Borkmann592867b2015-09-08 18:00:09 +0200536 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100537 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700538 if (IS_ERR(map))
539 return PTR_ERR(map);
540
Teng Qin8fe45922017-04-24 19:00:37 -0700541 if (ukey) {
542 err = -ENOMEM;
543 key = kmalloc(map->key_size, GFP_USER);
544 if (!key)
545 goto err_put;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700546
Teng Qin8fe45922017-04-24 19:00:37 -0700547 err = -EFAULT;
548 if (copy_from_user(key, ukey, map->key_size) != 0)
549 goto free_key;
550 } else {
551 key = NULL;
552 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700553
554 err = -ENOMEM;
555 next_key = kmalloc(map->key_size, GFP_USER);
556 if (!next_key)
557 goto free_key;
558
559 rcu_read_lock();
560 err = map->ops->map_get_next_key(map, key, next_key);
561 rcu_read_unlock();
562 if (err)
563 goto free_next_key;
564
565 err = -EFAULT;
566 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
567 goto free_next_key;
568
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100569 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700570 err = 0;
571
572free_next_key:
573 kfree(next_key);
574free_key:
575 kfree(key);
576err_put:
577 fdput(f);
578 return err;
579}
580
Johannes Bergbe9370a2017-04-11 15:34:57 +0200581static const struct bpf_verifier_ops * const bpf_prog_types[] = {
582#define BPF_PROG_TYPE(_id, _ops) \
583 [_id] = &_ops,
Johannes Berg40077e02017-04-11 15:34:58 +0200584#define BPF_MAP_TYPE(_id, _ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +0200585#include <linux/bpf_types.h>
586#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +0200587#undef BPF_MAP_TYPE
Johannes Bergbe9370a2017-04-11 15:34:57 +0200588};
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700589
590static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
591{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200592 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
593 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700594
Johannes Bergbe9370a2017-04-11 15:34:57 +0200595 prog->aux->ops = bpf_prog_types[type];
596 prog->type = type;
597 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700598}
599
600/* drop refcnt on maps used by eBPF program and free auxilary data */
601static void free_used_maps(struct bpf_prog_aux *aux)
602{
603 int i;
604
605 for (i = 0; i < aux->used_map_cnt; i++)
606 bpf_map_put(aux->used_maps[i]);
607
608 kfree(aux->used_maps);
609}
610
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100611int __bpf_prog_charge(struct user_struct *user, u32 pages)
612{
613 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
614 unsigned long user_bufs;
615
616 if (user) {
617 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
618 if (user_bufs > memlock_limit) {
619 atomic_long_sub(pages, &user->locked_vm);
620 return -EPERM;
621 }
622 }
623
624 return 0;
625}
626
627void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
628{
629 if (user)
630 atomic_long_sub(pages, &user->locked_vm);
631}
632
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700633static int bpf_prog_charge_memlock(struct bpf_prog *prog)
634{
635 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100636 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700637
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100638 ret = __bpf_prog_charge(user, prog->pages);
639 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700640 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100641 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700642 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100643
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700644 prog->aux->user = user;
645 return 0;
646}
647
648static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
649{
650 struct user_struct *user = prog->aux->user;
651
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100652 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700653 free_uid(user);
654}
655
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700656static int bpf_prog_alloc_id(struct bpf_prog *prog)
657{
658 int id;
659
660 spin_lock_bh(&prog_idr_lock);
661 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
662 if (id > 0)
663 prog->aux->id = id;
664 spin_unlock_bh(&prog_idr_lock);
665
666 /* id is in [1, INT_MAX) */
667 if (WARN_ON_ONCE(!id))
668 return -ENOSPC;
669
670 return id > 0 ? 0 : id;
671}
672
673static void bpf_prog_free_id(struct bpf_prog *prog)
674{
675 /* cBPF to eBPF migrations are currently not in the idr store. */
676 if (!prog->aux->id)
677 return;
678
679 spin_lock_bh(&prog_idr_lock);
680 idr_remove(&prog_idr, prog->aux->id);
681 spin_unlock_bh(&prog_idr_lock);
682}
683
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200684static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700685{
686 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
687
688 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700689 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700690 bpf_prog_free(aux->prog);
691}
692
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700693void bpf_prog_put(struct bpf_prog *prog)
694{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100695 if (atomic_dec_and_test(&prog->aux->refcnt)) {
696 trace_bpf_prog_put_rcu(prog);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700697 bpf_prog_free_id(prog);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100698 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200699 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100700 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700701}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100702EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700703
704static int bpf_prog_release(struct inode *inode, struct file *filp)
705{
706 struct bpf_prog *prog = filp->private_data;
707
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200708 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700709 return 0;
710}
711
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100712#ifdef CONFIG_PROC_FS
713static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
714{
715 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100716 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100717
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100718 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100719 seq_printf(m,
720 "prog_type:\t%u\n"
721 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100722 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100723 "memlock:\t%llu\n",
724 prog->type,
725 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100726 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100727 prog->pages * 1ULL << PAGE_SHIFT);
728}
729#endif
730
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700731static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100732#ifdef CONFIG_PROC_FS
733 .show_fdinfo = bpf_prog_show_fdinfo,
734#endif
735 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700736};
737
Daniel Borkmannb2197752015-10-29 14:58:09 +0100738int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100739{
740 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
741 O_RDWR | O_CLOEXEC);
742}
743
Daniel Borkmann113214b2016-06-30 17:24:44 +0200744static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700745{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700746 if (!f.file)
747 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700748 if (f.file->f_op != &bpf_prog_fops) {
749 fdput(f);
750 return ERR_PTR(-EINVAL);
751 }
752
Daniel Borkmannc2101292015-10-29 14:58:07 +0100753 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700754}
755
Brenden Blanco59d36562016-07-19 12:16:46 -0700756struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700757{
Brenden Blanco59d36562016-07-19 12:16:46 -0700758 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
759 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700760 return ERR_PTR(-EBUSY);
761 }
762 return prog;
763}
Brenden Blanco59d36562016-07-19 12:16:46 -0700764EXPORT_SYMBOL_GPL(bpf_prog_add);
765
Daniel Borkmannc5405942016-11-09 22:02:34 +0100766void bpf_prog_sub(struct bpf_prog *prog, int i)
767{
768 /* Only to be used for undoing previous bpf_prog_add() in some
769 * error path. We still know that another entity in our call
770 * path holds a reference to the program, thus atomic_sub() can
771 * be safely used in such cases!
772 */
773 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
774}
775EXPORT_SYMBOL_GPL(bpf_prog_sub);
776
Brenden Blanco59d36562016-07-19 12:16:46 -0700777struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
778{
779 return bpf_prog_add(prog, 1);
780}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100781EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700782
Daniel Borkmann113214b2016-06-30 17:24:44 +0200783static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700784{
785 struct fd f = fdget(ufd);
786 struct bpf_prog *prog;
787
Daniel Borkmann113214b2016-06-30 17:24:44 +0200788 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700789 if (IS_ERR(prog))
790 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200791 if (type && prog->type != *type) {
792 prog = ERR_PTR(-EINVAL);
793 goto out;
794 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700795
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700796 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200797out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700798 fdput(f);
799 return prog;
800}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200801
802struct bpf_prog *bpf_prog_get(u32 ufd)
803{
804 return __bpf_prog_get(ufd, NULL);
805}
806
807struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
808{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100809 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
810
811 if (!IS_ERR(prog))
812 trace_bpf_prog_get_type(prog);
813 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200814}
815EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700816
817/* last field in 'union bpf_attr' used by this command */
David S. Millere07b98d2017-05-10 11:38:07 -0700818#define BPF_PROG_LOAD_LAST_FIELD prog_flags
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700819
820static int bpf_prog_load(union bpf_attr *attr)
821{
822 enum bpf_prog_type type = attr->prog_type;
823 struct bpf_prog *prog;
824 int err;
825 char license[128];
826 bool is_gpl;
827
828 if (CHECK_ATTR(BPF_PROG_LOAD))
829 return -EINVAL;
830
David S. Millere07b98d2017-05-10 11:38:07 -0700831 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
832 return -EINVAL;
833
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700834 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100835 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700836 sizeof(license) - 1) < 0)
837 return -EFAULT;
838 license[sizeof(license) - 1] = 0;
839
840 /* eBPF programs must be GPL compatible to use GPL-ed functions */
841 is_gpl = license_is_gpl_compatible(license);
842
Daniel Borkmannef0915c2016-12-07 01:15:44 +0100843 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
844 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700845
Alexei Starovoitov25415172015-03-25 12:49:20 -0700846 if (type == BPF_PROG_TYPE_KPROBE &&
847 attr->kern_version != LINUX_VERSION_CODE)
848 return -EINVAL;
849
Chenbo Feng80b7d812017-05-31 18:16:00 -0700850 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
851 type != BPF_PROG_TYPE_CGROUP_SKB &&
852 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700853 return -EPERM;
854
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700855 /* plain bpf_prog allocation */
856 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
857 if (!prog)
858 return -ENOMEM;
859
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700860 err = bpf_prog_charge_memlock(prog);
861 if (err)
862 goto free_prog_nouncharge;
863
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700864 prog->len = attr->insn_cnt;
865
866 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100867 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100868 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700869 goto free_prog;
870
871 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200872 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700873
874 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200875 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700876
877 /* find program type: socket_filter vs tracing_filter */
878 err = find_prog_type(type, prog);
879 if (err < 0)
880 goto free_prog;
881
882 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700883 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700884 if (err < 0)
885 goto free_used_maps;
886
887 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200888 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700889 if (err < 0)
890 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700891
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700892 err = bpf_prog_alloc_id(prog);
893 if (err)
894 goto free_used_maps;
895
Daniel Borkmannaa797812015-10-29 14:58:06 +0100896 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700897 if (err < 0)
898 /* failed to allocate fd */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700899 goto free_id;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700900
Daniel Borkmann74451e662017-02-16 22:24:50 +0100901 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100902 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700903 return err;
904
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700905free_id:
906 bpf_prog_free_id(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700907free_used_maps:
908 free_used_maps(prog->aux);
909free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700910 bpf_prog_uncharge_memlock(prog);
911free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700912 bpf_prog_free(prog);
913 return err;
914}
915
Daniel Borkmannb2197752015-10-29 14:58:09 +0100916#define BPF_OBJ_LAST_FIELD bpf_fd
917
918static int bpf_obj_pin(const union bpf_attr *attr)
919{
920 if (CHECK_ATTR(BPF_OBJ))
921 return -EINVAL;
922
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100923 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100924}
925
926static int bpf_obj_get(const union bpf_attr *attr)
927{
928 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
929 return -EINVAL;
930
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100931 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100932}
933
Daniel Mackf4324552016-11-23 16:52:27 +0100934#ifdef CONFIG_CGROUP_BPF
935
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800936#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
Daniel Mackf4324552016-11-23 16:52:27 +0100937
938static int bpf_prog_attach(const union bpf_attr *attr)
939{
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800940 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +0100941 struct bpf_prog *prog;
942 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800943 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100944
945 if (!capable(CAP_NET_ADMIN))
946 return -EPERM;
947
948 if (CHECK_ATTR(BPF_PROG_ATTACH))
949 return -EINVAL;
950
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800951 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
952 return -EINVAL;
953
Daniel Mackf4324552016-11-23 16:52:27 +0100954 switch (attr->attach_type) {
955 case BPF_CGROUP_INET_INGRESS:
956 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -0800957 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +0100958 break;
David Ahern610236582016-12-01 08:48:04 -0800959 case BPF_CGROUP_INET_SOCK_CREATE:
960 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
961 break;
Daniel Mackf4324552016-11-23 16:52:27 +0100962 default:
963 return -EINVAL;
964 }
965
David Ahernb2cd1252016-12-01 08:48:03 -0800966 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
967 if (IS_ERR(prog))
968 return PTR_ERR(prog);
969
970 cgrp = cgroup_get_from_fd(attr->target_fd);
971 if (IS_ERR(cgrp)) {
972 bpf_prog_put(prog);
973 return PTR_ERR(cgrp);
974 }
975
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800976 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
977 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
978 if (ret)
979 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -0800980 cgroup_put(cgrp);
981
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800982 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100983}
984
985#define BPF_PROG_DETACH_LAST_FIELD attach_type
986
987static int bpf_prog_detach(const union bpf_attr *attr)
988{
989 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800990 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100991
992 if (!capable(CAP_NET_ADMIN))
993 return -EPERM;
994
995 if (CHECK_ATTR(BPF_PROG_DETACH))
996 return -EINVAL;
997
998 switch (attr->attach_type) {
999 case BPF_CGROUP_INET_INGRESS:
1000 case BPF_CGROUP_INET_EGRESS:
David Ahern610236582016-12-01 08:48:04 -08001001 case BPF_CGROUP_INET_SOCK_CREATE:
Daniel Mackf4324552016-11-23 16:52:27 +01001002 cgrp = cgroup_get_from_fd(attr->target_fd);
1003 if (IS_ERR(cgrp))
1004 return PTR_ERR(cgrp);
1005
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001006 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001007 cgroup_put(cgrp);
1008 break;
1009
1010 default:
1011 return -EINVAL;
1012 }
1013
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001014 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001015}
1016#endif /* CONFIG_CGROUP_BPF */
1017
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001018#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1019
1020static int bpf_prog_test_run(const union bpf_attr *attr,
1021 union bpf_attr __user *uattr)
1022{
1023 struct bpf_prog *prog;
1024 int ret = -ENOTSUPP;
1025
1026 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1027 return -EINVAL;
1028
1029 prog = bpf_prog_get(attr->test.prog_fd);
1030 if (IS_ERR(prog))
1031 return PTR_ERR(prog);
1032
1033 if (prog->aux->ops->test_run)
1034 ret = prog->aux->ops->test_run(prog, attr, uattr);
1035
1036 bpf_prog_put(prog);
1037 return ret;
1038}
1039
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001040SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1041{
1042 union bpf_attr attr = {};
1043 int err;
1044
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001045 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001046 return -EPERM;
1047
1048 if (!access_ok(VERIFY_READ, uattr, 1))
1049 return -EFAULT;
1050
1051 if (size > PAGE_SIZE) /* silly large */
1052 return -E2BIG;
1053
1054 /* If we're handed a bigger struct than we know of,
1055 * ensure all the unknown bits are 0 - i.e. new
1056 * user-space does not rely on any kernel feature
1057 * extensions we dont know about yet.
1058 */
1059 if (size > sizeof(attr)) {
1060 unsigned char __user *addr;
1061 unsigned char __user *end;
1062 unsigned char val;
1063
1064 addr = (void __user *)uattr + sizeof(attr);
1065 end = (void __user *)uattr + size;
1066
1067 for (; addr < end; addr++) {
1068 err = get_user(val, addr);
1069 if (err)
1070 return err;
1071 if (val)
1072 return -E2BIG;
1073 }
1074 size = sizeof(attr);
1075 }
1076
1077 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1078 if (copy_from_user(&attr, uattr, size) != 0)
1079 return -EFAULT;
1080
1081 switch (cmd) {
1082 case BPF_MAP_CREATE:
1083 err = map_create(&attr);
1084 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001085 case BPF_MAP_LOOKUP_ELEM:
1086 err = map_lookup_elem(&attr);
1087 break;
1088 case BPF_MAP_UPDATE_ELEM:
1089 err = map_update_elem(&attr);
1090 break;
1091 case BPF_MAP_DELETE_ELEM:
1092 err = map_delete_elem(&attr);
1093 break;
1094 case BPF_MAP_GET_NEXT_KEY:
1095 err = map_get_next_key(&attr);
1096 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001097 case BPF_PROG_LOAD:
1098 err = bpf_prog_load(&attr);
1099 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001100 case BPF_OBJ_PIN:
1101 err = bpf_obj_pin(&attr);
1102 break;
1103 case BPF_OBJ_GET:
1104 err = bpf_obj_get(&attr);
1105 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001106#ifdef CONFIG_CGROUP_BPF
1107 case BPF_PROG_ATTACH:
1108 err = bpf_prog_attach(&attr);
1109 break;
1110 case BPF_PROG_DETACH:
1111 err = bpf_prog_detach(&attr);
1112 break;
1113#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001114 case BPF_PROG_TEST_RUN:
1115 err = bpf_prog_test_run(&attr, uattr);
1116 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001117 default:
1118 err = -EINVAL;
1119 break;
1120 }
1121
1122 return err;
1123}