anon_inodes.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fs/anon_inodes.c
  4. *
  5. * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
  6. *
  7. * Thanks to Arnd Bergmann for code review and suggestions.
  8. * More changes for Thomas Gleixner suggestions.
  9. *
  10. */
  11. #include <linux/cred.h>
  12. #include <linux/file.h>
  13. #include <linux/poll.h>
  14. #include <linux/sched.h>
  15. #include <linux/init.h>
  16. #include <linux/fs.h>
  17. #include <linux/mount.h>
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/magic.h>
  21. #include <linux/anon_inodes.h>
  22. #include <linux/pseudo_fs.h>
  23. #include <linux/uaccess.h>
  24. static struct vfsmount *anon_inode_mnt __ro_after_init;
  25. static struct inode *anon_inode_inode __ro_after_init;
  26. /*
  27. * anon_inodefs_dname() is called from d_path().
  28. */
  29. static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen)
  30. {
  31. return dynamic_dname(buffer, buflen, "anon_inode:%s",
  32. dentry->d_name.name);
  33. }
  34. static const struct dentry_operations anon_inodefs_dentry_operations = {
  35. .d_dname = anon_inodefs_dname,
  36. };
  37. static int anon_inodefs_init_fs_context(struct fs_context *fc)
  38. {
  39. struct pseudo_fs_context *ctx = init_pseudo(fc, ANON_INODE_FS_MAGIC);
  40. if (!ctx)
  41. return -ENOMEM;
  42. ctx->dops = &anon_inodefs_dentry_operations;
  43. return 0;
  44. }
  45. static struct file_system_type anon_inode_fs_type = {
  46. .name = "anon_inodefs",
  47. .init_fs_context = anon_inodefs_init_fs_context,
  48. .kill_sb = kill_anon_super,
  49. };
  50. /**
  51. * anon_inode_make_secure_inode - allocate an anonymous inode with security context
  52. * @sb: [in] Superblock to allocate from
  53. * @name: [in] Name of the class of the newfile (e.g., "secretmem")
  54. * @context_inode:
  55. * [in] Optional parent inode for security inheritance
  56. *
  57. * The function ensures proper security initialization through the LSM hook
  58. * security_inode_init_security_anon().
  59. *
  60. * Return: Pointer to new inode on success, ERR_PTR on failure.
  61. */
  62. struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
  63. const struct inode *context_inode)
  64. {
  65. struct inode *inode;
  66. int error;
  67. inode = alloc_anon_inode(sb);
  68. if (IS_ERR(inode))
  69. return inode;
  70. inode->i_flags &= ~S_PRIVATE;
  71. error = security_inode_init_security_anon(inode, &QSTR(name),
  72. context_inode);
  73. if (error) {
  74. iput(inode);
  75. return ERR_PTR(error);
  76. }
  77. return inode;
  78. }
  79. EXPORT_SYMBOL_GPL_FOR_MODULES(anon_inode_make_secure_inode, "kvm");
  80. static struct file *__anon_inode_getfile(const char *name,
  81. const struct file_operations *fops,
  82. void *priv, int flags,
  83. const struct inode *context_inode,
  84. bool make_inode)
  85. {
  86. struct inode *inode;
  87. struct file *file;
  88. if (fops->owner && !try_module_get(fops->owner))
  89. return ERR_PTR(-ENOENT);
  90. if (make_inode) {
  91. inode = anon_inode_make_secure_inode(anon_inode_mnt->mnt_sb,
  92. name, context_inode);
  93. if (IS_ERR(inode)) {
  94. file = ERR_CAST(inode);
  95. goto err;
  96. }
  97. } else {
  98. inode = anon_inode_inode;
  99. if (IS_ERR(inode)) {
  100. file = ERR_PTR(-ENODEV);
  101. goto err;
  102. }
  103. /*
  104. * We know the anon_inode inode count is always
  105. * greater than zero, so ihold() is safe.
  106. */
  107. ihold(inode);
  108. }
  109. file = alloc_file_pseudo(inode, anon_inode_mnt, name,
  110. flags & (O_ACCMODE | O_NONBLOCK), fops);
  111. if (IS_ERR(file))
  112. goto err_iput;
  113. file->f_mapping = inode->i_mapping;
  114. file->private_data = priv;
  115. return file;
  116. err_iput:
  117. iput(inode);
  118. err:
  119. module_put(fops->owner);
  120. return file;
  121. }
  122. /**
  123. * anon_inode_getfile - creates a new file instance by hooking it up to an
  124. * anonymous inode, and a dentry that describe the "class"
  125. * of the file
  126. *
  127. * @name: [in] name of the "class" of the new file
  128. * @fops: [in] file operations for the new file
  129. * @priv: [in] private data for the new file (will be file's private_data)
  130. * @flags: [in] flags
  131. *
  132. * Creates a new file by hooking it on a single inode. This is useful for files
  133. * that do not need to have a full-fledged inode in order to operate correctly.
  134. * All the files created with anon_inode_getfile() will share a single inode,
  135. * hence saving memory and avoiding code duplication for the file/inode/dentry
  136. * setup. Returns the newly created file* or an error pointer.
  137. */
  138. struct file *anon_inode_getfile(const char *name,
  139. const struct file_operations *fops,
  140. void *priv, int flags)
  141. {
  142. return __anon_inode_getfile(name, fops, priv, flags, NULL, false);
  143. }
  144. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  145. /**
  146. * anon_inode_getfile_fmode - creates a new file instance by hooking it up to an
  147. * anonymous inode, and a dentry that describe the "class"
  148. * of the file
  149. *
  150. * @name: [in] name of the "class" of the new file
  151. * @fops: [in] file operations for the new file
  152. * @priv: [in] private data for the new file (will be file's private_data)
  153. * @flags: [in] flags
  154. * @f_mode: [in] fmode
  155. *
  156. * Creates a new file by hooking it on a single inode. This is useful for files
  157. * that do not need to have a full-fledged inode in order to operate correctly.
  158. * All the files created with anon_inode_getfile() will share a single inode,
  159. * hence saving memory and avoiding code duplication for the file/inode/dentry
  160. * setup. Allows setting the fmode. Returns the newly created file* or an error
  161. * pointer.
  162. */
  163. struct file *anon_inode_getfile_fmode(const char *name,
  164. const struct file_operations *fops,
  165. void *priv, int flags, fmode_t f_mode)
  166. {
  167. struct file *file;
  168. file = __anon_inode_getfile(name, fops, priv, flags, NULL, false);
  169. if (!IS_ERR(file))
  170. file->f_mode |= f_mode;
  171. return file;
  172. }
  173. EXPORT_SYMBOL_GPL(anon_inode_getfile_fmode);
  174. /**
  175. * anon_inode_create_getfile - Like anon_inode_getfile(), but creates a new
  176. * !S_PRIVATE anon inode rather than reuse the
  177. * singleton anon inode and calls the
  178. * inode_init_security_anon() LSM hook.
  179. *
  180. * @name: [in] name of the "class" of the new file
  181. * @fops: [in] file operations for the new file
  182. * @priv: [in] private data for the new file (will be file's private_data)
  183. * @flags: [in] flags
  184. * @context_inode:
  185. * [in] the logical relationship with the new inode (optional)
  186. *
  187. * Create a new anonymous inode and file pair. This can be done for two
  188. * reasons:
  189. *
  190. * - for the inode to have its own security context, so that LSMs can enforce
  191. * policy on the inode's creation;
  192. *
  193. * - if the caller needs a unique inode, for example in order to customize
  194. * the size returned by fstat()
  195. *
  196. * The LSM may use @context_inode in inode_init_security_anon(), but a
  197. * reference to it is not held.
  198. *
  199. * Returns the newly created file* or an error pointer.
  200. */
  201. struct file *anon_inode_create_getfile(const char *name,
  202. const struct file_operations *fops,
  203. void *priv, int flags,
  204. const struct inode *context_inode)
  205. {
  206. return __anon_inode_getfile(name, fops, priv, flags,
  207. context_inode, true);
  208. }
  209. EXPORT_SYMBOL_GPL(anon_inode_create_getfile);
  210. static int __anon_inode_getfd(const char *name,
  211. const struct file_operations *fops,
  212. void *priv, int flags,
  213. const struct inode *context_inode,
  214. bool make_inode)
  215. {
  216. int error, fd;
  217. struct file *file;
  218. error = get_unused_fd_flags(flags);
  219. if (error < 0)
  220. return error;
  221. fd = error;
  222. file = __anon_inode_getfile(name, fops, priv, flags, context_inode,
  223. make_inode);
  224. if (IS_ERR(file)) {
  225. error = PTR_ERR(file);
  226. goto err_put_unused_fd;
  227. }
  228. fd_install(fd, file);
  229. return fd;
  230. err_put_unused_fd:
  231. put_unused_fd(fd);
  232. return error;
  233. }
  234. /**
  235. * anon_inode_getfd - creates a new file instance by hooking it up to
  236. * an anonymous inode and a dentry that describe
  237. * the "class" of the file
  238. *
  239. * @name: [in] name of the "class" of the new file
  240. * @fops: [in] file operations for the new file
  241. * @priv: [in] private data for the new file (will be file's private_data)
  242. * @flags: [in] flags
  243. *
  244. * Creates a new file by hooking it on a single inode. This is
  245. * useful for files that do not need to have a full-fledged inode in
  246. * order to operate correctly. All the files created with
  247. * anon_inode_getfd() will use the same singleton inode, reducing
  248. * memory use and avoiding code duplication for the file/inode/dentry
  249. * setup. Returns a newly created file descriptor or an error code.
  250. */
  251. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  252. void *priv, int flags)
  253. {
  254. return __anon_inode_getfd(name, fops, priv, flags, NULL, false);
  255. }
  256. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  257. /**
  258. * anon_inode_create_getfd - Like anon_inode_getfd(), but creates a new
  259. * !S_PRIVATE anon inode rather than reuse the singleton anon inode, and calls
  260. * the inode_init_security_anon() LSM hook.
  261. *
  262. * @name: [in] name of the "class" of the new file
  263. * @fops: [in] file operations for the new file
  264. * @priv: [in] private data for the new file (will be file's private_data)
  265. * @flags: [in] flags
  266. * @context_inode:
  267. * [in] the logical relationship with the new inode (optional)
  268. *
  269. * Create a new anonymous inode and file pair. This can be done for two
  270. * reasons:
  271. *
  272. * - for the inode to have its own security context, so that LSMs can enforce
  273. * policy on the inode's creation;
  274. *
  275. * - if the caller needs a unique inode, for example in order to customize
  276. * the size returned by fstat()
  277. *
  278. * The LSM may use @context_inode in inode_init_security_anon(), but a
  279. * reference to it is not held.
  280. *
  281. * Returns a newly created file descriptor or an error code.
  282. */
  283. int anon_inode_create_getfd(const char *name, const struct file_operations *fops,
  284. void *priv, int flags,
  285. const struct inode *context_inode)
  286. {
  287. return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
  288. }
  289. static int __init anon_inode_init(void)
  290. {
  291. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  292. if (IS_ERR(anon_inode_mnt))
  293. panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
  294. anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
  295. if (IS_ERR(anon_inode_inode))
  296. panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
  297. return 0;
  298. }
  299. fs_initcall(anon_inode_init);