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