inode.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. #include <linux/init.h>
  11. #include <linux/magic.h>
  12. #include <linux/major.h>
  13. #include <linux/mount.h>
  14. #include <linux/namei.h>
  15. #include <linux/fs.h>
  16. #include <linux/fs_context.h>
  17. #include <linux/fs_parser.h>
  18. #include <linux/kdev_t.h>
  19. #include <linux/filter.h>
  20. #include <linux/bpf.h>
  21. #include <linux/bpf_trace.h>
  22. #include <linux/kstrtox.h>
  23. #include "preload/bpf_preload.h"
  24. enum bpf_type {
  25. BPF_TYPE_UNSPEC = 0,
  26. BPF_TYPE_PROG,
  27. BPF_TYPE_MAP,
  28. BPF_TYPE_LINK,
  29. };
  30. static void *bpf_any_get(void *raw, enum bpf_type type)
  31. {
  32. switch (type) {
  33. case BPF_TYPE_PROG:
  34. bpf_prog_inc(raw);
  35. break;
  36. case BPF_TYPE_MAP:
  37. bpf_map_inc_with_uref(raw);
  38. break;
  39. case BPF_TYPE_LINK:
  40. bpf_link_inc(raw);
  41. break;
  42. default:
  43. WARN_ON_ONCE(1);
  44. break;
  45. }
  46. return raw;
  47. }
  48. static void bpf_any_put(void *raw, enum bpf_type type)
  49. {
  50. switch (type) {
  51. case BPF_TYPE_PROG:
  52. bpf_prog_put(raw);
  53. break;
  54. case BPF_TYPE_MAP:
  55. bpf_map_put_with_uref(raw);
  56. break;
  57. case BPF_TYPE_LINK:
  58. bpf_link_put(raw);
  59. break;
  60. default:
  61. WARN_ON_ONCE(1);
  62. break;
  63. }
  64. }
  65. static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
  66. {
  67. void *raw;
  68. raw = bpf_map_get_with_uref(ufd);
  69. if (!IS_ERR(raw)) {
  70. *type = BPF_TYPE_MAP;
  71. return raw;
  72. }
  73. raw = bpf_prog_get(ufd);
  74. if (!IS_ERR(raw)) {
  75. *type = BPF_TYPE_PROG;
  76. return raw;
  77. }
  78. raw = bpf_link_get_from_fd(ufd);
  79. if (!IS_ERR(raw)) {
  80. *type = BPF_TYPE_LINK;
  81. return raw;
  82. }
  83. return ERR_PTR(-EINVAL);
  84. }
  85. static const struct inode_operations bpf_dir_iops;
  86. static const struct inode_operations bpf_prog_iops = { };
  87. static const struct inode_operations bpf_map_iops = { };
  88. static const struct inode_operations bpf_link_iops = { };
  89. struct inode *bpf_get_inode(struct super_block *sb,
  90. const struct inode *dir,
  91. umode_t mode)
  92. {
  93. struct inode *inode;
  94. switch (mode & S_IFMT) {
  95. case S_IFDIR:
  96. case S_IFREG:
  97. case S_IFLNK:
  98. break;
  99. default:
  100. return ERR_PTR(-EINVAL);
  101. }
  102. inode = new_inode(sb);
  103. if (!inode)
  104. return ERR_PTR(-ENOSPC);
  105. inode->i_ino = get_next_ino();
  106. simple_inode_init_ts(inode);
  107. inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
  108. return inode;
  109. }
  110. static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
  111. {
  112. *type = BPF_TYPE_UNSPEC;
  113. if (inode->i_op == &bpf_prog_iops)
  114. *type = BPF_TYPE_PROG;
  115. else if (inode->i_op == &bpf_map_iops)
  116. *type = BPF_TYPE_MAP;
  117. else if (inode->i_op == &bpf_link_iops)
  118. *type = BPF_TYPE_LINK;
  119. else
  120. return -EACCES;
  121. return 0;
  122. }
  123. static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
  124. struct inode *dir)
  125. {
  126. d_instantiate(dentry, inode);
  127. dget(dentry);
  128. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  129. }
  130. static int bpf_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  131. struct dentry *dentry, umode_t mode)
  132. {
  133. struct inode *inode;
  134. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
  135. if (IS_ERR(inode))
  136. return PTR_ERR(inode);
  137. inode->i_op = &bpf_dir_iops;
  138. inode->i_fop = &simple_dir_operations;
  139. inc_nlink(inode);
  140. inc_nlink(dir);
  141. bpf_dentry_finalize(dentry, inode, dir);
  142. return 0;
  143. }
  144. struct map_iter {
  145. void *key;
  146. bool done;
  147. };
  148. static struct map_iter *map_iter(struct seq_file *m)
  149. {
  150. return m->private;
  151. }
  152. static struct bpf_map *seq_file_to_map(struct seq_file *m)
  153. {
  154. return file_inode(m->file)->i_private;
  155. }
  156. static void map_iter_free(struct map_iter *iter)
  157. {
  158. if (iter) {
  159. kfree(iter->key);
  160. kfree(iter);
  161. }
  162. }
  163. static struct map_iter *map_iter_alloc(struct bpf_map *map)
  164. {
  165. struct map_iter *iter;
  166. iter = kzalloc(sizeof(*iter), GFP_KERNEL | __GFP_NOWARN);
  167. if (!iter)
  168. goto error;
  169. iter->key = kzalloc(map->key_size, GFP_KERNEL | __GFP_NOWARN);
  170. if (!iter->key)
  171. goto error;
  172. return iter;
  173. error:
  174. map_iter_free(iter);
  175. return NULL;
  176. }
  177. static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
  178. {
  179. struct bpf_map *map = seq_file_to_map(m);
  180. void *key = map_iter(m)->key;
  181. void *prev_key;
  182. (*pos)++;
  183. if (map_iter(m)->done)
  184. return NULL;
  185. if (unlikely(v == SEQ_START_TOKEN))
  186. prev_key = NULL;
  187. else
  188. prev_key = key;
  189. rcu_read_lock();
  190. if (map->ops->map_get_next_key(map, prev_key, key)) {
  191. map_iter(m)->done = true;
  192. key = NULL;
  193. }
  194. rcu_read_unlock();
  195. return key;
  196. }
  197. static void *map_seq_start(struct seq_file *m, loff_t *pos)
  198. {
  199. if (map_iter(m)->done)
  200. return NULL;
  201. return *pos ? map_iter(m)->key : SEQ_START_TOKEN;
  202. }
  203. static void map_seq_stop(struct seq_file *m, void *v)
  204. {
  205. }
  206. static int map_seq_show(struct seq_file *m, void *v)
  207. {
  208. struct bpf_map *map = seq_file_to_map(m);
  209. void *key = map_iter(m)->key;
  210. if (unlikely(v == SEQ_START_TOKEN)) {
  211. seq_puts(m, "# WARNING!! The output is for debug purpose only\n");
  212. seq_puts(m, "# WARNING!! The output format will change\n");
  213. } else {
  214. map->ops->map_seq_show_elem(map, key, m);
  215. }
  216. return 0;
  217. }
  218. static const struct seq_operations bpffs_map_seq_ops = {
  219. .start = map_seq_start,
  220. .next = map_seq_next,
  221. .show = map_seq_show,
  222. .stop = map_seq_stop,
  223. };
  224. static int bpffs_map_open(struct inode *inode, struct file *file)
  225. {
  226. struct bpf_map *map = inode->i_private;
  227. struct map_iter *iter;
  228. struct seq_file *m;
  229. int err;
  230. iter = map_iter_alloc(map);
  231. if (!iter)
  232. return -ENOMEM;
  233. err = seq_open(file, &bpffs_map_seq_ops);
  234. if (err) {
  235. map_iter_free(iter);
  236. return err;
  237. }
  238. m = file->private_data;
  239. m->private = iter;
  240. return 0;
  241. }
  242. static int bpffs_map_release(struct inode *inode, struct file *file)
  243. {
  244. struct seq_file *m = file->private_data;
  245. map_iter_free(map_iter(m));
  246. return seq_release(inode, file);
  247. }
  248. /* bpffs_map_fops should only implement the basic
  249. * read operation for a BPF map. The purpose is to
  250. * provide a simple user intuitive way to do
  251. * "cat bpffs/pathto/a-pinned-map".
  252. *
  253. * Other operations (e.g. write, lookup...) should be realized by
  254. * the userspace tools (e.g. bpftool) through the
  255. * BPF_OBJ_GET_INFO_BY_FD and the map's lookup/update
  256. * interface.
  257. */
  258. static const struct file_operations bpffs_map_fops = {
  259. .open = bpffs_map_open,
  260. .read = seq_read,
  261. .release = bpffs_map_release,
  262. };
  263. static int bpffs_obj_open(struct inode *inode, struct file *file)
  264. {
  265. return -EIO;
  266. }
  267. static const struct file_operations bpffs_obj_fops = {
  268. .open = bpffs_obj_open,
  269. };
  270. static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw,
  271. const struct inode_operations *iops,
  272. const struct file_operations *fops)
  273. {
  274. struct inode *dir = dentry->d_parent->d_inode;
  275. struct inode *inode = bpf_get_inode(dir->i_sb, dir, mode);
  276. if (IS_ERR(inode))
  277. return PTR_ERR(inode);
  278. inode->i_op = iops;
  279. inode->i_fop = fops;
  280. inode->i_private = raw;
  281. bpf_dentry_finalize(dentry, inode, dir);
  282. return 0;
  283. }
  284. static int bpf_mkprog(struct dentry *dentry, umode_t mode, void *arg)
  285. {
  286. return bpf_mkobj_ops(dentry, mode, arg, &bpf_prog_iops,
  287. &bpffs_obj_fops);
  288. }
  289. static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
  290. {
  291. struct bpf_map *map = arg;
  292. return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
  293. bpf_map_support_seq_show(map) ?
  294. &bpffs_map_fops : &bpffs_obj_fops);
  295. }
  296. static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
  297. {
  298. struct bpf_link *link = arg;
  299. return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
  300. bpf_link_is_iter(link) ?
  301. &bpf_iter_fops : &bpffs_obj_fops);
  302. }
  303. static struct dentry *
  304. bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
  305. {
  306. /* Dots in names (e.g. "/sys/fs/bpf/foo.bar") are reserved for future
  307. * extensions. That allows popoulate_bpffs() create special files.
  308. */
  309. if ((dir->i_mode & S_IALLUGO) &&
  310. strchr(dentry->d_name.name, '.'))
  311. return ERR_PTR(-EPERM);
  312. return simple_lookup(dir, dentry, flags);
  313. }
  314. static int bpf_symlink(struct mnt_idmap *idmap, struct inode *dir,
  315. struct dentry *dentry, const char *target)
  316. {
  317. char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
  318. struct inode *inode;
  319. if (!link)
  320. return -ENOMEM;
  321. inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
  322. if (IS_ERR(inode)) {
  323. kfree(link);
  324. return PTR_ERR(inode);
  325. }
  326. inode->i_op = &simple_symlink_inode_operations;
  327. inode->i_link = link;
  328. bpf_dentry_finalize(dentry, inode, dir);
  329. return 0;
  330. }
  331. static const struct inode_operations bpf_dir_iops = {
  332. .lookup = bpf_lookup,
  333. .mkdir = bpf_mkdir,
  334. .symlink = bpf_symlink,
  335. .rmdir = simple_rmdir,
  336. .rename = simple_rename,
  337. .link = simple_link,
  338. .unlink = simple_unlink,
  339. };
  340. /* pin iterator link into bpffs */
  341. static int bpf_iter_link_pin_kernel(struct dentry *parent,
  342. const char *name, struct bpf_link *link)
  343. {
  344. umode_t mode = S_IFREG | S_IRUSR;
  345. struct dentry *dentry;
  346. int ret;
  347. inode_lock(parent->d_inode);
  348. dentry = lookup_one_len(name, parent, strlen(name));
  349. if (IS_ERR(dentry)) {
  350. inode_unlock(parent->d_inode);
  351. return PTR_ERR(dentry);
  352. }
  353. ret = bpf_mkobj_ops(dentry, mode, link, &bpf_link_iops,
  354. &bpf_iter_fops);
  355. dput(dentry);
  356. inode_unlock(parent->d_inode);
  357. return ret;
  358. }
  359. static int bpf_obj_do_pin(int path_fd, const char __user *pathname, void *raw,
  360. enum bpf_type type)
  361. {
  362. struct dentry *dentry;
  363. struct inode *dir;
  364. struct path path;
  365. umode_t mode;
  366. int ret;
  367. dentry = user_path_create(path_fd, pathname, &path, 0);
  368. if (IS_ERR(dentry))
  369. return PTR_ERR(dentry);
  370. dir = d_inode(path.dentry);
  371. if (dir->i_op != &bpf_dir_iops) {
  372. ret = -EPERM;
  373. goto out;
  374. }
  375. mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
  376. ret = security_path_mknod(&path, dentry, mode, 0);
  377. if (ret)
  378. goto out;
  379. switch (type) {
  380. case BPF_TYPE_PROG:
  381. ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
  382. break;
  383. case BPF_TYPE_MAP:
  384. ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw);
  385. break;
  386. case BPF_TYPE_LINK:
  387. ret = vfs_mkobj(dentry, mode, bpf_mklink, raw);
  388. break;
  389. default:
  390. ret = -EPERM;
  391. }
  392. out:
  393. done_path_create(&path, dentry);
  394. return ret;
  395. }
  396. int bpf_obj_pin_user(u32 ufd, int path_fd, const char __user *pathname)
  397. {
  398. enum bpf_type type;
  399. void *raw;
  400. int ret;
  401. raw = bpf_fd_probe_obj(ufd, &type);
  402. if (IS_ERR(raw))
  403. return PTR_ERR(raw);
  404. ret = bpf_obj_do_pin(path_fd, pathname, raw, type);
  405. if (ret != 0)
  406. bpf_any_put(raw, type);
  407. return ret;
  408. }
  409. static void *bpf_obj_do_get(int path_fd, const char __user *pathname,
  410. enum bpf_type *type, int flags)
  411. {
  412. struct inode *inode;
  413. struct path path;
  414. void *raw;
  415. int ret;
  416. ret = user_path_at(path_fd, pathname, LOOKUP_FOLLOW, &path);
  417. if (ret)
  418. return ERR_PTR(ret);
  419. inode = d_backing_inode(path.dentry);
  420. ret = path_permission(&path, ACC_MODE(flags));
  421. if (ret)
  422. goto out;
  423. ret = bpf_inode_type(inode, type);
  424. if (ret)
  425. goto out;
  426. raw = bpf_any_get(inode->i_private, *type);
  427. if (!IS_ERR(raw))
  428. touch_atime(&path);
  429. path_put(&path);
  430. return raw;
  431. out:
  432. path_put(&path);
  433. return ERR_PTR(ret);
  434. }
  435. int bpf_obj_get_user(int path_fd, const char __user *pathname, int flags)
  436. {
  437. enum bpf_type type = BPF_TYPE_UNSPEC;
  438. int f_flags;
  439. void *raw;
  440. int ret;
  441. f_flags = bpf_get_file_flag(flags);
  442. if (f_flags < 0)
  443. return f_flags;
  444. raw = bpf_obj_do_get(path_fd, pathname, &type, f_flags);
  445. if (IS_ERR(raw))
  446. return PTR_ERR(raw);
  447. if (type == BPF_TYPE_PROG)
  448. ret = bpf_prog_new_fd(raw);
  449. else if (type == BPF_TYPE_MAP)
  450. ret = bpf_map_new_fd(raw, f_flags);
  451. else if (type == BPF_TYPE_LINK)
  452. ret = (f_flags != O_RDWR) ? -EINVAL : bpf_link_new_fd(raw);
  453. else
  454. return -ENOENT;
  455. if (ret < 0)
  456. bpf_any_put(raw, type);
  457. return ret;
  458. }
  459. static struct bpf_prog *__get_prog_inode(struct inode *inode, enum bpf_prog_type type)
  460. {
  461. struct bpf_prog *prog;
  462. int ret = inode_permission(&nop_mnt_idmap, inode, MAY_READ);
  463. if (ret)
  464. return ERR_PTR(ret);
  465. if (inode->i_op == &bpf_map_iops)
  466. return ERR_PTR(-EINVAL);
  467. if (inode->i_op == &bpf_link_iops)
  468. return ERR_PTR(-EINVAL);
  469. if (inode->i_op != &bpf_prog_iops)
  470. return ERR_PTR(-EACCES);
  471. prog = inode->i_private;
  472. ret = security_bpf_prog(prog);
  473. if (ret < 0)
  474. return ERR_PTR(ret);
  475. if (!bpf_prog_get_ok(prog, &type, false))
  476. return ERR_PTR(-EINVAL);
  477. bpf_prog_inc(prog);
  478. return prog;
  479. }
  480. struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type)
  481. {
  482. struct bpf_prog *prog;
  483. struct path path;
  484. int ret = kern_path(name, LOOKUP_FOLLOW, &path);
  485. if (ret)
  486. return ERR_PTR(ret);
  487. prog = __get_prog_inode(d_backing_inode(path.dentry), type);
  488. if (!IS_ERR(prog))
  489. touch_atime(&path);
  490. path_put(&path);
  491. return prog;
  492. }
  493. EXPORT_SYMBOL(bpf_prog_get_type_path);
  494. struct bpffs_btf_enums {
  495. const struct btf *btf;
  496. const struct btf_type *cmd_t;
  497. const struct btf_type *map_t;
  498. const struct btf_type *prog_t;
  499. const struct btf_type *attach_t;
  500. };
  501. static int find_bpffs_btf_enums(struct bpffs_btf_enums *info)
  502. {
  503. const struct btf *btf;
  504. const struct btf_type *t;
  505. const char *name;
  506. int i, n;
  507. memset(info, 0, sizeof(*info));
  508. btf = bpf_get_btf_vmlinux();
  509. if (IS_ERR(btf))
  510. return PTR_ERR(btf);
  511. if (!btf)
  512. return -ENOENT;
  513. info->btf = btf;
  514. for (i = 1, n = btf_nr_types(btf); i < n; i++) {
  515. t = btf_type_by_id(btf, i);
  516. if (!btf_type_is_enum(t))
  517. continue;
  518. name = btf_name_by_offset(btf, t->name_off);
  519. if (!name)
  520. continue;
  521. if (strcmp(name, "bpf_cmd") == 0)
  522. info->cmd_t = t;
  523. else if (strcmp(name, "bpf_map_type") == 0)
  524. info->map_t = t;
  525. else if (strcmp(name, "bpf_prog_type") == 0)
  526. info->prog_t = t;
  527. else if (strcmp(name, "bpf_attach_type") == 0)
  528. info->attach_t = t;
  529. else
  530. continue;
  531. if (info->cmd_t && info->map_t && info->prog_t && info->attach_t)
  532. return 0;
  533. }
  534. return -ESRCH;
  535. }
  536. static bool find_btf_enum_const(const struct btf *btf, const struct btf_type *enum_t,
  537. const char *prefix, const char *str, int *value)
  538. {
  539. const struct btf_enum *e;
  540. const char *name;
  541. int i, n, pfx_len = strlen(prefix);
  542. *value = 0;
  543. if (!btf || !enum_t)
  544. return false;
  545. for (i = 0, n = btf_vlen(enum_t); i < n; i++) {
  546. e = &btf_enum(enum_t)[i];
  547. name = btf_name_by_offset(btf, e->name_off);
  548. if (!name || strncasecmp(name, prefix, pfx_len) != 0)
  549. continue;
  550. /* match symbolic name case insensitive and ignoring prefix */
  551. if (strcasecmp(name + pfx_len, str) == 0) {
  552. *value = e->val;
  553. return true;
  554. }
  555. }
  556. return false;
  557. }
  558. static void seq_print_delegate_opts(struct seq_file *m,
  559. const char *opt_name,
  560. const struct btf *btf,
  561. const struct btf_type *enum_t,
  562. const char *prefix,
  563. u64 delegate_msk, u64 any_msk)
  564. {
  565. const struct btf_enum *e;
  566. bool first = true;
  567. const char *name;
  568. u64 msk;
  569. int i, n, pfx_len = strlen(prefix);
  570. delegate_msk &= any_msk; /* clear unknown bits */
  571. if (delegate_msk == 0)
  572. return;
  573. seq_printf(m, ",%s", opt_name);
  574. if (delegate_msk == any_msk) {
  575. seq_printf(m, "=any");
  576. return;
  577. }
  578. if (btf && enum_t) {
  579. for (i = 0, n = btf_vlen(enum_t); i < n; i++) {
  580. e = &btf_enum(enum_t)[i];
  581. name = btf_name_by_offset(btf, e->name_off);
  582. if (!name || strncasecmp(name, prefix, pfx_len) != 0)
  583. continue;
  584. msk = 1ULL << e->val;
  585. if (delegate_msk & msk) {
  586. /* emit lower-case name without prefix */
  587. seq_putc(m, first ? '=' : ':');
  588. name += pfx_len;
  589. while (*name) {
  590. seq_putc(m, tolower(*name));
  591. name++;
  592. }
  593. delegate_msk &= ~msk;
  594. first = false;
  595. }
  596. }
  597. }
  598. if (delegate_msk)
  599. seq_printf(m, "%c0x%llx", first ? '=' : ':', delegate_msk);
  600. }
  601. /*
  602. * Display the mount options in /proc/mounts.
  603. */
  604. static int bpf_show_options(struct seq_file *m, struct dentry *root)
  605. {
  606. struct inode *inode = d_inode(root);
  607. umode_t mode = inode->i_mode & S_IALLUGO & ~S_ISVTX;
  608. struct bpf_mount_opts *opts = root->d_sb->s_fs_info;
  609. u64 mask;
  610. if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID))
  611. seq_printf(m, ",uid=%u",
  612. from_kuid_munged(&init_user_ns, inode->i_uid));
  613. if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID))
  614. seq_printf(m, ",gid=%u",
  615. from_kgid_munged(&init_user_ns, inode->i_gid));
  616. if (mode != S_IRWXUGO)
  617. seq_printf(m, ",mode=%o", mode);
  618. if (opts->delegate_cmds || opts->delegate_maps ||
  619. opts->delegate_progs || opts->delegate_attachs) {
  620. struct bpffs_btf_enums info;
  621. /* ignore errors, fallback to hex */
  622. (void)find_bpffs_btf_enums(&info);
  623. mask = (1ULL << __MAX_BPF_CMD) - 1;
  624. seq_print_delegate_opts(m, "delegate_cmds",
  625. info.btf, info.cmd_t, "BPF_",
  626. opts->delegate_cmds, mask);
  627. mask = (1ULL << __MAX_BPF_MAP_TYPE) - 1;
  628. seq_print_delegate_opts(m, "delegate_maps",
  629. info.btf, info.map_t, "BPF_MAP_TYPE_",
  630. opts->delegate_maps, mask);
  631. mask = (1ULL << __MAX_BPF_PROG_TYPE) - 1;
  632. seq_print_delegate_opts(m, "delegate_progs",
  633. info.btf, info.prog_t, "BPF_PROG_TYPE_",
  634. opts->delegate_progs, mask);
  635. mask = (1ULL << __MAX_BPF_ATTACH_TYPE) - 1;
  636. seq_print_delegate_opts(m, "delegate_attachs",
  637. info.btf, info.attach_t, "BPF_",
  638. opts->delegate_attachs, mask);
  639. }
  640. return 0;
  641. }
  642. static void bpf_destroy_inode(struct inode *inode)
  643. {
  644. enum bpf_type type;
  645. if (S_ISLNK(inode->i_mode))
  646. kfree(inode->i_link);
  647. if (!bpf_inode_type(inode, &type))
  648. bpf_any_put(inode->i_private, type);
  649. free_inode_nonrcu(inode);
  650. }
  651. const struct super_operations bpf_super_ops = {
  652. .statfs = simple_statfs,
  653. .drop_inode = generic_delete_inode,
  654. .show_options = bpf_show_options,
  655. .destroy_inode = bpf_destroy_inode,
  656. };
  657. enum {
  658. OPT_UID,
  659. OPT_GID,
  660. OPT_MODE,
  661. OPT_DELEGATE_CMDS,
  662. OPT_DELEGATE_MAPS,
  663. OPT_DELEGATE_PROGS,
  664. OPT_DELEGATE_ATTACHS,
  665. };
  666. static const struct fs_parameter_spec bpf_fs_parameters[] = {
  667. fsparam_u32 ("uid", OPT_UID),
  668. fsparam_u32 ("gid", OPT_GID),
  669. fsparam_u32oct ("mode", OPT_MODE),
  670. fsparam_string ("delegate_cmds", OPT_DELEGATE_CMDS),
  671. fsparam_string ("delegate_maps", OPT_DELEGATE_MAPS),
  672. fsparam_string ("delegate_progs", OPT_DELEGATE_PROGS),
  673. fsparam_string ("delegate_attachs", OPT_DELEGATE_ATTACHS),
  674. {}
  675. };
  676. static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)
  677. {
  678. struct bpf_mount_opts *opts = fc->s_fs_info;
  679. struct fs_parse_result result;
  680. kuid_t uid;
  681. kgid_t gid;
  682. int opt, err;
  683. opt = fs_parse(fc, bpf_fs_parameters, param, &result);
  684. if (opt < 0) {
  685. /* We might like to report bad mount options here, but
  686. * traditionally we've ignored all mount options, so we'd
  687. * better continue to ignore non-existing options for bpf.
  688. */
  689. if (opt == -ENOPARAM) {
  690. opt = vfs_parse_fs_param_source(fc, param);
  691. if (opt != -ENOPARAM)
  692. return opt;
  693. return 0;
  694. }
  695. if (opt < 0)
  696. return opt;
  697. }
  698. switch (opt) {
  699. case OPT_UID:
  700. uid = make_kuid(current_user_ns(), result.uint_32);
  701. if (!uid_valid(uid))
  702. goto bad_value;
  703. /*
  704. * The requested uid must be representable in the
  705. * filesystem's idmapping.
  706. */
  707. if (!kuid_has_mapping(fc->user_ns, uid))
  708. goto bad_value;
  709. opts->uid = uid;
  710. break;
  711. case OPT_GID:
  712. gid = make_kgid(current_user_ns(), result.uint_32);
  713. if (!gid_valid(gid))
  714. goto bad_value;
  715. /*
  716. * The requested gid must be representable in the
  717. * filesystem's idmapping.
  718. */
  719. if (!kgid_has_mapping(fc->user_ns, gid))
  720. goto bad_value;
  721. opts->gid = gid;
  722. break;
  723. case OPT_MODE:
  724. opts->mode = result.uint_32 & S_IALLUGO;
  725. break;
  726. case OPT_DELEGATE_CMDS:
  727. case OPT_DELEGATE_MAPS:
  728. case OPT_DELEGATE_PROGS:
  729. case OPT_DELEGATE_ATTACHS: {
  730. struct bpffs_btf_enums info;
  731. const struct btf_type *enum_t;
  732. const char *enum_pfx;
  733. u64 *delegate_msk, msk = 0;
  734. char *p, *str;
  735. int val;
  736. /* ignore errors, fallback to hex */
  737. (void)find_bpffs_btf_enums(&info);
  738. switch (opt) {
  739. case OPT_DELEGATE_CMDS:
  740. delegate_msk = &opts->delegate_cmds;
  741. enum_t = info.cmd_t;
  742. enum_pfx = "BPF_";
  743. break;
  744. case OPT_DELEGATE_MAPS:
  745. delegate_msk = &opts->delegate_maps;
  746. enum_t = info.map_t;
  747. enum_pfx = "BPF_MAP_TYPE_";
  748. break;
  749. case OPT_DELEGATE_PROGS:
  750. delegate_msk = &opts->delegate_progs;
  751. enum_t = info.prog_t;
  752. enum_pfx = "BPF_PROG_TYPE_";
  753. break;
  754. case OPT_DELEGATE_ATTACHS:
  755. delegate_msk = &opts->delegate_attachs;
  756. enum_t = info.attach_t;
  757. enum_pfx = "BPF_";
  758. break;
  759. default:
  760. return -EINVAL;
  761. }
  762. str = param->string;
  763. while ((p = strsep(&str, ":"))) {
  764. if (strcmp(p, "any") == 0) {
  765. msk |= ~0ULL;
  766. } else if (find_btf_enum_const(info.btf, enum_t, enum_pfx, p, &val)) {
  767. msk |= 1ULL << val;
  768. } else {
  769. err = kstrtou64(p, 0, &msk);
  770. if (err)
  771. return err;
  772. }
  773. }
  774. /* Setting delegation mount options requires privileges */
  775. if (msk && !capable(CAP_SYS_ADMIN))
  776. return -EPERM;
  777. *delegate_msk |= msk;
  778. break;
  779. }
  780. default:
  781. /* ignore unknown mount options */
  782. break;
  783. }
  784. return 0;
  785. bad_value:
  786. return invalfc(fc, "Bad value for '%s'", param->key);
  787. }
  788. struct bpf_preload_ops *bpf_preload_ops;
  789. EXPORT_SYMBOL_GPL(bpf_preload_ops);
  790. static bool bpf_preload_mod_get(void)
  791. {
  792. /* If bpf_preload.ko wasn't loaded earlier then load it now.
  793. * When bpf_preload is built into vmlinux the module's __init
  794. * function will populate it.
  795. */
  796. if (!bpf_preload_ops) {
  797. request_module("bpf_preload");
  798. if (!bpf_preload_ops)
  799. return false;
  800. }
  801. /* And grab the reference, so the module doesn't disappear while the
  802. * kernel is interacting with the kernel module and its UMD.
  803. */
  804. if (!try_module_get(bpf_preload_ops->owner)) {
  805. pr_err("bpf_preload module get failed.\n");
  806. return false;
  807. }
  808. return true;
  809. }
  810. static void bpf_preload_mod_put(void)
  811. {
  812. if (bpf_preload_ops)
  813. /* now user can "rmmod bpf_preload" if necessary */
  814. module_put(bpf_preload_ops->owner);
  815. }
  816. static DEFINE_MUTEX(bpf_preload_lock);
  817. static int populate_bpffs(struct dentry *parent)
  818. {
  819. struct bpf_preload_info objs[BPF_PRELOAD_LINKS] = {};
  820. int err = 0, i;
  821. /* grab the mutex to make sure the kernel interactions with bpf_preload
  822. * are serialized
  823. */
  824. mutex_lock(&bpf_preload_lock);
  825. /* if bpf_preload.ko wasn't built into vmlinux then load it */
  826. if (!bpf_preload_mod_get())
  827. goto out;
  828. err = bpf_preload_ops->preload(objs);
  829. if (err)
  830. goto out_put;
  831. for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
  832. bpf_link_inc(objs[i].link);
  833. err = bpf_iter_link_pin_kernel(parent,
  834. objs[i].link_name, objs[i].link);
  835. if (err) {
  836. bpf_link_put(objs[i].link);
  837. goto out_put;
  838. }
  839. }
  840. out_put:
  841. bpf_preload_mod_put();
  842. out:
  843. mutex_unlock(&bpf_preload_lock);
  844. return err;
  845. }
  846. static int bpf_fill_super(struct super_block *sb, struct fs_context *fc)
  847. {
  848. static const struct tree_descr bpf_rfiles[] = { { "" } };
  849. struct bpf_mount_opts *opts = sb->s_fs_info;
  850. struct inode *inode;
  851. int ret;
  852. /* Mounting an instance of BPF FS requires privileges */
  853. if (fc->user_ns != &init_user_ns && !capable(CAP_SYS_ADMIN))
  854. return -EPERM;
  855. ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
  856. if (ret)
  857. return ret;
  858. sb->s_op = &bpf_super_ops;
  859. inode = sb->s_root->d_inode;
  860. inode->i_uid = opts->uid;
  861. inode->i_gid = opts->gid;
  862. inode->i_op = &bpf_dir_iops;
  863. inode->i_mode &= ~S_IALLUGO;
  864. populate_bpffs(sb->s_root);
  865. inode->i_mode |= S_ISVTX | opts->mode;
  866. return 0;
  867. }
  868. static int bpf_get_tree(struct fs_context *fc)
  869. {
  870. return get_tree_nodev(fc, bpf_fill_super);
  871. }
  872. static void bpf_free_fc(struct fs_context *fc)
  873. {
  874. kfree(fc->s_fs_info);
  875. }
  876. static const struct fs_context_operations bpf_context_ops = {
  877. .free = bpf_free_fc,
  878. .parse_param = bpf_parse_param,
  879. .get_tree = bpf_get_tree,
  880. };
  881. /*
  882. * Set up the filesystem mount context.
  883. */
  884. static int bpf_init_fs_context(struct fs_context *fc)
  885. {
  886. struct bpf_mount_opts *opts;
  887. opts = kzalloc(sizeof(struct bpf_mount_opts), GFP_KERNEL);
  888. if (!opts)
  889. return -ENOMEM;
  890. opts->mode = S_IRWXUGO;
  891. opts->uid = current_fsuid();
  892. opts->gid = current_fsgid();
  893. /* start out with no BPF token delegation enabled */
  894. opts->delegate_cmds = 0;
  895. opts->delegate_maps = 0;
  896. opts->delegate_progs = 0;
  897. opts->delegate_attachs = 0;
  898. fc->s_fs_info = opts;
  899. fc->ops = &bpf_context_ops;
  900. return 0;
  901. }
  902. static void bpf_kill_super(struct super_block *sb)
  903. {
  904. struct bpf_mount_opts *opts = sb->s_fs_info;
  905. kill_litter_super(sb);
  906. kfree(opts);
  907. }
  908. static struct file_system_type bpf_fs_type = {
  909. .owner = THIS_MODULE,
  910. .name = "bpf",
  911. .init_fs_context = bpf_init_fs_context,
  912. .parameters = bpf_fs_parameters,
  913. .kill_sb = bpf_kill_super,
  914. .fs_flags = FS_USERNS_MOUNT,
  915. };
  916. static int __init bpf_init(void)
  917. {
  918. int ret;
  919. ret = sysfs_create_mount_point(fs_kobj, "bpf");
  920. if (ret)
  921. return ret;
  922. ret = register_filesystem(&bpf_fs_type);
  923. if (ret)
  924. sysfs_remove_mount_point(fs_kobj, "bpf");
  925. return ret;
  926. }
  927. fs_initcall(bpf_init);