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