[PATCH mtd-utils] ubi-utils: FIX DIVISION BY ZERO in ubinize.c

Anton Moryakov ant.v.moryakov at gmail.com
Tue Dec 24 11:40:13 PST 2024


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:
Updated the validation logic for vi->alignment:
- Replaced the check for negative alignment (`vi->id < 0`) with a more comprehensive check for non-positive alignment (`vi->alignment <= 0`).
- Updated the corresponding error message to reflect the requirement for a positive volume alignment.
This ensures more robust validation and improves error clarity when invalid alignment values are encountered.

Triggers found by static analyzer Svace.

Signed-off-by: Anton Moryakov <ant.v.moryakov at gmail.com>

---
 ubi-utils/ubinize.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ubi-utils/ubinize.c b/ubi-utils/ubinize.c
index ac8c1e5..9c950b1 100644
--- a/ubi-utils/ubinize.c
+++ b/ubi-utils/ubinize.c
@@ -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);
-- 
2.30.2




More information about the linux-mtd mailing list