Skip to content

Commit 2a8dfab

Browse files
ankita-nvoupton
authored andcommitted
KVM: arm64: Block cacheable PFNMAP mapping
Fixes a security bug due to mismatched attributes between S1 and S2 mapping. Currently, it is possible for a region to be cacheable in the userspace VMA, but mapped non cached in S2. This creates a potential issue where the VMM may sanitize cacheable memory across VMs using cacheable stores, ensuring it is zeroed. However, if KVM subsequently assigns this memory to a VM as uncached, the VM could end up accessing stale, non-zeroed data from a previous VM, leading to unintended data exposure. This is a security risk. Block such mismatch attributes case by returning EINVAL when userspace try to map PFNMAP cacheable. Only allow NORMAL_NC and DEVICE_*. CC: Oliver Upton <oliver.upton@linux.dev> CC: Catalin Marinas <catalin.marinas@arm.com> CC: Sean Christopherson <seanjc@google.com> Suggested-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: David Hildenbrand <david@redhat.com> Tested-by: Donald Dutile <ddutile@redhat.com> Signed-off-by: Ankit Agrawal <ankita@nvidia.com> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Link: https://lore.kernel.org/r/20250705071717.5062-4-ankita@nvidia.com Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent 216887f commit 2a8dfab

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

arch/arm64/kvm/mmu.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,14 +1465,26 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)
14651465
return vma->vm_flags & VM_MTE_ALLOWED;
14661466
}
14671467

1468+
static bool kvm_vma_is_cacheable(struct vm_area_struct *vma)
1469+
{
1470+
switch (FIELD_GET(PTE_ATTRINDX_MASK, pgprot_val(vma->vm_page_prot))) {
1471+
case MT_NORMAL_NC:
1472+
case MT_DEVICE_nGnRnE:
1473+
case MT_DEVICE_nGnRE:
1474+
return false;
1475+
default:
1476+
return true;
1477+
}
1478+
}
1479+
14681480
static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
14691481
struct kvm_s2_trans *nested,
14701482
struct kvm_memory_slot *memslot, unsigned long hva,
14711483
bool fault_is_perm)
14721484
{
14731485
int ret = 0;
14741486
bool write_fault, writable, force_pte = false;
1475-
bool exec_fault, mte_allowed;
1487+
bool exec_fault, mte_allowed, is_vma_cacheable;
14761488
bool s2_force_noncacheable = false, vfio_allow_any_uc = false;
14771489
unsigned long mmu_seq;
14781490
phys_addr_t ipa = fault_ipa;
@@ -1617,6 +1629,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
16171629

16181630
vm_flags = vma->vm_flags;
16191631

1632+
is_vma_cacheable = kvm_vma_is_cacheable(vma);
1633+
16201634
/* Don't use the VMA after the unlock -- it may have vanished */
16211635
vma = NULL;
16221636

@@ -1660,6 +1674,15 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
16601674
writable = false;
16611675
}
16621676

1677+
/*
1678+
* Prevent non-cacheable mappings in the stage-2 if a region of memory
1679+
* is cacheable in the primary MMU and the kernel lacks a cacheable
1680+
* alias. KVM cannot guarantee coherency between the guest/host aliases
1681+
* without the ability to perform CMOs.
1682+
*/
1683+
if (is_vma_cacheable && s2_force_noncacheable)
1684+
return -EINVAL;
1685+
16631686
if (exec_fault && s2_force_noncacheable)
16641687
return -ENOEXEC;
16651688

@@ -2219,6 +2242,12 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
22192242
ret = -EINVAL;
22202243
break;
22212244
}
2245+
2246+
/* Cacheable PFNMAP is not allowed */
2247+
if (kvm_vma_is_cacheable(vma)) {
2248+
ret = -EINVAL;
2249+
break;
2250+
}
22222251
}
22232252
hva = min(reg_end, vma->vm_end);
22242253
} while (hva < reg_end);

0 commit comments

Comments
 (0)