blob: c0ac03a04880f85b2010dfc92381c113cf7855ed [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
Chenbo Feng6e71b042017-10-18 13:00:22 -070037#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
38
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080039DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070040static DEFINE_IDR(prog_idr);
41static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070042static DEFINE_IDR(map_idr);
43static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080044
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070045int sysctl_unprivileged_bpf_disabled __read_mostly;
46
Johannes Berg40077e02017-04-11 15:34:58 +020047static const struct bpf_map_ops * const bpf_map_types[] = {
48#define BPF_PROG_TYPE(_id, _ops)
49#define BPF_MAP_TYPE(_id, _ops) \
50 [_id] = &_ops,
51#include <linux/bpf_types.h>
52#undef BPF_PROG_TYPE
53#undef BPF_MAP_TYPE
54};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070055
Mickaël Salaün752ba562017-08-07 20:45:20 +020056/*
57 * If we're handed a bigger struct than we know of, ensure all the unknown bits
58 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
59 * we don't know about yet.
60 *
61 * There is a ToCToU between this function call and the following
62 * copy_from_user() call. However, this is not a concern since this function is
63 * meant to be a future-proofing of bits.
64 */
Mickaël Salaün58291a72017-08-07 20:45:19 +020065static int check_uarg_tail_zero(void __user *uaddr,
66 size_t expected_size,
67 size_t actual_size)
68{
69 unsigned char __user *addr;
70 unsigned char __user *end;
71 unsigned char val;
72 int err;
73
Mickaël Salaün752ba562017-08-07 20:45:20 +020074 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
75 return -E2BIG;
76
77 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
78 return -EFAULT;
79
Mickaël Salaün58291a72017-08-07 20:45:19 +020080 if (actual_size <= expected_size)
81 return 0;
82
83 addr = uaddr + expected_size;
84 end = uaddr + actual_size;
85
86 for (; addr < end; addr++) {
87 err = get_user(val, addr);
88 if (err)
89 return err;
90 if (val)
91 return -E2BIG;
92 }
93
94 return 0;
95}
96
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070097static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
98{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -080099 const struct bpf_map_ops *ops;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700100 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800101 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700102
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800103 if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
104 return ERR_PTR(-EINVAL);
105 ops = bpf_map_types[attr->map_type];
106 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200107 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700108
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800109 if (ops->map_alloc_check) {
110 err = ops->map_alloc_check(attr);
111 if (err)
112 return ERR_PTR(err);
113 }
114 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200115 if (IS_ERR(map))
116 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800117 map->ops = ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200118 map->map_type = attr->map_type;
119 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700120}
121
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700122void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100123{
124 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
125 * trigger under memory pressure as we really just want to
126 * fail instead.
127 */
128 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
129 void *area;
130
131 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700132 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100133 if (area != NULL)
134 return area;
135 }
136
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700137 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
138 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100139}
140
141void bpf_map_area_free(void *area)
142{
143 kvfree(area);
144}
145
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800146int bpf_map_precharge_memlock(u32 pages)
147{
148 struct user_struct *user = get_current_user();
149 unsigned long memlock_limit, cur;
150
151 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
152 cur = atomic_long_read(&user->locked_vm);
153 free_uid(user);
154 if (cur + pages > memlock_limit)
155 return -EPERM;
156 return 0;
157}
158
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700159static int bpf_map_charge_memlock(struct bpf_map *map)
160{
161 struct user_struct *user = get_current_user();
162 unsigned long memlock_limit;
163
164 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
165
166 atomic_long_add(map->pages, &user->locked_vm);
167
168 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
169 atomic_long_sub(map->pages, &user->locked_vm);
170 free_uid(user);
171 return -EPERM;
172 }
173 map->user = user;
174 return 0;
175}
176
177static void bpf_map_uncharge_memlock(struct bpf_map *map)
178{
179 struct user_struct *user = map->user;
180
181 atomic_long_sub(map->pages, &user->locked_vm);
182 free_uid(user);
183}
184
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700185static int bpf_map_alloc_id(struct bpf_map *map)
186{
187 int id;
188
189 spin_lock_bh(&map_idr_lock);
190 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
191 if (id > 0)
192 map->id = id;
193 spin_unlock_bh(&map_idr_lock);
194
195 if (WARN_ON_ONCE(!id))
196 return -ENOSPC;
197
198 return id > 0 ? 0 : id;
199}
200
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700201static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700202{
Eric Dumazet930651a2017-09-19 09:15:59 -0700203 unsigned long flags;
204
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700205 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700206 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700207 else
208 __acquire(&map_idr_lock);
209
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700210 idr_remove(&map_idr, map->id);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700211
212 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700213 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700214 else
215 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700216}
217
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700218/* called from workqueue */
219static void bpf_map_free_deferred(struct work_struct *work)
220{
221 struct bpf_map *map = container_of(work, struct bpf_map, work);
222
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700223 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700224 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700225 /* implementation dependent freeing */
226 map->ops->map_free(map);
227}
228
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100229static void bpf_map_put_uref(struct bpf_map *map)
230{
231 if (atomic_dec_and_test(&map->usercnt)) {
232 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
233 bpf_fd_array_map_clear(map);
234 }
235}
236
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700237/* decrement map refcnt and schedule it for freeing via workqueue
238 * (unrelying map implementation ops->map_free() might sleep)
239 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700240static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700241{
242 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700243 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700244 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700245 INIT_WORK(&map->work, bpf_map_free_deferred);
246 schedule_work(&map->work);
247 }
248}
249
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700250void bpf_map_put(struct bpf_map *map)
251{
252 __bpf_map_put(map, true);
253}
254
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100255void bpf_map_put_with_uref(struct bpf_map *map)
256{
257 bpf_map_put_uref(map);
258 bpf_map_put(map);
259}
260
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700261static int bpf_map_release(struct inode *inode, struct file *filp)
262{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200263 struct bpf_map *map = filp->private_data;
264
265 if (map->ops->map_release)
266 map->ops->map_release(map, filp);
267
268 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700269 return 0;
270}
271
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100272#ifdef CONFIG_PROC_FS
273static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
274{
275 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100276 const struct bpf_array *array;
277 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200278 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100279
280 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
281 array = container_of(map, struct bpf_array, map);
282 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200283 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100284 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100285
286 seq_printf(m,
287 "map_type:\t%u\n"
288 "key_size:\t%u\n"
289 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100290 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100291 "map_flags:\t%#x\n"
292 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100293 map->map_type,
294 map->key_size,
295 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100296 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100297 map->map_flags,
298 map->pages * 1ULL << PAGE_SHIFT);
299
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200300 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100301 seq_printf(m, "owner_prog_type:\t%u\n",
302 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200303 seq_printf(m, "owner_jited:\t%u\n",
304 owner_jited);
305 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100306}
307#endif
308
Chenbo Feng6e71b042017-10-18 13:00:22 -0700309static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
310 loff_t *ppos)
311{
312 /* We need this handler such that alloc_file() enables
313 * f_mode with FMODE_CAN_READ.
314 */
315 return -EINVAL;
316}
317
318static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
319 size_t siz, loff_t *ppos)
320{
321 /* We need this handler such that alloc_file() enables
322 * f_mode with FMODE_CAN_WRITE.
323 */
324 return -EINVAL;
325}
326
Chenbo Fengf66e4482017-10-18 13:00:26 -0700327const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100328#ifdef CONFIG_PROC_FS
329 .show_fdinfo = bpf_map_show_fdinfo,
330#endif
331 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700332 .read = bpf_dummy_read,
333 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700334};
335
Chenbo Feng6e71b042017-10-18 13:00:22 -0700336int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100337{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700338 int ret;
339
340 ret = security_bpf_map(map, OPEN_FMODE(flags));
341 if (ret < 0)
342 return ret;
343
Daniel Borkmannaa797812015-10-29 14:58:06 +0100344 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700345 flags | O_CLOEXEC);
346}
347
348int bpf_get_file_flag(int flags)
349{
350 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
351 return -EINVAL;
352 if (flags & BPF_F_RDONLY)
353 return O_RDONLY;
354 if (flags & BPF_F_WRONLY)
355 return O_WRONLY;
356 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100357}
358
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700359/* helper macro to check that unused fields 'union bpf_attr' are zero */
360#define CHECK_ATTR(CMD) \
361 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
362 sizeof(attr->CMD##_LAST_FIELD), 0, \
363 sizeof(*attr) - \
364 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
365 sizeof(attr->CMD##_LAST_FIELD)) != NULL
366
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700367/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
368 * Return 0 on success and < 0 on error.
369 */
370static int bpf_obj_name_cpy(char *dst, const char *src)
371{
372 const char *end = src + BPF_OBJ_NAME_LEN;
373
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700374 memset(dst, 0, BPF_OBJ_NAME_LEN);
375
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700376 /* Copy all isalnum() and '_' char */
377 while (src < end && *src) {
378 if (!isalnum(*src) && *src != '_')
379 return -EINVAL;
380 *dst++ = *src++;
381 }
382
383 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
384 if (src == end)
385 return -EINVAL;
386
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700387 return 0;
388}
389
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700390#define BPF_MAP_CREATE_LAST_FIELD map_name
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700391/* called via syscall */
392static int map_create(union bpf_attr *attr)
393{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700394 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700395 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700396 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700397 int err;
398
399 err = CHECK_ATTR(BPF_MAP_CREATE);
400 if (err)
401 return -EINVAL;
402
Chenbo Feng6e71b042017-10-18 13:00:22 -0700403 f_flags = bpf_get_file_flag(attr->map_flags);
404 if (f_flags < 0)
405 return f_flags;
406
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700407 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700408 ((unsigned int)numa_node >= nr_node_ids ||
409 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700410 return -EINVAL;
411
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700412 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
413 map = find_and_alloc_map(attr);
414 if (IS_ERR(map))
415 return PTR_ERR(map);
416
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700417 err = bpf_obj_name_cpy(map->name, attr->map_name);
418 if (err)
419 goto free_map_nouncharge;
420
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700421 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100422 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700423
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700424 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700425 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100426 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700427
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700428 err = bpf_map_charge_memlock(map);
429 if (err)
430 goto free_map_sec;
431
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700432 err = bpf_map_alloc_id(map);
433 if (err)
434 goto free_map;
435
Chenbo Feng6e71b042017-10-18 13:00:22 -0700436 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700437 if (err < 0) {
438 /* failed to allocate fd.
439 * bpf_map_put() is needed because the above
440 * bpf_map_alloc_id() has published the map
441 * to the userspace and the userspace may
442 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
443 */
444 bpf_map_put(map);
445 return err;
446 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700447
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100448 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700449 return err;
450
451free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100452 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700453free_map_sec:
454 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100455free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700456 map->ops->map_free(map);
457 return err;
458}
459
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700460/* if error is returned, fd is released.
461 * On success caller should complete fd access with matching fdput()
462 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100463struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700464{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700465 if (!f.file)
466 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700467 if (f.file->f_op != &bpf_map_fops) {
468 fdput(f);
469 return ERR_PTR(-EINVAL);
470 }
471
Daniel Borkmannc2101292015-10-29 14:58:07 +0100472 return f.file->private_data;
473}
474
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700475/* prog's and map's refcnt limit */
476#define BPF_MAX_REFCNT 32768
477
478struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100479{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700480 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
481 atomic_dec(&map->refcnt);
482 return ERR_PTR(-EBUSY);
483 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100484 if (uref)
485 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700486 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100487}
488
489struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100490{
491 struct fd f = fdget(ufd);
492 struct bpf_map *map;
493
494 map = __bpf_map_get(f);
495 if (IS_ERR(map))
496 return map;
497
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700498 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100499 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700500
501 return map;
502}
503
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700504/* map_idr_lock should have been held */
505static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
506 bool uref)
507{
508 int refold;
509
510 refold = __atomic_add_unless(&map->refcnt, 1, 0);
511
512 if (refold >= BPF_MAX_REFCNT) {
513 __bpf_map_put(map, false);
514 return ERR_PTR(-EBUSY);
515 }
516
517 if (!refold)
518 return ERR_PTR(-ENOENT);
519
520 if (uref)
521 atomic_inc(&map->usercnt);
522
523 return map;
524}
525
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800526int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
527{
528 return -ENOTSUPP;
529}
530
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700531/* last field in 'union bpf_attr' used by this command */
532#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
533
534static int map_lookup_elem(union bpf_attr *attr)
535{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100536 void __user *ukey = u64_to_user_ptr(attr->key);
537 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700538 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700539 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800540 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800541 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200542 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700543 int err;
544
545 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
546 return -EINVAL;
547
Daniel Borkmann592867b2015-09-08 18:00:09 +0200548 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100549 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700550 if (IS_ERR(map))
551 return PTR_ERR(map);
552
Chenbo Feng6e71b042017-10-18 13:00:22 -0700553 if (!(f.file->f_mode & FMODE_CAN_READ)) {
554 err = -EPERM;
555 goto err_put;
556 }
557
Al Viroe4448ed2017-05-13 18:43:00 -0400558 key = memdup_user(ukey, map->key_size);
559 if (IS_ERR(key)) {
560 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700561 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400562 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700563
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800564 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800565 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800566 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
567 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700568 else if (IS_FD_MAP(map))
569 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800570 else
571 value_size = map->value_size;
572
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800573 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800574 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700575 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800576 goto free_key;
577
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800578 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
579 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800580 err = bpf_percpu_hash_copy(map, key, value);
581 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
582 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800583 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
584 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700585 } else if (IS_FD_ARRAY(map)) {
586 err = bpf_fd_array_map_lookup_elem(map, key, value);
587 } else if (IS_FD_HASH(map)) {
588 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800589 } else {
590 rcu_read_lock();
591 ptr = map->ops->map_lookup_elem(map, key);
592 if (ptr)
593 memcpy(value, ptr, value_size);
594 rcu_read_unlock();
595 err = ptr ? 0 : -ENOENT;
596 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800597
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800598 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800599 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700600
601 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800602 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800603 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700604
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100605 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700606 err = 0;
607
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800608free_value:
609 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700610free_key:
611 kfree(key);
612err_put:
613 fdput(f);
614 return err;
615}
616
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800617#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700618
619static int map_update_elem(union bpf_attr *attr)
620{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100621 void __user *ukey = u64_to_user_ptr(attr->key);
622 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700623 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700624 struct bpf_map *map;
625 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800626 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200627 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700628 int err;
629
630 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
631 return -EINVAL;
632
Daniel Borkmann592867b2015-09-08 18:00:09 +0200633 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100634 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700635 if (IS_ERR(map))
636 return PTR_ERR(map);
637
Chenbo Feng6e71b042017-10-18 13:00:22 -0700638 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
639 err = -EPERM;
640 goto err_put;
641 }
642
Al Viroe4448ed2017-05-13 18:43:00 -0400643 key = memdup_user(ukey, map->key_size);
644 if (IS_ERR(key)) {
645 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700646 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400647 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700648
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800649 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800650 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800651 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
652 value_size = round_up(map->value_size, 8) * num_possible_cpus();
653 else
654 value_size = map->value_size;
655
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700656 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800657 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700658 if (!value)
659 goto free_key;
660
661 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800662 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700663 goto free_value;
664
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200665 /* Need to create a kthread, thus must support schedule */
666 if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
667 err = map->ops->map_update_elem(map, key, value, attr->flags);
668 goto out;
669 }
670
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800671 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
672 * inside bpf map update or delete otherwise deadlocks are possible
673 */
674 preempt_disable();
675 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800676 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
677 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800678 err = bpf_percpu_hash_update(map, key, value, attr->flags);
679 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
680 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200681 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700682 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700683 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
684 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200685 rcu_read_lock();
686 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
687 attr->flags);
688 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700689 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
690 rcu_read_lock();
691 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
692 attr->flags);
693 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800694 } else {
695 rcu_read_lock();
696 err = map->ops->map_update_elem(map, key, value, attr->flags);
697 rcu_read_unlock();
698 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800699 __this_cpu_dec(bpf_prog_active);
700 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200701out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100702 if (!err)
703 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700704free_value:
705 kfree(value);
706free_key:
707 kfree(key);
708err_put:
709 fdput(f);
710 return err;
711}
712
713#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
714
715static int map_delete_elem(union bpf_attr *attr)
716{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100717 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700718 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700719 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200720 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700721 void *key;
722 int err;
723
724 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
725 return -EINVAL;
726
Daniel Borkmann592867b2015-09-08 18:00:09 +0200727 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100728 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700729 if (IS_ERR(map))
730 return PTR_ERR(map);
731
Chenbo Feng6e71b042017-10-18 13:00:22 -0700732 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
733 err = -EPERM;
734 goto err_put;
735 }
736
Al Viroe4448ed2017-05-13 18:43:00 -0400737 key = memdup_user(ukey, map->key_size);
738 if (IS_ERR(key)) {
739 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700740 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400741 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700742
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800743 preempt_disable();
744 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700745 rcu_read_lock();
746 err = map->ops->map_delete_elem(map, key);
747 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800748 __this_cpu_dec(bpf_prog_active);
749 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700750
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100751 if (!err)
752 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700753 kfree(key);
754err_put:
755 fdput(f);
756 return err;
757}
758
759/* last field in 'union bpf_attr' used by this command */
760#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
761
762static int map_get_next_key(union bpf_attr *attr)
763{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100764 void __user *ukey = u64_to_user_ptr(attr->key);
765 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700766 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700767 struct bpf_map *map;
768 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200769 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700770 int err;
771
772 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
773 return -EINVAL;
774
Daniel Borkmann592867b2015-09-08 18:00:09 +0200775 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100776 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700777 if (IS_ERR(map))
778 return PTR_ERR(map);
779
Chenbo Feng6e71b042017-10-18 13:00:22 -0700780 if (!(f.file->f_mode & FMODE_CAN_READ)) {
781 err = -EPERM;
782 goto err_put;
783 }
784
Teng Qin8fe45922017-04-24 19:00:37 -0700785 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400786 key = memdup_user(ukey, map->key_size);
787 if (IS_ERR(key)) {
788 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700789 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400790 }
Teng Qin8fe45922017-04-24 19:00:37 -0700791 } else {
792 key = NULL;
793 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700794
795 err = -ENOMEM;
796 next_key = kmalloc(map->key_size, GFP_USER);
797 if (!next_key)
798 goto free_key;
799
800 rcu_read_lock();
801 err = map->ops->map_get_next_key(map, key, next_key);
802 rcu_read_unlock();
803 if (err)
804 goto free_next_key;
805
806 err = -EFAULT;
807 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
808 goto free_next_key;
809
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100810 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700811 err = 0;
812
813free_next_key:
814 kfree(next_key);
815free_key:
816 kfree(key);
817err_put:
818 fdput(f);
819 return err;
820}
821
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700822static const struct bpf_prog_ops * const bpf_prog_types[] = {
823#define BPF_PROG_TYPE(_id, _name) \
824 [_id] = & _name ## _prog_ops,
825#define BPF_MAP_TYPE(_id, _ops)
826#include <linux/bpf_types.h>
827#undef BPF_PROG_TYPE
828#undef BPF_MAP_TYPE
829};
830
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700831static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
832{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200833 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
834 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700835
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700836 if (!bpf_prog_is_dev_bound(prog->aux))
837 prog->aux->ops = bpf_prog_types[type];
838 else
839 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200840 prog->type = type;
841 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700842}
843
844/* drop refcnt on maps used by eBPF program and free auxilary data */
845static void free_used_maps(struct bpf_prog_aux *aux)
846{
847 int i;
848
849 for (i = 0; i < aux->used_map_cnt; i++)
850 bpf_map_put(aux->used_maps[i]);
851
852 kfree(aux->used_maps);
853}
854
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100855int __bpf_prog_charge(struct user_struct *user, u32 pages)
856{
857 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
858 unsigned long user_bufs;
859
860 if (user) {
861 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
862 if (user_bufs > memlock_limit) {
863 atomic_long_sub(pages, &user->locked_vm);
864 return -EPERM;
865 }
866 }
867
868 return 0;
869}
870
871void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
872{
873 if (user)
874 atomic_long_sub(pages, &user->locked_vm);
875}
876
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700877static int bpf_prog_charge_memlock(struct bpf_prog *prog)
878{
879 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100880 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700881
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100882 ret = __bpf_prog_charge(user, prog->pages);
883 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700884 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100885 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700886 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100887
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700888 prog->aux->user = user;
889 return 0;
890}
891
892static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
893{
894 struct user_struct *user = prog->aux->user;
895
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100896 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700897 free_uid(user);
898}
899
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700900static int bpf_prog_alloc_id(struct bpf_prog *prog)
901{
902 int id;
903
904 spin_lock_bh(&prog_idr_lock);
905 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
906 if (id > 0)
907 prog->aux->id = id;
908 spin_unlock_bh(&prog_idr_lock);
909
910 /* id is in [1, INT_MAX) */
911 if (WARN_ON_ONCE(!id))
912 return -ENOSPC;
913
914 return id > 0 ? 0 : id;
915}
916
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800917void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700918{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800919 /* cBPF to eBPF migrations are currently not in the idr store.
920 * Offloaded programs are removed from the store when their device
921 * disappears - even if someone grabs an fd to them they are unusable,
922 * simply waiting for refcnt to drop to be freed.
923 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700924 if (!prog->aux->id)
925 return;
926
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700927 if (do_idr_lock)
928 spin_lock_bh(&prog_idr_lock);
929 else
930 __acquire(&prog_idr_lock);
931
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700932 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800933 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700934
935 if (do_idr_lock)
936 spin_unlock_bh(&prog_idr_lock);
937 else
938 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700939}
940
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200941static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700942{
943 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
944
945 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700946 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700947 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700948 bpf_prog_free(aux->prog);
949}
950
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700951static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700952{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100953 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +0100954 int i;
955
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100956 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700957 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700958 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +0100959
960 for (i = 0; i < prog->aux->func_cnt; i++)
961 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100962 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +0100963
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200964 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100965 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700966}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700967
968void bpf_prog_put(struct bpf_prog *prog)
969{
970 __bpf_prog_put(prog, true);
971}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100972EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700973
974static int bpf_prog_release(struct inode *inode, struct file *filp)
975{
976 struct bpf_prog *prog = filp->private_data;
977
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200978 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700979 return 0;
980}
981
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100982#ifdef CONFIG_PROC_FS
983static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
984{
985 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100986 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100987
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100988 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100989 seq_printf(m,
990 "prog_type:\t%u\n"
991 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100992 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100993 "memlock:\t%llu\n",
994 prog->type,
995 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100996 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100997 prog->pages * 1ULL << PAGE_SHIFT);
998}
999#endif
1000
Chenbo Fengf66e4482017-10-18 13:00:26 -07001001const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001002#ifdef CONFIG_PROC_FS
1003 .show_fdinfo = bpf_prog_show_fdinfo,
1004#endif
1005 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001006 .read = bpf_dummy_read,
1007 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001008};
1009
Daniel Borkmannb2197752015-10-29 14:58:09 +01001010int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001011{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001012 int ret;
1013
1014 ret = security_bpf_prog(prog);
1015 if (ret < 0)
1016 return ret;
1017
Daniel Borkmannaa797812015-10-29 14:58:06 +01001018 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1019 O_RDWR | O_CLOEXEC);
1020}
1021
Daniel Borkmann113214b2016-06-30 17:24:44 +02001022static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001023{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001024 if (!f.file)
1025 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001026 if (f.file->f_op != &bpf_prog_fops) {
1027 fdput(f);
1028 return ERR_PTR(-EINVAL);
1029 }
1030
Daniel Borkmannc2101292015-10-29 14:58:07 +01001031 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001032}
1033
Brenden Blanco59d36562016-07-19 12:16:46 -07001034struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001035{
Brenden Blanco59d36562016-07-19 12:16:46 -07001036 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1037 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001038 return ERR_PTR(-EBUSY);
1039 }
1040 return prog;
1041}
Brenden Blanco59d36562016-07-19 12:16:46 -07001042EXPORT_SYMBOL_GPL(bpf_prog_add);
1043
Daniel Borkmannc5405942016-11-09 22:02:34 +01001044void bpf_prog_sub(struct bpf_prog *prog, int i)
1045{
1046 /* Only to be used for undoing previous bpf_prog_add() in some
1047 * error path. We still know that another entity in our call
1048 * path holds a reference to the program, thus atomic_sub() can
1049 * be safely used in such cases!
1050 */
1051 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1052}
1053EXPORT_SYMBOL_GPL(bpf_prog_sub);
1054
Brenden Blanco59d36562016-07-19 12:16:46 -07001055struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1056{
1057 return bpf_prog_add(prog, 1);
1058}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001059EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001060
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001061/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001062struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001063{
1064 int refold;
1065
1066 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1067
1068 if (refold >= BPF_MAX_REFCNT) {
1069 __bpf_prog_put(prog, false);
1070 return ERR_PTR(-EBUSY);
1071 }
1072
1073 if (!refold)
1074 return ERR_PTR(-ENOENT);
1075
1076 return prog;
1077}
John Fastabenda6f6df62017-08-15 22:32:22 -07001078EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001079
Al Viro040ee692017-12-02 20:20:38 -05001080bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001081 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001082{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001083 /* not an attachment, just a refcount inc, always allow */
1084 if (!attach_type)
1085 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001086
1087 if (prog->type != *attach_type)
1088 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001089 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001090 return false;
1091
1092 return true;
1093}
1094
1095static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001096 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001097{
1098 struct fd f = fdget(ufd);
1099 struct bpf_prog *prog;
1100
Daniel Borkmann113214b2016-06-30 17:24:44 +02001101 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001102 if (IS_ERR(prog))
1103 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001104 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001105 prog = ERR_PTR(-EINVAL);
1106 goto out;
1107 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001108
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001109 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001110out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001111 fdput(f);
1112 return prog;
1113}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001114
1115struct bpf_prog *bpf_prog_get(u32 ufd)
1116{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001117 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001118}
1119
Jakub Kicinski248f3462017-11-03 13:56:20 -07001120struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001121 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001122{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001123 struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001124
1125 if (!IS_ERR(prog))
1126 trace_bpf_prog_get_type(prog);
1127 return prog;
1128}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001129EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001130
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001131/* last field in 'union bpf_attr' used by this command */
Jakub Kicinski1f6f4cb2017-11-20 15:21:53 -08001132#define BPF_PROG_LOAD_LAST_FIELD prog_ifindex
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001133
1134static int bpf_prog_load(union bpf_attr *attr)
1135{
1136 enum bpf_prog_type type = attr->prog_type;
1137 struct bpf_prog *prog;
1138 int err;
1139 char license[128];
1140 bool is_gpl;
1141
1142 if (CHECK_ATTR(BPF_PROG_LOAD))
1143 return -EINVAL;
1144
David S. Millere07b98d2017-05-10 11:38:07 -07001145 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1146 return -EINVAL;
1147
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001148 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001149 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001150 sizeof(license) - 1) < 0)
1151 return -EFAULT;
1152 license[sizeof(license) - 1] = 0;
1153
1154 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1155 is_gpl = license_is_gpl_compatible(license);
1156
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001157 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1158 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001159
Alexei Starovoitov25415172015-03-25 12:49:20 -07001160 if (type == BPF_PROG_TYPE_KPROBE &&
1161 attr->kern_version != LINUX_VERSION_CODE)
1162 return -EINVAL;
1163
Chenbo Feng80b7d812017-05-31 18:16:00 -07001164 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1165 type != BPF_PROG_TYPE_CGROUP_SKB &&
1166 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001167 return -EPERM;
1168
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001169 /* plain bpf_prog allocation */
1170 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1171 if (!prog)
1172 return -ENOMEM;
1173
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001174 prog->aux->offload_requested = !!attr->prog_ifindex;
1175
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001176 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001177 if (err)
1178 goto free_prog_nouncharge;
1179
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001180 err = bpf_prog_charge_memlock(prog);
1181 if (err)
1182 goto free_prog_sec;
1183
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001184 prog->len = attr->insn_cnt;
1185
1186 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001187 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001188 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001189 goto free_prog;
1190
1191 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001192 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001193
1194 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001195 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001196
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001197 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001198 err = bpf_prog_offload_init(prog, attr);
1199 if (err)
1200 goto free_prog;
1201 }
1202
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001203 /* find program type: socket_filter vs tracing_filter */
1204 err = find_prog_type(type, prog);
1205 if (err < 0)
1206 goto free_prog;
1207
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001208 prog->aux->load_time = ktime_get_boot_ns();
1209 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1210 if (err)
1211 goto free_prog;
1212
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001213 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001214 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001215 if (err < 0)
1216 goto free_used_maps;
1217
1218 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001219 if (!prog->bpf_func)
1220 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001221 if (err < 0)
1222 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001223
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001224 err = bpf_prog_alloc_id(prog);
1225 if (err)
1226 goto free_used_maps;
1227
Daniel Borkmannaa797812015-10-29 14:58:06 +01001228 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001229 if (err < 0) {
1230 /* failed to allocate fd.
1231 * bpf_prog_put() is needed because the above
1232 * bpf_prog_alloc_id() has published the prog
1233 * to the userspace and the userspace may
1234 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1235 */
1236 bpf_prog_put(prog);
1237 return err;
1238 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001239
Daniel Borkmann74451e662017-02-16 22:24:50 +01001240 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001241 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001242 return err;
1243
1244free_used_maps:
1245 free_used_maps(prog->aux);
1246free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001247 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001248free_prog_sec:
1249 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001250free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001251 bpf_prog_free(prog);
1252 return err;
1253}
1254
Chenbo Feng6e71b042017-10-18 13:00:22 -07001255#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001256
1257static int bpf_obj_pin(const union bpf_attr *attr)
1258{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001259 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001260 return -EINVAL;
1261
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001262 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001263}
1264
1265static int bpf_obj_get(const union bpf_attr *attr)
1266{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001267 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1268 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001269 return -EINVAL;
1270
Chenbo Feng6e71b042017-10-18 13:00:22 -07001271 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1272 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001273}
1274
Daniel Mackf4324552016-11-23 16:52:27 +01001275#ifdef CONFIG_CGROUP_BPF
1276
John Fastabend464bc0f2017-08-28 07:10:04 -07001277#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001278
John Fastabend5a67da22017-09-08 14:00:49 -07001279static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001280{
John Fastabend5a67da22017-09-08 14:00:49 -07001281 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001282 int ufd = attr->target_fd;
1283 struct bpf_map *map;
1284 struct fd f;
1285 int err;
1286
1287 f = fdget(ufd);
1288 map = __bpf_map_get(f);
1289 if (IS_ERR(map))
1290 return PTR_ERR(map);
1291
John Fastabend5a67da22017-09-08 14:00:49 -07001292 if (attach) {
1293 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1294 BPF_PROG_TYPE_SK_SKB);
1295 if (IS_ERR(prog)) {
1296 fdput(f);
1297 return PTR_ERR(prog);
1298 }
John Fastabend174a79f2017-08-15 22:32:47 -07001299 }
1300
John Fastabend5a67da22017-09-08 14:00:49 -07001301 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001302 if (err) {
1303 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001304 if (prog)
1305 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001306 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001307 }
1308
1309 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001310 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001311}
Daniel Mackf4324552016-11-23 16:52:27 +01001312
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001313#define BPF_F_ATTACH_MASK \
1314 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1315
Daniel Mackf4324552016-11-23 16:52:27 +01001316static int bpf_prog_attach(const union bpf_attr *attr)
1317{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001318 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001319 struct bpf_prog *prog;
1320 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001321 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001322
1323 if (!capable(CAP_NET_ADMIN))
1324 return -EPERM;
1325
1326 if (CHECK_ATTR(BPF_PROG_ATTACH))
1327 return -EINVAL;
1328
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001329 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001330 return -EINVAL;
1331
Daniel Mackf4324552016-11-23 16:52:27 +01001332 switch (attr->attach_type) {
1333 case BPF_CGROUP_INET_INGRESS:
1334 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001335 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001336 break;
David Ahern610236582016-12-01 08:48:04 -08001337 case BPF_CGROUP_INET_SOCK_CREATE:
1338 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1339 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001340 case BPF_CGROUP_SOCK_OPS:
1341 ptype = BPF_PROG_TYPE_SOCK_OPS;
1342 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001343 case BPF_CGROUP_DEVICE:
1344 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1345 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001346 case BPF_SK_SKB_STREAM_PARSER:
1347 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend5a67da22017-09-08 14:00:49 -07001348 return sockmap_get_from_fd(attr, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001349 default:
1350 return -EINVAL;
1351 }
1352
David Ahernb2cd1252016-12-01 08:48:03 -08001353 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1354 if (IS_ERR(prog))
1355 return PTR_ERR(prog);
1356
1357 cgrp = cgroup_get_from_fd(attr->target_fd);
1358 if (IS_ERR(cgrp)) {
1359 bpf_prog_put(prog);
1360 return PTR_ERR(cgrp);
1361 }
1362
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001363 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1364 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001365 if (ret)
1366 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001367 cgroup_put(cgrp);
1368
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001369 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001370}
1371
1372#define BPF_PROG_DETACH_LAST_FIELD attach_type
1373
1374static int bpf_prog_detach(const union bpf_attr *attr)
1375{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001376 enum bpf_prog_type ptype;
1377 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001378 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001379 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001380
1381 if (!capable(CAP_NET_ADMIN))
1382 return -EPERM;
1383
1384 if (CHECK_ATTR(BPF_PROG_DETACH))
1385 return -EINVAL;
1386
1387 switch (attr->attach_type) {
1388 case BPF_CGROUP_INET_INGRESS:
1389 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001390 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1391 break;
David Ahern610236582016-12-01 08:48:04 -08001392 case BPF_CGROUP_INET_SOCK_CREATE:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001393 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1394 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001395 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001396 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001397 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001398 case BPF_CGROUP_DEVICE:
1399 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1400 break;
John Fastabend5a67da22017-09-08 14:00:49 -07001401 case BPF_SK_SKB_STREAM_PARSER:
1402 case BPF_SK_SKB_STREAM_VERDICT:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001403 return sockmap_get_from_fd(attr, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001404 default:
1405 return -EINVAL;
1406 }
1407
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001408 cgrp = cgroup_get_from_fd(attr->target_fd);
1409 if (IS_ERR(cgrp))
1410 return PTR_ERR(cgrp);
1411
1412 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1413 if (IS_ERR(prog))
1414 prog = NULL;
1415
1416 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1417 if (prog)
1418 bpf_prog_put(prog);
1419 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001420 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001421}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001422
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001423#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1424
1425static int bpf_prog_query(const union bpf_attr *attr,
1426 union bpf_attr __user *uattr)
1427{
1428 struct cgroup *cgrp;
1429 int ret;
1430
1431 if (!capable(CAP_NET_ADMIN))
1432 return -EPERM;
1433 if (CHECK_ATTR(BPF_PROG_QUERY))
1434 return -EINVAL;
1435 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1436 return -EINVAL;
1437
1438 switch (attr->query.attach_type) {
1439 case BPF_CGROUP_INET_INGRESS:
1440 case BPF_CGROUP_INET_EGRESS:
1441 case BPF_CGROUP_INET_SOCK_CREATE:
1442 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001443 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001444 break;
1445 default:
1446 return -EINVAL;
1447 }
1448 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1449 if (IS_ERR(cgrp))
1450 return PTR_ERR(cgrp);
1451 ret = cgroup_bpf_query(cgrp, attr, uattr);
1452 cgroup_put(cgrp);
1453 return ret;
1454}
Daniel Mackf4324552016-11-23 16:52:27 +01001455#endif /* CONFIG_CGROUP_BPF */
1456
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001457#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1458
1459static int bpf_prog_test_run(const union bpf_attr *attr,
1460 union bpf_attr __user *uattr)
1461{
1462 struct bpf_prog *prog;
1463 int ret = -ENOTSUPP;
1464
1465 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1466 return -EINVAL;
1467
1468 prog = bpf_prog_get(attr->test.prog_fd);
1469 if (IS_ERR(prog))
1470 return PTR_ERR(prog);
1471
1472 if (prog->aux->ops->test_run)
1473 ret = prog->aux->ops->test_run(prog, attr, uattr);
1474
1475 bpf_prog_put(prog);
1476 return ret;
1477}
1478
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001479#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1480
1481static int bpf_obj_get_next_id(const union bpf_attr *attr,
1482 union bpf_attr __user *uattr,
1483 struct idr *idr,
1484 spinlock_t *lock)
1485{
1486 u32 next_id = attr->start_id;
1487 int err = 0;
1488
1489 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1490 return -EINVAL;
1491
1492 if (!capable(CAP_SYS_ADMIN))
1493 return -EPERM;
1494
1495 next_id++;
1496 spin_lock_bh(lock);
1497 if (!idr_get_next(idr, &next_id))
1498 err = -ENOENT;
1499 spin_unlock_bh(lock);
1500
1501 if (!err)
1502 err = put_user(next_id, &uattr->next_id);
1503
1504 return err;
1505}
1506
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001507#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1508
1509static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1510{
1511 struct bpf_prog *prog;
1512 u32 id = attr->prog_id;
1513 int fd;
1514
1515 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1516 return -EINVAL;
1517
1518 if (!capable(CAP_SYS_ADMIN))
1519 return -EPERM;
1520
1521 spin_lock_bh(&prog_idr_lock);
1522 prog = idr_find(&prog_idr, id);
1523 if (prog)
1524 prog = bpf_prog_inc_not_zero(prog);
1525 else
1526 prog = ERR_PTR(-ENOENT);
1527 spin_unlock_bh(&prog_idr_lock);
1528
1529 if (IS_ERR(prog))
1530 return PTR_ERR(prog);
1531
1532 fd = bpf_prog_new_fd(prog);
1533 if (fd < 0)
1534 bpf_prog_put(prog);
1535
1536 return fd;
1537}
1538
Chenbo Feng6e71b042017-10-18 13:00:22 -07001539#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001540
1541static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1542{
1543 struct bpf_map *map;
1544 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001545 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001546 int fd;
1547
Chenbo Feng6e71b042017-10-18 13:00:22 -07001548 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1549 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001550 return -EINVAL;
1551
1552 if (!capable(CAP_SYS_ADMIN))
1553 return -EPERM;
1554
Chenbo Feng6e71b042017-10-18 13:00:22 -07001555 f_flags = bpf_get_file_flag(attr->open_flags);
1556 if (f_flags < 0)
1557 return f_flags;
1558
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001559 spin_lock_bh(&map_idr_lock);
1560 map = idr_find(&map_idr, id);
1561 if (map)
1562 map = bpf_map_inc_not_zero(map, true);
1563 else
1564 map = ERR_PTR(-ENOENT);
1565 spin_unlock_bh(&map_idr_lock);
1566
1567 if (IS_ERR(map))
1568 return PTR_ERR(map);
1569
Chenbo Feng6e71b042017-10-18 13:00:22 -07001570 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001571 if (fd < 0)
1572 bpf_map_put(map);
1573
1574 return fd;
1575}
1576
Daniel Borkmann7105e822017-12-20 13:42:57 +01001577static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1578 unsigned long addr)
1579{
1580 int i;
1581
1582 for (i = 0; i < prog->aux->used_map_cnt; i++)
1583 if (prog->aux->used_maps[i] == (void *)addr)
1584 return prog->aux->used_maps[i];
1585 return NULL;
1586}
1587
1588static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1589{
1590 const struct bpf_map *map;
1591 struct bpf_insn *insns;
1592 u64 imm;
1593 int i;
1594
1595 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1596 GFP_USER);
1597 if (!insns)
1598 return insns;
1599
1600 for (i = 0; i < prog->len; i++) {
1601 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1602 insns[i].code = BPF_JMP | BPF_CALL;
1603 insns[i].imm = BPF_FUNC_tail_call;
1604 /* fall-through */
1605 }
1606 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1607 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1608 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1609 insns[i].code = BPF_JMP | BPF_CALL;
1610 if (!bpf_dump_raw_ok())
1611 insns[i].imm = 0;
1612 continue;
1613 }
1614
1615 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1616 continue;
1617
1618 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1619 map = bpf_map_from_imm(prog, imm);
1620 if (map) {
1621 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1622 insns[i].imm = map->id;
1623 insns[i + 1].imm = 0;
1624 continue;
1625 }
1626
1627 if (!bpf_dump_raw_ok() &&
1628 imm == (unsigned long)prog->aux) {
1629 insns[i].imm = 0;
1630 insns[i + 1].imm = 0;
1631 continue;
1632 }
1633 }
1634
1635 return insns;
1636}
1637
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001638static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1639 const union bpf_attr *attr,
1640 union bpf_attr __user *uattr)
1641{
1642 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1643 struct bpf_prog_info info = {};
1644 u32 info_len = attr->info.info_len;
1645 char __user *uinsns;
1646 u32 ulen;
1647 int err;
1648
1649 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1650 if (err)
1651 return err;
1652 info_len = min_t(u32, sizeof(info), info_len);
1653
1654 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001655 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001656
1657 info.type = prog->type;
1658 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001659 info.load_time = prog->aux->load_time;
1660 info.created_by_uid = from_kuid_munged(current_user_ns(),
1661 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001662
1663 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001664 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1665
1666 ulen = info.nr_map_ids;
1667 info.nr_map_ids = prog->aux->used_map_cnt;
1668 ulen = min_t(u32, info.nr_map_ids, ulen);
1669 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001670 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001671 u32 i;
1672
1673 for (i = 0; i < ulen; i++)
1674 if (put_user(prog->aux->used_maps[i]->id,
1675 &user_map_ids[i]))
1676 return -EFAULT;
1677 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001678
1679 if (!capable(CAP_SYS_ADMIN)) {
1680 info.jited_prog_len = 0;
1681 info.xlated_prog_len = 0;
1682 goto done;
1683 }
1684
1685 ulen = info.jited_prog_len;
1686 info.jited_prog_len = prog->jited_len;
1687 if (info.jited_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001688 if (bpf_dump_raw_ok()) {
1689 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1690 ulen = min_t(u32, info.jited_prog_len, ulen);
1691 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1692 return -EFAULT;
1693 } else {
1694 info.jited_prog_insns = 0;
1695 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001696 }
1697
1698 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001699 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001700 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001701 struct bpf_insn *insns_sanitized;
1702 bool fault;
1703
1704 if (prog->blinded && !bpf_dump_raw_ok()) {
1705 info.xlated_prog_insns = 0;
1706 goto done;
1707 }
1708 insns_sanitized = bpf_insn_prepare_dump(prog);
1709 if (!insns_sanitized)
1710 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001711 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1712 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001713 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1714 kfree(insns_sanitized);
1715 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001716 return -EFAULT;
1717 }
1718
Jakub Kicinski675fc272017-12-27 18:39:09 -08001719 if (bpf_prog_is_dev_bound(prog->aux)) {
1720 err = bpf_prog_offload_info_fill(&info, prog);
1721 if (err)
1722 return err;
1723 }
1724
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001725done:
1726 if (copy_to_user(uinfo, &info, info_len) ||
1727 put_user(info_len, &uattr->info.info_len))
1728 return -EFAULT;
1729
1730 return 0;
1731}
1732
1733static int bpf_map_get_info_by_fd(struct bpf_map *map,
1734 const union bpf_attr *attr,
1735 union bpf_attr __user *uattr)
1736{
1737 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1738 struct bpf_map_info info = {};
1739 u32 info_len = attr->info.info_len;
1740 int err;
1741
1742 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1743 if (err)
1744 return err;
1745 info_len = min_t(u32, sizeof(info), info_len);
1746
1747 info.type = map->map_type;
1748 info.id = map->id;
1749 info.key_size = map->key_size;
1750 info.value_size = map->value_size;
1751 info.max_entries = map->max_entries;
1752 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001753 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001754
1755 if (copy_to_user(uinfo, &info, info_len) ||
1756 put_user(info_len, &uattr->info.info_len))
1757 return -EFAULT;
1758
1759 return 0;
1760}
1761
1762#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1763
1764static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1765 union bpf_attr __user *uattr)
1766{
1767 int ufd = attr->info.bpf_fd;
1768 struct fd f;
1769 int err;
1770
1771 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1772 return -EINVAL;
1773
1774 f = fdget(ufd);
1775 if (!f.file)
1776 return -EBADFD;
1777
1778 if (f.file->f_op == &bpf_prog_fops)
1779 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1780 uattr);
1781 else if (f.file->f_op == &bpf_map_fops)
1782 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1783 uattr);
1784 else
1785 err = -EINVAL;
1786
1787 fdput(f);
1788 return err;
1789}
1790
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001791SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1792{
1793 union bpf_attr attr = {};
1794 int err;
1795
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001796 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001797 return -EPERM;
1798
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001799 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1800 if (err)
1801 return err;
1802 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001803
1804 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1805 if (copy_from_user(&attr, uattr, size) != 0)
1806 return -EFAULT;
1807
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001808 err = security_bpf(cmd, &attr, size);
1809 if (err < 0)
1810 return err;
1811
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001812 switch (cmd) {
1813 case BPF_MAP_CREATE:
1814 err = map_create(&attr);
1815 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001816 case BPF_MAP_LOOKUP_ELEM:
1817 err = map_lookup_elem(&attr);
1818 break;
1819 case BPF_MAP_UPDATE_ELEM:
1820 err = map_update_elem(&attr);
1821 break;
1822 case BPF_MAP_DELETE_ELEM:
1823 err = map_delete_elem(&attr);
1824 break;
1825 case BPF_MAP_GET_NEXT_KEY:
1826 err = map_get_next_key(&attr);
1827 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001828 case BPF_PROG_LOAD:
1829 err = bpf_prog_load(&attr);
1830 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001831 case BPF_OBJ_PIN:
1832 err = bpf_obj_pin(&attr);
1833 break;
1834 case BPF_OBJ_GET:
1835 err = bpf_obj_get(&attr);
1836 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001837#ifdef CONFIG_CGROUP_BPF
1838 case BPF_PROG_ATTACH:
1839 err = bpf_prog_attach(&attr);
1840 break;
1841 case BPF_PROG_DETACH:
1842 err = bpf_prog_detach(&attr);
1843 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001844 case BPF_PROG_QUERY:
1845 err = bpf_prog_query(&attr, uattr);
1846 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001847#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001848 case BPF_PROG_TEST_RUN:
1849 err = bpf_prog_test_run(&attr, uattr);
1850 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001851 case BPF_PROG_GET_NEXT_ID:
1852 err = bpf_obj_get_next_id(&attr, uattr,
1853 &prog_idr, &prog_idr_lock);
1854 break;
1855 case BPF_MAP_GET_NEXT_ID:
1856 err = bpf_obj_get_next_id(&attr, uattr,
1857 &map_idr, &map_idr_lock);
1858 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001859 case BPF_PROG_GET_FD_BY_ID:
1860 err = bpf_prog_get_fd_by_id(&attr);
1861 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001862 case BPF_MAP_GET_FD_BY_ID:
1863 err = bpf_map_get_fd_by_id(&attr);
1864 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001865 case BPF_OBJ_GET_INFO_BY_FD:
1866 err = bpf_obj_get_info_by_fd(&attr, uattr);
1867 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001868 default:
1869 err = -EINVAL;
1870 break;
1871 }
1872
1873 return err;
1874}