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