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