inode.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/module.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/filter.h>
  21. #include <linux/bpf.h>
  22. enum bpf_type {
  23. BPF_TYPE_UNSPEC = 0,
  24. BPF_TYPE_PROG,
  25. BPF_TYPE_MAP,
  26. };
  27. static void *bpf_any_get(void *raw, enum bpf_type type)
  28. {
  29. switch (type) {
  30. case BPF_TYPE_PROG:
  31. raw = bpf_prog_inc(raw);
  32. break;
  33. case BPF_TYPE_MAP:
  34. raw = bpf_map_inc(raw, true);
  35. break;
  36. default:
  37. WARN_ON_ONCE(1);
  38. break;
  39. }
  40. return raw;
  41. }
  42. static void bpf_any_put(void *raw, enum bpf_type type)
  43. {
  44. switch (type) {
  45. case BPF_TYPE_PROG:
  46. bpf_prog_put(raw);
  47. break;
  48. case BPF_TYPE_MAP:
  49. bpf_map_put_with_uref(raw);
  50. break;
  51. default:
  52. WARN_ON_ONCE(1);
  53. break;
  54. }
  55. }
  56. static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
  57. {
  58. void *raw;
  59. *type = BPF_TYPE_MAP;
  60. raw = bpf_map_get_with_uref(ufd);
  61. if (IS_ERR(raw)) {
  62. *type = BPF_TYPE_PROG;
  63. raw = bpf_prog_get(ufd);
  64. }
  65. return raw;
  66. }
  67. static const struct inode_operations bpf_dir_iops;
  68. static const struct inode_operations bpf_prog_iops = { };
  69. static const struct inode_operations bpf_map_iops = { };
  70. static struct inode *bpf_get_inode(struct super_block *sb,
  71. const struct inode *dir,
  72. umode_t mode)
  73. {
  74. struct inode *inode;
  75. switch (mode & S_IFMT) {
  76. case S_IFDIR:
  77. case S_IFREG:
  78. break;
  79. default:
  80. return ERR_PTR(-EINVAL);
  81. }
  82. inode = new_inode(sb);
  83. if (!inode)
  84. return ERR_PTR(-ENOSPC);
  85. inode->i_ino = get_next_ino();
  86. inode->i_atime = CURRENT_TIME;
  87. inode->i_mtime = inode->i_atime;
  88. inode->i_ctime = inode->i_atime;
  89. inode_init_owner(inode, dir, mode);
  90. return inode;
  91. }
  92. static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
  93. {
  94. *type = BPF_TYPE_UNSPEC;
  95. if (inode->i_op == &bpf_prog_iops)
  96. *type = BPF_TYPE_PROG;
  97. else if (inode->i_op == &bpf_map_iops)
  98. *type = BPF_TYPE_MAP;
  99. else
  100. return -EACCES;
  101. return 0;
  102. }
  103. static bool bpf_dname_reserved(const struct dentry *dentry)
  104. {
  105. return strchr(dentry->d_name.name, '.');
  106. }
  107. static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  108. {
  109. struct inode *inode;
  110. if (bpf_dname_reserved(dentry))
  111. return -EPERM;
  112. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
  113. if (IS_ERR(inode))
  114. return PTR_ERR(inode);
  115. inode->i_op = &bpf_dir_iops;
  116. inode->i_fop = &simple_dir_operations;
  117. inc_nlink(inode);
  118. inc_nlink(dir);
  119. d_instantiate(dentry, inode);
  120. dget(dentry);
  121. return 0;
  122. }
  123. static int bpf_mkobj_ops(struct inode *dir, struct dentry *dentry,
  124. umode_t mode, const struct inode_operations *iops)
  125. {
  126. struct inode *inode;
  127. if (bpf_dname_reserved(dentry))
  128. return -EPERM;
  129. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFREG);
  130. if (IS_ERR(inode))
  131. return PTR_ERR(inode);
  132. inode->i_op = iops;
  133. inode->i_private = dentry->d_fsdata;
  134. d_instantiate(dentry, inode);
  135. dget(dentry);
  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 const struct inode_operations bpf_dir_iops = {
  155. .lookup = simple_lookup,
  156. .mknod = bpf_mkobj,
  157. .mkdir = bpf_mkdir,
  158. .rmdir = simple_rmdir,
  159. .unlink = simple_unlink,
  160. };
  161. static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
  162. enum bpf_type type)
  163. {
  164. struct dentry *dentry;
  165. struct inode *dir;
  166. struct path path;
  167. umode_t mode;
  168. dev_t devt;
  169. int ret;
  170. dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
  171. if (IS_ERR(dentry))
  172. return PTR_ERR(dentry);
  173. mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
  174. devt = MKDEV(UNNAMED_MAJOR, type);
  175. ret = security_path_mknod(&path, dentry, mode, devt);
  176. if (ret)
  177. goto out;
  178. dir = d_inode(path.dentry);
  179. if (dir->i_op != &bpf_dir_iops) {
  180. ret = -EPERM;
  181. goto out;
  182. }
  183. dentry->d_fsdata = raw;
  184. ret = vfs_mknod(dir, dentry, mode, devt);
  185. dentry->d_fsdata = NULL;
  186. out:
  187. done_path_create(&path, dentry);
  188. return ret;
  189. }
  190. int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
  191. {
  192. struct filename *pname;
  193. enum bpf_type type;
  194. void *raw;
  195. int ret;
  196. pname = getname(pathname);
  197. if (IS_ERR(pname))
  198. return PTR_ERR(pname);
  199. raw = bpf_fd_probe_obj(ufd, &type);
  200. if (IS_ERR(raw)) {
  201. ret = PTR_ERR(raw);
  202. goto out;
  203. }
  204. ret = bpf_obj_do_pin(pname, raw, type);
  205. if (ret != 0)
  206. bpf_any_put(raw, type);
  207. out:
  208. putname(pname);
  209. return ret;
  210. }
  211. static void *bpf_obj_do_get(const struct filename *pathname,
  212. enum bpf_type *type)
  213. {
  214. struct inode *inode;
  215. struct path path;
  216. void *raw;
  217. int ret;
  218. ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
  219. if (ret)
  220. return ERR_PTR(ret);
  221. inode = d_backing_inode(path.dentry);
  222. ret = inode_permission(inode, MAY_WRITE);
  223. if (ret)
  224. goto out;
  225. ret = bpf_inode_type(inode, type);
  226. if (ret)
  227. goto out;
  228. raw = bpf_any_get(inode->i_private, *type);
  229. if (!IS_ERR(raw))
  230. touch_atime(&path);
  231. path_put(&path);
  232. return raw;
  233. out:
  234. path_put(&path);
  235. return ERR_PTR(ret);
  236. }
  237. int bpf_obj_get_user(const char __user *pathname)
  238. {
  239. enum bpf_type type = BPF_TYPE_UNSPEC;
  240. struct filename *pname;
  241. int ret = -ENOENT;
  242. void *raw;
  243. pname = getname(pathname);
  244. if (IS_ERR(pname))
  245. return PTR_ERR(pname);
  246. raw = bpf_obj_do_get(pname, &type);
  247. if (IS_ERR(raw)) {
  248. ret = PTR_ERR(raw);
  249. goto out;
  250. }
  251. if (type == BPF_TYPE_PROG)
  252. ret = bpf_prog_new_fd(raw);
  253. else if (type == BPF_TYPE_MAP)
  254. ret = bpf_map_new_fd(raw);
  255. else
  256. goto out;
  257. if (ret < 0)
  258. bpf_any_put(raw, type);
  259. out:
  260. putname(pname);
  261. return ret;
  262. }
  263. static void bpf_evict_inode(struct inode *inode)
  264. {
  265. enum bpf_type type;
  266. truncate_inode_pages_final(&inode->i_data);
  267. clear_inode(inode);
  268. if (!bpf_inode_type(inode, &type))
  269. bpf_any_put(inode->i_private, type);
  270. }
  271. static const struct super_operations bpf_super_ops = {
  272. .statfs = simple_statfs,
  273. .drop_inode = generic_delete_inode,
  274. .evict_inode = bpf_evict_inode,
  275. };
  276. static int bpf_fill_super(struct super_block *sb, void *data, int silent)
  277. {
  278. static struct tree_descr bpf_rfiles[] = { { "" } };
  279. struct inode *inode;
  280. int ret;
  281. ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
  282. if (ret)
  283. return ret;
  284. sb->s_op = &bpf_super_ops;
  285. inode = sb->s_root->d_inode;
  286. inode->i_op = &bpf_dir_iops;
  287. inode->i_mode &= ~S_IALLUGO;
  288. inode->i_mode |= S_ISVTX | S_IRWXUGO;
  289. return 0;
  290. }
  291. static struct dentry *bpf_mount(struct file_system_type *type, int flags,
  292. const char *dev_name, void *data)
  293. {
  294. return mount_nodev(type, flags, data, bpf_fill_super);
  295. }
  296. static struct file_system_type bpf_fs_type = {
  297. .owner = THIS_MODULE,
  298. .name = "bpf",
  299. .mount = bpf_mount,
  300. .kill_sb = kill_litter_super,
  301. };
  302. MODULE_ALIAS_FS("bpf");
  303. static int __init bpf_init(void)
  304. {
  305. int ret;
  306. ret = sysfs_create_mount_point(fs_kobj, "bpf");
  307. if (ret)
  308. return ret;
  309. ret = register_filesystem(&bpf_fs_type);
  310. if (ret)
  311. sysfs_remove_mount_point(fs_kobj, "bpf");
  312. return ret;
  313. }
  314. fs_initcall(bpf_init);