Blame view
security/inode.c
10.6 KB
d2912cb15 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
b67dbf9d4 [PATCH] add secur... |
2 3 4 5 6 |
/* * inode.c - securityfs * * Copyright (C) 2005 Greg Kroah-Hartman <gregkh@suse.de> * |
b67dbf9d4 [PATCH] add secur... |
7 8 9 10 11 12 |
* Based on fs/debugfs/inode.c which had the following copyright notice: * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> * Copyright (C) 2004 IBM Inc. */ /* #define DEBUG */ |
1072bd678 security: fs: mak... |
13 14 |
#include <linux/sysfs.h> #include <linux/kobject.h> |
b67dbf9d4 [PATCH] add secur... |
15 |
#include <linux/fs.h> |
5c86d7e04 vfs: Convert secu... |
16 |
#include <linux/fs_context.h> |
b67dbf9d4 [PATCH] add secur... |
17 18 19 20 21 |
#include <linux/mount.h> #include <linux/pagemap.h> #include <linux/init.h> #include <linux/namei.h> #include <linux/security.h> |
d69dece5f LSM: Add /sys/ker... |
22 |
#include <linux/lsm_hooks.h> |
925629278 integrity: specia... |
23 |
#include <linux/magic.h> |
b67dbf9d4 [PATCH] add secur... |
24 25 26 |
static struct vfsmount *mount; static int mount_count; |
f614ee1e3 securityfs: switc... |
27 |
static void securityfs_free_inode(struct inode *inode) |
6623ec7c4 securityfs: add t... |
28 |
{ |
6623ec7c4 securityfs: add t... |
29 30 |
if (S_ISLNK(inode->i_mode)) kfree(inode->i_link); |
46c874419 securityfs: fix u... |
31 32 |
free_inode_nonrcu(inode); } |
6623ec7c4 securityfs: add t... |
33 34 |
static const struct super_operations securityfs_super_operations = { .statfs = simple_statfs, |
f614ee1e3 securityfs: switc... |
35 |
.free_inode = securityfs_free_inode, |
6623ec7c4 securityfs: add t... |
36 |
}; |
5c86d7e04 vfs: Convert secu... |
37 |
static int securityfs_fill_super(struct super_block *sb, struct fs_context *fc) |
b67dbf9d4 [PATCH] add secur... |
38 |
{ |
cda37124f fs: constify tree... |
39 |
static const struct tree_descr files[] = {{""}}; |
6623ec7c4 securityfs: add t... |
40 41 42 43 44 45 46 |
int error; error = simple_fill_super(sb, SECURITYFS_MAGIC, files); if (error) return error; sb->s_op = &securityfs_super_operations; |
b67dbf9d4 [PATCH] add secur... |
47 |
|
6623ec7c4 securityfs: add t... |
48 |
return 0; |
b67dbf9d4 [PATCH] add secur... |
49 |
} |
5c86d7e04 vfs: Convert secu... |
50 |
static int securityfs_get_tree(struct fs_context *fc) |
b67dbf9d4 [PATCH] add secur... |
51 |
{ |
5c86d7e04 vfs: Convert secu... |
52 53 54 55 56 57 58 59 60 61 62 |
return get_tree_single(fc, securityfs_fill_super); } static const struct fs_context_operations securityfs_context_ops = { .get_tree = securityfs_get_tree, }; static int securityfs_init_fs_context(struct fs_context *fc) { fc->ops = &securityfs_context_ops; return 0; |
b67dbf9d4 [PATCH] add secur... |
63 64 65 66 67 |
} static struct file_system_type fs_type = { .owner = THIS_MODULE, .name = "securityfs", |
5c86d7e04 vfs: Convert secu... |
68 |
.init_fs_context = securityfs_init_fs_context, |
b67dbf9d4 [PATCH] add secur... |
69 70 |
.kill_sb = kill_litter_super, }; |
b67dbf9d4 [PATCH] add secur... |
71 |
/** |
6623ec7c4 securityfs: add t... |
72 |
* securityfs_create_dentry - create a dentry in the securityfs filesystem |
b67dbf9d4 [PATCH] add secur... |
73 74 75 76 |
* * @name: a pointer to a string containing the name of the file to create. * @mode: the permission that the file should have * @parent: a pointer to the parent dentry for this file. This should be a |
3f23d815c security: add/fix... |
77 |
* directory dentry if set. If this parameter is %NULL, then the |
b67dbf9d4 [PATCH] add secur... |
78 79 |
* file will be created in the root of the securityfs filesystem. * @data: a pointer to something that the caller will want to get to later |
8e18e2941 [PATCH] inode_die... |
80 |
* on. The inode.i_private pointer will point to this value on |
b67dbf9d4 [PATCH] add secur... |
81 82 83 |
* the open() call. * @fops: a pointer to a struct file_operations that should be used for * this file. |
6623ec7c4 securityfs: add t... |
84 85 |
* @iops: a point to a struct of inode_operations that should be used for * this file/dir |
b67dbf9d4 [PATCH] add secur... |
86 |
* |
6623ec7c4 securityfs: add t... |
87 88 89 90 91 |
* This is the basic "create a file/dir/symlink" function for * securityfs. It allows for a wide range of flexibility in creating * a file, or a directory (if you want to create a directory, the * securityfs_create_dir() function is recommended to be used * instead). |
b67dbf9d4 [PATCH] add secur... |
92 |
* |
3f23d815c security: add/fix... |
93 |
* This function returns a pointer to a dentry if it succeeds. This |
6623ec7c4 securityfs: add t... |
94 95 96 97 |
* pointer must be passed to the securityfs_remove() function when the * file is to be removed (no automatic cleanup happens if your module * is unloaded, you are responsible here). If an error occurs, the * function will return the error value (via ERR_PTR). |
b67dbf9d4 [PATCH] add secur... |
98 |
* |
3f23d815c security: add/fix... |
99 |
* If securityfs is not enabled in the kernel, the value %-ENODEV is |
faa3aad75 securityfs: fix l... |
100 |
* returned. |
b67dbf9d4 [PATCH] add secur... |
101 |
*/ |
6623ec7c4 securityfs: add t... |
102 103 104 105 |
static struct dentry *securityfs_create_dentry(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops, const struct inode_operations *iops) |
b67dbf9d4 [PATCH] add secur... |
106 |
{ |
3e25eb9c4 securityfs: fix o... |
107 |
struct dentry *dentry; |
3e25eb9c4 securityfs: fix o... |
108 |
struct inode *dir, *inode; |
b67dbf9d4 [PATCH] add secur... |
109 |
int error; |
6623ec7c4 securityfs: add t... |
110 |
if (!(mode & S_IFMT)) |
3e25eb9c4 securityfs: fix o... |
111 |
mode = (mode & S_IALLUGO) | S_IFREG; |
3e25eb9c4 securityfs: fix o... |
112 |
|
b67dbf9d4 [PATCH] add secur... |
113 114 |
pr_debug("securityfs: creating file '%s' ",name); |
1f5ce9e93 VFS: Unexport do_... |
115 |
error = simple_pin_fs(&fs_type, &mount, &mount_count); |
3e25eb9c4 securityfs: fix o... |
116 117 118 119 120 |
if (error) return ERR_PTR(error); if (!parent) parent = mount->mnt_root; |
ce0b16ddf VFS: security/: d... |
121 |
dir = d_inode(parent); |
3e25eb9c4 securityfs: fix o... |
122 |
|
5955102c9 wrappers for ->i_... |
123 |
inode_lock(dir); |
3e25eb9c4 securityfs: fix o... |
124 125 126 |
dentry = lookup_one_len(name, parent, strlen(name)); if (IS_ERR(dentry)) goto out; |
ce0b16ddf VFS: security/: d... |
127 |
if (d_really_is_positive(dentry)) { |
3e25eb9c4 securityfs: fix o... |
128 129 |
error = -EEXIST; goto out1; |
b67dbf9d4 [PATCH] add secur... |
130 |
} |
3e25eb9c4 securityfs: fix o... |
131 132 133 134 |
inode = new_inode(dir->i_sb); if (!inode) { error = -ENOMEM; goto out1; |
b67dbf9d4 [PATCH] add secur... |
135 |
} |
3e25eb9c4 securityfs: fix o... |
136 137 |
inode->i_ino = get_next_ino(); inode->i_mode = mode; |
078cd8279 fs: Replace CURRE... |
138 |
inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
3e25eb9c4 securityfs: fix o... |
139 |
inode->i_private = data; |
6623ec7c4 securityfs: add t... |
140 |
if (S_ISDIR(mode)) { |
3e25eb9c4 securityfs: fix o... |
141 142 143 144 |
inode->i_op = &simple_dir_inode_operations; inode->i_fop = &simple_dir_operations; inc_nlink(inode); inc_nlink(dir); |
6623ec7c4 securityfs: add t... |
145 146 147 |
} else if (S_ISLNK(mode)) { inode->i_op = iops ? iops : &simple_symlink_inode_operations; inode->i_link = data; |
3e25eb9c4 securityfs: fix o... |
148 149 |
} else { inode->i_fop = fops; |
b67dbf9d4 [PATCH] add secur... |
150 |
} |
3e25eb9c4 securityfs: fix o... |
151 152 |
d_instantiate(dentry, inode); dget(dentry); |
5955102c9 wrappers for ->i_... |
153 |
inode_unlock(dir); |
3e25eb9c4 securityfs: fix o... |
154 155 156 157 158 159 |
return dentry; out1: dput(dentry); dentry = ERR_PTR(error); out: |
5955102c9 wrappers for ->i_... |
160 |
inode_unlock(dir); |
3e25eb9c4 securityfs: fix o... |
161 |
simple_release_fs(&mount, &mount_count); |
b67dbf9d4 [PATCH] add secur... |
162 163 |
return dentry; } |
6623ec7c4 securityfs: add t... |
164 165 166 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 194 195 |
/** * securityfs_create_file - create a file in the securityfs filesystem * * @name: a pointer to a string containing the name of the file to create. * @mode: the permission that the file should have * @parent: a pointer to the parent dentry for this file. This should be a * directory dentry if set. If this parameter is %NULL, then the * file will be created in the root of the securityfs filesystem. * @data: a pointer to something that the caller will want to get to later * on. The inode.i_private pointer will point to this value on * the open() call. * @fops: a pointer to a struct file_operations that should be used for * this file. * * This function creates a file in securityfs with the given @name. * * This function returns a pointer to a dentry if it succeeds. This * pointer must be passed to the securityfs_remove() function when the file is * to be removed (no automatic cleanup happens if your module is unloaded, * you are responsible here). If an error occurs, the function will return * the error value (via ERR_PTR). * * If securityfs is not enabled in the kernel, the value %-ENODEV is * returned. */ struct dentry *securityfs_create_file(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops) { return securityfs_create_dentry(name, mode, parent, data, fops, NULL); } |
b67dbf9d4 [PATCH] add secur... |
196 197 198 199 200 201 202 203 |
EXPORT_SYMBOL_GPL(securityfs_create_file); /** * securityfs_create_dir - create a directory in the securityfs filesystem * * @name: a pointer to a string containing the name of the directory to * create. * @parent: a pointer to the parent dentry for this file. This should be a |
3f23d815c security: add/fix... |
204 |
* directory dentry if set. If this parameter is %NULL, then the |
b67dbf9d4 [PATCH] add secur... |
205 206 |
* directory will be created in the root of the securityfs filesystem. * |
3f23d815c security: add/fix... |
207 |
* This function creates a directory in securityfs with the given @name. |
b67dbf9d4 [PATCH] add secur... |
208 |
* |
3f23d815c security: add/fix... |
209 |
* This function returns a pointer to a dentry if it succeeds. This |
b67dbf9d4 [PATCH] add secur... |
210 211 |
* pointer must be passed to the securityfs_remove() function when the file is * to be removed (no automatic cleanup happens if your module is unloaded, |
1b4606511 securityfs: fix s... |
212 213 |
* you are responsible here). If an error occurs, the function will return * the error value (via ERR_PTR). |
b67dbf9d4 [PATCH] add secur... |
214 |
* |
3f23d815c security: add/fix... |
215 |
* If securityfs is not enabled in the kernel, the value %-ENODEV is |
1b4606511 securityfs: fix s... |
216 |
* returned. |
b67dbf9d4 [PATCH] add secur... |
217 218 219 |
*/ struct dentry *securityfs_create_dir(const char *name, struct dentry *parent) { |
6623ec7c4 securityfs: add t... |
220 |
return securityfs_create_file(name, S_IFDIR | 0755, parent, NULL, NULL); |
b67dbf9d4 [PATCH] add secur... |
221 222 223 224 |
} EXPORT_SYMBOL_GPL(securityfs_create_dir); /** |
6623ec7c4 securityfs: add t... |
225 226 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
* securityfs_create_symlink - create a symlink in the securityfs filesystem * * @name: a pointer to a string containing the name of the symlink to * create. * @parent: a pointer to the parent dentry for the symlink. This should be a * directory dentry if set. If this parameter is %NULL, then the * directory will be created in the root of the securityfs filesystem. * @target: a pointer to a string containing the name of the symlink's target. * If this parameter is %NULL, then the @iops parameter needs to be * setup to handle .readlink and .get_link inode_operations. * @iops: a pointer to the struct inode_operations to use for the symlink. If * this parameter is %NULL, then the default simple_symlink_inode * operations will be used. * * This function creates a symlink in securityfs with the given @name. * * This function returns a pointer to a dentry if it succeeds. This * pointer must be passed to the securityfs_remove() function when the file is * to be removed (no automatic cleanup happens if your module is unloaded, * you are responsible here). If an error occurs, the function will return * the error value (via ERR_PTR). * * If securityfs is not enabled in the kernel, the value %-ENODEV is * returned. */ struct dentry *securityfs_create_symlink(const char *name, struct dentry *parent, const char *target, const struct inode_operations *iops) { struct dentry *dent; char *link = NULL; if (target) { link = kstrdup(target, GFP_KERNEL); if (!link) return ERR_PTR(-ENOMEM); } dent = securityfs_create_dentry(name, S_IFLNK | 0444, parent, link, NULL, iops); if (IS_ERR(dent)) kfree(link); return dent; } EXPORT_SYMBOL_GPL(securityfs_create_symlink); /** |
b67dbf9d4 [PATCH] add secur... |
273 274 |
* securityfs_remove - removes a file or directory from the securityfs filesystem * |
3f23d815c security: add/fix... |
275 |
* @dentry: a pointer to a the dentry of the file or directory to be removed. |
b67dbf9d4 [PATCH] add secur... |
276 277 278 279 280 281 |
* * This function removes a file or directory in securityfs that was previously * created with a call to another securityfs function (like * securityfs_create_file() or variants thereof.) * * This function is required to be called in order for the file to be |
3f23d815c security: add/fix... |
282 283 |
* removed. No automatic cleanup of files will happen when a module is * removed; you are responsible here. |
b67dbf9d4 [PATCH] add secur... |
284 285 286 |
*/ void securityfs_remove(struct dentry *dentry) { |
4093d306a securityfs: ->d_p... |
287 |
struct inode *dir; |
b67dbf9d4 [PATCH] add secur... |
288 |
|
d93e4c940 securityfs: secur... |
289 |
if (!dentry || IS_ERR(dentry)) |
b67dbf9d4 [PATCH] add secur... |
290 |
return; |
4093d306a securityfs: ->d_p... |
291 292 |
dir = d_inode(dentry->d_parent); inode_lock(dir); |
dc3f4198e make simple_posit... |
293 294 |
if (simple_positive(dentry)) { if (d_is_dir(dentry)) |
4093d306a securityfs: ->d_p... |
295 |
simple_rmdir(dir, dentry); |
dc3f4198e make simple_posit... |
296 |
else |
4093d306a securityfs: ->d_p... |
297 |
simple_unlink(dir, dentry); |
dc3f4198e make simple_posit... |
298 |
dput(dentry); |
b67dbf9d4 [PATCH] add secur... |
299 |
} |
4093d306a securityfs: ->d_p... |
300 |
inode_unlock(dir); |
b67dbf9d4 [PATCH] add secur... |
301 302 303 |
simple_release_fs(&mount, &mount_count); } EXPORT_SYMBOL_GPL(securityfs_remove); |
d69dece5f LSM: Add /sys/ker... |
304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
#ifdef CONFIG_SECURITY static struct dentry *lsm_dentry; static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) { return simple_read_from_buffer(buf, count, ppos, lsm_names, strlen(lsm_names)); } static const struct file_operations lsm_ops = { .read = lsm_read, .llseek = generic_file_llseek, }; #endif |
b67dbf9d4 [PATCH] add secur... |
318 319 320 |
static int __init securityfs_init(void) { int retval; |
f9bb48825 sysfs: Create mou... |
321 322 323 |
retval = sysfs_create_mount_point(kernel_kobj, "security"); if (retval) return retval; |
b67dbf9d4 [PATCH] add secur... |
324 325 |
retval = register_filesystem(&fs_type); |
d69dece5f LSM: Add /sys/ker... |
326 |
if (retval) { |
f9bb48825 sysfs: Create mou... |
327 |
sysfs_remove_mount_point(kernel_kobj, "security"); |
d69dece5f LSM: Add /sys/ker... |
328 329 330 331 332 333 334 |
return retval; } #ifdef CONFIG_SECURITY lsm_dentry = securityfs_create_file("lsm", 0444, NULL, NULL, &lsm_ops); #endif return 0; |
b67dbf9d4 [PATCH] add secur... |
335 |
} |
b67dbf9d4 [PATCH] add secur... |
336 |
core_initcall(securityfs_init); |