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