change_huge_pmd() and change_huge_pud() temporarily invalidate huge entries, for example during mprotect(). The invalidated entry is not present, but still contains the PFN. On x86 with L1TF, we invert the PFN of a not-present entry, which for a PFN at or above l1tf_pfn_limit() points into the lower half of the physical address space, where RAM might be. That's why pfn_modify_allowed() forbids such entries for unprivileged users. But we never check it when mapping huge PFN entries. This was not a problem as long as only DAX used huge PFN mappings, where all PFNs have a memory map. But vfio-pci now maps BARs, which can be at or above the limit, with huge entries. Consequently, an unprivileged user with access to such a BAR can make us temporarily create not-present entries that point at RAM, which is what the L1TF mitigation is supposed to prevent. Let's fix it by not mapping huge PFN entries at all in vmf_insert_pfn_pmd() and vmf_insert_pfn_pud() if the architecture restricts PFN modifications, and fall back to PTEs instead. We clear PTEs instead of invalidating them when modifying them, and mprotect() already checks for PROT_NONE PTEs. This only affects old CPUs that suffer from L1TF; not worth optimizing for. This has low urgency, as it requires a CPU affected by L1TF and access to a vfio-pci device. Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support") Cc: stable@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- mm/huge_memory.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 0b4834f843c73..38b16333d3987 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1732,6 +1732,13 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn, if (unlikely(is_huge_zero_pfn(pfn))) return VM_FAULT_SIGBUS; + /* + * Disallow huge mappings if we cannot reliably temporarily invalidate + * them later. + */ + if (arch_has_pfn_modify_check()) + return VM_FAULT_FALLBACK; + pfnmap_setup_cachemode_pfn(pfn, &pgprot); return insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write); @@ -1848,6 +1855,13 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, unsigned long pfn, VM_WARN_ON_ONCE(vma_test_all(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT)); VM_WARN_ON_ONCE(vma_test(vma, VMA_PFNMAP_BIT) && vma_is_cow_mapping(vma)); + /* + * Disallow huge mappings if we cannot reliably temporarily invalidate + * them later. + */ + if (arch_has_pfn_modify_check()) + return VM_FAULT_FALLBACK; + pfnmap_setup_cachemode_pfn(pfn, &pgprot); return insert_pud(vma, addr, vmf->pud, fop, pgprot, write); -- 2.56.1