Skip to content

Commit 7138679

Browse files
jxwufanpcmoore
authored andcommitted
lsm: add new securityfs delete function
When deleting a directory in the security file system, the existing securityfs_remove requires the directory to be empty, otherwise it will do nothing. This leads to a potential risk that the security file system might be in an unclean state when the intended deletion did not happen. This commit introduces a new function securityfs_recursive_remove to recursively delete a directory without leaving an unclean state. Co-developed-by: Christian Brauner (Microsoft) <[email protected]> Signed-off-by: Fan Wu <[email protected]> [PM: subject line tweak] Signed-off-by: Paul Moore <[email protected]>
1 parent a8a74df commit 7138679

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

include/linux/security.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,7 @@ struct dentry *securityfs_create_symlink(const char *name,
20902090
const char *target,
20912091
const struct inode_operations *iops);
20922092
extern void securityfs_remove(struct dentry *dentry);
2093+
extern void securityfs_recursive_remove(struct dentry *dentry);
20932094

20942095
#else /* CONFIG_SECURITYFS */
20952096

security/inode.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,31 @@ void securityfs_remove(struct dentry *dentry)
313313
}
314314
EXPORT_SYMBOL_GPL(securityfs_remove);
315315

316+
static void remove_one(struct dentry *victim)
317+
{
318+
simple_release_fs(&mount, &mount_count);
319+
}
320+
321+
/**
322+
* securityfs_recursive_remove - recursively removes a file or directory
323+
*
324+
* @dentry: a pointer to a the dentry of the file or directory to be removed.
325+
*
326+
* This function recursively removes a file or directory in securityfs that was
327+
* previously created with a call to another securityfs function (like
328+
* securityfs_create_file() or variants thereof.)
329+
*/
330+
void securityfs_recursive_remove(struct dentry *dentry)
331+
{
332+
if (IS_ERR_OR_NULL(dentry))
333+
return;
334+
335+
simple_pin_fs(&fs_type, &mount, &mount_count);
336+
simple_recursive_removal(dentry, remove_one);
337+
simple_release_fs(&mount, &mount_count);
338+
}
339+
EXPORT_SYMBOL_GPL(securityfs_recursive_remove);
340+
316341
#ifdef CONFIG_SECURITY
317342
static struct dentry *lsm_dentry;
318343
static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,

0 commit comments

Comments
 (0)