Commit 4f4aa80e authored by Lai Jiangshan's avatar Lai Jiangshan Committed by Paolo Bonzini

KVM: X86: Handle implicit supervisor access with SMAP

There are two kinds of implicit supervisor access
	implicit supervisor access when CPL = 3
	implicit supervisor access when CPL < 3

Current permission_fault() handles only the first kind for SMAP.

But if the access is implicit when SMAP is on, data may not be read
nor write from any user-mode address regardless the current CPL.

So the second kind should be also supported.

The first kind can be detect via CPL and access mode: if it is
supervisor access and CPL = 3, it must be implicit supervisor access.

But it is not possible to detect the second kind without extra
information, so this patch adds an artificial PFERR_EXPLICIT_ACCESS
into @access. This extra information also works for the first kind, so
the logic is changed to use this information for both cases.

The value of PFERR_EXPLICIT_ACCESS is deliberately chosen to be bit 48
which is in the most significant 16 bits of u64 and less likely to be
forced to change due to future hardware uses it.

This patch removes the call to ->get_cpl() for access mode is determined
by @access.  Not only does it reduce a function call, but also remove
confusions when the permission is checked for nested TDP.  The nested
TDP shouldn't have SMAP checking nor even the L2's CPL have any bearing
on it.  The original code works just because it is always user walk for
NPT and SMAP fault is not set for EPT in update_permission_bitmask.
Signed-off-by: default avatarLai Jiangshan <jiangshan.ljs@antgroup.com>
Message-Id: <20220311070346.45023-5-jiangshanlai@gmail.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 8873c143
...@@ -249,6 +249,7 @@ enum x86_intercept_stage; ...@@ -249,6 +249,7 @@ enum x86_intercept_stage;
#define PFERR_SGX_BIT 15 #define PFERR_SGX_BIT 15
#define PFERR_GUEST_FINAL_BIT 32 #define PFERR_GUEST_FINAL_BIT 32
#define PFERR_GUEST_PAGE_BIT 33 #define PFERR_GUEST_PAGE_BIT 33
#define PFERR_IMPLICIT_ACCESS_BIT 48
#define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT) #define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)
#define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT) #define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)
...@@ -259,6 +260,7 @@ enum x86_intercept_stage; ...@@ -259,6 +260,7 @@ enum x86_intercept_stage;
#define PFERR_SGX_MASK (1U << PFERR_SGX_BIT) #define PFERR_SGX_MASK (1U << PFERR_SGX_BIT)
#define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT) #define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)
#define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT) #define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT)
#define PFERR_IMPLICIT_ACCESS (1ULL << PFERR_IMPLICIT_ACCESS_BIT)
#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \
PFERR_WRITE_MASK | \ PFERR_WRITE_MASK | \
......
...@@ -218,25 +218,23 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, ...@@ -218,25 +218,23 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
{ {
/* strip nested paging fault error codes */ /* strip nested paging fault error codes */
unsigned int pfec = access; unsigned int pfec = access;
int cpl = static_call(kvm_x86_get_cpl)(vcpu);
unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu); unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu);
/* /*
* If CPL < 3, SMAP prevention are disabled if EFLAGS.AC = 1. * For explicit supervisor accesses, SMAP is disabled if EFLAGS.AC = 1.
* For implicit supervisor accesses, SMAP cannot be overridden.
* *
* If CPL = 3, SMAP applies to all supervisor-mode data accesses * SMAP works on supervisor accesses only, and not_smap can
* (these are implicit supervisor accesses) regardless of the value * be set or not set when user access with neither has any bearing
* of EFLAGS.AC. * on the result.
* *
* This computes (cpl < 3) && (rflags & X86_EFLAGS_AC), leaving * We put the SMAP checking bit in place of the PFERR_RSVD_MASK bit;
* the result in X86_EFLAGS_AC. We then insert it in place of * this bit will always be zero in pfec, but it will be one in index
* the PFERR_RSVD_MASK bit; this bit will always be zero in pfec, * if SMAP checks are being disabled.
* but it will be one in index if SMAP checks are being overridden.
* It is important to keep this branchless.
*/ */
unsigned long not_smap = (cpl - 3) & (rflags & X86_EFLAGS_AC); u64 implicit_access = access & PFERR_IMPLICIT_ACCESS;
int index = (pfec >> 1) + bool not_smap = ((rflags & X86_EFLAGS_AC) | implicit_access) == X86_EFLAGS_AC;
(not_smap >> (X86_EFLAGS_AC_BIT - PFERR_RSVD_BIT + 1)); int index = (pfec + (not_smap << PFERR_RSVD_BIT)) >> 1;
bool fault = (mmu->permissions[index] >> pte_access) & 1; bool fault = (mmu->permissions[index] >> pte_access) & 1;
u32 errcode = PFERR_PRESENT_MASK; u32 errcode = PFERR_PRESENT_MASK;
......
...@@ -4591,8 +4591,8 @@ static void update_permission_bitmask(struct kvm_mmu *mmu, bool ept) ...@@ -4591,8 +4591,8 @@ static void update_permission_bitmask(struct kvm_mmu *mmu, bool ept)
* - X86_CR4_SMAP is set in CR4 * - X86_CR4_SMAP is set in CR4
* - A user page is accessed * - A user page is accessed
* - The access is not a fetch * - The access is not a fetch
* - Page fault in kernel mode * - The access is supervisor mode
* - if CPL = 3 or X86_EFLAGS_AC is clear * - If implicit supervisor access or X86_EFLAGS_AC is clear
* *
* Here, we cover the first four conditions. * Here, we cover the first four conditions.
* The fifth is computed dynamically in permission_fault(); * The fifth is computed dynamically in permission_fault();
......
...@@ -6865,7 +6865,9 @@ static int emulator_read_std(struct x86_emulate_ctxt *ctxt, ...@@ -6865,7 +6865,9 @@ static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
u64 access = 0; u64 access = 0;
if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3) if (system)
access |= PFERR_IMPLICIT_ACCESS;
else if (static_call(kvm_x86_get_cpl)(vcpu) == 3)
access |= PFERR_USER_MASK; access |= PFERR_USER_MASK;
return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
...@@ -6917,7 +6919,9 @@ static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *v ...@@ -6917,7 +6919,9 @@ static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *v
struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
u64 access = PFERR_WRITE_MASK; u64 access = PFERR_WRITE_MASK;
if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3) if (system)
access |= PFERR_IMPLICIT_ACCESS;
else if (static_call(kvm_x86_get_cpl)(vcpu) == 3)
access |= PFERR_USER_MASK; access |= PFERR_USER_MASK;
return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment