Skip to content

Commit 0225fd5

Browse files
author
Marc Zyngier
committed
KVM: arm64: Fix 32bit PC wrap-around
In the unlikely event that a 32bit vcpu traps into the hypervisor on an instruction that is located right at the end of the 32bit range, the emulation of that instruction is going to increment PC past the 32bit range. This isn't great, as userspace can then observe this value and get a bit confused. Conversly, userspace can do things like (in the context of a 64bit guest that is capable of 32bit EL0) setting PSTATE to AArch64-EL0, set PC to a 64bit value, change PSTATE to AArch32-USR, and observe that PC hasn't been truncated. More confusion. Fix both by: - truncating PC increments for 32bit guests - sanitizing all 32bit regs every time a core reg is changed by userspace, and that PSTATE indicates a 32bit mode. Cc: [email protected] Acked-by: Will Deacon <[email protected]> Signed-off-by: Marc Zyngier <[email protected]>
1 parent 958e8e1 commit 0225fd5

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

arch/arm64/kvm/guest.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
200200
}
201201

202202
memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
203+
204+
if (*vcpu_cpsr(vcpu) & PSR_MODE32_BIT) {
205+
int i;
206+
207+
for (i = 0; i < 16; i++)
208+
*vcpu_reg32(vcpu, i) = (u32)*vcpu_reg32(vcpu, i);
209+
}
203210
out:
204211
return err;
205212
}

virt/kvm/arm/hyp/aarch32.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,16 @@ static void __hyp_text kvm_adjust_itstate(struct kvm_vcpu *vcpu)
125125
*/
126126
void __hyp_text kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr)
127127
{
128+
u32 pc = *vcpu_pc(vcpu);
128129
bool is_thumb;
129130

130131
is_thumb = !!(*vcpu_cpsr(vcpu) & PSR_AA32_T_BIT);
131132
if (is_thumb && !is_wide_instr)
132-
*vcpu_pc(vcpu) += 2;
133+
pc += 2;
133134
else
134-
*vcpu_pc(vcpu) += 4;
135+
pc += 4;
136+
137+
*vcpu_pc(vcpu) = pc;
138+
135139
kvm_adjust_itstate(vcpu);
136140
}

0 commit comments

Comments
 (0)