blob: 263e13ede0290b7695fbaf846caaae139013eeb6 [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>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070014#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070015#include <linux/syscalls.h>
16#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010017#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010018#include <linux/vmalloc.h>
19#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070020#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070021#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070022#include <linux/license.h>
23#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070024#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010025#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070026#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070027#include <linux/cred.h>
28#include <linux/timekeeping.h>
29#include <linux/ctype.h>
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070030#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070031
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070032#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
34 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
35 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
36#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
37#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
38
Chenbo Feng6e71b042017-10-18 13:00:22 -070039#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
40
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080041DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070042static DEFINE_IDR(prog_idr);
43static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070044static DEFINE_IDR(map_idr);
45static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080046
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070047int sysctl_unprivileged_bpf_disabled __read_mostly;
48
Johannes Berg40077e02017-04-11 15:34:58 +020049static const struct bpf_map_ops * const bpf_map_types[] = {
50#define BPF_PROG_TYPE(_id, _ops)
51#define BPF_MAP_TYPE(_id, _ops) \
52 [_id] = &_ops,
53#include <linux/bpf_types.h>
54#undef BPF_PROG_TYPE
55#undef BPF_MAP_TYPE
56};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070057
Mickaël Salaün752ba562017-08-07 20:45:20 +020058/*
59 * If we're handed a bigger struct than we know of, ensure all the unknown bits
60 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
61 * we don't know about yet.
62 *
63 * There is a ToCToU between this function call and the following
64 * copy_from_user() call. However, this is not a concern since this function is
65 * meant to be a future-proofing of bits.
66 */
Mickaël Salaün58291a72017-08-07 20:45:19 +020067static int check_uarg_tail_zero(void __user *uaddr,
68 size_t expected_size,
69 size_t actual_size)
70{
71 unsigned char __user *addr;
72 unsigned char __user *end;
73 unsigned char val;
74 int err;
75
Mickaël Salaün752ba562017-08-07 20:45:20 +020076 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
77 return -E2BIG;
78
79 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
80 return -EFAULT;
81
Mickaël Salaün58291a72017-08-07 20:45:19 +020082 if (actual_size <= expected_size)
83 return 0;
84
85 addr = uaddr + expected_size;
86 end = uaddr + actual_size;
87
88 for (; addr < end; addr++) {
89 err = get_user(val, addr);
90 if (err)
91 return err;
92 if (val)
93 return -E2BIG;
94 }
95
96 return 0;
97}
98
Jakub Kicinskia3884572018-01-11 20:29:09 -080099const struct bpf_map_ops bpf_map_offload_ops = {
100 .map_alloc = bpf_map_offload_map_alloc,
101 .map_free = bpf_map_offload_map_free,
102};
103
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700104static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
105{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800106 const struct bpf_map_ops *ops;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700107 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700109
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800110 if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
111 return ERR_PTR(-EINVAL);
112 ops = bpf_map_types[attr->map_type];
113 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200114 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700115
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800116 if (ops->map_alloc_check) {
117 err = ops->map_alloc_check(attr);
118 if (err)
119 return ERR_PTR(err);
120 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800121 if (attr->map_ifindex)
122 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800123 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200124 if (IS_ERR(map))
125 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800126 map->ops = ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200127 map->map_type = attr->map_type;
128 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700129}
130
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700131void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100132{
133 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
134 * trigger under memory pressure as we really just want to
135 * fail instead.
136 */
137 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
138 void *area;
139
140 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700141 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100142 if (area != NULL)
143 return area;
144 }
145
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700146 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
147 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100148}
149
150void bpf_map_area_free(void *area)
151{
152 kvfree(area);
153}
154
Jakub Kicinskibd475642018-01-11 20:29:06 -0800155void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
156{
157 map->map_type = attr->map_type;
158 map->key_size = attr->key_size;
159 map->value_size = attr->value_size;
160 map->max_entries = attr->max_entries;
161 map->map_flags = attr->map_flags;
162 map->numa_node = bpf_map_attr_numa_node(attr);
163}
164
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800165int bpf_map_precharge_memlock(u32 pages)
166{
167 struct user_struct *user = get_current_user();
168 unsigned long memlock_limit, cur;
169
170 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
171 cur = atomic_long_read(&user->locked_vm);
172 free_uid(user);
173 if (cur + pages > memlock_limit)
174 return -EPERM;
175 return 0;
176}
177
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700178static int bpf_map_charge_memlock(struct bpf_map *map)
179{
180 struct user_struct *user = get_current_user();
181 unsigned long memlock_limit;
182
183 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
184
185 atomic_long_add(map->pages, &user->locked_vm);
186
187 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
188 atomic_long_sub(map->pages, &user->locked_vm);
189 free_uid(user);
190 return -EPERM;
191 }
192 map->user = user;
193 return 0;
194}
195
196static void bpf_map_uncharge_memlock(struct bpf_map *map)
197{
198 struct user_struct *user = map->user;
199
200 atomic_long_sub(map->pages, &user->locked_vm);
201 free_uid(user);
202}
203
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700204static int bpf_map_alloc_id(struct bpf_map *map)
205{
206 int id;
207
Shaohua Lib76354c2018-03-27 11:53:21 -0700208 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700209 spin_lock_bh(&map_idr_lock);
210 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
211 if (id > 0)
212 map->id = id;
213 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700214 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700215
216 if (WARN_ON_ONCE(!id))
217 return -ENOSPC;
218
219 return id > 0 ? 0 : id;
220}
221
Jakub Kicinskia3884572018-01-11 20:29:09 -0800222void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700223{
Eric Dumazet930651a2017-09-19 09:15:59 -0700224 unsigned long flags;
225
Jakub Kicinskia3884572018-01-11 20:29:09 -0800226 /* Offloaded maps are removed from the IDR store when their device
227 * disappears - even if someone holds an fd to them they are unusable,
228 * the memory is gone, all ops will fail; they are simply waiting for
229 * refcnt to drop to be freed.
230 */
231 if (!map->id)
232 return;
233
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700234 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700235 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700236 else
237 __acquire(&map_idr_lock);
238
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700239 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800240 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700241
242 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700243 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700244 else
245 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700246}
247
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700248/* called from workqueue */
249static void bpf_map_free_deferred(struct work_struct *work)
250{
251 struct bpf_map *map = container_of(work, struct bpf_map, work);
252
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700253 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700254 security_bpf_map_free(map);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700255 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700256 /* implementation dependent freeing */
257 map->ops->map_free(map);
258}
259
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100260static void bpf_map_put_uref(struct bpf_map *map)
261{
262 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700263 if (map->ops->map_release_uref)
264 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100265 }
266}
267
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700268/* decrement map refcnt and schedule it for freeing via workqueue
269 * (unrelying map implementation ops->map_free() might sleep)
270 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700271static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700272{
273 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700274 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700275 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700276 INIT_WORK(&map->work, bpf_map_free_deferred);
277 schedule_work(&map->work);
278 }
279}
280
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700281void bpf_map_put(struct bpf_map *map)
282{
283 __bpf_map_put(map, true);
284}
285
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100286void bpf_map_put_with_uref(struct bpf_map *map)
287{
288 bpf_map_put_uref(map);
289 bpf_map_put(map);
290}
291
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700292static int bpf_map_release(struct inode *inode, struct file *filp)
293{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200294 struct bpf_map *map = filp->private_data;
295
296 if (map->ops->map_release)
297 map->ops->map_release(map, filp);
298
299 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700300 return 0;
301}
302
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100303#ifdef CONFIG_PROC_FS
304static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
305{
306 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100307 const struct bpf_array *array;
308 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200309 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100310
311 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
312 array = container_of(map, struct bpf_array, map);
313 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200314 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100315 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100316
317 seq_printf(m,
318 "map_type:\t%u\n"
319 "key_size:\t%u\n"
320 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100321 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100322 "map_flags:\t%#x\n"
323 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100324 map->map_type,
325 map->key_size,
326 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100327 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100328 map->map_flags,
329 map->pages * 1ULL << PAGE_SHIFT);
330
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200331 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100332 seq_printf(m, "owner_prog_type:\t%u\n",
333 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200334 seq_printf(m, "owner_jited:\t%u\n",
335 owner_jited);
336 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100337}
338#endif
339
Chenbo Feng6e71b042017-10-18 13:00:22 -0700340static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
341 loff_t *ppos)
342{
343 /* We need this handler such that alloc_file() enables
344 * f_mode with FMODE_CAN_READ.
345 */
346 return -EINVAL;
347}
348
349static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
350 size_t siz, loff_t *ppos)
351{
352 /* We need this handler such that alloc_file() enables
353 * f_mode with FMODE_CAN_WRITE.
354 */
355 return -EINVAL;
356}
357
Chenbo Fengf66e4482017-10-18 13:00:26 -0700358const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100359#ifdef CONFIG_PROC_FS
360 .show_fdinfo = bpf_map_show_fdinfo,
361#endif
362 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700363 .read = bpf_dummy_read,
364 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700365};
366
Chenbo Feng6e71b042017-10-18 13:00:22 -0700367int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100368{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700369 int ret;
370
371 ret = security_bpf_map(map, OPEN_FMODE(flags));
372 if (ret < 0)
373 return ret;
374
Daniel Borkmannaa797812015-10-29 14:58:06 +0100375 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700376 flags | O_CLOEXEC);
377}
378
379int bpf_get_file_flag(int flags)
380{
381 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
382 return -EINVAL;
383 if (flags & BPF_F_RDONLY)
384 return O_RDONLY;
385 if (flags & BPF_F_WRONLY)
386 return O_WRONLY;
387 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100388}
389
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700390/* helper macro to check that unused fields 'union bpf_attr' are zero */
391#define CHECK_ATTR(CMD) \
392 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
393 sizeof(attr->CMD##_LAST_FIELD), 0, \
394 sizeof(*attr) - \
395 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
396 sizeof(attr->CMD##_LAST_FIELD)) != NULL
397
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700398/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
399 * Return 0 on success and < 0 on error.
400 */
401static int bpf_obj_name_cpy(char *dst, const char *src)
402{
403 const char *end = src + BPF_OBJ_NAME_LEN;
404
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700405 memset(dst, 0, BPF_OBJ_NAME_LEN);
406
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700407 /* Copy all isalnum() and '_' char */
408 while (src < end && *src) {
409 if (!isalnum(*src) && *src != '_')
410 return -EINVAL;
411 *dst++ = *src++;
412 }
413
414 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
415 if (src == end)
416 return -EINVAL;
417
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700418 return 0;
419}
420
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700421#define BPF_MAP_CREATE_LAST_FIELD btf_value_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700422/* called via syscall */
423static int map_create(union bpf_attr *attr)
424{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700425 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700426 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700427 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700428 int err;
429
430 err = CHECK_ATTR(BPF_MAP_CREATE);
431 if (err)
432 return -EINVAL;
433
Chenbo Feng6e71b042017-10-18 13:00:22 -0700434 f_flags = bpf_get_file_flag(attr->map_flags);
435 if (f_flags < 0)
436 return f_flags;
437
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700438 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700439 ((unsigned int)numa_node >= nr_node_ids ||
440 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700441 return -EINVAL;
442
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700443 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
444 map = find_and_alloc_map(attr);
445 if (IS_ERR(map))
446 return PTR_ERR(map);
447
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700448 err = bpf_obj_name_cpy(map->name, attr->map_name);
449 if (err)
450 goto free_map_nouncharge;
451
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700452 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100453 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700454
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700455 if (bpf_map_support_seq_show(map) &&
456 (attr->btf_key_id || attr->btf_value_id)) {
457 struct btf *btf;
458
459 if (!attr->btf_key_id || !attr->btf_value_id) {
460 err = -EINVAL;
461 goto free_map_nouncharge;
462 }
463
464 btf = btf_get_by_fd(attr->btf_fd);
465 if (IS_ERR(btf)) {
466 err = PTR_ERR(btf);
467 goto free_map_nouncharge;
468 }
469
470 err = map->ops->map_check_btf(map, btf, attr->btf_key_id,
471 attr->btf_value_id);
472 if (err) {
473 btf_put(btf);
474 goto free_map_nouncharge;
475 }
476
477 map->btf = btf;
478 map->btf_key_id = attr->btf_key_id;
479 map->btf_value_id = attr->btf_value_id;
480 }
481
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700482 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700483 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100484 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700485
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700486 err = bpf_map_charge_memlock(map);
487 if (err)
488 goto free_map_sec;
489
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700490 err = bpf_map_alloc_id(map);
491 if (err)
492 goto free_map;
493
Chenbo Feng6e71b042017-10-18 13:00:22 -0700494 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700495 if (err < 0) {
496 /* failed to allocate fd.
497 * bpf_map_put() is needed because the above
498 * bpf_map_alloc_id() has published the map
499 * to the userspace and the userspace may
500 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
501 */
502 bpf_map_put(map);
503 return err;
504 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700505
506 return err;
507
508free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100509 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700510free_map_sec:
511 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100512free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700513 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700514 map->ops->map_free(map);
515 return err;
516}
517
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700518/* if error is returned, fd is released.
519 * On success caller should complete fd access with matching fdput()
520 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100521struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700522{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700523 if (!f.file)
524 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700525 if (f.file->f_op != &bpf_map_fops) {
526 fdput(f);
527 return ERR_PTR(-EINVAL);
528 }
529
Daniel Borkmannc2101292015-10-29 14:58:07 +0100530 return f.file->private_data;
531}
532
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700533/* prog's and map's refcnt limit */
534#define BPF_MAX_REFCNT 32768
535
536struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100537{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700538 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
539 atomic_dec(&map->refcnt);
540 return ERR_PTR(-EBUSY);
541 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100542 if (uref)
543 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700544 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100545}
546
547struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100548{
549 struct fd f = fdget(ufd);
550 struct bpf_map *map;
551
552 map = __bpf_map_get(f);
553 if (IS_ERR(map))
554 return map;
555
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700556 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100557 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700558
559 return map;
560}
561
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700562/* map_idr_lock should have been held */
563static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
564 bool uref)
565{
566 int refold;
567
568 refold = __atomic_add_unless(&map->refcnt, 1, 0);
569
570 if (refold >= BPF_MAX_REFCNT) {
571 __bpf_map_put(map, false);
572 return ERR_PTR(-EBUSY);
573 }
574
575 if (!refold)
576 return ERR_PTR(-ENOENT);
577
578 if (uref)
579 atomic_inc(&map->usercnt);
580
581 return map;
582}
583
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800584int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
585{
586 return -ENOTSUPP;
587}
588
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700589/* last field in 'union bpf_attr' used by this command */
590#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
591
592static int map_lookup_elem(union bpf_attr *attr)
593{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100594 void __user *ukey = u64_to_user_ptr(attr->key);
595 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700596 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700597 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800598 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800599 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200600 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700601 int err;
602
603 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
604 return -EINVAL;
605
Daniel Borkmann592867b2015-09-08 18:00:09 +0200606 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100607 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700608 if (IS_ERR(map))
609 return PTR_ERR(map);
610
Chenbo Feng6e71b042017-10-18 13:00:22 -0700611 if (!(f.file->f_mode & FMODE_CAN_READ)) {
612 err = -EPERM;
613 goto err_put;
614 }
615
Al Viroe4448ed2017-05-13 18:43:00 -0400616 key = memdup_user(ukey, map->key_size);
617 if (IS_ERR(key)) {
618 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700619 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400620 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700621
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800622 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800623 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800624 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
625 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700626 else if (IS_FD_MAP(map))
627 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800628 else
629 value_size = map->value_size;
630
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800631 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800632 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700633 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800634 goto free_key;
635
Jakub Kicinskia3884572018-01-11 20:29:09 -0800636 if (bpf_map_is_dev_bound(map)) {
637 err = bpf_map_offload_lookup_elem(map, key, value);
638 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
639 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800640 err = bpf_percpu_hash_copy(map, key, value);
641 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
642 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800643 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
644 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700645 } else if (IS_FD_ARRAY(map)) {
646 err = bpf_fd_array_map_lookup_elem(map, key, value);
647 } else if (IS_FD_HASH(map)) {
648 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800649 } else {
650 rcu_read_lock();
651 ptr = map->ops->map_lookup_elem(map, key);
652 if (ptr)
653 memcpy(value, ptr, value_size);
654 rcu_read_unlock();
655 err = ptr ? 0 : -ENOENT;
656 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800657
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800658 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800659 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700660
661 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800662 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800663 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700664
665 err = 0;
666
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800667free_value:
668 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700669free_key:
670 kfree(key);
671err_put:
672 fdput(f);
673 return err;
674}
675
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800676#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700677
678static int map_update_elem(union bpf_attr *attr)
679{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100680 void __user *ukey = u64_to_user_ptr(attr->key);
681 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700682 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700683 struct bpf_map *map;
684 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800685 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200686 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700687 int err;
688
689 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
690 return -EINVAL;
691
Daniel Borkmann592867b2015-09-08 18:00:09 +0200692 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100693 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700694 if (IS_ERR(map))
695 return PTR_ERR(map);
696
Chenbo Feng6e71b042017-10-18 13:00:22 -0700697 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
698 err = -EPERM;
699 goto err_put;
700 }
701
Al Viroe4448ed2017-05-13 18:43:00 -0400702 key = memdup_user(ukey, map->key_size);
703 if (IS_ERR(key)) {
704 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700705 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400706 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700707
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800708 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800709 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800710 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
711 value_size = round_up(map->value_size, 8) * num_possible_cpus();
712 else
713 value_size = map->value_size;
714
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700715 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800716 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700717 if (!value)
718 goto free_key;
719
720 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800721 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700722 goto free_value;
723
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200724 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800725 if (bpf_map_is_dev_bound(map)) {
726 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
727 goto out;
728 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200729 err = map->ops->map_update_elem(map, key, value, attr->flags);
730 goto out;
731 }
732
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800733 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
734 * inside bpf map update or delete otherwise deadlocks are possible
735 */
736 preempt_disable();
737 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800738 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
739 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800740 err = bpf_percpu_hash_update(map, key, value, attr->flags);
741 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
742 err = bpf_percpu_array_update(map, key, value, attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100743 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200744 rcu_read_lock();
745 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
746 attr->flags);
747 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700748 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
749 rcu_read_lock();
750 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
751 attr->flags);
752 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800753 } else {
754 rcu_read_lock();
755 err = map->ops->map_update_elem(map, key, value, attr->flags);
756 rcu_read_unlock();
757 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800758 __this_cpu_dec(bpf_prog_active);
759 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200760out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700761free_value:
762 kfree(value);
763free_key:
764 kfree(key);
765err_put:
766 fdput(f);
767 return err;
768}
769
770#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
771
772static int map_delete_elem(union bpf_attr *attr)
773{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100774 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700775 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700776 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200777 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700778 void *key;
779 int err;
780
781 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
782 return -EINVAL;
783
Daniel Borkmann592867b2015-09-08 18:00:09 +0200784 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100785 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700786 if (IS_ERR(map))
787 return PTR_ERR(map);
788
Chenbo Feng6e71b042017-10-18 13:00:22 -0700789 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
790 err = -EPERM;
791 goto err_put;
792 }
793
Al Viroe4448ed2017-05-13 18:43:00 -0400794 key = memdup_user(ukey, map->key_size);
795 if (IS_ERR(key)) {
796 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700797 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400798 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700799
Jakub Kicinskia3884572018-01-11 20:29:09 -0800800 if (bpf_map_is_dev_bound(map)) {
801 err = bpf_map_offload_delete_elem(map, key);
802 goto out;
803 }
804
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800805 preempt_disable();
806 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700807 rcu_read_lock();
808 err = map->ops->map_delete_elem(map, key);
809 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800810 __this_cpu_dec(bpf_prog_active);
811 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800812out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700813 kfree(key);
814err_put:
815 fdput(f);
816 return err;
817}
818
819/* last field in 'union bpf_attr' used by this command */
820#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
821
822static int map_get_next_key(union bpf_attr *attr)
823{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100824 void __user *ukey = u64_to_user_ptr(attr->key);
825 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700826 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700827 struct bpf_map *map;
828 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200829 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700830 int err;
831
832 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
833 return -EINVAL;
834
Daniel Borkmann592867b2015-09-08 18:00:09 +0200835 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100836 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700837 if (IS_ERR(map))
838 return PTR_ERR(map);
839
Chenbo Feng6e71b042017-10-18 13:00:22 -0700840 if (!(f.file->f_mode & FMODE_CAN_READ)) {
841 err = -EPERM;
842 goto err_put;
843 }
844
Teng Qin8fe45922017-04-24 19:00:37 -0700845 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400846 key = memdup_user(ukey, map->key_size);
847 if (IS_ERR(key)) {
848 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700849 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400850 }
Teng Qin8fe45922017-04-24 19:00:37 -0700851 } else {
852 key = NULL;
853 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700854
855 err = -ENOMEM;
856 next_key = kmalloc(map->key_size, GFP_USER);
857 if (!next_key)
858 goto free_key;
859
Jakub Kicinskia3884572018-01-11 20:29:09 -0800860 if (bpf_map_is_dev_bound(map)) {
861 err = bpf_map_offload_get_next_key(map, key, next_key);
862 goto out;
863 }
864
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700865 rcu_read_lock();
866 err = map->ops->map_get_next_key(map, key, next_key);
867 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800868out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700869 if (err)
870 goto free_next_key;
871
872 err = -EFAULT;
873 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
874 goto free_next_key;
875
876 err = 0;
877
878free_next_key:
879 kfree(next_key);
880free_key:
881 kfree(key);
882err_put:
883 fdput(f);
884 return err;
885}
886
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700887static const struct bpf_prog_ops * const bpf_prog_types[] = {
888#define BPF_PROG_TYPE(_id, _name) \
889 [_id] = & _name ## _prog_ops,
890#define BPF_MAP_TYPE(_id, _ops)
891#include <linux/bpf_types.h>
892#undef BPF_PROG_TYPE
893#undef BPF_MAP_TYPE
894};
895
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700896static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
897{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200898 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
899 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700900
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700901 if (!bpf_prog_is_dev_bound(prog->aux))
902 prog->aux->ops = bpf_prog_types[type];
903 else
904 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200905 prog->type = type;
906 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700907}
908
909/* drop refcnt on maps used by eBPF program and free auxilary data */
910static void free_used_maps(struct bpf_prog_aux *aux)
911{
912 int i;
913
914 for (i = 0; i < aux->used_map_cnt; i++)
915 bpf_map_put(aux->used_maps[i]);
916
917 kfree(aux->used_maps);
918}
919
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100920int __bpf_prog_charge(struct user_struct *user, u32 pages)
921{
922 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
923 unsigned long user_bufs;
924
925 if (user) {
926 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
927 if (user_bufs > memlock_limit) {
928 atomic_long_sub(pages, &user->locked_vm);
929 return -EPERM;
930 }
931 }
932
933 return 0;
934}
935
936void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
937{
938 if (user)
939 atomic_long_sub(pages, &user->locked_vm);
940}
941
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700942static int bpf_prog_charge_memlock(struct bpf_prog *prog)
943{
944 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100945 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700946
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100947 ret = __bpf_prog_charge(user, prog->pages);
948 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700949 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100950 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700951 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100952
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700953 prog->aux->user = user;
954 return 0;
955}
956
957static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
958{
959 struct user_struct *user = prog->aux->user;
960
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100961 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700962 free_uid(user);
963}
964
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700965static int bpf_prog_alloc_id(struct bpf_prog *prog)
966{
967 int id;
968
Shaohua Lib76354c2018-03-27 11:53:21 -0700969 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700970 spin_lock_bh(&prog_idr_lock);
971 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
972 if (id > 0)
973 prog->aux->id = id;
974 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700975 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700976
977 /* id is in [1, INT_MAX) */
978 if (WARN_ON_ONCE(!id))
979 return -ENOSPC;
980
981 return id > 0 ? 0 : id;
982}
983
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800984void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700985{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800986 /* cBPF to eBPF migrations are currently not in the idr store.
987 * Offloaded programs are removed from the store when their device
988 * disappears - even if someone grabs an fd to them they are unusable,
989 * simply waiting for refcnt to drop to be freed.
990 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700991 if (!prog->aux->id)
992 return;
993
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700994 if (do_idr_lock)
995 spin_lock_bh(&prog_idr_lock);
996 else
997 __acquire(&prog_idr_lock);
998
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700999 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001000 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001001
1002 if (do_idr_lock)
1003 spin_unlock_bh(&prog_idr_lock);
1004 else
1005 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001006}
1007
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001008static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001009{
1010 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1011
1012 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001013 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001014 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001015 bpf_prog_free(aux->prog);
1016}
1017
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001018static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001019{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001020 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001021 int i;
1022
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001023 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001024 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001025
1026 for (i = 0; i < prog->aux->func_cnt; i++)
1027 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001028 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001029
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001030 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001031 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001032}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001033
1034void bpf_prog_put(struct bpf_prog *prog)
1035{
1036 __bpf_prog_put(prog, true);
1037}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001038EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001039
1040static int bpf_prog_release(struct inode *inode, struct file *filp)
1041{
1042 struct bpf_prog *prog = filp->private_data;
1043
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001044 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001045 return 0;
1046}
1047
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001048#ifdef CONFIG_PROC_FS
1049static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1050{
1051 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001052 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001053
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001054 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001055 seq_printf(m,
1056 "prog_type:\t%u\n"
1057 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001058 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001059 "memlock:\t%llu\n",
1060 prog->type,
1061 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001062 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001063 prog->pages * 1ULL << PAGE_SHIFT);
1064}
1065#endif
1066
Chenbo Fengf66e4482017-10-18 13:00:26 -07001067const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001068#ifdef CONFIG_PROC_FS
1069 .show_fdinfo = bpf_prog_show_fdinfo,
1070#endif
1071 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001072 .read = bpf_dummy_read,
1073 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001074};
1075
Daniel Borkmannb2197752015-10-29 14:58:09 +01001076int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001077{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001078 int ret;
1079
1080 ret = security_bpf_prog(prog);
1081 if (ret < 0)
1082 return ret;
1083
Daniel Borkmannaa797812015-10-29 14:58:06 +01001084 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1085 O_RDWR | O_CLOEXEC);
1086}
1087
Daniel Borkmann113214b2016-06-30 17:24:44 +02001088static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001089{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001090 if (!f.file)
1091 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001092 if (f.file->f_op != &bpf_prog_fops) {
1093 fdput(f);
1094 return ERR_PTR(-EINVAL);
1095 }
1096
Daniel Borkmannc2101292015-10-29 14:58:07 +01001097 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001098}
1099
Brenden Blanco59d36562016-07-19 12:16:46 -07001100struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001101{
Brenden Blanco59d36562016-07-19 12:16:46 -07001102 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1103 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001104 return ERR_PTR(-EBUSY);
1105 }
1106 return prog;
1107}
Brenden Blanco59d36562016-07-19 12:16:46 -07001108EXPORT_SYMBOL_GPL(bpf_prog_add);
1109
Daniel Borkmannc5405942016-11-09 22:02:34 +01001110void bpf_prog_sub(struct bpf_prog *prog, int i)
1111{
1112 /* Only to be used for undoing previous bpf_prog_add() in some
1113 * error path. We still know that another entity in our call
1114 * path holds a reference to the program, thus atomic_sub() can
1115 * be safely used in such cases!
1116 */
1117 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1118}
1119EXPORT_SYMBOL_GPL(bpf_prog_sub);
1120
Brenden Blanco59d36562016-07-19 12:16:46 -07001121struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1122{
1123 return bpf_prog_add(prog, 1);
1124}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001125EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001126
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001127/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001128struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001129{
1130 int refold;
1131
1132 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1133
1134 if (refold >= BPF_MAX_REFCNT) {
1135 __bpf_prog_put(prog, false);
1136 return ERR_PTR(-EBUSY);
1137 }
1138
1139 if (!refold)
1140 return ERR_PTR(-ENOENT);
1141
1142 return prog;
1143}
John Fastabenda6f6df62017-08-15 22:32:22 -07001144EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001145
Al Viro040ee692017-12-02 20:20:38 -05001146bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001147 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001148{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001149 /* not an attachment, just a refcount inc, always allow */
1150 if (!attach_type)
1151 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001152
1153 if (prog->type != *attach_type)
1154 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001155 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001156 return false;
1157
1158 return true;
1159}
1160
1161static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001162 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001163{
1164 struct fd f = fdget(ufd);
1165 struct bpf_prog *prog;
1166
Daniel Borkmann113214b2016-06-30 17:24:44 +02001167 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001168 if (IS_ERR(prog))
1169 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001170 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001171 prog = ERR_PTR(-EINVAL);
1172 goto out;
1173 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001174
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001175 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001176out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001177 fdput(f);
1178 return prog;
1179}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001180
1181struct bpf_prog *bpf_prog_get(u32 ufd)
1182{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001183 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001184}
1185
Jakub Kicinski248f3462017-11-03 13:56:20 -07001186struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001187 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001188{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001189 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001190}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001191EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001192
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001193/* Initially all BPF programs could be loaded w/o specifying
1194 * expected_attach_type. Later for some of them specifying expected_attach_type
1195 * at load time became required so that program could be validated properly.
1196 * Programs of types that are allowed to be loaded both w/ and w/o (for
1197 * backward compatibility) expected_attach_type, should have the default attach
1198 * type assigned to expected_attach_type for the latter case, so that it can be
1199 * validated later at attach time.
1200 *
1201 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1202 * prog type requires it but has some attach types that have to be backward
1203 * compatible.
1204 */
1205static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1206{
1207 switch (attr->prog_type) {
1208 case BPF_PROG_TYPE_CGROUP_SOCK:
1209 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1210 * exist so checking for non-zero is the way to go here.
1211 */
1212 if (!attr->expected_attach_type)
1213 attr->expected_attach_type =
1214 BPF_CGROUP_INET_SOCK_CREATE;
1215 break;
1216 }
1217}
1218
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001219static int
1220bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1221 enum bpf_attach_type expected_attach_type)
1222{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001223 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001224 case BPF_PROG_TYPE_CGROUP_SOCK:
1225 switch (expected_attach_type) {
1226 case BPF_CGROUP_INET_SOCK_CREATE:
1227 case BPF_CGROUP_INET4_POST_BIND:
1228 case BPF_CGROUP_INET6_POST_BIND:
1229 return 0;
1230 default:
1231 return -EINVAL;
1232 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001233 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1234 switch (expected_attach_type) {
1235 case BPF_CGROUP_INET4_BIND:
1236 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001237 case BPF_CGROUP_INET4_CONNECT:
1238 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001239 return 0;
1240 default:
1241 return -EINVAL;
1242 }
1243 default:
1244 return 0;
1245 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001246}
1247
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001248/* last field in 'union bpf_attr' used by this command */
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001249#define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001250
1251static int bpf_prog_load(union bpf_attr *attr)
1252{
1253 enum bpf_prog_type type = attr->prog_type;
1254 struct bpf_prog *prog;
1255 int err;
1256 char license[128];
1257 bool is_gpl;
1258
1259 if (CHECK_ATTR(BPF_PROG_LOAD))
1260 return -EINVAL;
1261
David S. Millere07b98d2017-05-10 11:38:07 -07001262 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1263 return -EINVAL;
1264
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001265 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001266 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001267 sizeof(license) - 1) < 0)
1268 return -EFAULT;
1269 license[sizeof(license) - 1] = 0;
1270
1271 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1272 is_gpl = license_is_gpl_compatible(license);
1273
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001274 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1275 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001276
Alexei Starovoitov25415172015-03-25 12:49:20 -07001277 if (type == BPF_PROG_TYPE_KPROBE &&
1278 attr->kern_version != LINUX_VERSION_CODE)
1279 return -EINVAL;
1280
Chenbo Feng80b7d812017-05-31 18:16:00 -07001281 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1282 type != BPF_PROG_TYPE_CGROUP_SKB &&
1283 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001284 return -EPERM;
1285
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001286 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001287 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1288 return -EINVAL;
1289
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001290 /* plain bpf_prog allocation */
1291 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1292 if (!prog)
1293 return -ENOMEM;
1294
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001295 prog->expected_attach_type = attr->expected_attach_type;
1296
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001297 prog->aux->offload_requested = !!attr->prog_ifindex;
1298
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001299 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001300 if (err)
1301 goto free_prog_nouncharge;
1302
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001303 err = bpf_prog_charge_memlock(prog);
1304 if (err)
1305 goto free_prog_sec;
1306
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001307 prog->len = attr->insn_cnt;
1308
1309 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001310 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001311 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001312 goto free_prog;
1313
1314 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001315 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001316
1317 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001318 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001319
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001320 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001321 err = bpf_prog_offload_init(prog, attr);
1322 if (err)
1323 goto free_prog;
1324 }
1325
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001326 /* find program type: socket_filter vs tracing_filter */
1327 err = find_prog_type(type, prog);
1328 if (err < 0)
1329 goto free_prog;
1330
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001331 prog->aux->load_time = ktime_get_boot_ns();
1332 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1333 if (err)
1334 goto free_prog;
1335
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001336 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001337 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001338 if (err < 0)
1339 goto free_used_maps;
1340
1341 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001342 if (!prog->bpf_func)
1343 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001344 if (err < 0)
1345 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001346
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001347 err = bpf_prog_alloc_id(prog);
1348 if (err)
1349 goto free_used_maps;
1350
Daniel Borkmannaa797812015-10-29 14:58:06 +01001351 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001352 if (err < 0) {
1353 /* failed to allocate fd.
1354 * bpf_prog_put() is needed because the above
1355 * bpf_prog_alloc_id() has published the prog
1356 * to the userspace and the userspace may
1357 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1358 */
1359 bpf_prog_put(prog);
1360 return err;
1361 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001362
Daniel Borkmann74451e662017-02-16 22:24:50 +01001363 bpf_prog_kallsyms_add(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001364 return err;
1365
1366free_used_maps:
1367 free_used_maps(prog->aux);
1368free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001369 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001370free_prog_sec:
1371 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001372free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001373 bpf_prog_free(prog);
1374 return err;
1375}
1376
Chenbo Feng6e71b042017-10-18 13:00:22 -07001377#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001378
1379static int bpf_obj_pin(const union bpf_attr *attr)
1380{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001381 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001382 return -EINVAL;
1383
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001384 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001385}
1386
1387static int bpf_obj_get(const union bpf_attr *attr)
1388{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001389 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1390 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001391 return -EINVAL;
1392
Chenbo Feng6e71b042017-10-18 13:00:22 -07001393 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1394 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001395}
1396
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001397struct bpf_raw_tracepoint {
1398 struct bpf_raw_event_map *btp;
1399 struct bpf_prog *prog;
1400};
1401
1402static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1403{
1404 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1405
1406 if (raw_tp->prog) {
1407 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1408 bpf_prog_put(raw_tp->prog);
1409 }
1410 kfree(raw_tp);
1411 return 0;
1412}
1413
1414static const struct file_operations bpf_raw_tp_fops = {
1415 .release = bpf_raw_tracepoint_release,
1416 .read = bpf_dummy_read,
1417 .write = bpf_dummy_write,
1418};
1419
1420#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1421
1422static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1423{
1424 struct bpf_raw_tracepoint *raw_tp;
1425 struct bpf_raw_event_map *btp;
1426 struct bpf_prog *prog;
1427 char tp_name[128];
1428 int tp_fd, err;
1429
1430 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1431 sizeof(tp_name) - 1) < 0)
1432 return -EFAULT;
1433 tp_name[sizeof(tp_name) - 1] = 0;
1434
1435 btp = bpf_find_raw_tracepoint(tp_name);
1436 if (!btp)
1437 return -ENOENT;
1438
1439 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1440 if (!raw_tp)
1441 return -ENOMEM;
1442 raw_tp->btp = btp;
1443
1444 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1445 BPF_PROG_TYPE_RAW_TRACEPOINT);
1446 if (IS_ERR(prog)) {
1447 err = PTR_ERR(prog);
1448 goto out_free_tp;
1449 }
1450
1451 err = bpf_probe_register(raw_tp->btp, prog);
1452 if (err)
1453 goto out_put_prog;
1454
1455 raw_tp->prog = prog;
1456 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1457 O_CLOEXEC);
1458 if (tp_fd < 0) {
1459 bpf_probe_unregister(raw_tp->btp, prog);
1460 err = tp_fd;
1461 goto out_put_prog;
1462 }
1463 return tp_fd;
1464
1465out_put_prog:
1466 bpf_prog_put(prog);
1467out_free_tp:
1468 kfree(raw_tp);
1469 return err;
1470}
1471
Daniel Mackf4324552016-11-23 16:52:27 +01001472#ifdef CONFIG_CGROUP_BPF
1473
Anders Roxell33491582018-04-03 14:09:47 +02001474static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1475 enum bpf_attach_type attach_type)
1476{
1477 switch (prog->type) {
1478 case BPF_PROG_TYPE_CGROUP_SOCK:
1479 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1480 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1481 default:
1482 return 0;
1483 }
1484}
1485
John Fastabend464bc0f2017-08-28 07:10:04 -07001486#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001487
John Fastabend4f738ad2018-03-18 12:57:10 -07001488static int sockmap_get_from_fd(const union bpf_attr *attr,
1489 int type, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001490{
John Fastabend5a67da22017-09-08 14:00:49 -07001491 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001492 int ufd = attr->target_fd;
1493 struct bpf_map *map;
1494 struct fd f;
1495 int err;
1496
1497 f = fdget(ufd);
1498 map = __bpf_map_get(f);
1499 if (IS_ERR(map))
1500 return PTR_ERR(map);
1501
John Fastabend5a67da22017-09-08 14:00:49 -07001502 if (attach) {
John Fastabend4f738ad2018-03-18 12:57:10 -07001503 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
John Fastabend5a67da22017-09-08 14:00:49 -07001504 if (IS_ERR(prog)) {
1505 fdput(f);
1506 return PTR_ERR(prog);
1507 }
John Fastabend174a79f2017-08-15 22:32:47 -07001508 }
1509
John Fastabend5a67da22017-09-08 14:00:49 -07001510 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001511 if (err) {
1512 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001513 if (prog)
1514 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001515 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001516 }
1517
1518 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001519 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001520}
Daniel Mackf4324552016-11-23 16:52:27 +01001521
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001522#define BPF_F_ATTACH_MASK \
1523 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1524
Daniel Mackf4324552016-11-23 16:52:27 +01001525static int bpf_prog_attach(const union bpf_attr *attr)
1526{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001527 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001528 struct bpf_prog *prog;
1529 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001530 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001531
1532 if (!capable(CAP_NET_ADMIN))
1533 return -EPERM;
1534
1535 if (CHECK_ATTR(BPF_PROG_ATTACH))
1536 return -EINVAL;
1537
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001538 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001539 return -EINVAL;
1540
Daniel Mackf4324552016-11-23 16:52:27 +01001541 switch (attr->attach_type) {
1542 case BPF_CGROUP_INET_INGRESS:
1543 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001544 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001545 break;
David Ahern610236582016-12-01 08:48:04 -08001546 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001547 case BPF_CGROUP_INET4_POST_BIND:
1548 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001549 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1550 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001551 case BPF_CGROUP_INET4_BIND:
1552 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001553 case BPF_CGROUP_INET4_CONNECT:
1554 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001555 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1556 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001557 case BPF_CGROUP_SOCK_OPS:
1558 ptype = BPF_PROG_TYPE_SOCK_OPS;
1559 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001560 case BPF_CGROUP_DEVICE:
1561 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1562 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001563 case BPF_SK_MSG_VERDICT:
1564 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
John Fastabend464bc0f2017-08-28 07:10:04 -07001565 case BPF_SK_SKB_STREAM_PARSER:
1566 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001567 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001568 default:
1569 return -EINVAL;
1570 }
1571
David Ahernb2cd1252016-12-01 08:48:03 -08001572 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1573 if (IS_ERR(prog))
1574 return PTR_ERR(prog);
1575
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001576 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1577 bpf_prog_put(prog);
1578 return -EINVAL;
1579 }
1580
David Ahernb2cd1252016-12-01 08:48:03 -08001581 cgrp = cgroup_get_from_fd(attr->target_fd);
1582 if (IS_ERR(cgrp)) {
1583 bpf_prog_put(prog);
1584 return PTR_ERR(cgrp);
1585 }
1586
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001587 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1588 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001589 if (ret)
1590 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001591 cgroup_put(cgrp);
1592
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001593 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001594}
1595
1596#define BPF_PROG_DETACH_LAST_FIELD attach_type
1597
1598static int bpf_prog_detach(const union bpf_attr *attr)
1599{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001600 enum bpf_prog_type ptype;
1601 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001602 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001603 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001604
1605 if (!capable(CAP_NET_ADMIN))
1606 return -EPERM;
1607
1608 if (CHECK_ATTR(BPF_PROG_DETACH))
1609 return -EINVAL;
1610
1611 switch (attr->attach_type) {
1612 case BPF_CGROUP_INET_INGRESS:
1613 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001614 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1615 break;
David Ahern610236582016-12-01 08:48:04 -08001616 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001617 case BPF_CGROUP_INET4_POST_BIND:
1618 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001619 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1620 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001621 case BPF_CGROUP_INET4_BIND:
1622 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001623 case BPF_CGROUP_INET4_CONNECT:
1624 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001625 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1626 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001627 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001628 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001629 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001630 case BPF_CGROUP_DEVICE:
1631 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1632 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001633 case BPF_SK_MSG_VERDICT:
1634 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
John Fastabend5a67da22017-09-08 14:00:49 -07001635 case BPF_SK_SKB_STREAM_PARSER:
1636 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001637 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001638 default:
1639 return -EINVAL;
1640 }
1641
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001642 cgrp = cgroup_get_from_fd(attr->target_fd);
1643 if (IS_ERR(cgrp))
1644 return PTR_ERR(cgrp);
1645
1646 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1647 if (IS_ERR(prog))
1648 prog = NULL;
1649
1650 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1651 if (prog)
1652 bpf_prog_put(prog);
1653 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001654 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001655}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001656
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001657#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1658
1659static int bpf_prog_query(const union bpf_attr *attr,
1660 union bpf_attr __user *uattr)
1661{
1662 struct cgroup *cgrp;
1663 int ret;
1664
1665 if (!capable(CAP_NET_ADMIN))
1666 return -EPERM;
1667 if (CHECK_ATTR(BPF_PROG_QUERY))
1668 return -EINVAL;
1669 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1670 return -EINVAL;
1671
1672 switch (attr->query.attach_type) {
1673 case BPF_CGROUP_INET_INGRESS:
1674 case BPF_CGROUP_INET_EGRESS:
1675 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001676 case BPF_CGROUP_INET4_BIND:
1677 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001678 case BPF_CGROUP_INET4_POST_BIND:
1679 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001680 case BPF_CGROUP_INET4_CONNECT:
1681 case BPF_CGROUP_INET6_CONNECT:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001682 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001683 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001684 break;
1685 default:
1686 return -EINVAL;
1687 }
1688 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1689 if (IS_ERR(cgrp))
1690 return PTR_ERR(cgrp);
1691 ret = cgroup_bpf_query(cgrp, attr, uattr);
1692 cgroup_put(cgrp);
1693 return ret;
1694}
Daniel Mackf4324552016-11-23 16:52:27 +01001695#endif /* CONFIG_CGROUP_BPF */
1696
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001697#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1698
1699static int bpf_prog_test_run(const union bpf_attr *attr,
1700 union bpf_attr __user *uattr)
1701{
1702 struct bpf_prog *prog;
1703 int ret = -ENOTSUPP;
1704
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001705 if (!capable(CAP_SYS_ADMIN))
1706 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001707 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1708 return -EINVAL;
1709
1710 prog = bpf_prog_get(attr->test.prog_fd);
1711 if (IS_ERR(prog))
1712 return PTR_ERR(prog);
1713
1714 if (prog->aux->ops->test_run)
1715 ret = prog->aux->ops->test_run(prog, attr, uattr);
1716
1717 bpf_prog_put(prog);
1718 return ret;
1719}
1720
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001721#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1722
1723static int bpf_obj_get_next_id(const union bpf_attr *attr,
1724 union bpf_attr __user *uattr,
1725 struct idr *idr,
1726 spinlock_t *lock)
1727{
1728 u32 next_id = attr->start_id;
1729 int err = 0;
1730
1731 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1732 return -EINVAL;
1733
1734 if (!capable(CAP_SYS_ADMIN))
1735 return -EPERM;
1736
1737 next_id++;
1738 spin_lock_bh(lock);
1739 if (!idr_get_next(idr, &next_id))
1740 err = -ENOENT;
1741 spin_unlock_bh(lock);
1742
1743 if (!err)
1744 err = put_user(next_id, &uattr->next_id);
1745
1746 return err;
1747}
1748
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001749#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1750
1751static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1752{
1753 struct bpf_prog *prog;
1754 u32 id = attr->prog_id;
1755 int fd;
1756
1757 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1758 return -EINVAL;
1759
1760 if (!capable(CAP_SYS_ADMIN))
1761 return -EPERM;
1762
1763 spin_lock_bh(&prog_idr_lock);
1764 prog = idr_find(&prog_idr, id);
1765 if (prog)
1766 prog = bpf_prog_inc_not_zero(prog);
1767 else
1768 prog = ERR_PTR(-ENOENT);
1769 spin_unlock_bh(&prog_idr_lock);
1770
1771 if (IS_ERR(prog))
1772 return PTR_ERR(prog);
1773
1774 fd = bpf_prog_new_fd(prog);
1775 if (fd < 0)
1776 bpf_prog_put(prog);
1777
1778 return fd;
1779}
1780
Chenbo Feng6e71b042017-10-18 13:00:22 -07001781#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001782
1783static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1784{
1785 struct bpf_map *map;
1786 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001787 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001788 int fd;
1789
Chenbo Feng6e71b042017-10-18 13:00:22 -07001790 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1791 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001792 return -EINVAL;
1793
1794 if (!capable(CAP_SYS_ADMIN))
1795 return -EPERM;
1796
Chenbo Feng6e71b042017-10-18 13:00:22 -07001797 f_flags = bpf_get_file_flag(attr->open_flags);
1798 if (f_flags < 0)
1799 return f_flags;
1800
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001801 spin_lock_bh(&map_idr_lock);
1802 map = idr_find(&map_idr, id);
1803 if (map)
1804 map = bpf_map_inc_not_zero(map, true);
1805 else
1806 map = ERR_PTR(-ENOENT);
1807 spin_unlock_bh(&map_idr_lock);
1808
1809 if (IS_ERR(map))
1810 return PTR_ERR(map);
1811
Chenbo Feng6e71b042017-10-18 13:00:22 -07001812 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001813 if (fd < 0)
1814 bpf_map_put(map);
1815
1816 return fd;
1817}
1818
Daniel Borkmann7105e822017-12-20 13:42:57 +01001819static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1820 unsigned long addr)
1821{
1822 int i;
1823
1824 for (i = 0; i < prog->aux->used_map_cnt; i++)
1825 if (prog->aux->used_maps[i] == (void *)addr)
1826 return prog->aux->used_maps[i];
1827 return NULL;
1828}
1829
1830static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1831{
1832 const struct bpf_map *map;
1833 struct bpf_insn *insns;
1834 u64 imm;
1835 int i;
1836
1837 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1838 GFP_USER);
1839 if (!insns)
1840 return insns;
1841
1842 for (i = 0; i < prog->len; i++) {
1843 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1844 insns[i].code = BPF_JMP | BPF_CALL;
1845 insns[i].imm = BPF_FUNC_tail_call;
1846 /* fall-through */
1847 }
1848 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1849 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1850 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1851 insns[i].code = BPF_JMP | BPF_CALL;
1852 if (!bpf_dump_raw_ok())
1853 insns[i].imm = 0;
1854 continue;
1855 }
1856
1857 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1858 continue;
1859
1860 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1861 map = bpf_map_from_imm(prog, imm);
1862 if (map) {
1863 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1864 insns[i].imm = map->id;
1865 insns[i + 1].imm = 0;
1866 continue;
1867 }
1868
1869 if (!bpf_dump_raw_ok() &&
1870 imm == (unsigned long)prog->aux) {
1871 insns[i].imm = 0;
1872 insns[i + 1].imm = 0;
1873 continue;
1874 }
1875 }
1876
1877 return insns;
1878}
1879
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001880static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1881 const union bpf_attr *attr,
1882 union bpf_attr __user *uattr)
1883{
1884 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1885 struct bpf_prog_info info = {};
1886 u32 info_len = attr->info.info_len;
1887 char __user *uinsns;
1888 u32 ulen;
1889 int err;
1890
1891 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1892 if (err)
1893 return err;
1894 info_len = min_t(u32, sizeof(info), info_len);
1895
1896 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001897 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001898
1899 info.type = prog->type;
1900 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001901 info.load_time = prog->aux->load_time;
1902 info.created_by_uid = from_kuid_munged(current_user_ns(),
1903 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02001904 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001905
1906 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001907 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1908
1909 ulen = info.nr_map_ids;
1910 info.nr_map_ids = prog->aux->used_map_cnt;
1911 ulen = min_t(u32, info.nr_map_ids, ulen);
1912 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001913 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001914 u32 i;
1915
1916 for (i = 0; i < ulen; i++)
1917 if (put_user(prog->aux->used_maps[i]->id,
1918 &user_map_ids[i]))
1919 return -EFAULT;
1920 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001921
1922 if (!capable(CAP_SYS_ADMIN)) {
1923 info.jited_prog_len = 0;
1924 info.xlated_prog_len = 0;
1925 goto done;
1926 }
1927
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001928 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001929 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001930 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001931 struct bpf_insn *insns_sanitized;
1932 bool fault;
1933
1934 if (prog->blinded && !bpf_dump_raw_ok()) {
1935 info.xlated_prog_insns = 0;
1936 goto done;
1937 }
1938 insns_sanitized = bpf_insn_prepare_dump(prog);
1939 if (!insns_sanitized)
1940 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001941 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1942 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001943 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1944 kfree(insns_sanitized);
1945 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001946 return -EFAULT;
1947 }
1948
Jakub Kicinski675fc272017-12-27 18:39:09 -08001949 if (bpf_prog_is_dev_bound(prog->aux)) {
1950 err = bpf_prog_offload_info_fill(&info, prog);
1951 if (err)
1952 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001953 goto done;
1954 }
1955
1956 /* NOTE: the following code is supposed to be skipped for offload.
1957 * bpf_prog_offload_info_fill() is the place to fill similar fields
1958 * for offload.
1959 */
1960 ulen = info.jited_prog_len;
1961 info.jited_prog_len = prog->jited_len;
1962 if (info.jited_prog_len && ulen) {
1963 if (bpf_dump_raw_ok()) {
1964 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1965 ulen = min_t(u32, info.jited_prog_len, ulen);
1966 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1967 return -EFAULT;
1968 } else {
1969 info.jited_prog_insns = 0;
1970 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08001971 }
1972
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001973done:
1974 if (copy_to_user(uinfo, &info, info_len) ||
1975 put_user(info_len, &uattr->info.info_len))
1976 return -EFAULT;
1977
1978 return 0;
1979}
1980
1981static int bpf_map_get_info_by_fd(struct bpf_map *map,
1982 const union bpf_attr *attr,
1983 union bpf_attr __user *uattr)
1984{
1985 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1986 struct bpf_map_info info = {};
1987 u32 info_len = attr->info.info_len;
1988 int err;
1989
1990 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1991 if (err)
1992 return err;
1993 info_len = min_t(u32, sizeof(info), info_len);
1994
1995 info.type = map->map_type;
1996 info.id = map->id;
1997 info.key_size = map->key_size;
1998 info.value_size = map->value_size;
1999 info.max_entries = map->max_entries;
2000 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002001 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002002
Jakub Kicinski52775b32018-01-17 19:13:28 -08002003 if (bpf_map_is_dev_bound(map)) {
2004 err = bpf_map_offload_info_fill(&info, map);
2005 if (err)
2006 return err;
2007 }
2008
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002009 if (copy_to_user(uinfo, &info, info_len) ||
2010 put_user(info_len, &uattr->info.info_len))
2011 return -EFAULT;
2012
2013 return 0;
2014}
2015
2016#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2017
2018static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2019 union bpf_attr __user *uattr)
2020{
2021 int ufd = attr->info.bpf_fd;
2022 struct fd f;
2023 int err;
2024
2025 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2026 return -EINVAL;
2027
2028 f = fdget(ufd);
2029 if (!f.file)
2030 return -EBADFD;
2031
2032 if (f.file->f_op == &bpf_prog_fops)
2033 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2034 uattr);
2035 else if (f.file->f_op == &bpf_map_fops)
2036 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2037 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002038 else if (f.file->f_op == &btf_fops)
2039 err = btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002040 else
2041 err = -EINVAL;
2042
2043 fdput(f);
2044 return err;
2045}
2046
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002047#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2048
2049static int bpf_btf_load(const union bpf_attr *attr)
2050{
2051 if (CHECK_ATTR(BPF_BTF_LOAD))
2052 return -EINVAL;
2053
2054 if (!capable(CAP_SYS_ADMIN))
2055 return -EPERM;
2056
2057 return btf_new_fd(attr);
2058}
2059
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002060SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2061{
2062 union bpf_attr attr = {};
2063 int err;
2064
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002065 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002066 return -EPERM;
2067
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002068 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
2069 if (err)
2070 return err;
2071 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002072
2073 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2074 if (copy_from_user(&attr, uattr, size) != 0)
2075 return -EFAULT;
2076
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002077 err = security_bpf(cmd, &attr, size);
2078 if (err < 0)
2079 return err;
2080
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002081 switch (cmd) {
2082 case BPF_MAP_CREATE:
2083 err = map_create(&attr);
2084 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002085 case BPF_MAP_LOOKUP_ELEM:
2086 err = map_lookup_elem(&attr);
2087 break;
2088 case BPF_MAP_UPDATE_ELEM:
2089 err = map_update_elem(&attr);
2090 break;
2091 case BPF_MAP_DELETE_ELEM:
2092 err = map_delete_elem(&attr);
2093 break;
2094 case BPF_MAP_GET_NEXT_KEY:
2095 err = map_get_next_key(&attr);
2096 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002097 case BPF_PROG_LOAD:
2098 err = bpf_prog_load(&attr);
2099 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002100 case BPF_OBJ_PIN:
2101 err = bpf_obj_pin(&attr);
2102 break;
2103 case BPF_OBJ_GET:
2104 err = bpf_obj_get(&attr);
2105 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002106#ifdef CONFIG_CGROUP_BPF
2107 case BPF_PROG_ATTACH:
2108 err = bpf_prog_attach(&attr);
2109 break;
2110 case BPF_PROG_DETACH:
2111 err = bpf_prog_detach(&attr);
2112 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002113 case BPF_PROG_QUERY:
2114 err = bpf_prog_query(&attr, uattr);
2115 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002116#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002117 case BPF_PROG_TEST_RUN:
2118 err = bpf_prog_test_run(&attr, uattr);
2119 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002120 case BPF_PROG_GET_NEXT_ID:
2121 err = bpf_obj_get_next_id(&attr, uattr,
2122 &prog_idr, &prog_idr_lock);
2123 break;
2124 case BPF_MAP_GET_NEXT_ID:
2125 err = bpf_obj_get_next_id(&attr, uattr,
2126 &map_idr, &map_idr_lock);
2127 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002128 case BPF_PROG_GET_FD_BY_ID:
2129 err = bpf_prog_get_fd_by_id(&attr);
2130 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002131 case BPF_MAP_GET_FD_BY_ID:
2132 err = bpf_map_get_fd_by_id(&attr);
2133 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002134 case BPF_OBJ_GET_INFO_BY_FD:
2135 err = bpf_obj_get_info_by_fd(&attr, uattr);
2136 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002137 case BPF_RAW_TRACEPOINT_OPEN:
2138 err = bpf_raw_tracepoint_open(&attr);
2139 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002140 case BPF_BTF_LOAD:
2141 err = bpf_btf_load(&attr);
2142 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002143 default:
2144 err = -EINVAL;
2145 break;
2146 }
2147
2148 return err;
2149}