Skip to contentSkip to conversation

ubi-utils: Fix integer overflow in mtdinfo.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 ubi-utils: Fix integer overflow in mtdinfo.c

Report of the static analyzer:
The value of an arithmetic expression 'reginfo->offset + i * reginfo->erasesize' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
Added casting reginfo->offset to long long

Triggers found by static analyzer Svace.

---
 ubi-utils/mtdinfo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ubi-utils/mtdinfo.c +1 -1
index 7dff0de..850297b 100644
@@ -203,7 +203,7 @@ static void print_region_map(const struct mtd_dev_info *mtd, int fd,
 		ret_locked = ret_bad = errno_locked = errno_bad = 0;
 
 	for (i = 0; i < reginfo->numblocks; ++i) {
-		start = reginfo->offset + i * reginfo->erasesize;
+		start = (long long)reginfo->offset + i * reginfo->erasesize;
 		printf(" %*i: %08lx ", width, i, start);
 
 		if (ret_locked != -1) {
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 'reginfo->offset + i * reginfo->erasesize' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
Added casting reginfo->offset to long long

Triggers found by static analyzer Svace.

---
  ubi-utils/mtdinfo.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
ubi-utils/mtdinfo.c +0 -0
index 7dff0de..850297b 100644

ubi-utils/mtdinfo.c static void print_region_map(const struct mtd_dev_info *mtd, int fd, · line 206

Quoted 4 lines
ubi-utils/mtdinfo.c +0 -1
@@ -203,7 +203,7 @@ static void print_region_map(const struct mtd_dev_info *mtd, int fd,
  		ret_locked = ret_bad = errno_locked = errno_bad = 0;
 
  	for (i = 0; i < reginfo->numblocks; ++i) {
-		start = reginfo->offset + i * reginfo->erasesize;

Hi Anton.
I think the expression 'i*reginfo->erasesize' could overflow too. Besides, 'unsigned long start' could be a 32-bit number on a 32-bit platform.
So, we should make 'start' be the 'unsigned long long' type, and then convert 'i' into 'unsigned long long' in expression 'i * reginfo->erasesize'. For example, start = reginfo->offset + (unsigned long long)i * reginfo->erasesize.

ubi-utils/mtdinfo.c static void print_region_map(const struct mtd_dev_info *mtd, int fd, · line 209

Quoted 5 lines
ubi-utils/mtdinfo.c +1 -0
+		start = (long long)reginfo->offset + i * reginfo->erasesize;
  		printf(" %*i: %08lx ", width, i, start);
 
  		if (ret_locked != -1) {

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