anon_inodes.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 __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(dentry, 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. static int __anon_inode_getfd(const char *name,
  133. const struct file_operations *fops,
  134. void *priv, int flags,
  135. const struct inode *context_inode,
  136. bool secure)
  137. {
  138. int error, fd;
  139. struct file *file;
  140. error = get_unused_fd_flags(flags);
  141. if (error < 0)
  142. return error;
  143. fd = error;
  144. file = __anon_inode_getfile(name, fops, priv, flags, context_inode,
  145. secure);
  146. if (IS_ERR(file)) {
  147. error = PTR_ERR(file);
  148. goto err_put_unused_fd;
  149. }
  150. fd_install(fd, file);
  151. return fd;
  152. err_put_unused_fd:
  153. put_unused_fd(fd);
  154. return error;
  155. }
  156. /**
  157. * anon_inode_getfd - creates a new file instance by hooking it up to
  158. * an anonymous inode and a dentry that describe
  159. * the "class" of the file
  160. *
  161. * @name: [in] name of the "class" of the new file
  162. * @fops: [in] file operations for the new file
  163. * @priv: [in] private data for the new file (will be file's private_data)
  164. * @flags: [in] flags
  165. *
  166. * Creates a new file by hooking it on a single inode. This is
  167. * useful for files that do not need to have a full-fledged inode in
  168. * order to operate correctly. All the files created with
  169. * anon_inode_getfd() will use the same singleton inode, reducing
  170. * memory use and avoiding code duplication for the file/inode/dentry
  171. * setup. Returns a newly created file descriptor or an error code.
  172. */
  173. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  174. void *priv, int flags)
  175. {
  176. return __anon_inode_getfd(name, fops, priv, flags, NULL, false);
  177. }
  178. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  179. /**
  180. * anon_inode_getfd_secure - Like anon_inode_getfd(), but creates a new
  181. * !S_PRIVATE anon inode rather than reuse the singleton anon inode, and calls
  182. * the inode_init_security_anon() LSM hook. This allows the inode to have its
  183. * own security context and for a LSM to reject creation of the inode.
  184. *
  185. * @name: [in] name of the "class" of the new file
  186. * @fops: [in] file operations for the new file
  187. * @priv: [in] private data for the new file (will be file's private_data)
  188. * @flags: [in] flags
  189. * @context_inode:
  190. * [in] the logical relationship with the new inode (optional)
  191. *
  192. * The LSM may use @context_inode in inode_init_security_anon(), but a
  193. * reference to it is not held.
  194. */
  195. int anon_inode_getfd_secure(const char *name, const struct file_operations *fops,
  196. void *priv, int flags,
  197. const struct inode *context_inode)
  198. {
  199. return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
  200. }
  201. EXPORT_SYMBOL_GPL(anon_inode_getfd_secure);
  202. static int __init anon_inode_init(void)
  203. {
  204. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  205. if (IS_ERR(anon_inode_mnt))
  206. panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
  207. anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
  208. if (IS_ERR(anon_inode_inode))
  209. panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
  210. return 0;
  211. }
  212. fs_initcall(anon_inode_init);