Skip to contentSkip to conversation

misc-utils: fix integer overflow in ftl_check.c

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc-utils: fix integer overflow in ftl_check.c

Report of the static analyzer:
An integer overflow may occur due to arithmetic operation (multiplication) between variable 'nbam' and value '4' of 'sizeof(u_int)', when 'nbam' is in range

Corrections explained:
Avoid arithmetic overflow that could cause an incorrect amount of memory to be allocated. Handle memory allocation errors (malloc).
The code is robust and safe for large nbam values.

Triggers found by static analyzer Svace.

---
 misc-utils/ftl_check.c | 9 +++++++++
 1 file changed, 9 insertions(+)
misc-utils/ftl_check.c +9 -0
index 5b2dae5..fe43a24 100644
@@ -120,8 +120,17 @@ static void check_partition(int fd)
 
 	/* Create basic block allocation table for control blocks */
 	nbam = (mtd.erasesize >> hdr.BlockSize);
+	if (nbam > SIZE_MAX / sizeof(u_int)) {
+		fprintf(stderr, "Error: nbam value too large, potential overflow detected.\n");
+		free(bam);
+		return;
+	}
 	bam = malloc(nbam * sizeof(u_int));
 
+	if (!bam) {
+		perror("malloc failed");
+		return;
+	}
 	for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) {
 		if (lseek(fd, (i << hdr.EraseUnitSize), SEEK_SET) == -1) {
 			perror("seek failed");
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:
An integer overflow may occur due to arithmetic operation (multiplication) between variable 'nbam' and value '4' of 'sizeof(u_int)', when 'nbam' is in range

Corrections explained:
Avoid arithmetic overflow that could cause an incorrect amount of memory to be allocated. Handle memory allocation errors (malloc).
The code is robust and safe for large nbam values.

Triggers found by static analyzer Svace.

---
  misc-utils/ftl_check.c | 9 +++++++++
  1 file changed, 9 insertions(+)
misc-utils/ftl_check.c +0 -0
index 5b2dae5..fe43a24 100644

misc-utils/ftl_check.c static void check_partition(int fd) · line 122

Quoted 3 lines
misc-utils/ftl_check.c +0 -0
@@ -120,8 +120,17 @@ static void check_partition(int fd)
 
  	/* Create basic block allocation table for control blocks */
  	nbam = (mtd.erasesize >> hdr.BlockSize);

The 'mtd.erasesize' won't be a large number in fact, so I think the overflow checking is not really needed.

misc-utils/ftl_check.c static void check_partition(int fd) · line 133

Quoted 11 lines
misc-utils/ftl_check.c +9 -0
+	if (nbam > SIZE_MAX / sizeof(u_int)) {
+		fprintf(stderr, "Error: nbam value too large, potential overflow detected.\n");
+		free(bam);
+		return;
+	}
  	bam = malloc(nbam * sizeof(u_int));
 
+	if (!bam) {
+		perror("malloc failed");
+		return;
+	}

I'm fine with the check of 'bam', just put the blank line(in front of 'if (!bam)') before the for-loop.

misc-utils/ftl_check.c static void check_partition(int fd) · line 136

Quoted 4 lines
misc-utils/ftl_check.c +0 -0
  	for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) {
  		if (lseek(fd, (i << hdr.EraseUnitSize), SEEK_SET) == -1) {
  			perror("seek failed");

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