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 kfree(prog->aux->ctx_arg_info); 2318 if (prog->aux->attach_btf) 2319 btf_put(prog->aux->attach_btf); 2320 2321 if (deferred) { 2322 if (prog->sleepable) 2323 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu); 2324 else 2325 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); 2326 } else { 2327 __bpf_prog_put_rcu(&prog->aux->rcu); 2328 } 2329 } 2330 2331 static void bpf_prog_put_deferred(struct work_struct *work) 2332 { 2333 struct bpf_prog_aux *aux; 2334 struct bpf_prog *prog; 2335 2336 aux = container_of(work, struct bpf_prog_aux, work); 2337 prog = aux->prog; 2338 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); 2339 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD); 2340 bpf_prog_free_id(prog); 2341 __bpf_prog_put_noref(prog, true); 2342 } 2343 2344 static void __bpf_prog_put(struct bpf_prog *prog) 2345 { 2346 struct bpf_prog_aux *aux = prog->aux; 2347 2348 if (atomic64_dec_and_test(&aux->refcnt)) { 2349 if (in_irq() || irqs_disabled()) { 2350 INIT_WORK(&aux->work, bpf_prog_put_deferred); 2351 schedule_work(&aux->work); 2352 } else { 2353 bpf_prog_put_deferred(&aux->work); 2354 } 2355 } 2356 } 2357 2358 void bpf_prog_put(struct bpf_prog *prog) 2359 { 2360 __bpf_prog_put(prog); 2361 } 2362 EXPORT_SYMBOL_GPL(bpf_prog_put); 2363 2364 static int bpf_prog_release(struct inode *inode, struct file *filp) 2365 { 2366 struct bpf_prog *prog = filp->private_data; 2367 2368 bpf_prog_put(prog); 2369 return 0; 2370 } 2371 2372 struct bpf_prog_kstats { 2373 u64 nsecs; 2374 u64 cnt; 2375 u64 misses; 2376 }; 2377 2378 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog) 2379 { 2380 struct bpf_prog_stats *stats; 2381 unsigned int flags; 2382 2383 stats = this_cpu_ptr(prog->stats); 2384 flags = u64_stats_update_begin_irqsave(&stats->syncp); 2385 u64_stats_inc(&stats->misses); 2386 u64_stats_update_end_irqrestore(&stats->syncp, flags); 2387 } 2388 2389 static void bpf_prog_get_stats(const struct bpf_prog *prog, 2390 struct bpf_prog_kstats *stats) 2391 { 2392 u64 nsecs = 0, cnt = 0, misses = 0; 2393 int cpu; 2394 2395 for_each_possible_cpu(cpu) { 2396 const struct bpf_prog_stats *st; 2397 unsigned int start; 2398 u64 tnsecs, tcnt, tmisses; 2399 2400 st = per_cpu_ptr(prog->stats, cpu); 2401 do { 2402 start = u64_stats_fetch_begin(&st->syncp); 2403 tnsecs = u64_stats_read(&st->nsecs); 2404 tcnt = u64_stats_read(&st->cnt); 2405 tmisses = u64_stats_read(&st->misses); 2406 } while (u64_stats_fetch_retry(&st->syncp, start)); 2407 nsecs += tnsecs; 2408 cnt += tcnt; 2409 misses += tmisses; 2410 } 2411 stats->nsecs = nsecs; 2412 stats->cnt = cnt; 2413 stats->misses = misses; 2414 } 2415 2416 #ifdef CONFIG_PROC_FS 2417 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) 2418 { 2419 const struct bpf_prog *prog = filp->private_data; 2420 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 2421 struct bpf_prog_kstats stats; 2422 2423 bpf_prog_get_stats(prog, &stats); 2424 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 2425 seq_printf(m, 2426 "prog_type:\t%u\n" 2427 "prog_jited:\t%u\n" 2428 "prog_tag:\t%s\n" 2429 "memlock:\t%llu\n" 2430 "prog_id:\t%u\n" 2431 "run_time_ns:\t%llu\n" 2432 "run_cnt:\t%llu\n" 2433 "recursion_misses:\t%llu\n" 2434 "verified_insns:\t%u\n", 2435 prog->type, 2436 prog->jited, 2437 prog_tag, 2438 prog->pages * 1ULL << PAGE_SHIFT, 2439 prog->aux->id, 2440 stats.nsecs, 2441 stats.cnt, 2442 stats.misses, 2443 prog->aux->verified_insns); 2444 } 2445 #endif 2446 2447 const struct file_operations bpf_prog_fops = { 2448 #ifdef CONFIG_PROC_FS 2449 .show_fdinfo = bpf_prog_show_fdinfo, 2450 #endif 2451 .release = bpf_prog_release, 2452 .read = bpf_dummy_read, 2453 .write = bpf_dummy_write, 2454 }; 2455 2456 int bpf_prog_new_fd(struct bpf_prog *prog) 2457 { 2458 int ret; 2459 2460 ret = security_bpf_prog(prog); 2461 if (ret < 0) 2462 return ret; 2463 2464 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, 2465 O_RDWR | O_CLOEXEC); 2466 } 2467 2468 void bpf_prog_add(struct bpf_prog *prog, int i) 2469 { 2470 atomic64_add(i, &prog->aux->refcnt); 2471 } 2472 EXPORT_SYMBOL_GPL(bpf_prog_add); 2473 2474 void bpf_prog_sub(struct bpf_prog *prog, int i) 2475 { 2476 /* Only to be used for undoing previous bpf_prog_add() in some 2477 * error path. We still know that another entity in our call 2478 * path holds a reference to the program, thus atomic_sub() can 2479 * be safely used in such cases! 2480 */ 2481 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); 2482 } 2483 EXPORT_SYMBOL_GPL(bpf_prog_sub); 2484 2485 void bpf_prog_inc(struct bpf_prog *prog) 2486 { 2487 atomic64_inc(&prog->aux->refcnt); 2488 } 2489 EXPORT_SYMBOL_GPL(bpf_prog_inc); 2490 2491 /* prog_idr_lock should have been held */ 2492 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) 2493 { 2494 int refold; 2495 2496 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); 2497 2498 if (!refold) 2499 return ERR_PTR(-ENOENT); 2500 2501 return prog; 2502 } 2503 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); 2504 2505 bool bpf_prog_get_ok(struct bpf_prog *prog, 2506 enum bpf_prog_type *attach_type, bool attach_drv) 2507 { 2508 /* not an attachment, just a refcount inc, always allow */ 2509 if (!attach_type) 2510 return true; 2511 2512 if (prog->type != *attach_type) 2513 return false; 2514 if (bpf_prog_is_offloaded(prog->aux) && !attach_drv) 2515 return false; 2516 2517 return true; 2518 } 2519 2520 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, 2521 bool attach_drv) 2522 { 2523 CLASS(fd, f)(ufd); 2524 struct bpf_prog *prog; 2525 2526 if (fd_empty(f)) 2527 return ERR_PTR(-EBADF); 2528 if (fd_file(f)->f_op != &bpf_prog_fops) 2529 return ERR_PTR(-EINVAL); 2530 2531 prog = fd_file(f)->private_data; 2532 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) 2533 return ERR_PTR(-EINVAL); 2534 2535 bpf_prog_inc(prog); 2536 return prog; 2537 } 2538 2539 struct bpf_prog *bpf_prog_get(u32 ufd) 2540 { 2541 return __bpf_prog_get(ufd, NULL, false); 2542 } 2543 2544 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, 2545 bool attach_drv) 2546 { 2547 return __bpf_prog_get(ufd, &type, attach_drv); 2548 } 2549 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); 2550 2551 /* Initially all BPF programs could be loaded w/o specifying 2552 * expected_attach_type. Later for some of them specifying expected_attach_type 2553 * at load time became required so that program could be validated properly. 2554 * Programs of types that are allowed to be loaded both w/ and w/o (for 2555 * backward compatibility) expected_attach_type, should have the default attach 2556 * type assigned to expected_attach_type for the latter case, so that it can be 2557 * validated later at attach time. 2558 * 2559 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if 2560 * prog type requires it but has some attach types that have to be backward 2561 * compatible. 2562 */ 2563 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) 2564 { 2565 switch (attr->prog_type) { 2566 case BPF_PROG_TYPE_CGROUP_SOCK: 2567 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't 2568 * exist so checking for non-zero is the way to go here. 2569 */ 2570 if (!attr->expected_attach_type) 2571 attr->expected_attach_type = 2572 BPF_CGROUP_INET_SOCK_CREATE; 2573 break; 2574 case BPF_PROG_TYPE_SK_REUSEPORT: 2575 if (!attr->expected_attach_type) 2576 attr->expected_attach_type = 2577 BPF_SK_REUSEPORT_SELECT; 2578 break; 2579 } 2580 } 2581 2582 static int 2583 bpf_prog_load_check_attach(enum bpf_prog_type prog_type, 2584 enum bpf_attach_type expected_attach_type, 2585 struct btf *attach_btf, u32 btf_id, 2586 struct bpf_prog *dst_prog) 2587 { 2588 if (btf_id) { 2589 if (btf_id > BTF_MAX_TYPE) 2590 return -EINVAL; 2591 2592 if (!attach_btf && !dst_prog) 2593 return -EINVAL; 2594 2595 switch (prog_type) { 2596 case BPF_PROG_TYPE_TRACING: 2597 case BPF_PROG_TYPE_LSM: 2598 case BPF_PROG_TYPE_STRUCT_OPS: 2599 case BPF_PROG_TYPE_EXT: 2600 break; 2601 default: 2602 return -EINVAL; 2603 } 2604 } 2605 2606 if (attach_btf && (!btf_id || dst_prog)) 2607 return -EINVAL; 2608 2609 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING && 2610 prog_type != BPF_PROG_TYPE_EXT) 2611 return -EINVAL; 2612 2613 switch (prog_type) { 2614 case BPF_PROG_TYPE_CGROUP_SOCK: 2615 switch (expected_attach_type) { 2616 case BPF_CGROUP_INET_SOCK_CREATE: 2617 case BPF_CGROUP_INET_SOCK_RELEASE: 2618 case BPF_CGROUP_INET4_POST_BIND: 2619 case BPF_CGROUP_INET6_POST_BIND: 2620 return 0; 2621 default: 2622 return -EINVAL; 2623 } 2624 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2625 switch (expected_attach_type) { 2626 case BPF_CGROUP_INET4_BIND: 2627 case BPF_CGROUP_INET6_BIND: 2628 case BPF_CGROUP_INET4_CONNECT: 2629 case BPF_CGROUP_INET6_CONNECT: 2630 case BPF_CGROUP_UNIX_CONNECT: 2631 case BPF_CGROUP_INET4_GETPEERNAME: 2632 case BPF_CGROUP_INET6_GETPEERNAME: 2633 case BPF_CGROUP_UNIX_GETPEERNAME: 2634 case BPF_CGROUP_INET4_GETSOCKNAME: 2635 case BPF_CGROUP_INET6_GETSOCKNAME: 2636 case BPF_CGROUP_UNIX_GETSOCKNAME: 2637 case BPF_CGROUP_UDP4_SENDMSG: 2638 case BPF_CGROUP_UDP6_SENDMSG: 2639 case BPF_CGROUP_UNIX_SENDMSG: 2640 case BPF_CGROUP_UDP4_RECVMSG: 2641 case BPF_CGROUP_UDP6_RECVMSG: 2642 case BPF_CGROUP_UNIX_RECVMSG: 2643 return 0; 2644 default: 2645 return -EINVAL; 2646 } 2647 case BPF_PROG_TYPE_CGROUP_SKB: 2648 switch (expected_attach_type) { 2649 case BPF_CGROUP_INET_INGRESS: 2650 case BPF_CGROUP_INET_EGRESS: 2651 return 0; 2652 default: 2653 return -EINVAL; 2654 } 2655 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2656 switch (expected_attach_type) { 2657 case BPF_CGROUP_SETSOCKOPT: 2658 case BPF_CGROUP_GETSOCKOPT: 2659 return 0; 2660 default: 2661 return -EINVAL; 2662 } 2663 case BPF_PROG_TYPE_SK_LOOKUP: 2664 if (expected_attach_type == BPF_SK_LOOKUP) 2665 return 0; 2666 return -EINVAL; 2667 case BPF_PROG_TYPE_SK_REUSEPORT: 2668 switch (expected_attach_type) { 2669 case BPF_SK_REUSEPORT_SELECT: 2670 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE: 2671 return 0; 2672 default: 2673 return -EINVAL; 2674 } 2675 case BPF_PROG_TYPE_NETFILTER: 2676 if (expected_attach_type == BPF_NETFILTER) 2677 return 0; 2678 return -EINVAL; 2679 case BPF_PROG_TYPE_SYSCALL: 2680 case BPF_PROG_TYPE_EXT: 2681 if (expected_attach_type) 2682 return -EINVAL; 2683 fallthrough; 2684 default: 2685 return 0; 2686 } 2687 } 2688 2689 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type) 2690 { 2691 switch (prog_type) { 2692 case BPF_PROG_TYPE_SCHED_CLS: 2693 case BPF_PROG_TYPE_SCHED_ACT: 2694 case BPF_PROG_TYPE_XDP: 2695 case BPF_PROG_TYPE_LWT_IN: 2696 case BPF_PROG_TYPE_LWT_OUT: 2697 case BPF_PROG_TYPE_LWT_XMIT: 2698 case BPF_PROG_TYPE_LWT_SEG6LOCAL: 2699 case BPF_PROG_TYPE_SK_SKB: 2700 case BPF_PROG_TYPE_SK_MSG: 2701 case BPF_PROG_TYPE_FLOW_DISSECTOR: 2702 case BPF_PROG_TYPE_CGROUP_DEVICE: 2703 case BPF_PROG_TYPE_CGROUP_SOCK: 2704 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2705 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2706 case BPF_PROG_TYPE_CGROUP_SYSCTL: 2707 case BPF_PROG_TYPE_SOCK_OPS: 2708 case BPF_PROG_TYPE_EXT: /* extends any prog */ 2709 case BPF_PROG_TYPE_NETFILTER: 2710 return true; 2711 case BPF_PROG_TYPE_CGROUP_SKB: 2712 /* always unpriv */ 2713 case BPF_PROG_TYPE_SK_REUSEPORT: 2714 /* equivalent to SOCKET_FILTER. need CAP_BPF only */ 2715 default: 2716 return false; 2717 } 2718 } 2719 2720 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) 2721 { 2722 switch (prog_type) { 2723 case BPF_PROG_TYPE_KPROBE: 2724 case BPF_PROG_TYPE_TRACEPOINT: 2725 case BPF_PROG_TYPE_PERF_EVENT: 2726 case BPF_PROG_TYPE_RAW_TRACEPOINT: 2727 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 2728 case BPF_PROG_TYPE_TRACING: 2729 case BPF_PROG_TYPE_LSM: 2730 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */ 2731 case BPF_PROG_TYPE_EXT: /* extends any prog */ 2732 return true; 2733 default: 2734 return false; 2735 } 2736 } 2737 2738 /* last field in 'union bpf_attr' used by this command */ 2739 #define BPF_PROG_LOAD_LAST_FIELD fd_array_cnt 2740 2741 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) 2742 { 2743 enum bpf_prog_type type = attr->prog_type; 2744 struct bpf_prog *prog, *dst_prog = NULL; 2745 struct btf *attach_btf = NULL; 2746 struct bpf_token *token = NULL; 2747 bool bpf_cap; 2748 int err; 2749 char license[128]; 2750 2751 if (CHECK_ATTR(BPF_PROG_LOAD)) 2752 return -EINVAL; 2753 2754 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | 2755 BPF_F_ANY_ALIGNMENT | 2756 BPF_F_TEST_STATE_FREQ | 2757 BPF_F_SLEEPABLE | 2758 BPF_F_TEST_RND_HI32 | 2759 BPF_F_XDP_HAS_FRAGS | 2760 BPF_F_XDP_DEV_BOUND_ONLY | 2761 BPF_F_TEST_REG_INVARIANTS | 2762 BPF_F_TOKEN_FD)) 2763 return -EINVAL; 2764 2765 bpf_prog_load_fixup_attach_type(attr); 2766 2767 if (attr->prog_flags & BPF_F_TOKEN_FD) { 2768 token = bpf_token_get_from_fd(attr->prog_token_fd); 2769 if (IS_ERR(token)) 2770 return PTR_ERR(token); 2771 /* if current token doesn't grant prog loading permissions, 2772 * then we can't use this token, so ignore it and rely on 2773 * system-wide capabilities checks 2774 */ 2775 if (!bpf_token_allow_cmd(token, BPF_PROG_LOAD) || 2776 !bpf_token_allow_prog_type(token, attr->prog_type, 2777 attr->expected_attach_type)) { 2778 bpf_token_put(token); 2779 token = NULL; 2780 } 2781 } 2782 2783 bpf_cap = bpf_token_capable(token, CAP_BPF); 2784 err = -EPERM; 2785 2786 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && 2787 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && 2788 !bpf_cap) 2789 goto put_token; 2790 2791 /* Intent here is for unprivileged_bpf_disabled to block BPF program 2792 * creation for unprivileged users; other actions depend 2793 * on fd availability and access to bpffs, so are dependent on 2794 * object creation success. Even with unprivileged BPF disabled, 2795 * capability checks are still carried out for these 2796 * and other operations. 2797 */ 2798 if (sysctl_unprivileged_bpf_disabled && !bpf_cap) 2799 goto put_token; 2800 2801 if (attr->insn_cnt == 0 || 2802 attr->insn_cnt > (bpf_cap ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) { 2803 err = -E2BIG; 2804 goto put_token; 2805 } 2806 if (type != BPF_PROG_TYPE_SOCKET_FILTER && 2807 type != BPF_PROG_TYPE_CGROUP_SKB && 2808 !bpf_cap) 2809 goto put_token; 2810 2811 if (is_net_admin_prog_type(type) && !bpf_token_capable(token, CAP_NET_ADMIN)) 2812 goto put_token; 2813 if (is_perfmon_prog_type(type) && !bpf_token_capable(token, CAP_PERFMON)) 2814 goto put_token; 2815 2816 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog 2817 * or btf, we need to check which one it is 2818 */ 2819 if (attr->attach_prog_fd) { 2820 dst_prog = bpf_prog_get(attr->attach_prog_fd); 2821 if (IS_ERR(dst_prog)) { 2822 dst_prog = NULL; 2823 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd); 2824 if (IS_ERR(attach_btf)) { 2825 err = -EINVAL; 2826 goto put_token; 2827 } 2828 if (!btf_is_kernel(attach_btf)) { 2829 /* attaching through specifying bpf_prog's BTF 2830 * objects directly might be supported eventually 2831 */ 2832 btf_put(attach_btf); 2833 err = -ENOTSUPP; 2834 goto put_token; 2835 } 2836 } 2837 } else if (attr->attach_btf_id) { 2838 /* fall back to vmlinux BTF, if BTF type ID is specified */ 2839 attach_btf = bpf_get_btf_vmlinux(); 2840 if (IS_ERR(attach_btf)) { 2841 err = PTR_ERR(attach_btf); 2842 goto put_token; 2843 } 2844 if (!attach_btf) { 2845 err = -EINVAL; 2846 goto put_token; 2847 } 2848 btf_get(attach_btf); 2849 } 2850 2851 if (bpf_prog_load_check_attach(type, attr->expected_attach_type, 2852 attach_btf, attr->attach_btf_id, 2853 dst_prog)) { 2854 if (dst_prog) 2855 bpf_prog_put(dst_prog); 2856 if (attach_btf) 2857 btf_put(attach_btf); 2858 err = -EINVAL; 2859 goto put_token; 2860 } 2861 2862 /* plain bpf_prog allocation */ 2863 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); 2864 if (!prog) { 2865 if (dst_prog) 2866 bpf_prog_put(dst_prog); 2867 if (attach_btf) 2868 btf_put(attach_btf); 2869 err = -EINVAL; 2870 goto put_token; 2871 } 2872 2873 prog->expected_attach_type = attr->expected_attach_type; 2874 prog->sleepable = !!(attr->prog_flags & BPF_F_SLEEPABLE); 2875 prog->aux->attach_btf = attach_btf; 2876 prog->aux->attach_btf_id = attr->attach_btf_id; 2877 prog->aux->dst_prog = dst_prog; 2878 prog->aux->dev_bound = !!attr->prog_ifindex; 2879 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS; 2880 2881 /* move token into prog->aux, reuse taken refcnt */ 2882 prog->aux->token = token; 2883 token = NULL; 2884 2885 prog->aux->user = get_current_user(); 2886 prog->len = attr->insn_cnt; 2887 2888 err = -EFAULT; 2889 if (copy_from_bpfptr(prog->insns, 2890 make_bpfptr(attr->insns, uattr.is_kernel), 2891 bpf_prog_insn_size(prog)) != 0) 2892 goto free_prog; 2893 /* copy eBPF program license from user space */ 2894 if (strncpy_from_bpfptr(license, 2895 make_bpfptr(attr->license, uattr.is_kernel), 2896 sizeof(license) - 1) < 0) 2897 goto free_prog; 2898 license[sizeof(license) - 1] = 0; 2899 2900 /* eBPF programs must be GPL compatible to use GPL-ed functions */ 2901 prog->gpl_compatible = license_is_gpl_compatible(license) ? 1 : 0; 2902 2903 prog->orig_prog = NULL; 2904 prog->jited = 0; 2905 2906 atomic64_set(&prog->aux->refcnt, 1); 2907 2908 if (bpf_prog_is_dev_bound(prog->aux)) { 2909 err = bpf_prog_dev_bound_init(prog, attr); 2910 if (err) 2911 goto free_prog; 2912 } 2913 2914 if (type == BPF_PROG_TYPE_EXT && dst_prog && 2915 bpf_prog_is_dev_bound(dst_prog->aux)) { 2916 err = bpf_prog_dev_bound_inherit(prog, dst_prog); 2917 if (err) 2918 goto free_prog; 2919 } 2920 2921 /* 2922 * Bookkeeping for managing the program attachment chain. 2923 * 2924 * It might be tempting to set attach_tracing_prog flag at the attachment 2925 * time, but this will not prevent from loading bunch of tracing prog 2926 * first, then attach them one to another. 2927 * 2928 * The flag attach_tracing_prog is set for the whole program lifecycle, and 2929 * doesn't have to be cleared in bpf_tracing_link_release, since tracing 2930 * programs cannot change attachment target. 2931 */ 2932 if (type == BPF_PROG_TYPE_TRACING && dst_prog && 2933 dst_prog->type == BPF_PROG_TYPE_TRACING) { 2934 prog->aux->attach_tracing_prog = true; 2935 } 2936 2937 /* find program type: socket_filter vs tracing_filter */ 2938 err = find_prog_type(type, prog); 2939 if (err < 0) 2940 goto free_prog; 2941 2942 prog->aux->load_time = ktime_get_boottime_ns(); 2943 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, 2944 sizeof(attr->prog_name)); 2945 if (err < 0) 2946 goto free_prog; 2947 2948 err = security_bpf_prog_load(prog, attr, token); 2949 if (err) 2950 goto free_prog_sec; 2951 2952 /* run eBPF verifier */ 2953 err = bpf_check(&prog, attr, uattr, uattr_size); 2954 if (err < 0) 2955 goto free_used_maps; 2956 2957 prog = bpf_prog_select_runtime(prog, &err); 2958 if (err < 0) 2959 goto free_used_maps; 2960 2961 err = bpf_prog_alloc_id(prog); 2962 if (err) 2963 goto free_used_maps; 2964 2965 /* Upon success of bpf_prog_alloc_id(), the BPF prog is 2966 * effectively publicly exposed. However, retrieving via 2967 * bpf_prog_get_fd_by_id() will take another reference, 2968 * therefore it cannot be gone underneath us. 2969 * 2970 * Only for the time /after/ successful bpf_prog_new_fd() 2971 * and before returning to userspace, we might just hold 2972 * one reference and any parallel close on that fd could 2973 * rip everything out. Hence, below notifications must 2974 * happen before bpf_prog_new_fd(). 2975 * 2976 * Also, any failure handling from this point onwards must 2977 * be using bpf_prog_put() given the program is exposed. 2978 */ 2979 bpf_prog_kallsyms_add(prog); 2980 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); 2981 bpf_audit_prog(prog, BPF_AUDIT_LOAD); 2982 2983 err = bpf_prog_new_fd(prog); 2984 if (err < 0) 2985 bpf_prog_put(prog); 2986 return err; 2987 2988 free_used_maps: 2989 /* In case we have subprogs, we need to wait for a grace 2990 * period before we can tear down JIT memory since symbols 2991 * are already exposed under kallsyms. 2992 */ 2993 __bpf_prog_put_noref(prog, prog->aux->real_func_cnt); 2994 return err; 2995 2996 free_prog_sec: 2997 security_bpf_prog_free(prog); 2998 free_prog: 2999 free_uid(prog->aux->user); 3000 if (prog->aux->attach_btf) 3001 btf_put(prog->aux->attach_btf); 3002 bpf_prog_free(prog); 3003 put_token: 3004 bpf_token_put(token); 3005 return err; 3006 } 3007 3008 #define BPF_OBJ_LAST_FIELD path_fd 3009 3010 static int bpf_obj_pin(const union bpf_attr *attr) 3011 { 3012 int path_fd; 3013 3014 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags & ~BPF_F_PATH_FD) 3015 return -EINVAL; 3016 3017 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */ 3018 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd) 3019 return -EINVAL; 3020 3021 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD; 3022 return bpf_obj_pin_user(attr->bpf_fd, path_fd, 3023 u64_to_user_ptr(attr->pathname)); 3024 } 3025 3026 static int bpf_obj_get(const union bpf_attr *attr) 3027 { 3028 int path_fd; 3029 3030 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || 3031 attr->file_flags & ~(BPF_OBJ_FLAG_MASK | BPF_F_PATH_FD)) 3032 return -EINVAL; 3033 3034 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */ 3035 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd) 3036 return -EINVAL; 3037 3038 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD; 3039 return bpf_obj_get_user(path_fd, u64_to_user_ptr(attr->pathname), 3040 attr->file_flags); 3041 } 3042 3043 /* bpf_link_init_sleepable() allows to specify whether BPF link itself has 3044 * "sleepable" semantics, which normally would mean that BPF link's attach 3045 * hook can dereference link or link's underlying program for some time after 3046 * detachment due to RCU Tasks Trace-based lifetime protection scheme. 3047 * BPF program itself can be non-sleepable, yet, because it's transitively 3048 * reachable through BPF link, its freeing has to be delayed until after RCU 3049 * Tasks Trace GP. 3050 */ 3051 void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type, 3052 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3053 bool sleepable) 3054 { 3055 WARN_ON(ops->dealloc && ops->dealloc_deferred); 3056 atomic64_set(&link->refcnt, 1); 3057 link->type = type; 3058 link->sleepable = sleepable; 3059 link->id = 0; 3060 link->ops = ops; 3061 link->prog = prog; 3062 } 3063 3064 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 3065 const struct bpf_link_ops *ops, struct bpf_prog *prog) 3066 { 3067 bpf_link_init_sleepable(link, type, ops, prog, false); 3068 } 3069 3070 static void bpf_link_free_id(int id) 3071 { 3072 if (!id) 3073 return; 3074 3075 spin_lock_bh(&link_idr_lock); 3076 idr_remove(&link_idr, id); 3077 spin_unlock_bh(&link_idr_lock); 3078 } 3079 3080 /* Clean up bpf_link and corresponding anon_inode file and FD. After 3081 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred 3082 * anon_inode's release() call. This helper marks bpf_link as 3083 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt 3084 * is not decremented, it's the responsibility of a calling code that failed 3085 * to complete bpf_link initialization. 3086 * This helper eventually calls link's dealloc callback, but does not call 3087 * link's release callback. 3088 */ 3089 void bpf_link_cleanup(struct bpf_link_primer *primer) 3090 { 3091 primer->link->prog = NULL; 3092 bpf_link_free_id(primer->id); 3093 fput(primer->file); 3094 put_unused_fd(primer->fd); 3095 } 3096 3097 void bpf_link_inc(struct bpf_link *link) 3098 { 3099 atomic64_inc(&link->refcnt); 3100 } 3101 3102 static void bpf_link_dealloc(struct bpf_link *link) 3103 { 3104 /* now that we know that bpf_link itself can't be reached, put underlying BPF program */ 3105 if (link->prog) 3106 bpf_prog_put(link->prog); 3107 3108 /* free bpf_link and its containing memory */ 3109 if (link->ops->dealloc_deferred) 3110 link->ops->dealloc_deferred(link); 3111 else 3112 link->ops->dealloc(link); 3113 } 3114 3115 static void bpf_link_defer_dealloc_rcu_gp(struct rcu_head *rcu) 3116 { 3117 struct bpf_link *link = container_of(rcu, struct bpf_link, rcu); 3118 3119 bpf_link_dealloc(link); 3120 } 3121 3122 static void bpf_link_defer_dealloc_mult_rcu_gp(struct rcu_head *rcu) 3123 { 3124 if (rcu_trace_implies_rcu_gp()) 3125 bpf_link_defer_dealloc_rcu_gp(rcu); 3126 else 3127 call_rcu(rcu, bpf_link_defer_dealloc_rcu_gp); 3128 } 3129 3130 /* bpf_link_free is guaranteed to be called from process context */ 3131 static void bpf_link_free(struct bpf_link *link) 3132 { 3133 const struct bpf_link_ops *ops = link->ops; 3134 3135 bpf_link_free_id(link->id); 3136 /* detach BPF program, clean up used resources */ 3137 if (link->prog) 3138 ops->release(link); 3139 if (ops->dealloc_deferred) { 3140 /* Schedule BPF link deallocation, which will only then 3141 * trigger putting BPF program refcount. 3142 * If underlying BPF program is sleepable or BPF link's target 3143 * attach hookpoint is sleepable or otherwise requires RCU GPs 3144 * to ensure link and its underlying BPF program is not 3145 * reachable anymore, we need to first wait for RCU tasks 3146 * trace sync, and then go through "classic" RCU grace period 3147 */ 3148 if (link->sleepable || (link->prog && link->prog->sleepable)) 3149 call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp); 3150 else 3151 call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp); 3152 } else if (ops->dealloc) { 3153 bpf_link_dealloc(link); 3154 } 3155 } 3156 3157 static void bpf_link_put_deferred(struct work_struct *work) 3158 { 3159 struct bpf_link *link = container_of(work, struct bpf_link, work); 3160 3161 bpf_link_free(link); 3162 } 3163 3164 /* bpf_link_put might be called from atomic context. It needs to be called 3165 * from sleepable context in order to acquire sleeping locks during the process. 3166 */ 3167 void bpf_link_put(struct bpf_link *link) 3168 { 3169 if (!atomic64_dec_and_test(&link->refcnt)) 3170 return; 3171 3172 INIT_WORK(&link->work, bpf_link_put_deferred); 3173 schedule_work(&link->work); 3174 } 3175 EXPORT_SYMBOL(bpf_link_put); 3176 3177 static void bpf_link_put_direct(struct bpf_link *link) 3178 { 3179 if (!atomic64_dec_and_test(&link->refcnt)) 3180 return; 3181 bpf_link_free(link); 3182 } 3183 3184 static int bpf_link_release(struct inode *inode, struct file *filp) 3185 { 3186 struct bpf_link *link = filp->private_data; 3187 3188 bpf_link_put_direct(link); 3189 return 0; 3190 } 3191 3192 #ifdef CONFIG_PROC_FS 3193 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 3194 #define BPF_MAP_TYPE(_id, _ops) 3195 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, 3196 static const char *bpf_link_type_strs[] = { 3197 [BPF_LINK_TYPE_UNSPEC] = "<invalid>", 3198 #include <linux/bpf_types.h> 3199 }; 3200 #undef BPF_PROG_TYPE 3201 #undef BPF_MAP_TYPE 3202 #undef BPF_LINK_TYPE 3203 3204 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) 3205 { 3206 const struct bpf_link *link = filp->private_data; 3207 const struct bpf_prog *prog = link->prog; 3208 enum bpf_link_type type = link->type; 3209 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 3210 3211 if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) { 3212 seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]); 3213 } else { 3214 WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type); 3215 seq_printf(m, "link_type:\t<%u>\n", type); 3216 } 3217 seq_printf(m, "link_id:\t%u\n", link->id); 3218 3219 if (prog) { 3220 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 3221 seq_printf(m, 3222 "prog_tag:\t%s\n" 3223 "prog_id:\t%u\n", 3224 prog_tag, 3225 prog->aux->id); 3226 } 3227 if (link->ops->show_fdinfo) 3228 link->ops->show_fdinfo(link, m); 3229 } 3230 #endif 3231 3232 static __poll_t bpf_link_poll(struct file *file, struct poll_table_struct *pts) 3233 { 3234 struct bpf_link *link = file->private_data; 3235 3236 return link->ops->poll(file, pts); 3237 } 3238 3239 static const struct file_operations bpf_link_fops = { 3240 #ifdef CONFIG_PROC_FS 3241 .show_fdinfo = bpf_link_show_fdinfo, 3242 #endif 3243 .release = bpf_link_release, 3244 .read = bpf_dummy_read, 3245 .write = bpf_dummy_write, 3246 }; 3247 3248 static const struct file_operations bpf_link_fops_poll = { 3249 #ifdef CONFIG_PROC_FS 3250 .show_fdinfo = bpf_link_show_fdinfo, 3251 #endif 3252 .release = bpf_link_release, 3253 .read = bpf_dummy_read, 3254 .write = bpf_dummy_write, 3255 .poll = bpf_link_poll, 3256 }; 3257 3258 static int bpf_link_alloc_id(struct bpf_link *link) 3259 { 3260 int id; 3261 3262 idr_preload(GFP_KERNEL); 3263 spin_lock_bh(&link_idr_lock); 3264 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); 3265 spin_unlock_bh(&link_idr_lock); 3266 idr_preload_end(); 3267 3268 return id; 3269 } 3270 3271 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, 3272 * reserving unused FD and allocating ID from link_idr. This is to be paired 3273 * with bpf_link_settle() to install FD and ID and expose bpf_link to 3274 * user-space, if bpf_link is successfully attached. If not, bpf_link and 3275 * pre-allocated resources are to be freed with bpf_cleanup() call. All the 3276 * transient state is passed around in struct bpf_link_primer. 3277 * This is preferred way to create and initialize bpf_link, especially when 3278 * there are complicated and expensive operations in between creating bpf_link 3279 * itself and attaching it to BPF hook. By using bpf_link_prime() and 3280 * bpf_link_settle() kernel code using bpf_link doesn't have to perform 3281 * expensive (and potentially failing) roll back operations in a rare case 3282 * that file, FD, or ID can't be allocated. 3283 */ 3284 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) 3285 { 3286 struct file *file; 3287 int fd, id; 3288 3289 fd = get_unused_fd_flags(O_CLOEXEC); 3290 if (fd < 0) 3291 return fd; 3292 3293 3294 id = bpf_link_alloc_id(link); 3295 if (id < 0) { 3296 put_unused_fd(fd); 3297 return id; 3298 } 3299 3300 file = anon_inode_getfile("bpf_link", 3301 link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, 3302 link, O_CLOEXEC); 3303 if (IS_ERR(file)) { 3304 bpf_link_free_id(id); 3305 put_unused_fd(fd); 3306 return PTR_ERR(file); 3307 } 3308 3309 primer->link = link; 3310 primer->file = file; 3311 primer->fd = fd; 3312 primer->id = id; 3313 return 0; 3314 } 3315 3316 int bpf_link_settle(struct bpf_link_primer *primer) 3317 { 3318 /* make bpf_link fetchable by ID */ 3319 spin_lock_bh(&link_idr_lock); 3320 primer->link->id = primer->id; 3321 spin_unlock_bh(&link_idr_lock); 3322 /* make bpf_link fetchable by FD */ 3323 fd_install(primer->fd, primer->file); 3324 /* pass through installed FD */ 3325 return primer->fd; 3326 } 3327 3328 int bpf_link_new_fd(struct bpf_link *link) 3329 { 3330 return anon_inode_getfd("bpf-link", 3331 link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, 3332 link, O_CLOEXEC); 3333 } 3334 3335 struct bpf_link *bpf_link_get_from_fd(u32 ufd) 3336 { 3337 CLASS(fd, f)(ufd); 3338 struct bpf_link *link; 3339 3340 if (fd_empty(f)) 3341 return ERR_PTR(-EBADF); 3342 if (fd_file(f)->f_op != &bpf_link_fops && fd_file(f)->f_op != &bpf_link_fops_poll) 3343 return ERR_PTR(-EINVAL); 3344 3345 link = fd_file(f)->private_data; 3346 bpf_link_inc(link); 3347 return link; 3348 } 3349 EXPORT_SYMBOL(bpf_link_get_from_fd); 3350 3351 static void bpf_tracing_link_release(struct bpf_link *link) 3352 { 3353 struct bpf_tracing_link *tr_link = 3354 container_of(link, struct bpf_tracing_link, link.link); 3355 3356 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link, 3357 tr_link->trampoline, 3358 tr_link->tgt_prog)); 3359 3360 bpf_trampoline_put(tr_link->trampoline); 3361 3362 /* tgt_prog is NULL if target is a kernel function */ 3363 if (tr_link->tgt_prog) 3364 bpf_prog_put(tr_link->tgt_prog); 3365 } 3366 3367 static void bpf_tracing_link_dealloc(struct bpf_link *link) 3368 { 3369 struct bpf_tracing_link *tr_link = 3370 container_of(link, struct bpf_tracing_link, link.link); 3371 3372 kfree(tr_link); 3373 } 3374 3375 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, 3376 struct seq_file *seq) 3377 { 3378 struct bpf_tracing_link *tr_link = 3379 container_of(link, struct bpf_tracing_link, link.link); 3380 u32 target_btf_id, target_obj_id; 3381 3382 bpf_trampoline_unpack_key(tr_link->trampoline->key, 3383 &target_obj_id, &target_btf_id); 3384 seq_printf(seq, 3385 "attach_type:\t%d\n" 3386 "target_obj_id:\t%u\n" 3387 "target_btf_id:\t%u\n", 3388 tr_link->attach_type, 3389 target_obj_id, 3390 target_btf_id); 3391 } 3392 3393 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, 3394 struct bpf_link_info *info) 3395 { 3396 struct bpf_tracing_link *tr_link = 3397 container_of(link, struct bpf_tracing_link, link.link); 3398 3399 info->tracing.attach_type = tr_link->attach_type; 3400 bpf_trampoline_unpack_key(tr_link->trampoline->key, 3401 &info->tracing.target_obj_id, 3402 &info->tracing.target_btf_id); 3403 3404 return 0; 3405 } 3406 3407 static const struct bpf_link_ops bpf_tracing_link_lops = { 3408 .release = bpf_tracing_link_release, 3409 .dealloc = bpf_tracing_link_dealloc, 3410 .show_fdinfo = bpf_tracing_link_show_fdinfo, 3411 .fill_link_info = bpf_tracing_link_fill_link_info, 3412 }; 3413 3414 static int bpf_tracing_prog_attach(struct bpf_prog *prog, 3415 int tgt_prog_fd, 3416 u32 btf_id, 3417 u64 bpf_cookie) 3418 { 3419 struct bpf_link_primer link_primer; 3420 struct bpf_prog *tgt_prog = NULL; 3421 struct bpf_trampoline *tr = NULL; 3422 struct bpf_tracing_link *link; 3423 u64 key = 0; 3424 int err; 3425 3426 switch (prog->type) { 3427 case BPF_PROG_TYPE_TRACING: 3428 if (prog->expected_attach_type != BPF_TRACE_FENTRY && 3429 prog->expected_attach_type != BPF_TRACE_FEXIT && 3430 prog->expected_attach_type != BPF_MODIFY_RETURN) { 3431 err = -EINVAL; 3432 goto out_put_prog; 3433 } 3434 break; 3435 case BPF_PROG_TYPE_EXT: 3436 if (prog->expected_attach_type != 0) { 3437 err = -EINVAL; 3438 goto out_put_prog; 3439 } 3440 break; 3441 case BPF_PROG_TYPE_LSM: 3442 if (prog->expected_attach_type != BPF_LSM_MAC) { 3443 err = -EINVAL; 3444 goto out_put_prog; 3445 } 3446 break; 3447 default: 3448 err = -EINVAL; 3449 goto out_put_prog; 3450 } 3451 3452 if (!!tgt_prog_fd != !!btf_id) { 3453 err = -EINVAL; 3454 goto out_put_prog; 3455 } 3456 3457 if (tgt_prog_fd) { 3458 /* 3459 * For now we only allow new targets for BPF_PROG_TYPE_EXT. If this 3460 * part would be changed to implement the same for 3461 * BPF_PROG_TYPE_TRACING, do not forget to update the way how 3462 * attach_tracing_prog flag is set. 3463 */ 3464 if (prog->type != BPF_PROG_TYPE_EXT) { 3465 err = -EINVAL; 3466 goto out_put_prog; 3467 } 3468 3469 tgt_prog = bpf_prog_get(tgt_prog_fd); 3470 if (IS_ERR(tgt_prog)) { 3471 err = PTR_ERR(tgt_prog); 3472 tgt_prog = NULL; 3473 goto out_put_prog; 3474 } 3475 3476 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id); 3477 } 3478 3479 link = kzalloc(sizeof(*link), GFP_USER); 3480 if (!link) { 3481 err = -ENOMEM; 3482 goto out_put_prog; 3483 } 3484 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING, 3485 &bpf_tracing_link_lops, prog); 3486 link->attach_type = prog->expected_attach_type; 3487 link->link.cookie = bpf_cookie; 3488 3489 mutex_lock(&prog->aux->dst_mutex); 3490 3491 /* There are a few possible cases here: 3492 * 3493 * - if prog->aux->dst_trampoline is set, the program was just loaded 3494 * and not yet attached to anything, so we can use the values stored 3495 * in prog->aux 3496 * 3497 * - if prog->aux->dst_trampoline is NULL, the program has already been 3498 * attached to a target and its initial target was cleared (below) 3499 * 3500 * - if tgt_prog != NULL, the caller specified tgt_prog_fd + 3501 * target_btf_id using the link_create API. 3502 * 3503 * - if tgt_prog == NULL when this function was called using the old 3504 * raw_tracepoint_open API, and we need a target from prog->aux 3505 * 3506 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program 3507 * was detached and is going for re-attachment. 3508 * 3509 * - if prog->aux->dst_trampoline is NULL and tgt_prog and prog->aux->attach_btf 3510 * are NULL, then program was already attached and user did not provide 3511 * tgt_prog_fd so we have no way to find out or create trampoline 3512 */ 3513 if (!prog->aux->dst_trampoline && !tgt_prog) { 3514 /* 3515 * Allow re-attach for TRACING and LSM programs. If it's 3516 * currently linked, bpf_trampoline_link_prog will fail. 3517 * EXT programs need to specify tgt_prog_fd, so they 3518 * re-attach in separate code path. 3519 */ 3520 if (prog->type != BPF_PROG_TYPE_TRACING && 3521 prog->type != BPF_PROG_TYPE_LSM) { 3522 err = -EINVAL; 3523 goto out_unlock; 3524 } 3525 /* We can allow re-attach only if we have valid attach_btf. */ 3526 if (!prog->aux->attach_btf) { 3527 err = -EINVAL; 3528 goto out_unlock; 3529 } 3530 btf_id = prog->aux->attach_btf_id; 3531 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id); 3532 } 3533 3534 if (!prog->aux->dst_trampoline || 3535 (key && key != prog->aux->dst_trampoline->key)) { 3536 /* If there is no saved target, or the specified target is 3537 * different from the destination specified at load time, we 3538 * need a new trampoline and a check for compatibility 3539 */ 3540 struct bpf_attach_target_info tgt_info = {}; 3541 3542 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id, 3543 &tgt_info); 3544 if (err) 3545 goto out_unlock; 3546 3547 if (tgt_info.tgt_mod) { 3548 module_put(prog->aux->mod); 3549 prog->aux->mod = tgt_info.tgt_mod; 3550 } 3551 3552 tr = bpf_trampoline_get(key, &tgt_info); 3553 if (!tr) { 3554 err = -ENOMEM; 3555 goto out_unlock; 3556 } 3557 } else { 3558 /* The caller didn't specify a target, or the target was the 3559 * same as the destination supplied during program load. This 3560 * means we can reuse the trampoline and reference from program 3561 * load time, and there is no need to allocate a new one. This 3562 * can only happen once for any program, as the saved values in 3563 * prog->aux are cleared below. 3564 */ 3565 tr = prog->aux->dst_trampoline; 3566 tgt_prog = prog->aux->dst_prog; 3567 } 3568 3569 err = bpf_link_prime(&link->link.link, &link_primer); 3570 if (err) 3571 goto out_unlock; 3572 3573 err = bpf_trampoline_link_prog(&link->link, tr, tgt_prog); 3574 if (err) { 3575 bpf_link_cleanup(&link_primer); 3576 link = NULL; 3577 goto out_unlock; 3578 } 3579 3580 link->tgt_prog = tgt_prog; 3581 link->trampoline = tr; 3582 3583 /* Always clear the trampoline and target prog from prog->aux to make 3584 * sure the original attach destination is not kept alive after a 3585 * program is (re-)attached to another target. 3586 */ 3587 if (prog->aux->dst_prog && 3588 (tgt_prog_fd || tr != prog->aux->dst_trampoline)) 3589 /* got extra prog ref from syscall, or attaching to different prog */ 3590 bpf_prog_put(prog->aux->dst_prog); 3591 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline) 3592 /* we allocated a new trampoline, so free the old one */ 3593 bpf_trampoline_put(prog->aux->dst_trampoline); 3594 3595 prog->aux->dst_prog = NULL; 3596 prog->aux->dst_trampoline = NULL; 3597 mutex_unlock(&prog->aux->dst_mutex); 3598 3599 return bpf_link_settle(&link_primer); 3600 out_unlock: 3601 if (tr && tr != prog->aux->dst_trampoline) 3602 bpf_trampoline_put(tr); 3603 mutex_unlock(&prog->aux->dst_mutex); 3604 kfree(link); 3605 out_put_prog: 3606 if (tgt_prog_fd && tgt_prog) 3607 bpf_prog_put(tgt_prog); 3608 return err; 3609 } 3610 3611 static void bpf_raw_tp_link_release(struct bpf_link *link) 3612 { 3613 struct bpf_raw_tp_link *raw_tp = 3614 container_of(link, struct bpf_raw_tp_link, link); 3615 3616 bpf_probe_unregister(raw_tp->btp, raw_tp); 3617 bpf_put_raw_tracepoint(raw_tp->btp); 3618 } 3619 3620 static void bpf_raw_tp_link_dealloc(struct bpf_link *link) 3621 { 3622 struct bpf_raw_tp_link *raw_tp = 3623 container_of(link, struct bpf_raw_tp_link, link); 3624 3625 kfree(raw_tp); 3626 } 3627 3628 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, 3629 struct seq_file *seq) 3630 { 3631 struct bpf_raw_tp_link *raw_tp_link = 3632 container_of(link, struct bpf_raw_tp_link, link); 3633 3634 seq_printf(seq, 3635 "tp_name:\t%s\n", 3636 raw_tp_link->btp->tp->name); 3637 } 3638 3639 static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen, 3640 u32 len) 3641 { 3642 if (ulen >= len + 1) { 3643 if (copy_to_user(ubuf, buf, len + 1)) 3644 return -EFAULT; 3645 } else { 3646 char zero = '\0'; 3647 3648 if (copy_to_user(ubuf, buf, ulen - 1)) 3649 return -EFAULT; 3650 if (put_user(zero, ubuf + ulen - 1)) 3651 return -EFAULT; 3652 return -ENOSPC; 3653 } 3654 3655 return 0; 3656 } 3657 3658 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, 3659 struct bpf_link_info *info) 3660 { 3661 struct bpf_raw_tp_link *raw_tp_link = 3662 container_of(link, struct bpf_raw_tp_link, link); 3663 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); 3664 const char *tp_name = raw_tp_link->btp->tp->name; 3665 u32 ulen = info->raw_tracepoint.tp_name_len; 3666 size_t tp_len = strlen(tp_name); 3667 3668 if (!ulen ^ !ubuf) 3669 return -EINVAL; 3670 3671 info->raw_tracepoint.tp_name_len = tp_len + 1; 3672 3673 if (!ubuf) 3674 return 0; 3675 3676 return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len); 3677 } 3678 3679 static const struct bpf_link_ops bpf_raw_tp_link_lops = { 3680 .release = bpf_raw_tp_link_release, 3681 .dealloc_deferred = bpf_raw_tp_link_dealloc, 3682 .show_fdinfo = bpf_raw_tp_link_show_fdinfo, 3683 .fill_link_info = bpf_raw_tp_link_fill_link_info, 3684 }; 3685 3686 #ifdef CONFIG_PERF_EVENTS 3687 struct bpf_perf_link { 3688 struct bpf_link link; 3689 struct file *perf_file; 3690 }; 3691 3692 static void bpf_perf_link_release(struct bpf_link *link) 3693 { 3694 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); 3695 struct perf_event *event = perf_link->perf_file->private_data; 3696 3697 perf_event_free_bpf_prog(event); 3698 fput(perf_link->perf_file); 3699 } 3700 3701 static void bpf_perf_link_dealloc(struct bpf_link *link) 3702 { 3703 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); 3704 3705 kfree(perf_link); 3706 } 3707 3708 static int bpf_perf_link_fill_common(const struct perf_event *event, 3709 char __user *uname, u32 *ulenp, 3710 u64 *probe_offset, u64 *probe_addr, 3711 u32 *fd_type, unsigned long *missed) 3712 { 3713 const char *buf; 3714 u32 prog_id, ulen; 3715 size_t len; 3716 int err; 3717 3718 ulen = *ulenp; 3719 if (!ulen ^ !uname) 3720 return -EINVAL; 3721 3722 err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf, 3723 probe_offset, probe_addr, missed); 3724 if (err) 3725 return err; 3726 3727 if (buf) { 3728 len = strlen(buf); 3729 *ulenp = len + 1; 3730 } else { 3731 *ulenp = 1; 3732 } 3733 if (!uname) 3734 return 0; 3735 3736 if (buf) { 3737 err = bpf_copy_to_user(uname, buf, ulen, len); 3738 if (err) 3739 return err; 3740 } else { 3741 char zero = '\0'; 3742 3743 if (put_user(zero, uname)) 3744 return -EFAULT; 3745 } 3746 return 0; 3747 } 3748 3749 #ifdef CONFIG_KPROBE_EVENTS 3750 static int bpf_perf_link_fill_kprobe(const struct perf_event *event, 3751 struct bpf_link_info *info) 3752 { 3753 unsigned long missed; 3754 char __user *uname; 3755 u64 addr, offset; 3756 u32 ulen, type; 3757 int err; 3758 3759 uname = u64_to_user_ptr(info->perf_event.kprobe.func_name); 3760 ulen = info->perf_event.kprobe.name_len; 3761 err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr, 3762 &type, &missed); 3763 if (err) 3764 return err; 3765 if (type == BPF_FD_TYPE_KRETPROBE) 3766 info->perf_event.type = BPF_PERF_EVENT_KRETPROBE; 3767 else 3768 info->perf_event.type = BPF_PERF_EVENT_KPROBE; 3769 info->perf_event.kprobe.name_len = ulen; 3770 info->perf_event.kprobe.offset = offset; 3771 info->perf_event.kprobe.missed = missed; 3772 if (!kallsyms_show_value(current_cred())) 3773 addr = 0; 3774 info->perf_event.kprobe.addr = addr; 3775 info->perf_event.kprobe.cookie = event->bpf_cookie; 3776 return 0; 3777 } 3778 #endif 3779 3780 #ifdef CONFIG_UPROBE_EVENTS 3781 static int bpf_perf_link_fill_uprobe(const struct perf_event *event, 3782 struct bpf_link_info *info) 3783 { 3784 char __user *uname; 3785 u64 addr, offset; 3786 u32 ulen, type; 3787 int err; 3788 3789 uname = u64_to_user_ptr(info->perf_event.uprobe.file_name); 3790 ulen = info->perf_event.uprobe.name_len; 3791 err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr, 3792 &type, NULL); 3793 if (err) 3794 return err; 3795 3796 if (type == BPF_FD_TYPE_URETPROBE) 3797 info->perf_event.type = BPF_PERF_EVENT_URETPROBE; 3798 else 3799 info->perf_event.type = BPF_PERF_EVENT_UPROBE; 3800 info->perf_event.uprobe.name_len = ulen; 3801 info->perf_event.uprobe.offset = offset; 3802 info->perf_event.uprobe.cookie = event->bpf_cookie; 3803 return 0; 3804 } 3805 #endif 3806 3807 static int bpf_perf_link_fill_probe(const struct perf_event *event, 3808 struct bpf_link_info *info) 3809 { 3810 #ifdef CONFIG_KPROBE_EVENTS 3811 if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE) 3812 return bpf_perf_link_fill_kprobe(event, info); 3813 #endif 3814 #ifdef CONFIG_UPROBE_EVENTS 3815 if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE) 3816 return bpf_perf_link_fill_uprobe(event, info); 3817 #endif 3818 return -EOPNOTSUPP; 3819 } 3820 3821 static int bpf_perf_link_fill_tracepoint(const struct perf_event *event, 3822 struct bpf_link_info *info) 3823 { 3824 char __user *uname; 3825 u32 ulen; 3826 int err; 3827 3828 uname = u64_to_user_ptr(info->perf_event.tracepoint.tp_name); 3829 ulen = info->perf_event.tracepoint.name_len; 3830 err = bpf_perf_link_fill_common(event, uname, &ulen, NULL, NULL, NULL, NULL); 3831 if (err) 3832 return err; 3833 3834 info->perf_event.type = BPF_PERF_EVENT_TRACEPOINT; 3835 info->perf_event.tracepoint.name_len = ulen; 3836 info->perf_event.tracepoint.cookie = event->bpf_cookie; 3837 return 0; 3838 } 3839 3840 static int bpf_perf_link_fill_perf_event(const struct perf_event *event, 3841 struct bpf_link_info *info) 3842 { 3843 info->perf_event.event.type = event->attr.type; 3844 info->perf_event.event.config = event->attr.config; 3845 info->perf_event.event.cookie = event->bpf_cookie; 3846 info->perf_event.type = BPF_PERF_EVENT_EVENT; 3847 return 0; 3848 } 3849 3850 static int bpf_perf_link_fill_link_info(const struct bpf_link *link, 3851 struct bpf_link_info *info) 3852 { 3853 struct bpf_perf_link *perf_link; 3854 const struct perf_event *event; 3855 3856 perf_link = container_of(link, struct bpf_perf_link, link); 3857 event = perf_get_event(perf_link->perf_file); 3858 if (IS_ERR(event)) 3859 return PTR_ERR(event); 3860 3861 switch (event->prog->type) { 3862 case BPF_PROG_TYPE_PERF_EVENT: 3863 return bpf_perf_link_fill_perf_event(event, info); 3864 case BPF_PROG_TYPE_TRACEPOINT: 3865 return bpf_perf_link_fill_tracepoint(event, info); 3866 case BPF_PROG_TYPE_KPROBE: 3867 return bpf_perf_link_fill_probe(event, info); 3868 default: 3869 return -EOPNOTSUPP; 3870 } 3871 } 3872 3873 static const struct bpf_link_ops bpf_perf_link_lops = { 3874 .release = bpf_perf_link_release, 3875 .dealloc = bpf_perf_link_dealloc, 3876 .fill_link_info = bpf_perf_link_fill_link_info, 3877 }; 3878 3879 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3880 { 3881 struct bpf_link_primer link_primer; 3882 struct bpf_perf_link *link; 3883 struct perf_event *event; 3884 struct file *perf_file; 3885 int err; 3886 3887 if (attr->link_create.flags) 3888 return -EINVAL; 3889 3890 perf_file = perf_event_get(attr->link_create.target_fd); 3891 if (IS_ERR(perf_file)) 3892 return PTR_ERR(perf_file); 3893 3894 link = kzalloc(sizeof(*link), GFP_USER); 3895 if (!link) { 3896 err = -ENOMEM; 3897 goto out_put_file; 3898 } 3899 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog); 3900 link->perf_file = perf_file; 3901 3902 err = bpf_link_prime(&link->link, &link_primer); 3903 if (err) { 3904 kfree(link); 3905 goto out_put_file; 3906 } 3907 3908 event = perf_file->private_data; 3909 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie); 3910 if (err) { 3911 bpf_link_cleanup(&link_primer); 3912 goto out_put_file; 3913 } 3914 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */ 3915 bpf_prog_inc(prog); 3916 3917 return bpf_link_settle(&link_primer); 3918 3919 out_put_file: 3920 fput(perf_file); 3921 return err; 3922 } 3923 #else 3924 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3925 { 3926 return -EOPNOTSUPP; 3927 } 3928 #endif /* CONFIG_PERF_EVENTS */ 3929 3930 static int bpf_raw_tp_link_attach(struct bpf_prog *prog, 3931 const char __user *user_tp_name, u64 cookie) 3932 { 3933 struct bpf_link_primer link_primer; 3934 struct bpf_raw_tp_link *link; 3935 struct bpf_raw_event_map *btp; 3936 const char *tp_name; 3937 char buf[128]; 3938 int err; 3939 3940 switch (prog->type) { 3941 case BPF_PROG_TYPE_TRACING: 3942 case BPF_PROG_TYPE_EXT: 3943 case BPF_PROG_TYPE_LSM: 3944 if (user_tp_name) 3945 /* The attach point for this category of programs 3946 * should be specified via btf_id during program load. 3947 */ 3948 return -EINVAL; 3949 if (prog->type == BPF_PROG_TYPE_TRACING && 3950 prog->expected_attach_type == BPF_TRACE_RAW_TP) { 3951 tp_name = prog->aux->attach_func_name; 3952 break; 3953 } 3954 return bpf_tracing_prog_attach(prog, 0, 0, 0); 3955 case BPF_PROG_TYPE_RAW_TRACEPOINT: 3956 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 3957 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0) 3958 return -EFAULT; 3959 buf[sizeof(buf) - 1] = 0; 3960 tp_name = buf; 3961 break; 3962 default: 3963 return -EINVAL; 3964 } 3965 3966 btp = bpf_get_raw_tracepoint(tp_name); 3967 if (!btp) 3968 return -ENOENT; 3969 3970 link = kzalloc(sizeof(*link), GFP_USER); 3971 if (!link) { 3972 err = -ENOMEM; 3973 goto out_put_btp; 3974 } 3975 bpf_link_init_sleepable(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, 3976 &bpf_raw_tp_link_lops, prog, 3977 tracepoint_is_faultable(btp->tp)); 3978 link->btp = btp; 3979 link->cookie = cookie; 3980 3981 err = bpf_link_prime(&link->link, &link_primer); 3982 if (err) { 3983 kfree(link); 3984 goto out_put_btp; 3985 } 3986 3987 err = bpf_probe_register(link->btp, link); 3988 if (err) { 3989 bpf_link_cleanup(&link_primer); 3990 goto out_put_btp; 3991 } 3992 3993 return bpf_link_settle(&link_primer); 3994 3995 out_put_btp: 3996 bpf_put_raw_tracepoint(btp); 3997 return err; 3998 } 3999 4000 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.cookie 4001 4002 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 4003 { 4004 struct bpf_prog *prog; 4005 void __user *tp_name; 4006 __u64 cookie; 4007 int fd; 4008 4009 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) 4010 return -EINVAL; 4011 4012 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); 4013 if (IS_ERR(prog)) 4014 return PTR_ERR(prog); 4015 4016 tp_name = u64_to_user_ptr(attr->raw_tracepoint.name); 4017 cookie = attr->raw_tracepoint.cookie; 4018 fd = bpf_raw_tp_link_attach(prog, tp_name, cookie); 4019 if (fd < 0) 4020 bpf_prog_put(prog); 4021 return fd; 4022 } 4023 4024 static enum bpf_prog_type 4025 attach_type_to_prog_type(enum bpf_attach_type attach_type) 4026 { 4027 switch (attach_type) { 4028 case BPF_CGROUP_INET_INGRESS: 4029 case BPF_CGROUP_INET_EGRESS: 4030 return BPF_PROG_TYPE_CGROUP_SKB; 4031 case BPF_CGROUP_INET_SOCK_CREATE: 4032 case BPF_CGROUP_INET_SOCK_RELEASE: 4033 case BPF_CGROUP_INET4_POST_BIND: 4034 case BPF_CGROUP_INET6_POST_BIND: 4035 return BPF_PROG_TYPE_CGROUP_SOCK; 4036 case BPF_CGROUP_INET4_BIND: 4037 case BPF_CGROUP_INET6_BIND: 4038 case BPF_CGROUP_INET4_CONNECT: 4039 case BPF_CGROUP_INET6_CONNECT: 4040 case BPF_CGROUP_UNIX_CONNECT: 4041 case BPF_CGROUP_INET4_GETPEERNAME: 4042 case BPF_CGROUP_INET6_GETPEERNAME: 4043 case BPF_CGROUP_UNIX_GETPEERNAME: 4044 case BPF_CGROUP_INET4_GETSOCKNAME: 4045 case BPF_CGROUP_INET6_GETSOCKNAME: 4046 case BPF_CGROUP_UNIX_GETSOCKNAME: 4047 case BPF_CGROUP_UDP4_SENDMSG: 4048 case BPF_CGROUP_UDP6_SENDMSG: 4049 case BPF_CGROUP_UNIX_SENDMSG: 4050 case BPF_CGROUP_UDP4_RECVMSG: 4051 case BPF_CGROUP_UDP6_RECVMSG: 4052 case BPF_CGROUP_UNIX_RECVMSG: 4053 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 4054 case BPF_CGROUP_SOCK_OPS: 4055 return BPF_PROG_TYPE_SOCK_OPS; 4056 case BPF_CGROUP_DEVICE: 4057 return BPF_PROG_TYPE_CGROUP_DEVICE; 4058 case BPF_SK_MSG_VERDICT: 4059 return BPF_PROG_TYPE_SK_MSG; 4060 case BPF_SK_SKB_STREAM_PARSER: 4061 case BPF_SK_SKB_STREAM_VERDICT: 4062 case BPF_SK_SKB_VERDICT: 4063 return BPF_PROG_TYPE_SK_SKB; 4064 case BPF_LIRC_MODE2: 4065 return BPF_PROG_TYPE_LIRC_MODE2; 4066 case BPF_FLOW_DISSECTOR: 4067 return BPF_PROG_TYPE_FLOW_DISSECTOR; 4068 case BPF_CGROUP_SYSCTL: 4069 return BPF_PROG_TYPE_CGROUP_SYSCTL; 4070 case BPF_CGROUP_GETSOCKOPT: 4071 case BPF_CGROUP_SETSOCKOPT: 4072 return BPF_PROG_TYPE_CGROUP_SOCKOPT; 4073 case BPF_TRACE_ITER: 4074 case BPF_TRACE_RAW_TP: 4075 case BPF_TRACE_FENTRY: 4076 case BPF_TRACE_FEXIT: 4077 case BPF_MODIFY_RETURN: 4078 return BPF_PROG_TYPE_TRACING; 4079 case BPF_LSM_MAC: 4080 return BPF_PROG_TYPE_LSM; 4081 case BPF_SK_LOOKUP: 4082 return BPF_PROG_TYPE_SK_LOOKUP; 4083 case BPF_XDP: 4084 return BPF_PROG_TYPE_XDP; 4085 case BPF_LSM_CGROUP: 4086 return BPF_PROG_TYPE_LSM; 4087 case BPF_TCX_INGRESS: 4088 case BPF_TCX_EGRESS: 4089 case BPF_NETKIT_PRIMARY: 4090 case BPF_NETKIT_PEER: 4091 return BPF_PROG_TYPE_SCHED_CLS; 4092 default: 4093 return BPF_PROG_TYPE_UNSPEC; 4094 } 4095 } 4096 4097 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 4098 enum bpf_attach_type attach_type) 4099 { 4100 enum bpf_prog_type ptype; 4101 4102 switch (prog->type) { 4103 case BPF_PROG_TYPE_CGROUP_SOCK: 4104 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4105 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4106 case BPF_PROG_TYPE_SK_LOOKUP: 4107 return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 4108 case BPF_PROG_TYPE_CGROUP_SKB: 4109 if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN)) 4110 /* cg-skb progs can be loaded by unpriv user. 4111 * check permissions at attach time. 4112 */ 4113 return -EPERM; 4114 4115 ptype = attach_type_to_prog_type(attach_type); 4116 if (prog->type != ptype) 4117 return -EINVAL; 4118 4119 return prog->enforce_expected_attach_type && 4120 prog->expected_attach_type != attach_type ? 4121 -EINVAL : 0; 4122 case BPF_PROG_TYPE_EXT: 4123 return 0; 4124 case BPF_PROG_TYPE_NETFILTER: 4125 if (attach_type != BPF_NETFILTER) 4126 return -EINVAL; 4127 return 0; 4128 case BPF_PROG_TYPE_PERF_EVENT: 4129 case BPF_PROG_TYPE_TRACEPOINT: 4130 if (attach_type != BPF_PERF_EVENT) 4131 return -EINVAL; 4132 return 0; 4133 case BPF_PROG_TYPE_KPROBE: 4134 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI && 4135 attach_type != BPF_TRACE_KPROBE_MULTI) 4136 return -EINVAL; 4137 if (prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION && 4138 attach_type != BPF_TRACE_KPROBE_SESSION) 4139 return -EINVAL; 4140 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI && 4141 attach_type != BPF_TRACE_UPROBE_MULTI) 4142 return -EINVAL; 4143 if (prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION && 4144 attach_type != BPF_TRACE_UPROBE_SESSION) 4145 return -EINVAL; 4146 if (attach_type != BPF_PERF_EVENT && 4147 attach_type != BPF_TRACE_KPROBE_MULTI && 4148 attach_type != BPF_TRACE_KPROBE_SESSION && 4149 attach_type != BPF_TRACE_UPROBE_MULTI && 4150 attach_type != BPF_TRACE_UPROBE_SESSION) 4151 return -EINVAL; 4152 return 0; 4153 case BPF_PROG_TYPE_SCHED_CLS: 4154 if (attach_type != BPF_TCX_INGRESS && 4155 attach_type != BPF_TCX_EGRESS && 4156 attach_type != BPF_NETKIT_PRIMARY && 4157 attach_type != BPF_NETKIT_PEER) 4158 return -EINVAL; 4159 return 0; 4160 default: 4161 ptype = attach_type_to_prog_type(attach_type); 4162 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) 4163 return -EINVAL; 4164 return 0; 4165 } 4166 } 4167 4168 #define BPF_PROG_ATTACH_LAST_FIELD expected_revision 4169 4170 #define BPF_F_ATTACH_MASK_BASE \ 4171 (BPF_F_ALLOW_OVERRIDE | \ 4172 BPF_F_ALLOW_MULTI | \ 4173 BPF_F_REPLACE) 4174 4175 #define BPF_F_ATTACH_MASK_MPROG \ 4176 (BPF_F_REPLACE | \ 4177 BPF_F_BEFORE | \ 4178 BPF_F_AFTER | \ 4179 BPF_F_ID | \ 4180 BPF_F_LINK) 4181 4182 static int bpf_prog_attach(const union bpf_attr *attr) 4183 { 4184 enum bpf_prog_type ptype; 4185 struct bpf_prog *prog; 4186 int ret; 4187 4188 if (CHECK_ATTR(BPF_PROG_ATTACH)) 4189 return -EINVAL; 4190 4191 ptype = attach_type_to_prog_type(attr->attach_type); 4192 if (ptype == BPF_PROG_TYPE_UNSPEC) 4193 return -EINVAL; 4194 if (bpf_mprog_supported(ptype)) { 4195 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG) 4196 return -EINVAL; 4197 } else { 4198 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_BASE) 4199 return -EINVAL; 4200 if (attr->relative_fd || 4201 attr->expected_revision) 4202 return -EINVAL; 4203 } 4204 4205 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 4206 if (IS_ERR(prog)) 4207 return PTR_ERR(prog); 4208 4209 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 4210 bpf_prog_put(prog); 4211 return -EINVAL; 4212 } 4213 4214 switch (ptype) { 4215 case BPF_PROG_TYPE_SK_SKB: 4216 case BPF_PROG_TYPE_SK_MSG: 4217 ret = sock_map_get_from_fd(attr, prog); 4218 break; 4219 case BPF_PROG_TYPE_LIRC_MODE2: 4220 ret = lirc_prog_attach(attr, prog); 4221 break; 4222 case BPF_PROG_TYPE_FLOW_DISSECTOR: 4223 ret = netns_bpf_prog_attach(attr, prog); 4224 break; 4225 case BPF_PROG_TYPE_CGROUP_DEVICE: 4226 case BPF_PROG_TYPE_CGROUP_SKB: 4227 case BPF_PROG_TYPE_CGROUP_SOCK: 4228 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4229 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4230 case BPF_PROG_TYPE_CGROUP_SYSCTL: 4231 case BPF_PROG_TYPE_SOCK_OPS: 4232 case BPF_PROG_TYPE_LSM: 4233 if (ptype == BPF_PROG_TYPE_LSM && 4234 prog->expected_attach_type != BPF_LSM_CGROUP) 4235 ret = -EINVAL; 4236 else 4237 ret = cgroup_bpf_prog_attach(attr, ptype, prog); 4238 break; 4239 case BPF_PROG_TYPE_SCHED_CLS: 4240 if (attr->attach_type == BPF_TCX_INGRESS || 4241 attr->attach_type == BPF_TCX_EGRESS) 4242 ret = tcx_prog_attach(attr, prog); 4243 else 4244 ret = netkit_prog_attach(attr, prog); 4245 break; 4246 default: 4247 ret = -EINVAL; 4248 } 4249 4250 if (ret) 4251 bpf_prog_put(prog); 4252 return ret; 4253 } 4254 4255 #define BPF_PROG_DETACH_LAST_FIELD expected_revision 4256 4257 static int bpf_prog_detach(const union bpf_attr *attr) 4258 { 4259 struct bpf_prog *prog = NULL; 4260 enum bpf_prog_type ptype; 4261 int ret; 4262 4263 if (CHECK_ATTR(BPF_PROG_DETACH)) 4264 return -EINVAL; 4265 4266 ptype = attach_type_to_prog_type(attr->attach_type); 4267 if (bpf_mprog_supported(ptype)) { 4268 if (ptype == BPF_PROG_TYPE_UNSPEC) 4269 return -EINVAL; 4270 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG) 4271 return -EINVAL; 4272 if (attr->attach_bpf_fd) { 4273 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 4274 if (IS_ERR(prog)) 4275 return PTR_ERR(prog); 4276 } 4277 } else if (attr->attach_flags || 4278 attr->relative_fd || 4279 attr->expected_revision) { 4280 return -EINVAL; 4281 } 4282 4283 switch (ptype) { 4284 case BPF_PROG_TYPE_SK_MSG: 4285 case BPF_PROG_TYPE_SK_SKB: 4286 ret = sock_map_prog_detach(attr, ptype); 4287 break; 4288 case BPF_PROG_TYPE_LIRC_MODE2: 4289 ret = lirc_prog_detach(attr); 4290 break; 4291 case BPF_PROG_TYPE_FLOW_DISSECTOR: 4292 ret = netns_bpf_prog_detach(attr, ptype); 4293 break; 4294 case BPF_PROG_TYPE_CGROUP_DEVICE: 4295 case BPF_PROG_TYPE_CGROUP_SKB: 4296 case BPF_PROG_TYPE_CGROUP_SOCK: 4297 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4298 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4299 case BPF_PROG_TYPE_CGROUP_SYSCTL: 4300 case BPF_PROG_TYPE_SOCK_OPS: 4301 case BPF_PROG_TYPE_LSM: 4302 ret = cgroup_bpf_prog_detach(attr, ptype); 4303 break; 4304 case BPF_PROG_TYPE_SCHED_CLS: 4305 if (attr->attach_type == BPF_TCX_INGRESS || 4306 attr->attach_type == BPF_TCX_EGRESS) 4307 ret = tcx_prog_detach(attr, prog); 4308 else 4309 ret = netkit_prog_detach(attr, prog); 4310 break; 4311 default: 4312 ret = -EINVAL; 4313 } 4314 4315 if (prog) 4316 bpf_prog_put(prog); 4317 return ret; 4318 } 4319 4320 #define BPF_PROG_QUERY_LAST_FIELD query.revision 4321 4322 static int bpf_prog_query(const union bpf_attr *attr, 4323 union bpf_attr __user *uattr) 4324 { 4325 if (!bpf_net_capable()) 4326 return -EPERM; 4327 if (CHECK_ATTR(BPF_PROG_QUERY)) 4328 return -EINVAL; 4329 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 4330 return -EINVAL; 4331 4332 switch (attr->query.attach_type) { 4333 case BPF_CGROUP_INET_INGRESS: 4334 case BPF_CGROUP_INET_EGRESS: 4335 case BPF_CGROUP_INET_SOCK_CREATE: 4336 case BPF_CGROUP_INET_SOCK_RELEASE: 4337 case BPF_CGROUP_INET4_BIND: 4338 case BPF_CGROUP_INET6_BIND: 4339 case BPF_CGROUP_INET4_POST_BIND: 4340 case BPF_CGROUP_INET6_POST_BIND: 4341 case BPF_CGROUP_INET4_CONNECT: 4342 case BPF_CGROUP_INET6_CONNECT: 4343 case BPF_CGROUP_UNIX_CONNECT: 4344 case BPF_CGROUP_INET4_GETPEERNAME: 4345 case BPF_CGROUP_INET6_GETPEERNAME: 4346 case BPF_CGROUP_UNIX_GETPEERNAME: 4347 case BPF_CGROUP_INET4_GETSOCKNAME: 4348 case BPF_CGROUP_INET6_GETSOCKNAME: 4349 case BPF_CGROUP_UNIX_GETSOCKNAME: 4350 case BPF_CGROUP_UDP4_SENDMSG: 4351 case BPF_CGROUP_UDP6_SENDMSG: 4352 case BPF_CGROUP_UNIX_SENDMSG: 4353 case BPF_CGROUP_UDP4_RECVMSG: 4354 case BPF_CGROUP_UDP6_RECVMSG: 4355 case BPF_CGROUP_UNIX_RECVMSG: 4356 case BPF_CGROUP_SOCK_OPS: 4357 case BPF_CGROUP_DEVICE: 4358 case BPF_CGROUP_SYSCTL: 4359 case BPF_CGROUP_GETSOCKOPT: 4360 case BPF_CGROUP_SETSOCKOPT: 4361 case BPF_LSM_CGROUP: 4362 return cgroup_bpf_prog_query(attr, uattr); 4363 case BPF_LIRC_MODE2: 4364 return lirc_prog_query(attr, uattr); 4365 case BPF_FLOW_DISSECTOR: 4366 case BPF_SK_LOOKUP: 4367 return netns_bpf_prog_query(attr, uattr); 4368 case BPF_SK_SKB_STREAM_PARSER: 4369 case BPF_SK_SKB_STREAM_VERDICT: 4370 case BPF_SK_MSG_VERDICT: 4371 case BPF_SK_SKB_VERDICT: 4372 return sock_map_bpf_prog_query(attr, uattr); 4373 case BPF_TCX_INGRESS: 4374 case BPF_TCX_EGRESS: 4375 return tcx_prog_query(attr, uattr); 4376 case BPF_NETKIT_PRIMARY: 4377 case BPF_NETKIT_PEER: 4378 return netkit_prog_query(attr, uattr); 4379 default: 4380 return -EINVAL; 4381 } 4382 } 4383 4384 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size 4385 4386 static int bpf_prog_test_run(const union bpf_attr *attr, 4387 union bpf_attr __user *uattr) 4388 { 4389 struct bpf_prog *prog; 4390 int ret = -ENOTSUPP; 4391 4392 if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 4393 return -EINVAL; 4394 4395 if ((attr->test.ctx_size_in && !attr->test.ctx_in) || 4396 (!attr->test.ctx_size_in && attr->test.ctx_in)) 4397 return -EINVAL; 4398 4399 if ((attr->test.ctx_size_out && !attr->test.ctx_out) || 4400 (!attr->test.ctx_size_out && attr->test.ctx_out)) 4401 return -EINVAL; 4402 4403 prog = bpf_prog_get(attr->test.prog_fd); 4404 if (IS_ERR(prog)) 4405 return PTR_ERR(prog); 4406 4407 if (prog->aux->ops->test_run) 4408 ret = prog->aux->ops->test_run(prog, attr, uattr); 4409 4410 bpf_prog_put(prog); 4411 return ret; 4412 } 4413 4414 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 4415 4416 static int bpf_obj_get_next_id(const union bpf_attr *attr, 4417 union bpf_attr __user *uattr, 4418 struct idr *idr, 4419 spinlock_t *lock) 4420 { 4421 u32 next_id = attr->start_id; 4422 int err = 0; 4423 4424 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 4425 return -EINVAL; 4426 4427 if (!capable(CAP_SYS_ADMIN)) 4428 return -EPERM; 4429 4430 next_id++; 4431 spin_lock_bh(lock); 4432 if (!idr_get_next(idr, &next_id)) 4433 err = -ENOENT; 4434 spin_unlock_bh(lock); 4435 4436 if (!err) 4437 err = put_user(next_id, &uattr->next_id); 4438 4439 return err; 4440 } 4441 4442 struct bpf_map *bpf_map_get_curr_or_next(u32 *id) 4443 { 4444 struct bpf_map *map; 4445 4446 spin_lock_bh(&map_idr_lock); 4447 again: 4448 map = idr_get_next(&map_idr, id); 4449 if (map) { 4450 map = __bpf_map_inc_not_zero(map, false); 4451 if (IS_ERR(map)) { 4452 (*id)++; 4453 goto again; 4454 } 4455 } 4456 spin_unlock_bh(&map_idr_lock); 4457 4458 return map; 4459 } 4460 4461 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id) 4462 { 4463 struct bpf_prog *prog; 4464 4465 spin_lock_bh(&prog_idr_lock); 4466 again: 4467 prog = idr_get_next(&prog_idr, id); 4468 if (prog) { 4469 prog = bpf_prog_inc_not_zero(prog); 4470 if (IS_ERR(prog)) { 4471 (*id)++; 4472 goto again; 4473 } 4474 } 4475 spin_unlock_bh(&prog_idr_lock); 4476 4477 return prog; 4478 } 4479 4480 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 4481 4482 struct bpf_prog *bpf_prog_by_id(u32 id) 4483 { 4484 struct bpf_prog *prog; 4485 4486 if (!id) 4487 return ERR_PTR(-ENOENT); 4488 4489 spin_lock_bh(&prog_idr_lock); 4490 prog = idr_find(&prog_idr, id); 4491 if (prog) 4492 prog = bpf_prog_inc_not_zero(prog); 4493 else 4494 prog = ERR_PTR(-ENOENT); 4495 spin_unlock_bh(&prog_idr_lock); 4496 return prog; 4497 } 4498 4499 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 4500 { 4501 struct bpf_prog *prog; 4502 u32 id = attr->prog_id; 4503 int fd; 4504 4505 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 4506 return -EINVAL; 4507 4508 if (!capable(CAP_SYS_ADMIN)) 4509 return -EPERM; 4510 4511 prog = bpf_prog_by_id(id); 4512 if (IS_ERR(prog)) 4513 return PTR_ERR(prog); 4514 4515 fd = bpf_prog_new_fd(prog); 4516 if (fd < 0) 4517 bpf_prog_put(prog); 4518 4519 return fd; 4520 } 4521 4522 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 4523 4524 static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 4525 { 4526 struct bpf_map *map; 4527 u32 id = attr->map_id; 4528 int f_flags; 4529 int fd; 4530 4531 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 4532 attr->open_flags & ~BPF_OBJ_FLAG_MASK) 4533 return -EINVAL; 4534 4535 if (!capable(CAP_SYS_ADMIN)) 4536 return -EPERM; 4537 4538 f_flags = bpf_get_file_flag(attr->open_flags); 4539 if (f_flags < 0) 4540 return f_flags; 4541 4542 spin_lock_bh(&map_idr_lock); 4543 map = idr_find(&map_idr, id); 4544 if (map) 4545 map = __bpf_map_inc_not_zero(map, true); 4546 else 4547 map = ERR_PTR(-ENOENT); 4548 spin_unlock_bh(&map_idr_lock); 4549 4550 if (IS_ERR(map)) 4551 return PTR_ERR(map); 4552 4553 fd = bpf_map_new_fd(map, f_flags); 4554 if (fd < 0) 4555 bpf_map_put_with_uref(map); 4556 4557 return fd; 4558 } 4559 4560 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 4561 unsigned long addr, u32 *off, 4562 u32 *type) 4563 { 4564 const struct bpf_map *map; 4565 int i; 4566 4567 mutex_lock(&prog->aux->used_maps_mutex); 4568 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { 4569 map = prog->aux->used_maps[i]; 4570 if (map == (void *)addr) { 4571 *type = BPF_PSEUDO_MAP_FD; 4572 goto out; 4573 } 4574 if (!map->ops->map_direct_value_meta) 4575 continue; 4576 if (!map->ops->map_direct_value_meta(map, addr, off)) { 4577 *type = BPF_PSEUDO_MAP_VALUE; 4578 goto out; 4579 } 4580 } 4581 map = NULL; 4582 4583 out: 4584 mutex_unlock(&prog->aux->used_maps_mutex); 4585 return map; 4586 } 4587 4588 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, 4589 const struct cred *f_cred) 4590 { 4591 const struct bpf_map *map; 4592 struct bpf_insn *insns; 4593 u32 off, type; 4594 u64 imm; 4595 u8 code; 4596 int i; 4597 4598 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), 4599 GFP_USER); 4600 if (!insns) 4601 return insns; 4602 4603 for (i = 0; i < prog->len; i++) { 4604 code = insns[i].code; 4605 4606 if (code == (BPF_JMP | BPF_TAIL_CALL)) { 4607 insns[i].code = BPF_JMP | BPF_CALL; 4608 insns[i].imm = BPF_FUNC_tail_call; 4609 /* fall-through */ 4610 } 4611 if (code == (BPF_JMP | BPF_CALL) || 4612 code == (BPF_JMP | BPF_CALL_ARGS)) { 4613 if (code == (BPF_JMP | BPF_CALL_ARGS)) 4614 insns[i].code = BPF_JMP | BPF_CALL; 4615 if (!bpf_dump_raw_ok(f_cred)) 4616 insns[i].imm = 0; 4617 continue; 4618 } 4619 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) { 4620 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM; 4621 continue; 4622 } 4623 4624 if ((BPF_CLASS(code) == BPF_LDX || BPF_CLASS(code) == BPF_STX || 4625 BPF_CLASS(code) == BPF_ST) && BPF_MODE(code) == BPF_PROBE_MEM32) { 4626 insns[i].code = BPF_CLASS(code) | BPF_SIZE(code) | BPF_MEM; 4627 continue; 4628 } 4629 4630 if (code != (BPF_LD | BPF_IMM | BPF_DW)) 4631 continue; 4632 4633 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; 4634 map = bpf_map_from_imm(prog, imm, &off, &type); 4635 if (map) { 4636 insns[i].src_reg = type; 4637 insns[i].imm = map->id; 4638 insns[i + 1].imm = off; 4639 continue; 4640 } 4641 } 4642 4643 return insns; 4644 } 4645 4646 static int set_info_rec_size(struct bpf_prog_info *info) 4647 { 4648 /* 4649 * Ensure info.*_rec_size is the same as kernel expected size 4650 * 4651 * or 4652 * 4653 * Only allow zero *_rec_size if both _rec_size and _cnt are 4654 * zero. In this case, the kernel will set the expected 4655 * _rec_size back to the info. 4656 */ 4657 4658 if ((info->nr_func_info || info->func_info_rec_size) && 4659 info->func_info_rec_size != sizeof(struct bpf_func_info)) 4660 return -EINVAL; 4661 4662 if ((info->nr_line_info || info->line_info_rec_size) && 4663 info->line_info_rec_size != sizeof(struct bpf_line_info)) 4664 return -EINVAL; 4665 4666 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && 4667 info->jited_line_info_rec_size != sizeof(__u64)) 4668 return -EINVAL; 4669 4670 info->func_info_rec_size = sizeof(struct bpf_func_info); 4671 info->line_info_rec_size = sizeof(struct bpf_line_info); 4672 info->jited_line_info_rec_size = sizeof(__u64); 4673 4674 return 0; 4675 } 4676 4677 static int bpf_prog_get_info_by_fd(struct file *file, 4678 struct bpf_prog *prog, 4679 const union bpf_attr *attr, 4680 union bpf_attr __user *uattr) 4681 { 4682 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); 4683 struct btf *attach_btf = bpf_prog_get_target_btf(prog); 4684 struct bpf_prog_info info; 4685 u32 info_len = attr->info.info_len; 4686 struct bpf_prog_kstats stats; 4687 char __user *uinsns; 4688 u32 ulen; 4689 int err; 4690 4691 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); 4692 if (err) 4693 return err; 4694 info_len = min_t(u32, sizeof(info), info_len); 4695 4696 memset(&info, 0, sizeof(info)); 4697 if (copy_from_user(&info, uinfo, info_len)) 4698 return -EFAULT; 4699 4700 info.type = prog->type; 4701 info.id = prog->aux->id; 4702 info.load_time = prog->aux->load_time; 4703 info.created_by_uid = from_kuid_munged(current_user_ns(), 4704 prog->aux->user->uid); 4705 info.gpl_compatible = prog->gpl_compatible; 4706 4707 memcpy(info.tag, prog->tag, sizeof(prog->tag)); 4708 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); 4709 4710 mutex_lock(&prog->aux->used_maps_mutex); 4711 ulen = info.nr_map_ids; 4712 info.nr_map_ids = prog->aux->used_map_cnt; 4713 ulen = min_t(u32, info.nr_map_ids, ulen); 4714 if (ulen) { 4715 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 4716 u32 i; 4717 4718 for (i = 0; i < ulen; i++) 4719 if (put_user(prog->aux->used_maps[i]->id, 4720 &user_map_ids[i])) { 4721 mutex_unlock(&prog->aux->used_maps_mutex); 4722 return -EFAULT; 4723 } 4724 } 4725 mutex_unlock(&prog->aux->used_maps_mutex); 4726 4727 err = set_info_rec_size(&info); 4728 if (err) 4729 return err; 4730 4731 bpf_prog_get_stats(prog, &stats); 4732 info.run_time_ns = stats.nsecs; 4733 info.run_cnt = stats.cnt; 4734 info.recursion_misses = stats.misses; 4735 4736 info.verified_insns = prog->aux->verified_insns; 4737 4738 if (!bpf_capable()) { 4739 info.jited_prog_len = 0; 4740 info.xlated_prog_len = 0; 4741 info.nr_jited_ksyms = 0; 4742 info.nr_jited_func_lens = 0; 4743 info.nr_func_info = 0; 4744 info.nr_line_info = 0; 4745 info.nr_jited_line_info = 0; 4746 goto done; 4747 } 4748 4749 ulen = info.xlated_prog_len; 4750 info.xlated_prog_len = bpf_prog_insn_size(prog); 4751 if (info.xlated_prog_len && ulen) { 4752 struct bpf_insn *insns_sanitized; 4753 bool fault; 4754 4755 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) { 4756 info.xlated_prog_insns = 0; 4757 goto done; 4758 } 4759 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred); 4760 if (!insns_sanitized) 4761 return -ENOMEM; 4762 uinsns = u64_to_user_ptr(info.xlated_prog_insns); 4763 ulen = min_t(u32, info.xlated_prog_len, ulen); 4764 fault = copy_to_user(uinsns, insns_sanitized, ulen); 4765 kfree(insns_sanitized); 4766 if (fault) 4767 return -EFAULT; 4768 } 4769 4770 if (bpf_prog_is_offloaded(prog->aux)) { 4771 err = bpf_prog_offload_info_fill(&info, prog); 4772 if (err) 4773 return err; 4774 goto done; 4775 } 4776 4777 /* NOTE: the following code is supposed to be skipped for offload. 4778 * bpf_prog_offload_info_fill() is the place to fill similar fields 4779 * for offload. 4780 */ 4781 ulen = info.jited_prog_len; 4782 if (prog->aux->func_cnt) { 4783 u32 i; 4784 4785 info.jited_prog_len = 0; 4786 for (i = 0; i < prog->aux->func_cnt; i++) 4787 info.jited_prog_len += prog->aux->func[i]->jited_len; 4788 } else { 4789 info.jited_prog_len = prog->jited_len; 4790 } 4791 4792 if (info.jited_prog_len && ulen) { 4793 if (bpf_dump_raw_ok(file->f_cred)) { 4794 uinsns = u64_to_user_ptr(info.jited_prog_insns); 4795 ulen = min_t(u32, info.jited_prog_len, ulen); 4796 4797 /* for multi-function programs, copy the JITed 4798 * instructions for all the functions 4799 */ 4800 if (prog->aux->func_cnt) { 4801 u32 len, free, i; 4802 u8 *img; 4803 4804 free = ulen; 4805 for (i = 0; i < prog->aux->func_cnt; i++) { 4806 len = prog->aux->func[i]->jited_len; 4807 len = min_t(u32, len, free); 4808 img = (u8 *) prog->aux->func[i]->bpf_func; 4809 if (copy_to_user(uinsns, img, len)) 4810 return -EFAULT; 4811 uinsns += len; 4812 free -= len; 4813 if (!free) 4814 break; 4815 } 4816 } else { 4817 if (copy_to_user(uinsns, prog->bpf_func, ulen)) 4818 return -EFAULT; 4819 } 4820 } else { 4821 info.jited_prog_insns = 0; 4822 } 4823 } 4824 4825 ulen = info.nr_jited_ksyms; 4826 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; 4827 if (ulen) { 4828 if (bpf_dump_raw_ok(file->f_cred)) { 4829 unsigned long ksym_addr; 4830 u64 __user *user_ksyms; 4831 u32 i; 4832 4833 /* copy the address of the kernel symbol 4834 * corresponding to each function 4835 */ 4836 ulen = min_t(u32, info.nr_jited_ksyms, ulen); 4837 user_ksyms = u64_to_user_ptr(info.jited_ksyms); 4838 if (prog->aux->func_cnt) { 4839 for (i = 0; i < ulen; i++) { 4840 ksym_addr = (unsigned long) 4841 prog->aux->func[i]->bpf_func; 4842 if (put_user((u64) ksym_addr, 4843 &user_ksyms[i])) 4844 return -EFAULT; 4845 } 4846 } else { 4847 ksym_addr = (unsigned long) prog->bpf_func; 4848 if (put_user((u64) ksym_addr, &user_ksyms[0])) 4849 return -EFAULT; 4850 } 4851 } else { 4852 info.jited_ksyms = 0; 4853 } 4854 } 4855 4856 ulen = info.nr_jited_func_lens; 4857 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; 4858 if (ulen) { 4859 if (bpf_dump_raw_ok(file->f_cred)) { 4860 u32 __user *user_lens; 4861 u32 func_len, i; 4862 4863 /* copy the JITed image lengths for each function */ 4864 ulen = min_t(u32, info.nr_jited_func_lens, ulen); 4865 user_lens = u64_to_user_ptr(info.jited_func_lens); 4866 if (prog->aux->func_cnt) { 4867 for (i = 0; i < ulen; i++) { 4868 func_len = 4869 prog->aux->func[i]->jited_len; 4870 if (put_user(func_len, &user_lens[i])) 4871 return -EFAULT; 4872 } 4873 } else { 4874 func_len = prog->jited_len; 4875 if (put_user(func_len, &user_lens[0])) 4876 return -EFAULT; 4877 } 4878 } else { 4879 info.jited_func_lens = 0; 4880 } 4881 } 4882 4883 if (prog->aux->btf) 4884 info.btf_id = btf_obj_id(prog->aux->btf); 4885 info.attach_btf_id = prog->aux->attach_btf_id; 4886 if (attach_btf) 4887 info.attach_btf_obj_id = btf_obj_id(attach_btf); 4888 4889 ulen = info.nr_func_info; 4890 info.nr_func_info = prog->aux->func_info_cnt; 4891 if (info.nr_func_info && ulen) { 4892 char __user *user_finfo; 4893 4894 user_finfo = u64_to_user_ptr(info.func_info); 4895 ulen = min_t(u32, info.nr_func_info, ulen); 4896 if (copy_to_user(user_finfo, prog->aux->func_info, 4897 info.func_info_rec_size * ulen)) 4898 return -EFAULT; 4899 } 4900 4901 ulen = info.nr_line_info; 4902 info.nr_line_info = prog->aux->nr_linfo; 4903 if (info.nr_line_info && ulen) { 4904 __u8 __user *user_linfo; 4905 4906 user_linfo = u64_to_user_ptr(info.line_info); 4907 ulen = min_t(u32, info.nr_line_info, ulen); 4908 if (copy_to_user(user_linfo, prog->aux->linfo, 4909 info.line_info_rec_size * ulen)) 4910 return -EFAULT; 4911 } 4912 4913 ulen = info.nr_jited_line_info; 4914 if (prog->aux->jited_linfo) 4915 info.nr_jited_line_info = prog->aux->nr_linfo; 4916 else 4917 info.nr_jited_line_info = 0; 4918 if (info.nr_jited_line_info && ulen) { 4919 if (bpf_dump_raw_ok(file->f_cred)) { 4920 unsigned long line_addr; 4921 __u64 __user *user_linfo; 4922 u32 i; 4923 4924 user_linfo = u64_to_user_ptr(info.jited_line_info); 4925 ulen = min_t(u32, info.nr_jited_line_info, ulen); 4926 for (i = 0; i < ulen; i++) { 4927 line_addr = (unsigned long)prog->aux->jited_linfo[i]; 4928 if (put_user((__u64)line_addr, &user_linfo[i])) 4929 return -EFAULT; 4930 } 4931 } else { 4932 info.jited_line_info = 0; 4933 } 4934 } 4935 4936 ulen = info.nr_prog_tags; 4937 info.nr_prog_tags = prog->aux->func_cnt ? : 1; 4938 if (ulen) { 4939 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; 4940 u32 i; 4941 4942 user_prog_tags = u64_to_user_ptr(info.prog_tags); 4943 ulen = min_t(u32, info.nr_prog_tags, ulen); 4944 if (prog->aux->func_cnt) { 4945 for (i = 0; i < ulen; i++) { 4946 if (copy_to_user(user_prog_tags[i], 4947 prog->aux->func[i]->tag, 4948 BPF_TAG_SIZE)) 4949 return -EFAULT; 4950 } 4951 } else { 4952 if (copy_to_user(user_prog_tags[0], 4953 prog->tag, BPF_TAG_SIZE)) 4954 return -EFAULT; 4955 } 4956 } 4957 4958 done: 4959 if (copy_to_user(uinfo, &info, info_len) || 4960 put_user(info_len, &uattr->info.info_len)) 4961 return -EFAULT; 4962 4963 return 0; 4964 } 4965 4966 static int bpf_map_get_info_by_fd(struct file *file, 4967 struct bpf_map *map, 4968 const union bpf_attr *attr, 4969 union bpf_attr __user *uattr) 4970 { 4971 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 4972 struct bpf_map_info info; 4973 u32 info_len = attr->info.info_len; 4974 int err; 4975 4976 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); 4977 if (err) 4978 return err; 4979 info_len = min_t(u32, sizeof(info), info_len); 4980 4981 memset(&info, 0, sizeof(info)); 4982 info.type = map->map_type; 4983 info.id = map->id; 4984 info.key_size = map->key_size; 4985 info.value_size = map->value_size; 4986 info.max_entries = map->max_entries; 4987 info.map_flags = map->map_flags; 4988 info.map_extra = map->map_extra; 4989 memcpy(info.name, map->name, sizeof(map->name)); 4990 4991 if (map->btf) { 4992 info.btf_id = btf_obj_id(map->btf); 4993 info.btf_key_type_id = map->btf_key_type_id; 4994 info.btf_value_type_id = map->btf_value_type_id; 4995 } 4996 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; 4997 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) 4998 bpf_map_struct_ops_info_fill(&info, map); 4999 5000 if (bpf_map_is_offloaded(map)) { 5001 err = bpf_map_offload_info_fill(&info, map); 5002 if (err) 5003 return err; 5004 } 5005 5006 if (copy_to_user(uinfo, &info, info_len) || 5007 put_user(info_len, &uattr->info.info_len)) 5008 return -EFAULT; 5009 5010 return 0; 5011 } 5012 5013 static int bpf_btf_get_info_by_fd(struct file *file, 5014 struct btf *btf, 5015 const union bpf_attr *attr, 5016 union bpf_attr __user *uattr) 5017 { 5018 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5019 u32 info_len = attr->info.info_len; 5020 int err; 5021 5022 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len); 5023 if (err) 5024 return err; 5025 5026 return btf_get_info_by_fd(btf, attr, uattr); 5027 } 5028 5029 static int bpf_link_get_info_by_fd(struct file *file, 5030 struct bpf_link *link, 5031 const union bpf_attr *attr, 5032 union bpf_attr __user *uattr) 5033 { 5034 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5035 struct bpf_link_info info; 5036 u32 info_len = attr->info.info_len; 5037 int err; 5038 5039 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); 5040 if (err) 5041 return err; 5042 info_len = min_t(u32, sizeof(info), info_len); 5043 5044 memset(&info, 0, sizeof(info)); 5045 if (copy_from_user(&info, uinfo, info_len)) 5046 return -EFAULT; 5047 5048 info.type = link->type; 5049 info.id = link->id; 5050 if (link->prog) 5051 info.prog_id = link->prog->aux->id; 5052 5053 if (link->ops->fill_link_info) { 5054 err = link->ops->fill_link_info(link, &info); 5055 if (err) 5056 return err; 5057 } 5058 5059 if (copy_to_user(uinfo, &info, info_len) || 5060 put_user(info_len, &uattr->info.info_len)) 5061 return -EFAULT; 5062 5063 return 0; 5064 } 5065 5066 5067 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 5068 5069 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 5070 union bpf_attr __user *uattr) 5071 { 5072 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 5073 return -EINVAL; 5074 5075 CLASS(fd, f)(attr->info.bpf_fd); 5076 if (fd_empty(f)) 5077 return -EBADFD; 5078 5079 if (fd_file(f)->f_op == &bpf_prog_fops) 5080 return bpf_prog_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, 5081 uattr); 5082 else if (fd_file(f)->f_op == &bpf_map_fops) 5083 return bpf_map_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, 5084 uattr); 5085 else if (fd_file(f)->f_op == &btf_fops) 5086 return bpf_btf_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, uattr); 5087 else if (fd_file(f)->f_op == &bpf_link_fops || fd_file(f)->f_op == &bpf_link_fops_poll) 5088 return bpf_link_get_info_by_fd(fd_file(f), fd_file(f)->private_data, 5089 attr, uattr); 5090 return -EINVAL; 5091 } 5092 5093 #define BPF_BTF_LOAD_LAST_FIELD btf_token_fd 5094 5095 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size) 5096 { 5097 struct bpf_token *token = NULL; 5098 5099 if (CHECK_ATTR(BPF_BTF_LOAD)) 5100 return -EINVAL; 5101 5102 if (attr->btf_flags & ~BPF_F_TOKEN_FD) 5103 return -EINVAL; 5104 5105 if (attr->btf_flags & BPF_F_TOKEN_FD) { 5106 token = bpf_token_get_from_fd(attr->btf_token_fd); 5107 if (IS_ERR(token)) 5108 return PTR_ERR(token); 5109 if (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) { 5110 bpf_token_put(token); 5111 token = NULL; 5112 } 5113 } 5114 5115 if (!bpf_token_capable(token, CAP_BPF)) { 5116 bpf_token_put(token); 5117 return -EPERM; 5118 } 5119 5120 bpf_token_put(token); 5121 5122 return btf_new_fd(attr, uattr, uattr_size); 5123 } 5124 5125 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id 5126 5127 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) 5128 { 5129 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) 5130 return -EINVAL; 5131 5132 if (!capable(CAP_SYS_ADMIN)) 5133 return -EPERM; 5134 5135 return btf_get_fd_by_id(attr->btf_id); 5136 } 5137 5138 static int bpf_task_fd_query_copy(const union bpf_attr *attr, 5139 union bpf_attr __user *uattr, 5140 u32 prog_id, u32 fd_type, 5141 const char *buf, u64 probe_offset, 5142 u64 probe_addr) 5143 { 5144 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); 5145 u32 len = buf ? strlen(buf) : 0, input_len; 5146 int err = 0; 5147 5148 if (put_user(len, &uattr->task_fd_query.buf_len)) 5149 return -EFAULT; 5150 input_len = attr->task_fd_query.buf_len; 5151 if (input_len && ubuf) { 5152 if (!len) { 5153 /* nothing to copy, just make ubuf NULL terminated */ 5154 char zero = '\0'; 5155 5156 if (put_user(zero, ubuf)) 5157 return -EFAULT; 5158 } else if (input_len >= len + 1) { 5159 /* ubuf can hold the string with NULL terminator */ 5160 if (copy_to_user(ubuf, buf, len + 1)) 5161 return -EFAULT; 5162 } else { 5163 /* ubuf cannot hold the string with NULL terminator, 5164 * do a partial copy with NULL terminator. 5165 */ 5166 char zero = '\0'; 5167 5168 err = -ENOSPC; 5169 if (copy_to_user(ubuf, buf, input_len - 1)) 5170 return -EFAULT; 5171 if (put_user(zero, ubuf + input_len - 1)) 5172 return -EFAULT; 5173 } 5174 } 5175 5176 if (put_user(prog_id, &uattr->task_fd_query.prog_id) || 5177 put_user(fd_type, &uattr->task_fd_query.fd_type) || 5178 put_user(probe_offset, &uattr->task_fd_query.probe_offset) || 5179 put_user(probe_addr, &uattr->task_fd_query.probe_addr)) 5180 return -EFAULT; 5181 5182 return err; 5183 } 5184 5185 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr 5186 5187 static int bpf_task_fd_query(const union bpf_attr *attr, 5188 union bpf_attr __user *uattr) 5189 { 5190 pid_t pid = attr->task_fd_query.pid; 5191 u32 fd = attr->task_fd_query.fd; 5192 const struct perf_event *event; 5193 struct task_struct *task; 5194 struct file *file; 5195 int err; 5196 5197 if (CHECK_ATTR(BPF_TASK_FD_QUERY)) 5198 return -EINVAL; 5199 5200 if (!capable(CAP_SYS_ADMIN)) 5201 return -EPERM; 5202 5203 if (attr->task_fd_query.flags != 0) 5204 return -EINVAL; 5205 5206 rcu_read_lock(); 5207 task = get_pid_task(find_vpid(pid), PIDTYPE_PID); 5208 rcu_read_unlock(); 5209 if (!task) 5210 return -ENOENT; 5211 5212 err = 0; 5213 file = fget_task(task, fd); 5214 put_task_struct(task); 5215 if (!file) 5216 return -EBADF; 5217 5218 if (file->f_op == &bpf_link_fops || file->f_op == &bpf_link_fops_poll) { 5219 struct bpf_link *link = file->private_data; 5220 5221 if (link->ops == &bpf_raw_tp_link_lops) { 5222 struct bpf_raw_tp_link *raw_tp = 5223 container_of(link, struct bpf_raw_tp_link, link); 5224 struct bpf_raw_event_map *btp = raw_tp->btp; 5225 5226 err = bpf_task_fd_query_copy(attr, uattr, 5227 raw_tp->link.prog->aux->id, 5228 BPF_FD_TYPE_RAW_TRACEPOINT, 5229 btp->tp->name, 0, 0); 5230 goto put_file; 5231 } 5232 goto out_not_supp; 5233 } 5234 5235 event = perf_get_event(file); 5236 if (!IS_ERR(event)) { 5237 u64 probe_offset, probe_addr; 5238 u32 prog_id, fd_type; 5239 const char *buf; 5240 5241 err = bpf_get_perf_event_info(event, &prog_id, &fd_type, 5242 &buf, &probe_offset, 5243 &probe_addr, NULL); 5244 if (!err) 5245 err = bpf_task_fd_query_copy(attr, uattr, prog_id, 5246 fd_type, buf, 5247 probe_offset, 5248 probe_addr); 5249 goto put_file; 5250 } 5251 5252 out_not_supp: 5253 err = -ENOTSUPP; 5254 put_file: 5255 fput(file); 5256 return err; 5257 } 5258 5259 #define BPF_MAP_BATCH_LAST_FIELD batch.flags 5260 5261 #define BPF_DO_BATCH(fn, ...) \ 5262 do { \ 5263 if (!fn) { \ 5264 err = -ENOTSUPP; \ 5265 goto err_put; \ 5266 } \ 5267 err = fn(__VA_ARGS__); \ 5268 } while (0) 5269 5270 static int bpf_map_do_batch(const union bpf_attr *attr, 5271 union bpf_attr __user *uattr, 5272 int cmd) 5273 { 5274 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH || 5275 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH; 5276 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH; 5277 struct bpf_map *map; 5278 int err; 5279 5280 if (CHECK_ATTR(BPF_MAP_BATCH)) 5281 return -EINVAL; 5282 5283 CLASS(fd, f)(attr->batch.map_fd); 5284 5285 map = __bpf_map_get(f); 5286 if (IS_ERR(map)) 5287 return PTR_ERR(map); 5288 if (has_write) 5289 bpf_map_write_active_inc(map); 5290 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 5291 err = -EPERM; 5292 goto err_put; 5293 } 5294 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 5295 err = -EPERM; 5296 goto err_put; 5297 } 5298 5299 if (cmd == BPF_MAP_LOOKUP_BATCH) 5300 BPF_DO_BATCH(map->ops->map_lookup_batch, map, attr, uattr); 5301 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) 5302 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch, map, attr, uattr); 5303 else if (cmd == BPF_MAP_UPDATE_BATCH) 5304 BPF_DO_BATCH(map->ops->map_update_batch, map, fd_file(f), attr, uattr); 5305 else 5306 BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr); 5307 err_put: 5308 if (has_write) { 5309 maybe_wait_bpf_programs(map); 5310 bpf_map_write_active_dec(map); 5311 } 5312 return err; 5313 } 5314 5315 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.pid 5316 static int link_create(union bpf_attr *attr, bpfptr_t uattr) 5317 { 5318 struct bpf_prog *prog; 5319 int ret; 5320 5321 if (CHECK_ATTR(BPF_LINK_CREATE)) 5322 return -EINVAL; 5323 5324 if (attr->link_create.attach_type == BPF_STRUCT_OPS) 5325 return bpf_struct_ops_link_create(attr); 5326 5327 prog = bpf_prog_get(attr->link_create.prog_fd); 5328 if (IS_ERR(prog)) 5329 return PTR_ERR(prog); 5330 5331 ret = bpf_prog_attach_check_attach_type(prog, 5332 attr->link_create.attach_type); 5333 if (ret) 5334 goto out; 5335 5336 switch (prog->type) { 5337 case BPF_PROG_TYPE_CGROUP_SKB: 5338 case BPF_PROG_TYPE_CGROUP_SOCK: 5339 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 5340 case BPF_PROG_TYPE_SOCK_OPS: 5341 case BPF_PROG_TYPE_CGROUP_DEVICE: 5342 case BPF_PROG_TYPE_CGROUP_SYSCTL: 5343 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 5344 ret = cgroup_bpf_link_attach(attr, prog); 5345 break; 5346 case BPF_PROG_TYPE_EXT: 5347 ret = bpf_tracing_prog_attach(prog, 5348 attr->link_create.target_fd, 5349 attr->link_create.target_btf_id, 5350 attr->link_create.tracing.cookie); 5351 break; 5352 case BPF_PROG_TYPE_LSM: 5353 case BPF_PROG_TYPE_TRACING: 5354 if (attr->link_create.attach_type != prog->expected_attach_type) { 5355 ret = -EINVAL; 5356 goto out; 5357 } 5358 if (prog->expected_attach_type == BPF_TRACE_RAW_TP) 5359 ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie); 5360 else if (prog->expected_attach_type == BPF_TRACE_ITER) 5361 ret = bpf_iter_link_attach(attr, uattr, prog); 5362 else if (prog->expected_attach_type == BPF_LSM_CGROUP) 5363 ret = cgroup_bpf_link_attach(attr, prog); 5364 else 5365 ret = bpf_tracing_prog_attach(prog, 5366 attr->link_create.target_fd, 5367 attr->link_create.target_btf_id, 5368 attr->link_create.tracing.cookie); 5369 break; 5370 case BPF_PROG_TYPE_FLOW_DISSECTOR: 5371 case BPF_PROG_TYPE_SK_LOOKUP: 5372 ret = netns_bpf_link_create(attr, prog); 5373 break; 5374 case BPF_PROG_TYPE_SK_MSG: 5375 case BPF_PROG_TYPE_SK_SKB: 5376 ret = sock_map_link_create(attr, prog); 5377 break; 5378 #ifdef CONFIG_NET 5379 case BPF_PROG_TYPE_XDP: 5380 ret = bpf_xdp_link_attach(attr, prog); 5381 break; 5382 case BPF_PROG_TYPE_SCHED_CLS: 5383 if (attr->link_create.attach_type == BPF_TCX_INGRESS || 5384 attr->link_create.attach_type == BPF_TCX_EGRESS) 5385 ret = tcx_link_attach(attr, prog); 5386 else 5387 ret = netkit_link_attach(attr, prog); 5388 break; 5389 case BPF_PROG_TYPE_NETFILTER: 5390 ret = bpf_nf_link_attach(attr, prog); 5391 break; 5392 #endif 5393 case BPF_PROG_TYPE_PERF_EVENT: 5394 case BPF_PROG_TYPE_TRACEPOINT: 5395 ret = bpf_perf_link_attach(attr, prog); 5396 break; 5397 case BPF_PROG_TYPE_KPROBE: 5398 if (attr->link_create.attach_type == BPF_PERF_EVENT) 5399 ret = bpf_perf_link_attach(attr, prog); 5400 else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI || 5401 attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION) 5402 ret = bpf_kprobe_multi_link_attach(attr, prog); 5403 else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI || 5404 attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION) 5405 ret = bpf_uprobe_multi_link_attach(attr, prog); 5406 break; 5407 default: 5408 ret = -EINVAL; 5409 } 5410 5411 out: 5412 if (ret < 0) 5413 bpf_prog_put(prog); 5414 return ret; 5415 } 5416 5417 static int link_update_map(struct bpf_link *link, union bpf_attr *attr) 5418 { 5419 struct bpf_map *new_map, *old_map = NULL; 5420 int ret; 5421 5422 new_map = bpf_map_get(attr->link_update.new_map_fd); 5423 if (IS_ERR(new_map)) 5424 return PTR_ERR(new_map); 5425 5426 if (attr->link_update.flags & BPF_F_REPLACE) { 5427 old_map = bpf_map_get(attr->link_update.old_map_fd); 5428 if (IS_ERR(old_map)) { 5429 ret = PTR_ERR(old_map); 5430 goto out_put; 5431 } 5432 } else if (attr->link_update.old_map_fd) { 5433 ret = -EINVAL; 5434 goto out_put; 5435 } 5436 5437 ret = link->ops->update_map(link, new_map, old_map); 5438 5439 if (old_map) 5440 bpf_map_put(old_map); 5441 out_put: 5442 bpf_map_put(new_map); 5443 return ret; 5444 } 5445 5446 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd 5447 5448 static int link_update(union bpf_attr *attr) 5449 { 5450 struct bpf_prog *old_prog = NULL, *new_prog; 5451 struct bpf_link *link; 5452 u32 flags; 5453 int ret; 5454 5455 if (CHECK_ATTR(BPF_LINK_UPDATE)) 5456 return -EINVAL; 5457 5458 flags = attr->link_update.flags; 5459 if (flags & ~BPF_F_REPLACE) 5460 return -EINVAL; 5461 5462 link = bpf_link_get_from_fd(attr->link_update.link_fd); 5463 if (IS_ERR(link)) 5464 return PTR_ERR(link); 5465 5466 if (link->ops->update_map) { 5467 ret = link_update_map(link, attr); 5468 goto out_put_link; 5469 } 5470 5471 new_prog = bpf_prog_get(attr->link_update.new_prog_fd); 5472 if (IS_ERR(new_prog)) { 5473 ret = PTR_ERR(new_prog); 5474 goto out_put_link; 5475 } 5476 5477 if (flags & BPF_F_REPLACE) { 5478 old_prog = bpf_prog_get(attr->link_update.old_prog_fd); 5479 if (IS_ERR(old_prog)) { 5480 ret = PTR_ERR(old_prog); 5481 old_prog = NULL; 5482 goto out_put_progs; 5483 } 5484 } else if (attr->link_update.old_prog_fd) { 5485 ret = -EINVAL; 5486 goto out_put_progs; 5487 } 5488 5489 if (link->ops->update_prog) 5490 ret = link->ops->update_prog(link, new_prog, old_prog); 5491 else 5492 ret = -EINVAL; 5493 5494 out_put_progs: 5495 if (old_prog) 5496 bpf_prog_put(old_prog); 5497 if (ret) 5498 bpf_prog_put(new_prog); 5499 out_put_link: 5500 bpf_link_put_direct(link); 5501 return ret; 5502 } 5503 5504 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd 5505 5506 static int link_detach(union bpf_attr *attr) 5507 { 5508 struct bpf_link *link; 5509 int ret; 5510 5511 if (CHECK_ATTR(BPF_LINK_DETACH)) 5512 return -EINVAL; 5513 5514 link = bpf_link_get_from_fd(attr->link_detach.link_fd); 5515 if (IS_ERR(link)) 5516 return PTR_ERR(link); 5517 5518 if (link->ops->detach) 5519 ret = link->ops->detach(link); 5520 else 5521 ret = -EOPNOTSUPP; 5522 5523 bpf_link_put_direct(link); 5524 return ret; 5525 } 5526 5527 struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) 5528 { 5529 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT); 5530 } 5531 EXPORT_SYMBOL(bpf_link_inc_not_zero); 5532 5533 struct bpf_link *bpf_link_by_id(u32 id) 5534 { 5535 struct bpf_link *link; 5536 5537 if (!id) 5538 return ERR_PTR(-ENOENT); 5539 5540 spin_lock_bh(&link_idr_lock); 5541 /* before link is "settled", ID is 0, pretend it doesn't exist yet */ 5542 link = idr_find(&link_idr, id); 5543 if (link) { 5544 if (link->id) 5545 link = bpf_link_inc_not_zero(link); 5546 else 5547 link = ERR_PTR(-EAGAIN); 5548 } else { 5549 link = ERR_PTR(-ENOENT); 5550 } 5551 spin_unlock_bh(&link_idr_lock); 5552 return link; 5553 } 5554 5555 struct bpf_link *bpf_link_get_curr_or_next(u32 *id) 5556 { 5557 struct bpf_link *link; 5558 5559 spin_lock_bh(&link_idr_lock); 5560 again: 5561 link = idr_get_next(&link_idr, id); 5562 if (link) { 5563 link = bpf_link_inc_not_zero(link); 5564 if (IS_ERR(link)) { 5565 (*id)++; 5566 goto again; 5567 } 5568 } 5569 spin_unlock_bh(&link_idr_lock); 5570 5571 return link; 5572 } 5573 5574 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id 5575 5576 static int bpf_link_get_fd_by_id(const union bpf_attr *attr) 5577 { 5578 struct bpf_link *link; 5579 u32 id = attr->link_id; 5580 int fd; 5581 5582 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) 5583 return -EINVAL; 5584 5585 if (!capable(CAP_SYS_ADMIN)) 5586 return -EPERM; 5587 5588 link = bpf_link_by_id(id); 5589 if (IS_ERR(link)) 5590 return PTR_ERR(link); 5591 5592 fd = bpf_link_new_fd(link); 5593 if (fd < 0) 5594 bpf_link_put_direct(link); 5595 5596 return fd; 5597 } 5598 5599 DEFINE_MUTEX(bpf_stats_enabled_mutex); 5600 5601 static int bpf_stats_release(struct inode *inode, struct file *file) 5602 { 5603 mutex_lock(&bpf_stats_enabled_mutex); 5604 static_key_slow_dec(&bpf_stats_enabled_key.key); 5605 mutex_unlock(&bpf_stats_enabled_mutex); 5606 return 0; 5607 } 5608 5609 static const struct file_operations bpf_stats_fops = { 5610 .release = bpf_stats_release, 5611 }; 5612 5613 static int bpf_enable_runtime_stats(void) 5614 { 5615 int fd; 5616 5617 mutex_lock(&bpf_stats_enabled_mutex); 5618 5619 /* Set a very high limit to avoid overflow */ 5620 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { 5621 mutex_unlock(&bpf_stats_enabled_mutex); 5622 return -EBUSY; 5623 } 5624 5625 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); 5626 if (fd >= 0) 5627 static_key_slow_inc(&bpf_stats_enabled_key.key); 5628 5629 mutex_unlock(&bpf_stats_enabled_mutex); 5630 return fd; 5631 } 5632 5633 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type 5634 5635 static int bpf_enable_stats(union bpf_attr *attr) 5636 { 5637 5638 if (CHECK_ATTR(BPF_ENABLE_STATS)) 5639 return -EINVAL; 5640 5641 if (!capable(CAP_SYS_ADMIN)) 5642 return -EPERM; 5643 5644 switch (attr->enable_stats.type) { 5645 case BPF_STATS_RUN_TIME: 5646 return bpf_enable_runtime_stats(); 5647 default: 5648 break; 5649 } 5650 return -EINVAL; 5651 } 5652 5653 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags 5654 5655 static int bpf_iter_create(union bpf_attr *attr) 5656 { 5657 struct bpf_link *link; 5658 int err; 5659 5660 if (CHECK_ATTR(BPF_ITER_CREATE)) 5661 return -EINVAL; 5662 5663 if (attr->iter_create.flags) 5664 return -EINVAL; 5665 5666 link = bpf_link_get_from_fd(attr->iter_create.link_fd); 5667 if (IS_ERR(link)) 5668 return PTR_ERR(link); 5669 5670 err = bpf_iter_new_fd(link); 5671 bpf_link_put_direct(link); 5672 5673 return err; 5674 } 5675 5676 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags 5677 5678 static int bpf_prog_bind_map(union bpf_attr *attr) 5679 { 5680 struct bpf_prog *prog; 5681 struct bpf_map *map; 5682 struct bpf_map **used_maps_old, **used_maps_new; 5683 int i, ret = 0; 5684 5685 if (CHECK_ATTR(BPF_PROG_BIND_MAP)) 5686 return -EINVAL; 5687 5688 if (attr->prog_bind_map.flags) 5689 return -EINVAL; 5690 5691 prog = bpf_prog_get(attr->prog_bind_map.prog_fd); 5692 if (IS_ERR(prog)) 5693 return PTR_ERR(prog); 5694 5695 map = bpf_map_get(attr->prog_bind_map.map_fd); 5696 if (IS_ERR(map)) { 5697 ret = PTR_ERR(map); 5698 goto out_prog_put; 5699 } 5700 5701 mutex_lock(&prog->aux->used_maps_mutex); 5702 5703 used_maps_old = prog->aux->used_maps; 5704 5705 for (i = 0; i < prog->aux->used_map_cnt; i++) 5706 if (used_maps_old[i] == map) { 5707 bpf_map_put(map); 5708 goto out_unlock; 5709 } 5710 5711 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1, 5712 sizeof(used_maps_new[0]), 5713 GFP_KERNEL); 5714 if (!used_maps_new) { 5715 ret = -ENOMEM; 5716 goto out_unlock; 5717 } 5718 5719 /* The bpf program will not access the bpf map, but for the sake of 5720 * simplicity, increase sleepable_refcnt for sleepable program as well. 5721 */ 5722 if (prog->sleepable) 5723 atomic64_inc(&map->sleepable_refcnt); 5724 memcpy(used_maps_new, used_maps_old, 5725 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt); 5726 used_maps_new[prog->aux->used_map_cnt] = map; 5727 5728 prog->aux->used_map_cnt++; 5729 prog->aux->used_maps = used_maps_new; 5730 5731 kfree(used_maps_old); 5732 5733 out_unlock: 5734 mutex_unlock(&prog->aux->used_maps_mutex); 5735 5736 if (ret) 5737 bpf_map_put(map); 5738 out_prog_put: 5739 bpf_prog_put(prog); 5740 return ret; 5741 } 5742 5743 #define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd 5744 5745 static int token_create(union bpf_attr *attr) 5746 { 5747 if (CHECK_ATTR(BPF_TOKEN_CREATE)) 5748 return -EINVAL; 5749 5750 /* no flags are supported yet */ 5751 if (attr->token_create.flags) 5752 return -EINVAL; 5753 5754 return bpf_token_create(attr); 5755 } 5756 5757 static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) 5758 { 5759 union bpf_attr attr; 5760 int err; 5761 5762 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); 5763 if (err) 5764 return err; 5765 size = min_t(u32, size, sizeof(attr)); 5766 5767 /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 5768 memset(&attr, 0, sizeof(attr)); 5769 if (copy_from_bpfptr(&attr, uattr, size) != 0) 5770 return -EFAULT; 5771 5772 err = security_bpf(cmd, &attr, size); 5773 if (err < 0) 5774 return err; 5775 5776 switch (cmd) { 5777 case BPF_MAP_CREATE: 5778 err = map_create(&attr); 5779 break; 5780 case BPF_MAP_LOOKUP_ELEM: 5781 err = map_lookup_elem(&attr); 5782 break; 5783 case BPF_MAP_UPDATE_ELEM: 5784 err = map_update_elem(&attr, uattr); 5785 break; 5786 case BPF_MAP_DELETE_ELEM: 5787 err = map_delete_elem(&attr, uattr); 5788 break; 5789 case BPF_MAP_GET_NEXT_KEY: 5790 err = map_get_next_key(&attr); 5791 break; 5792 case BPF_MAP_FREEZE: 5793 err = map_freeze(&attr); 5794 break; 5795 case BPF_PROG_LOAD: 5796 err = bpf_prog_load(&attr, uattr, size); 5797 break; 5798 case BPF_OBJ_PIN: 5799 err = bpf_obj_pin(&attr); 5800 break; 5801 case BPF_OBJ_GET: 5802 err = bpf_obj_get(&attr); 5803 break; 5804 case BPF_PROG_ATTACH: 5805 err = bpf_prog_attach(&attr); 5806 break; 5807 case BPF_PROG_DETACH: 5808 err = bpf_prog_detach(&attr); 5809 break; 5810 case BPF_PROG_QUERY: 5811 err = bpf_prog_query(&attr, uattr.user); 5812 break; 5813 case BPF_PROG_TEST_RUN: 5814 err = bpf_prog_test_run(&attr, uattr.user); 5815 break; 5816 case BPF_PROG_GET_NEXT_ID: 5817 err = bpf_obj_get_next_id(&attr, uattr.user, 5818 &prog_idr, &prog_idr_lock); 5819 break; 5820 case BPF_MAP_GET_NEXT_ID: 5821 err = bpf_obj_get_next_id(&attr, uattr.user, 5822 &map_idr, &map_idr_lock); 5823 break; 5824 case BPF_BTF_GET_NEXT_ID: 5825 err = bpf_obj_get_next_id(&attr, uattr.user, 5826 &btf_idr, &btf_idr_lock); 5827 break; 5828 case BPF_PROG_GET_FD_BY_ID: 5829 err = bpf_prog_get_fd_by_id(&attr); 5830 break; 5831 case BPF_MAP_GET_FD_BY_ID: 5832 err = bpf_map_get_fd_by_id(&attr); 5833 break; 5834 case BPF_OBJ_GET_INFO_BY_FD: 5835 err = bpf_obj_get_info_by_fd(&attr, uattr.user); 5836 break; 5837 case BPF_RAW_TRACEPOINT_OPEN: 5838 err = bpf_raw_tracepoint_open(&attr); 5839 break; 5840 case BPF_BTF_LOAD: 5841 err = bpf_btf_load(&attr, uattr, size); 5842 break; 5843 case BPF_BTF_GET_FD_BY_ID: 5844 err = bpf_btf_get_fd_by_id(&attr); 5845 break; 5846 case BPF_TASK_FD_QUERY: 5847 err = bpf_task_fd_query(&attr, uattr.user); 5848 break; 5849 case BPF_MAP_LOOKUP_AND_DELETE_ELEM: 5850 err = map_lookup_and_delete_elem(&attr); 5851 break; 5852 case BPF_MAP_LOOKUP_BATCH: 5853 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH); 5854 break; 5855 case BPF_MAP_LOOKUP_AND_DELETE_BATCH: 5856 err = bpf_map_do_batch(&attr, uattr.user, 5857 BPF_MAP_LOOKUP_AND_DELETE_BATCH); 5858 break; 5859 case BPF_MAP_UPDATE_BATCH: 5860 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH); 5861 break; 5862 case BPF_MAP_DELETE_BATCH: 5863 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH); 5864 break; 5865 case BPF_LINK_CREATE: 5866 err = link_create(&attr, uattr); 5867 break; 5868 case BPF_LINK_UPDATE: 5869 err = link_update(&attr); 5870 break; 5871 case BPF_LINK_GET_FD_BY_ID: 5872 err = bpf_link_get_fd_by_id(&attr); 5873 break; 5874 case BPF_LINK_GET_NEXT_ID: 5875 err = bpf_obj_get_next_id(&attr, uattr.user, 5876 &link_idr, &link_idr_lock); 5877 break; 5878 case BPF_ENABLE_STATS: 5879 err = bpf_enable_stats(&attr); 5880 break; 5881 case BPF_ITER_CREATE: 5882 err = bpf_iter_create(&attr); 5883 break; 5884 case BPF_LINK_DETACH: 5885 err = link_detach(&attr); 5886 break; 5887 case BPF_PROG_BIND_MAP: 5888 err = bpf_prog_bind_map(&attr); 5889 break; 5890 case BPF_TOKEN_CREATE: 5891 err = token_create(&attr); 5892 break; 5893 default: 5894 err = -EINVAL; 5895 break; 5896 } 5897 5898 return err; 5899 } 5900 5901 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) 5902 { 5903 return __sys_bpf(cmd, USER_BPFPTR(uattr), size); 5904 } 5905 5906 static bool syscall_prog_is_valid_access(int off, int size, 5907 enum bpf_access_type type, 5908 const struct bpf_prog *prog, 5909 struct bpf_insn_access_aux *info) 5910 { 5911 if (off < 0 || off >= U16_MAX) 5912 return false; 5913 if (off % size != 0) 5914 return false; 5915 return true; 5916 } 5917 5918 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size) 5919 { 5920 switch (cmd) { 5921 case BPF_MAP_CREATE: 5922 case BPF_MAP_DELETE_ELEM: 5923 case BPF_MAP_UPDATE_ELEM: 5924 case BPF_MAP_FREEZE: 5925 case BPF_MAP_GET_FD_BY_ID: 5926 case BPF_PROG_LOAD: 5927 case BPF_BTF_LOAD: 5928 case BPF_LINK_CREATE: 5929 case BPF_RAW_TRACEPOINT_OPEN: 5930 break; 5931 default: 5932 return -EINVAL; 5933 } 5934 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size); 5935 } 5936 5937 5938 /* To shut up -Wmissing-prototypes. 5939 * This function is used by the kernel light skeleton 5940 * to load bpf programs when modules are loaded or during kernel boot. 5941 * See tools/lib/bpf/skel_internal.h 5942 */ 5943 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size); 5944 5945 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size) 5946 { 5947 struct bpf_prog * __maybe_unused prog; 5948 struct bpf_tramp_run_ctx __maybe_unused run_ctx; 5949 5950 switch (cmd) { 5951 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */ 5952 case BPF_PROG_TEST_RUN: 5953 if (attr->test.data_in || attr->test.data_out || 5954 attr->test.ctx_out || attr->test.duration || 5955 attr->test.repeat || attr->test.flags) 5956 return -EINVAL; 5957 5958 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL); 5959 if (IS_ERR(prog)) 5960 return PTR_ERR(prog); 5961 5962 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset || 5963 attr->test.ctx_size_in > U16_MAX) { 5964 bpf_prog_put(prog); 5965 return -EINVAL; 5966 } 5967 5968 run_ctx.bpf_cookie = 0; 5969 if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) { 5970 /* recursion detected */ 5971 __bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx); 5972 bpf_prog_put(prog); 5973 return -EBUSY; 5974 } 5975 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in); 5976 __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */, 5977 &run_ctx); 5978 bpf_prog_put(prog); 5979 return 0; 5980 #endif 5981 default: 5982 return ____bpf_sys_bpf(cmd, attr, size); 5983 } 5984 } 5985 EXPORT_SYMBOL(kern_sys_bpf); 5986 5987 static const struct bpf_func_proto bpf_sys_bpf_proto = { 5988 .func = bpf_sys_bpf, 5989 .gpl_only = false, 5990 .ret_type = RET_INTEGER, 5991 .arg1_type = ARG_ANYTHING, 5992 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5993 .arg3_type = ARG_CONST_SIZE, 5994 }; 5995 5996 const struct bpf_func_proto * __weak 5997 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5998 { 5999 return bpf_base_func_proto(func_id, prog); 6000 } 6001 6002 BPF_CALL_1(bpf_sys_close, u32, fd) 6003 { 6004 /* When bpf program calls this helper there should not be 6005 * an fdget() without matching completed fdput(). 6006 * This helper is allowed in the following callchain only: 6007 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close 6008 */ 6009 return close_fd(fd); 6010 } 6011 6012 static const struct bpf_func_proto bpf_sys_close_proto = { 6013 .func = bpf_sys_close, 6014 .gpl_only = false, 6015 .ret_type = RET_INTEGER, 6016 .arg1_type = ARG_ANYTHING, 6017 }; 6018 6019 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) 6020 { 6021 *res = 0; 6022 if (flags) 6023 return -EINVAL; 6024 6025 if (name_sz <= 1 || name[name_sz - 1]) 6026 return -EINVAL; 6027 6028 if (!bpf_dump_raw_ok(current_cred())) 6029 return -EPERM; 6030 6031 *res = kallsyms_lookup_name(name); 6032 return *res ? 0 : -ENOENT; 6033 } 6034 6035 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { 6036 .func = bpf_kallsyms_lookup_name, 6037 .gpl_only = false, 6038 .ret_type = RET_INTEGER, 6039 .arg1_type = ARG_PTR_TO_MEM, 6040 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 6041 .arg3_type = ARG_ANYTHING, 6042 .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, 6043 .arg4_size = sizeof(u64), 6044 }; 6045 6046 static const struct bpf_func_proto * 6047 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6048 { 6049 switch (func_id) { 6050 case BPF_FUNC_sys_bpf: 6051 return !bpf_token_capable(prog->aux->token, CAP_PERFMON) 6052 ? NULL : &bpf_sys_bpf_proto; 6053 case BPF_FUNC_btf_find_by_name_kind: 6054 return &bpf_btf_find_by_name_kind_proto; 6055 case BPF_FUNC_sys_close: 6056 return &bpf_sys_close_proto; 6057 case BPF_FUNC_kallsyms_lookup_name: 6058 return &bpf_kallsyms_lookup_name_proto; 6059 default: 6060 return tracing_prog_func_proto(func_id, prog); 6061 } 6062 } 6063 6064 const struct bpf_verifier_ops bpf_syscall_verifier_ops = { 6065 .get_func_proto = syscall_prog_func_proto, 6066 .is_valid_access = syscall_prog_is_valid_access, 6067 }; 6068 6069 const struct bpf_prog_ops bpf_syscall_prog_ops = { 6070 .test_run = bpf_prog_test_run_syscall, 6071 }; 6072 6073 #ifdef CONFIG_SYSCTL 6074 static int bpf_stats_handler(const struct ctl_table *table, int write, 6075 void *buffer, size_t *lenp, loff_t *ppos) 6076 { 6077 struct static_key *key = (struct static_key *)table->data; 6078 static int saved_val; 6079 int val, ret; 6080 struct ctl_table tmp = { 6081 .data = &val, 6082 .maxlen = sizeof(val), 6083 .mode = table->mode, 6084 .extra1 = SYSCTL_ZERO, 6085 .extra2 = SYSCTL_ONE, 6086 }; 6087 6088 if (write && !capable(CAP_SYS_ADMIN)) 6089 return -EPERM; 6090 6091 mutex_lock(&bpf_stats_enabled_mutex); 6092 val = saved_val; 6093 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 6094 if (write && !ret && val != saved_val) { 6095 if (val) 6096 static_key_slow_inc(key); 6097 else 6098 static_key_slow_dec(key); 6099 saved_val = val; 6100 } 6101 mutex_unlock(&bpf_stats_enabled_mutex); 6102 return ret; 6103 } 6104 6105 void __weak unpriv_ebpf_notify(int new_state) 6106 { 6107 } 6108 6109 static int bpf_unpriv_handler(const struct ctl_table *table, int write, 6110 void *buffer, size_t *lenp, loff_t *ppos) 6111 { 6112 int ret, unpriv_enable = *(int *)table->data; 6113 bool locked_state = unpriv_enable == 1; 6114 struct ctl_table tmp = *table; 6115 6116 if (write && !capable(CAP_SYS_ADMIN)) 6117 return -EPERM; 6118 6119 tmp.data = &unpriv_enable; 6120 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 6121 if (write && !ret) { 6122 if (locked_state && unpriv_enable != 1) 6123 return -EPERM; 6124 *(int *)table->data = unpriv_enable; 6125 } 6126 6127 if (write) 6128 unpriv_ebpf_notify(unpriv_enable); 6129 6130 return ret; 6131 } 6132 6133 static const struct ctl_table bpf_syscall_table[] = { 6134 { 6135 .procname = "unprivileged_bpf_disabled", 6136 .data = &sysctl_unprivileged_bpf_disabled, 6137 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled), 6138 .mode = 0644, 6139 .proc_handler = bpf_unpriv_handler, 6140 .extra1 = SYSCTL_ZERO, 6141 .extra2 = SYSCTL_TWO, 6142 }, 6143 { 6144 .procname = "bpf_stats_enabled", 6145 .data = &bpf_stats_enabled_key.key, 6146 .mode = 0644, 6147 .proc_handler = bpf_stats_handler, 6148 }, 6149 }; 6150 6151 static int __init bpf_syscall_sysctl_init(void) 6152 { 6153 register_sysctl_init("kernel", bpf_syscall_table); 6154 return 0; 6155 } 6156 late_initcall(bpf_syscall_sysctl_init); 6157 #endif /* CONFIG_SYSCTL */ 6158