Skip to contentSkip to conversation

ubifs-utils: mkfs.ubifs: fix integer overflow in mkfs.ubifs.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 ubifs-utils: mkfs.ubifs: fix integer overflow in mkfs.ubifs.c

Report of the static analyzer:
The value of an arithmetic expression '4 * c->leb_size' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
To avoid overflow, we cast one of the operands (in this case c->leb_size) to type long long, which has a larger range.

Triggers found by static analyzer Svace.

---
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +2 -2
index b5f3892..9f276c5 100644
@@ -858,8 +858,8 @@ static int get_options(int argc, char**argv)
 		/* Make the max journal size 8MiB */
 		if (c->max_bud_bytes > 8 * 1024 * 1024)
 			c->max_bud_bytes = 8 * 1024 * 1024;
-		if (c->max_bud_bytes < 4 * c->leb_size)
-			c->max_bud_bytes = 4 * c->leb_size;
+		if (c->max_bud_bytes < 4 * (long long)c->leb_size)
+			c->max_bud_bytes = 4 * (long long)c->leb_size;
 	}
 
 	if (c->log_lebs == -1) {
Signature · 6 lines
2.30.2


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

Report of the static analyzer:
The value of an arithmetic expression '4 * c->leb_size' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
To avoid overflow, we cast one of the operands (in this case c->leb_size) to type long long, which has a larger range.

Triggers found by static analyzer Svace.

---
  ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +0 -0
index b5f3892..9f276c5 100644

ubifs-utils/mkfs.ubifs/mkfs.ubifs.c static int get_options(int argc, char**argv) · line 863

Quoted 8 lines
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +2 -2
@@ -858,8 +858,8 @@ static int get_options(int argc, char**argv)
  		/* Make the max journal size 8MiB */
  		if (c->max_bud_bytes > 8 * 1024 * 1024)
  			c->max_bud_bytes = 8 * 1024 * 1024;
-		if (c->max_bud_bytes < 4 * c->leb_size)
-			c->max_bud_bytes = 4 * c->leb_size;
+		if (c->max_bud_bytes < 4 * (long long)c->leb_size)
+			c->max_bud_bytes = 4 * (long long)c->leb_size;
  	}

The '4 * c->leb_size' could trigger an overflow, because 'c->leb_size' is initialized by user option '-e'. However, function validate_options() checks 'c->leb_size' and terminates the program if it exceeds UBIFS_MAX_LEB_SZ[2M], so there will be no effects.

ubifs-utils/mkfs.ubifs/mkfs.ubifs.c static int get_options(int argc, char**argv) · line 865

Quoted 3 lines
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +0 -0
  	if (c->log_lebs == -1) {
 

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