Skip to content

Commit efff9dd

Browse files
author
Marc Zyngier
committed
KVM: arm64: Handle out-of-bound write to MDCR_EL2.HPMN
We don't really pay attention to what gets written to MDCR_EL2.HPMN, and funky guests could play ugly games on us. Restrict what gets written there, and limit the number of counters to what the PMU is allowed to have. Reviewed-by: Oliver Upton <oliver.upton@linux.dev> Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent cd84a42 commit efff9dd

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

arch/arm64/kvm/sys_regs.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,16 +2571,33 @@ static bool access_mdcr(struct kvm_vcpu *vcpu,
25712571
struct sys_reg_params *p,
25722572
const struct sys_reg_desc *r)
25732573
{
2574-
u64 old = __vcpu_sys_reg(vcpu, MDCR_EL2);
2574+
u64 hpmn, val, old = __vcpu_sys_reg(vcpu, MDCR_EL2);
25752575

2576-
if (!access_rw(vcpu, p, r))
2577-
return false;
2576+
if (!p->is_write) {
2577+
p->regval = old;
2578+
return true;
2579+
}
2580+
2581+
val = p->regval;
2582+
hpmn = FIELD_GET(MDCR_EL2_HPMN, val);
2583+
2584+
/*
2585+
* If HPMN is out of bounds, limit it to what we actually
2586+
* support. This matches the UNKNOWN definition of the field
2587+
* in that case, and keeps the emulation simple. Sort of.
2588+
*/
2589+
if (hpmn > vcpu->kvm->arch.nr_pmu_counters) {
2590+
hpmn = vcpu->kvm->arch.nr_pmu_counters;
2591+
u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
2592+
}
2593+
2594+
__vcpu_sys_reg(vcpu, MDCR_EL2) = val;
25782595

25792596
/*
2580-
* Request a reload of the PMU to enable/disable the counters affected
2581-
* by HPME.
2597+
* Request a reload of the PMU to enable/disable the counters
2598+
* affected by HPME.
25822599
*/
2583-
if ((old ^ __vcpu_sys_reg(vcpu, MDCR_EL2)) & MDCR_EL2_HPME)
2600+
if ((old ^ val) & MDCR_EL2_HPME)
25842601
kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
25852602

25862603
return true;

0 commit comments

Comments
 (0)