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