linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] exfat: fix refcount leak in exfat_find
@ 2025-10-20 15:00 Shuhao Fu
  2025-10-21  1:38 ` Yuezhang.Mo
  0 siblings, 1 reply; 5+ messages in thread
From: Shuhao Fu @ 2025-10-20 15:00 UTC (permalink / raw)
  To: Namjae Jeon, Sungjong Seo; +Cc: Yuezhang Mo, linux-fsdevel, linux-kernel

Fix refcount leaks in `exfat_find` related to `exfat_get_dentry_set`.

Function `exfat_get_dentry_set` would increase the reference counter of 
`es->bh` on success. Therefore, `exfat_put_dentry_set` must be called
after `exfat_get_dentry_set` to ensure refcount consistency. In 
`exfat_find`, two branchs fail to call `exfat_put_dentry_set`, leading
to possible resource leaks.

Fixes: 82ebecdc74ff ("exfat: fix improper check of dentry.stream.valid_size")
Fixes: 13940cef9549 ("exfat: add a check for invalid data size")
Signed-off-by: Shuhao Fu <sfual@cse.ust.hk>
---
 fs/exfat/namei.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c
index 745dce29d..083a9d790 100644
--- a/fs/exfat/namei.c
+++ b/fs/exfat/namei.c
@@ -646,11 +646,13 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
 	info->size = le64_to_cpu(ep2->dentry.stream.size);
 
 	if (info->valid_size < 0) {
+		exfat_put_dentry_set(&es, false);
 		exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
 		return -EIO;
 	}
 
 	if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
+		exfat_put_dentry_set(&es, false);
 		exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
 		return -EIO;
 	}
-- 
2.39.5 (Apple Git-154)


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

* Re: [PATCH] exfat: fix refcount leak in exfat_find
  2025-10-20 15:00 [PATCH] exfat: fix refcount leak in exfat_find Shuhao Fu
@ 2025-10-21  1:38 ` Yuezhang.Mo
  2025-10-21  8:04   ` Shuhao Fu
  0 siblings, 1 reply; 5+ messages in thread
From: Yuezhang.Mo @ 2025-10-21  1:38 UTC (permalink / raw)
  To: Shuhao Fu, Namjae Jeon, Sungjong Seo
  Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org

On Mon, Oct 20, 2025 23:00 Shuhao Fu <sfual@cse.ust.hk> wrote:
> Fix refcount leaks in `exfat_find` related to `exfat_get_dentry_set`.
> 
> Function `exfat_get_dentry_set` would increase the reference counter of
> `es->bh` on success. Therefore, `exfat_put_dentry_set` must be called
> after `exfat_get_dentry_set` to ensure refcount consistency. In
> `exfat_find`, two branchs fail to call `exfat_put_dentry_set`, leading
> to possible resource leaks.
> 
> Fixes: 82ebecdc74ff ("exfat: fix improper check of dentry.stream.valid_size")
> Fixes: 13940cef9549 ("exfat: add a check for invalid data size")
> Signed-off-by: Shuhao Fu <sfual@cse.ust.hk>
> ---
>  fs/exfat/namei.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c
> index 745dce29d..083a9d790 100644
> --- a/fs/exfat/namei.c
> +++ b/fs/exfat/namei.c
> @@ -646,11 +646,13 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
>         info->size = le64_to_cpu(ep2->dentry.stream.size);
> 
>         if (info->valid_size < 0) {
> +               exfat_put_dentry_set(&es, false);
>                 exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
>                 return -EIO;
>         }
> 
>         if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
> +               exfat_put_dentry_set(&es, false);
>                 exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
>                 return -EIO;
>         }

Thanks for your patch.

I think it would be better to move these checks after exfat_put_dentry_set().
Because the following check will correct ->valid_size and ->size.

        if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
                exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
                                info->start_clu);
                info->size = 0;
                info->valid_size = 0;
        }

> --
> 2.39.5 (Apple Git-154)

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

* Re: [PATCH] exfat: fix refcount leak in exfat_find
  2025-10-21  1:38 ` Yuezhang.Mo
@ 2025-10-21  8:04   ` Shuhao Fu
  2025-10-21  8:21     ` Yuezhang.Mo
  0 siblings, 1 reply; 5+ messages in thread
From: Shuhao Fu @ 2025-10-21  8:04 UTC (permalink / raw)
  To: Yuezhang.Mo@sony.com
  Cc: Namjae Jeon, Sungjong Seo, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Tue, Oct 21, 2025 at 01:38:29AM +0000, Yuezhang.Mo@sony.com wrote:
> On Mon, Oct 20, 2025 23:00 Shuhao Fu <sfual@cse.ust.hk> wrote:
> 
> I think it would be better to move these checks after exfat_put_dentry_set().
> Because the following check will correct ->valid_size and ->size.
> 
>         if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
>                 exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
>                                 info->start_clu);
>                 info->size = 0;
>                 info->valid_size = 0;
>         }
> 

Do you mean that we should put these two checks after
`exfat_put_dentry_set`, like below?

