|
Kmods SIG |
9e3ffb |
From 33404a159828a97fefada0d8f4cf910873b8e9f1 Mon Sep 17 00:00:00 2001
|
|
Kmods SIG |
9e3ffb |
From: Tetsuhiro Kohada <kohada.t2@gmail.com>
|
|
Kmods SIG |
9e3ffb |
Date: Fri, 29 May 2020 19:14:57 +0900
|
|
Kmods SIG |
9e3ffb |
Subject: [Backport 33404a159828] exfat: separate the boot sector analysis
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
Separate the boot sector analysis to read_boot_sector().
|
|
Kmods SIG |
9e3ffb |
And add a check for the fs_name field.
|
|
Kmods SIG |
9e3ffb |
Furthermore, add a strict consistency check, because overlapping areas
|
|
Kmods SIG |
9e3ffb |
can cause serious corruption.
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
Signed-off-by: Tetsuhiro Kohada <kohada.t2@gmail.com>
|
|
Kmods SIG |
9e3ffb |
Reviewed-by: Sungjong Seo <sj1557.seo@samsung.com>
|
|
Kmods SIG |
9e3ffb |
Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
|
|
Kmods SIG |
9e3ffb |
---
|
|
Kmods SIG |
9e3ffb |
src/exfat_raw.h | 2 +
|
|
Kmods SIG |
9e3ffb |
src/super.c | 97 ++++++++++++++++++++++++--------------------
|
|
Kmods SIG |
9e3ffb |
2 files changed, 56 insertions(+), 43 deletions(-)
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
diff --git a/src/exfat_raw.h b/src/exfat_raw.h
|
|
Kmods SIG |
9e3ffb |
index 07f74190df44b215128d4dcf2a8ee33d32a7cf21..350ce59cc32478919d9ffb9823726b8b9015de48 100644
|
|
Kmods SIG |
9e3ffb |
--- a/src/exfat_raw.h
|
|
Kmods SIG |
9e3ffb |
+++ b/src/exfat_raw.h
|
|
Kmods SIG |
9e3ffb |
@@ -10,11 +10,13 @@
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
#define BOOT_SIGNATURE 0xAA55
|
|
Kmods SIG |
9e3ffb |
#define EXBOOT_SIGNATURE 0xAA550000
|
|
Kmods SIG |
9e3ffb |
+#define STR_EXFAT "EXFAT " /* size should be 8 */
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
#define EXFAT_MAX_FILE_LEN 255
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
#define VOL_CLEAN 0x0000
|
|
Kmods SIG |
9e3ffb |
#define VOL_DIRTY 0x0002
|
|
Kmods SIG |
9e3ffb |
+#define ERR_MEDIUM 0x0004
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
#define EXFAT_EOF_CLUSTER 0xFFFFFFFFu
|
|
Kmods SIG |
9e3ffb |
#define EXFAT_BAD_CLUSTER 0xFFFFFFF7u
|
|
Kmods SIG |
9e3ffb |
diff --git a/src/super.c b/src/super.c
|
|
Kmods SIG |
9e3ffb |
index e60d28e73ff007b85eafc751693d332911fbcfd3..6a1330be5a9a4b58e2a39b197788f139bfbaba4f 100644
|
|
Kmods SIG |
9e3ffb |
--- a/src/super.c
|
|
Kmods SIG |
9e3ffb |
+++ b/src/super.c
|
|
Kmods SIG |
9e3ffb |
@@ -366,25 +366,20 @@ static int exfat_read_root(struct inode *inode)
|
|
Kmods SIG |
9e3ffb |
return 0;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
-static struct boot_sector *exfat_read_boot_with_logical_sector(
|
|
Kmods SIG |
9e3ffb |
- struct super_block *sb)
|
|
Kmods SIG |
9e3ffb |
+static int exfat_calibrate_blocksize(struct super_block *sb, int logical_sect)
|
|
Kmods SIG |
9e3ffb |
{
|
|
Kmods SIG |
9e3ffb |
struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
|
Kmods SIG |
9e3ffb |
- struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
|
|
Kmods SIG |
9e3ffb |
- unsigned short logical_sect = 0;
|
|
Kmods SIG |
9e3ffb |
-
|
|
Kmods SIG |
9e3ffb |
- logical_sect = 1 << p_boot->sect_size_bits;
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
if (!is_power_of_2(logical_sect) ||
|
|
Kmods SIG |
9e3ffb |
logical_sect < 512 || logical_sect > 4096) {
|
|
Kmods SIG |
9e3ffb |
exfat_err(sb, "bogus logical sector size %u", logical_sect);
|
|
Kmods SIG |
9e3ffb |
- return NULL;
|
|
Kmods SIG |
9e3ffb |
+ return -EIO;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
if (logical_sect < sb->s_blocksize) {
|
|
Kmods SIG |
9e3ffb |
exfat_err(sb, "logical sector size too small for device (logical sector size = %u)",
|
|
Kmods SIG |
9e3ffb |
logical_sect);
|
|
Kmods SIG |
9e3ffb |
- return NULL;
|
|
Kmods SIG |
9e3ffb |
+ return -EIO;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
if (logical_sect > sb->s_blocksize) {
|
|
Kmods SIG |
9e3ffb |
@@ -394,24 +389,20 @@ static struct boot_sector *exfat_read_boot_with_logical_sector(
|
|
Kmods SIG |
9e3ffb |
if (!sb_set_blocksize(sb, logical_sect)) {
|
|
Kmods SIG |
9e3ffb |
exfat_err(sb, "unable to set blocksize %u",
|
|
Kmods SIG |
9e3ffb |
logical_sect);
|
|
Kmods SIG |
9e3ffb |
- return NULL;
|
|
Kmods SIG |
9e3ffb |
+ return -EIO;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
sbi->boot_bh = sb_bread(sb, 0);
|
|
Kmods SIG |
9e3ffb |
if (!sbi->boot_bh) {
|
|
Kmods SIG |
9e3ffb |
exfat_err(sb, "unable to read boot sector (logical sector size = %lu)",
|
|
Kmods SIG |
9e3ffb |
sb->s_blocksize);
|
|
Kmods SIG |
9e3ffb |
- return NULL;
|
|
Kmods SIG |
9e3ffb |
+ return -EIO;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
-
|
|
Kmods SIG |
9e3ffb |
- p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
- return p_boot;
|
|
Kmods SIG |
9e3ffb |
+ return 0;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
-/* mount the file system volume */
|
|
Kmods SIG |
9e3ffb |
-static int __exfat_fill_super(struct super_block *sb)
|
|
Kmods SIG |
9e3ffb |
+static int exfat_read_boot_sector(struct super_block *sb)
|
|
Kmods SIG |
9e3ffb |
{
|
|
Kmods SIG |
9e3ffb |
- int ret;
|
|
Kmods SIG |
9e3ffb |
struct boot_sector *p_boot;
|
|
Kmods SIG |
9e3ffb |
struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
@@ -424,51 +415,41 @@ static int __exfat_fill_super(struct super_block *sb)
|
|
Kmods SIG |
9e3ffb |
exfat_err(sb, "unable to read boot sector");
|
|
Kmods SIG |
9e3ffb |
return -EIO;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
-
|
|
Kmods SIG |
9e3ffb |
- /* PRB is read */
|
|
Kmods SIG |
9e3ffb |
p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
/* check the validity of BOOT */
|
|
Kmods SIG |
9e3ffb |
if (le16_to_cpu((p_boot->signature)) != BOOT_SIGNATURE) {
|
|
Kmods SIG |
9e3ffb |
exfat_err(sb, "invalid boot record signature");
|
|
Kmods SIG |
9e3ffb |
- ret = -EINVAL;
|
|
Kmods SIG |
9e3ffb |
- goto free_bh;
|
|
Kmods SIG |
9e3ffb |
+ return -EINVAL;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
-
|
|
Kmods SIG |
9e3ffb |
- /* check logical sector size */
|
|
Kmods SIG |
9e3ffb |
- p_boot = exfat_read_boot_with_logical_sector(sb);
|
|
Kmods SIG |
9e3ffb |
- if (!p_boot) {
|
|
Kmods SIG |
9e3ffb |
- ret = -EIO;
|
|
Kmods SIG |
9e3ffb |
- goto free_bh;
|
|
Kmods SIG |
9e3ffb |
+ if (memcmp(p_boot->fs_name, STR_EXFAT, BOOTSEC_FS_NAME_LEN)) {
|
|
Kmods SIG |
9e3ffb |
+ exfat_err(sb, "invalid fs_name"); /* fs_name may unprintable */
|
|
Kmods SIG |
9e3ffb |
+ return -EINVAL;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
/*
|
|
Kmods SIG |
9e3ffb |
- * res_zero field must be filled with zero to prevent mounting
|
|
Kmods SIG |
9e3ffb |
+ * must_be_zero field must be filled with zero to prevent mounting
|
|
Kmods SIG |
9e3ffb |
* from FAT volume.
|
|
Kmods SIG |
9e3ffb |
*/
|
|
Kmods SIG |
9e3ffb |
- if (memchr_inv(p_boot->must_be_zero, 0,
|
|
Kmods SIG |
9e3ffb |
- sizeof(p_boot->must_be_zero))) {
|
|
Kmods SIG |
9e3ffb |
- ret = -EINVAL;
|
|
Kmods SIG |
9e3ffb |
- goto free_bh;
|
|
Kmods SIG |
9e3ffb |
- }
|
|
Kmods SIG |
9e3ffb |
+ if (memchr_inv(p_boot->must_be_zero, 0, sizeof(p_boot->must_be_zero)))
|
|
Kmods SIG |
9e3ffb |
+ return -EINVAL;
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
- p_boot = (struct boot_sector *)p_boot;
|
|
Kmods SIG |
9e3ffb |
- if (!p_boot->num_fats) {
|
|
Kmods SIG |
9e3ffb |
+ if (p_boot->num_fats != 1 && p_boot->num_fats != 2) {
|
|
Kmods SIG |
9e3ffb |
exfat_err(sb, "bogus number of FAT structure");
|
|
Kmods SIG |
9e3ffb |
- ret = -EINVAL;
|
|
Kmods SIG |
9e3ffb |
- goto free_bh;
|
|
Kmods SIG |
9e3ffb |
+ return -EINVAL;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
sbi->sect_per_clus = 1 << p_boot->sect_per_clus_bits;
|
|
Kmods SIG |
9e3ffb |
sbi->sect_per_clus_bits = p_boot->sect_per_clus_bits;
|
|
Kmods SIG |
9e3ffb |
- sbi->cluster_size_bits = sbi->sect_per_clus_bits + sb->s_blocksize_bits;
|
|
Kmods SIG |
9e3ffb |
+ sbi->cluster_size_bits = p_boot->sect_per_clus_bits +
|
|
Kmods SIG |
9e3ffb |
+ p_boot->sect_size_bits;
|
|
Kmods SIG |
9e3ffb |
sbi->cluster_size = 1 << sbi->cluster_size_bits;
|
|
Kmods SIG |
9e3ffb |
sbi->num_FAT_sectors = le32_to_cpu(p_boot->fat_length);
|
|
Kmods SIG |
9e3ffb |
sbi->FAT1_start_sector = le32_to_cpu(p_boot->fat_offset);
|
|
Kmods SIG |
9e3ffb |
- sbi->FAT2_start_sector = p_boot->num_fats == 1 ?
|
|
Kmods SIG |
9e3ffb |
- sbi->FAT1_start_sector :
|
|
Kmods SIG |
9e3ffb |
- sbi->FAT1_start_sector + sbi->num_FAT_sectors;
|
|
Kmods SIG |
9e3ffb |
+ sbi->FAT2_start_sector = le32_to_cpu(p_boot->fat_offset);
|
|
Kmods SIG |
9e3ffb |
+ if (p_boot->num_fats == 2)
|
|
Kmods SIG |
9e3ffb |
+ sbi->FAT2_start_sector += sbi->num_FAT_sectors;
|
|
Kmods SIG |
9e3ffb |
sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
|
|
Kmods SIG |
9e3ffb |
sbi->num_sectors = le64_to_cpu(p_boot->vol_length);
|
|
Kmods SIG |
9e3ffb |
/* because the cluster index starts with 2 */
|
|
Kmods SIG |
9e3ffb |
@@ -483,15 +464,45 @@ static int __exfat_fill_super(struct super_block *sb)
|
|
Kmods SIG |
9e3ffb |
sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
|
|
Kmods SIG |
9e3ffb |
sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED;
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
- if (le16_to_cpu(p_boot->vol_flags) & VOL_DIRTY) {
|
|
Kmods SIG |
9e3ffb |
- sbi->vol_flag |= VOL_DIRTY;
|
|
Kmods SIG |
9e3ffb |
- exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
|
|
Kmods SIG |
9e3ffb |
+ /* check consistencies */
|
|
Kmods SIG |
9e3ffb |
+ if (sbi->num_FAT_sectors << p_boot->sect_size_bits <
|
|
Kmods SIG |
9e3ffb |
+ sbi->num_clusters * 4) {
|
|
Kmods SIG |
9e3ffb |
+ exfat_err(sb, "bogus fat length");
|
|
Kmods SIG |
9e3ffb |
+ return -EINVAL;
|
|
Kmods SIG |
9e3ffb |
+ }
|
|
Kmods SIG |
9e3ffb |
+ if (sbi->data_start_sector <
|
|
Kmods SIG |
9e3ffb |
+ sbi->FAT1_start_sector + sbi->num_FAT_sectors * p_boot->num_fats) {
|
|
Kmods SIG |
9e3ffb |
+ exfat_err(sb, "bogus data start sector");
|
|
Kmods SIG |
9e3ffb |
+ return -EINVAL;
|
|
Kmods SIG |
9e3ffb |
}
|
|
Kmods SIG |
9e3ffb |
+ if (sbi->vol_flag & VOL_DIRTY)
|
|
Kmods SIG |
9e3ffb |
+ exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
|
|
Kmods SIG |
9e3ffb |
+ if (sbi->vol_flag & ERR_MEDIUM)
|
|
Kmods SIG |
9e3ffb |
+ exfat_warn(sb, "Medium has reported failures. Some data may be lost.");
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
/* exFAT file size is limited by a disk volume size */
|
|
Kmods SIG |
9e3ffb |
sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) <<
|
|
Kmods SIG |
9e3ffb |
sbi->cluster_size_bits;
|
|
Kmods SIG |
9e3ffb |
|
|
Kmods SIG |
9e3ffb |
+ /* check logical sector size */
|
|
Kmods SIG |
9e3ffb |
+ if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits))
|
|
Kmods SIG |
9e3ffb |
+ return -EIO;
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ return 0;
|
|
Kmods SIG |
9e3ffb |
+}
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+/* mount the file system volume */
|
|
Kmods SIG |
9e3ffb |
+static int __exfat_fill_super(struct super_block *sb)
|
|
Kmods SIG |
9e3ffb |
+{
|
|
Kmods SIG |
9e3ffb |
+ int ret;
|
|
Kmods SIG |
9e3ffb |
+ struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
+ ret = exfat_read_boot_sector(sb);
|
|
Kmods SIG |
9e3ffb |
+ if (ret) {
|
|
Kmods SIG |
9e3ffb |
+ exfat_err(sb, "failed to read boot sector");
|
|
Kmods SIG |
9e3ffb |
+ goto free_bh;
|
|
Kmods SIG |
9e3ffb |
+ }
|
|
Kmods SIG |
9e3ffb |
+
|
|
Kmods SIG |
9e3ffb |
ret = exfat_create_upcase_table(sb);
|
|
Kmods SIG |
9e3ffb |
if (ret) {
|
|
Kmods SIG |
9e3ffb |
exfat_err(sb, "failed to load upcase table");
|
|
Kmods SIG |
9e3ffb |
--
|
|
Kmods SIG |
9e3ffb |
2.31.1
|
|
Kmods SIG |
9e3ffb |
|