Back to home page

LXR

 
 

    


0001 /*
0002  *  inode.c - securityfs
0003  *
0004  *  Copyright (C) 2005 Greg Kroah-Hartman <gregkh@suse.de>
0005  *
0006  *  This program is free software; you can redistribute it and/or
0007  *  modify it under the terms of the GNU General Public License version
0008  *  2 as published by the Free Software Foundation.
0009  *
0010  *  Based on fs/debugfs/inode.c which had the following copyright notice:
0011  *    Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
0012  *    Copyright (C) 2004 IBM Inc.
0013  */
0014 
0015 /* #define DEBUG */
0016 #include <linux/module.h>
0017 #include <linux/fs.h>
0018 #include <linux/mount.h>
0019 #include <linux/pagemap.h>
0020 #include <linux/init.h>
0021 #include <linux/namei.h>
0022 #include <linux/security.h>
0023 #include <linux/magic.h>
0024 
0025 static struct vfsmount *mount;
0026 static int mount_count;
0027 
0028 static int fill_super(struct super_block *sb, void *data, int silent)
0029 {
0030     static struct tree_descr files[] = {{""}};
0031 
0032     return simple_fill_super(sb, SECURITYFS_MAGIC, files);
0033 }
0034 
0035 static struct dentry *get_sb(struct file_system_type *fs_type,
0036           int flags, const char *dev_name,
0037           void *data)
0038 {
0039     return mount_single(fs_type, flags, data, fill_super);
0040 }
0041 
0042 static struct file_system_type fs_type = {
0043     .owner =    THIS_MODULE,
0044     .name =     "securityfs",
0045     .mount =    get_sb,
0046     .kill_sb =  kill_litter_super,
0047 };
0048 
0049 /**
0050  * securityfs_create_file - create a file in the securityfs filesystem
0051  *
0052  * @name: a pointer to a string containing the name of the file to create.
0053  * @mode: the permission that the file should have
0054  * @parent: a pointer to the parent dentry for this file.  This should be a
0055  *          directory dentry if set.  If this parameter is %NULL, then the
0056  *          file will be created in the root of the securityfs filesystem.
0057  * @data: a pointer to something that the caller will want to get to later
0058  *        on.  The inode.i_private pointer will point to this value on
0059  *        the open() call.
0060  * @fops: a pointer to a struct file_operations that should be used for
0061  *        this file.
0062  *
0063  * This is the basic "create a file" function for securityfs.  It allows for a
0064  * wide range of flexibility in creating a file, or a directory (if you
0065  * want to create a directory, the securityfs_create_dir() function is
0066  * recommended to be used instead).
0067  *
0068  * This function returns a pointer to a dentry if it succeeds.  This
0069  * pointer must be passed to the securityfs_remove() function when the file is
0070  * to be removed (no automatic cleanup happens if your module is unloaded,
0071  * you are responsible here).  If an error occurs, the function will return
0072  * the error value (via ERR_PTR).
0073  *
0074  * If securityfs is not enabled in the kernel, the value %-ENODEV is
0075  * returned.
0076  */
0077 struct dentry *securityfs_create_file(const char *name, umode_t mode,
0078                    struct dentry *parent, void *data,
0079                    const struct file_operations *fops)
0080 {
0081     struct dentry *dentry;
0082     int is_dir = S_ISDIR(mode);
0083     struct inode *dir, *inode;
0084     int error;
0085 
0086     if (!is_dir) {
0087         BUG_ON(!fops);
0088         mode = (mode & S_IALLUGO) | S_IFREG;
0089     }
0090 
0091     pr_debug("securityfs: creating file '%s'\n",name);
0092 
0093     error = simple_pin_fs(&fs_type, &mount, &mount_count);
0094     if (error)
0095         return ERR_PTR(error);
0096 
0097     if (!parent)
0098         parent = mount->mnt_root;
0099 
0100     dir = d_inode(parent);
0101 
0102     inode_lock(dir);
0103     dentry = lookup_one_len(name, parent, strlen(name));
0104     if (IS_ERR(dentry))
0105         goto out;
0106 
0107     if (d_really_is_positive(dentry)) {
0108         error = -EEXIST;
0109         goto out1;
0110     }
0111 
0112     inode = new_inode(dir->i_sb);
0113     if (!inode) {
0114         error = -ENOMEM;
0115         goto out1;
0116     }
0117 
0118     inode->i_ino = get_next_ino();
0119     inode->i_mode = mode;
0120     inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0121     inode->i_private = data;
0122     if (is_dir) {
0123         inode->i_op = &simple_dir_inode_operations;
0124         inode->i_fop = &simple_dir_operations;
0125         inc_nlink(inode);
0126         inc_nlink(dir);
0127     } else {
0128         inode->i_fop = fops;
0129     }
0130     d_instantiate(dentry, inode);
0131     dget(dentry);
0132     inode_unlock(dir);
0133     return dentry;
0134 
0135 out1:
0136     dput(dentry);
0137     dentry = ERR_PTR(error);
0138 out:
0139     inode_unlock(dir);
0140     simple_release_fs(&mount, &mount_count);
0141     return dentry;
0142 }
0143 EXPORT_SYMBOL_GPL(securityfs_create_file);
0144 
0145 /**
0146  * securityfs_create_dir - create a directory in the securityfs filesystem
0147  *
0148  * @name: a pointer to a string containing the name of the directory to
0149  *        create.
0150  * @parent: a pointer to the parent dentry for this file.  This should be a
0151  *          directory dentry if set.  If this parameter is %NULL, then the
0152  *          directory will be created in the root of the securityfs filesystem.
0153  *
0154  * This function creates a directory in securityfs with the given @name.
0155  *
0156  * This function returns a pointer to a dentry if it succeeds.  This
0157  * pointer must be passed to the securityfs_remove() function when the file is
0158  * to be removed (no automatic cleanup happens if your module is unloaded,
0159  * you are responsible here).  If an error occurs, the function will return
0160  * the error value (via ERR_PTR).
0161  *
0162  * If securityfs is not enabled in the kernel, the value %-ENODEV is
0163  * returned.
0164  */
0165 struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
0166 {
0167     return securityfs_create_file(name,
0168                       S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
0169                       parent, NULL, NULL);
0170 }
0171 EXPORT_SYMBOL_GPL(securityfs_create_dir);
0172 
0173 /**
0174  * securityfs_remove - removes a file or directory from the securityfs filesystem
0175  *
0176  * @dentry: a pointer to a the dentry of the file or directory to be removed.
0177  *
0178  * This function removes a file or directory in securityfs that was previously
0179  * created with a call to another securityfs function (like
0180  * securityfs_create_file() or variants thereof.)
0181  *
0182  * This function is required to be called in order for the file to be
0183  * removed. No automatic cleanup of files will happen when a module is
0184  * removed; you are responsible here.
0185  */
0186 void securityfs_remove(struct dentry *dentry)
0187 {
0188     struct inode *dir;
0189 
0190     if (!dentry || IS_ERR(dentry))
0191         return;
0192 
0193     dir = d_inode(dentry->d_parent);
0194     inode_lock(dir);
0195     if (simple_positive(dentry)) {
0196         if (d_is_dir(dentry))
0197             simple_rmdir(dir, dentry);
0198         else
0199             simple_unlink(dir, dentry);
0200         dput(dentry);
0201     }
0202     inode_unlock(dir);
0203     simple_release_fs(&mount, &mount_count);
0204 }
0205 EXPORT_SYMBOL_GPL(securityfs_remove);
0206 
0207 static int __init securityfs_init(void)
0208 {
0209     int retval;
0210 
0211     retval = sysfs_create_mount_point(kernel_kobj, "security");
0212     if (retval)
0213         return retval;
0214 
0215     retval = register_filesystem(&fs_type);
0216     if (retval)
0217         sysfs_remove_mount_point(kernel_kobj, "security");
0218     return retval;
0219 }
0220 
0221 core_initcall(securityfs_init);
0222 MODULE_LICENSE("GPL");
0223