Skip to contentSkip to conversation

mm: uffd fault-around mask omits MISSING; PMD install ungated

SECURITY n3rd 1 participant 1 message Nov 10 Memory management

lore
Share & tools
lore

n3rd

author
#0 lore raw

Hello,

I am reporting a security bug in the Linux kernel.

1. Affected kernel versions
Linux v7.3-rc7 (mainline, built and tested as 7.3.0-rc7) is affected and was reproduced on two fresh boots. v7.3-rc6 carries the same code and is affected as well. v7.2.9 (stable) is affected too: its uffd_disable_fault_around() mask is smaller, only WP and MINOR, but MISSING is absent from the mask in v7.2.9 just as it is in v7.3-rc7, and the do_set_pmd() call in finish_fault() already precedes the userfaultfd_armed() check there. Triggering additionally requires CONFIG_TRANSPARENT_HUGEPAGE, without which do_set_pmd() is compiled to the fallback stub and no PMD can be installed; the RWP scenario needs CONFIG_USERFAULTFD_RWP and the MINOR scenario needs CONFIG_HAVE_ARCH_USERFAULTFD_MINOR. There is no upstream fix as of v7.3-rc7 and no CVE has been assigned.

2. Problem description
The mask in uffd_disable_fault_around() in include/linux/userfaultfd_k.h, at line 200 in v7.3-rc7, is VMA_UFFD_WP, VMA_UFFD_RWP and VMA_UFFD_MINOR. MISSING is the one uffd mode that is not in it. The comment above the predicate gives a separate reason for each of the three modes it does cover and says nothing at all about MISSING, while the flag set defined at line 27 of the same header, the full registered set, is MISSING, WP, MINOR and RWP. The mask has exactly one consumer, should_fault_around() in mm/memory.c at line 5967, whose only caller is do_read_fault() at line 5990, so the mask gates the fault-around path only, meaning do_fault_around, filemap_map_pages and filemap_map_pmd. The other path on the same read fault chain is not covered by the mask at all. In finish_fault() in mm/memory.c, after the pmd_none test at line 5806, line 5808 calls do_set_pmd() to install a whole 2MB PMD and returns immediately whenever the result is not VM_FAULT_FALLBACK. The comment stating that per-page faults maintain the uffd semantics sits at line 5821 and the userfaultfd_armed() check sits at line 5822, both after that call, so for the PMD case the check is dead code. do_set_pmd() itself, starting at line 5608, contains no userfaultfd check at all; searching for uffd inside its body yields zero hits. Because userfaultfd_armed() tests the full four-mode set, this ordering defect is not specific to MISSING: WP and RWP, the modes the mask does protect, are bypassed by it as well. Impact. For a shmem range, memfd or tmpfs, whose registration does not contain MINOR, and whose page cache already holds an order-9 folio, which an unprivileged madvise(MADV_COLLAPSE) can create, a single 4KB read touch silently turns the whole 2MB into a PMD mapping. Seven registrations were measured. MISSING alone, MISSING with WP, MISSING with RWP and WP alone all end with pagemap present 512 out of 512, ShmemPmdMapped up by 2048 kB, zero uffd events and UFFDIO_ZEROPAGE returning minus one with errno 17 EEXIST. The MISSING with WP case is the decisive one: WP is in the mask, so fault-around is already disabled for that VMA, and the PMD is still installed, which attributes the violation to the ordering defect in finish_fault() rather than to the missing mask bit. The only structurally safe combination is a registration that contains MINOR, because mm/shmem.c line 2456 checks userfaultfd_minor() and delivers a minor event before the folio is returned, so do_set_pmd() is never reached; measured, that combination delivers the minor event, resolves per page through UFFDIO_CONTINUE, leaves present at one page and installs no PMD. A PMD that would cross into a neighbouring differently registered VMA is rejected by thp_vma_suitable_order and the neighbouring WP VMA keeps present at zero, so there is no cross VMA bleed. To be precise about what is not happening: no event that would otherwise be delivered is suppressed. On shmem a MISSING event is only delivered for a real hole, a page cache hit is mapped silently by design, which is exactly why MINOR exists. The defect is therefore about granularity and page table shape, and about a security gate that does not gate what it appears to gate, not about a lost event. There is also an in tree counter example: collapse_file() in mm/khugepaged.c at line 2571 does respect MISSING and rolls back rather than silently populating such a page. The issue gives no kernel memory corruption, no out of bounds access, no use after free, no information leak and no privilege escalation; KASAN, Oops, general protection and WARNING counts were zero across both rounds. Note that shmem_enabled set to never is not a mitigation, because do_set_pmd() bypasses it with forced_collapse=true; the runs reproduced with the knob at never.

3. Reproducer
A single statically linked C program, mm11, built from mm11_mask_matrix.c, is run as root inside a QEMU guest, plus a small guest side script that turns the program's own readings into an explicit verdict. The program walks seven registrations on tmpfs file mappings: MISSING alone, MISSING with WP, MISSING with RWP, MISSING with MINOR, WP alone, and a MISSING VMA next to a WP VMA for the boundary case. For each one it populates a 2MB chunk, issues madvise(MADV_COLLAPSE), registers the chosen mode through UFFDIO_REGISTER, issues madvise(MADV_DONTNEED), touches exactly one 4KB page and then reads back ShmemPmdMapped from /proc/meminfo, the present bits from /proc/self/pagemap and the return value of UFFDIO_ZEROPAGE. Verdict lines are computed at run time, for example the M2 line reporting that ShmemPmdMapped rose by 2048 kB after the mask had already disabled fault-around, the M2 present line reporting 512 out of 512, the M4 line reporting a minor event with flags 0x4 and no PMD, and the M6 line reporting that the cross VMA PMD was rejected and the neighbouring WP VMA stayed at zero present. The summary line is a FAIL count of zero and the marker MM11-MATRIX-AS-PREDICTED. As a control the program is run a second time with fault_around_bytes set to 4096, which makes should_fault_around() return false and removes the fault-around path globally; the M2 result is unchanged, present 512 out of 512 with a 2048 kB increase, which proves the PMD comes from do_set_pmd() in finish_fault() and not from fault-around. A kprobe on do_set_pmd provides the function level attribution. dmesg shows zero KASAN, Oops, general protection and WARNING hits. The complete reproducer, the build commands and the raw console logs are available on request; I am not posting them publicly.

