Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
27static struct vfsmount *mount;
28static int mount_count;
29
30static 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
37static const struct super_operations securityfs_super_operations = {
38 .statfs = simple_statfs,
39 .free_inode = securityfs_free_inode,
40};
41
42static 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
56static int securityfs_get_tree(struct fs_context *fc)
57{
58 return get_tree_single(fc, securityfs_fill_super);
59}
60
61static const struct fs_context_operations securityfs_context_ops = {
62 .get_tree = securityfs_get_tree,
63};
64
65static int securityfs_init_fs_context(struct fs_context *fc)
66{
67 fc->ops = &securityfs_context_ops;
68 return 0;
69}
70
71static 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_anon_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 */
109static 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 inode = new_inode(parent->d_sb);
133 if (unlikely(!inode)) {
134 dentry = ERR_PTR(-ENOMEM);
135 goto out;
136 }
137
138 dir = d_inode(parent);
139
140 dentry = simple_start_creating(parent, name);
141 if (IS_ERR(dentry)) {
142 iput(inode);
143 goto out;
144 }
145 inode->i_ino = get_next_ino();
146 inode->i_mode = mode;
147 simple_inode_init_ts(inode);
148 inode->i_private = data;
149 if (S_ISDIR(mode)) {
150 inode->i_op = &simple_dir_inode_operations;
151 inode->i_fop = &simple_dir_operations;
152 inc_nlink(inode);
153 inc_nlink(dir);
154 } else if (S_ISLNK(mode)) {
155 inode->i_op = iops ? iops : &simple_symlink_inode_operations;
156 inode->i_link = data;
157 } else {
158 inode->i_fop = fops;
159 }
160 d_make_persistent(dentry, inode);
161 simple_done_creating(dentry);
162 return dentry; // borrowed
163
164out:
165 if (pinned)
166 simple_release_fs(&mount, &mount_count);
167 return dentry;
168}
169
170/**
171 * securityfs_create_file - create a file in the securityfs filesystem
172 *
173 * @name: a pointer to a string containing the name of the file to create.
174 * @mode: the permission that the file should have
175 * @parent: a pointer to the parent dentry for this file. This should be a
176 * directory dentry if set. If this parameter is %NULL, then the
177 * file will be created in the root of the securityfs filesystem.
178 * @data: a pointer to something that the caller will want to get to later
179 * on. The inode.i_private pointer will point to this value on
180 * the open() call.
181 * @fops: a pointer to a struct file_operations that should be used for
182 * this file.
183 *
184 * This function creates a file in securityfs with the given @name.
185 *
186 * This function returns a pointer to a dentry if it succeeds. This
187 * pointer must be passed to the securityfs_remove() function when the file is
188 * to be removed (no automatic cleanup happens if your module is unloaded,
189 * you are responsible here). If an error occurs, the function will return
190 * the error value (via ERR_PTR).
191 *
192 * If securityfs is not enabled in the kernel, the value %-ENODEV is
193 * returned.
194 */
195struct dentry *securityfs_create_file(const char *name, umode_t mode,
196 struct dentry *parent, void *data,
197 const struct file_operations *fops)
198{
199 return securityfs_create_dentry(name, mode, parent, data, fops, NULL);
200}
201EXPORT_SYMBOL_GPL(securityfs_create_file);
202
203/**
204 * securityfs_create_dir - create a directory in the securityfs filesystem
205 *
206 * @name: a pointer to a string containing the name of the directory to
207 * create.
208 * @parent: a pointer to the parent dentry for this file. This should be a
209 * directory dentry if set. If this parameter is %NULL, then the
210 * directory will be created in the root of the securityfs filesystem.
211 *
212 * This function creates a directory in securityfs with the given @name.
213 *
214 * This function returns a pointer to a dentry if it succeeds. This
215 * pointer must be passed to the securityfs_remove() function when the file is
216 * to be removed (no automatic cleanup happens if your module is unloaded,
217 * you are responsible here). If an error occurs, the function will return
218 * the error value (via ERR_PTR).
219 *
220 * If securityfs is not enabled in the kernel, the value %-ENODEV is
221 * returned.
222 */
223struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
224{
225 return securityfs_create_file(name, S_IFDIR | 0755, parent, NULL, NULL);
226}
227EXPORT_SYMBOL_GPL(securityfs_create_dir);
228
229/**
230 * securityfs_create_symlink - create a symlink in the securityfs filesystem
231 *
232 * @name: a pointer to a string containing the name of the symlink to
233 * create.
234 * @parent: a pointer to the parent dentry for the symlink. This should be a
235 * directory dentry if set. If this parameter is %NULL, then the
236 * directory will be created in the root of the securityfs filesystem.
237 * @target: a pointer to a string containing the name of the symlink's target.
238 * If this parameter is %NULL, then the @iops parameter needs to be
239 * setup to handle .readlink and .get_link inode_operations.
240 * @iops: a pointer to the struct inode_operations to use for the symlink. If
241 * this parameter is %NULL, then the default simple_symlink_inode
242 * operations will be used.
243 *
244 * This function creates a symlink in securityfs with the given @name.
245 *
246 * This function returns a pointer to a dentry if it succeeds. This
247 * pointer must be passed to the securityfs_remove() function when the file is
248 * to be removed (no automatic cleanup happens if your module is unloaded,
249 * you are responsible here). If an error occurs, the function will return
250 * the error value (via ERR_PTR).
251 *
252 * If securityfs is not enabled in the kernel, the value %-ENODEV is
253 * returned.
254 */
255struct dentry *securityfs_create_symlink(const char *name,
256 struct dentry *parent,
257 const char *target,
258 const struct inode_operations *iops)
259{
260 struct dentry *dent;
261 char *link = NULL;
262
263 if (target) {
264 link = kstrdup(target, GFP_KERNEL);
265 if (!link)
266 return ERR_PTR(-ENOMEM);
267 }
268 dent = securityfs_create_dentry(name, S_IFLNK | 0444, parent,
269 link, NULL, iops);
270 if (IS_ERR(dent))
271 kfree(link);
272
273 return dent;
274}
275EXPORT_SYMBOL_GPL(securityfs_create_symlink);
276
277static void remove_one(struct dentry *victim)
278{
279 if (victim->d_parent == victim->d_sb->s_root)
280 simple_release_fs(&mount, &mount_count);
281}
282
283/**
284 * securityfs_remove - removes a file or directory from the securityfs filesystem
285 *
286 * @dentry: a pointer to a the dentry of the file or directory to be removed.
287 *
288 * This function removes a file or directory in securityfs that was previously
289 * created with a call to another securityfs function (like
290 * securityfs_create_file() or variants thereof.)
291 *
292 * This function is required to be called in order for the file to be
293 * removed. No automatic cleanup of files will happen when a module is
294 * removed; you are responsible here.
295 *
296 * AV: when applied to directory it will take all children out; no need to call
297 * it for descendents if ancestor is getting killed.
298 */
299void securityfs_remove(struct dentry *dentry)
300{
301 if (IS_ERR_OR_NULL(dentry))
302 return;
303
304 simple_pin_fs(&fs_type, &mount, &mount_count);
305 simple_recursive_removal(dentry, remove_one);
306 simple_release_fs(&mount, &mount_count);
307}
308EXPORT_SYMBOL_GPL(securityfs_remove);
309
310#ifdef CONFIG_SECURITY
311#include <linux/spinlock.h>
312
313static struct dentry *lsm_dentry;
314
315static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,
316 loff_t *ppos)
317{
318 int i;
319 static char *str;
320 static size_t len;
321 static DEFINE_SPINLOCK(lock);
322
323 /* NOTE: we never free or modify the string once it is set */
324
325 if (unlikely(!str || !len)) {
326 char *str_tmp;
327 size_t len_tmp = 0;
328
329 for (i = 0; i < lsm_active_cnt; i++)
330 /* the '+ 1' accounts for either a comma or a NUL */
331 len_tmp += strlen(lsm_idlist[i]->name) + 1;
332
333 str_tmp = kmalloc(len_tmp, GFP_KERNEL);
334 if (!str_tmp)
335 return -ENOMEM;
336 str_tmp[0] = '\0';
337
338 for (i = 0; i < lsm_active_cnt; i++) {
339 if (i > 0)
340 strcat(str_tmp, ",");
341 strcat(str_tmp, lsm_idlist[i]->name);
342 }
343
344 spin_lock(&lock);
345 if (!str) {
346 str = str_tmp;
347 len = len_tmp - 1;
348 } else
349 kfree(str_tmp);
350 spin_unlock(&lock);
351 }
352
353 return simple_read_from_buffer(buf, count, ppos, str, len);
354}
355
356static const struct file_operations lsm_ops = {
357 .read = lsm_read,
358 .llseek = generic_file_llseek,
359};
360#endif
361
362int __init securityfs_init(void)
363{
364 int retval;
365
366 retval = sysfs_create_mount_point(kernel_kobj, "security");
367 if (retval)
368 return retval;
369
370 retval = register_filesystem(&fs_type);
371 if (retval) {
372 sysfs_remove_mount_point(kernel_kobj, "security");
373 return retval;
374 }
375#ifdef CONFIG_SECURITY
376 lsm_dentry = securityfs_create_file("lsm", 0444, NULL, NULL,
377 &lsm_ops);
378#endif
379 return 0;
380}