From nobody Wed Apr 8 04:47:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD61E3EAC7C for ; Tue, 10 Mar 2026 20:24:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773174266; cv=none; b=ogAp7TKfhgp1FfM5eS+3dSS4kE3kFMV3RLqMxkPKwlXeXyCVr7TjS9YAJUo84i1zFEUKTVVw/gtedA+AF5P8qxtd2tx3I0AaNIY/N4RZcbvUpqml0EtUSOJSJpqMzNpdcxgeOUDFkDEAjc6ZJpgE0lGtuCYWgsTercWUNnQZDF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773174266; c=relaxed/simple; bh=JJO5qewKWIqGbxpfcpQoE00DHR/fbyp+xQU2r4R6u5Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D4cECKHYn+qsf3J/SNhamG5JJwgC9Yd2W8SMZZwN8ih7bcP+fsBU4yMaATdHlDRDK137Lh6EjXaNxNGbdbxEom6BkSF0g+AMGWTTJ54coGEnwB85ruWtEflugZ0JayPmft1TQ2PsoOZzPPUbOBNVMd7CmDDDZBIMkWvpIMy2obw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=R/mg7vTN; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=ENN7Aunw; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="R/mg7vTN"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="ENN7Aunw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1773174263; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BJ85Lkzx3nSPKz8vbaVq+hpymVq+1ScaQgtLPpniKs4=; b=R/mg7vTNunwmphze+aKEUqx9asam6I/erYAcRCWIPVSit/NfPCzd/ACZGFHH48KBHT3Iu2 beh6B0cu1qVtqKh5PZAZAGJe4lxYKRZm0YHDJ4o5f1PJyvb8Kbkj0VcTkJaTYCOwMtil8I VzGL8xYf83XO5SYwXhPmcTghFKsozMY= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-443-W6ElGCsuPyqCFuxfi_ejeA-1; Tue, 10 Mar 2026 16:24:21 -0400 X-MC-Unique: W6ElGCsuPyqCFuxfi_ejeA-1 X-Mimecast-MFC-AGG-ID: W6ElGCsuPyqCFuxfi_ejeA_1773174260 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-439c54e0f6aso186540f8f.0 for ; Tue, 10 Mar 2026 13:24:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1773174259; x=1773779059; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BJ85Lkzx3nSPKz8vbaVq+hpymVq+1ScaQgtLPpniKs4=; b=ENN7AunwnuoxQNdjN9tfHrr0HKXiET+Gl7YqtAzhncsOaua049ULloIia0dTfg2/pH Vnc6gSGuBaQJ6zb3nYNtPYauC9VO1vlivLVJ3XvDN/RKpWFIokA/TDaiw81jnmgElGGt APn4S6+w8+WGLIFyST3Q7Mlg7oqPYFcHfksrab5/mfn+nS24laPkl1e/FrNNvIqyIzeA q+iZxg5NoCbp49RZD8ywG1ToovQaASe8xQ/AOSRil/qjQdHzgcMiveHtlcQKU8Dg4JCG tP/n6DoMmLEzu5nbGwjh10FQhlP1wXkeXqzrDxTBmmPHcquChGlnGKU7Uk/sBkof8/TV QLkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773174259; x=1773779059; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=BJ85Lkzx3nSPKz8vbaVq+hpymVq+1ScaQgtLPpniKs4=; b=dRmomdqndfiLuiW73OzQFJ/HcwIoVW9m0XIsKQQc8iypgQf5Anco06LMI1tm2fdwMx fN95MBiwRQL04NJSNPebV1+I4IgutUv6FTWR4/5EilBYVoAQcK4cIu7R7+2eFLvTf2TF +oi+qoyFxbcPkOlDodDhJD2R25hC3NuYaFPH0xVxjEHJH0xvI78/lYl8h8+dJY5lAEHw f+1AvWcgZVEppIptoLiF9ff6NHK1n1r/fuwYsGPcNlpcihdhgA60GaBdNYkDuQjQq1Tj cPFZxrCQAi10AXtxaShwt2RaPuFV+mW7815VTdZBhX0Rmn2iTXTv8BCEk7109xfxvAKh 9iKQ== X-Gm-Message-State: AOJu0YwuodNo9IONFp7sQ33pWIflv5kqz1J/Jaqutw0TpiVpJ1psfyya R79GyoM5TXEG+uzbVVlkWhx60H4DXW8tyMOM5t7HrwADbBDgZMgoMyE6Tuauab3zzd/YmmMkSj+ 8w9K/NdAW1Aakme6Do+ZLABeYACwA/d9e9u1T3igV0bl7aqDFnPRx7ERz74oXgfGgNp4WYyu5Nb 9AfG8jmDExJNCyQxpcgD+gK/DiaLa5cL7YRjYkGPW37W5LV1Zdag== X-Gm-Gg: ATEYQzy6Q5+0GL9uuTYiuYjoH+PHRT0IZSFErpMTYeV0JiOqlL7MtrLuo1VfdCmHkXF SlhjENQ9Di3z9C3omYak3hbveh7dzKu7RvZCcf+8pyenDia+G6Pfjj75UU1CYOJcWO8VONmmbJV xcnRoRzAm2E/eX8zjx0sMiqNlJao9dkoKWcUOgHSquoWVQdutRWovAX5OgLsunervebqkYyrUG6 dcyst3TJAalZHFHpgDbQnD5GK9MbIOxV10Ksf9DeTRy3Ly9kSL0CorjDBc5PUBKR7UYUN9oOn44 6kt2zRJa9Ox7B0iESNJZFsEVhw5g3CFk2Z33Goulsr36VTdKp/a6+bAOun5f17nmdZMlwYA5czu s9Uutb3JDcfqVqpBg4iMzvCcxys56IYV9dofIp6iJJh8kp2dKTb28qn6MjyptyETEqlGVrF9fwy nQjBmogwTHdzuhdM7Joa+54bwG8l8= X-Received: by 2002:a05:6000:2908:b0:439:b6b3:faa7 with SMTP id ffacd0b85a97d-439f8c10e3dmr43768f8f.28.1773174259430; Tue, 10 Mar 2026 13:24:19 -0700 (PDT) X-Received: by 2002:a05:6000:2908:b0:439:b6b3:faa7 with SMTP id ffacd0b85a97d-439f8c10e3dmr43703f8f.28.1773174258827; Tue, 10 Mar 2026 13:24:18 -0700 (PDT) Received: from [192.168.10.48] ([151.95.144.138]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439f81a3638sm621443f8f.9.2026.03.10.13.24.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2026 13:24:16 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, xinyang@anthropic.com, stable@vger.kernel.org Subject: [PATCH 1/5] KVM: VMX: check validity of VMCS controls when returning from SMM Date: Tue, 10 Mar 2026 21:24:10 +0100 Message-ID: <20260310202414.406078-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260310202414.406078-1-pbonzini@redhat.com> References: <20260310202414.406078-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The VMCS12 is not available while in SMM. However, it can be overwritten if userspace manages to trigger copy_enlightened_to_vmcs12() - for example via KVM_GET_NESTED_STATE. Because of this, the VMCS12 has to be checked for validity before it is used to generate the VMCS02. Move the check code out of vmx_set_nested_sta= te() (the other "not a VMLAUNCH/VMRESUME" path that emulates a nested vmentry) and reuse it in vmx_leave_smm(). Cc: stable@vger.kernel.org Reported-by: Xinyang Ge Signed-off-by: Paolo Bonzini --- arch/x86/kvm/vmx/nested.c | 39 +++++++++++++++++++++++++++------------ arch/x86/kvm/vmx/nested.h | 1 + arch/x86/kvm/vmx/vmx.c | 4 ++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index cb925cc53389..d4bc47079809 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -6849,13 +6849,34 @@ void vmx_leave_nested(struct kvm_vcpu *vcpu) free_nested(vcpu); } =20 +int nested_vmx_check_restored_vmcs12(struct kvm_vcpu *vcpu) +{ + enum vm_entry_failure_code ignored; + struct vmcs12 *vmcs12 =3D get_vmcs12(vcpu); + + if (nested_cpu_has_shadow_vmcs(vmcs12) && + vmcs12->vmcs_link_pointer !=3D INVALID_GPA) { + struct vmcs12 *shadow_vmcs12 =3D get_shadow_vmcs12(vcpu); + + if (shadow_vmcs12->hdr.revision_id !=3D VMCS12_REVISION || + !shadow_vmcs12->hdr.shadow_vmcs) + return -EINVAL; + } + + if (nested_vmx_check_controls(vcpu, vmcs12) || + nested_vmx_check_host_state(vcpu, vmcs12) || + nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) + return -EINVAL; + + return 0; +} + static int vmx_set_nested_state(struct kvm_vcpu *vcpu, struct kvm_nested_state __user *user_kvm_nested_state, struct kvm_nested_state *kvm_state) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); struct vmcs12 *vmcs12; - enum vm_entry_failure_code ignored; struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =3D &user_kvm_nested_state->data.vmx[0]; int ret; @@ -6986,25 +7007,20 @@ static int vmx_set_nested_state(struct kvm_vcpu *vc= pu, vmx->nested.mtf_pending =3D !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); =20 - ret =3D -EINVAL; if (nested_cpu_has_shadow_vmcs(vmcs12) && vmcs12->vmcs_link_pointer !=3D INVALID_GPA) { struct vmcs12 *shadow_vmcs12 =3D get_shadow_vmcs12(vcpu); =20 + ret =3D -EINVAL; if (kvm_state->size < sizeof(*kvm_state) + sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) goto error_guest_mode; =20 + ret =3D -EFAULT; if (copy_from_user(shadow_vmcs12, user_vmx_nested_state->shadow_vmcs12, - sizeof(*shadow_vmcs12))) { - ret =3D -EFAULT; - goto error_guest_mode; - } - - if (shadow_vmcs12->hdr.revision_id !=3D VMCS12_REVISION || - !shadow_vmcs12->hdr.shadow_vmcs) + sizeof(*shadow_vmcs12))) goto error_guest_mode; } =20 @@ -7015,9 +7031,8 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu, kvm_state->hdr.vmx.preemption_timer_deadline; } =20 - if (nested_vmx_check_controls(vcpu, vmcs12) || - nested_vmx_check_host_state(vcpu, vmcs12) || - nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) + ret =3D nested_vmx_check_restored_vmcs12(vcpu); + if (ret < 0) goto error_guest_mode; =20 vmx->nested.dirty_vmcs12 =3D true; diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h index b844c5d59025..213a448104af 100644 --- a/arch/x86/kvm/vmx/nested.h +++ b/arch/x86/kvm/vmx/nested.h @@ -22,6 +22,7 @@ void nested_vmx_setup_ctls_msrs(struct vmcs_config *vmcs_= conf, u32 ept_caps); void nested_vmx_hardware_unsetup(void); __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcp= u *)); void nested_vmx_set_vmcs_shadowing_bitmap(void); +int nested_vmx_check_restored_vmcs12(struct kvm_vcpu *vcpu); void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu); enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *v= cpu, bool from_vmentry); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 48f0e426a8a2..e9fa59e92548 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8540,6 +8540,10 @@ int vmx_leave_smm(struct kvm_vcpu *vcpu, const union= kvm_smram *smram) } =20 if (vmx->nested.smm.guest_mode) { + /* Triple fault if the state is invalid. */ + if (nested_vmx_check_restored_vmcs12(vcpu) < 0) + return 1; + ret =3D nested_vmx_enter_non_root_mode(vcpu, false); if (ret) return ret; --=20 2.53.0