public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH mtd-utils] misc-utils: Fix integer overflow in docfdisk.c
@ 2024-12-17 12:44 Anton Moryakov
  2024-12-18  1:30 ` Zhihao Cheng
  0 siblings, 1 reply; 2+ messages in thread
From: Anton Moryakov @ 2024-12-17 12:44 UTC (permalink / raw)
  To: chengzhihao1, linux-mtd; +Cc: Anton Moryakov

Report of the static analyzer:
Possible integer underflow: left operand is tainted. An integer underflow may occur due to arithmetic operation (subtraction) between variable 'block' and value '1', when 'block' is tainted { [-2147483648, 2147483647] }

Corrections explained:
Added the block <= 0 check, which prevents integer underflow when calculating block - 1 and ensures that block is always positive.

Triggers found by static analyzer Svace.

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

---
 misc-utils/docfdisk.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/misc-utils/docfdisk.c b/misc-utils/docfdisk.c
index 486ce29..e473ceb 100644
--- a/misc-utils/docfdisk.c
+++ b/misc-utils/docfdisk.c
@@ -255,6 +255,10 @@ int main(int argc, char **argv)
 		ip->firstUnit = cpu_to_le32(block);
 		if (!nblocks[i])
 			nblocks[i] = totblocks - block;
+		if (block <= 0) {
+			fprintf(stderr, "Error: Invalid block value (%d). Block must be greater than 0.\n", block);
+			return -1;
+		}
 		ip->virtualUnits = cpu_to_le32(nblocks[i]);
 		block += nblocks[i];
 		ip->lastUnit = cpu_to_le32(block-1);
-- 
2.30.2


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

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

* Re: [PATCH mtd-utils] misc-utils: Fix integer overflow in docfdisk.c
  2024-12-17 12:44 [PATCH mtd-utils] misc-utils: Fix integer overflow in docfdisk.c Anton Moryakov
@ 2024-12-18  1:30 ` Zhihao Cheng
  0 siblings, 0 replies; 2+ messages in thread
From: Zhihao Cheng @ 2024-12-18  1:30 UTC (permalink / raw)
  To: Anton Moryakov, linux-mtd

在 2024/12/17 20:44, Anton Moryakov 写道:
> Report of the static analyzer:
> Possible integer underflow: left operand is tainted. An integer underflow may occur due to arithmetic operation (subtraction) between variable 'block' and value '1', when 'block' is tainted { [-2147483648, 2147483647] }
> 
> Corrections explained:
> Added the block <= 0 check, which prevents integer underflow when calculating block - 1 and ensures that block is always positive.
> 
> Triggers found by static analyzer Svace.
> 
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
> 
> ---
>   misc-utils/docfdisk.c | 4 ++++
>   1 file changed, 4 insertions(+)
> 
> diff --git a/misc-utils/docfdisk.c b/misc-utils/docfdisk.c
> index 486ce29..e473ceb 100644
> --- a/misc-utils/docfdisk.c
> +++ b/misc-utils/docfdisk.c
> @@ -255,6 +255,10 @@ int main(int argc, char **argv)
>   		ip->firstUnit = cpu_to_le32(block);
>   		if (!nblocks[i])
>   			nblocks[i] = totblocks - block;
> +		if (block <= 0) {
> +			fprintf(stderr, "Error: Invalid block value (%d). Block must be greater than 0.\n", block);
> +			return -1;
> +		}

In the previous code, 'block' is already initialized as:
  block = mhoffs / unitsize;
  block++;
So I don't think underflow could happen.
>   		ip->virtualUnits = cpu_to_le32(nblocks[i]);
>   		block += nblocks[i];
>   		ip->lastUnit = cpu_to_le32(block-1);
> 


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

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

end of thread, other threads:[~2024-12-18  1:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-17 12:44 [PATCH mtd-utils] misc-utils: Fix integer overflow in docfdisk.c Anton Moryakov
2024-12-18  1:30 ` Zhihao Cheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox