anon_inodes.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fs/anon_inodes.c
  4. *
  5. * Copyright (C) 2007 Davide Libenzi <[email protected]>
  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 __read_mostly;
  25. static struct inode *anon_inode_inode;
  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. static struct inode *anon_inode_make_secure_inode(
  51. const char *name,
  52. const struct inode *context_inode)
  53. {
  54. struct inode *inode;
  55. const struct qstr qname = QSTR_INIT(name, strlen(name));
  56. int error;
  57. inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
  58. if (IS_ERR(inode))
  59. return inode;
  60. inode->i_flags &= ~S_PRIVATE;
  61. error = security_inode_init_security_anon(inode, &qname, context_inode);
  62. if (error) {
  63. iput(inode);
  64. return ERR_PTR(error);
  65. }
  66. return inode;
  67. }
  68. static struct file *__anon_inode_getfile(const char *name,
  69. const struct file_operations *fops,
  70. void *priv, int flags,
  71. const struct inode *context_inode,
  72. bool secure)
  73. {
  74. struct inode *inode;
  75. struct file *file;
  76. if (fops->owner && !try_module_get(fops->owner))
  77. return ERR_PTR(-ENOENT);
  78. if (secure) {
  79. inode = anon_inode_make_secure_inode(name, context_inode);
  80. if (IS_ERR(inode)) {
  81. file = ERR_CAST(inode);
  82. goto err;
  83. }
  84. } else {
  85. inode = anon_inode_inode;
  86. if (IS_ERR(inode)) {
  87. file = ERR_PTR(-ENODEV);
  88. goto err;
  89. }
  90. /*
  91. * We know the anon_inode inode count is always
  92. * greater than zero, so ihold() is safe.
  93. */
  94. ihold(inode);
  95. }
  96. file = alloc_file_pseudo(inode, anon_inode_mnt, name,
  97. flags & (O_ACCMODE | O_NONBLOCK), fops);
  98. if (IS_ERR(file))
  99. goto err_iput;
  100. file->f_mapping = inode->i_mapping;
  101. file->private_data = priv;
  102. return file;
  103. err_iput:
  104. iput(inode);
  105. err:
  106. module_put(fops->owner);
  107. return file;
  108. }
  109. /**
  110. * anon_inode_getfile - creates a new file instance by hooking it up to an
  111. * anonymous inode, and a dentry that describe the "class"
  112. * of the file
  113. *
  114. * @name: [in] name of the "class" of the new file
  115. * @fops: [in] file operations for the new file
  116. * @priv: [in] private data for the new file (will be file's private_data)
  117. * @flags: [in] flags
  118. *
  119. * Creates a new file by hooking it on a single inode. This is useful for files
  120. * that do not need to have a full-fledged inode in order to operate correctly.
  121. * All the files created with anon_inode_getfile() will share a single inode,
  122. * hence saving memory and avoiding code duplication for the file/inode/dentry
  123. * setup. Returns the newly created file* or an error pointer.
  124. */
  125. struct file *anon_inode_getfile(const char *name,
  126. const struct file_operations *fops,
  127. void *priv, int flags)
  128. {
  129. return __anon_inode_getfile(name, fops, priv, flags, NULL, false);
  130. }
  131. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  132. /**
  133. * anon_inode_getfile_secure - Like anon_inode_getfile(), but creates a new
  134. * !S_PRIVATE anon inode rather than reuse the
  135. * singleton anon inode and calls the
  136. * inode_init_security_anon() LSM hook. This
  137. * allows for both the inode to have its own
  138. * security context and for the LSM to enforce
  139. * policy on the inode's creation.
  140. *
  141. * @name: [in] name of the "class" of the new file
  142. * @fops: [in] file operations for the new file
  143. * @priv: [in] private data for the new file (will be file's private_data)
  144. * @flags: [in] flags
  145. * @context_inode:
  146. * [in] the logical relationship with the new inode (optional)
  147. *
  148. * The LSM may use @context_inode in inode_init_security_anon(), but a
  149. * reference to it is not held. Returns the newly created file* or an error
  150. * pointer. See the anon_inode_getfile() documentation for more information.
  151. */
  152. struct file *anon_inode_getfile_secure(const char *name,
  153. const struct file_operations *fops,
  154. void *priv, int flags,
  155. const struct inode *context_inode)
  156. {
  157. return __anon_inode_getfile(name, fops, priv, flags,
  158. context_inode, true);
  159. }
  160. static int __anon_inode_getfd(const char *name,
  161. const struct file_operations *fops,
  162. void *priv, int flags,
  163. const struct inode *context_inode,
  164. bool secure)
  165. {
  166. int error, fd;
  167. struct file *file;
  168. error = get_unused_fd_flags(flags);
  169. if (error < 0)
  170. return error;
  171. fd = error;
  172. file = __anon_inode_getfile(name, fops, priv, flags, context_inode,
  173. secure);
  174. if (IS_ERR(file)) {
  175. error = PTR_ERR(file);
  176. goto err_put_unused_fd;
  177. }
  178. fd_install(fd, file);
  179. return fd;
  180. err_put_unused_fd:
  181. put_unused_fd(fd);
  182. return error;
  183. }
  184. /**
  185. * anon_inode_getfd - creates a new file instance by hooking it up to
  186. * an anonymous inode and a dentry that describe
  187. * the "class" of the file
  188. *
  189. * @name: [in] name of the "class" of the new file
  190. * @fops: [in] file operations for the new file
  191. * @priv: [in] private data for the new file (will be file's private_data)
  192. * @flags: [in] flags
  193. *
  194. * Creates a new file by hooking it on a single inode. This is
  195. * useful for files that do not need to have a full-fledged inode in
  196. * order to operate correctly. All the files created with
  197. * anon_inode_getfd() will use the same singleton inode, reducing
  198. * memory use and avoiding code duplication for the file/inode/dentry
  199. * setup. Returns a newly created file descriptor or an error code.
  200. */
  201. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  202. void *priv, int flags)
  203. {
  204. return __anon_inode_getfd(name, fops, priv, flags, NULL, false);
  205. }
  206. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  207. /**
  208. * anon_inode_getfd_secure - Like anon_inode_getfd(), but creates a new
  209. * !S_PRIVATE anon inode rather than reuse the singleton anon inode, and calls
  210. * the inode_init_security_anon() LSM hook. This allows the inode to have its
  211. * own security context and for a LSM to reject creation of the inode.
  212. *
  213. * @name: [in] name of the "class" of the new file
  214. * @fops: [in] file operations for the new file
  215. * @priv: [in] private data for the new file (will be file's private_data)
  216. * @flags: [in] flags
  217. * @context_inode:
  218. * [in] the logical relationship with the new inode (optional)
  219. *
  220. * The LSM may use @context_inode in inode_init_security_anon(), but a
  221. * reference to it is not held.
  222. */
  223. int anon_inode_getfd_secure(const char *name, const struct file_operations *fops,
  224. void *priv, int flags,
  225. const struct inode *context_inode)
  226. {
  227. return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
  228. }
  229. EXPORT_SYMBOL_GPL(anon_inode_getfd_secure);
  230. static int __init anon_inode_init(void)
  231. {
  232. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  233. if (IS_ERR(anon_inode_mnt))
  234. panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
  235. anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
  236. if (IS_ERR(anon_inode_inode))
  237. panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
  238. return 0;
  239. }
  240. fs_initcall(anon_inode_init);