Skip to contentSkip to conversation

FIX: NO_CAST.INTEGER_OVERFLOW in ubirsvol.c

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 FIX: NO_CAST.INTEGER_OVERFLOW in ubirsvol.c

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 uint64_t

Triggers found by static analyzer Svace.

---
 ubi-utils/ubirsvol.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
ubi-utils/ubirsvol.c +4 -2
index 0854abc..87286d4 100644
@@ -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 =(uint64_t)vol_info.leb_size * args.lebs;
+	}
 
 	err = ubi_rsvol(libubi, args.node, args.vol_id, args.bytes);
 	if (err) {
Signature · 6 lines
2.30.2


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

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 uint64_t

Quoted 7 lines

Triggers found by static analyzer Svace.

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

Hi Anton. Please make the title be something like '[PATCH mtd-utils] ubi-utils: ubirsvol: Fix integer overflow ...'.

Quoted 6 lines
ubi-utils/ubirsvol.c +0 -0
index 0854abc..87286d4 100644

ubi-utils/ubirsvol.c int main(int argc, char * const argv[]) · line 236

Quoted 9 lines
ubi-utils/ubirsvol.c +4 -2
@@ -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 =(uint64_t)vol_info.leb_size * args.lebs;
+	}

The 'args.bytes' is a long long type value and will be parsed as s64 in ioctl(UBI_IOCRSVOL), so we should convert the type as 'long long' or 'int64_t'. The 'vol_info.leb_size' and 'args.lebs' are guaranteed to be positive numbers by ubi_get_vol_info1() and parse_opt(), so we can write this like:
if (args.lebs != -1)

	args.bytes = (long long)vol_info.leb_size * args.lebs;

ubi-utils/ubirsvol.c int main(int argc, char * const argv[])

Quoted 4 lines
ubi-utils/ubirsvol.c +0 -0
  	err = ubi_rsvol(libubi, args.node, args.vol_id, args.bytes);
  	if (err) {
 

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