Skip to contentSkip to conversation

nand-utils: Fix integer overflow in nandflipbits.c

PATCH In review Anton Moryakov 2 participants 2 messages Dec 17, 2024 – Dec 18, 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 'page * (mtd.min_io_size + mtd.oob_size)' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
Casting bit_to_flip->block to unsigned long long expands the range of values ​​and allows the result of the multiplication to be calculated in a wider data type. The arithmetic is performed in a safe range, preventing overflow.
Sum + blkoffs is now also safe, since the result of the expression is already calculated in unsigned long long.

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 ef663c6..c4642fe 100644
@@ -213,7 +213,7 @@ int main(int argc, char **argv)
 		bits_to_flip[i].block = bits_to_flip[i].offset / blklen;
 		bits_to_flip[i].offset %= blklen;
 		page = bits_to_flip[i].offset / pagelen;
-		bits_to_flip[i].offset = (page *
+		bits_to_flip[i].offset = ((unsigned long long)page *
 					  (mtd.min_io_size + mtd.oob_size)) +
 					 (bits_to_flip[i].offset % pagelen);
 	}
Signature · 6 lines
2.30.2


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

Report of the static analyzer:
The value of an arithmetic expression 'page * (mtd.min_io_size + mtd.oob_size)' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
Casting bit_to_flip->block to unsigned long long expands the range of values ​​and allows the result of the multiplication to be calculated in a wider data type. The arithmetic is performed in a safe range, preventing overflow.
Sum + blkoffs is now also safe, since the result of the expression is already calculated in unsigned long long.

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 ef663c6..c4642fe 100644

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

Quoted 7 lines
nand-utils/nandflipbits.c +1 -1
@@ -213,7 +213,7 @@ int main(int argc, char **argv)
  		bits_to_flip[i].block = bits_to_flip[i].offset / blklen;
  		bits_to_flip[i].offset %= blklen;
  		page = bits_to_flip[i].offset / pagelen;
-		bits_to_flip[i].offset = (page *
+		bits_to_flip[i].offset = ((unsigned long long)page *
  					  (mtd.min_io_size + mtd.oob_size)) +
  					 (bits_to_flip[i].offset % pagelen);

I think the 'bits_to_flip[i].offset' is limited smaller than 'mtd.eb_size' by the code:
bits_to_flip[i].offset %= blklen;
page = bits_to_flip[i].offset / pagelen;
So, I don't think an overflow could happen here.

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

Quoted 2 lines
nand-utils/nandflipbits.c +0 -0
  	}

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