[PATCH 2/2] mm/hugetlb: Fix cow where page writtable in child

From: Peter Xu
Date: Sat May 01 2021 - 10:42:37 EST


When fork() and copy hugetlb page range, we'll remember to wrprotect src pte if
needed, however we forget about the child! Without it, the child will be able
to write to parent's pages when mapped as PROT_READ|PROT_WRITE and MAP_PRIVATE,
which will cause data corruption in the parent process.

This issue can also be exposed by "memfd_test hugetlbfs" kselftest (if it can
pass the F_SEAL_FUTURE_WRITE test first, though).

Signed-off-by: Peter Xu <peterx@xxxxxxxxxx>
---
mm/hugetlb.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 629aa4c2259c8..9978fb73b8caf 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4056,6 +4056,8 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
* See Documentation/vm/mmu_notifier.rst
*/
huge_ptep_set_wrprotect(src, addr, src_pte);
+ /* Child cannot write too! */
+ entry = huge_pte_wrprotect(entry);
}

page_dup_rmap(ptepage, true);
--
2.31.1