All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] FIX: NO_CAST.INTEGER_OVERFLOW in ubirsvol.c
@ 2024-12-06 12:34 Anton Moryakov
  0 siblings, 0 replies; 4+ messages in thread
From: Anton Moryakov @ 2024-12-06 12:34 UTC (permalink / raw)
  To: linux-mtd; +Cc: 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

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 --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 = vol_info.leb_size * args.lebs;
+	}
 
 	err = ubi_rsvol(libubi, args.node, args.vol_id, args.bytes);
 	if (err) {
-- 
2.30.2


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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] FIX: NO_CAST.INTEGER_OVERFLOW in ubirsvol.c
@ 2024-12-06 12:37 Anton Moryakov
  2024-12-07  5:00 ` Zhihao Cheng
  0 siblings, 1 reply; 4+ messages in thread
From: Anton Moryakov @ 2024-12-06 12:37 UTC (permalink / raw)
  To: linux-mtd; +Cc: 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

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 --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 =(uint64_t)vol_info.leb_size * args.lebs;
+	}
 
 	err = ubi_rsvol(libubi, args.node, args.vol_id, args.bytes);
 	if (err) {
-- 
2.30.2


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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] FIX: NO_CAST.INTEGER_OVERFLOW in ubirsvol.c
  2024-12-06 12:37 [PATCH] FIX: NO_CAST.INTEGER_OVERFLOW in ubirsvol.c Anton Moryakov
@ 2024-12-07  5:00 ` Zhihao Cheng
  0 siblings, 0 replies; 4+ messages in thread
From: Zhihao Cheng @ 2024-12-07  5:00 UTC (permalink / raw)
  To: Anton Moryakov, linux-mtd

在 2024/12/6 20:37, 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
> 
> 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(-)

Hi Anton. Please make the title be something like '[PATCH mtd-utils] 
ubi-utils: ubirsvol: Fix integer overflow ...'.
> 
> 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 =(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;

>   
>   	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/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] FIX: NO_CAST.INTEGER_OVERFLOW in ubirsvol.c
@ 2024-12-09 21:09 Anton Moryakov
  0 siblings, 0 replies; 4+ messages in thread
From: Anton Moryakov @ 2024-12-09 21:09 UTC (permalink / raw)
  To: linux-mtd, chengzhihao1; +Cc: 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.
Added 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 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ubi-utils/ubirsvol.c b/ubi-utils/ubirsvol.c
index 0854abc..73d2f68 100644
--- a/ubi-utils/ubirsvol.c
+++ b/ubi-utils/ubirsvol.c
@@ -231,7 +231,7 @@ int main(int argc, char * const argv[])
 	}
 
 	if (args.lebs != -1)
-		args.bytes = vol_info.leb_size * args.lebs;
+		args.bytes = (long long)vol_info.leb_size * args.lebs;
 
 	err = ubi_rsvol(libubi, args.node, args.vol_id, args.bytes);
 	if (err) {
-- 
2.30.2


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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-12-09 21:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-06 12:37 [PATCH] FIX: NO_CAST.INTEGER_OVERFLOW in ubirsvol.c Anton Moryakov
2024-12-07  5:00 ` Zhihao Cheng
  -- strict thread matches above, loose matches on Subject: below --
2024-12-09 21:09 Anton Moryakov
2024-12-06 12:34 Anton Moryakov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.