Skip to contentSkip to conversation

KVM: arm64: Fix 32bit PC wrap-around

PATCH In review Marc Zyngier 2 participants 6 messages Apr 30, 2020

Subsystems (3)
lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 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
- sanitize PC every time a core reg is changed by userspace, and that PSTATE indicates a 32bit mode.

---
 arch/arm64/kvm/guest.c     | 4 ++++
 virt/kvm/arm/hyp/aarch32.c | 8 ++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/guest.c +4 -0
index 23ebe51410f0..2a159af82429 100644
@@ -200,6 +200,10 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	}
 
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
+
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
+		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));
+
 out:
 	return err;
 }
virt/kvm/arm/hyp/aarch32.c +6 -2
index d31f267961e7..25c0e47d57cb 100644
@@ -125,12 +125,16 @@ static void __hyp_text kvm_adjust_itstate(struct kvm_vcpu *vcpu)
  */
 void __hyp_text kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr)
 {
+	u32 pc = *vcpu_pc(vcpu);
 	bool is_thumb;
 
 	is_thumb = !!(*vcpu_cpsr(vcpu) & PSR_AA32_T_BIT);
 	if (is_thumb && !is_wide_instr)
-		*vcpu_pc(vcpu) += 2;
+		pc += 2;
 	else
-		*vcpu_pc(vcpu) += 4;
+		pc += 4;
+
+	*vcpu_pc(vcpu) = pc;
+
 	kvm_adjust_itstate(vcpu);
 }
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 11 lines · Marc Zyngier

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.

Quoted 4 lines

Fix both by:
- truncating PC increments for 32bit guests
- sanitize PC every time a core reg is changed by userspace, and that PSTATE indicates a 32bit mode.

It's not clear to me whether this needs a cc stable. What do you think? I suppose that it really depends on how confused e.g. QEMU gets.

Quoted 11 lines
---
 arch/arm64/kvm/guest.c     | 4 ++++
 virt/kvm/arm/hyp/aarch32.c | 8 ++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/guest.c +0 -0
index 23ebe51410f0..2a159af82429 100644

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 205

Quoted 6 lines
arch/arm64/kvm/guest.c +3 -0
@@ -200,6 +200,10 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	}
 
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
+
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
+		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));

It seems slightly odd to me that we don't enforce this for *all* the registers when running as a 32-bit guest. Couldn't userspace be equally confused by a 64-bit lr or sp?

Will

author maintainer
#2 lore raw
Quoted 12 lines · Will Deacon
Quoted 11 lines · Marc Zyngier

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.

Quoted 7 lines
Quoted 4 lines

Fix both by:
- truncating PC increments for 32bit guests
- sanitize PC every time a core reg is changed by userspace, and that PSTATE indicates a 32bit mode.

It's not clear to me whether this needs a cc stable. What do you think? I suppose that it really depends on how confused e.g. QEMU gets.

It isn't so much QEMU itself that I'm worried about (the emulation shouldn't really care about the PC), but the likes of GDB. So yes, a cc stable seems to be in order.

Quoted 15 lines
Quoted 15 lines
---
 arch/arm64/kvm/guest.c     | 4 ++++
 virt/kvm/arm/hyp/aarch32.c | 8 ++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/guest.c +0 -0
index 23ebe51410f0..2a159af82429 100644
@@ -200,6 +200,10 @@ static int set_core_reg(struct kvm_vcpu *vcpu, 
 const struct kvm_one_reg *reg)
 	}
 
Quoted 8 lines

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, · line 206

Quoted 4 lines
arch/arm64/kvm/guest.c +3 -0
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
+
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
+		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));

It seems slightly odd to me that we don't enforce this for *all* the registers when running as a 32-bit guest. Couldn't userspace be equally confused by a 64-bit lr or sp?

Fair point. How about this on top, which wipes the upper 32 bits for each and every register in the current mode:

arch/arm64/kvm/guest.c +5 -2
index 2a159af82429..f958c3c7bf65 100644
@@ -201,9 +201,12 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
 
-	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
-		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK) {
+		int i;
 
+		for (i = 0; i < 16; i++)
+			*vcpu_reg32(vcpu, i) = (u32)*vcpu_reg32(vcpu, i);
+	}
 out:
 	return err;
 }

I'm tempted to make the whole SET_REG hunk a separate patch though.

Thanks,

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...
maintainer
#3 lore raw
Quoted 18 lines · Marc Zyngier
Quoted 17 lines · Will Deacon
Quoted 15 lines · Marc Zyngier

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
- sanitize PC every time a core reg is changed by userspace, and that PSTATE indicates a 32bit mode.

Quoted 8 lines
Quoted 3 lines

It's not clear to me whether this needs a cc stable. What do you think? I
suppose that it really depends on how confused e.g. QEMU gets.

It isn't so much QEMU itself that I'm worried about (the emulation shouldn't really care about the PC), but the likes of GDB. So yes, a cc stable seems to
be in order.

Okey doke.

Quoted 32 lines
Quoted 14 lines
Quoted 14 lines
---
 arch/arm64/kvm/guest.c     | 4 ++++
 virt/kvm/arm/hyp/aarch32.c | 8 ++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/guest.c +0 -0
index 23ebe51410f0..2a159af82429 100644
@@ -200,6 +200,10 @@ static int set_core_reg(struct kvm_vcpu *vcpu,
 const struct kvm_one_reg *reg)
 	}
 
Quoted 8 lines

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, · line 206

