* [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c
@ 2024-12-12 18:39 Anton Moryakov
2024-12-13 2:37 ` Zhihao Cheng
0 siblings, 1 reply; 8+ messages in thread
From: Anton Moryakov @ 2024-12-12 18:39 UTC (permalink / raw)
To: chengzhihao1, linux-mtd; +Cc: Anton Moryakov
Report of the static analyzer:
The value of an arithmetic expression 'bit_to_flip->block * mtd.eb_size + blkoffs' is a subject to overflow because its operands are not cast to a larger data type before performing arith$
Corrections explained:
Prevent arithmetic overflow in OOB read operation
Resolved an issue where the calculation of the offset in the OOB read operation could overflow due to operands not being cast to a larger data type. Specifically, the multiplication of bi$
Triggers found by static analyzer Svace.
Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
nand-utils/nandflipbits.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
index 7066408..ef663c6 100644
--- a/nand-utils/nandflipbits.c
+++ b/nand-utils/nandflipbits.c
@@ -251,7 +251,7 @@ int main(int argc, char **argv)
bufoffs += mtd.min_io_size;
ret = mtd_read_oob(mtd_desc, &mtd, fd,
- bit_to_flip->block * mtd.eb_size +
+ (long long)bit_to_flip->block * (long long)mtd.eb_size +
blkoffs,
mtd.oob_size, buffer + bufoffs);
if (ret) {
--
2.30.2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c
2024-12-12 18:39 [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c Anton Moryakov
@ 2024-12-13 2:37 ` Zhihao Cheng
0 siblings, 0 replies; 8+ messages in thread
From: Zhihao Cheng @ 2024-12-13 2:37 UTC (permalink / raw)
To: Anton Moryakov, linux-mtd
在 2024/12/13 2:39, Anton Moryakov 写道:
> Report of the static analyzer:
> The value of an arithmetic expression 'bit_to_flip->block * mtd.eb_size + blkoffs' is a subject to overflow because its operands are not cast to a larger data type before performing arith$
>
> Corrections explained:
> Prevent arithmetic overflow in OOB read operation
> Resolved an issue where the calculation of the offset in the OOB read operation could overflow due to operands not being cast to a larger data type. Specifically, the multiplication of bi$
>
> Triggers found by static analyzer Svace.
>
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
>
> ---
> nand-utils/nandflipbits.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
> index 7066408..ef663c6 100644
> --- a/nand-utils/nandflipbits.c
> +++ b/nand-utils/nandflipbits.c
> @@ -251,7 +251,7 @@ int main(int argc, char **argv)
> bufoffs += mtd.min_io_size;
>
> ret = mtd_read_oob(mtd_desc, &mtd, fd,
> - bit_to_flip->block * mtd.eb_size +
> + (long long)bit_to_flip->block * (long long)mtd.eb_size +
Hi Anton.
Type converting for 'bit_to_flip->block' is enough, and it could be
better making the type as 'unsigned long long'.
> blkoffs,
> mtd.oob_size, buffer + bufoffs);
> if (ret) {
>
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c
@ 2024-12-13 13:19 Anton Moryakov
2024-12-14 2:59 ` Zhihao Cheng
0 siblings, 1 reply; 8+ messages in thread
From: Anton Moryakov @ 2024-12-13 13:19 UTC (permalink / raw)
To: chengzhihao1, linux-mtd; +Cc: Anton Moryakov
Report of the static analyzer:
The value of an arithmetic expression 'bit_to_flip->block * mtd.eb_size + blkoffs' is a subject to overflow because its operands are not cast to a larger data type before performing arith$
Corrections explained:
Prevent arithmetic overflow in OOB read operation
Resolved an issue where the calculation of the offset in the OOB read operation could overflow due to operands not being cast to a larger data type. Specifically, the multiplication of bi$
Triggers found by static analyzer Svace.
Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
nand-utils/nandflipbits.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
index 7066408..ef663c6 100644
--- a/nand-utils/nandflipbits.c
+++ b/nand-utils/nandflipbits.c
@@ -251,7 +251,7 @@ int main(int argc, char **argv)
bufoffs += mtd.min_io_size;
ret = mtd_read_oob(mtd_desc, &mtd, fd,
- bit_to_flip->block * mtd.eb_size +
+ (unsigned long long)bit_to_flip->block * (long long)mtd.eb_size +
blkoffs,
mtd.oob_size, buffer + bufoffs);
if (ret) {
--
2.30.2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c
2024-12-13 13:19 Anton Moryakov
@ 2024-12-14 2:59 ` Zhihao Cheng
0 siblings, 0 replies; 8+ messages in thread
From: Zhihao Cheng @ 2024-12-14 2:59 UTC (permalink / raw)
To: Anton Moryakov, linux-mtd
在 2024/12/13 21:19, Anton Moryakov 写道:
> Report of the static analyzer:
> The value of an arithmetic expression 'bit_to_flip->block * mtd.eb_size + blkoffs' is a subject to overflow because its operands are not cast to a larger data type before performing arith$
>
> Corrections explained:
> Prevent arithmetic overflow in OOB read operation
> Resolved an issue where the calculation of the offset in the OOB read operation could overflow due to operands not being cast to a larger data type. Specifically, the multiplication of bi$
>
> Triggers found by static analyzer Svace.
>
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
>
> ---
> nand-utils/nandflipbits.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
> index 7066408..ef663c6 100644
> --- a/nand-utils/nandflipbits.c
> +++ b/nand-utils/nandflipbits.c
> @@ -251,7 +251,7 @@ int main(int argc, char **argv)
> bufoffs += mtd.min_io_size;
>
> ret = mtd_read_oob(mtd_desc, &mtd, fd,
> - bit_to_flip->block * mtd.eb_size +
> + (unsigned long long)bit_to_flip->block * (long long)mtd.eb_size +
Hi, Anton. Change it like:
(unsigned long long)bit_to_flip->block * mtd.eb_size + blkoffs
> blkoffs,
> mtd.oob_size, buffer + bufoffs);
> if (ret) {
>
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c
@ 2024-12-14 12:18 Anton Moryakov
2024-12-16 1:08 ` Zhihao Cheng
0 siblings, 1 reply; 8+ messages in thread
From: Anton Moryakov @ 2024-12-14 12:18 UTC (permalink / raw)
To: chengzhihao1, linux-mtd; +Cc: Anton Moryakov
Report of the static analyzer:
The value of an arithmetic expression 'bit_to_flip->block * mtd.eb_size + blkoffs' is a subject to overflow because its operands are not cast to a larger data type before performing arith$
Corrections explained:
Prevent arithmetic overflow in OOB read operation
Resolved an issue where the calculation of the offset in the OOB read operation could overflow due to operands not being cast to a larger data type. Specifically, the multiplication of bi$
Triggers found by static analyzer Svace.
Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
nand-utils/nandflipbits.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
index 7066408..ef663c6 100644
--- a/nand-utils/nandflipbits.c
+++ b/nand-utils/nandflipbits.c
@@ -251,7 +251,7 @@ int main(int argc, char **argv)
bufoffs += mtd.min_io_size;
ret = mtd_read_oob(mtd_desc, &mtd, fd,
- bit_to_flip->block * mtd.eb_size +
+ (unsigned long long)bit_to_flip->block * mtd.eb_size +
blkoffs,
mtd.oob_size, buffer + bufoffs);
if (ret) {
--
2.30.2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c
2024-12-14 12:18 Anton Moryakov
@ 2024-12-16 1:08 ` Zhihao Cheng
0 siblings, 0 replies; 8+ messages in thread
From: Zhihao Cheng @ 2024-12-16 1:08 UTC (permalink / raw)
To: Anton Moryakov, linux-mtd
在 2024/12/14 20:18, Anton Moryakov 写道:
> Report of the static analyzer:
> The value of an arithmetic expression 'bit_to_flip->block * mtd.eb_size + blkoffs' is a subject to overflow because its operands are not cast to a larger data type before performing arith$
>
> Corrections explained:
> Prevent arithmetic overflow in OOB read operation
> Resolved an issue where the calculation of the offset in the OOB read operation could overflow due to operands not being cast to a larger data type. Specifically, the multiplication of bi$
>
> Triggers found by static analyzer Svace.
>
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
>
> ---
> nand-utils/nandflipbits.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
> diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
> index 7066408..ef663c6 100644
> --- a/nand-utils/nandflipbits.c
> +++ b/nand-utils/nandflipbits.c
> @@ -251,7 +251,7 @@ int main(int argc, char **argv)
> bufoffs += mtd.min_io_size;
>
> ret = mtd_read_oob(mtd_desc, &mtd, fd,
> - bit_to_flip->block * mtd.eb_size +
> + (unsigned long long)bit_to_flip->block * mtd.eb_size +
> blkoffs,
> mtd.oob_size, buffer + bufoffs);
> if (ret) {
>
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c
@ 2024-12-17 16:36 Anton Moryakov
2024-12-18 2:50 ` Zhihao Cheng
0 siblings, 1 reply; 8+ messages in thread
From: Anton Moryakov @ 2024-12-17 16:36 UTC (permalink / raw)
To: chengzhihao1, linux-mtd; +Cc: Anton Moryakov
Report of the static analyzer:
The value of an arithmetic expression 'page * (mtd.min_io_size + mtd.oob_size)' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic
Corrections explained:
Casting bit_to_flip->block to unsigned long long expands the range of values and allows the result of the multiplication to be calculated in a wider data type.
The arithmetic is performed in a safe range, preventing overflow.
Sum + blkoffs is now also safe, since the result of the expression is already calculated in unsigned long long.
Triggers found by static analyzer Svace.
Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
nand-utils/nandflipbits.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
index ef663c6..c4642fe 100644
--- a/nand-utils/nandflipbits.c
+++ b/nand-utils/nandflipbits.c
@@ -213,7 +213,7 @@ int main(int argc, char **argv)
bits_to_flip[i].block = bits_to_flip[i].offset / blklen;
bits_to_flip[i].offset %= blklen;
page = bits_to_flip[i].offset / pagelen;
- bits_to_flip[i].offset = (page *
+ bits_to_flip[i].offset = ((unsigned long long)page *
(mtd.min_io_size + mtd.oob_size)) +
(bits_to_flip[i].offset % pagelen);
}
--
2.30.2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c
2024-12-17 16:36 Anton Moryakov
@ 2024-12-18 2:50 ` Zhihao Cheng
0 siblings, 0 replies; 8+ messages in thread
From: Zhihao Cheng @ 2024-12-18 2:50 UTC (permalink / raw)
To: Anton Moryakov, linux-mtd
在 2024/12/18 0:36, Anton Moryakov 写道:
> Report of the static analyzer:
> The value of an arithmetic expression 'page * (mtd.min_io_size + mtd.oob_size)' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic
>
> Corrections explained:
> Casting bit_to_flip->block to unsigned long long expands the range of values and allows the result of the multiplication to be calculated in a wider data type.
> The arithmetic is performed in a safe range, preventing overflow.
> Sum + blkoffs is now also safe, since the result of the expression is already calculated in unsigned long long.
>
> Triggers found by static analyzer Svace.
>
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
>
> ---
> nand-utils/nandflipbits.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
> index ef663c6..c4642fe 100644
> --- a/nand-utils/nandflipbits.c
> +++ b/nand-utils/nandflipbits.c
> @@ -213,7 +213,7 @@ int main(int argc, char **argv)
> bits_to_flip[i].block = bits_to_flip[i].offset / blklen;
> bits_to_flip[i].offset %= blklen;
> page = bits_to_flip[i].offset / pagelen;
> - bits_to_flip[i].offset = (page *
> + bits_to_flip[i].offset = ((unsigned long long)page *
> (mtd.min_io_size + mtd.oob_size)) +
> (bits_to_flip[i].offset % pagelen);
I think the 'bits_to_flip[i].offset' is limited smaller than
'mtd.eb_size' by the code:
bits_to_flip[i].offset %= blklen;
page = bits_to_flip[i].offset / pagelen;
So, I don't think an overflow could happen here.
> }
>
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2024-12-18 2:51 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-12 18:39 [PATCH mtd-utils] nand-utils: Fix integer overflow in nandflipbits.c Anton Moryakov
2024-12-13 2:37 ` Zhihao Cheng
-- strict thread matches above, loose matches on Subject: below --
2024-12-13 13:19 Anton Moryakov
2024-12-14 2:59 ` Zhihao Cheng
2024-12-14 12:18 Anton Moryakov
2024-12-16 1:08 ` Zhihao Cheng
2024-12-17 16:36 Anton Moryakov
2024-12-18 2:50 ` Zhihao Cheng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox