inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Minimal file system backend for holding eBPF maps and programs,
  3. * used by bpf(2) object pinning.
  4. *
  5. * Authors:
  6. *
  7. * Daniel Borkmann <daniel@iogearbox.net>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/magic.h>
  15. #include <linux/major.h>
  16. #include <linux/mount.h>
  17. #include <linux/namei.h>
  18. #include <linux/fs.h>
  19. #include <linux/kdev_t.h>
  20. #include <linux/parser.h>
  21. #include <linux/filter.h>
  22. #include <linux/bpf.h>
  23. #include <linux/bpf_trace.h>
  24. enum bpf_type {
  25. BPF_TYPE_UNSPEC = 0,
  26. BPF_TYPE_PROG,
  27. BPF_TYPE_MAP,
  28. };
  29. static void *bpf_any_get(void *raw, enum bpf_type type)
  30. {
  31. switch (type) {
  32. case BPF_TYPE_PROG:
  33. raw = bpf_prog_inc(raw);
  34. break;
  35. case BPF_TYPE_MAP:
  36. raw = bpf_map_inc(raw, true);
  37. break;
  38. default:
  39. WARN_ON_ONCE(1);
  40. break;
  41. }
  42. return raw;
  43. }
  44. static void bpf_any_put(void *raw, enum bpf_type type)
  45. {
  46. switch (type) {
  47. case BPF_TYPE_PROG:
  48. bpf_prog_put(raw);
  49. break;
  50. case BPF_TYPE_MAP:
  51. bpf_map_put_with_uref(raw);
  52. break;
  53. default:
  54. WARN_ON_ONCE(1);
  55. break;
  56. }
  57. }
  58. static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
  59. {
  60. void *raw;
  61. *type = BPF_TYPE_MAP;
  62. raw = bpf_map_get_with_uref(ufd);
  63. if (IS_ERR(raw)) {
  64. *type = BPF_TYPE_PROG;
  65. raw = bpf_prog_get(ufd);
  66. }
  67. return raw;
  68. }
  69. static const struct inode_operations bpf_dir_iops;
  70. static const struct inode_operations bpf_prog_iops = { };
  71. static const struct inode_operations bpf_map_iops = { };
  72. static struct inode *bpf_get_inode(struct super_block *sb,
  73. const struct inode *dir,
  74. umode_t mode)
  75. {
  76. struct inode *inode;
  77. switch (mode & S_IFMT) {
  78. case S_IFDIR:
  79. case S_IFREG:
  80. case S_IFLNK:
  81. break;
  82. default:
  83. return ERR_PTR(-EINVAL);
  84. }
  85. inode = new_inode(sb);
  86. if (!inode)
  87. return ERR_PTR(-ENOSPC);
  88. inode->i_ino = get_next_ino();
  89. inode->i_atime = current_time(inode);
  90. inode->i_mtime = inode->i_atime;
  91. inode->i_ctime = inode->i_atime;
  92. inode_init_owner(inode, dir, mode);
  93. return inode;
  94. }
  95. static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
  96. {
  97. *type = BPF_TYPE_UNSPEC;
  98. if (inode->i_op == &bpf_prog_iops)
  99. *type = BPF_TYPE_PROG;
  100. else if (inode->i_op == &bpf_map_iops)
  101. *type = BPF_TYPE_MAP;
  102. else
  103. return -EACCES;
  104. return 0;
  105. }
  106. static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
  107. struct inode *dir)
  108. {
  109. d_instantiate(dentry, inode);
  110. dget(dentry);
  111. dir->i_mtime = current_time(dir);
  112. dir->i_ctime = dir->i_mtime;
  113. }
  114. static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  115. {
  116. struct inode *inode;
  117. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
  118. if (IS_ERR(inode))
  119. return PTR_ERR(inode);
  120. inode->i_op = &bpf_dir_iops;
  121. inode->i_fop = &simple_dir_operations;
  122. inc_nlink(inode);
  123. inc_nlink(dir);
  124. bpf_dentry_finalize(dentry, inode, dir);
  125. return 0;
  126. }
  127. static int bpf_mkobj_ops(struct inode *dir, struct dentry *dentry,
  128. umode_t mode, const struct inode_operations *iops)
  129. {
  130. struct inode *inode;
  131. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFREG);
  132. if (IS_ERR(inode))
  133. return PTR_ERR(inode);
  134. inode->i_op = iops;
  135. inode->i_private = dentry->d_fsdata;
  136. bpf_dentry_finalize(dentry, inode, dir);
  137. return 0;
  138. }
  139. static int bpf_mkobj(struct inode *dir, struct dentry *dentry, umode_t mode,
  140. dev_t devt)
  141. {
  142. enum bpf_type type = MINOR(devt);
  143. if (MAJOR(devt) != UNNAMED_MAJOR || !S_ISREG(mode) ||
  144. dentry->d_fsdata == NULL)
  145. return -EPERM;
  146. switch (type) {
  147. case BPF_TYPE_PROG:
  148. return bpf_mkobj_ops(dir, dentry, mode, &bpf_prog_iops);
  149. case BPF_TYPE_MAP:
  150. return bpf_mkobj_ops(dir, dentry, mode, &bpf_map_iops);
  151. default:
  152. return -EPERM;
  153. }
  154. }
  155. static struct dentry *
  156. bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
  157. {
  158. if (strchr(dentry->d_name.name, '.'))
  159. return ERR_PTR(-EPERM);
  160. return simple_lookup(dir, dentry, flags);
  161. }
  162. static int bpf_symlink(struct inode *dir, struct dentry *dentry,
  163. const char *target)
  164. {
  165. char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
  166. struct inode *inode;
  167. if (!link)
  168. return -ENOMEM;
  169. inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
  170. if (IS_ERR(inode)) {
  171. kfree(link);
  172. return PTR_ERR(inode);
  173. }
  174. inode->i_op = &simple_symlink_inode_operations;
  175. inode->i_link = link;
  176. bpf_dentry_finalize(dentry, inode, dir);
  177. return 0;
  178. }
  179. static const struct inode_operations bpf_dir_iops = {
  180. .lookup = bpf_lookup,
  181. .mknod = bpf_mkobj,
  182. .mkdir = bpf_mkdir,
  183. .symlink = bpf_symlink,
  184. .rmdir = simple_rmdir,
  185. .rename = simple_rename,
  186. .link = simple_link,
  187. .unlink = simple_unlink,
  188. };
  189. static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
  190. enum bpf_type type)
  191. {
  192. struct dentry *dentry;
  193. struct inode *dir;
  194. struct path path;
  195. umode_t mode;
  196. dev_t devt;
  197. int ret;
  198. dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
  199. if (IS_ERR(dentry))
  200. return PTR_ERR(dentry);
  201. mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
  202. devt = MKDEV(UNNAMED_MAJOR, type);
  203. ret = security_path_mknod(&path, dentry, mode, devt);
  204. if (ret)
  205. goto out;
  206. dir = d_inode(path.dentry);
  207. if (dir->i_op != &bpf_dir_iops) {
  208. ret = -EPERM;
  209. goto out;
  210. }
  211. dentry->d_fsdata = raw;
  212. ret = vfs_mknod(dir, dentry, mode, devt);
  213. dentry->d_fsdata = NULL;
  214. out:
  215. done_path_create(&path, dentry);
  216. return ret;
  217. }
  218. int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
  219. {
  220. struct filename *pname;
  221. enum bpf_type type;
  222. void *raw;
  223. int ret;
  224. pname = getname(pathname);
  225. if (IS_ERR(pname))
  226. return PTR_ERR(pname);
  227. raw = bpf_fd_probe_obj(ufd, &type);
  228. if (IS_ERR(raw)) {
  229. ret = PTR_ERR(raw);
  230. goto out;
  231. }
  232. ret = bpf_obj_do_pin(pname, raw, type);
  233. if (ret != 0)
  234. bpf_any_put(raw, type);
  235. if ((trace_bpf_obj_pin_prog_enabled() ||
  236. trace_bpf_obj_pin_map_enabled()) && !ret) {
  237. if (type == BPF_TYPE_PROG)
  238. trace_bpf_obj_pin_prog(raw, ufd, pname);
  239. if (type == BPF_TYPE_MAP)
  240. trace_bpf_obj_pin_map(raw, ufd, pname);
  241. }
  242. out:
  243. putname(pname);
  244. return ret;
  245. }
  246. static void *bpf_obj_do_get(const struct filename *pathname,
  247. enum bpf_type *type, int flags)
  248. {
  249. struct inode *inode;
  250. struct path path;
  251. void *raw;
  252. int ret;
  253. ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
  254. if (ret)
  255. return ERR_PTR(ret);
  256. inode = d_backing_inode(path.dentry);
  257. ret = inode_permission(inode, ACC_MODE(flags));
  258. if (ret)
  259. goto out;
  260. ret = bpf_inode_type(inode, type);
  261. if (ret)
  262. goto out;
  263. raw = bpf_any_get(inode->i_private, *type);
  264. if (!IS_ERR(raw))
  265. touch_atime(&path);
  266. path_put(&path);
  267. return raw;
  268. out:
  269. path_put(&path);
  270. return ERR_PTR(ret);
  271. }
  272. int bpf_obj_get_user(const char __user *pathname, int flags)
  273. {
  274. enum bpf_type type = BPF_TYPE_UNSPEC;
  275. struct filename *pname;
  276. int ret = -ENOENT;
  277. int f_flags;
  278. void *raw;
  279. f_flags = bpf_get_file_flag(flags);
  280. if (f_flags < 0)
  281. return f_flags;
  282. pname = getname(pathname);
  283. if (IS_ERR(pname))
  284. return PTR_ERR(pname);
  285. raw = bpf_obj_do_get(pname, &type, f_flags);
  286. if (IS_ERR(raw)) {
  287. ret = PTR_ERR(raw);
  288. goto out;
  289. }
  290. if (type == BPF_TYPE_PROG)
  291. ret = bpf_prog_new_fd(raw);
  292. else if (type == BPF_TYPE_MAP)
  293. ret = bpf_map_new_fd(raw, f_flags);
  294. else
  295. goto out;
  296. if (ret < 0) {
  297. bpf_any_put(raw, type);
  298. } else if (trace_bpf_obj_get_prog_enabled() ||
  299. trace_bpf_obj_get_map_enabled()) {
  300. if (type == BPF_TYPE_PROG)
  301. trace_bpf_obj_get_prog(raw, ret, pname);
  302. if (type == BPF_TYPE_MAP)
  303. trace_bpf_obj_get_map(raw, ret, pname);
  304. }
  305. out:
  306. putname(pname);
  307. return ret;
  308. }
  309. static struct bpf_prog *__get_prog_inode(struct inode *inode, enum bpf_prog_type type)
  310. {
  311. struct bpf_prog *prog;
  312. int ret = inode_permission(inode, MAY_READ);
  313. if (ret)
  314. return ERR_PTR(ret);
  315. if (inode->i_op == &bpf_map_iops)
  316. return ERR_PTR(-EINVAL);
  317. if (inode->i_op != &bpf_prog_iops)
  318. return ERR_PTR(-EACCES);
  319. prog = inode->i_private;
  320. ret = security_bpf_prog(prog);
  321. if (ret < 0)
  322. return ERR_PTR(ret);
  323. return bpf_prog_inc(prog);
  324. }
  325. struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type)
  326. {
  327. struct bpf_prog *prog;
  328. struct path path;
  329. int ret = kern_path(name, LOOKUP_FOLLOW, &path);
  330. if (ret)
  331. return ERR_PTR(ret);
  332. prog = __get_prog_inode(d_backing_inode(path.dentry), type);
  333. if (!IS_ERR(prog))
  334. touch_atime(&path);
  335. path_put(&path);
  336. return prog;
  337. }
  338. EXPORT_SYMBOL(bpf_prog_get_type_path);
  339. /*
  340. * Display the mount options in /proc/mounts.
  341. */
  342. static int bpf_show_options(struct seq_file *m, struct dentry *root)
  343. {
  344. umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
  345. if (mode != S_IRWXUGO)
  346. seq_printf(m, ",mode=%o", mode);
  347. return 0;
  348. }
  349. static void bpf_destroy_inode_deferred(struct rcu_head *head)
  350. {
  351. struct inode *inode = container_of(head, struct inode, i_rcu);
  352. enum bpf_type type;
  353. if (S_ISLNK(inode->i_mode))
  354. kfree(inode->i_link);
  355. if (!bpf_inode_type(inode, &type))
  356. bpf_any_put(inode->i_private, type);
  357. free_inode_nonrcu(inode);
  358. }
  359. static void bpf_destroy_inode(struct inode *inode)
  360. {
  361. call_rcu(&inode->i_rcu, bpf_destroy_inode_deferred);
  362. }
  363. static const struct super_operations bpf_super_ops = {
  364. .statfs = simple_statfs,
  365. .drop_inode = generic_delete_inode,
  366. .show_options = bpf_show_options,
  367. .destroy_inode = bpf_destroy_inode,
  368. };
  369. enum {
  370. OPT_MODE,
  371. OPT_ERR,
  372. };
  373. static const match_table_t bpf_mount_tokens = {
  374. { OPT_MODE, "mode=%o" },
  375. { OPT_ERR, NULL },
  376. };
  377. struct bpf_mount_opts {
  378. umode_t mode;
  379. };
  380. static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
  381. {
  382. substring_t args[MAX_OPT_ARGS];
  383. int option, token;
  384. char *ptr;
  385. opts->mode = S_IRWXUGO;
  386. while ((ptr = strsep(&data, ",")) != NULL) {
  387. if (!*ptr)
  388. continue;
  389. token = match_token(ptr, bpf_mount_tokens, args);
  390. switch (token) {
  391. case OPT_MODE:
  392. if (match_octal(&args[0], &option))
  393. return -EINVAL;
  394. opts->mode = option & S_IALLUGO;
  395. break;
  396. /* We might like to report bad mount options here, but
  397. * traditionally we've ignored all mount options, so we'd
  398. * better continue to ignore non-existing options for bpf.
  399. */
  400. }
  401. }
  402. return 0;
  403. }
  404. static int bpf_fill_super(struct super_block *sb, void *data, int silent)
  405. {
  406. static const struct tree_descr bpf_rfiles[] = { { "" } };
  407. struct bpf_mount_opts opts;
  408. struct inode *inode;
  409. int ret;
  410. ret = bpf_parse_options(data, &opts);
  411. if (ret)
  412. return ret;
  413. ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
  414. if (ret)
  415. return ret;
  416. sb->s_op = &bpf_super_ops;
  417. inode = sb->s_root->d_inode;
  418. inode->i_op = &bpf_dir_iops;
  419. inode->i_mode &= ~S_IALLUGO;
  420. inode->i_mode |= S_ISVTX | opts.mode;
  421. return 0;
  422. }
  423. static struct dentry *bpf_mount(struct file_system_type *type, int flags,
  424. const char *dev_name, void *data)
  425. {
  426. return mount_nodev(type, flags, data, bpf_fill_super);
  427. }
  428. static struct file_system_type bpf_fs_type = {
  429. .owner = THIS_MODULE,
  430. .name = "bpf",
  431. .mount = bpf_mount,
  432. .kill_sb = kill_litter_super,
  433. };
  434. static int __init bpf_init(void)
  435. {
  436. int ret;
  437. ret = sysfs_create_mount_point(fs_kobj, "bpf");
  438. if (ret)
  439. return ret;
  440. ret = register_filesystem(&bpf_fs_type);
  441. if (ret)
  442. sysfs_remove_mount_point(fs_kobj, "bpf");
  443. return ret;
  444. }
  445. fs_initcall(bpf_init);