1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com 3 */ 4 #include <linux/bpf.h> 5 #include <linux/bpf-cgroup.h> 6 #include <linux/bpf_trace.h> 7 #include <linux/bpf_lirc.h> 8 #include <linux/bpf_verifier.h> 9 #include <linux/bsearch.h> 10 #include <linux/btf.h> 11 #include <linux/syscalls.h> 12 #include <linux/slab.h> 13 #include <linux/sched/signal.h> 14 #include <linux/vmalloc.h> 15 #include <linux/mmzone.h> 16 #include <linux/anon_inodes.h> 17 #include <linux/fdtable.h> 18 #include <linux/file.h> 19 #include <linux/fs.h> 20 #include <linux/license.h> 21 #include <linux/filter.h> 22 #include <linux/kernel.h> 23 #include <linux/idr.h> 24 #include <linux/cred.h> 25 #include <linux/timekeeping.h> 26 #include <linux/ctype.h> 27 #include <linux/nospec.h> 28 #include <linux/audit.h> 29 #include <uapi/linux/btf.h> 30 #include <linux/pgtable.h> 31 #include <linux/bpf_lsm.h> 32 #include <linux/poll.h> 33 #include <linux/sort.h> 34 #include <linux/bpf-netns.h> 35 #include <linux/rcupdate_trace.h> 36 #include <linux/memcontrol.h> 37 #include <linux/trace_events.h> 38 #include <linux/tracepoint.h> 39 40 #include <net/netfilter/nf_bpf_link.h> 41 #include <net/netkit.h> 42 #include <net/tcx.h> 43 44 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ 45 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ 46 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 47 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY) 48 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) 49 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \ 50 IS_FD_HASH(map)) 51 52 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) 53 54 DEFINE_PER_CPU(int, bpf_prog_active); 55 static DEFINE_IDR(prog_idr); 56 static DEFINE_SPINLOCK(prog_idr_lock); 57 static DEFINE_IDR(map_idr); 58 static DEFINE_SPINLOCK(map_idr_lock); 59 static DEFINE_IDR(link_idr); 60 static DEFINE_SPINLOCK(link_idr_lock); 61 62 int sysctl_unprivileged_bpf_disabled __read_mostly = 63 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0; 64 65 static const struct bpf_map_ops * const bpf_map_types[] = { 66 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 67 #define BPF_MAP_TYPE(_id, _ops) \ 68 [_id] = &_ops, 69 #define BPF_LINK_TYPE(_id, _name) 70 #include <linux/bpf_types.h> 71 #undef BPF_PROG_TYPE 72 #undef BPF_MAP_TYPE 73 #undef BPF_LINK_TYPE 74 }; 75 76 /* 77 * If we're handed a bigger struct than we know of, ensure all the unknown bits 78 * are 0 - i.e. new user-space does not rely on any kernel feature extensions 79 * we don't know about yet. 80 * 81 * There is a ToCToU between this function call and the following 82 * copy_from_user() call. However, this is not a concern since this function is 83 * meant to be a future-proofing of bits. 84 */ 85 int bpf_check_uarg_tail_zero(bpfptr_t uaddr, 86 size_t expected_size, 87 size_t actual_size) 88 { 89 int res; 90 91 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ 92 return -E2BIG; 93 94 if (actual_size <= expected_size) 95 return 0; 96 97 if (uaddr.is_kernel) 98 res = memchr_inv(uaddr.kernel + expected_size, 0, 99 actual_size - expected_size) == NULL; 100 else 101 res = check_zeroed_user(uaddr.user + expected_size, 102 actual_size - expected_size); 103 if (res < 0) 104 return res; 105 return res ? 0 : -E2BIG; 106 } 107 108 const struct bpf_map_ops bpf_map_offload_ops = { 109 .map_meta_equal = bpf_map_meta_equal, 110 .map_alloc = bpf_map_offload_map_alloc, 111 .map_free = bpf_map_offload_map_free, 112 .map_check_btf = map_check_no_btf, 113 .map_mem_usage = bpf_map_offload_map_mem_usage, 114 }; 115 116 static void bpf_map_write_active_inc(struct bpf_map *map) 117 { 118 atomic64_inc(&map->writecnt); 119 } 120 121 static void bpf_map_write_active_dec(struct bpf_map *map) 122 { 123 atomic64_dec(&map->writecnt); 124 } 125 126 bool bpf_map_write_active(const struct bpf_map *map) 127 { 128 return atomic64_read(&map->writecnt) != 0; 129 } 130 131 static u32 bpf_map_value_size(const struct bpf_map *map) 132 { 133 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 134 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 135 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || 136 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) 137 return round_up(map->value_size, 8) * num_possible_cpus(); 138 else if (IS_FD_MAP(map)) 139 return sizeof(u32); 140 else 141 return map->value_size; 142 } 143 144 static void maybe_wait_bpf_programs(struct bpf_map *map) 145 { 146 /* Wait for any running non-sleepable BPF programs to complete so that 147 * userspace, when we return to it, knows that all non-sleepable 148 * programs that could be running use the new map value. For sleepable 149 * BPF programs, synchronize_rcu_tasks_trace() should be used to wait 150 * for the completions of these programs, but considering the waiting 151 * time can be very long and userspace may think it will hang forever, 152 * so don't handle sleepable BPF programs now. 153 */ 154 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || 155 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 156 synchronize_rcu(); 157 } 158 159 static void unpin_uptr_kaddr(void *kaddr) 160 { 161 if (kaddr) 162 unpin_user_page(virt_to_page(kaddr)); 163 } 164 165 static void __bpf_obj_unpin_uptrs(struct btf_record *rec, u32 cnt, void *obj) 166 { 167 const struct btf_field *field; 168 void **uptr_addr; 169 int i; 170 171 for (i = 0, field = rec->fields; i < cnt; i++, field++) { 172 if (field->type != BPF_UPTR) 173 continue; 174 175 uptr_addr = obj + field->offset; 176 unpin_uptr_kaddr(*uptr_addr); 177 } 178 } 179 180 static void bpf_obj_unpin_uptrs(struct btf_record *rec, void *obj) 181 { 182 if (!btf_record_has_field(rec, BPF_UPTR)) 183 return; 184 185 __bpf_obj_unpin_uptrs(rec, rec->cnt, obj); 186 } 187 188 static int bpf_obj_pin_uptrs(struct btf_record *rec, void *obj) 189 { 190 const struct btf_field *field; 191 const struct btf_type *t; 192 unsigned long start, end; 193 struct page *page; 194 void **uptr_addr; 195 int i, err; 196 197 if (!btf_record_has_field(rec, BPF_UPTR)) 198 return 0; 199 200 for (i = 0, field = rec->fields; i < rec->cnt; i++, field++) { 201 if (field->type != BPF_UPTR) 202 continue; 203 204 uptr_addr = obj + field->offset; 205 start = *(unsigned long *)uptr_addr; 206 if (!start) 207 continue; 208 209 t = btf_type_by_id(field->kptr.btf, field->kptr.btf_id); 210 /* t->size was checked for zero before */ 211 if (check_add_overflow(start, t->size - 1, &end)) { 212 err = -EFAULT; 213 goto unpin_all; 214 } 215 216 /* The uptr's struct cannot span across two pages */ 217 if ((start & PAGE_MASK) != (end & PAGE_MASK)) { 218 err = -EOPNOTSUPP; 219 goto unpin_all; 220 } 221 222 err = pin_user_pages_fast(start, 1, FOLL_LONGTERM | FOLL_WRITE, &page); 223 if (err != 1) 224 goto unpin_all; 225 226 if (PageHighMem(page)) { 227 err = -EOPNOTSUPP; 228 unpin_user_page(page); 229 goto unpin_all; 230 } 231 232 *uptr_addr = page_address(page) + offset_in_page(start); 233 } 234 235 return 0; 236 237 unpin_all: 238 __bpf_obj_unpin_uptrs(rec, i, obj); 239 return err; 240 } 241 242 static int bpf_map_update_value(struct bpf_map *map, struct file *map_file, 243 void *key, void *value, __u64 flags) 244 { 245 int err; 246 247 /* Need to create a kthread, thus must support schedule */ 248 if (bpf_map_is_offloaded(map)) { 249 return bpf_map_offload_update_elem(map, key, value, flags); 250 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || 251 map->map_type == BPF_MAP_TYPE_ARENA || 252 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 253 return map->ops->map_update_elem(map, key, value, flags); 254 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH || 255 map->map_type == BPF_MAP_TYPE_SOCKMAP) { 256 return sock_map_update_elem_sys(map, key, value, flags); 257 } else if (IS_FD_PROG_ARRAY(map)) { 258 return bpf_fd_array_map_update_elem(map, map_file, key, value, 259 flags); 260 } 261 262 bpf_disable_instrumentation(); 263 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 264 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 265 err = bpf_percpu_hash_update(map, key, value, flags); 266 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 267 err = bpf_percpu_array_update(map, key, value, flags); 268 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 269 err = bpf_percpu_cgroup_storage_update(map, key, value, 270 flags); 271 } else if (IS_FD_ARRAY(map)) { 272 err = bpf_fd_array_map_update_elem(map, map_file, key, value, 273 flags); 274 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { 275 err = bpf_fd_htab_map_update_elem(map, map_file, key, value, 276 flags); 277 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 278 /* rcu_read_lock() is not needed */ 279 err = bpf_fd_reuseport_array_update_elem(map, key, value, 280 flags); 281 } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 282 map->map_type == BPF_MAP_TYPE_STACK || 283 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { 284 err = map->ops->map_push_elem(map, value, flags); 285 } else { 286 err = bpf_obj_pin_uptrs(map->record, value); 287 if (!err) { 288 rcu_read_lock(); 289 err = map->ops->map_update_elem(map, key, value, flags); 290 rcu_read_unlock(); 291 if (err) 292 bpf_obj_unpin_uptrs(map->record, value); 293 } 294 } 295 bpf_enable_instrumentation(); 296 297 return err; 298 } 299 300 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, 301 __u64 flags) 302 { 303 void *ptr; 304 int err; 305 306 if (bpf_map_is_offloaded(map)) 307 return bpf_map_offload_lookup_elem(map, key, value); 308 309 bpf_disable_instrumentation(); 310 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 311 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 312 err = bpf_percpu_hash_copy(map, key, value); 313 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 314 err = bpf_percpu_array_copy(map, key, value); 315 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 316 err = bpf_percpu_cgroup_storage_copy(map, key, value); 317 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { 318 err = bpf_stackmap_copy(map, key, value); 319 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) { 320 err = bpf_fd_array_map_lookup_elem(map, key, value); 321 } else if (IS_FD_HASH(map)) { 322 err = bpf_fd_htab_map_lookup_elem(map, key, value); 323 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 324 err = bpf_fd_reuseport_array_lookup_elem(map, key, value); 325 } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 326 map->map_type == BPF_MAP_TYPE_STACK || 327 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { 328 err = map->ops->map_peek_elem(map, value); 329 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 330 /* struct_ops map requires directly updating "value" */ 331 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value); 332 } else { 333 rcu_read_lock(); 334 if (map->ops->map_lookup_elem_sys_only) 335 ptr = map->ops->map_lookup_elem_sys_only(map, key); 336 else 337 ptr = map->ops->map_lookup_elem(map, key); 338 if (IS_ERR(ptr)) { 339 err = PTR_ERR(ptr); 340 } else if (!ptr) { 341 err = -ENOENT; 342 } else { 343 err = 0; 344 if (flags & BPF_F_LOCK) 345 /* lock 'ptr' and copy everything but lock */ 346 copy_map_value_locked(map, value, ptr, true); 347 else 348 copy_map_value(map, value, ptr); 349 /* mask lock and timer, since value wasn't zero inited */ 350 check_and_init_map_value(map, value); 351 } 352 rcu_read_unlock(); 353 } 354 355 bpf_enable_instrumentation(); 356 357 return err; 358 } 359 360 /* Please, do not use this function outside from the map creation path 361 * (e.g. in map update path) without taking care of setting the active 362 * memory cgroup (see at bpf_map_kmalloc_node() for example). 363 */ 364 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) 365 { 366 /* We really just want to fail instead of triggering OOM killer 367 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, 368 * which is used for lower order allocation requests. 369 * 370 * It has been observed that higher order allocation requests done by 371 * vmalloc with __GFP_NORETRY being set might fail due to not trying 372 * to reclaim memory from the page cache, thus we set 373 * __GFP_RETRY_MAYFAIL to avoid such situations. 374 */ 375 376 gfp_t gfp = bpf_memcg_flags(__GFP_NOWARN | __GFP_ZERO); 377 unsigned int flags = 0; 378 unsigned long align = 1; 379 void *area; 380 381 if (size >= SIZE_MAX) 382 return NULL; 383 384 /* kmalloc()'ed memory can't be mmap()'ed */ 385 if (mmapable) { 386 BUG_ON(!PAGE_ALIGNED(size)); 387 align = SHMLBA; 388 flags = VM_USERMAP; 389 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { 390 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY, 391 numa_node); 392 if (area != NULL) 393 return area; 394 } 395 396 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 397 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL, 398 flags, numa_node, __builtin_return_address(0)); 399 } 400 401 void *bpf_map_area_alloc(u64 size, int numa_node) 402 { 403 return __bpf_map_area_alloc(size, numa_node, false); 404 } 405 406 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node) 407 { 408 return __bpf_map_area_alloc(size, numa_node, true); 409 } 410 411 void bpf_map_area_free(void *area) 412 { 413 kvfree(area); 414 } 415 416 static u32 bpf_map_flags_retain_permanent(u32 flags) 417 { 418 /* Some map creation flags are not tied to the map object but 419 * rather to the map fd instead, so they have no meaning upon 420 * map object inspection since multiple file descriptors with 421 * different (access) properties can exist here. Thus, given 422 * this has zero meaning for the map itself, lets clear these 423 * from here. 424 */ 425 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); 426 } 427 428 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) 429 { 430 map->map_type = attr->map_type; 431 map->key_size = attr->key_size; 432 map->value_size = attr->value_size; 433 map->max_entries = attr->max_entries; 434 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); 435 map->numa_node = bpf_map_attr_numa_node(attr); 436 map->map_extra = attr->map_extra; 437 } 438 439 static int bpf_map_alloc_id(struct bpf_map *map) 440 { 441 int id; 442 443 idr_preload(GFP_KERNEL); 444 spin_lock_bh(&map_idr_lock); 445 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); 446 if (id > 0) 447 map->id = id; 448 spin_unlock_bh(&map_idr_lock); 449 idr_preload_end(); 450 451 if (WARN_ON_ONCE(!id)) 452 return -ENOSPC; 453 454 return id > 0 ? 0 : id; 455 } 456 457 void bpf_map_free_id(struct bpf_map *map) 458 { 459 unsigned long flags; 460 461 /* Offloaded maps are removed from the IDR store when their device 462 * disappears - even if someone holds an fd to them they are unusable, 463 * the memory is gone, all ops will fail; they are simply waiting for 464 * refcnt to drop to be freed. 465 */ 466 if (!map->id) 467 return; 468 469 spin_lock_irqsave(&map_idr_lock, flags); 470 471 idr_remove(&map_idr, map->id); 472 map->id = 0; 473 474 spin_unlock_irqrestore(&map_idr_lock, flags); 475 } 476 477 #ifdef CONFIG_MEMCG 478 static void bpf_map_save_memcg(struct bpf_map *map) 479 { 480 /* Currently if a map is created by a process belonging to the root 481 * memory cgroup, get_obj_cgroup_from_current() will return NULL. 482 * So we have to check map->objcg for being NULL each time it's 483 * being used. 484 */ 485 if (memcg_bpf_enabled()) 486 map->objcg = get_obj_cgroup_from_current(); 487 } 488 489 static void bpf_map_release_memcg(struct bpf_map *map) 490 { 491 if (map->objcg) 492 obj_cgroup_put(map->objcg); 493 } 494 495 static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map) 496 { 497 if (map->objcg) 498 return get_mem_cgroup_from_objcg(map->objcg); 499 500 return root_mem_cgroup; 501 } 502 503 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, 504 int node) 505 { 506 struct mem_cgroup *memcg, *old_memcg; 507 void *ptr; 508 509 memcg = bpf_map_get_memcg(map); 510 old_memcg = set_active_memcg(memcg); 511 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node); 512 set_active_memcg(old_memcg); 513 mem_cgroup_put(memcg); 514 515 return ptr; 516 } 517 518 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags) 519 { 520 struct mem_cgroup *memcg, *old_memcg; 521 void *ptr; 522 523 memcg = bpf_map_get_memcg(map); 524 old_memcg = set_active_memcg(memcg); 525 ptr = kzalloc(size, flags | __GFP_ACCOUNT); 526 set_active_memcg(old_memcg); 527 mem_cgroup_put(memcg); 528 529 return ptr; 530 } 531 532 void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size, 533 gfp_t flags) 534 { 535 struct mem_cgroup *memcg, *old_memcg; 536 void *ptr; 537 538 memcg = bpf_map_get_memcg(map); 539 old_memcg = set_active_memcg(memcg); 540 ptr = kvcalloc(n, size, flags | __GFP_ACCOUNT); 541 set_active_memcg(old_memcg); 542 mem_cgroup_put(memcg); 543 544 return ptr; 545 } 546 547 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, 548 size_t align, gfp_t flags) 549 { 550 struct mem_cgroup *memcg, *old_memcg; 551 void __percpu *ptr; 552 553 memcg = bpf_map_get_memcg(map); 554 old_memcg = set_active_memcg(memcg); 555 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT); 556 set_active_memcg(old_memcg); 557 mem_cgroup_put(memcg); 558 559 return ptr; 560 } 561 562 #else 563 static void bpf_map_save_memcg(struct bpf_map *map) 564 { 565 } 566 567 static void bpf_map_release_memcg(struct bpf_map *map) 568 { 569 } 570 #endif 571 572 int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid, 573 unsigned long nr_pages, struct page **pages) 574 { 575 unsigned long i, j; 576 struct page *pg; 577 int ret = 0; 578 #ifdef CONFIG_MEMCG 579 struct mem_cgroup *memcg, *old_memcg; 580 581 memcg = bpf_map_get_memcg(map); 582 old_memcg = set_active_memcg(memcg); 583 #endif 584 for (i = 0; i < nr_pages; i++) { 585 pg = alloc_pages_node(nid, gfp | __GFP_ACCOUNT, 0); 586 587 if (pg) { 588 pages[i] = pg; 589 continue; 590 } 591 for (j = 0; j < i; j++) 592 __free_page(pages[j]); 593 ret = -ENOMEM; 594 break; 595 } 596 597 #ifdef CONFIG_MEMCG 598 set_active_memcg(old_memcg); 599 mem_cgroup_put(memcg); 600 #endif 601 return ret; 602 } 603 604 605 static int btf_field_cmp(const void *a, const void *b) 606 { 607 const struct btf_field *f1 = a, *f2 = b; 608 609 if (f1->offset < f2->offset) 610 return -1; 611 else if (f1->offset > f2->offset) 612 return 1; 613 return 0; 614 } 615 616 struct btf_field *btf_record_find(const struct btf_record *rec, u32 offset, 617 u32 field_mask) 618 { 619 struct btf_field *field; 620 621 if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & field_mask)) 622 return NULL; 623 field = bsearch(&offset, rec->fields, rec->cnt, sizeof(rec->fields[0]), btf_field_cmp); 624 if (!field || !(field->type & field_mask)) 625 return NULL; 626 return field; 627 } 628 629 void btf_record_free(struct btf_record *rec) 630 { 631 int i; 632 633 if (IS_ERR_OR_NULL(rec)) 634 return; 635 for (i = 0; i < rec->cnt; i++) { 636 switch (rec->fields[i].type) { 637 case BPF_KPTR_UNREF: 638 case BPF_KPTR_REF: 639 case BPF_KPTR_PERCPU: 640 case BPF_UPTR: 641 if (rec->fields[i].kptr.module) 642 module_put(rec->fields[i].kptr.module); 643 if (btf_is_kernel(rec->fields[i].kptr.btf)) 644 btf_put(rec->fields[i].kptr.btf); 645 break; 646 case BPF_LIST_HEAD: 647 case BPF_LIST_NODE: 648 case BPF_RB_ROOT: 649 case BPF_RB_NODE: 650 case BPF_SPIN_LOCK: 651 case BPF_TIMER: 652 case BPF_REFCOUNT: 653 case BPF_WORKQUEUE: 654 /* Nothing to release */ 655 break; 656 default: 657 WARN_ON_ONCE(1); 658 continue; 659 } 660 } 661 kfree(rec); 662 } 663 664 void bpf_map_free_record(struct bpf_map *map) 665 { 666 btf_record_free(map->record); 667 map->record = NULL; 668 } 669 670 struct btf_record *btf_record_dup(const struct btf_record *rec) 671 { 672 const struct btf_field *fields; 673 struct btf_record *new_rec; 674 int ret, size, i; 675 676 if (IS_ERR_OR_NULL(rec)) 677 return NULL; 678 size = offsetof(struct btf_record, fields[rec->cnt]); 679 new_rec = kmemdup(rec, size, GFP_KERNEL | __GFP_NOWARN); 680 if (!new_rec) 681 return ERR_PTR(-ENOMEM); 682 /* Do a deep copy of the btf_record */ 683 fields = rec->fields; 684 new_rec->cnt = 0; 685 for (i = 0; i < rec->cnt; i++) { 686 switch (fields[i].type) { 687 case BPF_KPTR_UNREF: 688 case BPF_KPTR_REF: 689 case BPF_KPTR_PERCPU: 690 case BPF_UPTR: 691 if (btf_is_kernel(fields[i].kptr.btf)) 692 btf_get(fields[i].kptr.btf); 693 if (fields[i].kptr.module && !try_module_get(fields[i].kptr.module)) { 694 ret = -ENXIO; 695 goto free; 696 } 697 break; 698 case BPF_LIST_HEAD: 699 case BPF_LIST_NODE: 700 case BPF_RB_ROOT: 701 case BPF_RB_NODE: 702 case BPF_SPIN_LOCK: 703 case BPF_TIMER: 704 case BPF_REFCOUNT: 705 case BPF_WORKQUEUE: 706 /* Nothing to acquire */ 707 break; 708 default: 709 ret = -EFAULT; 710 WARN_ON_ONCE(1); 711 goto free; 712 } 713 new_rec->cnt++; 714 } 715 return new_rec; 716 free: 717 btf_record_free(new_rec); 718 return ERR_PTR(ret); 719 } 720 721 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b) 722 { 723 bool a_has_fields = !IS_ERR_OR_NULL(rec_a), b_has_fields = !IS_ERR_OR_NULL(rec_b); 724 int size; 725 726 if (!a_has_fields && !b_has_fields) 727 return true; 728 if (a_has_fields != b_has_fields) 729 return false; 730 if (rec_a->cnt != rec_b->cnt) 731 return false; 732 size = offsetof(struct btf_record, fields[rec_a->cnt]); 733 /* btf_parse_fields uses kzalloc to allocate a btf_record, so unused 734 * members are zeroed out. So memcmp is safe to do without worrying 735 * about padding/unused fields. 736 * 737 * While spin_lock, timer, and kptr have no relation to map BTF, 738 * list_head metadata is specific to map BTF, the btf and value_rec 739 * members in particular. btf is the map BTF, while value_rec points to 740 * btf_record in that map BTF. 741 * 742 * So while by default, we don't rely on the map BTF (which the records 743 * were parsed from) matching for both records, which is not backwards 744 * compatible, in case list_head is part of it, we implicitly rely on 745 * that by way of depending on memcmp succeeding for it. 746 */ 747 return !memcmp(rec_a, rec_b, size); 748 } 749 750 void bpf_obj_free_timer(const struct btf_record *rec, void *obj) 751 { 752 if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_TIMER))) 753 return; 754 bpf_timer_cancel_and_free(obj + rec->timer_off); 755 } 756 757 void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj) 758 { 759 if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_WORKQUEUE))) 760 return; 761 bpf_wq_cancel_and_free(obj + rec->wq_off); 762 } 763 764 void bpf_obj_free_fields(const struct btf_record *rec, void *obj) 765 { 766 const struct btf_field *fields; 767 int i; 768 769 if (IS_ERR_OR_NULL(rec)) 770 return; 771 fields = rec->fields; 772 for (i = 0; i < rec->cnt; i++) { 773 struct btf_struct_meta *pointee_struct_meta; 774 const struct btf_field *field = &fields[i]; 775 void *field_ptr = obj + field->offset; 776 void *xchgd_field; 777 778 switch (fields[i].type) { 779 case BPF_SPIN_LOCK: 780 break; 781 case BPF_TIMER: 782 bpf_timer_cancel_and_free(field_ptr); 783 break; 784 case BPF_WORKQUEUE: 785 bpf_wq_cancel_and_free(field_ptr); 786 break; 787 case BPF_KPTR_UNREF: 788 WRITE_ONCE(*(u64 *)field_ptr, 0); 789 break; 790 case BPF_KPTR_REF: 791 case BPF_KPTR_PERCPU: 792 xchgd_field = (void *)xchg((unsigned long *)field_ptr, 0); 793 if (!xchgd_field) 794 break; 795 796 if (!btf_is_kernel(field->kptr.btf)) { 797 pointee_struct_meta = btf_find_struct_meta(field->kptr.btf, 798 field->kptr.btf_id); 799 __bpf_obj_drop_impl(xchgd_field, pointee_struct_meta ? 800 pointee_struct_meta->record : NULL, 801 fields[i].type == BPF_KPTR_PERCPU); 802 } else { 803 field->kptr.dtor(xchgd_field); 804 } 805 break; 806 case BPF_UPTR: 807 /* The caller ensured that no one is using the uptr */ 808 unpin_uptr_kaddr(*(void **)field_ptr); 809 break; 810 case BPF_LIST_HEAD: 811 if (WARN_ON_ONCE(rec->spin_lock_off < 0)) 812 continue; 813 bpf_list_head_free(field, field_ptr, obj + rec->spin_lock_off); 814 break; 815 case BPF_RB_ROOT: 816 if (WARN_ON_ONCE(rec->spin_lock_off < 0)) 817 continue; 818 bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off); 819 break; 820 case BPF_LIST_NODE: 821 case BPF_RB_NODE: 822 case BPF_REFCOUNT: 823 break; 824 default: 825 WARN_ON_ONCE(1); 826 continue; 827 } 828 } 829 } 830 831 static void bpf_map_free(struct bpf_map *map) 832 { 833 struct btf_record *rec = map->record; 834 struct btf *btf = map->btf; 835 836 /* implementation dependent freeing. Disabling migration to simplify 837 * the free of values or special fields allocated from bpf memory 838 * allocator. 839 */ 840 migrate_disable(); 841 map->ops->map_free(map); 842 migrate_enable(); 843 844 /* Delay freeing of btf_record for maps, as map_free 845 * callback usually needs access to them. It is better to do it here 846 * than require each callback to do the free itself manually. 847 * 848 * Note that the btf_record stashed in map->inner_map_meta->record was 849 * already freed using the map_free callback for map in map case which 850 * eventually calls bpf_map_free_meta, since inner_map_meta is only a 851 * template bpf_map struct used during verification. 852 */ 853 btf_record_free(rec); 854 /* Delay freeing of btf for maps, as map_free callback may need 855 * struct_meta info which will be freed with btf_put(). 856 */ 857 btf_put(btf); 858 } 859 860 /* called from workqueue */ 861 static void bpf_map_free_deferred(struct work_struct *work) 862 { 863 struct bpf_map *map = container_of(work, struct bpf_map, work); 864 865 security_bpf_map_free(map); 866 bpf_map_release_memcg(map); 867 bpf_map_free(map); 868 } 869 870 static void bpf_map_put_uref(struct bpf_map *map) 871 { 872 if (atomic64_dec_and_test(&map->usercnt)) { 873 if (map->ops->map_release_uref) 874 map->ops->map_release_uref(map); 875 } 876 } 877 878 static void bpf_map_free_in_work(struct bpf_map *map) 879 { 880 INIT_WORK(&map->work, bpf_map_free_deferred); 881 /* Avoid spawning kworkers, since they all might contend 882 * for the same mutex like slab_mutex. 883 */ 884 queue_work(system_unbound_wq, &map->work); 885 } 886 887 static void bpf_map_free_rcu_gp(struct rcu_head *rcu) 888 { 889 bpf_map_free_in_work(container_of(rcu, struct bpf_map, rcu)); 890 } 891 892 static void bpf_map_free_mult_rcu_gp(struct rcu_head *rcu) 893 { 894 if (rcu_trace_implies_rcu_gp()) 895 bpf_map_free_rcu_gp(rcu); 896 else 897 call_rcu(rcu, bpf_map_free_rcu_gp); 898 } 899 900 /* decrement map refcnt and schedule it for freeing via workqueue 901 * (underlying map implementation ops->map_free() might sleep) 902 */ 903 void bpf_map_put(struct bpf_map *map) 904 { 905 if (atomic64_dec_and_test(&map->refcnt)) { 906 /* bpf_map_free_id() must be called first */ 907 bpf_map_free_id(map); 908 909 WARN_ON_ONCE(atomic64_read(&map->sleepable_refcnt)); 910 if (READ_ONCE(map->free_after_mult_rcu_gp)) 911 call_rcu_tasks_trace(&map->rcu, bpf_map_free_mult_rcu_gp); 912 else if (READ_ONCE(map->free_after_rcu_gp)) 913 call_rcu(&map->rcu, bpf_map_free_rcu_gp); 914 else 915 bpf_map_free_in_work(map); 916 } 917 } 918 EXPORT_SYMBOL_GPL(bpf_map_put); 919 920 void bpf_map_put_with_uref(struct bpf_map *map) 921 { 922 bpf_map_put_uref(map); 923 bpf_map_put(map); 924 } 925 926 static int bpf_map_release(struct inode *inode, struct file *filp) 927 { 928 struct bpf_map *map = filp->private_data; 929 930 if (map->ops->map_release) 931 map->ops->map_release(map, filp); 932 933 bpf_map_put_with_uref(map); 934 return 0; 935 } 936 937 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) 938 { 939 fmode_t mode = fd_file(f)->f_mode; 940 941 /* Our file permissions may have been overridden by global 942 * map permissions facing syscall side. 943 */ 944 if (READ_ONCE(map->frozen)) 945 mode &= ~FMODE_CAN_WRITE; 946 return mode; 947 } 948 949 #ifdef CONFIG_PROC_FS 950 /* Show the memory usage of a bpf map */ 951 static u64 bpf_map_memory_usage(const struct bpf_map *map) 952 { 953 return map->ops->map_mem_usage(map); 954 } 955 956 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) 957 { 958 struct bpf_map *map = filp->private_data; 959 u32 type = 0, jited = 0; 960 961 if (map_type_contains_progs(map)) { 962 spin_lock(&map->owner.lock); 963 type = map->owner.type; 964 jited = map->owner.jited; 965 spin_unlock(&map->owner.lock); 966 } 967 968 seq_printf(m, 969 "map_type:\t%u\n" 970 "key_size:\t%u\n" 971 "value_size:\t%u\n" 972 "max_entries:\t%u\n" 973 "map_flags:\t%#x\n" 974 "map_extra:\t%#llx\n" 975 "memlock:\t%llu\n" 976 "map_id:\t%u\n" 977 "frozen:\t%u\n", 978 map->map_type, 979 map->key_size, 980 map->value_size, 981 map->max_entries, 982 map->map_flags, 983 (unsigned long long)map->map_extra, 984 bpf_map_memory_usage(map), 985 map->id, 986 READ_ONCE(map->frozen)); 987 if (type) { 988 seq_printf(m, "owner_prog_type:\t%u\n", type); 989 seq_printf(m, "owner_jited:\t%u\n", jited); 990 } 991 } 992 #endif 993 994 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, 995 loff_t *ppos) 996 { 997 /* We need this handler such that alloc_file() enables 998 * f_mode with FMODE_CAN_READ. 999 */ 1000 return -EINVAL; 1001 } 1002 1003 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, 1004 size_t siz, loff_t *ppos) 1005 { 1006 /* We need this handler such that alloc_file() enables 1007 * f_mode with FMODE_CAN_WRITE. 1008 */ 1009 return -EINVAL; 1010 } 1011 1012 /* called for any extra memory-mapped regions (except initial) */ 1013 static void bpf_map_mmap_open(struct vm_area_struct *vma) 1014 { 1015 struct bpf_map *map = vma->vm_file->private_data; 1016 1017 if (vma->vm_flags & VM_MAYWRITE) 1018 bpf_map_write_active_inc(map); 1019 } 1020 1021 /* called for all unmapped memory region (including initial) */ 1022 static void bpf_map_mmap_close(struct vm_area_struct *vma) 1023 { 1024 struct bpf_map *map = vma->vm_file->private_data; 1025 1026 if (vma->vm_flags & VM_MAYWRITE) 1027 bpf_map_write_active_dec(map); 1028 } 1029 1030 static const struct vm_operations_struct bpf_map_default_vmops = { 1031 .open = bpf_map_mmap_open, 1032 .close = bpf_map_mmap_close, 1033 }; 1034 1035 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma) 1036 { 1037 struct bpf_map *map = filp->private_data; 1038 int err = 0; 1039 1040 if (!map->ops->map_mmap || !IS_ERR_OR_NULL(map->record)) 1041 return -ENOTSUPP; 1042 1043 if (!(vma->vm_flags & VM_SHARED)) 1044 return -EINVAL; 1045 1046 mutex_lock(&map->freeze_mutex); 1047 1048 if (vma->vm_flags & VM_WRITE) { 1049 if (map->frozen) { 1050 err = -EPERM; 1051 goto out; 1052 } 1053 /* map is meant to be read-only, so do not allow mapping as 1054 * writable, because it's possible to leak a writable page 1055 * reference and allows user-space to still modify it after 1056 * freezing, while verifier will assume contents do not change 1057 */ 1058 if (map->map_flags & BPF_F_RDONLY_PROG) { 1059 err = -EACCES; 1060 goto out; 1061 } 1062 bpf_map_write_active_inc(map); 1063 } 1064 out: 1065 mutex_unlock(&map->freeze_mutex); 1066 if (err) 1067 return err; 1068 1069 /* set default open/close callbacks */ 1070 vma->vm_ops = &bpf_map_default_vmops; 1071 vma->vm_private_data = map; 1072 vm_flags_clear(vma, VM_MAYEXEC); 1073 /* If mapping is read-only, then disallow potentially re-mapping with 1074 * PROT_WRITE by dropping VM_MAYWRITE flag. This VM_MAYWRITE clearing 1075 * means that as far as BPF map's memory-mapped VMAs are concerned, 1076 * VM_WRITE and VM_MAYWRITE and equivalent, if one of them is set, 1077 * both should be set, so we can forget about VM_MAYWRITE and always 1078 * check just VM_WRITE 1079 */ 1080 if (!(vma->vm_flags & VM_WRITE)) 1081 vm_flags_clear(vma, VM_MAYWRITE); 1082 1083 err = map->ops->map_mmap(map, vma); 1084 if (err) { 1085 if (vma->vm_flags & VM_WRITE) 1086 bpf_map_write_active_dec(map); 1087 } 1088 1089 return err; 1090 } 1091 1092 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts) 1093 { 1094 struct bpf_map *map = filp->private_data; 1095 1096 if (map->ops->map_poll) 1097 return map->ops->map_poll(map, filp, pts); 1098 1099 return EPOLLERR; 1100 } 1101 1102 static unsigned long bpf_get_unmapped_area(struct file *filp, unsigned long addr, 1103 unsigned long len, unsigned long pgoff, 1104 unsigned long flags) 1105 { 1106 struct bpf_map *map = filp->private_data; 1107 1108 if (map->ops->map_get_unmapped_area) 1109 return map->ops->map_get_unmapped_area(filp, addr, len, pgoff, flags); 1110 #ifdef CONFIG_MMU 1111 return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags); 1112 #else 1113 return addr; 1114 #endif 1115 } 1116 1117 const struct file_operations bpf_map_fops = { 1118 #ifdef CONFIG_PROC_FS 1119 .show_fdinfo = bpf_map_show_fdinfo, 1120 #endif 1121 .release = bpf_map_release, 1122 .read = bpf_dummy_read, 1123 .write = bpf_dummy_write, 1124 .mmap = bpf_map_mmap, 1125 .poll = bpf_map_poll, 1126 .get_unmapped_area = bpf_get_unmapped_area, 1127 }; 1128 1129 int bpf_map_new_fd(struct bpf_map *map, int flags) 1130 { 1131 int ret; 1132 1133 ret = security_bpf_map(map, OPEN_FMODE(flags)); 1134 if (ret < 0) 1135 return ret; 1136 1137 return anon_inode_getfd("bpf-map", &bpf_map_fops, map, 1138 flags | O_CLOEXEC); 1139 } 1140 1141 int bpf_get_file_flag(int flags) 1142 { 1143 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) 1144 return -EINVAL; 1145 if (flags & BPF_F_RDONLY) 1146 return O_RDONLY; 1147 if (flags & BPF_F_WRONLY) 1148 return O_WRONLY; 1149 return O_RDWR; 1150 } 1151 1152 /* helper macro to check that unused fields 'union bpf_attr' are zero */ 1153 #define CHECK_ATTR(CMD) \ 1154 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ 1155 sizeof(attr->CMD##_LAST_FIELD), 0, \ 1156 sizeof(*attr) - \ 1157 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ 1158 sizeof(attr->CMD##_LAST_FIELD)) != NULL 1159 1160 /* dst and src must have at least "size" number of bytes. 1161 * Return strlen on success and < 0 on error. 1162 */ 1163 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size) 1164 { 1165 const char *end = src + size; 1166 const char *orig_src = src; 1167 1168 memset(dst, 0, size); 1169 /* Copy all isalnum(), '_' and '.' chars. */ 1170 while (src < end && *src) { 1171 if (!isalnum(*src) && 1172 *src != '_' && *src != '.') 1173 return -EINVAL; 1174 *dst++ = *src++; 1175 } 1176 1177 /* No '\0' found in "size" number of bytes */ 1178 if (src == end) 1179 return -EINVAL; 1180 1181 return src - orig_src; 1182 } 1183 1184 int map_check_no_btf(const struct bpf_map *map, 1185 const struct btf *btf, 1186 const struct btf_type *key_type, 1187 const struct btf_type *value_type) 1188 { 1189 return -ENOTSUPP; 1190 } 1191 1192 static int map_check_btf(struct bpf_map *map, struct bpf_token *token, 1193 const struct btf *btf, u32 btf_key_id, u32 btf_value_id) 1194 { 1195 const struct btf_type *key_type, *value_type; 1196 u32 key_size, value_size; 1197 int ret = 0; 1198 1199 /* Some maps allow key to be unspecified. */ 1200 if (btf_key_id) { 1201 key_type = btf_type_id_size(btf, &btf_key_id, &key_size); 1202 if (!key_type || key_size != map->key_size) 1203 return -EINVAL; 1204 } else { 1205 key_type = btf_type_by_id(btf, 0); 1206 if (!map->ops->map_check_btf) 1207 return -EINVAL; 1208 } 1209 1210 value_type = btf_type_id_size(btf, &btf_value_id, &value_size); 1211 if (!value_type || value_size != map->value_size) 1212 return -EINVAL; 1213 1214 map->record = btf_parse_fields(btf, value_type, 1215 BPF_SPIN_LOCK | BPF_TIMER | BPF_KPTR | BPF_LIST_HEAD | 1216 BPF_RB_ROOT | BPF_REFCOUNT | BPF_WORKQUEUE | BPF_UPTR, 1217 map->value_size); 1218 if (!IS_ERR_OR_NULL(map->record)) { 1219 int i; 1220 1221 if (!bpf_token_capable(token, CAP_BPF)) { 1222 ret = -EPERM; 1223 goto free_map_tab; 1224 } 1225 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) { 1226 ret = -EACCES; 1227 goto free_map_tab; 1228 } 1229 for (i = 0; i < sizeof(map->record->field_mask) * 8; i++) { 1230 switch (map->record->field_mask & (1 << i)) { 1231 case 0: 1232 continue; 1233 case BPF_SPIN_LOCK: 1234 if (map->map_type != BPF_MAP_TYPE_HASH && 1235 map->map_type != BPF_MAP_TYPE_ARRAY && 1236 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && 1237 map->map_type != BPF_MAP_TYPE_SK_STORAGE && 1238 map->map_type != BPF_MAP_TYPE_INODE_STORAGE && 1239 map->map_type != BPF_MAP_TYPE_TASK_STORAGE && 1240 map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) { 1241 ret = -EOPNOTSUPP; 1242 goto free_map_tab; 1243 } 1244 break; 1245 case BPF_TIMER: 1246 case BPF_WORKQUEUE: 1247 if (map->map_type != BPF_MAP_TYPE_HASH && 1248 map->map_type != BPF_MAP_TYPE_LRU_HASH && 1249 map->map_type != BPF_MAP_TYPE_ARRAY) { 1250 ret = -EOPNOTSUPP; 1251 goto free_map_tab; 1252 } 1253 break; 1254 case BPF_KPTR_UNREF: 1255 case BPF_KPTR_REF: 1256 case BPF_KPTR_PERCPU: 1257 case BPF_REFCOUNT: 1258 if (map->map_type != BPF_MAP_TYPE_HASH && 1259 map->map_type != BPF_MAP_TYPE_PERCPU_HASH && 1260 map->map_type != BPF_MAP_TYPE_LRU_HASH && 1261 map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH && 1262 map->map_type != BPF_MAP_TYPE_ARRAY && 1263 map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY && 1264 map->map_type != BPF_MAP_TYPE_SK_STORAGE && 1265 map->map_type != BPF_MAP_TYPE_INODE_STORAGE && 1266 map->map_type != BPF_MAP_TYPE_TASK_STORAGE && 1267 map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) { 1268 ret = -EOPNOTSUPP; 1269 goto free_map_tab; 1270 } 1271 break; 1272 case BPF_UPTR: 1273 if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) { 1274 ret = -EOPNOTSUPP; 1275 goto free_map_tab; 1276 } 1277 break; 1278 case BPF_LIST_HEAD: 1279 case BPF_RB_ROOT: 1280 if (map->map_type != BPF_MAP_TYPE_HASH && 1281 map->map_type != BPF_MAP_TYPE_LRU_HASH && 1282 map->map_type != BPF_MAP_TYPE_ARRAY) { 1283 ret = -EOPNOTSUPP; 1284 goto free_map_tab; 1285 } 1286 break; 1287 default: 1288 /* Fail if map_type checks are missing for a field type */ 1289 ret = -EOPNOTSUPP; 1290 goto free_map_tab; 1291 } 1292 } 1293 } 1294 1295 ret = btf_check_and_fixup_fields(btf, map->record); 1296 if (ret < 0) 1297 goto free_map_tab; 1298 1299 if (map->ops->map_check_btf) { 1300 ret = map->ops->map_check_btf(map, btf, key_type, value_type); 1301 if (ret < 0) 1302 goto free_map_tab; 1303 } 1304 1305 return ret; 1306 free_map_tab: 1307 bpf_map_free_record(map); 1308 return ret; 1309 } 1310 1311 static bool bpf_net_capable(void) 1312 { 1313 return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN); 1314 } 1315 1316 #define BPF_MAP_CREATE_LAST_FIELD map_token_fd 1317 /* called via syscall */ 1318 static int map_create(union bpf_attr *attr) 1319 { 1320 const struct bpf_map_ops *ops; 1321 struct bpf_token *token = NULL; 1322 int numa_node = bpf_map_attr_numa_node(attr); 1323 u32 map_type = attr->map_type; 1324 struct bpf_map *map; 1325 bool token_flag; 1326 int f_flags; 1327 int err; 1328 1329 err = CHECK_ATTR(BPF_MAP_CREATE); 1330 if (err) 1331 return -EINVAL; 1332 1333 /* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it 1334 * to avoid per-map type checks tripping on unknown flag 1335 */ 1336 token_flag = attr->map_flags & BPF_F_TOKEN_FD; 1337 attr->map_flags &= ~BPF_F_TOKEN_FD; 1338 1339 if (attr->btf_vmlinux_value_type_id) { 1340 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS || 1341 attr->btf_key_type_id || attr->btf_value_type_id) 1342 return -EINVAL; 1343 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) { 1344 return -EINVAL; 1345 } 1346 1347 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER && 1348 attr->map_type != BPF_MAP_TYPE_ARENA && 1349 attr->map_extra != 0) 1350 return -EINVAL; 1351 1352 f_flags = bpf_get_file_flag(attr->map_flags); 1353 if (f_flags < 0) 1354 return f_flags; 1355 1356 if (numa_node != NUMA_NO_NODE && 1357 ((unsigned int)numa_node >= nr_node_ids || 1358 !node_online(numa_node))) 1359 return -EINVAL; 1360 1361 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ 1362 map_type = attr->map_type; 1363 if (map_type >= ARRAY_SIZE(bpf_map_types)) 1364 return -EINVAL; 1365 map_type = array_index_nospec(map_type, ARRAY_SIZE(bpf_map_types)); 1366 ops = bpf_map_types[map_type]; 1367 if (!ops) 1368 return -EINVAL; 1369 1370 if (ops->map_alloc_check) { 1371 err = ops->map_alloc_check(attr); 1372 if (err) 1373 return err; 1374 } 1375 if (attr->map_ifindex) 1376 ops = &bpf_map_offload_ops; 1377 if (!ops->map_mem_usage) 1378 return -EINVAL; 1379 1380 if (token_flag) { 1381 token = bpf_token_get_from_fd(attr->map_token_fd); 1382 if (IS_ERR(token)) 1383 return PTR_ERR(token); 1384 1385 /* if current token doesn't grant map creation permissions, 1386 * then we can't use this token, so ignore it and rely on 1387 * system-wide capabilities checks 1388 */ 1389 if (!bpf_token_allow_cmd(token, BPF_MAP_CREATE) || 1390 !bpf_token_allow_map_type(token, attr->map_type)) { 1391 bpf_token_put(token); 1392 token = NULL; 1393 } 1394 } 1395 1396 err = -EPERM; 1397 1398 /* Intent here is for unprivileged_bpf_disabled to block BPF map 1399 * creation for unprivileged users; other actions depend 1400 * on fd availability and access to bpffs, so are dependent on 1401 * object creation success. Even with unprivileged BPF disabled, 1402 * capability checks are still carried out. 1403 */ 1404 if (sysctl_unprivileged_bpf_disabled && !bpf_token_capable(token, CAP_BPF)) 1405 goto put_token; 1406 1407 /* check privileged map type permissions */ 1408 switch (map_type) { 1409 case BPF_MAP_TYPE_ARRAY: 1410 case BPF_MAP_TYPE_PERCPU_ARRAY: 1411 case BPF_MAP_TYPE_PROG_ARRAY: 1412 case BPF_MAP_TYPE_PERF_EVENT_ARRAY: 1413 case BPF_MAP_TYPE_CGROUP_ARRAY: 1414 case BPF_MAP_TYPE_ARRAY_OF_MAPS: 1415 case BPF_MAP_TYPE_HASH: 1416 case BPF_MAP_TYPE_PERCPU_HASH: 1417 case BPF_MAP_TYPE_HASH_OF_MAPS: 1418 case BPF_MAP_TYPE_RINGBUF: 1419 case BPF_MAP_TYPE_USER_RINGBUF: 1420 case BPF_MAP_TYPE_CGROUP_STORAGE: 1421 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: 1422 /* unprivileged */ 1423 break; 1424 case BPF_MAP_TYPE_SK_STORAGE: 1425 case BPF_MAP_TYPE_INODE_STORAGE: 1426 case BPF_MAP_TYPE_TASK_STORAGE: 1427 case BPF_MAP_TYPE_CGRP_STORAGE: 1428 case BPF_MAP_TYPE_BLOOM_FILTER: 1429 case BPF_MAP_TYPE_LPM_TRIE: 1430 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: 1431 case BPF_MAP_TYPE_STACK_TRACE: 1432 case BPF_MAP_TYPE_QUEUE: 1433 case BPF_MAP_TYPE_STACK: 1434 case BPF_MAP_TYPE_LRU_HASH: 1435 case BPF_MAP_TYPE_LRU_PERCPU_HASH: 1436 case BPF_MAP_TYPE_STRUCT_OPS: 1437 case BPF_MAP_TYPE_CPUMAP: 1438 case BPF_MAP_TYPE_ARENA: 1439 if (!bpf_token_capable(token, CAP_BPF)) 1440 goto put_token; 1441 break; 1442 case BPF_MAP_TYPE_SOCKMAP: 1443 case BPF_MAP_TYPE_SOCKHASH: 1444 case BPF_MAP_TYPE_DEVMAP: 1445 case BPF_MAP_TYPE_DEVMAP_HASH: 1446 case BPF_MAP_TYPE_XSKMAP: 1447 if (!bpf_token_capable(token, CAP_NET_ADMIN)) 1448 goto put_token; 1449 break; 1450 default: 1451 WARN(1, "unsupported map type %d", map_type); 1452 goto put_token; 1453 } 1454 1455 map = ops->map_alloc(attr); 1456 if (IS_ERR(map)) { 1457 err = PTR_ERR(map); 1458 goto put_token; 1459 } 1460 map->ops = ops; 1461 map->map_type = map_type; 1462 1463 err = bpf_obj_name_cpy(map->name, attr->map_name, 1464 sizeof(attr->map_name)); 1465 if (err < 0) 1466 goto free_map; 1467 1468 atomic64_set(&map->refcnt, 1); 1469 atomic64_set(&map->usercnt, 1); 1470 mutex_init(&map->freeze_mutex); 1471 spin_lock_init(&map->owner.lock); 1472 1473 if (attr->btf_key_type_id || attr->btf_value_type_id || 1474 /* Even the map's value is a kernel's struct, 1475 * the bpf_prog.o must have BTF to begin with 1476 * to figure out the corresponding kernel's 1477 * counter part. Thus, attr->btf_fd has 1478 * to be valid also. 1479 */ 1480 attr->btf_vmlinux_value_type_id) { 1481 struct btf *btf; 1482 1483 btf = btf_get_by_fd(attr->btf_fd); 1484 if (IS_ERR(btf)) { 1485 err = PTR_ERR(btf); 1486 goto free_map; 1487 } 1488 if (btf_is_kernel(btf)) { 1489 btf_put(btf); 1490 err = -EACCES; 1491 goto free_map; 1492 } 1493 map->btf = btf; 1494 1495 if (attr->btf_value_type_id) { 1496 err = map_check_btf(map, token, btf, attr->btf_key_type_id, 1497 attr->btf_value_type_id); 1498 if (err) 1499 goto free_map; 1500 } 1501 1502 map->btf_key_type_id = attr->btf_key_type_id; 1503 map->btf_value_type_id = attr->btf_value_type_id; 1504 map->btf_vmlinux_value_type_id = 1505 attr->btf_vmlinux_value_type_id; 1506 } 1507 1508 err = security_bpf_map_create(map, attr, token); 1509 if (err) 1510 goto free_map_sec; 1511 1512 err = bpf_map_alloc_id(map); 1513 if (err) 1514 goto free_map_sec; 1515 1516 bpf_map_save_memcg(map); 1517 bpf_token_put(token); 1518 1519 err = bpf_map_new_fd(map, f_flags); 1520 if (err < 0) { 1521 /* failed to allocate fd. 1522 * bpf_map_put_with_uref() is needed because the above 1523 * bpf_map_alloc_id() has published the map 1524 * to the userspace and the userspace may 1525 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. 1526 */ 1527 bpf_map_put_with_uref(map); 1528 return err; 1529 } 1530 1531 return err; 1532 1533 free_map_sec: 1534 security_bpf_map_free(map); 1535 free_map: 1536 bpf_map_free(map); 1537 put_token: 1538 bpf_token_put(token); 1539 return err; 1540 } 1541 1542 void bpf_map_inc(struct bpf_map *map) 1543 { 1544 atomic64_inc(&map->refcnt); 1545 } 1546 EXPORT_SYMBOL_GPL(bpf_map_inc); 1547 1548 void bpf_map_inc_with_uref(struct bpf_map *map) 1549 { 1550 atomic64_inc(&map->refcnt); 1551 atomic64_inc(&map->usercnt); 1552 } 1553 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref); 1554 1555 struct bpf_map *bpf_map_get(u32 ufd) 1556 { 1557 CLASS(fd, f)(ufd); 1558 struct bpf_map *map = __bpf_map_get(f); 1559 1560 if (!IS_ERR(map)) 1561 bpf_map_inc(map); 1562 1563 return map; 1564 } 1565 EXPORT_SYMBOL(bpf_map_get); 1566 1567 struct bpf_map *bpf_map_get_with_uref(u32 ufd) 1568 { 1569 CLASS(fd, f)(ufd); 1570 struct bpf_map *map = __bpf_map_get(f); 1571 1572 if (!IS_ERR(map)) 1573 bpf_map_inc_with_uref(map); 1574 1575 return map; 1576 } 1577 1578 /* map_idr_lock should have been held or the map should have been 1579 * protected by rcu read lock. 1580 */ 1581 struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref) 1582 { 1583 int refold; 1584 1585 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0); 1586 if (!refold) 1587 return ERR_PTR(-ENOENT); 1588 if (uref) 1589 atomic64_inc(&map->usercnt); 1590 1591 return map; 1592 } 1593 1594 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map) 1595 { 1596 spin_lock_bh(&map_idr_lock); 1597 map = __bpf_map_inc_not_zero(map, false); 1598 spin_unlock_bh(&map_idr_lock); 1599 1600 return map; 1601 } 1602 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); 1603 1604 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) 1605 { 1606 return -ENOTSUPP; 1607 } 1608 1609 static void *__bpf_copy_key(void __user *ukey, u64 key_size) 1610 { 1611 if (key_size) 1612 return vmemdup_user(ukey, key_size); 1613 1614 if (ukey) 1615 return ERR_PTR(-EINVAL); 1616 1617 return NULL; 1618 } 1619 1620 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size) 1621 { 1622 if (key_size) 1623 return kvmemdup_bpfptr(ukey, key_size); 1624 1625 if (!bpfptr_is_null(ukey)) 1626 return ERR_PTR(-EINVAL); 1627 1628 return NULL; 1629 } 1630 1631 /* last field in 'union bpf_attr' used by this command */ 1632 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags 1633 1634 static int map_lookup_elem(union bpf_attr *attr) 1635 { 1636 void __user *ukey = u64_to_user_ptr(attr->key); 1637 void __user *uvalue = u64_to_user_ptr(attr->value); 1638 struct bpf_map *map; 1639 void *key, *value; 1640 u32 value_size; 1641 int err; 1642 1643 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) 1644 return -EINVAL; 1645 1646 if (attr->flags & ~BPF_F_LOCK) 1647 return -EINVAL; 1648 1649 CLASS(fd, f)(attr->map_fd); 1650 map = __bpf_map_get(f); 1651 if (IS_ERR(map)) 1652 return PTR_ERR(map); 1653 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) 1654 return -EPERM; 1655 1656 if ((attr->flags & BPF_F_LOCK) && 1657 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) 1658 return -EINVAL; 1659 1660 key = __bpf_copy_key(ukey, map->key_size); 1661 if (IS_ERR(key)) 1662 return PTR_ERR(key); 1663 1664 value_size = bpf_map_value_size(map); 1665 1666 err = -ENOMEM; 1667 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); 1668 if (!value) 1669 goto free_key; 1670 1671 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { 1672 if (copy_from_user(value, uvalue, value_size)) 1673 err = -EFAULT; 1674 else 1675 err = bpf_map_copy_value(map, key, value, attr->flags); 1676 goto free_value; 1677 } 1678 1679 err = bpf_map_copy_value(map, key, value, attr->flags); 1680 if (err) 1681 goto free_value; 1682 1683 err = -EFAULT; 1684 if (copy_to_user(uvalue, value, value_size) != 0) 1685 goto free_value; 1686 1687 err = 0; 1688 1689 free_value: 1690 kvfree(value); 1691 free_key: 1692 kvfree(key); 1693 return err; 1694 } 1695 1696 1697 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags 1698 1699 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) 1700 { 1701 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel); 1702 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel); 1703 struct bpf_map *map; 1704 void *key, *value; 1705 u32 value_size; 1706 int err; 1707 1708 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) 1709 return -EINVAL; 1710 1711 CLASS(fd, f)(attr->map_fd); 1712 map = __bpf_map_get(f); 1713 if (IS_ERR(map)) 1714 return PTR_ERR(map); 1715 bpf_map_write_active_inc(map); 1716 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 1717 err = -EPERM; 1718 goto err_put; 1719 } 1720 1721 if ((attr->flags & BPF_F_LOCK) && 1722 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { 1723 err = -EINVAL; 1724 goto err_put; 1725 } 1726 1727 key = ___bpf_copy_key(ukey, map->key_size); 1728 if (IS_ERR(key)) { 1729 err = PTR_ERR(key); 1730 goto err_put; 1731 } 1732 1733 value_size = bpf_map_value_size(map); 1734 value = kvmemdup_bpfptr(uvalue, value_size); 1735 if (IS_ERR(value)) { 1736 err = PTR_ERR(value); 1737 goto free_key; 1738 } 1739 1740 err = bpf_map_update_value(map, fd_file(f), key, value, attr->flags); 1741 if (!err) 1742 maybe_wait_bpf_programs(map); 1743 1744 kvfree(value); 1745 free_key: 1746 kvfree(key); 1747 err_put: 1748 bpf_map_write_active_dec(map); 1749 return err; 1750 } 1751 1752 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key 1753 1754 static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr) 1755 { 1756 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel); 1757 struct bpf_map *map; 1758 void *key; 1759 int err; 1760 1761 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) 1762 return -EINVAL; 1763 1764 CLASS(fd, f)(attr->map_fd); 1765 map = __bpf_map_get(f); 1766 if (IS_ERR(map)) 1767 return PTR_ERR(map); 1768 bpf_map_write_active_inc(map); 1769 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 1770 err = -EPERM; 1771 goto err_put; 1772 } 1773 1774 key = ___bpf_copy_key(ukey, map->key_size); 1775 if (IS_ERR(key)) { 1776 err = PTR_ERR(key); 1777 goto err_put; 1778 } 1779 1780 if (bpf_map_is_offloaded(map)) { 1781 err = bpf_map_offload_delete_elem(map, key); 1782 goto out; 1783 } else if (IS_FD_PROG_ARRAY(map) || 1784 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 1785 /* These maps require sleepable context */ 1786 err = map->ops->map_delete_elem(map, key); 1787 goto out; 1788 } 1789 1790 bpf_disable_instrumentation(); 1791 rcu_read_lock(); 1792 err = map->ops->map_delete_elem(map, key); 1793 rcu_read_unlock(); 1794 bpf_enable_instrumentation(); 1795 if (!err) 1796 maybe_wait_bpf_programs(map); 1797 out: 1798 kvfree(key); 1799 err_put: 1800 bpf_map_write_active_dec(map); 1801 return err; 1802 } 1803 1804 /* last field in 'union bpf_attr' used by this command */ 1805 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key 1806 1807 static int map_get_next_key(union bpf_attr *attr) 1808 { 1809 void __user *ukey = u64_to_user_ptr(attr->key); 1810 void __user *unext_key = u64_to_user_ptr(attr->next_key); 1811 struct bpf_map *map; 1812 void *key, *next_key; 1813 int err; 1814 1815 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) 1816 return -EINVAL; 1817 1818 CLASS(fd, f)(attr->map_fd); 1819 map = __bpf_map_get(f); 1820 if (IS_ERR(map)) 1821 return PTR_ERR(map); 1822 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) 1823 return -EPERM; 1824 1825 if (ukey) { 1826 key = __bpf_copy_key(ukey, map->key_size); 1827 if (IS_ERR(key)) 1828 return PTR_ERR(key); 1829 } else { 1830 key = NULL; 1831 } 1832 1833 err = -ENOMEM; 1834 next_key = kvmalloc(map->key_size, GFP_USER); 1835 if (!next_key) 1836 goto free_key; 1837 1838 if (bpf_map_is_offloaded(map)) { 1839 err = bpf_map_offload_get_next_key(map, key, next_key); 1840 goto out; 1841 } 1842 1843 rcu_read_lock(); 1844 err = map->ops->map_get_next_key(map, key, next_key); 1845 rcu_read_unlock(); 1846 out: 1847 if (err) 1848 goto free_next_key; 1849 1850 err = -EFAULT; 1851 if (copy_to_user(unext_key, next_key, map->key_size) != 0) 1852 goto free_next_key; 1853 1854 err = 0; 1855 1856 free_next_key: 1857 kvfree(next_key); 1858 free_key: 1859 kvfree(key); 1860 return err; 1861 } 1862 1863 int generic_map_delete_batch(struct bpf_map *map, 1864 const union bpf_attr *attr, 1865 union bpf_attr __user *uattr) 1866 { 1867 void __user *keys = u64_to_user_ptr(attr->batch.keys); 1868 u32 cp, max_count; 1869 int err = 0; 1870 void *key; 1871 1872 if (attr->batch.elem_flags & ~BPF_F_LOCK) 1873 return -EINVAL; 1874 1875 if ((attr->batch.elem_flags & BPF_F_LOCK) && 1876 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { 1877 return -EINVAL; 1878 } 1879 1880 max_count = attr->batch.count; 1881 if (!max_count) 1882 return 0; 1883 1884 if (put_user(0, &uattr->batch.count)) 1885 return -EFAULT; 1886 1887 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 1888 if (!key) 1889 return -ENOMEM; 1890 1891 for (cp = 0; cp < max_count; cp++) { 1892 err = -EFAULT; 1893 if (copy_from_user(key, keys + cp * map->key_size, 1894 map->key_size)) 1895 break; 1896 1897 if (bpf_map_is_offloaded(map)) { 1898 err = bpf_map_offload_delete_elem(map, key); 1899 break; 1900 } 1901 1902 bpf_disable_instrumentation(); 1903 rcu_read_lock(); 1904 err = map->ops->map_delete_elem(map, key); 1905 rcu_read_unlock(); 1906 bpf_enable_instrumentation(); 1907 if (err) 1908 break; 1909 cond_resched(); 1910 } 1911 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) 1912 err = -EFAULT; 1913 1914 kvfree(key); 1915 1916 return err; 1917 } 1918 1919 int generic_map_update_batch(struct bpf_map *map, struct file *map_file, 1920 const union bpf_attr *attr, 1921 union bpf_attr __user *uattr) 1922 { 1923 void __user *values = u64_to_user_ptr(attr->batch.values); 1924 void __user *keys = u64_to_user_ptr(attr->batch.keys); 1925 u32 value_size, cp, max_count; 1926 void *key, *value; 1927 int err = 0; 1928 1929 if (attr->batch.elem_flags & ~BPF_F_LOCK) 1930 return -EINVAL; 1931 1932 if ((attr->batch.elem_flags & BPF_F_LOCK) && 1933 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { 1934 return -EINVAL; 1935 } 1936 1937 value_size = bpf_map_value_size(map); 1938 1939 max_count = attr->batch.count; 1940 if (!max_count) 1941 return 0; 1942 1943 if (put_user(0, &uattr->batch.count)) 1944 return -EFAULT; 1945 1946 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 1947 if (!key) 1948 return -ENOMEM; 1949 1950 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); 1951 if (!value) { 1952 kvfree(key); 1953 return -ENOMEM; 1954 } 1955 1956 for (cp = 0; cp < max_count; cp++) { 1957 err = -EFAULT; 1958 if (copy_from_user(key, keys + cp * map->key_size, 1959 map->key_size) || 1960 copy_from_user(value, values + cp * value_size, value_size)) 1961 break; 1962 1963 err = bpf_map_update_value(map, map_file, key, value, 1964 attr->batch.elem_flags); 1965 1966 if (err) 1967 break; 1968 cond_resched(); 1969 } 1970 1971 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) 1972 err = -EFAULT; 1973 1974 kvfree(value); 1975 kvfree(key); 1976 1977 return err; 1978 } 1979 1980 int generic_map_lookup_batch(struct bpf_map *map, 1981 const union bpf_attr *attr, 1982 union bpf_attr __user *uattr) 1983 { 1984 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch); 1985 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); 1986 void __user *values = u64_to_user_ptr(attr->batch.values); 1987 void __user *keys = u64_to_user_ptr(attr->batch.keys); 1988 void *buf, *buf_prevkey, *prev_key, *key, *value; 1989 u32 value_size, cp, max_count; 1990 int err; 1991 1992 if (attr->batch.elem_flags & ~BPF_F_LOCK) 1993 return -EINVAL; 1994 1995 if ((attr->batch.elem_flags & BPF_F_LOCK) && 1996 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) 1997 return -EINVAL; 1998 1999 value_size = bpf_map_value_size(map); 2000 2001 max_count = attr->batch.count; 2002 if (!max_count) 2003 return 0; 2004 2005 if (put_user(0, &uattr->batch.count)) 2006 return -EFAULT; 2007 2008 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 2009 if (!buf_prevkey) 2010 return -ENOMEM; 2011 2012 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN); 2013 if (!buf) { 2014 kvfree(buf_prevkey); 2015 return -ENOMEM; 2016 } 2017 2018 err = -EFAULT; 2019 prev_key = NULL; 2020 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size)) 2021 goto free_buf; 2022 key = buf; 2023 value = key + map->key_size; 2024 if (ubatch) 2025 prev_key = buf_prevkey; 2026 2027 for (cp = 0; cp < max_count;) { 2028 rcu_read_lock(); 2029 err = map->ops->map_get_next_key(map, prev_key, key); 2030 rcu_read_unlock(); 2031 if (err) 2032 break; 2033 err = bpf_map_copy_value(map, key, value, 2034 attr->batch.elem_flags); 2035 2036 if (err == -ENOENT) 2037 goto next_key; 2038 2039 if (err) 2040 goto free_buf; 2041 2042 if (copy_to_user(keys + cp * map->key_size, key, 2043 map->key_size)) { 2044 err = -EFAULT; 2045 goto free_buf; 2046 } 2047 if (copy_to_user(values + cp * value_size, value, value_size)) { 2048 err = -EFAULT; 2049 goto free_buf; 2050 } 2051 2052 cp++; 2053 next_key: 2054 if (!prev_key) 2055 prev_key = buf_prevkey; 2056 2057 swap(prev_key, key); 2058 cond_resched(); 2059 } 2060 2061 if (err == -EFAULT) 2062 goto free_buf; 2063 2064 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) || 2065 (cp && copy_to_user(uobatch, prev_key, map->key_size)))) 2066 err = -EFAULT; 2067 2068 free_buf: 2069 kvfree(buf_prevkey); 2070 kvfree(buf); 2071 return err; 2072 } 2073 2074 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags 2075 2076 static int map_lookup_and_delete_elem(union bpf_attr *attr) 2077 { 2078 void __user *ukey = u64_to_user_ptr(attr->key); 2079 void __user *uvalue = u64_to_user_ptr(attr->value); 2080 struct bpf_map *map; 2081 void *key, *value; 2082 u32 value_size; 2083 int err; 2084 2085 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) 2086 return -EINVAL; 2087 2088 if (attr->flags & ~BPF_F_LOCK) 2089 return -EINVAL; 2090 2091 CLASS(fd, f)(attr->map_fd); 2092 map = __bpf_map_get(f); 2093 if (IS_ERR(map)) 2094 return PTR_ERR(map); 2095 bpf_map_write_active_inc(map); 2096 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) || 2097 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 2098 err = -EPERM; 2099 goto err_put; 2100 } 2101 2102 if (attr->flags && 2103 (map->map_type == BPF_MAP_TYPE_QUEUE || 2104 map->map_type == BPF_MAP_TYPE_STACK)) { 2105 err = -EINVAL; 2106 goto err_put; 2107 } 2108 2109 if ((attr->flags & BPF_F_LOCK) && 2110 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { 2111 err = -EINVAL; 2112 goto err_put; 2113 } 2114 2115 key = __bpf_copy_key(ukey, map->key_size); 2116 if (IS_ERR(key)) { 2117 err = PTR_ERR(key); 2118 goto err_put; 2119 } 2120 2121 value_size = bpf_map_value_size(map); 2122 2123 err = -ENOMEM; 2124 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); 2125 if (!value) 2126 goto free_key; 2127 2128 err = -ENOTSUPP; 2129 if (map->map_type == BPF_MAP_TYPE_QUEUE || 2130 map->map_type == BPF_MAP_TYPE_STACK) { 2131 err = map->ops->map_pop_elem(map, value); 2132 } else if (map->map_type == BPF_MAP_TYPE_HASH || 2133 map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 2134 map->map_type == BPF_MAP_TYPE_LRU_HASH || 2135 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 2136 if (!bpf_map_is_offloaded(map)) { 2137 bpf_disable_instrumentation(); 2138 rcu_read_lock(); 2139 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags); 2140 rcu_read_unlock(); 2141 bpf_enable_instrumentation(); 2142 } 2143 } 2144 2145 if (err) 2146 goto free_value; 2147 2148 if (copy_to_user(uvalue, value, value_size) != 0) { 2149 err = -EFAULT; 2150 goto free_value; 2151 } 2152 2153 err = 0; 2154 2155 free_value: 2156 kvfree(value); 2157 free_key: 2158 kvfree(key); 2159 err_put: 2160 bpf_map_write_active_dec(map); 2161 return err; 2162 } 2163 2164 #define BPF_MAP_FREEZE_LAST_FIELD map_fd 2165 2166 static int map_freeze(const union bpf_attr *attr) 2167 { 2168 int err = 0; 2169 struct bpf_map *map; 2170 2171 if (CHECK_ATTR(BPF_MAP_FREEZE)) 2172 return -EINVAL; 2173 2174 CLASS(fd, f)(attr->map_fd); 2175 map = __bpf_map_get(f); 2176 if (IS_ERR(map)) 2177 return PTR_ERR(map); 2178 2179 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS || !IS_ERR_OR_NULL(map->record)) 2180 return -ENOTSUPP; 2181 2182 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) 2183 return -EPERM; 2184 2185 mutex_lock(&map->freeze_mutex); 2186 if (bpf_map_write_active(map)) { 2187 err = -EBUSY; 2188 goto err_put; 2189 } 2190 if (READ_ONCE(map->frozen)) { 2191 err = -EBUSY; 2192 goto err_put; 2193 } 2194 2195 WRITE_ONCE(map->frozen, true); 2196 err_put: 2197 mutex_unlock(&map->freeze_mutex); 2198 return err; 2199 } 2200 2201 static const struct bpf_prog_ops * const bpf_prog_types[] = { 2202 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 2203 [_id] = & _name ## _prog_ops, 2204 #define BPF_MAP_TYPE(_id, _ops) 2205 #define BPF_LINK_TYPE(_id, _name) 2206 #include <linux/bpf_types.h> 2207 #undef BPF_PROG_TYPE 2208 #undef BPF_MAP_TYPE 2209 #undef BPF_LINK_TYPE 2210 }; 2211 2212 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) 2213 { 2214 const struct bpf_prog_ops *ops; 2215 2216 if (type >= ARRAY_SIZE(bpf_prog_types)) 2217 return -EINVAL; 2218 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); 2219 ops = bpf_prog_types[type]; 2220 if (!ops) 2221 return -EINVAL; 2222 2223 if (!bpf_prog_is_offloaded(prog->aux)) 2224 prog->aux->ops = ops; 2225 else 2226 prog->aux->ops = &bpf_offload_prog_ops; 2227 prog->type = type; 2228 return 0; 2229 } 2230 2231 enum bpf_audit { 2232 BPF_AUDIT_LOAD, 2233 BPF_AUDIT_UNLOAD, 2234 BPF_AUDIT_MAX, 2235 }; 2236 2237 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = { 2238 [BPF_AUDIT_LOAD] = "LOAD", 2239 [BPF_AUDIT_UNLOAD] = "UNLOAD", 2240 }; 2241 2242 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op) 2243 { 2244 struct audit_context *ctx = NULL; 2245 struct audit_buffer *ab; 2246 2247 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX)) 2248 return; 2249 if (audit_enabled == AUDIT_OFF) 2250 return; 2251 if (!in_irq() && !irqs_disabled()) 2252 ctx = audit_context(); 2253 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF); 2254 if (unlikely(!ab)) 2255 return; 2256 audit_log_format(ab, "prog-id=%u op=%s", 2257 prog->aux->id, bpf_audit_str[op]); 2258 audit_log_end(ab); 2259 } 2260 2261 static int bpf_prog_alloc_id(struct bpf_prog *prog) 2262 { 2263 int id; 2264 2265 idr_preload(GFP_KERNEL); 2266 spin_lock_bh(&prog_idr_lock); 2267 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); 2268 if (id > 0) 2269 prog->aux->id = id; 2270 spin_unlock_bh(&prog_idr_lock); 2271 idr_preload_end(); 2272 2273 /* id is in [1, INT_MAX) */ 2274 if (WARN_ON_ONCE(!id)) 2275 return -ENOSPC; 2276 2277 return id > 0 ? 0 : id; 2278 } 2279 2280 void bpf_prog_free_id(struct bpf_prog *prog) 2281 { 2282 unsigned long flags; 2283 2284 /* cBPF to eBPF migrations are currently not in the idr store. 2285 * Offloaded programs are removed from the store when their device 2286 * disappears - even if someone grabs an fd to them they are unusable, 2287 * simply waiting for refcnt to drop to be freed. 2288 */ 2289 if (!prog->aux->id) 2290 return; 2291 2292 spin_lock_irqsave(&prog_idr_lock, flags); 2293 idr_remove(&prog_idr, prog->aux->id); 2294 prog->aux->id = 0; 2295 spin_unlock_irqrestore(&prog_idr_lock, flags); 2296 } 2297 2298 static void __bpf_prog_put_rcu(struct rcu_head *rcu) 2299 { 2300 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); 2301 2302 kvfree(aux->func_info); 2303 kfree(aux->func_info_aux); 2304 free_uid(aux->user); 2305 security_bpf_prog_free(aux->prog); 2306 bpf_prog_free(aux->prog); 2307 } 2308 2309 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) 2310 { 2311 bpf_prog_kallsyms_del_all(prog); 2312 btf_put(prog->aux->btf); 2313 module_put(prog->aux->mod); 2314 kvfree(prog->aux->jited_linfo); 2315 kvfree(prog->aux->linfo); 2316 kfree(prog->aux->kfunc_tab); 2317 if (prog->aux->attach_btf) 2318 btf_put(prog->aux->attach_btf); 2319 2320 if (deferred) { 2321 if (prog->sleepable) 2322 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu); 2323 else 2324 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); 2325 } else { 2326 __bpf_prog_put_rcu(&prog->aux->rcu); 2327 } 2328 } 2329 2330 static void bpf_prog_put_deferred(struct work_struct *work) 2331 { 2332 struct bpf_prog_aux *aux; 2333 struct bpf_prog *prog; 2334 2335 aux = container_of(work, struct bpf_prog_aux, work); 2336 prog = aux->prog; 2337 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); 2338 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD); 2339 bpf_prog_free_id(prog); 2340 __bpf_prog_put_noref(prog, true); 2341 } 2342 2343 static void __bpf_prog_put(struct bpf_prog *prog) 2344 { 2345 struct bpf_prog_aux *aux = prog->aux; 2346 2347 if (atomic64_dec_and_test(&aux->refcnt)) { 2348 if (in_irq() || irqs_disabled()) { 2349 INIT_WORK(&aux->work, bpf_prog_put_deferred); 2350 schedule_work(&aux->work); 2351 } else { 2352 bpf_prog_put_deferred(&aux->work); 2353 } 2354 } 2355 } 2356 2357 void bpf_prog_put(struct bpf_prog *prog) 2358 { 2359 __bpf_prog_put(prog); 2360 } 2361 EXPORT_SYMBOL_GPL(bpf_prog_put); 2362 2363 static int bpf_prog_release(struct inode *inode, struct file *filp) 2364 { 2365 struct bpf_prog *prog = filp->private_data; 2366 2367 bpf_prog_put(prog); 2368 return 0; 2369 } 2370 2371 struct bpf_prog_kstats { 2372 u64 nsecs; 2373 u64 cnt; 2374 u64 misses; 2375 }; 2376 2377 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog) 2378 { 2379 struct bpf_prog_stats *stats; 2380 unsigned int flags; 2381 2382 stats = this_cpu_ptr(prog->stats); 2383 flags = u64_stats_update_begin_irqsave(&stats->syncp); 2384 u64_stats_inc(&stats->misses); 2385 u64_stats_update_end_irqrestore(&stats->syncp, flags); 2386 } 2387 2388 static void bpf_prog_get_stats(const struct bpf_prog *prog, 2389 struct bpf_prog_kstats *stats) 2390 { 2391 u64 nsecs = 0, cnt = 0, misses = 0; 2392 int cpu; 2393 2394 for_each_possible_cpu(cpu) { 2395 const struct bpf_prog_stats *st; 2396 unsigned int start; 2397 u64 tnsecs, tcnt, tmisses; 2398 2399 st = per_cpu_ptr(prog->stats, cpu); 2400 do { 2401 start = u64_stats_fetch_begin(&st->syncp); 2402 tnsecs = u64_stats_read(&st->nsecs); 2403 tcnt = u64_stats_read(&st->cnt); 2404 tmisses = u64_stats_read(&st->misses); 2405 } while (u64_stats_fetch_retry(&st->syncp, start)); 2406 nsecs += tnsecs; 2407 cnt += tcnt; 2408 misses += tmisses; 2409 } 2410 stats->nsecs = nsecs; 2411 stats->cnt = cnt; 2412 stats->misses = misses; 2413 } 2414 2415 #ifdef CONFIG_PROC_FS 2416 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) 2417 { 2418 const struct bpf_prog *prog = filp->private_data; 2419 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 2420 struct bpf_prog_kstats stats; 2421 2422 bpf_prog_get_stats(prog, &stats); 2423 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 2424 seq_printf(m, 2425 "prog_type:\t%u\n" 2426 "prog_jited:\t%u\n" 2427 "prog_tag:\t%s\n" 2428 "memlock:\t%llu\n" 2429 "prog_id:\t%u\n" 2430 "run_time_ns:\t%llu\n" 2431 "run_cnt:\t%llu\n" 2432 "recursion_misses:\t%llu\n" 2433 "verified_insns:\t%u\n", 2434 prog->type, 2435 prog->jited, 2436 prog_tag, 2437 prog->pages * 1ULL << PAGE_SHIFT, 2438 prog->aux->id, 2439 stats.nsecs, 2440 stats.cnt, 2441 stats.misses, 2442 prog->aux->verified_insns); 2443 } 2444 #endif 2445 2446 const struct file_operations bpf_prog_fops = { 2447 #ifdef CONFIG_PROC_FS 2448 .show_fdinfo = bpf_prog_show_fdinfo, 2449 #endif 2450 .release = bpf_prog_release, 2451 .read = bpf_dummy_read, 2452 .write = bpf_dummy_write, 2453 }; 2454 2455 int bpf_prog_new_fd(struct bpf_prog *prog) 2456 { 2457 int ret; 2458 2459 ret = security_bpf_prog(prog); 2460 if (ret < 0) 2461 return ret; 2462 2463 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, 2464 O_RDWR | O_CLOEXEC); 2465 } 2466 2467 void bpf_prog_add(struct bpf_prog *prog, int i) 2468 { 2469 atomic64_add(i, &prog->aux->refcnt); 2470 } 2471 EXPORT_SYMBOL_GPL(bpf_prog_add); 2472 2473 void bpf_prog_sub(struct bpf_prog *prog, int i) 2474 { 2475 /* Only to be used for undoing previous bpf_prog_add() in some 2476 * error path. We still know that another entity in our call 2477 * path holds a reference to the program, thus atomic_sub() can 2478 * be safely used in such cases! 2479 */ 2480 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); 2481 } 2482 EXPORT_SYMBOL_GPL(bpf_prog_sub); 2483 2484 void bpf_prog_inc(struct bpf_prog *prog) 2485 { 2486 atomic64_inc(&prog->aux->refcnt); 2487 } 2488 EXPORT_SYMBOL_GPL(bpf_prog_inc); 2489 2490 /* prog_idr_lock should have been held */ 2491 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) 2492 { 2493 int refold; 2494 2495 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); 2496 2497 if (!refold) 2498 return ERR_PTR(-ENOENT); 2499 2500 return prog; 2501 } 2502 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); 2503 2504 bool bpf_prog_get_ok(struct bpf_prog *prog, 2505 enum bpf_prog_type *attach_type, bool attach_drv) 2506 { 2507 /* not an attachment, just a refcount inc, always allow */ 2508 if (!attach_type) 2509 return true; 2510 2511 if (prog->type != *attach_type) 2512 return false; 2513 if (bpf_prog_is_offloaded(prog->aux) && !attach_drv) 2514 return false; 2515 2516 return true; 2517 } 2518 2519 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, 2520 bool attach_drv) 2521 { 2522 CLASS(fd, f)(ufd); 2523 struct bpf_prog *prog; 2524 2525 if (fd_empty(f)) 2526 return ERR_PTR(-EBADF); 2527 if (fd_file(f)->f_op != &bpf_prog_fops) 2528 return ERR_PTR(-EINVAL); 2529 2530 prog = fd_file(f)->private_data; 2531 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) 2532 return ERR_PTR(-EINVAL); 2533 2534 bpf_prog_inc(prog); 2535 return prog; 2536 } 2537 2538 struct bpf_prog *bpf_prog_get(u32 ufd) 2539 { 2540 return __bpf_prog_get(ufd, NULL, false); 2541 } 2542 2543 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, 2544 bool attach_drv) 2545 { 2546 return __bpf_prog_get(ufd, &type, attach_drv); 2547 } 2548 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); 2549 2550 /* Initially all BPF programs could be loaded w/o specifying 2551 * expected_attach_type. Later for some of them specifying expected_attach_type 2552 * at load time became required so that program could be validated properly. 2553 * Programs of types that are allowed to be loaded both w/ and w/o (for 2554 * backward compatibility) expected_attach_type, should have the default attach 2555 * type assigned to expected_attach_type for the latter case, so that it can be 2556 * validated later at attach time. 2557 * 2558 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if 2559 * prog type requires it but has some attach types that have to be backward 2560 * compatible. 2561 */ 2562 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) 2563 { 2564 switch (attr->prog_type) { 2565 case BPF_PROG_TYPE_CGROUP_SOCK: 2566 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't 2567 * exist so checking for non-zero is the way to go here. 2568 */ 2569 if (!attr->expected_attach_type) 2570 attr->expected_attach_type = 2571 BPF_CGROUP_INET_SOCK_CREATE; 2572 break; 2573 case BPF_PROG_TYPE_SK_REUSEPORT: 2574 if (!attr->expected_attach_type) 2575 attr->expected_attach_type = 2576 BPF_SK_REUSEPORT_SELECT; 2577 break; 2578 } 2579 } 2580 2581 static int 2582 bpf_prog_load_check_attach(enum bpf_prog_type prog_type, 2583 enum bpf_attach_type expected_attach_type, 2584 struct btf *attach_btf, u32 btf_id, 2585 struct bpf_prog *dst_prog) 2586 { 2587 if (btf_id) { 2588 if (btf_id > BTF_MAX_TYPE) 2589 return -EINVAL; 2590 2591 if (!attach_btf && !dst_prog) 2592 return -EINVAL; 2593 2594 switch (prog_type) { 2595 case BPF_PROG_TYPE_TRACING: 2596 case BPF_PROG_TYPE_LSM: 2597 case BPF_PROG_TYPE_STRUCT_OPS: 2598 case BPF_PROG_TYPE_EXT: 2599 break; 2600 default: 2601 return -EINVAL; 2602 } 2603 } 2604 2605 if (attach_btf && (!btf_id || dst_prog)) 2606 return -EINVAL; 2607 2608 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING && 2609 prog_type != BPF_PROG_TYPE_EXT) 2610 return -EINVAL; 2611 2612 switch (prog_type) { 2613 case BPF_PROG_TYPE_CGROUP_SOCK: 2614 switch (expected_attach_type) { 2615 case BPF_CGROUP_INET_SOCK_CREATE: 2616 case BPF_CGROUP_INET_SOCK_RELEASE: 2617 case BPF_CGROUP_INET4_POST_BIND: 2618 case BPF_CGROUP_INET6_POST_BIND: 2619 return 0; 2620 default: 2621 return -EINVAL; 2622 } 2623 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2624 switch (expected_attach_type) { 2625 case BPF_CGROUP_INET4_BIND: 2626 case BPF_CGROUP_INET6_BIND: 2627 case BPF_CGROUP_INET4_CONNECT: 2628 case BPF_CGROUP_INET6_CONNECT: 2629 case BPF_CGROUP_UNIX_CONNECT: 2630 case BPF_CGROUP_INET4_GETPEERNAME: 2631 case BPF_CGROUP_INET6_GETPEERNAME: 2632 case BPF_CGROUP_UNIX_GETPEERNAME: 2633 case BPF_CGROUP_INET4_GETSOCKNAME: 2634 case BPF_CGROUP_INET6_GETSOCKNAME: 2635 case BPF_CGROUP_UNIX_GETSOCKNAME: 2636 case BPF_CGROUP_UDP4_SENDMSG: 2637 case BPF_CGROUP_UDP6_SENDMSG: 2638 case BPF_CGROUP_UNIX_SENDMSG: 2639 case BPF_CGROUP_UDP4_RECVMSG: 2640 case BPF_CGROUP_UDP6_RECVMSG: 2641 case BPF_CGROUP_UNIX_RECVMSG: 2642 return 0; 2643 default: 2644 return -EINVAL; 2645 } 2646 case BPF_PROG_TYPE_CGROUP_SKB: 2647 switch (expected_attach_type) { 2648 case BPF_CGROUP_INET_INGRESS: 2649 case BPF_CGROUP_INET_EGRESS: 2650 return 0; 2651 default: 2652 return -EINVAL; 2653 } 2654 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2655 switch (expected_attach_type) { 2656 case BPF_CGROUP_SETSOCKOPT: 2657 case BPF_CGROUP_GETSOCKOPT: 2658 return 0; 2659 default: 2660 return -EINVAL; 2661 } 2662 case BPF_PROG_TYPE_SK_LOOKUP: 2663 if (expected_attach_type == BPF_SK_LOOKUP) 2664 return 0; 2665 return -EINVAL; 2666 case BPF_PROG_TYPE_SK_REUSEPORT: 2667 switch (expected_attach_type) { 2668 case BPF_SK_REUSEPORT_SELECT: 2669 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE: 2670 return 0; 2671 default: 2672 return -EINVAL; 2673 } 2674 case BPF_PROG_TYPE_NETFILTER: 2675 if (expected_attach_type == BPF_NETFILTER) 2676 return 0; 2677 return -EINVAL; 2678 case BPF_PROG_TYPE_SYSCALL: 2679 case BPF_PROG_TYPE_EXT: 2680 if (expected_attach_type) 2681 return -EINVAL; 2682 fallthrough; 2683 default: 2684 return 0; 2685 } 2686 } 2687 2688 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type) 2689 { 2690 switch (prog_type) { 2691 case BPF_PROG_TYPE_SCHED_CLS: 2692 case BPF_PROG_TYPE_SCHED_ACT: 2693 case BPF_PROG_TYPE_XDP: 2694 case BPF_PROG_TYPE_LWT_IN: 2695 case BPF_PROG_TYPE_LWT_OUT: 2696 case BPF_PROG_TYPE_LWT_XMIT: 2697 case BPF_PROG_TYPE_LWT_SEG6LOCAL: 2698 case BPF_PROG_TYPE_SK_SKB: 2699 case BPF_PROG_TYPE_SK_MSG: 2700 case BPF_PROG_TYPE_FLOW_DISSECTOR: 2701 case BPF_PROG_TYPE_CGROUP_DEVICE: 2702 case BPF_PROG_TYPE_CGROUP_SOCK: 2703 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2704 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2705 case BPF_PROG_TYPE_CGROUP_SYSCTL: 2706 case BPF_PROG_TYPE_SOCK_OPS: 2707 case BPF_PROG_TYPE_EXT: /* extends any prog */ 2708 case BPF_PROG_TYPE_NETFILTER: 2709 return true; 2710 case BPF_PROG_TYPE_CGROUP_SKB: 2711 /* always unpriv */ 2712 case BPF_PROG_TYPE_SK_REUSEPORT: 2713 /* equivalent to SOCKET_FILTER. need CAP_BPF only */ 2714 default: 2715 return false; 2716 } 2717 } 2718 2719 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) 2720 { 2721 switch (prog_type) { 2722 case BPF_PROG_TYPE_KPROBE: 2723 case BPF_PROG_TYPE_TRACEPOINT: 2724 case BPF_PROG_TYPE_PERF_EVENT: 2725 case BPF_PROG_TYPE_RAW_TRACEPOINT: 2726 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 2727 case BPF_PROG_TYPE_TRACING: 2728 case BPF_PROG_TYPE_LSM: 2729 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */ 2730 case BPF_PROG_TYPE_EXT: /* extends any prog */ 2731 return true; 2732 default: 2733 return false; 2734 } 2735 } 2736 2737 /* last field in 'union bpf_attr' used by this command */ 2738 #define BPF_PROG_LOAD_LAST_FIELD fd_array_cnt 2739 2740 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) 2741 { 2742 enum bpf_prog_type type = attr->prog_type; 2743 struct bpf_prog *prog, *dst_prog = NULL; 2744 struct btf *attach_btf = NULL; 2745 struct bpf_token *token = NULL; 2746 bool bpf_cap; 2747 int err; 2748 char license[128]; 2749 2750 if (CHECK_ATTR(BPF_PROG_LOAD)) 2751 return -EINVAL; 2752 2753 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | 2754 BPF_F_ANY_ALIGNMENT | 2755 BPF_F_TEST_STATE_FREQ | 2756 BPF_F_SLEEPABLE | 2757 BPF_F_TEST_RND_HI32 | 2758 BPF_F_XDP_HAS_FRAGS | 2759 BPF_F_XDP_DEV_BOUND_ONLY | 2760 BPF_F_TEST_REG_INVARIANTS | 2761 BPF_F_TOKEN_FD)) 2762 return -EINVAL; 2763 2764 bpf_prog_load_fixup_attach_type(attr); 2765 2766 if (attr->prog_flags & BPF_F_TOKEN_FD) { 2767 token = bpf_token_get_from_fd(attr->prog_token_fd); 2768 if (IS_ERR(token)) 2769 return PTR_ERR(token); 2770 /* if current token doesn't grant prog loading permissions, 2771 * then we can't use this token, so ignore it and rely on 2772 * system-wide capabilities checks 2773 */ 2774 if (!bpf_token_allow_cmd(token, BPF_PROG_LOAD) || 2775 !bpf_token_allow_prog_type(token, attr->prog_type, 2776 attr->expected_attach_type)) { 2777 bpf_token_put(token); 2778 token = NULL; 2779 } 2780 } 2781 2782 bpf_cap = bpf_token_capable(token, CAP_BPF); 2783 err = -EPERM; 2784 2785 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && 2786 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && 2787 !bpf_cap) 2788 goto put_token; 2789 2790 /* Intent here is for unprivileged_bpf_disabled to block BPF program 2791 * creation for unprivileged users; other actions depend 2792 * on fd availability and access to bpffs, so are dependent on 2793 * object creation success. Even with unprivileged BPF disabled, 2794 * capability checks are still carried out for these 2795 * and other operations. 2796 */ 2797 if (sysctl_unprivileged_bpf_disabled && !bpf_cap) 2798 goto put_token; 2799 2800 if (attr->insn_cnt == 0 || 2801 attr->insn_cnt > (bpf_cap ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) { 2802 err = -E2BIG; 2803 goto put_token; 2804 } 2805 if (type != BPF_PROG_TYPE_SOCKET_FILTER && 2806 type != BPF_PROG_TYPE_CGROUP_SKB && 2807 !bpf_cap) 2808 goto put_token; 2809 2810 if (is_net_admin_prog_type(type) && !bpf_token_capable(token, CAP_NET_ADMIN)) 2811 goto put_token; 2812 if (is_perfmon_prog_type(type) && !bpf_token_capable(token, CAP_PERFMON)) 2813 goto put_token; 2814 2815 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog 2816 * or btf, we need to check which one it is 2817 */ 2818 if (attr->attach_prog_fd) { 2819 dst_prog = bpf_prog_get(attr->attach_prog_fd); 2820 if (IS_ERR(dst_prog)) { 2821 dst_prog = NULL; 2822 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd); 2823 if (IS_ERR(attach_btf)) { 2824 err = -EINVAL; 2825 goto put_token; 2826 } 2827 if (!btf_is_kernel(attach_btf)) { 2828 /* attaching through specifying bpf_prog's BTF 2829 * objects directly might be supported eventually 2830 */ 2831 btf_put(attach_btf); 2832 err = -ENOTSUPP; 2833 goto put_token; 2834 } 2835 } 2836 } else if (attr->attach_btf_id) { 2837 /* fall back to vmlinux BTF, if BTF type ID is specified */ 2838 attach_btf = bpf_get_btf_vmlinux(); 2839 if (IS_ERR(attach_btf)) { 2840 err = PTR_ERR(attach_btf); 2841 goto put_token; 2842 } 2843 if (!attach_btf) { 2844 err = -EINVAL; 2845 goto put_token; 2846 } 2847 btf_get(attach_btf); 2848 } 2849 2850 if (bpf_prog_load_check_attach(type, attr->expected_attach_type, 2851 attach_btf, attr->attach_btf_id, 2852 dst_prog)) { 2853 if (dst_prog) 2854 bpf_prog_put(dst_prog); 2855 if (attach_btf) 2856 btf_put(attach_btf); 2857 err = -EINVAL; 2858 goto put_token; 2859 } 2860 2861 /* plain bpf_prog allocation */ 2862 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); 2863 if (!prog) { 2864 if (dst_prog) 2865 bpf_prog_put(dst_prog); 2866 if (attach_btf) 2867 btf_put(attach_btf); 2868 err = -EINVAL; 2869 goto put_token; 2870 } 2871 2872 prog->expected_attach_type = attr->expected_attach_type; 2873 prog->sleepable = !!(attr->prog_flags & BPF_F_SLEEPABLE); 2874 prog->aux->attach_btf = attach_btf; 2875 prog->aux->attach_btf_id = attr->attach_btf_id; 2876 prog->aux->dst_prog = dst_prog; 2877 prog->aux->dev_bound = !!attr->prog_ifindex; 2878 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS; 2879 2880 /* move token into prog->aux, reuse taken refcnt */ 2881 prog->aux->token = token; 2882 token = NULL; 2883 2884 prog->aux->user = get_current_user(); 2885 prog->len = attr->insn_cnt; 2886 2887 err = -EFAULT; 2888 if (copy_from_bpfptr(prog->insns, 2889 make_bpfptr(attr->insns, uattr.is_kernel), 2890 bpf_prog_insn_size(prog)) != 0) 2891 goto free_prog; 2892 /* copy eBPF program license from user space */ 2893 if (strncpy_from_bpfptr(license, 2894 make_bpfptr(attr->license, uattr.is_kernel), 2895 sizeof(license) - 1) < 0) 2896 goto free_prog; 2897 license[sizeof(license) - 1] = 0; 2898 2899 /* eBPF programs must be GPL compatible to use GPL-ed functions */ 2900 prog->gpl_compatible = license_is_gpl_compatible(license) ? 1 : 0; 2901 2902 prog->orig_prog = NULL; 2903 prog->jited = 0; 2904 2905 atomic64_set(&prog->aux->refcnt, 1); 2906 2907 if (bpf_prog_is_dev_bound(prog->aux)) { 2908 err = bpf_prog_dev_bound_init(prog, attr); 2909 if (err) 2910 goto free_prog; 2911 } 2912 2913 if (type == BPF_PROG_TYPE_EXT && dst_prog && 2914 bpf_prog_is_dev_bound(dst_prog->aux)) { 2915 err = bpf_prog_dev_bound_inherit(prog, dst_prog); 2916 if (err) 2917 goto free_prog; 2918 } 2919 2920 /* 2921 * Bookkeeping for managing the program attachment chain. 2922 * 2923 * It might be tempting to set attach_tracing_prog flag at the attachment 2924 * time, but this will not prevent from loading bunch of tracing prog 2925 * first, then attach them one to another. 2926 * 2927 * The flag attach_tracing_prog is set for the whole program lifecycle, and 2928 * doesn't have to be cleared in bpf_tracing_link_release, since tracing 2929 * programs cannot change attachment target. 2930 */ 2931 if (type == BPF_PROG_TYPE_TRACING && dst_prog && 2932 dst_prog->type == BPF_PROG_TYPE_TRACING) { 2933 prog->aux->attach_tracing_prog = true; 2934 } 2935 2936 /* find program type: socket_filter vs tracing_filter */ 2937 err = find_prog_type(type, prog); 2938 if (err < 0) 2939 goto free_prog; 2940 2941 prog->aux->load_time = ktime_get_boottime_ns(); 2942 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, 2943 sizeof(attr->prog_name)); 2944 if (err < 0) 2945 goto free_prog; 2946 2947 err = security_bpf_prog_load(prog, attr, token); 2948 if (err) 2949 goto free_prog_sec; 2950 2951 /* run eBPF verifier */ 2952 err = bpf_check(&prog, attr, uattr, uattr_size); 2953 if (err < 0) 2954 goto free_used_maps; 2955 2956 prog = bpf_prog_select_runtime(prog, &err); 2957 if (err < 0) 2958 goto free_used_maps; 2959 2960 err = bpf_prog_alloc_id(prog); 2961 if (err) 2962 goto free_used_maps; 2963 2964 /* Upon success of bpf_prog_alloc_id(), the BPF prog is 2965 * effectively publicly exposed. However, retrieving via 2966 * bpf_prog_get_fd_by_id() will take another reference, 2967 * therefore it cannot be gone underneath us. 2968 * 2969 * Only for the time /after/ successful bpf_prog_new_fd() 2970 * and before returning to userspace, we might just hold 2971 * one reference and any parallel close on that fd could 2972 * rip everything out. Hence, below notifications must 2973 * happen before bpf_prog_new_fd(). 2974 * 2975 * Also, any failure handling from this point onwards must 2976 * be using bpf_prog_put() given the program is exposed. 2977 */ 2978 bpf_prog_kallsyms_add(prog); 2979 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); 2980 bpf_audit_prog(prog, BPF_AUDIT_LOAD); 2981 2982 err = bpf_prog_new_fd(prog); 2983 if (err < 0) 2984 bpf_prog_put(prog); 2985 return err; 2986 2987 free_used_maps: 2988 /* In case we have subprogs, we need to wait for a grace 2989 * period before we can tear down JIT memory since symbols 2990 * are already exposed under kallsyms. 2991 */ 2992 __bpf_prog_put_noref(prog, prog->aux->real_func_cnt); 2993 return err; 2994 2995 free_prog_sec: 2996 security_bpf_prog_free(prog); 2997 free_prog: 2998 free_uid(prog->aux->user); 2999 if (prog->aux->attach_btf) 3000 btf_put(prog->aux->attach_btf); 3001 bpf_prog_free(prog); 3002 put_token: 3003 bpf_token_put(token); 3004 return err; 3005 } 3006 3007 #define BPF_OBJ_LAST_FIELD path_fd 3008 3009 static int bpf_obj_pin(const union bpf_attr *attr) 3010 { 3011 int path_fd; 3012 3013 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags & ~BPF_F_PATH_FD) 3014 return -EINVAL; 3015 3016 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */ 3017 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd) 3018 return -EINVAL; 3019 3020 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD; 3021 return bpf_obj_pin_user(attr->bpf_fd, path_fd, 3022 u64_to_user_ptr(attr->pathname)); 3023 } 3024 3025 static int bpf_obj_get(const union bpf_attr *attr) 3026 { 3027 int path_fd; 3028 3029 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || 3030 attr->file_flags & ~(BPF_OBJ_FLAG_MASK | BPF_F_PATH_FD)) 3031 return -EINVAL; 3032 3033 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */ 3034 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd) 3035 return -EINVAL; 3036 3037 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD; 3038 return bpf_obj_get_user(path_fd, u64_to_user_ptr(attr->pathname), 3039 attr->file_flags); 3040 } 3041 3042 /* bpf_link_init_sleepable() allows to specify whether BPF link itself has 3043 * "sleepable" semantics, which normally would mean that BPF link's attach 3044 * hook can dereference link or link's underlying program for some time after 3045 * detachment due to RCU Tasks Trace-based lifetime protection scheme. 3046 * BPF program itself can be non-sleepable, yet, because it's transitively 3047 * reachable through BPF link, its freeing has to be delayed until after RCU 3048 * Tasks Trace GP. 3049 */ 3050 void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type, 3051 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3052 bool sleepable) 3053 { 3054 WARN_ON(ops->dealloc && ops->dealloc_deferred); 3055 atomic64_set(&link->refcnt, 1); 3056 link->type = type; 3057 link->sleepable = sleepable; 3058 link->id = 0; 3059 link->ops = ops; 3060 link->prog = prog; 3061 } 3062 3063 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 3064 const struct bpf_link_ops *ops, struct bpf_prog *prog) 3065 { 3066 bpf_link_init_sleepable(link, type, ops, prog, false); 3067 } 3068 3069 static void bpf_link_free_id(int id) 3070 { 3071 if (!id) 3072 return; 3073 3074 spin_lock_bh(&link_idr_lock); 3075 idr_remove(&link_idr, id); 3076 spin_unlock_bh(&link_idr_lock); 3077 } 3078 3079 /* Clean up bpf_link and corresponding anon_inode file and FD. After 3080 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred 3081 * anon_inode's release() call. This helper marks bpf_link as 3082 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt 3083 * is not decremented, it's the responsibility of a calling code that failed 3084 * to complete bpf_link initialization. 3085 * This helper eventually calls link's dealloc callback, but does not call 3086 * link's release callback. 3087 */ 3088 void bpf_link_cleanup(struct bpf_link_primer *primer) 3089 { 3090 primer->link->prog = NULL; 3091 bpf_link_free_id(primer->id); 3092 fput(primer->file); 3093 put_unused_fd(primer->fd); 3094 } 3095 3096 void bpf_link_inc(struct bpf_link *link) 3097 { 3098 atomic64_inc(&link->refcnt); 3099 } 3100 3101 static void bpf_link_dealloc(struct bpf_link *link) 3102 { 3103 /* now that we know that bpf_link itself can't be reached, put underlying BPF program */ 3104 if (link->prog) 3105 bpf_prog_put(link->prog); 3106 3107 /* free bpf_link and its containing memory */ 3108 if (link->ops->dealloc_deferred) 3109 link->ops->dealloc_deferred(link); 3110 else 3111 link->ops->dealloc(link); 3112 } 3113 3114 static void bpf_link_defer_dealloc_rcu_gp(struct rcu_head *rcu) 3115 { 3116 struct bpf_link *link = container_of(rcu, struct bpf_link, rcu); 3117 3118 bpf_link_dealloc(link); 3119 } 3120 3121 static void bpf_link_defer_dealloc_mult_rcu_gp(struct rcu_head *rcu) 3122 { 3123 if (rcu_trace_implies_rcu_gp()) 3124 bpf_link_defer_dealloc_rcu_gp(rcu); 3125 else 3126 call_rcu(rcu, bpf_link_defer_dealloc_rcu_gp); 3127 } 3128 3129 /* bpf_link_free is guaranteed to be called from process context */ 3130 static void bpf_link_free(struct bpf_link *link) 3131 { 3132 const struct bpf_link_ops *ops = link->ops; 3133 3134 bpf_link_free_id(link->id); 3135 /* detach BPF program, clean up used resources */ 3136 if (link->prog) 3137 ops->release(link); 3138 if (ops->dealloc_deferred) { 3139 /* Schedule BPF link deallocation, which will only then 3140 * trigger putting BPF program refcount. 3141 * If underlying BPF program is sleepable or BPF link's target 3142 * attach hookpoint is sleepable or otherwise requires RCU GPs 3143 * to ensure link and its underlying BPF program is not 3144 * reachable anymore, we need to first wait for RCU tasks 3145 * trace sync, and then go through "classic" RCU grace period 3146 */ 3147 if (link->sleepable || (link->prog && link->prog->sleepable)) 3148 call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp); 3149 else 3150 call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp); 3151 } else if (ops->dealloc) { 3152 bpf_link_dealloc(link); 3153 } 3154 } 3155 3156 static void bpf_link_put_deferred(struct work_struct *work) 3157 { 3158 struct bpf_link *link = container_of(work, struct bpf_link, work); 3159 3160 bpf_link_free(link); 3161 } 3162 3163 /* bpf_link_put might be called from atomic context. It needs to be called 3164 * from sleepable context in order to acquire sleeping locks during the process. 3165 */ 3166 void bpf_link_put(struct bpf_link *link) 3167 { 3168 if (!atomic64_dec_and_test(&link->refcnt)) 3169 return; 3170 3171 INIT_WORK(&link->work, bpf_link_put_deferred); 3172 schedule_work(&link->work); 3173 } 3174 EXPORT_SYMBOL(bpf_link_put); 3175 3176 static void bpf_link_put_direct(struct bpf_link *link) 3177 { 3178 if (!atomic64_dec_and_test(&link->refcnt)) 3179 return; 3180 bpf_link_free(link); 3181 } 3182 3183 static int bpf_link_release(struct inode *inode, struct file *filp) 3184 { 3185 struct bpf_link *link = filp->private_data; 3186 3187 bpf_link_put_direct(link); 3188 return 0; 3189 } 3190 3191 #ifdef CONFIG_PROC_FS 3192 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 3193 #define BPF_MAP_TYPE(_id, _ops) 3194 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, 3195 static const char *bpf_link_type_strs[] = { 3196 [BPF_LINK_TYPE_UNSPEC] = "<invalid>", 3197 #include <linux/bpf_types.h> 3198 }; 3199 #undef BPF_PROG_TYPE 3200 #undef BPF_MAP_TYPE 3201 #undef BPF_LINK_TYPE 3202 3203 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) 3204 { 3205 const struct bpf_link *link = filp->private_data; 3206 const struct bpf_prog *prog = link->prog; 3207 enum bpf_link_type type = link->type; 3208 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 3209 3210 if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) { 3211 seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]); 3212 } else { 3213 WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type); 3214 seq_printf(m, "link_type:\t<%u>\n", type); 3215 } 3216 seq_printf(m, "link_id:\t%u\n", link->id); 3217 3218 if (prog) { 3219 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 3220 seq_printf(m, 3221 "prog_tag:\t%s\n" 3222 "prog_id:\t%u\n", 3223 prog_tag, 3224 prog->aux->id); 3225 } 3226 if (link->ops->show_fdinfo) 3227 link->ops->show_fdinfo(link, m); 3228 } 3229 #endif 3230 3231 static __poll_t bpf_link_poll(struct file *file, struct poll_table_struct *pts) 3232 { 3233 struct bpf_link *link = file->private_data; 3234 3235 return link->ops->poll(file, pts); 3236 } 3237 3238 static const struct file_operations bpf_link_fops = { 3239 #ifdef CONFIG_PROC_FS 3240 .show_fdinfo = bpf_link_show_fdinfo, 3241 #endif 3242 .release = bpf_link_release, 3243 .read = bpf_dummy_read, 3244 .write = bpf_dummy_write, 3245 }; 3246 3247 static const struct file_operations bpf_link_fops_poll = { 3248 #ifdef CONFIG_PROC_FS 3249 .show_fdinfo = bpf_link_show_fdinfo, 3250 #endif 3251 .release = bpf_link_release, 3252 .read = bpf_dummy_read, 3253 .write = bpf_dummy_write, 3254 .poll = bpf_link_poll, 3255 }; 3256 3257 static int bpf_link_alloc_id(struct bpf_link *link) 3258 { 3259 int id; 3260 3261 idr_preload(GFP_KERNEL); 3262 spin_lock_bh(&link_idr_lock); 3263 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); 3264 spin_unlock_bh(&link_idr_lock); 3265 idr_preload_end(); 3266 3267 return id; 3268 } 3269 3270 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, 3271 * reserving unused FD and allocating ID from link_idr. This is to be paired 3272 * with bpf_link_settle() to install FD and ID and expose bpf_link to 3273 * user-space, if bpf_link is successfully attached. If not, bpf_link and 3274 * pre-allocated resources are to be freed with bpf_cleanup() call. All the 3275 * transient state is passed around in struct bpf_link_primer. 3276 * This is preferred way to create and initialize bpf_link, especially when 3277 * there are complicated and expensive operations in between creating bpf_link 3278 * itself and attaching it to BPF hook. By using bpf_link_prime() and 3279 * bpf_link_settle() kernel code using bpf_link doesn't have to perform 3280 * expensive (and potentially failing) roll back operations in a rare case 3281 * that file, FD, or ID can't be allocated. 3282 */ 3283 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) 3284 { 3285 struct file *file; 3286 int fd, id; 3287 3288 fd = get_unused_fd_flags(O_CLOEXEC); 3289 if (fd < 0) 3290 return fd; 3291 3292 3293 id = bpf_link_alloc_id(link); 3294 if (id < 0) { 3295 put_unused_fd(fd); 3296 return id; 3297 } 3298 3299 file = anon_inode_getfile("bpf_link", 3300 link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, 3301 link, O_CLOEXEC); 3302 if (IS_ERR(file)) { 3303 bpf_link_free_id(id); 3304 put_unused_fd(fd); 3305 return PTR_ERR(file); 3306 } 3307 3308 primer->link = link; 3309 primer->file = file; 3310 primer->fd = fd; 3311 primer->id = id; 3312 return 0; 3313 } 3314 3315 int bpf_link_settle(struct bpf_link_primer *primer) 3316 { 3317 /* make bpf_link fetchable by ID */ 3318 spin_lock_bh(&link_idr_lock); 3319 primer->link->id = primer->id; 3320 spin_unlock_bh(&link_idr_lock); 3321 /* make bpf_link fetchable by FD */ 3322 fd_install(primer->fd, primer->file); 3323 /* pass through installed FD */ 3324 return primer->fd; 3325 } 3326 3327 int bpf_link_new_fd(struct bpf_link *link) 3328 { 3329 return anon_inode_getfd("bpf-link", 3330 link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, 3331 link, O_CLOEXEC); 3332 } 3333 3334 struct bpf_link *bpf_link_get_from_fd(u32 ufd) 3335 { 3336 CLASS(fd, f)(ufd); 3337 struct bpf_link *link; 3338 3339 if (fd_empty(f)) 3340 return ERR_PTR(-EBADF); 3341 if (fd_file(f)->f_op != &bpf_link_fops && fd_file(f)->f_op != &bpf_link_fops_poll) 3342 return ERR_PTR(-EINVAL); 3343 3344 link = fd_file(f)->private_data; 3345 bpf_link_inc(link); 3346 return link; 3347 } 3348 EXPORT_SYMBOL(bpf_link_get_from_fd); 3349 3350 static void bpf_tracing_link_release(struct bpf_link *link) 3351 { 3352 struct bpf_tracing_link *tr_link = 3353 container_of(link, struct bpf_tracing_link, link.link); 3354 3355 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link, 3356 tr_link->trampoline, 3357 tr_link->tgt_prog)); 3358 3359 bpf_trampoline_put(tr_link->trampoline); 3360 3361 /* tgt_prog is NULL if target is a kernel function */ 3362 if (tr_link->tgt_prog) 3363 bpf_prog_put(tr_link->tgt_prog); 3364 } 3365 3366 static void bpf_tracing_link_dealloc(struct bpf_link *link) 3367 { 3368 struct bpf_tracing_link *tr_link = 3369 container_of(link, struct bpf_tracing_link, link.link); 3370 3371 kfree(tr_link); 3372 } 3373 3374 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, 3375 struct seq_file *seq) 3376 { 3377 struct bpf_tracing_link *tr_link = 3378 container_of(link, struct bpf_tracing_link, link.link); 3379 u32 target_btf_id, target_obj_id; 3380 3381 bpf_trampoline_unpack_key(tr_link->trampoline->key, 3382 &target_obj_id, &target_btf_id); 3383 seq_printf(seq, 3384 "attach_type:\t%d\n" 3385 "target_obj_id:\t%u\n" 3386 "target_btf_id:\t%u\n", 3387 tr_link->attach_type, 3388 target_obj_id, 3389 target_btf_id); 3390 } 3391 3392 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, 3393 struct bpf_link_info *info) 3394 { 3395 struct bpf_tracing_link *tr_link = 3396 container_of(link, struct bpf_tracing_link, link.link); 3397 3398 info->tracing.attach_type = tr_link->attach_type; 3399 bpf_trampoline_unpack_key(tr_link->trampoline->key, 3400 &info->tracing.target_obj_id, 3401 &info->tracing.target_btf_id); 3402 3403 return 0; 3404 } 3405 3406 static const struct bpf_link_ops bpf_tracing_link_lops = { 3407 .release = bpf_tracing_link_release, 3408 .dealloc = bpf_tracing_link_dealloc, 3409 .show_fdinfo = bpf_tracing_link_show_fdinfo, 3410 .fill_link_info = bpf_tracing_link_fill_link_info, 3411 }; 3412 3413 static int bpf_tracing_prog_attach(struct bpf_prog *prog, 3414 int tgt_prog_fd, 3415 u32 btf_id, 3416 u64 bpf_cookie) 3417 { 3418 struct bpf_link_primer link_primer; 3419 struct bpf_prog *tgt_prog = NULL; 3420 struct bpf_trampoline *tr = NULL; 3421 struct bpf_tracing_link *link; 3422 u64 key = 0; 3423 int err; 3424 3425 switch (prog->type) { 3426 case BPF_PROG_TYPE_TRACING: 3427 if (prog->expected_attach_type != BPF_TRACE_FENTRY && 3428 prog->expected_attach_type != BPF_TRACE_FEXIT && 3429 prog->expected_attach_type != BPF_MODIFY_RETURN) { 3430 err = -EINVAL; 3431 goto out_put_prog; 3432 } 3433 break; 3434 case BPF_PROG_TYPE_EXT: 3435 if (prog->expected_attach_type != 0) { 3436 err = -EINVAL; 3437 goto out_put_prog; 3438 } 3439 break; 3440 case BPF_PROG_TYPE_LSM: 3441 if (prog->expected_attach_type != BPF_LSM_MAC) { 3442 err = -EINVAL; 3443 goto out_put_prog; 3444 } 3445 break; 3446 default: 3447 err = -EINVAL; 3448 goto out_put_prog; 3449 } 3450 3451 if (!!tgt_prog_fd != !!btf_id) { 3452 err = -EINVAL; 3453 goto out_put_prog; 3454 } 3455 3456 if (tgt_prog_fd) { 3457 /* 3458 * For now we only allow new targets for BPF_PROG_TYPE_EXT. If this 3459 * part would be changed to implement the same for 3460 * BPF_PROG_TYPE_TRACING, do not forget to update the way how 3461 * attach_tracing_prog flag is set. 3462 */ 3463 if (prog->type != BPF_PROG_TYPE_EXT) { 3464 err = -EINVAL; 3465 goto out_put_prog; 3466 } 3467 3468 tgt_prog = bpf_prog_get(tgt_prog_fd); 3469 if (IS_ERR(tgt_prog)) { 3470 err = PTR_ERR(tgt_prog); 3471 tgt_prog = NULL; 3472 goto out_put_prog; 3473 } 3474 3475 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id); 3476 } 3477 3478 link = kzalloc(sizeof(*link), GFP_USER); 3479 if (!link) { 3480 err = -ENOMEM; 3481 goto out_put_prog; 3482 } 3483 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING, 3484 &bpf_tracing_link_lops, prog); 3485 link->attach_type = prog->expected_attach_type; 3486 link->link.cookie = bpf_cookie; 3487 3488 mutex_lock(&prog->aux->dst_mutex); 3489 3490 /* There are a few possible cases here: 3491 * 3492 * - if prog->aux->dst_trampoline is set, the program was just loaded 3493 * and not yet attached to anything, so we can use the values stored 3494 * in prog->aux 3495 * 3496 * - if prog->aux->dst_trampoline is NULL, the program has already been 3497 * attached to a target and its initial target was cleared (below) 3498 * 3499 * - if tgt_prog != NULL, the caller specified tgt_prog_fd + 3500 * target_btf_id using the link_create API. 3501 * 3502 * - if tgt_prog == NULL when this function was called using the old 3503 * raw_tracepoint_open API, and we need a target from prog->aux 3504 * 3505 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program 3506 * was detached and is going for re-attachment. 3507 * 3508 * - if prog->aux->dst_trampoline is NULL and tgt_prog and prog->aux->attach_btf 3509 * are NULL, then program was already attached and user did not provide 3510 * tgt_prog_fd so we have no way to find out or create trampoline 3511 */ 3512 if (!prog->aux->dst_trampoline && !tgt_prog) { 3513 /* 3514 * Allow re-attach for TRACING and LSM programs. If it's 3515 * currently linked, bpf_trampoline_link_prog will fail. 3516 * EXT programs need to specify tgt_prog_fd, so they 3517 * re-attach in separate code path. 3518 */ 3519 if (prog->type != BPF_PROG_TYPE_TRACING && 3520 prog->type != BPF_PROG_TYPE_LSM) { 3521 err = -EINVAL; 3522 goto out_unlock; 3523 } 3524 /* We can allow re-attach only if we have valid attach_btf. */ 3525 if (!prog->aux->attach_btf) { 3526 err = -EINVAL; 3527 goto out_unlock; 3528 } 3529 btf_id = prog->aux->attach_btf_id; 3530 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id); 3531 } 3532 3533 if (!prog->aux->dst_trampoline || 3534 (key && key != prog->aux->dst_trampoline->key)) { 3535 /* If there is no saved target, or the specified target is 3536 * different from the destination specified at load time, we 3537 * need a new trampoline and a check for compatibility 3538 */ 3539 struct bpf_attach_target_info tgt_info = {}; 3540 3541 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id, 3542 &tgt_info); 3543 if (err) 3544 goto out_unlock; 3545 3546 if (tgt_info.tgt_mod) { 3547 module_put(prog->aux->mod); 3548 prog->aux->mod = tgt_info.tgt_mod; 3549 } 3550 3551 tr = bpf_trampoline_get(key, &tgt_info); 3552 if (!tr) { 3553 err = -ENOMEM; 3554 goto out_unlock; 3555 } 3556 } else { 3557 /* The caller didn't specify a target, or the target was the 3558 * same as the destination supplied during program load. This 3559 * means we can reuse the trampoline and reference from program 3560 * load time, and there is no need to allocate a new one. This 3561 * can only happen once for any program, as the saved values in 3562 * prog->aux are cleared below. 3563 */ 3564 tr = prog->aux->dst_trampoline; 3565 tgt_prog = prog->aux->dst_prog; 3566 } 3567 3568 err = bpf_link_prime(&link->link.link, &link_primer); 3569 if (err) 3570 goto out_unlock; 3571 3572 err = bpf_trampoline_link_prog(&link->link, tr, tgt_prog); 3573 if (err) { 3574 bpf_link_cleanup(&link_primer); 3575 link = NULL; 3576 goto out_unlock; 3577 } 3578 3579 link->tgt_prog = tgt_prog; 3580 link->trampoline = tr; 3581 3582 /* Always clear the trampoline and target prog from prog->aux to make 3583 * sure the original attach destination is not kept alive after a 3584 * program is (re-)attached to another target. 3585 */ 3586 if (prog->aux->dst_prog && 3587 (tgt_prog_fd || tr != prog->aux->dst_trampoline)) 3588 /* got extra prog ref from syscall, or attaching to different prog */ 3589 bpf_prog_put(prog->aux->dst_prog); 3590 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline) 3591 /* we allocated a new trampoline, so free the old one */ 3592 bpf_trampoline_put(prog->aux->dst_trampoline); 3593 3594 prog->aux->dst_prog = NULL; 3595 prog->aux->dst_trampoline = NULL; 3596 mutex_unlock(&prog->aux->dst_mutex); 3597 3598 return bpf_link_settle(&link_primer); 3599 out_unlock: 3600 if (tr && tr != prog->aux->dst_trampoline) 3601 bpf_trampoline_put(tr); 3602 mutex_unlock(&prog->aux->dst_mutex); 3603 kfree(link); 3604 out_put_prog: 3605 if (tgt_prog_fd && tgt_prog) 3606 bpf_prog_put(tgt_prog); 3607 return err; 3608 } 3609 3610 static void bpf_raw_tp_link_release(struct bpf_link *link) 3611 { 3612 struct bpf_raw_tp_link *raw_tp = 3613 container_of(link, struct bpf_raw_tp_link, link); 3614 3615 bpf_probe_unregister(raw_tp->btp, raw_tp); 3616 bpf_put_raw_tracepoint(raw_tp->btp); 3617 } 3618 3619 static void bpf_raw_tp_link_dealloc(struct bpf_link *link) 3620 { 3621 struct bpf_raw_tp_link *raw_tp = 3622 container_of(link, struct bpf_raw_tp_link, link); 3623 3624 kfree(raw_tp); 3625 } 3626 3627 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, 3628 struct seq_file *seq) 3629 { 3630 struct bpf_raw_tp_link *raw_tp_link = 3631 container_of(link, struct bpf_raw_tp_link, link); 3632 3633 seq_printf(seq, 3634 "tp_name:\t%s\n", 3635 raw_tp_link->btp->tp->name); 3636 } 3637 3638 static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen, 3639 u32 len) 3640 { 3641 if (ulen >= len + 1) { 3642 if (copy_to_user(ubuf, buf, len + 1)) 3643 return -EFAULT; 3644 } else { 3645 char zero = '\0'; 3646 3647 if (copy_to_user(ubuf, buf, ulen - 1)) 3648 return -EFAULT; 3649 if (put_user(zero, ubuf + ulen - 1)) 3650 return -EFAULT; 3651 return -ENOSPC; 3652 } 3653 3654 return 0; 3655 } 3656 3657 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, 3658 struct bpf_link_info *info) 3659 { 3660 struct bpf_raw_tp_link *raw_tp_link = 3661 container_of(link, struct bpf_raw_tp_link, link); 3662 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); 3663 const char *tp_name = raw_tp_link->btp->tp->name; 3664 u32 ulen = info->raw_tracepoint.tp_name_len; 3665 size_t tp_len = strlen(tp_name); 3666 3667 if (!ulen ^ !ubuf) 3668 return -EINVAL; 3669 3670 info->raw_tracepoint.tp_name_len = tp_len + 1; 3671 3672 if (!ubuf) 3673 return 0; 3674 3675 return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len); 3676 } 3677 3678 static const struct bpf_link_ops bpf_raw_tp_link_lops = { 3679 .release = bpf_raw_tp_link_release, 3680 .dealloc_deferred = bpf_raw_tp_link_dealloc, 3681 .show_fdinfo = bpf_raw_tp_link_show_fdinfo, 3682 .fill_link_info = bpf_raw_tp_link_fill_link_info, 3683 }; 3684 3685 #ifdef CONFIG_PERF_EVENTS 3686 struct bpf_perf_link { 3687 struct bpf_link link; 3688 struct file *perf_file; 3689 }; 3690 3691 static void bpf_perf_link_release(struct bpf_link *link) 3692 { 3693 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); 3694 struct perf_event *event = perf_link->perf_file->private_data; 3695 3696 perf_event_free_bpf_prog(event); 3697 fput(perf_link->perf_file); 3698 } 3699 3700 static void bpf_perf_link_dealloc(struct bpf_link *link) 3701 { 3702 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); 3703 3704 kfree(perf_link); 3705 } 3706 3707 static int bpf_perf_link_fill_common(const struct perf_event *event, 3708 char __user *uname, u32 *ulenp, 3709 u64 *probe_offset, u64 *probe_addr, 3710 u32 *fd_type, unsigned long *missed) 3711 { 3712 const char *buf; 3713 u32 prog_id, ulen; 3714 size_t len; 3715 int err; 3716 3717 ulen = *ulenp; 3718 if (!ulen ^ !uname) 3719 return -EINVAL; 3720 3721 err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf, 3722 probe_offset, probe_addr, missed); 3723 if (err) 3724 return err; 3725 3726 if (buf) { 3727 len = strlen(buf); 3728 *ulenp = len + 1; 3729 } else { 3730 *ulenp = 1; 3731 } 3732 if (!uname) 3733 return 0; 3734 3735 if (buf) { 3736 err = bpf_copy_to_user(uname, buf, ulen, len); 3737 if (err) 3738 return err; 3739 } else { 3740 char zero = '\0'; 3741 3742 if (put_user(zero, uname)) 3743 return -EFAULT; 3744 } 3745 return 0; 3746 } 3747 3748 #ifdef CONFIG_KPROBE_EVENTS 3749 static int bpf_perf_link_fill_kprobe(const struct perf_event *event, 3750 struct bpf_link_info *info) 3751 { 3752 unsigned long missed; 3753 char __user *uname; 3754 u64 addr, offset; 3755 u32 ulen, type; 3756 int err; 3757 3758 uname = u64_to_user_ptr(info->perf_event.kprobe.func_name); 3759 ulen = info->perf_event.kprobe.name_len; 3760 err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr, 3761 &type, &missed); 3762 if (err) 3763 return err; 3764 if (type == BPF_FD_TYPE_KRETPROBE) 3765 info->perf_event.type = BPF_PERF_EVENT_KRETPROBE; 3766 else 3767 info->perf_event.type = BPF_PERF_EVENT_KPROBE; 3768 info->perf_event.kprobe.name_len = ulen; 3769 info->perf_event.kprobe.offset = offset; 3770 info->perf_event.kprobe.missed = missed; 3771 if (!kallsyms_show_value(current_cred())) 3772 addr = 0; 3773 info->perf_event.kprobe.addr = addr; 3774 info->perf_event.kprobe.cookie = event->bpf_cookie; 3775 return 0; 3776 } 3777 #endif 3778 3779 #ifdef CONFIG_UPROBE_EVENTS 3780 static int bpf_perf_link_fill_uprobe(const struct perf_event *event, 3781 struct bpf_link_info *info) 3782 { 3783 char __user *uname; 3784 u64 addr, offset; 3785 u32 ulen, type; 3786 int err; 3787 3788 uname = u64_to_user_ptr(info->perf_event.uprobe.file_name); 3789 ulen = info->perf_event.uprobe.name_len; 3790 err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr, 3791 &type, NULL); 3792 if (err) 3793 return err; 3794 3795 if (type == BPF_FD_TYPE_URETPROBE) 3796 info->perf_event.type = BPF_PERF_EVENT_URETPROBE; 3797 else 3798 info->perf_event.type = BPF_PERF_EVENT_UPROBE; 3799 info->perf_event.uprobe.name_len = ulen; 3800 info->perf_event.uprobe.offset = offset; 3801 info->perf_event.uprobe.cookie = event->bpf_cookie; 3802 return 0; 3803 } 3804 #endif 3805 3806 static int bpf_perf_link_fill_probe(const struct perf_event *event, 3807 struct bpf_link_info *info) 3808 { 3809 #ifdef CONFIG_KPROBE_EVENTS 3810 if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE) 3811 return bpf_perf_link_fill_kprobe(event, info); 3812 #endif 3813 #ifdef CONFIG_UPROBE_EVENTS 3814 if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE) 3815 return bpf_perf_link_fill_uprobe(event, info); 3816 #endif 3817 return -EOPNOTSUPP; 3818 } 3819 3820 static int bpf_perf_link_fill_tracepoint(const struct perf_event *event, 3821 struct bpf_link_info *info) 3822 { 3823 char __user *uname; 3824 u32 ulen; 3825 int err; 3826 3827 uname = u64_to_user_ptr(info->perf_event.tracepoint.tp_name); 3828 ulen = info->perf_event.tracepoint.name_len; 3829 err = bpf_perf_link_fill_common(event, uname, &ulen, NULL, NULL, NULL, NULL); 3830 if (err) 3831 return err; 3832 3833 info->perf_event.type = BPF_PERF_EVENT_TRACEPOINT; 3834 info->perf_event.tracepoint.name_len = ulen; 3835 info->perf_event.tracepoint.cookie = event->bpf_cookie; 3836 return 0; 3837 } 3838 3839 static int bpf_perf_link_fill_perf_event(const struct perf_event *event, 3840 struct bpf_link_info *info) 3841 { 3842 info->perf_event.event.type = event->attr.type; 3843 info->perf_event.event.config = event->attr.config; 3844 info->perf_event.event.cookie = event->bpf_cookie; 3845 info->perf_event.type = BPF_PERF_EVENT_EVENT; 3846 return 0; 3847 } 3848 3849 static int bpf_perf_link_fill_link_info(const struct bpf_link *link, 3850 struct bpf_link_info *info) 3851 { 3852 struct bpf_perf_link *perf_link; 3853 const struct perf_event *event; 3854 3855 perf_link = container_of(link, struct bpf_perf_link, link); 3856 event = perf_get_event(perf_link->perf_file); 3857 if (IS_ERR(event)) 3858 return PTR_ERR(event); 3859 3860 switch (event->prog->type) { 3861 case BPF_PROG_TYPE_PERF_EVENT: 3862 return bpf_perf_link_fill_perf_event(event, info); 3863 case BPF_PROG_TYPE_TRACEPOINT: 3864 return bpf_perf_link_fill_tracepoint(event, info); 3865 case BPF_PROG_TYPE_KPROBE: 3866 return bpf_perf_link_fill_probe(event, info); 3867 default: 3868 return -EOPNOTSUPP; 3869 } 3870 } 3871 3872 static const struct bpf_link_ops bpf_perf_link_lops = { 3873 .release = bpf_perf_link_release, 3874 .dealloc = bpf_perf_link_dealloc, 3875 .fill_link_info = bpf_perf_link_fill_link_info, 3876 }; 3877 3878 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3879 { 3880 struct bpf_link_primer link_primer; 3881 struct bpf_perf_link *link; 3882 struct perf_event *event; 3883 struct file *perf_file; 3884 int err; 3885 3886 if (attr->link_create.flags) 3887 return -EINVAL; 3888 3889 perf_file = perf_event_get(attr->link_create.target_fd); 3890 if (IS_ERR(perf_file)) 3891 return PTR_ERR(perf_file); 3892 3893 link = kzalloc(sizeof(*link), GFP_USER); 3894 if (!link) { 3895 err = -ENOMEM; 3896 goto out_put_file; 3897 } 3898 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog); 3899 link->perf_file = perf_file; 3900 3901 err = bpf_link_prime(&link->link, &link_primer); 3902 if (err) { 3903 kfree(link); 3904 goto out_put_file; 3905 } 3906 3907 event = perf_file->private_data; 3908 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie); 3909 if (err) { 3910 bpf_link_cleanup(&link_primer); 3911 goto out_put_file; 3912 } 3913 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */ 3914 bpf_prog_inc(prog); 3915 3916 return bpf_link_settle(&link_primer); 3917 3918 out_put_file: 3919 fput(perf_file); 3920 return err; 3921 } 3922 #else 3923 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3924 { 3925 return -EOPNOTSUPP; 3926 } 3927 #endif /* CONFIG_PERF_EVENTS */ 3928 3929 static int bpf_raw_tp_link_attach(struct bpf_prog *prog, 3930 const char __user *user_tp_name, u64 cookie) 3931 { 3932 struct bpf_link_primer link_primer; 3933 struct bpf_raw_tp_link *link; 3934 struct bpf_raw_event_map *btp; 3935 const char *tp_name; 3936 char buf[128]; 3937 int err; 3938 3939 switch (prog->type) { 3940 case BPF_PROG_TYPE_TRACING: 3941 case BPF_PROG_TYPE_EXT: 3942 case BPF_PROG_TYPE_LSM: 3943 if (user_tp_name) 3944 /* The attach point for this category of programs 3945 * should be specified via btf_id during program load. 3946 */ 3947 return -EINVAL; 3948 if (prog->type == BPF_PROG_TYPE_TRACING && 3949 prog->expected_attach_type == BPF_TRACE_RAW_TP) { 3950 tp_name = prog->aux->attach_func_name; 3951 break; 3952 } 3953 return bpf_tracing_prog_attach(prog, 0, 0, 0); 3954 case BPF_PROG_TYPE_RAW_TRACEPOINT: 3955 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 3956 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0) 3957 return -EFAULT; 3958 buf[sizeof(buf) - 1] = 0; 3959 tp_name = buf; 3960 break; 3961 default: 3962 return -EINVAL; 3963 } 3964 3965 btp = bpf_get_raw_tracepoint(tp_name); 3966 if (!btp) 3967 return -ENOENT; 3968 3969 link = kzalloc(sizeof(*link), GFP_USER); 3970 if (!link) { 3971 err = -ENOMEM; 3972 goto out_put_btp; 3973 } 3974 bpf_link_init_sleepable(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, 3975 &bpf_raw_tp_link_lops, prog, 3976 tracepoint_is_faultable(btp->tp)); 3977 link->btp = btp; 3978 link->cookie = cookie; 3979 3980 err = bpf_link_prime(&link->link, &link_primer); 3981 if (err) { 3982 kfree(link); 3983 goto out_put_btp; 3984 } 3985 3986 err = bpf_probe_register(link->btp, link); 3987 if (err) { 3988 bpf_link_cleanup(&link_primer); 3989 goto out_put_btp; 3990 } 3991 3992 return bpf_link_settle(&link_primer); 3993 3994 out_put_btp: 3995 bpf_put_raw_tracepoint(btp); 3996 return err; 3997 } 3998 3999 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.cookie 4000 4001 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 4002 { 4003 struct bpf_prog *prog; 4004 void __user *tp_name; 4005 __u64 cookie; 4006 int fd; 4007 4008 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) 4009 return -EINVAL; 4010 4011 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); 4012 if (IS_ERR(prog)) 4013 return PTR_ERR(prog); 4014 4015 tp_name = u64_to_user_ptr(attr->raw_tracepoint.name); 4016 cookie = attr->raw_tracepoint.cookie; 4017 fd = bpf_raw_tp_link_attach(prog, tp_name, cookie); 4018 if (fd < 0) 4019 bpf_prog_put(prog); 4020 return fd; 4021 } 4022 4023 static enum bpf_prog_type 4024 attach_type_to_prog_type(enum bpf_attach_type attach_type) 4025 { 4026 switch (attach_type) { 4027 case BPF_CGROUP_INET_INGRESS: 4028 case BPF_CGROUP_INET_EGRESS: 4029 return BPF_PROG_TYPE_CGROUP_SKB; 4030 case BPF_CGROUP_INET_SOCK_CREATE: 4031 case BPF_CGROUP_INET_SOCK_RELEASE: 4032 case BPF_CGROUP_INET4_POST_BIND: 4033 case BPF_CGROUP_INET6_POST_BIND: 4034 return BPF_PROG_TYPE_CGROUP_SOCK; 4035 case BPF_CGROUP_INET4_BIND: 4036 case BPF_CGROUP_INET6_BIND: 4037 case BPF_CGROUP_INET4_CONNECT: 4038 case BPF_CGROUP_INET6_CONNECT: 4039 case BPF_CGROUP_UNIX_CONNECT: 4040 case BPF_CGROUP_INET4_GETPEERNAME: 4041 case BPF_CGROUP_INET6_GETPEERNAME: 4042 case BPF_CGROUP_UNIX_GETPEERNAME: 4043 case BPF_CGROUP_INET4_GETSOCKNAME: 4044 case BPF_CGROUP_INET6_GETSOCKNAME: 4045 case BPF_CGROUP_UNIX_GETSOCKNAME: 4046 case BPF_CGROUP_UDP4_SENDMSG: 4047 case BPF_CGROUP_UDP6_SENDMSG: 4048 case BPF_CGROUP_UNIX_SENDMSG: 4049 case BPF_CGROUP_UDP4_RECVMSG: 4050 case BPF_CGROUP_UDP6_RECVMSG: 4051 case BPF_CGROUP_UNIX_RECVMSG: 4052 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 4053 case BPF_CGROUP_SOCK_OPS: 4054 return BPF_PROG_TYPE_SOCK_OPS; 4055 case BPF_CGROUP_DEVICE: 4056 return BPF_PROG_TYPE_CGROUP_DEVICE; 4057 case BPF_SK_MSG_VERDICT: 4058 return BPF_PROG_TYPE_SK_MSG; 4059 case BPF_SK_SKB_STREAM_PARSER: 4060 case BPF_SK_SKB_STREAM_VERDICT: 4061 case BPF_SK_SKB_VERDICT: 4062 return BPF_PROG_TYPE_SK_SKB; 4063 case BPF_LIRC_MODE2: 4064 return BPF_PROG_TYPE_LIRC_MODE2; 4065 case BPF_FLOW_DISSECTOR: 4066 return BPF_PROG_TYPE_FLOW_DISSECTOR; 4067 case BPF_CGROUP_SYSCTL: 4068 return BPF_PROG_TYPE_CGROUP_SYSCTL; 4069 case BPF_CGROUP_GETSOCKOPT: 4070 case BPF_CGROUP_SETSOCKOPT: 4071 return BPF_PROG_TYPE_CGROUP_SOCKOPT; 4072 case BPF_TRACE_ITER: 4073 case BPF_TRACE_RAW_TP: 4074 case BPF_TRACE_FENTRY: 4075 case BPF_TRACE_FEXIT: 4076 case BPF_MODIFY_RETURN: 4077 return BPF_PROG_TYPE_TRACING; 4078 case BPF_LSM_MAC: 4079 return BPF_PROG_TYPE_LSM; 4080 case BPF_SK_LOOKUP: 4081 return BPF_PROG_TYPE_SK_LOOKUP; 4082 case BPF_XDP: 4083 return BPF_PROG_TYPE_XDP; 4084 case BPF_LSM_CGROUP: 4085 return BPF_PROG_TYPE_LSM; 4086 case BPF_TCX_INGRESS: 4087 case BPF_TCX_EGRESS: 4088 case BPF_NETKIT_PRIMARY: 4089 case BPF_NETKIT_PEER: 4090 return BPF_PROG_TYPE_SCHED_CLS; 4091 default: 4092 return BPF_PROG_TYPE_UNSPEC; 4093 } 4094 } 4095 4096 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 4097 enum bpf_attach_type attach_type) 4098 { 4099 enum bpf_prog_type ptype; 4100 4101 switch (prog->type) { 4102 case BPF_PROG_TYPE_CGROUP_SOCK: 4103 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4104 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4105 case BPF_PROG_TYPE_SK_LOOKUP: 4106 return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 4107 case BPF_PROG_TYPE_CGROUP_SKB: 4108 if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN)) 4109 /* cg-skb progs can be loaded by unpriv user. 4110 * check permissions at attach time. 4111 */ 4112 return -EPERM; 4113 4114 ptype = attach_type_to_prog_type(attach_type); 4115 if (prog->type != ptype) 4116 return -EINVAL; 4117 4118 return prog->enforce_expected_attach_type && 4119 prog->expected_attach_type != attach_type ? 4120 -EINVAL : 0; 4121 case BPF_PROG_TYPE_EXT: 4122 return 0; 4123 case BPF_PROG_TYPE_NETFILTER: 4124 if (attach_type != BPF_NETFILTER) 4125 return -EINVAL; 4126 return 0; 4127 case BPF_PROG_TYPE_PERF_EVENT: 4128 case BPF_PROG_TYPE_TRACEPOINT: 4129 if (attach_type != BPF_PERF_EVENT) 4130 return -EINVAL; 4131 return 0; 4132 case BPF_PROG_TYPE_KPROBE: 4133 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI && 4134 attach_type != BPF_TRACE_KPROBE_MULTI) 4135 return -EINVAL; 4136 if (prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION && 4137 attach_type != BPF_TRACE_KPROBE_SESSION) 4138 return -EINVAL; 4139 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI && 4140 attach_type != BPF_TRACE_UPROBE_MULTI) 4141 return -EINVAL; 4142 if (prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION && 4143 attach_type != BPF_TRACE_UPROBE_SESSION) 4144 return -EINVAL; 4145 if (attach_type != BPF_PERF_EVENT && 4146 attach_type != BPF_TRACE_KPROBE_MULTI && 4147 attach_type != BPF_TRACE_KPROBE_SESSION && 4148 attach_type != BPF_TRACE_UPROBE_MULTI && 4149 attach_type != BPF_TRACE_UPROBE_SESSION) 4150 return -EINVAL; 4151 return 0; 4152 case BPF_PROG_TYPE_SCHED_CLS: 4153 if (attach_type != BPF_TCX_INGRESS && 4154 attach_type != BPF_TCX_EGRESS && 4155 attach_type != BPF_NETKIT_PRIMARY && 4156 attach_type != BPF_NETKIT_PEER) 4157 return -EINVAL; 4158 return 0; 4159 default: 4160 ptype = attach_type_to_prog_type(attach_type); 4161 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) 4162 return -EINVAL; 4163 return 0; 4164 } 4165 } 4166 4167 #define BPF_PROG_ATTACH_LAST_FIELD expected_revision 4168 4169 #define BPF_F_ATTACH_MASK_BASE \ 4170 (BPF_F_ALLOW_OVERRIDE | \ 4171 BPF_F_ALLOW_MULTI | \ 4172 BPF_F_REPLACE) 4173 4174 #define BPF_F_ATTACH_MASK_MPROG \ 4175 (BPF_F_REPLACE | \ 4176 BPF_F_BEFORE | \ 4177 BPF_F_AFTER | \ 4178 BPF_F_ID | \ 4179 BPF_F_LINK) 4180 4181 static int bpf_prog_attach(const union bpf_attr *attr) 4182 { 4183 enum bpf_prog_type ptype; 4184 struct bpf_prog *prog; 4185 int ret; 4186 4187 if (CHECK_ATTR(BPF_PROG_ATTACH)) 4188 return -EINVAL; 4189 4190 ptype = attach_type_to_prog_type(attr->attach_type); 4191 if (ptype == BPF_PROG_TYPE_UNSPEC) 4192 return -EINVAL; 4193 if (bpf_mprog_supported(ptype)) { 4194 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG) 4195 return -EINVAL; 4196 } else { 4197 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_BASE) 4198 return -EINVAL; 4199 if (attr->relative_fd || 4200 attr->expected_revision) 4201 return -EINVAL; 4202 } 4203 4204 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 4205 if (IS_ERR(prog)) 4206 return PTR_ERR(prog); 4207 4208 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 4209 bpf_prog_put(prog); 4210 return -EINVAL; 4211 } 4212 4213 switch (ptype) { 4214 case BPF_PROG_TYPE_SK_SKB: 4215 case BPF_PROG_TYPE_SK_MSG: 4216 ret = sock_map_get_from_fd(attr, prog); 4217 break; 4218 case BPF_PROG_TYPE_LIRC_MODE2: 4219 ret = lirc_prog_attach(attr, prog); 4220 break; 4221 case BPF_PROG_TYPE_FLOW_DISSECTOR: 4222 ret = netns_bpf_prog_attach(attr, prog); 4223 break; 4224 case BPF_PROG_TYPE_CGROUP_DEVICE: 4225 case BPF_PROG_TYPE_CGROUP_SKB: 4226 case BPF_PROG_TYPE_CGROUP_SOCK: 4227 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4228 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4229 case BPF_PROG_TYPE_CGROUP_SYSCTL: 4230 case BPF_PROG_TYPE_SOCK_OPS: 4231 case BPF_PROG_TYPE_LSM: 4232 if (ptype == BPF_PROG_TYPE_LSM && 4233 prog->expected_attach_type != BPF_LSM_CGROUP) 4234 ret = -EINVAL; 4235 else 4236 ret = cgroup_bpf_prog_attach(attr, ptype, prog); 4237 break; 4238 case BPF_PROG_TYPE_SCHED_CLS: 4239 if (attr->attach_type == BPF_TCX_INGRESS || 4240 attr->attach_type == BPF_TCX_EGRESS) 4241 ret = tcx_prog_attach(attr, prog); 4242 else 4243 ret = netkit_prog_attach(attr, prog); 4244 break; 4245 default: 4246 ret = -EINVAL; 4247 } 4248 4249 if (ret) 4250 bpf_prog_put(prog); 4251 return ret; 4252 } 4253 4254 #define BPF_PROG_DETACH_LAST_FIELD expected_revision 4255 4256 static int bpf_prog_detach(const union bpf_attr *attr) 4257 { 4258 struct bpf_prog *prog = NULL; 4259 enum bpf_prog_type ptype; 4260 int ret; 4261 4262 if (CHECK_ATTR(BPF_PROG_DETACH)) 4263 return -EINVAL; 4264 4265 ptype = attach_type_to_prog_type(attr->attach_type); 4266 if (bpf_mprog_supported(ptype)) { 4267 if (ptype == BPF_PROG_TYPE_UNSPEC) 4268 return -EINVAL; 4269 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG) 4270 return -EINVAL; 4271 if (attr->attach_bpf_fd) { 4272 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 4273 if (IS_ERR(prog)) 4274 return PTR_ERR(prog); 4275 } 4276 } else if (attr->attach_flags || 4277 attr->relative_fd || 4278 attr->expected_revision) { 4279 return -EINVAL; 4280 } 4281 4282 switch (ptype) { 4283 case BPF_PROG_TYPE_SK_MSG: 4284 case BPF_PROG_TYPE_SK_SKB: 4285 ret = sock_map_prog_detach(attr, ptype); 4286 break; 4287 case BPF_PROG_TYPE_LIRC_MODE2: 4288 ret = lirc_prog_detach(attr); 4289 break; 4290 case BPF_PROG_TYPE_FLOW_DISSECTOR: 4291 ret = netns_bpf_prog_detach(attr, ptype); 4292 break; 4293 case BPF_PROG_TYPE_CGROUP_DEVICE: 4294 case BPF_PROG_TYPE_CGROUP_SKB: 4295 case BPF_PROG_TYPE_CGROUP_SOCK: 4296 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4297 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4298 case BPF_PROG_TYPE_CGROUP_SYSCTL: 4299 case BPF_PROG_TYPE_SOCK_OPS: 4300 case BPF_PROG_TYPE_LSM: 4301 ret = cgroup_bpf_prog_detach(attr, ptype); 4302 break; 4303 case BPF_PROG_TYPE_SCHED_CLS: 4304 if (attr->attach_type == BPF_TCX_INGRESS || 4305 attr->attach_type == BPF_TCX_EGRESS) 4306 ret = tcx_prog_detach(attr, prog); 4307 else 4308 ret = netkit_prog_detach(attr, prog); 4309 break; 4310 default: 4311 ret = -EINVAL; 4312 } 4313 4314 if (prog) 4315 bpf_prog_put(prog); 4316 return ret; 4317 } 4318 4319 #define BPF_PROG_QUERY_LAST_FIELD query.revision 4320 4321 static int bpf_prog_query(const union bpf_attr *attr, 4322 union bpf_attr __user *uattr) 4323 { 4324 if (!bpf_net_capable()) 4325 return -EPERM; 4326 if (CHECK_ATTR(BPF_PROG_QUERY)) 4327 return -EINVAL; 4328 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 4329 return -EINVAL; 4330 4331 switch (attr->query.attach_type) { 4332 case BPF_CGROUP_INET_INGRESS: 4333 case BPF_CGROUP_INET_EGRESS: 4334 case BPF_CGROUP_INET_SOCK_CREATE: 4335 case BPF_CGROUP_INET_SOCK_RELEASE: 4336 case BPF_CGROUP_INET4_BIND: 4337 case BPF_CGROUP_INET6_BIND: 4338 case BPF_CGROUP_INET4_POST_BIND: 4339 case BPF_CGROUP_INET6_POST_BIND: 4340 case BPF_CGROUP_INET4_CONNECT: 4341 case BPF_CGROUP_INET6_CONNECT: 4342 case BPF_CGROUP_UNIX_CONNECT: 4343 case BPF_CGROUP_INET4_GETPEERNAME: 4344 case BPF_CGROUP_INET6_GETPEERNAME: 4345 case BPF_CGROUP_UNIX_GETPEERNAME: 4346 case BPF_CGROUP_INET4_GETSOCKNAME: 4347 case BPF_CGROUP_INET6_GETSOCKNAME: 4348 case BPF_CGROUP_UNIX_GETSOCKNAME: 4349 case BPF_CGROUP_UDP4_SENDMSG: 4350 case BPF_CGROUP_UDP6_SENDMSG: 4351 case BPF_CGROUP_UNIX_SENDMSG: 4352 case BPF_CGROUP_UDP4_RECVMSG: 4353 case BPF_CGROUP_UDP6_RECVMSG: 4354 case BPF_CGROUP_UNIX_RECVMSG: 4355 case BPF_CGROUP_SOCK_OPS: 4356 case BPF_CGROUP_DEVICE: 4357 case BPF_CGROUP_SYSCTL: 4358 case BPF_CGROUP_GETSOCKOPT: 4359 case BPF_CGROUP_SETSOCKOPT: 4360 case BPF_LSM_CGROUP: 4361 return cgroup_bpf_prog_query(attr, uattr); 4362 case BPF_LIRC_MODE2: 4363 return lirc_prog_query(attr, uattr); 4364 case BPF_FLOW_DISSECTOR: 4365 case BPF_SK_LOOKUP: 4366 return netns_bpf_prog_query(attr, uattr); 4367 case BPF_SK_SKB_STREAM_PARSER: 4368 case BPF_SK_SKB_STREAM_VERDICT: 4369 case BPF_SK_MSG_VERDICT: 4370 case BPF_SK_SKB_VERDICT: 4371 return sock_map_bpf_prog_query(attr, uattr); 4372 case BPF_TCX_INGRESS: 4373 case BPF_TCX_EGRESS: 4374 return tcx_prog_query(attr, uattr); 4375 case BPF_NETKIT_PRIMARY: 4376 case BPF_NETKIT_PEER: 4377 return netkit_prog_query(attr, uattr); 4378 default: 4379 return -EINVAL; 4380 } 4381 } 4382 4383 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size 4384 4385 static int bpf_prog_test_run(const union bpf_attr *attr, 4386 union bpf_attr __user *uattr) 4387 { 4388 struct bpf_prog *prog; 4389 int ret = -ENOTSUPP; 4390 4391 if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 4392 return -EINVAL; 4393 4394 if ((attr->test.ctx_size_in && !attr->test.ctx_in) || 4395 (!attr->test.ctx_size_in && attr->test.ctx_in)) 4396 return -EINVAL; 4397 4398 if ((attr->test.ctx_size_out && !attr->test.ctx_out) || 4399 (!attr->test.ctx_size_out && attr->test.ctx_out)) 4400 return -EINVAL; 4401 4402 prog = bpf_prog_get(attr->test.prog_fd); 4403 if (IS_ERR(prog)) 4404 return PTR_ERR(prog); 4405 4406 if (prog->aux->ops->test_run) 4407 ret = prog->aux->ops->test_run(prog, attr, uattr); 4408 4409 bpf_prog_put(prog); 4410 return ret; 4411 } 4412 4413 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 4414 4415 static int bpf_obj_get_next_id(const union bpf_attr *attr, 4416 union bpf_attr __user *uattr, 4417 struct idr *idr, 4418 spinlock_t *lock) 4419 { 4420 u32 next_id = attr->start_id; 4421 int err = 0; 4422 4423 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 4424 return -EINVAL; 4425 4426 if (!capable(CAP_SYS_ADMIN)) 4427 return -EPERM; 4428 4429 next_id++; 4430 spin_lock_bh(lock); 4431 if (!idr_get_next(idr, &next_id)) 4432 err = -ENOENT; 4433 spin_unlock_bh(lock); 4434 4435 if (!err) 4436 err = put_user(next_id, &uattr->next_id); 4437 4438 return err; 4439 } 4440 4441 struct bpf_map *bpf_map_get_curr_or_next(u32 *id) 4442 { 4443 struct bpf_map *map; 4444 4445 spin_lock_bh(&map_idr_lock); 4446 again: 4447 map = idr_get_next(&map_idr, id); 4448 if (map) { 4449 map = __bpf_map_inc_not_zero(map, false); 4450 if (IS_ERR(map)) { 4451 (*id)++; 4452 goto again; 4453 } 4454 } 4455 spin_unlock_bh(&map_idr_lock); 4456 4457 return map; 4458 } 4459 4460 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id) 4461 { 4462 struct bpf_prog *prog; 4463 4464 spin_lock_bh(&prog_idr_lock); 4465 again: 4466 prog = idr_get_next(&prog_idr, id); 4467 if (prog) { 4468 prog = bpf_prog_inc_not_zero(prog); 4469 if (IS_ERR(prog)) { 4470 (*id)++; 4471 goto again; 4472 } 4473 } 4474 spin_unlock_bh(&prog_idr_lock); 4475 4476 return prog; 4477 } 4478 4479 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 4480 4481 struct bpf_prog *bpf_prog_by_id(u32 id) 4482 { 4483 struct bpf_prog *prog; 4484 4485 if (!id) 4486 return ERR_PTR(-ENOENT); 4487 4488 spin_lock_bh(&prog_idr_lock); 4489 prog = idr_find(&prog_idr, id); 4490 if (prog) 4491 prog = bpf_prog_inc_not_zero(prog); 4492 else 4493 prog = ERR_PTR(-ENOENT); 4494 spin_unlock_bh(&prog_idr_lock); 4495 return prog; 4496 } 4497 4498 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 4499 { 4500 struct bpf_prog *prog; 4501 u32 id = attr->prog_id; 4502 int fd; 4503 4504 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 4505 return -EINVAL; 4506 4507 if (!capable(CAP_SYS_ADMIN)) 4508 return -EPERM; 4509 4510 prog = bpf_prog_by_id(id); 4511 if (IS_ERR(prog)) 4512 return PTR_ERR(prog); 4513 4514 fd = bpf_prog_new_fd(prog); 4515 if (fd < 0) 4516 bpf_prog_put(prog); 4517 4518 return fd; 4519 } 4520 4521 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 4522 4523 static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 4524 { 4525 struct bpf_map *map; 4526 u32 id = attr->map_id; 4527 int f_flags; 4528 int fd; 4529 4530 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 4531 attr->open_flags & ~BPF_OBJ_FLAG_MASK) 4532 return -EINVAL; 4533 4534 if (!capable(CAP_SYS_ADMIN)) 4535 return -EPERM; 4536 4537 f_flags = bpf_get_file_flag(attr->open_flags); 4538 if (f_flags < 0) 4539 return f_flags; 4540 4541 spin_lock_bh(&map_idr_lock); 4542 map = idr_find(&map_idr, id); 4543 if (map) 4544 map = __bpf_map_inc_not_zero(map, true); 4545 else 4546 map = ERR_PTR(-ENOENT); 4547 spin_unlock_bh(&map_idr_lock); 4548 4549 if (IS_ERR(map)) 4550 return PTR_ERR(map); 4551 4552 fd = bpf_map_new_fd(map, f_flags); 4553 if (fd < 0) 4554 bpf_map_put_with_uref(map); 4555 4556 return fd; 4557 } 4558 4559 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 4560 unsigned long addr, u32 *off, 4561 u32 *type) 4562 { 4563 const struct bpf_map *map; 4564 int i; 4565 4566 mutex_lock(&prog->aux->used_maps_mutex); 4567 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { 4568 map = prog->aux->used_maps[i]; 4569 if (map == (void *)addr) { 4570 *type = BPF_PSEUDO_MAP_FD; 4571 goto out; 4572 } 4573 if (!map->ops->map_direct_value_meta) 4574 continue; 4575 if (!map->ops->map_direct_value_meta(map, addr, off)) { 4576 *type = BPF_PSEUDO_MAP_VALUE; 4577 goto out; 4578 } 4579 } 4580 map = NULL; 4581 4582 out: 4583 mutex_unlock(&prog->aux->used_maps_mutex); 4584 return map; 4585 } 4586 4587 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, 4588 const struct cred *f_cred) 4589 { 4590 const struct bpf_map *map; 4591 struct bpf_insn *insns; 4592 u32 off, type; 4593 u64 imm; 4594 u8 code; 4595 int i; 4596 4597 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), 4598 GFP_USER); 4599 if (!insns) 4600 return insns; 4601 4602 for (i = 0; i < prog->len; i++) { 4603 code = insns[i].code; 4604 4605 if (code == (BPF_JMP | BPF_TAIL_CALL)) { 4606 insns[i].code = BPF_JMP | BPF_CALL; 4607 insns[i].imm = BPF_FUNC_tail_call; 4608 /* fall-through */ 4609 } 4610 if (code == (BPF_JMP | BPF_CALL) || 4611 code == (BPF_JMP | BPF_CALL_ARGS)) { 4612 if (code == (BPF_JMP | BPF_CALL_ARGS)) 4613 insns[i].code = BPF_JMP | BPF_CALL; 4614 if (!bpf_dump_raw_ok(f_cred)) 4615 insns[i].imm = 0; 4616 continue; 4617 } 4618 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) { 4619 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM; 4620 continue; 4621 } 4622 4623 if ((BPF_CLASS(code) == BPF_LDX || BPF_CLASS(code) == BPF_STX || 4624 BPF_CLASS(code) == BPF_ST) && BPF_MODE(code) == BPF_PROBE_MEM32) { 4625 insns[i].code = BPF_CLASS(code) | BPF_SIZE(code) | BPF_MEM; 4626 continue; 4627 } 4628 4629 if (code != (BPF_LD | BPF_IMM | BPF_DW)) 4630 continue; 4631 4632 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; 4633 map = bpf_map_from_imm(prog, imm, &off, &type); 4634 if (map) { 4635 insns[i].src_reg = type; 4636 insns[i].imm = map->id; 4637 insns[i + 1].imm = off; 4638 continue; 4639 } 4640 } 4641 4642 return insns; 4643 } 4644 4645 static int set_info_rec_size(struct bpf_prog_info *info) 4646 { 4647 /* 4648 * Ensure info.*_rec_size is the same as kernel expected size 4649 * 4650 * or 4651 * 4652 * Only allow zero *_rec_size if both _rec_size and _cnt are 4653 * zero. In this case, the kernel will set the expected 4654 * _rec_size back to the info. 4655 */ 4656 4657 if ((info->nr_func_info || info->func_info_rec_size) && 4658 info->func_info_rec_size != sizeof(struct bpf_func_info)) 4659 return -EINVAL; 4660 4661 if ((info->nr_line_info || info->line_info_rec_size) && 4662 info->line_info_rec_size != sizeof(struct bpf_line_info)) 4663 return -EINVAL; 4664 4665 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && 4666 info->jited_line_info_rec_size != sizeof(__u64)) 4667 return -EINVAL; 4668 4669 info->func_info_rec_size = sizeof(struct bpf_func_info); 4670 info->line_info_rec_size = sizeof(struct bpf_line_info); 4671 info->jited_line_info_rec_size = sizeof(__u64); 4672 4673 return 0; 4674 } 4675 4676 static int bpf_prog_get_info_by_fd(struct file *file, 4677 struct bpf_prog *prog, 4678 const union bpf_attr *attr, 4679 union bpf_attr __user *uattr) 4680 { 4681 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); 4682 struct btf *attach_btf = bpf_prog_get_target_btf(prog); 4683 struct bpf_prog_info info; 4684 u32 info_len = attr->info.info_len; 4685 struct bpf_prog_kstats stats; 4686 char __user *uinsns; 4687 u32 ulen; 4688 int err; 4689 4690 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); 4691 if (err) 4692 return err; 4693 info_len = min_t(u32, sizeof(info), info_len); 4694 4695 memset(&info, 0, sizeof(info)); 4696 if (copy_from_user(&info, uinfo, info_len)) 4697 return -EFAULT; 4698 4699 info.type = prog->type; 4700 info.id = prog->aux->id; 4701 info.load_time = prog->aux->load_time; 4702 info.created_by_uid = from_kuid_munged(current_user_ns(), 4703 prog->aux->user->uid); 4704 info.gpl_compatible = prog->gpl_compatible; 4705 4706 memcpy(info.tag, prog->tag, sizeof(prog->tag)); 4707 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); 4708 4709 mutex_lock(&prog->aux->used_maps_mutex); 4710 ulen = info.nr_map_ids; 4711 info.nr_map_ids = prog->aux->used_map_cnt; 4712 ulen = min_t(u32, info.nr_map_ids, ulen); 4713 if (ulen) { 4714 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 4715 u32 i; 4716 4717 for (i = 0; i < ulen; i++) 4718 if (put_user(prog->aux->used_maps[i]->id, 4719 &user_map_ids[i])) { 4720 mutex_unlock(&prog->aux->used_maps_mutex); 4721 return -EFAULT; 4722 } 4723 } 4724 mutex_unlock(&prog->aux->used_maps_mutex); 4725 4726 err = set_info_rec_size(&info); 4727 if (err) 4728 return err; 4729 4730 bpf_prog_get_stats(prog, &stats); 4731 info.run_time_ns = stats.nsecs; 4732 info.run_cnt = stats.cnt; 4733 info.recursion_misses = stats.misses; 4734 4735 info.verified_insns = prog->aux->verified_insns; 4736 4737 if (!bpf_capable()) { 4738 info.jited_prog_len = 0; 4739 info.xlated_prog_len = 0; 4740 info.nr_jited_ksyms = 0; 4741 info.nr_jited_func_lens = 0; 4742 info.nr_func_info = 0; 4743 info.nr_line_info = 0; 4744 info.nr_jited_line_info = 0; 4745 goto done; 4746 } 4747 4748 ulen = info.xlated_prog_len; 4749 info.xlated_prog_len = bpf_prog_insn_size(prog); 4750 if (info.xlated_prog_len && ulen) { 4751 struct bpf_insn *insns_sanitized; 4752 bool fault; 4753 4754 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) { 4755 info.xlated_prog_insns = 0; 4756 goto done; 4757 } 4758 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred); 4759 if (!insns_sanitized) 4760 return -ENOMEM; 4761 uinsns = u64_to_user_ptr(info.xlated_prog_insns); 4762 ulen = min_t(u32, info.xlated_prog_len, ulen); 4763 fault = copy_to_user(uinsns, insns_sanitized, ulen); 4764 kfree(insns_sanitized); 4765 if (fault) 4766 return -EFAULT; 4767 } 4768 4769 if (bpf_prog_is_offloaded(prog->aux)) { 4770 err = bpf_prog_offload_info_fill(&info, prog); 4771 if (err) 4772 return err; 4773 goto done; 4774 } 4775 4776 /* NOTE: the following code is supposed to be skipped for offload. 4777 * bpf_prog_offload_info_fill() is the place to fill similar fields 4778 * for offload. 4779 */ 4780 ulen = info.jited_prog_len; 4781 if (prog->aux->func_cnt) { 4782 u32 i; 4783 4784 info.jited_prog_len = 0; 4785 for (i = 0; i < prog->aux->func_cnt; i++) 4786 info.jited_prog_len += prog->aux->func[i]->jited_len; 4787 } else { 4788 info.jited_prog_len = prog->jited_len; 4789 } 4790 4791 if (info.jited_prog_len && ulen) { 4792 if (bpf_dump_raw_ok(file->f_cred)) { 4793 uinsns = u64_to_user_ptr(info.jited_prog_insns); 4794 ulen = min_t(u32, info.jited_prog_len, ulen); 4795 4796 /* for multi-function programs, copy the JITed 4797 * instructions for all the functions 4798 */ 4799 if (prog->aux->func_cnt) { 4800 u32 len, free, i; 4801 u8 *img; 4802 4803 free = ulen; 4804 for (i = 0; i < prog->aux->func_cnt; i++) { 4805 len = prog->aux->func[i]->jited_len; 4806 len = min_t(u32, len, free); 4807 img = (u8 *) prog->aux->func[i]->bpf_func; 4808 if (copy_to_user(uinsns, img, len)) 4809 return -EFAULT; 4810 uinsns += len; 4811 free -= len; 4812 if (!free) 4813 break; 4814 } 4815 } else { 4816 if (copy_to_user(uinsns, prog->bpf_func, ulen)) 4817 return -EFAULT; 4818 } 4819 } else { 4820 info.jited_prog_insns = 0; 4821 } 4822 } 4823 4824 ulen = info.nr_jited_ksyms; 4825 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; 4826 if (ulen) { 4827 if (bpf_dump_raw_ok(file->f_cred)) { 4828 unsigned long ksym_addr; 4829 u64 __user *user_ksyms; 4830 u32 i; 4831 4832 /* copy the address of the kernel symbol 4833 * corresponding to each function 4834 */ 4835 ulen = min_t(u32, info.nr_jited_ksyms, ulen); 4836 user_ksyms = u64_to_user_ptr(info.jited_ksyms); 4837 if (prog->aux->func_cnt) { 4838 for (i = 0; i < ulen; i++) { 4839 ksym_addr = (unsigned long) 4840 prog->aux->func[i]->bpf_func; 4841 if (put_user((u64) ksym_addr, 4842 &user_ksyms[i])) 4843 return -EFAULT; 4844 } 4845 } else { 4846 ksym_addr = (unsigned long) prog->bpf_func; 4847 if (put_user((u64) ksym_addr, &user_ksyms[0])) 4848 return -EFAULT; 4849 } 4850 } else { 4851 info.jited_ksyms = 0; 4852 } 4853 } 4854 4855 ulen = info.nr_jited_func_lens; 4856 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; 4857 if (ulen) { 4858 if (bpf_dump_raw_ok(file->f_cred)) { 4859 u32 __user *user_lens; 4860 u32 func_len, i; 4861 4862 /* copy the JITed image lengths for each function */ 4863 ulen = min_t(u32, info.nr_jited_func_lens, ulen); 4864 user_lens = u64_to_user_ptr(info.jited_func_lens); 4865 if (prog->aux->func_cnt) { 4866 for (i = 0; i < ulen; i++) { 4867 func_len = 4868 prog->aux->func[i]->jited_len; 4869 if (put_user(func_len, &user_lens[i])) 4870 return -EFAULT; 4871 } 4872 } else { 4873 func_len = prog->jited_len; 4874 if (put_user(func_len, &user_lens[0])) 4875 return -EFAULT; 4876 } 4877 } else { 4878 info.jited_func_lens = 0; 4879 } 4880 } 4881 4882 if (prog->aux->btf) 4883 info.btf_id = btf_obj_id(prog->aux->btf); 4884 info.attach_btf_id = prog->aux->attach_btf_id; 4885 if (attach_btf) 4886 info.attach_btf_obj_id = btf_obj_id(attach_btf); 4887 4888 ulen = info.nr_func_info; 4889 info.nr_func_info = prog->aux->func_info_cnt; 4890 if (info.nr_func_info && ulen) { 4891 char __user *user_finfo; 4892 4893 user_finfo = u64_to_user_ptr(info.func_info); 4894 ulen = min_t(u32, info.nr_func_info, ulen); 4895 if (copy_to_user(user_finfo, prog->aux->func_info, 4896 info.func_info_rec_size * ulen)) 4897 return -EFAULT; 4898 } 4899 4900 ulen = info.nr_line_info; 4901 info.nr_line_info = prog->aux->nr_linfo; 4902 if (info.nr_line_info && ulen) { 4903 __u8 __user *user_linfo; 4904 4905 user_linfo = u64_to_user_ptr(info.line_info); 4906 ulen = min_t(u32, info.nr_line_info, ulen); 4907 if (copy_to_user(user_linfo, prog->aux->linfo, 4908 info.line_info_rec_size * ulen)) 4909 return -EFAULT; 4910 } 4911 4912 ulen = info.nr_jited_line_info; 4913 if (prog->aux->jited_linfo) 4914 info.nr_jited_line_info = prog->aux->nr_linfo; 4915 else 4916 info.nr_jited_line_info = 0; 4917 if (info.nr_jited_line_info && ulen) { 4918 if (bpf_dump_raw_ok(file->f_cred)) { 4919 unsigned long line_addr; 4920 __u64 __user *user_linfo; 4921 u32 i; 4922 4923 user_linfo = u64_to_user_ptr(info.jited_line_info); 4924 ulen = min_t(u32, info.nr_jited_line_info, ulen); 4925 for (i = 0; i < ulen; i++) { 4926 line_addr = (unsigned long)prog->aux->jited_linfo[i]; 4927 if (put_user((__u64)line_addr, &user_linfo[i])) 4928 return -EFAULT; 4929 } 4930 } else { 4931 info.jited_line_info = 0; 4932 } 4933 } 4934 4935 ulen = info.nr_prog_tags; 4936 info.nr_prog_tags = prog->aux->func_cnt ? : 1; 4937 if (ulen) { 4938 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; 4939 u32 i; 4940 4941 user_prog_tags = u64_to_user_ptr(info.prog_tags); 4942 ulen = min_t(u32, info.nr_prog_tags, ulen); 4943 if (prog->aux->func_cnt) { 4944 for (i = 0; i < ulen; i++) { 4945 if (copy_to_user(user_prog_tags[i], 4946 prog->aux->func[i]->tag, 4947 BPF_TAG_SIZE)) 4948 return -EFAULT; 4949 } 4950 } else { 4951 if (copy_to_user(user_prog_tags[0], 4952 prog->tag, BPF_TAG_SIZE)) 4953 return -EFAULT; 4954 } 4955 } 4956 4957 done: 4958 if (copy_to_user(uinfo, &info, info_len) || 4959 put_user(info_len, &uattr->info.info_len)) 4960 return -EFAULT; 4961 4962 return 0; 4963 } 4964 4965 static int bpf_map_get_info_by_fd(struct file *file, 4966 struct bpf_map *map, 4967 const union bpf_attr *attr, 4968 union bpf_attr __user *uattr) 4969 { 4970 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 4971 struct bpf_map_info info; 4972 u32 info_len = attr->info.info_len; 4973 int err; 4974 4975 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); 4976 if (err) 4977 return err; 4978 info_len = min_t(u32, sizeof(info), info_len); 4979 4980 memset(&info, 0, sizeof(info)); 4981 info.type = map->map_type; 4982 info.id = map->id; 4983 info.key_size = map->key_size; 4984 info.value_size = map->value_size; 4985 info.max_entries = map->max_entries; 4986 info.map_flags = map->map_flags; 4987 info.map_extra = map->map_extra; 4988 memcpy(info.name, map->name, sizeof(map->name)); 4989 4990 if (map->btf) { 4991 info.btf_id = btf_obj_id(map->btf); 4992 info.btf_key_type_id = map->btf_key_type_id; 4993 info.btf_value_type_id = map->btf_value_type_id; 4994 } 4995 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; 4996 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) 4997 bpf_map_struct_ops_info_fill(&info, map); 4998 4999 if (bpf_map_is_offloaded(map)) { 5000 err = bpf_map_offload_info_fill(&info, map); 5001 if (err) 5002 return err; 5003 } 5004 5005 if (copy_to_user(uinfo, &info, info_len) || 5006 put_user(info_len, &uattr->info.info_len)) 5007 return -EFAULT; 5008 5009 return 0; 5010 } 5011 5012 static int bpf_btf_get_info_by_fd(struct file *file, 5013 struct btf *btf, 5014 const union bpf_attr *attr, 5015 union bpf_attr __user *uattr) 5016 { 5017 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5018 u32 info_len = attr->info.info_len; 5019 int err; 5020 5021 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len); 5022 if (err) 5023 return err; 5024 5025 return btf_get_info_by_fd(btf, attr, uattr); 5026 } 5027 5028 static int bpf_link_get_info_by_fd(struct file *file, 5029 struct bpf_link *link, 5030 const union bpf_attr *attr, 5031 union bpf_attr __user *uattr) 5032 { 5033 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5034 struct bpf_link_info info; 5035 u32 info_len = attr->info.info_len; 5036 int err; 5037 5038 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); 5039 if (err) 5040 return err; 5041 info_len = min_t(u32, sizeof(info), info_len); 5042 5043 memset(&info, 0, sizeof(info)); 5044 if (copy_from_user(&info, uinfo, info_len)) 5045 return -EFAULT; 5046 5047 info.type = link->type; 5048 info.id = link->id; 5049 if (link->prog) 5050 info.prog_id = link->prog->aux->id; 5051 5052 if (link->ops->fill_link_info) { 5053 err = link->ops->fill_link_info(link, &info); 5054 if (err) 5055 return err; 5056 } 5057 5058 if (copy_to_user(uinfo, &info, info_len) || 5059 put_user(info_len, &uattr->info.info_len)) 5060 return -EFAULT; 5061 5062 return 0; 5063 } 5064 5065 5066 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 5067 5068 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 5069 union bpf_attr __user *uattr) 5070 { 5071 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 5072 return -EINVAL; 5073 5074 CLASS(fd, f)(attr->info.bpf_fd); 5075 if (fd_empty(f)) 5076 return -EBADFD; 5077 5078 if (fd_file(f)->f_op == &bpf_prog_fops) 5079 return bpf_prog_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, 5080 uattr); 5081 else if (fd_file(f)->f_op == &bpf_map_fops) 5082 return bpf_map_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, 5083 uattr); 5084 else if (fd_file(f)->f_op == &btf_fops) 5085 return bpf_btf_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, uattr); 5086 else if (fd_file(f)->f_op == &bpf_link_fops || fd_file(f)->f_op == &bpf_link_fops_poll) 5087 return bpf_link_get_info_by_fd(fd_file(f), fd_file(f)->private_data, 5088 attr, uattr); 5089 return -EINVAL; 5090 } 5091 5092 #define BPF_BTF_LOAD_LAST_FIELD btf_token_fd 5093 5094 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size) 5095 { 5096 struct bpf_token *token = NULL; 5097 5098 if (CHECK_ATTR(BPF_BTF_LOAD)) 5099 return -EINVAL; 5100 5101 if (attr->btf_flags & ~BPF_F_TOKEN_FD) 5102 return -EINVAL; 5103 5104 if (attr->btf_flags & BPF_F_TOKEN_FD) { 5105 token = bpf_token_get_from_fd(attr->btf_token_fd); 5106 if (IS_ERR(token)) 5107 return PTR_ERR(token); 5108 if (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) { 5109 bpf_token_put(token); 5110 token = NULL; 5111 } 5112 } 5113 5114 if (!bpf_token_capable(token, CAP_BPF)) { 5115 bpf_token_put(token); 5116 return -EPERM; 5117 } 5118 5119 bpf_token_put(token); 5120 5121 return btf_new_fd(attr, uattr, uattr_size); 5122 } 5123 5124 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id 5125 5126 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) 5127 { 5128 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) 5129 return -EINVAL; 5130 5131 if (!capable(CAP_SYS_ADMIN)) 5132 return -EPERM; 5133 5134 return btf_get_fd_by_id(attr->btf_id); 5135 } 5136 5137 static int bpf_task_fd_query_copy(const union bpf_attr *attr, 5138 union bpf_attr __user *uattr, 5139 u32 prog_id, u32 fd_type, 5140 const char *buf, u64 probe_offset, 5141 u64 probe_addr) 5142 { 5143 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); 5144 u32 len = buf ? strlen(buf) : 0, input_len; 5145 int err = 0; 5146 5147 if (put_user(len, &uattr->task_fd_query.buf_len)) 5148 return -EFAULT; 5149 input_len = attr->task_fd_query.buf_len; 5150 if (input_len && ubuf) { 5151 if (!len) { 5152 /* nothing to copy, just make ubuf NULL terminated */ 5153 char zero = '\0'; 5154 5155 if (put_user(zero, ubuf)) 5156 return -EFAULT; 5157 } else if (input_len >= len + 1) { 5158 /* ubuf can hold the string with NULL terminator */ 5159 if (copy_to_user(ubuf, buf, len + 1)) 5160 return -EFAULT; 5161 } else { 5162 /* ubuf cannot hold the string with NULL terminator, 5163 * do a partial copy with NULL terminator. 5164 */ 5165 char zero = '\0'; 5166 5167 err = -ENOSPC; 5168 if (copy_to_user(ubuf, buf, input_len - 1)) 5169 return -EFAULT; 5170 if (put_user(zero, ubuf + input_len - 1)) 5171 return -EFAULT; 5172 } 5173 } 5174 5175 if (put_user(prog_id, &uattr->task_fd_query.prog_id) || 5176 put_user(fd_type, &uattr->task_fd_query.fd_type) || 5177 put_user(probe_offset, &uattr->task_fd_query.probe_offset) || 5178 put_user(probe_addr, &uattr->task_fd_query.probe_addr)) 5179 return -EFAULT; 5180 5181 return err; 5182 } 5183 5184 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr 5185 5186 static int bpf_task_fd_query(const union bpf_attr *attr, 5187 union bpf_attr __user *uattr) 5188 { 5189 pid_t pid = attr->task_fd_query.pid; 5190 u32 fd = attr->task_fd_query.fd; 5191 const struct perf_event *event; 5192 struct task_struct *task; 5193 struct file *file; 5194 int err; 5195 5196 if (CHECK_ATTR(BPF_TASK_FD_QUERY)) 5197 return -EINVAL; 5198 5199 if (!capable(CAP_SYS_ADMIN)) 5200 return -EPERM; 5201 5202 if (attr->task_fd_query.flags != 0) 5203 return -EINVAL; 5204 5205 rcu_read_lock(); 5206 task = get_pid_task(find_vpid(pid), PIDTYPE_PID); 5207 rcu_read_unlock(); 5208 if (!task) 5209 return -ENOENT; 5210 5211 err = 0; 5212 file = fget_task(task, fd); 5213 put_task_struct(task); 5214 if (!file) 5215 return -EBADF; 5216 5217 if (file->f_op == &bpf_link_fops || file->f_op == &bpf_link_fops_poll) { 5218 struct bpf_link *link = file->private_data; 5219 5220 if (link->ops == &bpf_raw_tp_link_lops) { 5221 struct bpf_raw_tp_link *raw_tp = 5222 container_of(link, struct bpf_raw_tp_link, link); 5223 struct bpf_raw_event_map *btp = raw_tp->btp; 5224 5225 err = bpf_task_fd_query_copy(attr, uattr, 5226 raw_tp->link.prog->aux->id, 5227 BPF_FD_TYPE_RAW_TRACEPOINT, 5228 btp->tp->name, 0, 0); 5229 goto put_file; 5230 } 5231 goto out_not_supp; 5232 } 5233 5234 event = perf_get_event(file); 5235 if (!IS_ERR(event)) { 5236 u64 probe_offset, probe_addr; 5237 u32 prog_id, fd_type; 5238 const char *buf; 5239 5240 err = bpf_get_perf_event_info(event, &prog_id, &fd_type, 5241 &buf, &probe_offset, 5242 &probe_addr, NULL); 5243 if (!err) 5244 err = bpf_task_fd_query_copy(attr, uattr, prog_id, 5245 fd_type, buf, 5246 probe_offset, 5247 probe_addr); 5248 goto put_file; 5249 } 5250 5251 out_not_supp: 5252 err = -ENOTSUPP; 5253 put_file: 5254 fput(file); 5255 return err; 5256 } 5257 5258 #define BPF_MAP_BATCH_LAST_FIELD batch.flags 5259 5260 #define BPF_DO_BATCH(fn, ...) \ 5261 do { \ 5262 if (!fn) { \ 5263 err = -ENOTSUPP; \ 5264 goto err_put; \ 5265 } \ 5266 err = fn(__VA_ARGS__); \ 5267 } while (0) 5268 5269 static int bpf_map_do_batch(const union bpf_attr *attr, 5270 union bpf_attr __user *uattr, 5271 int cmd) 5272 { 5273 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH || 5274 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH; 5275 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH; 5276 struct bpf_map *map; 5277 int err; 5278 5279 if (CHECK_ATTR(BPF_MAP_BATCH)) 5280 return -EINVAL; 5281 5282 CLASS(fd, f)(attr->batch.map_fd); 5283 5284 map = __bpf_map_get(f); 5285 if (IS_ERR(map)) 5286 return PTR_ERR(map); 5287 if (has_write) 5288 bpf_map_write_active_inc(map); 5289 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 5290 err = -EPERM; 5291 goto err_put; 5292 } 5293 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 5294 err = -EPERM; 5295 goto err_put; 5296 } 5297 5298 if (cmd == BPF_MAP_LOOKUP_BATCH) 5299 BPF_DO_BATCH(map->ops->map_lookup_batch, map, attr, uattr); 5300 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) 5301 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch, map, attr, uattr); 5302 else if (cmd == BPF_MAP_UPDATE_BATCH) 5303 BPF_DO_BATCH(map->ops->map_update_batch, map, fd_file(f), attr, uattr); 5304 else 5305 BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr); 5306 err_put: 5307 if (has_write) { 5308 maybe_wait_bpf_programs(map); 5309 bpf_map_write_active_dec(map); 5310 } 5311 return err; 5312 } 5313 5314 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.pid 5315 static int link_create(union bpf_attr *attr, bpfptr_t uattr) 5316 { 5317 struct bpf_prog *prog; 5318 int ret; 5319 5320 if (CHECK_ATTR(BPF_LINK_CREATE)) 5321 return -EINVAL; 5322 5323 if (attr->link_create.attach_type == BPF_STRUCT_OPS) 5324 return bpf_struct_ops_link_create(attr); 5325 5326 prog = bpf_prog_get(attr->link_create.prog_fd); 5327 if (IS_ERR(prog)) 5328 return PTR_ERR(prog); 5329 5330 ret = bpf_prog_attach_check_attach_type(prog, 5331 attr->link_create.attach_type); 5332 if (ret) 5333 goto out; 5334 5335 switch (prog->type) { 5336 case BPF_PROG_TYPE_CGROUP_SKB: 5337 case BPF_PROG_TYPE_CGROUP_SOCK: 5338 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 5339 case BPF_PROG_TYPE_SOCK_OPS: 5340 case BPF_PROG_TYPE_CGROUP_DEVICE: 5341 case BPF_PROG_TYPE_CGROUP_SYSCTL: 5342 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 5343 ret = cgroup_bpf_link_attach(attr, prog); 5344 break; 5345 case BPF_PROG_TYPE_EXT: 5346 ret = bpf_tracing_prog_attach(prog, 5347 attr->link_create.target_fd, 5348 attr->link_create.target_btf_id, 5349 attr->link_create.tracing.cookie); 5350 break; 5351 case BPF_PROG_TYPE_LSM: 5352 case BPF_PROG_TYPE_TRACING: 5353 if (attr->link_create.attach_type != prog->expected_attach_type) { 5354 ret = -EINVAL; 5355 goto out; 5356 } 5357 if (prog->expected_attach_type == BPF_TRACE_RAW_TP) 5358 ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie); 5359 else if (prog->expected_attach_type == BPF_TRACE_ITER) 5360 ret = bpf_iter_link_attach(attr, uattr, prog); 5361 else if (prog->expected_attach_type == BPF_LSM_CGROUP) 5362 ret = cgroup_bpf_link_attach(attr, prog); 5363 else 5364 ret = bpf_tracing_prog_attach(prog, 5365 attr->link_create.target_fd, 5366 attr->link_create.target_btf_id, 5367 attr->link_create.tracing.cookie); 5368 break; 5369 case BPF_PROG_TYPE_FLOW_DISSECTOR: 5370 case BPF_PROG_TYPE_SK_LOOKUP: 5371 ret = netns_bpf_link_create(attr, prog); 5372 break; 5373 case BPF_PROG_TYPE_SK_MSG: 5374 case BPF_PROG_TYPE_SK_SKB: 5375 ret = sock_map_link_create(attr, prog); 5376 break; 5377 #ifdef CONFIG_NET 5378 case BPF_PROG_TYPE_XDP: 5379 ret = bpf_xdp_link_attach(attr, prog); 5380 break; 5381 case BPF_PROG_TYPE_SCHED_CLS: 5382 if (attr->link_create.attach_type == BPF_TCX_INGRESS || 5383 attr->link_create.attach_type == BPF_TCX_EGRESS) 5384 ret = tcx_link_attach(attr, prog); 5385 else 5386 ret = netkit_link_attach(attr, prog); 5387 break; 5388 case BPF_PROG_TYPE_NETFILTER: 5389 ret = bpf_nf_link_attach(attr, prog); 5390 break; 5391 #endif 5392 case BPF_PROG_TYPE_PERF_EVENT: 5393 case BPF_PROG_TYPE_TRACEPOINT: 5394 ret = bpf_perf_link_attach(attr, prog); 5395 break; 5396 case BPF_PROG_TYPE_KPROBE: 5397 if (attr->link_create.attach_type == BPF_PERF_EVENT) 5398 ret = bpf_perf_link_attach(attr, prog); 5399 else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI || 5400 attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION) 5401 ret = bpf_kprobe_multi_link_attach(attr, prog); 5402 else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI || 5403 attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION) 5404 ret = bpf_uprobe_multi_link_attach(attr, prog); 5405 break; 5406 default: 5407 ret = -EINVAL; 5408 } 5409 5410 out: 5411 if (ret < 0) 5412 bpf_prog_put(prog); 5413 return ret; 5414 } 5415 5416 static int link_update_map(struct bpf_link *link, union bpf_attr *attr) 5417 { 5418 struct bpf_map *new_map, *old_map = NULL; 5419 int ret; 5420 5421 new_map = bpf_map_get(attr->link_update.new_map_fd); 5422 if (IS_ERR(new_map)) 5423 return PTR_ERR(new_map); 5424 5425 if (attr->link_update.flags & BPF_F_REPLACE) { 5426 old_map = bpf_map_get(attr->link_update.old_map_fd); 5427 if (IS_ERR(old_map)) { 5428 ret = PTR_ERR(old_map); 5429 goto out_put; 5430 } 5431 } else if (attr->link_update.old_map_fd) { 5432 ret = -EINVAL; 5433 goto out_put; 5434 } 5435 5436 ret = link->ops->update_map(link, new_map, old_map); 5437 5438 if (old_map) 5439 bpf_map_put(old_map); 5440 out_put: 5441 bpf_map_put(new_map); 5442 return ret; 5443 } 5444 5445 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd 5446 5447 static int link_update(union bpf_attr *attr) 5448 { 5449 struct bpf_prog *old_prog = NULL, *new_prog; 5450 struct bpf_link *link; 5451 u32 flags; 5452 int ret; 5453 5454 if (CHECK_ATTR(BPF_LINK_UPDATE)) 5455 return -EINVAL; 5456 5457 flags = attr->link_update.flags; 5458 if (flags & ~BPF_F_REPLACE) 5459 return -EINVAL; 5460 5461 link = bpf_link_get_from_fd(attr->link_update.link_fd); 5462 if (IS_ERR(link)) 5463 return PTR_ERR(link); 5464 5465 if (link->ops->update_map) { 5466 ret = link_update_map(link, attr); 5467 goto out_put_link; 5468 } 5469 5470 new_prog = bpf_prog_get(attr->link_update.new_prog_fd); 5471 if (IS_ERR(new_prog)) { 5472 ret = PTR_ERR(new_prog); 5473 goto out_put_link; 5474 } 5475 5476 if (flags & BPF_F_REPLACE) { 5477 old_prog = bpf_prog_get(attr->link_update.old_prog_fd); 5478 if (IS_ERR(old_prog)) { 5479 ret = PTR_ERR(old_prog); 5480 old_prog = NULL; 5481 goto out_put_progs; 5482 } 5483 } else if (attr->link_update.old_prog_fd) { 5484 ret = -EINVAL; 5485 goto out_put_progs; 5486 } 5487 5488 if (link->ops->update_prog) 5489 ret = link->ops->update_prog(link, new_prog, old_prog); 5490 else 5491 ret = -EINVAL; 5492 5493 out_put_progs: 5494 if (old_prog) 5495 bpf_prog_put(old_prog); 5496 if (ret) 5497 bpf_prog_put(new_prog); 5498 out_put_link: 5499 bpf_link_put_direct(link); 5500 return ret; 5501 } 5502 5503 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd 5504 5505 static int link_detach(union bpf_attr *attr) 5506 { 5507 struct bpf_link *link; 5508 int ret; 5509 5510 if (CHECK_ATTR(BPF_LINK_DETACH)) 5511 return -EINVAL; 5512 5513 link = bpf_link_get_from_fd(attr->link_detach.link_fd); 5514 if (IS_ERR(link)) 5515 return PTR_ERR(link); 5516 5517 if (link->ops->detach) 5518 ret = link->ops->detach(link); 5519 else 5520 ret = -EOPNOTSUPP; 5521 5522 bpf_link_put_direct(link); 5523 return ret; 5524 } 5525 5526 struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) 5527 { 5528 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT); 5529 } 5530 EXPORT_SYMBOL(bpf_link_inc_not_zero); 5531 5532 struct bpf_link *bpf_link_by_id(u32 id) 5533 { 5534 struct bpf_link *link; 5535 5536 if (!id) 5537 return ERR_PTR(-ENOENT); 5538 5539 spin_lock_bh(&link_idr_lock); 5540 /* before link is "settled", ID is 0, pretend it doesn't exist yet */ 5541 link = idr_find(&link_idr, id); 5542 if (link) { 5543 if (link->id) 5544 link = bpf_link_inc_not_zero(link); 5545 else 5546 link = ERR_PTR(-EAGAIN); 5547 } else { 5548 link = ERR_PTR(-ENOENT); 5549 } 5550 spin_unlock_bh(&link_idr_lock); 5551 return link; 5552 } 5553 5554 struct bpf_link *bpf_link_get_curr_or_next(u32 *id) 5555 { 5556 struct bpf_link *link; 5557 5558 spin_lock_bh(&link_idr_lock); 5559 again: 5560 link = idr_get_next(&link_idr, id); 5561 if (link) { 5562 link = bpf_link_inc_not_zero(link); 5563 if (IS_ERR(link)) { 5564 (*id)++; 5565 goto again; 5566 } 5567 } 5568 spin_unlock_bh(&link_idr_lock); 5569 5570 return link; 5571 } 5572 5573 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id 5574 5575 static int bpf_link_get_fd_by_id(const union bpf_attr *attr) 5576 { 5577 struct bpf_link *link; 5578 u32 id = attr->link_id; 5579 int fd; 5580 5581 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) 5582 return -EINVAL; 5583 5584 if (!capable(CAP_SYS_ADMIN)) 5585 return -EPERM; 5586 5587 link = bpf_link_by_id(id); 5588 if (IS_ERR(link)) 5589 return PTR_ERR(link); 5590 5591 fd = bpf_link_new_fd(link); 5592 if (fd < 0) 5593 bpf_link_put_direct(link); 5594 5595 return fd; 5596 } 5597 5598 DEFINE_MUTEX(bpf_stats_enabled_mutex); 5599 5600 static int bpf_stats_release(struct inode *inode, struct file *file) 5601 { 5602 mutex_lock(&bpf_stats_enabled_mutex); 5603 static_key_slow_dec(&bpf_stats_enabled_key.key); 5604 mutex_unlock(&bpf_stats_enabled_mutex); 5605 return 0; 5606 } 5607 5608 static const struct file_operations bpf_stats_fops = { 5609 .release = bpf_stats_release, 5610 }; 5611 5612 static int bpf_enable_runtime_stats(void) 5613 { 5614 int fd; 5615 5616 mutex_lock(&bpf_stats_enabled_mutex); 5617 5618 /* Set a very high limit to avoid overflow */ 5619 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { 5620 mutex_unlock(&bpf_stats_enabled_mutex); 5621 return -EBUSY; 5622 } 5623 5624 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); 5625 if (fd >= 0) 5626 static_key_slow_inc(&bpf_stats_enabled_key.key); 5627 5628 mutex_unlock(&bpf_stats_enabled_mutex); 5629 return fd; 5630 } 5631 5632 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type 5633 5634 static int bpf_enable_stats(union bpf_attr *attr) 5635 { 5636 5637 if (CHECK_ATTR(BPF_ENABLE_STATS)) 5638 return -EINVAL; 5639 5640 if (!capable(CAP_SYS_ADMIN)) 5641 return -EPERM; 5642 5643 switch (attr->enable_stats.type) { 5644 case BPF_STATS_RUN_TIME: 5645 return bpf_enable_runtime_stats(); 5646 default: 5647 break; 5648 } 5649 return -EINVAL; 5650 } 5651 5652 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags 5653 5654 static int bpf_iter_create(union bpf_attr *attr) 5655 { 5656 struct bpf_link *link; 5657 int err; 5658 5659 if (CHECK_ATTR(BPF_ITER_CREATE)) 5660 return -EINVAL; 5661 5662 if (attr->iter_create.flags) 5663 return -EINVAL; 5664 5665 link = bpf_link_get_from_fd(attr->iter_create.link_fd); 5666 if (IS_ERR(link)) 5667 return PTR_ERR(link); 5668 5669 err = bpf_iter_new_fd(link); 5670 bpf_link_put_direct(link); 5671 5672 return err; 5673 } 5674 5675 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags 5676 5677 static int bpf_prog_bind_map(union bpf_attr *attr) 5678 { 5679 struct bpf_prog *prog; 5680 struct bpf_map *map; 5681 struct bpf_map **used_maps_old, **used_maps_new; 5682 int i, ret = 0; 5683 5684 if (CHECK_ATTR(BPF_PROG_BIND_MAP)) 5685 return -EINVAL; 5686 5687 if (attr->prog_bind_map.flags) 5688 return -EINVAL; 5689 5690 prog = bpf_prog_get(attr->prog_bind_map.prog_fd); 5691 if (IS_ERR(prog)) 5692 return PTR_ERR(prog); 5693 5694 map = bpf_map_get(attr->prog_bind_map.map_fd); 5695 if (IS_ERR(map)) { 5696 ret = PTR_ERR(map); 5697 goto out_prog_put; 5698 } 5699 5700 mutex_lock(&prog->aux->used_maps_mutex); 5701 5702 used_maps_old = prog->aux->used_maps; 5703 5704 for (i = 0; i < prog->aux->used_map_cnt; i++) 5705 if (used_maps_old[i] == map) { 5706 bpf_map_put(map); 5707 goto out_unlock; 5708 } 5709 5710 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1, 5711 sizeof(used_maps_new[0]), 5712 GFP_KERNEL); 5713 if (!used_maps_new) { 5714 ret = -ENOMEM; 5715 goto out_unlock; 5716 } 5717 5718 /* The bpf program will not access the bpf map, but for the sake of 5719 * simplicity, increase sleepable_refcnt for sleepable program as well. 5720 */ 5721 if (prog->sleepable) 5722 atomic64_inc(&map->sleepable_refcnt); 5723 memcpy(used_maps_new, used_maps_old, 5724 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt); 5725 used_maps_new[prog->aux->used_map_cnt] = map; 5726 5727 prog->aux->used_map_cnt++; 5728 prog->aux->used_maps = used_maps_new; 5729 5730 kfree(used_maps_old); 5731 5732 out_unlock: 5733 mutex_unlock(&prog->aux->used_maps_mutex); 5734 5735 if (ret) 5736 bpf_map_put(map); 5737 out_prog_put: 5738 bpf_prog_put(prog); 5739 return ret; 5740 } 5741 5742 #define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd 5743 5744 static int token_create(union bpf_attr *attr) 5745 { 5746 if (CHECK_ATTR(BPF_TOKEN_CREATE)) 5747 return -EINVAL; 5748 5749 /* no flags are supported yet */ 5750 if (attr->token_create.flags) 5751 return -EINVAL; 5752 5753 return bpf_token_create(attr); 5754 } 5755 5756 static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) 5757 { 5758 union bpf_attr attr; 5759 int err; 5760 5761 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); 5762 if (err) 5763 return err; 5764 size = min_t(u32, size, sizeof(attr)); 5765 5766 /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 5767 memset(&attr, 0, sizeof(attr)); 5768 if (copy_from_bpfptr(&attr, uattr, size) != 0) 5769 return -EFAULT; 5770 5771 err = security_bpf(cmd, &attr, size); 5772 if (err < 0) 5773 return err; 5774 5775 switch (cmd) { 5776 case BPF_MAP_CREATE: 5777 err = map_create(&attr); 5778 break; 5779 case BPF_MAP_LOOKUP_ELEM: 5780 err = map_lookup_elem(&attr); 5781 break; 5782 case BPF_MAP_UPDATE_ELEM: 5783 err = map_update_elem(&attr, uattr); 5784 break; 5785 case BPF_MAP_DELETE_ELEM: 5786 err = map_delete_elem(&attr, uattr); 5787 break; 5788 case BPF_MAP_GET_NEXT_KEY: 5789 err = map_get_next_key(&attr); 5790 break; 5791 case BPF_MAP_FREEZE: 5792 err = map_freeze(&attr); 5793 break; 5794 case BPF_PROG_LOAD: 5795 err = bpf_prog_load(&attr, uattr, size); 5796 break; 5797 case BPF_OBJ_PIN: 5798 err = bpf_obj_pin(&attr); 5799 break; 5800 case BPF_OBJ_GET: 5801 err = bpf_obj_get(&attr); 5802 break; 5803 case BPF_PROG_ATTACH: 5804 err = bpf_prog_attach(&attr); 5805 break; 5806 case BPF_PROG_DETACH: 5807 err = bpf_prog_detach(&attr); 5808 break; 5809 case BPF_PROG_QUERY: 5810 err = bpf_prog_query(&attr, uattr.user); 5811 break; 5812 case BPF_PROG_TEST_RUN: 5813 err = bpf_prog_test_run(&attr, uattr.user); 5814 break; 5815 case BPF_PROG_GET_NEXT_ID: 5816 err = bpf_obj_get_next_id(&attr, uattr.user, 5817 &prog_idr, &prog_idr_lock); 5818 break; 5819 case BPF_MAP_GET_NEXT_ID: 5820 err = bpf_obj_get_next_id(&attr, uattr.user, 5821 &map_idr, &map_idr_lock); 5822 break; 5823 case BPF_BTF_GET_NEXT_ID: 5824 err = bpf_obj_get_next_id(&attr, uattr.user, 5825 &btf_idr, &btf_idr_lock); 5826 break; 5827 case BPF_PROG_GET_FD_BY_ID: 5828 err = bpf_prog_get_fd_by_id(&attr); 5829 break; 5830 case BPF_MAP_GET_FD_BY_ID: 5831 err = bpf_map_get_fd_by_id(&attr); 5832 break; 5833 case BPF_OBJ_GET_INFO_BY_FD: 5834 err = bpf_obj_get_info_by_fd(&attr, uattr.user); 5835 break; 5836 case BPF_RAW_TRACEPOINT_OPEN: 5837 err = bpf_raw_tracepoint_open(&attr); 5838 break; 5839 case BPF_BTF_LOAD: 5840 err = bpf_btf_load(&attr, uattr, size); 5841 break; 5842 case BPF_BTF_GET_FD_BY_ID: 5843 err = bpf_btf_get_fd_by_id(&attr); 5844 break; 5845 case BPF_TASK_FD_QUERY: 5846 err = bpf_task_fd_query(&attr, uattr.user); 5847 break; 5848 case BPF_MAP_LOOKUP_AND_DELETE_ELEM: 5849 err = map_lookup_and_delete_elem(&attr); 5850 break; 5851 case BPF_MAP_LOOKUP_BATCH: 5852 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH); 5853 break; 5854 case BPF_MAP_LOOKUP_AND_DELETE_BATCH: 5855 err = bpf_map_do_batch(&attr, uattr.user, 5856 BPF_MAP_LOOKUP_AND_DELETE_BATCH); 5857 break; 5858 case BPF_MAP_UPDATE_BATCH: 5859 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH); 5860 break; 5861 case BPF_MAP_DELETE_BATCH: 5862 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH); 5863 break; 5864 case BPF_LINK_CREATE: 5865 err = link_create(&attr, uattr); 5866 break; 5867 case BPF_LINK_UPDATE: 5868 err = link_update(&attr); 5869 break; 5870 case BPF_LINK_GET_FD_BY_ID: 5871 err = bpf_link_get_fd_by_id(&attr); 5872 break; 5873 case BPF_LINK_GET_NEXT_ID: 5874 err = bpf_obj_get_next_id(&attr, uattr.user, 5875 &link_idr, &link_idr_lock); 5876 break; 5877 case BPF_ENABLE_STATS: 5878 err = bpf_enable_stats(&attr); 5879 break; 5880 case BPF_ITER_CREATE: 5881 err = bpf_iter_create(&attr); 5882 break; 5883 case BPF_LINK_DETACH: 5884 err = link_detach(&attr); 5885 break; 5886 case BPF_PROG_BIND_MAP: 5887 err = bpf_prog_bind_map(&attr); 5888 break; 5889 case BPF_TOKEN_CREATE: 5890 err = token_create(&attr); 5891 break; 5892 default: 5893 err = -EINVAL; 5894 break; 5895 } 5896 5897 return err; 5898 } 5899 5900 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) 5901 { 5902 return __sys_bpf(cmd, USER_BPFPTR(uattr), size); 5903 } 5904 5905 static bool syscall_prog_is_valid_access(int off, int size, 5906 enum bpf_access_type type, 5907 const struct bpf_prog *prog, 5908 struct bpf_insn_access_aux *info) 5909 { 5910 if (off < 0 || off >= U16_MAX) 5911 return false; 5912 if (off % size != 0) 5913 return false; 5914 return true; 5915 } 5916 5917 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size) 5918 { 5919 switch (cmd) { 5920 case BPF_MAP_CREATE: 5921 case BPF_MAP_DELETE_ELEM: 5922 case BPF_MAP_UPDATE_ELEM: 5923 case BPF_MAP_FREEZE: 5924 case BPF_MAP_GET_FD_BY_ID: 5925 case BPF_PROG_LOAD: 5926 case BPF_BTF_LOAD: 5927 case BPF_LINK_CREATE: 5928 case BPF_RAW_TRACEPOINT_OPEN: 5929 break; 5930 default: 5931 return -EINVAL; 5932 } 5933 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size); 5934 } 5935 5936 5937 /* To shut up -Wmissing-prototypes. 5938 * This function is used by the kernel light skeleton 5939 * to load bpf programs when modules are loaded or during kernel boot. 5940 * See tools/lib/bpf/skel_internal.h 5941 */ 5942 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size); 5943 5944 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size) 5945 { 5946 struct bpf_prog * __maybe_unused prog; 5947 struct bpf_tramp_run_ctx __maybe_unused run_ctx; 5948 5949 switch (cmd) { 5950 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */ 5951 case BPF_PROG_TEST_RUN: 5952 if (attr->test.data_in || attr->test.data_out || 5953 attr->test.ctx_out || attr->test.duration || 5954 attr->test.repeat || attr->test.flags) 5955 return -EINVAL; 5956 5957 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL); 5958 if (IS_ERR(prog)) 5959 return PTR_ERR(prog); 5960 5961 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset || 5962 attr->test.ctx_size_in > U16_MAX) { 5963 bpf_prog_put(prog); 5964 return -EINVAL; 5965 } 5966 5967 run_ctx.bpf_cookie = 0; 5968 if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) { 5969 /* recursion detected */ 5970 __bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx); 5971 bpf_prog_put(prog); 5972 return -EBUSY; 5973 } 5974 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in); 5975 __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */, 5976 &run_ctx); 5977 bpf_prog_put(prog); 5978 return 0; 5979 #endif 5980 default: 5981 return ____bpf_sys_bpf(cmd, attr, size); 5982 } 5983 } 5984 EXPORT_SYMBOL(kern_sys_bpf); 5985 5986 static const struct bpf_func_proto bpf_sys_bpf_proto = { 5987 .func = bpf_sys_bpf, 5988 .gpl_only = false, 5989 .ret_type = RET_INTEGER, 5990 .arg1_type = ARG_ANYTHING, 5991 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5992 .arg3_type = ARG_CONST_SIZE, 5993 }; 5994 5995 const struct bpf_func_proto * __weak 5996 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5997 { 5998 return bpf_base_func_proto(func_id, prog); 5999 } 6000 6001 BPF_CALL_1(bpf_sys_close, u32, fd) 6002 { 6003 /* When bpf program calls this helper there should not be 6004 * an fdget() without matching completed fdput(). 6005 * This helper is allowed in the following callchain only: 6006 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close 6007 */ 6008 return close_fd(fd); 6009 } 6010 6011 static const struct bpf_func_proto bpf_sys_close_proto = { 6012 .func = bpf_sys_close, 6013 .gpl_only = false, 6014 .ret_type = RET_INTEGER, 6015 .arg1_type = ARG_ANYTHING, 6016 }; 6017 6018 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) 6019 { 6020 *res = 0; 6021 if (flags) 6022 return -EINVAL; 6023 6024 if (name_sz <= 1 || name[name_sz - 1]) 6025 return -EINVAL; 6026 6027 if (!bpf_dump_raw_ok(current_cred())) 6028 return -EPERM; 6029 6030 *res = kallsyms_lookup_name(name); 6031 return *res ? 0 : -ENOENT; 6032 } 6033 6034 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { 6035 .func = bpf_kallsyms_lookup_name, 6036 .gpl_only = false, 6037 .ret_type = RET_INTEGER, 6038 .arg1_type = ARG_PTR_TO_MEM, 6039 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 6040 .arg3_type = ARG_ANYTHING, 6041 .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, 6042 .arg4_size = sizeof(u64), 6043 }; 6044 6045 static const struct bpf_func_proto * 6046 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6047 { 6048 switch (func_id) { 6049 case BPF_FUNC_sys_bpf: 6050 return !bpf_token_capable(prog->aux->token, CAP_PERFMON) 6051 ? NULL : &bpf_sys_bpf_proto; 6052 case BPF_FUNC_btf_find_by_name_kind: 6053 return &bpf_btf_find_by_name_kind_proto; 6054 case BPF_FUNC_sys_close: 6055 return &bpf_sys_close_proto; 6056 case BPF_FUNC_kallsyms_lookup_name: 6057 return &bpf_kallsyms_lookup_name_proto; 6058 default: 6059 return tracing_prog_func_proto(func_id, prog); 6060 } 6061 } 6062 6063 const struct bpf_verifier_ops bpf_syscall_verifier_ops = { 6064 .get_func_proto = syscall_prog_func_proto, 6065 .is_valid_access = syscall_prog_is_valid_access, 6066 }; 6067 6068 const struct bpf_prog_ops bpf_syscall_prog_ops = { 6069 .test_run = bpf_prog_test_run_syscall, 6070 }; 6071 6072 #ifdef CONFIG_SYSCTL 6073 static int bpf_stats_handler(const struct ctl_table *table, int write, 6074 void *buffer, size_t *lenp, loff_t *ppos) 6075 { 6076 struct static_key *key = (struct static_key *)table->data; 6077 static int saved_val; 6078 int val, ret; 6079 struct ctl_table tmp = { 6080 .data = &val, 6081 .maxlen = sizeof(val), 6082 .mode = table->mode, 6083 .extra1 = SYSCTL_ZERO, 6084 .extra2 = SYSCTL_ONE, 6085 }; 6086 6087 if (write && !capable(CAP_SYS_ADMIN)) 6088 return -EPERM; 6089 6090 mutex_lock(&bpf_stats_enabled_mutex); 6091 val = saved_val; 6092 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 6093 if (write && !ret && val != saved_val) { 6094 if (val) 6095 static_key_slow_inc(key); 6096 else 6097 static_key_slow_dec(key); 6098 saved_val = val; 6099 } 6100 mutex_unlock(&bpf_stats_enabled_mutex); 6101 return ret; 6102 } 6103 6104 void __weak unpriv_ebpf_notify(int new_state) 6105 { 6106 } 6107 6108 static int bpf_unpriv_handler(const struct ctl_table *table, int write, 6109 void *buffer, size_t *lenp, loff_t *ppos) 6110 { 6111 int ret, unpriv_enable = *(int *)table->data; 6112 bool locked_state = unpriv_enable == 1; 6113 struct ctl_table tmp = *table; 6114 6115 if (write && !capable(CAP_SYS_ADMIN)) 6116 return -EPERM; 6117 6118 tmp.data = &unpriv_enable; 6119 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 6120 if (write && !ret) { 6121 if (locked_state && unpriv_enable != 1) 6122 return -EPERM; 6123 *(int *)table->data = unpriv_enable; 6124 } 6125 6126 if (write) 6127 unpriv_ebpf_notify(unpriv_enable); 6128 6129 return ret; 6130 } 6131 6132 static const struct ctl_table bpf_syscall_table[] = { 6133 { 6134 .procname = "unprivileged_bpf_disabled", 6135 .data = &sysctl_unprivileged_bpf_disabled, 6136 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled), 6137 .mode = 0644, 6138 .proc_handler = bpf_unpriv_handler, 6139 .extra1 = SYSCTL_ZERO, 6140 .extra2 = SYSCTL_TWO, 6141 }, 6142 { 6143 .procname = "bpf_stats_enabled", 6144 .data = &bpf_stats_enabled_key.key, 6145 .mode = 0644, 6146 .proc_handler = bpf_stats_handler, 6147 }, 6148 }; 6149 6150 static int __init bpf_syscall_sysctl_init(void) 6151 { 6152 register_sysctl_init("kernel", bpf_syscall_table); 6153 return 0; 6154 } 6155 late_initcall(bpf_syscall_sysctl_init); 6156 #endif /* CONFIG_SYSCTL */ 6157