From nobody Mon Nov 23 22:28:35 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1794458886; cv=none; d=zohomail.com; s=zohoarc; b=MP7g2tJFkcZGUTFBY8hDTR4VUNYDwzPHc6QcUiOhWWG4LGgZooXezzyRx9cXTVKwBqUgDkXRPFd7PkpMtrrSbw/Jzj4PjCIF7buD/fegJC1zcSZ73txoOKBcOrWWmhU5hcqWRZHjxxdk5jZtpOTC53w2TokseUPqx3keLyJM9Ss= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794458886; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2+dJk09S+6bQtbHtJHd1AoWeYtEP5tfHh8DUksgsO7k=; b=j7qfshP0nBxPNivZIzT3dDCZNSjyChxU0LHlfu9r9P3bQujWSNlNrJIqX8EHcrWfKpy4IRyKH1AwTHRfhAnnLFKXoN1wLjusu4qEtKrWgBiNa7+/0LAmsRNEwrwEYCs+USPvmiYZahvFGwycQRaTFSEFT2QSZHYHx8HHaHhwKkU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179445888664338.389143891838785; Wed, 11 Nov 2026 20:48:06 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1485090.1688395 (Exim 4.92) (envelope-from ) id 1xRMih-0004Co-5B; Thu, 12 Nov 2026 04:47:31 +0000 Received: by outflank-mailman (output) from mailman id 1485090.1688395; Thu, 12 Nov 2026 04:47:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xRMih-0004Cf-10; Thu, 12 Nov 2026 04:47:31 +0000 Received: by outflank-mailman (input) for mailman id 1485090; Thu, 12 Nov 2026 04:47:30 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xRMif-0004CZ-UG for xen-devel@lists.xenproject.org; Thu, 12 Nov 2026 04:47:30 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xRMie-00Cb1g-Sz for xen-devel@lists.xenproject.org; Thu, 12 Nov 2026 05:47:28 +0100 Received: from [10.42.69.7] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6af544d8-e002-0a2a0a5209dd-0a2a4507e70a-12 for ; Thu, 12 Nov 2026 05:47:28 +0100 Received: from [209.85.215.179] (helo=mail-pg1-f179.google.com) by tlsNG-ef75cf.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6af544de-b4ea-0a2a45070019-d155d7b3d822-3 for ; Thu, 12 Nov 2026 05:47:28 +0100 Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-cd62c570b5dso2355673a12.2 for ; Wed, 11 Nov 2026 20:47:27 -0800 (PST) Received: from cs-arch-srv04.cmpt.sfu.ca (cs-arch-srv04.cmpt.sfu.ca. [142.58.10.48]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cdd8d79d406sm2187387a12.17.2026.11.11.20.47.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Nov 2026 20:47:25 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794458846; x=1795063646; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=2+dJk09S+6bQtbHtJHd1AoWeYtEP5tfHh8DUksgsO7k=; b=pNr960ls40CoYzvJw8ro9bbRfEYhGz9yeIwWMAnRnYMMEE9shLic2WVKh8Wl38aZ+z AC2UXnYs3UmTNd2fr8jvOVAMpI7A79/3Opxth/qJ8ujE59AS4E813qhAM5/UFBN5pwBJ F3UaftDc6n2hOv3pqhODpJ07YSp9uaPZIJwcLVGq8mGSHYuw+f4HZItL2ORYztdikkCr 2cH25VrVyfkmJJPedmvYTqMV8Xto+0cbCIIQVr2RxyPIyKiDa5+tBEH9W/Dkld+L9IQd 1aupQhzbWxUnlPX956hhwsAax1jcqq+trcERZ84R2F9L8Z9GnBb/Bs81PCC9O589gSZR yVjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794458846; x=1795063646; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2+dJk09S+6bQtbHtJHd1AoWeYtEP5tfHh8DUksgsO7k=; b=EmVOcoV8ZmaQhiMDbxkYsS38bfOUcPW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UywCDUNluGDfclAcl862+8mFWN7aQn9M8WIi7a8xCNaJ64 3vqXsG2fpb4odXC3SfXXMyxcNkxAcS0mfCJz3BAibWMkz/ZtV070XLt8OPlw2k26oEyQXNVH0Hb 7Cowez7ntWCAjq0cUXvcGE00veH42sFEYR X-Received: by 2002:a05:6372:a709:b0:3f1:fdd4:572b with SMTP id adf61e73a8af0-3f2fda991f5mr4616390637.36.1794458846002; Wed, 11 Nov 2026 20:47:26 -0800 (PST) From: Mahmoud Abumandour To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Mahmoud Abumandour Subject: [PATCH v2] x86/nestedhvm: process the MSR-load and MSR-store areas of L1's VMCS Date: Wed, 11 Nov 2026 20:47:24 -0800 Message-Id: <20261112044724.1039961-1-ma.mandourr@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ef75cf/1794458848-36CDFAE4-8452A79A/0/0 X-purgate-type: clean X-purgate-size: 10663 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1794458887826158500 Content-Type: text/plain; charset="utf-8" Nothing in vvmx.c reads the VM-entry MSR-load, VM-exit MSR-store or VM-exit MSR-load area of L1's VMCS, so L2 runs with the listed MSRs as the previous L2 or L1 left them, and L1 gets neither L2's values stored nor its own loaded back. Process each area where the SDM does, with the SDM's checks, an entry as a WRMSR or RDMSR at CPL 0 that L1 does not intercept. A bad VM-entry entry fails the VM entry with exit reason 34 and leaves the VMCS unlaunched, a bad VM-exit entry is a VMX abort, which crashes the domain, and IA32_VMX_MISC now recommends the 512 entries an area may hold. Fixes: 9390642b7fa5 ("Nested VMX: Switch shadow/virtual VMCS between n1/n2 = guests.") Assisted-by: Claude:claude-opus-5-5 --- Changes in v2: - use paddr_t and unsigned int in msr_area_valid() (Jan) - reword process_msr_area()'s comment (Jan) Found by a fuzzer for VMX isolation. A VMX abort crashes the domain as vvmx.c does elsewhere when nested VMX cannot continue. Alternatives: hvm_triple_fault(), which applies the domain's triple-fault action (reboot by default), as KVM does, or hvm_vcpu_down() on the aborting vCPU alone with the VMX-abort indicator written, which is closest to the SDM but needs more code. Happy to switch to either. xen/arch/x86/hvm/vmx/vvmx.c | 136 ++++++++++++++++++++---- xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 1 + 2 files changed, 118 insertions(+), 19 deletions(-) diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c index 3309432873..dc5f4b7bfe 100644 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -23,6 +23,8 @@ static DEFINE_PER_CPU(u64 *, vvmcs_buf); =20 static void nvmx_purge_vvmcs(struct vcpu *v); +static int set_vvmcs_launched(struct list_head *launched_list, + unsigned long vvmcs_mfn); =20 static bool nvmx_vcpu_in_vmx(const struct vcpu *v) { @@ -1212,11 +1214,68 @@ static void nvmx_clear_vmcs_pointer(struct vcpu *v,= struct vmcs_struct *vvmcs) __vmwrite(VMWRITE_BITMAP, 0); } =20 -static void virtual_vmentry(struct cpu_user_regs *regs) +/* The longest MSR list that IA32_VMX_MISC recommends to L1. */ +#define NVMX_MAX_MSRS 512 + +/* A nonempty MSR area must be 16-byte aligned and within MAXPHYADDR. */ +static bool msr_area_valid(const struct vcpu *v, unsigned int addr_field, + unsigned int count_field) +{ + paddr_t addr =3D get_vvmcs(v, addr_field); + unsigned int count =3D get_vvmcs(v, count_field); + unsigned int width =3D v->domain->arch.cpuid->extd.maxphysaddr; + + return !count || + (!(addr & 0xf) && !(addr >> width) && + !((addr + count * sizeof(struct vmx_msr_entry) - 1) >> width)); +} + +/* + * Process an MSR load or store area of L1's VMCS, each entry as an + * unintercepted WRMSR or RDMSR. Returns 0, or the 1-based index of the + * failing entry. + */ +static unsigned int process_msr_area(struct vcpu *v, unsigned int addr_fie= ld, + unsigned int count_field) +{ + paddr_t addr =3D get_vvmcs(v, addr_field); + unsigned int i, count =3D get_vvmcs(v, count_field); + struct vmx_msr_entry ent; + + for ( i =3D 0; i < count; i++, addr +=3D sizeof(ent) ) + { + if ( i =3D=3D NVMX_MAX_MSRS || + hvm_copy_from_guest_phys(&ent, addr, + sizeof(ent)) !=3D HVMTRANS_okay || + ent.mbz || + (ent.index >=3D MSR_X2APIC_FIRST && ent.index <=3D MSR_X2APIC= _LAST) ) + return i + 1; + + if ( addr_field =3D=3D VM_EXIT_MSR_STORE_ADDR ) + { + if ( hvm_msr_read_intercept(ent.index, &ent.data) !=3D X86EMUL= _OKAY || + hvm_copy_to_guest_phys(addr + offsetof(struct vmx_msr_ent= ry, + data), + &ent.data, sizeof(ent.data), + v) !=3D HVMTRANS_okay ) + return i + 1; + } + else if ( ent.index =3D=3D MSR_FS_BASE || ent.index =3D=3D MSR_GS_= BASE || + hvm_msr_write_intercept(ent.index, ent.data, + false) !=3D X86EMUL_OKAY ) + return i + 1; + } + + return 0; +} + +/* Returns 0, or the 1-based index of the failing MSR-load entry. */ +static unsigned int virtual_vmentry(struct cpu_user_regs *regs) { struct vcpu *v =3D current; struct nestedvcpu *nvcpu =3D &vcpu_nestedhvm(v); unsigned long lm_l1, lm_l2; + unsigned int msr_fail; =20 vmx_vmcs_switch(v->arch.hvm.vmx.vmcs_pa, nvcpu->nv_n2vmcx_pa); =20 @@ -1277,6 +1336,15 @@ static void virtual_vmentry(struct cpu_user_regs *re= gs) } } =20 + /* MSRs load last, and only a successful entry launches the VMCS. */ + msr_fail =3D process_msr_area(v, VM_ENTRY_MSR_LOAD_ADDR, + VM_ENTRY_MSR_LOAD_COUNT); + if ( !msr_fail && + set_vvmcs_launched(&vcpu_2_nvmx(v).launched_list, + PFN_DOWN(v->arch.hvm.vmx.vmcs_shadow_maddr)) <= 0 ) + domain_crash(v->domain); + + return msr_fail; } =20 static void sync_vvmcs_guest_state(struct vcpu *v, struct cpu_user_regs *r= egs) @@ -1454,19 +1522,37 @@ static void nvmx_update_apicv(struct vcpu *v) } } =20 -static void virtual_vmexit(struct cpu_user_regs *regs) +/* + * A VM exit, or with msr_fail a VM entry that failed loading MSRs, which + * records only the exit reason and qualification and saves nothing. + */ +static void virtual_vmexit(struct cpu_user_regs *regs, unsigned int msr_fa= il) { struct vcpu *v =3D current; struct nestedvcpu *nvcpu =3D &vcpu_nestedhvm(v); - unsigned long lm_l1, lm_l2; + unsigned long lm_l1, lm_l2, intr_info =3D 0; =20 - sync_vvmcs_ro(v); - sync_vvmcs_guest_state(v, regs); - sync_exception_state(v); + if ( msr_fail ) + { + intr_info =3D get_vvmcs(v, VM_ENTRY_INTR_INFO); + set_vvmcs(v, VM_EXIT_REASON, + VMX_EXIT_REASONS_FAILED_VMENTRY | EXIT_REASON_MSR_LOADIN= G); + set_vvmcs(v, EXIT_QUALIFICATION, msr_fail); + } + else + { + sync_vvmcs_ro(v); + sync_vvmcs_guest_state(v, regs); + sync_exception_state(v); =20 - if ( nvmx_ept_enabled(v) && hvm_pae_enabled(v) && - !hvm_long_mode_active(v) ) - shadow_to_vvmcs_bulk(v, ARRAY_SIZE(gpdpte_fields), gpdpte_fields); + if ( nvmx_ept_enabled(v) && hvm_pae_enabled(v) && + !hvm_long_mode_active(v) ) + shadow_to_vvmcs_bulk(v, ARRAY_SIZE(gpdpte_fields), gpdpte_fiel= ds); + + if ( process_msr_area(v, VM_EXIT_MSR_STORE_ADDR, + VM_EXIT_MSR_STORE_COUNT) ) + domain_crash(v->domain, "%pv VMX abort: storing MSRs failed\n"= , v); + } =20 /* This will clear current pCPU bit in p2m->dirty_cpumask */ np2m_schedule(NP2M_SCHEDLE_OUT); @@ -1490,6 +1576,9 @@ static void virtual_vmexit(struct cpu_user_regs *regs) vmx_update_exception_bitmap(v); =20 load_vvmcs_host_state(v); + /* A failed VM entry keeps its interruption information. */ + if ( msr_fail ) + set_vvmcs(v, VM_ENTRY_INTR_INFO, intr_info); =20 if ( lm_l1 !=3D lm_l2 ) paging_update_paging_modes(v); @@ -1499,6 +1588,9 @@ static void virtual_vmexit(struct cpu_user_regs *regs) /* VM exit clears all bits except bit 1 */ regs->rflags =3D X86_EFLAGS_MBS; =20 + if ( process_msr_area(v, VM_EXIT_MSR_LOAD_ADDR, VM_EXIT_MSR_LOAD_COUNT= ) ) + domain_crash(v->domain, "%pv VMX abort: loading MSRs failed\n", v); + if ( cpu_has_vmx_virtual_intr_delivery ) nvmx_update_apicv(v); =20 @@ -1553,9 +1645,14 @@ void asmlinkage nvmx_switch_guest(void) return; =20 if ( nestedhvm_vcpu_in_guestmode(v) && nvcpu->nv_vmexit_pending ) - virtual_vmexit(regs); + virtual_vmexit(regs, 0); else if ( !nestedhvm_vcpu_in_guestmode(v) && nvcpu->nv_vmentry_pending= ) - virtual_vmentry(regs); + { + unsigned int msr_fail =3D virtual_vmentry(regs); + + if ( msr_fail ) + virtual_vmexit(regs, msr_fail); + } } =20 /* @@ -1704,6 +1801,11 @@ static enum vmx_insn_errno nvmx_vmresume(struct vcpu= *v) goto invalid_control_state; } =20 + if ( !msr_area_valid(v, VM_EXIT_MSR_STORE_ADDR, VM_EXIT_MSR_STORE_COUN= T) || + !msr_area_valid(v, VM_EXIT_MSR_LOAD_ADDR, VM_EXIT_MSR_LOAD_COUNT)= || + !msr_area_valid(v, VM_ENTRY_MSR_LOAD_ADDR, VM_ENTRY_MSR_LOAD_COUN= T) ) + goto invalid_control_state; + nvcpu->nv_vmentry_pending =3D 1; =20 return VMX_INSN_SUCCEED; @@ -1777,15 +1879,10 @@ static int nvmx_handle_vmlaunch(struct cpu_user_reg= s *regs) return X86EMUL_OKAY; } else { + /* virtual_vmentry() launches the VMCS. */ rc =3D nvmx_vmresume(v); if ( rc ) vmfail_valid(regs, rc); - else - { - if ( set_vvmcs_launched(&nvmx->launched_list, - PFN_DOWN(v->arch.hvm.vmx.vmcs_shadow_m= addr)) < 0 ) - return X86EMUL_UNHANDLEABLE; - } rc =3D X86EMUL_OKAY; } return rc; @@ -2354,8 +2451,9 @@ int nvmx_msr_read_intercept(unsigned int msr, u64 *ms= r_content) data =3D hvm_cr4_guest_valid_bits(d); break; case MSR_IA32_VMX_MISC: - /* Do not support CR3-targets or activity states. */ - data =3D host_data & ~(VMX_MISC_CR3_TARGET | VMX_MISC_ACTIVITY_MAS= K); + /* No CR3-targets or activity states; MSR lists of NVMX_MAX_MSRS. = */ + data =3D host_data & ~(VMX_MISC_CR3_TARGET | VMX_MISC_ACTIVITY_MAS= K | + VMX_MISC_MAX_MSRS); break; case MSR_IA32_VMX_EPT_VPID_CAP: data =3D nept_get_ept_vpid_cap(); diff --git a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h b/xen/arch/x86/include= /asm/hvm/vmx/vmcs.h index d0716e97d2..24b502382f 100644 --- a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h +++ b/xen/arch/x86/include/asm/hvm/vmx/vmcs.h @@ -304,6 +304,7 @@ void vmx_vmcs_reload(struct vcpu *v); #define VMX_MISC_ACTIVITY_MASK 0x000001c0 #define VMX_MISC_PROC_TRACE 0x00004000 #define VMX_MISC_CR3_TARGET 0x01ff0000 +#define VMX_MISC_MAX_MSRS 0x0e000000 #define VMX_MISC_VMWRITE_ALL 0x20000000 =20 #define VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL --=20 2.25.1