@@ -645,18 +645,6 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
 	info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size);
 	info->size = le64_to_cpu(ep2->dentry.stream.size);
 
-	if (info->valid_size < 0) {
-		exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
-		return -EIO;
-	}
-
-	if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
-		exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
-		return -EIO;
-	}
-
 	info->start_clu = le32_to_cpu(ep2->dentry.stream.start_clu);
 	if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
 		exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
@@ -694,6 +682,16 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
 			     0);
 	exfat_put_dentry_set(&es, false);
 
+	if (info->valid_size < 0) {
+		exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
+		return -EIO;
+	}
+
+	if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
+		exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
+		return -EIO;
+	}
+
 	if (ei->start_clu == EXFAT_FREE_CLUSTER) {
 		exfat_fs_error(sb,
 			       "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",

> > --
> > 2.39.5 (Apple Git-154)

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

* Re: [PATCH] exfat: fix refcount leak in exfat_find
  2025-10-21  8:04   ` Shuhao Fu
@ 2025-10-21  8:21     ` Yuezhang.Mo
  2025-10-21  8:47       ` Shuhao Fu
  0 siblings, 1 reply; 5+ messages in thread
From: Yuezhang.Mo @ 2025-10-21  8:21 UTC (permalink / raw)
  To: Shuhao Fu
  Cc: Namjae Jeon, Sungjong Seo, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Tue, Oct 21, 2025 16:04 Shuhao Fu <sfual@cse.ust.hk> wrote:
> On Tue, Oct 21, 2025 at 01:38:29AM +0000, Yuezhang.Mo@sony.com wrote:
> > On Mon, Oct 20, 2025 23:00 Shuhao Fu <sfual@cse.ust.hk> wrote:
> > 
> > I think it would be better to move these checks after exfat_put_dentry_set().
> > Because the following check will correct ->valid_size and ->size.
> > 
> >         if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
> >                 exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
> >                                 info->start_clu);
> >                 info->size = 0;
> >                 info->valid_size = 0;
> >         }
> > 
> 
> Do you mean that we should put these two checks after
> `exfat_put_dentry_set`, like below?
> 

Yes, that's what I mean.

> @@ -645,18 +645,6 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
>  	info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size);
>  	info->size = le64_to_cpu(ep2->dentry.stream.size);
>  
> -	if (info->valid_size < 0) {
> -		exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
> -		return -EIO;
> -	}
> -
> -	if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
> -		exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
> -		return -EIO;
> -	}
> -
>  	info->start_clu = le32_to_cpu(ep2->dentry.stream.start_clu);
>  	if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
>  		exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
> @@ -694,6 +682,16 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
>  			     0);
>  	exfat_put_dentry_set(&es, false);
>  
> +	if (info->valid_size < 0) {
> +		exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
> +		return -EIO;
> +	}
> +
> +	if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
> +		exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
> +		return -EIO;
> +	}
> +
>  	if (ei->start_clu == EXFAT_FREE_CLUSTER) {
>  		exfat_fs_error(sb,
>  			       "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
> 
> > > --
> > > 2.39.5 (Apple Git-154)
> 

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

* Re: [PATCH] exfat: fix refcount leak in exfat_find
  2025-10-21  8:21     ` Yuezhang.Mo
@ 2025-10-21  8:47       ` Shuhao Fu
  0 siblings, 0 replies; 5+ messages in thread
From: Shuhao Fu @ 2025-10-21  8:47 UTC (permalink / raw)
  To: Yuezhang.Mo@sony.com
  Cc: Namjae Jeon, Sungjong Seo, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Tue, Oct 21, 2025 at 08:21:57AM +0000, Yuezhang.Mo@sony.com wrote:
> On Tue, Oct 21, 2025 16:04 Shuhao Fu <sfual@cse.ust.hk> wrote:
> > On Tue, Oct 21, 2025 at 01:38:29AM +0000, Yuezhang.Mo@sony.com wrote:
> > > On Mon, Oct 20, 2025 23:00 Shuhao Fu <sfual@cse.ust.hk> wrote:
> > > 
> > > I think it would be better to move these checks after exfat_put_dentry_set().
> > > Because the following check will correct ->valid_size and ->size.
> > > 
> > >         if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
> > >                 exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
> > >                                 info->start_clu);
> > >                 info->size = 0;
> > >                 info->valid_size = 0;
> > >         }
> > > 
> > 
> > Do you mean that we should put these two checks after
> > `exfat_put_dentry_set`, like below?
> > 
> 
> Yes, that's what I mean.
> 

Thank you for your suggestion. Patch v2 has been sent out addressing
your comments.

https://lore.kernel.org/linux-fsdevel/aPdHWFiCupwDRiFM@osx.local/

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

end of thread, other threads:[~2025-10-21  8:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-20 15:00 [PATCH] exfat: fix refcount leak in exfat_find Shuhao Fu
2025-10-21  1:38 ` Yuezhang.Mo
2025-10-21  8:04   ` Shuhao Fu
2025-10-21  8:21     ` Yuezhang.Mo
2025-10-21  8:47       ` Shuhao Fu

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).