linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: nand: ecc-mxic: Fix use of uninitialized variable ret
@ 2025-04-08 21:39 Mikhail Arkhipov
  2025-04-09  7:38 ` Miquel Raynal
  2025-04-29  9:26 ` Miquel Raynal
  0 siblings, 2 replies; 3+ messages in thread
From: Mikhail Arkhipov @ 2025-04-08 21:39 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Mikhail Arkhipov, Richard Weinberger, Vignesh Raghavendra,
	linux-mtd, linux-kernel, lvc-project

If ctx->steps is zero, the loop processing ECC steps is skipped,
and the variable ret remains uninitialized. It is later checked
and returned, which leads to undefined behavior and may cause
unpredictable results in user space or kernel crashes.

This scenario can be triggered in edge cases such as misconfigured
geometry, ECC engine misuse, or if ctx->steps is not validated
after initialization.

Initialize ret to zero before the loop to ensure correct and safe
behavior regardless of the ctx->steps value.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 48e6633a9fa2 ("mtd: nand: mxic-ecc: Add Macronix external ECC engine support")
Signed-off-by: Mikhail Arkhipov <m.arhipov@rosa.ru>
---
 drivers/mtd/nand/ecc-mxic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/ecc-mxic.c b/drivers/mtd/nand/ecc-mxic.c
index 6b487ffe2f2d..e8bbe009c04e 100644
--- a/drivers/mtd/nand/ecc-mxic.c
+++ b/drivers/mtd/nand/ecc-mxic.c
@@ -614,7 +614,7 @@ static int mxic_ecc_finish_io_req_external(struct nand_device *nand,
 {
 	struct mxic_ecc_engine *mxic = nand_to_mxic(nand);
 	struct mxic_ecc_ctx *ctx = nand_to_ecc_ctx(nand);
-	int nents, step, ret;
+	int nents, step, ret = 0;
 
 	if (req->mode == MTD_OPS_RAW)
 		return 0;
-- 
2.39.5 (Apple Git-154)


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

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

* Re: [PATCH] mtd: nand: ecc-mxic: Fix use of uninitialized variable ret
  2025-04-08 21:39 [PATCH] mtd: nand: ecc-mxic: Fix use of uninitialized variable ret Mikhail Arkhipov
@ 2025-04-09  7:38 ` Miquel Raynal
  2025-04-29  9:26 ` Miquel Raynal
  1 sibling, 0 replies; 3+ messages in thread
From: Miquel Raynal @ 2025-04-09  7:38 UTC (permalink / raw)
  To: Mikhail Arkhipov
  Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
	lvc-project

On 09/04/2025 at 00:39:06 +03, Mikhail Arkhipov <m.arhipov@rosa.ru> wrote:

> If ctx->steps is zero, the loop processing ECC steps is skipped,
> and the variable ret remains uninitialized. It is later checked
> and returned, which leads to undefined behavior and may cause
> unpredictable results in user space or kernel crashes.
>
> This scenario can be triggered in edge cases such as misconfigured
> geometry,

Would clearly be a huge bug, nothing else would work in this case, not a
single read, ie. we would stop way before reaching this point.

> ECC engine misuse,

Ditto.

> or if ctx->steps is not validated after initialization.

steps = mtd->writesize / 1K
and
we only run on large-page NANDs, ie. chips with mtd->writesize >= 1K.
so this case is not plausible.

> Initialize ret to zero before the loop to ensure correct and safe
> behavior regardless of the ctx->steps value.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Fixes: 48e6633a9fa2 ("mtd: nand: mxic-ecc: Add Macronix external ECC
> engine support")

I will apply, but honestly I do not think this fixes anything.

> Signed-off-by: Mikhail Arkhipov <m.arhipov@rosa.ru>

Thanks,
Miquèl

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

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

* Re: [PATCH] mtd: nand: ecc-mxic: Fix use of uninitialized variable ret
  2025-04-08 21:39 [PATCH] mtd: nand: ecc-mxic: Fix use of uninitialized variable ret Mikhail Arkhipov
  2025-04-09  7:38 ` Miquel Raynal
@ 2025-04-29  9:26 ` Miquel Raynal
  1 sibling, 0 replies; 3+ messages in thread
From: Miquel Raynal @ 2025-04-29  9:26 UTC (permalink / raw)
  To: Mikhail Arkhipov
  Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
	lvc-project

On Wed, 09 Apr 2025 00:39:06 +0300, Mikhail Arkhipov wrote:
> If ctx->steps is zero, the loop processing ECC steps is skipped,
> and the variable ret remains uninitialized. It is later checked
> and returned, which leads to undefined behavior and may cause
> unpredictable results in user space or kernel crashes.
> 
> This scenario can be triggered in edge cases such as misconfigured
> geometry, ECC engine misuse, or if ctx->steps is not validated
> after initialization.
> 
> [...]

Applied to nand/next, thanks!

[1/1] mtd: nand: ecc-mxic: Fix use of uninitialized variable ret
      commit: d95846350aac72303036a70c4cdc69ae314aa26d

Patche(s) should be available on mtd/linux.git and will be
part of the next PR (provided that no robot complains by then).

Kind regards,
Miquèl


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

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

end of thread, other threads:[~2025-04-29  9:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-08 21:39 [PATCH] mtd: nand: ecc-mxic: Fix use of uninitialized variable ret Mikhail Arkhipov
2025-04-09  7:38 ` Miquel Raynal
2025-04-29  9:26 ` Miquel Raynal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).