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