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