public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
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 unsigned underflow in INFTL virtual unit calculation
Date: Tue, 27 Jan 2026 15:31:00 +0300	[thread overview]
Message-ID: <20260127123100.47486-1-ant.v.moryakov@gmail.com> (raw)

When formatting an INFTL partition, the code computes:
    numvunits = (ezones - 2) * PERCENTUSED / 100;
If ezones < 2 (e.g. due to user specifying start offset >= device size),
this causes an unsigned underflow, leading to a huge and invalid numvunits.

Add a guard to ensure ezones >= 2 before subtraction. If not, abort with
an error message, as a valid INFTL partition requires at least two erase
blocks for media headers.

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
 nand-utils/nftl_format.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/nand-utils/nftl_format.c b/nand-utils/nftl_format.c
index c8b8b50..3667be3 100644
--- a/nand-utils/nftl_format.c
+++ b/nand-utils/nftl_format.c
@@ -371,6 +371,8 @@ int main(int argc, char **argv)
 		maxzones = meminfo.size / meminfo.erasesize;
 		pezstart = startofs / meminfo.erasesize + 1;
 		pezend = startofs / meminfo.erasesize + ezones - 1;
+		if (ezones < 2)
+   			exit(EXIT_FAILURE);
 		numvunits = (ezones - 2) * PERCENTUSED / 100;
 		for (ezone = pezstart; ezone < maxzones; ezone++) {
 			if (BadUnitTable[ezone] != ZONE_GOOD) {
-- 
2.39.2


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

                 reply	other threads:[~2026-01-27 12:31 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=20260127123100.47486-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