Skip to contentSkip to conversation

mtd: fix read buffer overflow

PATCH In review [email protected] 2 participants 3 messages Aug 6, 2009 – Aug 10, 2009 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 7/8 mtd: fix read buffer overflow

From: Roel Kluin <[email protected]>

Check whether index is within bounds before testing the element.

with `for (i = 0; bbt[i] && i < ebcnt; ++i)'
we test bbt[ebcnt] in the last iteration

with `for (i = 0; bbt[ebcnt - i - 1] && i < ebcnt; ++i)' we test bbt[-1] in the last iteration

---

 drivers/mtd/tests/mtd_oobtest.c  |    2 +-
 drivers/mtd/tests/mtd_pagetest.c |   12 ++++++------
 2 files changed, 7 insertions(+), 7 deletions(-)
drivers/mtd/tests/mtd_oobtest.c renamed from drivers/mtd/tests/mtd_oobtest.c~mtd-fix-read-buffer-overflow +1 -1
diff -puN drivers/mtd/tests/mtd_oobtest.c~mtd-fix-read-buffer-overflow drivers/mtd/tests/mtd_oobtest.c
@@ -512,7 +512,7 @@ static int __init mtd_oobtest_init(void)
 		goto out;
 
 	addr0 = 0;
-	for (i = 0; bbt[i] && i < ebcnt; ++i)
+	for (i = 0; i < ebcnt && bbt[i]; ++i)
 		addr0 += mtd->erasesize;
 
 	/* Attempt to write off end of OOB */
drivers/mtd/tests/mtd_pagetest.c renamed from drivers/mtd/tests/mtd_pagetest.c~mtd-fix-read-buffer-overflow +6 -6
diff -puN drivers/mtd/tests/mtd_pagetest.c~mtd-fix-read-buffer-overflow drivers/mtd/tests/mtd_pagetest.c
@@ -116,11 +116,11 @@ static int verify_eraseblock(int ebnum)
 	loff_t addr = ebnum * mtd->erasesize;
 
 	addr0 = 0;
-	for (i = 0; bbt[i] && i < ebcnt; ++i)
+	for (i = 0; i < ebcnt && bbt[i]; ++i)
 		addr0 += mtd->erasesize;
 
 	addrn = mtd->size;
-	for (i = 0; bbt[ebcnt - i - 1] && i < ebcnt; ++i)
+	for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
 		addrn -= mtd->erasesize;
 
 	set_random_data(writebuf, mtd->erasesize);
@@ -219,11 +219,11 @@ static int crosstest(void)
 	memset(pp1, 0, pgsize * 4);
 
 	addr0 = 0;
-	for (i = 0; bbt[i] && i < ebcnt; ++i)
+	for (i = 0; i < ebcnt && bbt[i]; ++i)
 		addr0 += mtd->erasesize;
 
 	addrn = mtd->size;
-	for (i = 0; bbt[ebcnt - i - 1] && i < ebcnt; ++i)
+	for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
 		addrn -= mtd->erasesize;
 
 	/* Read 2nd-to-last page to pp1 */
@@ -317,7 +317,7 @@ static int erasecrosstest(void)
 
 	ebnum = 0;
 	addr0 = 0;
-	for (i = 0; bbt[i] && i < ebcnt; ++i) {
+	for (i = 0; i < ebcnt && bbt[i]; ++i) {
 		addr0 += mtd->erasesize;
 		ebnum += 1;
 	}
@@ -413,7 +413,7 @@ static int erasetest(void)
 
 	ebnum = 0;
 	addr0 = 0;
-	for (i = 0; bbt[i] && i < ebcnt; ++i) {
+	for (i = 0; i < ebcnt && bbt[i]; ++i) {
 		addr0 += mtd->erasesize;
 		ebnum += 1;
 	}

_

#1 lore raw
Quoted 7 lines · On Thu, 2009-08-06 at 16:05 -0700, [email protected] wrote:

From: Roel Kluin <[email protected]>

Check whether index is within bounds before testing the element.

with `for (i = 0; bbt[i] && i < ebcnt; ++i)'
we test bbt[ebcnt] in the last iteration

Quoted 6 lines

with `for (i = 0; bbt[ebcnt - i - 1] && i < ebcnt; ++i)' we test bbt[-1] in the last iteration

This one is in my l2-mtd-2.6.git as well now.

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
#2 lore raw
In reply to [email protected]
Quoted 7 lines · On Thu, 2009-08-06 at 16:05 -0700, [email protected] wrote:

From: Roel Kluin <[email protected]>

Check whether index is within bounds before testing the element.

with `for (i = 0; bbt[i] && i < ebcnt; ++i)'
we test bbt[ebcnt] in the last iteration

Quoted 6 lines

with `for (i = 0; bbt[ebcnt - i - 1] && i < ebcnt; ++i)' we test bbt[-1] in the last iteration

This one is in my l2-mtd-2.6.git as well now.

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)