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
securityfs_free_inode(struct inode * inode)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
securityfs_fill_super(struct super_block * sb,struct fs_context * fc)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
securityfs_get_tree(struct fs_context * fc)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
securityfs_init_fs_context(struct fs_context * fc)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 */
securityfs_create_dentry(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops,const struct inode_operations * iops)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
116 if (!(mode & S_IFMT))
117 mode = (mode & S_IALLUGO) | S_IFREG;
118
119 pr_debug("securityfs: creating file '%s'\n",name);
120
121 error = simple_pin_fs(&fs_type, &mount, &mount_count);
122 if (error)
123 return ERR_PTR(error);
124
125 if (!parent)
126 parent = mount->mnt_root;
127
128 dir = d_inode(parent);
129
130 inode_lock(dir);
131 dentry = lookup_one_len(name, parent, strlen(name));
132 if (IS_ERR(dentry))
133 goto out;
134
135 if (d_really_is_positive(dentry)) {
136 error = -EEXIST;
137 goto out1;
138 }
139
140 inode = new_inode(dir->i_sb);
141 if (!inode) {
142 error = -ENOMEM;
143 goto out1;
144 }
145
146 inode->i_ino = get_next_ino();
147 inode->i_mode = mode;
148 simple_inode_init_ts(inode);
149 inode->i_private = data;
150 if (S_ISDIR(mode)) {
151 inode->i_op = &simple_dir_inode_operations;
152 inode->i_fop = &simple_dir_operations;
153 inc_nlink(inode);
154 inc_nlink(dir);
155 } else if (S_ISLNK(mode)) {
156 inode->i_op = iops ? iops : &simple_symlink_inode_operations;
157 inode->i_link = data;
158 } else {
159 inode->i_fop = fops;
160 }
161 d_instantiate(dentry, inode);
162 inode_unlock(dir);
163 return dentry;
164
165 out1:
166 dput(dentry);
167 dentry = ERR_PTR(error);
168 out:
169 inode_unlock(dir);
170 simple_release_fs(&mount, &mount_count);
171 return dentry;
172 }
173
174 /**
175 * securityfs_create_file - create a file in the securityfs filesystem
176 *
177 * @name: a pointer to a string containing the name of the file to create.
178 * @mode: the permission that the file should have
179 * @parent: a pointer to the parent dentry for this file. This should be a
180 * directory dentry if set. If this parameter is %NULL, then the
181 * file will be created in the root of the securityfs filesystem.
182 * @data: a pointer to something that the caller will want to get to later
183 * on. The inode.i_private pointer will point to this value on
184 * the open() call.
185 * @fops: a pointer to a struct file_operations that should be used for
186 * this file.
187 *
188 * This function creates a file in securityfs with the given @name.
189 *
190 * This function returns a pointer to a dentry if it succeeds. This
191 * pointer must be passed to the securityfs_remove() function when the file is
192 * to be removed (no automatic cleanup happens if your module is unloaded,
193 * you are responsible here). If an error occurs, the function will return
194 * the error value (via ERR_PTR).
195 *
196 * If securityfs is not enabled in the kernel, the value %-ENODEV is
197 * returned.
198 */
securityfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)199 struct dentry *securityfs_create_file(const char *name, umode_t mode,
200 struct dentry *parent, void *data,
201 const struct file_operations *fops)
202 {
203 return securityfs_create_dentry(name, mode, parent, data, fops, NULL);
204 }
205 EXPORT_SYMBOL_GPL(securityfs_create_file);
206
207 /**
208 * securityfs_create_dir - create a directory in the securityfs filesystem
209 *
210 * @name: a pointer to a string containing the name of the directory to
211 * create.
212 * @parent: a pointer to the parent dentry for this file. This should be a
213 * directory dentry if set. If this parameter is %NULL, then the
214 * directory will be created in the root of the securityfs filesystem.
215 *
216 * This function creates a directory in securityfs with the given @name.
217 *
218 * This function returns a pointer to a dentry if it succeeds. This
219 * pointer must be passed to the securityfs_remove() function when the file is
220 * to be removed (no automatic cleanup happens if your module is unloaded,
221 * you are responsible here). If an error occurs, the function will return
222 * the error value (via ERR_PTR).
223 *
224 * If securityfs is not enabled in the kernel, the value %-ENODEV is
225 * returned.
226 */
securityfs_create_dir(const char * name,struct dentry * parent)227 struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
228 {
229 return securityfs_create_file(name, S_IFDIR | 0755, parent, NULL, NULL);
230 }
231 EXPORT_SYMBOL_GPL(securityfs_create_dir);
232
233 /**
234 * securityfs_create_symlink - create a symlink in the securityfs filesystem
235 *
236 * @name: a pointer to a string containing the name of the symlink to
237 * create.
238 * @parent: a pointer to the parent dentry for the symlink. This should be a
239 * directory dentry if set. If this parameter is %NULL, then the
240 * directory will be created in the root of the securityfs filesystem.
241 * @target: a pointer to a string containing the name of the symlink's target.
242 * If this parameter is %NULL, then the @iops parameter needs to be
243 * setup to handle .readlink and .get_link inode_operations.
244 * @iops: a pointer to the struct inode_operations to use for the symlink. If
245 * this parameter is %NULL, then the default simple_symlink_inode
246 * operations will be used.
247 *
248 * This function creates a symlink in securityfs with the given @name.
249 *
250 * This function returns a pointer to a dentry if it succeeds. This
251 * pointer must be passed to the securityfs_remove() function when the file is
252 * to be removed (no automatic cleanup happens if your module is unloaded,
253 * you are responsible here). If an error occurs, the function will return
254 * the error value (via ERR_PTR).
255 *
256 * If securityfs is not enabled in the kernel, the value %-ENODEV is
257 * returned.
258 */
securityfs_create_symlink(const char * name,struct dentry * parent,const char * target,const struct inode_operations * iops)259 struct dentry *securityfs_create_symlink(const char *name,
260 struct dentry *parent,
261 const char *target,
262 const struct inode_operations *iops)
263 {
264 struct dentry *dent;
265 char *link = NULL;
266
267 if (target) {
268 link = kstrdup(target, GFP_KERNEL);
269 if (!link)
270 return ERR_PTR(-ENOMEM);
271 }
272 dent = securityfs_create_dentry(name, S_IFLNK | 0444, parent,
273 link, NULL, iops);
274 if (IS_ERR(dent))
275 kfree(link);
276
277 return dent;
278 }
279 EXPORT_SYMBOL_GPL(securityfs_create_symlink);
280
281 /**
282 * securityfs_remove - removes a file or directory from the securityfs filesystem
283 *
284 * @dentry: a pointer to a the dentry of the file or directory to be removed.
285 *
286 * This function removes a file or directory in securityfs that was previously
287 * created with a call to another securityfs function (like
288 * securityfs_create_file() or variants thereof.)
289 *
290 * This function is required to be called in order for the file to be
291 * removed. No automatic cleanup of files will happen when a module is
292 * removed; you are responsible here.
293 */
securityfs_remove(struct dentry * dentry)294 void securityfs_remove(struct dentry *dentry)
295 {
296 struct inode *dir;
297
298 if (IS_ERR_OR_NULL(dentry))
299 return;
300
301 dir = d_inode(dentry->d_parent);
302 inode_lock(dir);
303 if (simple_positive(dentry)) {
304 if (d_is_dir(dentry))
305 simple_rmdir(dir, dentry);
306 else
307 simple_unlink(dir, dentry);
308 }
309 inode_unlock(dir);
310 simple_release_fs(&mount, &mount_count);
311 }
312 EXPORT_SYMBOL_GPL(securityfs_remove);
313
remove_one(struct dentry * victim)314 static void remove_one(struct dentry *victim)
315 {
316 simple_release_fs(&mount, &mount_count);
317 }
318
319 /**
320 * securityfs_recursive_remove - recursively removes a file or directory
321 *
322 * @dentry: a pointer to a the dentry of the file or directory to be removed.
323 *
324 * This function recursively removes a file or directory in securityfs that was
325 * previously created with a call to another securityfs function (like
326 * securityfs_create_file() or variants thereof.)
327 */
securityfs_recursive_remove(struct dentry * dentry)328 void securityfs_recursive_remove(struct dentry *dentry)
329 {
330 if (IS_ERR_OR_NULL(dentry))
331 return;
332
333 simple_pin_fs(&fs_type, &mount, &mount_count);
334 simple_recursive_removal(dentry, remove_one);
335 simple_release_fs(&mount, &mount_count);
336 }
337 EXPORT_SYMBOL_GPL(securityfs_recursive_remove);
338
339 #ifdef CONFIG_SECURITY
340 static struct dentry *lsm_dentry;
lsm_read(struct file * filp,char __user * buf,size_t count,loff_t * ppos)341 static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,
342 loff_t *ppos)
343 {
344 return simple_read_from_buffer(buf, count, ppos, lsm_names,
345 strlen(lsm_names));
346 }
347
348 static const struct file_operations lsm_ops = {
349 .read = lsm_read,
350 .llseek = generic_file_llseek,
351 };
352 #endif
353
securityfs_init(void)354 static int __init securityfs_init(void)
355 {
356 int retval;
357
358 retval = sysfs_create_mount_point(kernel_kobj, "security");
359 if (retval)
360 return retval;
361
362 retval = register_filesystem(&fs_type);
363 if (retval) {
364 sysfs_remove_mount_point(kernel_kobj, "security");
365 return retval;
366 }
367 #ifdef CONFIG_SECURITY
368 lsm_dentry = securityfs_create_file("lsm", 0444, NULL, NULL,
369 &lsm_ops);
370 #endif
371 return 0;
372 }
373 core_initcall(securityfs_init);
374