From: Anton Moryakov <ant.v.moryakov@gmail.com>
To: linux-mtd@lists.infradead.org
Cc: David Oberhollenzer <david.oberhollenzer@sigma-star.at>,
Anton Moryakov <ant.v.moryakov@gmail.com>
Subject: [PATCH] nftl_format: prevent buffer overflow in BadUnitTable access
Date: Tue, 27 Jan 2026 16:16:09 +0300 [thread overview]
Message-ID: <20260127131609.53932-1-ant.v.moryakov@gmail.com> (raw)
In the INFTL formatting path, the code iterates over erase blocks from
`pezstart` to `maxzones` (total blocks on device) and accesses
`BadUnitTable[ezone]`. However, `BadUnitTable` is a fixed-size array of
`MAX_ERASE_ZONES` elements (typically 1024 or 4096).
If the MTD device has more erase blocks than `MAX_ERASE_ZONES`,
`ezone` can exceed the array bounds, causing a buffer overflow.
Fix by limiting the loop upper bound to `MIN(maxzones, MAX_ERASE_ZONES)`.
Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
nand-utils/nftl_format.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/nand-utils/nftl_format.c b/nand-utils/nftl_format.c
index c8b8b50..0d38ddd 100644
--- a/nand-utils/nftl_format.c
+++ b/nand-utils/nftl_format.c
@@ -372,7 +372,8 @@ int main(int argc, char **argv)
pezstart = startofs / meminfo.erasesize + 1;
pezend = startofs / meminfo.erasesize + ezones - 1;
numvunits = (ezones - 2) * PERCENTUSED / 100;
- for (ezone = pezstart; ezone < maxzones; ezone++) {
+ unsigned long max_iter = (maxzones < MAX_ERASE_ZONES) ? maxzones : MAX_ERASE_ZONES;
+ for (ezone = pezstart; ezone < max_iter; ezone++) {
if (BadUnitTable[ezone] != ZONE_GOOD) {
if (numvunits > 1)
numvunits--;
--
2.39.2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reply other threads:[~2026-01-27 13:16 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260127131609.53932-1-ant.v.moryakov@gmail.com \
--to=ant.v.moryakov@gmail.com \
--cc=david.oberhollenzer@sigma-star.at \
--cc=linux-mtd@lists.infradead.org \
/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