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