In case of running a guest with 4-level page tables on a 5-level page
table host, it might happen that a guest might have a physical address
with reserved bits set, but the host won't see that and trap it.
Hence, we need to check page faults' physical addresses against the guest's
maximum physical memory and if it's exceeded, we need to add
the PFERR_RSVD_MASK bits to the PF's error code.
Also make sure the error code isn't overwritten by the page table walker.
Signed-off-by: Mohammed Gamal <[email protected]>
---
arch/x86/kvm/mmu/mmu.c | 4 ++++
arch/x86/kvm/mmu/paging_tmpl.h | 2 +-
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 099643edfdeb..994e8377b65f 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -523,6 +523,10 @@ static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte)
static gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
struct x86_exception *exception)
{
+ /* Check if guest physical address doesn't exceed guest maximum */
+ if (gpa >= (1ull << cpuid_maxphyaddr(vcpu)))
+ exception->error_code |= PFERR_RSVD_MASK;
+
return gpa;
}
diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h
index e4c8a4cbf407..aa3db722604b 100644
--- a/arch/x86/kvm/mmu/paging_tmpl.h
+++ b/arch/x86/kvm/mmu/paging_tmpl.h
@@ -476,7 +476,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
walker->fault.vector = PF_VECTOR;
walker->fault.error_code_valid = true;
- walker->fault.error_code = errcode;
+ walker->fault.error_code |= errcode;
#if PTTYPE == PTTYPE_EPT
/*
--
2.21.1
On 27/02/20 18:23, Mohammed Gamal wrote:
> In case of running a guest with 4-level page tables on a 5-level page
> table host, it might happen that a guest might have a physical address
> with reserved bits set, but the host won't see that and trap it.
>
> Hence, we need to check page faults' physical addresses against the guest's
> maximum physical memory and if it's exceeded, we need to add
> the PFERR_RSVD_MASK bits to the PF's error code.
You can just set it to PFERR_RSVD_MASK | PFERR_PRESENT_MASK (no need to
use an "|") and return UNMAPPED_GBA. But I would have thought that this
is not needed and the
if (unlikely(FNAME(is_rsvd_bits_set)(mmu, pte, walker->level))) {
errcode = PFERR_RSVD_MASK | PFERR_PRESENT_MASK;
goto error;
}
code would have catch the reserved bits.
> Also make sure the error code isn't overwritten by the page table walker.
Returning UNMAPPED_GVA would remove that as well.
I'm not sure this patch is enough however. For a usermode access with
"!pte.u pte.40" for example you should be getting:
- a #PF with PRESENT|USER error code on a machine with physical address
width >=41; in this case you don't get an EPT violation or misconfig.
- a #PF with RSVD error code on a machine with physical address with <41.
You can enable verbose mode in access.c to see if this case is being generated,
and if so debug it.
The solution for this would be to trap page faults and do a page table
walk (with vcpu->arch.walk_mmu->gva_to_gpa) to find the correct error
code.
Paolo
On Thu, Feb 27, 2020 at 07:00:33PM +0100, Paolo Bonzini wrote:
> On 27/02/20 18:23, Mohammed Gamal wrote:
> > In case of running a guest with 4-level page tables on a 5-level page
> > table host, it might happen that a guest might have a physical address
> > with reserved bits set, but the host won't see that and trap it.
> >
> > Hence, we need to check page faults' physical addresses against the guest's
> > maximum physical memory and if it's exceeded, we need to add
> > the PFERR_RSVD_MASK bits to the PF's error code.
>
> You can just set it to PFERR_RSVD_MASK | PFERR_PRESENT_MASK (no need to
> use an "|") and return UNMAPPED_GBA. But I would have thought that this
> is not needed and the
>
> if (unlikely(FNAME(is_rsvd_bits_set)(mmu, pte, walker->level))) {
> errcode = PFERR_RSVD_MASK | PFERR_PRESENT_MASK;
> goto error;
> }
>
> code would have catch the reserved bits.
That would be my assumption as well. The only manual check should be in
the top level EPT and NPT handlers.
> > Also make sure the error code isn't overwritten by the page table walker.
>
> Returning UNMAPPED_GVA would remove that as well.
>
> I'm not sure this patch is enough however. For a usermode access with
> "!pte.u pte.40" for example you should be getting:
>
> - a #PF with PRESENT|USER error code on a machine with physical address
> width >=41; in this case you don't get an EPT violation or misconfig.
>
> - a #PF with RSVD error code on a machine with physical address with <41.
>
> You can enable verbose mode in access.c to see if this case is being generated,
> and if so debug it.
>
> The solution for this would be to trap page faults and do a page table
> walk (with vcpu->arch.walk_mmu->gva_to_gpa) to find the correct error
> code.
>
> Paolo
>