Skip to contentSkip to conversation

exfat: add cluster chain loop check for dir

PATCH Superseded Yuezhang Mo 2 participants 6 messages Jun 13, 2025 – Jun 18, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yuezhang Mo

author reviewer
#0 lore raw

PATCH 1/1 exfat: add cluster chain loop check for dir

An infinite loop may occur if the following conditions occur due to file system corruption.

(1) Condition for exfat_count_dir_entries() to loop infinitely.
- The cluster chain includes a loop.
- There is no UNUSED entry in the cluster chain.

(2) Condition for exfat_create_upcase_table() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and up-case table entry in the cluster chain of the root directory.

(3) Condition for exfat_load_bitmap() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and bitmap entry in the cluster chain of the root directory.

This commit adds checks in exfat_count_num_clusters() and exfat_count_dir_entries() to see if the cluster chain includes a loop, thus avoiding the above infinite loops.

---
 fs/exfat/dir.c    | 33 +++++++++++++++++++++------------
 fs/exfat/fatent.c | 10 ++++++++++
 fs/exfat/super.c  | 32 +++++++++++++++++++++-----------
 3 files changed, 52 insertions(+), 23 deletions(-)
fs/exfat/dir.c +21 -12
index 3103b932b674..467271ad4d71 100644
@@ -1194,7 +1194,8 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
 {
 	int i, count = 0;
 	int dentries_per_clu;
-	unsigned int entry_type;
+	unsigned int entry_type = TYPE_FILE;
+	unsigned int clu_count = 0;
 	struct exfat_chain clu;
 	struct exfat_dentry *ep;
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -1205,18 +1206,26 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
 	exfat_chain_dup(&clu, p_dir);
 
 	while (clu.dir != EXFAT_EOF_CLUSTER) {
-		for (i = 0; i < dentries_per_clu; i++) {
-			ep = exfat_get_dentry(sb, &clu, i, &bh);
-			if (!ep)
-				return -EIO;
-			entry_type = exfat_get_entry_type(ep);
-			brelse(bh);
+		clu_count++;
+		if (clu_count > sbi->used_clusters) {
+			exfat_fs_error(sb, "dir size or FAT or bitmap is corrupted");
+			return -EIO;
+		}
 
-			if (entry_type == TYPE_UNUSED)
-				return count;
-			if (entry_type != TYPE_DIR)
-				continue;
-			count++;
+		if (entry_type != TYPE_UNUSED) {
+			for (i = 0; i < dentries_per_clu; i++) {
+				ep = exfat_get_dentry(sb, &clu, i, &bh);
+				if (!ep)
+					return -EIO;
+				entry_type = exfat_get_entry_type(ep);
+				brelse(bh);
+
+				if (entry_type == TYPE_UNUSED)
+					break;
+				if (entry_type != TYPE_DIR)
+					continue;
+				count++;
+			}
 		}
 
 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
fs/exfat/fatent.c +10 -0
index 23065f948ae7..2a2615ca320f 100644
@@ -490,5 +490,15 @@ int exfat_count_num_clusters(struct super_block *sb,
 	}
 
 	*ret_count = count;
+
+	/*
+	 * since exfat_count_used_clusters() is not called, sbi->used_clusters
+	 * cannot be used here.
+	 */
+	if (i == sbi->num_clusters) {
+		exfat_fs_error(sb, "The cluster chain has a loop");
+		return -EIO;
+	}
+
 	return 0;
 }
fs/exfat/super.c +21 -11
index 7ed858937d45..3a9ec75ab452 100644
@@ -341,13 +341,12 @@ static void exfat_hash_init(struct super_block *sb)
 		INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
 }
 
-static int exfat_read_root(struct inode *inode)
+static int exfat_read_root(struct inode *inode, struct exfat_chain *root_clu)
 {
 	struct super_block *sb = inode->i_sb;
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
 	struct exfat_inode_info *ei = EXFAT_I(inode);
-	struct exfat_chain cdir;
-	int num_subdirs, num_clu = 0;
+	int num_subdirs;
 
 	exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
 	ei->entry = -1;
@@ -360,12 +359,9 @@ static int exfat_read_root(struct inode *inode)
 	ei->hint_stat.clu = sbi->root_dir;
 	ei->hint_femp.eidx = EXFAT_HINT_NONE;
 
-	exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
-	if (exfat_count_num_clusters(sb, &cdir, &num_clu))
-		return -EIO;
-	i_size_write(inode, num_clu << sbi->cluster_size_bits);
+	i_size_write(inode, EXFAT_CLU_TO_B(root_clu->size, sbi));
 
-	num_subdirs = exfat_count_dir_entries(sb, &cdir);
+	num_subdirs = exfat_count_dir_entries(sb, root_clu);
 	if (num_subdirs < 0)
 		return -EIO;
 	set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR);
@@ -578,7 +574,8 @@ static int exfat_verify_boot_region(struct super_block *sb)
 }
 
 /* mount the file system volume */
