blob: d124e702e040eece4c09f65980bb561dbd6bb7cc [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
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700325 memset(dst, 0, BPF_OBJ_NAME_LEN);
326
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700327 /* Copy all isalnum() and '_' char */
328 while (src < end && *src) {
329 if (!isalnum(*src) && *src != '_')
330 return -EINVAL;
331 *dst++ = *src++;
332 }
333
334 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
335 if (src == end)
336 return -EINVAL;
337
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700338 return 0;
339}
340
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700341#define BPF_MAP_CREATE_LAST_FIELD map_name
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700342/* called via syscall */
343static int map_create(union bpf_attr *attr)
344{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700345 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700346 struct bpf_map *map;
347 int err;
348
349 err = CHECK_ATTR(BPF_MAP_CREATE);
350 if (err)
351 return -EINVAL;
352
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700353 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700354 ((unsigned int)numa_node >= nr_node_ids ||
355 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700356 return -EINVAL;
357
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700358 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
359 map = find_and_alloc_map(attr);
360 if (IS_ERR(map))
361 return PTR_ERR(map);
362
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700363 err = bpf_obj_name_cpy(map->name, attr->map_name);
364 if (err)
365 goto free_map_nouncharge;
366
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700367 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100368 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700369
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700370 err = bpf_map_charge_memlock(map);
371 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100372 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700373
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700374 err = bpf_map_alloc_id(map);
375 if (err)
376 goto free_map;
377
Daniel Borkmannaa797812015-10-29 14:58:06 +0100378 err = bpf_map_new_fd(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700379 if (err < 0) {
380 /* failed to allocate fd.
381 * bpf_map_put() is needed because the above
382 * bpf_map_alloc_id() has published the map
383 * to the userspace and the userspace may
384 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
385 */
386 bpf_map_put(map);
387 return err;
388 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700389
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100390 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700391 return err;
392
393free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100394 bpf_map_uncharge_memlock(map);
395free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700396 map->ops->map_free(map);
397 return err;
398}
399
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700400/* if error is returned, fd is released.
401 * On success caller should complete fd access with matching fdput()
402 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100403struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700404{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700405 if (!f.file)
406 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700407 if (f.file->f_op != &bpf_map_fops) {
408 fdput(f);
409 return ERR_PTR(-EINVAL);
410 }
411
Daniel Borkmannc2101292015-10-29 14:58:07 +0100412 return f.file->private_data;
413}
414
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700415/* prog's and map's refcnt limit */
416#define BPF_MAX_REFCNT 32768
417
418struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100419{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700420 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
421 atomic_dec(&map->refcnt);
422 return ERR_PTR(-EBUSY);
423 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100424 if (uref)
425 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700426 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100427}
428
429struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100430{
431 struct fd f = fdget(ufd);
432 struct bpf_map *map;
433
434 map = __bpf_map_get(f);
435 if (IS_ERR(map))
436 return map;
437
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700438 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100439 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700440
441 return map;
442}
443
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700444/* map_idr_lock should have been held */
445static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
446 bool uref)
447{
448 int refold;
449
450 refold = __atomic_add_unless(&map->refcnt, 1, 0);
451
452 if (refold >= BPF_MAX_REFCNT) {
453 __bpf_map_put(map, false);
454 return ERR_PTR(-EBUSY);
455 }
456
457 if (!refold)
458 return ERR_PTR(-ENOENT);
459
460 if (uref)
461 atomic_inc(&map->usercnt);
462
463 return map;
464}
465
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800466int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
467{
468 return -ENOTSUPP;
469}
470
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700471/* last field in 'union bpf_attr' used by this command */
472#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
473
474static int map_lookup_elem(union bpf_attr *attr)
475{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100476 void __user *ukey = u64_to_user_ptr(attr->key);
477 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700478 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700479 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800480 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800481 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200482 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700483 int err;
484
485 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
486 return -EINVAL;
487
Daniel Borkmann592867b2015-09-08 18:00:09 +0200488 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100489 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700490 if (IS_ERR(map))
491 return PTR_ERR(map);
492
Al Viroe4448ed2017-05-13 18:43:00 -0400493 key = memdup_user(ukey, map->key_size);
494 if (IS_ERR(key)) {
495 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700496 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400497 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700498
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800499 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800500 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800501 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
502 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700503 else if (IS_FD_MAP(map))
504 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800505 else
506 value_size = map->value_size;
507
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800508 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800509 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700510 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800511 goto free_key;
512
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800513 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
514 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800515 err = bpf_percpu_hash_copy(map, key, value);
516 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
517 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800518 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
519 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700520 } else if (IS_FD_ARRAY(map)) {
521 err = bpf_fd_array_map_lookup_elem(map, key, value);
522 } else if (IS_FD_HASH(map)) {
523 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800524 } else {
525 rcu_read_lock();
526 ptr = map->ops->map_lookup_elem(map, key);
527 if (ptr)
528 memcpy(value, ptr, value_size);
529 rcu_read_unlock();
530 err = ptr ? 0 : -ENOENT;
531 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800532
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800533 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800534 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700535
536 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800537 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800538 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700539
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100540 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700541 err = 0;
542
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800543free_value:
544 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700545free_key:
546 kfree(key);
547err_put:
548 fdput(f);
549 return err;
550}
551
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800552#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700553
554static int map_update_elem(union bpf_attr *attr)
555{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100556 void __user *ukey = u64_to_user_ptr(attr->key);
557 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700558 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700559 struct bpf_map *map;
560 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800561 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200562 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700563 int err;
564
565 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
566 return -EINVAL;
567
Daniel Borkmann592867b2015-09-08 18:00:09 +0200568 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100569 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700570 if (IS_ERR(map))
571 return PTR_ERR(map);
572
Al Viroe4448ed2017-05-13 18:43:00 -0400573 key = memdup_user(ukey, map->key_size);
574 if (IS_ERR(key)) {
575 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700576 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400577 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700578
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800579 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800580 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800581 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
582 value_size = round_up(map->value_size, 8) * num_possible_cpus();
583 else
584 value_size = map->value_size;
585
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700586 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800587 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700588 if (!value)
589 goto free_key;
590
591 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800592 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700593 goto free_value;
594
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800595 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
596 * inside bpf map update or delete otherwise deadlocks are possible
597 */
598 preempt_disable();
599 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800600 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
601 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800602 err = bpf_percpu_hash_update(map, key, value, attr->flags);
603 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
604 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200605 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700606 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700607 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
608 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200609 rcu_read_lock();
610 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
611 attr->flags);
612 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700613 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
614 rcu_read_lock();
615 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
616 attr->flags);
617 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800618 } else {
619 rcu_read_lock();
620 err = map->ops->map_update_elem(map, key, value, attr->flags);
621 rcu_read_unlock();
622 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800623 __this_cpu_dec(bpf_prog_active);
624 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700625
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100626 if (!err)
627 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700628free_value:
629 kfree(value);
630free_key:
631 kfree(key);
632err_put:
633 fdput(f);
634 return err;
635}
636
637#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
638
639static int map_delete_elem(union bpf_attr *attr)
640{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100641 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700642 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700643 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200644 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700645 void *key;
646 int err;
647
648 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
649 return -EINVAL;
650
Daniel Borkmann592867b2015-09-08 18:00:09 +0200651 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100652 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700653 if (IS_ERR(map))
654 return PTR_ERR(map);
655
Al Viroe4448ed2017-05-13 18:43:00 -0400656 key = memdup_user(ukey, map->key_size);
657 if (IS_ERR(key)) {
658 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700659 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400660 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700661
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800662 preempt_disable();
663 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700664 rcu_read_lock();
665 err = map->ops->map_delete_elem(map, key);
666 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800667 __this_cpu_dec(bpf_prog_active);
668 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700669
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100670 if (!err)
671 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700672 kfree(key);
673err_put:
674 fdput(f);
675 return err;
676}
677
678/* last field in 'union bpf_attr' used by this command */
679#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
680
681static int map_get_next_key(union bpf_attr *attr)
682{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100683 void __user *ukey = u64_to_user_ptr(attr->key);
684 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700685 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700686 struct bpf_map *map;
687 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200688 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700689 int err;
690
691 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
692 return -EINVAL;
693
Daniel Borkmann592867b2015-09-08 18:00:09 +0200694 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100695 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700696 if (IS_ERR(map))
697 return PTR_ERR(map);
698
Teng Qin8fe45922017-04-24 19:00:37 -0700699 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400700 key = memdup_user(ukey, map->key_size);
701 if (IS_ERR(key)) {
702 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700703 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400704 }
Teng Qin8fe45922017-04-24 19:00:37 -0700705 } else {
706 key = NULL;
707 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700708
709 err = -ENOMEM;
710 next_key = kmalloc(map->key_size, GFP_USER);
711 if (!next_key)
712 goto free_key;
713
714 rcu_read_lock();
715 err = map->ops->map_get_next_key(map, key, next_key);
716 rcu_read_unlock();
717 if (err)
718 goto free_next_key;
719
720 err = -EFAULT;
721 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
722 goto free_next_key;
723
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100724 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700725 err = 0;
726
727free_next_key:
728 kfree(next_key);
729free_key:
730 kfree(key);
731err_put:
732 fdput(f);
733 return err;
734}
735
Johannes Bergbe9370a2017-04-11 15:34:57 +0200736static const struct bpf_verifier_ops * const bpf_prog_types[] = {
737#define BPF_PROG_TYPE(_id, _ops) \
738 [_id] = &_ops,
Johannes Berg40077e02017-04-11 15:34:58 +0200739#define BPF_MAP_TYPE(_id, _ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +0200740#include <linux/bpf_types.h>
741#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +0200742#undef BPF_MAP_TYPE
Johannes Bergbe9370a2017-04-11 15:34:57 +0200743};
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700744
745static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
746{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200747 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
748 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700749
Johannes Bergbe9370a2017-04-11 15:34:57 +0200750 prog->aux->ops = bpf_prog_types[type];
751 prog->type = type;
752 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700753}
754
755/* drop refcnt on maps used by eBPF program and free auxilary data */
756static void free_used_maps(struct bpf_prog_aux *aux)
757{
758 int i;
759
760 for (i = 0; i < aux->used_map_cnt; i++)
761 bpf_map_put(aux->used_maps[i]);
762
763 kfree(aux->used_maps);
764}
765
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100766int __bpf_prog_charge(struct user_struct *user, u32 pages)
767{
768 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
769 unsigned long user_bufs;
770
771 if (user) {
772 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
773 if (user_bufs > memlock_limit) {
774 atomic_long_sub(pages, &user->locked_vm);
775 return -EPERM;
776 }
777 }
778
779 return 0;
780}
781
782void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
783{
784 if (user)
785 atomic_long_sub(pages, &user->locked_vm);
786}
787
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700788static int bpf_prog_charge_memlock(struct bpf_prog *prog)
789{
790 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100791 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700792
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100793 ret = __bpf_prog_charge(user, prog->pages);
794 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700795 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100796 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700797 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100798
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700799 prog->aux->user = user;
800 return 0;
801}
802
803static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
804{
805 struct user_struct *user = prog->aux->user;
806
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100807 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700808 free_uid(user);
809}
810
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700811static int bpf_prog_alloc_id(struct bpf_prog *prog)
812{
813 int id;
814
815 spin_lock_bh(&prog_idr_lock);
816 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
817 if (id > 0)
818 prog->aux->id = id;
819 spin_unlock_bh(&prog_idr_lock);
820
821 /* id is in [1, INT_MAX) */
822 if (WARN_ON_ONCE(!id))
823 return -ENOSPC;
824
825 return id > 0 ? 0 : id;
826}
827
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700828static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700829{
830 /* cBPF to eBPF migrations are currently not in the idr store. */
831 if (!prog->aux->id)
832 return;
833
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700834 if (do_idr_lock)
835 spin_lock_bh(&prog_idr_lock);
836 else
837 __acquire(&prog_idr_lock);
838
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700839 idr_remove(&prog_idr, prog->aux->id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700840
841 if (do_idr_lock)
842 spin_unlock_bh(&prog_idr_lock);
843 else
844 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700845}
846
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200847static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700848{
849 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
850
851 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700852 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700853 bpf_prog_free(aux->prog);
854}
855
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700856static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700857{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100858 if (atomic_dec_and_test(&prog->aux->refcnt)) {
859 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700860 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700861 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100862 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200863 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100864 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700865}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700866
867void bpf_prog_put(struct bpf_prog *prog)
868{
869 __bpf_prog_put(prog, true);
870}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100871EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700872
873static int bpf_prog_release(struct inode *inode, struct file *filp)
874{
875 struct bpf_prog *prog = filp->private_data;
876
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200877 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700878 return 0;
879}
880
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100881#ifdef CONFIG_PROC_FS
882static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
883{
884 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100885 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100886
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100887 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100888 seq_printf(m,
889 "prog_type:\t%u\n"
890 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100891 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100892 "memlock:\t%llu\n",
893 prog->type,
894 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100895 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100896 prog->pages * 1ULL << PAGE_SHIFT);
897}
898#endif
899
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700900static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100901#ifdef CONFIG_PROC_FS
902 .show_fdinfo = bpf_prog_show_fdinfo,
903#endif
904 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700905};
906
Daniel Borkmannb2197752015-10-29 14:58:09 +0100907int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100908{
909 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
910 O_RDWR | O_CLOEXEC);
911}
912
Daniel Borkmann113214b2016-06-30 17:24:44 +0200913static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700914{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700915 if (!f.file)
916 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700917 if (f.file->f_op != &bpf_prog_fops) {
918 fdput(f);
919 return ERR_PTR(-EINVAL);
920 }
921
Daniel Borkmannc2101292015-10-29 14:58:07 +0100922 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700923}
924
Brenden Blanco59d36562016-07-19 12:16:46 -0700925struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700926{
Brenden Blanco59d36562016-07-19 12:16:46 -0700927 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
928 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700929 return ERR_PTR(-EBUSY);
930 }
931 return prog;
932}
Brenden Blanco59d36562016-07-19 12:16:46 -0700933EXPORT_SYMBOL_GPL(bpf_prog_add);
934
Daniel Borkmannc5405942016-11-09 22:02:34 +0100935void bpf_prog_sub(struct bpf_prog *prog, int i)
936{
937 /* Only to be used for undoing previous bpf_prog_add() in some
938 * error path. We still know that another entity in our call
939 * path holds a reference to the program, thus atomic_sub() can
940 * be safely used in such cases!
941 */
942 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
943}
944EXPORT_SYMBOL_GPL(bpf_prog_sub);
945
Brenden Blanco59d36562016-07-19 12:16:46 -0700946struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
947{
948 return bpf_prog_add(prog, 1);
949}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100950EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700951
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700952/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -0700953struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700954{
955 int refold;
956
957 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
958
959 if (refold >= BPF_MAX_REFCNT) {
960 __bpf_prog_put(prog, false);
961 return ERR_PTR(-EBUSY);
962 }
963
964 if (!refold)
965 return ERR_PTR(-ENOENT);
966
967 return prog;
968}
John Fastabenda6f6df62017-08-15 22:32:22 -0700969EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700970
Daniel Borkmann113214b2016-06-30 17:24:44 +0200971static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700972{
973 struct fd f = fdget(ufd);
974 struct bpf_prog *prog;
975
Daniel Borkmann113214b2016-06-30 17:24:44 +0200976 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700977 if (IS_ERR(prog))
978 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200979 if (type && prog->type != *type) {
980 prog = ERR_PTR(-EINVAL);
981 goto out;
982 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700983
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700984 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200985out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700986 fdput(f);
987 return prog;
988}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200989
990struct bpf_prog *bpf_prog_get(u32 ufd)
991{
992 return __bpf_prog_get(ufd, NULL);
993}
994
995struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
996{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100997 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
998
999 if (!IS_ERR(prog))
1000 trace_bpf_prog_get_type(prog);
1001 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +02001002}
1003EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001004
1005/* last field in 'union bpf_attr' used by this command */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001006#define BPF_PROG_LOAD_LAST_FIELD prog_name
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001007
1008static int bpf_prog_load(union bpf_attr *attr)
1009{
1010 enum bpf_prog_type type = attr->prog_type;
1011 struct bpf_prog *prog;
1012 int err;
1013 char license[128];
1014 bool is_gpl;
1015
1016 if (CHECK_ATTR(BPF_PROG_LOAD))
1017 return -EINVAL;
1018
David S. Millere07b98d2017-05-10 11:38:07 -07001019 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1020 return -EINVAL;
1021
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001022 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001023 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001024 sizeof(license) - 1) < 0)
1025 return -EFAULT;
1026 license[sizeof(license) - 1] = 0;
1027
1028 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1029 is_gpl = license_is_gpl_compatible(license);
1030
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001031 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1032 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001033
Alexei Starovoitov25415172015-03-25 12:49:20 -07001034 if (type == BPF_PROG_TYPE_KPROBE &&
1035 attr->kern_version != LINUX_VERSION_CODE)
1036 return -EINVAL;
1037
Chenbo Feng80b7d812017-05-31 18:16:00 -07001038 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1039 type != BPF_PROG_TYPE_CGROUP_SKB &&
1040 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001041 return -EPERM;
1042
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001043 /* plain bpf_prog allocation */
1044 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1045 if (!prog)
1046 return -ENOMEM;
1047
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001048 err = bpf_prog_charge_memlock(prog);
1049 if (err)
1050 goto free_prog_nouncharge;
1051
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001052 prog->len = attr->insn_cnt;
1053
1054 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001055 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001056 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001057 goto free_prog;
1058
1059 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001060 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001061
1062 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001063 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001064
1065 /* find program type: socket_filter vs tracing_filter */
1066 err = find_prog_type(type, prog);
1067 if (err < 0)
1068 goto free_prog;
1069
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001070 prog->aux->load_time = ktime_get_boot_ns();
1071 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1072 if (err)
1073 goto free_prog;
1074
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001075 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001076 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001077 if (err < 0)
1078 goto free_used_maps;
1079
1080 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001081 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001082 if (err < 0)
1083 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001084
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001085 err = bpf_prog_alloc_id(prog);
1086 if (err)
1087 goto free_used_maps;
1088
Daniel Borkmannaa797812015-10-29 14:58:06 +01001089 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001090 if (err < 0) {
1091 /* failed to allocate fd.
1092 * bpf_prog_put() is needed because the above
1093 * bpf_prog_alloc_id() has published the prog
1094 * to the userspace and the userspace may
1095 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1096 */
1097 bpf_prog_put(prog);
1098 return err;
1099 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001100
Daniel Borkmann74451e662017-02-16 22:24:50 +01001101 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001102 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001103 return err;
1104
1105free_used_maps:
1106 free_used_maps(prog->aux);
1107free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001108 bpf_prog_uncharge_memlock(prog);
1109free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001110 bpf_prog_free(prog);
1111 return err;
1112}
1113
Daniel Borkmannb2197752015-10-29 14:58:09 +01001114#define BPF_OBJ_LAST_FIELD bpf_fd
1115
1116static int bpf_obj_pin(const union bpf_attr *attr)
1117{
1118 if (CHECK_ATTR(BPF_OBJ))
1119 return -EINVAL;
1120
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001121 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001122}
1123
1124static int bpf_obj_get(const union bpf_attr *attr)
1125{
1126 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1127 return -EINVAL;
1128
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001129 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001130}
1131
Daniel Mackf4324552016-11-23 16:52:27 +01001132#ifdef CONFIG_CGROUP_BPF
1133
John Fastabend464bc0f2017-08-28 07:10:04 -07001134#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001135
John Fastabend5a67da22017-09-08 14:00:49 -07001136static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001137{
John Fastabend5a67da22017-09-08 14:00:49 -07001138 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001139 int ufd = attr->target_fd;
1140 struct bpf_map *map;
1141 struct fd f;
1142 int err;
1143
1144 f = fdget(ufd);
1145 map = __bpf_map_get(f);
1146 if (IS_ERR(map))
1147 return PTR_ERR(map);
1148
John Fastabend5a67da22017-09-08 14:00:49 -07001149 if (attach) {
1150 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1151 BPF_PROG_TYPE_SK_SKB);
1152 if (IS_ERR(prog)) {
1153 fdput(f);
1154 return PTR_ERR(prog);
1155 }
John Fastabend174a79f2017-08-15 22:32:47 -07001156 }
1157
John Fastabend5a67da22017-09-08 14:00:49 -07001158 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001159 if (err) {
1160 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001161 if (prog)
1162 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001163 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001164 }
1165
1166 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001167 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001168}
Daniel Mackf4324552016-11-23 16:52:27 +01001169
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001170#define BPF_F_ATTACH_MASK \
1171 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1172
Daniel Mackf4324552016-11-23 16:52:27 +01001173static int bpf_prog_attach(const union bpf_attr *attr)
1174{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001175 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001176 struct bpf_prog *prog;
1177 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001178 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001179
1180 if (!capable(CAP_NET_ADMIN))
1181 return -EPERM;
1182
1183 if (CHECK_ATTR(BPF_PROG_ATTACH))
1184 return -EINVAL;
1185
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001186 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001187 return -EINVAL;
1188
Daniel Mackf4324552016-11-23 16:52:27 +01001189 switch (attr->attach_type) {
1190 case BPF_CGROUP_INET_INGRESS:
1191 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001192 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001193 break;
David Ahern610236582016-12-01 08:48:04 -08001194 case BPF_CGROUP_INET_SOCK_CREATE:
1195 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1196 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001197 case BPF_CGROUP_SOCK_OPS:
1198 ptype = BPF_PROG_TYPE_SOCK_OPS;
1199 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001200 case BPF_SK_SKB_STREAM_PARSER:
1201 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend5a67da22017-09-08 14:00:49 -07001202 return sockmap_get_from_fd(attr, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001203 default:
1204 return -EINVAL;
1205 }
1206
David Ahernb2cd1252016-12-01 08:48:03 -08001207 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1208 if (IS_ERR(prog))
1209 return PTR_ERR(prog);
1210
1211 cgrp = cgroup_get_from_fd(attr->target_fd);
1212 if (IS_ERR(cgrp)) {
1213 bpf_prog_put(prog);
1214 return PTR_ERR(cgrp);
1215 }
1216
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001217 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1218 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001219 if (ret)
1220 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001221 cgroup_put(cgrp);
1222
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001223 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001224}
1225
1226#define BPF_PROG_DETACH_LAST_FIELD attach_type
1227
1228static int bpf_prog_detach(const union bpf_attr *attr)
1229{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001230 enum bpf_prog_type ptype;
1231 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001232 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001233 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001234
1235 if (!capable(CAP_NET_ADMIN))
1236 return -EPERM;
1237
1238 if (CHECK_ATTR(BPF_PROG_DETACH))
1239 return -EINVAL;
1240
1241 switch (attr->attach_type) {
1242 case BPF_CGROUP_INET_INGRESS:
1243 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001244 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1245 break;
David Ahern610236582016-12-01 08:48:04 -08001246 case BPF_CGROUP_INET_SOCK_CREATE:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001247 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1248 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001249 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001250 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001251 break;
John Fastabend5a67da22017-09-08 14:00:49 -07001252 case BPF_SK_SKB_STREAM_PARSER:
1253 case BPF_SK_SKB_STREAM_VERDICT:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001254 return sockmap_get_from_fd(attr, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001255 default:
1256 return -EINVAL;
1257 }
1258
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001259 cgrp = cgroup_get_from_fd(attr->target_fd);
1260 if (IS_ERR(cgrp))
1261 return PTR_ERR(cgrp);
1262
1263 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1264 if (IS_ERR(prog))
1265 prog = NULL;
1266
1267 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1268 if (prog)
1269 bpf_prog_put(prog);
1270 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001271 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001272}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001273
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001274#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1275
1276static int bpf_prog_query(const union bpf_attr *attr,
1277 union bpf_attr __user *uattr)
1278{
1279 struct cgroup *cgrp;
1280 int ret;
1281
1282 if (!capable(CAP_NET_ADMIN))
1283 return -EPERM;
1284 if (CHECK_ATTR(BPF_PROG_QUERY))
1285 return -EINVAL;
1286 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1287 return -EINVAL;
1288
1289 switch (attr->query.attach_type) {
1290 case BPF_CGROUP_INET_INGRESS:
1291 case BPF_CGROUP_INET_EGRESS:
1292 case BPF_CGROUP_INET_SOCK_CREATE:
1293 case BPF_CGROUP_SOCK_OPS:
1294 break;
1295 default:
1296 return -EINVAL;
1297 }
1298 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1299 if (IS_ERR(cgrp))
1300 return PTR_ERR(cgrp);
1301 ret = cgroup_bpf_query(cgrp, attr, uattr);
1302 cgroup_put(cgrp);
1303 return ret;
1304}
Daniel Mackf4324552016-11-23 16:52:27 +01001305#endif /* CONFIG_CGROUP_BPF */
1306
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001307#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1308
1309static int bpf_prog_test_run(const union bpf_attr *attr,
1310 union bpf_attr __user *uattr)
1311{
1312 struct bpf_prog *prog;
1313 int ret = -ENOTSUPP;
1314
1315 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1316 return -EINVAL;
1317
1318 prog = bpf_prog_get(attr->test.prog_fd);
1319 if (IS_ERR(prog))
1320 return PTR_ERR(prog);
1321
1322 if (prog->aux->ops->test_run)
1323 ret = prog->aux->ops->test_run(prog, attr, uattr);
1324
1325 bpf_prog_put(prog);
1326 return ret;
1327}
1328
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001329#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1330
1331static int bpf_obj_get_next_id(const union bpf_attr *attr,
1332 union bpf_attr __user *uattr,
1333 struct idr *idr,
1334 spinlock_t *lock)
1335{
1336 u32 next_id = attr->start_id;
1337 int err = 0;
1338
1339 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1340 return -EINVAL;
1341
1342 if (!capable(CAP_SYS_ADMIN))
1343 return -EPERM;
1344
1345 next_id++;
1346 spin_lock_bh(lock);
1347 if (!idr_get_next(idr, &next_id))
1348 err = -ENOENT;
1349 spin_unlock_bh(lock);
1350
1351 if (!err)
1352 err = put_user(next_id, &uattr->next_id);
1353
1354 return err;
1355}
1356
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001357#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1358
1359static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1360{
1361 struct bpf_prog *prog;
1362 u32 id = attr->prog_id;
1363 int fd;
1364
1365 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1366 return -EINVAL;
1367
1368 if (!capable(CAP_SYS_ADMIN))
1369 return -EPERM;
1370
1371 spin_lock_bh(&prog_idr_lock);
1372 prog = idr_find(&prog_idr, id);
1373 if (prog)
1374 prog = bpf_prog_inc_not_zero(prog);
1375 else
1376 prog = ERR_PTR(-ENOENT);
1377 spin_unlock_bh(&prog_idr_lock);
1378
1379 if (IS_ERR(prog))
1380 return PTR_ERR(prog);
1381
1382 fd = bpf_prog_new_fd(prog);
1383 if (fd < 0)
1384 bpf_prog_put(prog);
1385
1386 return fd;
1387}
1388
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001389#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1390
1391static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1392{
1393 struct bpf_map *map;
1394 u32 id = attr->map_id;
1395 int fd;
1396
1397 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1398 return -EINVAL;
1399
1400 if (!capable(CAP_SYS_ADMIN))
1401 return -EPERM;
1402
1403 spin_lock_bh(&map_idr_lock);
1404 map = idr_find(&map_idr, id);
1405 if (map)
1406 map = bpf_map_inc_not_zero(map, true);
1407 else
1408 map = ERR_PTR(-ENOENT);
1409 spin_unlock_bh(&map_idr_lock);
1410
1411 if (IS_ERR(map))
1412 return PTR_ERR(map);
1413
1414 fd = bpf_map_new_fd(map);
1415 if (fd < 0)
1416 bpf_map_put(map);
1417
1418 return fd;
1419}
1420
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001421static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1422 const union bpf_attr *attr,
1423 union bpf_attr __user *uattr)
1424{
1425 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1426 struct bpf_prog_info info = {};
1427 u32 info_len = attr->info.info_len;
1428 char __user *uinsns;
1429 u32 ulen;
1430 int err;
1431
1432 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1433 if (err)
1434 return err;
1435 info_len = min_t(u32, sizeof(info), info_len);
1436
1437 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001438 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001439
1440 info.type = prog->type;
1441 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001442 info.load_time = prog->aux->load_time;
1443 info.created_by_uid = from_kuid_munged(current_user_ns(),
1444 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001445
1446 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001447 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1448
1449 ulen = info.nr_map_ids;
1450 info.nr_map_ids = prog->aux->used_map_cnt;
1451 ulen = min_t(u32, info.nr_map_ids, ulen);
1452 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001453 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001454 u32 i;
1455
1456 for (i = 0; i < ulen; i++)
1457 if (put_user(prog->aux->used_maps[i]->id,
1458 &user_map_ids[i]))
1459 return -EFAULT;
1460 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001461
1462 if (!capable(CAP_SYS_ADMIN)) {
1463 info.jited_prog_len = 0;
1464 info.xlated_prog_len = 0;
1465 goto done;
1466 }
1467
1468 ulen = info.jited_prog_len;
1469 info.jited_prog_len = prog->jited_len;
1470 if (info.jited_prog_len && ulen) {
1471 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1472 ulen = min_t(u32, info.jited_prog_len, ulen);
1473 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1474 return -EFAULT;
1475 }
1476
1477 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001478 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001479 if (info.xlated_prog_len && ulen) {
1480 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1481 ulen = min_t(u32, info.xlated_prog_len, ulen);
1482 if (copy_to_user(uinsns, prog->insnsi, ulen))
1483 return -EFAULT;
1484 }
1485
1486done:
1487 if (copy_to_user(uinfo, &info, info_len) ||
1488 put_user(info_len, &uattr->info.info_len))
1489 return -EFAULT;
1490
1491 return 0;
1492}
1493
1494static int bpf_map_get_info_by_fd(struct bpf_map *map,
1495 const union bpf_attr *attr,
1496 union bpf_attr __user *uattr)
1497{
1498 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1499 struct bpf_map_info info = {};
1500 u32 info_len = attr->info.info_len;
1501 int err;
1502
1503 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1504 if (err)
1505 return err;
1506 info_len = min_t(u32, sizeof(info), info_len);
1507
1508 info.type = map->map_type;
1509 info.id = map->id;
1510 info.key_size = map->key_size;
1511 info.value_size = map->value_size;
1512 info.max_entries = map->max_entries;
1513 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001514 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001515
1516 if (copy_to_user(uinfo, &info, info_len) ||
1517 put_user(info_len, &uattr->info.info_len))
1518 return -EFAULT;
1519
1520 return 0;
1521}
1522
1523#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1524
1525static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1526 union bpf_attr __user *uattr)
1527{
1528 int ufd = attr->info.bpf_fd;
1529 struct fd f;
1530 int err;
1531
1532 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1533 return -EINVAL;
1534
1535 f = fdget(ufd);
1536 if (!f.file)
1537 return -EBADFD;
1538
1539 if (f.file->f_op == &bpf_prog_fops)
1540 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1541 uattr);
1542 else if (f.file->f_op == &bpf_map_fops)
1543 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1544 uattr);
1545 else
1546 err = -EINVAL;
1547
1548 fdput(f);
1549 return err;
1550}
1551
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001552SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1553{
1554 union bpf_attr attr = {};
1555 int err;
1556
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001557 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001558 return -EPERM;
1559
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001560 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1561 if (err)
1562 return err;
1563 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001564
1565 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1566 if (copy_from_user(&attr, uattr, size) != 0)
1567 return -EFAULT;
1568
1569 switch (cmd) {
1570 case BPF_MAP_CREATE:
1571 err = map_create(&attr);
1572 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001573 case BPF_MAP_LOOKUP_ELEM:
1574 err = map_lookup_elem(&attr);
1575 break;
1576 case BPF_MAP_UPDATE_ELEM:
1577 err = map_update_elem(&attr);
1578 break;
1579 case BPF_MAP_DELETE_ELEM:
1580 err = map_delete_elem(&attr);
1581 break;
1582 case BPF_MAP_GET_NEXT_KEY:
1583 err = map_get_next_key(&attr);
1584 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001585 case BPF_PROG_LOAD:
1586 err = bpf_prog_load(&attr);
1587 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001588 case BPF_OBJ_PIN:
1589 err = bpf_obj_pin(&attr);
1590 break;
1591 case BPF_OBJ_GET:
1592 err = bpf_obj_get(&attr);
1593 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001594#ifdef CONFIG_CGROUP_BPF
1595 case BPF_PROG_ATTACH:
1596 err = bpf_prog_attach(&attr);
1597 break;
1598 case BPF_PROG_DETACH:
1599 err = bpf_prog_detach(&attr);
1600 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001601 case BPF_PROG_QUERY:
1602 err = bpf_prog_query(&attr, uattr);
1603 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001604#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001605 case BPF_PROG_TEST_RUN:
1606 err = bpf_prog_test_run(&attr, uattr);
1607 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001608 case BPF_PROG_GET_NEXT_ID:
1609 err = bpf_obj_get_next_id(&attr, uattr,
1610 &prog_idr, &prog_idr_lock);
1611 break;
1612 case BPF_MAP_GET_NEXT_ID:
1613 err = bpf_obj_get_next_id(&attr, uattr,
1614 &map_idr, &map_idr_lock);
1615 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001616 case BPF_PROG_GET_FD_BY_ID:
1617 err = bpf_prog_get_fd_by_id(&attr);
1618 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001619 case BPF_MAP_GET_FD_BY_ID:
1620 err = bpf_map_get_fd_by_id(&attr);
1621 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001622 case BPF_OBJ_GET_INFO_BY_FD:
1623 err = bpf_obj_get_info_by_fd(&attr, uattr);
1624 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001625 default:
1626 err = -EINVAL;
1627 break;
1628 }
1629
1630 return err;
1631}