Skip to content

Commit 1de09a7

Browse files
sjp38akpm00
authored andcommitted
mm/damon/dbgfs: check if rm_contexts input is for a real context
A user could write a name of a file under 'damon/' debugfs directory, which is not a user-created context, to 'rm_contexts' file. In the case, 'dbgfs_rm_context()' just assumes it's the valid DAMON context directory only if a file of the name exist. As a result, invalid memory access could happen as below. Fix the bug by checking if the given input is for a directory. This check can filter out non-context inputs because directories under 'damon/' debugfs directory can be created via only 'mk_contexts' file. This bug has found by syzbot[1]. [1] https://lore.kernel.org/damon/000000000000ede3ac05ec4abf8e@google.com/ Link: https://lkml.kernel.org/r/20221107165001.5717-2-sj@kernel.org Fixes: 75c1c2b ("mm/damon/dbgfs: support multiple contexts") Signed-off-by: SeongJae Park <sj@kernel.org> Reported-by: syzbot+6087eafb76a94c4ac9eb@syzkaller.appspotmail.com Cc: <stable@vger.kernel.org> [5.15.x] Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 7dc5ba6 commit 1de09a7

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

mm/damon/dbgfs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,7 @@ static ssize_t dbgfs_mk_context_write(struct file *file,
890890
static int dbgfs_rm_context(char *name)
891891
{
892892
struct dentry *root, *dir, **new_dirs;
893+
struct inode *inode;
893894
struct damon_ctx **new_ctxs;
894895
int i, j;
895896
int ret = 0;
@@ -905,6 +906,12 @@ static int dbgfs_rm_context(char *name)
905906
if (!dir)
906907
return -ENOENT;
907908

909+
inode = d_inode(dir);
910+
if (!S_ISDIR(inode->i_mode)) {
911+
ret = -EINVAL;
912+
goto out_dput;
913+
}
914+
908915
new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
909916
GFP_KERNEL);
910917
if (!new_dirs) {

0 commit comments

Comments
 (0)