Blame SOURCES/0065-exfat-fix-shift-out-of-bounds-in-exfat_fill_super.patch

Kmods SIG 9e3ffb
From 78c276f5495aa53a8beebb627e5bf6a54f0af34f Mon Sep 17 00:00:00 2001
Kmods SIG 9e3ffb
From: Namjae Jeon <namjae.jeon@samsung.com>
Kmods SIG 9e3ffb
Date: Mon, 1 Feb 2021 09:23:37 +0900
Kmods SIG 9e3ffb
Subject: [Backport 78c276f5495a] exfat: fix shift-out-of-bounds in
Kmods SIG 9e3ffb
 exfat_fill_super()
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
syzbot reported a warning which could cause shift-out-of-bounds issue.
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
Call Trace:
Kmods SIG 9e3ffb
 __dump_stack lib/dump_stack.c:79 [inline]
Kmods SIG 9e3ffb
 dump_stack+0x183/0x22e lib/dump_stack.c:120
Kmods SIG 9e3ffb
 ubsan_epilogue lib/ubsan.c:148 [inline]
Kmods SIG 9e3ffb
 __ubsan_handle_shift_out_of_bounds+0x432/0x4d0 lib/ubsan.c:395
Kmods SIG 9e3ffb
 exfat_read_boot_sector src/super.c:471 [inline]
Kmods SIG 9e3ffb
 __exfat_fill_super src/super.c:556 [inline]
Kmods SIG 9e3ffb
 exfat_fill_super+0x2acb/0x2d00 src/super.c:624
Kmods SIG 9e3ffb
 get_tree_bdev+0x406/0x630 fs/super.c:1291
Kmods SIG 9e3ffb
 vfs_get_tree+0x86/0x270 fs/super.c:1496
Kmods SIG 9e3ffb
 do_new_mount fs/namespace.c:2881 [inline]
Kmods SIG 9e3ffb
 path_mount+0x1937/0x2c50 fs/namespace.c:3211
Kmods SIG 9e3ffb
 do_mount fs/namespace.c:3224 [inline]
Kmods SIG 9e3ffb
 __do_sys_mount fs/namespace.c:3432 [inline]
Kmods SIG 9e3ffb
 __se_sys_mount+0x2f9/0x3b0 fs/namespace.c:3409
Kmods SIG 9e3ffb
 do_syscall_64+0x2d/0x70 arch/x86/entry/common.c:46
Kmods SIG 9e3ffb
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
exfat specification describe sect_per_clus_bits field of boot sector
Kmods SIG 9e3ffb
could be at most 25 - sect_size_bits and at least 0. And sect_size_bits
Kmods SIG 9e3ffb
can also affect this calculation, It also needs validation.
Kmods SIG 9e3ffb
This patch add validation for sect_per_clus_bits and sect_size_bits
Kmods SIG 9e3ffb
field of boot sector.
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
Fixes: 719c1e182916 ("exfat: add super block operations")
Kmods SIG 9e3ffb
Cc: stable@vger.kernel.org # v5.9+
Kmods SIG 9e3ffb
Reported-by: syzbot+da4fe66aaadd3c2e2d1c@syzkaller.appspotmail.com
Kmods SIG 9e3ffb
Reviewed-by: Sungjong Seo <sj1557.seo@samsung.com>
Kmods SIG 9e3ffb
Tested-by: Randy Dunlap <rdunlap@infradead.org>
Kmods SIG 9e3ffb
Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
Kmods SIG 9e3ffb
---
Kmods SIG 9e3ffb
 src/exfat_raw.h |  4 ++++
Kmods SIG 9e3ffb
 src/super.c     | 31 ++++++++++++++++++++++++++-----
Kmods SIG 9e3ffb
 2 files changed, 30 insertions(+), 5 deletions(-)
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
diff --git a/src/exfat_raw.h b/src/exfat_raw.h
Kmods SIG 9e3ffb
index 6aec6288e1f21981920f802083d591034c3fb6a2..7f39b1c6469c4e6938fc855ee4cd7a51ede9ec57 100644
Kmods SIG 9e3ffb
--- a/src/exfat_raw.h
Kmods SIG 9e3ffb
+++ b/src/exfat_raw.h
Kmods SIG 9e3ffb
@@ -77,6 +77,10 @@
Kmods SIG 9e3ffb
 
Kmods SIG 9e3ffb
 #define EXFAT_FILE_NAME_LEN		15
Kmods SIG 9e3ffb
 
