Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  fs/anon_inodes.c
0004  *
0005  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
0006  *
0007  *  Thanks to Arnd Bergmann for code review and suggestions.
0008  *  More changes for Thomas Gleixner suggestions.
0009  *
0010  */
0011 
0012 #include <linux/cred.h>
0013 #include <linux/file.h>
0014 #include <linux/poll.h>
0015 #include <linux/sched.h>
0016 #include <linux/init.h>
0017 #include <linux/fs.h>
0018 #include <linux/mount.h>
0019 #include <linux/module.h>
0020 #include <linux/kernel.h>
0021 #include <linux/magic.h>
0022 #include <linux/anon_inodes.h>
0023 #include <linux/pseudo_fs.h>
0024 
0025 #include <linux/uaccess.h>
0026 
0027 static struct vfsmount *anon_inode_mnt __read_mostly;
0028 static struct inode *anon_inode_inode;
0029 
0030 /*
0031  * anon_inodefs_dname() is called from d_path().
0032  */
0033 static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen)
0034 {
0035     return dynamic_dname(dentry, buffer, buflen, "anon_inode:%s",
0036                 dentry->d_name.name);
0037 }
0038 
0039 static const struct dentry_operations anon_inodefs_dentry_operations = {
0040     .d_dname    = anon_inodefs_dname,
0041 };
0042 
0043 static int anon_inodefs_init_fs_context(struct fs_context *fc)
0044 {
0045     struct pseudo_fs_context *ctx = init_pseudo(fc, ANON_INODE_FS_MAGIC);
0046     if (!ctx)
0047         return -ENOMEM;
0048     ctx->dops = &anon_inodefs_dentry_operations;
0049     return 0;
0050 }
0051 
0052 static struct file_system_type anon_inode_fs_type = {
0053     .name       = "anon_inodefs",
0054     .init_fs_context = anon_inodefs_init_fs_context,
0055     .kill_sb    = kill_anon_super,
0056 };
0057 
0058 static struct inode *anon_inode_make_secure_inode(
0059     const char *name,
0060     const struct inode *context_inode)
0061 {
0062     struct inode *inode;
0063     const struct qstr qname = QSTR_INIT(name, strlen(name));
0064     int error;
0065 
0066     inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
0067     if (IS_ERR(inode))
0068         return inode;
0069     inode->i_flags &= ~S_PRIVATE;
0070     error = security_inode_init_security_anon(inode, &qname, context_inode);
0071     if (error) {
0072         iput(inode);
0073         return ERR_PTR(error);
0074     }
0075     return inode;
0076 }
0077 
0078 static struct file *__anon_inode_getfile(const char *name,
0079                      const struct file_operations *fops,
0080                      void *priv, int flags,
0081                      const struct inode *context_inode,
0082                      bool secure)
0083 {
0084     struct inode *inode;
0085     struct file *file;
0086 
0087     if (fops->owner && !try_module_get(fops->owner))
0088         return ERR_PTR(-ENOENT);
0089 
0090     if (secure) {
0091         inode = anon_inode_make_secure_inode(name, context_inode);
0092         if (IS_ERR(inode)) {
0093             file = ERR_CAST(inode);
0094             goto err;
0095         }
0096     } else {
0097         inode = anon_inode_inode;
0098         if (IS_ERR(inode)) {
0099             file = ERR_PTR(-ENODEV);
0100             goto err;
0101         }
0102         /*
0103          * We know the anon_inode inode count is always
0104          * greater than zero, so ihold() is safe.
0105          */
0106         ihold(inode);
0107     }
0108 
0109     file = alloc_file_pseudo(inode, anon_inode_mnt, name,
0110                  flags & (O_ACCMODE | O_NONBLOCK), fops);
0111     if (IS_ERR(file))
0112         goto err_iput;
0113 
0114     file->f_mapping = inode->i_mapping;
0115 
0116     file->private_data = priv;
0117 
0118     return file;
0119 
0120 err_iput:
0121     iput(inode);
0122 err:
0123     module_put(fops->owner);
0124     return file;
0125 }
0126 
0127 /**
0128  * anon_inode_getfile - creates a new file instance by hooking it up to an
0129  *                      anonymous inode, and a dentry that describe the "class"
0130  *                      of the file
0131  *
0132  * @name:    [in]    name of the "class" of the new file
0133  * @fops:    [in]    file operations for the new file
0134  * @priv:    [in]    private data for the new file (will be file's private_data)
0135  * @flags:   [in]    flags
0136  *
0137  * Creates a new file by hooking it on a single inode. This is useful for files
0138  * that do not need to have a full-fledged inode in order to operate correctly.
0139  * All the files created with anon_inode_getfile() will share a single inode,
0140  * hence saving memory and avoiding code duplication for the file/inode/dentry
0141  * setup.  Returns the newly created file* or an error pointer.
0142  */
0143 struct file *anon_inode_getfile(const char *name,
0144                 const struct file_operations *fops,
0145                 void *priv, int flags)
0146 {
0147     return __anon_inode_getfile(name, fops, priv, flags, NULL, false);
0148 }
0149 EXPORT_SYMBOL_GPL(anon_inode_getfile);
0150 
0151 /**
0152  * anon_inode_getfile_secure - Like anon_inode_getfile(), but creates a new
0153  *                             !S_PRIVATE anon inode rather than reuse the
0154  *                             singleton anon inode and calls the
0155  *                             inode_init_security_anon() LSM hook.  This
0156  *                             allows for both the inode to have its own
0157  *                             security context and for the LSM to enforce
0158  *                             policy on the inode's creation.
0159  *
0160  * @name:    [in]    name of the "class" of the new file
0161  * @fops:    [in]    file operations for the new file
0162  * @priv:    [in]    private data for the new file (will be file's private_data)
0163  * @flags:   [in]    flags
0164  * @context_inode:
0165  *           [in]    the logical relationship with the new inode (optional)
0166  *
0167  * The LSM may use @context_inode in inode_init_security_anon(), but a
0168  * reference to it is not held.  Returns the newly created file* or an error
0169  * pointer.  See the anon_inode_getfile() documentation for more information.
0170  */
0171 struct file *anon_inode_getfile_secure(const char *name,
0172                        const struct file_operations *fops,
0173                        void *priv, int flags,
0174                        const struct inode *context_inode)
0175 {
0176     return __anon_inode_getfile(name, fops, priv, flags,
0177                     context_inode, true);
0178 }
0179 
0180 static int __anon_inode_getfd(const char *name,
0181                   const struct file_operations *fops,
0182                   void *priv, int flags,
0183                   const struct inode *context_inode,
0184                   bool secure)
0185 {
0186     int error, fd;
0187     struct file *file;
0188 
0189     error = get_unused_fd_flags(flags);
0190     if (error < 0)
0191         return error;
0192     fd = error;
0193 
0194     file = __anon_inode_getfile(name, fops, priv, flags, context_inode,
0195                     secure);
0196     if (IS_ERR(file)) {
0197         error = PTR_ERR(file);
0198         goto err_put_unused_fd;
0199     }
0200     fd_install(fd, file);
0201 
0202     return fd;
0203 
0204 err_put_unused_fd:
0205     put_unused_fd(fd);
0206     return error;
0207 }
0208 
0209 /**
0210  * anon_inode_getfd - creates a new file instance by hooking it up to
0211  *                    an anonymous inode and a dentry that describe
0212  *                    the "class" of the file
0213  *
0214  * @name:    [in]    name of the "class" of the new file
0215  * @fops:    [in]    file operations for the new file
0216  * @priv:    [in]    private data for the new file (will be file's private_data)
0217  * @flags:   [in]    flags
0218  *
0219  * Creates a new file by hooking it on a single inode. This is
0220  * useful for files that do not need to have a full-fledged inode in
0221  * order to operate correctly.  All the files created with
0222  * anon_inode_getfd() will use the same singleton inode, reducing
0223  * memory use and avoiding code duplication for the file/inode/dentry
0224  * setup.  Returns a newly created file descriptor or an error code.
0225  */
0226 int anon_inode_getfd(const char *name, const struct file_operations *fops,
0227              void *priv, int flags)
0228 {
0229     return __anon_inode_getfd(name, fops, priv, flags, NULL, false);
0230 }
0231 EXPORT_SYMBOL_GPL(anon_inode_getfd);
0232 
0233 /**
0234  * anon_inode_getfd_secure - Like anon_inode_getfd(), but creates a new
0235  * !S_PRIVATE anon inode rather than reuse the singleton anon inode, and calls
0236  * the inode_init_security_anon() LSM hook. This allows the inode to have its
0237  * own security context and for a LSM to reject creation of the inode.
0238  *
0239  * @name:    [in]    name of the "class" of the new file
0240  * @fops:    [in]    file operations for the new file
0241  * @priv:    [in]    private data for the new file (will be file's private_data)
0242  * @flags:   [in]    flags
0243  * @context_inode:
0244  *           [in]    the logical relationship with the new inode (optional)
0245  *
0246  * The LSM may use @context_inode in inode_init_security_anon(), but a
0247  * reference to it is not held.
0248  */
0249 int anon_inode_getfd_secure(const char *name, const struct file_operations *fops,
0250                 void *priv, int flags,
0251                 const struct inode *context_inode)
0252 {
0253     return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
0254 }
0255 EXPORT_SYMBOL_GPL(anon_inode_getfd_secure);
0256 
0257 static int __init anon_inode_init(void)
0258 {
0259     anon_inode_mnt = kern_mount(&anon_inode_fs_type);
0260     if (IS_ERR(anon_inode_mnt))
0261         panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
0262 
0263     anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
0264     if (IS_ERR(anon_inode_inode))
0265         panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
0266 
0267     return 0;
0268 }
0269 
0270 fs_initcall(anon_inode_init);
0271