Skip to contentSkip to conversation

exfat: fix refcount leak in exfat_find

PATCH Superseded Shuhao Fu 2 participants 5 messages Oct 20, 2025 – Oct 21, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Shuhao Fu

author
#0 lore raw

PATCH 1/1 exfat: fix refcount leak in exfat_find

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")
---
 fs/exfat/namei.c | 2 ++
 1 file changed, 2 insertions(+)
fs/exfat/namei.c +2 -0
index 745dce29d..083a9d790 100644
@@ -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;
 	}
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
Quoted 23 lines · On Mon, Oct 20, 2025 23:00 Shuhao Fu <[email protected]> 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")
---
 fs/exfat/namei.c | 2 ++
 1 file changed, 2 insertions(+)
fs/exfat/namei.c +0 -0
index 745dce29d..083a9d790 100644
@@ -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) {

fs/exfat/namei.c static int exfat_find(struct inode *dir, const struct qstr *qname, · line 658

Quoted 10 lines
fs/exfat/namei.c +2 -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;
        }
Quoted 2 lines

--
2.39.5 (Apple Git-154)

author
#2 lore raw
Quoted 5 lines · On Tue, Oct 21, 2025 at 01:38:29AM +0000, [email protected] wrote:

On Mon, Oct 20, 2025 23:00 Shuhao Fu <[email protected]> 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.

Quoted 7 lines
        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)",

Quoted 2 lines
Quoted 2 lines

--
2.39.5 (Apple Git-154)

#3 lore raw
Quoted 14 lines · On Tue, Oct 21, 2025 16:04 Shuhao Fu <[email protected]> wrote:
Quoted 5 lines · On Tue, Oct 21, 2025 at 01:38:29AM +0000, [email protected] wrote:

On Mon, Oct 20, 2025 23:00 Shuhao Fu <[email protected]> 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.

Quoted 7 lines
        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;
        }
Quoted 3 lines

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

Yes, that's what I mean.

Quoted 38 lines
(quoted diff) +10 -10
@@ -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)",
Quoted 3 lines
Quoted 3 lines

--
2.39.5 (Apple Git-154)

author
#4 lore raw
Quoted 15 lines · On Tue, Oct 21, 2025 at 08:21:57AM +0000, [email protected] wrote:
Quoted 14 lines · On Tue, Oct 21, 2025 16:04 Shuhao Fu <[email protected]> wrote:
Quoted 12 lines · On Tue, Oct 21, 2025 at 01:38:29AM +0000, [email protected] wrote:

On Mon, Oct 20, 2025 23:00 Shuhao Fu <[email protected]> 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;
        }
Quoted 6 lines
Quoted 3 lines

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/[email protected]/