Kmods SIG 9e3ffb
+#define EXFAT_MIN_SECT_SIZE_BITS		9
Kmods SIG 9e3ffb
+#define EXFAT_MAX_SECT_SIZE_BITS		12
Kmods SIG 9e3ffb
+#define EXFAT_MAX_SECT_PER_CLUS_BITS(x)		(25 - (x)->sect_size_bits)
Kmods SIG 9e3ffb
+
Kmods SIG 9e3ffb
 /* EXFAT: Main and Backup Boot Sector (512 bytes) */
Kmods SIG 9e3ffb
 struct boot_sector {
Kmods SIG 9e3ffb
 	__u8	jmp_boot[BOOTSEC_JUMP_BOOT_LEN];
Kmods SIG 9e3ffb
diff --git a/src/super.c b/src/super.c
Kmods SIG 9e3ffb
index 87be5bfc31eb41bce6dbbe4f9c5e5d9c01d7d829..c6d8d2e534865236d16386575627e0a442a8db52 100644
Kmods SIG 9e3ffb
--- a/src/super.c
Kmods SIG 9e3ffb
+++ b/src/super.c
Kmods SIG 9e3ffb
@@ -381,8 +381,7 @@ 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
 
Kmods SIG 9e3ffb
-	if (!is_power_of_2(logical_sect) ||
Kmods SIG 9e3ffb
-	    logical_sect < 512 || logical_sect > 4096) {
Kmods SIG 9e3ffb
+	if (!is_power_of_2(logical_sect)) {
Kmods SIG 9e3ffb
 		exfat_err(sb, "bogus logical sector size %u", logical_sect);
Kmods SIG 9e3ffb
 		return -EIO;
Kmods SIG 9e3ffb
 	}
Kmods SIG 9e3ffb
@@ -451,6 +450,25 @@ static int exfat_read_boot_sector(struct super_block *sb)
Kmods SIG 9e3ffb
 		return -EINVAL;
Kmods SIG 9e3ffb
 	}
Kmods SIG 9e3ffb
 
Kmods SIG 9e3ffb
+	/*
Kmods SIG 9e3ffb
+	 * sect_size_bits could be at least 9 and at most 12.
Kmods SIG 9e3ffb
+	 */
Kmods SIG 9e3ffb
+	if (p_boot->sect_size_bits < EXFAT_MIN_SECT_SIZE_BITS ||
Kmods SIG 9e3ffb
+	    p_boot->sect_size_bits > EXFAT_MAX_SECT_SIZE_BITS) {
Kmods SIG 9e3ffb
+		exfat_err(sb, "bogus sector size bits : %u\n",
Kmods SIG 9e3ffb
+				p_boot->sect_size_bits);
Kmods SIG 9e3ffb
+		return -EINVAL;
Kmods SIG 9e3ffb
+	}
Kmods SIG 9e3ffb
+
Kmods SIG 9e3ffb
+	/*
Kmods SIG 9e3ffb
+	 * sect_per_clus_bits could be at least 0 and at most 25 - sect_size_bits.
Kmods SIG 9e3ffb
+	 */
Kmods SIG 9e3ffb
+	if (p_boot->sect_per_clus_bits > EXFAT_MAX_SECT_PER_CLUS_BITS(p_boot)) {
Kmods SIG 9e3ffb
+		exfat_err(sb, "bogus sectors bits per cluster : %u\n",
Kmods SIG 9e3ffb
+				p_boot->sect_per_clus_bits);
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 = p_boot->sect_per_clus_bits +
Kmods SIG 9e3ffb
@@ -477,16 +495,19 @@ static int exfat_read_boot_sector(struct super_block *sb)
Kmods SIG 9e3ffb
 	sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED;
Kmods SIG 9e3ffb
 
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
+	if ((u64)sbi->num_FAT_sectors << p_boot->sect_size_bits <
Kmods SIG 9e3ffb
+	    (u64)sbi->num_clusters * 4) {
Kmods SIG 9e3ffb
 		exfat_err(sb, "bogus fat length");
Kmods SIG 9e3ffb
 		return -EINVAL;
Kmods SIG 9e3ffb
 	}
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
+	    (u64)sbi->FAT1_start_sector +
Kmods SIG 9e3ffb
+	    (u64)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
+
Kmods SIG 9e3ffb
 	if (sbi->vol_flags & VOLUME_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_flags & MEDIA_FAILURE)
Kmods SIG 9e3ffb
-- 
Kmods SIG 9e3ffb
2.31.1
Kmods SIG 9e3ffb