configfs: Fix use-after-free when accessing sd->s_dentry
PATCH 1/1 configfs: Fix use-after-free when accessing sd->s_dentry
In the vfs_statx() context, during path lookup, the dentry gets added to sd->s_dentry via configfs_attach_attr(). In the end, vfs_statx() kills the dentry by calling path_put(), which invokes configfs_d_iput(). Ideally, this dentry must be removed from sd->s_dentry but it doesn't if the sd->s_count >= 3. As a result, sd->s_dentry is holding reference to a stale dentry pointer whose memory is already freed up. This results in use-after-free issue, when this stale sd->s_dentry is accessed later in configfs_readdir() path.
This issue can be easily reproduced, by running the LTP test case - sh fs_racer_file_list.sh /config
(https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/fs/racer/fs_racer_file_list.sh)
--- fs/configfs/dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/configfs/dir.c +1 -1
@@ -66,7 +66,7 @@ static void configfs_d_iput(struct dentry * dentry,* configfs_attach_attr and set sd->s_dentry to null * even it's still in use. */ -if (atomic_read (&sd->s_count) <=2 ) +if (sd->s_dentry == dentry) sd->s_dentry =NULL ;spin_unlock (&configfs_dirent_lock);
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 24 lines · Sahitya Tummala
In the
vfs_statx()context, during path lookup, the dentry gets added tosd->s_dentryviaconfigfs_attach_attr(). In the end,vfs_statx()kills the dentry by callingpath_put(), which invokesconfigfs_d_iput(). Ideally, this dentry must be removed fromsd->s_dentrybut it doesn't if thesd->s_count>= 3. As a result,sd->s_dentryis holding reference to a stale dentry pointer whose memory is already freed up. This results in use-after-free issue, when this stalesd->s_dentryis accessed later inconfigfs_readdir()path.This issue can be easily reproduced, by running the LTP test case - sh
fs_racer_file_list.sh/config
(https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/fs/racer/fs_racer_file_list.sh)Signed-off-by Sahitya Tummala <[email protected]>--- fs/configfs/dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/configfs/dir.c +0
-0
fs/configfs/dir.c static void configfs_d_iput(struct dentry * dentry, · line 72
Quoted 8 lines
fs/configfs/dir.c +1
-1@@ -66,7 +66,7 @@ static void configfs_d_iput(struct dentry * dentry,* configfs_attach_attr and set sd->s_dentry to null * even it's still in use. */ -if (atomic_read (&sd->s_count) <=2 ) +if (sd->s_dentry == dentry) sd->s_dentry =NULL ;spin_unlock (&configfs_dirent_lock);
Please, update the comment above as well - it refers the increment of sd->s_count, which becomes stale. I agree that the problem is real and the fix is sane (and that's what should've been done in the first place, back in "configfs: fix race between dentry put and lookup", which has introduced that logics).