public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Pierre Bourdon <delroth@gmail.com>
To: u-boot@lists.denx.de
Cc: Pierre Bourdon <delroth@gmail.com>,
	Dario Binacchi <dario.binacchi@amarulasolutions.com>,
	Jagan Teki <jagan@openedev.com>,
	Michael Trimarchi <michael@amarulasolutions.com>,
	Miquel Raynal <miquel.raynal@bootlin.com>
Subject: [PATCH] mtd: nand: pxa3xx: Fix buffer overflow during raw reads
Date: Sat, 29 Jul 2023 20:07:18 +0200	[thread overview]
Message-ID: <20230729180732.4056248-1-delroth@gmail.com> (raw)

Chunked raw reads get accumulated to the data buffer, but in some
ECC configurations they can end up being larger than the originally
computed size (write page size + OOB size). For example:

4K page size, ECC strength 8:
- Normal reads: writesize (4096B) + oobsize (128B) = 4224 bytes.
- Chunked raw reads: 4 chunks of 1024B + 1 final spare area of 64B + 5
  ECC areas of 32B = 4320B.

Fixes: 6293b0361d9 ("mtd: nand: pxa3xx: add raw read support")
Signed-off-by: Pierre Bourdon <delroth@gmail.com>
---

 drivers/mtd/nand/raw/pxa3xx_nand.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/pxa3xx_nand.c b/drivers/mtd/nand/raw/pxa3xx_nand.c
index d502e967f9..2894ababbe 100644
--- a/drivers/mtd/nand/raw/pxa3xx_nand.c
+++ b/drivers/mtd/nand/raw/pxa3xx_nand.c
@@ -1471,6 +1471,19 @@ static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
 
 static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
 {
+	unsigned int chunk_size;
+	unsigned int last_chunk_size;
+
+	/*
+	 * The data buffer needs to not only be large enough for normal + OOB
+	 * reads, but also for raw reads. The raw reads can end up taking more
+	 * space due to the chunking scheme.
+	 */
+	chunk_size = info->chunk_size + info->spare_size + info->ecc_size;
+	last_chunk_size =
+		info->last_chunk_size + info->last_spare_size + info->ecc_size;
+	info->buf_size = info->nfullchunks * chunk_size + last_chunk_size;
+
 	info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
 	if (info->data_buff == NULL)
 		return -ENOMEM;
@@ -1661,7 +1674,6 @@ static int pxa3xx_nand_scan(struct mtd_info *mtd)
 	kfree(info->data_buff);
 
 	/* allocate the real data + oob buffer */
-	info->buf_size = mtd->writesize + mtd->oobsize;
 	ret = pxa3xx_nand_init_buff(info);
 	if (ret)
 		return ret;
-- 
2.41.0


             reply	other threads:[~2023-07-29 18:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-29 18:07 Pierre Bourdon [this message]
2023-07-30 21:21 ` [PATCH] mtd: nand: pxa3xx: Fix buffer overflow during raw reads Chris Packham
2023-07-30 21:28   ` Michael Nazzareno Trimarchi
2023-07-30 21:29   ` Pierre Bourdon
2023-07-31  4:32     ` Chris Packham
2023-07-31  5:26     ` Chris Packham

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230729180732.4056248-1-delroth@gmail.com \
    --to=delroth@gmail.com \
    --cc=dario.binacchi@amarulasolutions.com \
    --cc=jagan@openedev.com \
    --cc=michael@amarulasolutions.com \
    --cc=miquel.raynal@bootlin.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox