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