Skip to contentSkip to conversation

fs/ntfs3: validate BOOT sectors_per_clusters

PATCH Applied Randy Dunlap 2 participants 2 messages May 2, 2022 – May 3, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 fs/ntfs3: validate BOOT sectors_per_clusters

When the NTFS BOOT sectors_per_clusters field is > 0x80, it represents a shift value. Make sure that the shift value is not too large before using it (NTFS max cluster size is 2MB). Return -EVINVAL if it too large.

This prevents negative shift values and shift values that are larger than the field size.

Prevents this UBSAN error:

 UBSAN: shift-out-of-bounds in ../fs/ntfs3/super.c:673:16
 shift exponent -192 is negative
Fixes 82cae269cfa9 ("fs/ntfs3: Add initialization of super block")
Reported-by [email protected]
Cc Konstantin Komarov <[email protected]>
Cc Alexander Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Kari Argillander <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Matthew Wilcox <[email protected]>

---
v2: use Willy's suggestions
v3: use Namjae's suggestions -- but now Konstantin can decide.
drop Willy's Rev-by: tag due to changes

 fs/ntfs3/super.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fs/ntfs3/super.c +7 -3
@@ -668,9 +668,11 @@ static u32 format_size_gb(const u64 byte
 
 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
 {
-	return boot->sectors_per_clusters <= 0x80
-		       ? boot->sectors_per_clusters
-		       : (1u << (0 - boot->sectors_per_clusters));
+	if (boot->sectors_per_clusters <= 0x80)
+		return boot->sectors_per_clusters;
+	if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
+		return 1U << (0 - boot->sectors_per_clusters);
+	return -EINVAL;
 }
 
 /*
@@ -713,6 +715,8 @@ static int ntfs_init_from_boot(struct su
 
 	/* cluster size: 512, 1K, 2K, 4K, ... 2M */
 	sct_per_clst = true_sectors_per_clst(boot);
+	if ((int)sct_per_clst < 0)
+		goto out;
 	if (!is_power_of_2(sct_per_clst))
 		goto out;
 
Reviewed-by on v3 1/1
#1 lore raw

2022-05-03 2:53 GMT+09:00, Randy Dunlap <[email protected]>:

Quoted 13 lines

When the NTFS BOOT sectors_per_clusters field is > 0x80, it represents a shift value. Make sure that the shift value is not too large before using it (NTFS max cluster size is 2MB). Return -EVINVAL if it too large.

This prevents negative shift values and shift values that are larger than the field size.

Prevents this UBSAN error:

 UBSAN: shift-out-of-bounds in ../fs/ntfs3/super.c:673:16
 shift exponent -192 is negative
Quoted 10 lines
Fixes 82cae269cfa9 ("fs/ntfs3: Add initialization of super block")
Reported-by [email protected]
Cc Konstantin Komarov <[email protected]>
Cc Alexander Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Kari Argillander <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Matthew Wilcox <[email protected]>

Looks good to me:)

Thanks.

Quoted 36 lines

---
v2: use Willy's suggestions
v3: use Namjae's suggestions -- but now Konstantin can decide.
drop Willy's Rev-by: tag due to changes

 fs/ntfs3/super.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fs/ntfs3/super.c +7 -3
@@ -668,9 +668,11 @@ static u32 format_size_gb(const u64 byte
 
 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
 {
-	return boot->sectors_per_clusters <= 0x80
-		       ? boot->sectors_per_clusters
-		       : (1u << (0 - boot->sectors_per_clusters));
+	if (boot->sectors_per_clusters <= 0x80)
+		return boot->sectors_per_clusters;
+	if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
+		return 1U << (0 - boot->sectors_per_clusters);
+	return -EINVAL;
 }
 
 /*
@@ -713,6 +715,8 @@ static int ntfs_init_from_boot(struct su
 
 	/* cluster size: 512, 1K, 2K, 4K, ... 2M */
 	sct_per_clst = true_sectors_per_clst(boot);
+	if ((int)sct_per_clst < 0)
+		goto out;
 	if (!is_power_of_2(sct_per_clst))
 		goto out;