kernel_optimize_test/Documentation/virt/kvm
Lai Jiangshan 6b6ff4d1f3 KVM: X86: MMU: Use the correct inherited permissions to get shadow page
commit b1bd5cba3306691c771d558e94baa73e8b0b96b7 upstream.

When computing the access permissions of a shadow page, use the effective
permissions of the walk up to that point, i.e. the logic AND of its parents'
permissions.  Two guest PxE entries that point at the same table gfn need to
be shadowed with different shadow pages if their parents' permissions are
different.  KVM currently uses the effective permissions of the last
non-leaf entry for all non-leaf entries.  Because all non-leaf SPTEs have
full ("uwx") permissions, and the effective permissions are recorded only
in role.access and merged into the leaves, this can lead to incorrect
reuse of a shadow page and eventually to a missing guest protection page
fault.

For example, here is a shared pagetable:

   pgd[]   pud[]        pmd[]            virtual address pointers
                     /->pmd1(u--)->pte1(uw-)->page1 <- ptr1 (u--)
        /->pud1(uw-)--->pmd2(uw-)->pte2(uw-)->page2 <- ptr2 (uw-)
   pgd-|           (shared pmd[] as above)
        \->pud2(u--)--->pmd1(u--)->pte1(uw-)->page1 <- ptr3 (u--)
                     \->pmd2(uw-)->pte2(uw-)->page2 <- ptr4 (u--)

  pud1 and pud2 point to the same pmd table, so:
  - ptr1 and ptr3 points to the same page.
  - ptr2 and ptr4 points to the same page.

(pud1 and pud2 here are pud entries, while pmd1 and pmd2 here are pmd entries)

- First, the guest reads from ptr1 first and KVM prepares a shadow
  page table with role.access=u--, from ptr1's pud1 and ptr1's pmd1.
  "u--" comes from the effective permissions of pgd, pud1 and
  pmd1, which are stored in pt->access.  "u--" is used also to get
  the pagetable for pud1, instead of "uw-".

- Then the guest writes to ptr2 and KVM reuses pud1 which is present.
  The hypervisor set up a shadow page for ptr2 with pt->access is "uw-"
  even though the pud1 pmd (because of the incorrect argument to
  kvm_mmu_get_page in the previous step) has role.access="u--".

- Then the guest reads from ptr3.  The hypervisor reuses pud1's
  shadow pmd for pud2, because both use "u--" for their permissions.
  Thus, the shadow pmd already includes entries for both pmd1 and pmd2.

- At last, the guest writes to ptr4.  This causes no vmexit or pagefault,
  because pud1's shadow page structures included an "uw-" page even though
  its role.access was "u--".

Any kind of shared pagetable might have the similar problem when in
virtual machine without TDP enabled if the permissions are different
from different ancestors.

In order to fix the problem, we change pt->access to be an array, and
any access in it will not include permissions ANDed from child ptes.

The test code is: https://lore.kernel.org/kvm/20210603050537.19605-1-jiangshanlai@gmail.com/
Remember to test it with TDP disabled.

The problem had existed long before the commit 41074d07c7 ("KVM: MMU:
Fix inherited permissions for emulated guest pte updates"), and it
is hard to find which is the culprit.  So there is no fixes tag here.

Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com>
Message-Id: <20210603052455.21023-1-jiangshanlai@gmail.com>
Cc: stable@vger.kernel.org
Fixes: cea0f0e7ea ("[PATCH] KVM: MMU: Shadow page table caching")
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-06-16 12:01:40 +02:00
..
arm Documentation/kvm/arm: improve description of HVC_SOFT_RESTART 2020-09-11 19:29:10 +01:00
devices KVM: arm64: Fix some documentation build warnings 2020-10-02 09:12:25 +01:00
amd-memory-encryption.rst docs: kvm: fix referenced ioctl symbol 2020-09-09 11:19:39 -06:00
api.rst KVM: x86: Protect userspace MSR filter with SRCU, and set atomically-ish 2021-03-30 14:31:53 +02:00
cpuid.rst x86/kvm: Reserve KVM_FEATURE_MSI_EXT_DEST_ID 2020-10-28 13:52:05 -04:00
halt-polling.rst docs: virt: convert halt-polling.txt to ReST format 2020-02-12 20:09:50 +01:00
hypercalls.rst docs: fix broken references to text files 2020-04-20 15:35:59 -06:00
index.rst docs/virt/kvm: Document configuring and running nested guests 2020-05-06 05:45:47 -04:00
locking.rst KVM: Documentation: Update fast page fault for indirect sp 2020-03-16 17:58:56 +01:00
mmu.rst KVM: X86: MMU: Use the correct inherited permissions to get shadow page 2021-06-16 12:01:40 +02:00
msr.rst KVM: x86: announce KVM_FEATURE_ASYNC_PF_INT 2020-06-01 04:26:08 -04:00
nested-vmx.rst docs: kvm: Replace HTTP links with HTTPS ones 2020-07-13 09:34:07 -06:00
ppc-pv.rst docs: kvm: Convert ppc-pv.txt to ReST format 2020-02-12 20:10:05 +01:00
review-checklist.rst docs: fix broken references for ReST files that moved around 2020-04-20 15:45:03 -06:00
running-nested-guests.rst docs/virt/kvm: Document configuring and running nested guests 2020-05-06 05:45:47 -04:00
s390-diag.rst docs: kvm: Convert s390-diag.txt to ReST format 2020-02-12 20:10:06 +01:00
s390-pv-boot.rst DOCUMENTATION: Protected virtual machine introduction and IPL 2020-02-27 19:47:12 +01:00
s390-pv.rst Documentation: virt: kvm/s390-pv: drop doubled words 2020-07-05 14:42:17 -06:00
timekeeping.rst docs: kvm: Convert timekeeping.txt to ReST format 2020-02-12 20:10:06 +01:00
vcpu-requests.rst