Skip to contentSkip to conversation

misc-utils: flash_erase: FIX integer overflow in flash_erase.c

PATCH In review Anton Moryakov 2 participants 2 messages Dec 11, 2024 – Dec 12, 2024 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc-utils: flash_erase: FIX integer overflow in flash_erase.c

Report of the static analyzer:
The value of an arithmetic expression 'eb_cnt * mtd.eb_size' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
Added explicit casting of eb_cnt to long long in the condition
if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size)
to ensure the multiplication is performed in a 64-bit context, preventing potential overflow for large values of eb_cnt and mtd.eb_size. This ensures correct handling of devices with large block counts or block sizes.

Triggers found by static analyzer Svace.

---
 misc-utils/flash_erase.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc-utils/flash_erase.c +1 -1
index c6f6f66..78a3bd1 100644
@@ -239,7 +239,7 @@ int main(int argc, char *argv[])
 	if (eb_cnt == 0)
 		eb_cnt = (mtd.size / mtd.eb_size) - eb_start;
 
-	if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size)
+	if (eb_start == 0 && mtd.size == (long long)eb_cnt * mtd.eb_size)
 		erase_chip = true;
 
 	/* If MTD device may have bad eraseblocks,
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Anton Moryakov

Report of the static analyzer:
The value of an arithmetic expression 'eb_cnt * mtd.eb_size' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
Added explicit casting of eb_cnt to long long in the condition
if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size)
to ensure the multiplication is performed in a 64-bit context, preventing potential overflow for large values of eb_cnt and mtd.eb_size. This ensures correct handling of devices with large block counts or block sizes.

Quoted 7 lines

Triggers found by static analyzer Svace.

---
  misc-utils/flash_erase.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 7 lines
misc-utils/flash_erase.c +0 -0
index c6f6f66..78a3bd1 100644

misc-utils/flash_erase.c int main(int argc, char *argv[]) · line 245

Quoted 8 lines
misc-utils/flash_erase.c +1 -1
@@ -239,7 +239,7 @@ int main(int argc, char *argv[])
  	if (eb_cnt == 0)
  		eb_cnt = (mtd.size / mtd.eb_size) - eb_start;
 
-	if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size)
+	if (eb_start == 0 && mtd.size == (long long)eb_cnt * mtd.eb_size)
  		erase_chip = true;
 
  	/* If MTD device may have bad eraseblocks,

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/