1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Minimal file system backend for holding eBPF maps and programs, 4 * used by bpf(2) object pinning. 5 * 6 * Authors: 7 * 8 * Daniel Borkmann <daniel@iogearbox.net> 9 */ 10 11 #include <linux/init.h> 12 #include <linux/magic.h> 13 #include <linux/major.h> 14 #include <linux/mount.h> 15 #include <linux/namei.h> 16 #include <linux/fs.h> 17 #include <linux/fs_context.h> 18 #include <linux/fs_parser.h> 19 #include <linux/kdev_t.h> 20 #include <linux/filter.h> 21 #include <linux/bpf.h> 22 #include <linux/bpf_trace.h> 23 #include <linux/kstrtox.h> 24 #include <linux/xattr.h> 25 #include <linux/security.h> 26 27 #include "preload/bpf_preload.h" 28 29 enum bpf_type { 30 BPF_TYPE_UNSPEC = 0, 31 BPF_TYPE_PROG, 32 BPF_TYPE_MAP, 33 BPF_TYPE_LINK, 34 }; 35 36 struct bpf_fs_inode { 37 struct list_head xattrs; 38 struct simple_xattr_limits xlimits; 39 struct inode vfs_inode; 40 }; 41 42 static inline struct bpf_fs_inode *BPF_FS_I(struct inode *inode) 43 { 44 return container_of(inode, struct bpf_fs_inode, vfs_inode); 45 } 46 47 static struct kmem_cache *bpf_fs_inode_cachep __ro_after_init; 48 49 static int bpf_fs_initxattrs(struct inode *inode, 50 const struct xattr *xattr_array, void *fs_info); 51 static ssize_t bpf_fs_listxattr(struct dentry *dentry, char *buf, size_t size); 52 53 static void *bpf_any_get(void *raw, enum bpf_type type) 54 { 55 switch (type) { 56 case BPF_TYPE_PROG: 57 bpf_prog_inc(raw); 58 break; 59 case BPF_TYPE_MAP: 60 bpf_map_inc_with_uref(raw); 61 break; 62 case BPF_TYPE_LINK: 63 bpf_link_inc(raw); 64 break; 65 default: 66 WARN_ON_ONCE(1); 67 break; 68 } 69 70 return raw; 71 } 72 73 static void bpf_any_put(void *raw, enum bpf_type type) 74 { 75 switch (type) { 76 case BPF_TYPE_PROG: 77 bpf_prog_put(raw); 78 break; 79 case BPF_TYPE_MAP: 80 bpf_map_put_with_uref(raw); 81 break; 82 case BPF_TYPE_LINK: 83 bpf_link_put(raw); 84 break; 85 default: 86 WARN_ON_ONCE(1); 87 break; 88 } 89 } 90 91 static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type) 92 { 93 void *raw; 94 95 raw = bpf_map_get_with_uref(ufd); 96 if (!IS_ERR(raw)) { 97 *type = BPF_TYPE_MAP; 98 return raw; 99 } 100 101 raw = bpf_prog_get(ufd); 102 if (!IS_ERR(raw)) { 103 *type = BPF_TYPE_PROG; 104 return raw; 105 } 106 107 raw = bpf_link_get_from_fd(ufd); 108 if (!IS_ERR(raw)) { 109 *type = BPF_TYPE_LINK; 110 return raw; 111 } 112 113 return ERR_PTR(-EINVAL); 114 } 115 116 static const struct inode_operations bpf_dir_iops; 117 static const struct inode_operations bpf_symlink_iops; 118 119 static const struct inode_operations bpf_prog_iops = { 120 .listxattr = bpf_fs_listxattr, 121 }; 122 static const struct inode_operations bpf_map_iops = { 123 .listxattr = bpf_fs_listxattr, 124 }; 125 static const struct inode_operations bpf_link_iops = { 126 .listxattr = bpf_fs_listxattr, 127 }; 128 129 struct inode *bpf_get_inode(struct super_block *sb, 130 const struct inode *dir, 131 umode_t mode) 132 { 133 struct inode *inode; 134 135 switch (mode & S_IFMT) { 136 case S_IFDIR: 137 case S_IFREG: 138 case S_IFLNK: 139 break; 140 default: 141 return ERR_PTR(-EINVAL); 142 } 143 144 inode = new_inode(sb); 145 if (!inode) 146 return ERR_PTR(-ENOSPC); 147 148 inode->i_ino = get_next_ino(); 149 simple_inode_init_ts(inode); 150 151 inode_init_owner(&nop_mnt_idmap, inode, dir, mode); 152 153 return inode; 154 } 155 156 static int bpf_inode_type(const struct inode *inode, enum bpf_type *type) 157 { 158 *type = BPF_TYPE_UNSPEC; 159 if (inode->i_op == &bpf_prog_iops) 160 *type = BPF_TYPE_PROG; 161 else if (inode->i_op == &bpf_map_iops) 162 *type = BPF_TYPE_MAP; 163 else if (inode->i_op == &bpf_link_iops) 164 *type = BPF_TYPE_LINK; 165 else 166 return -EACCES; 167 168 return 0; 169 } 170 171 static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode, 172 struct inode *dir) 173 { 174 d_make_persistent(dentry, inode); 175 176 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 177 } 178 179 static struct dentry *bpf_mkdir(const struct mnt_idmap *idmap, struct inode *dir, 180 struct dentry *dentry, umode_t mode) 181 { 182 struct inode *inode; 183 int ret; 184 185 inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR); 186 if (IS_ERR(inode)) 187 return ERR_CAST(inode); 188 189 ret = security_inode_init_security(inode, dir, &dentry->d_name, 190 bpf_fs_initxattrs, NULL); 191 if (ret && ret != -EOPNOTSUPP) { 192 iput(inode); 193 return ERR_PTR(ret); 194 } 195 196 inode->i_op = &bpf_dir_iops; 197 inode->i_fop = &simple_dir_operations; 198 199 inc_nlink(inode); 200 inc_nlink(dir); 201 202 bpf_dentry_finalize(dentry, inode, dir); 203 return NULL; 204 } 205 206 struct map_iter { 207 void *key; 208 bool done; 209 }; 210 211 static struct map_iter *map_iter(struct seq_file *m) 212 { 213 return m->private; 214 } 215 216 static struct bpf_map *seq_file_to_map(struct seq_file *m) 217 { 218 return file_inode(m->file)->i_private; 219 } 220 221 static void map_iter_free(struct map_iter *iter) 222 { 223 if (iter) { 224 kfree(iter->key); 225 kfree(iter); 226 } 227 } 228 229 static struct map_iter *map_iter_alloc(struct bpf_map *map) 230 { 231 struct map_iter *iter; 232 233 iter = kzalloc_obj(*iter, GFP_KERNEL | __GFP_NOWARN); 234 if (!iter) 235 goto error; 236 237 iter->key = kzalloc(map->key_size, GFP_KERNEL | __GFP_NOWARN); 238 if (!iter->key) 239 goto error; 240 241 return iter; 242 243 error: 244 map_iter_free(iter); 245 return NULL; 246 } 247 248 static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos) 249 { 250 struct bpf_map *map = seq_file_to_map(m); 251 void *key = map_iter(m)->key; 252 void *prev_key; 253 254 (*pos)++; 255 if (map_iter(m)->done) 256 return NULL; 257 258 if (unlikely(v == SEQ_START_TOKEN)) 259 prev_key = NULL; 260 else 261 prev_key = key; 262 263 rcu_read_lock(); 264 if (map->ops->map_get_next_key(map, prev_key, key)) { 265 map_iter(m)->done = true; 266 key = NULL; 267 } 268 rcu_read_unlock(); 269 return key; 270 } 271 272 static void *map_seq_start(struct seq_file *m, loff_t *pos) 273 { 274 if (map_iter(m)->done) 275 return NULL; 276 277 return *pos ? map_iter(m)->key : SEQ_START_TOKEN; 278 } 279 280 static void map_seq_stop(struct seq_file *m, void *v) 281 { 282 } 283 284 static int map_seq_show(struct seq_file *m, void *v) 285 { 286 struct bpf_map *map = seq_file_to_map(m); 287 void *key = map_iter(m)->key; 288 289 if (unlikely(v == SEQ_START_TOKEN)) { 290 seq_puts(m, "# WARNING!! The output is for debug purpose only\n"); 291 seq_puts(m, "# WARNING!! The output format will change\n"); 292 } else { 293 map->ops->map_seq_show_elem(map, key, m); 294 } 295 296 return 0; 297 } 298 299 static const struct seq_operations bpffs_map_seq_ops = { 300 .start = map_seq_start, 301 .next = map_seq_next, 302 .show = map_seq_show, 303 .stop = map_seq_stop, 304 }; 305 306 static int bpffs_map_open(struct inode *inode, struct file *file) 307 { 308 struct bpf_map *map = inode->i_private; 309 struct map_iter *iter; 310 struct seq_file *m; 311 int err; 312 313 /* Special fields are printed as they are. Most have addresses. */ 314 if (btf_record_has_field(map->record, ~(BPF_SPIN_LOCK | BPF_RES_SPIN_LOCK)) && 315 !perfmon_capable()) 316 return -EPERM; 317 318 iter = map_iter_alloc(map); 319 if (!iter) 320 return -ENOMEM; 321 322 err = seq_open(file, &bpffs_map_seq_ops); 323 if (err) { 324 map_iter_free(iter); 325 return err; 326 } 327 328 m = file->private_data; 329 m->private = iter; 330 331 return 0; 332 } 333 334 static int bpffs_map_release(struct inode *inode, struct file *file) 335 { 336 struct seq_file *m = file->private_data; 337 338 map_iter_free(map_iter(m)); 339 340 return seq_release(inode, file); 341 } 342 343 /* bpffs_map_fops should only implement the basic 344 * read operation for a BPF map. The purpose is to 345 * provide a simple user intuitive way to do 346 * "cat bpffs/pathto/a-pinned-map". 347 * 348 * Other operations (e.g. write, lookup...) should be realized by 349 * the userspace tools (e.g. bpftool) through the 350 * BPF_OBJ_GET_INFO_BY_FD and the map's lookup/update 351 * interface. 352 */ 353 static const struct file_operations bpffs_map_fops = { 354 .open = bpffs_map_open, 355 .read = seq_read, 356 .release = bpffs_map_release, 357 }; 358 359 static int bpffs_obj_open(struct inode *inode, struct file *file) 360 { 361 return -EIO; 362 } 363 364 static const struct file_operations bpffs_obj_fops = { 365 .open = bpffs_obj_open, 366 }; 367 368 static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw, 369 const struct inode_operations *iops, 370 const struct file_operations *fops) 371 { 372 struct inode *dir = dentry->d_parent->d_inode; 373 struct inode *inode; 374 int ret; 375 376 inode = bpf_get_inode(dir->i_sb, dir, mode); 377 if (IS_ERR(inode)) 378 return PTR_ERR(inode); 379 380 ret = security_inode_init_security(inode, dir, &dentry->d_name, 381 bpf_fs_initxattrs, NULL); 382 if (ret && ret != -EOPNOTSUPP) { 383 iput(inode); 384 return ret; 385 } 386 387 inode->i_op = iops; 388 inode->i_fop = fops; 389 inode->i_private = raw; 390 391 bpf_dentry_finalize(dentry, inode, dir); 392 return 0; 393 } 394 395 static int bpf_mkprog(struct dentry *dentry, umode_t mode, void *arg) 396 { 397 return bpf_mkobj_ops(dentry, mode, arg, &bpf_prog_iops, 398 &bpffs_obj_fops); 399 } 400 401 static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg) 402 { 403 struct bpf_map *map = arg; 404 405 return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops, 406 bpf_map_support_seq_show(map) ? 407 &bpffs_map_fops : &bpffs_obj_fops); 408 } 409 410 static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg) 411 { 412 struct bpf_link *link = arg; 413 414 return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops, 415 bpf_link_is_iter(link) ? 416 &bpf_iter_fops : &bpffs_obj_fops); 417 } 418 419 static struct dentry * 420 bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags) 421 { 422 /* Dots in names (e.g. "/sys/fs/bpf/foo.bar") are reserved for future 423 * extensions. That allows popoulate_bpffs() create special files. 424 */ 425 if ((dir->i_mode & S_IALLUGO) && 426 strchr(dentry->d_name.name, '.')) 427 return ERR_PTR(-EPERM); 428 429 return simple_lookup(dir, dentry, flags); 430 } 431 432 static int bpf_symlink(const struct mnt_idmap *idmap, struct inode *dir, 433 struct dentry *dentry, const char *target) 434 { 435 struct inode *inode; 436 char *link; 437 int ret; 438 439 link = kstrdup(target, GFP_KERNEL_ACCOUNT | __GFP_NOWARN); 440 if (!link) 441 return -ENOMEM; 442 443 inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK); 444 if (IS_ERR(inode)) { 445 kfree(link); 446 return PTR_ERR(inode); 447 } 448 449 inode->i_op = &bpf_symlink_iops; 450 inode->i_link = link; 451 452 ret = security_inode_init_security(inode, dir, &dentry->d_name, 453 bpf_fs_initxattrs, NULL); 454 if (ret && ret != -EOPNOTSUPP) { 455 iput(inode); 456 return ret; 457 } 458 459 bpf_dentry_finalize(dentry, inode, dir); 460 return 0; 461 } 462 463 static const struct inode_operations bpf_symlink_iops = { 464 .get_link = simple_get_link, 465 .listxattr = bpf_fs_listxattr, 466 }; 467 468 static const struct inode_operations bpf_dir_iops = { 469 .lookup = bpf_lookup, 470 .mkdir = bpf_mkdir, 471 .symlink = bpf_symlink, 472 .rmdir = simple_rmdir, 473 .rename = simple_rename, 474 .link = simple_link, 475 .unlink = simple_unlink, 476 .listxattr = bpf_fs_listxattr, 477 }; 478 479 /* pin iterator link into bpffs */ 480 static int bpf_iter_link_pin_kernel(struct dentry *parent, 481 const char *name, struct bpf_link *link) 482 { 483 umode_t mode = S_IFREG | S_IRUSR; 484 struct dentry *dentry; 485 int ret; 486 487 dentry = simple_start_creating(parent, name); 488 if (IS_ERR(dentry)) 489 return PTR_ERR(dentry); 490 ret = bpf_mkobj_ops(dentry, mode, link, &bpf_link_iops, 491 &bpf_iter_fops); 492 simple_done_creating(dentry); 493 return ret; 494 } 495 496 static int bpf_obj_do_pin(int path_fd, const char __user *pathname, void *raw, 497 enum bpf_type type) 498 { 499 struct dentry *dentry; 500 struct inode *dir; 501 struct path path; 502 umode_t mode; 503 int ret; 504 505 dentry = start_creating_user_path(path_fd, pathname, &path, 0); 506 if (IS_ERR(dentry)) 507 return PTR_ERR(dentry); 508 509 dir = d_inode(path.dentry); 510 if (dir->i_op != &bpf_dir_iops) { 511 ret = -EPERM; 512 goto out; 513 } 514 515 mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask()); 516 ret = security_path_mknod(&path, dentry, mode, 0); 517 if (ret) 518 goto out; 519 520 switch (type) { 521 case BPF_TYPE_PROG: 522 ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw); 523 break; 524 case BPF_TYPE_MAP: 525 ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw); 526 break; 527 case BPF_TYPE_LINK: 528 ret = vfs_mkobj(dentry, mode, bpf_mklink, raw); 529 break; 530 default: 531 ret = -EPERM; 532 } 533 out: 534 end_creating_path(&path, dentry); 535 return ret; 536 } 537 538 int bpf_obj_pin_user(u32 ufd, int path_fd, const char __user *pathname) 539 { 540 enum bpf_type type; 541 void *raw; 542 int ret; 543 544 raw = bpf_fd_probe_obj(ufd, &type); 545 if (IS_ERR(raw)) 546 return PTR_ERR(raw); 547 548 ret = bpf_obj_do_pin(path_fd, pathname, raw, type); 549 if (ret != 0) 550 bpf_any_put(raw, type); 551 552 return ret; 553 } 554 555 static void *bpf_obj_do_get(int path_fd, const char __user *pathname, 556 enum bpf_type *type, int flags) 557 { 558 struct inode *inode; 559 struct path path; 560 void *raw; 561 int ret; 562 563 ret = user_path_at(path_fd, pathname, LOOKUP_FOLLOW, &path); 564 if (ret) 565 return ERR_PTR(ret); 566 567 inode = d_backing_inode(path.dentry); 568 ret = path_permission(&path, ACC_MODE(flags)); 569 if (ret) 570 goto out; 571 572 ret = bpf_inode_type(inode, type); 573 if (ret) 574 goto out; 575 576 raw = bpf_any_get(inode->i_private, *type); 577 if (!IS_ERR(raw)) 578 touch_atime(&path); 579 580 path_put(&path); 581 return raw; 582 out: 583 path_put(&path); 584 return ERR_PTR(ret); 585 } 586 587 int bpf_obj_get_user(int path_fd, const char __user *pathname, int flags) 588 { 589 enum bpf_type type = BPF_TYPE_UNSPEC; 590 int f_flags; 591 void *raw; 592 int ret; 593 594 f_flags = bpf_get_file_flag(flags); 595 if (f_flags < 0) 596 return f_flags; 597 598 raw = bpf_obj_do_get(path_fd, pathname, &type, f_flags); 599 if (IS_ERR(raw)) 600 return PTR_ERR(raw); 601 602 if (type == BPF_TYPE_PROG) 603 ret = bpf_prog_new_fd(raw); 604 else if (type == BPF_TYPE_MAP) 605 ret = bpf_map_new_fd(raw, f_flags); 606 else if (type == BPF_TYPE_LINK) 607 ret = (f_flags != O_RDWR) ? -EINVAL : bpf_link_new_fd(raw); 608 else 609 return -ENOENT; 610 611 if (ret < 0) 612 bpf_any_put(raw, type); 613 return ret; 614 } 615 616 static struct bpf_prog *__get_prog_inode(struct inode *inode, enum bpf_prog_type type) 617 { 618 struct bpf_prog *prog; 619 int ret = inode_permission(&nop_mnt_idmap, inode, MAY_READ); 620 if (ret) 621 return ERR_PTR(ret); 622 623 if (inode->i_op == &bpf_map_iops) 624 return ERR_PTR(-EINVAL); 625 if (inode->i_op == &bpf_link_iops) 626 return ERR_PTR(-EINVAL); 627 if (inode->i_op != &bpf_prog_iops) 628 return ERR_PTR(-EACCES); 629 630 prog = inode->i_private; 631 632 ret = security_bpf_prog(prog); 633 if (ret < 0) 634 return ERR_PTR(ret); 635 636 if (!bpf_prog_get_ok(prog, &type, false)) 637 return ERR_PTR(-EINVAL); 638 639 bpf_prog_inc(prog); 640 return prog; 641 } 642 643 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type) 644 { 645 struct bpf_prog *prog; 646 struct path path; 647 int ret = kern_path(name, LOOKUP_FOLLOW, &path); 648 if (ret) 649 return ERR_PTR(ret); 650 prog = __get_prog_inode(d_backing_inode(path.dentry), type); 651 if (!IS_ERR(prog)) 652 touch_atime(&path); 653 path_put(&path); 654 return prog; 655 } 656 EXPORT_SYMBOL(bpf_prog_get_type_path); 657 658 struct bpffs_btf_enums { 659 const struct btf *btf; 660 const struct btf_type *cmd_t; 661 const struct btf_type *map_t; 662 const struct btf_type *prog_t; 663 const struct btf_type *attach_t; 664 }; 665 666 static int find_bpffs_btf_enums(struct bpffs_btf_enums *info) 667 { 668 struct { 669 const struct btf_type **type; 670 const char *name; 671 } btf_enums[] = { 672 {&info->cmd_t, "bpf_cmd"}, 673 {&info->map_t, "bpf_map_type"}, 674 {&info->prog_t, "bpf_prog_type"}, 675 {&info->attach_t, "bpf_attach_type"}, 676 }; 677 const struct btf *btf; 678 int i, id; 679 680 memset(info, 0, sizeof(*info)); 681 682 btf = bpf_get_btf_vmlinux(); 683 if (IS_ERR(btf)) 684 return PTR_ERR(btf); 685 if (!btf) 686 return -ENOENT; 687 688 info->btf = btf; 689 690 for (i = 0; i < ARRAY_SIZE(btf_enums); i++) { 691 id = btf_find_by_name_kind(btf, btf_enums[i].name, 692 BTF_KIND_ENUM); 693 if (id < 0) 694 return -ESRCH; 695 696 *btf_enums[i].type = btf_type_by_id(btf, id); 697 } 698 699 return 0; 700 } 701 702 static bool find_btf_enum_const(const struct btf *btf, const struct btf_type *enum_t, 703 const char *prefix, const char *str, int *value) 704 { 705 const struct btf_enum *e; 706 const char *name; 707 int i, n, pfx_len = strlen(prefix); 708 709 *value = 0; 710 711 if (!btf || !enum_t) 712 return false; 713 714 for (i = 0, n = btf_vlen(enum_t); i < n; i++) { 715 e = &btf_enum(enum_t)[i]; 716 717 name = btf_name_by_offset(btf, e->name_off); 718 if (!name || strncasecmp(name, prefix, pfx_len) != 0) 719 continue; 720 721 /* match symbolic name case insensitive and ignoring prefix */ 722 if (strcasecmp(name + pfx_len, str) == 0) { 723 *value = e->val; 724 return true; 725 } 726 } 727 728 return false; 729 } 730 731 static void seq_print_delegate_opts(struct seq_file *m, 732 const char *opt_name, 733 const struct btf *btf, 734 const struct btf_type *enum_t, 735 const char *prefix, 736 u64 delegate_msk, u64 any_msk) 737 { 738 const struct btf_enum *e; 739 bool first = true; 740 const char *name; 741 u64 msk; 742 int i, n, pfx_len = strlen(prefix); 743 744 delegate_msk &= any_msk; /* clear unknown bits */ 745 746 if (delegate_msk == 0) 747 return; 748 749 seq_printf(m, ",%s", opt_name); 750 if (delegate_msk == any_msk) { 751 seq_printf(m, "=any"); 752 return; 753 } 754 755 if (btf && enum_t) { 756 for (i = 0, n = btf_vlen(enum_t); i < n; i++) { 757 e = &btf_enum(enum_t)[i]; 758 name = btf_name_by_offset(btf, e->name_off); 759 if (!name || strncasecmp(name, prefix, pfx_len) != 0) 760 continue; 761 msk = 1ULL << e->val; 762 if (delegate_msk & msk) { 763 /* emit lower-case name without prefix */ 764 seq_putc(m, first ? '=' : ':'); 765 name += pfx_len; 766 while (*name) { 767 seq_putc(m, tolower(*name)); 768 name++; 769 } 770 771 delegate_msk &= ~msk; 772 first = false; 773 } 774 } 775 } 776 if (delegate_msk) 777 seq_printf(m, "%c0x%llx", first ? '=' : ':', delegate_msk); 778 } 779 780 /* 781 * Display the mount options in /proc/mounts. 782 */ 783 static int bpf_show_options(struct seq_file *m, struct dentry *root) 784 { 785 struct inode *inode = d_inode(root); 786 umode_t mode = inode->i_mode & S_IALLUGO & ~S_ISVTX; 787 struct bpf_mount_opts *opts = root->d_sb->s_fs_info; 788 u64 mask; 789 790 if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID)) 791 seq_printf(m, ",uid=%u", 792 from_kuid_munged(&init_user_ns, inode->i_uid)); 793 if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID)) 794 seq_printf(m, ",gid=%u", 795 from_kgid_munged(&init_user_ns, inode->i_gid)); 796 if (mode != S_IRWXUGO) 797 seq_printf(m, ",mode=%o", mode); 798 799 if (opts->delegate_cmds || opts->delegate_maps || 800 opts->delegate_progs || opts->delegate_attachs) { 801 struct bpffs_btf_enums info; 802 803 /* ignore errors, fallback to hex */ 804 (void)find_bpffs_btf_enums(&info); 805 806 mask = (1ULL << __MAX_BPF_CMD) - 1; 807 seq_print_delegate_opts(m, "delegate_cmds", 808 info.btf, info.cmd_t, "BPF_", 809 opts->delegate_cmds, mask); 810 811 mask = (1ULL << __MAX_BPF_MAP_TYPE) - 1; 812 seq_print_delegate_opts(m, "delegate_maps", 813 info.btf, info.map_t, "BPF_MAP_TYPE_", 814 opts->delegate_maps, mask); 815 816 mask = (1ULL << __MAX_BPF_PROG_TYPE) - 1; 817 seq_print_delegate_opts(m, "delegate_progs", 818 info.btf, info.prog_t, "BPF_PROG_TYPE_", 819 opts->delegate_progs, mask); 820 821 mask = (1ULL << __MAX_BPF_ATTACH_TYPE) - 1; 822 seq_print_delegate_opts(m, "delegate_attachs", 823 info.btf, info.attach_t, "BPF_", 824 opts->delegate_attachs, mask); 825 } 826 827 return 0; 828 } 829 830 static struct inode *bpf_fs_alloc_inode(struct super_block *sb) 831 { 832 struct bpf_fs_inode *bi; 833 834 bi = alloc_inode_sb(sb, bpf_fs_inode_cachep, GFP_KERNEL); 835 if (!bi) 836 return NULL; 837 INIT_LIST_HEAD_RCU(&bi->xattrs); 838 simple_xattr_limits_init(&bi->xlimits); 839 return &bi->vfs_inode; 840 } 841 842 static void bpf_destroy_inode(struct inode *inode) 843 { 844 struct bpf_mount_opts *opts = inode->i_sb->s_fs_info; 845 struct bpf_fs_inode *bi = BPF_FS_I(inode); 846 enum bpf_type type; 847 848 if (!bpf_inode_type(inode, &type)) 849 bpf_any_put(inode->i_private, type); 850 simple_xattrs_free(&opts->xa_cache, &bi->xattrs, NULL); 851 } 852 853 /* 854 * Called after RCU grace period - safe to free inode and anything 855 * that might be accessed by RCU pathwalk (inode fields, i_link). 856 */ 857 static void bpf_free_inode(struct inode *inode) 858 { 859 if (S_ISLNK(inode->i_mode)) 860 kfree(inode->i_link); 861 kmem_cache_free(bpf_fs_inode_cachep, BPF_FS_I(inode)); 862 } 863 864 static int bpf_fs_xattr_get(const struct xattr_handler *handler, 865 struct dentry *unused, struct inode *inode, 866 const char *name, void *value, size_t size) 867 { 868 struct bpf_mount_opts *opts = inode->i_sb->s_fs_info; 869 struct bpf_fs_inode *bi = BPF_FS_I(inode); 870 871 name = xattr_full_name(handler, name); 872 return simple_xattr_get(&opts->xa_cache, &bi->xattrs, name, value, size); 873 } 874 875 enum { 876 BPF_FS_XATTR_UNSPEC, 877 BPF_FS_XATTR_SECURITY, 878 BPF_FS_XATTR_TRUSTED, 879 }; 880 881 static int bpf_fs_xattr_set(const struct xattr_handler *handler, 882 const struct mnt_idmap *idmap, struct dentry *unused, 883 struct inode *inode, const char *name, 884 const void *value, size_t size, int flags) 885 { 886 struct bpf_mount_opts *opts = inode->i_sb->s_fs_info; 887 struct bpf_fs_inode *bi = BPF_FS_I(inode); 888 struct simple_xattr *old; 889 int err = -EINVAL; 890 891 name = xattr_full_name(handler, name); 892 switch (handler->flags) { 893 case BPF_FS_XATTR_SECURITY: 894 err = simple_xattr_set_limited(&opts->xa_cache, &bi->xattrs, 895 &bi->xlimits, name, value, size, 896 flags); 897 break; 898 case BPF_FS_XATTR_TRUSTED: 899 old = simple_xattr_set(&opts->xa_cache, &bi->xattrs, name, 900 value, size, flags); 901 err = IS_ERR(old) ? PTR_ERR(old) : 0; 902 if (!err) 903 simple_xattr_free_rcu(old); 904 break; 905 } 906 if (err) 907 return err; 908 inode_set_ctime_current(inode); 909 return 0; 910 } 911 912 static const struct xattr_handler bpf_fs_trusted_xattr_handler = { 913 .prefix = XATTR_TRUSTED_PREFIX, 914 .flags = BPF_FS_XATTR_TRUSTED, 915 .get = bpf_fs_xattr_get, 916 .set = bpf_fs_xattr_set, 917 }; 918 919 static const struct xattr_handler bpf_fs_security_xattr_handler = { 920 .prefix = XATTR_SECURITY_PREFIX, 921 .flags = BPF_FS_XATTR_SECURITY, 922 .get = bpf_fs_xattr_get, 923 .set = bpf_fs_xattr_set, 924 }; 925 926 static const struct xattr_handler * const bpf_fs_xattr_handlers[] = { 927 &bpf_fs_trusted_xattr_handler, 928 &bpf_fs_security_xattr_handler, 929 NULL, 930 }; 931 932 static ssize_t bpf_fs_listxattr(struct dentry *dentry, char *buf, size_t size) 933 { 934 struct inode *inode = d_inode(dentry); 935 936 return simple_xattr_list(inode, &BPF_FS_I(inode)->xattrs, buf, size); 937 } 938 939 static int bpf_fs_initxattrs(struct inode *inode, 940 const struct xattr *xattr_array, void *fs_info) 941 { 942 struct bpf_mount_opts *opts = inode->i_sb->s_fs_info; 943 struct bpf_fs_inode *bi = BPF_FS_I(inode); 944 const struct xattr *xattr; 945 int err; 946 947 for (xattr = xattr_array; xattr->name != NULL; xattr++) { 948 CLASS(simple_xattr, new_xattr)(xattr->value, xattr->value_len); 949 if (IS_ERR(new_xattr)) 950 return PTR_ERR(new_xattr); 951 952 new_xattr->name = kasprintf(GFP_KERNEL_ACCOUNT, 953 XATTR_SECURITY_PREFIX "%s", 954 xattr->name); 955 if (!new_xattr->name) 956 return -ENOMEM; 957 958 err = simple_xattr_add_limited(&opts->xa_cache, &bi->xattrs, 959 &bi->xlimits, new_xattr); 960 if (err) 961 return err; 962 963 retain_and_null_ptr(new_xattr); 964 } 965 return 0; 966 } 967 968 const struct super_operations bpf_super_ops = { 969 .statfs = simple_statfs, 970 .drop_inode = inode_just_drop, 971 .show_options = bpf_show_options, 972 .alloc_inode = bpf_fs_alloc_inode, 973 .destroy_inode = bpf_destroy_inode, 974 .free_inode = bpf_free_inode, 975 }; 976 977 enum { 978 OPT_UID, 979 OPT_GID, 980 OPT_MODE, 981 OPT_DELEGATE_CMDS, 982 OPT_DELEGATE_MAPS, 983 OPT_DELEGATE_PROGS, 984 OPT_DELEGATE_ATTACHS, 985 }; 986 987 static const struct fs_parameter_spec bpf_fs_parameters[] = { 988 fsparam_u32 ("uid", OPT_UID), 989 fsparam_u32 ("gid", OPT_GID), 990 fsparam_u32oct ("mode", OPT_MODE), 991 fsparam_string ("delegate_cmds", OPT_DELEGATE_CMDS), 992 fsparam_string ("delegate_maps", OPT_DELEGATE_MAPS), 993 fsparam_string ("delegate_progs", OPT_DELEGATE_PROGS), 994 fsparam_string ("delegate_attachs", OPT_DELEGATE_ATTACHS), 995 {} 996 }; 997 998 static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param) 999 { 1000 struct bpf_mount_opts *opts = fc->s_fs_info; 1001 struct fs_parse_result result; 1002 kuid_t uid; 1003 kgid_t gid; 1004 int opt, err; 1005 1006 opt = fs_parse(fc, bpf_fs_parameters, param, &result); 1007 if (opt < 0) { 1008 /* We might like to report bad mount options here, but 1009 * traditionally we've ignored all mount options, so we'd 1010 * better continue to ignore non-existing options for bpf. 1011 */ 1012 if (opt == -ENOPARAM) { 1013 opt = vfs_parse_fs_param_source(fc, param); 1014 if (opt != -ENOPARAM) 1015 return opt; 1016 1017 return 0; 1018 } 1019 1020 if (opt < 0) 1021 return opt; 1022 } 1023 1024 switch (opt) { 1025 case OPT_UID: 1026 uid = make_kuid(current_user_ns(), result.uint_32); 1027 if (!uid_valid(uid)) 1028 goto bad_value; 1029 1030 /* 1031 * The requested uid must be representable in the 1032 * filesystem's idmapping. 1033 */ 1034 if (!kuid_has_mapping(fc->user_ns, uid)) 1035 goto bad_value; 1036 1037 opts->uid = uid; 1038 break; 1039 case OPT_GID: 1040 gid = make_kgid(current_user_ns(), result.uint_32); 1041 if (!gid_valid(gid)) 1042 goto bad_value; 1043 1044 /* 1045 * The requested gid must be representable in the 1046 * filesystem's idmapping. 1047 */ 1048 if (!kgid_has_mapping(fc->user_ns, gid)) 1049 goto bad_value; 1050 1051 opts->gid = gid; 1052 break; 1053 case OPT_MODE: 1054 opts->mode = result.uint_32 & S_IALLUGO; 1055 break; 1056 case OPT_DELEGATE_CMDS: 1057 case OPT_DELEGATE_MAPS: 1058 case OPT_DELEGATE_PROGS: 1059 case OPT_DELEGATE_ATTACHS: { 1060 struct bpffs_btf_enums info; 1061 const struct btf_type *enum_t; 1062 const char *enum_pfx; 1063 u64 *delegate_msk, msk = 0, num; 1064 char *p, *str; 1065 int val; 1066 1067 /* ignore errors, fallback to hex */ 1068 (void)find_bpffs_btf_enums(&info); 1069 1070 switch (opt) { 1071 case OPT_DELEGATE_CMDS: 1072 delegate_msk = &opts->delegate_cmds; 1073 enum_t = info.cmd_t; 1074 enum_pfx = "BPF_"; 1075 break; 1076 case OPT_DELEGATE_MAPS: 1077 delegate_msk = &opts->delegate_maps; 1078 enum_t = info.map_t; 1079 enum_pfx = "BPF_MAP_TYPE_"; 1080 break; 1081 case OPT_DELEGATE_PROGS: 1082 delegate_msk = &opts->delegate_progs; 1083 enum_t = info.prog_t; 1084 enum_pfx = "BPF_PROG_TYPE_"; 1085 break; 1086 case OPT_DELEGATE_ATTACHS: 1087 delegate_msk = &opts->delegate_attachs; 1088 enum_t = info.attach_t; 1089 enum_pfx = "BPF_"; 1090 break; 1091 default: 1092 return -EINVAL; 1093 } 1094 1095 str = param->string; 1096 while ((p = strsep(&str, ":"))) { 1097 if (strcmp(p, "any") == 0) { 1098 msk |= ~0ULL; 1099 } else if (find_btf_enum_const(info.btf, enum_t, enum_pfx, p, &val)) { 1100 msk |= 1ULL << val; 1101 } else { 1102 err = kstrtou64(p, 0, &num); 1103 if (err) 1104 return err; 1105 msk |= num; 1106 } 1107 } 1108 1109 /* Setting delegation mount options requires privileges */ 1110 if (msk && !capable(CAP_SYS_ADMIN)) 1111 return -EPERM; 1112 1113 *delegate_msk |= msk; 1114 break; 1115 } 1116 default: 1117 /* ignore unknown mount options */ 1118 break; 1119 } 1120 1121 return 0; 1122 bad_value: 1123 return invalfc(fc, "Bad value for '%s'", param->key); 1124 } 1125 1126 struct bpf_preload_ops *bpf_preload_ops; 1127 EXPORT_SYMBOL_GPL(bpf_preload_ops); 1128 1129 static bool bpf_preload_mod_get(void) 1130 { 1131 /* If bpf_preload.ko wasn't loaded earlier then load it now. 1132 * When bpf_preload is built into vmlinux the module's __init 1133 * function will populate it. 1134 */ 1135 if (!bpf_preload_ops) { 1136 request_module("bpf_preload"); 1137 if (!bpf_preload_ops) 1138 return false; 1139 } 1140 /* And grab the reference, so the module doesn't disappear while the 1141 * kernel is interacting with the kernel module and its UMD. 1142 */ 1143 if (!try_module_get(bpf_preload_ops->owner)) { 1144 pr_err("bpf_preload module get failed.\n"); 1145 return false; 1146 } 1147 return true; 1148 } 1149 1150 static void bpf_preload_mod_put(void) 1151 { 1152 if (bpf_preload_ops) 1153 /* now user can "rmmod bpf_preload" if necessary */ 1154 module_put(bpf_preload_ops->owner); 1155 } 1156 1157 static DEFINE_MUTEX(bpf_preload_lock); 1158 1159 static int populate_bpffs(struct dentry *parent) 1160 { 1161 struct bpf_preload_info objs[BPF_PRELOAD_LINKS] = {}; 1162 int err = 0, i; 1163 1164 /* grab the mutex to make sure the kernel interactions with bpf_preload 1165 * are serialized 1166 */ 1167 mutex_lock(&bpf_preload_lock); 1168 1169 /* if bpf_preload.ko wasn't built into vmlinux then load it */ 1170 if (!bpf_preload_mod_get()) 1171 goto out; 1172 1173 err = bpf_preload_ops->preload(objs); 1174 if (err) 1175 goto out_put; 1176 for (i = 0; i < BPF_PRELOAD_LINKS; i++) { 1177 bpf_link_inc(objs[i].link); 1178 err = bpf_iter_link_pin_kernel(parent, 1179 objs[i].link_name, objs[i].link); 1180 if (err) { 1181 bpf_link_put(objs[i].link); 1182 goto out_put; 1183 } 1184 } 1185 out_put: 1186 bpf_preload_mod_put(); 1187 out: 1188 mutex_unlock(&bpf_preload_lock); 1189 return err; 1190 } 1191 1192 static int bpf_fill_super(struct super_block *sb, struct fs_context *fc) 1193 { 1194 struct bpf_mount_opts *opts = sb->s_fs_info; 1195 struct inode *inode; 1196 1197 /* Mounting an instance of BPF FS requires privileges */ 1198 if (fc->user_ns != &init_user_ns && !capable(CAP_SYS_ADMIN)) 1199 return -EPERM; 1200 1201 sb->s_blocksize = PAGE_SIZE; 1202 sb->s_blocksize_bits = PAGE_SHIFT; 1203 sb->s_magic = BPF_FS_MAGIC; 1204 sb->s_op = &bpf_super_ops; 1205 sb->s_xattr = bpf_fs_xattr_handlers; 1206 sb->s_iflags |= SB_I_NOEXEC; 1207 sb->s_iflags |= SB_I_NODEV; 1208 sb->s_time_gran = 1; 1209 1210 inode = bpf_get_inode(sb, NULL, S_IFDIR | 0777); 1211 if (IS_ERR(inode)) 1212 return PTR_ERR(inode); 1213 1214 inode->i_ino = 1; 1215 inode->i_op = &bpf_dir_iops; 1216 inode->i_fop = &simple_dir_operations; 1217 set_nlink(inode, 2); 1218 1219 sb->s_root = d_make_root(inode); 1220 if (!sb->s_root) 1221 return -ENOMEM; 1222 1223 inode = d_inode(sb->s_root); 1224 inode->i_uid = opts->uid; 1225 inode->i_gid = opts->gid; 1226 inode->i_mode &= ~S_IALLUGO; 1227 populate_bpffs(sb->s_root); 1228 inode->i_mode |= S_ISVTX | opts->mode; 1229 return 0; 1230 } 1231 1232 static int bpf_get_tree(struct fs_context *fc) 1233 { 1234 return get_tree_nodev(fc, bpf_fill_super); 1235 } 1236 1237 static void bpf_free_fc(struct fs_context *fc) 1238 { 1239 kfree(fc->s_fs_info); 1240 } 1241 1242 static const struct fs_context_operations bpf_context_ops = { 1243 .free = bpf_free_fc, 1244 .parse_param = bpf_parse_param, 1245 .get_tree = bpf_get_tree, 1246 }; 1247 1248 /* 1249 * Set up the filesystem mount context. 1250 */ 1251 static int bpf_init_fs_context(struct fs_context *fc) 1252 { 1253 struct bpf_mount_opts *opts; 1254 1255 opts = kzalloc_obj(struct bpf_mount_opts); 1256 if (!opts) 1257 return -ENOMEM; 1258 1259 opts->mode = S_IRWXUGO; 1260 opts->uid = current_fsuid(); 1261 opts->gid = current_fsgid(); 1262 1263 /* start out with no BPF token delegation enabled */ 1264 opts->delegate_cmds = 0; 1265 opts->delegate_maps = 0; 1266 opts->delegate_progs = 0; 1267 opts->delegate_attachs = 0; 1268 1269 fc->s_fs_info = opts; 1270 fc->ops = &bpf_context_ops; 1271 return 0; 1272 } 1273 1274 static void bpf_kill_super(struct super_block *sb) 1275 { 1276 struct bpf_mount_opts *opts = sb->s_fs_info; 1277 1278 kill_anon_super(sb); 1279 simple_xattr_cache_cleanup(&opts->xa_cache); 1280 kfree(opts); 1281 } 1282 1283 static struct file_system_type bpf_fs_type = { 1284 .owner = THIS_MODULE, 1285 .name = "bpf", 1286 .init_fs_context = bpf_init_fs_context, 1287 .parameters = bpf_fs_parameters, 1288 .kill_sb = bpf_kill_super, 1289 .fs_flags = FS_USERNS_MOUNT, 1290 }; 1291 1292 static void bpf_fs_inode_init_once(void *foo) 1293 { 1294 struct bpf_fs_inode *bi = foo; 1295 1296 inode_init_once(&bi->vfs_inode); 1297 } 1298 1299 static int __init bpf_init(void) 1300 { 1301 int ret; 1302 1303 bpf_fs_inode_cachep = kmem_cache_create("bpf_fs_inode_cache", 1304 sizeof(struct bpf_fs_inode), 1305 0, SLAB_ACCOUNT, 1306 bpf_fs_inode_init_once); 1307 if (!bpf_fs_inode_cachep) 1308 return -ENOMEM; 1309 1310 ret = sysfs_create_mount_point(fs_kobj, "bpf"); 1311 if (ret) 1312 goto out_cache; 1313 1314 ret = register_filesystem(&bpf_fs_type); 1315 if (ret) { 1316 sysfs_remove_mount_point(fs_kobj, "bpf"); 1317 goto out_cache; 1318 } 1319 1320 return 0; 1321 out_cache: 1322 kmem_cache_destroy(bpf_fs_inode_cachep); 1323 return ret; 1324 } 1325 fs_initcall(bpf_init); 1326