4. Conditions
The target VMA must be registered with any uffd mode combination that does not contain MINOR, that is MISSING alone, MISSING with WP, MISSING with RWP, or WP alone. The page cache must already hold an order-9 folio for that range, which the caller can create with an unprivileged madvise(MADV_COLLAPSE), or which can be left over from a pre-copy phase, or created by another process sharing the same memfd, or produced by a background khugepaged scan. The PTEs must have been dropped while the page cache is retained, which madvise(MADV_DONTNEED), reclaim or migration will do, and then a single 4KB read touch is enough. The sequence is deterministic with no race window. No capability is required: the whole chain is user space syscalls, madvise acts only on the caller own mm, and userfaultfd opened with UFFD_USER_MODE_ONLY is available to unprivileged users even with vm.unprivileged_userfaultfd set to zero, which is how the test guest ran. The attacker is the monitored process itself or code sharing its mm and its memfd, and the victim is the uffd monitor that relies on per-page semantics. The kernel needs CONFIG_TRANSPARENT_HUGEPAGE, CONFIG_USERFAULTFD, CONFIG_USERFAULTFD_RWP, CONFIG_HAVE_ARCH_USERFAULTFD_MINOR, CONFIG_SHMEM and CONFIG_TMPFS, and the test kept CONFIG_TRANSPARENT_HUGEPAGE_SHMEM_HUGE_NEVER so that the initial fill is order-0 and the collapse is a real small page to huge page transition. Tested on x86_64 with QEMU 6.2.0 in TCG mode, two vCPUs, 4096 MB of RAM and an initramfs, with KASAN on as a control.

5. Suspected location
include/linux/userfaultfd_k.h: uffd_disable_fault_around() at line 200, mask VMA_UFFD_WP, VMA_UFFD_RWP, VMA_UFFD_MINOR, without MISSING; the comment at lines 196 to 199; and the full registered flag set at line 27. mm/memory.c: should_fault_around() at line 5967 and its only call site in do_read_fault() at line 5990; the do_set_pmd() call in finish_fault() at line 5808 and the userfaultfd_armed() check at line 5822 with the per-page comment at line 5821; do_set_pmd() starting at line 5608, which has no uffd check. mm/filemap.c: filemap_map_pmd() starting at line 3726 with the ungated do_set_pmd() call at line 3730. mm/shmem.c: the userfaultfd_minor() check at line 2456, which is the only structural interception. mm/khugepaged.c: collapse_file() respecting MISSING at line 2571, the in tree counter example.

6. Suggested fix
Two changes are needed and neither is sufficient alone. First, close the mask: add MISSING to uffd_disable_fault_around() in include/linux/userfaultfd_k.h so that it matches the full registered flag set at line 27 of the same header. Second, fix the ordering: in finish_fault() in mm/memory.c, move the userfaultfd_armed() test ahead of the do_set_pmd() call so that an armed VMA skips the PMD install and takes the per-page path, matching what thp_swapin_suitable_orders() and alloc_anon_folio() already do. Doing only the first is not enough, because the mask covers the fault-around path only, and the WP only and RWP only registrations, the very modes the mask protects, are still served a PMD by finish_fault(); the WP only case was reproduced. A selftest case in tools/testing/selftests/mm/uffd-unit-tests.c would be worthwhile: collapse a shmem range, register MISSING with WP, dontneed, touch one page, and assert that present stays at one page rather than 512 and that do_set_pmd() is not called.

7. Mitigation
Monitors should not rely on events or on the present bits alone. Periodically cross check the page table shape of critical ranges, for example with PAGEMAP_SCAN PAGE_IS_PRESENT against PAGE_IS_ACCESSED, and treat a 2MB range that is fully present after a single page touch as anomalous. Handle EEXIST from UFFDIO_ZEROPAGE and UFFDIO_COPY rather than assuming the state machine cannot reach that branch. Where the workload allows it, always including MINOR in the registration is the one combination that stayed per page in these tests, but that changes the monitor state machine and needs to be evaluated first. Applications should avoid madvise(MADV_COLLAPSE) on a range that is registered with uffd and should keep such ranges out of khugepaged scans. Administrators can keep vm.unprivileged_userfaultfd at zero, although a root monitor still has to self check. Note that shmem_enabled set to never does not help, because do_set_pmd() bypasses it with forced_collapse=true.

8. Disclosure
Public. This report is sent to the public linux-mm list and LKML and no embargo is requested. The reproducer is not posted publicly; it is available on request.

9. AI-assistance note
This report was prepared with the help of an AI coding assistant, which was used for the source review, for building and running the test kernel, and for drafting this text. Per the kernel security process I treat the issue as public and I am not posting the reproducer publicly.