REDHAT-BUG-2577753: High severity Linux Linux kernel vulnerability
In the Linux kernel, the following vulnerability has been resolved:
mm/userfaultfd: only allocate anonvma in CoW mappings
findvmaandprepareanon() can end up allocating an anonvma in VMAs that are not CoW mappings, which is wrong.
If we have a MAPSHARED R/O file mapped, we lack VMSHARED (but do have VMMAYSHARE) and lack VMMAYWRITE.
While uffd later rejects such mappings (uffd registration requires VMMAYWRITE, see comment in validatedstvma) findvmaandprepareanon() still wrongly allocates an anon vma before it validates the vma.
This in turn later confuses copypagerange()->vmaneedscopy() such that we end up calling copyhugetlbpagerange() with ranges that actually do not contain any per-pagetable information that would require copying, which is the basis for PMD-table sharing in the first place.
copyhugetlbpagerange() should not have to worry about shared PMD tables at all: it doesn't make any sense to copy page tables that are shared, as they can be fully recreated later. Ending up in the function with shareable page tables is the real issue.
If a destination PMD table is shared in copyhugetlbpagerange(), it might get concurrently unshared as we are not holding relevant locks, resulting in us using a page table that is no longer mapped and possibly freed after calling:
dstpte = hugeptealloc(dst, dstvma, addr, sz);
To fix it, do not allocate an anonvma in non-CoW mappings and add a safety guard to copyhugetlbpagerange(), making sure that we never end up messing with shared PMD tables in there.
Before commit 867a43a34ff8a ("userfaultfd: use per-vma locks in userfaultfd operations") we would always validate the VMA before allocating an anonvma, and consequently never wrongly allocate an anonvma.
As uffdlockvma() is part of the same lookup machinery and also contains a misleading VMSHARED check, change that one as well directly for consistency.
A follow-up cleanup will stop taking the hugetlbvmalockread(srcvma) lock in copyhugetlbpagerange() entirely. Further, the usage of VMSHARED in userfaultfd code should be cleaned up later to focus on CoW vs. non-CoW mappings.
Affected Software
Event History
Frequently Asked Questions
Are read-only shared file mappings relevant to this issue?
Yes. The described problematic case is a MAP_SHARED read-only file mapping that has VM_MAYSHARE but lacks VM_SHARED and VM_MAYWRITE.
Can the affected mapping be registered with userfaultfd?
No. Later userfaultfd validation rejects these mappings because registration requires VM_MAYWRITE. However, an anon_vma may already have been allocated before that validation occurs.
What makes the condition unsafe after the incorrect allocation?
The allocation can cause copy_page_range() to treat the VMA as needing copying and invoke copy_hugetlb_page_range() for shareable PMD tables. A destination PMD table may then be concurrently unshared while that function is operating on it.