KVM: arm64: Reject attempts to set invalid debug arch version
PATCH 1/1 KVM: arm64: Reject attempts to set invalid debug arch version
The debug architecture is mandatory in ARMv8, so KVM should not allow userspace to configure a vCPU with less than that. Of course, this isn't handled elegantly by the generic ID register plumbing, as the respective ID register fields have a nonzero starting value.
Add an explicit check for debug versions less than v8 of the architecture.
--- arch/arm64/kvm/sys_regs.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/arm64/kvm/sys_regs.c +12 -0
@@ -1482,6 +1482,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd, u64 val) { + u8 debugver = SYS_FIELD_GET(ID_AA64DFR0_EL1, DebugVer, val);u8 pmuver =SYS_FIELD_GET (ID_AA64DFR0_EL1 , PMUVer, val);/*
@@ -1501,6 +1502,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; +/* +* ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a +* nonzero minimum safe value. +*/ +if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP) +return -EINVAL ; +return set_id_reg (vcpu, rd, val); }
@@ -1522,6 +1530,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, u64 val) {u8 perfmon =SYS_FIELD_GET (ID_DFR0_EL1 , PerfMon, val); +u8 copdbg =SYS_FIELD_GET (ID_DFR0_EL1 , CopDbg, val);if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) { val &= ~ID_DFR0_EL1_PerfMon_MASK;
@@ -1537,6 +1546,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,if (perfmon !=0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3)return -EINVAL ; +if (copdbg < ID_DFR0_EL1_CopDbg_Armv8) +return -EINVAL ; +return set_id_reg (vcpu, rd, val); }
base-commit: 192df2aa0113ddddee2a93e453ff46610807b425
Signature · 1 line
2.41.0.178.g377b9f9a00-goog
Marc Zyngier
maintainerQuoted 28 lines · Oliver Upton
The debug architecture is mandatory in ARMv8, so KVM should not allow userspace to configure a vCPU with less than that. Of course, this isn't handled elegantly by the generic ID register plumbing, as the respective ID register fields have a nonzero starting value.
Add an explicit check for debug versions less than v8 of the architecture.
Fixes c118cead07a7 ("KVM: arm64: Use generic sanitisation for ID_(AA64)DFR0_EL1")Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/sys_regs.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/arm64/kvm/sys_regs.c +1
-0@@ -1482,6 +1482,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd, u64 val) { + u8 debugver = SYS_FIELD_GET(ID_AA64DFR0_EL1, DebugVer, val);u8 pmuver =SYS_FIELD_GET (ID_AA64DFR0_EL1 , PMUVer, val);/*
arch/arm64/kvm/sys_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 1511
Quoted 10 lines
arch/arm64/kvm/sys_regs.c +7
-0@@ -1501,6 +1502,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; +/* +* ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a +* nonzero minimum safe value. +*/ +if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP) +return -EINVAL ; +
Why isn't that caught by the check at the end of arm64_check_features which says that for RO fields, the only safe value is the sanitised version?
Am I missing something obvious (full disclosure, I've had a single coffee at this time of the day...)?
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 33 lines · Marc Zyngier
Quoted 31 lines · Oliver Upton
The debug architecture is mandatory in ARMv8, so KVM should not allow userspace to configure a vCPU with less than that. Of course, this isn't handled elegantly by the generic ID register plumbing, as the respective ID register fields have a nonzero starting value.
Add an explicit check for debug versions less than v8 of the architecture.
Fixes c118cead07a7 ("KVM: arm64: Use generic sanitisation for ID_(AA64)DFR0_EL1")Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/sys_regs.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/arm64/kvm/sys_regs.c +1
-0@@ -1482,6 +1482,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd, u64 val) { + u8 debugver = SYS_FIELD_GET(ID_AA64DFR0_EL1, DebugVer, val);u8 pmuver =SYS_FIELD_GET (ID_AA64DFR0_EL1 , PMUVer, val);/* @@ -1501,6 +1502,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
Quoted 11 lines
arch/arm64/kvm/sys_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 1511
Quoted 7 lines
arch/arm64/kvm/sys_regs.c +7
-0+/* +* ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a +* nonzero minimum safe value. +*/ +if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP) +return -EINVAL ; +Why isn't that caught by the check at the end of
arm64_check_featureswhich says that for RO fields, the only safe value is the sanitised version?
You're right, I was mistaken in thinking the field was already writable. Now, having said that, it _is_ an issue with Jing's series to make DFR0 fully writable.
https://lore.kernel.org/kvmarm/[email protected]/
Signature · 2 lines
Thanks, Oliver
Hi Oliver,
Quoted 35 lines · Oliver Upton
The debug architecture is mandatory in ARMv8, so KVM should not allow userspace to configure a vCPU with less than that. Of course, this isn't
handled elegantly by the generic ID register plumbing, as the respective
ID register fields have a nonzero starting value.Add an explicit check for debug versions less than v8 of the architecture.
Fixes c118cead07a7 ("KVM: arm64: Use generic sanitisation forID_(AA64)DFR0_EL1")
Signed-off-by Oliver Upton <[email protected]>---
arch/arm64/kvm/sys_regs.c | 12 ++++++++++++1 file changed, 12 insertions(+)arch/arm64/kvm/sys_regs.c +1
-0@@ -1482,6 +1482,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val) { + u8 debugver = SYS_FIELD_GET(ID_AA64DFR0_EL1, DebugVer, val); u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, val);
/*arch/arm64/kvm/sys_regs.c +0
-0@@ -1501,6 +1502,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
Quoted 8 lines
+ /*
+ * ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a
+ * nonzero minimum safe value.
+ */
+ if (debugver <ID_AA64DFR0_EL1_DebugVer_IMP)
+ return-EINVAL;
+
AA64DFR0_EL1_DebugVer has feature type FTR_EXACT so the guest value must exactly match the host value. So I don't believe this check is required.
Quoted 16 lines
return
set_id_reg(vcpu, rd, val);
}arch/arm64/kvm/sys_regs.c +1
-0@@ -1522,6 +1530,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, u64 val) { u8 perfmon = SYS_FIELD_GET(ID_DFR0_EL1, PerfMon, val); + u8 copdbg = SYS_FIELD_GET(ID_DFR0_EL1, CopDbg, val);
if (perfmon ==ID_DFR0_EL1_PerfMon_IMPDEF) { val &= ~ID_DFR0_EL1_PerfMon_MASK;arch/arm64/kvm/sys_regs.c +0
-0@@ -1537,6 +1546,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, if (perfmon != 0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3) return -EINVAL;
Quoted 4 lines
+ if (copdbg <ID_DFR0_EL1_CopDbg_Armv8)
+ return-EINVAL;
+
This one has type FTR_LOWER_SAFE and so the check is required.
Thanks,
Suraj
Quoted 5 lines
return
set_id_reg(vcpu, rd, val);
}base-commit:
192df2aa0113ddddee2a93e453ff46610807b425
Hi Suraj
Quoted 9 lines · Suraj
Quoted 9 lines
arch/arm64/kvm/sys_regs.c +0
-0@@ -1501,6 +1502,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
+ /*
+ * ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a
+ * nonzero minimum safe value.
Quoted 8 lines
Quoted 4 lines
+ */
+ if (debugver <ID_AA64DFR0_EL1_DebugVer_IMP) + return-EINVAL;
+
AA64DFR0_EL1_DebugVerhas feature typeFTR_EXACTso the guest value must exactly match the host value. So I don't believe this check is required.
I got waaaay ahead of myself and sent this out of context. As I had said to Marc, the fixes is utter crap on this patch since we don't permit changes to this field.
What I was actually looking at is raising the limit for the debug architecture on KVM and permitting safe values less than that of the host. So in isolation this is pointless, but will be useful for a functional change.
Quoted 14 lines
Quoted 14 lines
return
set_id_reg(vcpu, rd, val);
}arch/arm64/kvm/sys_regs.c +1
-0@@ -1522,6 +1530,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, u64 val) { u8 perfmon = SYS_FIELD_GET(ID_DFR0_EL1, PerfMon, val); + u8 copdbg = SYS_FIELD_GET(ID_DFR0_EL1, CopDbg, val);
if (perfmon ==ID_DFR0_EL1_PerfMon_IMPDEF) { val &= ~ID_DFR0_EL1_PerfMon_MASK;(quoted diff) +0
-0@@ -1537,6 +1546,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
Quoted 8 lines
Quoted 6 lines
arch/arm64/kvm/sys_regs.c +0
-0if (perfmon != 0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3) return -EINVAL;
+ if (copdbg <ID_DFR0_EL1_CopDbg_Armv8)
+ return-EINVAL;
+This one has type
FTR_LOWER_SAFEand so the check is required.
Same goes for this field -- any changes are forbidden since it is not in the writable mask.
Thanks for having a look, apologies for the confusion.
--
Thanks,
Oliver
Hi Oliver,
Quoted 24 lines · Oliver Upton
Hi Suraj
Quoted 11 lines · Suraj
Quoted 11 lines
(quoted diff) +0
-0@@ -1501,6 +1502,13 @@ static int set_id_aa64dfr0_el1(struct
kvm_vcpu
*vcpu,
if (pmuver ==ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
+ /*
+ * ID_AA64DFR0_EL1.DebugVer is one of those awkward fields
with a
+ * nonzero minimum safe value.Quoted 8 lines
Quoted 4 lines
+ */
+ if (debugver <ID_AA64DFR0_EL1_DebugVer_IMP) + return-EINVAL;
+
AA64DFR0_EL1_DebugVerhas feature typeFTR_EXACTso the guest value must exactly match the host value. So I don't believe this check is required.
Quoted 4 lines
I got waaaay ahead of myself and sent this out of context. As I had said
to Marc, the fixes is utter crap on this patch since we don't permit changes to this field.
For now this is correct. However with the series from Jing enabling writes to all fields of these registers [1] it will be necessary in future to have such checks so potentially worth folding this patch into that series.
[1]
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Quoted 5 lines
What I was actually looking at is raising the limit for the debug architecture on KVM and permitting safe values less than that of the host. So in isolation this is pointless, but will be useful for a functional change.
Understood. I guess doing something like setting the type to FTR_LOWER_SAFE in kvm_arm64_ftr_safe_value(). Then this check will be required :)
Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
return
set_id_reg(vcpu, rd, val);
}arch/arm64/kvm/sys_regs.c +1
-0@@ -1522,6 +1530,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, u64 val) { u8 perfmon = SYS_FIELD_GET(ID_DFR0_EL1, PerfMon, val); + u8 copdbg = SYS_FIELD_GET(ID_DFR0_EL1, CopDbg, val);
if (perfmon ==ID_DFR0_EL1_PerfMon_IMPDEF) { val &= ~ID_DFR0_EL1_PerfMon_MASK;(quoted diff) +0
-0@@ -1537,6 +1546,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,Quoted 8 lines
Quoted 6 lines
arch/arm64/kvm/sys_regs.c +0
-0if (perfmon != 0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3) return -EINVAL;
+ if (copdbg <ID_DFR0_EL1_CopDbg_Armv8)
+ return-EINVAL;
+This one has type
FTR_LOWER_SAFEand so the check is required.
Quoted 3 lines
Same goes for this field -- any changes are forbidden since it is not in
the writable mask.
Comment as above. This is required when enabling writes to all fields in the register.
Thanks,
Suraj
Quoted 6 lines
Thanks for having a look, apologies for the confusion.
--
Thanks,
Oliver