* [PATCH 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct()
@ 2026-11-05 4:01 Guanglei Zhu
2026-11-05 4:01 ` [PATCH 2/2] mtd: rawnand: ingenic: validate BCH error index in jz4780_correct() Guanglei Zhu
2026-11-05 4:24 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Guanglei Zhu
0 siblings, 2 replies; 6+ messages in thread
From: Guanglei Zhu @ 2026-11-05 4:01 UTC (permalink / raw)
To: Paul Cercueil, Harvey Hunt, Miquel Raynal
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mips, linux-mtd,
linux-kernel, zhugl3, Weibin Liu
From: Weibin Liu <liuwb@xiaopeng.com>
The JZ4725B BCH controller reports each corrected bit error as an
index into the codeword it was asked to decode, in the INDEX0/INDEX1
fields of the BCH_BHERR registers. These fields are 13 bits wide, but
nothing in jz4725b_correct() checks the reported index against the
size of the data buffer being corrected:
buf[(bit >> 3)] ^= BIT(bit & 0x7);
With data or ECC coming from an untrusted flash, the decoder can
report an index that points past the end of the data buffer, either
into the parity area of the codeword or beyond it entirely, and the
above statement then flips a bit outside the buffer.
Reject the page with -EBADMSG when the reported index does not fit
in the data area.
Fixes: 3e6ac2ad90b3 ("mtd: rawnand: ingenic: Add support for the JZ4725B")
Signed-off-by: Weibin Liu <liuwb@xiaopeng.com>
---
Verified with a userspace replica of the correction loop: with
params->size = 512, an index pointing into the parity area (bit =
4099) or at the field maximum (0x1fff) flips bits past the data
buffer (ASAN heap-buffer-overflow); in-range indices keep
correcting as before.
drivers/mtd/nand/raw/ingenic/jz4725b_bch.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/mtd/nand/raw/ingenic/jz4725b_bch.c b/drivers/mtd/nand/raw/ingenic/jz4725b_bch.c
index 2d0e0a2192ae..727f135f381e 100644
--- a/drivers/mtd/nand/raw/ingenic/jz4725b_bch.c
+++ b/drivers/mtd/nand/raw/ingenic/jz4725b_bch.c
@@ -259,6 +259,11 @@ static int jz4725b_correct(struct ingenic_ecc *bch,
bit = (reg & BCH_BHERR_INDEX0_MASK) >> BCH_BHERR_INDEX0_SHIFT;
}
+ if (bit >= params->size * 8) {
+ ret = -EBADMSG;
+ goto out;
+ }
+
buf[(bit >> 3)] ^= BIT(bit & 0x7);
}
--
2.50.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH 2/2] mtd: rawnand: ingenic: validate BCH error index in jz4780_correct() 2026-11-05 4:01 [PATCH 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Guanglei Zhu @ 2026-11-05 4:01 ` Guanglei Zhu 2026-11-05 4:24 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Guanglei Zhu 1 sibling, 0 replies; 6+ messages in thread From: Guanglei Zhu @ 2026-11-05 4:01 UTC (permalink / raw) To: Paul Cercueil, Harvey Hunt, Miquel Raynal Cc: Richard Weinberger, Vignesh Raghavendra, linux-mips, linux-mtd, linux-kernel, zhugl3, Weibin Liu From: Weibin Liu <liuwb@xiaopeng.com> The JZ4780 BCH controller reports each corrected bit error as a byte pair index, in the INDEX field of the BCH_BHERR registers. The field is 11 bits wide, but nothing in jz4780_correct() checks the reported index against the size of the data buffer being corrected: buf[(index * 2) + 0] ^= mask; buf[(index * 2) + 1] ^= mask >> 8; With data or ECC coming from an untrusted flash, the decoder can report an index that points past the end of the data buffer, and the above statements then flip bits outside the buffer. Reject the page with -EBADMSG when the reported index does not fit in the data area. Fixes: ae02ab00aa3c ("mtd: nand: jz4780: driver for NAND devices on JZ4780 SoCs") Signed-off-by: Weibin Liu <liuwb@xiaopeng.com> --- Verified with a userspace replica of the correction loop: with params->size = 512, an index of 256 (first parity word) or at the field maximum (0x7ff) flips bits past the data buffer (ASAN heap-buffer-overflow); in-range indices keep correcting as before. drivers/mtd/nand/raw/ingenic/jz4780_bch.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/mtd/nand/raw/ingenic/jz4780_bch.c b/drivers/mtd/nand/raw/ingenic/jz4780_bch.c index 12b5b0484fe9..204bc853c1b4 100644 --- a/drivers/mtd/nand/raw/ingenic/jz4780_bch.c +++ b/drivers/mtd/nand/raw/ingenic/jz4780_bch.c @@ -216,6 +216,11 @@ static int jz4780_correct(struct ingenic_ecc *bch, BCH_BHERR_MASK_SHIFT; index = (reg & BCH_BHERR_INDEX_MASK) >> BCH_BHERR_INDEX_SHIFT; + if ((index * 2) + 1 >= params->size) { + ret = -EBADMSG; + goto out; + } + buf[(index * 2) + 0] ^= mask; buf[(index * 2) + 1] ^= mask >> 8; } -- 2.50.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() 2026-11-05 4:01 [PATCH 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Guanglei Zhu 2026-11-05 4:01 ` [PATCH 2/2] mtd: rawnand: ingenic: validate BCH error index in jz4780_correct() Guanglei Zhu @ 2026-11-05 4:24 ` Guanglei Zhu 2026-11-05 4:24 ` [PATCH v2 2/2] mtd: rawnand: ingenic: validate BCH error index in jz4780_correct() Guanglei Zhu 2026-11-06 14:37 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Paul Cercueil 1 sibling, 2 replies; 6+ messages in thread From: Guanglei Zhu @ 2026-11-05 4:24 UTC (permalink / raw) To: Paul Cercueil, Harvey Hunt, Miquel Raynal Cc: Richard Weinberger, Vignesh Raghavendra, linux-mips, linux-mtd, linux-kernel, zhugl3, Weibin Liu From: Weibin Liu <liuwb@xiaopeng.com> The JZ4725B BCH controller reports each corrected bit error as an index into the codeword it was asked to decode, in the INDEX0/INDEX1 fields of the BCH_BHERR registers. These fields are 13 bits wide, but nothing in jz4725b_correct() checks the reported index against the size of the data buffer being corrected: buf[(bit >> 3)] ^= BIT(bit & 0x7); With data or ECC coming from an untrusted flash, the decoder can report an index that points past the end of the data buffer, and the above statement then flips a bit outside the buffer. Skip the correction when the reported index does not point into the data area. A bitflip in the parity bytes does not affect the data, so rejecting the page for it would fail reads that are perfectly correctable, and an index beyond the codeword has no buffer to update in the first place. Fixes: 3e6ac2ad90b3 ("mtd: rawnand: ingenic: Add support for the JZ4725B") Signed-off-by: Weibin Liu <liuwb@xiaopeng.com> --- Changes in v2: skip the correction for an index that does not point into the data area instead of failing the page with -EBADMSG. A correctable bitflip in the ECC bytes leaves the data valid, and the early exit also dropped the error entries that followed it. This matches how jz4740_ecc_correct() treats its indexes. Verified with a userspace replica of the correction loop: with params->size = 512, an index of 4099 (parity area) followed by a data-area index of 100 in the same BHERR register is now corrected (v1 returned -EBADMSG and left the data error unfixed), the field maximum (0x1fff) produces no write outside the buffer, and in-range indexes keep correcting as before. drivers/mtd/nand/raw/ingenic/jz4725b_bch.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/mtd/nand/raw/ingenic/jz4725b_bch.c b/drivers/mtd/nand/raw/ingenic/jz4725b_bch.c index 2d0e0a2192ae..9b1c3d4e5f6a 100644 --- a/drivers/mtd/nand/raw/ingenic/jz4725b_bch.c +++ b/drivers/mtd/nand/raw/ingenic/jz4725b_bch.c @@ -259,6 +259,10 @@ static int jz4725b_correct(struct ingenic_ecc *bch, bit = (reg & BCH_BHERR_INDEX0_MASK) >> BCH_BHERR_INDEX0_SHIFT; } + /* Skip indexes outside the data area. */ + if (bit >= params->size * 8) + continue; + buf[(bit >> 3)] ^= BIT(bit & 0x7); } -- 2.50.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 2/2] mtd: rawnand: ingenic: validate BCH error index in jz4780_correct() 2026-11-05 4:24 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Guanglei Zhu @ 2026-11-05 4:24 ` Guanglei Zhu 2026-11-06 14:37 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Paul Cercueil 1 sibling, 0 replies; 6+ messages in thread From: Guanglei Zhu @ 2026-11-05 4:24 UTC (permalink / raw) To: Paul Cercueil, Harvey Hunt, Miquel Raynal Cc: Richard Weinberger, Vignesh Raghavendra, linux-mips, linux-mtd, linux-kernel, zhugl3, Weibin Liu From: Weibin Liu <liuwb@xiaopeng.com> The JZ4780 BCH controller reports each corrected bit error as a byte pair index, in the INDEX field of the BCH_BHERR registers. The field is 11 bits wide, but nothing in jz4780_correct() checks the reported index against the size of the data buffer being corrected: buf[(index * 2) + 0] ^= mask; buf[(index * 2) + 1] ^= mask >> 8; With data or ECC coming from an untrusted flash, the decoder can report an index that points past the end of the data buffer, and the above statements then flip bits outside the buffer. Skip the correction when the reported index does not point into the data area. A bitflip in the parity bytes does not affect the data, so rejecting the page for it would fail reads that are perfectly correctable, and an index beyond the codeword has no buffer to update in the first place. Fixes: ae02ab00aa3c ("mtd: nand: jz4780: driver for NAND devices on JZ4780 SoCs") Signed-off-by: Weibin Liu <liuwb@xiaopeng.com> --- Changes in v2: skip the correction for an index that does not point into the data area instead of failing the page with -EBADMSG. A correctable bitflip in the ECC bytes leaves the data valid, and the early exit also dropped the error entries that followed it. This matches how jz4740_ecc_correct() treats its indexes. Verified with a userspace replica of the correction loop: with params->size = 512, an index of 256 (first parity word) or at the field maximum (0x7ff) produces no write outside the data buffer, and in-range indexes keep correcting as before. drivers/mtd/nand/raw/ingenic/jz4780_bch.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/mtd/nand/raw/ingenic/jz4780_bch.c b/drivers/mtd/nand/raw/ingenic/jz4780_bch.c index 12b5b0484fe9..3a4c5d6e7f8a 100644 --- a/drivers/mtd/nand/raw/ingenic/jz4780_bch.c +++ b/drivers/mtd/nand/raw/ingenic/jz4780_bch.c @@ -216,6 +216,10 @@ static int jz4780_correct(struct ingenic_ecc *bch, BCH_BHERR_MASK_SHIFT; index = (reg & BCH_BHERR_INDEX_MASK) >> BCH_BHERR_INDEX_SHIFT; + /* Skip indexes outside the data area. */ + if ((index * 2) + 1 >= params->size) + continue; + buf[(index * 2) + 0] ^= mask; buf[(index * 2) + 1] ^= mask >> 8; } -- 2.50.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() 2026-11-05 4:24 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Guanglei Zhu 2026-11-05 4:24 ` [PATCH v2 2/2] mtd: rawnand: ingenic: validate BCH error index in jz4780_correct() Guanglei Zhu @ 2026-11-06 14:37 ` Paul Cercueil 2026-11-16 17:06 ` Miquel Raynal 1 sibling, 1 reply; 6+ messages in thread From: Paul Cercueil @ 2026-11-06 14:37 UTC (permalink / raw) To: Guanglei Zhu, Harvey Hunt, Miquel Raynal Cc: Richard Weinberger, Vignesh Raghavendra, linux-mips, linux-mtd, linux-kernel, Weibin Liu, Thomas Petazzoni Hi, Le jeudi 05 novembre 2026 à 12:24 +0800, Guanglei Zhu a écrit : > From: Weibin Liu <liuwb@xiaopeng.com> > > The JZ4725B BCH controller reports each corrected bit error as an > index into the codeword it was asked to decode, in the INDEX0/INDEX1 > fields of the BCH_BHERR registers. These fields are 13 bits wide, but > nothing in jz4725b_correct() checks the reported index against the > size of the data buffer being corrected: > > buf[(bit >> 3)] ^= BIT(bit & 0x7); > > With data or ECC coming from an untrusted flash, the decoder can > report an index that points past the end of the data buffer, and the > above statement then flips a bit outside the buffer. > > Skip the correction when the reported index does not point into > the data area. A bitflip in the parity bytes does not affect the > data, so rejecting the page for it would fail reads that are > perfectly correctable, and an index beyond the codeword has no > buffer to update in the first place. > > Fixes: 3e6ac2ad90b3 ("mtd: rawnand: ingenic: Add support for the JZ4725B") > Signed-off-by: Weibin Liu <liuwb@xiaopeng.com> > --- > Changes in v2: skip the correction for an index that does not point > into the data area instead of failing the page with -EBADMSG. A > correctable bitflip in the ECC bytes leaves the data valid, and the > early exit also dropped the error entries that followed it. This > matches how jz4740_ecc_correct() treats its indexes. > > Verified with a userspace replica of the correction loop: with > params->size = 512, an index of 4099 (parity area) followed by a > data-area index of 100 in the same BHERR register is now corrected > (v1 returned -EBADMSG and left the data error unfixed), the field > maximum (0x1fff) produces no write outside the buffer, and in-range > indexes keep correcting as before. What bothers me, and maybe it's just me not knowing NAND that well: wouldn't a bit-flip in the parity area cause a cascade of false-positives in the data too, leading to BCH_BHINT_UNCOR? Because the ECC code is read from the parity region, am I wrong? Cheers, -Paul ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() 2026-11-06 14:37 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Paul Cercueil @ 2026-11-16 17:06 ` Miquel Raynal 0 siblings, 0 replies; 6+ messages in thread From: Miquel Raynal @ 2026-11-16 17:06 UTC (permalink / raw) To: Paul Cercueil Cc: Guanglei Zhu, Harvey Hunt, Richard Weinberger, Vignesh Raghavendra, linux-mips, linux-mtd, linux-kernel, Weibin Liu, Thomas Petazzoni On 06/11/2026 at 15:37:47 +01, Paul Cercueil <paul@crapouillou.net> wrote: > Hi, > > Le jeudi 05 novembre 2026 à 12:24 +0800, Guanglei Zhu a écrit : >> From: Weibin Liu <liuwb@xiaopeng.com> >> >> The JZ4725B BCH controller reports each corrected bit error as an >> index into the codeword it was asked to decode, in the INDEX0/INDEX1 >> fields of the BCH_BHERR registers. These fields are 13 bits wide, but >> nothing in jz4725b_correct() checks the reported index against the >> size of the data buffer being corrected: >> >> buf[(bit >> 3)] ^= BIT(bit & 0x7); >> >> With data or ECC coming from an untrusted flash, the decoder can >> report an index that points past the end of the data buffer, and the >> above statement then flips a bit outside the buffer. >> >> Skip the correction when the reported index does not point into >> the data area. A bitflip in the parity bytes does not affect the >> data, so rejecting the page for it would fail reads that are >> perfectly correctable, and an index beyond the codeword has no >> buffer to update in the first place. >> >> Fixes: 3e6ac2ad90b3 ("mtd: rawnand: ingenic: Add support for the JZ4725B") >> Signed-off-by: Weibin Liu <liuwb@xiaopeng.com> >> --- >> Changes in v2: skip the correction for an index that does not point >> into the data area instead of failing the page with -EBADMSG. A >> correctable bitflip in the ECC bytes leaves the data valid, and the >> early exit also dropped the error entries that followed it. This >> matches how jz4740_ecc_correct() treats its indexes. >> >> Verified with a userspace replica of the correction loop: with >> params->size = 512, an index of 4099 (parity area) followed by a >> data-area index of 100 in the same BHERR register is now corrected >> (v1 returned -EBADMSG and left the data error unfixed), the field >> maximum (0x1fff) produces no write outside the buffer, and in-range >> indexes keep correcting as before. > > What bothers me, and maybe it's just me not knowing NAND that well: wouldn't a > bit-flip in the parity area cause a cascade of false-positives in the > data too, No, because the data vs. parity split only exist because our human brains prefer when the room is tidy, data with data, parity with parity. But in practice bitflips can happen anywhere in the data or in the so called parity area, it's the same pool of bits for the algorithm. It does not matter where bitflips are as long as the threshold is not exceeded. If I remember well, in the hamming software ECC implementation (under lib/), we do not even bother fixing the bitflips in the parity area. Thanks, Miquèl ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-16 17:06 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-05 4:01 [PATCH 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Guanglei Zhu 2026-11-05 4:01 ` [PATCH 2/2] mtd: rawnand: ingenic: validate BCH error index in jz4780_correct() Guanglei Zhu 2026-11-05 4:24 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Guanglei Zhu 2026-11-05 4:24 ` [PATCH v2 2/2] mtd: rawnand: ingenic: validate BCH error index in jz4780_correct() Guanglei Zhu 2026-11-06 14:37 ` [PATCH v2 1/2] mtd: rawnand: ingenic: validate BCH error index in jz4725b_correct() Paul Cercueil 2026-11-16 17:06 ` Miquel Raynal
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox