diff mbox series

[mtd-utils] ubi-utils: ubirsvol: Fix integer overflow in ubirsvol.c

Message ID 20241207141855.240376-1-ant.v.moryakov@gmail.com
State Superseded
Delegated to: David Oberhollenzer
Headers show
Series [mtd-utils] ubi-utils: ubirsvol: Fix integer overflow in ubirsvol.c | expand

Commit Message

Anton Moryakov Dec. 7, 2024, 2:18 p.m. UTC
Report of the static analyzer:
The value of an arithmetic expression 'vol_info.leb_size * args.lebs' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
the fix ensures values ​​are checked before multiplication. 
an exception check for the negativity of vol_info.leb_size and args.lebs was added, as well as casting vol_info.leb_size to long long

Triggers found by static analyzer Svace.

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

---
 ubi-utils/ubirsvol.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/ubi-utils/ubirsvol.c b/ubi-utils/ubirsvol.c
index 0854abc..87286d4 100644
--- a/ubi-utils/ubirsvol.c
+++ b/ubi-utils/ubirsvol.c
@@ -230,8 +230,10 @@  int main(int argc, char * const argv[])
 		}
 	}
 
-	if (args.lebs != -1)
-		args.bytes = vol_info.leb_size * args.lebs;
+	if (args.lebs != -1){
+		if(vol_info.leb_size > 0 && args.lebs > 0)
+			args.bytes = (long long)vol_info.leb_size * args.lebs;
+	}
 
 	err = ubi_rsvol(libubi, args.node, args.vol_id, args.bytes);
 	if (err) {