1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 2 /* 3 * fs/anon_inodes.c 3 * fs/anon_inodes.c 4 * 4 * 5 * Copyright (C) 2007 Davide Libenzi <davide 5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org> 6 * 6 * 7 * Thanks to Arnd Bergmann for code review an 7 * Thanks to Arnd Bergmann for code review and suggestions. 8 * More changes for Thomas Gleixner suggestio 8 * More changes for Thomas Gleixner suggestions. 9 * 9 * 10 */ 10 */ 11 11 12 #include <linux/cred.h> 12 #include <linux/cred.h> 13 #include <linux/file.h> 13 #include <linux/file.h> 14 #include <linux/poll.h> 14 #include <linux/poll.h> 15 #include <linux/sched.h> 15 #include <linux/sched.h> 16 #include <linux/init.h> 16 #include <linux/init.h> 17 #include <linux/fs.h> 17 #include <linux/fs.h> 18 #include <linux/mount.h> 18 #include <linux/mount.h> 19 #include <linux/module.h> 19 #include <linux/module.h> 20 #include <linux/kernel.h> 20 #include <linux/kernel.h> 21 #include <linux/magic.h> 21 #include <linux/magic.h> 22 #include <linux/anon_inodes.h> 22 #include <linux/anon_inodes.h> 23 #include <linux/pseudo_fs.h> 23 #include <linux/pseudo_fs.h> 24 24 25 #include <linux/uaccess.h> 25 #include <linux/uaccess.h> 26 26 27 static struct vfsmount *anon_inode_mnt __ro_af !! 27 static struct vfsmount *anon_inode_mnt __read_mostly; 28 static struct inode *anon_inode_inode __ro_aft !! 28 static struct inode *anon_inode_inode; 29 29 30 /* 30 /* 31 * anon_inodefs_dname() is called from d_path( 31 * anon_inodefs_dname() is called from d_path(). 32 */ 32 */ 33 static char *anon_inodefs_dname(struct dentry 33 static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen) 34 { 34 { 35 return dynamic_dname(buffer, buflen, " !! 35 return dynamic_dname(dentry, buffer, buflen, "anon_inode:%s", 36 dentry->d_name 36 dentry->d_name.name); 37 } 37 } 38 38 39 static const struct dentry_operations anon_ino 39 static const struct dentry_operations anon_inodefs_dentry_operations = { 40 .d_dname = anon_inodefs_dname, 40 .d_dname = anon_inodefs_dname, 41 }; 41 }; 42 42 43 static int anon_inodefs_init_fs_context(struct 43 static int anon_inodefs_init_fs_context(struct fs_context *fc) 44 { 44 { 45 struct pseudo_fs_context *ctx = init_p 45 struct pseudo_fs_context *ctx = init_pseudo(fc, ANON_INODE_FS_MAGIC); 46 if (!ctx) 46 if (!ctx) 47 return -ENOMEM; 47 return -ENOMEM; 48 ctx->dops = &anon_inodefs_dentry_opera 48 ctx->dops = &anon_inodefs_dentry_operations; 49 return 0; 49 return 0; 50 } 50 } 51 51 52 static struct file_system_type anon_inode_fs_t 52 static struct file_system_type anon_inode_fs_type = { 53 .name = "anon_inodefs", 53 .name = "anon_inodefs", 54 .init_fs_context = anon_inodefs_init_f 54 .init_fs_context = anon_inodefs_init_fs_context, 55 .kill_sb = kill_anon_super, 55 .kill_sb = kill_anon_super, 56 }; 56 }; 57 57 58 static struct inode *anon_inode_make_secure_in << 59 const char *name, << 60 const struct inode *context_inode) << 61 { << 62 struct inode *inode; << 63 const struct qstr qname = QSTR_INIT(na << 64 int error; << 65 << 66 inode = alloc_anon_inode(anon_inode_mn << 67 if (IS_ERR(inode)) << 68 return inode; << 69 inode->i_flags &= ~S_PRIVATE; << 70 error = security_inode_init_security_a << 71 if (error) { << 72 iput(inode); << 73 return ERR_PTR(error); << 74 } << 75 return inode; << 76 } << 77 << 78 static struct file *__anon_inode_getfile(const << 79 const << 80 void << 81 const << 82 bool << 83 { << 84 struct inode *inode; << 85 struct file *file; << 86 << 87 if (fops->owner && !try_module_get(fop << 88 return ERR_PTR(-ENOENT); << 89 << 90 if (make_inode) { << 91 inode = anon_inode_make_secure << 92 if (IS_ERR(inode)) { << 93 file = ERR_CAST(inode) << 94 goto err; << 95 } << 96 } else { << 97 inode = anon_inode_inode; << 98 if (IS_ERR(inode)) { << 99 file = ERR_PTR(-ENODEV << 100 goto err; << 101 } << 102 /* << 103 * We know the anon_inode inod << 104 * greater than zero, so ihold << 105 */ << 106 ihold(inode); << 107 } << 108 << 109 file = alloc_file_pseudo(inode, anon_i << 110 flags & (O_AC << 111 if (IS_ERR(file)) << 112 goto err_iput; << 113 << 114 file->f_mapping = inode->i_mapping; << 115 << 116 file->private_data = priv; << 117 << 118 return file; << 119 << 120 err_iput: << 121 iput(inode); << 122 err: << 123 module_put(fops->owner); << 124 return file; << 125 } << 126 << 127 /** 58 /** 128 * anon_inode_getfile - creates a new file ins 59 * anon_inode_getfile - creates a new file instance by hooking it up to an 129 * anonymous inode, and a 60 * anonymous inode, and a dentry that describe the "class" 130 * of the file 61 * of the file 131 * 62 * 132 * @name: [in] name of the "class" of th 63 * @name: [in] name of the "class" of the new file 133 * @fops: [in] file operations for the n 64 * @fops: [in] file operations for the new file 134 * @priv: [in] private data for the new 65 * @priv: [in] private data for the new file (will be file's private_data) 135 * @flags: [in] flags 66 * @flags: [in] flags 136 * 67 * 137 * Creates a new file by hooking it on a singl 68 * Creates a new file by hooking it on a single inode. This is useful for files 138 * that do not need to have a full-fledged ino 69 * that do not need to have a full-fledged inode in order to operate correctly. 139 * All the files created with anon_inode_getfi 70 * All the files created with anon_inode_getfile() will share a single inode, 140 * hence saving memory and avoiding code dupli 71 * hence saving memory and avoiding code duplication for the file/inode/dentry 141 * setup. Returns the newly created file* or 72 * setup. Returns the newly created file* or an error pointer. 142 */ 73 */ 143 struct file *anon_inode_getfile(const char *na 74 struct file *anon_inode_getfile(const char *name, 144 const struct f 75 const struct file_operations *fops, 145 void *priv, in 76 void *priv, int flags) 146 { 77 { 147 return __anon_inode_getfile(name, fops << 148 } << 149 EXPORT_SYMBOL_GPL(anon_inode_getfile); << 150 << 151 /** << 152 * anon_inode_getfile_fmode - creates a new fi << 153 * anonymous inode, and a << 154 * of the file << 155 * << 156 * @name: [in] name of the "class" of th << 157 * @fops: [in] file operations for the n << 158 * @priv: [in] private data for the new << 159 * @flags: [in] flags << 160 * @f_mode: [in] fmode << 161 * << 162 * Creates a new file by hooking it on a singl << 163 * that do not need to have a full-fledged ino << 164 * All the files created with anon_inode_getfi << 165 * hence saving memory and avoiding code dupli << 166 * setup. Allows setting the fmode. Returns th << 167 * pointer. << 168 */ << 169 struct file *anon_inode_getfile_fmode(const ch << 170 const struct f << 171 void *priv, in << 172 { << 173 struct file *file; 78 struct file *file; 174 79 175 file = __anon_inode_getfile(name, fops !! 80 if (IS_ERR(anon_inode_inode)) 176 if (!IS_ERR(file)) !! 81 return ERR_PTR(-ENODEV); 177 file->f_mode |= f_mode; !! 82 >> 83 if (fops->owner && !try_module_get(fops->owner)) >> 84 return ERR_PTR(-ENOENT); >> 85 >> 86 /* >> 87 * We know the anon_inode inode count is always greater than zero, >> 88 * so ihold() is safe. >> 89 */ >> 90 ihold(anon_inode_inode); >> 91 file = alloc_file_pseudo(anon_inode_inode, anon_inode_mnt, name, >> 92 flags & (O_ACCMODE | O_NONBLOCK), fops); >> 93 if (IS_ERR(file)) >> 94 goto err; 178 95 >> 96 file->f_mapping = anon_inode_inode->i_mapping; >> 97 >> 98 file->private_data = priv; >> 99 >> 100 return file; >> 101 >> 102 err: >> 103 iput(anon_inode_inode); >> 104 module_put(fops->owner); 179 return file; 105 return file; 180 } 106 } 181 EXPORT_SYMBOL_GPL(anon_inode_getfile_fmode); !! 107 EXPORT_SYMBOL_GPL(anon_inode_getfile); 182 108 183 /** 109 /** 184 * anon_inode_create_getfile - Like anon_inode !! 110 * anon_inode_getfd - creates a new file instance by hooking it up to an 185 * !S_PRIVATE anon !! 111 * anonymous inode, and a dentry that describe the "class" 186 * singleton anon !! 112 * of the file 187 * inode_init_secu << 188 * 113 * 189 * @name: [in] name of the "class" of th 114 * @name: [in] name of the "class" of the new file 190 * @fops: [in] file operations for the n 115 * @fops: [in] file operations for the new file 191 * @priv: [in] private data for the new 116 * @priv: [in] private data for the new file (will be file's private_data) 192 * @flags: [in] flags 117 * @flags: [in] flags 193 * @context_inode: << 194 * [in] the logical relationship << 195 * << 196 * Create a new anonymous inode and file pair. << 197 * reasons: << 198 * << 199 * - for the inode to have its own security co << 200 * policy on the inode's creation; << 201 * 118 * 202 * - if the caller needs a unique inode, for e !! 119 * Creates a new file by hooking it on a single inode. This is useful for files 203 * the size returned by fstat() !! 120 * that do not need to have a full-fledged inode in order to operate correctly. 204 * !! 121 * All the files created with anon_inode_getfd() will share a single inode, 205 * The LSM may use @context_inode in inode_ini !! 122 * hence saving memory and avoiding code duplication for the file/inode/dentry 206 * reference to it is not held. !! 123 * setup. Returns new descriptor or an error code. 207 * << 208 * Returns the newly created file* or an error << 209 */ 124 */ 210 struct file *anon_inode_create_getfile(const c !! 125 int anon_inode_getfd(const char *name, const struct file_operations *fops, 211 const s !! 126 void *priv, int flags) 212 void *p << 213 const s << 214 { << 215 return __anon_inode_getfile(name, fops << 216 context_in << 217 } << 218 EXPORT_SYMBOL_GPL(anon_inode_create_getfile); << 219 << 220 static int __anon_inode_getfd(const char *name << 221 const struct fil << 222 void *priv, int << 223 const struct ino << 224 bool make_inode) << 225 { 127 { 226 int error, fd; 128 int error, fd; 227 struct file *file; 129 struct file *file; 228 130 229 error = get_unused_fd_flags(flags); 131 error = get_unused_fd_flags(flags); 230 if (error < 0) 132 if (error < 0) 231 return error; 133 return error; 232 fd = error; 134 fd = error; 233 135 234 file = __anon_inode_getfile(name, fops !! 136 file = anon_inode_getfile(name, fops, priv, flags); 235 make_inode << 236 if (IS_ERR(file)) { 137 if (IS_ERR(file)) { 237 error = PTR_ERR(file); 138 error = PTR_ERR(file); 238 goto err_put_unused_fd; 139 goto err_put_unused_fd; 239 } 140 } 240 fd_install(fd, file); 141 fd_install(fd, file); 241 142 242 return fd; 143 return fd; 243 144 244 err_put_unused_fd: 145 err_put_unused_fd: 245 put_unused_fd(fd); 146 put_unused_fd(fd); 246 return error; 147 return error; 247 } 148 } 248 << 249 /** << 250 * anon_inode_getfd - creates a new file insta << 251 * an anonymous inode and a << 252 * the "class" of the file << 253 * << 254 * @name: [in] name of the "class" of th << 255 * @fops: [in] file operations for the n << 256 * @priv: [in] private data for the new << 257 * @flags: [in] flags << 258 * << 259 * Creates a new file by hooking it on a singl << 260 * useful for files that do not need to have a << 261 * order to operate correctly. All the files << 262 * anon_inode_getfd() will use the same single << 263 * memory use and avoiding code duplication fo << 264 * setup. Returns a newly created file descri << 265 */ << 266 int anon_inode_getfd(const char *name, const s << 267 void *priv, int flags) << 268 { << 269 return __anon_inode_getfd(name, fops, << 270 } << 271 EXPORT_SYMBOL_GPL(anon_inode_getfd); 149 EXPORT_SYMBOL_GPL(anon_inode_getfd); 272 << 273 /** << 274 * anon_inode_create_getfd - Like anon_inode_g << 275 * !S_PRIVATE anon inode rather than reuse the << 276 * the inode_init_security_anon() LSM hook. << 277 * << 278 * @name: [in] name of the "class" of th << 279 * @fops: [in] file operations for the n << 280 * @priv: [in] private data for the new << 281 * @flags: [in] flags << 282 * @context_inode: << 283 * [in] the logical relationship << 284 * << 285 * Create a new anonymous inode and file pair. << 286 * reasons: << 287 * << 288 * - for the inode to have its own security co << 289 * policy on the inode's creation; << 290 * << 291 * - if the caller needs a unique inode, for e << 292 * the size returned by fstat() << 293 * << 294 * The LSM may use @context_inode in inode_ini << 295 * reference to it is not held. << 296 * << 297 * Returns a newly created file descriptor or << 298 */ << 299 int anon_inode_create_getfd(const char *name, << 300 void *priv, int fl << 301 const struct inode << 302 { << 303 return __anon_inode_getfd(name, fops, << 304 } << 305 << 306 150 307 static int __init anon_inode_init(void) 151 static int __init anon_inode_init(void) 308 { 152 { 309 anon_inode_mnt = kern_mount(&anon_inod 153 anon_inode_mnt = kern_mount(&anon_inode_fs_type); 310 if (IS_ERR(anon_inode_mnt)) 154 if (IS_ERR(anon_inode_mnt)) 311 panic("anon_inode_init() kerne 155 panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt)); 312 156 313 anon_inode_inode = alloc_anon_inode(an 157 anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb); 314 if (IS_ERR(anon_inode_inode)) 158 if (IS_ERR(anon_inode_inode)) 315 panic("anon_inode_init() inode 159 panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode)); 316 160 317 return 0; 161 return 0; 318 } 162 } 319 163 320 fs_initcall(anon_inode_init); 164 fs_initcall(anon_inode_init); 321 165 322 166
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.