0001
0002
0003
0004
0005
0006
0007
0008
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
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
0104
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
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
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
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
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
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
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
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
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