1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25dc8bf81SDavide Libenzi /*
35dc8bf81SDavide Libenzi * fs/anon_inodes.c
45dc8bf81SDavide Libenzi *
55dc8bf81SDavide Libenzi * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
65dc8bf81SDavide Libenzi *
75dc8bf81SDavide Libenzi * Thanks to Arnd Bergmann for code review and suggestions.
85dc8bf81SDavide Libenzi * More changes for Thomas Gleixner suggestions.
95dc8bf81SDavide Libenzi *
105dc8bf81SDavide Libenzi */
115dc8bf81SDavide Libenzi
12a99bbaf5SAlexey Dobriyan #include <linux/cred.h>
135dc8bf81SDavide Libenzi #include <linux/file.h>
145dc8bf81SDavide Libenzi #include <linux/poll.h>
15a99bbaf5SAlexey Dobriyan #include <linux/sched.h>
165dc8bf81SDavide Libenzi #include <linux/init.h>
175dc8bf81SDavide Libenzi #include <linux/fs.h>
185dc8bf81SDavide Libenzi #include <linux/mount.h>
195dc8bf81SDavide Libenzi #include <linux/module.h>
205dc8bf81SDavide Libenzi #include <linux/kernel.h>
215dc8bf81SDavide Libenzi #include <linux/magic.h>
225dc8bf81SDavide Libenzi #include <linux/anon_inodes.h>
2333cada40SDavid Howells #include <linux/pseudo_fs.h>
245dc8bf81SDavide Libenzi
257c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
265dc8bf81SDavide Libenzi
275dc8bf81SDavide Libenzi static struct vfsmount *anon_inode_mnt __read_mostly;
285dc8bf81SDavide Libenzi static struct inode *anon_inode_inode;
295dc8bf81SDavide Libenzi
30b9aff027SNick Piggin /*
31b9aff027SNick Piggin * anon_inodefs_dname() is called from d_path().
32b9aff027SNick Piggin */
anon_inodefs_dname(struct dentry * dentry,char * buffer,int buflen)33b9aff027SNick Piggin static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen)
34b9aff027SNick Piggin {
35*0f60d288SAl Viro return dynamic_dname(buffer, buflen, "anon_inode:%s",
36b9aff027SNick Piggin dentry->d_name.name);
37b9aff027SNick Piggin }
38b9aff027SNick Piggin
39c74a1cbbSAl Viro static const struct dentry_operations anon_inodefs_dentry_operations = {
40c74a1cbbSAl Viro .d_dname = anon_inodefs_dname,
41c74a1cbbSAl Viro };
42c74a1cbbSAl Viro
anon_inodefs_init_fs_context(struct fs_context * fc)4333cada40SDavid Howells static int anon_inodefs_init_fs_context(struct fs_context *fc)
44ca7068c4SAl Viro {
4533cada40SDavid Howells struct pseudo_fs_context *ctx = init_pseudo(fc, ANON_INODE_FS_MAGIC);
4633cada40SDavid Howells if (!ctx)
4733cada40SDavid Howells return -ENOMEM;
4833cada40SDavid Howells ctx->dops = &anon_inodefs_dentry_operations;
4933cada40SDavid Howells return 0;
50ca7068c4SAl Viro }
51ca7068c4SAl Viro
52ca7068c4SAl Viro static struct file_system_type anon_inode_fs_type = {
53ca7068c4SAl Viro .name = "anon_inodefs",
5433cada40SDavid Howells .init_fs_context = anon_inodefs_init_fs_context,
55ca7068c4SAl Viro .kill_sb = kill_anon_super,
56ca7068c4SAl Viro };
57ca7068c4SAl Viro
anon_inode_make_secure_inode(const char * name,const struct inode * context_inode)58e7e832ceSDaniel Colascione static struct inode *anon_inode_make_secure_inode(
59e7e832ceSDaniel Colascione const char *name,
60e7e832ceSDaniel Colascione const struct inode *context_inode)
61e7e832ceSDaniel Colascione {
62e7e832ceSDaniel Colascione struct inode *inode;
63e7e832ceSDaniel Colascione const struct qstr qname = QSTR_INIT(name, strlen(name));
64e7e832ceSDaniel Colascione int error;
65e7e832ceSDaniel Colascione
66e7e832ceSDaniel Colascione inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
67e7e832ceSDaniel Colascione if (IS_ERR(inode))
68e7e832ceSDaniel Colascione return inode;
69e7e832ceSDaniel Colascione inode->i_flags &= ~S_PRIVATE;
70e7e832ceSDaniel Colascione error = security_inode_init_security_anon(inode, &qname, context_inode);
71e7e832ceSDaniel Colascione if (error) {
72e7e832ceSDaniel Colascione iput(inode);
73e7e832ceSDaniel Colascione return ERR_PTR(error);
74e7e832ceSDaniel Colascione }
75e7e832ceSDaniel Colascione return inode;
76e7e832ceSDaniel Colascione }
77e7e832ceSDaniel Colascione
__anon_inode_getfile(const char * name,const struct file_operations * fops,void * priv,int flags,const struct inode * context_inode,bool secure)78e7e832ceSDaniel Colascione static struct file *__anon_inode_getfile(const char *name,
79e7e832ceSDaniel Colascione const struct file_operations *fops,
80e7e832ceSDaniel Colascione void *priv, int flags,
81e7e832ceSDaniel Colascione const struct inode *context_inode,
82e7e832ceSDaniel Colascione bool secure)
83e7e832ceSDaniel Colascione {
84e7e832ceSDaniel Colascione struct inode *inode;
85e7e832ceSDaniel Colascione struct file *file;
86e7e832ceSDaniel Colascione
87e7e832ceSDaniel Colascione if (fops->owner && !try_module_get(fops->owner))
88e7e832ceSDaniel Colascione return ERR_PTR(-ENOENT);
89e7e832ceSDaniel Colascione
90e7e832ceSDaniel Colascione if (secure) {
91e7e832ceSDaniel Colascione inode = anon_inode_make_secure_inode(name, context_inode);
92e7e832ceSDaniel Colascione if (IS_ERR(inode)) {
93e7e832ceSDaniel Colascione file = ERR_CAST(inode);
94e7e832ceSDaniel Colascione goto err;
95e7e832ceSDaniel Colascione }
96e7e832ceSDaniel Colascione } else {
97e7e832ceSDaniel Colascione inode = anon_inode_inode;
98e7e832ceSDaniel Colascione if (IS_ERR(inode)) {
99e7e832ceSDaniel Colascione file = ERR_PTR(-ENODEV);
100e7e832ceSDaniel Colascione goto err;
101e7e832ceSDaniel Colascione }
102e7e832ceSDaniel Colascione /*
103e7e832ceSDaniel Colascione * We know the anon_inode inode count is always
104e7e832ceSDaniel Colascione * greater than zero, so ihold() is safe.
105e7e832ceSDaniel Colascione */
106e7e832ceSDaniel Colascione ihold(inode);
107e7e832ceSDaniel Colascione }
108e7e832ceSDaniel Colascione
109e7e832ceSDaniel Colascione file = alloc_file_pseudo(inode, anon_inode_mnt, name,
110e7e832ceSDaniel Colascione flags & (O_ACCMODE | O_NONBLOCK), fops);
111e7e832ceSDaniel Colascione if (IS_ERR(file))
112e7e832ceSDaniel Colascione goto err_iput;
113e7e832ceSDaniel Colascione
114e7e832ceSDaniel Colascione file->f_mapping = inode->i_mapping;
115e7e832ceSDaniel Colascione
116e7e832ceSDaniel Colascione file->private_data = priv;
117e7e832ceSDaniel Colascione
118e7e832ceSDaniel Colascione return file;
119e7e832ceSDaniel Colascione
120e7e832ceSDaniel Colascione err_iput:
121e7e832ceSDaniel Colascione iput(inode);
122e7e832ceSDaniel Colascione err:
123e7e832ceSDaniel Colascione module_put(fops->owner);
124e7e832ceSDaniel Colascione return file;
125e7e832ceSDaniel Colascione }
126e7e832ceSDaniel Colascione
1275dc8bf81SDavide Libenzi /**
128c0d8768aSNamhyung Kim * anon_inode_getfile - creates a new file instance by hooking it up to an
1295dc8bf81SDavide Libenzi * anonymous inode, and a dentry that describe the "class"
1305dc8bf81SDavide Libenzi * of the file
1315dc8bf81SDavide Libenzi *
1325dc8bf81SDavide Libenzi * @name: [in] name of the "class" of the new file
1337d9dbca3SUlrich Drepper * @fops: [in] file operations for the new file
1347d9dbca3SUlrich Drepper * @priv: [in] private data for the new file (will be file's private_data)
1357d9dbca3SUlrich Drepper * @flags: [in] flags
1365dc8bf81SDavide Libenzi *
1375dc8bf81SDavide Libenzi * Creates a new file by hooking it on a single inode. This is useful for files
1385dc8bf81SDavide Libenzi * that do not need to have a full-fledged inode in order to operate correctly.
139562787a5SDavide Libenzi * All the files created with anon_inode_getfile() will share a single inode,
1405dc8bf81SDavide Libenzi * hence saving memory and avoiding code duplication for the file/inode/dentry
141562787a5SDavide Libenzi * setup. Returns the newly created file* or an error pointer.
1425dc8bf81SDavide Libenzi */
anon_inode_getfile(const char * name,const struct file_operations * fops,void * priv,int flags)143562787a5SDavide Libenzi struct file *anon_inode_getfile(const char *name,
144562787a5SDavide Libenzi const struct file_operations *fops,
1457d9dbca3SUlrich Drepper void *priv, int flags)
1465dc8bf81SDavide Libenzi {
147e7e832ceSDaniel Colascione return __anon_inode_getfile(name, fops, priv, flags, NULL, false);
148562787a5SDavide Libenzi }
149562787a5SDavide Libenzi EXPORT_SYMBOL_GPL(anon_inode_getfile);
150562787a5SDavide Libenzi
1513a862cacSPaul Moore /**
1523a862cacSPaul Moore * anon_inode_getfile_secure - Like anon_inode_getfile(), but creates a new
1533a862cacSPaul Moore * !S_PRIVATE anon inode rather than reuse the
1543a862cacSPaul Moore * singleton anon inode and calls the
1553a862cacSPaul Moore * inode_init_security_anon() LSM hook. This
1563a862cacSPaul Moore * allows for both the inode to have its own
1573a862cacSPaul Moore * security context and for the LSM to enforce
1583a862cacSPaul Moore * policy on the inode's creation.
1593a862cacSPaul Moore *
1603a862cacSPaul Moore * @name: [in] name of the "class" of the new file
1613a862cacSPaul Moore * @fops: [in] file operations for the new file
1623a862cacSPaul Moore * @priv: [in] private data for the new file (will be file's private_data)
1633a862cacSPaul Moore * @flags: [in] flags
1643a862cacSPaul Moore * @context_inode:
1653a862cacSPaul Moore * [in] the logical relationship with the new inode (optional)
1663a862cacSPaul Moore *
1673a862cacSPaul Moore * The LSM may use @context_inode in inode_init_security_anon(), but a
1683a862cacSPaul Moore * reference to it is not held. Returns the newly created file* or an error
1693a862cacSPaul Moore * pointer. See the anon_inode_getfile() documentation for more information.
1703a862cacSPaul Moore */
anon_inode_getfile_secure(const char * name,const struct file_operations * fops,void * priv,int flags,const struct inode * context_inode)1713a862cacSPaul Moore struct file *anon_inode_getfile_secure(const char *name,
1723a862cacSPaul Moore const struct file_operations *fops,
1733a862cacSPaul Moore void *priv, int flags,
1743a862cacSPaul Moore const struct inode *context_inode)
1753a862cacSPaul Moore {
1763a862cacSPaul Moore return __anon_inode_getfile(name, fops, priv, flags,
1773a862cacSPaul Moore context_inode, true);
1783a862cacSPaul Moore }
1793a862cacSPaul Moore
__anon_inode_getfd(const char * name,const struct file_operations * fops,void * priv,int flags,const struct inode * context_inode,bool secure)180e7e832ceSDaniel Colascione static int __anon_inode_getfd(const char *name,
181e7e832ceSDaniel Colascione const struct file_operations *fops,
182e7e832ceSDaniel Colascione void *priv, int flags,
183e7e832ceSDaniel Colascione const struct inode *context_inode,
184e7e832ceSDaniel Colascione bool secure)
185562787a5SDavide Libenzi {
186562787a5SDavide Libenzi int error, fd;
187562787a5SDavide Libenzi struct file *file;
188562787a5SDavide Libenzi
189562787a5SDavide Libenzi error = get_unused_fd_flags(flags);
190562787a5SDavide Libenzi if (error < 0)
191562787a5SDavide Libenzi return error;
192562787a5SDavide Libenzi fd = error;
193562787a5SDavide Libenzi
194e7e832ceSDaniel Colascione file = __anon_inode_getfile(name, fops, priv, flags, context_inode,
195e7e832ceSDaniel Colascione secure);
196562787a5SDavide Libenzi if (IS_ERR(file)) {
197562787a5SDavide Libenzi error = PTR_ERR(file);
198562787a5SDavide Libenzi goto err_put_unused_fd;
199562787a5SDavide Libenzi }
2005dc8bf81SDavide Libenzi fd_install(fd, file);
2015dc8bf81SDavide Libenzi
2022030a42cSAl Viro return fd;
2035dc8bf81SDavide Libenzi
2045dc8bf81SDavide Libenzi err_put_unused_fd:
2055dc8bf81SDavide Libenzi put_unused_fd(fd);
2065dc8bf81SDavide Libenzi return error;
2075dc8bf81SDavide Libenzi }
208e7e832ceSDaniel Colascione
209e7e832ceSDaniel Colascione /**
210e7e832ceSDaniel Colascione * anon_inode_getfd - creates a new file instance by hooking it up to
211e7e832ceSDaniel Colascione * an anonymous inode and a dentry that describe
212e7e832ceSDaniel Colascione * the "class" of the file
213e7e832ceSDaniel Colascione *
214e7e832ceSDaniel Colascione * @name: [in] name of the "class" of the new file
215e7e832ceSDaniel Colascione * @fops: [in] file operations for the new file
216e7e832ceSDaniel Colascione * @priv: [in] private data for the new file (will be file's private_data)
217e7e832ceSDaniel Colascione * @flags: [in] flags
218e7e832ceSDaniel Colascione *
219e7e832ceSDaniel Colascione * Creates a new file by hooking it on a single inode. This is
220e7e832ceSDaniel Colascione * useful for files that do not need to have a full-fledged inode in
221e7e832ceSDaniel Colascione * order to operate correctly. All the files created with
222e7e832ceSDaniel Colascione * anon_inode_getfd() will use the same singleton inode, reducing
223e7e832ceSDaniel Colascione * memory use and avoiding code duplication for the file/inode/dentry
224e7e832ceSDaniel Colascione * setup. Returns a newly created file descriptor or an error code.
225e7e832ceSDaniel Colascione */
anon_inode_getfd(const char * name,const struct file_operations * fops,void * priv,int flags)226e7e832ceSDaniel Colascione int anon_inode_getfd(const char *name, const struct file_operations *fops,
227e7e832ceSDaniel Colascione void *priv, int flags)
228e7e832ceSDaniel Colascione {
229e7e832ceSDaniel Colascione return __anon_inode_getfd(name, fops, priv, flags, NULL, false);
230e7e832ceSDaniel Colascione }
231d6d28168SAvi Kivity EXPORT_SYMBOL_GPL(anon_inode_getfd);
2325dc8bf81SDavide Libenzi
233e7e832ceSDaniel Colascione /**
234365982abSLukas Bulwahn * anon_inode_getfd_secure - Like anon_inode_getfd(), but creates a new
235365982abSLukas Bulwahn * !S_PRIVATE anon inode rather than reuse the singleton anon inode, and calls
236365982abSLukas Bulwahn * the inode_init_security_anon() LSM hook. This allows the inode to have its
237365982abSLukas Bulwahn * own security context and for a LSM to reject creation of the inode.
238365982abSLukas Bulwahn *
239365982abSLukas Bulwahn * @name: [in] name of the "class" of the new file
240365982abSLukas Bulwahn * @fops: [in] file operations for the new file
241365982abSLukas Bulwahn * @priv: [in] private data for the new file (will be file's private_data)
242365982abSLukas Bulwahn * @flags: [in] flags
243365982abSLukas Bulwahn * @context_inode:
244365982abSLukas Bulwahn * [in] the logical relationship with the new inode (optional)
245365982abSLukas Bulwahn *
246365982abSLukas Bulwahn * The LSM may use @context_inode in inode_init_security_anon(), but a
247365982abSLukas Bulwahn * reference to it is not held.
248e7e832ceSDaniel Colascione */
anon_inode_getfd_secure(const char * name,const struct file_operations * fops,void * priv,int flags,const struct inode * context_inode)249e7e832ceSDaniel Colascione int anon_inode_getfd_secure(const char *name, const struct file_operations *fops,
250e7e832ceSDaniel Colascione void *priv, int flags,
251e7e832ceSDaniel Colascione const struct inode *context_inode)
252e7e832ceSDaniel Colascione {
253e7e832ceSDaniel Colascione return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
254e7e832ceSDaniel Colascione }
255e7e832ceSDaniel Colascione EXPORT_SYMBOL_GPL(anon_inode_getfd_secure);
256e7e832ceSDaniel Colascione
anon_inode_init(void)2575dc8bf81SDavide Libenzi static int __init anon_inode_init(void)
2585dc8bf81SDavide Libenzi {
2595dc8bf81SDavide Libenzi anon_inode_mnt = kern_mount(&anon_inode_fs_type);
26075c5a52dSJan Kara if (IS_ERR(anon_inode_mnt))
26175c5a52dSJan Kara panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
2625dc8bf81SDavide Libenzi
26375c5a52dSJan Kara anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
26475c5a52dSJan Kara if (IS_ERR(anon_inode_inode))
26575c5a52dSJan Kara panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
26675c5a52dSJan Kara
26775c5a52dSJan Kara return 0;
2685dc8bf81SDavide Libenzi }
2695dc8bf81SDavide Libenzi
2705dc8bf81SDavide Libenzi fs_initcall(anon_inode_init);
2715dc8bf81SDavide Libenzi
272