[PATCH v2] exfat: skip FAT walk for out-of-range readdir offsets
From: Shawn Shao
Date: Tue Dec 01 2026 - 09:19:28 EST
Hello Namjae,
Thanks for the suggestions. This version uses `EXFAT_DATA_CLUSTER_COUNT()`
when calculating the maximum dentry count, excluding the two reserved
cluster values included in `num_clusters`. It compares the original
`loff_t` position with the byte limit before converting it to a dentry
index, so a large offset cannot wrap during the narrowing conversion.
The check returns through the existing end-of-directory path without
walking the FAT. The maximum-offset cyclic-image case now returns promptly;
the offset-zero case still returns 48 bytes. The acyclic control and the
concurrent-create probe also return promptly.
Tested on Linux 7.4-rc4, commit
7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree
194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU 6.2.0.
The x86-64 bzImage build completed. The reproducer, updated kernel
configuration, patch, and v2 console logs are collected here:
https://gist.github.com/shaos26/d4b02e7959f89c86725e379c1abb6a96
Changes in v2:
- Bound the dentry count using `EXFAT_DATA_CLUSTER_COUNT(sbi)`.
- Check `*cpos` against the byte limit before converting it to a dentry.
Fixes: 8534eb11ac05 ("exfat: fix reading a directory whose FAT chain is longer than its size")
Cc: stable@xxxxxxxxxxxxxxx
Thanks,
Shawn Shao
Signed-off-by: Shawn Shao <shawnq1@xxxxxxx>
---
diff --git a/fs/exfat/dir.c b/fs/exfat/dir.c
index 46514b13bebda..a04f76e98f3f4 100644
--- a/fs/exfat/dir.c
+++ b/fs/exfat/dir.c
@@ -76,7 +76,7 @@ static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_ent
struct super_block *sb = inode->i_sb;
struct exfat_sb_info *sbi = EXFAT_SB(sb);
struct exfat_inode_info *ei = EXFAT_I(inode);
- unsigned int dentry = exfat_bytes_to_dentries(*cpos) & 0xFFFFFFFF;
+ unsigned int dentry;
struct buffer_head *bh;
/* check if the given file ID is opened */
@@ -88,7 +88,13 @@ static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_ent
dentries_per_clu = sbi->dentries_per_clu;
max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
- exfat_cluster_to_dentries(sbi, sbi->num_clusters));
+ exfat_cluster_to_dentries(sbi,
+ EXFAT_DATA_CLUSTER_COUNT(sbi)));
+ if (*cpos >= exfat_dentries_to_bytes(max_dentries)) {
+ dentry = max_dentries;
+ goto out;
+ }
+ dentry = exfat_bytes_to_dentries(*cpos);
clu_offset = exfat_dentries_to_cluster(sbi, dentry);
exfat_chain_dup(&clu, &dir);