• Lai Jiangshan's avatar
    KVM: X86/MMU: Fix shadowing 5-level NPT for 4-level NPT L1 guest · 84e5ffd0
    Lai Jiangshan authored
    When shadowing 5-level NPT for 4-level NPT L1 guest, the root_sp is
    allocated with role.level = 5 and the guest pagetable's root gfn.
    
    And root_sp->spt[0] is also allocated with the same gfn and the same
    role except role.level = 4.  Luckily that they are different shadow
    pages, but only root_sp->spt[0] is the real translation of the guest
    pagetable.
    
    Here comes a problem:
    
    If the guest switches from gCR4_LA57=0 to gCR4_LA57=1 (or vice verse)
    and uses the same gfn as the root page for nested NPT before and after
    switching gCR4_LA57.  The host (hCR4_LA57=1) might use the same root_sp
    for the guest even the guest switches gCR4_LA57.  The guest will see
    unexpected page mapped and L2 may exploit the bug and hurt L1.  It is
    lucky that the problem can't hurt L0.
    
    And three special cases need to be handled:
    
    The root_sp should be like role.direct=1 sometimes: its contents are
    not backed by gptes, root_sp->gfns is meaningless.  (For a normal high
    level sp in shadow paging, sp->gfns is often unused and kept zero, but
    it could be relevant and meaningful if sp->gfns is used because they
    are backed by concrete gptes.)
    
    For such root_sp in the case, root_sp is just a portal to contribute
    root_sp->spt[0], and root_sp->gfns should not be used and
    root_sp->spt[0] should not be dropped if gpte[0] of the guest root
    pagetable is changed.
    
    Such root_sp should not be accounted too.
    
    So add role.passthrough to distinguish the shadow pages in the hash
    when gCR4_LA57 is toggled and fix above special cases by using it in
    kvm_mmu_page_{get|set}_gfn() and sp_has_gptes().
    Signed-off-by: default avatarLai Jiangshan <jiangshan.ljs@antgroup.com>
    Message-Id: <20220420131204.2850-3-jiangshanlai@gmail.com>
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    84e5ffd0
mmu.c 174 KB