mm: do_wp_page recheck PageKsm after obtaining the page_lock, pte_same not enough
Subsystems (2)
Hi Hugh,
what do you think about this?
===
Subject: mm: do_wp_page recheck PageKsm after obtaining the page_lock, pte_same not enough
From: Andrea Arcangeli <[email protected]>
There seems to be a bug in do_wp_page that if not fixed, it would lead to a Ksm shared page to be mapped read-write into some process pte leading to random memory corruption in userland MADV_MEARGEABLE vmas.
If the orig_pte value was read by do_wp_page after write_protect_page() (likely as if the pte wasn't originally read as readonly by handle_pte_fault, do_wp_page wouldn't be called in the first place), but if we reach lock_page() in the !PageKsm path (before reuse_swap_page is called), but before set_page_stable_node() run (the kpage == NULL case), the orig_pte wouldn't have changed (after write_protect_page returned the pte doesn't change anymore and then we release the page lock), and the pte_same() check would succeed, but the old_page would have become a PageKsm already before releasing the page lock in try_to_merge_one_page, so we shouldn't go ahead with reuse_swap_page in do_wp_page in that case. But we do, and then we reuse the wrprotected PageKsm in the stable tree allowing userland to map it read-write. The PageKsm check I introduced below in memory.c should close this race, it is enough to check the page is not Ksm to know if we can takeover it or not after we obtain the page lock.
To say it in another way, the current and only PageKsm check in do_wp_page in short is racy because it's run before trying to obtain the page lock, so it could run before set_page_stable_node() had a chance to run yet.
---
mm/memory.c +2 -1
@@ -2454,7 +2454,8 @@ static int do_wp_page(struct mm_struct *lock_page (old_page); page_table =pte_offset_map_lock (mm, pmd, address, &ptl); -if (!pte_same (*page_table, orig_pte)) { +if (!pte_same (*page_table, orig_pte) || +PageKsm (old_page)) {unlock_page (old_page); goto unlock; }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 43 lines · Andrea Arcangeli
Hi Hugh,
what do you think about this?
===
Subject: mm:do_wp_pagerecheck PageKsm after obtaining thepage_lock,pte_samenot enoughFrom: Andrea Arcangeli <[email protected]>
There seems to be a bug in
do_wp_pagethat if not fixed, it would lead to a Ksm shared page to be mapped read-write into some process pte leading
to random memory corruption in userlandMADV_MEARGEABLEvmas.If the
orig_ptevalue was read bydo_wp_pageafterwrite_protect_page()(likely as if the pte wasn't originally read as readonly byhandle_pte_fault,do_wp_pagewouldn't be called in the first place), but if we reachlock_page()in the !PageKsm path (beforereuse_swap_pageis called), but beforeset_page_stable_node()run (the kpage == NULL case), theorig_ptewouldn't have changed (afterwrite_protect_pagereturned the pte doesn't change anymore and then we release the page lock), and thepte_same()check would succeed, but theold_pagewould have become a PageKsm already before releasing the page lock intry_to_merge_one_page, so we shouldn't go ahead withreuse_swap_pageindo_wp_pagein that case. But we do, and then we reuse the wrprotected PageKsm in the stable tree allowing userland to map it read-write. The PageKsm check I introduced below inmemory.cshould close this race, it is enough to check the page is not Ksm to know if we can takeover it or not after we obtain the page lock.To say it in another way, the current and only PageKsm check in
do_wp_pagein short is racy because it's run before trying to obtain the page lock, so it could run beforeset_page_stable_node()had a chance to run yet.Signed-off-by Andrea Arcangeli <[email protected]>---
mm/memory.c static int do_wp_page(struct mm_struct * · line 2458
Quoted 6 lines
mm/memory.c +2
-1@@ -2454,7 +2454,8 @@ static int do_wp_page(struct mm_struct *lock_page (old_page); page_table =pte_offset_map_lock (mm, pmd, address, &ptl); -if (!pte_same (*page_table, orig_pte)) { +if (!pte_same (*page_table, orig_pte) || +PageKsm (old_page)) {
I think in this case we should copy the page instead of going to unlock.
And I think reuse_swap_page() has checked the PageKsm(page) inside and in this case it will go to the copy path already?
Quoted 5 lines
mm/memory.c +0
-1unlock_page (old_page); goto unlock; } --
Quoted 7 lines
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign
http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 3 lines · Andrea Arcangeli
Hi Hugh,
what do you think about this?
Ah I just noticed it couldn't happen so you can ignore the previous patch... a second check is indeed inside reuse_swap_page.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 4 lines · Nai Xia
I think in this case we should copy the page instead of going to unlock.
And I think
reuse_swap_page()has checked the PageKsm(page) inside and in this case it will go to the copy path already?
yes this is why it's unnecessary, I've been a bit in a paranoid mode on this code lately, one more check wouldn't have hurted but it's definitely unnecessary so please ignore...
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>