Quoted 4 lines
arch/arm64/kvm/guest.c +3 -0
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
+
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
+		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));

It seems slightly odd to me that we don't enforce this for *all* the registers when running as a 32-bit guest. Couldn't userspace be equally confused by a 64-bit lr or sp?

Fair point. How about this on top, which wipes the upper 32 bits for each and every register in the current mode:

arch/arm64/kvm/guest.c +0 -0
index 2a159af82429..f958c3c7bf65 100644
@@ -201,9 +201,12 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const
 struct kvm_one_reg *reg)

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, const · line 209

Quoted 10 lines
arch/arm64/kvm/guest.c +4 -2
 
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
 
-	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
-		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK) {
+		int i;
 
+		for (i = 0; i < 16; i++)
+			*vcpu_reg32(vcpu, i) = (u32)*vcpu_reg32(vcpu, i);

I think you're missing all the funny banked registers that live all the way up to x30 iirc.

Will

author maintainer
#4 lore raw
Quoted 71 lines · Will Deacon
Quoted 18 lines · Marc Zyngier
Quoted 17 lines · Will Deacon
Quoted 15 lines · Marc Zyngier

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
- sanitize PC every time a core reg is changed by userspace, and that PSTATE indicates a 32bit mode.

Quoted 8 lines
Quoted 3 lines

It's not clear to me whether this needs a cc stable. What do you think? I
suppose that it really depends on how confused e.g. QEMU gets.

It isn't so much QEMU itself that I'm worried about (the emulation shouldn't really care about the PC), but the likes of GDB. So yes, a cc stable seems to
be in order.

Okey doke.

Quoted 41 lines
Quoted 14 lines
Quoted 14 lines
---
 arch/arm64/kvm/guest.c     | 4 ++++
 virt/kvm/arm/hyp/aarch32.c | 8 ++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/guest.c +0 -0
index 23ebe51410f0..2a159af82429 100644
@@ -200,6 +200,10 @@ static int set_core_reg(struct kvm_vcpu *vcpu,
 const struct kvm_one_reg *reg)
 	}
 
Quoted 8 lines

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, · line 206

Quoted 4 lines
arch/arm64/kvm/guest.c +3 -0
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
+
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
+		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));

It seems slightly odd to me that we don't enforce this for *all* the registers when running as a 32-bit guest. Couldn't userspace be equally confused by a 64-bit lr or sp?

Fair point. How about this on top, which wipes the upper 32 bits for each and every register in the current mode:

arch/arm64/kvm/guest.c +0 -0
index 2a159af82429..f958c3c7bf65 100644
@@ -201,9 +201,12 @@ static int set_core_reg(struct kvm_vcpu *vcpu, 

const
struct kvm_one_reg *reg)

arch/arm64/kvm/guest.c +2 -2
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
 
-	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
-		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK) {
+		int i;
Quoted 5 lines

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 208

Quoted 2 lines
arch/arm64/kvm/guest.c +2 -0
+		for (i = 0; i < 16; i++)
+			*vcpu_reg32(vcpu, i) = (u32)*vcpu_reg32(vcpu, i);

I think you're missing all the funny banked registers that live all the way up to x30 iirc.

No, they are all indirected via vcpu_reg32(), which has the magic tables. And the whole point is that we only want to affect the current mode (no point in repainting the FIQ registers if the PSR says USR).

Or am I missing something obvious?

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...
maintainer Acked-by
#5 lore raw
Quoted 46 lines · Marc Zyngier
Quoted 38 lines · Will Deacon
Quoted 37 lines · Marc Zyngier
Quoted 9 lines · Will Deacon
Quoted 8 lines · Marc Zyngier
arch/arm64/kvm/guest.c +0 -0
index 23ebe51410f0..2a159af82429 100644
@@ -200,6 +200,10 @@ static int set_core_reg(struct kvm_vcpu *vcpu,
 const struct kvm_one_reg *reg)
 	}
 
Quoted 8 lines

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, · line 206

Quoted 4 lines
arch/arm64/kvm/guest.c +3 -0
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
+
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
+		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));

It seems slightly odd to me that we don't enforce this for *all* the registers when running as a 32-bit guest. Couldn't userspace be equally confused by a 64-bit lr or sp?

Fair point. How about this on top, which wipes the upper 32 bits for each and every register in the current mode:

arch/arm64/kvm/guest.c +0 -0
index 2a159af82429..f958c3c7bf65 100644
@@ -201,9 +201,12 @@ static int set_core_reg(struct kvm_vcpu *vcpu,

const
struct kvm_one_reg *reg)

arch/arm64/kvm/guest.c +2 -2
 	memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
 
-	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK)
-		*vcpu_pc(vcpu) = lower_32_bits(*vcpu_pc(vcpu));
+	if (*vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK) {
+		int i;
Quoted 6 lines

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 208

Quoted 2 lines
arch/arm64/kvm/guest.c +2 -0
+		for (i = 0; i < 16; i++)
+			*vcpu_reg32(vcpu, i) = (u32)*vcpu_reg32(vcpu, i);

I think you're missing all the funny banked registers that live all the way
up to x30 iirc.

Quoted 6 lines

No, they are all indirected via vcpu_reg32(), which has the magic tables. And the whole point is that we only want to affect the current mode (no point
in repainting the FIQ registers if the PSR says USR).

Or am I missing something obvious?

Nope, just my inability to parse vcpu_reg32 the first time around! So, for the updated patch:

Thanks,

Will