Skip to contentSkip to conversation

ubi-utils: FIX DIVISION BY ZERO in ubinize.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 ubi-utils: FIX DIVISION BY ZERO in ubinize.c

Report of the static analyzer:
Variable vi->alignment, whose possible value set allows a zero value at ubinize.c:375, is used as a denominator at ubinize.c:410.

If you look at the code more closely, it will be clear that the vi->alignment parameter is obtained from an external file passed as a command line argument.

A check was also performed if you pass a test.ini file of the following type to the input:

[jffs2-volume]
mode=ubi
image=../jffs2.img
vol_id=1
vol_size=30MiB
vol_type=dynamic
vol_name=jffs2_volume
vol_flags=autoresize
vol_alignment=0

and execute the command:

./ubinize -o ubi.img -p 16KiB -m 512 -s 256 test.ini

we will get the result:

Floating point exception (core dumped)

Corrections explained:

Triggers found by static analyzer Svace.

---
 ubi-utils/ubinize.c | 8 ++++++++
 1 file changed, 8 insertions(+)
ubi-utils/ubinize.c +8 -0
index ac8c1e5..afffaa0 100644
@@ -406,6 +406,14 @@ static int read_section(const struct ubigen_info *ui, const char *sname,
 		}
 	}
 
+
+	/* Validate alignment to avoid division by zero */
+	if (vi->alignment <= 0) {
+		fprintf(stderr, "Error: alignment value (%d) is invalid. It must be greater than 0.\n", vi->alignment);
+		vi->alignment = 1; /* Set a safe default value */
+		fprintf(stderr, "Warning: alignment set to default value (1).\n");
+	}
+
 	/* Initialize the rest of the volume information */
 	vi->data_pad = ui->leb_size % vi->alignment;
 	vi->usable_leb_size = ui->leb_size - vi->data_pad;
Signature · 6 lines
2.30.2


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

Report of the static analyzer:
Variable vi->alignment, whose possible value set allows a zero value at ubinize.c:375, is used as a denominator at ubinize.c:410.

If you look at the code more closely, it will be clear that the vi->alignment parameter is obtained from an external file passed as a command line argument.

A check was also performed if you pass a test.ini file of the following type to the input:

[jffs2-volume]
mode=ubi
image=../jffs2.img
vol_id=1
vol_size=30MiB
vol_type=dynamic
vol_name=jffs2_volume
vol_flags=autoresize
vol_alignment=0

and execute the command:

./ubinize -o ubi.img -p 16KiB -m 512 -s 256 test.ini

we will get the result:

Floating point exception (core dumped)

Corrections explained:

Triggers found by static analyzer Svace.

---
  ubi-utils/ubinize.c | 8 ++++++++
  1 file changed, 8 insertions(+)
ubi-utils/ubinize.c +0 -0
index ac8c1e5..afffaa0 100644
@@ -406,6 +406,14 @@ static int read_section(const struct ubigen_info *ui, const char *sname,
  		}

ubi-utils/ubinize.c static int read_section(const struct ubigen_info *ui, const char *sname, · line 416

Quoted 10 lines
ubi-utils/ubinize.c +8 -0
  	}
 
+
+	/* Validate alignment to avoid division by zero */
+	if (vi->alignment <= 0) {
+		fprintf(stderr, "Error: alignment value (%d) is invalid. It must be greater than 0.\n", vi->alignment);
+		vi->alignment = 1; /* Set a safe default value */
+		fprintf(stderr, "Warning: alignment set to default value (1).\n");
+	}
+

How about changing like this?

ubi-utils/ubinize.c +2 -2
index ac8c1e5..9c950b1 100644
@@ -375,8 +375,8 @@ static int read_section(const struct ubigen_info *ui, const char *sname,
        vi->alignment = iniparser_getint(args.dict, buf, -1);
        if (vi->alignment == -1)
                vi->alignment = 1;
-       else if (vi->id < 0)
-               return errmsg("negative volume alignment %d in section \"%s\"",
+       else if (vi->alignment <= 0)
+               return errmsg("not positive volume alignment %d in section \"%s\"",
                              vi->alignment, sname);
 
        verbose(args.verbose, "volume alignment: %d", vi->alignment);

I find that the 'vi->id' is misspelled as 'vi->alignment'.

ubi-utils/ubinize.c static int read_section(const struct ubigen_info *ui, const char *sname, · line 419

Quoted 4 lines
ubi-utils/ubinize.c +0 -0
  	/* Initialize the rest of the volume information */
  	vi->data_pad = ui->leb_size % vi->alignment;
  	vi->usable_leb_size = ui->leb_size - vi->data_pad;

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