Skip to content

Commit

Permalink
lsm: add new securityfs delete function
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jxwufan authored and pcmoore committed Aug 20, 2024
1 parent a8a74df commit 7138679
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/linux/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,7 @@ struct dentry *securityfs_create_symlink(const char *name,
const char *target,
const struct inode_operations *iops);
extern void securityfs_remove(struct dentry *dentry);
extern void securityfs_recursive_remove(struct dentry *dentry);

#else /* CONFIG_SECURITYFS */

Expand Down
25 changes: 25 additions & 0 deletions security/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,31 @@ void securityfs_remove(struct dentry *dentry)
}
EXPORT_SYMBOL_GPL(securityfs_remove);

static void remove_one(struct dentry *victim)
{
simple_release_fs(&mount, &mount_count);
}

/**
* securityfs_recursive_remove - recursively removes a file or directory
*
* @dentry: a pointer to a the dentry of the file or directory to be removed.
*
* This function recursively 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.)
*/
void securityfs_recursive_remove(struct dentry *dentry)
{
if (IS_ERR_OR_NULL(dentry))
return;

simple_pin_fs(&fs_type, &mount, &mount_count);
simple_recursive_removal(dentry, remove_one);
simple_release_fs(&mount, &mount_count);
}
EXPORT_SYMBOL_GPL(securityfs_recursive_remove);

#ifdef CONFIG_SECURITY
static struct dentry *lsm_dentry;
static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,
Expand Down

0 comments on commit 7138679

Please sign in to comment.