Skip to contentSkip to conversation

nand-utils: Fix integer overflow in nandflipbits.c

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nand-utils: Fix integer overflow in nandflipbits.c

Report of the static analyzer:
The value of an arithmetic expression 'bit_to_flip->block * mtd.eb_size + blkoffs' is a subject to overflow because its operands are not cast to a larger data type before performing arith$

Corrections explained:
Prevent arithmetic overflow in OOB read operation
Resolved an issue where the calculation of the offset in the OOB read operation could overflow due to operands not being cast to a larger data type. Specifically, the multiplication of bi$

Triggers found by static analyzer Svace.

---
 nand-utils/nandflipbits.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
nand-utils/nandflipbits.c +1 -1
index 7066408..ef663c6 100644
@@ -251,7 +251,7 @@ int main(int argc, char **argv)
 			bufoffs += mtd.min_io_size;
 
 			ret = mtd_read_oob(mtd_desc, &mtd, fd,
-					   bit_to_flip->block * mtd.eb_size +
+					   (long long)bit_to_flip->block * (long long)mtd.eb_size +
 					   blkoffs,
 					   mtd.oob_size, buffer + bufoffs);
 			if (ret) {
Signature · 6 lines
2.30.2


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

Report of the static analyzer:
The value of an arithmetic expression 'bit_to_flip->block * mtd.eb_size + blkoffs' is a subject to overflow because its operands are not cast to a larger data type before performing arith$

Corrections explained:
Prevent arithmetic overflow in OOB read operation
Resolved an issue where the calculation of the offset in the OOB read operation could overflow due to operands not being cast to a larger data type. Specifically, the multiplication of bi$

Triggers found by static analyzer Svace.

---
  nand-utils/nandflipbits.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
nand-utils/nandflipbits.c +0 -0
index 7066408..ef663c6 100644

nand-utils/nandflipbits.c int main(int argc, char **argv) · line 254

Quoted 5 lines
nand-utils/nandflipbits.c +1 -1
@@ -251,7 +251,7 @@ int main(int argc, char **argv)
  			bufoffs += mtd.min_io_size;
 
  			ret = mtd_read_oob(mtd_desc, &mtd, fd,
-					   bit_to_flip->block * mtd.eb_size +
+					   (long long)bit_to_flip->block * (long long)mtd.eb_size +

Hi Anton.
Type converting for 'bit_to_flip->block' is enough, and it could be better making the type as 'unsigned long long'.

nand-utils/nandflipbits.c int main(int argc, char **argv) · line 257

Quoted 4 lines
nand-utils/nandflipbits.c +0 -0
  					   blkoffs,
  					   mtd.oob_size, buffer + bufoffs);
  			if (ret) {

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