// SPDX-License-Identifier: GPL-2.0-only1/*2* inode.c - securityfs3*4* Copyright (C) 2005 Greg Kroah-Hartman <[email protected]>5*6* Based on fs/debugfs/inode.c which had the following copyright notice:7* Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>8* Copyright (C) 2004 IBM Inc.9*/1011/* #define DEBUG */12#include <linux/sysfs.h>13#include <linux/kobject.h>14#include <linux/fs.h>15#include <linux/fs_context.h>16#include <linux/mount.h>17#include <linux/pagemap.h>18#include <linux/init.h>19#include <linux/namei.h>20#include <linux/security.h>21#include <linux/lsm_hooks.h>22#include <linux/magic.h>2324static struct vfsmount *mount;25static int mount_count;2627static void securityfs_free_inode(struct inode *inode)28{29if (S_ISLNK(inode->i_mode))30kfree(inode->i_link);31free_inode_nonrcu(inode);32}3334static const struct super_operations securityfs_super_operations = {35.statfs = simple_statfs,36.free_inode = securityfs_free_inode,37};3839static int securityfs_fill_super(struct super_block *sb, struct fs_context *fc)40{41static const struct tree_descr files[] = {{""}};42int error;4344error = simple_fill_super(sb, SECURITYFS_MAGIC, files);45if (error)46return error;4748sb->s_op = &securityfs_super_operations;4950return 0;51}5253static int securityfs_get_tree(struct fs_context *fc)54{55return get_tree_single(fc, securityfs_fill_super);56}5758static const struct fs_context_operations securityfs_context_ops = {59.get_tree = securityfs_get_tree,60};6162static int securityfs_init_fs_context(struct fs_context *fc)63{64fc->ops = &securityfs_context_ops;65return 0;66}6768static struct file_system_type fs_type = {69.owner = THIS_MODULE,70.name = "securityfs",71.init_fs_context = securityfs_init_fs_context,72.kill_sb = kill_litter_super,73};7475/**76* securityfs_create_dentry - create a dentry in the securityfs filesystem77*78* @name: a pointer to a string containing the name of the file to create.79* @mode: the permission that the file should have80* @parent: a pointer to the parent dentry for this file. This should be a81* directory dentry if set. If this parameter is %NULL, then the82* file will be created in the root of the securityfs filesystem.83* @data: a pointer to something that the caller will want to get to later84* on. The inode.i_private pointer will point to this value on85* the open() call.86* @fops: a pointer to a struct file_operations that should be used for87* this file.88* @iops: a point to a struct of inode_operations that should be used for89* this file/dir90*91* This is the basic "create a file/dir/symlink" function for92* securityfs. It allows for a wide range of flexibility in creating93* a file, or a directory (if you want to create a directory, the94* securityfs_create_dir() function is recommended to be used95* instead).96*97* This function returns a pointer to a dentry if it succeeds. This98* pointer must be passed to the securityfs_remove() function when the99* file is to be removed (no automatic cleanup happens if your module100* is unloaded, you are responsible here). If an error occurs, the101* function will return the error value (via ERR_PTR).102*103* If securityfs is not enabled in the kernel, the value %-ENODEV is104* returned.105*/106static struct dentry *securityfs_create_dentry(const char *name, umode_t mode,107struct dentry *parent, void *data,108const struct file_operations *fops,109const struct inode_operations *iops)110{111struct dentry *dentry;112struct inode *dir, *inode;113int error;114bool pinned = false;115116if (!(mode & S_IFMT))117mode = (mode & S_IALLUGO) | S_IFREG;118119pr_debug("securityfs: creating file '%s'\n",name);120121if (!parent) {122error = simple_pin_fs(&fs_type, &mount, &mount_count);123if (error)124return ERR_PTR(error);125pinned = true;126parent = mount->mnt_root;127}128129dir = d_inode(parent);130131inode_lock(dir);132dentry = lookup_noperm(&QSTR(name), parent);133if (IS_ERR(dentry))134goto out;135136if (d_really_is_positive(dentry)) {137error = -EEXIST;138goto out1;139}140141inode = new_inode(dir->i_sb);142if (!inode) {143error = -ENOMEM;144goto out1;145}146147inode->i_ino = get_next_ino();148inode->i_mode = mode;149simple_inode_init_ts(inode);150inode->i_private = data;151if (S_ISDIR(mode)) {152inode->i_op = &simple_dir_inode_operations;153inode->i_fop = &simple_dir_operations;154inc_nlink(inode);155inc_nlink(dir);156} else if (S_ISLNK(mode)) {157inode->i_op = iops ? iops : &simple_symlink_inode_operations;158inode->i_link = data;159} else {160inode->i_fop = fops;161}162d_instantiate(dentry, inode);163inode_unlock(dir);164return dentry;165166out1:167dput(dentry);168dentry = ERR_PTR(error);169out:170inode_unlock(dir);171if (pinned)172simple_release_fs(&mount, &mount_count);173return dentry;174}175176/**177* securityfs_create_file - create a file in the securityfs filesystem178*179* @name: a pointer to a string containing the name of the file to create.180* @mode: the permission that the file should have181* @parent: a pointer to the parent dentry for this file. This should be a182* directory dentry if set. If this parameter is %NULL, then the183* file will be created in the root of the securityfs filesystem.184* @data: a pointer to something that the caller will want to get to later185* on. The inode.i_private pointer will point to this value on186* the open() call.187* @fops: a pointer to a struct file_operations that should be used for188* this file.189*190* This function creates a file in securityfs with the given @name.191*192* This function returns a pointer to a dentry if it succeeds. This193* pointer must be passed to the securityfs_remove() function when the file is194* to be removed (no automatic cleanup happens if your module is unloaded,195* you are responsible here). If an error occurs, the function will return196* the error value (via ERR_PTR).197*198* If securityfs is not enabled in the kernel, the value %-ENODEV is199* returned.200*/201struct dentry *securityfs_create_file(const char *name, umode_t mode,202struct dentry *parent, void *data,203const struct file_operations *fops)204{205return securityfs_create_dentry(name, mode, parent, data, fops, NULL);206}207EXPORT_SYMBOL_GPL(securityfs_create_file);208209/**210* securityfs_create_dir - create a directory in the securityfs filesystem211*212* @name: a pointer to a string containing the name of the directory to213* create.214* @parent: a pointer to the parent dentry for this file. This should be a215* directory dentry if set. If this parameter is %NULL, then the216* directory will be created in the root of the securityfs filesystem.217*218* This function creates a directory in securityfs with the given @name.219*220* This function returns a pointer to a dentry if it succeeds. This221* pointer must be passed to the securityfs_remove() function when the file is222* to be removed (no automatic cleanup happens if your module is unloaded,223* you are responsible here). If an error occurs, the function will return224* the error value (via ERR_PTR).225*226* If securityfs is not enabled in the kernel, the value %-ENODEV is227* returned.228*/229struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)230{231return securityfs_create_file(name, S_IFDIR | 0755, parent, NULL, NULL);232}233EXPORT_SYMBOL_GPL(securityfs_create_dir);234235/**236* securityfs_create_symlink - create a symlink in the securityfs filesystem237*238* @name: a pointer to a string containing the name of the symlink to239* create.240* @parent: a pointer to the parent dentry for the symlink. This should be a241* directory dentry if set. If this parameter is %NULL, then the242* directory will be created in the root of the securityfs filesystem.243* @target: a pointer to a string containing the name of the symlink's target.244* If this parameter is %NULL, then the @iops parameter needs to be245* setup to handle .readlink and .get_link inode_operations.246* @iops: a pointer to the struct inode_operations to use for the symlink. If247* this parameter is %NULL, then the default simple_symlink_inode248* operations will be used.249*250* This function creates a symlink in securityfs with the given @name.251*252* This function returns a pointer to a dentry if it succeeds. This253* pointer must be passed to the securityfs_remove() function when the file is254* to be removed (no automatic cleanup happens if your module is unloaded,255* you are responsible here). If an error occurs, the function will return256* the error value (via ERR_PTR).257*258* If securityfs is not enabled in the kernel, the value %-ENODEV is259* returned.260*/261struct dentry *securityfs_create_symlink(const char *name,262struct dentry *parent,263const char *target,264const struct inode_operations *iops)265{266struct dentry *dent;267char *link = NULL;268269if (target) {270link = kstrdup(target, GFP_KERNEL);271if (!link)272return ERR_PTR(-ENOMEM);273}274dent = securityfs_create_dentry(name, S_IFLNK | 0444, parent,275link, NULL, iops);276if (IS_ERR(dent))277kfree(link);278279return dent;280}281EXPORT_SYMBOL_GPL(securityfs_create_symlink);282283static void remove_one(struct dentry *victim)284{285if (victim->d_parent == victim->d_sb->s_root)286simple_release_fs(&mount, &mount_count);287}288289/**290* securityfs_remove - removes a file or directory from the securityfs filesystem291*292* @dentry: a pointer to a the dentry of the file or directory to be removed.293*294* This function removes a file or directory in securityfs that was previously295* created with a call to another securityfs function (like296* securityfs_create_file() or variants thereof.)297*298* This function is required to be called in order for the file to be299* removed. No automatic cleanup of files will happen when a module is300* removed; you are responsible here.301*302* AV: when applied to directory it will take all children out; no need to call303* it for descendents if ancestor is getting killed.304*/305void securityfs_remove(struct dentry *dentry)306{307if (IS_ERR_OR_NULL(dentry))308return;309310simple_pin_fs(&fs_type, &mount, &mount_count);311simple_recursive_removal(dentry, remove_one);312simple_release_fs(&mount, &mount_count);313}314EXPORT_SYMBOL_GPL(securityfs_remove);315316#ifdef CONFIG_SECURITY317static struct dentry *lsm_dentry;318static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,319loff_t *ppos)320{321return simple_read_from_buffer(buf, count, ppos, lsm_names,322strlen(lsm_names));323}324325static const struct file_operations lsm_ops = {326.read = lsm_read,327.llseek = generic_file_llseek,328};329#endif330331static int __init securityfs_init(void)332{333int retval;334335retval = sysfs_create_mount_point(kernel_kobj, "security");336if (retval)337return retval;338339retval = register_filesystem(&fs_type);340if (retval) {341sysfs_remove_mount_point(kernel_kobj, "security");342return retval;343}344#ifdef CONFIG_SECURITY345lsm_dentry = securityfs_create_file("lsm", 0444, NULL, NULL,346&lsm_ops);347#endif348return 0;349}350core_initcall(securityfs_init);351352353