blob: d4d47de75bba09f4e82e08214ac5fde04c8399b2 [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
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070027#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
33
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080034DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070035static DEFINE_IDR(prog_idr);
36static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070037static DEFINE_IDR(map_idr);
38static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080039
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070040int sysctl_unprivileged_bpf_disabled __read_mostly;
41
Johannes Berg40077e02017-04-11 15:34:58 +020042static const struct bpf_map_ops * const bpf_map_types[] = {
43#define BPF_PROG_TYPE(_id, _ops)
44#define BPF_MAP_TYPE(_id, _ops) \
45 [_id] = &_ops,
46#include <linux/bpf_types.h>
47#undef BPF_PROG_TYPE
48#undef BPF_MAP_TYPE
49};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070050
51static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
52{
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070053 struct bpf_map *map;
54
Johannes Berg40077e02017-04-11 15:34:58 +020055 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
56 !bpf_map_types[attr->map_type])
57 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070058
Johannes Berg40077e02017-04-11 15:34:58 +020059 map = bpf_map_types[attr->map_type]->map_alloc(attr);
60 if (IS_ERR(map))
61 return map;
62 map->ops = bpf_map_types[attr->map_type];
63 map->map_type = attr->map_type;
64 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070065}
66
Daniel Borkmannd407bd22017-01-18 15:14:17 +010067void *bpf_map_area_alloc(size_t size)
68{
69 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
70 * trigger under memory pressure as we really just want to
71 * fail instead.
72 */
73 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
74 void *area;
75
76 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
77 area = kmalloc(size, GFP_USER | flags);
78 if (area != NULL)
79 return area;
80 }
81
Michal Hocko19809c22017-05-08 15:57:44 -070082 return __vmalloc(size, GFP_KERNEL | flags, PAGE_KERNEL);
Daniel Borkmannd407bd22017-01-18 15:14:17 +010083}
84
85void bpf_map_area_free(void *area)
86{
87 kvfree(area);
88}
89
Alexei Starovoitov6c905982016-03-07 21:57:15 -080090int bpf_map_precharge_memlock(u32 pages)
91{
92 struct user_struct *user = get_current_user();
93 unsigned long memlock_limit, cur;
94
95 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
96 cur = atomic_long_read(&user->locked_vm);
97 free_uid(user);
98 if (cur + pages > memlock_limit)
99 return -EPERM;
100 return 0;
101}
102
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700103static int bpf_map_charge_memlock(struct bpf_map *map)
104{
105 struct user_struct *user = get_current_user();
106 unsigned long memlock_limit;
107
108 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
109
110 atomic_long_add(map->pages, &user->locked_vm);
111
112 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
113 atomic_long_sub(map->pages, &user->locked_vm);
114 free_uid(user);
115 return -EPERM;
116 }
117 map->user = user;
118 return 0;
119}
120
121static void bpf_map_uncharge_memlock(struct bpf_map *map)
122{
123 struct user_struct *user = map->user;
124
125 atomic_long_sub(map->pages, &user->locked_vm);
126 free_uid(user);
127}
128
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700129static int bpf_map_alloc_id(struct bpf_map *map)
130{
131 int id;
132
133 spin_lock_bh(&map_idr_lock);
134 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
135 if (id > 0)
136 map->id = id;
137 spin_unlock_bh(&map_idr_lock);
138
139 if (WARN_ON_ONCE(!id))
140 return -ENOSPC;
141
142 return id > 0 ? 0 : id;
143}
144
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700145static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700146{
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700147 if (do_idr_lock)
148 spin_lock_bh(&map_idr_lock);
149 else
150 __acquire(&map_idr_lock);
151
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700152 idr_remove(&map_idr, map->id);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700153
154 if (do_idr_lock)
155 spin_unlock_bh(&map_idr_lock);
156 else
157 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700158}
159
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700160/* called from workqueue */
161static void bpf_map_free_deferred(struct work_struct *work)
162{
163 struct bpf_map *map = container_of(work, struct bpf_map, work);
164
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700165 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700166 /* implementation dependent freeing */
167 map->ops->map_free(map);
168}
169
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100170static void bpf_map_put_uref(struct bpf_map *map)
171{
172 if (atomic_dec_and_test(&map->usercnt)) {
173 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
174 bpf_fd_array_map_clear(map);
175 }
176}
177
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700178/* decrement map refcnt and schedule it for freeing via workqueue
179 * (unrelying map implementation ops->map_free() might sleep)
180 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700181static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700182{
183 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700184 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700185 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700186 INIT_WORK(&map->work, bpf_map_free_deferred);
187 schedule_work(&map->work);
188 }
189}
190
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700191void bpf_map_put(struct bpf_map *map)
192{
193 __bpf_map_put(map, true);
194}
195
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100196void bpf_map_put_with_uref(struct bpf_map *map)
197{
198 bpf_map_put_uref(map);
199 bpf_map_put(map);
200}
201
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700202static int bpf_map_release(struct inode *inode, struct file *filp)
203{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200204 struct bpf_map *map = filp->private_data;
205
206 if (map->ops->map_release)
207 map->ops->map_release(map, filp);
208
209 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700210 return 0;
211}
212
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100213#ifdef CONFIG_PROC_FS
214static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
215{
216 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100217 const struct bpf_array *array;
218 u32 owner_prog_type = 0;
219
220 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
221 array = container_of(map, struct bpf_array, map);
222 owner_prog_type = array->owner_prog_type;
223 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100224
225 seq_printf(m,
226 "map_type:\t%u\n"
227 "key_size:\t%u\n"
228 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100229 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100230 "map_flags:\t%#x\n"
231 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100232 map->map_type,
233 map->key_size,
234 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100235 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100236 map->map_flags,
237 map->pages * 1ULL << PAGE_SHIFT);
238
239 if (owner_prog_type)
240 seq_printf(m, "owner_prog_type:\t%u\n",
241 owner_prog_type);
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100242}
243#endif
244
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700245static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100246#ifdef CONFIG_PROC_FS
247 .show_fdinfo = bpf_map_show_fdinfo,
248#endif
249 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700250};
251
Daniel Borkmannb2197752015-10-29 14:58:09 +0100252int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100253{
254 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
255 O_RDWR | O_CLOEXEC);
256}
257
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700258/* helper macro to check that unused fields 'union bpf_attr' are zero */
259#define CHECK_ATTR(CMD) \
260 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
261 sizeof(attr->CMD##_LAST_FIELD), 0, \
262 sizeof(*attr) - \
263 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
264 sizeof(attr->CMD##_LAST_FIELD)) != NULL
265
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700266#define BPF_MAP_CREATE_LAST_FIELD inner_map_fd
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700267/* called via syscall */
268static int map_create(union bpf_attr *attr)
269{
270 struct bpf_map *map;
271 int err;
272
273 err = CHECK_ATTR(BPF_MAP_CREATE);
274 if (err)
275 return -EINVAL;
276
277 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
278 map = find_and_alloc_map(attr);
279 if (IS_ERR(map))
280 return PTR_ERR(map);
281
282 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100283 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700284
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700285 err = bpf_map_charge_memlock(map);
286 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100287 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700288
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700289 err = bpf_map_alloc_id(map);
290 if (err)
291 goto free_map;
292
Daniel Borkmannaa797812015-10-29 14:58:06 +0100293 err = bpf_map_new_fd(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700294 if (err < 0) {
295 /* failed to allocate fd.
296 * bpf_map_put() is needed because the above
297 * bpf_map_alloc_id() has published the map
298 * to the userspace and the userspace may
299 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
300 */
301 bpf_map_put(map);
302 return err;
303 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700304
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100305 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700306 return err;
307
308free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100309 bpf_map_uncharge_memlock(map);
310free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700311 map->ops->map_free(map);
312 return err;
313}
314
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700315/* if error is returned, fd is released.
316 * On success caller should complete fd access with matching fdput()
317 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100318struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700319{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700320 if (!f.file)
321 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700322 if (f.file->f_op != &bpf_map_fops) {
323 fdput(f);
324 return ERR_PTR(-EINVAL);
325 }
326
Daniel Borkmannc2101292015-10-29 14:58:07 +0100327 return f.file->private_data;
328}
329
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700330/* prog's and map's refcnt limit */
331#define BPF_MAX_REFCNT 32768
332
333struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100334{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700335 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
336 atomic_dec(&map->refcnt);
337 return ERR_PTR(-EBUSY);
338 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100339 if (uref)
340 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700341 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100342}
343
344struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100345{
346 struct fd f = fdget(ufd);
347 struct bpf_map *map;
348
349 map = __bpf_map_get(f);
350 if (IS_ERR(map))
351 return map;
352
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700353 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100354 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700355
356 return map;
357}
358
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700359/* map_idr_lock should have been held */
360static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
361 bool uref)
362{
363 int refold;
364
365 refold = __atomic_add_unless(&map->refcnt, 1, 0);
366
367 if (refold >= BPF_MAX_REFCNT) {
368 __bpf_map_put(map, false);
369 return ERR_PTR(-EBUSY);
370 }
371
372 if (!refold)
373 return ERR_PTR(-ENOENT);
374
375 if (uref)
376 atomic_inc(&map->usercnt);
377
378 return map;
379}
380
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800381int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
382{
383 return -ENOTSUPP;
384}
385
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700386/* last field in 'union bpf_attr' used by this command */
387#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
388
389static int map_lookup_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;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800395 void *key, *value, *ptr;
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_LOOKUP_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();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700421 else if (IS_FD_MAP(map))
422 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800423 else
424 value_size = map->value_size;
425
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800426 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800427 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700428 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800429 goto free_key;
430
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800431 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
432 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800433 err = bpf_percpu_hash_copy(map, key, value);
434 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
435 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800436 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
437 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700438 } else if (IS_FD_ARRAY(map)) {
439 err = bpf_fd_array_map_lookup_elem(map, key, value);
440 } else if (IS_FD_HASH(map)) {
441 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800442 } else {
443 rcu_read_lock();
444 ptr = map->ops->map_lookup_elem(map, key);
445 if (ptr)
446 memcpy(value, ptr, value_size);
447 rcu_read_unlock();
448 err = ptr ? 0 : -ENOENT;
449 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800450
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800451 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800452 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700453
454 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800455 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800456 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700457
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100458 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700459 err = 0;
460
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800461free_value:
462 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700463free_key:
464 kfree(key);
465err_put:
466 fdput(f);
467 return err;
468}
469
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800470#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700471
472static int map_update_elem(union bpf_attr *attr)
473{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100474 void __user *ukey = u64_to_user_ptr(attr->key);
475 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700476 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700477 struct bpf_map *map;
478 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800479 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200480 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700481 int err;
482
483 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
484 return -EINVAL;
485
Daniel Borkmann592867b2015-09-08 18:00:09 +0200486 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100487 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700488 if (IS_ERR(map))
489 return PTR_ERR(map);
490
491 err = -ENOMEM;
492 key = kmalloc(map->key_size, GFP_USER);
493 if (!key)
494 goto err_put;
495
496 err = -EFAULT;
497 if (copy_from_user(key, ukey, map->key_size) != 0)
498 goto free_key;
499
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800500 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800501 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800502 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
503 value_size = round_up(map->value_size, 8) * num_possible_cpus();
504 else
505 value_size = map->value_size;
506
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700507 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800508 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700509 if (!value)
510 goto free_key;
511
512 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800513 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700514 goto free_value;
515
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800516 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
517 * inside bpf map update or delete otherwise deadlocks are possible
518 */
519 preempt_disable();
520 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800521 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
522 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800523 err = bpf_percpu_hash_update(map, key, value, attr->flags);
524 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
525 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200526 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700527 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700528 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
529 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200530 rcu_read_lock();
531 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
532 attr->flags);
533 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700534 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
535 rcu_read_lock();
536 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
537 attr->flags);
538 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800539 } else {
540 rcu_read_lock();
541 err = map->ops->map_update_elem(map, key, value, attr->flags);
542 rcu_read_unlock();
543 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800544 __this_cpu_dec(bpf_prog_active);
545 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700546
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100547 if (!err)
548 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700549free_value:
550 kfree(value);
551free_key:
552 kfree(key);
553err_put:
554 fdput(f);
555 return err;
556}
557
558#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
559
560static int map_delete_elem(union bpf_attr *attr)
561{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100562 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700563 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700564 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200565 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700566 void *key;
567 int err;
568
569 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
570 return -EINVAL;
571
Daniel Borkmann592867b2015-09-08 18:00:09 +0200572 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100573 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700574 if (IS_ERR(map))
575 return PTR_ERR(map);
576
577 err = -ENOMEM;
578 key = kmalloc(map->key_size, GFP_USER);
579 if (!key)
580 goto err_put;
581
582 err = -EFAULT;
583 if (copy_from_user(key, ukey, map->key_size) != 0)
584 goto free_key;
585
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800586 preempt_disable();
587 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700588 rcu_read_lock();
589 err = map->ops->map_delete_elem(map, key);
590 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800591 __this_cpu_dec(bpf_prog_active);
592 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700593
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100594 if (!err)
595 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700596free_key:
597 kfree(key);
598err_put:
599 fdput(f);
600 return err;
601}
602
603/* last field in 'union bpf_attr' used by this command */
604#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
605
606static int map_get_next_key(union bpf_attr *attr)
607{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100608 void __user *ukey = u64_to_user_ptr(attr->key);
609 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700610 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700611 struct bpf_map *map;
612 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200613 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700614 int err;
615
616 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
617 return -EINVAL;
618
Daniel Borkmann592867b2015-09-08 18:00:09 +0200619 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100620 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700621 if (IS_ERR(map))
622 return PTR_ERR(map);
623
Teng Qin8fe45922017-04-24 19:00:37 -0700624 if (ukey) {
625 err = -ENOMEM;
626 key = kmalloc(map->key_size, GFP_USER);
627 if (!key)
628 goto err_put;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700629
Teng Qin8fe45922017-04-24 19:00:37 -0700630 err = -EFAULT;
631 if (copy_from_user(key, ukey, map->key_size) != 0)
632 goto free_key;
633 } else {
634 key = NULL;
635 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700636
637 err = -ENOMEM;
638 next_key = kmalloc(map->key_size, GFP_USER);
639 if (!next_key)
640 goto free_key;
641
642 rcu_read_lock();
643 err = map->ops->map_get_next_key(map, key, next_key);
644 rcu_read_unlock();
645 if (err)
646 goto free_next_key;
647
648 err = -EFAULT;
649 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
650 goto free_next_key;
651
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100652 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700653 err = 0;
654
655free_next_key:
656 kfree(next_key);
657free_key:
658 kfree(key);
659err_put:
660 fdput(f);
661 return err;
662}
663
Johannes Bergbe9370a2017-04-11 15:34:57 +0200664static const struct bpf_verifier_ops * const bpf_prog_types[] = {
665#define BPF_PROG_TYPE(_id, _ops) \
666 [_id] = &_ops,
Johannes Berg40077e02017-04-11 15:34:58 +0200667#define BPF_MAP_TYPE(_id, _ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +0200668#include <linux/bpf_types.h>
669#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +0200670#undef BPF_MAP_TYPE
Johannes Bergbe9370a2017-04-11 15:34:57 +0200671};
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700672
673static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
674{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200675 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
676 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700677
Johannes Bergbe9370a2017-04-11 15:34:57 +0200678 prog->aux->ops = bpf_prog_types[type];
679 prog->type = type;
680 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700681}
682
683/* drop refcnt on maps used by eBPF program and free auxilary data */
684static void free_used_maps(struct bpf_prog_aux *aux)
685{
686 int i;
687
688 for (i = 0; i < aux->used_map_cnt; i++)
689 bpf_map_put(aux->used_maps[i]);
690
691 kfree(aux->used_maps);
692}
693
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100694int __bpf_prog_charge(struct user_struct *user, u32 pages)
695{
696 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
697 unsigned long user_bufs;
698
699 if (user) {
700 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
701 if (user_bufs > memlock_limit) {
702 atomic_long_sub(pages, &user->locked_vm);
703 return -EPERM;
704 }
705 }
706
707 return 0;
708}
709
710void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
711{
712 if (user)
713 atomic_long_sub(pages, &user->locked_vm);
714}
715
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700716static int bpf_prog_charge_memlock(struct bpf_prog *prog)
717{
718 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100719 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700720
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100721 ret = __bpf_prog_charge(user, prog->pages);
722 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700723 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100724 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700725 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100726
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700727 prog->aux->user = user;
728 return 0;
729}
730
731static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
732{
733 struct user_struct *user = prog->aux->user;
734
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100735 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700736 free_uid(user);
737}
738
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700739static int bpf_prog_alloc_id(struct bpf_prog *prog)
740{
741 int id;
742
743 spin_lock_bh(&prog_idr_lock);
744 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
745 if (id > 0)
746 prog->aux->id = id;
747 spin_unlock_bh(&prog_idr_lock);
748
749 /* id is in [1, INT_MAX) */
750 if (WARN_ON_ONCE(!id))
751 return -ENOSPC;
752
753 return id > 0 ? 0 : id;
754}
755
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700756static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700757{
758 /* cBPF to eBPF migrations are currently not in the idr store. */
759 if (!prog->aux->id)
760 return;
761
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700762 if (do_idr_lock)
763 spin_lock_bh(&prog_idr_lock);
764 else
765 __acquire(&prog_idr_lock);
766
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700767 idr_remove(&prog_idr, prog->aux->id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700768
769 if (do_idr_lock)
770 spin_unlock_bh(&prog_idr_lock);
771 else
772 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700773}
774
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200775static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700776{
777 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
778
779 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700780 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700781 bpf_prog_free(aux->prog);
782}
783
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700784static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700785{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100786 if (atomic_dec_and_test(&prog->aux->refcnt)) {
787 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700788 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700789 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100790 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200791 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100792 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700793}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700794
795void bpf_prog_put(struct bpf_prog *prog)
796{
797 __bpf_prog_put(prog, true);
798}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100799EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700800
801static int bpf_prog_release(struct inode *inode, struct file *filp)
802{
803 struct bpf_prog *prog = filp->private_data;
804
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200805 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700806 return 0;
807}
808
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100809#ifdef CONFIG_PROC_FS
810static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
811{
812 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100813 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100814
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100815 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100816 seq_printf(m,
817 "prog_type:\t%u\n"
818 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100819 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100820 "memlock:\t%llu\n",
821 prog->type,
822 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100823 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100824 prog->pages * 1ULL << PAGE_SHIFT);
825}
826#endif
827
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700828static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100829#ifdef CONFIG_PROC_FS
830 .show_fdinfo = bpf_prog_show_fdinfo,
831#endif
832 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700833};
834
Daniel Borkmannb2197752015-10-29 14:58:09 +0100835int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100836{
837 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
838 O_RDWR | O_CLOEXEC);
839}
840
Daniel Borkmann113214b2016-06-30 17:24:44 +0200841static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700842{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700843 if (!f.file)
844 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700845 if (f.file->f_op != &bpf_prog_fops) {
846 fdput(f);
847 return ERR_PTR(-EINVAL);
848 }
849
Daniel Borkmannc2101292015-10-29 14:58:07 +0100850 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700851}
852
Brenden Blanco59d36562016-07-19 12:16:46 -0700853struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700854{
Brenden Blanco59d36562016-07-19 12:16:46 -0700855 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
856 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700857 return ERR_PTR(-EBUSY);
858 }
859 return prog;
860}
Brenden Blanco59d36562016-07-19 12:16:46 -0700861EXPORT_SYMBOL_GPL(bpf_prog_add);
862
Daniel Borkmannc5405942016-11-09 22:02:34 +0100863void bpf_prog_sub(struct bpf_prog *prog, int i)
864{
865 /* Only to be used for undoing previous bpf_prog_add() in some
866 * error path. We still know that another entity in our call
867 * path holds a reference to the program, thus atomic_sub() can
868 * be safely used in such cases!
869 */
870 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
871}
872EXPORT_SYMBOL_GPL(bpf_prog_sub);
873
Brenden Blanco59d36562016-07-19 12:16:46 -0700874struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
875{
876 return bpf_prog_add(prog, 1);
877}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100878EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700879
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700880/* prog_idr_lock should have been held */
881static struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
882{
883 int refold;
884
885 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
886
887 if (refold >= BPF_MAX_REFCNT) {
888 __bpf_prog_put(prog, false);
889 return ERR_PTR(-EBUSY);
890 }
891
892 if (!refold)
893 return ERR_PTR(-ENOENT);
894
895 return prog;
896}
897
Daniel Borkmann113214b2016-06-30 17:24:44 +0200898static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700899{
900 struct fd f = fdget(ufd);
901 struct bpf_prog *prog;
902
Daniel Borkmann113214b2016-06-30 17:24:44 +0200903 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700904 if (IS_ERR(prog))
905 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200906 if (type && prog->type != *type) {
907 prog = ERR_PTR(-EINVAL);
908 goto out;
909 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700910
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700911 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200912out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700913 fdput(f);
914 return prog;
915}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200916
917struct bpf_prog *bpf_prog_get(u32 ufd)
918{
919 return __bpf_prog_get(ufd, NULL);
920}
921
922struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
923{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100924 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
925
926 if (!IS_ERR(prog))
927 trace_bpf_prog_get_type(prog);
928 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200929}
930EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700931
932/* last field in 'union bpf_attr' used by this command */
David S. Millere07b98d2017-05-10 11:38:07 -0700933#define BPF_PROG_LOAD_LAST_FIELD prog_flags
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700934
935static int bpf_prog_load(union bpf_attr *attr)
936{
937 enum bpf_prog_type type = attr->prog_type;
938 struct bpf_prog *prog;
939 int err;
940 char license[128];
941 bool is_gpl;
942
943 if (CHECK_ATTR(BPF_PROG_LOAD))
944 return -EINVAL;
945
David S. Millere07b98d2017-05-10 11:38:07 -0700946 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
947 return -EINVAL;
948
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700949 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100950 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700951 sizeof(license) - 1) < 0)
952 return -EFAULT;
953 license[sizeof(license) - 1] = 0;
954
955 /* eBPF programs must be GPL compatible to use GPL-ed functions */
956 is_gpl = license_is_gpl_compatible(license);
957
Daniel Borkmannef0915c2016-12-07 01:15:44 +0100958 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
959 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700960
Alexei Starovoitov25415172015-03-25 12:49:20 -0700961 if (type == BPF_PROG_TYPE_KPROBE &&
962 attr->kern_version != LINUX_VERSION_CODE)
963 return -EINVAL;
964
Chenbo Feng80b7d812017-05-31 18:16:00 -0700965 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
966 type != BPF_PROG_TYPE_CGROUP_SKB &&
967 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700968 return -EPERM;
969
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700970 /* plain bpf_prog allocation */
971 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
972 if (!prog)
973 return -ENOMEM;
974
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700975 err = bpf_prog_charge_memlock(prog);
976 if (err)
977 goto free_prog_nouncharge;
978
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700979 prog->len = attr->insn_cnt;
980
981 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100982 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100983 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700984 goto free_prog;
985
986 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200987 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700988
989 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200990 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700991
992 /* find program type: socket_filter vs tracing_filter */
993 err = find_prog_type(type, prog);
994 if (err < 0)
995 goto free_prog;
996
997 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700998 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700999 if (err < 0)
1000 goto free_used_maps;
1001
1002 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001003 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001004 if (err < 0)
1005 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001006
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001007 err = bpf_prog_alloc_id(prog);
1008 if (err)
1009 goto free_used_maps;
1010
Daniel Borkmannaa797812015-10-29 14:58:06 +01001011 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001012 if (err < 0) {
1013 /* failed to allocate fd.
1014 * bpf_prog_put() is needed because the above
1015 * bpf_prog_alloc_id() has published the prog
1016 * to the userspace and the userspace may
1017 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1018 */
1019 bpf_prog_put(prog);
1020 return err;
1021 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001022
Daniel Borkmann74451e662017-02-16 22:24:50 +01001023 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001024 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001025 return err;
1026
1027free_used_maps:
1028 free_used_maps(prog->aux);
1029free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001030 bpf_prog_uncharge_memlock(prog);
1031free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001032 bpf_prog_free(prog);
1033 return err;
1034}
1035
Daniel Borkmannb2197752015-10-29 14:58:09 +01001036#define BPF_OBJ_LAST_FIELD bpf_fd
1037
1038static int bpf_obj_pin(const union bpf_attr *attr)
1039{
1040 if (CHECK_ATTR(BPF_OBJ))
1041 return -EINVAL;
1042
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001043 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001044}
1045
1046static int bpf_obj_get(const union bpf_attr *attr)
1047{
1048 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1049 return -EINVAL;
1050
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001051 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001052}
1053
Daniel Mackf4324552016-11-23 16:52:27 +01001054#ifdef CONFIG_CGROUP_BPF
1055
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001056#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
Daniel Mackf4324552016-11-23 16:52:27 +01001057
1058static int bpf_prog_attach(const union bpf_attr *attr)
1059{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001060 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001061 struct bpf_prog *prog;
1062 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001063 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001064
1065 if (!capable(CAP_NET_ADMIN))
1066 return -EPERM;
1067
1068 if (CHECK_ATTR(BPF_PROG_ATTACH))
1069 return -EINVAL;
1070
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001071 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1072 return -EINVAL;
1073
Daniel Mackf4324552016-11-23 16:52:27 +01001074 switch (attr->attach_type) {
1075 case BPF_CGROUP_INET_INGRESS:
1076 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001077 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001078 break;
David Ahern610236582016-12-01 08:48:04 -08001079 case BPF_CGROUP_INET_SOCK_CREATE:
1080 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1081 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001082 case BPF_CGROUP_SOCK_OPS:
1083 ptype = BPF_PROG_TYPE_SOCK_OPS;
1084 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001085 default:
1086 return -EINVAL;
1087 }
1088
David Ahernb2cd1252016-12-01 08:48:03 -08001089 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1090 if (IS_ERR(prog))
1091 return PTR_ERR(prog);
1092
1093 cgrp = cgroup_get_from_fd(attr->target_fd);
1094 if (IS_ERR(cgrp)) {
1095 bpf_prog_put(prog);
1096 return PTR_ERR(cgrp);
1097 }
1098
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001099 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1100 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1101 if (ret)
1102 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001103 cgroup_put(cgrp);
1104
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001105 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001106}
1107
1108#define BPF_PROG_DETACH_LAST_FIELD attach_type
1109
1110static int bpf_prog_detach(const union bpf_attr *attr)
1111{
1112 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001113 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001114
1115 if (!capable(CAP_NET_ADMIN))
1116 return -EPERM;
1117
1118 if (CHECK_ATTR(BPF_PROG_DETACH))
1119 return -EINVAL;
1120
1121 switch (attr->attach_type) {
1122 case BPF_CGROUP_INET_INGRESS:
1123 case BPF_CGROUP_INET_EGRESS:
David Ahern610236582016-12-01 08:48:04 -08001124 case BPF_CGROUP_INET_SOCK_CREATE:
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001125 case BPF_CGROUP_SOCK_OPS:
Daniel Mackf4324552016-11-23 16:52:27 +01001126 cgrp = cgroup_get_from_fd(attr->target_fd);
1127 if (IS_ERR(cgrp))
1128 return PTR_ERR(cgrp);
1129
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001130 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001131 cgroup_put(cgrp);
1132 break;
1133
1134 default:
1135 return -EINVAL;
1136 }
1137
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001138 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001139}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001140
Daniel Mackf4324552016-11-23 16:52:27 +01001141#endif /* CONFIG_CGROUP_BPF */
1142
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001143#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1144
1145static int bpf_prog_test_run(const union bpf_attr *attr,
1146 union bpf_attr __user *uattr)
1147{
1148 struct bpf_prog *prog;
1149 int ret = -ENOTSUPP;
1150
1151 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1152 return -EINVAL;
1153
1154 prog = bpf_prog_get(attr->test.prog_fd);
1155 if (IS_ERR(prog))
1156 return PTR_ERR(prog);
1157
1158 if (prog->aux->ops->test_run)
1159 ret = prog->aux->ops->test_run(prog, attr, uattr);
1160
1161 bpf_prog_put(prog);
1162 return ret;
1163}
1164
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001165#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1166
1167static int bpf_obj_get_next_id(const union bpf_attr *attr,
1168 union bpf_attr __user *uattr,
1169 struct idr *idr,
1170 spinlock_t *lock)
1171{
1172 u32 next_id = attr->start_id;
1173 int err = 0;
1174
1175 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1176 return -EINVAL;
1177
1178 if (!capable(CAP_SYS_ADMIN))
1179 return -EPERM;
1180
1181 next_id++;
1182 spin_lock_bh(lock);
1183 if (!idr_get_next(idr, &next_id))
1184 err = -ENOENT;
1185 spin_unlock_bh(lock);
1186
1187 if (!err)
1188 err = put_user(next_id, &uattr->next_id);
1189
1190 return err;
1191}
1192
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001193#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1194
1195static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1196{
1197 struct bpf_prog *prog;
1198 u32 id = attr->prog_id;
1199 int fd;
1200
1201 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1202 return -EINVAL;
1203
1204 if (!capable(CAP_SYS_ADMIN))
1205 return -EPERM;
1206
1207 spin_lock_bh(&prog_idr_lock);
1208 prog = idr_find(&prog_idr, id);
1209 if (prog)
1210 prog = bpf_prog_inc_not_zero(prog);
1211 else
1212 prog = ERR_PTR(-ENOENT);
1213 spin_unlock_bh(&prog_idr_lock);
1214
1215 if (IS_ERR(prog))
1216 return PTR_ERR(prog);
1217
1218 fd = bpf_prog_new_fd(prog);
1219 if (fd < 0)
1220 bpf_prog_put(prog);
1221
1222 return fd;
1223}
1224
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001225#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1226
1227static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1228{
1229 struct bpf_map *map;
1230 u32 id = attr->map_id;
1231 int fd;
1232
1233 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1234 return -EINVAL;
1235
1236 if (!capable(CAP_SYS_ADMIN))
1237 return -EPERM;
1238
1239 spin_lock_bh(&map_idr_lock);
1240 map = idr_find(&map_idr, id);
1241 if (map)
1242 map = bpf_map_inc_not_zero(map, true);
1243 else
1244 map = ERR_PTR(-ENOENT);
1245 spin_unlock_bh(&map_idr_lock);
1246
1247 if (IS_ERR(map))
1248 return PTR_ERR(map);
1249
1250 fd = bpf_map_new_fd(map);
1251 if (fd < 0)
1252 bpf_map_put(map);
1253
1254 return fd;
1255}
1256
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001257static int check_uarg_tail_zero(void __user *uaddr,
1258 size_t expected_size,
1259 size_t actual_size)
1260{
1261 unsigned char __user *addr;
1262 unsigned char __user *end;
1263 unsigned char val;
1264 int err;
1265
1266 if (actual_size <= expected_size)
1267 return 0;
1268
1269 addr = uaddr + expected_size;
1270 end = uaddr + actual_size;
1271
1272 for (; addr < end; addr++) {
1273 err = get_user(val, addr);
1274 if (err)
1275 return err;
1276 if (val)
1277 return -E2BIG;
1278 }
1279
1280 return 0;
1281}
1282
1283static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1284 const union bpf_attr *attr,
1285 union bpf_attr __user *uattr)
1286{
1287 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1288 struct bpf_prog_info info = {};
1289 u32 info_len = attr->info.info_len;
1290 char __user *uinsns;
1291 u32 ulen;
1292 int err;
1293
1294 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1295 if (err)
1296 return err;
1297 info_len = min_t(u32, sizeof(info), info_len);
1298
1299 if (copy_from_user(&info, uinfo, info_len))
1300 return err;
1301
1302 info.type = prog->type;
1303 info.id = prog->aux->id;
1304
1305 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1306
1307 if (!capable(CAP_SYS_ADMIN)) {
1308 info.jited_prog_len = 0;
1309 info.xlated_prog_len = 0;
1310 goto done;
1311 }
1312
1313 ulen = info.jited_prog_len;
1314 info.jited_prog_len = prog->jited_len;
1315 if (info.jited_prog_len && ulen) {
1316 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1317 ulen = min_t(u32, info.jited_prog_len, ulen);
1318 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1319 return -EFAULT;
1320 }
1321
1322 ulen = info.xlated_prog_len;
1323 info.xlated_prog_len = bpf_prog_size(prog->len);
1324 if (info.xlated_prog_len && ulen) {
1325 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1326 ulen = min_t(u32, info.xlated_prog_len, ulen);
1327 if (copy_to_user(uinsns, prog->insnsi, ulen))
1328 return -EFAULT;
1329 }
1330
1331done:
1332 if (copy_to_user(uinfo, &info, info_len) ||
1333 put_user(info_len, &uattr->info.info_len))
1334 return -EFAULT;
1335
1336 return 0;
1337}
1338
1339static int bpf_map_get_info_by_fd(struct bpf_map *map,
1340 const union bpf_attr *attr,
1341 union bpf_attr __user *uattr)
1342{
1343 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1344 struct bpf_map_info info = {};
1345 u32 info_len = attr->info.info_len;
1346 int err;
1347
1348 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1349 if (err)
1350 return err;
1351 info_len = min_t(u32, sizeof(info), info_len);
1352
1353 info.type = map->map_type;
1354 info.id = map->id;
1355 info.key_size = map->key_size;
1356 info.value_size = map->value_size;
1357 info.max_entries = map->max_entries;
1358 info.map_flags = map->map_flags;
1359
1360 if (copy_to_user(uinfo, &info, info_len) ||
1361 put_user(info_len, &uattr->info.info_len))
1362 return -EFAULT;
1363
1364 return 0;
1365}
1366
1367#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1368
1369static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1370 union bpf_attr __user *uattr)
1371{
1372 int ufd = attr->info.bpf_fd;
1373 struct fd f;
1374 int err;
1375
1376 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1377 return -EINVAL;
1378
1379 f = fdget(ufd);
1380 if (!f.file)
1381 return -EBADFD;
1382
1383 if (f.file->f_op == &bpf_prog_fops)
1384 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1385 uattr);
1386 else if (f.file->f_op == &bpf_map_fops)
1387 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1388 uattr);
1389 else
1390 err = -EINVAL;
1391
1392 fdput(f);
1393 return err;
1394}
1395
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001396SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1397{
1398 union bpf_attr attr = {};
1399 int err;
1400
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001401 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001402 return -EPERM;
1403
1404 if (!access_ok(VERIFY_READ, uattr, 1))
1405 return -EFAULT;
1406
1407 if (size > PAGE_SIZE) /* silly large */
1408 return -E2BIG;
1409
1410 /* If we're handed a bigger struct than we know of,
1411 * ensure all the unknown bits are 0 - i.e. new
1412 * user-space does not rely on any kernel feature
1413 * extensions we dont know about yet.
1414 */
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001415 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1416 if (err)
1417 return err;
1418 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001419
1420 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1421 if (copy_from_user(&attr, uattr, size) != 0)
1422 return -EFAULT;
1423
1424 switch (cmd) {
1425 case BPF_MAP_CREATE:
1426 err = map_create(&attr);
1427 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001428 case BPF_MAP_LOOKUP_ELEM:
1429 err = map_lookup_elem(&attr);
1430 break;
1431 case BPF_MAP_UPDATE_ELEM:
1432 err = map_update_elem(&attr);
1433 break;
1434 case BPF_MAP_DELETE_ELEM:
1435 err = map_delete_elem(&attr);
1436 break;
1437 case BPF_MAP_GET_NEXT_KEY:
1438 err = map_get_next_key(&attr);
1439 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001440 case BPF_PROG_LOAD:
1441 err = bpf_prog_load(&attr);
1442 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001443 case BPF_OBJ_PIN:
1444 err = bpf_obj_pin(&attr);
1445 break;
1446 case BPF_OBJ_GET:
1447 err = bpf_obj_get(&attr);
1448 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001449#ifdef CONFIG_CGROUP_BPF
1450 case BPF_PROG_ATTACH:
1451 err = bpf_prog_attach(&attr);
1452 break;
1453 case BPF_PROG_DETACH:
1454 err = bpf_prog_detach(&attr);
1455 break;
1456#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001457 case BPF_PROG_TEST_RUN:
1458 err = bpf_prog_test_run(&attr, uattr);
1459 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001460 case BPF_PROG_GET_NEXT_ID:
1461 err = bpf_obj_get_next_id(&attr, uattr,
1462 &prog_idr, &prog_idr_lock);
1463 break;
1464 case BPF_MAP_GET_NEXT_ID:
1465 err = bpf_obj_get_next_id(&attr, uattr,
1466 &map_idr, &map_idr_lock);
1467 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001468 case BPF_PROG_GET_FD_BY_ID:
1469 err = bpf_prog_get_fd_by_id(&attr);
1470 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001471 case BPF_MAP_GET_FD_BY_ID:
1472 err = bpf_map_get_fd_by_id(&attr);
1473 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001474 case BPF_OBJ_GET_INFO_BY_FD:
1475 err = bpf_obj_get_info_by_fd(&attr, uattr);
1476 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001477 default:
1478 err = -EINVAL;
1479 break;
1480 }
1481
1482 return err;
1483}