KVM/x86: Drop "1" as MSR emulation return value
Subsystems (5)
v2 cover KVM/x86: Drop "1" as MSR emulation return value
Get rid of the literal "1" used as general error return value in KVM MSR emulation. It can easily be replaced by negative errno values instead.
This is meant to avoid confusion with the literal "1" used as return value for "return to guest".
Changes in V2:
- series carved out from initial "KVM: Avoid literal numbers as return values" series
- don't use new KVM_MSR_RET_* defines, but 0 and -errno
Juergen Gross (5):
KVM/x86: Change comment before KVM_MSR_RET_* defines KVM/x86: Return -errno instead of "1" for APIC related MSR emulation KVM/x86: Return -errno instead of "1" for Hyper-V related MSR
emulation
KVM/x86: Return -errno instead of "1" for VMX related MSR emulation KVM/x86: Return -errno instead of "1" for SVM related MSR emulation
arch/x86/kvm/hyperv.c | 72 +++++++++++++-------------- arch/x86/kvm/lapic.c | 39 +++++++-------- arch/x86/kvm/svm/pmu.c | 4 +- arch/x86/kvm/svm/svm.c | 36 +++++++------- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/pmu_intel.c | 16 +++--- arch/x86/kvm/vmx/tdx.c | 10 ++-- arch/x86/kvm/vmx/vmx.c | 96 ++++++++++++++++++------------------ arch/x86/kvm/x86.h | 4 +- 9 files changed, 139 insertions(+), 140 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 1/5 KVM/x86: Change comment before KVM_MSR_RET_* defines
For MSR emulation return values only 2 special cases have defines, while the most used values 0 and 1 don't.
Reason seems to be the maze of function calls of MSR emulation intertwined with the KVM guest exit handlers, which are using the values 0 and 1 for other purposes. This even led to the comment above the already existing defines, warning to use the values 0 and 1 (and negative errno values) in the MSR emulation at all.
Fact is that MSR emulation and exit handlers are in fact rather well distinct, with only very few exceptions which are handled in a sane way. Negative errno values and 0 are fine to be used, while the value "1" is just used for unspecific error cases.
Drop the warning comment part, as it is just plainly wrong.
Replace it with a statement that it is fine to use negative errno values, 0, and the special MSR emulation return values defined.
Don't mention the value "1", as current cases where MSR emulation is returning "1" should be modified to return a negative errno value instead.
No change of functionality intended.
---
V2:
- instead of defining new return value macros, adapt the comment
--- arch/x86/kvm/x86.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.h +2 -2
@@ -675,8 +675,8 @@ enum kvm_msr_access {/* * Internal error codes that are used to indicate that MSR emulation encountered * an error that should result in #GP in the guest, unless userspace handles it. -* Note, '1', '0', and negative numbers are off limits, as they are used by KVM -* as part of KVM's lightly documented internal KVM_RUN return codes. +* Other than these internal error codes negative errno values can be used for +* error cases, or 0 for success. * * UNSUPPORTED - The MSR isn't supported, either because it is completely * unknown to KVM, or because the MSR should not exist according
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 1/5] KVM/x86: Change comment before KVM_MSR_RET_* defines
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The commit encourages returning negative errno values instead of '1' for MSR emulation errors…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The commit encourages returning negative errno values instead of '1' for MSR emulation errors, which breaks the x86 instruction emulator and causes fatal VM exits. --
commit 9a1f204ea737ebfbc9866132f9f3977dfdf82723
Author: Juergen Gross <[email protected]>
KVM/x86: Change comment before KVM_MSR_RET_* defines
This commit modifies the documentation for MSR emulation return values, stating that negative errno values can be used for error cases instead of the value 1. It drops the warning about returning 1, 0, and negative numbers.
Quoted 3 lines
Don't mention the value "1", as current cases where MSR emulation is returning "1" should be modified to return a negative errno value instead.
[Severity: High]
Does recommending negative errno values inadvertently break the x86 instruction emulator?
Looking at emulator_set_msr_with_filter() and emulator_get_msr_with_filter() in arch/x86/kvm/x86.c, the sign of the return value is used to determine the outcome of the emulation:
r = kvm_emulate_msr_read(vcpu, msr_index, pdata);
if (r < 0)
return X86EMUL_UNHANDLEABLE;
if (r) {
if (r == KVM_MSR_RET_INVALID)
return X86EMUL_IO_NEEDED;
trace_kvm_msr_read_ex(msr_index);
return X86EMUL_PROPAGATE_FAULT;
}If a negative errno is returned, these functions return X86EMUL_UNHANDLEABLE, which x86_emulate_insn() treats as an emulation failure by returning EMULATION_FAILED. This causes KVM to exit to userspace with KVM_EXIT_INTERNAL_ERROR and abort the VM.
Conversely, a positive return value like 1 correctly results in X86EMUL_PROPAGATE_FAULT, safely injecting a #GP into the guest.
Could standard guest #GP conditions be transformed into host-level VM crashes if developers follow this guidance and switch to negative errno values?
arch/x86/kvm/x86.h enum kvm_msr_access { · line 679
Quoted 12 lines
arch/x86/kvm/x86.h +2
-2@@ -675,8 +675,8 @@ enum kvm_msr_access {/* * Internal error codes that are used to indicate that MSR emulation encountered * an error that should result in #GP in the guest, unless userspace handles it. -* Note, '1', '0', and negative numbers are off limits, as they are used by KVM -* as part of KVM's lightly documented internal KVM_RUN return codes. +* Other than these internal error codes negative errno values can be used for +* error cases, or 0 for success.
[Severity: High]
As mentioned above, updating this comment to encourage negative errno values might lead to unintended VM crashes during emulation. Is it possible we should keep the warning about negative numbers being off limits here?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/5 KVM/x86: Return -errno instead of "1" for APIC related MSR emulation
In reply to Juergen GrossInstead of a literal "1" for signalling an error, use a negative errno value in the emulation code of APIC related MSR registers.
Remove some not needed braces.
---
V2:
- use -errno instead of KVM_MSR_RET_ERR
--- arch/x86/kvm/lapic.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-)
arch/x86/kvm/lapic.c +19 -20
@@ -2412,11 +2412,10 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)switch (reg) {case APIC_ID :/* Local APIC ID */ -if (!apic_x2apic_mode (apic)) { +if (!apic_x2apic_mode (apic))kvm_apic_set_xapic_id (apic, val >>24 ); - }else { - ret =1 ; - } +else + ret = -EINVAL ;break ;case APIC_TASKPRI :
@@ -2432,14 +2431,14 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)if (!apic_x2apic_mode (apic))kvm_apic_set_ldr (apic, val &APIC_LDR_MASK );else - ret =1 ; + ret = -EINVAL ;break ;case APIC_DFR :if (!apic_x2apic_mode (apic))kvm_apic_set_dfr (apic, val |0x0FFFFFFF );else - ret =1 ; + ret = -EINVAL ;break ;case APIC_SPIV : {
@@ -2470,7 +2469,7 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)break ;case APIC_ICR2 :if (apic_x2apic_mode (apic)) - ret =1 ; + ret = -EINVAL ;else kvm_lapic_set_reg (apic,APIC_ICR2 , val &0xff000000 );break ;
@@ -2485,7 +2484,7 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)case APIC_LVTCMCI : {u32 index =get_lvt_index (reg);if (!kvm_lapic_lvt_supported (apic, index)) { - ret =1 ; + ret = -EINVAL ;break ; }if (!kvm_apic_sw_enabled (apic))
@@ -2527,7 +2526,7 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) }case APIC_ESR :if (apic_x2apic_mode (apic) && val !=0 ) - ret =1 ; + ret = -EINVAL ;break ;case APIC_SELF_IPI :
@@ -2536,12 +2535,12 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)* the vector, everything else is reserved. */ if (!apic_x2apic_mode (apic) || (val & ~APIC_VECTOR_MASK )) - ret =1 ; + ret = -EINVAL ;else kvm_apic_send_ipi (apic,APIC_DEST_SELF | val,0 );break ;default : - ret =1 ; + ret = -EINVAL ;break ; }
@@ -2599,7 +2598,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_lapic_set_eoi);static int __kvm_x2apic_icr_write (struct kvm_lapic *apic,u64 data,bool fast) {if (data &X2APIC_ICR_RESERVED_BITS ) -return 1 ; +return -EINVAL ;/* * The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but
@@ -2804,12 +2803,12 @@ int kvm_apic_set_base(struct kvm_vcpu *vcpu, u64 value, bool host_initiated) (guest_cpu_cap_has (vcpu,X86_FEATURE_X2APIC ) ?0 :X2APIC_ENABLE );if ((value & reserved_bits) !=0 || new_mode ==LAPIC_MODE_INVALID ) -return 1 ; +return -EINVAL ;if (!host_initiated) {if (old_mode ==LAPIC_MODE_X2APIC && new_mode ==LAPIC_MODE_XAPIC )return 1 ;if (old_mode ==LAPIC_MODE_DISABLED && new_mode ==LAPIC_MODE_X2APIC ) -return 1 ; +return -EINVAL ; }__kvm_apic_set_base (vcpu, value);
@@ -3438,7 +3437,7 @@ static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data) }if (kvm_lapic_reg_read (apic, reg,4 , &low)) -return 1 ; +return -EINVAL ; *data = low;
@@ -3457,7 +3456,7 @@ static int kvm_lapic_msr_write(struct kvm_lapic *apic, u32 reg, u64 data)/* Bits 63:32 are reserved in all other registers. */ if (data >>32 ) -return 1 ; +return -EINVAL ;return kvm_lapic_reg_write (apic, reg, (u32 )data); }
@@ -3468,7 +3467,7 @@ int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)u32 reg = (msr -APIC_BASE_MSR ) <<4 ;if (!lapic_in_kernel (vcpu) || !apic_x2apic_mode (apic)) -return 1 ; +return -EINVAL ;return kvm_lapic_msr_write (apic, reg, data); }
@@ -3479,7 +3478,7 @@ int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)u32 reg = (msr -APIC_BASE_MSR ) <<4 ;if (!lapic_in_kernel (vcpu) || !apic_x2apic_mode (apic)) -return 1 ; +return -EINVAL ;return kvm_lapic_msr_read (apic, reg, data); }
@@ -3487,7 +3486,7 @@ int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)int kvm_hv_vapic_msr_write (struct kvm_vcpu *vcpu,u32 reg,u64 data) {if (!lapic_in_kernel (vcpu)) -return 1 ; +return -EINVAL ;return kvm_lapic_msr_write (vcpu->arch.apic, reg, data); }
@@ -3495,7 +3494,7 @@ int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)int kvm_hv_vapic_msr_read (struct kvm_vcpu *vcpu,u32 reg,u64 *data) {if (!lapic_in_kernel (vcpu)) -return 1 ; +return -EINVAL ;return kvm_lapic_msr_read (vcpu->arch.apic, reg, data); }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 2/5] KVM/x86: Return -errno instead of "1" for APIC related MSR emulation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Changing the return value of APIC-related MSR emulation functions from `1` to a negative error…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Changing the return value of APIC-related MSR emulation functions from 1 to a negative error code (-EINVAL) breaks KVM's MSR handling contract, allowing a guest to crash the entire VM instead of receiving a #GP fault. --
commit f7cc9a2fd2433a7138900d7cd7af1e72eb4fc13a
Author: Juergen Gross <[email protected]>
KVM/x86: Return -errno instead of "1" for APIC related MSR emulation
This commit updates the APIC related MSR emulation code to return negative errno values instead of a literal 1 on errors. It also cleans up some unnecessary braces in the code.
Quoted 6 lines
arch/x86/kvm/lapic.c +0
-0@@ -2412,11 +2412,10 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
arch/x86/kvm/lapic.c static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) · line 2418
Quoted 10 lines
arch/x86/kvm/lapic.c +3
-4switch (reg) {case APIC_ID :/* Local APIC ID */ -if (!apic_x2apic_mode (apic)) { +if (!apic_x2apic_mode (apic))kvm_apic_set_xapic_id (apic, val >>24 ); - }else { - ret =1 ; - } +else + ret = -EINVAL ;
[Severity: Critical]
Does returning a negative error code here alter the KVM MSR handling contract?
In the x86 MSR emulation framework, returning a positive value like 1 signals an architectural fault and results in a #GP being injected into the guest. If a negative value like -EINVAL is returned, it is treated as an unrecoverable ioctl error during MSR writes.
Functions like __kvm_emulate_wrmsr check for negative return values and bubble them up through the VM-exit handler. This causes the KVM_RUN ioctl to exit with -EINVAL, which typically causes the userspace VMM to abort.
Could this regression allow a guest OS to intentionally crash its own VM process by performing an invalid MSR access, rather than correctly receiving a general protection fault?
Quoted 1 line
arch/x86/kvm/lapic.c +0
-0break ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/5 KVM/x86: Return -errno instead of "1" for Hyper-V related MSR emulation
In reply to Juergen GrossInstead of a literal "1" for signalling an error, use a negative errno value in the emulation code of Hyper-V related MSR registers.
---
V2:
- use -errno instead of KVM_MSR_RET_ERR
--- arch/x86/kvm/hyperv.c | 72 +++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 36 deletions(-)
arch/x86/kvm/hyperv.c +36 -36
@@ -167,7 +167,7 @@ static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,* allow zero-initing the register from host as well. */ if (vector <HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked) -return 1 ; +return -EINVAL ;/* * Guest may configure multiple SINTs to use the same vector, so * we maintain a bitmap of vectors handled by synic, and a
@@ -263,7 +263,7 @@ static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,int ret;if (!synic->active && (!host || data)) -return 1 ; +return -EINVAL ;trace_kvm_hv_synic_set_msr (vcpu->vcpu_id, msr, data, host);
@@ -276,7 +276,7 @@ static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,break ;case HV_X64_MSR_SVERSION :if (!host) { - ret =1 ; + ret = -EINVAL ;break ; } synic->version = data;
@@ -286,7 +286,7 @@ static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, !synic->dont_zero_synic_pages)if (kvm_clear_guest (vcpu->kvm, data &PAGE_MASK ,PAGE_SIZE )) { - ret =1 ; + ret = -EFAULT ;break ; } synic->evt_page = data;
@@ -298,7 +298,7 @@ static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, !synic->dont_zero_synic_pages)if (kvm_clear_guest (vcpu->kvm, data &PAGE_MASK ,PAGE_SIZE )) { - ret =1 ; + ret = -EFAULT ;break ; } synic->msg_page = data;
@@ -319,7 +319,7 @@ static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, ret =synic_set_sint (synic, msr -HV_X64_MSR_SINT0 , data, host);break ;default : - ret =1 ; + ret = -EINVAL ;break ; }return ret;
@@ -365,7 +365,7 @@ static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)struct kvm_hv_syndbg *syndbg =to_hv_syndbg (vcpu);if (!kvm_hv_is_syndbg_enabled (vcpu) && !host) -return 1 ; +return -EINVAL ;trace_kvm_hv_syndbg_set_msr (vcpu->vcpu_id,to_hv_vcpu (vcpu)->vp_index, msr, data);
@@ -404,7 +404,7 @@ static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)struct kvm_hv_syndbg *syndbg =to_hv_syndbg (vcpu);if (!kvm_hv_is_syndbg_enabled (vcpu) && !host) -return 1 ; +return -EINVAL ;switch (msr) {case HV_X64_MSR_SYNDBG_CONTROL :
@@ -440,7 +440,7 @@ static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,int ret;if (!synic->active && !host) -return 1 ; +return -EINVAL ; ret =0 ;switch (msr) {
@@ -463,7 +463,7 @@ static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata, *pdata =atomic64_read (&synic->sint[msr -HV_X64_MSR_SINT0 ]);break ;default : - ret =1 ; + ret = -EINVAL ;break ; }return ret;
@@ -695,12 +695,12 @@ static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,struct kvm_vcpu_hv_synic *synic =to_hv_synic (vcpu);if (!synic->active && (!host || config)) -return 1 ; +return -EINVAL ;if (unlikely (!host && hv_vcpu->enforce_cpuid && new_config.direct_mode && !(hv_vcpu->cpuid_cache.features_edx &HV_STIMER_DIRECT_MODE_AVAILABLE ))) -return 1 ; +return -EINVAL ;trace_kvm_hv_stimer_set_config (hv_stimer_to_vcpu (stimer)->vcpu_id, stimer->index, config, host);
@@ -724,7 +724,7 @@ static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,struct kvm_vcpu_hv_synic *synic =to_hv_synic (vcpu);if (!synic->active && (!host || count)) -return 1 ; +return -EINVAL ;trace_kvm_hv_stimer_set_count (hv_stimer_to_vcpu (stimer)->vcpu_id, stimer->index, count, host);
@@ -1380,7 +1380,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,struct kvm_hv *hv =to_kvm_hv (kvm);if (unlikely (!host && !hv_check_msr_access (to_hv_vcpu (vcpu), msr))) -return 1 ; +return -EINVAL ;switch (msr) {case HV_X64_MSR_GUEST_OS_ID :
@@ -1476,23 +1476,23 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,break ;case HV_X64_MSR_TSC_EMULATION_STATUS :if (data && !host) -return 1 ; +return -EINVAL ; hv->hv_tsc_emulation_status = data;break ;case HV_X64_MSR_TIME_REF_COUNT :/* read-only, but still ignore it if host-initiated */ if (!host) -return 1 ; +return -EINVAL ;break ;case HV_X64_MSR_TSC_INVARIANT_CONTROL :/* Only bit 0 is supported */ if (data & ~HV_EXPOSE_INVARIANT_TSC ) -return 1 ; +return -EINVAL ;/* The feature can't be disabled from the guest */ if (!host && hv->hv_invtsc_control && !data) -return 1 ; +return -EINVAL ; hv->hv_invtsc_control = data;break ;
@@ -1501,7 +1501,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,return syndbg_set_msr (vcpu, msr, data, host);default :kvm_pr_unimpl_wrmsr (vcpu, msr, data); -return 1 ; +return -EINVAL ; }return 0 ; }
@@ -1521,7 +1521,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)struct kvm_vcpu_hv *hv_vcpu =to_hv_vcpu (vcpu);if (unlikely (!host && !hv_check_msr_access (hv_vcpu, msr))) -return 1 ; +return -EINVAL ;switch (msr) {case HV_X64_MSR_VP_INDEX : {
@@ -1529,7 +1529,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)u32 new_vp_index = (u32 )data;if (!host || new_vp_index >=KVM_MAX_VCPUS ) -return 1 ; +return -EINVAL ;if (new_vp_index == hv_vcpu->vp_index)return 0 ;
@@ -1555,13 +1555,13 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)if (!(data &HV_X64_MSR_VP_ASSIST_PAGE_ENABLE )) { hv_vcpu->hv_vapic = data;if (kvm_lapic_set_pv_eoi (vcpu,0 ,0 )) -return 1 ; +return -EINVAL ;break ; } gfn = data >>HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT ; addr =kvm_vcpu_gfn_to_hva (vcpu, gfn);if (kvm_is_error_hva (addr)) -return 1 ; +return -EINVAL ;/* * Clear apic_assist portion of struct hv_vp_assist_page
@@ -1569,13 +1569,13 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)* to be preserved e.g. on migration. */ if (put_user (0 , (u32 __user *)addr)) -return 1 ; +return -EFAULT ; hv_vcpu->hv_vapic = data;kvm_vcpu_mark_page_dirty (vcpu, gfn);if (kvm_lapic_set_pv_eoi (vcpu,gfn_to_gpa (gfn) |KVM_MSR_ENABLED ,sizeof (struct hv_vp_assist_page ))) -return 1 ; +return -EINVAL ;break ; }case HV_X64_MSR_EOI :
@@ -1586,7 +1586,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)return kvm_hv_vapic_msr_write (vcpu,APIC_TASKPRI , data);case HV_X64_MSR_VP_RUNTIME :if (!host) -return 1 ; +return -EINVAL ; hv_vcpu->runtime_offset = data -current_task_runtime_100ns ();break ;case HV_X64_MSR_SCONTROL :
@@ -1618,11 +1618,11 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)case HV_X64_MSR_APIC_FREQUENCY :/* read-only, but still ignore it if host-initiated */ if (!host) -return 1 ; +return -EINVAL ;break ;default :kvm_pr_unimpl_wrmsr (vcpu, msr, data); -return 1 ; +return -EINVAL ; }return 0 ;
@@ -1636,7 +1636,7 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,struct kvm_hv *hv =to_kvm_hv (kvm);if (unlikely (!host && !hv_check_msr_access (to_hv_vcpu (vcpu), msr))) -return 1 ; +return -EINVAL ;switch (msr) {case HV_X64_MSR_GUEST_OS_ID :
@@ -1677,7 +1677,7 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,return syndbg_get_msr (vcpu, msr, pdata, host);default :kvm_pr_unimpl_rdmsr (vcpu, msr); -return 1 ; +return -EINVAL ; } *pdata = data;
@@ -1691,7 +1691,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,struct kvm_vcpu_hv *hv_vcpu =to_hv_vcpu (vcpu);if (unlikely (!host && !hv_check_msr_access (hv_vcpu, msr))) -return 1 ; +return -EINVAL ;switch (msr) {case HV_X64_MSR_VP_INDEX :
@@ -1743,7 +1743,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,break ;default :kvm_pr_unimpl_rdmsr (vcpu, msr); -return 1 ; +return -EINVAL ; } *pdata = data;return 0 ;
@@ -1754,10 +1754,10 @@ int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)struct kvm_hv *hv =to_kvm_hv (vcpu->kvm);if (!host && !vcpu->arch.hyperv_enabled) -return 1 ; +return -EINVAL ;if (kvm_hv_vcpu_init (vcpu)) -return 1 ; +return -EINVAL ;if (kvm_hv_msr_partition_wide (msr)) {int r;
@@ -1775,10 +1775,10 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)struct kvm_hv *hv =to_kvm_hv (vcpu->kvm);if (!host && !vcpu->arch.hyperv_enabled) -return 1 ; +return -EINVAL ;if (kvm_hv_vcpu_init (vcpu)) -return 1 ; +return -EINVAL ;if (kvm_hv_msr_partition_wide (msr)) {int r;
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 3/5] KVM/x86: Return -errno instead of "1" for Hyper-V related MSR emulation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Returning negative error codes (e.g., -EINVAL, -EFAULT) from MSR handlers instead of 1 causes KVM…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Returning negative error codes (e.g., -EINVAL, -EFAULT) from MSR handlers instead of 1 causes KVM to abort the VM instead of injecting an architectural #GP exception, creating a guest-triggerable Denial of Service. --
commit 4122a3bbc2071f6540c3f278e39fcef59eeaf6c9
Author: Juergen Gross <[email protected]>
KVM/x86: Return -errno instead of "1" for Hyper-V related MSR emulation
This commit changes the return values for Hyper-V related MSR emulation handlers, replacing the literal "1" with standard negative errno values like -EINVAL and -EFAULT.
arch/x86/kvm/hyperv.c static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, · line 170
Quoted 10 lines
arch/x86/kvm/hyperv.c +1
-1@@ -167,7 +167,7 @@ static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,* allow zero-initing the register from host as well. */ if (vector <HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked) -return 1 ; +return -EINVAL ;
[Severity: High]
In KVM's x86 MSR emulation framework, doesn't returning 1 from MSR handlers signal an emulation failure that should be handled by injecting a General Protection Fault (#GP) into the guest?
Looking at __kvm_emulate_wrmsr(), there is a specific check for negative return values:
if (r < 0)
return r;If we return -EINVAL or -EFAULT here instead of 1, won't the negative value propagate all the way up the call stack (e.g., through kvm_emulate_msr_write and vcpu_run) and terminate the KVM_RUN ioctl with an error?
Since userspace VMMs typically treat unexpected negative returns from KVM_RUN as fatal, could a guest OS use this to intentionally crash its own VM process on the host by performing a WRMSR to an unsupported MSR?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/5 KVM/x86: Return -errno instead of "1" for VMX related MSR emulation
In reply to Juergen GrossInstead of a literal "1" for signalling an error, use a negative errno value in the emulation code of VMX related MSR registers.
---
V2:
- use -errno instead of KVM_MSR_RET_ERR
--- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/pmu_intel.c | 16 +++--- arch/x86/kvm/vmx/tdx.c | 10 ++-- arch/x86/kvm/vmx/vmx.c | 96 ++++++++++++++++++------------------ 4 files changed, 62 insertions(+), 62 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -1
@@ -1611,7 +1611,7 @@ int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) *pdata = msrs->vmfunc_controls;break ;default : -return 1 ; +return -EINVAL ; }return 0 ;
arch/x86/kvm/vmx/pmu_intel.c +8 -8
@@ -362,7 +362,7 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) }else if (intel_pmu_handle_lbr_msrs_access (vcpu, msr_info, true)) {break ; } -return 1 ; +return -EINVAL ; }return 0 ;
@@ -379,14 +379,14 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)switch (msr) {case MSR_CORE_PERF_FIXED_CTR_CTRL :if (data & pmu->fixed_ctr_ctrl_rsvd) -return 1 ; +return -EINVAL ;if (pmu->fixed_ctr_ctrl != data)reprogram_fixed_counters (pmu, data);break ;case MSR_IA32_PEBS_ENABLE :if (data & pmu->pebs_enable_rsvd) -return 1 ; +return -EINVAL ;if (pmu->pebs_enable != data) { diff = pmu->pebs_enable ^ data;
@@ -396,13 +396,13 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ;case MSR_IA32_DS_AREA :if (is_noncanonical_msr_address (data, vcpu)) -return 1 ; +return -EINVAL ; pmu->ds_area = data;break ;case MSR_PEBS_DATA_CFG :if (data & pmu->pebs_data_cfg_rsvd) -return 1 ; +return -EINVAL ; pmu->pebs_data_cfg = data;break ;
@@ -411,7 +411,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {if ((msr &MSR_PMC_FULL_WIDTH_BIT ) && (data & ~pmu->counter_bitmask[KVM_PMC_GP ])) -return 1 ; +return -EINVAL ;if (!msr_info->host_initiated && !(msr &MSR_PMC_FULL_WIDTH_BIT ))
@@ -427,7 +427,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) (pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED)) reserved_bits ^=HSW_IN_TX_CHECKPOINTED ;if (data & reserved_bits) -return 1 ; +return -EINVAL ;if (data != pmc->eventsel) { pmc->eventsel = data;
@@ -439,7 +439,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ; }/* Not a known PMU MSR. */ -return 1 ; +return -EINVAL ; }return 0 ;
arch/x86/kvm/vmx/tdx.c +5 -5
@@ -2158,12 +2158,12 @@ int tdx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)return 0 ;case MSR_IA32_MCG_EXT_CTL :if (!msr->host_initiated && !(vcpu->arch.mcg_cap &MCG_LMCE_P )) -return 1 ; +return -EINVAL ; msr->data = vcpu->arch.mcg_ext_ctl;return 0 ;default :if (!tdx_has_emulated_msr (msr->index)) -return 1 ; +return -EACCES ;return kvm_get_msr_common (vcpu, msr); }
@@ -2175,15 +2175,15 @@ int tdx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)case MSR_IA32_MCG_EXT_CTL :if ((!msr->host_initiated && !(vcpu->arch.mcg_cap &MCG_LMCE_P )) || (msr->data & ~MCG_EXT_CTL_LMCE_EN )) -return 1 ; +return -EINVAL ; vcpu->arch.mcg_ext_ctl = msr->data;return 0 ;default :if (tdx_is_read_only_msr (msr->index)) -return 1 ; +return -EACCES ;if (!tdx_has_emulated_msr (msr->index)) -return 1 ; +return -EACCES ;return kvm_set_msr_common (vcpu, msr); }
arch/x86/kvm/vmx/vmx.c +48 -48
292 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 4/5] KVM/x86: Return -errno instead of "1" for VMX related MSR emulation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Guest-to-Host Denial of Service via MSR Emulation (KVM_RUN ioctl abort) --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Guest-to-Host Denial of Service via MSR Emulation (KVM_RUN ioctl abort) --
commit 68dde399e7fbf1e1f2516727d71c3f6a0bd2fb77
Author: Juergen Gross <[email protected]>
KVM/x86: Return -errno instead of "1" for VMX related MSR emulation
This patch updates VMX MSR emulation routines to return negative errno values instead of a literal "1" when signaling an error.
arch/x86/kvm/vmx/nested.c int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) · line 1614
Quoted 10 lines
arch/x86/kvm/vmx/nested.c +1
-1@@ -1611,7 +1611,7 @@ int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) *pdata = msrs->vmfunc_controls;break ;default : -return 1 ; +return -EINVAL ;
[Severity: High]
Will returning a negative error code here bypass #GP injection and terminate the VM?
In KVM's x86 MSR emulation framework, returning 1 from an MSR handler signals KVM to inject a General Protection Fault (#GP) into the guest.
If this is changed to a negative error like -EINVAL, the error handling path in __kvm_emulate_rdmsr() and __kvm_emulate_wrmsr() treats it as a fatal error:
r = kvm_set_msr(vcpu, msr_index, data);
if (r < 0)
return r;This propagates the negative error code directly up the call stack to vcpu_run(), terminating the KVM_RUN ioctl with an error. Since userspace VMMs like QEMU generally treat KVM_RUN failures as fatal, does this allow any guest to trivially crash its own hypervisor process by performing an invalid MSR access?
Quoted 3 lines
arch/x86/kvm/vmx/nested.c +0
-0}return 0 ;
[ ... ]
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -2359,7 +2359,7 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ;case MSR_IA32_DEBUGCTLMSR :if (!vmx_is_valid_debugctl (vcpu, data, msr_info->host_initiated)) -return 1 ; +return -EINVAL ; data &=vmx_get_supported_debugctl (vcpu, msr_info->host_initiated);
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v2 5/5 KVM/x86: Return -errno instead of "1" for SVM related MSR emulation
In reply to Juergen GrossInstead of a literal "1" for signalling an error, use a negative errno value in the emulation code of SVM related MSR registers.
---
V2:
- use -errno instead of KVM_MSR_RET_ERR
--- arch/x86/kvm/svm/pmu.c | 4 ++-- arch/x86/kvm/svm/svm.c | 36 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 20 deletions(-)
arch/x86/kvm/svm/pmu.c +2 -2
@@ -144,7 +144,7 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; } -return 1 ; +return -EINVAL ; }static int amd_pmu_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info)
@@ -173,7 +173,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; } -return 1 ; +return -EINVAL ; }static void amd_pmu_refresh (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.c +18 -18
@@ -2791,7 +2791,7 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_AMD64_TSC_RATIO :if (!msr_info->host_initiated && !guest_cpu_cap_has (vcpu,X86_FEATURE_TSCRATEMSR )) -return 1 ; +return -EINVAL ; msr_info->data = svm->tsc_ratio_msr;break ;case MSR_STAR :
@@ -2860,7 +2860,7 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_IA32_SPEC_CTRL :if (!msr_info->host_initiated && !guest_has_spec_ctrl_msr (vcpu)) -return 1 ; +return -EINVAL ;if (boot_cpu_has (X86_FEATURE_V_SPEC_CTRL )) msr_info->data = svm->vmcb->save.spec_ctrl;
@@ -2870,7 +2870,7 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_AMD64_VIRT_SPEC_CTRL :if (!msr_info->host_initiated && !guest_cpu_cap_has (vcpu,X86_FEATURE_VIRT_SSBD )) -return 1 ; +return -EINVAL ; msr_info->data = svm->virt_spec_ctrl;break ;
@@ -2907,7 +2907,7 @@ static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)return kvm_complete_insn_gp (vcpu, err);svm_vmgexit_inject_exception (svm,X86_TRAP_GP ); -return 1 ; +return -EINVAL ; }static int svm_set_vm_cr (struct kvm_vcpu *vcpu,u64 data)
@@ -2916,7 +2916,7 @@ static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)int svm_dis, chg_mask;if (data & ~SVM_VM_CR_VALID_MASK ) -return 1 ; +return -EINVAL ; chg_mask =SVM_VM_CR_VALID_MASK ;
@@ -2930,7 +2930,7 @@ static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)/* check for svm_disable while efer.svme is set */ if (svm_dis && (vcpu->arch.efer &EFER_SVME )) -return 1 ; +return -EINVAL ;return 0 ; }
@@ -2952,7 +2952,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)if (!guest_cpu_cap_has (vcpu,X86_FEATURE_TSCRATEMSR )) {if (!msr->host_initiated) -return 1 ; +return -EINVAL ;/* * In case TSC scaling is not enabled, always * leave this MSR at the default value.
@@ -2962,12 +2962,12 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)* Ignore this value as well. */ if (data !=0 && data != svm->tsc_ratio_msr) -return 1 ; +return -EINVAL ;break ; }if (data &SVM_TSC_RATIO_RSVD ) -return 1 ; +return -EINVAL ; svm->tsc_ratio_msr = data;
@@ -2989,10 +2989,10 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)case MSR_IA32_SPEC_CTRL :if (!msr->host_initiated && !guest_has_spec_ctrl_msr (vcpu)) -return 1 ; +return -EINVAL ;if (kvm_spec_ctrl_test_value (data)) -return 1 ; +return -EINVAL ;if (boot_cpu_has (X86_FEATURE_V_SPEC_CTRL )) svm->vmcb->save.spec_ctrl = data;
@@ -3017,10 +3017,10 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)case MSR_AMD64_VIRT_SPEC_CTRL :if (!msr->host_initiated && !guest_cpu_cap_has (vcpu,X86_FEATURE_VIRT_SSBD )) -return 1 ; +return -EINVAL ;if (data & ~SPEC_CTRL_SSBD ) -return 1 ; +return -EINVAL ; svm->virt_spec_ctrl = data;break ;
@@ -3115,7 +3115,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) }if (data &DEBUGCTL_RESERVED_BITS ) -return 1 ; +return -EINVAL ;if (svm->vmcb->save.dbgctl == data)break ;
@@ -3131,7 +3131,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)if (!lbrv)return KVM_MSR_RET_UNSUPPORTED ;if (!msr->host_initiated) -return 1 ; +return -EINVAL ; *svm_vmcb_lbr (svm, ecx) = data;vmcb_mark_dirty (svm->vmcb,VMCB_LBR );break ;
@@ -3143,7 +3143,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)* originating from those kernels. */ if (!msr->host_initiated && !page_address_valid (vcpu, data)) -return 1 ; +return -EINVAL ; svm->nested.hsave_msr = data &PAGE_MASK ;break ;
@@ -3156,10 +3156,10 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)u64 supported_de_cfg;if (svm_get_feature_msr (ecx, &supported_de_cfg)) -return 1 ; +return -EINVAL ;if (data & ~supported_de_cfg) -return 1 ; +return -EINVAL ; svm->msr_decfg = data;break ;
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 5/5] KVM/x86: Return -errno instead of "1" for SVM related MSR emulation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Returning `-EINVAL` instead of `1` from KVM MSR emulation handlers causes the `KVM_RUN` ioctl to…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Returning -EINVAL instead of 1 from KVM MSR emulation handlers causes the KVM_RUN ioctl to abort with an error, allowing a malicious guest to crash the VM. --
commit 3c24eb40043531539adbce28beb46f96bd3b2db7
Author: Juergen Gross <[email protected]>
KVM/x86: Return -errno instead of "1" for SVM related MSR emulation
This commit changes the return value of SVM-related MSR emulation functions from 1 to negative errno values such as -EINVAL.
arch/x86/kvm/svm/pmu.c static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 147
Quoted 10 lines
arch/x86/kvm/svm/pmu.c +1
-1@@ -144,7 +144,7 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; } -return 1 ; +return -EINVAL ;
[Severity: High]
Could this regression allow a guest to crash the VM?
In KVM's x86 MSR emulation framework, returning a positive integer like 1 from MSR getters and setters conventionally indicates a guest-visible fault that should be handled by injecting a General Protection Fault into the guest.
Returning a negative value like -EINVAL signals a fatal host-side emulation error.
For example, if the underlying MSR write function returns a negative value during __kvm_emulate_wrmsr(), it will bypass exception injection:
if (r < 0) return r;
This propagates the negative error code up through the VM exit handler chain and terminates the KVM_RUN ioctl loop.
By returning -EINVAL for guest-induced invalid MSR accesses, could an unprivileged guest intentionally crash the VM instead of receiving a catchable fault?
[ ... ]
Quoted 4 lines
arch/x86/kvm/svm/svm.c +0
-0
[ ... ]
arch/x86/kvm/svm/svm.c static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err) · line 2910
Quoted 6 lines
arch/x86/kvm/svm/svm.c +1
-1@@ -2907,7 +2907,7 @@ static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)return kvm_complete_insn_gp (vcpu, err);svm_vmgexit_inject_exception (svm,X86_TRAP_GP ); -return 1 ; +return -EINVAL ;
[Severity: High]
Does returning -EINVAL here cause a regression for SEV-ES guests handling MSR access faults?
Because this function serves as the complete_emulated_msr callback, returning -EINVAL might guarantee that any MSR access fault triggered by an SEV-ES guest will crash the VM instead of allowing the guest to handle the injected exception.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Quoted 20 lines · Juergen Gross
Get rid of the literal "1" used as general error return value in KVM MSR emulation. It can easily be replaced by negative errno values instead.
This is meant to avoid confusion with the literal "1" used as return value for "return to guest".
Changes in V2:
- series carved out from initial "KVM: Avoid literal numbers as return values" series
- don't use new KVM_MSR_RET_* defines, but 0 and -errnoJuergen Gross (5):
KVM/x86: Change comment before KVM_MSR_RET_* defines KVM/x86: Return -errno instead of "1" for APIC related MSR emulation KVM/x86: Return -errno instead of "1" for Hyper-V related MSR
emulation
KVM/x86: Return -errno instead of "1" for VMX related MSR emulation KVM/x86: Return -errno instead of "1" for SVM related MSR emulation
Quoted 11 lines
arch/x86/kvm/hyperv.c | 72 +++++++++++++-------------- arch/x86/kvm/lapic.c | 39 +++++++-------- arch/x86/kvm/svm/pmu.c | 4 +- arch/x86/kvm/svm/svm.c | 36 +++++++------- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/pmu_intel.c | 16 +++--- arch/x86/kvm/vmx/tdx.c | 10 ++-- arch/x86/kvm/vmx/vmx.c | 96 ++++++++++++++++++------------------ arch/x86/kvm/x86.h | 4 +- 9 files changed, 139 insertions(+), 140 deletions(-)
Oh, sorry, there is another patch. Resending.
Juergen