blob: 45970df3f8204e37ccb6b8ccd31a236f3dbf7b99 [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>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070026#include <linux/cred.h>
27#include <linux/timekeeping.h>
28#include <linux/ctype.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070029
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070030#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
34#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
35#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
36
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080037DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070038static DEFINE_IDR(prog_idr);
39static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070040static DEFINE_IDR(map_idr);
41static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080042
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070043int sysctl_unprivileged_bpf_disabled __read_mostly;
44
Johannes Berg40077e02017-04-11 15:34:58 +020045static const struct bpf_map_ops * const bpf_map_types[] = {
46#define BPF_PROG_TYPE(_id, _ops)
47#define BPF_MAP_TYPE(_id, _ops) \
48 [_id] = &_ops,
49#include <linux/bpf_types.h>
50#undef BPF_PROG_TYPE
51#undef BPF_MAP_TYPE
52};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070053
Mickaël Salaün752ba562017-08-07 20:45:20 +020054/*
55 * If we're handed a bigger struct than we know of, ensure all the unknown bits
56 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
57 * we don't know about yet.
58 *
59 * There is a ToCToU between this function call and the following
60 * copy_from_user() call. However, this is not a concern since this function is
61 * meant to be a future-proofing of bits.
62 */
Mickaël Salaün58291a72017-08-07 20:45:19 +020063static int check_uarg_tail_zero(void __user *uaddr,
64 size_t expected_size,
65 size_t actual_size)
66{
67 unsigned char __user *addr;
68 unsigned char __user *end;
69 unsigned char val;
70 int err;
71
Mickaël Salaün752ba562017-08-07 20:45:20 +020072 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
73 return -E2BIG;
74
75 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
76 return -EFAULT;
77
Mickaël Salaün58291a72017-08-07 20:45:19 +020078 if (actual_size <= expected_size)
79 return 0;
80
81 addr = uaddr + expected_size;
82 end = uaddr + actual_size;
83
84 for (; addr < end; addr++) {
85 err = get_user(val, addr);
86 if (err)
87 return err;
88 if (val)
89 return -E2BIG;
90 }
91
92 return 0;
93}
94
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070095static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
96{
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070097 struct bpf_map *map;
98
Johannes Berg40077e02017-04-11 15:34:58 +020099 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
100 !bpf_map_types[attr->map_type])
101 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700102
Johannes Berg40077e02017-04-11 15:34:58 +0200103 map = bpf_map_types[attr->map_type]->map_alloc(attr);
104 if (IS_ERR(map))
105 return map;
106 map->ops = bpf_map_types[attr->map_type];
107 map->map_type = attr->map_type;
108 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700109}
110
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700111void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100112{
113 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
114 * trigger under memory pressure as we really just want to
115 * fail instead.
116 */
117 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
118 void *area;
119
120 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700121 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100122 if (area != NULL)
123 return area;
124 }
125
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700126 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
127 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100128}
129
130void bpf_map_area_free(void *area)
131{
132 kvfree(area);
133}
134
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800135int bpf_map_precharge_memlock(u32 pages)
136{
137 struct user_struct *user = get_current_user();
138 unsigned long memlock_limit, cur;
139
140 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
141 cur = atomic_long_read(&user->locked_vm);
142 free_uid(user);
143 if (cur + pages > memlock_limit)
144 return -EPERM;
145 return 0;
146}
147
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700148static int bpf_map_charge_memlock(struct bpf_map *map)
149{
150 struct user_struct *user = get_current_user();
151 unsigned long memlock_limit;
152
153 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
154
155 atomic_long_add(map->pages, &user->locked_vm);
156
157 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
158 atomic_long_sub(map->pages, &user->locked_vm);
159 free_uid(user);
160 return -EPERM;
161 }
162 map->user = user;
163 return 0;
164}
165
166static void bpf_map_uncharge_memlock(struct bpf_map *map)
167{
168 struct user_struct *user = map->user;
169
170 atomic_long_sub(map->pages, &user->locked_vm);
171 free_uid(user);
172}
173
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700174static int bpf_map_alloc_id(struct bpf_map *map)
175{
176 int id;
177
178 spin_lock_bh(&map_idr_lock);
179 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
180 if (id > 0)
181 map->id = id;
182 spin_unlock_bh(&map_idr_lock);
183
184 if (WARN_ON_ONCE(!id))
185 return -ENOSPC;
186
187 return id > 0 ? 0 : id;
188}
189
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700190static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700191{
Eric Dumazet930651a2017-09-19 09:15:59 -0700192 unsigned long flags;
193
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700194 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700195 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700196 else
197 __acquire(&map_idr_lock);
198
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700199 idr_remove(&map_idr, map->id);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700200
201 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700202 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700203 else
204 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700205}
206
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700207/* called from workqueue */
208static void bpf_map_free_deferred(struct work_struct *work)
209{
210 struct bpf_map *map = container_of(work, struct bpf_map, work);
211
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700212 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700213 /* implementation dependent freeing */
214 map->ops->map_free(map);
215}
216
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100217static void bpf_map_put_uref(struct bpf_map *map)
218{
219 if (atomic_dec_and_test(&map->usercnt)) {
220 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
221 bpf_fd_array_map_clear(map);
222 }
223}
224
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700225/* decrement map refcnt and schedule it for freeing via workqueue
226 * (unrelying map implementation ops->map_free() might sleep)
227 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700228static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700229{
230 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700231 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700232 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700233 INIT_WORK(&map->work, bpf_map_free_deferred);
234 schedule_work(&map->work);
235 }
236}
237
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700238void bpf_map_put(struct bpf_map *map)
239{
240 __bpf_map_put(map, true);
241}
242
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100243void bpf_map_put_with_uref(struct bpf_map *map)
244{
245 bpf_map_put_uref(map);
246 bpf_map_put(map);
247}
248
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700249static int bpf_map_release(struct inode *inode, struct file *filp)
250{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200251 struct bpf_map *map = filp->private_data;
252
253 if (map->ops->map_release)
254 map->ops->map_release(map, filp);
255
256 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700257 return 0;
258}
259
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100260#ifdef CONFIG_PROC_FS
261static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
262{
263 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100264 const struct bpf_array *array;
265 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200266 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100267
268 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
269 array = container_of(map, struct bpf_array, map);
270 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200271 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100272 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100273
274 seq_printf(m,
275 "map_type:\t%u\n"
276 "key_size:\t%u\n"
277 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100278 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100279 "map_flags:\t%#x\n"
280 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100281 map->map_type,
282 map->key_size,
283 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100284 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100285 map->map_flags,
286 map->pages * 1ULL << PAGE_SHIFT);
287
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200288 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100289 seq_printf(m, "owner_prog_type:\t%u\n",
290 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200291 seq_printf(m, "owner_jited:\t%u\n",
292 owner_jited);
293 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100294}
295#endif
296
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700297static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100298#ifdef CONFIG_PROC_FS
299 .show_fdinfo = bpf_map_show_fdinfo,
300#endif
301 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700302};
303
Daniel Borkmannb2197752015-10-29 14:58:09 +0100304int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100305{
306 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
307 O_RDWR | O_CLOEXEC);
308}
309
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700310/* helper macro to check that unused fields 'union bpf_attr' are zero */
311#define CHECK_ATTR(CMD) \
312 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
313 sizeof(attr->CMD##_LAST_FIELD), 0, \
314 sizeof(*attr) - \
315 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
316 sizeof(attr->CMD##_LAST_FIELD)) != NULL
317
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700318/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
319 * Return 0 on success and < 0 on error.
320 */
321static int bpf_obj_name_cpy(char *dst, const char *src)
322{
323 const char *end = src + BPF_OBJ_NAME_LEN;
324
325 /* Copy all isalnum() and '_' char */
326 while (src < end && *src) {
327 if (!isalnum(*src) && *src != '_')
328 return -EINVAL;
329 *dst++ = *src++;
330 }
331
332 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
333 if (src == end)
334 return -EINVAL;
335
336 /* '\0' terminates dst */
337 *dst = 0;
338
339 return 0;
340}
341
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700342#define BPF_MAP_CREATE_LAST_FIELD numa_node
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700343/* called via syscall */
344static int map_create(union bpf_attr *attr)
345{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700346 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700347 struct bpf_map *map;
348 int err;
349
350 err = CHECK_ATTR(BPF_MAP_CREATE);
351 if (err)
352 return -EINVAL;
353
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700354 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700355 ((unsigned int)numa_node >= nr_node_ids ||
356 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700357 return -EINVAL;
358
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700359 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
360 map = find_and_alloc_map(attr);
361 if (IS_ERR(map))
362 return PTR_ERR(map);
363
364 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100365 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700366
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700367 err = bpf_map_charge_memlock(map);
368 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100369 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700370
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700371 err = bpf_map_alloc_id(map);
372 if (err)
373 goto free_map;
374
Daniel Borkmannaa797812015-10-29 14:58:06 +0100375 err = bpf_map_new_fd(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700376 if (err < 0) {
377 /* failed to allocate fd.
378 * bpf_map_put() is needed because the above
379 * bpf_map_alloc_id() has published the map
380 * to the userspace and the userspace may
381 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
382 */
383 bpf_map_put(map);
384 return err;
385 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700386
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100387 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700388 return err;
389
390free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100391 bpf_map_uncharge_memlock(map);
392free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700393 map->ops->map_free(map);
394 return err;
395}
396
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700397/* if error is returned, fd is released.
398 * On success caller should complete fd access with matching fdput()
399 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100400struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700401{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700402 if (!f.file)
403 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700404 if (f.file->f_op != &bpf_map_fops) {
405 fdput(f);
406 return ERR_PTR(-EINVAL);
407 }
408
Daniel Borkmannc2101292015-10-29 14:58:07 +0100409 return f.file->private_data;
410}
411
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700412/* prog's and map's refcnt limit */
413#define BPF_MAX_REFCNT 32768
414
415struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100416{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700417 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
418 atomic_dec(&map->refcnt);
419 return ERR_PTR(-EBUSY);
420 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100421 if (uref)
422 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700423 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100424}
425
426struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100427{
428 struct fd f = fdget(ufd);
429 struct bpf_map *map;
430
431 map = __bpf_map_get(f);
432 if (IS_ERR(map))
433 return map;
434
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700435 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100436 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700437
438 return map;
439}
440
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700441/* map_idr_lock should have been held */
442static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
443 bool uref)
444{
445 int refold;
446
447 refold = __atomic_add_unless(&map->refcnt, 1, 0);
448
449 if (refold >= BPF_MAX_REFCNT) {
450 __bpf_map_put(map, false);
451 return ERR_PTR(-EBUSY);
452 }
453
454 if (!refold)
455 return ERR_PTR(-ENOENT);
456
457 if (uref)
458 atomic_inc(&map->usercnt);
459
460 return map;
461}
462
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800463int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
464{
465 return -ENOTSUPP;
466}
467
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700468/* last field in 'union bpf_attr' used by this command */
469#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
470
471static int map_lookup_elem(union bpf_attr *attr)
472{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100473 void __user *ukey = u64_to_user_ptr(attr->key);
474 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700475 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700476 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800477 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800478 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200479 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700480 int err;
481
482 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
483 return -EINVAL;
484
Daniel Borkmann592867b2015-09-08 18:00:09 +0200485 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100486 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700487 if (IS_ERR(map))
488 return PTR_ERR(map);
489
Al Viroe4448ed2017-05-13 18:43:00 -0400490 key = memdup_user(ukey, map->key_size);
491 if (IS_ERR(key)) {
492 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700493 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400494 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700495
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800496 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800497 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800498 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
499 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700500 else if (IS_FD_MAP(map))
501 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800502 else
503 value_size = map->value_size;
504
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800505 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800506 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700507 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800508 goto free_key;
509
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800510 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
511 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800512 err = bpf_percpu_hash_copy(map, key, value);
513 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
514 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800515 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
516 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700517 } else if (IS_FD_ARRAY(map)) {
518 err = bpf_fd_array_map_lookup_elem(map, key, value);
519 } else if (IS_FD_HASH(map)) {
520 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800521 } else {
522 rcu_read_lock();
523 ptr = map->ops->map_lookup_elem(map, key);
524 if (ptr)
525 memcpy(value, ptr, value_size);
526 rcu_read_unlock();
527 err = ptr ? 0 : -ENOENT;
528 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800529
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800530 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800531 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700532
533 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800534 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800535 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700536
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100537 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700538 err = 0;
539
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800540free_value:
541 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700542free_key:
543 kfree(key);
544err_put:
545 fdput(f);
546 return err;
547}
548
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800549#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700550
551static int map_update_elem(union bpf_attr *attr)
552{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100553 void __user *ukey = u64_to_user_ptr(attr->key);
554 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700555 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700556 struct bpf_map *map;
557 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800558 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200559 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700560 int err;
561
562 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
563 return -EINVAL;
564
Daniel Borkmann592867b2015-09-08 18:00:09 +0200565 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100566 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700567 if (IS_ERR(map))
568 return PTR_ERR(map);
569
Al Viroe4448ed2017-05-13 18:43:00 -0400570 key = memdup_user(ukey, map->key_size);
571 if (IS_ERR(key)) {
572 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700573 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400574 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700575
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800576 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800577 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800578 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
579 value_size = round_up(map->value_size, 8) * num_possible_cpus();
580 else
581 value_size = map->value_size;
582
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700583 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800584 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700585 if (!value)
586 goto free_key;
587
588 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800589 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700590 goto free_value;
591
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800592 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
593 * inside bpf map update or delete otherwise deadlocks are possible
594 */
595 preempt_disable();
596 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800597 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
598 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800599 err = bpf_percpu_hash_update(map, key, value, attr->flags);
600 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
601 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200602 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700603 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700604 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
605 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200606 rcu_read_lock();
607 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
608 attr->flags);
609 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700610 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
611 rcu_read_lock();
612 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
613 attr->flags);
614 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800615 } else {
616 rcu_read_lock();
617 err = map->ops->map_update_elem(map, key, value, attr->flags);
618 rcu_read_unlock();
619 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800620 __this_cpu_dec(bpf_prog_active);
621 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700622
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100623 if (!err)
624 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700625free_value:
626 kfree(value);
627free_key:
628 kfree(key);
629err_put:
630 fdput(f);
631 return err;
632}
633
634#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
635
636static int map_delete_elem(union bpf_attr *attr)
637{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100638 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700639 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700640 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200641 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700642 void *key;
643 int err;
644
645 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
646 return -EINVAL;
647
Daniel Borkmann592867b2015-09-08 18:00:09 +0200648 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100649 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700650 if (IS_ERR(map))
651 return PTR_ERR(map);
652
Al Viroe4448ed2017-05-13 18:43:00 -0400653 key = memdup_user(ukey, map->key_size);
654 if (IS_ERR(key)) {
655 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700656 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400657 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700658
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800659 preempt_disable();
660 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700661 rcu_read_lock();
662 err = map->ops->map_delete_elem(map, key);
663 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800664 __this_cpu_dec(bpf_prog_active);
665 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700666
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100667 if (!err)
668 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700669 kfree(key);
670err_put:
671 fdput(f);
672 return err;
673}
674
675/* last field in 'union bpf_attr' used by this command */
676#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
677
678static int map_get_next_key(union bpf_attr *attr)
679{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100680 void __user *ukey = u64_to_user_ptr(attr->key);
681 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700682 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700683 struct bpf_map *map;
684 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200685 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700686 int err;
687
688 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
689 return -EINVAL;
690
Daniel Borkmann592867b2015-09-08 18:00:09 +0200691 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100692 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700693 if (IS_ERR(map))
694 return PTR_ERR(map);
695
Teng Qin8fe45922017-04-24 19:00:37 -0700696 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400697 key = memdup_user(ukey, map->key_size);
698 if (IS_ERR(key)) {
699 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700700 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400701 }
Teng Qin8fe45922017-04-24 19:00:37 -0700702 } else {
703 key = NULL;
704 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700705
706 err = -ENOMEM;
707 next_key = kmalloc(map->key_size, GFP_USER);
708 if (!next_key)
709 goto free_key;
710
711 rcu_read_lock();
712 err = map->ops->map_get_next_key(map, key, next_key);
713 rcu_read_unlock();
714 if (err)
715 goto free_next_key;
716
717 err = -EFAULT;
718 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
719 goto free_next_key;
720
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100721 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700722 err = 0;
723
724free_next_key:
725 kfree(next_key);
726free_key:
727 kfree(key);
728err_put:
729 fdput(f);
730 return err;
731}
732
Johannes Bergbe9370a2017-04-11 15:34:57 +0200733static const struct bpf_verifier_ops * const bpf_prog_types[] = {
734#define BPF_PROG_TYPE(_id, _ops) \
735 [_id] = &_ops,
Johannes Berg40077e02017-04-11 15:34:58 +0200736#define BPF_MAP_TYPE(_id, _ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +0200737#include <linux/bpf_types.h>
738#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +0200739#undef BPF_MAP_TYPE
Johannes Bergbe9370a2017-04-11 15:34:57 +0200740};
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700741
742static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
743{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200744 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
745 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700746
Johannes Bergbe9370a2017-04-11 15:34:57 +0200747 prog->aux->ops = bpf_prog_types[type];
748 prog->type = type;
749 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700750}
751
752/* drop refcnt on maps used by eBPF program and free auxilary data */
753static void free_used_maps(struct bpf_prog_aux *aux)
754{
755 int i;
756
757 for (i = 0; i < aux->used_map_cnt; i++)
758 bpf_map_put(aux->used_maps[i]);
759
760 kfree(aux->used_maps);
761}
762
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100763int __bpf_prog_charge(struct user_struct *user, u32 pages)
764{
765 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
766 unsigned long user_bufs;
767
768 if (user) {
769 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
770 if (user_bufs > memlock_limit) {
771 atomic_long_sub(pages, &user->locked_vm);
772 return -EPERM;
773 }
774 }
775
776 return 0;
777}
778
779void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
780{
781 if (user)
782 atomic_long_sub(pages, &user->locked_vm);
783}
784
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700785static int bpf_prog_charge_memlock(struct bpf_prog *prog)
786{
787 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100788 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700789
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100790 ret = __bpf_prog_charge(user, prog->pages);
791 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700792 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100793 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700794 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100795
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700796 prog->aux->user = user;
797 return 0;
798}
799
800static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
801{
802 struct user_struct *user = prog->aux->user;
803
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100804 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700805 free_uid(user);
806}
807
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700808static int bpf_prog_alloc_id(struct bpf_prog *prog)
809{
810 int id;
811
812 spin_lock_bh(&prog_idr_lock);
813 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
814 if (id > 0)
815 prog->aux->id = id;
816 spin_unlock_bh(&prog_idr_lock);
817
818 /* id is in [1, INT_MAX) */
819 if (WARN_ON_ONCE(!id))
820 return -ENOSPC;
821
822 return id > 0 ? 0 : id;
823}
824
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700825static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700826{
827 /* cBPF to eBPF migrations are currently not in the idr store. */
828 if (!prog->aux->id)
829 return;
830
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700831 if (do_idr_lock)
832 spin_lock_bh(&prog_idr_lock);
833 else
834 __acquire(&prog_idr_lock);
835
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700836 idr_remove(&prog_idr, prog->aux->id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700837
838 if (do_idr_lock)
839 spin_unlock_bh(&prog_idr_lock);
840 else
841 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700842}
843
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200844static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700845{
846 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
847
848 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700849 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700850 bpf_prog_free(aux->prog);
851}
852
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700853static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700854{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100855 if (atomic_dec_and_test(&prog->aux->refcnt)) {
856 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700857 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700858 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100859 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200860 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100861 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700862}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700863
864void bpf_prog_put(struct bpf_prog *prog)
865{
866 __bpf_prog_put(prog, true);
867}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100868EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700869
870static int bpf_prog_release(struct inode *inode, struct file *filp)
871{
872 struct bpf_prog *prog = filp->private_data;
873
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200874 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700875 return 0;
876}
877
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100878#ifdef CONFIG_PROC_FS
879static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
880{
881 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100882 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100883
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100884 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100885 seq_printf(m,
886 "prog_type:\t%u\n"
887 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100888 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100889 "memlock:\t%llu\n",
890 prog->type,
891 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100892 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100893 prog->pages * 1ULL << PAGE_SHIFT);
894}
895#endif
896
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700897static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100898#ifdef CONFIG_PROC_FS
899 .show_fdinfo = bpf_prog_show_fdinfo,
900#endif
901 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700902};
903
Daniel Borkmannb2197752015-10-29 14:58:09 +0100904int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100905{
906 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
907 O_RDWR | O_CLOEXEC);
908}
909
Daniel Borkmann113214b2016-06-30 17:24:44 +0200910static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700911{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700912 if (!f.file)
913 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700914 if (f.file->f_op != &bpf_prog_fops) {
915 fdput(f);
916 return ERR_PTR(-EINVAL);
917 }
918
Daniel Borkmannc2101292015-10-29 14:58:07 +0100919 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700920}
921
Brenden Blanco59d36562016-07-19 12:16:46 -0700922struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700923{
Brenden Blanco59d36562016-07-19 12:16:46 -0700924 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
925 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700926 return ERR_PTR(-EBUSY);
927 }
928 return prog;
929}
Brenden Blanco59d36562016-07-19 12:16:46 -0700930EXPORT_SYMBOL_GPL(bpf_prog_add);
931
Daniel Borkmannc5405942016-11-09 22:02:34 +0100932void bpf_prog_sub(struct bpf_prog *prog, int i)
933{
934 /* Only to be used for undoing previous bpf_prog_add() in some
935 * error path. We still know that another entity in our call
936 * path holds a reference to the program, thus atomic_sub() can
937 * be safely used in such cases!
938 */
939 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
940}
941EXPORT_SYMBOL_GPL(bpf_prog_sub);
942
Brenden Blanco59d36562016-07-19 12:16:46 -0700943struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
944{
945 return bpf_prog_add(prog, 1);
946}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100947EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700948
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700949/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -0700950struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700951{
952 int refold;
953
954 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
955
956 if (refold >= BPF_MAX_REFCNT) {
957 __bpf_prog_put(prog, false);
958 return ERR_PTR(-EBUSY);
959 }
960
961 if (!refold)
962 return ERR_PTR(-ENOENT);
963
964 return prog;
965}
John Fastabenda6f6df62017-08-15 22:32:22 -0700966EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700967
Daniel Borkmann113214b2016-06-30 17:24:44 +0200968static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700969{
970 struct fd f = fdget(ufd);
971 struct bpf_prog *prog;
972
Daniel Borkmann113214b2016-06-30 17:24:44 +0200973 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700974 if (IS_ERR(prog))
975 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200976 if (type && prog->type != *type) {
977 prog = ERR_PTR(-EINVAL);
978 goto out;
979 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700980
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700981 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200982out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700983 fdput(f);
984 return prog;
985}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200986
987struct bpf_prog *bpf_prog_get(u32 ufd)
988{
989 return __bpf_prog_get(ufd, NULL);
990}
991
992struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
993{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100994 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
995
996 if (!IS_ERR(prog))
997 trace_bpf_prog_get_type(prog);
998 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200999}
1000EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001001
1002/* last field in 'union bpf_attr' used by this command */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001003#define BPF_PROG_LOAD_LAST_FIELD prog_name
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001004
1005static int bpf_prog_load(union bpf_attr *attr)
1006{
1007 enum bpf_prog_type type = attr->prog_type;
1008 struct bpf_prog *prog;
1009 int err;
1010 char license[128];
1011 bool is_gpl;
1012
1013 if (CHECK_ATTR(BPF_PROG_LOAD))
1014 return -EINVAL;
1015
David S. Millere07b98d2017-05-10 11:38:07 -07001016 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1017 return -EINVAL;
1018
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001019 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001020 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001021 sizeof(license) - 1) < 0)
1022 return -EFAULT;
1023 license[sizeof(license) - 1] = 0;
1024
1025 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1026 is_gpl = license_is_gpl_compatible(license);
1027
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001028 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1029 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001030
Alexei Starovoitov25415172015-03-25 12:49:20 -07001031 if (type == BPF_PROG_TYPE_KPROBE &&
1032 attr->kern_version != LINUX_VERSION_CODE)
1033 return -EINVAL;
1034
Chenbo Feng80b7d812017-05-31 18:16:00 -07001035 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1036 type != BPF_PROG_TYPE_CGROUP_SKB &&
1037 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001038 return -EPERM;
1039
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001040 /* plain bpf_prog allocation */
1041 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1042 if (!prog)
1043 return -ENOMEM;
1044
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001045 err = bpf_prog_charge_memlock(prog);
1046 if (err)
1047 goto free_prog_nouncharge;
1048
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001049 prog->len = attr->insn_cnt;
1050
1051 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001052 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001053 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001054 goto free_prog;
1055
1056 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001057 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001058
1059 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001060 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001061
1062 /* find program type: socket_filter vs tracing_filter */
1063 err = find_prog_type(type, prog);
1064 if (err < 0)
1065 goto free_prog;
1066
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001067 prog->aux->load_time = ktime_get_boot_ns();
1068 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1069 if (err)
1070 goto free_prog;
1071
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001072 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001073 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001074 if (err < 0)
1075 goto free_used_maps;
1076
1077 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001078 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001079 if (err < 0)
1080 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001081
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001082 err = bpf_prog_alloc_id(prog);
1083 if (err)
1084 goto free_used_maps;
1085
Daniel Borkmannaa797812015-10-29 14:58:06 +01001086 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001087 if (err < 0) {
1088 /* failed to allocate fd.
1089 * bpf_prog_put() is needed because the above
1090 * bpf_prog_alloc_id() has published the prog
1091 * to the userspace and the userspace may
1092 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1093 */
1094 bpf_prog_put(prog);
1095 return err;
1096 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001097
Daniel Borkmann74451e662017-02-16 22:24:50 +01001098 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001099 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001100 return err;
1101
1102free_used_maps:
1103 free_used_maps(prog->aux);
1104free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001105 bpf_prog_uncharge_memlock(prog);
1106free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001107 bpf_prog_free(prog);
1108 return err;
1109}
1110
Daniel Borkmannb2197752015-10-29 14:58:09 +01001111#define BPF_OBJ_LAST_FIELD bpf_fd
1112
1113static int bpf_obj_pin(const union bpf_attr *attr)
1114{
1115 if (CHECK_ATTR(BPF_OBJ))
1116 return -EINVAL;
1117
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001118 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001119}
1120
1121static int bpf_obj_get(const union bpf_attr *attr)
1122{
1123 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1124 return -EINVAL;
1125
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001126 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001127}
1128
Daniel Mackf4324552016-11-23 16:52:27 +01001129#ifdef CONFIG_CGROUP_BPF
1130
John Fastabend464bc0f2017-08-28 07:10:04 -07001131#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001132
John Fastabend5a67da22017-09-08 14:00:49 -07001133static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001134{
John Fastabend5a67da22017-09-08 14:00:49 -07001135 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001136 int ufd = attr->target_fd;
1137 struct bpf_map *map;
1138 struct fd f;
1139 int err;
1140
1141 f = fdget(ufd);
1142 map = __bpf_map_get(f);
1143 if (IS_ERR(map))
1144 return PTR_ERR(map);
1145
John Fastabend5a67da22017-09-08 14:00:49 -07001146 if (attach) {
1147 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1148 BPF_PROG_TYPE_SK_SKB);
1149 if (IS_ERR(prog)) {
1150 fdput(f);
1151 return PTR_ERR(prog);
1152 }
John Fastabend174a79f2017-08-15 22:32:47 -07001153 }
1154
John Fastabend5a67da22017-09-08 14:00:49 -07001155 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001156 if (err) {
1157 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001158 if (prog)
1159 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001160 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001161 }
1162
1163 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001164 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001165}
Daniel Mackf4324552016-11-23 16:52:27 +01001166
1167static int bpf_prog_attach(const union bpf_attr *attr)
1168{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001169 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001170 struct bpf_prog *prog;
1171 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001172 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001173
1174 if (!capable(CAP_NET_ADMIN))
1175 return -EPERM;
1176
1177 if (CHECK_ATTR(BPF_PROG_ATTACH))
1178 return -EINVAL;
1179
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001180 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1181 return -EINVAL;
1182
Daniel Mackf4324552016-11-23 16:52:27 +01001183 switch (attr->attach_type) {
1184 case BPF_CGROUP_INET_INGRESS:
1185 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001186 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001187 break;
David Ahern610236582016-12-01 08:48:04 -08001188 case BPF_CGROUP_INET_SOCK_CREATE:
1189 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1190 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001191 case BPF_CGROUP_SOCK_OPS:
1192 ptype = BPF_PROG_TYPE_SOCK_OPS;
1193 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001194 case BPF_SK_SKB_STREAM_PARSER:
1195 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend5a67da22017-09-08 14:00:49 -07001196 return sockmap_get_from_fd(attr, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001197 default:
1198 return -EINVAL;
1199 }
1200
David Ahernb2cd1252016-12-01 08:48:03 -08001201 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1202 if (IS_ERR(prog))
1203 return PTR_ERR(prog);
1204
1205 cgrp = cgroup_get_from_fd(attr->target_fd);
1206 if (IS_ERR(cgrp)) {
1207 bpf_prog_put(prog);
1208 return PTR_ERR(cgrp);
1209 }
1210
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001211 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1212 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1213 if (ret)
1214 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001215 cgroup_put(cgrp);
1216
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001217 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001218}
1219
1220#define BPF_PROG_DETACH_LAST_FIELD attach_type
1221
1222static int bpf_prog_detach(const union bpf_attr *attr)
1223{
1224 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001225 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001226
1227 if (!capable(CAP_NET_ADMIN))
1228 return -EPERM;
1229
1230 if (CHECK_ATTR(BPF_PROG_DETACH))
1231 return -EINVAL;
1232
1233 switch (attr->attach_type) {
1234 case BPF_CGROUP_INET_INGRESS:
1235 case BPF_CGROUP_INET_EGRESS:
David Ahern610236582016-12-01 08:48:04 -08001236 case BPF_CGROUP_INET_SOCK_CREATE:
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001237 case BPF_CGROUP_SOCK_OPS:
Daniel Mackf4324552016-11-23 16:52:27 +01001238 cgrp = cgroup_get_from_fd(attr->target_fd);
1239 if (IS_ERR(cgrp))
1240 return PTR_ERR(cgrp);
1241
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001242 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001243 cgroup_put(cgrp);
1244 break;
John Fastabend5a67da22017-09-08 14:00:49 -07001245 case BPF_SK_SKB_STREAM_PARSER:
1246 case BPF_SK_SKB_STREAM_VERDICT:
1247 ret = sockmap_get_from_fd(attr, false);
1248 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001249 default:
1250 return -EINVAL;
1251 }
1252
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001253 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001254}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001255
Daniel Mackf4324552016-11-23 16:52:27 +01001256#endif /* CONFIG_CGROUP_BPF */
1257
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001258#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1259
1260static int bpf_prog_test_run(const union bpf_attr *attr,
1261 union bpf_attr __user *uattr)
1262{
1263 struct bpf_prog *prog;
1264 int ret = -ENOTSUPP;
1265
1266 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1267 return -EINVAL;
1268
1269 prog = bpf_prog_get(attr->test.prog_fd);
1270 if (IS_ERR(prog))
1271 return PTR_ERR(prog);
1272
1273 if (prog->aux->ops->test_run)
1274 ret = prog->aux->ops->test_run(prog, attr, uattr);
1275
1276 bpf_prog_put(prog);
1277 return ret;
1278}
1279
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001280#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1281
1282static int bpf_obj_get_next_id(const union bpf_attr *attr,
1283 union bpf_attr __user *uattr,
1284 struct idr *idr,
1285 spinlock_t *lock)
1286{
1287 u32 next_id = attr->start_id;
1288 int err = 0;
1289
1290 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1291 return -EINVAL;
1292
1293 if (!capable(CAP_SYS_ADMIN))
1294 return -EPERM;
1295
1296 next_id++;
1297 spin_lock_bh(lock);
1298 if (!idr_get_next(idr, &next_id))
1299 err = -ENOENT;
1300 spin_unlock_bh(lock);
1301
1302 if (!err)
1303 err = put_user(next_id, &uattr->next_id);
1304
1305 return err;
1306}
1307
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001308#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1309
1310static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1311{
1312 struct bpf_prog *prog;
1313 u32 id = attr->prog_id;
1314 int fd;
1315
1316 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1317 return -EINVAL;
1318
1319 if (!capable(CAP_SYS_ADMIN))
1320 return -EPERM;
1321
1322 spin_lock_bh(&prog_idr_lock);
1323 prog = idr_find(&prog_idr, id);
1324 if (prog)
1325 prog = bpf_prog_inc_not_zero(prog);
1326 else
1327 prog = ERR_PTR(-ENOENT);
1328 spin_unlock_bh(&prog_idr_lock);
1329
1330 if (IS_ERR(prog))
1331 return PTR_ERR(prog);
1332
1333 fd = bpf_prog_new_fd(prog);
1334 if (fd < 0)
1335 bpf_prog_put(prog);
1336
1337 return fd;
1338}
1339
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001340#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1341
1342static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1343{
1344 struct bpf_map *map;
1345 u32 id = attr->map_id;
1346 int fd;
1347
1348 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1349 return -EINVAL;
1350
1351 if (!capable(CAP_SYS_ADMIN))
1352 return -EPERM;
1353
1354 spin_lock_bh(&map_idr_lock);
1355 map = idr_find(&map_idr, id);
1356 if (map)
1357 map = bpf_map_inc_not_zero(map, true);
1358 else
1359 map = ERR_PTR(-ENOENT);
1360 spin_unlock_bh(&map_idr_lock);
1361
1362 if (IS_ERR(map))
1363 return PTR_ERR(map);
1364
1365 fd = bpf_map_new_fd(map);
1366 if (fd < 0)
1367 bpf_map_put(map);
1368
1369 return fd;
1370}
1371
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001372static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1373 const union bpf_attr *attr,
1374 union bpf_attr __user *uattr)
1375{
1376 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1377 struct bpf_prog_info info = {};
1378 u32 info_len = attr->info.info_len;
1379 char __user *uinsns;
1380 u32 ulen;
1381 int err;
1382
1383 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1384 if (err)
1385 return err;
1386 info_len = min_t(u32, sizeof(info), info_len);
1387
1388 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001389 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001390
1391 info.type = prog->type;
1392 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001393 info.load_time = prog->aux->load_time;
1394 info.created_by_uid = from_kuid_munged(current_user_ns(),
1395 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001396
1397 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001398 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1399
1400 ulen = info.nr_map_ids;
1401 info.nr_map_ids = prog->aux->used_map_cnt;
1402 ulen = min_t(u32, info.nr_map_ids, ulen);
1403 if (ulen) {
1404 u32 *user_map_ids = (u32 *)info.map_ids;
1405 u32 i;
1406
1407 for (i = 0; i < ulen; i++)
1408 if (put_user(prog->aux->used_maps[i]->id,
1409 &user_map_ids[i]))
1410 return -EFAULT;
1411 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001412
1413 if (!capable(CAP_SYS_ADMIN)) {
1414 info.jited_prog_len = 0;
1415 info.xlated_prog_len = 0;
1416 goto done;
1417 }
1418
1419 ulen = info.jited_prog_len;
1420 info.jited_prog_len = prog->jited_len;
1421 if (info.jited_prog_len && ulen) {
1422 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1423 ulen = min_t(u32, info.jited_prog_len, ulen);
1424 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1425 return -EFAULT;
1426 }
1427
1428 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001429 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001430 if (info.xlated_prog_len && ulen) {
1431 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1432 ulen = min_t(u32, info.xlated_prog_len, ulen);
1433 if (copy_to_user(uinsns, prog->insnsi, ulen))
1434 return -EFAULT;
1435 }
1436
1437done:
1438 if (copy_to_user(uinfo, &info, info_len) ||
1439 put_user(info_len, &uattr->info.info_len))
1440 return -EFAULT;
1441
1442 return 0;
1443}
1444
1445static int bpf_map_get_info_by_fd(struct bpf_map *map,
1446 const union bpf_attr *attr,
1447 union bpf_attr __user *uattr)
1448{
1449 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1450 struct bpf_map_info info = {};
1451 u32 info_len = attr->info.info_len;
1452 int err;
1453
1454 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1455 if (err)
1456 return err;
1457 info_len = min_t(u32, sizeof(info), info_len);
1458
1459 info.type = map->map_type;
1460 info.id = map->id;
1461 info.key_size = map->key_size;
1462 info.value_size = map->value_size;
1463 info.max_entries = map->max_entries;
1464 info.map_flags = map->map_flags;
1465
1466 if (copy_to_user(uinfo, &info, info_len) ||
1467 put_user(info_len, &uattr->info.info_len))
1468 return -EFAULT;
1469
1470 return 0;
1471}
1472
1473#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1474
1475static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1476 union bpf_attr __user *uattr)
1477{
1478 int ufd = attr->info.bpf_fd;
1479 struct fd f;
1480 int err;
1481
1482 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1483 return -EINVAL;
1484
1485 f = fdget(ufd);
1486 if (!f.file)
1487 return -EBADFD;
1488
1489 if (f.file->f_op == &bpf_prog_fops)
1490 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1491 uattr);
1492 else if (f.file->f_op == &bpf_map_fops)
1493 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1494 uattr);
1495 else
1496 err = -EINVAL;
1497
1498 fdput(f);
1499 return err;
1500}
1501
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001502SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1503{
1504 union bpf_attr attr = {};
1505 int err;
1506
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001507 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001508 return -EPERM;
1509
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001510 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1511 if (err)
1512 return err;
1513 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001514
1515 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1516 if (copy_from_user(&attr, uattr, size) != 0)
1517 return -EFAULT;
1518
1519 switch (cmd) {
1520 case BPF_MAP_CREATE:
1521 err = map_create(&attr);
1522 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001523 case BPF_MAP_LOOKUP_ELEM:
1524 err = map_lookup_elem(&attr);
1525 break;
1526 case BPF_MAP_UPDATE_ELEM:
1527 err = map_update_elem(&attr);
1528 break;
1529 case BPF_MAP_DELETE_ELEM:
1530 err = map_delete_elem(&attr);
1531 break;
1532 case BPF_MAP_GET_NEXT_KEY:
1533 err = map_get_next_key(&attr);
1534 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001535 case BPF_PROG_LOAD:
1536 err = bpf_prog_load(&attr);
1537 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001538 case BPF_OBJ_PIN:
1539 err = bpf_obj_pin(&attr);
1540 break;
1541 case BPF_OBJ_GET:
1542 err = bpf_obj_get(&attr);
1543 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001544#ifdef CONFIG_CGROUP_BPF
1545 case BPF_PROG_ATTACH:
1546 err = bpf_prog_attach(&attr);
1547 break;
1548 case BPF_PROG_DETACH:
1549 err = bpf_prog_detach(&attr);
1550 break;
1551#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001552 case BPF_PROG_TEST_RUN:
1553 err = bpf_prog_test_run(&attr, uattr);
1554 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001555 case BPF_PROG_GET_NEXT_ID:
1556 err = bpf_obj_get_next_id(&attr, uattr,
1557 &prog_idr, &prog_idr_lock);
1558 break;
1559 case BPF_MAP_GET_NEXT_ID:
1560 err = bpf_obj_get_next_id(&attr, uattr,
1561 &map_idr, &map_idr_lock);
1562 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001563 case BPF_PROG_GET_FD_BY_ID:
1564 err = bpf_prog_get_fd_by_id(&attr);
1565 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001566 case BPF_MAP_GET_FD_BY_ID:
1567 err = bpf_map_get_fd_by_id(&attr);
1568 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001569 case BPF_OBJ_GET_INFO_BY_FD:
1570 err = bpf_obj_get_info_by_fd(&attr, uattr);
1571 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001572 default:
1573 err = -EINVAL;
1574 break;
1575 }
1576
1577 return err;
1578}