1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/mount.h>
19#include <linux/pagemap.h>
20#include <linux/init.h>
21#include <linux/namei.h>
22#include <linux/security.h>
23#include <linux/lsm_hooks.h>
24#include <linux/magic.h>
25
26static struct vfsmount *mount;
27static int mount_count;
28
29static void securityfs_evict_inode(struct inode *inode)
30{
31 truncate_inode_pages_final(&inode->i_data);
32 clear_inode(inode);
33 if (S_ISLNK(inode->i_mode))
34 kfree(inode->i_link);
35}
36
37static const struct super_operations securityfs_super_operations = {
38 .statfs = simple_statfs,
39 .evict_inode = securityfs_evict_inode,
40};
41
42static int fill_super(struct super_block *sb, void *data, int silent)
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 struct dentry *get_sb(struct file_system_type *fs_type,
57 int flags, const char *dev_name,
58 void *data)
59{
60 return mount_single(fs_type, flags, data, fill_super);
61}
62
63static struct file_system_type fs_type = {
64 .owner = THIS_MODULE,
65 .name = "securityfs",
66 .mount = get_sb,
67 .kill_sb = kill_litter_super,
68};
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101static struct dentry *securityfs_create_dentry(const char *name, umode_t mode,
102 struct dentry *parent, void *data,
103 const struct file_operations *fops,
104 const struct inode_operations *iops)
105{
106 struct dentry *dentry;
107 struct inode *dir, *inode;
108 int error;
109
110 if (!(mode & S_IFMT))
111 mode = (mode & S_IALLUGO) | S_IFREG;
112
113 pr_debug("securityfs: creating file '%s'\n",name);
114
115 error = simple_pin_fs(&fs_type, &mount, &mount_count);
116 if (error)
117 return ERR_PTR(error);
118
119 if (!parent)
120 parent = mount->mnt_root;
121
122 dir = d_inode(parent);
123
124 inode_lock(dir);
125 dentry = lookup_one_len(name, parent, strlen(name));
126 if (IS_ERR(dentry))
127 goto out;
128
129 if (d_really_is_positive(dentry)) {
130 error = -EEXIST;
131 goto out1;
132 }
133
134 inode = new_inode(dir->i_sb);
135 if (!inode) {
136 error = -ENOMEM;
137 goto out1;
138 }
139
140 inode->i_ino = get_next_ino();
141 inode->i_mode = mode;
142 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
143 inode->i_private = data;
144 if (S_ISDIR(mode)) {
145 inode->i_op = &simple_dir_inode_operations;
146 inode->i_fop = &simple_dir_operations;
147 inc_nlink(inode);
148 inc_nlink(dir);
149 } else if (S_ISLNK(mode)) {
150 inode->i_op = iops ? iops : &simple_symlink_inode_operations;
151 inode->i_link = data;
152 } else {
153 inode->i_fop = fops;
154 }
155 d_instantiate(dentry, inode);
156 dget(dentry);
157 inode_unlock(dir);
158 return dentry;
159
160out1:
161 dput(dentry);
162 dentry = ERR_PTR(error);
163out:
164 inode_unlock(dir);
165 simple_release_fs(&mount, &mount_count);
166 return dentry;
167}
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194struct dentry *securityfs_create_file(const char *name, umode_t mode,
195 struct dentry *parent, void *data,
196 const struct file_operations *fops)
197{
198 return securityfs_create_dentry(name, mode, parent, data, fops, NULL);
199}
200EXPORT_SYMBOL_GPL(securityfs_create_file);
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
223{
224 return securityfs_create_file(name, S_IFDIR | 0755, parent, NULL, NULL);
225}
226EXPORT_SYMBOL_GPL(securityfs_create_dir);
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254struct dentry *securityfs_create_symlink(const char *name,
255 struct dentry *parent,
256 const char *target,
257 const struct inode_operations *iops)
258{
259 struct dentry *dent;
260 char *link = NULL;
261
262 if (target) {
263 link = kstrdup(target, GFP_KERNEL);
264 if (!link)
265 return ERR_PTR(-ENOMEM);
266 }
267 dent = securityfs_create_dentry(name, S_IFLNK | 0444, parent,
268 link, NULL, iops);
269 if (IS_ERR(dent))
270 kfree(link);
271
272 return dent;
273}
274EXPORT_SYMBOL_GPL(securityfs_create_symlink);
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289void securityfs_remove(struct dentry *dentry)
290{
291 struct inode *dir;
292
293 if (!dentry || IS_ERR(dentry))
294 return;
295
296 dir = d_inode(dentry->d_parent);
297 inode_lock(dir);
298 if (simple_positive(dentry)) {
299 if (d_is_dir(dentry))
300 simple_rmdir(dir, dentry);
301 else
302 simple_unlink(dir, dentry);
303 dput(dentry);
304 }
305 inode_unlock(dir);
306 simple_release_fs(&mount, &mount_count);
307}
308EXPORT_SYMBOL_GPL(securityfs_remove);
309
310#ifdef CONFIG_SECURITY
311static struct dentry *lsm_dentry;
312static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,
313 loff_t *ppos)
314{
315 return simple_read_from_buffer(buf, count, ppos, lsm_names,
316 strlen(lsm_names));
317}
318
319static const struct file_operations lsm_ops = {
320 .read = lsm_read,
321 .llseek = generic_file_llseek,
322};
323#endif
324
325static int __init securityfs_init(void)
326{
327 int retval;
328
329 retval = sysfs_create_mount_point(kernel_kobj, "security");
330 if (retval)
331 return retval;
332
333 retval = register_filesystem(&fs_type);
334 if (retval) {
335 sysfs_remove_mount_point(kernel_kobj, "security");
336 return retval;
337 }
338#ifdef CONFIG_SECURITY
339 lsm_dentry = securityfs_create_file("lsm", 0444, NULL, NULL,
340 &lsm_ops);
341#endif
342 return 0;
343}
344
345core_initcall(securityfs_init);
346MODULE_LICENSE("GPL");
347
348