The patch titled Subject: mm: change page type prior to adding page table entry has been added to the -mm tree. Its filename is mm-change-page-type-prior-to-adding-page-table-entry.patch This patch should soon appear at https://ozlabs.org/~akpm/mmots/broken-out/mm-change-page-type-prior-to-adding-page-table-entry.patch and later at https://ozlabs.org/~akpm/mmotm/broken-out/mm-change-page-type-prior-to-adding-page-table-entry.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx> Subject: mm: change page type prior to adding page table entry Patch series "page table check", v2. Ensure that some memory corruptions are prevented by checking at the time of insertion of entries into user page tables that there is no illegal sharing. We have recently found a problem [1] that existed in kernel since 4.14. The problem was caused by broken page ref count and led to memory leaking from one process into another. The problem was accidentally detected by studying a dump of one process and noticing that one page contains memory that should not belong to this process. There are some other page->_refcount related problems that were recently fixed: [2], [3] which potentially could also lead to illegal sharing. In addition to hardening refcount [4] itself, this work is an attempt to prevent this class of memory corruption issues. It uses a simple state machine that is independent from regular MM logic to check for illegal sharing at time pages are inserted and removed from page tables. [1] https://lore.kernel.org/all/xr9335nxwc5y.fsf@xxxxxxxxxxxxxxxxxxxxxxxxxxxx [2] https://lore.kernel.org/all/1582661774-30925-2-git-send-email-akaher@xxxxxxxxxx [3] https://lore.kernel.org/all/20210622021423.154662-3-mike.kravetz@xxxxxxxxxx [4] https://lore.kernel.org/all/20211026173822.502506-1-pasha.tatashin@xxxxxxxxxx This patch (of 4): In do_swap_page() we first insert the entry into user page table, and later change the struct page to indicate that this is anonymous page. In all other places we first configure the page metadata and then insert entries into the page table. While odd, the behaviour is benign. Page table check, however, will use the information from struct page to verify the type of entry is inserted. Change the order in do_swap_page() to first update struct page, and later insert pte entry into the page table. Link: https://lkml.kernel.org/r/20211204182314.1470076-1-pasha.tatashin@xxxxxxxxxx Link: https://lkml.kernel.org/r/20211204182314.1470076-2-pasha.tatashin@xxxxxxxxxx Signed-off-by: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx> Cc: David Rientjes <rientjes@xxxxxxxxxx> Cc: Paul Turner <pjt@xxxxxxxxxx> Cc: Wei Xu <weixugc@xxxxxxxxxx> Cc: Greg Thelen <gthelen@xxxxxxxxxx> Cc: Ingo Molnar <mingo@xxxxxxxxxx> Cc: Jonathan Corbet <corbet@xxxxxxx> Cc: Will Deacon <will@xxxxxxxxxx> Cc: Mike Rapoport <rppt@xxxxxxxxxx> Cc: Kees Cook <keescook@xxxxxxxxxxxx> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Cc: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx> Cc: Masahiro Yamada <masahiroy@xxxxxxxxxx> Cc: Sami Tolvanen <samitolvanen@xxxxxxxxxx> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx> Cc: Frederic Weisbecker <frederic@xxxxxxxxxx> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx> Cc: Aneesh Kumar K.V <aneesh.kumar@xxxxxxxxxxxxx> Cc: Jiri Slaby <jirislaby@xxxxxxxxxx> Cc: Muchun Song <songmuchun@xxxxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/memory.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) --- a/mm/memory.c~mm-change-page-type-prior-to-adding-page-table-entry +++ a/mm/memory.c @@ -3639,9 +3639,6 @@ vm_fault_t do_swap_page(struct vm_fault pte = pte_mkuffd_wp(pte); pte = pte_wrprotect(pte); } - set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); - arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); - vmf->orig_pte = pte; /* ksm created a completely new copy */ if (unlikely(page != swapcache && swapcache)) { @@ -3651,6 +3648,10 @@ vm_fault_t do_swap_page(struct vm_fault do_page_add_anon_rmap(page, vma, vmf->address, exclusive); } + set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); + arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); + vmf->orig_pte = pte; + swap_free(entry); if (mem_cgroup_swap_full(page) || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) _ Patches currently in -mm which might be from pasha.tatashin@xxxxxxxxxx are mm-change-page-type-prior-to-adding-page-table-entry.patch mm-ptep_clear-page-table-helper.patch mm-page-table-check.patch x86-mm-add-x86_64-support-for-page-table-check.patch