blob: e2aeb5e89f44683950b7442c5d2a1ea2e9acddc2 [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);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700255 /* implementation dependent freeing */
256 map->ops->map_free(map);
257}
258
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100259static void bpf_map_put_uref(struct bpf_map *map)
260{
261 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700262 if (map->ops->map_release_uref)
263 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100264 }
265}
266
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700267/* decrement map refcnt and schedule it for freeing via workqueue
268 * (unrelying map implementation ops->map_free() might sleep)
269 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700270static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700271{
272 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700273 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700274 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700275 btf_put(map->btf);
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}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700285EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700286
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100287void bpf_map_put_with_uref(struct bpf_map *map)
288{
289 bpf_map_put_uref(map);
290 bpf_map_put(map);
291}
292
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700293static int bpf_map_release(struct inode *inode, struct file *filp)
294{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200295 struct bpf_map *map = filp->private_data;
296
297 if (map->ops->map_release)
298 map->ops->map_release(map, filp);
299
300 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700301 return 0;
302}
303
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100304#ifdef CONFIG_PROC_FS
305static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
306{
307 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100308 const struct bpf_array *array;
309 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200310 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100311
312 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
313 array = container_of(map, struct bpf_array, map);
314 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200315 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100316 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100317
318 seq_printf(m,
319 "map_type:\t%u\n"
320 "key_size:\t%u\n"
321 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100322 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100323 "map_flags:\t%#x\n"
324 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100325 map->map_type,
326 map->key_size,
327 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100328 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100329 map->map_flags,
330 map->pages * 1ULL << PAGE_SHIFT);
331
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200332 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100333 seq_printf(m, "owner_prog_type:\t%u\n",
334 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200335 seq_printf(m, "owner_jited:\t%u\n",
336 owner_jited);
337 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100338}
339#endif
340
Chenbo Feng6e71b042017-10-18 13:00:22 -0700341static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
342 loff_t *ppos)
343{
344 /* We need this handler such that alloc_file() enables
345 * f_mode with FMODE_CAN_READ.
346 */
347 return -EINVAL;
348}
349
350static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
351 size_t siz, loff_t *ppos)
352{
353 /* We need this handler such that alloc_file() enables
354 * f_mode with FMODE_CAN_WRITE.
355 */
356 return -EINVAL;
357}
358
Chenbo Fengf66e4482017-10-18 13:00:26 -0700359const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100360#ifdef CONFIG_PROC_FS
361 .show_fdinfo = bpf_map_show_fdinfo,
362#endif
363 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700364 .read = bpf_dummy_read,
365 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700366};
367
Chenbo Feng6e71b042017-10-18 13:00:22 -0700368int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100369{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700370 int ret;
371
372 ret = security_bpf_map(map, OPEN_FMODE(flags));
373 if (ret < 0)
374 return ret;
375
Daniel Borkmannaa797812015-10-29 14:58:06 +0100376 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700377 flags | O_CLOEXEC);
378}
379
380int bpf_get_file_flag(int flags)
381{
382 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
383 return -EINVAL;
384 if (flags & BPF_F_RDONLY)
385 return O_RDONLY;
386 if (flags & BPF_F_WRONLY)
387 return O_WRONLY;
388 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100389}
390
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700391/* helper macro to check that unused fields 'union bpf_attr' are zero */
392#define CHECK_ATTR(CMD) \
393 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
394 sizeof(attr->CMD##_LAST_FIELD), 0, \
395 sizeof(*attr) - \
396 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
397 sizeof(attr->CMD##_LAST_FIELD)) != NULL
398
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700399/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
400 * Return 0 on success and < 0 on error.
401 */
402static int bpf_obj_name_cpy(char *dst, const char *src)
403{
404 const char *end = src + BPF_OBJ_NAME_LEN;
405
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700406 memset(dst, 0, BPF_OBJ_NAME_LEN);
407
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700408 /* Copy all isalnum() and '_' char */
409 while (src < end && *src) {
410 if (!isalnum(*src) && *src != '_')
411 return -EINVAL;
412 *dst++ = *src++;
413 }
414
415 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
416 if (src == end)
417 return -EINVAL;
418
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700419 return 0;
420}
421
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700422#define BPF_MAP_CREATE_LAST_FIELD btf_value_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700423/* called via syscall */
424static int map_create(union bpf_attr *attr)
425{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700426 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700427 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700428 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700429 int err;
430
431 err = CHECK_ATTR(BPF_MAP_CREATE);
432 if (err)
433 return -EINVAL;
434
Chenbo Feng6e71b042017-10-18 13:00:22 -0700435 f_flags = bpf_get_file_flag(attr->map_flags);
436 if (f_flags < 0)
437 return f_flags;
438
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700439 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700440 ((unsigned int)numa_node >= nr_node_ids ||
441 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700442 return -EINVAL;
443
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700444 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
445 map = find_and_alloc_map(attr);
446 if (IS_ERR(map))
447 return PTR_ERR(map);
448
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700449 err = bpf_obj_name_cpy(map->name, attr->map_name);
450 if (err)
451 goto free_map_nouncharge;
452
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700453 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100454 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700455
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700456 if (bpf_map_support_seq_show(map) &&
457 (attr->btf_key_id || attr->btf_value_id)) {
458 struct btf *btf;
459
460 if (!attr->btf_key_id || !attr->btf_value_id) {
461 err = -EINVAL;
462 goto free_map_nouncharge;
463 }
464
465 btf = btf_get_by_fd(attr->btf_fd);
466 if (IS_ERR(btf)) {
467 err = PTR_ERR(btf);
468 goto free_map_nouncharge;
469 }
470
471 err = map->ops->map_check_btf(map, btf, attr->btf_key_id,
472 attr->btf_value_id);
473 if (err) {
474 btf_put(btf);
475 goto free_map_nouncharge;
476 }
477
478 map->btf = btf;
479 map->btf_key_id = attr->btf_key_id;
480 map->btf_value_id = attr->btf_value_id;
481 }
482
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700483 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700484 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100485 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700486
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700487 err = bpf_map_charge_memlock(map);
488 if (err)
489 goto free_map_sec;
490
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700491 err = bpf_map_alloc_id(map);
492 if (err)
493 goto free_map;
494
Chenbo Feng6e71b042017-10-18 13:00:22 -0700495 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700496 if (err < 0) {
497 /* failed to allocate fd.
498 * bpf_map_put() is needed because the above
499 * bpf_map_alloc_id() has published the map
500 * to the userspace and the userspace may
501 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
502 */
503 bpf_map_put(map);
504 return err;
505 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700506
507 return err;
508
509free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100510 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700511free_map_sec:
512 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100513free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700514 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700515 map->ops->map_free(map);
516 return err;
517}
518
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700519/* if error is returned, fd is released.
520 * On success caller should complete fd access with matching fdput()
521 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100522struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700523{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700524 if (!f.file)
525 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700526 if (f.file->f_op != &bpf_map_fops) {
527 fdput(f);
528 return ERR_PTR(-EINVAL);
529 }
530
Daniel Borkmannc2101292015-10-29 14:58:07 +0100531 return f.file->private_data;
532}
533
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700534/* prog's and map's refcnt limit */
535#define BPF_MAX_REFCNT 32768
536
537struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100538{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700539 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
540 atomic_dec(&map->refcnt);
541 return ERR_PTR(-EBUSY);
542 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100543 if (uref)
544 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700545 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100546}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700547EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100548
549struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100550{
551 struct fd f = fdget(ufd);
552 struct bpf_map *map;
553
554 map = __bpf_map_get(f);
555 if (IS_ERR(map))
556 return map;
557
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700558 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100559 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700560
561 return map;
562}
563
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700564/* map_idr_lock should have been held */
565static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
566 bool uref)
567{
568 int refold;
569
570 refold = __atomic_add_unless(&map->refcnt, 1, 0);
571
572 if (refold >= BPF_MAX_REFCNT) {
573 __bpf_map_put(map, false);
574 return ERR_PTR(-EBUSY);
575 }
576
577 if (!refold)
578 return ERR_PTR(-ENOENT);
579
580 if (uref)
581 atomic_inc(&map->usercnt);
582
583 return map;
584}
585
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800586int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
587{
588 return -ENOTSUPP;
589}
590
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700591/* last field in 'union bpf_attr' used by this command */
592#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
593
594static int map_lookup_elem(union bpf_attr *attr)
595{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100596 void __user *ukey = u64_to_user_ptr(attr->key);
597 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700598 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700599 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800600 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800601 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200602 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700603 int err;
604
605 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
606 return -EINVAL;
607
Daniel Borkmann592867b2015-09-08 18:00:09 +0200608 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100609 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700610 if (IS_ERR(map))
611 return PTR_ERR(map);
612
Chenbo Feng6e71b042017-10-18 13:00:22 -0700613 if (!(f.file->f_mode & FMODE_CAN_READ)) {
614 err = -EPERM;
615 goto err_put;
616 }
617
Al Viroe4448ed2017-05-13 18:43:00 -0400618 key = memdup_user(ukey, map->key_size);
619 if (IS_ERR(key)) {
620 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700621 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400622 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700623
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800624 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800625 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800626 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
627 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700628 else if (IS_FD_MAP(map))
629 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800630 else
631 value_size = map->value_size;
632
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800633 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800634 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700635 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800636 goto free_key;
637
Jakub Kicinskia3884572018-01-11 20:29:09 -0800638 if (bpf_map_is_dev_bound(map)) {
639 err = bpf_map_offload_lookup_elem(map, key, value);
640 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
641 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800642 err = bpf_percpu_hash_copy(map, key, value);
643 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
644 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800645 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
646 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700647 } else if (IS_FD_ARRAY(map)) {
648 err = bpf_fd_array_map_lookup_elem(map, key, value);
649 } else if (IS_FD_HASH(map)) {
650 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800651 } else {
652 rcu_read_lock();
653 ptr = map->ops->map_lookup_elem(map, key);
654 if (ptr)
655 memcpy(value, ptr, value_size);
656 rcu_read_unlock();
657 err = ptr ? 0 : -ENOENT;
658 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800659
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800660 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800661 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700662
663 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800664 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800665 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700666
667 err = 0;
668
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800669free_value:
670 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700671free_key:
672 kfree(key);
673err_put:
674 fdput(f);
675 return err;
676}
677
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800678#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700679
680static int map_update_elem(union bpf_attr *attr)
681{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100682 void __user *ukey = u64_to_user_ptr(attr->key);
683 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700684 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700685 struct bpf_map *map;
686 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800687 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200688 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700689 int err;
690
691 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
692 return -EINVAL;
693
Daniel Borkmann592867b2015-09-08 18:00:09 +0200694 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100695 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700696 if (IS_ERR(map))
697 return PTR_ERR(map);
698
Chenbo Feng6e71b042017-10-18 13:00:22 -0700699 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
700 err = -EPERM;
701 goto err_put;
702 }
703
Al Viroe4448ed2017-05-13 18:43:00 -0400704 key = memdup_user(ukey, map->key_size);
705 if (IS_ERR(key)) {
706 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700707 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400708 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700709
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800710 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800711 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800712 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
713 value_size = round_up(map->value_size, 8) * num_possible_cpus();
714 else
715 value_size = map->value_size;
716
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700717 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800718 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700719 if (!value)
720 goto free_key;
721
722 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800723 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700724 goto free_value;
725
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200726 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800727 if (bpf_map_is_dev_bound(map)) {
728 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
729 goto out;
730 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200731 err = map->ops->map_update_elem(map, key, value, attr->flags);
732 goto out;
733 }
734
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800735 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
736 * inside bpf map update or delete otherwise deadlocks are possible
737 */
738 preempt_disable();
739 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800740 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
741 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800742 err = bpf_percpu_hash_update(map, key, value, attr->flags);
743 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
744 err = bpf_percpu_array_update(map, key, value, attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100745 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200746 rcu_read_lock();
747 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
748 attr->flags);
749 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700750 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
751 rcu_read_lock();
752 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
753 attr->flags);
754 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800755 } else {
756 rcu_read_lock();
757 err = map->ops->map_update_elem(map, key, value, attr->flags);
758 rcu_read_unlock();
759 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800760 __this_cpu_dec(bpf_prog_active);
761 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200762out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700763free_value:
764 kfree(value);
765free_key:
766 kfree(key);
767err_put:
768 fdput(f);
769 return err;
770}
771
772#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
773
774static int map_delete_elem(union bpf_attr *attr)
775{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100776 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700777 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700778 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200779 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700780 void *key;
781 int err;
782
783 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
784 return -EINVAL;
785
Daniel Borkmann592867b2015-09-08 18:00:09 +0200786 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100787 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700788 if (IS_ERR(map))
789 return PTR_ERR(map);
790
Chenbo Feng6e71b042017-10-18 13:00:22 -0700791 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
792 err = -EPERM;
793 goto err_put;
794 }
795
Al Viroe4448ed2017-05-13 18:43:00 -0400796 key = memdup_user(ukey, map->key_size);
797 if (IS_ERR(key)) {
798 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700799 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400800 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700801
Jakub Kicinskia3884572018-01-11 20:29:09 -0800802 if (bpf_map_is_dev_bound(map)) {
803 err = bpf_map_offload_delete_elem(map, key);
804 goto out;
805 }
806
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800807 preempt_disable();
808 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700809 rcu_read_lock();
810 err = map->ops->map_delete_elem(map, key);
811 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800812 __this_cpu_dec(bpf_prog_active);
813 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800814out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700815 kfree(key);
816err_put:
817 fdput(f);
818 return err;
819}
820
821/* last field in 'union bpf_attr' used by this command */
822#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
823
824static int map_get_next_key(union bpf_attr *attr)
825{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100826 void __user *ukey = u64_to_user_ptr(attr->key);
827 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700828 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700829 struct bpf_map *map;
830 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200831 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700832 int err;
833
834 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
835 return -EINVAL;
836
Daniel Borkmann592867b2015-09-08 18:00:09 +0200837 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100838 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700839 if (IS_ERR(map))
840 return PTR_ERR(map);
841
Chenbo Feng6e71b042017-10-18 13:00:22 -0700842 if (!(f.file->f_mode & FMODE_CAN_READ)) {
843 err = -EPERM;
844 goto err_put;
845 }
846
Teng Qin8fe45922017-04-24 19:00:37 -0700847 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400848 key = memdup_user(ukey, map->key_size);
849 if (IS_ERR(key)) {
850 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700851 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400852 }
Teng Qin8fe45922017-04-24 19:00:37 -0700853 } else {
854 key = NULL;
855 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700856
857 err = -ENOMEM;
858 next_key = kmalloc(map->key_size, GFP_USER);
859 if (!next_key)
860 goto free_key;
861
Jakub Kicinskia3884572018-01-11 20:29:09 -0800862 if (bpf_map_is_dev_bound(map)) {
863 err = bpf_map_offload_get_next_key(map, key, next_key);
864 goto out;
865 }
866
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700867 rcu_read_lock();
868 err = map->ops->map_get_next_key(map, key, next_key);
869 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800870out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700871 if (err)
872 goto free_next_key;
873
874 err = -EFAULT;
875 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
876 goto free_next_key;
877
878 err = 0;
879
880free_next_key:
881 kfree(next_key);
882free_key:
883 kfree(key);
884err_put:
885 fdput(f);
886 return err;
887}
888
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700889static const struct bpf_prog_ops * const bpf_prog_types[] = {
890#define BPF_PROG_TYPE(_id, _name) \
891 [_id] = & _name ## _prog_ops,
892#define BPF_MAP_TYPE(_id, _ops)
893#include <linux/bpf_types.h>
894#undef BPF_PROG_TYPE
895#undef BPF_MAP_TYPE
896};
897
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700898static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
899{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200900 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
901 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700902
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700903 if (!bpf_prog_is_dev_bound(prog->aux))
904 prog->aux->ops = bpf_prog_types[type];
905 else
906 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200907 prog->type = type;
908 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700909}
910
911/* drop refcnt on maps used by eBPF program and free auxilary data */
912static void free_used_maps(struct bpf_prog_aux *aux)
913{
914 int i;
915
916 for (i = 0; i < aux->used_map_cnt; i++)
917 bpf_map_put(aux->used_maps[i]);
918
919 kfree(aux->used_maps);
920}
921
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100922int __bpf_prog_charge(struct user_struct *user, u32 pages)
923{
924 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
925 unsigned long user_bufs;
926
927 if (user) {
928 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
929 if (user_bufs > memlock_limit) {
930 atomic_long_sub(pages, &user->locked_vm);
931 return -EPERM;
932 }
933 }
934
935 return 0;
936}
937
938void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
939{
940 if (user)
941 atomic_long_sub(pages, &user->locked_vm);
942}
943
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700944static int bpf_prog_charge_memlock(struct bpf_prog *prog)
945{
946 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100947 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700948
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100949 ret = __bpf_prog_charge(user, prog->pages);
950 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700951 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100952 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700953 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100954
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700955 prog->aux->user = user;
956 return 0;
957}
958
959static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
960{
961 struct user_struct *user = prog->aux->user;
962
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100963 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700964 free_uid(user);
965}
966
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700967static int bpf_prog_alloc_id(struct bpf_prog *prog)
968{
969 int id;
970
Shaohua Lib76354c2018-03-27 11:53:21 -0700971 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700972 spin_lock_bh(&prog_idr_lock);
973 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
974 if (id > 0)
975 prog->aux->id = id;
976 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700977 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700978
979 /* id is in [1, INT_MAX) */
980 if (WARN_ON_ONCE(!id))
981 return -ENOSPC;
982
983 return id > 0 ? 0 : id;
984}
985
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800986void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700987{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800988 /* cBPF to eBPF migrations are currently not in the idr store.
989 * Offloaded programs are removed from the store when their device
990 * disappears - even if someone grabs an fd to them they are unusable,
991 * simply waiting for refcnt to drop to be freed.
992 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700993 if (!prog->aux->id)
994 return;
995
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700996 if (do_idr_lock)
997 spin_lock_bh(&prog_idr_lock);
998 else
999 __acquire(&prog_idr_lock);
1000
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001001 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001002 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001003
1004 if (do_idr_lock)
1005 spin_unlock_bh(&prog_idr_lock);
1006 else
1007 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001008}
1009
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001010static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001011{
1012 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1013
1014 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001015 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001016 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001017 bpf_prog_free(aux->prog);
1018}
1019
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001020static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001021{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001022 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001023 int i;
1024
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001025 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001026 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001027
1028 for (i = 0; i < prog->aux->func_cnt; i++)
1029 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001030 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001031
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001032 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001033 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001034}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001035
1036void bpf_prog_put(struct bpf_prog *prog)
1037{
1038 __bpf_prog_put(prog, true);
1039}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001040EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001041
1042static int bpf_prog_release(struct inode *inode, struct file *filp)
1043{
1044 struct bpf_prog *prog = filp->private_data;
1045
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001046 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001047 return 0;
1048}
1049
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001050#ifdef CONFIG_PROC_FS
1051static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1052{
1053 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001054 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001055
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001056 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001057 seq_printf(m,
1058 "prog_type:\t%u\n"
1059 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001060 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001061 "memlock:\t%llu\n",
1062 prog->type,
1063 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001064 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001065 prog->pages * 1ULL << PAGE_SHIFT);
1066}
1067#endif
1068
Chenbo Fengf66e4482017-10-18 13:00:26 -07001069const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001070#ifdef CONFIG_PROC_FS
1071 .show_fdinfo = bpf_prog_show_fdinfo,
1072#endif
1073 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001074 .read = bpf_dummy_read,
1075 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001076};
1077
Daniel Borkmannb2197752015-10-29 14:58:09 +01001078int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001079{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001080 int ret;
1081
1082 ret = security_bpf_prog(prog);
1083 if (ret < 0)
1084 return ret;
1085
Daniel Borkmannaa797812015-10-29 14:58:06 +01001086 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1087 O_RDWR | O_CLOEXEC);
1088}
1089
Daniel Borkmann113214b2016-06-30 17:24:44 +02001090static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001091{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001092 if (!f.file)
1093 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001094 if (f.file->f_op != &bpf_prog_fops) {
1095 fdput(f);
1096 return ERR_PTR(-EINVAL);
1097 }
1098
Daniel Borkmannc2101292015-10-29 14:58:07 +01001099 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001100}
1101
Brenden Blanco59d36562016-07-19 12:16:46 -07001102struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001103{
Brenden Blanco59d36562016-07-19 12:16:46 -07001104 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1105 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001106 return ERR_PTR(-EBUSY);
1107 }
1108 return prog;
1109}
Brenden Blanco59d36562016-07-19 12:16:46 -07001110EXPORT_SYMBOL_GPL(bpf_prog_add);
1111
Daniel Borkmannc5405942016-11-09 22:02:34 +01001112void bpf_prog_sub(struct bpf_prog *prog, int i)
1113{
1114 /* Only to be used for undoing previous bpf_prog_add() in some
1115 * error path. We still know that another entity in our call
1116 * path holds a reference to the program, thus atomic_sub() can
1117 * be safely used in such cases!
1118 */
1119 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1120}
1121EXPORT_SYMBOL_GPL(bpf_prog_sub);
1122
Brenden Blanco59d36562016-07-19 12:16:46 -07001123struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1124{
1125 return bpf_prog_add(prog, 1);
1126}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001127EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001128
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001129/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001130struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001131{
1132 int refold;
1133
1134 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1135
1136 if (refold >= BPF_MAX_REFCNT) {
1137 __bpf_prog_put(prog, false);
1138 return ERR_PTR(-EBUSY);
1139 }
1140
1141 if (!refold)
1142 return ERR_PTR(-ENOENT);
1143
1144 return prog;
1145}
John Fastabenda6f6df62017-08-15 22:32:22 -07001146EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001147
Al Viro040ee692017-12-02 20:20:38 -05001148bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001149 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001150{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001151 /* not an attachment, just a refcount inc, always allow */
1152 if (!attach_type)
1153 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001154
1155 if (prog->type != *attach_type)
1156 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001157 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001158 return false;
1159
1160 return true;
1161}
1162
1163static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001164 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001165{
1166 struct fd f = fdget(ufd);
1167 struct bpf_prog *prog;
1168
Daniel Borkmann113214b2016-06-30 17:24:44 +02001169 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001170 if (IS_ERR(prog))
1171 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001172 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001173 prog = ERR_PTR(-EINVAL);
1174 goto out;
1175 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001176
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001177 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001178out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001179 fdput(f);
1180 return prog;
1181}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001182
1183struct bpf_prog *bpf_prog_get(u32 ufd)
1184{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001185 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001186}
1187
Jakub Kicinski248f3462017-11-03 13:56:20 -07001188struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001189 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001190{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001191 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001192}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001193EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001194
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001195/* Initially all BPF programs could be loaded w/o specifying
1196 * expected_attach_type. Later for some of them specifying expected_attach_type
1197 * at load time became required so that program could be validated properly.
1198 * Programs of types that are allowed to be loaded both w/ and w/o (for
1199 * backward compatibility) expected_attach_type, should have the default attach
1200 * type assigned to expected_attach_type for the latter case, so that it can be
1201 * validated later at attach time.
1202 *
1203 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1204 * prog type requires it but has some attach types that have to be backward
1205 * compatible.
1206 */
1207static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1208{
1209 switch (attr->prog_type) {
1210 case BPF_PROG_TYPE_CGROUP_SOCK:
1211 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1212 * exist so checking for non-zero is the way to go here.
1213 */
1214 if (!attr->expected_attach_type)
1215 attr->expected_attach_type =
1216 BPF_CGROUP_INET_SOCK_CREATE;
1217 break;
1218 }
1219}
1220
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001221static int
1222bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1223 enum bpf_attach_type expected_attach_type)
1224{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001225 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001226 case BPF_PROG_TYPE_CGROUP_SOCK:
1227 switch (expected_attach_type) {
1228 case BPF_CGROUP_INET_SOCK_CREATE:
1229 case BPF_CGROUP_INET4_POST_BIND:
1230 case BPF_CGROUP_INET6_POST_BIND:
1231 return 0;
1232 default:
1233 return -EINVAL;
1234 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001235 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1236 switch (expected_attach_type) {
1237 case BPF_CGROUP_INET4_BIND:
1238 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001239 case BPF_CGROUP_INET4_CONNECT:
1240 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001241 return 0;
1242 default:
1243 return -EINVAL;
1244 }
1245 default:
1246 return 0;
1247 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001248}
1249
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001250/* last field in 'union bpf_attr' used by this command */
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001251#define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001252
1253static int bpf_prog_load(union bpf_attr *attr)
1254{
1255 enum bpf_prog_type type = attr->prog_type;
1256 struct bpf_prog *prog;
1257 int err;
1258 char license[128];
1259 bool is_gpl;
1260
1261 if (CHECK_ATTR(BPF_PROG_LOAD))
1262 return -EINVAL;
1263
David S. Millere07b98d2017-05-10 11:38:07 -07001264 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1265 return -EINVAL;
1266
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001267 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001268 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001269 sizeof(license) - 1) < 0)
1270 return -EFAULT;
1271 license[sizeof(license) - 1] = 0;
1272
1273 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1274 is_gpl = license_is_gpl_compatible(license);
1275
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001276 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1277 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001278
Alexei Starovoitov25415172015-03-25 12:49:20 -07001279 if (type == BPF_PROG_TYPE_KPROBE &&
1280 attr->kern_version != LINUX_VERSION_CODE)
1281 return -EINVAL;
1282
Chenbo Feng80b7d812017-05-31 18:16:00 -07001283 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1284 type != BPF_PROG_TYPE_CGROUP_SKB &&
1285 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001286 return -EPERM;
1287
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001288 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001289 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1290 return -EINVAL;
1291
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001292 /* plain bpf_prog allocation */
1293 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1294 if (!prog)
1295 return -ENOMEM;
1296
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001297 prog->expected_attach_type = attr->expected_attach_type;
1298
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001299 prog->aux->offload_requested = !!attr->prog_ifindex;
1300
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001301 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001302 if (err)
1303 goto free_prog_nouncharge;
1304
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001305 err = bpf_prog_charge_memlock(prog);
1306 if (err)
1307 goto free_prog_sec;
1308
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001309 prog->len = attr->insn_cnt;
1310
1311 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001312 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001313 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001314 goto free_prog;
1315
1316 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001317 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001318
1319 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001320 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001321
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001322 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001323 err = bpf_prog_offload_init(prog, attr);
1324 if (err)
1325 goto free_prog;
1326 }
1327
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001328 /* find program type: socket_filter vs tracing_filter */
1329 err = find_prog_type(type, prog);
1330 if (err < 0)
1331 goto free_prog;
1332
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001333 prog->aux->load_time = ktime_get_boot_ns();
1334 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1335 if (err)
1336 goto free_prog;
1337
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001338 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001339 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001340 if (err < 0)
1341 goto free_used_maps;
1342
1343 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001344 if (!prog->bpf_func)
1345 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001346 if (err < 0)
1347 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001348
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001349 err = bpf_prog_alloc_id(prog);
1350 if (err)
1351 goto free_used_maps;
1352
Daniel Borkmannaa797812015-10-29 14:58:06 +01001353 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001354 if (err < 0) {
1355 /* failed to allocate fd.
1356 * bpf_prog_put() is needed because the above
1357 * bpf_prog_alloc_id() has published the prog
1358 * to the userspace and the userspace may
1359 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1360 */
1361 bpf_prog_put(prog);
1362 return err;
1363 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001364
Daniel Borkmann74451e662017-02-16 22:24:50 +01001365 bpf_prog_kallsyms_add(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001366 return err;
1367
1368free_used_maps:
1369 free_used_maps(prog->aux);
1370free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001371 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001372free_prog_sec:
1373 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001374free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001375 bpf_prog_free(prog);
1376 return err;
1377}
1378
Chenbo Feng6e71b042017-10-18 13:00:22 -07001379#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001380
1381static int bpf_obj_pin(const union bpf_attr *attr)
1382{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001383 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001384 return -EINVAL;
1385
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001386 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001387}
1388
1389static int bpf_obj_get(const union bpf_attr *attr)
1390{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001391 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1392 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001393 return -EINVAL;
1394
Chenbo Feng6e71b042017-10-18 13:00:22 -07001395 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1396 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001397}
1398
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001399struct bpf_raw_tracepoint {
1400 struct bpf_raw_event_map *btp;
1401 struct bpf_prog *prog;
1402};
1403
1404static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1405{
1406 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1407
1408 if (raw_tp->prog) {
1409 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1410 bpf_prog_put(raw_tp->prog);
1411 }
1412 kfree(raw_tp);
1413 return 0;
1414}
1415
1416static const struct file_operations bpf_raw_tp_fops = {
1417 .release = bpf_raw_tracepoint_release,
1418 .read = bpf_dummy_read,
1419 .write = bpf_dummy_write,
1420};
1421
1422#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1423
1424static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1425{
1426 struct bpf_raw_tracepoint *raw_tp;
1427 struct bpf_raw_event_map *btp;
1428 struct bpf_prog *prog;
1429 char tp_name[128];
1430 int tp_fd, err;
1431
1432 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1433 sizeof(tp_name) - 1) < 0)
1434 return -EFAULT;
1435 tp_name[sizeof(tp_name) - 1] = 0;
1436
1437 btp = bpf_find_raw_tracepoint(tp_name);
1438 if (!btp)
1439 return -ENOENT;
1440
1441 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1442 if (!raw_tp)
1443 return -ENOMEM;
1444 raw_tp->btp = btp;
1445
1446 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1447 BPF_PROG_TYPE_RAW_TRACEPOINT);
1448 if (IS_ERR(prog)) {
1449 err = PTR_ERR(prog);
1450 goto out_free_tp;
1451 }
1452
1453 err = bpf_probe_register(raw_tp->btp, prog);
1454 if (err)
1455 goto out_put_prog;
1456
1457 raw_tp->prog = prog;
1458 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1459 O_CLOEXEC);
1460 if (tp_fd < 0) {
1461 bpf_probe_unregister(raw_tp->btp, prog);
1462 err = tp_fd;
1463 goto out_put_prog;
1464 }
1465 return tp_fd;
1466
1467out_put_prog:
1468 bpf_prog_put(prog);
1469out_free_tp:
1470 kfree(raw_tp);
1471 return err;
1472}
1473
Daniel Mackf4324552016-11-23 16:52:27 +01001474#ifdef CONFIG_CGROUP_BPF
1475
Anders Roxell33491582018-04-03 14:09:47 +02001476static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1477 enum bpf_attach_type attach_type)
1478{
1479 switch (prog->type) {
1480 case BPF_PROG_TYPE_CGROUP_SOCK:
1481 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1482 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1483 default:
1484 return 0;
1485 }
1486}
1487
John Fastabend464bc0f2017-08-28 07:10:04 -07001488#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001489
John Fastabend4f738ad2018-03-18 12:57:10 -07001490static int sockmap_get_from_fd(const union bpf_attr *attr,
1491 int type, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001492{
John Fastabend5a67da22017-09-08 14:00:49 -07001493 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001494 int ufd = attr->target_fd;
1495 struct bpf_map *map;
1496 struct fd f;
1497 int err;
1498
1499 f = fdget(ufd);
1500 map = __bpf_map_get(f);
1501 if (IS_ERR(map))
1502 return PTR_ERR(map);
1503
John Fastabend5a67da22017-09-08 14:00:49 -07001504 if (attach) {
John Fastabend4f738ad2018-03-18 12:57:10 -07001505 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
John Fastabend5a67da22017-09-08 14:00:49 -07001506 if (IS_ERR(prog)) {
1507 fdput(f);
1508 return PTR_ERR(prog);
1509 }
John Fastabend174a79f2017-08-15 22:32:47 -07001510 }
1511
John Fastabend5a67da22017-09-08 14:00:49 -07001512 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001513 if (err) {
1514 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001515 if (prog)
1516 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001517 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001518 }
1519
1520 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001521 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001522}
Daniel Mackf4324552016-11-23 16:52:27 +01001523
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001524#define BPF_F_ATTACH_MASK \
1525 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1526
Daniel Mackf4324552016-11-23 16:52:27 +01001527static int bpf_prog_attach(const union bpf_attr *attr)
1528{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001529 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001530 struct bpf_prog *prog;
1531 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001532 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001533
1534 if (!capable(CAP_NET_ADMIN))
1535 return -EPERM;
1536
1537 if (CHECK_ATTR(BPF_PROG_ATTACH))
1538 return -EINVAL;
1539
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001540 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001541 return -EINVAL;
1542
Daniel Mackf4324552016-11-23 16:52:27 +01001543 switch (attr->attach_type) {
1544 case BPF_CGROUP_INET_INGRESS:
1545 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001546 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001547 break;
David Ahern610236582016-12-01 08:48:04 -08001548 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001549 case BPF_CGROUP_INET4_POST_BIND:
1550 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001551 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1552 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001553 case BPF_CGROUP_INET4_BIND:
1554 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001555 case BPF_CGROUP_INET4_CONNECT:
1556 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001557 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1558 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001559 case BPF_CGROUP_SOCK_OPS:
1560 ptype = BPF_PROG_TYPE_SOCK_OPS;
1561 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001562 case BPF_CGROUP_DEVICE:
1563 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1564 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001565 case BPF_SK_MSG_VERDICT:
1566 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
John Fastabend464bc0f2017-08-28 07:10:04 -07001567 case BPF_SK_SKB_STREAM_PARSER:
1568 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001569 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001570 default:
1571 return -EINVAL;
1572 }
1573
David Ahernb2cd1252016-12-01 08:48:03 -08001574 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1575 if (IS_ERR(prog))
1576 return PTR_ERR(prog);
1577
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001578 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1579 bpf_prog_put(prog);
1580 return -EINVAL;
1581 }
1582
David Ahernb2cd1252016-12-01 08:48:03 -08001583 cgrp = cgroup_get_from_fd(attr->target_fd);
1584 if (IS_ERR(cgrp)) {
1585 bpf_prog_put(prog);
1586 return PTR_ERR(cgrp);
1587 }
1588
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001589 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1590 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001591 if (ret)
1592 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001593 cgroup_put(cgrp);
1594
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001595 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001596}
1597
1598#define BPF_PROG_DETACH_LAST_FIELD attach_type
1599
1600static int bpf_prog_detach(const union bpf_attr *attr)
1601{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001602 enum bpf_prog_type ptype;
1603 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001604 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001605 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001606
1607 if (!capable(CAP_NET_ADMIN))
1608 return -EPERM;
1609
1610 if (CHECK_ATTR(BPF_PROG_DETACH))
1611 return -EINVAL;
1612
1613 switch (attr->attach_type) {
1614 case BPF_CGROUP_INET_INGRESS:
1615 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001616 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1617 break;
David Ahern610236582016-12-01 08:48:04 -08001618 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001619 case BPF_CGROUP_INET4_POST_BIND:
1620 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001621 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1622 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001623 case BPF_CGROUP_INET4_BIND:
1624 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001625 case BPF_CGROUP_INET4_CONNECT:
1626 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001627 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1628 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001629 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001630 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001631 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001632 case BPF_CGROUP_DEVICE:
1633 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1634 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001635 case BPF_SK_MSG_VERDICT:
1636 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
John Fastabend5a67da22017-09-08 14:00:49 -07001637 case BPF_SK_SKB_STREAM_PARSER:
1638 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001639 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001640 default:
1641 return -EINVAL;
1642 }
1643
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001644 cgrp = cgroup_get_from_fd(attr->target_fd);
1645 if (IS_ERR(cgrp))
1646 return PTR_ERR(cgrp);
1647
1648 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1649 if (IS_ERR(prog))
1650 prog = NULL;
1651
1652 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1653 if (prog)
1654 bpf_prog_put(prog);
1655 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001656 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001657}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001658
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001659#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1660
1661static int bpf_prog_query(const union bpf_attr *attr,
1662 union bpf_attr __user *uattr)
1663{
1664 struct cgroup *cgrp;
1665 int ret;
1666
1667 if (!capable(CAP_NET_ADMIN))
1668 return -EPERM;
1669 if (CHECK_ATTR(BPF_PROG_QUERY))
1670 return -EINVAL;
1671 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1672 return -EINVAL;
1673
1674 switch (attr->query.attach_type) {
1675 case BPF_CGROUP_INET_INGRESS:
1676 case BPF_CGROUP_INET_EGRESS:
1677 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001678 case BPF_CGROUP_INET4_BIND:
1679 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001680 case BPF_CGROUP_INET4_POST_BIND:
1681 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001682 case BPF_CGROUP_INET4_CONNECT:
1683 case BPF_CGROUP_INET6_CONNECT:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001684 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001685 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001686 break;
1687 default:
1688 return -EINVAL;
1689 }
1690 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1691 if (IS_ERR(cgrp))
1692 return PTR_ERR(cgrp);
1693 ret = cgroup_bpf_query(cgrp, attr, uattr);
1694 cgroup_put(cgrp);
1695 return ret;
1696}
Daniel Mackf4324552016-11-23 16:52:27 +01001697#endif /* CONFIG_CGROUP_BPF */
1698
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001699#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1700
1701static int bpf_prog_test_run(const union bpf_attr *attr,
1702 union bpf_attr __user *uattr)
1703{
1704 struct bpf_prog *prog;
1705 int ret = -ENOTSUPP;
1706
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001707 if (!capable(CAP_SYS_ADMIN))
1708 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001709 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1710 return -EINVAL;
1711
1712 prog = bpf_prog_get(attr->test.prog_fd);
1713 if (IS_ERR(prog))
1714 return PTR_ERR(prog);
1715
1716 if (prog->aux->ops->test_run)
1717 ret = prog->aux->ops->test_run(prog, attr, uattr);
1718
1719 bpf_prog_put(prog);
1720 return ret;
1721}
1722
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001723#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1724
1725static int bpf_obj_get_next_id(const union bpf_attr *attr,
1726 union bpf_attr __user *uattr,
1727 struct idr *idr,
1728 spinlock_t *lock)
1729{
1730 u32 next_id = attr->start_id;
1731 int err = 0;
1732
1733 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1734 return -EINVAL;
1735
1736 if (!capable(CAP_SYS_ADMIN))
1737 return -EPERM;
1738
1739 next_id++;
1740 spin_lock_bh(lock);
1741 if (!idr_get_next(idr, &next_id))
1742 err = -ENOENT;
1743 spin_unlock_bh(lock);
1744
1745 if (!err)
1746 err = put_user(next_id, &uattr->next_id);
1747
1748 return err;
1749}
1750
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001751#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1752
1753static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1754{
1755 struct bpf_prog *prog;
1756 u32 id = attr->prog_id;
1757 int fd;
1758
1759 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1760 return -EINVAL;
1761
1762 if (!capable(CAP_SYS_ADMIN))
1763 return -EPERM;
1764
1765 spin_lock_bh(&prog_idr_lock);
1766 prog = idr_find(&prog_idr, id);
1767 if (prog)
1768 prog = bpf_prog_inc_not_zero(prog);
1769 else
1770 prog = ERR_PTR(-ENOENT);
1771 spin_unlock_bh(&prog_idr_lock);
1772
1773 if (IS_ERR(prog))
1774 return PTR_ERR(prog);
1775
1776 fd = bpf_prog_new_fd(prog);
1777 if (fd < 0)
1778 bpf_prog_put(prog);
1779
1780 return fd;
1781}
1782
Chenbo Feng6e71b042017-10-18 13:00:22 -07001783#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001784
1785static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1786{
1787 struct bpf_map *map;
1788 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001789 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001790 int fd;
1791
Chenbo Feng6e71b042017-10-18 13:00:22 -07001792 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1793 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001794 return -EINVAL;
1795
1796 if (!capable(CAP_SYS_ADMIN))
1797 return -EPERM;
1798
Chenbo Feng6e71b042017-10-18 13:00:22 -07001799 f_flags = bpf_get_file_flag(attr->open_flags);
1800 if (f_flags < 0)
1801 return f_flags;
1802
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001803 spin_lock_bh(&map_idr_lock);
1804 map = idr_find(&map_idr, id);
1805 if (map)
1806 map = bpf_map_inc_not_zero(map, true);
1807 else
1808 map = ERR_PTR(-ENOENT);
1809 spin_unlock_bh(&map_idr_lock);
1810
1811 if (IS_ERR(map))
1812 return PTR_ERR(map);
1813
Chenbo Feng6e71b042017-10-18 13:00:22 -07001814 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001815 if (fd < 0)
1816 bpf_map_put(map);
1817
1818 return fd;
1819}
1820
Daniel Borkmann7105e822017-12-20 13:42:57 +01001821static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1822 unsigned long addr)
1823{
1824 int i;
1825
1826 for (i = 0; i < prog->aux->used_map_cnt; i++)
1827 if (prog->aux->used_maps[i] == (void *)addr)
1828 return prog->aux->used_maps[i];
1829 return NULL;
1830}
1831
1832static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1833{
1834 const struct bpf_map *map;
1835 struct bpf_insn *insns;
1836 u64 imm;
1837 int i;
1838
1839 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1840 GFP_USER);
1841 if (!insns)
1842 return insns;
1843
1844 for (i = 0; i < prog->len; i++) {
1845 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1846 insns[i].code = BPF_JMP | BPF_CALL;
1847 insns[i].imm = BPF_FUNC_tail_call;
1848 /* fall-through */
1849 }
1850 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1851 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1852 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1853 insns[i].code = BPF_JMP | BPF_CALL;
1854 if (!bpf_dump_raw_ok())
1855 insns[i].imm = 0;
1856 continue;
1857 }
1858
1859 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1860 continue;
1861
1862 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1863 map = bpf_map_from_imm(prog, imm);
1864 if (map) {
1865 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1866 insns[i].imm = map->id;
1867 insns[i + 1].imm = 0;
1868 continue;
1869 }
1870
1871 if (!bpf_dump_raw_ok() &&
1872 imm == (unsigned long)prog->aux) {
1873 insns[i].imm = 0;
1874 insns[i + 1].imm = 0;
1875 continue;
1876 }
1877 }
1878
1879 return insns;
1880}
1881
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001882static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1883 const union bpf_attr *attr,
1884 union bpf_attr __user *uattr)
1885{
1886 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1887 struct bpf_prog_info info = {};
1888 u32 info_len = attr->info.info_len;
1889 char __user *uinsns;
1890 u32 ulen;
1891 int err;
1892
1893 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1894 if (err)
1895 return err;
1896 info_len = min_t(u32, sizeof(info), info_len);
1897
1898 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001899 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001900
1901 info.type = prog->type;
1902 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001903 info.load_time = prog->aux->load_time;
1904 info.created_by_uid = from_kuid_munged(current_user_ns(),
1905 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02001906 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001907
1908 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001909 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1910
1911 ulen = info.nr_map_ids;
1912 info.nr_map_ids = prog->aux->used_map_cnt;
1913 ulen = min_t(u32, info.nr_map_ids, ulen);
1914 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001915 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001916 u32 i;
1917
1918 for (i = 0; i < ulen; i++)
1919 if (put_user(prog->aux->used_maps[i]->id,
1920 &user_map_ids[i]))
1921 return -EFAULT;
1922 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001923
1924 if (!capable(CAP_SYS_ADMIN)) {
1925 info.jited_prog_len = 0;
1926 info.xlated_prog_len = 0;
1927 goto done;
1928 }
1929
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001930 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001931 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001932 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001933 struct bpf_insn *insns_sanitized;
1934 bool fault;
1935
1936 if (prog->blinded && !bpf_dump_raw_ok()) {
1937 info.xlated_prog_insns = 0;
1938 goto done;
1939 }
1940 insns_sanitized = bpf_insn_prepare_dump(prog);
1941 if (!insns_sanitized)
1942 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001943 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1944 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001945 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1946 kfree(insns_sanitized);
1947 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001948 return -EFAULT;
1949 }
1950
Jakub Kicinski675fc272017-12-27 18:39:09 -08001951 if (bpf_prog_is_dev_bound(prog->aux)) {
1952 err = bpf_prog_offload_info_fill(&info, prog);
1953 if (err)
1954 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001955 goto done;
1956 }
1957
1958 /* NOTE: the following code is supposed to be skipped for offload.
1959 * bpf_prog_offload_info_fill() is the place to fill similar fields
1960 * for offload.
1961 */
1962 ulen = info.jited_prog_len;
1963 info.jited_prog_len = prog->jited_len;
1964 if (info.jited_prog_len && ulen) {
1965 if (bpf_dump_raw_ok()) {
1966 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1967 ulen = min_t(u32, info.jited_prog_len, ulen);
1968 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1969 return -EFAULT;
1970 } else {
1971 info.jited_prog_insns = 0;
1972 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08001973 }
1974
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001975done:
1976 if (copy_to_user(uinfo, &info, info_len) ||
1977 put_user(info_len, &uattr->info.info_len))
1978 return -EFAULT;
1979
1980 return 0;
1981}
1982
1983static int bpf_map_get_info_by_fd(struct bpf_map *map,
1984 const union bpf_attr *attr,
1985 union bpf_attr __user *uattr)
1986{
1987 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1988 struct bpf_map_info info = {};
1989 u32 info_len = attr->info.info_len;
1990 int err;
1991
1992 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1993 if (err)
1994 return err;
1995 info_len = min_t(u32, sizeof(info), info_len);
1996
1997 info.type = map->map_type;
1998 info.id = map->id;
1999 info.key_size = map->key_size;
2000 info.value_size = map->value_size;
2001 info.max_entries = map->max_entries;
2002 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002003 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002004
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002005 if (map->btf) {
2006 info.btf_id = btf_id(map->btf);
2007 info.btf_key_id = map->btf_key_id;
2008 info.btf_value_id = map->btf_value_id;
2009 }
2010
Jakub Kicinski52775b32018-01-17 19:13:28 -08002011 if (bpf_map_is_dev_bound(map)) {
2012 err = bpf_map_offload_info_fill(&info, map);
2013 if (err)
2014 return err;
2015 }
2016
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002017 if (copy_to_user(uinfo, &info, info_len) ||
2018 put_user(info_len, &uattr->info.info_len))
2019 return -EFAULT;
2020
2021 return 0;
2022}
2023
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002024static int bpf_btf_get_info_by_fd(struct btf *btf,
2025 const union bpf_attr *attr,
2026 union bpf_attr __user *uattr)
2027{
2028 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2029 u32 info_len = attr->info.info_len;
2030 int err;
2031
2032 err = check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2033 if (err)
2034 return err;
2035
2036 return btf_get_info_by_fd(btf, attr, uattr);
2037}
2038
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002039#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2040
2041static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2042 union bpf_attr __user *uattr)
2043{
2044 int ufd = attr->info.bpf_fd;
2045 struct fd f;
2046 int err;
2047
2048 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2049 return -EINVAL;
2050
2051 f = fdget(ufd);
2052 if (!f.file)
2053 return -EBADFD;
2054
2055 if (f.file->f_op == &bpf_prog_fops)
2056 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2057 uattr);
2058 else if (f.file->f_op == &bpf_map_fops)
2059 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2060 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002061 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002062 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002063 else
2064 err = -EINVAL;
2065
2066 fdput(f);
2067 return err;
2068}
2069
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002070#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2071
2072static int bpf_btf_load(const union bpf_attr *attr)
2073{
2074 if (CHECK_ATTR(BPF_BTF_LOAD))
2075 return -EINVAL;
2076
2077 if (!capable(CAP_SYS_ADMIN))
2078 return -EPERM;
2079
2080 return btf_new_fd(attr);
2081}
2082
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002083#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2084
2085static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2086{
2087 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2088 return -EINVAL;
2089
2090 if (!capable(CAP_SYS_ADMIN))
2091 return -EPERM;
2092
2093 return btf_get_fd_by_id(attr->btf_id);
2094}
2095
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002096SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2097{
2098 union bpf_attr attr = {};
2099 int err;
2100
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002101 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002102 return -EPERM;
2103
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002104 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
2105 if (err)
2106 return err;
2107 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002108
2109 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2110 if (copy_from_user(&attr, uattr, size) != 0)
2111 return -EFAULT;
2112
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002113 err = security_bpf(cmd, &attr, size);
2114 if (err < 0)
2115 return err;
2116
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002117 switch (cmd) {
2118 case BPF_MAP_CREATE:
2119 err = map_create(&attr);
2120 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002121 case BPF_MAP_LOOKUP_ELEM:
2122 err = map_lookup_elem(&attr);
2123 break;
2124 case BPF_MAP_UPDATE_ELEM:
2125 err = map_update_elem(&attr);
2126 break;
2127 case BPF_MAP_DELETE_ELEM:
2128 err = map_delete_elem(&attr);
2129 break;
2130 case BPF_MAP_GET_NEXT_KEY:
2131 err = map_get_next_key(&attr);
2132 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002133 case BPF_PROG_LOAD:
2134 err = bpf_prog_load(&attr);
2135 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002136 case BPF_OBJ_PIN:
2137 err = bpf_obj_pin(&attr);
2138 break;
2139 case BPF_OBJ_GET:
2140 err = bpf_obj_get(&attr);
2141 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002142#ifdef CONFIG_CGROUP_BPF
2143 case BPF_PROG_ATTACH:
2144 err = bpf_prog_attach(&attr);
2145 break;
2146 case BPF_PROG_DETACH:
2147 err = bpf_prog_detach(&attr);
2148 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002149 case BPF_PROG_QUERY:
2150 err = bpf_prog_query(&attr, uattr);
2151 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002152#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002153 case BPF_PROG_TEST_RUN:
2154 err = bpf_prog_test_run(&attr, uattr);
2155 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002156 case BPF_PROG_GET_NEXT_ID:
2157 err = bpf_obj_get_next_id(&attr, uattr,
2158 &prog_idr, &prog_idr_lock);
2159 break;
2160 case BPF_MAP_GET_NEXT_ID:
2161 err = bpf_obj_get_next_id(&attr, uattr,
2162 &map_idr, &map_idr_lock);
2163 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002164 case BPF_PROG_GET_FD_BY_ID:
2165 err = bpf_prog_get_fd_by_id(&attr);
2166 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002167 case BPF_MAP_GET_FD_BY_ID:
2168 err = bpf_map_get_fd_by_id(&attr);
2169 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002170 case BPF_OBJ_GET_INFO_BY_FD:
2171 err = bpf_obj_get_info_by_fd(&attr, uattr);
2172 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002173 case BPF_RAW_TRACEPOINT_OPEN:
2174 err = bpf_raw_tracepoint_open(&attr);
2175 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002176 case BPF_BTF_LOAD:
2177 err = bpf_btf_load(&attr);
2178 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002179 case BPF_BTF_GET_FD_BY_ID:
2180 err = bpf_btf_get_fd_by_id(&attr);
2181 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002182 default:
2183 err = -EINVAL;
2184 break;
2185 }
2186
2187 return err;
2188}