Commit 4ed36a5a authored by Sahitya Tummala's avatar Sahitya Tummala Committed by Stefan Bader

configfs: Fix use-after-free when accessing sd->s_dentry

BugLink: https://bugs.launchpad.net/bugs/1836666

[ Upstream commit f6122ed2 ]

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)

Fixes: 76ae281f ('configfs: fix race between dentry put and lookup')
Signed-off-by: default avatarSahitya Tummala <stummala@codeaurora.org>
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
Signed-off-by: default avatarKleber Sacilotto de Souza <kleber.souza@canonical.com>
parent eccdf7ad
...@@ -58,15 +58,13 @@ static void configfs_d_iput(struct dentry * dentry, ...@@ -58,15 +58,13 @@ static void configfs_d_iput(struct dentry * dentry,
if (sd) { if (sd) {
/* Coordinate with configfs_readdir */ /* Coordinate with configfs_readdir */
spin_lock(&configfs_dirent_lock); spin_lock(&configfs_dirent_lock);
/* Coordinate with configfs_attach_attr where will increase /*
* sd->s_count and update sd->s_dentry to new allocated one. * Set sd->s_dentry to null only when this dentry is the one
* Only set sd->dentry to null when this dentry is the only * that is going to be killed. Otherwise configfs_d_iput may
* sd owner. * run just after configfs_attach_attr and set sd->s_dentry to
* If not do so, configfs_d_iput may run just after * NULL even it's still in use.
* 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; sd->s_dentry = NULL;
spin_unlock(&configfs_dirent_lock); spin_unlock(&configfs_dirent_lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment