Skip to content

Commit e4e11cc

Browse files
christofferdall-armMarc Zyngier
authored andcommitted
KVM: arm64: Safety check PSTATE when entering guest and handle IL
This commit adds a paranoid check when entering the guest to make sure we don't attempt running guest code in an equally or more privilged mode than the hypervisor. We also catch other accidental programming of the SPSR_EL2 which results in an illegal exception return and report this safely back to the user. Signed-off-by: Christoffer Dall <christoffer.dall@arm.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 58bf437 commit e4e11cc

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

arch/arm64/include/asm/kvm_asm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define ARM_EXCEPTION_IRQ 0
3131
#define ARM_EXCEPTION_EL1_SERROR 1
3232
#define ARM_EXCEPTION_TRAP 2
33+
#define ARM_EXCEPTION_IL 3
3334
/* The hyp-stub will return this for any kvm_call_hyp() call */
3435
#define ARM_EXCEPTION_HYP_GONE HVC_STUB_ERR
3536

arch/arm64/include/asm/ptrace.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#define CurrentEL_EL1 (1 << 2)
2626
#define CurrentEL_EL2 (2 << 2)
2727

28+
/* Additional SPSR bits not exposed in the UABI */
29+
#define PSR_IL_BIT (1 << 20)
30+
2831
/* AArch32-specific ptrace requests */
2932
#define COMPAT_PTRACE_GETREGS 12
3033
#define COMPAT_PTRACE_SETREGS 13

arch/arm64/kvm/handle_exit.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
284284
*/
285285
run->exit_reason = KVM_EXIT_FAIL_ENTRY;
286286
return 0;
287+
case ARM_EXCEPTION_IL:
288+
/*
289+
* We attempted an illegal exception return. Guest state must
290+
* have been corrupted somehow. Give up.
291+
*/
292+
run->exit_reason = KVM_EXIT_FAIL_ENTRY;
293+
return -EINVAL;
287294
default:
288295
kvm_pr_unimpl("Unsupported exception type: %d",
289296
exception_index);

arch/arm64/kvm/hyp/hyp-entry.S

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ el1_error:
162162
mov x0, #ARM_EXCEPTION_EL1_SERROR
163163
b __guest_exit
164164

165+
el2_sync:
166+
/* Check for illegal exception return, otherwise panic */
167+
mrs x0, spsr_el2
168+
169+
/* if this was something else, then panic! */
170+
tst x0, #PSR_IL_BIT
171+
b.eq __hyp_panic
172+
173+
/* Let's attempt a recovery from the illegal exception return */
174+
get_vcpu_ptr x1, x0
175+
mov x0, #ARM_EXCEPTION_IL
176+
b __guest_exit
177+
178+
165179
el2_error:
166180
ldp x0, x1, [sp], #16
167181

@@ -240,7 +254,7 @@ ENTRY(__kvm_hyp_vector)
240254
invalid_vect el2t_fiq_invalid // FIQ EL2t
241255
invalid_vect el2t_error_invalid // Error EL2t
242256

243-
invalid_vect el2h_sync_invalid // Synchronous EL2h
257+
valid_vect el2_sync // Synchronous EL2h
244258
invalid_vect el2h_irq_invalid // IRQ EL2h
245259
invalid_vect el2h_fiq_invalid // FIQ EL2h
246260
valid_vect el2_error // Error EL2h

arch/arm64/kvm/hyp/sysreg-sr.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,25 @@ static void __hyp_text __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt)
152152
static void __hyp_text
153153
__sysreg_restore_el2_return_state(struct kvm_cpu_context *ctxt)
154154
{
155+
u64 pstate = ctxt->gp_regs.regs.pstate;
156+
u64 mode = pstate & PSR_AA32_MODE_MASK;
157+
158+
/*
159+
* Safety check to ensure we're setting the CPU up to enter the guest
160+
* in a less privileged mode.
161+
*
162+
* If we are attempting a return to EL2 or higher in AArch64 state,
163+
* program SPSR_EL2 with M=EL2h and the IL bit set which ensures that
164+
* we'll take an illegal exception state exception immediately after
165+
* the ERET to the guest. Attempts to return to AArch32 Hyp will
166+
* result in an illegal exception return because EL2's execution state
167+
* is determined by SCR_EL3.RW.
168+
*/
169+
if (!(mode & PSR_MODE32_BIT) && mode >= PSR_MODE_EL2t)
170+
pstate = PSR_MODE_EL2h | PSR_IL_BIT;
171+
155172
write_sysreg_el2(ctxt->gp_regs.regs.pc, elr);
156-
write_sysreg_el2(ctxt->gp_regs.regs.pstate, spsr);
173+
write_sysreg_el2(pstate, spsr);
157174

158175
if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN))
159176
write_sysreg_s(ctxt->sys_regs[DISR_EL1], SYS_VDISR_EL2);

0 commit comments

Comments
 (0)