^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * inode.c - securityfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005 Greg Kroah-Hartman <gregkh@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on fs/debugfs/inode.c which had the following copyright notice:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2004 IBM Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /* #define DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/fs_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/lsm_hooks.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static struct vfsmount *mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int mount_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static void securityfs_free_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (S_ISLNK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) kfree(inode->i_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) free_inode_nonrcu(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static const struct super_operations securityfs_super_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) .statfs = simple_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .free_inode = securityfs_free_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int securityfs_fill_super(struct super_block *sb, struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static const struct tree_descr files[] = {{""}};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) error = simple_fill_super(sb, SECURITYFS_MAGIC, files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) sb->s_op = &securityfs_super_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int securityfs_get_tree(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return get_tree_single(fc, securityfs_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static const struct fs_context_operations securityfs_context_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .get_tree = securityfs_get_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int securityfs_init_fs_context(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) fc->ops = &securityfs_context_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct file_system_type fs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .name = "securityfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .init_fs_context = securityfs_init_fs_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .kill_sb = kill_litter_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * securityfs_create_dentry - create a dentry in the securityfs filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @name: a pointer to a string containing the name of the file to create.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @mode: the permission that the file should have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @parent: a pointer to the parent dentry for this file. This should be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * directory dentry if set. If this parameter is %NULL, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * file will be created in the root of the securityfs filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @data: a pointer to something that the caller will want to get to later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * on. The inode.i_private pointer will point to this value on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * the open() call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @fops: a pointer to a struct file_operations that should be used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * @iops: a point to a struct of inode_operations that should be used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * this file/dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * This is the basic "create a file/dir/symlink" function for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * securityfs. It allows for a wide range of flexibility in creating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * a file, or a directory (if you want to create a directory, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * securityfs_create_dir() function is recommended to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * instead).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * This function returns a pointer to a dentry if it succeeds. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * pointer must be passed to the securityfs_remove() function when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * file is to be removed (no automatic cleanup happens if your module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * is unloaded, you are responsible here). If an error occurs, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * function will return the error value (via ERR_PTR).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * If securityfs is not enabled in the kernel, the value %-ENODEV is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static struct dentry *securityfs_create_dentry(const char *name, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct dentry *parent, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) const struct file_operations *fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) const struct inode_operations *iops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct inode *dir, *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!(mode & S_IFMT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) mode = (mode & S_IALLUGO) | S_IFREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) pr_debug("securityfs: creating file '%s'\n",name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) error = simple_pin_fs(&fs_type, &mount, &mount_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) parent = mount->mnt_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dir = d_inode(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) inode_lock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dentry = lookup_one_len(name, parent, strlen(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (IS_ERR(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (d_really_is_positive(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) error = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) inode = new_inode(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) inode->i_ino = get_next_ino();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) inode->i_private = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (S_ISDIR(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) inode->i_op = &simple_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) inode->i_fop = &simple_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) inc_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) } else if (S_ISLNK(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) inode->i_op = iops ? iops : &simple_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) inode->i_link = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) inode->i_fop = fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) dget(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) dentry = ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) simple_release_fs(&mount, &mount_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * securityfs_create_file - create a file in the securityfs filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @name: a pointer to a string containing the name of the file to create.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @mode: the permission that the file should have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @parent: a pointer to the parent dentry for this file. This should be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * directory dentry if set. If this parameter is %NULL, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * file will be created in the root of the securityfs filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * @data: a pointer to something that the caller will want to get to later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * on. The inode.i_private pointer will point to this value on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * the open() call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @fops: a pointer to a struct file_operations that should be used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * This function creates a file in securityfs with the given @name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * This function returns a pointer to a dentry if it succeeds. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * pointer must be passed to the securityfs_remove() function when the file is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * to be removed (no automatic cleanup happens if your module is unloaded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * you are responsible here). If an error occurs, the function will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * the error value (via ERR_PTR).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * If securityfs is not enabled in the kernel, the value %-ENODEV is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct dentry *securityfs_create_file(const char *name, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct dentry *parent, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) const struct file_operations *fops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return securityfs_create_dentry(name, mode, parent, data, fops, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) EXPORT_SYMBOL_GPL(securityfs_create_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * securityfs_create_dir - create a directory in the securityfs filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * @name: a pointer to a string containing the name of the directory to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * create.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * @parent: a pointer to the parent dentry for this file. This should be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * directory dentry if set. If this parameter is %NULL, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * directory will be created in the root of the securityfs filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * This function creates a directory in securityfs with the given @name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * This function returns a pointer to a dentry if it succeeds. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * pointer must be passed to the securityfs_remove() function when the file is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * to be removed (no automatic cleanup happens if your module is unloaded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * you are responsible here). If an error occurs, the function will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * the error value (via ERR_PTR).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * If securityfs is not enabled in the kernel, the value %-ENODEV is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return securityfs_create_file(name, S_IFDIR | 0755, parent, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) EXPORT_SYMBOL_GPL(securityfs_create_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * securityfs_create_symlink - create a symlink in the securityfs filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * @name: a pointer to a string containing the name of the symlink to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * create.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * @parent: a pointer to the parent dentry for the symlink. This should be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * directory dentry if set. If this parameter is %NULL, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * directory will be created in the root of the securityfs filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * @target: a pointer to a string containing the name of the symlink's target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * If this parameter is %NULL, then the @iops parameter needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * setup to handle .readlink and .get_link inode_operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * @iops: a pointer to the struct inode_operations to use for the symlink. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * this parameter is %NULL, then the default simple_symlink_inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * operations will be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * This function creates a symlink in securityfs with the given @name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * This function returns a pointer to a dentry if it succeeds. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * pointer must be passed to the securityfs_remove() function when the file is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * to be removed (no automatic cleanup happens if your module is unloaded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * you are responsible here). If an error occurs, the function will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * the error value (via ERR_PTR).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * If securityfs is not enabled in the kernel, the value %-ENODEV is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct dentry *securityfs_create_symlink(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) const char *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) const struct inode_operations *iops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct dentry *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) char *link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) link = kstrdup(target, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) dent = securityfs_create_dentry(name, S_IFLNK | 0444, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) link, NULL, iops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (IS_ERR(dent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) kfree(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) EXPORT_SYMBOL_GPL(securityfs_create_symlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * securityfs_remove - removes a file or directory from the securityfs filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * @dentry: a pointer to a the dentry of the file or directory to be removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * This function removes a file or directory in securityfs that was previously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * created with a call to another securityfs function (like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * securityfs_create_file() or variants thereof.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * This function is required to be called in order for the file to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * removed. No automatic cleanup of files will happen when a module is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * removed; you are responsible here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) void securityfs_remove(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct inode *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!dentry || IS_ERR(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) dir = d_inode(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) inode_lock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (simple_positive(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (d_is_dir(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) simple_rmdir(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) simple_unlink(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) simple_release_fs(&mount, &mount_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) EXPORT_SYMBOL_GPL(securityfs_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) #ifdef CONFIG_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static struct dentry *lsm_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return simple_read_from_buffer(buf, count, ppos, lsm_names,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) strlen(lsm_names));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static const struct file_operations lsm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .read = lsm_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static int __init securityfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) retval = sysfs_create_mount_point(kernel_kobj, "security");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) retval = register_filesystem(&fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) sysfs_remove_mount_point(kernel_kobj, "security");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) #ifdef CONFIG_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) lsm_dentry = securityfs_create_file("lsm", 0444, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) &lsm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) core_initcall(securityfs_init);