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> |
f56a653c | 14 | #include <linux/btf.h> |
99c55f7d AS |
15 | #include <linux/syscalls.h> |
16 | #include <linux/slab.h> | |
3f07c014 | 17 | #include <linux/sched/signal.h> |
d407bd25 DB |
18 | #include <linux/vmalloc.h> |
19 | #include <linux/mmzone.h> | |
99c55f7d | 20 | #include <linux/anon_inodes.h> |
db20fd2b | 21 | #include <linux/file.h> |
09756af4 AS |
22 | #include <linux/license.h> |
23 | #include <linux/filter.h> | |
2541517c | 24 | #include <linux/version.h> |
535e7b4b | 25 | #include <linux/kernel.h> |
dc4bb0e2 | 26 | #include <linux/idr.h> |
cb4d2b3f MKL |
27 | #include <linux/cred.h> |
28 | #include <linux/timekeeping.h> | |
29 | #include <linux/ctype.h> | |
99c55f7d | 30 | |
14dc6f04 MKL |
31 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
32 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ | |
33 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ | |
34 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) | |
35 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) | |
36 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) | |
37 | ||
6e71b04a CF |
38 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
39 | ||
b121d1e7 | 40 | DEFINE_PER_CPU(int, bpf_prog_active); |
dc4bb0e2 MKL |
41 | static DEFINE_IDR(prog_idr); |
42 | static DEFINE_SPINLOCK(prog_idr_lock); | |
f3f1c054 MKL |
43 | static DEFINE_IDR(map_idr); |
44 | static DEFINE_SPINLOCK(map_idr_lock); | |
b121d1e7 | 45 | |
1be7f75d AS |
46 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
47 | ||
40077e0c JB |
48 | static const struct bpf_map_ops * const bpf_map_types[] = { |
49 | #define BPF_PROG_TYPE(_id, _ops) | |
50 | #define BPF_MAP_TYPE(_id, _ops) \ | |
51 | [_id] = &_ops, | |
52 | #include <linux/bpf_types.h> | |
53 | #undef BPF_PROG_TYPE | |
54 | #undef BPF_MAP_TYPE | |
55 | }; | |
99c55f7d | 56 | |
752ba56f MS |
57 | /* |
58 | * If we're handed a bigger struct than we know of, ensure all the unknown bits | |
59 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions | |
60 | * we don't know about yet. | |
61 | * | |
62 | * There is a ToCToU between this function call and the following | |
63 | * copy_from_user() call. However, this is not a concern since this function is | |
64 | * meant to be a future-proofing of bits. | |
65 | */ | |
58291a74 MS |
66 | static int check_uarg_tail_zero(void __user *uaddr, |
67 | size_t expected_size, | |
68 | size_t actual_size) | |
69 | { | |
70 | unsigned char __user *addr; | |
71 | unsigned char __user *end; | |
72 | unsigned char val; | |
73 | int err; | |
74 | ||
752ba56f MS |
75 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
76 | return -E2BIG; | |
77 | ||
78 | if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size))) | |
79 | return -EFAULT; | |
80 | ||
58291a74 MS |
81 | if (actual_size <= expected_size) |
82 | return 0; | |
83 | ||
84 | addr = uaddr + expected_size; | |
85 | end = uaddr + actual_size; | |
86 | ||
87 | for (; addr < end; addr++) { | |
88 | err = get_user(val, addr); | |
89 | if (err) | |
90 | return err; | |
91 | if (val) | |
92 | return -E2BIG; | |
93 | } | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
a3884572 JK |
98 | const struct bpf_map_ops bpf_map_offload_ops = { |
99 | .map_alloc = bpf_map_offload_map_alloc, | |
100 | .map_free = bpf_map_offload_map_free, | |
101 | }; | |
102 | ||
99c55f7d AS |
103 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
104 | { | |
1110f3a9 | 105 | const struct bpf_map_ops *ops; |
99c55f7d | 106 | struct bpf_map *map; |
1110f3a9 | 107 | int err; |
99c55f7d | 108 | |
1110f3a9 JK |
109 | if (attr->map_type >= ARRAY_SIZE(bpf_map_types)) |
110 | return ERR_PTR(-EINVAL); | |
111 | ops = bpf_map_types[attr->map_type]; | |
112 | if (!ops) | |
40077e0c | 113 | return ERR_PTR(-EINVAL); |
99c55f7d | 114 | |
1110f3a9 JK |
115 | if (ops->map_alloc_check) { |
116 | err = ops->map_alloc_check(attr); | |
117 | if (err) | |
118 | return ERR_PTR(err); | |
119 | } | |
a3884572 JK |
120 | if (attr->map_ifindex) |
121 | ops = &bpf_map_offload_ops; | |
1110f3a9 | 122 | map = ops->map_alloc(attr); |
40077e0c JB |
123 | if (IS_ERR(map)) |
124 | return map; | |
1110f3a9 | 125 | map->ops = ops; |
40077e0c JB |
126 | map->map_type = attr->map_type; |
127 | return map; | |
99c55f7d AS |
128 | } |
129 | ||
96eabe7a | 130 | void *bpf_map_area_alloc(size_t size, int numa_node) |
d407bd25 DB |
131 | { |
132 | /* We definitely need __GFP_NORETRY, so OOM killer doesn't | |
133 | * trigger under memory pressure as we really just want to | |
134 | * fail instead. | |
135 | */ | |
136 | const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; | |
137 | void *area; | |
138 | ||
139 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { | |
96eabe7a | 140 | area = kmalloc_node(size, GFP_USER | flags, numa_node); |
d407bd25 DB |
141 | if (area != NULL) |
142 | return area; | |
143 | } | |
144 | ||
96eabe7a MKL |
145 | return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, |
146 | __builtin_return_address(0)); | |
d407bd25 DB |
147 | } |
148 | ||
149 | void bpf_map_area_free(void *area) | |
150 | { | |
151 | kvfree(area); | |
152 | } | |
153 | ||
bd475643 JK |
154 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
155 | { | |
156 | map->map_type = attr->map_type; | |
157 | map->key_size = attr->key_size; | |
158 | map->value_size = attr->value_size; | |
159 | map->max_entries = attr->max_entries; | |
160 | map->map_flags = attr->map_flags; | |
161 | map->numa_node = bpf_map_attr_numa_node(attr); | |
162 | } | |
163 | ||
6c905981 AS |
164 | int bpf_map_precharge_memlock(u32 pages) |
165 | { | |
166 | struct user_struct *user = get_current_user(); | |
167 | unsigned long memlock_limit, cur; | |
168 | ||
169 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
170 | cur = atomic_long_read(&user->locked_vm); | |
171 | free_uid(user); | |
172 | if (cur + pages > memlock_limit) | |
173 | return -EPERM; | |
174 | return 0; | |
175 | } | |
176 | ||
aaac3ba9 AS |
177 | static int bpf_map_charge_memlock(struct bpf_map *map) |
178 | { | |
179 | struct user_struct *user = get_current_user(); | |
180 | unsigned long memlock_limit; | |
181 | ||
182 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
183 | ||
184 | atomic_long_add(map->pages, &user->locked_vm); | |
185 | ||
186 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { | |
187 | atomic_long_sub(map->pages, &user->locked_vm); | |
188 | free_uid(user); | |
189 | return -EPERM; | |
190 | } | |
191 | map->user = user; | |
192 | return 0; | |
193 | } | |
194 | ||
195 | static void bpf_map_uncharge_memlock(struct bpf_map *map) | |
196 | { | |
197 | struct user_struct *user = map->user; | |
198 | ||
199 | atomic_long_sub(map->pages, &user->locked_vm); | |
200 | free_uid(user); | |
201 | } | |
202 | ||
f3f1c054 MKL |
203 | static int bpf_map_alloc_id(struct bpf_map *map) |
204 | { | |
205 | int id; | |
206 | ||
b76354cd | 207 | idr_preload(GFP_KERNEL); |
f3f1c054 MKL |
208 | spin_lock_bh(&map_idr_lock); |
209 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); | |
210 | if (id > 0) | |
211 | map->id = id; | |
212 | spin_unlock_bh(&map_idr_lock); | |
b76354cd | 213 | idr_preload_end(); |
f3f1c054 MKL |
214 | |
215 | if (WARN_ON_ONCE(!id)) | |
216 | return -ENOSPC; | |
217 | ||
218 | return id > 0 ? 0 : id; | |
219 | } | |
220 | ||
a3884572 | 221 | void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) |
f3f1c054 | 222 | { |
930651a7 ED |
223 | unsigned long flags; |
224 | ||
a3884572 JK |
225 | /* Offloaded maps are removed from the IDR store when their device |
226 | * disappears - even if someone holds an fd to them they are unusable, | |
227 | * the memory is gone, all ops will fail; they are simply waiting for | |
228 | * refcnt to drop to be freed. | |
229 | */ | |
230 | if (!map->id) | |
231 | return; | |
232 | ||
bd5f5f4e | 233 | if (do_idr_lock) |
930651a7 | 234 | spin_lock_irqsave(&map_idr_lock, flags); |
bd5f5f4e MKL |
235 | else |
236 | __acquire(&map_idr_lock); | |
237 | ||
f3f1c054 | 238 | idr_remove(&map_idr, map->id); |
a3884572 | 239 | map->id = 0; |
bd5f5f4e MKL |
240 | |
241 | if (do_idr_lock) | |
930651a7 | 242 | spin_unlock_irqrestore(&map_idr_lock, flags); |
bd5f5f4e MKL |
243 | else |
244 | __release(&map_idr_lock); | |
f3f1c054 MKL |
245 | } |
246 | ||
99c55f7d AS |
247 | /* called from workqueue */ |
248 | static void bpf_map_free_deferred(struct work_struct *work) | |
249 | { | |
250 | struct bpf_map *map = container_of(work, struct bpf_map, work); | |
251 | ||
aaac3ba9 | 252 | bpf_map_uncharge_memlock(map); |
afdb09c7 | 253 | security_bpf_map_free(map); |
99c55f7d AS |
254 | /* implementation dependent freeing */ |
255 | map->ops->map_free(map); | |
256 | } | |
257 | ||
c9da161c DB |
258 | static void bpf_map_put_uref(struct bpf_map *map) |
259 | { | |
260 | if (atomic_dec_and_test(&map->usercnt)) { | |
261 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) | |
262 | bpf_fd_array_map_clear(map); | |
263 | } | |
264 | } | |
265 | ||
99c55f7d AS |
266 | /* decrement map refcnt and schedule it for freeing via workqueue |
267 | * (unrelying map implementation ops->map_free() might sleep) | |
268 | */ | |
bd5f5f4e | 269 | static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) |
99c55f7d AS |
270 | { |
271 | if (atomic_dec_and_test(&map->refcnt)) { | |
34ad5580 | 272 | /* bpf_map_free_id() must be called first */ |
bd5f5f4e | 273 | bpf_map_free_id(map, do_idr_lock); |
99c55f7d AS |
274 | INIT_WORK(&map->work, bpf_map_free_deferred); |
275 | schedule_work(&map->work); | |
276 | } | |
277 | } | |
278 | ||
bd5f5f4e MKL |
279 | void bpf_map_put(struct bpf_map *map) |
280 | { | |
281 | __bpf_map_put(map, true); | |
282 | } | |
283 | ||
c9da161c | 284 | void bpf_map_put_with_uref(struct bpf_map *map) |
99c55f7d | 285 | { |
c9da161c | 286 | bpf_map_put_uref(map); |
99c55f7d | 287 | bpf_map_put(map); |
c9da161c DB |
288 | } |
289 | ||
290 | static int bpf_map_release(struct inode *inode, struct file *filp) | |
291 | { | |
61d1b6a4 DB |
292 | struct bpf_map *map = filp->private_data; |
293 | ||
294 | if (map->ops->map_release) | |
295 | map->ops->map_release(map, filp); | |
296 | ||
297 | bpf_map_put_with_uref(map); | |
99c55f7d AS |
298 | return 0; |
299 | } | |
300 | ||
f99bf205 DB |
301 | #ifdef CONFIG_PROC_FS |
302 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) | |
303 | { | |
304 | const struct bpf_map *map = filp->private_data; | |
21116b70 DB |
305 | const struct bpf_array *array; |
306 | u32 owner_prog_type = 0; | |
9780c0ab | 307 | u32 owner_jited = 0; |
21116b70 DB |
308 | |
309 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { | |
310 | array = container_of(map, struct bpf_array, map); | |
311 | owner_prog_type = array->owner_prog_type; | |
9780c0ab | 312 | owner_jited = array->owner_jited; |
21116b70 | 313 | } |
f99bf205 DB |
314 | |
315 | seq_printf(m, | |
316 | "map_type:\t%u\n" | |
317 | "key_size:\t%u\n" | |
318 | "value_size:\t%u\n" | |
322cea2f | 319 | "max_entries:\t%u\n" |
21116b70 DB |
320 | "map_flags:\t%#x\n" |
321 | "memlock:\t%llu\n", | |
f99bf205 DB |
322 | map->map_type, |
323 | map->key_size, | |
324 | map->value_size, | |
322cea2f | 325 | map->max_entries, |
21116b70 DB |
326 | map->map_flags, |
327 | map->pages * 1ULL << PAGE_SHIFT); | |
328 | ||
9780c0ab | 329 | if (owner_prog_type) { |
21116b70 DB |
330 | seq_printf(m, "owner_prog_type:\t%u\n", |
331 | owner_prog_type); | |
9780c0ab DB |
332 | seq_printf(m, "owner_jited:\t%u\n", |
333 | owner_jited); | |
334 | } | |
f99bf205 DB |
335 | } |
336 | #endif | |
337 | ||
6e71b04a CF |
338 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
339 | loff_t *ppos) | |
340 | { | |
341 | /* We need this handler such that alloc_file() enables | |
342 | * f_mode with FMODE_CAN_READ. | |
343 | */ | |
344 | return -EINVAL; | |
345 | } | |
346 | ||
347 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, | |
348 | size_t siz, loff_t *ppos) | |
349 | { | |
350 | /* We need this handler such that alloc_file() enables | |
351 | * f_mode with FMODE_CAN_WRITE. | |
352 | */ | |
353 | return -EINVAL; | |
354 | } | |
355 | ||
f66e448c | 356 | const struct file_operations bpf_map_fops = { |
f99bf205 DB |
357 | #ifdef CONFIG_PROC_FS |
358 | .show_fdinfo = bpf_map_show_fdinfo, | |
359 | #endif | |
360 | .release = bpf_map_release, | |
6e71b04a CF |
361 | .read = bpf_dummy_read, |
362 | .write = bpf_dummy_write, | |
99c55f7d AS |
363 | }; |
364 | ||
6e71b04a | 365 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
aa79781b | 366 | { |
afdb09c7 CF |
367 | int ret; |
368 | ||
369 | ret = security_bpf_map(map, OPEN_FMODE(flags)); | |
370 | if (ret < 0) | |
371 | return ret; | |
372 | ||
aa79781b | 373 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
6e71b04a CF |
374 | flags | O_CLOEXEC); |
375 | } | |
376 | ||
377 | int bpf_get_file_flag(int flags) | |
378 | { | |
379 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) | |
380 | return -EINVAL; | |
381 | if (flags & BPF_F_RDONLY) | |
382 | return O_RDONLY; | |
383 | if (flags & BPF_F_WRONLY) | |
384 | return O_WRONLY; | |
385 | return O_RDWR; | |
aa79781b DB |
386 | } |
387 | ||
99c55f7d AS |
388 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
389 | #define CHECK_ATTR(CMD) \ | |
390 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ | |
391 | sizeof(attr->CMD##_LAST_FIELD), 0, \ | |
392 | sizeof(*attr) - \ | |
393 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ | |
394 | sizeof(attr->CMD##_LAST_FIELD)) != NULL | |
395 | ||
cb4d2b3f MKL |
396 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
397 | * Return 0 on success and < 0 on error. | |
398 | */ | |
399 | static int bpf_obj_name_cpy(char *dst, const char *src) | |
400 | { | |
401 | const char *end = src + BPF_OBJ_NAME_LEN; | |
402 | ||
473d9734 MKL |
403 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
404 | ||
cb4d2b3f MKL |
405 | /* Copy all isalnum() and '_' char */ |
406 | while (src < end && *src) { | |
407 | if (!isalnum(*src) && *src != '_') | |
408 | return -EINVAL; | |
409 | *dst++ = *src++; | |
410 | } | |
411 | ||
412 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ | |
413 | if (src == end) | |
414 | return -EINVAL; | |
415 | ||
cb4d2b3f MKL |
416 | return 0; |
417 | } | |
418 | ||
a3884572 | 419 | #define BPF_MAP_CREATE_LAST_FIELD map_ifindex |
99c55f7d AS |
420 | /* called via syscall */ |
421 | static int map_create(union bpf_attr *attr) | |
422 | { | |
96eabe7a | 423 | int numa_node = bpf_map_attr_numa_node(attr); |
99c55f7d | 424 | struct bpf_map *map; |
6e71b04a | 425 | int f_flags; |
99c55f7d AS |
426 | int err; |
427 | ||
428 | err = CHECK_ATTR(BPF_MAP_CREATE); | |
429 | if (err) | |
430 | return -EINVAL; | |
431 | ||
6e71b04a CF |
432 | f_flags = bpf_get_file_flag(attr->map_flags); |
433 | if (f_flags < 0) | |
434 | return f_flags; | |
435 | ||
96eabe7a | 436 | if (numa_node != NUMA_NO_NODE && |
96e5ae4e ED |
437 | ((unsigned int)numa_node >= nr_node_ids || |
438 | !node_online(numa_node))) | |
96eabe7a MKL |
439 | return -EINVAL; |
440 | ||
99c55f7d AS |
441 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
442 | map = find_and_alloc_map(attr); | |
443 | if (IS_ERR(map)) | |
444 | return PTR_ERR(map); | |
445 | ||
ad5b177b MKL |
446 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
447 | if (err) | |
448 | goto free_map_nouncharge; | |
449 | ||
99c55f7d | 450 | atomic_set(&map->refcnt, 1); |
c9da161c | 451 | atomic_set(&map->usercnt, 1); |
99c55f7d | 452 | |
afdb09c7 | 453 | err = security_bpf_map_alloc(map); |
aaac3ba9 | 454 | if (err) |
20b2b24f | 455 | goto free_map_nouncharge; |
aaac3ba9 | 456 | |
afdb09c7 CF |
457 | err = bpf_map_charge_memlock(map); |
458 | if (err) | |
459 | goto free_map_sec; | |
460 | ||
f3f1c054 MKL |
461 | err = bpf_map_alloc_id(map); |
462 | if (err) | |
463 | goto free_map; | |
464 | ||
6e71b04a | 465 | err = bpf_map_new_fd(map, f_flags); |
bd5f5f4e MKL |
466 | if (err < 0) { |
467 | /* failed to allocate fd. | |
468 | * bpf_map_put() is needed because the above | |
469 | * bpf_map_alloc_id() has published the map | |
470 | * to the userspace and the userspace may | |
471 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. | |
472 | */ | |
473 | bpf_map_put(map); | |
474 | return err; | |
475 | } | |
99c55f7d | 476 | |
a67edbf4 | 477 | trace_bpf_map_create(map, err); |
99c55f7d AS |
478 | return err; |
479 | ||
480 | free_map: | |
20b2b24f | 481 | bpf_map_uncharge_memlock(map); |
afdb09c7 CF |
482 | free_map_sec: |
483 | security_bpf_map_free(map); | |
20b2b24f | 484 | free_map_nouncharge: |
99c55f7d AS |
485 | map->ops->map_free(map); |
486 | return err; | |
487 | } | |
488 | ||
db20fd2b AS |
489 | /* if error is returned, fd is released. |
490 | * On success caller should complete fd access with matching fdput() | |
491 | */ | |
c2101297 | 492 | struct bpf_map *__bpf_map_get(struct fd f) |
db20fd2b | 493 | { |
db20fd2b AS |
494 | if (!f.file) |
495 | return ERR_PTR(-EBADF); | |
db20fd2b AS |
496 | if (f.file->f_op != &bpf_map_fops) { |
497 | fdput(f); | |
498 | return ERR_PTR(-EINVAL); | |
499 | } | |
500 | ||
c2101297 DB |
501 | return f.file->private_data; |
502 | } | |
503 | ||
92117d84 AS |
504 | /* prog's and map's refcnt limit */ |
505 | #define BPF_MAX_REFCNT 32768 | |
506 | ||
507 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) | |
c9da161c | 508 | { |
92117d84 AS |
509 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
510 | atomic_dec(&map->refcnt); | |
511 | return ERR_PTR(-EBUSY); | |
512 | } | |
c9da161c DB |
513 | if (uref) |
514 | atomic_inc(&map->usercnt); | |
92117d84 | 515 | return map; |
c9da161c DB |
516 | } |
517 | ||
518 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) | |
c2101297 DB |
519 | { |
520 | struct fd f = fdget(ufd); | |
521 | struct bpf_map *map; | |
522 | ||
523 | map = __bpf_map_get(f); | |
524 | if (IS_ERR(map)) | |
525 | return map; | |
526 | ||
92117d84 | 527 | map = bpf_map_inc(map, true); |
c2101297 | 528 | fdput(f); |
db20fd2b AS |
529 | |
530 | return map; | |
531 | } | |
532 | ||
bd5f5f4e MKL |
533 | /* map_idr_lock should have been held */ |
534 | static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, | |
535 | bool uref) | |
536 | { | |
537 | int refold; | |
538 | ||
539 | refold = __atomic_add_unless(&map->refcnt, 1, 0); | |
540 | ||
541 | if (refold >= BPF_MAX_REFCNT) { | |
542 | __bpf_map_put(map, false); | |
543 | return ERR_PTR(-EBUSY); | |
544 | } | |
545 | ||
546 | if (!refold) | |
547 | return ERR_PTR(-ENOENT); | |
548 | ||
549 | if (uref) | |
550 | atomic_inc(&map->usercnt); | |
551 | ||
552 | return map; | |
553 | } | |
554 | ||
b8cdc051 AS |
555 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
556 | { | |
557 | return -ENOTSUPP; | |
558 | } | |
559 | ||
db20fd2b AS |
560 | /* last field in 'union bpf_attr' used by this command */ |
561 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value | |
562 | ||
563 | static int map_lookup_elem(union bpf_attr *attr) | |
564 | { | |
535e7b4b MS |
565 | void __user *ukey = u64_to_user_ptr(attr->key); |
566 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 567 | int ufd = attr->map_fd; |
db20fd2b | 568 | struct bpf_map *map; |
8ebe667c | 569 | void *key, *value, *ptr; |
15a07b33 | 570 | u32 value_size; |
592867bf | 571 | struct fd f; |
db20fd2b AS |
572 | int err; |
573 | ||
574 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) | |
575 | return -EINVAL; | |
576 | ||
592867bf | 577 | f = fdget(ufd); |
c2101297 | 578 | map = __bpf_map_get(f); |
db20fd2b AS |
579 | if (IS_ERR(map)) |
580 | return PTR_ERR(map); | |
581 | ||
6e71b04a CF |
582 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
583 | err = -EPERM; | |
584 | goto err_put; | |
585 | } | |
586 | ||
e4448ed8 AV |
587 | key = memdup_user(ukey, map->key_size); |
588 | if (IS_ERR(key)) { | |
589 | err = PTR_ERR(key); | |
db20fd2b | 590 | goto err_put; |
e4448ed8 | 591 | } |
db20fd2b | 592 | |
15a07b33 | 593 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 594 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
15a07b33 AS |
595 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
596 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); | |
14dc6f04 MKL |
597 | else if (IS_FD_MAP(map)) |
598 | value_size = sizeof(u32); | |
15a07b33 AS |
599 | else |
600 | value_size = map->value_size; | |
601 | ||
8ebe667c | 602 | err = -ENOMEM; |
15a07b33 | 603 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b | 604 | if (!value) |
8ebe667c AS |
605 | goto free_key; |
606 | ||
a3884572 JK |
607 | if (bpf_map_is_dev_bound(map)) { |
608 | err = bpf_map_offload_lookup_elem(map, key, value); | |
609 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || | |
610 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
611 | err = bpf_percpu_hash_copy(map, key, value); |
612 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
613 | err = bpf_percpu_array_copy(map, key, value); | |
557c0c6e AS |
614 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
615 | err = bpf_stackmap_copy(map, key, value); | |
14dc6f04 MKL |
616 | } else if (IS_FD_ARRAY(map)) { |
617 | err = bpf_fd_array_map_lookup_elem(map, key, value); | |
618 | } else if (IS_FD_HASH(map)) { | |
619 | err = bpf_fd_htab_map_lookup_elem(map, key, value); | |
15a07b33 AS |
620 | } else { |
621 | rcu_read_lock(); | |
622 | ptr = map->ops->map_lookup_elem(map, key); | |
623 | if (ptr) | |
624 | memcpy(value, ptr, value_size); | |
625 | rcu_read_unlock(); | |
626 | err = ptr ? 0 : -ENOENT; | |
627 | } | |
8ebe667c | 628 | |
15a07b33 | 629 | if (err) |
8ebe667c | 630 | goto free_value; |
db20fd2b AS |
631 | |
632 | err = -EFAULT; | |
15a07b33 | 633 | if (copy_to_user(uvalue, value, value_size) != 0) |
8ebe667c | 634 | goto free_value; |
db20fd2b | 635 | |
a67edbf4 | 636 | trace_bpf_map_lookup_elem(map, ufd, key, value); |
db20fd2b AS |
637 | err = 0; |
638 | ||
8ebe667c AS |
639 | free_value: |
640 | kfree(value); | |
db20fd2b AS |
641 | free_key: |
642 | kfree(key); | |
643 | err_put: | |
644 | fdput(f); | |
645 | return err; | |
646 | } | |
647 | ||
3274f520 | 648 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
db20fd2b AS |
649 | |
650 | static int map_update_elem(union bpf_attr *attr) | |
651 | { | |
535e7b4b MS |
652 | void __user *ukey = u64_to_user_ptr(attr->key); |
653 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 654 | int ufd = attr->map_fd; |
db20fd2b AS |
655 | struct bpf_map *map; |
656 | void *key, *value; | |
15a07b33 | 657 | u32 value_size; |
592867bf | 658 | struct fd f; |
db20fd2b AS |
659 | int err; |
660 | ||
661 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) | |
662 | return -EINVAL; | |
663 | ||
592867bf | 664 | f = fdget(ufd); |
c2101297 | 665 | map = __bpf_map_get(f); |
db20fd2b AS |
666 | if (IS_ERR(map)) |
667 | return PTR_ERR(map); | |
668 | ||
6e71b04a CF |
669 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
670 | err = -EPERM; | |
671 | goto err_put; | |
672 | } | |
673 | ||
e4448ed8 AV |
674 | key = memdup_user(ukey, map->key_size); |
675 | if (IS_ERR(key)) { | |
676 | err = PTR_ERR(key); | |
db20fd2b | 677 | goto err_put; |
e4448ed8 | 678 | } |
db20fd2b | 679 | |
15a07b33 | 680 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 681 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
15a07b33 AS |
682 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
683 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); | |
684 | else | |
685 | value_size = map->value_size; | |
686 | ||
db20fd2b | 687 | err = -ENOMEM; |
15a07b33 | 688 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b AS |
689 | if (!value) |
690 | goto free_key; | |
691 | ||
692 | err = -EFAULT; | |
15a07b33 | 693 | if (copy_from_user(value, uvalue, value_size) != 0) |
db20fd2b AS |
694 | goto free_value; |
695 | ||
6710e112 | 696 | /* Need to create a kthread, thus must support schedule */ |
a3884572 JK |
697 | if (bpf_map_is_dev_bound(map)) { |
698 | err = bpf_map_offload_update_elem(map, key, value, attr->flags); | |
699 | goto out; | |
700 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) { | |
6710e112 JDB |
701 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
702 | goto out; | |
703 | } | |
704 | ||
b121d1e7 AS |
705 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
706 | * inside bpf map update or delete otherwise deadlocks are possible | |
707 | */ | |
708 | preempt_disable(); | |
709 | __this_cpu_inc(bpf_prog_active); | |
8f844938 MKL |
710 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
711 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
712 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
713 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
714 | err = bpf_percpu_array_update(map, key, value, attr->flags); | |
9c147b56 | 715 | } else if (IS_FD_ARRAY(map)) { |
d056a788 DB |
716 | rcu_read_lock(); |
717 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, | |
718 | attr->flags); | |
719 | rcu_read_unlock(); | |
bcc6b1b7 MKL |
720 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
721 | rcu_read_lock(); | |
722 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, | |
723 | attr->flags); | |
724 | rcu_read_unlock(); | |
15a07b33 AS |
725 | } else { |
726 | rcu_read_lock(); | |
727 | err = map->ops->map_update_elem(map, key, value, attr->flags); | |
728 | rcu_read_unlock(); | |
729 | } | |
b121d1e7 AS |
730 | __this_cpu_dec(bpf_prog_active); |
731 | preempt_enable(); | |
6710e112 | 732 | out: |
a67edbf4 DB |
733 | if (!err) |
734 | trace_bpf_map_update_elem(map, ufd, key, value); | |
db20fd2b AS |
735 | free_value: |
736 | kfree(value); | |
737 | free_key: | |
738 | kfree(key); | |
739 | err_put: | |
740 | fdput(f); | |
741 | return err; | |
742 | } | |
743 | ||
744 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key | |
745 | ||
746 | static int map_delete_elem(union bpf_attr *attr) | |
747 | { | |
535e7b4b | 748 | void __user *ukey = u64_to_user_ptr(attr->key); |
db20fd2b | 749 | int ufd = attr->map_fd; |
db20fd2b | 750 | struct bpf_map *map; |
592867bf | 751 | struct fd f; |
db20fd2b AS |
752 | void *key; |
753 | int err; | |
754 | ||
755 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) | |
756 | return -EINVAL; | |
757 | ||
592867bf | 758 | f = fdget(ufd); |
c2101297 | 759 | map = __bpf_map_get(f); |
db20fd2b AS |
760 | if (IS_ERR(map)) |
761 | return PTR_ERR(map); | |
762 | ||
6e71b04a CF |
763 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
764 | err = -EPERM; | |
765 | goto err_put; | |
766 | } | |
767 | ||
e4448ed8 AV |
768 | key = memdup_user(ukey, map->key_size); |
769 | if (IS_ERR(key)) { | |
770 | err = PTR_ERR(key); | |
db20fd2b | 771 | goto err_put; |
e4448ed8 | 772 | } |
db20fd2b | 773 | |
a3884572 JK |
774 | if (bpf_map_is_dev_bound(map)) { |
775 | err = bpf_map_offload_delete_elem(map, key); | |
776 | goto out; | |
777 | } | |
778 | ||
b121d1e7 AS |
779 | preempt_disable(); |
780 | __this_cpu_inc(bpf_prog_active); | |
db20fd2b AS |
781 | rcu_read_lock(); |
782 | err = map->ops->map_delete_elem(map, key); | |
783 | rcu_read_unlock(); | |
b121d1e7 AS |
784 | __this_cpu_dec(bpf_prog_active); |
785 | preempt_enable(); | |
a3884572 | 786 | out: |
a67edbf4 DB |
787 | if (!err) |
788 | trace_bpf_map_delete_elem(map, ufd, key); | |
db20fd2b AS |
789 | kfree(key); |
790 | err_put: | |
791 | fdput(f); | |
792 | return err; | |
793 | } | |
794 | ||
795 | /* last field in 'union bpf_attr' used by this command */ | |
796 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key | |
797 | ||
798 | static int map_get_next_key(union bpf_attr *attr) | |
799 | { | |
535e7b4b MS |
800 | void __user *ukey = u64_to_user_ptr(attr->key); |
801 | void __user *unext_key = u64_to_user_ptr(attr->next_key); | |
db20fd2b | 802 | int ufd = attr->map_fd; |
db20fd2b AS |
803 | struct bpf_map *map; |
804 | void *key, *next_key; | |
592867bf | 805 | struct fd f; |
db20fd2b AS |
806 | int err; |
807 | ||
808 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) | |
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_READ)) { |
817 | err = -EPERM; | |
818 | goto err_put; | |
819 | } | |
820 | ||
8fe45924 | 821 | if (ukey) { |
e4448ed8 AV |
822 | key = memdup_user(ukey, map->key_size); |
823 | if (IS_ERR(key)) { | |
824 | err = PTR_ERR(key); | |
8fe45924 | 825 | goto err_put; |
e4448ed8 | 826 | } |
8fe45924 TQ |
827 | } else { |
828 | key = NULL; | |
829 | } | |
db20fd2b AS |
830 | |
831 | err = -ENOMEM; | |
832 | next_key = kmalloc(map->key_size, GFP_USER); | |
833 | if (!next_key) | |
834 | goto free_key; | |
835 | ||
a3884572 JK |
836 | if (bpf_map_is_dev_bound(map)) { |
837 | err = bpf_map_offload_get_next_key(map, key, next_key); | |
838 | goto out; | |
839 | } | |
840 | ||
db20fd2b AS |
841 | rcu_read_lock(); |
842 | err = map->ops->map_get_next_key(map, key, next_key); | |
843 | rcu_read_unlock(); | |
a3884572 | 844 | out: |
db20fd2b AS |
845 | if (err) |
846 | goto free_next_key; | |
847 | ||
848 | err = -EFAULT; | |
849 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) | |
850 | goto free_next_key; | |
851 | ||
a67edbf4 | 852 | trace_bpf_map_next_key(map, ufd, key, next_key); |
db20fd2b AS |
853 | err = 0; |
854 | ||
855 | free_next_key: | |
856 | kfree(next_key); | |
857 | free_key: | |
858 | kfree(key); | |
859 | err_put: | |
860 | fdput(f); | |
861 | return err; | |
862 | } | |
863 | ||
7de16e3a JK |
864 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
865 | #define BPF_PROG_TYPE(_id, _name) \ | |
866 | [_id] = & _name ## _prog_ops, | |
867 | #define BPF_MAP_TYPE(_id, _ops) | |
868 | #include <linux/bpf_types.h> | |
869 | #undef BPF_PROG_TYPE | |
870 | #undef BPF_MAP_TYPE | |
871 | }; | |
872 | ||
09756af4 AS |
873 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
874 | { | |
be9370a7 JB |
875 | if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type]) |
876 | return -EINVAL; | |
09756af4 | 877 | |
ab3f0063 JK |
878 | if (!bpf_prog_is_dev_bound(prog->aux)) |
879 | prog->aux->ops = bpf_prog_types[type]; | |
880 | else | |
881 | prog->aux->ops = &bpf_offload_prog_ops; | |
be9370a7 JB |
882 | prog->type = type; |
883 | return 0; | |
09756af4 AS |
884 | } |
885 | ||
886 | /* drop refcnt on maps used by eBPF program and free auxilary data */ | |
887 | static void free_used_maps(struct bpf_prog_aux *aux) | |
888 | { | |
889 | int i; | |
890 | ||
891 | for (i = 0; i < aux->used_map_cnt; i++) | |
892 | bpf_map_put(aux->used_maps[i]); | |
893 | ||
894 | kfree(aux->used_maps); | |
895 | } | |
896 | ||
5ccb071e DB |
897 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
898 | { | |
899 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
900 | unsigned long user_bufs; | |
901 | ||
902 | if (user) { | |
903 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); | |
904 | if (user_bufs > memlock_limit) { | |
905 | atomic_long_sub(pages, &user->locked_vm); | |
906 | return -EPERM; | |
907 | } | |
908 | } | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) | |
914 | { | |
915 | if (user) | |
916 | atomic_long_sub(pages, &user->locked_vm); | |
917 | } | |
918 | ||
aaac3ba9 AS |
919 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
920 | { | |
921 | struct user_struct *user = get_current_user(); | |
5ccb071e | 922 | int ret; |
aaac3ba9 | 923 | |
5ccb071e DB |
924 | ret = __bpf_prog_charge(user, prog->pages); |
925 | if (ret) { | |
aaac3ba9 | 926 | free_uid(user); |
5ccb071e | 927 | return ret; |
aaac3ba9 | 928 | } |
5ccb071e | 929 | |
aaac3ba9 AS |
930 | prog->aux->user = user; |
931 | return 0; | |
932 | } | |
933 | ||
934 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) | |
935 | { | |
936 | struct user_struct *user = prog->aux->user; | |
937 | ||
5ccb071e | 938 | __bpf_prog_uncharge(user, prog->pages); |
aaac3ba9 AS |
939 | free_uid(user); |
940 | } | |
941 | ||
dc4bb0e2 MKL |
942 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
943 | { | |
944 | int id; | |
945 | ||
b76354cd | 946 | idr_preload(GFP_KERNEL); |
dc4bb0e2 MKL |
947 | spin_lock_bh(&prog_idr_lock); |
948 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); | |
949 | if (id > 0) | |
950 | prog->aux->id = id; | |
951 | spin_unlock_bh(&prog_idr_lock); | |
b76354cd | 952 | idr_preload_end(); |
dc4bb0e2 MKL |
953 | |
954 | /* id is in [1, INT_MAX) */ | |
955 | if (WARN_ON_ONCE(!id)) | |
956 | return -ENOSPC; | |
957 | ||
958 | return id > 0 ? 0 : id; | |
959 | } | |
960 | ||
ad8ad79f | 961 | void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) |
dc4bb0e2 | 962 | { |
ad8ad79f JK |
963 | /* cBPF to eBPF migrations are currently not in the idr store. |
964 | * Offloaded programs are removed from the store when their device | |
965 | * disappears - even if someone grabs an fd to them they are unusable, | |
966 | * simply waiting for refcnt to drop to be freed. | |
967 | */ | |
dc4bb0e2 MKL |
968 | if (!prog->aux->id) |
969 | return; | |
970 | ||
b16d9aa4 MKL |
971 | if (do_idr_lock) |
972 | spin_lock_bh(&prog_idr_lock); | |
973 | else | |
974 | __acquire(&prog_idr_lock); | |
975 | ||
dc4bb0e2 | 976 | idr_remove(&prog_idr, prog->aux->id); |
ad8ad79f | 977 | prog->aux->id = 0; |
b16d9aa4 MKL |
978 | |
979 | if (do_idr_lock) | |
980 | spin_unlock_bh(&prog_idr_lock); | |
981 | else | |
982 | __release(&prog_idr_lock); | |
dc4bb0e2 MKL |
983 | } |
984 | ||
1aacde3d | 985 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
abf2e7d6 AS |
986 | { |
987 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); | |
988 | ||
989 | free_used_maps(aux); | |
aaac3ba9 | 990 | bpf_prog_uncharge_memlock(aux->prog); |
afdb09c7 | 991 | security_bpf_prog_free(aux); |
abf2e7d6 AS |
992 | bpf_prog_free(aux->prog); |
993 | } | |
994 | ||
b16d9aa4 | 995 | static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
09756af4 | 996 | { |
a67edbf4 | 997 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
4f74d809 DB |
998 | int i; |
999 | ||
a67edbf4 | 1000 | trace_bpf_prog_put_rcu(prog); |
34ad5580 | 1001 | /* bpf_prog_free_id() must be called first */ |
b16d9aa4 | 1002 | bpf_prog_free_id(prog, do_idr_lock); |
4f74d809 DB |
1003 | |
1004 | for (i = 0; i < prog->aux->func_cnt; i++) | |
1005 | bpf_prog_kallsyms_del(prog->aux->func[i]); | |
74451e66 | 1006 | bpf_prog_kallsyms_del(prog); |
4f74d809 | 1007 | |
1aacde3d | 1008 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
a67edbf4 | 1009 | } |
09756af4 | 1010 | } |
b16d9aa4 MKL |
1011 | |
1012 | void bpf_prog_put(struct bpf_prog *prog) | |
1013 | { | |
1014 | __bpf_prog_put(prog, true); | |
1015 | } | |
e2e9b654 | 1016 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
09756af4 AS |
1017 | |
1018 | static int bpf_prog_release(struct inode *inode, struct file *filp) | |
1019 | { | |
1020 | struct bpf_prog *prog = filp->private_data; | |
1021 | ||
1aacde3d | 1022 | bpf_prog_put(prog); |
09756af4 AS |
1023 | return 0; |
1024 | } | |
1025 | ||
7bd509e3 DB |
1026 | #ifdef CONFIG_PROC_FS |
1027 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) | |
1028 | { | |
1029 | const struct bpf_prog *prog = filp->private_data; | |
f1f7714e | 1030 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
7bd509e3 | 1031 | |
f1f7714e | 1032 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
7bd509e3 DB |
1033 | seq_printf(m, |
1034 | "prog_type:\t%u\n" | |
1035 | "prog_jited:\t%u\n" | |
f1f7714e | 1036 | "prog_tag:\t%s\n" |
7bd509e3 DB |
1037 | "memlock:\t%llu\n", |
1038 | prog->type, | |
1039 | prog->jited, | |
f1f7714e | 1040 | prog_tag, |
7bd509e3 DB |
1041 | prog->pages * 1ULL << PAGE_SHIFT); |
1042 | } | |
1043 | #endif | |
1044 | ||
f66e448c | 1045 | const struct file_operations bpf_prog_fops = { |
7bd509e3 DB |
1046 | #ifdef CONFIG_PROC_FS |
1047 | .show_fdinfo = bpf_prog_show_fdinfo, | |
1048 | #endif | |
1049 | .release = bpf_prog_release, | |
6e71b04a CF |
1050 | .read = bpf_dummy_read, |
1051 | .write = bpf_dummy_write, | |
09756af4 AS |
1052 | }; |
1053 | ||
b2197755 | 1054 | int bpf_prog_new_fd(struct bpf_prog *prog) |
aa79781b | 1055 | { |
afdb09c7 CF |
1056 | int ret; |
1057 | ||
1058 | ret = security_bpf_prog(prog); | |
1059 | if (ret < 0) | |
1060 | return ret; | |
1061 | ||
aa79781b DB |
1062 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
1063 | O_RDWR | O_CLOEXEC); | |
1064 | } | |
1065 | ||
113214be | 1066 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
09756af4 | 1067 | { |
09756af4 AS |
1068 | if (!f.file) |
1069 | return ERR_PTR(-EBADF); | |
09756af4 AS |
1070 | if (f.file->f_op != &bpf_prog_fops) { |
1071 | fdput(f); | |
1072 | return ERR_PTR(-EINVAL); | |
1073 | } | |
1074 | ||
c2101297 | 1075 | return f.file->private_data; |
09756af4 AS |
1076 | } |
1077 | ||
59d3656d | 1078 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
92117d84 | 1079 | { |
59d3656d BB |
1080 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
1081 | atomic_sub(i, &prog->aux->refcnt); | |
92117d84 AS |
1082 | return ERR_PTR(-EBUSY); |
1083 | } | |
1084 | return prog; | |
1085 | } | |
59d3656d BB |
1086 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
1087 | ||
c540594f DB |
1088 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
1089 | { | |
1090 | /* Only to be used for undoing previous bpf_prog_add() in some | |
1091 | * error path. We still know that another entity in our call | |
1092 | * path holds a reference to the program, thus atomic_sub() can | |
1093 | * be safely used in such cases! | |
1094 | */ | |
1095 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); | |
1096 | } | |
1097 | EXPORT_SYMBOL_GPL(bpf_prog_sub); | |
1098 | ||
59d3656d BB |
1099 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
1100 | { | |
1101 | return bpf_prog_add(prog, 1); | |
1102 | } | |
97bc402d | 1103 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
92117d84 | 1104 | |
b16d9aa4 | 1105 | /* prog_idr_lock should have been held */ |
a6f6df69 | 1106 | struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) |
b16d9aa4 MKL |
1107 | { |
1108 | int refold; | |
1109 | ||
1110 | refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0); | |
1111 | ||
1112 | if (refold >= BPF_MAX_REFCNT) { | |
1113 | __bpf_prog_put(prog, false); | |
1114 | return ERR_PTR(-EBUSY); | |
1115 | } | |
1116 | ||
1117 | if (!refold) | |
1118 | return ERR_PTR(-ENOENT); | |
1119 | ||
1120 | return prog; | |
1121 | } | |
a6f6df69 | 1122 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
b16d9aa4 | 1123 | |
040ee692 | 1124 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
288b3de5 | 1125 | enum bpf_prog_type *attach_type, bool attach_drv) |
248f346f | 1126 | { |
288b3de5 JK |
1127 | /* not an attachment, just a refcount inc, always allow */ |
1128 | if (!attach_type) | |
1129 | return true; | |
248f346f JK |
1130 | |
1131 | if (prog->type != *attach_type) | |
1132 | return false; | |
288b3de5 | 1133 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
248f346f JK |
1134 | return false; |
1135 | ||
1136 | return true; | |
1137 | } | |
1138 | ||
1139 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, | |
288b3de5 | 1140 | bool attach_drv) |
09756af4 AS |
1141 | { |
1142 | struct fd f = fdget(ufd); | |
1143 | struct bpf_prog *prog; | |
1144 | ||
113214be | 1145 | prog = ____bpf_prog_get(f); |
09756af4 AS |
1146 | if (IS_ERR(prog)) |
1147 | return prog; | |
288b3de5 | 1148 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
113214be DB |
1149 | prog = ERR_PTR(-EINVAL); |
1150 | goto out; | |
1151 | } | |
09756af4 | 1152 | |
92117d84 | 1153 | prog = bpf_prog_inc(prog); |
113214be | 1154 | out: |
09756af4 AS |
1155 | fdput(f); |
1156 | return prog; | |
1157 | } | |
113214be DB |
1158 | |
1159 | struct bpf_prog *bpf_prog_get(u32 ufd) | |
1160 | { | |
288b3de5 | 1161 | return __bpf_prog_get(ufd, NULL, false); |
113214be DB |
1162 | } |
1163 | ||
248f346f | 1164 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
288b3de5 | 1165 | bool attach_drv) |
248f346f | 1166 | { |
288b3de5 | 1167 | struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv); |
248f346f JK |
1168 | |
1169 | if (!IS_ERR(prog)) | |
1170 | trace_bpf_prog_get_type(prog); | |
1171 | return prog; | |
1172 | } | |
6c8dfe21 | 1173 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
248f346f | 1174 | |
aac3fc32 AI |
1175 | /* Initially all BPF programs could be loaded w/o specifying |
1176 | * expected_attach_type. Later for some of them specifying expected_attach_type | |
1177 | * at load time became required so that program could be validated properly. | |
1178 | * Programs of types that are allowed to be loaded both w/ and w/o (for | |
1179 | * backward compatibility) expected_attach_type, should have the default attach | |
1180 | * type assigned to expected_attach_type for the latter case, so that it can be | |
1181 | * validated later at attach time. | |
1182 | * | |
1183 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if | |
1184 | * prog type requires it but has some attach types that have to be backward | |
1185 | * compatible. | |
1186 | */ | |
1187 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) | |
1188 | { | |
1189 | switch (attr->prog_type) { | |
1190 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
1191 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't | |
1192 | * exist so checking for non-zero is the way to go here. | |
1193 | */ | |
1194 | if (!attr->expected_attach_type) | |
1195 | attr->expected_attach_type = | |
1196 | BPF_CGROUP_INET_SOCK_CREATE; | |
1197 | break; | |
1198 | } | |
1199 | } | |
1200 | ||
5e43f899 AI |
1201 | static int |
1202 | bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, | |
1203 | enum bpf_attach_type expected_attach_type) | |
1204 | { | |
4fbac77d | 1205 | switch (prog_type) { |
aac3fc32 AI |
1206 | case BPF_PROG_TYPE_CGROUP_SOCK: |
1207 | switch (expected_attach_type) { | |
1208 | case BPF_CGROUP_INET_SOCK_CREATE: | |
1209 | case BPF_CGROUP_INET4_POST_BIND: | |
1210 | case BPF_CGROUP_INET6_POST_BIND: | |
1211 | return 0; | |
1212 | default: | |
1213 | return -EINVAL; | |
1214 | } | |
4fbac77d AI |
1215 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
1216 | switch (expected_attach_type) { | |
1217 | case BPF_CGROUP_INET4_BIND: | |
1218 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
1219 | case BPF_CGROUP_INET4_CONNECT: |
1220 | case BPF_CGROUP_INET6_CONNECT: | |
4fbac77d AI |
1221 | return 0; |
1222 | default: | |
1223 | return -EINVAL; | |
1224 | } | |
1225 | default: | |
1226 | return 0; | |
1227 | } | |
5e43f899 AI |
1228 | } |
1229 | ||
09756af4 | 1230 | /* last field in 'union bpf_attr' used by this command */ |
5e43f899 | 1231 | #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type |
09756af4 AS |
1232 | |
1233 | static int bpf_prog_load(union bpf_attr *attr) | |
1234 | { | |
1235 | enum bpf_prog_type type = attr->prog_type; | |
1236 | struct bpf_prog *prog; | |
1237 | int err; | |
1238 | char license[128]; | |
1239 | bool is_gpl; | |
1240 | ||
1241 | if (CHECK_ATTR(BPF_PROG_LOAD)) | |
1242 | return -EINVAL; | |
1243 | ||
e07b98d9 DM |
1244 | if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT) |
1245 | return -EINVAL; | |
1246 | ||
09756af4 | 1247 | /* copy eBPF program license from user space */ |
535e7b4b | 1248 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
09756af4 AS |
1249 | sizeof(license) - 1) < 0) |
1250 | return -EFAULT; | |
1251 | license[sizeof(license) - 1] = 0; | |
1252 | ||
1253 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
1254 | is_gpl = license_is_gpl_compatible(license); | |
1255 | ||
ef0915ca DB |
1256 | if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) |
1257 | return -E2BIG; | |
09756af4 | 1258 | |
2541517c AS |
1259 | if (type == BPF_PROG_TYPE_KPROBE && |
1260 | attr->kern_version != LINUX_VERSION_CODE) | |
1261 | return -EINVAL; | |
1262 | ||
80b7d819 CF |
1263 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
1264 | type != BPF_PROG_TYPE_CGROUP_SKB && | |
1265 | !capable(CAP_SYS_ADMIN)) | |
1be7f75d AS |
1266 | return -EPERM; |
1267 | ||
aac3fc32 | 1268 | bpf_prog_load_fixup_attach_type(attr); |
5e43f899 AI |
1269 | if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) |
1270 | return -EINVAL; | |
1271 | ||
09756af4 AS |
1272 | /* plain bpf_prog allocation */ |
1273 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); | |
1274 | if (!prog) | |
1275 | return -ENOMEM; | |
1276 | ||
5e43f899 AI |
1277 | prog->expected_attach_type = attr->expected_attach_type; |
1278 | ||
9a18eedb JK |
1279 | prog->aux->offload_requested = !!attr->prog_ifindex; |
1280 | ||
afdb09c7 | 1281 | err = security_bpf_prog_alloc(prog->aux); |
aaac3ba9 AS |
1282 | if (err) |
1283 | goto free_prog_nouncharge; | |
1284 | ||
afdb09c7 CF |
1285 | err = bpf_prog_charge_memlock(prog); |
1286 | if (err) | |
1287 | goto free_prog_sec; | |
1288 | ||
09756af4 AS |
1289 | prog->len = attr->insn_cnt; |
1290 | ||
1291 | err = -EFAULT; | |
535e7b4b | 1292 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
aafe6ae9 | 1293 | bpf_prog_insn_size(prog)) != 0) |
09756af4 AS |
1294 | goto free_prog; |
1295 | ||
1296 | prog->orig_prog = NULL; | |
a91263d5 | 1297 | prog->jited = 0; |
09756af4 AS |
1298 | |
1299 | atomic_set(&prog->aux->refcnt, 1); | |
a91263d5 | 1300 | prog->gpl_compatible = is_gpl ? 1 : 0; |
09756af4 | 1301 | |
9a18eedb | 1302 | if (bpf_prog_is_dev_bound(prog->aux)) { |
ab3f0063 JK |
1303 | err = bpf_prog_offload_init(prog, attr); |
1304 | if (err) | |
1305 | goto free_prog; | |
1306 | } | |
1307 | ||
09756af4 AS |
1308 | /* find program type: socket_filter vs tracing_filter */ |
1309 | err = find_prog_type(type, prog); | |
1310 | if (err < 0) | |
1311 | goto free_prog; | |
1312 | ||
cb4d2b3f MKL |
1313 | prog->aux->load_time = ktime_get_boot_ns(); |
1314 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); | |
1315 | if (err) | |
1316 | goto free_prog; | |
1317 | ||
09756af4 | 1318 | /* run eBPF verifier */ |
9bac3d6d | 1319 | err = bpf_check(&prog, attr); |
09756af4 AS |
1320 | if (err < 0) |
1321 | goto free_used_maps; | |
1322 | ||
1323 | /* eBPF program is ready to be JITed */ | |
1c2a088a AS |
1324 | if (!prog->bpf_func) |
1325 | prog = bpf_prog_select_runtime(prog, &err); | |
04fd61ab AS |
1326 | if (err < 0) |
1327 | goto free_used_maps; | |
09756af4 | 1328 | |
dc4bb0e2 MKL |
1329 | err = bpf_prog_alloc_id(prog); |
1330 | if (err) | |
1331 | goto free_used_maps; | |
1332 | ||
aa79781b | 1333 | err = bpf_prog_new_fd(prog); |
b16d9aa4 MKL |
1334 | if (err < 0) { |
1335 | /* failed to allocate fd. | |
1336 | * bpf_prog_put() is needed because the above | |
1337 | * bpf_prog_alloc_id() has published the prog | |
1338 | * to the userspace and the userspace may | |
1339 | * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. | |
1340 | */ | |
1341 | bpf_prog_put(prog); | |
1342 | return err; | |
1343 | } | |
09756af4 | 1344 | |
74451e66 | 1345 | bpf_prog_kallsyms_add(prog); |
a67edbf4 | 1346 | trace_bpf_prog_load(prog, err); |
09756af4 AS |
1347 | return err; |
1348 | ||
1349 | free_used_maps: | |
1350 | free_used_maps(prog->aux); | |
1351 | free_prog: | |
aaac3ba9 | 1352 | bpf_prog_uncharge_memlock(prog); |
afdb09c7 CF |
1353 | free_prog_sec: |
1354 | security_bpf_prog_free(prog->aux); | |
aaac3ba9 | 1355 | free_prog_nouncharge: |
09756af4 AS |
1356 | bpf_prog_free(prog); |
1357 | return err; | |
1358 | } | |
1359 | ||
6e71b04a | 1360 | #define BPF_OBJ_LAST_FIELD file_flags |
b2197755 DB |
1361 | |
1362 | static int bpf_obj_pin(const union bpf_attr *attr) | |
1363 | { | |
6e71b04a | 1364 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
b2197755 DB |
1365 | return -EINVAL; |
1366 | ||
535e7b4b | 1367 | return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); |
b2197755 DB |
1368 | } |
1369 | ||
1370 | static int bpf_obj_get(const union bpf_attr *attr) | |
1371 | { | |
6e71b04a CF |
1372 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
1373 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) | |
b2197755 DB |
1374 | return -EINVAL; |
1375 | ||
6e71b04a CF |
1376 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
1377 | attr->file_flags); | |
b2197755 DB |
1378 | } |
1379 | ||
c4f6699d AS |
1380 | struct bpf_raw_tracepoint { |
1381 | struct bpf_raw_event_map *btp; | |
1382 | struct bpf_prog *prog; | |
1383 | }; | |
1384 | ||
1385 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) | |
1386 | { | |
1387 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; | |
1388 | ||
1389 | if (raw_tp->prog) { | |
1390 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); | |
1391 | bpf_prog_put(raw_tp->prog); | |
1392 | } | |
1393 | kfree(raw_tp); | |
1394 | return 0; | |
1395 | } | |
1396 | ||
1397 | static const struct file_operations bpf_raw_tp_fops = { | |
1398 | .release = bpf_raw_tracepoint_release, | |
1399 | .read = bpf_dummy_read, | |
1400 | .write = bpf_dummy_write, | |
1401 | }; | |
1402 | ||
1403 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd | |
1404 | ||
1405 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) | |
1406 | { | |
1407 | struct bpf_raw_tracepoint *raw_tp; | |
1408 | struct bpf_raw_event_map *btp; | |
1409 | struct bpf_prog *prog; | |
1410 | char tp_name[128]; | |
1411 | int tp_fd, err; | |
1412 | ||
1413 | if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), | |
1414 | sizeof(tp_name) - 1) < 0) | |
1415 | return -EFAULT; | |
1416 | tp_name[sizeof(tp_name) - 1] = 0; | |
1417 | ||
1418 | btp = bpf_find_raw_tracepoint(tp_name); | |
1419 | if (!btp) | |
1420 | return -ENOENT; | |
1421 | ||
1422 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); | |
1423 | if (!raw_tp) | |
1424 | return -ENOMEM; | |
1425 | raw_tp->btp = btp; | |
1426 | ||
1427 | prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd, | |
1428 | BPF_PROG_TYPE_RAW_TRACEPOINT); | |
1429 | if (IS_ERR(prog)) { | |
1430 | err = PTR_ERR(prog); | |
1431 | goto out_free_tp; | |
1432 | } | |
1433 | ||
1434 | err = bpf_probe_register(raw_tp->btp, prog); | |
1435 | if (err) | |
1436 | goto out_put_prog; | |
1437 | ||
1438 | raw_tp->prog = prog; | |
1439 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, | |
1440 | O_CLOEXEC); | |
1441 | if (tp_fd < 0) { | |
1442 | bpf_probe_unregister(raw_tp->btp, prog); | |
1443 | err = tp_fd; | |
1444 | goto out_put_prog; | |
1445 | } | |
1446 | return tp_fd; | |
1447 | ||
1448 | out_put_prog: | |
1449 | bpf_prog_put(prog); | |
1450 | out_free_tp: | |
1451 | kfree(raw_tp); | |
1452 | return err; | |
1453 | } | |
1454 | ||
f4324551 DM |
1455 | #ifdef CONFIG_CGROUP_BPF |
1456 | ||
33491588 AR |
1457 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
1458 | enum bpf_attach_type attach_type) | |
1459 | { | |
1460 | switch (prog->type) { | |
1461 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
1462 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: | |
1463 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; | |
1464 | default: | |
1465 | return 0; | |
1466 | } | |
1467 | } | |
1468 | ||
464bc0fd | 1469 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
174a79ff | 1470 | |
4f738adb JF |
1471 | static int sockmap_get_from_fd(const union bpf_attr *attr, |
1472 | int type, bool attach) | |
174a79ff | 1473 | { |
5a67da2a | 1474 | struct bpf_prog *prog = NULL; |
174a79ff JF |
1475 | int ufd = attr->target_fd; |
1476 | struct bpf_map *map; | |
1477 | struct fd f; | |
1478 | int err; | |
1479 | ||
1480 | f = fdget(ufd); | |
1481 | map = __bpf_map_get(f); | |
1482 | if (IS_ERR(map)) | |
1483 | return PTR_ERR(map); | |
1484 | ||
5a67da2a | 1485 | if (attach) { |
4f738adb | 1486 | prog = bpf_prog_get_type(attr->attach_bpf_fd, type); |
5a67da2a JF |
1487 | if (IS_ERR(prog)) { |
1488 | fdput(f); | |
1489 | return PTR_ERR(prog); | |
1490 | } | |
174a79ff JF |
1491 | } |
1492 | ||
5a67da2a | 1493 | err = sock_map_prog(map, prog, attr->attach_type); |
174a79ff JF |
1494 | if (err) { |
1495 | fdput(f); | |
5a67da2a JF |
1496 | if (prog) |
1497 | bpf_prog_put(prog); | |
ae2b27b8 | 1498 | return err; |
174a79ff JF |
1499 | } |
1500 | ||
1501 | fdput(f); | |
ae2b27b8 | 1502 | return 0; |
174a79ff | 1503 | } |
f4324551 | 1504 | |
324bda9e AS |
1505 | #define BPF_F_ATTACH_MASK \ |
1506 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) | |
1507 | ||
f4324551 DM |
1508 | static int bpf_prog_attach(const union bpf_attr *attr) |
1509 | { | |
7f677633 | 1510 | enum bpf_prog_type ptype; |
f4324551 DM |
1511 | struct bpf_prog *prog; |
1512 | struct cgroup *cgrp; | |
7f677633 | 1513 | int ret; |
f4324551 DM |
1514 | |
1515 | if (!capable(CAP_NET_ADMIN)) | |
1516 | return -EPERM; | |
1517 | ||
1518 | if (CHECK_ATTR(BPF_PROG_ATTACH)) | |
1519 | return -EINVAL; | |
1520 | ||
324bda9e | 1521 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
7f677633 AS |
1522 | return -EINVAL; |
1523 | ||
f4324551 DM |
1524 | switch (attr->attach_type) { |
1525 | case BPF_CGROUP_INET_INGRESS: | |
1526 | case BPF_CGROUP_INET_EGRESS: | |
b2cd1257 | 1527 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
f4324551 | 1528 | break; |
61023658 | 1529 | case BPF_CGROUP_INET_SOCK_CREATE: |
aac3fc32 AI |
1530 | case BPF_CGROUP_INET4_POST_BIND: |
1531 | case BPF_CGROUP_INET6_POST_BIND: | |
61023658 DA |
1532 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
1533 | break; | |
4fbac77d AI |
1534 | case BPF_CGROUP_INET4_BIND: |
1535 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
1536 | case BPF_CGROUP_INET4_CONNECT: |
1537 | case BPF_CGROUP_INET6_CONNECT: | |
4fbac77d AI |
1538 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
1539 | break; | |
40304b2a LB |
1540 | case BPF_CGROUP_SOCK_OPS: |
1541 | ptype = BPF_PROG_TYPE_SOCK_OPS; | |
1542 | break; | |
ebc614f6 RG |
1543 | case BPF_CGROUP_DEVICE: |
1544 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; | |
1545 | break; | |
4f738adb JF |
1546 | case BPF_SK_MSG_VERDICT: |
1547 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true); | |
464bc0fd JF |
1548 | case BPF_SK_SKB_STREAM_PARSER: |
1549 | case BPF_SK_SKB_STREAM_VERDICT: | |
4f738adb | 1550 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true); |
f4324551 DM |
1551 | default: |
1552 | return -EINVAL; | |
1553 | } | |
1554 | ||
b2cd1257 DA |
1555 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
1556 | if (IS_ERR(prog)) | |
1557 | return PTR_ERR(prog); | |
1558 | ||
5e43f899 AI |
1559 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
1560 | bpf_prog_put(prog); | |
1561 | return -EINVAL; | |
1562 | } | |
1563 | ||
b2cd1257 DA |
1564 | cgrp = cgroup_get_from_fd(attr->target_fd); |
1565 | if (IS_ERR(cgrp)) { | |
1566 | bpf_prog_put(prog); | |
1567 | return PTR_ERR(cgrp); | |
1568 | } | |
1569 | ||
324bda9e AS |
1570 | ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type, |
1571 | attr->attach_flags); | |
7f677633 AS |
1572 | if (ret) |
1573 | bpf_prog_put(prog); | |
b2cd1257 DA |
1574 | cgroup_put(cgrp); |
1575 | ||
7f677633 | 1576 | return ret; |
f4324551 DM |
1577 | } |
1578 | ||
1579 | #define BPF_PROG_DETACH_LAST_FIELD attach_type | |
1580 | ||
1581 | static int bpf_prog_detach(const union bpf_attr *attr) | |
1582 | { | |
324bda9e AS |
1583 | enum bpf_prog_type ptype; |
1584 | struct bpf_prog *prog; | |
f4324551 | 1585 | struct cgroup *cgrp; |
7f677633 | 1586 | int ret; |
f4324551 DM |
1587 | |
1588 | if (!capable(CAP_NET_ADMIN)) | |
1589 | return -EPERM; | |
1590 | ||
1591 | if (CHECK_ATTR(BPF_PROG_DETACH)) | |
1592 | return -EINVAL; | |
1593 | ||
1594 | switch (attr->attach_type) { | |
1595 | case BPF_CGROUP_INET_INGRESS: | |
1596 | case BPF_CGROUP_INET_EGRESS: | |
324bda9e AS |
1597 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
1598 | break; | |
61023658 | 1599 | case BPF_CGROUP_INET_SOCK_CREATE: |
aac3fc32 AI |
1600 | case BPF_CGROUP_INET4_POST_BIND: |
1601 | case BPF_CGROUP_INET6_POST_BIND: | |
324bda9e AS |
1602 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
1603 | break; | |
4fbac77d AI |
1604 | case BPF_CGROUP_INET4_BIND: |
1605 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
1606 | case BPF_CGROUP_INET4_CONNECT: |
1607 | case BPF_CGROUP_INET6_CONNECT: | |
4fbac77d AI |
1608 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
1609 | break; | |
40304b2a | 1610 | case BPF_CGROUP_SOCK_OPS: |
324bda9e | 1611 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
f4324551 | 1612 | break; |
ebc614f6 RG |
1613 | case BPF_CGROUP_DEVICE: |
1614 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; | |
1615 | break; | |
4f738adb JF |
1616 | case BPF_SK_MSG_VERDICT: |
1617 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false); | |
5a67da2a JF |
1618 | case BPF_SK_SKB_STREAM_PARSER: |
1619 | case BPF_SK_SKB_STREAM_VERDICT: | |
4f738adb | 1620 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false); |
f4324551 DM |
1621 | default: |
1622 | return -EINVAL; | |
1623 | } | |
1624 | ||
324bda9e AS |
1625 | cgrp = cgroup_get_from_fd(attr->target_fd); |
1626 | if (IS_ERR(cgrp)) | |
1627 | return PTR_ERR(cgrp); | |
1628 | ||
1629 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); | |
1630 | if (IS_ERR(prog)) | |
1631 | prog = NULL; | |
1632 | ||
1633 | ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0); | |
1634 | if (prog) | |
1635 | bpf_prog_put(prog); | |
1636 | cgroup_put(cgrp); | |
7f677633 | 1637 | return ret; |
f4324551 | 1638 | } |
40304b2a | 1639 | |
468e2f64 AS |
1640 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
1641 | ||
1642 | static int bpf_prog_query(const union bpf_attr *attr, | |
1643 | union bpf_attr __user *uattr) | |
1644 | { | |
1645 | struct cgroup *cgrp; | |
1646 | int ret; | |
1647 | ||
1648 | if (!capable(CAP_NET_ADMIN)) | |
1649 | return -EPERM; | |
1650 | if (CHECK_ATTR(BPF_PROG_QUERY)) | |
1651 | return -EINVAL; | |
1652 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) | |
1653 | return -EINVAL; | |
1654 | ||
1655 | switch (attr->query.attach_type) { | |
1656 | case BPF_CGROUP_INET_INGRESS: | |
1657 | case BPF_CGROUP_INET_EGRESS: | |
1658 | case BPF_CGROUP_INET_SOCK_CREATE: | |
4fbac77d AI |
1659 | case BPF_CGROUP_INET4_BIND: |
1660 | case BPF_CGROUP_INET6_BIND: | |
aac3fc32 AI |
1661 | case BPF_CGROUP_INET4_POST_BIND: |
1662 | case BPF_CGROUP_INET6_POST_BIND: | |
d74bad4e AI |
1663 | case BPF_CGROUP_INET4_CONNECT: |
1664 | case BPF_CGROUP_INET6_CONNECT: | |
468e2f64 | 1665 | case BPF_CGROUP_SOCK_OPS: |
ebc614f6 | 1666 | case BPF_CGROUP_DEVICE: |
468e2f64 AS |
1667 | break; |
1668 | default: | |
1669 | return -EINVAL; | |
1670 | } | |
1671 | cgrp = cgroup_get_from_fd(attr->query.target_fd); | |
1672 | if (IS_ERR(cgrp)) | |
1673 | return PTR_ERR(cgrp); | |
1674 | ret = cgroup_bpf_query(cgrp, attr, uattr); | |
1675 | cgroup_put(cgrp); | |
1676 | return ret; | |
1677 | } | |
f4324551 DM |
1678 | #endif /* CONFIG_CGROUP_BPF */ |
1679 | ||
1cf1cae9 AS |
1680 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration |
1681 | ||
1682 | static int bpf_prog_test_run(const union bpf_attr *attr, | |
1683 | union bpf_attr __user *uattr) | |
1684 | { | |
1685 | struct bpf_prog *prog; | |
1686 | int ret = -ENOTSUPP; | |
1687 | ||
61f3c964 AS |
1688 | if (!capable(CAP_SYS_ADMIN)) |
1689 | return -EPERM; | |
1cf1cae9 AS |
1690 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
1691 | return -EINVAL; | |
1692 | ||
1693 | prog = bpf_prog_get(attr->test.prog_fd); | |
1694 | if (IS_ERR(prog)) | |
1695 | return PTR_ERR(prog); | |
1696 | ||
1697 | if (prog->aux->ops->test_run) | |
1698 | ret = prog->aux->ops->test_run(prog, attr, uattr); | |
1699 | ||
1700 | bpf_prog_put(prog); | |
1701 | return ret; | |
1702 | } | |
1703 | ||
34ad5580 MKL |
1704 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
1705 | ||
1706 | static int bpf_obj_get_next_id(const union bpf_attr *attr, | |
1707 | union bpf_attr __user *uattr, | |
1708 | struct idr *idr, | |
1709 | spinlock_t *lock) | |
1710 | { | |
1711 | u32 next_id = attr->start_id; | |
1712 | int err = 0; | |
1713 | ||
1714 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) | |
1715 | return -EINVAL; | |
1716 | ||
1717 | if (!capable(CAP_SYS_ADMIN)) | |
1718 | return -EPERM; | |
1719 | ||
1720 | next_id++; | |
1721 | spin_lock_bh(lock); | |
1722 | if (!idr_get_next(idr, &next_id)) | |
1723 | err = -ENOENT; | |
1724 | spin_unlock_bh(lock); | |
1725 | ||
1726 | if (!err) | |
1727 | err = put_user(next_id, &uattr->next_id); | |
1728 | ||
1729 | return err; | |
1730 | } | |
1731 | ||
b16d9aa4 MKL |
1732 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
1733 | ||
1734 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) | |
1735 | { | |
1736 | struct bpf_prog *prog; | |
1737 | u32 id = attr->prog_id; | |
1738 | int fd; | |
1739 | ||
1740 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) | |
1741 | return -EINVAL; | |
1742 | ||
1743 | if (!capable(CAP_SYS_ADMIN)) | |
1744 | return -EPERM; | |
1745 | ||
1746 | spin_lock_bh(&prog_idr_lock); | |
1747 | prog = idr_find(&prog_idr, id); | |
1748 | if (prog) | |
1749 | prog = bpf_prog_inc_not_zero(prog); | |
1750 | else | |
1751 | prog = ERR_PTR(-ENOENT); | |
1752 | spin_unlock_bh(&prog_idr_lock); | |
1753 | ||
1754 | if (IS_ERR(prog)) | |
1755 | return PTR_ERR(prog); | |
1756 | ||
1757 | fd = bpf_prog_new_fd(prog); | |
1758 | if (fd < 0) | |
1759 | bpf_prog_put(prog); | |
1760 | ||
1761 | return fd; | |
1762 | } | |
1763 | ||
6e71b04a | 1764 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
bd5f5f4e MKL |
1765 | |
1766 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) | |
1767 | { | |
1768 | struct bpf_map *map; | |
1769 | u32 id = attr->map_id; | |
6e71b04a | 1770 | int f_flags; |
bd5f5f4e MKL |
1771 | int fd; |
1772 | ||
6e71b04a CF |
1773 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
1774 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) | |
bd5f5f4e MKL |
1775 | return -EINVAL; |
1776 | ||
1777 | if (!capable(CAP_SYS_ADMIN)) | |
1778 | return -EPERM; | |
1779 | ||
6e71b04a CF |
1780 | f_flags = bpf_get_file_flag(attr->open_flags); |
1781 | if (f_flags < 0) | |
1782 | return f_flags; | |
1783 | ||
bd5f5f4e MKL |
1784 | spin_lock_bh(&map_idr_lock); |
1785 | map = idr_find(&map_idr, id); | |
1786 | if (map) | |
1787 | map = bpf_map_inc_not_zero(map, true); | |
1788 | else | |
1789 | map = ERR_PTR(-ENOENT); | |
1790 | spin_unlock_bh(&map_idr_lock); | |
1791 | ||
1792 | if (IS_ERR(map)) | |
1793 | return PTR_ERR(map); | |
1794 | ||
6e71b04a | 1795 | fd = bpf_map_new_fd(map, f_flags); |
bd5f5f4e MKL |
1796 | if (fd < 0) |
1797 | bpf_map_put(map); | |
1798 | ||
1799 | return fd; | |
1800 | } | |
1801 | ||
7105e828 DB |
1802 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
1803 | unsigned long addr) | |
1804 | { | |
1805 | int i; | |
1806 | ||
1807 | for (i = 0; i < prog->aux->used_map_cnt; i++) | |
1808 | if (prog->aux->used_maps[i] == (void *)addr) | |
1809 | return prog->aux->used_maps[i]; | |
1810 | return NULL; | |
1811 | } | |
1812 | ||
1813 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) | |
1814 | { | |
1815 | const struct bpf_map *map; | |
1816 | struct bpf_insn *insns; | |
1817 | u64 imm; | |
1818 | int i; | |
1819 | ||
1820 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), | |
1821 | GFP_USER); | |
1822 | if (!insns) | |
1823 | return insns; | |
1824 | ||
1825 | for (i = 0; i < prog->len; i++) { | |
1826 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { | |
1827 | insns[i].code = BPF_JMP | BPF_CALL; | |
1828 | insns[i].imm = BPF_FUNC_tail_call; | |
1829 | /* fall-through */ | |
1830 | } | |
1831 | if (insns[i].code == (BPF_JMP | BPF_CALL) || | |
1832 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { | |
1833 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) | |
1834 | insns[i].code = BPF_JMP | BPF_CALL; | |
1835 | if (!bpf_dump_raw_ok()) | |
1836 | insns[i].imm = 0; | |
1837 | continue; | |
1838 | } | |
1839 | ||
1840 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) | |
1841 | continue; | |
1842 | ||
1843 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; | |
1844 | map = bpf_map_from_imm(prog, imm); | |
1845 | if (map) { | |
1846 | insns[i].src_reg = BPF_PSEUDO_MAP_FD; | |
1847 | insns[i].imm = map->id; | |
1848 | insns[i + 1].imm = 0; | |
1849 | continue; | |
1850 | } | |
1851 | ||
1852 | if (!bpf_dump_raw_ok() && | |
1853 | imm == (unsigned long)prog->aux) { | |
1854 | insns[i].imm = 0; | |
1855 | insns[i + 1].imm = 0; | |
1856 | continue; | |
1857 | } | |
1858 | } | |
1859 | ||
1860 | return insns; | |
1861 | } | |
1862 | ||
1e270976 MKL |
1863 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
1864 | const union bpf_attr *attr, | |
1865 | union bpf_attr __user *uattr) | |
1866 | { | |
1867 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
1868 | struct bpf_prog_info info = {}; | |
1869 | u32 info_len = attr->info.info_len; | |
1870 | char __user *uinsns; | |
1871 | u32 ulen; | |
1872 | int err; | |
1873 | ||
1874 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); | |
1875 | if (err) | |
1876 | return err; | |
1877 | info_len = min_t(u32, sizeof(info), info_len); | |
1878 | ||
1879 | if (copy_from_user(&info, uinfo, info_len)) | |
89b09689 | 1880 | return -EFAULT; |
1e270976 MKL |
1881 | |
1882 | info.type = prog->type; | |
1883 | info.id = prog->aux->id; | |
cb4d2b3f MKL |
1884 | info.load_time = prog->aux->load_time; |
1885 | info.created_by_uid = from_kuid_munged(current_user_ns(), | |
1886 | prog->aux->user->uid); | |
1e270976 MKL |
1887 | |
1888 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); | |
cb4d2b3f MKL |
1889 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
1890 | ||
1891 | ulen = info.nr_map_ids; | |
1892 | info.nr_map_ids = prog->aux->used_map_cnt; | |
1893 | ulen = min_t(u32, info.nr_map_ids, ulen); | |
1894 | if (ulen) { | |
721e08da | 1895 | u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); |
cb4d2b3f MKL |
1896 | u32 i; |
1897 | ||
1898 | for (i = 0; i < ulen; i++) | |
1899 | if (put_user(prog->aux->used_maps[i]->id, | |
1900 | &user_map_ids[i])) | |
1901 | return -EFAULT; | |
1902 | } | |
1e270976 MKL |
1903 | |
1904 | if (!capable(CAP_SYS_ADMIN)) { | |
1905 | info.jited_prog_len = 0; | |
1906 | info.xlated_prog_len = 0; | |
1907 | goto done; | |
1908 | } | |
1909 | ||
1e270976 | 1910 | ulen = info.xlated_prog_len; |
9975a54b | 1911 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
1e270976 | 1912 | if (info.xlated_prog_len && ulen) { |
7105e828 DB |
1913 | struct bpf_insn *insns_sanitized; |
1914 | bool fault; | |
1915 | ||
1916 | if (prog->blinded && !bpf_dump_raw_ok()) { | |
1917 | info.xlated_prog_insns = 0; | |
1918 | goto done; | |
1919 | } | |
1920 | insns_sanitized = bpf_insn_prepare_dump(prog); | |
1921 | if (!insns_sanitized) | |
1922 | return -ENOMEM; | |
1e270976 MKL |
1923 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
1924 | ulen = min_t(u32, info.xlated_prog_len, ulen); | |
7105e828 DB |
1925 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
1926 | kfree(insns_sanitized); | |
1927 | if (fault) | |
1e270976 MKL |
1928 | return -EFAULT; |
1929 | } | |
1930 | ||
675fc275 JK |
1931 | if (bpf_prog_is_dev_bound(prog->aux)) { |
1932 | err = bpf_prog_offload_info_fill(&info, prog); | |
1933 | if (err) | |
1934 | return err; | |
fcfb126d JW |
1935 | goto done; |
1936 | } | |
1937 | ||
1938 | /* NOTE: the following code is supposed to be skipped for offload. | |
1939 | * bpf_prog_offload_info_fill() is the place to fill similar fields | |
1940 | * for offload. | |
1941 | */ | |
1942 | ulen = info.jited_prog_len; | |
1943 | info.jited_prog_len = prog->jited_len; | |
1944 | if (info.jited_prog_len && ulen) { | |
1945 | if (bpf_dump_raw_ok()) { | |
1946 | uinsns = u64_to_user_ptr(info.jited_prog_insns); | |
1947 | ulen = min_t(u32, info.jited_prog_len, ulen); | |
1948 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) | |
1949 | return -EFAULT; | |
1950 | } else { | |
1951 | info.jited_prog_insns = 0; | |
1952 | } | |
675fc275 JK |
1953 | } |
1954 | ||
1e270976 MKL |
1955 | done: |
1956 | if (copy_to_user(uinfo, &info, info_len) || | |
1957 | put_user(info_len, &uattr->info.info_len)) | |
1958 | return -EFAULT; | |
1959 | ||
1960 | return 0; | |
1961 | } | |
1962 | ||
1963 | static int bpf_map_get_info_by_fd(struct bpf_map *map, | |
1964 | const union bpf_attr *attr, | |
1965 | union bpf_attr __user *uattr) | |
1966 | { | |
1967 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
1968 | struct bpf_map_info info = {}; | |
1969 | u32 info_len = attr->info.info_len; | |
1970 | int err; | |
1971 | ||
1972 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); | |
1973 | if (err) | |
1974 | return err; | |
1975 | info_len = min_t(u32, sizeof(info), info_len); | |
1976 | ||
1977 | info.type = map->map_type; | |
1978 | info.id = map->id; | |
1979 | info.key_size = map->key_size; | |
1980 | info.value_size = map->value_size; | |
1981 | info.max_entries = map->max_entries; | |
1982 | info.map_flags = map->map_flags; | |
ad5b177b | 1983 | memcpy(info.name, map->name, sizeof(map->name)); |
1e270976 | 1984 | |
52775b33 JK |
1985 | if (bpf_map_is_dev_bound(map)) { |
1986 | err = bpf_map_offload_info_fill(&info, map); | |
1987 | if (err) | |
1988 | return err; | |
1989 | } | |
1990 | ||
1e270976 MKL |
1991 | if (copy_to_user(uinfo, &info, info_len) || |
1992 | put_user(info_len, &uattr->info.info_len)) | |
1993 | return -EFAULT; | |
1994 | ||
1995 | return 0; | |
1996 | } | |
1997 | ||
1998 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info | |
1999 | ||
2000 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, | |
2001 | union bpf_attr __user *uattr) | |
2002 | { | |
2003 | int ufd = attr->info.bpf_fd; | |
2004 | struct fd f; | |
2005 | int err; | |
2006 | ||
2007 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) | |
2008 | return -EINVAL; | |
2009 | ||
2010 | f = fdget(ufd); | |
2011 | if (!f.file) | |
2012 | return -EBADFD; | |
2013 | ||
2014 | if (f.file->f_op == &bpf_prog_fops) | |
2015 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, | |
2016 | uattr); | |
2017 | else if (f.file->f_op == &bpf_map_fops) | |
2018 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, | |
2019 | uattr); | |
60197cfb MKL |
2020 | else if (f.file->f_op == &btf_fops) |
2021 | err = btf_get_info_by_fd(f.file->private_data, attr, uattr); | |
1e270976 MKL |
2022 | else |
2023 | err = -EINVAL; | |
2024 | ||
2025 | fdput(f); | |
2026 | return err; | |
2027 | } | |
2028 | ||
f56a653c MKL |
2029 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
2030 | ||
2031 | static int bpf_btf_load(const union bpf_attr *attr) | |
2032 | { | |
2033 | if (CHECK_ATTR(BPF_BTF_LOAD)) | |
2034 | return -EINVAL; | |
2035 | ||
2036 | if (!capable(CAP_SYS_ADMIN)) | |
2037 | return -EPERM; | |
2038 | ||
2039 | return btf_new_fd(attr); | |
2040 | } | |
2041 | ||
99c55f7d AS |
2042 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
2043 | { | |
2044 | union bpf_attr attr = {}; | |
2045 | int err; | |
2046 | ||
0fa4fe85 | 2047 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
99c55f7d AS |
2048 | return -EPERM; |
2049 | ||
1e270976 MKL |
2050 | err = check_uarg_tail_zero(uattr, sizeof(attr), size); |
2051 | if (err) | |
2052 | return err; | |
2053 | size = min_t(u32, size, sizeof(attr)); | |
99c55f7d AS |
2054 | |
2055 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ | |
2056 | if (copy_from_user(&attr, uattr, size) != 0) | |
2057 | return -EFAULT; | |
2058 | ||
afdb09c7 CF |
2059 | err = security_bpf(cmd, &attr, size); |
2060 | if (err < 0) | |
2061 | return err; | |
2062 | ||
99c55f7d AS |
2063 | switch (cmd) { |
2064 | case BPF_MAP_CREATE: | |
2065 | err = map_create(&attr); | |
2066 | break; | |
db20fd2b AS |
2067 | case BPF_MAP_LOOKUP_ELEM: |
2068 | err = map_lookup_elem(&attr); | |
2069 | break; | |
2070 | case BPF_MAP_UPDATE_ELEM: | |
2071 | err = map_update_elem(&attr); | |
2072 | break; | |
2073 | case BPF_MAP_DELETE_ELEM: | |
2074 | err = map_delete_elem(&attr); | |
2075 | break; | |
2076 | case BPF_MAP_GET_NEXT_KEY: | |
2077 | err = map_get_next_key(&attr); | |
2078 | break; | |
09756af4 AS |
2079 | case BPF_PROG_LOAD: |
2080 | err = bpf_prog_load(&attr); | |
2081 | break; | |
b2197755 DB |
2082 | case BPF_OBJ_PIN: |
2083 | err = bpf_obj_pin(&attr); | |
2084 | break; | |
2085 | case BPF_OBJ_GET: | |
2086 | err = bpf_obj_get(&attr); | |
2087 | break; | |
f4324551 DM |
2088 | #ifdef CONFIG_CGROUP_BPF |
2089 | case BPF_PROG_ATTACH: | |
2090 | err = bpf_prog_attach(&attr); | |
2091 | break; | |
2092 | case BPF_PROG_DETACH: | |
2093 | err = bpf_prog_detach(&attr); | |
2094 | break; | |
468e2f64 AS |
2095 | case BPF_PROG_QUERY: |
2096 | err = bpf_prog_query(&attr, uattr); | |
2097 | break; | |
f4324551 | 2098 | #endif |
1cf1cae9 AS |
2099 | case BPF_PROG_TEST_RUN: |
2100 | err = bpf_prog_test_run(&attr, uattr); | |
2101 | break; | |
34ad5580 MKL |
2102 | case BPF_PROG_GET_NEXT_ID: |
2103 | err = bpf_obj_get_next_id(&attr, uattr, | |
2104 | &prog_idr, &prog_idr_lock); | |
2105 | break; | |
2106 | case BPF_MAP_GET_NEXT_ID: | |
2107 | err = bpf_obj_get_next_id(&attr, uattr, | |
2108 | &map_idr, &map_idr_lock); | |
2109 | break; | |
b16d9aa4 MKL |
2110 | case BPF_PROG_GET_FD_BY_ID: |
2111 | err = bpf_prog_get_fd_by_id(&attr); | |
2112 | break; | |
bd5f5f4e MKL |
2113 | case BPF_MAP_GET_FD_BY_ID: |
2114 | err = bpf_map_get_fd_by_id(&attr); | |
2115 | break; | |
1e270976 MKL |
2116 | case BPF_OBJ_GET_INFO_BY_FD: |
2117 | err = bpf_obj_get_info_by_fd(&attr, uattr); | |
2118 | break; | |
c4f6699d AS |
2119 | case BPF_RAW_TRACEPOINT_OPEN: |
2120 | err = bpf_raw_tracepoint_open(&attr); | |
2121 | break; | |
f56a653c MKL |
2122 | case BPF_BTF_LOAD: |
2123 | err = bpf_btf_load(&attr); | |
2124 | break; | |
99c55f7d AS |
2125 | default: |
2126 | err = -EINVAL; | |
2127 | break; | |
2128 | } | |
2129 | ||
2130 | return err; | |
2131 | } |