-static int __exfat_fill_super(struct super_block *sb)
+static int __exfat_fill_super(struct super_block *sb,
+		struct exfat_chain *root_clu)
 {
 	int ret;
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -595,6 +592,18 @@ static int __exfat_fill_super(struct super_block *sb)
 		goto free_bh;
 	}
 
+	/*
+	 * Call exfat_count_num_cluster() before searching for up-case and
+	 * bitmap directory entries to avoid infinite loop if they are missing
+	 * and the cluster chain includes a loop.
+	 */
+	exfat_chain_set(root_clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
+	ret = exfat_count_num_clusters(sb, root_clu, &root_clu->size);
+	if (ret) {
+		exfat_err(sb, "failed to count the number of clusters in root");
+		goto free_bh;
+	}
+
 	ret = exfat_create_upcase_table(sb);
 	if (ret) {
 		exfat_err(sb, "failed to load upcase table");
@@ -627,6 +636,7 @@ static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
 	struct exfat_sb_info *sbi = sb->s_fs_info;
 	struct exfat_mount_options *opts = &sbi->options;
 	struct inode *root_inode;
+	struct exfat_chain root_clu;
 	int err;
 
 	if (opts->allow_utime == (unsigned short)-1)
@@ -645,7 +655,7 @@ static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
 	sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS;
 	sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS;
 
-	err = __exfat_fill_super(sb);
+	err = __exfat_fill_super(sb, &root_clu);
 	if (err) {
 		exfat_err(sb, "failed to recognize exfat type");
 		goto check_nls_io;
@@ -680,7 +690,7 @@ static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
 
 	root_inode->i_ino = EXFAT_ROOT_INO;
 	inode_set_iversion(root_inode, 1);
-	err = exfat_read_root(root_inode);
+	err = exfat_read_root(root_inode, &root_clu);
 	if (err) {
 		exfat_err(sb, "failed to initialize root inode");
 		goto put_inode;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 45 lines · Yuezhang Mo

An infinite loop may occur if the following conditions occur due to file system corruption.

(1) Condition for exfat_count_dir_entries() to loop infinitely.
- The cluster chain includes a loop.
- There is no UNUSED entry in the cluster chain.

(2) Condition for exfat_create_upcase_table() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and up-case table entry in the cluster chain of the root directory.

(3) Condition for exfat_load_bitmap() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and bitmap entry in the cluster chain of the root directory.

This commit adds checks in exfat_count_num_clusters() and exfat_count_dir_entries() to see if the cluster chain includes a loop, thus avoiding the above infinite loops.

---
 fs/exfat/dir.c    | 33 +++++++++++++++++++++------------
 fs/exfat/fatent.c | 10 ++++++++++
 fs/exfat/super.c  | 32 +++++++++++++++++++++-----------
 3 files changed, 52 insertions(+), 23 deletions(-)
fs/exfat/dir.c +2 -1
index 3103b932b674..467271ad4d71 100644
@@ -1194,7 +1194,8 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
 {
        int i, count = 0;
        int dentries_per_clu;
-       unsigned int entry_type;
+       unsigned int entry_type = TYPE_FILE;
+       unsigned int clu_count = 0;
        struct exfat_chain clu;
        struct exfat_dentry *ep;
        struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -1205,18 +1206,26 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
        exfat_chain_dup(&clu, p_dir);

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1210

Quoted 10 lines
fs/exfat/dir.c +2 -6
 
        while (clu.dir != EXFAT_EOF_CLUSTER) {
-               for (i = 0; i < dentries_per_clu; i++) {
-                       ep = exfat_get_dentry(sb, &clu, i, &bh);
-                       if (!ep)
-                               return -EIO;
-                       entry_type = exfat_get_entry_type(ep);
-                       brelse(bh);
+               clu_count++;
+               if (clu_count > sbi->used_clusters) {
                    if (++clu_count > sbi->used_clusters) {
Quoted 9 lines
fs/exfat/dir.c +3 -5
+                       exfat_fs_error(sb, "dir size or FAT or bitmap is corrupted");
+                       return -EIO;
+               }
 
-                       if (entry_type == TYPE_UNUSED)
-                               return count;
-                       if (entry_type != TYPE_DIR)
-                               continue;
-                       count++;

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1224

Quoted 10 lines
fs/exfat/dir.c +10 -0
+               if (entry_type != TYPE_UNUSED) {
+                       for (i = 0; i < dentries_per_clu; i++) {
+                               ep = exfat_get_dentry(sb, &clu, i, &bh);
+                               if (!ep)
+                                       return -EIO;
+                               entry_type = exfat_get_entry_type(ep);
+                               brelse(bh);
+
+                               if (entry_type == TYPE_UNUSED)
+                                       break;

Is there any reason why you keep doing loop even though you found an unused entry?

Quoted 12 lines
fs/exfat/dir.c +4 -0
+                               if (entry_type != TYPE_DIR)
+                                       continue;
+                               count++;
+                       }
                }
 
                if (clu.flags == ALLOC_NO_FAT_CHAIN) {
fs/exfat/fatent.c +0 -0
index 23065f948ae7..2a2615ca320f 100644

fs/exfat/fatent.c int exfat_count_num_clusters(struct super_block *sb, · line 498

Quoted 9 lines
fs/exfat/fatent.c +6 -0
@@ -490,5 +490,15 @@ int exfat_count_num_clusters(struct super_block *sb,
        }
 
        *ret_count = count;
+
+       /*
+        * since exfat_count_used_clusters() is not called, sbi->used_clusters
+        * cannot be used here.
+        */
+       if (i == sbi->num_clusters) {

This is also right, But to make it more clear, wouldn't it be better to do clu != EXFAT_EOF_CLUSTER?

Thanks.

Quoted 6 lines
fs/exfat/fatent.c +4 -0
+               exfat_fs_error(sb, "The cluster chain has a loop");
+               return -EIO;
+       }
+
        return 0;
 }
author reviewer
#2 lore raw
Quoted 47 lines · Yuezhang Mo
Quoted 46 lines

An infinite loop may occur if the following conditions occur due to file system corruption.

(1) Condition for exfat_count_dir_entries() to loop infinitely.
- The cluster chain includes a loop.
- There is no UNUSED entry in the cluster chain.

(2) Condition for exfat_create_upcase_table() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and up-case table entry in the cluster chain of the root directory.

(3) Condition for exfat_load_bitmap() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and bitmap entry in the cluster chain of the root directory.

This commit adds checks in exfat_count_num_clusters() and exfat_count_dir_entries() to see if the cluster chain includes a loop, thus avoiding the above infinite loops.

---
 fs/exfat/dir.c    | 33 +++++++++++++++++++++------------
 fs/exfat/fatent.c | 10 ++++++++++
 fs/exfat/super.c  | 32 +++++++++++++++++++++-----------
 3 files changed, 52 insertions(+), 23 deletions(-)
fs/exfat/dir.c +2 -1
index 3103b932b674..467271ad4d71 100644
@@ -1194,7 +1194,8 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
 {
        int i, count = 0;
        int dentries_per_clu;
-       unsigned int entry_type;
+       unsigned int entry_type = TYPE_FILE;
+       unsigned int clu_count = 0;
        struct exfat_chain clu;
        struct exfat_dentry *ep;
        struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -1205,18 +1206,26 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
        exfat_chain_dup(&clu, p_dir);
 
Quoted 10 lines

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1210

Quoted 9 lines
fs/exfat/dir.c +2 -6
        while (clu.dir != EXFAT_EOF_CLUSTER) {
-               for (i = 0; i < dentries_per_clu; i++) {
-                       ep = exfat_get_dentry(sb, &clu, i, &bh);
-                       if (!ep)
-                               return -EIO;
-                       entry_type = exfat_get_entry_type(ep);
-                       brelse(bh);
+               clu_count++;
+               if (clu_count > sbi->used_clusters) {
                    if (++clu_count > sbi->used_clusters) {

Well, that's more concise.

Quoted 19 lines
Quoted 9 lines
fs/exfat/dir.c +3 -5
+                       exfat_fs_error(sb, "dir size or FAT or bitmap is corrupted");
+                       return -EIO;
+               }
 
-                       if (entry_type == TYPE_UNUSED)
-                               return count;
-                       if (entry_type != TYPE_DIR)
-                               continue;
-                       count++;

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1224

Quoted 10 lines
fs/exfat/dir.c +10 -0
+               if (entry_type != TYPE_UNUSED) {
+                       for (i = 0; i < dentries_per_clu; i++) {
+                               ep = exfat_get_dentry(sb, &clu, i, &bh);
+                               if (!ep)
+                                       return -EIO;
+                               entry_type = exfat_get_entry_type(ep);
+                               brelse(bh);
+
+                               if (entry_type == TYPE_UNUSED)
+                                       break;
Quoted 2 lines

Is there any reason why you keep doing loop even though you found an unused entry?

There are unused directory entries when calling this func, but there may be none after files are created. That will cause a infinite loop in exfat_check_dir_empty() and exfat_find_dir_entry().

Quoted 15 lines
Quoted 15 lines
fs/exfat/dir.c +4 -0
+                               if (entry_type != TYPE_DIR)
+                                       continue;
+                               count++;
+                       }
                }
 
                if (clu.flags == ALLOC_NO_FAT_CHAIN) {
fs/exfat/fatent.c +0 -0
index 23065f948ae7..2a2615ca320f 100644
@@ -490,5 +490,15 @@ int exfat_count_num_clusters(struct super_block *sb,
        }
 
Quoted 9 lines

fs/exfat/fatent.c int exfat_count_num_clusters(struct super_block *sb, · line 498

Quoted 7 lines
fs/exfat/fatent.c +6 -0
        *ret_count = count;
+
+       /*
+        * since exfat_count_used_clusters() is not called, sbi->used_clusters
+        * cannot be used here.
+        */
+       if (i == sbi->num_clusters) {

This is also right, But to make it more clear, wouldn't it be better to do clu != EXFAT_EOF_CLUSTER?

OK, I will add this.

Quoted 8 lines

Thanks.

Quoted 6 lines
fs/exfat/fatent.c +4 -0
+               exfat_fs_error(sb, "The cluster chain has a loop");
+               return -EIO;
+       }
+
        return 0;
 }
maintainer
#3 lore raw
Quoted 80 lines · On Tue, Jun 17, 2025 at 2:38 PM [email protected] <[email protected]>…
Quoted 48 lines · Yuezhang Mo
Quoted 46 lines

An infinite loop may occur if the following conditions occur due to file system corruption.

(1) Condition for exfat_count_dir_entries() to loop infinitely.
- The cluster chain includes a loop.
- There is no UNUSED entry in the cluster chain.

(2) Condition for exfat_create_upcase_table() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and up-case table entry in the cluster chain of the root directory.

(3) Condition for exfat_load_bitmap() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and bitmap entry in the cluster chain of the root directory.

This commit adds checks in exfat_count_num_clusters() and exfat_count_dir_entries() to see if the cluster chain includes a loop, thus avoiding the above infinite loops.

---
 fs/exfat/dir.c    | 33 +++++++++++++++++++++------------
 fs/exfat/fatent.c | 10 ++++++++++
 fs/exfat/super.c  | 32 +++++++++++++++++++++-----------
 3 files changed, 52 insertions(+), 23 deletions(-)
fs/exfat/dir.c +2 -1
index 3103b932b674..467271ad4d71 100644
@@ -1194,7 +1194,8 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
 {
        int i, count = 0;
        int dentries_per_clu;
-       unsigned int entry_type;
+       unsigned int entry_type = TYPE_FILE;
+       unsigned int clu_count = 0;
        struct exfat_chain clu;
        struct exfat_dentry *ep;
        struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -1205,18 +1206,26 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
        exfat_chain_dup(&clu, p_dir);
 
Quoted 10 lines

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1210

Quoted 9 lines
fs/exfat/dir.c +2 -6
        while (clu.dir != EXFAT_EOF_CLUSTER) {
-               for (i = 0; i < dentries_per_clu; i++) {
-                       ep = exfat_get_dentry(sb, &clu, i, &bh);
-                       if (!ep)
-                               return -EIO;
-                       entry_type = exfat_get_entry_type(ep);
-                       brelse(bh);
+               clu_count++;
+               if (clu_count > sbi->used_clusters) {
                    if (++clu_count > sbi->used_clusters) {

Well, that's more concise.

Quoted 19 lines
Quoted 19 lines
fs/exfat/dir.c +13 -5
+                       exfat_fs_error(sb, "dir size or FAT or bitmap is corrupted");
+                       return -EIO;
+               }
 
-                       if (entry_type == TYPE_UNUSED)
-                               return count;
-                       if (entry_type != TYPE_DIR)
-                               continue;
-                       count++;
+               if (entry_type != TYPE_UNUSED) {
+                       for (i = 0; i < dentries_per_clu; i++) {
+                               ep = exfat_get_dentry(sb, &clu, i, &bh);
+                               if (!ep)
+                                       return -EIO;
+                               entry_type = exfat_get_entry_type(ep);
+                               brelse(bh);
+
+                               if (entry_type == TYPE_UNUSED)
+                                       break;
Quoted 6 lines
Quoted 2 lines

Is there any reason why you keep doing loop even though you found an unused entry?

There are unused directory entries when calling this func, but there may be none after files are created. That will cause a infinite loop in exfat_check_dir_empty() and exfat_find_dir_entry().

Isn't the infinite loop issue in this patch improved by checking clu_count and ->used_clusters?
If it reaches an unused entry, how about returning right away like before?

Thanks.

Quoted 37 lines
Quoted 16 lines
Quoted 16 lines
fs/exfat/dir.c +4 -0
+                               if (entry_type != TYPE_DIR)
+                                       continue;
+                               count++;
+                       }
                }
 
                if (clu.flags == ALLOC_NO_FAT_CHAIN) {
fs/exfat/fatent.c +0 -0
index 23065f948ae7..2a2615ca320f 100644
@@ -490,5 +490,15 @@ int exfat_count_num_clusters(struct super_block *sb,
        }
 
Quoted 9 lines

fs/exfat/fatent.c int exfat_count_num_clusters(struct super_block *sb, · line 498

Quoted 7 lines
fs/exfat/fatent.c +6 -0
        *ret_count = count;
+
+       /*
+        * since exfat_count_used_clusters() is not called, sbi->used_clusters
+        * cannot be used here.
+        */
+       if (i == sbi->num_clusters) {

This is also right, But to make it more clear, wouldn't it be better to do clu != EXFAT_EOF_CLUSTER?

OK, I will add this.

Quoted 8 lines

Thanks.

Quoted 6 lines
fs/exfat/fatent.c +4 -0
+               exfat_fs_error(sb, "The cluster chain has a loop");
+               return -EIO;
+       }
+
        return 0;
 }
author reviewer
#4 lore raw
Quoted 85 lines
Quoted 83 lines · On Tue, Jun 17, 2025 at 2:38 PM [email protected] <[email protected]>…
Quoted 48 lines · Yuezhang Mo
Quoted 46 lines

An infinite loop may occur if the following conditions occur due to file system corruption.

(1) Condition for exfat_count_dir_entries() to loop infinitely.
- The cluster chain includes a loop.
- There is no UNUSED entry in the cluster chain.

(2) Condition for exfat_create_upcase_table() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and up-case table entry in the cluster chain of the root directory.

(3) Condition for exfat_load_bitmap() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and bitmap entry in the cluster chain of the root directory.

This commit adds checks in exfat_count_num_clusters() and exfat_count_dir_entries() to see if the cluster chain includes a loop, thus avoiding the above infinite loops.

---
 fs/exfat/dir.c    | 33 +++++++++++++++++++++------------
 fs/exfat/fatent.c | 10 ++++++++++
 fs/exfat/super.c  | 32 +++++++++++++++++++++-----------
 3 files changed, 52 insertions(+), 23 deletions(-)
fs/exfat/dir.c +2 -1
index 3103b932b674..467271ad4d71 100644
@@ -1194,7 +1194,8 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
 {
        int i, count = 0;
        int dentries_per_clu;
-       unsigned int entry_type;
+       unsigned int entry_type = TYPE_FILE;
+       unsigned int clu_count = 0;
        struct exfat_chain clu;
        struct exfat_dentry *ep;
        struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -1205,18 +1206,26 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
        exfat_chain_dup(&clu, p_dir);
 
Quoted 10 lines

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1210

Quoted 9 lines
fs/exfat/dir.c +2 -6
        while (clu.dir != EXFAT_EOF_CLUSTER) {
-               for (i = 0; i < dentries_per_clu; i++) {
-                       ep = exfat_get_dentry(sb, &clu, i, &bh);
-                       if (!ep)
-                               return -EIO;
-                       entry_type = exfat_get_entry_type(ep);
-                       brelse(bh);
+               clu_count++;
+               if (clu_count > sbi->used_clusters) {
                    if (++clu_count > sbi->used_clusters) {

Well, that's more concise.

Quoted 21 lines
Quoted 9 lines
fs/exfat/dir.c +3 -5
+                       exfat_fs_error(sb, "dir size or FAT or bitmap is corrupted");
+                       return -EIO;
+               }
 
-                       if (entry_type == TYPE_UNUSED)
-                               return count;
-                       if (entry_type != TYPE_DIR)
-                               continue;
-                       count++;

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1224

Quoted 10 lines
fs/exfat/dir.c +10 -0
+               if (entry_type != TYPE_UNUSED) {
+                       for (i = 0; i < dentries_per_clu; i++) {
+                               ep = exfat_get_dentry(sb, &clu, i, &bh);
+                               if (!ep)
+                                       return -EIO;
+                               entry_type = exfat_get_entry_type(ep);
+                               brelse(bh);
+
+                               if (entry_type == TYPE_UNUSED)
+                                       break;

Is there any reason why you keep doing loop even though you found an unused entry?

Quoted 7 lines
Quoted 3 lines

There are unused directory entries when calling this func, but there may be none after files are created. That will cause a infinite loop in exfat_check_dir_empty() and exfat_find_dir_entry().

Isn't the infinite loop issue in this patch improved by checking clu_count and ->used_clusters?
If it reaches an unused entry, how about returning right away like before?

It reaches an unused directory entry and exits the loop, but it does not find that the cluster chain includes a loop. After using up these unused directory entries, an infinite loop will occur in exfat_check_dir_empty() and exfat_find_dir_entry(). To avoid this, it continues traversing to ensure that the cluster chain does not include a loop.

For an un-corrupted file system, unused directory entries will only in the last cluster, so this only makes FAT is read one more time. What concerns do you have about this?

maintainer
#5 lore raw
Quoted 100 lines · On Wed, Jun 18, 2025 at 8:10 PM [email protected] <[email protected]>…
Quoted 86 lines
Quoted 83 lines · On Tue, Jun 17, 2025 at 2:38 PM [email protected] <[email protected]>…
Quoted 48 lines · Yuezhang Mo
Quoted 46 lines

An infinite loop may occur if the following conditions occur due to file system corruption.

(1) Condition for exfat_count_dir_entries() to loop infinitely.
- The cluster chain includes a loop.
- There is no UNUSED entry in the cluster chain.

(2) Condition for exfat_create_upcase_table() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and up-case table entry in the cluster chain of the root directory.

(3) Condition for exfat_load_bitmap() to loop infinitely.
- The cluster chain of the root directory includes a loop.
- There are no UNUSED entry and bitmap entry in the cluster chain of the root directory.

This commit adds checks in exfat_count_num_clusters() and exfat_count_dir_entries() to see if the cluster chain includes a loop, thus avoiding the above infinite loops.

---
 fs/exfat/dir.c    | 33 +++++++++++++++++++++------------
 fs/exfat/fatent.c | 10 ++++++++++
 fs/exfat/super.c  | 32 +++++++++++++++++++++-----------
 3 files changed, 52 insertions(+), 23 deletions(-)
fs/exfat/dir.c +2 -1
index 3103b932b674..467271ad4d71 100644
@@ -1194,7 +1194,8 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
 {
        int i, count = 0;
        int dentries_per_clu;
-       unsigned int entry_type;
+       unsigned int entry_type = TYPE_FILE;
+       unsigned int clu_count = 0;
        struct exfat_chain clu;
        struct exfat_dentry *ep;
        struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -1205,18 +1206,26 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
        exfat_chain_dup(&clu, p_dir);
 
Quoted 10 lines

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1210

Quoted 9 lines
fs/exfat/dir.c +2 -6
        while (clu.dir != EXFAT_EOF_CLUSTER) {
-               for (i = 0; i < dentries_per_clu; i++) {
-                       ep = exfat_get_dentry(sb, &clu, i, &bh);
-                       if (!ep)
-                               return -EIO;
-                       entry_type = exfat_get_entry_type(ep);
-                       brelse(bh);
+               clu_count++;
+               if (clu_count > sbi->used_clusters) {
                    if (++clu_count > sbi->used_clusters) {

Well, that's more concise.

Quoted 21 lines
Quoted 9 lines
fs/exfat/dir.c +3 -5
+                       exfat_fs_error(sb, "dir size or FAT or bitmap is corrupted");
+                       return -EIO;
+               }
 
-                       if (entry_type == TYPE_UNUSED)
-                               return count;
-                       if (entry_type != TYPE_DIR)
-                               continue;
-                       count++;

fs/exfat/dir.c int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) · line 1224

Quoted 10 lines
fs/exfat/dir.c +10 -0
+               if (entry_type != TYPE_UNUSED) {
+                       for (i = 0; i < dentries_per_clu; i++) {
+                               ep = exfat_get_dentry(sb, &clu, i, &bh);
+                               if (!ep)
+                                       return -EIO;
+                               entry_type = exfat_get_entry_type(ep);
+                               brelse(bh);
+
+                               if (entry_type == TYPE_UNUSED)
+                                       break;

Is there any reason why you keep doing loop even though you found an unused entry?

Quoted 7 lines
Quoted 3 lines

There are unused directory entries when calling this func, but there may be none after files are created. That will cause a infinite loop in exfat_check_dir_empty() and exfat_find_dir_entry().

Isn't the infinite loop issue in this patch improved by checking clu_count and ->used_clusters?
If it reaches an unused entry, how about returning right away like before?

It reaches an unused directory entry and exits the loop, but it does not find that the cluster chain includes a loop. After using up these unused directory entries, an infinite loop will occur in exfat_check_dir_empty() and exfat_find_dir_entry(). To avoid this, it continues traversing to ensure that the cluster chain does not include a loop.

Quoted 3 lines

For an un-corrupted file system, unused directory entries will only in the last cluster, so this only makes FAT is read one more time. What concerns do you have about this?

You described there is a potential infinite loop when there is no unused entry in the patch description. There is an unused type check to break a loop in exfat_check_dir_empty and exfat_find_dir_entry. I can not understand how there can be an infinite loop if there is an unused entry. So there is no need to strictly check the self-linked chain in exfat_count_dir_entries when there is an unused entry. It can be treated as an error even though it can be handled normally.