Skip to contentSkip to conversation

mtd-utils: Check mtdoffset is not larger than mtd.size in case of a bad block.

PATCH Applied Tomer Barletz 2 participants 2 messages Jun 26, 2012 – Jun 29, 2012 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd-utils: Check mtdoffset is not larger than mtd.size in case of a bad block.

mtdoffset is being tested against mtd.size in the outer two loops, but the third nested one does not test against it. In case of a bad block we'll try to access an out of bounds offset in the next MEMGETBADBLOCK ioctl, which will fail with EINVAL. In case mtdoffset is indeed larger than the partition size, we need to bail, since there are not enough "good" blocks to complete the write.

---
 nandwrite.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
nandwrite.c +5 -0
index a42f7c9..8bd00c1 100644
@@ -399,6 +399,11 @@ int main(int argc, char * const argv[])
 
 				if (baderaseblock) {
 					mtdoffset = blockstart + ebsize_aligned;
+
+					if (mtdoffset > mtd.size) {
+						perror("Too many bad blocks - cannot complete request.");
+						goto closeall;
+					}
 				}
 				offs +=  ebsize_aligned / blockalign;
 			} while (offs < blockstart + ebsize_aligned);
Signature · 4 lines
1.7.1


--Tomer
#1 lore raw
Quoted 8 lines · Tomer Barletz

mtdoffset is being tested against mtd.size in the outer two loops, but the third nested one does not test against it.
In case of a bad block we'll try to access an out of bounds offset in the next MEMGETBADBLOCK ioctl, which will fail with EINVAL. In case mtdoffset is indeed larger than the partition size, we need to bail, since there are not enough "good" blocks to complete the write.

Pushed to mtd-utils, thanks!

Signature · 2 lines
Best Regards,
Artem Bityutskiy