Skip to contentSkip to conversation

nftl_format: prevent unsigned underflow in INFTL virtual unit calculation

PATCH In review Anton Moryakov 2 participants 2 messages Jan 27 – Apr 13 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nftl_format: prevent unsigned underflow in INFTL virtual unit calculation

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.

---
 nand-utils/nftl_format.c | 2 ++
 1 file changed, 2 insertions(+)
nand-utils/nftl_format.c +2 -0
index c8b8b50..3667be3 100644
@@ -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) {
Signature · 6 lines
2.39.2


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

Hi,

nand-utils/nftl_format.c int main(int argc, char **argv) · line 378

Quoted 9 lines · Anton Moryakov
nand-utils/nftl_format.c +2 -0
@@ -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) {

I think at the very least we should produce some sort of error message if this is triggered. Maybe a simple assert(ezones >= 2) should be preferred here?

Greetings,

David

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