inode.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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/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(inode);
  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 int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  104. {
  105. struct inode *inode;
  106. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
  107. if (IS_ERR(inode))
  108. return PTR_ERR(inode);
  109. inode->i_op = &bpf_dir_iops;
  110. inode->i_fop = &simple_dir_operations;
  111. inc_nlink(inode);
  112. inc_nlink(dir);
  113. d_instantiate(dentry, inode);
  114. dget(dentry);
  115. return 0;
  116. }
  117. static int bpf_mkobj_ops(struct inode *dir, struct dentry *dentry,
  118. umode_t mode, const struct inode_operations *iops)
  119. {
  120. struct inode *inode;
  121. inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFREG);
  122. if (IS_ERR(inode))
  123. return PTR_ERR(inode);
  124. inode->i_op = iops;
  125. inode->i_private = dentry->d_fsdata;
  126. d_instantiate(dentry, inode);
  127. dget(dentry);
  128. return 0;
  129. }
  130. static int bpf_mkobj(struct inode *dir, struct dentry *dentry, umode_t mode,
  131. dev_t devt)
  132. {
  133. enum bpf_type type = MINOR(devt);
  134. if (MAJOR(devt) != UNNAMED_MAJOR || !S_ISREG(mode) ||
  135. dentry->d_fsdata == NULL)
  136. return -EPERM;
  137. switch (type) {
  138. case BPF_TYPE_PROG:
  139. return bpf_mkobj_ops(dir, dentry, mode, &bpf_prog_iops);
  140. case BPF_TYPE_MAP:
  141. return bpf_mkobj_ops(dir, dentry, mode, &bpf_map_iops);
  142. default:
  143. return -EPERM;
  144. }
  145. }
  146. static struct dentry *
  147. bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
  148. {
  149. if (strchr(dentry->d_name.name, '.'))
  150. return ERR_PTR(-EPERM);
  151. return simple_lookup(dir, dentry, flags);
  152. }
  153. static const struct inode_operations bpf_dir_iops = {
  154. .lookup = bpf_lookup,
  155. .mknod = bpf_mkobj,
  156. .mkdir = bpf_mkdir,
  157. .rmdir = simple_rmdir,
  158. .rename = simple_rename,
  159. .link = simple_link,
  160. .unlink = simple_unlink,
  161. };
  162. static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
  163. enum bpf_type type)
  164. {
  165. struct dentry *dentry;
  166. struct inode *dir;
  167. struct path path;
  168. umode_t mode;
  169. dev_t devt;
  170. int ret;
  171. dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
  172. if (IS_ERR(dentry))
  173. return PTR_ERR(dentry);
  174. mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
  175. devt = MKDEV(UNNAMED_MAJOR, type);
  176. ret = security_path_mknod(&path, dentry, mode, devt);
  177. if (ret)
  178. goto out;
  179. dir = d_inode(path.dentry);
  180. if (dir->i_op != &bpf_dir_iops) {
  181. ret = -EPERM;
  182. goto out;
  183. }
  184. dentry->d_fsdata = raw;
  185. ret = vfs_mknod(dir, dentry, mode, devt);
  186. dentry->d_fsdata = NULL;
  187. out:
  188. done_path_create(&path, dentry);
  189. return ret;
  190. }
  191. int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
  192. {
  193. struct filename *pname;
  194. enum bpf_type type;
  195. void *raw;
  196. int ret;
  197. pname = getname(pathname);
  198. if (IS_ERR(pname))
  199. return PTR_ERR(pname);
  200. raw = bpf_fd_probe_obj(ufd, &type);
  201. if (IS_ERR(raw)) {
  202. ret = PTR_ERR(raw);
  203. goto out;
  204. }
  205. ret = bpf_obj_do_pin(pname, raw, type);
  206. if (ret != 0)
  207. bpf_any_put(raw, type);
  208. out:
  209. putname(pname);
  210. return ret;
  211. }
  212. static void *bpf_obj_do_get(const struct filename *pathname,
  213. enum bpf_type *type)
  214. {
  215. struct inode *inode;
  216. struct path path;
  217. void *raw;
  218. int ret;
  219. ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
  220. if (ret)
  221. return ERR_PTR(ret);
  222. inode = d_backing_inode(path.dentry);
  223. ret = inode_permission(inode, MAY_WRITE);
  224. if (ret)
  225. goto out;
  226. ret = bpf_inode_type(inode, type);
  227. if (ret)
  228. goto out;
  229. raw = bpf_any_get(inode->i_private, *type);
  230. if (!IS_ERR(raw))
  231. touch_atime(&path);
  232. path_put(&path);
  233. return raw;
  234. out:
  235. path_put(&path);
  236. return ERR_PTR(ret);
  237. }
  238. int bpf_obj_get_user(const char __user *pathname)
  239. {
  240. enum bpf_type type = BPF_TYPE_UNSPEC;
  241. struct filename *pname;
  242. int ret = -ENOENT;
  243. void *raw;
  244. pname = getname(pathname);
  245. if (IS_ERR(pname))
  246. return PTR_ERR(pname);
  247. raw = bpf_obj_do_get(pname, &type);
  248. if (IS_ERR(raw)) {
  249. ret = PTR_ERR(raw);
  250. goto out;
  251. }
  252. if (type == BPF_TYPE_PROG)
  253. ret = bpf_prog_new_fd(raw);
  254. else if (type == BPF_TYPE_MAP)
  255. ret = bpf_map_new_fd(raw);
  256. else
  257. goto out;
  258. if (ret < 0)
  259. bpf_any_put(raw, type);
  260. out:
  261. putname(pname);
  262. return ret;
  263. }
  264. static void bpf_evict_inode(struct inode *inode)
  265. {
  266. enum bpf_type type;
  267. truncate_inode_pages_final(&inode->i_data);
  268. clear_inode(inode);
  269. if (!bpf_inode_type(inode, &type))
  270. bpf_any_put(inode->i_private, type);
  271. }
  272. static const struct super_operations bpf_super_ops = {
  273. .statfs = simple_statfs,
  274. .drop_inode = generic_delete_inode,
  275. .evict_inode = bpf_evict_inode,
  276. };
  277. static int bpf_fill_super(struct super_block *sb, void *data, int silent)
  278. {
  279. static struct tree_descr bpf_rfiles[] = { { "" } };
  280. struct inode *inode;
  281. int ret;
  282. ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
  283. if (ret)
  284. return ret;
  285. sb->s_op = &bpf_super_ops;
  286. inode = sb->s_root->d_inode;
  287. inode->i_op = &bpf_dir_iops;
  288. inode->i_mode &= ~S_IALLUGO;
  289. inode->i_mode |= S_ISVTX | S_IRWXUGO;
  290. return 0;
  291. }
  292. static struct dentry *bpf_mount(struct file_system_type *type, int flags,
  293. const char *dev_name, void *data)
  294. {
  295. return mount_nodev(type, flags, data, bpf_fill_super);
  296. }
  297. static struct file_system_type bpf_fs_type = {
  298. .owner = THIS_MODULE,
  299. .name = "bpf",
  300. .mount = bpf_mount,
  301. .kill_sb = kill_litter_super,
  302. };
  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);