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