// SPDX-License-Identifier: GPL-2.0-only1/*2* fs/anon_inodes.c3*4* Copyright (C) 2007 Davide Libenzi <[email protected]>5*6* Thanks to Arnd Bergmann for code review and suggestions.7* More changes for Thomas Gleixner suggestions.8*9*/1011#include <linux/cred.h>12#include <linux/file.h>13#include <linux/poll.h>14#include <linux/sched.h>15#include <linux/init.h>16#include <linux/fs.h>17#include <linux/mount.h>18#include <linux/module.h>19#include <linux/kernel.h>20#include <linux/magic.h>21#include <linux/anon_inodes.h>22#include <linux/pseudo_fs.h>2324#include <linux/uaccess.h>2526#include "internal.h"2728static struct vfsmount *anon_inode_mnt __ro_after_init;29static struct inode *anon_inode_inode __ro_after_init;3031/*32* User space expects anonymous inodes to have no file type in st_mode.33*34* In particular, 'lsof' has this legacy logic:35*36* type = s->st_mode & S_IFMT;37* switch (type) {38* ...39* case 0:40* if (!strcmp(p, "anon_inode"))41* Lf->ntype = Ntype = N_ANON_INODE;42*43* to detect our old anon_inode logic.44*45* Rather than mess with our internal sane inode data, just fix it46* up here in getattr() by masking off the format bits.47*/48int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,49struct kstat *stat, u32 request_mask,50unsigned int query_flags)51{52struct inode *inode = d_inode(path->dentry);5354generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);55stat->mode &= ~S_IFMT;56return 0;57}5859int anon_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,60struct iattr *attr)61{62return -EOPNOTSUPP;63}6465static const struct inode_operations anon_inode_operations = {66.getattr = anon_inode_getattr,67.setattr = anon_inode_setattr,68};6970/*71* anon_inodefs_dname() is called from d_path().72*/73static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen)74{75return dynamic_dname(buffer, buflen, "anon_inode:%s",76dentry->d_name.name);77}7879static const struct dentry_operations anon_inodefs_dentry_operations = {80.d_dname = anon_inodefs_dname,81};8283static int anon_inodefs_init_fs_context(struct fs_context *fc)84{85struct pseudo_fs_context *ctx = init_pseudo(fc, ANON_INODE_FS_MAGIC);86if (!ctx)87return -ENOMEM;88fc->s_iflags |= SB_I_NOEXEC;89fc->s_iflags |= SB_I_NODEV;90ctx->dops = &anon_inodefs_dentry_operations;91return 0;92}9394static struct file_system_type anon_inode_fs_type = {95.name = "anon_inodefs",96.init_fs_context = anon_inodefs_init_fs_context,97.kill_sb = kill_anon_super,98};99100/**101* anon_inode_make_secure_inode - allocate an anonymous inode with security context102* @sb: [in] Superblock to allocate from103* @name: [in] Name of the class of the newfile (e.g., "secretmem")104* @context_inode:105* [in] Optional parent inode for security inheritance106*107* The function ensures proper security initialization through the LSM hook108* security_inode_init_security_anon().109*110* Return: Pointer to new inode on success, ERR_PTR on failure.111*/112struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,113const struct inode *context_inode)114{115struct inode *inode;116int error;117118inode = alloc_anon_inode(sb);119if (IS_ERR(inode))120return inode;121inode->i_flags &= ~S_PRIVATE;122inode->i_op = &anon_inode_operations;123error = security_inode_init_security_anon(inode, &QSTR(name),124context_inode);125if (error) {126iput(inode);127return ERR_PTR(error);128}129return inode;130}131EXPORT_SYMBOL_FOR_MODULES(anon_inode_make_secure_inode, "kvm");132133static struct file *__anon_inode_getfile(const char *name,134const struct file_operations *fops,135void *priv, int flags,136const struct inode *context_inode,137bool make_inode)138{139struct inode *inode;140struct file *file;141142if (fops->owner && !try_module_get(fops->owner))143return ERR_PTR(-ENOENT);144145if (make_inode) {146inode = anon_inode_make_secure_inode(anon_inode_mnt->mnt_sb,147name, context_inode);148if (IS_ERR(inode)) {149file = ERR_CAST(inode);150goto err;151}152} else {153inode = anon_inode_inode;154if (IS_ERR(inode)) {155file = ERR_PTR(-ENODEV);156goto err;157}158/*159* We know the anon_inode inode count is always160* greater than zero, so ihold() is safe.161*/162ihold(inode);163}164165file = alloc_file_pseudo(inode, anon_inode_mnt, name,166flags & (O_ACCMODE | O_NONBLOCK), fops);167if (IS_ERR(file))168goto err_iput;169170file->f_mapping = inode->i_mapping;171172file->private_data = priv;173174return file;175176err_iput:177iput(inode);178err:179module_put(fops->owner);180return file;181}182183/**184* anon_inode_getfile - creates a new file instance by hooking it up to an185* anonymous inode, and a dentry that describe the "class"186* of the file187*188* @name: [in] name of the "class" of the new file189* @fops: [in] file operations for the new file190* @priv: [in] private data for the new file (will be file's private_data)191* @flags: [in] flags192*193* Creates a new file by hooking it on a single inode. This is useful for files194* that do not need to have a full-fledged inode in order to operate correctly.195* All the files created with anon_inode_getfile() will share a single inode,196* hence saving memory and avoiding code duplication for the file/inode/dentry197* setup. Returns the newly created file* or an error pointer.198*/199struct file *anon_inode_getfile(const char *name,200const struct file_operations *fops,201void *priv, int flags)202{203return __anon_inode_getfile(name, fops, priv, flags, NULL, false);204}205EXPORT_SYMBOL_GPL(anon_inode_getfile);206207/**208* anon_inode_getfile_fmode - creates a new file instance by hooking it up to an209* anonymous inode, and a dentry that describe the "class"210* of the file211*212* @name: [in] name of the "class" of the new file213* @fops: [in] file operations for the new file214* @priv: [in] private data for the new file (will be file's private_data)215* @flags: [in] flags216* @f_mode: [in] fmode217*218* Creates a new file by hooking it on a single inode. This is useful for files219* that do not need to have a full-fledged inode in order to operate correctly.220* All the files created with anon_inode_getfile() will share a single inode,221* hence saving memory and avoiding code duplication for the file/inode/dentry222* setup. Allows setting the fmode. Returns the newly created file* or an error223* pointer.224*/225struct file *anon_inode_getfile_fmode(const char *name,226const struct file_operations *fops,227void *priv, int flags, fmode_t f_mode)228{229struct file *file;230231file = __anon_inode_getfile(name, fops, priv, flags, NULL, false);232if (!IS_ERR(file))233file->f_mode |= f_mode;234235return file;236}237EXPORT_SYMBOL_GPL(anon_inode_getfile_fmode);238239/**240* anon_inode_create_getfile - Like anon_inode_getfile(), but creates a new241* !S_PRIVATE anon inode rather than reuse the242* singleton anon inode and calls the243* inode_init_security_anon() LSM hook.244*245* @name: [in] name of the "class" of the new file246* @fops: [in] file operations for the new file247* @priv: [in] private data for the new file (will be file's private_data)248* @flags: [in] flags249* @context_inode:250* [in] the logical relationship with the new inode (optional)251*252* Create a new anonymous inode and file pair. This can be done for two253* reasons:254*255* - for the inode to have its own security context, so that LSMs can enforce256* policy on the inode's creation;257*258* - if the caller needs a unique inode, for example in order to customize259* the size returned by fstat()260*261* The LSM may use @context_inode in inode_init_security_anon(), but a262* reference to it is not held.263*264* Returns the newly created file* or an error pointer.265*/266struct file *anon_inode_create_getfile(const char *name,267const struct file_operations *fops,268void *priv, int flags,269const struct inode *context_inode)270{271return __anon_inode_getfile(name, fops, priv, flags,272context_inode, true);273}274EXPORT_SYMBOL_GPL(anon_inode_create_getfile);275276static int __anon_inode_getfd(const char *name,277const struct file_operations *fops,278void *priv, int flags,279const struct inode *context_inode,280bool make_inode)281{282int error, fd;283struct file *file;284285error = get_unused_fd_flags(flags);286if (error < 0)287return error;288fd = error;289290file = __anon_inode_getfile(name, fops, priv, flags, context_inode,291make_inode);292if (IS_ERR(file)) {293error = PTR_ERR(file);294goto err_put_unused_fd;295}296fd_install(fd, file);297298return fd;299300err_put_unused_fd:301put_unused_fd(fd);302return error;303}304305/**306* anon_inode_getfd - creates a new file instance by hooking it up to307* an anonymous inode and a dentry that describe308* the "class" of the file309*310* @name: [in] name of the "class" of the new file311* @fops: [in] file operations for the new file312* @priv: [in] private data for the new file (will be file's private_data)313* @flags: [in] flags314*315* Creates a new file by hooking it on a single inode. This is316* useful for files that do not need to have a full-fledged inode in317* order to operate correctly. All the files created with318* anon_inode_getfd() will use the same singleton inode, reducing319* memory use and avoiding code duplication for the file/inode/dentry320* setup. Returns a newly created file descriptor or an error code.321*/322int anon_inode_getfd(const char *name, const struct file_operations *fops,323void *priv, int flags)324{325return __anon_inode_getfd(name, fops, priv, flags, NULL, false);326}327EXPORT_SYMBOL_GPL(anon_inode_getfd);328329/**330* anon_inode_create_getfd - Like anon_inode_getfd(), but creates a new331* !S_PRIVATE anon inode rather than reuse the singleton anon inode, and calls332* the inode_init_security_anon() LSM hook.333*334* @name: [in] name of the "class" of the new file335* @fops: [in] file operations for the new file336* @priv: [in] private data for the new file (will be file's private_data)337* @flags: [in] flags338* @context_inode:339* [in] the logical relationship with the new inode (optional)340*341* Create a new anonymous inode and file pair. This can be done for two342* reasons:343*344* - for the inode to have its own security context, so that LSMs can enforce345* policy on the inode's creation;346*347* - if the caller needs a unique inode, for example in order to customize348* the size returned by fstat()349*350* The LSM may use @context_inode in inode_init_security_anon(), but a351* reference to it is not held.352*353* Returns a newly created file descriptor or an error code.354*/355int anon_inode_create_getfd(const char *name, const struct file_operations *fops,356void *priv, int flags,357const struct inode *context_inode)358{359return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);360}361362363static int __init anon_inode_init(void)364{365anon_inode_mnt = kern_mount(&anon_inode_fs_type);366if (IS_ERR(anon_inode_mnt))367panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));368369anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);370if (IS_ERR(anon_inode_inode))371panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));372anon_inode_inode->i_op = &anon_inode_operations;373374return 0;375}376377fs_initcall(anon_inode_init);378379380381