[PATCH] nftl_format: prevent unsigned underflow in INFTL virtual unit calculation

Anton Moryakov ant.v.moryakov at gmail.com
Tue Jan 27 04:31:00 PST 2026


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 at 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




More information about the linux-mtd mailing list