inode.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. enum bpf_type {
  24. BPF_TYPE_UNSPEC = 0,
  25. BPF_TYPE_PROG,
  26. BPF_TYPE_MAP,
  27. };
  28. static void *bpf_any_get(void *raw, enum bpf_type type)
  29. {
  30. switch (type) {
  31. case BPF_TYPE_PROG:
  32. raw = bpf_prog_inc(raw);
  33. break;
  34. case BPF_TYPE_MAP:
  35. raw = bpf_map_inc(raw, true);
  36. break;
  37. default:
  38. WARN_ON_ONCE(1);
  39. break;
  40. }
  41. return raw;
  42. }
  43. static void bpf_any_put(void *raw, enum bpf_type type)
  44. {
  45. switch (type) {
  46. case BPF_TYPE_PROG:
  47. bpf_prog_put(raw);
  48. break;
  49. case BPF_TYPE_MAP:
  50. bpf_map_put_with_uref(raw);
  51. break;
  52. default:
  53. WARN_ON_ONCE(1);
  54. break;
  55. }
  56. }
  57. static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
  58. {
  59. void *raw;
  60. *type = BPF_TYPE_MAP;
  61. raw = bpf_map_get_with_uref(ufd);
  62. if (IS_ERR(raw)) {
  63. *type = BPF_TYPE_PROG;
  64. raw = bpf_prog_get(ufd);
  65. }
  66. return raw;
  67. }
  68. static const struct inode_operations bpf_dir_iops;
  69. static const struct inode_operations bpf_prog_iops = { };
  70. static const struct inode_operations bpf_map_iops = { };
  71. static struct inode *bpf_get_inode(struct super_block *sb,
  72. const struct inode *dir,
  73. umode_t mode)
  74. {
  75. struct inode *inode;
  76. switch (mode & S_IFMT) {
  77. case S_IFDIR:
  78. case S_IFREG:
  79. case S_IFLNK:
  80. break;
  81. default:
  82. return ERR_PTR(-EINVAL);
  83. }
  84. inode = new_inode(sb);
  85. if (!inode)
  86. return ERR_PTR(-ENOSPC);
  87. inode->i_ino = get_next_ino();
  88. inode->i_atime = current_time(inode);
  89. inode->i_mtime = inode->i_atime;
  90. inode->i_ctime = inode->i_atime;
  91. inode_init_owner(inode, dir, mode);
  92. return inode;
  93. }
  94. static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
  95. {
  96. *type = BPF_TYPE_UNSPEC;
  97. if (inode->i_op == &bpf_prog_iops)
  98. *type = BPF_TYPE_PROG;
  99. else if (inode->i_op == &bpf_map_iops)
  100. *type = BPF_TYPE_MAP;
  101. else
  102. return -EACCES;
  103. return 0;
  104. }
  105. static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
  106. struct inode *dir)
  107. {
  108. d_instantiate(dentry, inode);
  109. dget(dentry);
  110. dir->i_mtime = current_time(dir);
  111. dir->i_ctime = dir->i_mtime;
  112. }
  113. static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  114. {
  115. struct inode *inode;
  116. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
  117. if (IS_ERR(inode))
  118. return PTR_ERR(inode);
  119. inode->i_op = &bpf_dir_iops;
  120. inode->i_fop = &simple_dir_operations;
  121. inc_nlink(inode);
  122. inc_nlink(dir);
  123. bpf_dentry_finalize(dentry, inode, dir);
  124. return 0;
  125. }
  126. static int bpf_mkobj_ops(struct inode *dir, struct dentry *dentry,
  127. umode_t mode, const struct inode_operations *iops)
  128. {
  129. struct inode *inode;
  130. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFREG);
  131. if (IS_ERR(inode))
  132. return PTR_ERR(inode);
  133. inode->i_op = iops;
  134. inode->i_private = dentry->d_fsdata;
  135. bpf_dentry_finalize(dentry, inode, dir);
  136. return 0;
  137. }
  138. static int bpf_mkobj(struct inode *dir, struct dentry *dentry, umode_t mode,
  139. dev_t devt)
  140. {
  141. enum bpf_type type = MINOR(devt);
  142. if (MAJOR(devt) != UNNAMED_MAJOR || !S_ISREG(mode) ||
  143. dentry->d_fsdata == NULL)
  144. return -EPERM;
  145. switch (type) {
  146. case BPF_TYPE_PROG:
  147. return bpf_mkobj_ops(dir, dentry, mode, &bpf_prog_iops);
  148. case BPF_TYPE_MAP:
  149. return bpf_mkobj_ops(dir, dentry, mode, &bpf_map_iops);
  150. default:
  151. return -EPERM;
  152. }
  153. }
  154. static struct dentry *
  155. bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
  156. {
  157. if (strchr(dentry->d_name.name, '.'))
  158. return ERR_PTR(-EPERM);
  159. return simple_lookup(dir, dentry, flags);
  160. }
  161. static int bpf_symlink(struct inode *dir, struct dentry *dentry,
  162. const char *target)
  163. {
  164. char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
  165. struct inode *inode;
  166. if (!link)
  167. return -ENOMEM;
  168. inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
  169. if (IS_ERR(inode)) {
  170. kfree(link);
  171. return PTR_ERR(inode);
  172. }
  173. inode->i_op = &simple_symlink_inode_operations;
  174. inode->i_link = link;
  175. bpf_dentry_finalize(dentry, inode, dir);
  176. return 0;
  177. }
  178. static const struct inode_operations bpf_dir_iops = {
  179. .lookup = bpf_lookup,
  180. .mknod = bpf_mkobj,
  181. .mkdir = bpf_mkdir,
  182. .symlink = bpf_symlink,
  183. .rmdir = simple_rmdir,
  184. .rename = simple_rename,
  185. .link = simple_link,
  186. .unlink = simple_unlink,
  187. };
  188. static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
  189. enum bpf_type type)
  190. {
  191. struct dentry *dentry;
  192. struct inode *dir;
  193. struct path path;
  194. umode_t mode;
  195. dev_t devt;
  196. int ret;
  197. dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
  198. if (IS_ERR(dentry))
  199. return PTR_ERR(dentry);
  200. mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
  201. devt = MKDEV(UNNAMED_MAJOR, type);
  202. ret = security_path_mknod(&path, dentry, mode, devt);
  203. if (ret)
  204. goto out;
  205. dir = d_inode(path.dentry);
  206. if (dir->i_op != &bpf_dir_iops) {
  207. ret = -EPERM;
  208. goto out;
  209. }
  210. dentry->d_fsdata = raw;
  211. ret = vfs_mknod(dir, dentry, mode, devt);
  212. dentry->d_fsdata = NULL;
  213. out:
  214. done_path_create(&path, dentry);
  215. return ret;
  216. }
  217. int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
  218. {
  219. struct filename *pname;
  220. enum bpf_type type;
  221. void *raw;
  222. int ret;
  223. pname = getname(pathname);
  224. if (IS_ERR(pname))
  225. return PTR_ERR(pname);
  226. raw = bpf_fd_probe_obj(ufd, &type);
  227. if (IS_ERR(raw)) {
  228. ret = PTR_ERR(raw);
  229. goto out;
  230. }
  231. ret = bpf_obj_do_pin(pname, raw, type);
  232. if (ret != 0)
  233. bpf_any_put(raw, type);
  234. out:
  235. putname(pname);
  236. return ret;
  237. }
  238. static void *bpf_obj_do_get(const struct filename *pathname,
  239. enum bpf_type *type)
  240. {
  241. struct inode *inode;
  242. struct path path;
  243. void *raw;
  244. int ret;
  245. ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
  246. if (ret)
  247. return ERR_PTR(ret);
  248. inode = d_backing_inode(path.dentry);
  249. ret = inode_permission(inode, MAY_WRITE);
  250. if (ret)
  251. goto out;
  252. ret = bpf_inode_type(inode, type);
  253. if (ret)
  254. goto out;
  255. raw = bpf_any_get(inode->i_private, *type);
  256. if (!IS_ERR(raw))
  257. touch_atime(&path);
  258. path_put(&path);
  259. return raw;
  260. out:
  261. path_put(&path);
  262. return ERR_PTR(ret);
  263. }
  264. int bpf_obj_get_user(const char __user *pathname)
  265. {
  266. enum bpf_type type = BPF_TYPE_UNSPEC;
  267. struct filename *pname;
  268. int ret = -ENOENT;
  269. void *raw;
  270. pname = getname(pathname);
  271. if (IS_ERR(pname))
  272. return PTR_ERR(pname);
  273. raw = bpf_obj_do_get(pname, &type);
  274. if (IS_ERR(raw)) {
  275. ret = PTR_ERR(raw);
  276. goto out;
  277. }
  278. if (type == BPF_TYPE_PROG)
  279. ret = bpf_prog_new_fd(raw);
  280. else if (type == BPF_TYPE_MAP)
  281. ret = bpf_map_new_fd(raw);
  282. else
  283. goto out;
  284. if (ret < 0)
  285. bpf_any_put(raw, type);
  286. out:
  287. putname(pname);
  288. return ret;
  289. }
  290. static void bpf_evict_inode(struct inode *inode)
  291. {
  292. enum bpf_type type;
  293. truncate_inode_pages_final(&inode->i_data);
  294. clear_inode(inode);
  295. if (S_ISLNK(inode->i_mode))
  296. kfree(inode->i_link);
  297. if (!bpf_inode_type(inode, &type))
  298. bpf_any_put(inode->i_private, type);
  299. }
  300. static const struct super_operations bpf_super_ops = {
  301. .statfs = simple_statfs,
  302. .drop_inode = generic_delete_inode,
  303. .show_options = generic_show_options,
  304. .evict_inode = bpf_evict_inode,
  305. };
  306. enum {
  307. OPT_MODE,
  308. OPT_ERR,
  309. };
  310. static const match_table_t bpf_mount_tokens = {
  311. { OPT_MODE, "mode=%o" },
  312. { OPT_ERR, NULL },
  313. };
  314. struct bpf_mount_opts {
  315. umode_t mode;
  316. };
  317. static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
  318. {
  319. substring_t args[MAX_OPT_ARGS];
  320. int option, token;
  321. char *ptr;
  322. opts->mode = S_IRWXUGO;
  323. while ((ptr = strsep(&data, ",")) != NULL) {
  324. if (!*ptr)
  325. continue;
  326. token = match_token(ptr, bpf_mount_tokens, args);
  327. switch (token) {
  328. case OPT_MODE:
  329. if (match_octal(&args[0], &option))
  330. return -EINVAL;
  331. opts->mode = option & S_IALLUGO;
  332. break;
  333. /* We might like to report bad mount options here, but
  334. * traditionally we've ignored all mount options, so we'd
  335. * better continue to ignore non-existing options for bpf.
  336. */
  337. }
  338. }
  339. return 0;
  340. }
  341. static int bpf_fill_super(struct super_block *sb, void *data, int silent)
  342. {
  343. static struct tree_descr bpf_rfiles[] = { { "" } };
  344. struct bpf_mount_opts opts;
  345. struct inode *inode;
  346. int ret;
  347. save_mount_options(sb, data);
  348. ret = bpf_parse_options(data, &opts);
  349. if (ret)
  350. return ret;
  351. ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
  352. if (ret)
  353. return ret;
  354. sb->s_op = &bpf_super_ops;
  355. inode = sb->s_root->d_inode;
  356. inode->i_op = &bpf_dir_iops;
  357. inode->i_mode &= ~S_IALLUGO;
  358. inode->i_mode |= S_ISVTX | opts.mode;
  359. return 0;
  360. }
  361. static struct dentry *bpf_mount(struct file_system_type *type, int flags,
  362. const char *dev_name, void *data)
  363. {
  364. return mount_nodev(type, flags, data, bpf_fill_super);
  365. }
  366. static struct file_system_type bpf_fs_type = {
  367. .owner = THIS_MODULE,
  368. .name = "bpf",
  369. .mount = bpf_mount,
  370. .kill_sb = kill_litter_super,
  371. };
  372. static int __init bpf_init(void)
  373. {
  374. int ret;
  375. ret = sysfs_create_mount_point(fs_kobj, "bpf");
  376. if (ret)
  377. return ret;
  378. ret = register_filesystem(&bpf_fs_type);
  379. if (ret)
  380. sysfs_remove_mount_point(fs_kobj, "bpf");
  381. return ret;
  382. }
  383. fs_initcall(bpf_init);