linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] configfs: Fix use-after-free when accessing sd->s_dentry
@ 2019-01-03  8:11 Sahitya Tummala
  2019-01-03  8:48 ` Al Viro
  0 siblings, 1 reply; 2+ messages in thread
From: Sahitya Tummala @ 2019-01-03  8:11 UTC (permalink / raw)
  To: Junxiao Bi, Joel Becker, Christoph Hellwig, Al Viro, linux-kernel
  Cc: Sahitya Tummala

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)

Signed-off-by: Sahitya Tummala <stummala@codeaurora.org>
---
 fs/configfs/dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index 39843fa..a5906c5 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -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);
-- 
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.


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] configfs: Fix use-after-free when accessing sd->s_dentry
  2019-01-03  8:11 [PATCH] configfs: Fix use-after-free when accessing sd->s_dentry Sahitya Tummala
@ 2019-01-03  8:48 ` Al Viro
  0 siblings, 0 replies; 2+ messages in thread
From: Al Viro @ 2019-01-03  8:48 UTC (permalink / raw)
  To: Sahitya Tummala; +Cc: Junxiao Bi, Joel Becker, Christoph Hellwig, linux-kernel

On Thu, Jan 03, 2019 at 01:41:04PM +0530, Sahitya Tummala wrote:
> 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)
> 
> Signed-off-by: Sahitya Tummala <stummala@codeaurora.org>
> ---
>  fs/configfs/dir.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
> index 39843fa..a5906c5 100644
> --- a/fs/configfs/dir.c
> +++ b/fs/configfs/dir.c
> @@ -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).

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-01-03  8:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-03  8:11 [PATCH] configfs: Fix use-after-free when accessing sd->s_dentry Sahitya Tummala
2019-01-03  8:48 ` Al Viro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).