From: David Timber <dxdt@dev.snart.me>
To: Namjae Jeon <linkinjeon@kernel.org>,
Sungjong Seo <sj1557.seo@samsung.com>,
Yuezhang Mo <yuezhang.mo@sony.com>
Cc: linux-fsdevel@vger.kernel.org, David Timber <dxdt@dev.snart.me>
Subject: [PATCH v2 4/4] exfat: more pedantic upcase table validity check
Date: Tue, 5 May 2026 21:31:44 +0900 [thread overview]
Message-ID: <20260505123144.730782-5-dxdt@dev.snart.me> (raw)
In-Reply-To: <20260505123144.730782-1-dxdt@dev.snart.me>
It is observed that most exFAT implementations reject a volume with an
upcase table whose index of the last entry is not 0xFFFF and treat the
volume as damaged.
Upon encoutering an incomplete or malformed upcase table:
- whose index of last entry is not 0xFFFF
- that has extra data after the end of the table
Raise exfat_fs_error() to mark the volume read-only.
Signed-off-by: David Timber <dxdt@dev.snart.me>
---
fs/exfat/nls.c | 90 +++++++++++++++++++++++++++-----------------------
1 file changed, 49 insertions(+), 41 deletions(-)
diff --git a/fs/exfat/nls.c b/fs/exfat/nls.c
index 68b09a99f8be..61b191a79fb6 100644
--- a/fs/exfat/nls.c
+++ b/fs/exfat/nls.c
@@ -283,42 +283,45 @@ int exfat_nls_to_utf16(struct super_block *sb, const unsigned char *p_cstring,
return exfat_nls_to_ucs2(sb, p_cstring, len, uniname, p_lossy);
}
-static int exfat_load_upcase_table(struct super_block *sb,
- sector_t sector, unsigned long long num_sectors,
- unsigned int utbl_checksum)
+static int exfat_load_upcase_table(struct super_block *sb, sector_t sector,
+ unsigned long long tbl_size, unsigned int utbl_checksum)
{
struct exfat_sb_info *sbi = EXFAT_SB(sb);
- unsigned int sect_size = sb->s_blocksize;
+ struct buffer_head *bh = NULL;
unsigned int i, index = 0;
u32 chksum = 0;
- unsigned char skip = false;
- struct exfat_upcase_ptable *upcase_table;
+ bool skip = false, is_default = true;
+ struct exfat_upcase_ptable *upcase_table = NULL;
unsigned short def_upcase;
- bool is_default;
unsigned int entries = 0;
int ret = -EINVAL;
+ if (tbl_size == 0 || tbl_size % 2 != 0 || tbl_size > EXFAT_UPTBL_SIZE * 2) {
+ exfat_fs_error(sb, "bogus upcase table size(%llu bytes). Please run fsck", tbl_size);
+ return -EINVAL;
+ }
+
upcase_table = kvcalloc(1, sizeof(struct exfat_upcase_ptable), GFP_KERNEL);
if (!upcase_table)
return -ENOMEM;
- num_sectors += sector;
- is_default = sector < num_sectors;
-
- while (sector < num_sectors) {
- struct buffer_head *bh;
-
+ for (; tbl_size > 1; sector++) {
+ brelse(bh);
bh = sb_bread(sb, sector);
if (!bh) {
- exfat_err(sb, "failed to read sector(0x%llx)",
+ exfat_err(sb, "failed to read upcase table sector(0x%llx)",
(unsigned long long)sector);
ret = -EIO;
goto err;
}
- sector++;
- for (i = 0; i < sect_size && index <= 0xFFFF; i += 2) {
+ chksum = exfat_calc_chksum32(bh->b_data, MIN(tbl_size, sb->s_blocksize),
+ chksum, CS_DEFAULT);
+
+ for (i = 0; i < sb->s_blocksize && tbl_size > 1; i += 2) {
unsigned short uni = get_unaligned_le16(bh->b_data + i);
+ tbl_size -= 2;
+
if (skip) {
index += uni;
skip = false;
@@ -328,10 +331,8 @@ static int exfat_load_upcase_table(struct super_block *sb,
skip = true;
} else { /* uni != index , uni != 0xFFFF */
ret = exfat_set_upcase_ptable(upcase_table, index, uni);
- if (ret) {
- brelse(bh);
+ if (ret)
goto err;
- }
def_upcase = exfat_lookup_upcase_ptable(&exfat_def_upcase_ptable,
index);
@@ -340,12 +341,14 @@ static int exfat_load_upcase_table(struct super_block *sb,
entries++;
index++;
}
+
+ if (index > 0xFFFF)
+ goto indexed;
}
- chksum = exfat_calc_chksum32(bh->b_data, i, chksum, CS_DEFAULT);
- brelse(bh);
}
- if (index >= 0xFFFF && utbl_checksum == chksum) {
+indexed:
+ if (index == 0x10000 && utbl_checksum == chksum && tbl_size == 0) {
/*
* is_default being set does not necessarily mean the contents are exact same as the
* upcase table loaded from the volume may be missing some entries. The checksum
@@ -356,17 +359,22 @@ static int exfat_load_upcase_table(struct super_block *sb,
kvfree(upcase_table);
} else {
sbi->vol_utbl = sbi->vol_utbl_own = upcase_table;
- exfat_info(sb, "using non-default upcase table (chksum: 0x%08x, entries: %u, memsize: %zu+)",
- chksum, entries, upcase_table->cnt * EXFAT_UPTBL_PAGESIZE);
+ exfat_info(sb, "using non-default upcase table "
+ "(chksum: 0x%08x, entries: %u, memsize: %zu+ bytes)",
+ chksum, entries, upcase_table->cnt * EXFAT_UPTBL_PAGESIZE * 2);
}
- return 0;
- }
-
- exfat_err(sb, "failed to load upcase table (idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x)",
- index, chksum, utbl_checksum);
+ upcase_table = NULL;
+ ret = 0;
+ } else
+ ret = -EINVAL;
err:
+ if (ret == -EINVAL)
+ exfat_fs_error(sb, "damaged upcase table. Please run fsck "
+ "(idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x, rem : %llu bytes)",
+ index, chksum, utbl_checksum, tbl_size);
+ brelse(bh);
exfat_free_upcase_ptable(upcase_table);
kvfree(upcase_table);
@@ -377,8 +385,8 @@ int exfat_create_upcase_table(struct super_block *sb)
{
unsigned int tbl_clu, type;
sector_t sector;
- unsigned long long tbl_size, num_sectors;
- unsigned char blksize_bits = sb->s_blocksize_bits;
+ unsigned long long tbl_size;
+ unsigned int chksum;
struct exfat_chain clu;
struct exfat_dentry *ep;
struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -411,18 +419,18 @@ int exfat_create_upcase_table(struct super_block *sb)
tbl_clu = le32_to_cpu(ep->dentry.upcase.start_clu);
tbl_size = le64_to_cpu(ep->dentry.upcase.size);
- if (tbl_size) {
- sector = exfat_cluster_to_sector(sbi, tbl_clu);
- num_sectors = ((tbl_size - 1) >> blksize_bits) + 1;
- ret = exfat_load_upcase_table(sb, sector, num_sectors,
- le32_to_cpu(ep->dentry.upcase.checksum));
- } else
- exfat_fs_error(sb,
- "bad upcase table size (0 bytes). Please run fsck");
+ sector = exfat_cluster_to_sector(sbi, tbl_clu);
+ chksum = le32_to_cpu(ep->dentry.upcase.checksum);
+
+ ret = exfat_load_upcase_table(sb, sector, tbl_size, chksum);
brelse(bh);
- if (ret && ret != -EIO)
- ret = 0;
+ /*
+ * Continue w/ damaged table(EINVAL) in read-only mode, unless overridden.
+ * Treat ENOMEM and EIO as fatal.
+ */
+ if (ret == -EINVAL)
+ return 0;
return ret;
}
--
2.53.0.1.ga224b40d3f.dirty
prev parent reply other threads:[~2026-05-05 12:32 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-05 12:31 [PATCH v2 0/4] exfat: memory optimisations and stringent integrity checks for up-case table David Timber
2026-05-05 12:31 ` [PATCH v2 1/4] exfat: use upcase_ptable and upcase_range_info to reduce memory footprint David Timber
2026-05-07 12:03 ` Yuezhang.Mo
2026-05-10 22:22 ` David Timber
2026-05-05 12:31 ` [PATCH v2 2/4] exfat: optimise and refactor filename up-case conversion David Timber
2026-05-07 12:03 ` Yuezhang.Mo
2026-05-10 22:58 ` David Timber
2026-05-05 12:31 ` [PATCH v2 3/4] exfat: add default_upcase option (read-only) David Timber
2026-05-05 12:31 ` David Timber [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260505123144.730782-5-dxdt@dev.snart.me \
--to=dxdt@dev.snart.me \
--cc=linkinjeon@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=sj1557.seo@samsung.com \
--cc=yuezhang.mo@sony.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox