Commit | Line | Data |
---|---|---|
99c55f7d AS |
1 | /* 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> | |
a67edbf4 | 13 | #include <linux/bpf_trace.h> |
f4364dcf | 14 | #include <linux/bpf_lirc.h> |
f56a653c | 15 | #include <linux/btf.h> |
99c55f7d AS |
16 | #include <linux/syscalls.h> |
17 | #include <linux/slab.h> | |
3f07c014 | 18 | #include <linux/sched/signal.h> |
d407bd25 DB |
19 | #include <linux/vmalloc.h> |
20 | #include <linux/mmzone.h> | |
99c55f7d | 21 | #include <linux/anon_inodes.h> |
41bdc4b4 | 22 | #include <linux/fdtable.h> |
db20fd2b | 23 | #include <linux/file.h> |
41bdc4b4 | 24 | #include <linux/fs.h> |
09756af4 AS |
25 | #include <linux/license.h> |
26 | #include <linux/filter.h> | |
2541517c | 27 | #include <linux/version.h> |
535e7b4b | 28 | #include <linux/kernel.h> |
dc4bb0e2 | 29 | #include <linux/idr.h> |
cb4d2b3f MKL |
30 | #include <linux/cred.h> |
31 | #include <linux/timekeeping.h> | |
32 | #include <linux/ctype.h> | |
9ef09e35 | 33 | #include <linux/nospec.h> |
99c55f7d | 34 | |
14dc6f04 MKL |
35 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
36 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ | |
37 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ | |
38 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) | |
39 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) | |
40 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) | |
41 | ||
6e71b04a CF |
42 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
43 | ||
b121d1e7 | 44 | DEFINE_PER_CPU(int, bpf_prog_active); |
dc4bb0e2 MKL |
45 | static DEFINE_IDR(prog_idr); |
46 | static DEFINE_SPINLOCK(prog_idr_lock); | |
f3f1c054 MKL |
47 | static DEFINE_IDR(map_idr); |
48 | static DEFINE_SPINLOCK(map_idr_lock); | |
b121d1e7 | 49 | |
1be7f75d AS |
50 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
51 | ||
40077e0c JB |
52 | static const struct bpf_map_ops * const bpf_map_types[] = { |
53 | #define BPF_PROG_TYPE(_id, _ops) | |
54 | #define BPF_MAP_TYPE(_id, _ops) \ | |
55 | [_id] = &_ops, | |
56 | #include <linux/bpf_types.h> | |
57 | #undef BPF_PROG_TYPE | |
58 | #undef BPF_MAP_TYPE | |
59 | }; | |
99c55f7d | 60 | |
752ba56f MS |
61 | /* |
62 | * If we're handed a bigger struct than we know of, ensure all the unknown bits | |
63 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions | |
64 | * we don't know about yet. | |
65 | * | |
66 | * There is a ToCToU between this function call and the following | |
67 | * copy_from_user() call. However, this is not a concern since this function is | |
68 | * meant to be a future-proofing of bits. | |
69 | */ | |
dcab51f1 MKL |
70 | int bpf_check_uarg_tail_zero(void __user *uaddr, |
71 | size_t expected_size, | |
72 | size_t actual_size) | |
58291a74 MS |
73 | { |
74 | unsigned char __user *addr; | |
75 | unsigned char __user *end; | |
76 | unsigned char val; | |
77 | int err; | |
78 | ||
752ba56f MS |
79 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
80 | return -E2BIG; | |
81 | ||
96d4f267 | 82 | if (unlikely(!access_ok(uaddr, actual_size))) |
752ba56f MS |
83 | return -EFAULT; |
84 | ||
58291a74 MS |
85 | if (actual_size <= expected_size) |
86 | return 0; | |
87 | ||
88 | addr = uaddr + expected_size; | |
89 | end = uaddr + actual_size; | |
90 | ||
91 | for (; addr < end; addr++) { | |
92 | err = get_user(val, addr); | |
93 | if (err) | |
94 | return err; | |
95 | if (val) | |
96 | return -E2BIG; | |
97 | } | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
a3884572 JK |
102 | const struct bpf_map_ops bpf_map_offload_ops = { |
103 | .map_alloc = bpf_map_offload_map_alloc, | |
104 | .map_free = bpf_map_offload_map_free, | |
e8d2bec0 | 105 | .map_check_btf = map_check_no_btf, |
a3884572 JK |
106 | }; |
107 | ||
99c55f7d AS |
108 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
109 | { | |
1110f3a9 | 110 | const struct bpf_map_ops *ops; |
9ef09e35 | 111 | u32 type = attr->map_type; |
99c55f7d | 112 | struct bpf_map *map; |
1110f3a9 | 113 | int err; |
99c55f7d | 114 | |
9ef09e35 | 115 | if (type >= ARRAY_SIZE(bpf_map_types)) |
1110f3a9 | 116 | return ERR_PTR(-EINVAL); |
9ef09e35 MR |
117 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
118 | ops = bpf_map_types[type]; | |
1110f3a9 | 119 | if (!ops) |
40077e0c | 120 | return ERR_PTR(-EINVAL); |
99c55f7d | 121 | |
1110f3a9 JK |
122 | if (ops->map_alloc_check) { |
123 | err = ops->map_alloc_check(attr); | |
124 | if (err) | |
125 | return ERR_PTR(err); | |
126 | } | |
a3884572 JK |
127 | if (attr->map_ifindex) |
128 | ops = &bpf_map_offload_ops; | |
1110f3a9 | 129 | map = ops->map_alloc(attr); |
40077e0c JB |
130 | if (IS_ERR(map)) |
131 | return map; | |
1110f3a9 | 132 | map->ops = ops; |
9ef09e35 | 133 | map->map_type = type; |
40077e0c | 134 | return map; |
99c55f7d AS |
135 | } |
136 | ||
96eabe7a | 137 | void *bpf_map_area_alloc(size_t size, int numa_node) |
d407bd25 DB |
138 | { |
139 | /* We definitely need __GFP_NORETRY, so OOM killer doesn't | |
140 | * trigger under memory pressure as we really just want to | |
141 | * fail instead. | |
142 | */ | |
143 | const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; | |
144 | void *area; | |
145 | ||
146 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { | |
96eabe7a | 147 | area = kmalloc_node(size, GFP_USER | flags, numa_node); |
d407bd25 DB |
148 | if (area != NULL) |
149 | return area; | |
150 | } | |
151 | ||
96eabe7a MKL |
152 | return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, |
153 | __builtin_return_address(0)); | |
d407bd25 DB |
154 | } |
155 | ||
156 | void bpf_map_area_free(void *area) | |
157 | { | |
158 | kvfree(area); | |
159 | } | |
160 | ||
bd475643 JK |
161 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
162 | { | |
163 | map->map_type = attr->map_type; | |
164 | map->key_size = attr->key_size; | |
165 | map->value_size = attr->value_size; | |
166 | map->max_entries = attr->max_entries; | |
167 | map->map_flags = attr->map_flags; | |
168 | map->numa_node = bpf_map_attr_numa_node(attr); | |
169 | } | |
170 | ||
6c905981 AS |
171 | int bpf_map_precharge_memlock(u32 pages) |
172 | { | |
173 | struct user_struct *user = get_current_user(); | |
174 | unsigned long memlock_limit, cur; | |
175 | ||
176 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
177 | cur = atomic_long_read(&user->locked_vm); | |
178 | free_uid(user); | |
179 | if (cur + pages > memlock_limit) | |
180 | return -EPERM; | |
181 | return 0; | |
182 | } | |
183 | ||
0a4c58f5 | 184 | static int bpf_charge_memlock(struct user_struct *user, u32 pages) |
aaac3ba9 | 185 | { |
0a4c58f5 | 186 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
aaac3ba9 | 187 | |
0a4c58f5 RG |
188 | if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) { |
189 | atomic_long_sub(pages, &user->locked_vm); | |
190 | return -EPERM; | |
191 | } | |
192 | return 0; | |
193 | } | |
aaac3ba9 | 194 | |
0a4c58f5 RG |
195 | static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) |
196 | { | |
197 | atomic_long_sub(pages, &user->locked_vm); | |
198 | } | |
aaac3ba9 | 199 | |
0a4c58f5 RG |
200 | static int bpf_map_init_memlock(struct bpf_map *map) |
201 | { | |
202 | struct user_struct *user = get_current_user(); | |
203 | int ret; | |
aaac3ba9 | 204 | |
0a4c58f5 RG |
205 | ret = bpf_charge_memlock(user, map->pages); |
206 | if (ret) { | |
aaac3ba9 | 207 | free_uid(user); |
0a4c58f5 | 208 | return ret; |
aaac3ba9 AS |
209 | } |
210 | map->user = user; | |
0a4c58f5 | 211 | return ret; |
aaac3ba9 AS |
212 | } |
213 | ||
0a4c58f5 | 214 | static void bpf_map_release_memlock(struct bpf_map *map) |
aaac3ba9 AS |
215 | { |
216 | struct user_struct *user = map->user; | |
0a4c58f5 | 217 | bpf_uncharge_memlock(user, map->pages); |
aaac3ba9 AS |
218 | free_uid(user); |
219 | } | |
220 | ||
0a4c58f5 RG |
221 | int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) |
222 | { | |
223 | int ret; | |
224 | ||
225 | ret = bpf_charge_memlock(map->user, pages); | |
226 | if (ret) | |
227 | return ret; | |
228 | map->pages += pages; | |
229 | return ret; | |
230 | } | |
231 | ||
232 | void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) | |
233 | { | |
234 | bpf_uncharge_memlock(map->user, pages); | |
235 | map->pages -= pages; | |
236 | } | |
237 | ||
f3f1c054 MKL |
238 | static int bpf_map_alloc_id(struct bpf_map *map) |
239 | { | |
240 | int id; | |
241 | ||
b76354cd | 242 | idr_preload(GFP_KERNEL); |
f3f1c054 MKL |
243 | spin_lock_bh(&map_idr_lock); |
244 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); | |
245 | if (id > 0) | |
246 | map->id = id; | |
247 | spin_unlock_bh(&map_idr_lock); | |
b76354cd | 248 | idr_preload_end(); |
f3f1c054 MKL |
249 | |
250 | if (WARN_ON_ONCE(!id)) | |
251 | return -ENOSPC; | |
252 | ||
253 | return id > 0 ? 0 : id; | |
254 | } | |
255 | ||
a3884572 | 256 | void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) |
f3f1c054 | 257 | { |
930651a7 ED |
258 | unsigned long flags; |
259 | ||
a3884572 JK |
260 | /* Offloaded maps are removed from the IDR store when their device |
261 | * disappears - even if someone holds an fd to them they are unusable, | |
262 | * the memory is gone, all ops will fail; they are simply waiting for | |
263 | * refcnt to drop to be freed. | |
264 | */ | |
265 | if (!map->id) | |
266 | return; | |
267 | ||
bd5f5f4e | 268 | if (do_idr_lock) |
930651a7 | 269 | spin_lock_irqsave(&map_idr_lock, flags); |
bd5f5f4e MKL |
270 | else |
271 | __acquire(&map_idr_lock); | |
272 | ||
f3f1c054 | 273 | idr_remove(&map_idr, map->id); |
a3884572 | 274 | map->id = 0; |
bd5f5f4e MKL |
275 | |
276 | if (do_idr_lock) | |
930651a7 | 277 | spin_unlock_irqrestore(&map_idr_lock, flags); |
bd5f5f4e MKL |
278 | else |
279 | __release(&map_idr_lock); | |
f3f1c054 MKL |
280 | } |
281 | ||
99c55f7d AS |
282 | /* called from workqueue */ |
283 | static void bpf_map_free_deferred(struct work_struct *work) | |
284 | { | |
285 | struct bpf_map *map = container_of(work, struct bpf_map, work); | |
286 | ||
0a4c58f5 | 287 | bpf_map_release_memlock(map); |
afdb09c7 | 288 | security_bpf_map_free(map); |
99c55f7d AS |
289 | /* implementation dependent freeing */ |
290 | map->ops->map_free(map); | |
291 | } | |
292 | ||
c9da161c DB |
293 | static void bpf_map_put_uref(struct bpf_map *map) |
294 | { | |
295 | if (atomic_dec_and_test(&map->usercnt)) { | |
ba6b8de4 JF |
296 | if (map->ops->map_release_uref) |
297 | map->ops->map_release_uref(map); | |
c9da161c DB |
298 | } |
299 | } | |
300 | ||
99c55f7d AS |
301 | /* decrement map refcnt and schedule it for freeing via workqueue |
302 | * (unrelying map implementation ops->map_free() might sleep) | |
303 | */ | |
bd5f5f4e | 304 | static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) |
99c55f7d AS |
305 | { |
306 | if (atomic_dec_and_test(&map->refcnt)) { | |
34ad5580 | 307 | /* bpf_map_free_id() must be called first */ |
bd5f5f4e | 308 | bpf_map_free_id(map, do_idr_lock); |
78958fca | 309 | btf_put(map->btf); |
99c55f7d AS |
310 | INIT_WORK(&map->work, bpf_map_free_deferred); |
311 | schedule_work(&map->work); | |
312 | } | |
313 | } | |
314 | ||
bd5f5f4e MKL |
315 | void bpf_map_put(struct bpf_map *map) |
316 | { | |
317 | __bpf_map_put(map, true); | |
318 | } | |
630a4d38 | 319 | EXPORT_SYMBOL_GPL(bpf_map_put); |
bd5f5f4e | 320 | |
c9da161c | 321 | void bpf_map_put_with_uref(struct bpf_map *map) |
99c55f7d | 322 | { |
c9da161c | 323 | bpf_map_put_uref(map); |
99c55f7d | 324 | bpf_map_put(map); |
c9da161c DB |
325 | } |
326 | ||
327 | static int bpf_map_release(struct inode *inode, struct file *filp) | |
328 | { | |
61d1b6a4 DB |
329 | struct bpf_map *map = filp->private_data; |
330 | ||
331 | if (map->ops->map_release) | |
332 | map->ops->map_release(map, filp); | |
333 | ||
334 | bpf_map_put_with_uref(map); | |
99c55f7d AS |
335 | return 0; |
336 | } | |
337 | ||
f99bf205 DB |
338 | #ifdef CONFIG_PROC_FS |
339 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) | |
340 | { | |
341 | const struct bpf_map *map = filp->private_data; | |
21116b70 DB |
342 | const struct bpf_array *array; |
343 | u32 owner_prog_type = 0; | |
9780c0ab | 344 | u32 owner_jited = 0; |
21116b70 DB |
345 | |
346 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { | |
347 | array = container_of(map, struct bpf_array, map); | |
348 | owner_prog_type = array->owner_prog_type; | |
9780c0ab | 349 | owner_jited = array->owner_jited; |
21116b70 | 350 | } |
f99bf205 DB |
351 | |
352 | seq_printf(m, | |
353 | "map_type:\t%u\n" | |
354 | "key_size:\t%u\n" | |
355 | "value_size:\t%u\n" | |
322cea2f | 356 | "max_entries:\t%u\n" |
21116b70 | 357 | "map_flags:\t%#x\n" |
4316b409 DB |
358 | "memlock:\t%llu\n" |
359 | "map_id:\t%u\n", | |
f99bf205 DB |
360 | map->map_type, |
361 | map->key_size, | |
362 | map->value_size, | |
322cea2f | 363 | map->max_entries, |
21116b70 | 364 | map->map_flags, |
4316b409 DB |
365 | map->pages * 1ULL << PAGE_SHIFT, |
366 | map->id); | |
21116b70 | 367 | |
9780c0ab | 368 | if (owner_prog_type) { |
21116b70 DB |
369 | seq_printf(m, "owner_prog_type:\t%u\n", |
370 | owner_prog_type); | |
9780c0ab DB |
371 | seq_printf(m, "owner_jited:\t%u\n", |
372 | owner_jited); | |
373 | } | |
f99bf205 DB |
374 | } |
375 | #endif | |
376 | ||
6e71b04a CF |
377 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
378 | loff_t *ppos) | |
379 | { | |
380 | /* We need this handler such that alloc_file() enables | |
381 | * f_mode with FMODE_CAN_READ. | |
382 | */ | |
383 | return -EINVAL; | |
384 | } | |
385 | ||
386 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, | |
387 | size_t siz, loff_t *ppos) | |
388 | { | |
389 | /* We need this handler such that alloc_file() enables | |
390 | * f_mode with FMODE_CAN_WRITE. | |
391 | */ | |
392 | return -EINVAL; | |
393 | } | |
394 | ||
f66e448c | 395 | const struct file_operations bpf_map_fops = { |
f99bf205 DB |
396 | #ifdef CONFIG_PROC_FS |
397 | .show_fdinfo = bpf_map_show_fdinfo, | |
398 | #endif | |
399 | .release = bpf_map_release, | |
6e71b04a CF |
400 | .read = bpf_dummy_read, |
401 | .write = bpf_dummy_write, | |
99c55f7d AS |
402 | }; |
403 | ||
6e71b04a | 404 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
aa79781b | 405 | { |
afdb09c7 CF |
406 | int ret; |
407 | ||
408 | ret = security_bpf_map(map, OPEN_FMODE(flags)); | |
409 | if (ret < 0) | |
410 | return ret; | |
411 | ||
aa79781b | 412 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
6e71b04a CF |
413 | flags | O_CLOEXEC); |
414 | } | |
415 | ||
416 | int bpf_get_file_flag(int flags) | |
417 | { | |
418 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) | |
419 | return -EINVAL; | |
420 | if (flags & BPF_F_RDONLY) | |
421 | return O_RDONLY; | |
422 | if (flags & BPF_F_WRONLY) | |
423 | return O_WRONLY; | |
424 | return O_RDWR; | |
aa79781b DB |
425 | } |
426 | ||
99c55f7d AS |
427 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
428 | #define CHECK_ATTR(CMD) \ | |
429 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ | |
430 | sizeof(attr->CMD##_LAST_FIELD), 0, \ | |
431 | sizeof(*attr) - \ | |
432 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ | |
433 | sizeof(attr->CMD##_LAST_FIELD)) != NULL | |
434 | ||
cb4d2b3f MKL |
435 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
436 | * Return 0 on success and < 0 on error. | |
437 | */ | |
438 | static int bpf_obj_name_cpy(char *dst, const char *src) | |
439 | { | |
440 | const char *end = src + BPF_OBJ_NAME_LEN; | |
441 | ||
473d9734 MKL |
442 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
443 | ||
cb4d2b3f MKL |
444 | /* Copy all isalnum() and '_' char */ |
445 | while (src < end && *src) { | |
446 | if (!isalnum(*src) && *src != '_') | |
447 | return -EINVAL; | |
448 | *dst++ = *src++; | |
449 | } | |
450 | ||
451 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ | |
452 | if (src == end) | |
453 | return -EINVAL; | |
454 | ||
cb4d2b3f MKL |
455 | return 0; |
456 | } | |
457 | ||
e8d2bec0 | 458 | int map_check_no_btf(const struct bpf_map *map, |
1b2b234b | 459 | const struct btf *btf, |
e8d2bec0 DB |
460 | const struct btf_type *key_type, |
461 | const struct btf_type *value_type) | |
462 | { | |
463 | return -ENOTSUPP; | |
464 | } | |
465 | ||
d83525ca | 466 | static int map_check_btf(struct bpf_map *map, const struct btf *btf, |
e8d2bec0 DB |
467 | u32 btf_key_id, u32 btf_value_id) |
468 | { | |
469 | const struct btf_type *key_type, *value_type; | |
470 | u32 key_size, value_size; | |
471 | int ret = 0; | |
472 | ||
473 | key_type = btf_type_id_size(btf, &btf_key_id, &key_size); | |
474 | if (!key_type || key_size != map->key_size) | |
475 | return -EINVAL; | |
476 | ||
477 | value_type = btf_type_id_size(btf, &btf_value_id, &value_size); | |
478 | if (!value_type || value_size != map->value_size) | |
479 | return -EINVAL; | |
480 | ||
d83525ca AS |
481 | map->spin_lock_off = btf_find_spin_lock(btf, value_type); |
482 | ||
483 | if (map_value_has_spin_lock(map)) { | |
484 | if (map->map_type != BPF_MAP_TYPE_HASH && | |
e16d2f1a AS |
485 | map->map_type != BPF_MAP_TYPE_ARRAY && |
486 | map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE) | |
d83525ca AS |
487 | return -ENOTSUPP; |
488 | if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > | |
489 | map->value_size) { | |
490 | WARN_ONCE(1, | |
491 | "verifier bug spin_lock_off %d value_size %d\n", | |
492 | map->spin_lock_off, map->value_size); | |
493 | return -EFAULT; | |
494 | } | |
495 | } | |
496 | ||
e8d2bec0 | 497 | if (map->ops->map_check_btf) |
1b2b234b | 498 | ret = map->ops->map_check_btf(map, btf, key_type, value_type); |
e8d2bec0 DB |
499 | |
500 | return ret; | |
501 | } | |
502 | ||
9b2cf328 | 503 | #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id |
99c55f7d AS |
504 | /* called via syscall */ |
505 | static int map_create(union bpf_attr *attr) | |
506 | { | |
96eabe7a | 507 | int numa_node = bpf_map_attr_numa_node(attr); |
99c55f7d | 508 | struct bpf_map *map; |
6e71b04a | 509 | int f_flags; |
99c55f7d AS |
510 | int err; |
511 | ||
512 | err = CHECK_ATTR(BPF_MAP_CREATE); | |
513 | if (err) | |
514 | return -EINVAL; | |
515 | ||
6e71b04a CF |
516 | f_flags = bpf_get_file_flag(attr->map_flags); |
517 | if (f_flags < 0) | |
518 | return f_flags; | |
519 | ||
96eabe7a | 520 | if (numa_node != NUMA_NO_NODE && |
96e5ae4e ED |
521 | ((unsigned int)numa_node >= nr_node_ids || |
522 | !node_online(numa_node))) | |
96eabe7a MKL |
523 | return -EINVAL; |
524 | ||
99c55f7d AS |
525 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
526 | map = find_and_alloc_map(attr); | |
527 | if (IS_ERR(map)) | |
528 | return PTR_ERR(map); | |
529 | ||
ad5b177b MKL |
530 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
531 | if (err) | |
532 | goto free_map_nouncharge; | |
533 | ||
99c55f7d | 534 | atomic_set(&map->refcnt, 1); |
c9da161c | 535 | atomic_set(&map->usercnt, 1); |
99c55f7d | 536 | |
e8d2bec0 | 537 | if (attr->btf_key_type_id || attr->btf_value_type_id) { |
a26ca7c9 MKL |
538 | struct btf *btf; |
539 | ||
9b2cf328 | 540 | if (!attr->btf_key_type_id || !attr->btf_value_type_id) { |
a26ca7c9 MKL |
541 | err = -EINVAL; |
542 | goto free_map_nouncharge; | |
543 | } | |
544 | ||
545 | btf = btf_get_by_fd(attr->btf_fd); | |
546 | if (IS_ERR(btf)) { | |
547 | err = PTR_ERR(btf); | |
548 | goto free_map_nouncharge; | |
549 | } | |
550 | ||
e8d2bec0 DB |
551 | err = map_check_btf(map, btf, attr->btf_key_type_id, |
552 | attr->btf_value_type_id); | |
a26ca7c9 MKL |
553 | if (err) { |
554 | btf_put(btf); | |
555 | goto free_map_nouncharge; | |
556 | } | |
557 | ||
558 | map->btf = btf; | |
9b2cf328 MKL |
559 | map->btf_key_type_id = attr->btf_key_type_id; |
560 | map->btf_value_type_id = attr->btf_value_type_id; | |
d83525ca AS |
561 | } else { |
562 | map->spin_lock_off = -EINVAL; | |
a26ca7c9 MKL |
563 | } |
564 | ||
afdb09c7 | 565 | err = security_bpf_map_alloc(map); |
aaac3ba9 | 566 | if (err) |
20b2b24f | 567 | goto free_map_nouncharge; |
aaac3ba9 | 568 | |
0a4c58f5 | 569 | err = bpf_map_init_memlock(map); |
afdb09c7 CF |
570 | if (err) |
571 | goto free_map_sec; | |
572 | ||
f3f1c054 MKL |
573 | err = bpf_map_alloc_id(map); |
574 | if (err) | |
575 | goto free_map; | |
576 | ||
6e71b04a | 577 | err = bpf_map_new_fd(map, f_flags); |
bd5f5f4e MKL |
578 | if (err < 0) { |
579 | /* failed to allocate fd. | |
580 | * bpf_map_put() is needed because the above | |
581 | * bpf_map_alloc_id() has published the map | |
582 | * to the userspace and the userspace may | |
583 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. | |
584 | */ | |
585 | bpf_map_put(map); | |
586 | return err; | |
587 | } | |
99c55f7d AS |
588 | |
589 | return err; | |
590 | ||
591 | free_map: | |
0a4c58f5 | 592 | bpf_map_release_memlock(map); |
afdb09c7 CF |
593 | free_map_sec: |
594 | security_bpf_map_free(map); | |
20b2b24f | 595 | free_map_nouncharge: |
a26ca7c9 | 596 | btf_put(map->btf); |
99c55f7d AS |
597 | map->ops->map_free(map); |
598 | return err; | |
599 | } | |
600 | ||
db20fd2b AS |
601 | /* if error is returned, fd is released. |
602 | * On success caller should complete fd access with matching fdput() | |
603 | */ | |
c2101297 | 604 | struct bpf_map *__bpf_map_get(struct fd f) |
db20fd2b | 605 | { |
db20fd2b AS |
606 | if (!f.file) |
607 | return ERR_PTR(-EBADF); | |
db20fd2b AS |
608 | if (f.file->f_op != &bpf_map_fops) { |
609 | fdput(f); | |
610 | return ERR_PTR(-EINVAL); | |
611 | } | |
612 | ||
c2101297 DB |
613 | return f.file->private_data; |
614 | } | |
615 | ||
92117d84 AS |
616 | /* prog's and map's refcnt limit */ |
617 | #define BPF_MAX_REFCNT 32768 | |
618 | ||
619 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) | |
c9da161c | 620 | { |
92117d84 AS |
621 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
622 | atomic_dec(&map->refcnt); | |
623 | return ERR_PTR(-EBUSY); | |
624 | } | |
c9da161c DB |
625 | if (uref) |
626 | atomic_inc(&map->usercnt); | |
92117d84 | 627 | return map; |
c9da161c | 628 | } |
630a4d38 | 629 | EXPORT_SYMBOL_GPL(bpf_map_inc); |
c9da161c DB |
630 | |
631 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) | |
c2101297 DB |
632 | { |
633 | struct fd f = fdget(ufd); | |
634 | struct bpf_map *map; | |
635 | ||
636 | map = __bpf_map_get(f); | |
637 | if (IS_ERR(map)) | |
638 | return map; | |
639 | ||
92117d84 | 640 | map = bpf_map_inc(map, true); |
c2101297 | 641 | fdput(f); |
db20fd2b AS |
642 | |
643 | return map; | |
644 | } | |
645 | ||
bd5f5f4e MKL |
646 | /* map_idr_lock should have been held */ |
647 | static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, | |
648 | bool uref) | |
649 | { | |
650 | int refold; | |
651 | ||
bfc18e38 | 652 | refold = atomic_fetch_add_unless(&map->refcnt, 1, 0); |
bd5f5f4e MKL |
653 | |
654 | if (refold >= BPF_MAX_REFCNT) { | |
655 | __bpf_map_put(map, false); | |
656 | return ERR_PTR(-EBUSY); | |
657 | } | |
658 | ||
659 | if (!refold) | |
660 | return ERR_PTR(-ENOENT); | |
661 | ||
662 | if (uref) | |
663 | atomic_inc(&map->usercnt); | |
664 | ||
665 | return map; | |
666 | } | |
667 | ||
b8cdc051 AS |
668 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
669 | { | |
670 | return -ENOTSUPP; | |
671 | } | |
672 | ||
c9d29f46 MV |
673 | static void *__bpf_copy_key(void __user *ukey, u64 key_size) |
674 | { | |
675 | if (key_size) | |
676 | return memdup_user(ukey, key_size); | |
677 | ||
678 | if (ukey) | |
679 | return ERR_PTR(-EINVAL); | |
680 | ||
681 | return NULL; | |
682 | } | |
683 | ||
db20fd2b AS |
684 | /* last field in 'union bpf_attr' used by this command */ |
685 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value | |
686 | ||
687 | static int map_lookup_elem(union bpf_attr *attr) | |
688 | { | |
535e7b4b MS |
689 | void __user *ukey = u64_to_user_ptr(attr->key); |
690 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 691 | int ufd = attr->map_fd; |
db20fd2b | 692 | struct bpf_map *map; |
8ebe667c | 693 | void *key, *value, *ptr; |
15a07b33 | 694 | u32 value_size; |
592867bf | 695 | struct fd f; |
db20fd2b AS |
696 | int err; |
697 | ||
698 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) | |
699 | return -EINVAL; | |
700 | ||
592867bf | 701 | f = fdget(ufd); |
c2101297 | 702 | map = __bpf_map_get(f); |
db20fd2b AS |
703 | if (IS_ERR(map)) |
704 | return PTR_ERR(map); | |
705 | ||
6e71b04a CF |
706 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
707 | err = -EPERM; | |
708 | goto err_put; | |
709 | } | |
710 | ||
c9d29f46 | 711 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
712 | if (IS_ERR(key)) { |
713 | err = PTR_ERR(key); | |
db20fd2b | 714 | goto err_put; |
e4448ed8 | 715 | } |
db20fd2b | 716 | |
15a07b33 | 717 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 718 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
b741f163 RG |
719 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
720 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
15a07b33 | 721 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
14dc6f04 MKL |
722 | else if (IS_FD_MAP(map)) |
723 | value_size = sizeof(u32); | |
15a07b33 AS |
724 | else |
725 | value_size = map->value_size; | |
726 | ||
8ebe667c | 727 | err = -ENOMEM; |
15a07b33 | 728 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b | 729 | if (!value) |
8ebe667c AS |
730 | goto free_key; |
731 | ||
a3884572 JK |
732 | if (bpf_map_is_dev_bound(map)) { |
733 | err = bpf_map_offload_lookup_elem(map, key, value); | |
734 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || | |
735 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
736 | err = bpf_percpu_hash_copy(map, key, value); |
737 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
738 | err = bpf_percpu_array_copy(map, key, value); | |
b741f163 RG |
739 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
740 | err = bpf_percpu_cgroup_storage_copy(map, key, value); | |
557c0c6e AS |
741 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
742 | err = bpf_stackmap_copy(map, key, value); | |
14dc6f04 MKL |
743 | } else if (IS_FD_ARRAY(map)) { |
744 | err = bpf_fd_array_map_lookup_elem(map, key, value); | |
745 | } else if (IS_FD_HASH(map)) { | |
746 | err = bpf_fd_htab_map_lookup_elem(map, key, value); | |
5dc4c4b7 MKL |
747 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
748 | err = bpf_fd_reuseport_array_lookup_elem(map, key, value); | |
f1a2e44a MV |
749 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
750 | map->map_type == BPF_MAP_TYPE_STACK) { | |
751 | err = map->ops->map_peek_elem(map, value); | |
15a07b33 AS |
752 | } else { |
753 | rcu_read_lock(); | |
754 | ptr = map->ops->map_lookup_elem(map, key); | |
509db283 PB |
755 | if (IS_ERR(ptr)) { |
756 | err = PTR_ERR(ptr); | |
757 | } else if (!ptr) { | |
758 | err = -ENOENT; | |
759 | } else { | |
760 | err = 0; | |
d83525ca | 761 | copy_map_value(map, value, ptr); |
509db283 | 762 | } |
15a07b33 | 763 | rcu_read_unlock(); |
15a07b33 | 764 | } |
8ebe667c | 765 | |
15a07b33 | 766 | if (err) |
8ebe667c | 767 | goto free_value; |
db20fd2b AS |
768 | |
769 | err = -EFAULT; | |
15a07b33 | 770 | if (copy_to_user(uvalue, value, value_size) != 0) |
8ebe667c | 771 | goto free_value; |
db20fd2b AS |
772 | |
773 | err = 0; | |
774 | ||
8ebe667c AS |
775 | free_value: |
776 | kfree(value); | |
db20fd2b AS |
777 | free_key: |
778 | kfree(key); | |
779 | err_put: | |
780 | fdput(f); | |
781 | return err; | |
782 | } | |
783 | ||
1ae80cf3 DC |
784 | static void maybe_wait_bpf_programs(struct bpf_map *map) |
785 | { | |
786 | /* Wait for any running BPF programs to complete so that | |
787 | * userspace, when we return to it, knows that all programs | |
788 | * that could be running use the new map value. | |
789 | */ | |
790 | if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || | |
791 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) | |
792 | synchronize_rcu(); | |
793 | } | |
794 | ||
3274f520 | 795 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
db20fd2b AS |
796 | |
797 | static int map_update_elem(union bpf_attr *attr) | |
798 | { | |
535e7b4b MS |
799 | void __user *ukey = u64_to_user_ptr(attr->key); |
800 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 801 | int ufd = attr->map_fd; |
db20fd2b AS |
802 | struct bpf_map *map; |
803 | void *key, *value; | |
15a07b33 | 804 | u32 value_size; |
592867bf | 805 | struct fd f; |
db20fd2b AS |
806 | int err; |
807 | ||
808 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) | |
809 | return -EINVAL; | |
810 | ||
592867bf | 811 | f = fdget(ufd); |
c2101297 | 812 | map = __bpf_map_get(f); |
db20fd2b AS |
813 | if (IS_ERR(map)) |
814 | return PTR_ERR(map); | |
815 | ||
6e71b04a CF |
816 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
817 | err = -EPERM; | |
818 | goto err_put; | |
819 | } | |
820 | ||
c9d29f46 | 821 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
822 | if (IS_ERR(key)) { |
823 | err = PTR_ERR(key); | |
db20fd2b | 824 | goto err_put; |
e4448ed8 | 825 | } |
db20fd2b | 826 | |
15a07b33 | 827 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 828 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
b741f163 RG |
829 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
830 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
15a07b33 AS |
831 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
832 | else | |
833 | value_size = map->value_size; | |
834 | ||
db20fd2b | 835 | err = -ENOMEM; |
15a07b33 | 836 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b AS |
837 | if (!value) |
838 | goto free_key; | |
839 | ||
840 | err = -EFAULT; | |
15a07b33 | 841 | if (copy_from_user(value, uvalue, value_size) != 0) |
db20fd2b AS |
842 | goto free_value; |
843 | ||
6710e112 | 844 | /* Need to create a kthread, thus must support schedule */ |
a3884572 JK |
845 | if (bpf_map_is_dev_bound(map)) { |
846 | err = bpf_map_offload_update_elem(map, key, value, attr->flags); | |
847 | goto out; | |
99ba2b5a JF |
848 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || |
849 | map->map_type == BPF_MAP_TYPE_SOCKHASH || | |
850 | map->map_type == BPF_MAP_TYPE_SOCKMAP) { | |
6710e112 JDB |
851 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
852 | goto out; | |
853 | } | |
854 | ||
b121d1e7 AS |
855 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
856 | * inside bpf map update or delete otherwise deadlocks are possible | |
857 | */ | |
858 | preempt_disable(); | |
859 | __this_cpu_inc(bpf_prog_active); | |
8f844938 MKL |
860 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
861 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
862 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
863 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
864 | err = bpf_percpu_array_update(map, key, value, attr->flags); | |
b741f163 RG |
865 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
866 | err = bpf_percpu_cgroup_storage_update(map, key, value, | |
867 | attr->flags); | |
9c147b56 | 868 | } else if (IS_FD_ARRAY(map)) { |
d056a788 DB |
869 | rcu_read_lock(); |
870 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, | |
871 | attr->flags); | |
872 | rcu_read_unlock(); | |
bcc6b1b7 MKL |
873 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
874 | rcu_read_lock(); | |
875 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, | |
876 | attr->flags); | |
877 | rcu_read_unlock(); | |
5dc4c4b7 MKL |
878 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
879 | /* rcu_read_lock() is not needed */ | |
880 | err = bpf_fd_reuseport_array_update_elem(map, key, value, | |
881 | attr->flags); | |
f1a2e44a MV |
882 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
883 | map->map_type == BPF_MAP_TYPE_STACK) { | |
884 | err = map->ops->map_push_elem(map, value, attr->flags); | |
15a07b33 AS |
885 | } else { |
886 | rcu_read_lock(); | |
887 | err = map->ops->map_update_elem(map, key, value, attr->flags); | |
888 | rcu_read_unlock(); | |
889 | } | |
b121d1e7 AS |
890 | __this_cpu_dec(bpf_prog_active); |
891 | preempt_enable(); | |
1ae80cf3 | 892 | maybe_wait_bpf_programs(map); |
6710e112 | 893 | out: |
db20fd2b AS |
894 | free_value: |
895 | kfree(value); | |
896 | free_key: | |
897 | kfree(key); | |
898 | err_put: | |
899 | fdput(f); | |
900 | return err; | |
901 | } | |
902 | ||
903 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key | |
904 | ||
905 | static int map_delete_elem(union bpf_attr *attr) | |
906 | { | |
535e7b4b | 907 | void __user *ukey = u64_to_user_ptr(attr->key); |
db20fd2b | 908 | int ufd = attr->map_fd; |
db20fd2b | 909 | struct bpf_map *map; |
592867bf | 910 | struct fd f; |
db20fd2b AS |
911 | void *key; |
912 | int err; | |
913 | ||
914 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) | |
915 | return -EINVAL; | |
916 | ||
592867bf | 917 | f = fdget(ufd); |
c2101297 | 918 | map = __bpf_map_get(f); |
db20fd2b AS |
919 | if (IS_ERR(map)) |
920 | return PTR_ERR(map); | |
921 | ||
6e71b04a CF |
922 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
923 | err = -EPERM; | |
924 | goto err_put; | |
925 | } | |
926 | ||
c9d29f46 | 927 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
928 | if (IS_ERR(key)) { |
929 | err = PTR_ERR(key); | |
db20fd2b | 930 | goto err_put; |
e4448ed8 | 931 | } |
db20fd2b | 932 | |
a3884572 JK |
933 | if (bpf_map_is_dev_bound(map)) { |
934 | err = bpf_map_offload_delete_elem(map, key); | |
935 | goto out; | |
936 | } | |
937 | ||
b121d1e7 AS |
938 | preempt_disable(); |
939 | __this_cpu_inc(bpf_prog_active); | |
db20fd2b AS |
940 | rcu_read_lock(); |
941 | err = map->ops->map_delete_elem(map, key); | |
942 | rcu_read_unlock(); | |
b121d1e7 AS |
943 | __this_cpu_dec(bpf_prog_active); |
944 | preempt_enable(); | |
1ae80cf3 | 945 | maybe_wait_bpf_programs(map); |
a3884572 | 946 | out: |
db20fd2b AS |
947 | kfree(key); |
948 | err_put: | |
949 | fdput(f); | |
950 | return err; | |
951 | } | |
952 | ||
953 | /* last field in 'union bpf_attr' used by this command */ | |
954 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key | |
955 | ||
956 | static int map_get_next_key(union bpf_attr *attr) | |
957 | { | |
535e7b4b MS |
958 | void __user *ukey = u64_to_user_ptr(attr->key); |
959 | void __user *unext_key = u64_to_user_ptr(attr->next_key); | |
db20fd2b | 960 | int ufd = attr->map_fd; |
db20fd2b AS |
961 | struct bpf_map *map; |
962 | void *key, *next_key; | |
592867bf | 963 | struct fd f; |
db20fd2b AS |
964 | int err; |
965 | ||
966 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) | |
967 | return -EINVAL; | |
968 | ||
592867bf | 969 | f = fdget(ufd); |
c2101297 | 970 | map = __bpf_map_get(f); |
db20fd2b AS |
971 | if (IS_ERR(map)) |
972 | return PTR_ERR(map); | |
973 | ||
6e71b04a CF |
974 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
975 | err = -EPERM; | |
976 | goto err_put; | |
977 | } | |
978 | ||
8fe45924 | 979 | if (ukey) { |
c9d29f46 | 980 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
981 | if (IS_ERR(key)) { |
982 | err = PTR_ERR(key); | |
8fe45924 | 983 | goto err_put; |
e4448ed8 | 984 | } |
8fe45924 TQ |
985 | } else { |
986 | key = NULL; | |
987 | } | |
db20fd2b AS |
988 | |
989 | err = -ENOMEM; | |
990 | next_key = kmalloc(map->key_size, GFP_USER); | |
991 | if (!next_key) | |
992 | goto free_key; | |
993 | ||
a3884572 JK |
994 | if (bpf_map_is_dev_bound(map)) { |
995 | err = bpf_map_offload_get_next_key(map, key, next_key); | |
996 | goto out; | |
997 | } | |
998 | ||
db20fd2b AS |
999 | rcu_read_lock(); |
1000 | err = map->ops->map_get_next_key(map, key, next_key); | |
1001 | rcu_read_unlock(); | |
a3884572 | 1002 | out: |
db20fd2b AS |
1003 | if (err) |
1004 | goto free_next_key; | |
1005 | ||
1006 | err = -EFAULT; | |
1007 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) | |
1008 | goto free_next_key; | |
1009 | ||
1010 | err = 0; | |
1011 | ||
1012 | free_next_key: | |
1013 | kfree(next_key); | |
1014 | free_key: | |
1015 | kfree(key); | |
1016 | err_put: | |
1017 | fdput(f); | |
1018 | return err; | |
1019 | } | |
1020 | ||
bd513cd0 MV |
1021 | #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value |
1022 | ||
1023 | static int map_lookup_and_delete_elem(union bpf_attr *attr) | |
1024 | { | |
1025 | void __user *ukey = u64_to_user_ptr(attr->key); | |
1026 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
1027 | int ufd = attr->map_fd; | |
1028 | struct bpf_map *map; | |
540fefc0 | 1029 | void *key, *value; |
bd513cd0 MV |
1030 | u32 value_size; |
1031 | struct fd f; | |
1032 | int err; | |
1033 | ||
1034 | if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) | |
1035 | return -EINVAL; | |
1036 | ||
1037 | f = fdget(ufd); | |
1038 | map = __bpf_map_get(f); | |
1039 | if (IS_ERR(map)) | |
1040 | return PTR_ERR(map); | |
1041 | ||
1042 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { | |
1043 | err = -EPERM; | |
1044 | goto err_put; | |
1045 | } | |
1046 | ||
1047 | key = __bpf_copy_key(ukey, map->key_size); | |
1048 | if (IS_ERR(key)) { | |
1049 | err = PTR_ERR(key); | |
1050 | goto err_put; | |
1051 | } | |
1052 | ||
1053 | value_size = map->value_size; | |
1054 | ||
1055 | err = -ENOMEM; | |
1056 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); | |
1057 | if (!value) | |
1058 | goto free_key; | |
1059 | ||
1060 | if (map->map_type == BPF_MAP_TYPE_QUEUE || | |
1061 | map->map_type == BPF_MAP_TYPE_STACK) { | |
1062 | err = map->ops->map_pop_elem(map, value); | |
1063 | } else { | |
1064 | err = -ENOTSUPP; | |
1065 | } | |
1066 | ||
1067 | if (err) | |
1068 | goto free_value; | |
1069 | ||
1070 | if (copy_to_user(uvalue, value, value_size) != 0) | |
1071 | goto free_value; | |
1072 | ||
1073 | err = 0; | |
1074 | ||
1075 | free_value: | |
1076 | kfree(value); | |
1077 | free_key: | |
1078 | kfree(key); | |
1079 | err_put: | |
1080 | fdput(f); | |
1081 | return err; | |
1082 | } | |
1083 | ||
7de16e3a JK |
1084 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
1085 | #define BPF_PROG_TYPE(_id, _name) \ | |
1086 | [_id] = & _name ## _prog_ops, | |
1087 | #define BPF_MAP_TYPE(_id, _ops) | |
1088 | #include <linux/bpf_types.h> | |
1089 | #undef BPF_PROG_TYPE | |
1090 | #undef BPF_MAP_TYPE | |
1091 | }; | |
1092 | ||
09756af4 AS |
1093 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
1094 | { | |
d0f1a451 DB |
1095 | const struct bpf_prog_ops *ops; |
1096 | ||
1097 | if (type >= ARRAY_SIZE(bpf_prog_types)) | |
1098 | return -EINVAL; | |
1099 | type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); | |
1100 | ops = bpf_prog_types[type]; | |
1101 | if (!ops) | |
be9370a7 | 1102 | return -EINVAL; |
09756af4 | 1103 | |
ab3f0063 | 1104 | if (!bpf_prog_is_dev_bound(prog->aux)) |
d0f1a451 | 1105 | prog->aux->ops = ops; |
ab3f0063 JK |
1106 | else |
1107 | prog->aux->ops = &bpf_offload_prog_ops; | |
be9370a7 JB |
1108 | prog->type = type; |
1109 | return 0; | |
09756af4 AS |
1110 | } |
1111 | ||
1112 | /* drop refcnt on maps used by eBPF program and free auxilary data */ | |
1113 | static void free_used_maps(struct bpf_prog_aux *aux) | |
1114 | { | |
8bad74f9 | 1115 | enum bpf_cgroup_storage_type stype; |
09756af4 AS |
1116 | int i; |
1117 | ||
8bad74f9 RG |
1118 | for_each_cgroup_storage_type(stype) { |
1119 | if (!aux->cgroup_storage[stype]) | |
1120 | continue; | |
1121 | bpf_cgroup_storage_release(aux->prog, | |
1122 | aux->cgroup_storage[stype]); | |
1123 | } | |
de9cbbaa | 1124 | |
09756af4 AS |
1125 | for (i = 0; i < aux->used_map_cnt; i++) |
1126 | bpf_map_put(aux->used_maps[i]); | |
1127 | ||
1128 | kfree(aux->used_maps); | |
1129 | } | |
1130 | ||
5ccb071e DB |
1131 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
1132 | { | |
1133 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
1134 | unsigned long user_bufs; | |
1135 | ||
1136 | if (user) { | |
1137 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); | |
1138 | if (user_bufs > memlock_limit) { | |
1139 | atomic_long_sub(pages, &user->locked_vm); | |
1140 | return -EPERM; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | return 0; | |
1145 | } | |
1146 | ||
1147 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) | |
1148 | { | |
1149 | if (user) | |
1150 | atomic_long_sub(pages, &user->locked_vm); | |
1151 | } | |
1152 | ||
aaac3ba9 AS |
1153 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
1154 | { | |
1155 | struct user_struct *user = get_current_user(); | |
5ccb071e | 1156 | int ret; |
aaac3ba9 | 1157 | |
5ccb071e DB |
1158 | ret = __bpf_prog_charge(user, prog->pages); |
1159 | if (ret) { | |
aaac3ba9 | 1160 | free_uid(user); |
5ccb071e | 1161 | return ret; |
aaac3ba9 | 1162 | } |
5ccb071e | 1163 | |
aaac3ba9 AS |
1164 | prog->aux->user = user; |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) | |
1169 | { | |
1170 | struct user_struct *user = prog->aux->user; | |
1171 | ||
5ccb071e | 1172 | __bpf_prog_uncharge(user, prog->pages); |
aaac3ba9 AS |
1173 | free_uid(user); |
1174 | } | |
1175 | ||
dc4bb0e2 MKL |
1176 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
1177 | { | |
1178 | int id; | |
1179 | ||
b76354cd | 1180 | idr_preload(GFP_KERNEL); |
dc4bb0e2 MKL |
1181 | spin_lock_bh(&prog_idr_lock); |
1182 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); | |
1183 | if (id > 0) | |
1184 | prog->aux->id = id; | |
1185 | spin_unlock_bh(&prog_idr_lock); | |
b76354cd | 1186 | idr_preload_end(); |
dc4bb0e2 MKL |
1187 | |
1188 | /* id is in [1, INT_MAX) */ | |
1189 | if (WARN_ON_ONCE(!id)) | |
1190 | return -ENOSPC; | |
1191 | ||
1192 | return id > 0 ? 0 : id; | |
1193 | } | |
1194 | ||
ad8ad79f | 1195 | void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) |
dc4bb0e2 | 1196 | { |
ad8ad79f JK |
1197 | /* cBPF to eBPF migrations are currently not in the idr store. |
1198 | * Offloaded programs are removed from the store when their device | |
1199 | * disappears - even if someone grabs an fd to them they are unusable, | |
1200 | * simply waiting for refcnt to drop to be freed. | |
1201 | */ | |
dc4bb0e2 MKL |
1202 | if (!prog->aux->id) |
1203 | return; | |
1204 | ||
b16d9aa4 MKL |
1205 | if (do_idr_lock) |
1206 | spin_lock_bh(&prog_idr_lock); | |
1207 | else | |
1208 | __acquire(&prog_idr_lock); | |
1209 | ||
dc4bb0e2 | 1210 | idr_remove(&prog_idr, prog->aux->id); |
ad8ad79f | 1211 | prog->aux->id = 0; |
b16d9aa4 MKL |
1212 | |
1213 | if (do_idr_lock) | |
1214 | spin_unlock_bh(&prog_idr_lock); | |
1215 | else | |
1216 | __release(&prog_idr_lock); | |
dc4bb0e2 MKL |
1217 | } |
1218 | ||
1aacde3d | 1219 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
abf2e7d6 AS |
1220 | { |
1221 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); | |
1222 | ||
1223 | free_used_maps(aux); | |
aaac3ba9 | 1224 | bpf_prog_uncharge_memlock(aux->prog); |
afdb09c7 | 1225 | security_bpf_prog_free(aux); |
abf2e7d6 AS |
1226 | bpf_prog_free(aux->prog); |
1227 | } | |
1228 | ||
b16d9aa4 | 1229 | static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
09756af4 | 1230 | { |
a67edbf4 | 1231 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
34ad5580 | 1232 | /* bpf_prog_free_id() must be called first */ |
b16d9aa4 | 1233 | bpf_prog_free_id(prog, do_idr_lock); |
7d1982b4 | 1234 | bpf_prog_kallsyms_del_all(prog); |
838e9690 | 1235 | btf_put(prog->aux->btf); |
ba64e7d8 | 1236 | kvfree(prog->aux->func_info); |
c454a46b | 1237 | bpf_prog_free_linfo(prog); |
4f74d809 | 1238 | |
1aacde3d | 1239 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
a67edbf4 | 1240 | } |
09756af4 | 1241 | } |
b16d9aa4 MKL |
1242 | |
1243 | void bpf_prog_put(struct bpf_prog *prog) | |
1244 | { | |
1245 | __bpf_prog_put(prog, true); | |
1246 | } | |
e2e9b654 | 1247 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
09756af4 AS |
1248 | |
1249 | static int bpf_prog_release(struct inode *inode, struct file *filp) | |
1250 | { | |
1251 | struct bpf_prog *prog = filp->private_data; | |
1252 | ||
1aacde3d | 1253 | bpf_prog_put(prog); |
09756af4 AS |
1254 | return 0; |
1255 | } | |
1256 | ||
7bd509e3 DB |
1257 | #ifdef CONFIG_PROC_FS |
1258 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) | |
1259 | { | |
1260 | const struct bpf_prog *prog = filp->private_data; | |
f1f7714e | 1261 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
7bd509e3 | 1262 | |
f1f7714e | 1263 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
7bd509e3 DB |
1264 | seq_printf(m, |
1265 | "prog_type:\t%u\n" | |
1266 | "prog_jited:\t%u\n" | |
f1f7714e | 1267 | "prog_tag:\t%s\n" |
4316b409 DB |
1268 | "memlock:\t%llu\n" |
1269 | "prog_id:\t%u\n", | |
7bd509e3 DB |
1270 | prog->type, |
1271 | prog->jited, | |
f1f7714e | 1272 | prog_tag, |
4316b409 DB |
1273 | prog->pages * 1ULL << PAGE_SHIFT, |
1274 | prog->aux->id); | |
7bd509e3 DB |
1275 | } |
1276 | #endif | |
1277 | ||
f66e448c | 1278 | const struct file_operations bpf_prog_fops = { |
7bd509e3 DB |
1279 | #ifdef CONFIG_PROC_FS |
1280 | .show_fdinfo = bpf_prog_show_fdinfo, | |
1281 | #endif | |
1282 | .release = bpf_prog_release, | |
6e71b04a CF |
1283 | .read = bpf_dummy_read, |
1284 | .write = bpf_dummy_write, | |
09756af4 AS |
1285 | }; |
1286 | ||
b2197755 | 1287 | int bpf_prog_new_fd(struct bpf_prog *prog) |
aa79781b | 1288 | { |
afdb09c7 CF |
1289 | int ret; |
1290 | ||
1291 | ret = security_bpf_prog(prog); | |
1292 | if (ret < 0) | |
1293 | return ret; | |
1294 | ||
aa79781b DB |
1295 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
1296 | O_RDWR | O_CLOEXEC); | |
1297 | } | |
1298 | ||
113214be | 1299 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
09756af4 | 1300 | { |
09756af4 AS |
1301 | if (!f.file) |
1302 | return ERR_PTR(-EBADF); | |
09756af4 AS |
1303 | if (f.file->f_op != &bpf_prog_fops) { |
1304 | fdput(f); | |
1305 | return ERR_PTR(-EINVAL); | |
1306 | } | |
1307 | ||
c2101297 | 1308 | return f.file->private_data; |
09756af4 AS |
1309 | } |
1310 | ||
59d3656d | 1311 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
92117d84 | 1312 | { |
59d3656d BB |
1313 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
1314 | atomic_sub(i, &prog->aux->refcnt); | |
92117d84 AS |
1315 | return ERR_PTR(-EBUSY); |
1316 | } | |
1317 | return prog; | |
1318 | } | |
59d3656d BB |
1319 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
1320 | ||
c540594f DB |
1321 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
1322 | { | |
1323 | /* Only to be used for undoing previous bpf_prog_add() in some | |
1324 | * error path. We still know that another entity in our call | |
1325 | * path holds a reference to the program, thus atomic_sub() can | |
1326 | * be safely used in such cases! | |
1327 | */ | |
1328 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); | |
1329 | } | |
1330 | EXPORT_SYMBOL_GPL(bpf_prog_sub); | |
1331 | ||
59d3656d BB |
1332 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
1333 | { | |
1334 | return bpf_prog_add(prog, 1); | |
1335 | } | |
97bc402d | 1336 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
92117d84 | 1337 | |
b16d9aa4 | 1338 | /* prog_idr_lock should have been held */ |
a6f6df69 | 1339 | struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) |
b16d9aa4 MKL |
1340 | { |
1341 | int refold; | |
1342 | ||
bfc18e38 | 1343 | refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0); |
b16d9aa4 MKL |
1344 | |
1345 | if (refold >= BPF_MAX_REFCNT) { | |
1346 | __bpf_prog_put(prog, false); | |
1347 | return ERR_PTR(-EBUSY); | |
1348 | } | |
1349 | ||
1350 | if (!refold) | |
1351 | return ERR_PTR(-ENOENT); | |
1352 | ||
1353 | return prog; | |
1354 | } | |
a6f6df69 | 1355 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
b16d9aa4 | 1356 | |
040ee692 | 1357 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
288b3de5 | 1358 | enum bpf_prog_type *attach_type, bool attach_drv) |
248f346f | 1359 | { |
288b3de5 JK |
1360 | /* not an attachment, just a refcount inc, always allow */ |
1361 | if (!attach_type) | |
1362 | return true; | |
248f346f JK |
1363 | |
1364 | if (prog->type != *attach_type) | |
1365 | return false; | |
288b3de5 | 1366 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
248f346f JK |
1367 | return false; |
1368 | ||
1369 | return true; | |
1370 | } | |
1371 | ||
1372 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, | |
288b3de5 | 1373 | bool attach_drv) |
09756af4 AS |
1374 | { |
1375 | struct fd f = fdget(ufd); | |
1376 | struct bpf_prog *prog; | |
1377 | ||
113214be | 1378 | prog = ____bpf_prog_get(f); |
09756af4 AS |
1379 | if (IS_ERR(prog)) |
1380 | return prog; | |
288b3de5 | 1381 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
113214be DB |
1382 | prog = ERR_PTR(-EINVAL); |
1383 | goto out; | |
1384 | } | |
09756af4 | 1385 | |
92117d84 | 1386 | prog = bpf_prog_inc(prog); |
113214be | 1387 | out: |
09756af4 AS |
1388 | fdput(f); |
1389 | return prog; | |
1390 | } | |
113214be DB |
1391 | |
1392 | struct bpf_prog *bpf_prog_get(u32 ufd) | |
1393 | { | |
288b3de5 | 1394 | return __bpf_prog_get(ufd, NULL, false); |
113214be DB |
1395 | } |
1396 | ||
248f346f | 1397 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
288b3de5 | 1398 | bool attach_drv) |
248f346f | 1399 | { |
4d220ed0 | 1400 | return __bpf_prog_get(ufd, &type, attach_drv); |
248f346f | 1401 | } |
6c8dfe21 | 1402 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
248f346f | 1403 | |
aac3fc32 AI |
1404 | /* Initially all BPF programs could be loaded w/o specifying |
1405 | * expected_attach_type. Later for some of them specifying expected_attach_type | |
1406 | * at load time became required so that program could be validated properly. | |
1407 | * Programs of types that are allowed to be loaded both w/ and w/o (for | |
1408 | * backward compatibility) expected_attach_type, should have the default attach | |
1409 | * type assigned to expected_attach_type for the latter case, so that it can be | |
1410 | * validated later at attach time. | |
1411 | * | |
1412 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if | |
1413 | * prog type requires it but has some attach types that have to be backward | |
1414 | * compatible. | |
1415 | */ | |
1416 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) | |
1417 | { | |
1418 | switch (attr->prog_type) { | |
1419 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
1420 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't | |
1421 | * exist so checking for non-zero is the way to go here. | |
1422 | */ | |
1423 | if (!attr->expected_attach_type) | |
1424 | attr->expected_attach_type = | |
1425 | BPF_CGROUP_INET_SOCK_CREATE; | |
1426 | break; | |
1427 | } | |
1428 | } | |
1429 | ||
5e43f899 AI |
1430 | static int |
1431 | bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, | |
1432 | enum bpf_attach_type expected_attach_type) | |
1433 | { | |
4fbac77d | 1434 | switch (prog_type) { |
aac3fc32 AI |
1435 | case BPF_PROG_TYPE_CGROUP_SOCK: |
1436 | switch (expected_attach_type) { | |
1437 | case BPF_CGROUP_INET_SOCK_CREATE: | |
1438 | case BPF_CGROUP_INET4_POST_BIND: | |
1439 | case BPF_CGROUP_INET6_POST_BIND: | |
1440 | return 0; | |
1441 | default: | |
1442 | return -EINVAL; | |
1443 | } | |
4fbac77d AI |
1444 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
1445 | switch (expected_attach_type) { | |
1446 | case BPF_CGROUP_INET4_BIND: | |
1447 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
1448 | case BPF_CGROUP_INET4_CONNECT: |
1449 | case BPF_CGROUP_INET6_CONNECT: | |
1cedee13 AI |
1450 | case BPF_CGROUP_UDP4_SENDMSG: |
1451 | case BPF_CGROUP_UDP6_SENDMSG: | |
4fbac77d AI |
1452 | return 0; |
1453 | default: | |
1454 | return -EINVAL; | |
1455 | } | |
1456 | default: | |
1457 | return 0; | |
1458 | } | |
5e43f899 AI |
1459 | } |
1460 | ||
09756af4 | 1461 | /* last field in 'union bpf_attr' used by this command */ |
c454a46b | 1462 | #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt |
09756af4 | 1463 | |
838e9690 | 1464 | static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) |
09756af4 AS |
1465 | { |
1466 | enum bpf_prog_type type = attr->prog_type; | |
1467 | struct bpf_prog *prog; | |
1468 | int err; | |
1469 | char license[128]; | |
1470 | bool is_gpl; | |
1471 | ||
1472 | if (CHECK_ATTR(BPF_PROG_LOAD)) | |
1473 | return -EINVAL; | |
1474 | ||
e9ee9efc | 1475 | if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT)) |
e07b98d9 DM |
1476 | return -EINVAL; |
1477 | ||
e9ee9efc DM |
1478 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && |
1479 | (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && | |
1480 | !capable(CAP_SYS_ADMIN)) | |
1481 | return -EPERM; | |
1482 | ||
09756af4 | 1483 | /* copy eBPF program license from user space */ |
535e7b4b | 1484 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
09756af4 AS |
1485 | sizeof(license) - 1) < 0) |
1486 | return -EFAULT; | |
1487 | license[sizeof(license) - 1] = 0; | |
1488 | ||
1489 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
1490 | is_gpl = license_is_gpl_compatible(license); | |
1491 | ||
ef0915ca DB |
1492 | if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) |
1493 | return -E2BIG; | |
80b7d819 CF |
1494 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
1495 | type != BPF_PROG_TYPE_CGROUP_SKB && | |
1496 | !capable(CAP_SYS_ADMIN)) | |
1be7f75d AS |
1497 | return -EPERM; |
1498 | ||
aac3fc32 | 1499 | bpf_prog_load_fixup_attach_type(attr); |
5e43f899 AI |
1500 | if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) |
1501 | return -EINVAL; | |
1502 | ||
09756af4 AS |
1503 | /* plain bpf_prog allocation */ |
1504 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); | |
1505 | if (!prog) | |
1506 | return -ENOMEM; | |
1507 | ||
5e43f899 AI |
1508 | prog->expected_attach_type = attr->expected_attach_type; |
1509 | ||
9a18eedb JK |
1510 | prog->aux->offload_requested = !!attr->prog_ifindex; |
1511 | ||
afdb09c7 | 1512 | err = security_bpf_prog_alloc(prog->aux); |
aaac3ba9 AS |
1513 | if (err) |
1514 | goto free_prog_nouncharge; | |
1515 | ||
afdb09c7 CF |
1516 | err = bpf_prog_charge_memlock(prog); |
1517 | if (err) | |
1518 | goto free_prog_sec; | |
1519 | ||
09756af4 AS |
1520 | prog->len = attr->insn_cnt; |
1521 | ||
1522 | err = -EFAULT; | |
535e7b4b | 1523 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
aafe6ae9 | 1524 | bpf_prog_insn_size(prog)) != 0) |
09756af4 AS |
1525 | goto free_prog; |
1526 | ||
1527 | prog->orig_prog = NULL; | |
a91263d5 | 1528 | prog->jited = 0; |
09756af4 AS |
1529 | |
1530 | atomic_set(&prog->aux->refcnt, 1); | |
a91263d5 | 1531 | prog->gpl_compatible = is_gpl ? 1 : 0; |
09756af4 | 1532 | |
9a18eedb | 1533 | if (bpf_prog_is_dev_bound(prog->aux)) { |
ab3f0063 JK |
1534 | err = bpf_prog_offload_init(prog, attr); |
1535 | if (err) | |
1536 | goto free_prog; | |
1537 | } | |
1538 | ||
09756af4 AS |
1539 | /* find program type: socket_filter vs tracing_filter */ |
1540 | err = find_prog_type(type, prog); | |
1541 | if (err < 0) | |
1542 | goto free_prog; | |
1543 | ||
cb4d2b3f MKL |
1544 | prog->aux->load_time = ktime_get_boot_ns(); |
1545 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); | |
1546 | if (err) | |
1547 | goto free_prog; | |
1548 | ||
09756af4 | 1549 | /* run eBPF verifier */ |
838e9690 | 1550 | err = bpf_check(&prog, attr, uattr); |
09756af4 AS |
1551 | if (err < 0) |
1552 | goto free_used_maps; | |
1553 | ||
9facc336 | 1554 | prog = bpf_prog_select_runtime(prog, &err); |
04fd61ab AS |
1555 | if (err < 0) |
1556 | goto free_used_maps; | |
09756af4 | 1557 | |
dc4bb0e2 MKL |
1558 | err = bpf_prog_alloc_id(prog); |
1559 | if (err) | |
1560 | goto free_used_maps; | |
1561 | ||
aa79781b | 1562 | err = bpf_prog_new_fd(prog); |
b16d9aa4 MKL |
1563 | if (err < 0) { |
1564 | /* failed to allocate fd. | |
1565 | * bpf_prog_put() is needed because the above | |
1566 | * bpf_prog_alloc_id() has published the prog | |
1567 | * to the userspace and the userspace may | |
1568 | * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. | |
1569 | */ | |
1570 | bpf_prog_put(prog); | |
1571 | return err; | |
1572 | } | |
09756af4 | 1573 | |
74451e66 | 1574 | bpf_prog_kallsyms_add(prog); |
09756af4 AS |
1575 | return err; |
1576 | ||
1577 | free_used_maps: | |
c454a46b | 1578 | bpf_prog_free_linfo(prog); |
5482e9a9 MKL |
1579 | kvfree(prog->aux->func_info); |
1580 | btf_put(prog->aux->btf); | |
7d1982b4 | 1581 | bpf_prog_kallsyms_del_subprogs(prog); |
09756af4 AS |
1582 | free_used_maps(prog->aux); |
1583 | free_prog: | |
aaac3ba9 | 1584 | bpf_prog_uncharge_memlock(prog); |
afdb09c7 CF |
1585 | free_prog_sec: |
1586 | security_bpf_prog_free(prog->aux); | |
aaac3ba9 | 1587 | free_prog_nouncharge: |
09756af4 AS |
1588 | bpf_prog_free(prog); |
1589 | return err; | |
1590 | } | |
1591 | ||
6e71b04a | 1592 | #define BPF_OBJ_LAST_FIELD file_flags |
b2197755 DB |
1593 | |
1594 | static int bpf_obj_pin(const union bpf_attr *attr) | |
1595 | { | |
6e71b04a | 1596 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
b2197755 DB |
1597 | return -EINVAL; |
1598 | ||
535e7b4b | 1599 | return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); |
b2197755 DB |
1600 | } |
1601 | ||
1602 | static int bpf_obj_get(const union bpf_attr *attr) | |
1603 | { | |
6e71b04a CF |
1604 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
1605 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) | |
b2197755 DB |
1606 | return -EINVAL; |
1607 | ||
6e71b04a CF |
1608 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
1609 | attr->file_flags); | |
b2197755 DB |
1610 | } |
1611 | ||
c4f6699d AS |
1612 | struct bpf_raw_tracepoint { |
1613 | struct bpf_raw_event_map *btp; | |
1614 | struct bpf_prog *prog; | |
1615 | }; | |
1616 | ||
1617 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) | |
1618 | { | |
1619 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; | |
1620 | ||
1621 | if (raw_tp->prog) { | |
1622 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); | |
1623 | bpf_prog_put(raw_tp->prog); | |
1624 | } | |
a38d1107 | 1625 | bpf_put_raw_tracepoint(raw_tp->btp); |
c4f6699d AS |
1626 | kfree(raw_tp); |
1627 | return 0; | |
1628 | } | |
1629 | ||
1630 | static const struct file_operations bpf_raw_tp_fops = { | |
1631 | .release = bpf_raw_tracepoint_release, | |
1632 | .read = bpf_dummy_read, | |
1633 | .write = bpf_dummy_write, | |
1634 | }; | |
1635 | ||
1636 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd | |
1637 | ||
1638 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) | |
1639 | { | |
1640 | struct bpf_raw_tracepoint *raw_tp; | |
1641 | struct bpf_raw_event_map *btp; | |
1642 | struct bpf_prog *prog; | |
1643 | char tp_name[128]; | |
1644 | int tp_fd, err; | |
1645 | ||
1646 | if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), | |
1647 | sizeof(tp_name) - 1) < 0) | |
1648 | return -EFAULT; | |
1649 | tp_name[sizeof(tp_name) - 1] = 0; | |
1650 | ||
a38d1107 | 1651 | btp = bpf_get_raw_tracepoint(tp_name); |
c4f6699d AS |
1652 | if (!btp) |
1653 | return -ENOENT; | |
1654 | ||
1655 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); | |
a38d1107 MM |
1656 | if (!raw_tp) { |
1657 | err = -ENOMEM; | |
1658 | goto out_put_btp; | |
1659 | } | |
c4f6699d AS |
1660 | raw_tp->btp = btp; |
1661 | ||
1662 | prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd, | |
1663 | BPF_PROG_TYPE_RAW_TRACEPOINT); | |
1664 | if (IS_ERR(prog)) { | |
1665 | err = PTR_ERR(prog); | |
1666 | goto out_free_tp; | |
1667 | } | |
1668 | ||
1669 | err = bpf_probe_register(raw_tp->btp, prog); | |
1670 | if (err) | |
1671 | goto out_put_prog; | |
1672 | ||
1673 | raw_tp->prog = prog; | |
1674 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, | |
1675 | O_CLOEXEC); | |
1676 | if (tp_fd < 0) { | |
1677 | bpf_probe_unregister(raw_tp->btp, prog); | |
1678 | err = tp_fd; | |
1679 | goto out_put_prog; | |
1680 | } | |
1681 | return tp_fd; | |
1682 | ||
1683 | out_put_prog: | |
1684 | bpf_prog_put(prog); | |
1685 | out_free_tp: | |
1686 | kfree(raw_tp); | |
a38d1107 MM |
1687 | out_put_btp: |
1688 | bpf_put_raw_tracepoint(btp); | |
c4f6699d AS |
1689 | return err; |
1690 | } | |
1691 | ||
33491588 AR |
1692 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
1693 | enum bpf_attach_type attach_type) | |
1694 | { | |
1695 | switch (prog->type) { | |
1696 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
1697 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: | |
1698 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; | |
1699 | default: | |
1700 | return 0; | |
1701 | } | |
1702 | } | |
1703 | ||
464bc0fd | 1704 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
174a79ff | 1705 | |
324bda9e AS |
1706 | #define BPF_F_ATTACH_MASK \ |
1707 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) | |
1708 | ||
f4324551 DM |
1709 | static int bpf_prog_attach(const union bpf_attr *attr) |
1710 | { | |
7f677633 | 1711 | enum bpf_prog_type ptype; |
f4324551 | 1712 | struct bpf_prog *prog; |
7f677633 | 1713 | int ret; |
f4324551 DM |
1714 | |
1715 | if (!capable(CAP_NET_ADMIN)) | |
1716 | return -EPERM; | |
1717 | ||
1718 | if (CHECK_ATTR(BPF_PROG_ATTACH)) | |
1719 | return -EINVAL; | |
1720 | ||
324bda9e | 1721 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
7f677633 AS |
1722 | return -EINVAL; |
1723 | ||
f4324551 DM |
1724 | switch (attr->attach_type) { |
1725 | case BPF_CGROUP_INET_INGRESS: | |
1726 | case BPF_CGROUP_INET_EGRESS: | |
b2cd1257 | 1727 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
f4324551 | 1728 | break; |
61023658 | 1729 | case BPF_CGROUP_INET_SOCK_CREATE: |
aac3fc32 AI |
1730 | case BPF_CGROUP_INET4_POST_BIND: |
1731 | case BPF_CGROUP_INET6_POST_BIND: | |
61023658 DA |
1732 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
1733 | break; | |
4fbac77d AI |
1734 | case BPF_CGROUP_INET4_BIND: |
1735 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
1736 | case BPF_CGROUP_INET4_CONNECT: |
1737 | case BPF_CGROUP_INET6_CONNECT: | |
1cedee13 AI |
1738 | case BPF_CGROUP_UDP4_SENDMSG: |
1739 | case BPF_CGROUP_UDP6_SENDMSG: | |
4fbac77d AI |
1740 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
1741 | break; | |
40304b2a LB |
1742 | case BPF_CGROUP_SOCK_OPS: |
1743 | ptype = BPF_PROG_TYPE_SOCK_OPS; | |
1744 | break; | |
ebc614f6 RG |
1745 | case BPF_CGROUP_DEVICE: |
1746 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; | |
1747 | break; | |
4f738adb | 1748 | case BPF_SK_MSG_VERDICT: |
fdb5c453 SY |
1749 | ptype = BPF_PROG_TYPE_SK_MSG; |
1750 | break; | |
464bc0fd JF |
1751 | case BPF_SK_SKB_STREAM_PARSER: |
1752 | case BPF_SK_SKB_STREAM_VERDICT: | |
fdb5c453 SY |
1753 | ptype = BPF_PROG_TYPE_SK_SKB; |
1754 | break; | |
f4364dcf | 1755 | case BPF_LIRC_MODE2: |
fdb5c453 SY |
1756 | ptype = BPF_PROG_TYPE_LIRC_MODE2; |
1757 | break; | |
d58e468b PP |
1758 | case BPF_FLOW_DISSECTOR: |
1759 | ptype = BPF_PROG_TYPE_FLOW_DISSECTOR; | |
1760 | break; | |
f4324551 DM |
1761 | default: |
1762 | return -EINVAL; | |
1763 | } | |
1764 | ||
b2cd1257 DA |
1765 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
1766 | if (IS_ERR(prog)) | |
1767 | return PTR_ERR(prog); | |
1768 | ||
5e43f899 AI |
1769 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
1770 | bpf_prog_put(prog); | |
1771 | return -EINVAL; | |
1772 | } | |
1773 | ||
fdb5c453 SY |
1774 | switch (ptype) { |
1775 | case BPF_PROG_TYPE_SK_SKB: | |
1776 | case BPF_PROG_TYPE_SK_MSG: | |
604326b4 | 1777 | ret = sock_map_get_from_fd(attr, prog); |
fdb5c453 SY |
1778 | break; |
1779 | case BPF_PROG_TYPE_LIRC_MODE2: | |
1780 | ret = lirc_prog_attach(attr, prog); | |
1781 | break; | |
d58e468b PP |
1782 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
1783 | ret = skb_flow_dissector_bpf_prog_attach(attr, prog); | |
1784 | break; | |
fdb5c453 SY |
1785 | default: |
1786 | ret = cgroup_bpf_prog_attach(attr, ptype, prog); | |
b2cd1257 DA |
1787 | } |
1788 | ||
7f677633 AS |
1789 | if (ret) |
1790 | bpf_prog_put(prog); | |
7f677633 | 1791 | return ret; |
f4324551 DM |
1792 | } |
1793 | ||
1794 | #define BPF_PROG_DETACH_LAST_FIELD attach_type | |
1795 | ||
1796 | static int bpf_prog_detach(const union bpf_attr *attr) | |
1797 | { | |
324bda9e | 1798 | enum bpf_prog_type ptype; |
f4324551 DM |
1799 | |
1800 | if (!capable(CAP_NET_ADMIN)) | |
1801 | return -EPERM; | |
1802 | ||
1803 | if (CHECK_ATTR(BPF_PROG_DETACH)) | |
1804 | return -EINVAL; | |
1805 | ||
1806 | switch (attr->attach_type) { | |
1807 | case BPF_CGROUP_INET_INGRESS: | |
1808 | case BPF_CGROUP_INET_EGRESS: | |
324bda9e AS |
1809 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
1810 | break; | |
61023658 | 1811 | case BPF_CGROUP_INET_SOCK_CREATE: |
aac3fc32 AI |
1812 | case BPF_CGROUP_INET4_POST_BIND: |
1813 | case BPF_CGROUP_INET6_POST_BIND: | |
324bda9e AS |
1814 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
1815 | break; | |
4fbac77d AI |
1816 | case BPF_CGROUP_INET4_BIND: |
1817 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
1818 | case BPF_CGROUP_INET4_CONNECT: |
1819 | case BPF_CGROUP_INET6_CONNECT: | |
1cedee13 AI |
1820 | case BPF_CGROUP_UDP4_SENDMSG: |
1821 | case BPF_CGROUP_UDP6_SENDMSG: | |
4fbac77d AI |
1822 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
1823 | break; | |
40304b2a | 1824 | case BPF_CGROUP_SOCK_OPS: |
324bda9e | 1825 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
f4324551 | 1826 | break; |
ebc614f6 RG |
1827 | case BPF_CGROUP_DEVICE: |
1828 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; | |
1829 | break; | |
4f738adb | 1830 | case BPF_SK_MSG_VERDICT: |
604326b4 | 1831 | return sock_map_get_from_fd(attr, NULL); |
5a67da2a JF |
1832 | case BPF_SK_SKB_STREAM_PARSER: |
1833 | case BPF_SK_SKB_STREAM_VERDICT: | |
604326b4 | 1834 | return sock_map_get_from_fd(attr, NULL); |
f4364dcf SY |
1835 | case BPF_LIRC_MODE2: |
1836 | return lirc_prog_detach(attr); | |
d58e468b PP |
1837 | case BPF_FLOW_DISSECTOR: |
1838 | return skb_flow_dissector_bpf_prog_detach(attr); | |
f4324551 DM |
1839 | default: |
1840 | return -EINVAL; | |
1841 | } | |
1842 | ||
fdb5c453 | 1843 | return cgroup_bpf_prog_detach(attr, ptype); |
f4324551 | 1844 | } |
40304b2a | 1845 | |
468e2f64 AS |
1846 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
1847 | ||
1848 | static int bpf_prog_query(const union bpf_attr *attr, | |
1849 | union bpf_attr __user *uattr) | |
1850 | { | |
468e2f64 AS |
1851 | if (!capable(CAP_NET_ADMIN)) |
1852 | return -EPERM; | |
1853 | if (CHECK_ATTR(BPF_PROG_QUERY)) | |
1854 | return -EINVAL; | |
1855 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) | |
1856 | return -EINVAL; | |
1857 | ||
1858 | switch (attr->query.attach_type) { | |
1859 | case BPF_CGROUP_INET_INGRESS: | |
1860 | case BPF_CGROUP_INET_EGRESS: | |
1861 | case BPF_CGROUP_INET_SOCK_CREATE: | |
4fbac77d AI |
1862 | case BPF_CGROUP_INET4_BIND: |
1863 | case BPF_CGROUP_INET6_BIND: | |
aac3fc32 AI |
1864 | case BPF_CGROUP_INET4_POST_BIND: |
1865 | case BPF_CGROUP_INET6_POST_BIND: | |
d74bad4e AI |
1866 | case BPF_CGROUP_INET4_CONNECT: |
1867 | case BPF_CGROUP_INET6_CONNECT: | |
1cedee13 AI |
1868 | case BPF_CGROUP_UDP4_SENDMSG: |
1869 | case BPF_CGROUP_UDP6_SENDMSG: | |
468e2f64 | 1870 | case BPF_CGROUP_SOCK_OPS: |
ebc614f6 | 1871 | case BPF_CGROUP_DEVICE: |
468e2f64 | 1872 | break; |
f4364dcf SY |
1873 | case BPF_LIRC_MODE2: |
1874 | return lirc_prog_query(attr, uattr); | |
468e2f64 AS |
1875 | default: |
1876 | return -EINVAL; | |
1877 | } | |
fdb5c453 SY |
1878 | |
1879 | return cgroup_bpf_prog_query(attr, uattr); | |
468e2f64 | 1880 | } |
f4324551 | 1881 | |
1cf1cae9 AS |
1882 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration |
1883 | ||
1884 | static int bpf_prog_test_run(const union bpf_attr *attr, | |
1885 | union bpf_attr __user *uattr) | |
1886 | { | |
1887 | struct bpf_prog *prog; | |
1888 | int ret = -ENOTSUPP; | |
1889 | ||
61f3c964 AS |
1890 | if (!capable(CAP_SYS_ADMIN)) |
1891 | return -EPERM; | |
1cf1cae9 AS |
1892 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
1893 | return -EINVAL; | |
1894 | ||
1895 | prog = bpf_prog_get(attr->test.prog_fd); | |
1896 | if (IS_ERR(prog)) | |
1897 | return PTR_ERR(prog); | |
1898 | ||
1899 | if (prog->aux->ops->test_run) | |
1900 | ret = prog->aux->ops->test_run(prog, attr, uattr); | |
1901 | ||
1902 | bpf_prog_put(prog); | |
1903 | return ret; | |
1904 | } | |
1905 | ||
34ad5580 MKL |
1906 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
1907 | ||
1908 | static int bpf_obj_get_next_id(const union bpf_attr *attr, | |
1909 | union bpf_attr __user *uattr, | |
1910 | struct idr *idr, | |
1911 | spinlock_t *lock) | |
1912 | { | |
1913 | u32 next_id = attr->start_id; | |
1914 | int err = 0; | |
1915 | ||
1916 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) | |
1917 | return -EINVAL; | |
1918 | ||
1919 | if (!capable(CAP_SYS_ADMIN)) | |
1920 | return -EPERM; | |
1921 | ||
1922 | next_id++; | |
1923 | spin_lock_bh(lock); | |
1924 | if (!idr_get_next(idr, &next_id)) | |
1925 | err = -ENOENT; | |
1926 | spin_unlock_bh(lock); | |
1927 | ||
1928 | if (!err) | |
1929 | err = put_user(next_id, &uattr->next_id); | |
1930 | ||
1931 | return err; | |
1932 | } | |
1933 | ||
b16d9aa4 MKL |
1934 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
1935 | ||
1936 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) | |
1937 | { | |
1938 | struct bpf_prog *prog; | |
1939 | u32 id = attr->prog_id; | |
1940 | int fd; | |
1941 | ||
1942 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) | |
1943 | return -EINVAL; | |
1944 | ||
1945 | if (!capable(CAP_SYS_ADMIN)) | |
1946 | return -EPERM; | |
1947 | ||
1948 | spin_lock_bh(&prog_idr_lock); | |
1949 | prog = idr_find(&prog_idr, id); | |
1950 | if (prog) | |
1951 | prog = bpf_prog_inc_not_zero(prog); | |
1952 | else | |
1953 | prog = ERR_PTR(-ENOENT); | |
1954 | spin_unlock_bh(&prog_idr_lock); | |
1955 | ||
1956 | if (IS_ERR(prog)) | |
1957 | return PTR_ERR(prog); | |
1958 | ||
1959 | fd = bpf_prog_new_fd(prog); | |
1960 | if (fd < 0) | |
1961 | bpf_prog_put(prog); | |
1962 | ||
1963 | return fd; | |
1964 | } | |
1965 | ||
6e71b04a | 1966 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
bd5f5f4e MKL |
1967 | |
1968 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) | |
1969 | { | |
1970 | struct bpf_map *map; | |
1971 | u32 id = attr->map_id; | |
6e71b04a | 1972 | int f_flags; |
bd5f5f4e MKL |
1973 | int fd; |
1974 | ||
6e71b04a CF |
1975 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
1976 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) | |
bd5f5f4e MKL |
1977 | return -EINVAL; |
1978 | ||
1979 | if (!capable(CAP_SYS_ADMIN)) | |
1980 | return -EPERM; | |
1981 | ||
6e71b04a CF |
1982 | f_flags = bpf_get_file_flag(attr->open_flags); |
1983 | if (f_flags < 0) | |
1984 | return f_flags; | |
1985 | ||
bd5f5f4e MKL |
1986 | spin_lock_bh(&map_idr_lock); |
1987 | map = idr_find(&map_idr, id); | |
1988 | if (map) | |
1989 | map = bpf_map_inc_not_zero(map, true); | |
1990 | else | |
1991 | map = ERR_PTR(-ENOENT); | |
1992 | spin_unlock_bh(&map_idr_lock); | |
1993 | ||
1994 | if (IS_ERR(map)) | |
1995 | return PTR_ERR(map); | |
1996 | ||
6e71b04a | 1997 | fd = bpf_map_new_fd(map, f_flags); |
bd5f5f4e MKL |
1998 | if (fd < 0) |
1999 | bpf_map_put(map); | |
2000 | ||
2001 | return fd; | |
2002 | } | |
2003 | ||
7105e828 DB |
2004 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
2005 | unsigned long addr) | |
2006 | { | |
2007 | int i; | |
2008 | ||
2009 | for (i = 0; i < prog->aux->used_map_cnt; i++) | |
2010 | if (prog->aux->used_maps[i] == (void *)addr) | |
2011 | return prog->aux->used_maps[i]; | |
2012 | return NULL; | |
2013 | } | |
2014 | ||
2015 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) | |
2016 | { | |
2017 | const struct bpf_map *map; | |
2018 | struct bpf_insn *insns; | |
2019 | u64 imm; | |
2020 | int i; | |
2021 | ||
2022 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), | |
2023 | GFP_USER); | |
2024 | if (!insns) | |
2025 | return insns; | |
2026 | ||
2027 | for (i = 0; i < prog->len; i++) { | |
2028 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { | |
2029 | insns[i].code = BPF_JMP | BPF_CALL; | |
2030 | insns[i].imm = BPF_FUNC_tail_call; | |
2031 | /* fall-through */ | |
2032 | } | |
2033 | if (insns[i].code == (BPF_JMP | BPF_CALL) || | |
2034 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { | |
2035 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) | |
2036 | insns[i].code = BPF_JMP | BPF_CALL; | |
2037 | if (!bpf_dump_raw_ok()) | |
2038 | insns[i].imm = 0; | |
2039 | continue; | |
2040 | } | |
2041 | ||
2042 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) | |
2043 | continue; | |
2044 | ||
2045 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; | |
2046 | map = bpf_map_from_imm(prog, imm); | |
2047 | if (map) { | |
2048 | insns[i].src_reg = BPF_PSEUDO_MAP_FD; | |
2049 | insns[i].imm = map->id; | |
2050 | insns[i + 1].imm = 0; | |
2051 | continue; | |
2052 | } | |
7105e828 DB |
2053 | } |
2054 | ||
2055 | return insns; | |
2056 | } | |
2057 | ||
c454a46b MKL |
2058 | static int set_info_rec_size(struct bpf_prog_info *info) |
2059 | { | |
2060 | /* | |
2061 | * Ensure info.*_rec_size is the same as kernel expected size | |
2062 | * | |
2063 | * or | |
2064 | * | |
2065 | * Only allow zero *_rec_size if both _rec_size and _cnt are | |
2066 | * zero. In this case, the kernel will set the expected | |
2067 | * _rec_size back to the info. | |
2068 | */ | |
2069 | ||
11d8b82d | 2070 | if ((info->nr_func_info || info->func_info_rec_size) && |
c454a46b MKL |
2071 | info->func_info_rec_size != sizeof(struct bpf_func_info)) |
2072 | return -EINVAL; | |
2073 | ||
11d8b82d | 2074 | if ((info->nr_line_info || info->line_info_rec_size) && |
c454a46b MKL |
2075 | info->line_info_rec_size != sizeof(struct bpf_line_info)) |
2076 | return -EINVAL; | |
2077 | ||
11d8b82d | 2078 | if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && |
c454a46b MKL |
2079 | info->jited_line_info_rec_size != sizeof(__u64)) |
2080 | return -EINVAL; | |
2081 | ||
2082 | info->func_info_rec_size = sizeof(struct bpf_func_info); | |
2083 | info->line_info_rec_size = sizeof(struct bpf_line_info); | |
2084 | info->jited_line_info_rec_size = sizeof(__u64); | |
2085 | ||
2086 | return 0; | |
2087 | } | |
2088 | ||
1e270976 MKL |
2089 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
2090 | const union bpf_attr *attr, | |
2091 | union bpf_attr __user *uattr) | |
2092 | { | |
2093 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
2094 | struct bpf_prog_info info = {}; | |
2095 | u32 info_len = attr->info.info_len; | |
2096 | char __user *uinsns; | |
2097 | u32 ulen; | |
2098 | int err; | |
2099 | ||
dcab51f1 | 2100 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
1e270976 MKL |
2101 | if (err) |
2102 | return err; | |
2103 | info_len = min_t(u32, sizeof(info), info_len); | |
2104 | ||
2105 | if (copy_from_user(&info, uinfo, info_len)) | |
89b09689 | 2106 | return -EFAULT; |
1e270976 MKL |
2107 | |
2108 | info.type = prog->type; | |
2109 | info.id = prog->aux->id; | |
cb4d2b3f MKL |
2110 | info.load_time = prog->aux->load_time; |
2111 | info.created_by_uid = from_kuid_munged(current_user_ns(), | |
2112 | prog->aux->user->uid); | |
b85fab0e | 2113 | info.gpl_compatible = prog->gpl_compatible; |
1e270976 MKL |
2114 | |
2115 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); | |
cb4d2b3f MKL |
2116 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
2117 | ||
2118 | ulen = info.nr_map_ids; | |
2119 | info.nr_map_ids = prog->aux->used_map_cnt; | |
2120 | ulen = min_t(u32, info.nr_map_ids, ulen); | |
2121 | if (ulen) { | |
721e08da | 2122 | u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); |
cb4d2b3f MKL |
2123 | u32 i; |
2124 | ||
2125 | for (i = 0; i < ulen; i++) | |
2126 | if (put_user(prog->aux->used_maps[i]->id, | |
2127 | &user_map_ids[i])) | |
2128 | return -EFAULT; | |
2129 | } | |
1e270976 | 2130 | |
c454a46b MKL |
2131 | err = set_info_rec_size(&info); |
2132 | if (err) | |
2133 | return err; | |
7337224f | 2134 | |
1e270976 MKL |
2135 | if (!capable(CAP_SYS_ADMIN)) { |
2136 | info.jited_prog_len = 0; | |
2137 | info.xlated_prog_len = 0; | |
dbecd738 | 2138 | info.nr_jited_ksyms = 0; |
28c2fae7 | 2139 | info.nr_jited_func_lens = 0; |
11d8b82d YS |
2140 | info.nr_func_info = 0; |
2141 | info.nr_line_info = 0; | |
2142 | info.nr_jited_line_info = 0; | |
1e270976 MKL |
2143 | goto done; |
2144 | } | |
2145 | ||
1e270976 | 2146 | ulen = info.xlated_prog_len; |
9975a54b | 2147 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
1e270976 | 2148 | if (info.xlated_prog_len && ulen) { |
7105e828 DB |
2149 | struct bpf_insn *insns_sanitized; |
2150 | bool fault; | |
2151 | ||
2152 | if (prog->blinded && !bpf_dump_raw_ok()) { | |
2153 | info.xlated_prog_insns = 0; | |
2154 | goto done; | |
2155 | } | |
2156 | insns_sanitized = bpf_insn_prepare_dump(prog); | |
2157 | if (!insns_sanitized) | |
2158 | return -ENOMEM; | |
1e270976 MKL |
2159 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
2160 | ulen = min_t(u32, info.xlated_prog_len, ulen); | |
7105e828 DB |
2161 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
2162 | kfree(insns_sanitized); | |
2163 | if (fault) | |
1e270976 MKL |
2164 | return -EFAULT; |
2165 | } | |
2166 | ||
675fc275 JK |
2167 | if (bpf_prog_is_dev_bound(prog->aux)) { |
2168 | err = bpf_prog_offload_info_fill(&info, prog); | |
2169 | if (err) | |
2170 | return err; | |
fcfb126d JW |
2171 | goto done; |
2172 | } | |
2173 | ||
2174 | /* NOTE: the following code is supposed to be skipped for offload. | |
2175 | * bpf_prog_offload_info_fill() is the place to fill similar fields | |
2176 | * for offload. | |
2177 | */ | |
2178 | ulen = info.jited_prog_len; | |
4d56a76e SD |
2179 | if (prog->aux->func_cnt) { |
2180 | u32 i; | |
2181 | ||
2182 | info.jited_prog_len = 0; | |
2183 | for (i = 0; i < prog->aux->func_cnt; i++) | |
2184 | info.jited_prog_len += prog->aux->func[i]->jited_len; | |
2185 | } else { | |
2186 | info.jited_prog_len = prog->jited_len; | |
2187 | } | |
2188 | ||
fcfb126d JW |
2189 | if (info.jited_prog_len && ulen) { |
2190 | if (bpf_dump_raw_ok()) { | |
2191 | uinsns = u64_to_user_ptr(info.jited_prog_insns); | |
2192 | ulen = min_t(u32, info.jited_prog_len, ulen); | |
4d56a76e SD |
2193 | |
2194 | /* for multi-function programs, copy the JITed | |
2195 | * instructions for all the functions | |
2196 | */ | |
2197 | if (prog->aux->func_cnt) { | |
2198 | u32 len, free, i; | |
2199 | u8 *img; | |
2200 | ||
2201 | free = ulen; | |
2202 | for (i = 0; i < prog->aux->func_cnt; i++) { | |
2203 | len = prog->aux->func[i]->jited_len; | |
2204 | len = min_t(u32, len, free); | |
2205 | img = (u8 *) prog->aux->func[i]->bpf_func; | |
2206 | if (copy_to_user(uinsns, img, len)) | |
2207 | return -EFAULT; | |
2208 | uinsns += len; | |
2209 | free -= len; | |
2210 | if (!free) | |
2211 | break; | |
2212 | } | |
2213 | } else { | |
2214 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) | |
2215 | return -EFAULT; | |
2216 | } | |
fcfb126d JW |
2217 | } else { |
2218 | info.jited_prog_insns = 0; | |
2219 | } | |
675fc275 JK |
2220 | } |
2221 | ||
dbecd738 | 2222 | ulen = info.nr_jited_ksyms; |
ff1889fc | 2223 | info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; |
7a5725dd | 2224 | if (ulen) { |
dbecd738 | 2225 | if (bpf_dump_raw_ok()) { |
ff1889fc | 2226 | unsigned long ksym_addr; |
dbecd738 | 2227 | u64 __user *user_ksyms; |
dbecd738 SD |
2228 | u32 i; |
2229 | ||
2230 | /* copy the address of the kernel symbol | |
2231 | * corresponding to each function | |
2232 | */ | |
2233 | ulen = min_t(u32, info.nr_jited_ksyms, ulen); | |
2234 | user_ksyms = u64_to_user_ptr(info.jited_ksyms); | |
ff1889fc SL |
2235 | if (prog->aux->func_cnt) { |
2236 | for (i = 0; i < ulen; i++) { | |
2237 | ksym_addr = (unsigned long) | |
2238 | prog->aux->func[i]->bpf_func; | |
2239 | if (put_user((u64) ksym_addr, | |
2240 | &user_ksyms[i])) | |
2241 | return -EFAULT; | |
2242 | } | |
2243 | } else { | |
2244 | ksym_addr = (unsigned long) prog->bpf_func; | |
2245 | if (put_user((u64) ksym_addr, &user_ksyms[0])) | |
dbecd738 SD |
2246 | return -EFAULT; |
2247 | } | |
2248 | } else { | |
2249 | info.jited_ksyms = 0; | |
2250 | } | |
2251 | } | |
2252 | ||
815581c1 | 2253 | ulen = info.nr_jited_func_lens; |
ff1889fc | 2254 | info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; |
7a5725dd | 2255 | if (ulen) { |
815581c1 SD |
2256 | if (bpf_dump_raw_ok()) { |
2257 | u32 __user *user_lens; | |
2258 | u32 func_len, i; | |
2259 | ||
2260 | /* copy the JITed image lengths for each function */ | |
2261 | ulen = min_t(u32, info.nr_jited_func_lens, ulen); | |
2262 | user_lens = u64_to_user_ptr(info.jited_func_lens); | |
ff1889fc SL |
2263 | if (prog->aux->func_cnt) { |
2264 | for (i = 0; i < ulen; i++) { | |
2265 | func_len = | |
2266 | prog->aux->func[i]->jited_len; | |
2267 | if (put_user(func_len, &user_lens[i])) | |
2268 | return -EFAULT; | |
2269 | } | |
2270 | } else { | |
2271 | func_len = prog->jited_len; | |
2272 | if (put_user(func_len, &user_lens[0])) | |
815581c1 SD |
2273 | return -EFAULT; |
2274 | } | |
2275 | } else { | |
2276 | info.jited_func_lens = 0; | |
2277 | } | |
2278 | } | |
2279 | ||
7337224f | 2280 | if (prog->aux->btf) |
838e9690 YS |
2281 | info.btf_id = btf_id(prog->aux->btf); |
2282 | ||
11d8b82d YS |
2283 | ulen = info.nr_func_info; |
2284 | info.nr_func_info = prog->aux->func_info_cnt; | |
2285 | if (info.nr_func_info && ulen) { | |
9e794163 | 2286 | char __user *user_finfo; |
7337224f | 2287 | |
9e794163 MKL |
2288 | user_finfo = u64_to_user_ptr(info.func_info); |
2289 | ulen = min_t(u32, info.nr_func_info, ulen); | |
2290 | if (copy_to_user(user_finfo, prog->aux->func_info, | |
2291 | info.func_info_rec_size * ulen)) | |
2292 | return -EFAULT; | |
838e9690 YS |
2293 | } |
2294 | ||
11d8b82d YS |
2295 | ulen = info.nr_line_info; |
2296 | info.nr_line_info = prog->aux->nr_linfo; | |
2297 | if (info.nr_line_info && ulen) { | |
9e794163 | 2298 | __u8 __user *user_linfo; |
c454a46b | 2299 | |
9e794163 MKL |
2300 | user_linfo = u64_to_user_ptr(info.line_info); |
2301 | ulen = min_t(u32, info.nr_line_info, ulen); | |
2302 | if (copy_to_user(user_linfo, prog->aux->linfo, | |
2303 | info.line_info_rec_size * ulen)) | |
2304 | return -EFAULT; | |
c454a46b MKL |
2305 | } |
2306 | ||
11d8b82d | 2307 | ulen = info.nr_jited_line_info; |
c454a46b | 2308 | if (prog->aux->jited_linfo) |
11d8b82d | 2309 | info.nr_jited_line_info = prog->aux->nr_linfo; |
c454a46b | 2310 | else |
11d8b82d YS |
2311 | info.nr_jited_line_info = 0; |
2312 | if (info.nr_jited_line_info && ulen) { | |
c454a46b MKL |
2313 | if (bpf_dump_raw_ok()) { |
2314 | __u64 __user *user_linfo; | |
2315 | u32 i; | |
2316 | ||
2317 | user_linfo = u64_to_user_ptr(info.jited_line_info); | |
11d8b82d | 2318 | ulen = min_t(u32, info.nr_jited_line_info, ulen); |
c454a46b MKL |
2319 | for (i = 0; i < ulen; i++) { |
2320 | if (put_user((__u64)(long)prog->aux->jited_linfo[i], | |
2321 | &user_linfo[i])) | |
2322 | return -EFAULT; | |
2323 | } | |
2324 | } else { | |
2325 | info.jited_line_info = 0; | |
2326 | } | |
2327 | } | |
2328 | ||
c872bdb3 SL |
2329 | ulen = info.nr_prog_tags; |
2330 | info.nr_prog_tags = prog->aux->func_cnt ? : 1; | |
2331 | if (ulen) { | |
2332 | __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; | |
2333 | u32 i; | |
2334 | ||
2335 | user_prog_tags = u64_to_user_ptr(info.prog_tags); | |
2336 | ulen = min_t(u32, info.nr_prog_tags, ulen); | |
2337 | if (prog->aux->func_cnt) { | |
2338 | for (i = 0; i < ulen; i++) { | |
2339 | if (copy_to_user(user_prog_tags[i], | |
2340 | prog->aux->func[i]->tag, | |
2341 | BPF_TAG_SIZE)) | |
2342 | return -EFAULT; | |
2343 | } | |
2344 | } else { | |
2345 | if (copy_to_user(user_prog_tags[0], | |
2346 | prog->tag, BPF_TAG_SIZE)) | |
2347 | return -EFAULT; | |
2348 | } | |
2349 | } | |
2350 | ||
1e270976 MKL |
2351 | done: |
2352 | if (copy_to_user(uinfo, &info, info_len) || | |
2353 | put_user(info_len, &uattr->info.info_len)) | |
2354 | return -EFAULT; | |
2355 | ||
2356 | return 0; | |
2357 | } | |
2358 | ||
2359 | static int bpf_map_get_info_by_fd(struct bpf_map *map, | |
2360 | const union bpf_attr *attr, | |
2361 | union bpf_attr __user *uattr) | |
2362 | { | |
2363 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
2364 | struct bpf_map_info info = {}; | |
2365 | u32 info_len = attr->info.info_len; | |
2366 | int err; | |
2367 | ||
dcab51f1 | 2368 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
1e270976 MKL |
2369 | if (err) |
2370 | return err; | |
2371 | info_len = min_t(u32, sizeof(info), info_len); | |
2372 | ||
2373 | info.type = map->map_type; | |
2374 | info.id = map->id; | |
2375 | info.key_size = map->key_size; | |
2376 | info.value_size = map->value_size; | |
2377 | info.max_entries = map->max_entries; | |
2378 | info.map_flags = map->map_flags; | |
ad5b177b | 2379 | memcpy(info.name, map->name, sizeof(map->name)); |
1e270976 | 2380 | |
78958fca MKL |
2381 | if (map->btf) { |
2382 | info.btf_id = btf_id(map->btf); | |
9b2cf328 MKL |
2383 | info.btf_key_type_id = map->btf_key_type_id; |
2384 | info.btf_value_type_id = map->btf_value_type_id; | |
78958fca MKL |
2385 | } |
2386 | ||
52775b33 JK |
2387 | if (bpf_map_is_dev_bound(map)) { |
2388 | err = bpf_map_offload_info_fill(&info, map); | |
2389 | if (err) | |
2390 | return err; | |
2391 | } | |
2392 | ||
1e270976 MKL |
2393 | if (copy_to_user(uinfo, &info, info_len) || |
2394 | put_user(info_len, &uattr->info.info_len)) | |
2395 | return -EFAULT; | |
2396 | ||
2397 | return 0; | |
2398 | } | |
2399 | ||
62dab84c MKL |
2400 | static int bpf_btf_get_info_by_fd(struct btf *btf, |
2401 | const union bpf_attr *attr, | |
2402 | union bpf_attr __user *uattr) | |
2403 | { | |
2404 | struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
2405 | u32 info_len = attr->info.info_len; | |
2406 | int err; | |
2407 | ||
dcab51f1 | 2408 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); |
62dab84c MKL |
2409 | if (err) |
2410 | return err; | |
2411 | ||
2412 | return btf_get_info_by_fd(btf, attr, uattr); | |
2413 | } | |
2414 | ||
1e270976 MKL |
2415 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
2416 | ||
2417 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, | |
2418 | union bpf_attr __user *uattr) | |
2419 | { | |
2420 | int ufd = attr->info.bpf_fd; | |
2421 | struct fd f; | |
2422 | int err; | |
2423 | ||
2424 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) | |
2425 | return -EINVAL; | |
2426 | ||
2427 | f = fdget(ufd); | |
2428 | if (!f.file) | |
2429 | return -EBADFD; | |
2430 | ||
2431 | if (f.file->f_op == &bpf_prog_fops) | |
2432 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, | |
2433 | uattr); | |
2434 | else if (f.file->f_op == &bpf_map_fops) | |
2435 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, | |
2436 | uattr); | |
60197cfb | 2437 | else if (f.file->f_op == &btf_fops) |
62dab84c | 2438 | err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); |
1e270976 MKL |
2439 | else |
2440 | err = -EINVAL; | |
2441 | ||
2442 | fdput(f); | |
2443 | return err; | |
2444 | } | |
2445 | ||
f56a653c MKL |
2446 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
2447 | ||
2448 | static int bpf_btf_load(const union bpf_attr *attr) | |
2449 | { | |
2450 | if (CHECK_ATTR(BPF_BTF_LOAD)) | |
2451 | return -EINVAL; | |
2452 | ||
2453 | if (!capable(CAP_SYS_ADMIN)) | |
2454 | return -EPERM; | |
2455 | ||
2456 | return btf_new_fd(attr); | |
2457 | } | |
2458 | ||
78958fca MKL |
2459 | #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id |
2460 | ||
2461 | static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) | |
2462 | { | |
2463 | if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) | |
2464 | return -EINVAL; | |
2465 | ||
2466 | if (!capable(CAP_SYS_ADMIN)) | |
2467 | return -EPERM; | |
2468 | ||
2469 | return btf_get_fd_by_id(attr->btf_id); | |
2470 | } | |
2471 | ||
41bdc4b4 YS |
2472 | static int bpf_task_fd_query_copy(const union bpf_attr *attr, |
2473 | union bpf_attr __user *uattr, | |
2474 | u32 prog_id, u32 fd_type, | |
2475 | const char *buf, u64 probe_offset, | |
2476 | u64 probe_addr) | |
2477 | { | |
2478 | char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); | |
2479 | u32 len = buf ? strlen(buf) : 0, input_len; | |
2480 | int err = 0; | |
2481 | ||
2482 | if (put_user(len, &uattr->task_fd_query.buf_len)) | |
2483 | return -EFAULT; | |
2484 | input_len = attr->task_fd_query.buf_len; | |
2485 | if (input_len && ubuf) { | |
2486 | if (!len) { | |
2487 | /* nothing to copy, just make ubuf NULL terminated */ | |
2488 | char zero = '\0'; | |
2489 | ||
2490 | if (put_user(zero, ubuf)) | |
2491 | return -EFAULT; | |
2492 | } else if (input_len >= len + 1) { | |
2493 | /* ubuf can hold the string with NULL terminator */ | |
2494 | if (copy_to_user(ubuf, buf, len + 1)) | |
2495 | return -EFAULT; | |
2496 | } else { | |
2497 | /* ubuf cannot hold the string with NULL terminator, | |
2498 | * do a partial copy with NULL terminator. | |
2499 | */ | |
2500 | char zero = '\0'; | |
2501 | ||
2502 | err = -ENOSPC; | |
2503 | if (copy_to_user(ubuf, buf, input_len - 1)) | |
2504 | return -EFAULT; | |
2505 | if (put_user(zero, ubuf + input_len - 1)) | |
2506 | return -EFAULT; | |
2507 | } | |
2508 | } | |
2509 | ||
2510 | if (put_user(prog_id, &uattr->task_fd_query.prog_id) || | |
2511 | put_user(fd_type, &uattr->task_fd_query.fd_type) || | |
2512 | put_user(probe_offset, &uattr->task_fd_query.probe_offset) || | |
2513 | put_user(probe_addr, &uattr->task_fd_query.probe_addr)) | |
2514 | return -EFAULT; | |
2515 | ||
2516 | return err; | |
2517 | } | |
2518 | ||
2519 | #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr | |
2520 | ||
2521 | static int bpf_task_fd_query(const union bpf_attr *attr, | |
2522 | union bpf_attr __user *uattr) | |
2523 | { | |
2524 | pid_t pid = attr->task_fd_query.pid; | |
2525 | u32 fd = attr->task_fd_query.fd; | |
2526 | const struct perf_event *event; | |
2527 | struct files_struct *files; | |
2528 | struct task_struct *task; | |
2529 | struct file *file; | |
2530 | int err; | |
2531 | ||
2532 | if (CHECK_ATTR(BPF_TASK_FD_QUERY)) | |
2533 | return -EINVAL; | |
2534 | ||
2535 | if (!capable(CAP_SYS_ADMIN)) | |
2536 | return -EPERM; | |
2537 | ||
2538 | if (attr->task_fd_query.flags != 0) | |
2539 | return -EINVAL; | |
2540 | ||
2541 | task = get_pid_task(find_vpid(pid), PIDTYPE_PID); | |
2542 | if (!task) | |
2543 | return -ENOENT; | |
2544 | ||
2545 | files = get_files_struct(task); | |
2546 | put_task_struct(task); | |
2547 | if (!files) | |
2548 | return -ENOENT; | |
2549 | ||
2550 | err = 0; | |
2551 | spin_lock(&files->file_lock); | |
2552 | file = fcheck_files(files, fd); | |
2553 | if (!file) | |
2554 | err = -EBADF; | |
2555 | else | |
2556 | get_file(file); | |
2557 | spin_unlock(&files->file_lock); | |
2558 | put_files_struct(files); | |
2559 | ||
2560 | if (err) | |
2561 | goto out; | |
2562 | ||
2563 | if (file->f_op == &bpf_raw_tp_fops) { | |
2564 | struct bpf_raw_tracepoint *raw_tp = file->private_data; | |
2565 | struct bpf_raw_event_map *btp = raw_tp->btp; | |
2566 | ||
2567 | err = bpf_task_fd_query_copy(attr, uattr, | |
2568 | raw_tp->prog->aux->id, | |
2569 | BPF_FD_TYPE_RAW_TRACEPOINT, | |
2570 | btp->tp->name, 0, 0); | |
2571 | goto put_file; | |
2572 | } | |
2573 | ||
2574 | event = perf_get_event(file); | |
2575 | if (!IS_ERR(event)) { | |
2576 | u64 probe_offset, probe_addr; | |
2577 | u32 prog_id, fd_type; | |
2578 | const char *buf; | |
2579 | ||
2580 | err = bpf_get_perf_event_info(event, &prog_id, &fd_type, | |
2581 | &buf, &probe_offset, | |
2582 | &probe_addr); | |
2583 | if (!err) | |
2584 | err = bpf_task_fd_query_copy(attr, uattr, prog_id, | |
2585 | fd_type, buf, | |
2586 | probe_offset, | |
2587 | probe_addr); | |
2588 | goto put_file; | |
2589 | } | |
2590 | ||
2591 | err = -ENOTSUPP; | |
2592 | put_file: | |
2593 | fput(file); | |
2594 | out: | |
2595 | return err; | |
2596 | } | |
2597 | ||
99c55f7d AS |
2598 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
2599 | { | |
2600 | union bpf_attr attr = {}; | |
2601 | int err; | |
2602 | ||
0fa4fe85 | 2603 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
99c55f7d AS |
2604 | return -EPERM; |
2605 | ||
dcab51f1 | 2606 | err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); |
1e270976 MKL |
2607 | if (err) |
2608 | return err; | |
2609 | size = min_t(u32, size, sizeof(attr)); | |
99c55f7d AS |
2610 | |
2611 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ | |
2612 | if (copy_from_user(&attr, uattr, size) != 0) | |
2613 | return -EFAULT; | |
2614 | ||
afdb09c7 CF |
2615 | err = security_bpf(cmd, &attr, size); |
2616 | if (err < 0) | |
2617 | return err; | |
2618 | ||
99c55f7d AS |
2619 | switch (cmd) { |
2620 | case BPF_MAP_CREATE: | |
2621 | err = map_create(&attr); | |
2622 | break; | |
db20fd2b AS |
2623 | case BPF_MAP_LOOKUP_ELEM: |
2624 | err = map_lookup_elem(&attr); | |
2625 | break; | |
2626 | case BPF_MAP_UPDATE_ELEM: | |
2627 | err = map_update_elem(&attr); | |
2628 | break; | |
2629 | case BPF_MAP_DELETE_ELEM: | |
2630 | err = map_delete_elem(&attr); | |
2631 | break; | |
2632 | case BPF_MAP_GET_NEXT_KEY: | |
2633 | err = map_get_next_key(&attr); | |
2634 | break; | |
09756af4 | 2635 | case BPF_PROG_LOAD: |
838e9690 | 2636 | err = bpf_prog_load(&attr, uattr); |
09756af4 | 2637 | break; |
b2197755 DB |
2638 | case BPF_OBJ_PIN: |
2639 | err = bpf_obj_pin(&attr); | |
2640 | break; | |
2641 | case BPF_OBJ_GET: | |
2642 | err = bpf_obj_get(&attr); | |
2643 | break; | |
f4324551 DM |
2644 | case BPF_PROG_ATTACH: |
2645 | err = bpf_prog_attach(&attr); | |
2646 | break; | |
2647 | case BPF_PROG_DETACH: | |
2648 | err = bpf_prog_detach(&attr); | |
2649 | break; | |
468e2f64 AS |
2650 | case BPF_PROG_QUERY: |
2651 | err = bpf_prog_query(&attr, uattr); | |
2652 | break; | |
1cf1cae9 AS |
2653 | case BPF_PROG_TEST_RUN: |
2654 | err = bpf_prog_test_run(&attr, uattr); | |
2655 | break; | |
34ad5580 MKL |
2656 | case BPF_PROG_GET_NEXT_ID: |
2657 | err = bpf_obj_get_next_id(&attr, uattr, | |
2658 | &prog_idr, &prog_idr_lock); | |
2659 | break; | |
2660 | case BPF_MAP_GET_NEXT_ID: | |
2661 | err = bpf_obj_get_next_id(&attr, uattr, | |
2662 | &map_idr, &map_idr_lock); | |
2663 | break; | |
b16d9aa4 MKL |
2664 | case BPF_PROG_GET_FD_BY_ID: |
2665 | err = bpf_prog_get_fd_by_id(&attr); | |
2666 | break; | |
bd5f5f4e MKL |
2667 | case BPF_MAP_GET_FD_BY_ID: |
2668 | err = bpf_map_get_fd_by_id(&attr); | |
2669 | break; | |
1e270976 MKL |
2670 | case BPF_OBJ_GET_INFO_BY_FD: |
2671 | err = bpf_obj_get_info_by_fd(&attr, uattr); | |
2672 | break; | |
c4f6699d AS |
2673 | case BPF_RAW_TRACEPOINT_OPEN: |
2674 | err = bpf_raw_tracepoint_open(&attr); | |
2675 | break; | |
f56a653c MKL |
2676 | case BPF_BTF_LOAD: |
2677 | err = bpf_btf_load(&attr); | |
2678 | break; | |
78958fca MKL |
2679 | case BPF_BTF_GET_FD_BY_ID: |
2680 | err = bpf_btf_get_fd_by_id(&attr); | |
2681 | break; | |
41bdc4b4 YS |
2682 | case BPF_TASK_FD_QUERY: |
2683 | err = bpf_task_fd_query(&attr, uattr); | |
2684 | break; | |
bd513cd0 MV |
2685 | case BPF_MAP_LOOKUP_AND_DELETE_ELEM: |
2686 | err = map_lookup_and_delete_elem(&attr); | |
2687 | break; | |
99c55f7d AS |
2688 | default: |
2689 | err = -EINVAL; | |
2690 | break; | |
2691 | } | |
2692 | ||
2693 | return err; | |
2694 | } |