xref: /openbmc/linux/security/inode.c (revision 428c33f2)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b67dbf9dSGreg KH /*
3b67dbf9dSGreg KH  *  inode.c - securityfs
4b67dbf9dSGreg KH  *
5b67dbf9dSGreg KH  *  Copyright (C) 2005 Greg Kroah-Hartman <gregkh@suse.de>
6b67dbf9dSGreg KH  *
7b67dbf9dSGreg KH  *  Based on fs/debugfs/inode.c which had the following copyright notice:
8b67dbf9dSGreg KH  *    Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
9b67dbf9dSGreg KH  *    Copyright (C) 2004 IBM Inc.
10b67dbf9dSGreg KH  */
11b67dbf9dSGreg KH 
12b67dbf9dSGreg KH /* #define DEBUG */
131072bd67SPaul Gortmaker #include <linux/sysfs.h>
141072bd67SPaul Gortmaker #include <linux/kobject.h>
15b67dbf9dSGreg KH #include <linux/fs.h>
165c86d7e0SDavid Howells #include <linux/fs_context.h>
17b67dbf9dSGreg KH #include <linux/mount.h>
18b67dbf9dSGreg KH #include <linux/pagemap.h>
19b67dbf9dSGreg KH #include <linux/init.h>
20b67dbf9dSGreg KH #include <linux/namei.h>
21b67dbf9dSGreg KH #include <linux/security.h>
22d69dece5SCasey Schaufler #include <linux/lsm_hooks.h>
2392562927SMimi Zohar #include <linux/magic.h>
24b67dbf9dSGreg KH 
25b67dbf9dSGreg KH static struct vfsmount *mount;
26b67dbf9dSGreg KH static int mount_count;
27b67dbf9dSGreg KH 
securityfs_free_inode(struct inode * inode)28f614ee1eSAl Viro static void securityfs_free_inode(struct inode *inode)
296623ec7cSJohn Johansen {
306623ec7cSJohn Johansen 	if (S_ISLNK(inode->i_mode))
316623ec7cSJohn Johansen 		kfree(inode->i_link);
3246c87441SAl Viro 	free_inode_nonrcu(inode);
3346c87441SAl Viro }
3446c87441SAl Viro 
356623ec7cSJohn Johansen static const struct super_operations securityfs_super_operations = {
366623ec7cSJohn Johansen 	.statfs		= simple_statfs,
37f614ee1eSAl Viro 	.free_inode	= securityfs_free_inode,
386623ec7cSJohn Johansen };
396623ec7cSJohn Johansen 
securityfs_fill_super(struct super_block * sb,struct fs_context * fc)405c86d7e0SDavid Howells static int securityfs_fill_super(struct super_block *sb, struct fs_context *fc)
41b67dbf9dSGreg KH {
42cda37124SEric Biggers 	static const struct tree_descr files[] = {{""}};
436623ec7cSJohn Johansen 	int error;
44b67dbf9dSGreg KH 
456623ec7cSJohn Johansen 	error = simple_fill_super(sb, SECURITYFS_MAGIC, files);
466623ec7cSJohn Johansen 	if (error)
476623ec7cSJohn Johansen 		return error;
486623ec7cSJohn Johansen 
496623ec7cSJohn Johansen 	sb->s_op = &securityfs_super_operations;
506623ec7cSJohn Johansen 
516623ec7cSJohn Johansen 	return 0;
52b67dbf9dSGreg KH }
53b67dbf9dSGreg KH 
securityfs_get_tree(struct fs_context * fc)545c86d7e0SDavid Howells static int securityfs_get_tree(struct fs_context *fc)
55b67dbf9dSGreg KH {
565c86d7e0SDavid Howells 	return get_tree_single(fc, securityfs_fill_super);
575c86d7e0SDavid Howells }
585c86d7e0SDavid Howells 
595c86d7e0SDavid Howells static const struct fs_context_operations securityfs_context_ops = {
605c86d7e0SDavid Howells 	.get_tree	= securityfs_get_tree,
615c86d7e0SDavid Howells };
625c86d7e0SDavid Howells 
securityfs_init_fs_context(struct fs_context * fc)635c86d7e0SDavid Howells static int securityfs_init_fs_context(struct fs_context *fc)
645c86d7e0SDavid Howells {
655c86d7e0SDavid Howells 	fc->ops = &securityfs_context_ops;
665c86d7e0SDavid Howells 	return 0;
67b67dbf9dSGreg KH }
68b67dbf9dSGreg KH 
69b67dbf9dSGreg KH static struct file_system_type fs_type = {
70b67dbf9dSGreg KH 	.owner =	THIS_MODULE,
71b67dbf9dSGreg KH 	.name =		"securityfs",
725c86d7e0SDavid Howells 	.init_fs_context = securityfs_init_fs_context,
73b67dbf9dSGreg KH 	.kill_sb =	kill_litter_super,
74b67dbf9dSGreg KH };
75b67dbf9dSGreg KH 
76b67dbf9dSGreg KH /**
776623ec7cSJohn Johansen  * securityfs_create_dentry - create a dentry in the securityfs filesystem
78b67dbf9dSGreg KH  *
79b67dbf9dSGreg KH  * @name: a pointer to a string containing the name of the file to create.
80b67dbf9dSGreg KH  * @mode: the permission that the file should have
81b67dbf9dSGreg KH  * @parent: a pointer to the parent dentry for this file.  This should be a
823f23d815SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
83b67dbf9dSGreg KH  *          file will be created in the root of the securityfs filesystem.
84b67dbf9dSGreg KH  * @data: a pointer to something that the caller will want to get to later
858e18e294STheodore Ts'o  *        on.  The inode.i_private pointer will point to this value on
86b67dbf9dSGreg KH  *        the open() call.
87b67dbf9dSGreg KH  * @fops: a pointer to a struct file_operations that should be used for
88b67dbf9dSGreg KH  *        this file.
896623ec7cSJohn Johansen  * @iops: a point to a struct of inode_operations that should be used for
906623ec7cSJohn Johansen  *        this file/dir
91b67dbf9dSGreg KH  *
926623ec7cSJohn Johansen  * This is the basic "create a file/dir/symlink" function for
936623ec7cSJohn Johansen  * securityfs.  It allows for a wide range of flexibility in creating
946623ec7cSJohn Johansen  * a file, or a directory (if you want to create a directory, the
956623ec7cSJohn Johansen  * securityfs_create_dir() function is recommended to be used
966623ec7cSJohn Johansen  * instead).
97b67dbf9dSGreg KH  *
983f23d815SRandy Dunlap  * This function returns a pointer to a dentry if it succeeds.  This
996623ec7cSJohn Johansen  * pointer must be passed to the securityfs_remove() function when the
1006623ec7cSJohn Johansen  * file is to be removed (no automatic cleanup happens if your module
1016623ec7cSJohn Johansen  * is unloaded, you are responsible here).  If an error occurs, the
1026623ec7cSJohn Johansen  * function will return the error value (via ERR_PTR).
103b67dbf9dSGreg KH  *
1043f23d815SRandy Dunlap  * If securityfs is not enabled in the kernel, the value %-ENODEV is
105faa3aad7SSerge E. Hallyn  * returned.
106b67dbf9dSGreg KH  */
securityfs_create_dentry(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops,const struct inode_operations * iops)1076623ec7cSJohn Johansen static struct dentry *securityfs_create_dentry(const char *name, umode_t mode,
108b67dbf9dSGreg KH 					struct dentry *parent, void *data,
1096623ec7cSJohn Johansen 					const struct file_operations *fops,
1106623ec7cSJohn Johansen 					const struct inode_operations *iops)
111b67dbf9dSGreg KH {
1123e25eb9cSAl Viro 	struct dentry *dentry;
1133e25eb9cSAl Viro 	struct inode *dir, *inode;
114b67dbf9dSGreg KH 	int error;
115b67dbf9dSGreg KH 
1166623ec7cSJohn Johansen 	if (!(mode & S_IFMT))
1173e25eb9cSAl Viro 		mode = (mode & S_IALLUGO) | S_IFREG;
1183e25eb9cSAl Viro 
119b67dbf9dSGreg KH 	pr_debug("securityfs: creating file '%s'\n",name);
120b67dbf9dSGreg KH 
1211f5ce9e9STrond Myklebust 	error = simple_pin_fs(&fs_type, &mount, &mount_count);
1223e25eb9cSAl Viro 	if (error)
1233e25eb9cSAl Viro 		return ERR_PTR(error);
124b67dbf9dSGreg KH 
1253e25eb9cSAl Viro 	if (!parent)
1263e25eb9cSAl Viro 		parent = mount->mnt_root;
1273e25eb9cSAl Viro 
128ce0b16ddSDavid Howells 	dir = d_inode(parent);
1293e25eb9cSAl Viro 
1305955102cSAl Viro 	inode_lock(dir);
1313e25eb9cSAl Viro 	dentry = lookup_one_len(name, parent, strlen(name));
1323e25eb9cSAl Viro 	if (IS_ERR(dentry))
1333e25eb9cSAl Viro 		goto out;
134b67dbf9dSGreg KH 
135ce0b16ddSDavid Howells 	if (d_really_is_positive(dentry)) {
1363e25eb9cSAl Viro 		error = -EEXIST;
1373e25eb9cSAl Viro 		goto out1;
138b67dbf9dSGreg KH 	}
1393e25eb9cSAl Viro 
1403e25eb9cSAl Viro 	inode = new_inode(dir->i_sb);
1413e25eb9cSAl Viro 	if (!inode) {
1423e25eb9cSAl Viro 		error = -ENOMEM;
1433e25eb9cSAl Viro 		goto out1;
1443e25eb9cSAl Viro 	}
1453e25eb9cSAl Viro 
1463e25eb9cSAl Viro 	inode->i_ino = get_next_ino();
1473e25eb9cSAl Viro 	inode->i_mode = mode;
148*428c33f2SJeff Layton 	inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
1493e25eb9cSAl Viro 	inode->i_private = data;
1506623ec7cSJohn Johansen 	if (S_ISDIR(mode)) {
1513e25eb9cSAl Viro 		inode->i_op = &simple_dir_inode_operations;
1523e25eb9cSAl Viro 		inode->i_fop = &simple_dir_operations;
1533e25eb9cSAl Viro 		inc_nlink(inode);
1543e25eb9cSAl Viro 		inc_nlink(dir);
1556623ec7cSJohn Johansen 	} else if (S_ISLNK(mode)) {
1566623ec7cSJohn Johansen 		inode->i_op = iops ? iops : &simple_symlink_inode_operations;
1576623ec7cSJohn Johansen 		inode->i_link = data;
1583e25eb9cSAl Viro 	} else {
1593e25eb9cSAl Viro 		inode->i_fop = fops;
1603e25eb9cSAl Viro 	}
1613e25eb9cSAl Viro 	d_instantiate(dentry, inode);
1623e25eb9cSAl Viro 	dget(dentry);
1635955102cSAl Viro 	inode_unlock(dir);
1643e25eb9cSAl Viro 	return dentry;
1653e25eb9cSAl Viro 
1663e25eb9cSAl Viro out1:
1673e25eb9cSAl Viro 	dput(dentry);
1683e25eb9cSAl Viro 	dentry = ERR_PTR(error);
1693e25eb9cSAl Viro out:
1705955102cSAl Viro 	inode_unlock(dir);
1713e25eb9cSAl Viro 	simple_release_fs(&mount, &mount_count);
172b67dbf9dSGreg KH 	return dentry;
173b67dbf9dSGreg KH }
1746623ec7cSJohn Johansen 
1756623ec7cSJohn Johansen /**
1766623ec7cSJohn Johansen  * securityfs_create_file - create a file in the securityfs filesystem
1776623ec7cSJohn Johansen  *
1786623ec7cSJohn Johansen  * @name: a pointer to a string containing the name of the file to create.
1796623ec7cSJohn Johansen  * @mode: the permission that the file should have
1806623ec7cSJohn Johansen  * @parent: a pointer to the parent dentry for this file.  This should be a
1816623ec7cSJohn Johansen  *          directory dentry if set.  If this parameter is %NULL, then the
1826623ec7cSJohn Johansen  *          file will be created in the root of the securityfs filesystem.
1836623ec7cSJohn Johansen  * @data: a pointer to something that the caller will want to get to later
1846623ec7cSJohn Johansen  *        on.  The inode.i_private pointer will point to this value on
1856623ec7cSJohn Johansen  *        the open() call.
1866623ec7cSJohn Johansen  * @fops: a pointer to a struct file_operations that should be used for
1876623ec7cSJohn Johansen  *        this file.
1886623ec7cSJohn Johansen  *
1896623ec7cSJohn Johansen  * This function creates a file in securityfs with the given @name.
1906623ec7cSJohn Johansen  *
1916623ec7cSJohn Johansen  * This function returns a pointer to a dentry if it succeeds.  This
1926623ec7cSJohn Johansen  * pointer must be passed to the securityfs_remove() function when the file is
1936623ec7cSJohn Johansen  * to be removed (no automatic cleanup happens if your module is unloaded,
1946623ec7cSJohn Johansen  * you are responsible here).  If an error occurs, the function will return
1956623ec7cSJohn Johansen  * the error value (via ERR_PTR).
1966623ec7cSJohn Johansen  *
1976623ec7cSJohn Johansen  * If securityfs is not enabled in the kernel, the value %-ENODEV is
1986623ec7cSJohn Johansen  * returned.
1996623ec7cSJohn Johansen  */
securityfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)2006623ec7cSJohn Johansen struct dentry *securityfs_create_file(const char *name, umode_t mode,
2016623ec7cSJohn Johansen 				      struct dentry *parent, void *data,
2026623ec7cSJohn Johansen 				      const struct file_operations *fops)
2036623ec7cSJohn Johansen {
2046623ec7cSJohn Johansen 	return securityfs_create_dentry(name, mode, parent, data, fops, NULL);
2056623ec7cSJohn Johansen }
206b67dbf9dSGreg KH EXPORT_SYMBOL_GPL(securityfs_create_file);
207b67dbf9dSGreg KH 
208b67dbf9dSGreg KH /**
209b67dbf9dSGreg KH  * securityfs_create_dir - create a directory in the securityfs filesystem
210b67dbf9dSGreg KH  *
211b67dbf9dSGreg KH  * @name: a pointer to a string containing the name of the directory to
212b67dbf9dSGreg KH  *        create.
213b67dbf9dSGreg KH  * @parent: a pointer to the parent dentry for this file.  This should be a
2143f23d815SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
215b67dbf9dSGreg KH  *          directory will be created in the root of the securityfs filesystem.
216b67dbf9dSGreg KH  *
2173f23d815SRandy Dunlap  * This function creates a directory in securityfs with the given @name.
218b67dbf9dSGreg KH  *
2193f23d815SRandy Dunlap  * This function returns a pointer to a dentry if it succeeds.  This
220b67dbf9dSGreg KH  * pointer must be passed to the securityfs_remove() function when the file is
221b67dbf9dSGreg KH  * to be removed (no automatic cleanup happens if your module is unloaded,
2221b460651SLaurent Georget  * you are responsible here).  If an error occurs, the function will return
2231b460651SLaurent Georget  * the error value (via ERR_PTR).
224b67dbf9dSGreg KH  *
2253f23d815SRandy Dunlap  * If securityfs is not enabled in the kernel, the value %-ENODEV is
2261b460651SLaurent Georget  * returned.
227b67dbf9dSGreg KH  */
securityfs_create_dir(const char * name,struct dentry * parent)228b67dbf9dSGreg KH struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
229b67dbf9dSGreg KH {
2306623ec7cSJohn Johansen 	return securityfs_create_file(name, S_IFDIR | 0755, parent, NULL, NULL);
231b67dbf9dSGreg KH }
232b67dbf9dSGreg KH EXPORT_SYMBOL_GPL(securityfs_create_dir);
233b67dbf9dSGreg KH 
234b67dbf9dSGreg KH /**
2356623ec7cSJohn Johansen  * securityfs_create_symlink - create a symlink in the securityfs filesystem
2366623ec7cSJohn Johansen  *
2376623ec7cSJohn Johansen  * @name: a pointer to a string containing the name of the symlink to
2386623ec7cSJohn Johansen  *        create.
2396623ec7cSJohn Johansen  * @parent: a pointer to the parent dentry for the symlink.  This should be a
2406623ec7cSJohn Johansen  *          directory dentry if set.  If this parameter is %NULL, then the
2416623ec7cSJohn Johansen  *          directory will be created in the root of the securityfs filesystem.
2426623ec7cSJohn Johansen  * @target: a pointer to a string containing the name of the symlink's target.
2436623ec7cSJohn Johansen  *          If this parameter is %NULL, then the @iops parameter needs to be
2446623ec7cSJohn Johansen  *          setup to handle .readlink and .get_link inode_operations.
2456623ec7cSJohn Johansen  * @iops: a pointer to the struct inode_operations to use for the symlink. If
2466623ec7cSJohn Johansen  *        this parameter is %NULL, then the default simple_symlink_inode
2476623ec7cSJohn Johansen  *        operations will be used.
2486623ec7cSJohn Johansen  *
2496623ec7cSJohn Johansen  * This function creates a symlink in securityfs with the given @name.
2506623ec7cSJohn Johansen  *
2516623ec7cSJohn Johansen  * This function returns a pointer to a dentry if it succeeds.  This
2526623ec7cSJohn Johansen  * pointer must be passed to the securityfs_remove() function when the file is
2536623ec7cSJohn Johansen  * to be removed (no automatic cleanup happens if your module is unloaded,
2546623ec7cSJohn Johansen  * you are responsible here).  If an error occurs, the function will return
2556623ec7cSJohn Johansen  * the error value (via ERR_PTR).
2566623ec7cSJohn Johansen  *
2576623ec7cSJohn Johansen  * If securityfs is not enabled in the kernel, the value %-ENODEV is
2586623ec7cSJohn Johansen  * returned.
2596623ec7cSJohn Johansen  */
securityfs_create_symlink(const char * name,struct dentry * parent,const char * target,const struct inode_operations * iops)2606623ec7cSJohn Johansen struct dentry *securityfs_create_symlink(const char *name,
2616623ec7cSJohn Johansen 					 struct dentry *parent,
2626623ec7cSJohn Johansen 					 const char *target,
2636623ec7cSJohn Johansen 					 const struct inode_operations *iops)
2646623ec7cSJohn Johansen {
2656623ec7cSJohn Johansen 	struct dentry *dent;
2666623ec7cSJohn Johansen 	char *link = NULL;
2676623ec7cSJohn Johansen 
2686623ec7cSJohn Johansen 	if (target) {
2696623ec7cSJohn Johansen 		link = kstrdup(target, GFP_KERNEL);
2706623ec7cSJohn Johansen 		if (!link)
2716623ec7cSJohn Johansen 			return ERR_PTR(-ENOMEM);
2726623ec7cSJohn Johansen 	}
2736623ec7cSJohn Johansen 	dent = securityfs_create_dentry(name, S_IFLNK | 0444, parent,
2746623ec7cSJohn Johansen 					link, NULL, iops);
2756623ec7cSJohn Johansen 	if (IS_ERR(dent))
2766623ec7cSJohn Johansen 		kfree(link);
2776623ec7cSJohn Johansen 
2786623ec7cSJohn Johansen 	return dent;
2796623ec7cSJohn Johansen }
2806623ec7cSJohn Johansen EXPORT_SYMBOL_GPL(securityfs_create_symlink);
2816623ec7cSJohn Johansen 
2826623ec7cSJohn Johansen /**
283b67dbf9dSGreg KH  * securityfs_remove - removes a file or directory from the securityfs filesystem
284b67dbf9dSGreg KH  *
2853f23d815SRandy Dunlap  * @dentry: a pointer to a the dentry of the file or directory to be removed.
286b67dbf9dSGreg KH  *
287b67dbf9dSGreg KH  * This function removes a file or directory in securityfs that was previously
288b67dbf9dSGreg KH  * created with a call to another securityfs function (like
289b67dbf9dSGreg KH  * securityfs_create_file() or variants thereof.)
290b67dbf9dSGreg KH  *
291b67dbf9dSGreg KH  * This function is required to be called in order for the file to be
2923f23d815SRandy Dunlap  * removed. No automatic cleanup of files will happen when a module is
2933f23d815SRandy Dunlap  * removed; you are responsible here.
294b67dbf9dSGreg KH  */
securityfs_remove(struct dentry * dentry)295b67dbf9dSGreg KH void securityfs_remove(struct dentry *dentry)
296b67dbf9dSGreg KH {
2974093d306SAl Viro 	struct inode *dir;
298b67dbf9dSGreg KH 
299d93e4c94SEric Paris 	if (!dentry || IS_ERR(dentry))
300b67dbf9dSGreg KH 		return;
301b67dbf9dSGreg KH 
3024093d306SAl Viro 	dir = d_inode(dentry->d_parent);
3034093d306SAl Viro 	inode_lock(dir);
304dc3f4198SAl Viro 	if (simple_positive(dentry)) {
305e36cb0b8SDavid Howells 		if (d_is_dir(dentry))
3064093d306SAl Viro 			simple_rmdir(dir, dentry);
307b67dbf9dSGreg KH 		else
3084093d306SAl Viro 			simple_unlink(dir, dentry);
309b67dbf9dSGreg KH 		dput(dentry);
310b67dbf9dSGreg KH 	}
3114093d306SAl Viro 	inode_unlock(dir);
312b67dbf9dSGreg KH 	simple_release_fs(&mount, &mount_count);
313b67dbf9dSGreg KH }
314b67dbf9dSGreg KH EXPORT_SYMBOL_GPL(securityfs_remove);
315b67dbf9dSGreg KH 
316d69dece5SCasey Schaufler #ifdef CONFIG_SECURITY
317d69dece5SCasey Schaufler static struct dentry *lsm_dentry;
lsm_read(struct file * filp,char __user * buf,size_t count,loff_t * ppos)318d69dece5SCasey Schaufler static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,
319d69dece5SCasey Schaufler 			loff_t *ppos)
320d69dece5SCasey Schaufler {
321d69dece5SCasey Schaufler 	return simple_read_from_buffer(buf, count, ppos, lsm_names,
322d69dece5SCasey Schaufler 		strlen(lsm_names));
323d69dece5SCasey Schaufler }
324d69dece5SCasey Schaufler 
325d69dece5SCasey Schaufler static const struct file_operations lsm_ops = {
326d69dece5SCasey Schaufler 	.read = lsm_read,
327d69dece5SCasey Schaufler 	.llseek = generic_file_llseek,
328d69dece5SCasey Schaufler };
329d69dece5SCasey Schaufler #endif
330d69dece5SCasey Schaufler 
securityfs_init(void)331b67dbf9dSGreg KH static int __init securityfs_init(void)
332b67dbf9dSGreg KH {
333b67dbf9dSGreg KH 	int retval;
334b67dbf9dSGreg KH 
335f9bb4882SEric W. Biederman 	retval = sysfs_create_mount_point(kernel_kobj, "security");
336f9bb4882SEric W. Biederman 	if (retval)
337f9bb4882SEric W. Biederman 		return retval;
338b67dbf9dSGreg KH 
339b67dbf9dSGreg KH 	retval = register_filesystem(&fs_type);
340d69dece5SCasey Schaufler 	if (retval) {
341f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(kernel_kobj, "security");
342b67dbf9dSGreg KH 		return retval;
343b67dbf9dSGreg KH 	}
344d69dece5SCasey Schaufler #ifdef CONFIG_SECURITY
345d69dece5SCasey Schaufler 	lsm_dentry = securityfs_create_file("lsm", 0444, NULL, NULL,
346d69dece5SCasey Schaufler 						&lsm_ops);
347d69dece5SCasey Schaufler #endif
348d69dece5SCasey Schaufler 	return 0;
349d69dece5SCasey Schaufler }
350b67dbf9dSGreg KH core_initcall(securityfs_init);
351