Skip to contentSkip to conversation

fs/ntfs3: validate BOOT sectors_per_clusters

PATCH Superseded Randy Dunlap 3 participants 3 messages Apr 29, 2022 – Apr 30, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 (> 31) before using it. Return 0xffffffff if it is.

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

 fs/ntfs3/super.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ntfs3/super.c +5 -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 > 0xe0) /* limit to 31-bit shift */
+		return 1U << (0 - boot->sectors_per_clusters);
+	return 0xffffffff;
 }
 
 /*
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Randy Dunlap

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 (> 31) before using it. Return 0xffffffff if it is.

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

Quoted 8 lines

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]
#2 lore raw
In reply to Randy Dunlap

2022-04-30 5:01 GMT+09:00, Randy Dunlap <[email protected]>:

Quoted 31 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 (> 31) before using it. Return 0xffffffff if it is.

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

 fs/ntfs3/super.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

fs/ntfs3/super.c static u32 format_size_gb(const u64 byte · line 673

Quoted 9 lines
fs/ntfs3/super.c +3 -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 > 0xe0) /* limit to 31-bit shift */

ntfs maximum cluster size is 2MB. I think that we can change it to boot->sectors_per_clusters >= 0xf4.

fs/ntfs3/super.c static u32 format_size_gb(const u64 byte · line 675

Quoted 2 lines
fs/ntfs3/super.c +2 -0
+		return 1U << (0 - boot->sectors_per_clusters);
+	return 0xffffffff;

It would be better to change it to return an error(-EINVAL) instead of 0xffffffff.
and if sct_per_clst < 0, goto out immediately..

Quoted 5 lines
fs/ntfs3/super.c +0 -0
 }
 
 /*