Skip to contentSkip to conversation

mm: Fix possible off-by-one in walk_pte_range()

PATCH Superseded Johannes Weiner 3 participants 5 messages Apr 15, 2008 – Apr 16, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: Fix possible off-by-one in walk_pte_range()

After the loop in walk_pte_range() pte might point to the first address after the pmd it walks. The pte_unmap() is then applied to something bad.

Spotted by Roel Kluin and Andreas Schwab.

CC Roel Kluin <[email protected]>
CC Andreas Schwab <[email protected]>
CC Matt Mackall <[email protected]>
CC Andrew Morton <[email protected]>

---

A bug is unlikely, though. kunmap_atomic() looks up the kmap entry by map-type instead of the address the pte points. So the worst thing I could find with a quick grep was that a wrong TLB entry is being flushed. Still, the code is wrong :)

mm/pagewalk.c +1 -1
index 1cf1417..cf3c004 100644
@@ -13,7 +13,7 @@ static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 		err = walk->pte_entry(pte, addr, addr + PAGE_SIZE, private);
 		if (err)
 		       break;
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (addr += PAGE_SIZE, addr != end && pte++);
 
 	pte_unmap(pte);
 	return err;
#1 lore raw
Quoted 23 lines · Johannes Weiner

After the loop in walk_pte_range() pte might point to the first address after the pmd it walks. The pte_unmap() is then applied to something bad.

Spotted by Roel Kluin and Andreas Schwab.

CC Roel Kluin <[email protected]>
CC Andreas Schwab <[email protected]>
CC Matt Mackall <[email protected]>
CC Andrew Morton <[email protected]>

---

A bug is unlikely, though. kunmap_atomic() looks up the kmap entry by map-type instead of the address the pte points. So the worst thing I could find with a quick grep was that a wrong TLB entry is being flushed. Still, the code is wrong :)

mm/pagewalk.c +0 -0
index 1cf1417..cf3c004 100644

mm/pagewalk.c static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 16

Quoted 5 lines
mm/pagewalk.c +1 -1
@@ -13,7 +13,7 @@ static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 		err = walk->pte_entry(pte, addr, addr + PAGE_SIZE, private);
 		if (err)
 		       break;
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (addr += PAGE_SIZE, addr != end && pte++);

Instead of obfuscating the code by putting "&& pte++" in the condition (it will always be true in valid C), you should IMO rewrite the do-while as a for loop + break, like this:

	for (;;) {
	    // same body as before
	    addr += PAGE_SIZE;
	    if (addr == end)
	        break;
	    pte++;
	}

This makes the ordering constraints very clear.

#2 lore raw

PATCH v2 1/1 mm: Fix possible off-by-one in walk_pte_range()

Hi,

Quoted 34 lines · Mikael Pettersson
Quoted 23 lines · Johannes Weiner

After the loop in walk_pte_range() pte might point to the first address after the pmd it walks. The pte_unmap() is then applied to something bad.

Spotted by Roel Kluin and Andreas Schwab.

CC Roel Kluin <[email protected]>
CC Andreas Schwab <[email protected]>
CC Matt Mackall <[email protected]>
CC Andrew Morton <[email protected]>

---

A bug is unlikely, though. kunmap_atomic() looks up the kmap entry by map-type instead of the address the pte points. So the worst thing I could find with a quick grep was that a wrong TLB entry is being flushed. Still, the code is wrong :)

mm/pagewalk.c +0 -0
index 1cf1417..cf3c004 100644

mm/pagewalk.c static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 16

Quoted 5 lines
mm/pagewalk.c +1 -1
@@ -13,7 +13,7 @@ static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 		err = walk->pte_entry(pte, addr, addr + PAGE_SIZE, private);
 		if (err)
 		       break;
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (addr += PAGE_SIZE, addr != end && pte++);

Instead of obfuscating the code by putting "&& pte++" in the condition (it will always be true in valid C), you should IMO rewrite the do-while as a for loop + break, like this:

Quoted 7 lines
	for (;;) {
	    // same body as before
	    addr += PAGE_SIZE;
	    if (addr == end)
	        break;
	    pte++;
	}

Sorry, I think too lispy :)

Hannes
---

From: Johannes Weiner <[email protected]>
Subject: [PATCH] mm: Fix possible off-by-one in walk_pte_range()

After the loop in walk_pte_range() pte might point to the first address after the pmd it walks. The pte_unmap() is then applied to something bad.

Spotted by Roel Kluin and Andreas Schwab.

CC Roel Kluin <[email protected]>
CC Andreas Schwab <[email protected]>
CC Matt Mackall <[email protected]>
CC Andrew Morton <[email protected]>

---

A bug is unlikely, though. kunmap_atomic() looks up the kmap entry by map-type instead of the address the pte points. So the worst thing I could find with a quick grep was that a wrong TLB entry is being flushed. Still, the code is wrong :)

mm/pagewalk.c +6 -2
index 1cf1417..0afd238 100644
@@ -9,11 +9,15 @@ static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	int err = 0;
 
 	pte = pte_offset_map(pmd, addr);
-	do {
+	for (;;) {
 		err = walk->pte_entry(pte, addr, addr + PAGE_SIZE, private);
 		if (err)
 		       break;
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+		addr += PAGE_SIZE;
+		if (addr == end)
+			break;
+		pte++;
+	}
 
 	pte_unmap(pte);
 	return err;
Acked-by on v2 1/1
#3 lore raw
Quoted 10 lines · Johannes Weiner

From: Johannes Weiner <[email protected]>
Subject: [PATCH] mm: Fix possible off-by-one in walk_pte_range()

After the loop in walk_pte_range() pte might point to the first address after the pmd it walks. The pte_unmap() is then applied to something bad.

Spotted by Roel Kluin and Andreas Schwab.

Signature · 1 line
Mathematics is the supreme nostalgia of our time.
Acked-by on v2 1/1
#4 lore raw
In reply to Johannes Weiner
Quoted 7 lines · Johannes Weiner

From: Johannes Weiner <[email protected]>
Subject: [PATCH] mm: Fix possible off-by-one in walk_pte_range()

After the loop in walk_pte_range() pte might point to the first address after the pmd it walks. The pte_unmap() is then applied to something bad.

Quoted 7 lines

Spotted by Roel Kluin and Andreas Schwab.

CC Roel Kluin <[email protected]>
CC Andreas Schwab <[email protected]>
CC Matt Mackall <[email protected]>
CC Andrew Morton <[email protected]>

Thanks, v2 looks much nicer.