inode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. atomic_inc(&((struct bpf_prog *)raw)->aux->refcnt);
  32. break;
  33. case BPF_TYPE_MAP:
  34. 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 int bpf_link(struct dentry *old_dentry, struct inode *dir,
  155. struct dentry *new_dentry)
  156. {
  157. if (bpf_dname_reserved(new_dentry))
  158. return -EPERM;
  159. return simple_link(old_dentry, dir, new_dentry);
  160. }
  161. static int bpf_rename(struct inode *old_dir, struct dentry *old_dentry,
  162. struct inode *new_dir, struct dentry *new_dentry)
  163. {
  164. if (bpf_dname_reserved(new_dentry))
  165. return -EPERM;
  166. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  167. }
  168. static const struct inode_operations bpf_dir_iops = {
  169. .lookup = simple_lookup,
  170. .mknod = bpf_mkobj,
  171. .mkdir = bpf_mkdir,
  172. .rmdir = simple_rmdir,
  173. .rename = bpf_rename,
  174. .link = bpf_link,
  175. .unlink = simple_unlink,
  176. };
  177. static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
  178. enum bpf_type type)
  179. {
  180. struct dentry *dentry;
  181. struct inode *dir;
  182. struct path path;
  183. umode_t mode;
  184. dev_t devt;
  185. int ret;
  186. dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
  187. if (IS_ERR(dentry))
  188. return PTR_ERR(dentry);
  189. mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
  190. devt = MKDEV(UNNAMED_MAJOR, type);
  191. ret = security_path_mknod(&path, dentry, mode, devt);
  192. if (ret)
  193. goto out;
  194. dir = d_inode(path.dentry);
  195. if (dir->i_op != &bpf_dir_iops) {
  196. ret = -EPERM;
  197. goto out;
  198. }
  199. dentry->d_fsdata = raw;
  200. ret = vfs_mknod(dir, dentry, mode, devt);
  201. dentry->d_fsdata = NULL;
  202. out:
  203. done_path_create(&path, dentry);
  204. return ret;
  205. }
  206. int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
  207. {
  208. struct filename *pname;
  209. enum bpf_type type;
  210. void *raw;
  211. int ret;
  212. pname = getname(pathname);
  213. if (IS_ERR(pname))
  214. return PTR_ERR(pname);
  215. raw = bpf_fd_probe_obj(ufd, &type);
  216. if (IS_ERR(raw)) {
  217. ret = PTR_ERR(raw);
  218. goto out;
  219. }
  220. ret = bpf_obj_do_pin(pname, raw, type);
  221. if (ret != 0)
  222. bpf_any_put(raw, type);
  223. out:
  224. putname(pname);
  225. return ret;
  226. }
  227. static void *bpf_obj_do_get(const struct filename *pathname,
  228. enum bpf_type *type)
  229. {
  230. struct inode *inode;
  231. struct path path;
  232. void *raw;
  233. int ret;
  234. ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
  235. if (ret)
  236. return ERR_PTR(ret);
  237. inode = d_backing_inode(path.dentry);
  238. ret = inode_permission(inode, MAY_WRITE);
  239. if (ret)
  240. goto out;
  241. ret = bpf_inode_type(inode, type);
  242. if (ret)
  243. goto out;
  244. raw = bpf_any_get(inode->i_private, *type);
  245. touch_atime(&path);
  246. path_put(&path);
  247. return raw;
  248. out:
  249. path_put(&path);
  250. return ERR_PTR(ret);
  251. }
  252. int bpf_obj_get_user(const char __user *pathname)
  253. {
  254. enum bpf_type type = BPF_TYPE_UNSPEC;
  255. struct filename *pname;
  256. int ret = -ENOENT;
  257. void *raw;
  258. pname = getname(pathname);
  259. if (IS_ERR(pname))
  260. return PTR_ERR(pname);
  261. raw = bpf_obj_do_get(pname, &type);
  262. if (IS_ERR(raw)) {
  263. ret = PTR_ERR(raw);
  264. goto out;
  265. }
  266. if (type == BPF_TYPE_PROG)
  267. ret = bpf_prog_new_fd(raw);
  268. else if (type == BPF_TYPE_MAP)
  269. ret = bpf_map_new_fd(raw);
  270. else
  271. goto out;
  272. if (ret < 0)
  273. bpf_any_put(raw, type);
  274. out:
  275. putname(pname);
  276. return ret;
  277. }
  278. static void bpf_evict_inode(struct inode *inode)
  279. {
  280. enum bpf_type type;
  281. truncate_inode_pages_final(&inode->i_data);
  282. clear_inode(inode);
  283. if (!bpf_inode_type(inode, &type))
  284. bpf_any_put(inode->i_private, type);
  285. }
  286. static const struct super_operations bpf_super_ops = {
  287. .statfs = simple_statfs,
  288. .drop_inode = generic_delete_inode,
  289. .evict_inode = bpf_evict_inode,
  290. };
  291. static int bpf_fill_super(struct super_block *sb, void *data, int silent)
  292. {
  293. static struct tree_descr bpf_rfiles[] = { { "" } };
  294. struct inode *inode;
  295. int ret;
  296. ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
  297. if (ret)
  298. return ret;
  299. sb->s_op = &bpf_super_ops;
  300. inode = sb->s_root->d_inode;
  301. inode->i_op = &bpf_dir_iops;
  302. inode->i_mode &= ~S_IALLUGO;
  303. inode->i_mode |= S_ISVTX | S_IRWXUGO;
  304. return 0;
  305. }
  306. static struct dentry *bpf_mount(struct file_system_type *type, int flags,
  307. const char *dev_name, void *data)
  308. {
  309. return mount_ns(type, flags, current->nsproxy->mnt_ns, bpf_fill_super);
  310. }
  311. static struct file_system_type bpf_fs_type = {
  312. .owner = THIS_MODULE,
  313. .name = "bpf",
  314. .mount = bpf_mount,
  315. .kill_sb = kill_litter_super,
  316. .fs_flags = FS_USERNS_MOUNT,
  317. };
  318. MODULE_ALIAS_FS("bpf");
  319. static int __init bpf_init(void)
  320. {
  321. int ret;
  322. ret = sysfs_create_mount_point(fs_kobj, "bpf");
  323. if (ret)
  324. return ret;
  325. ret = register_filesystem(&bpf_fs_type);
  326. if (ret)
  327. sysfs_remove_mount_point(fs_kobj, "bpf");
  328. return ret;
  329. }
  330. fs_initcall(bpf_init);