Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3879864rwi; Sat, 22 Oct 2022 01:27:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4ZkGloLmBqtpOtk0VVPpUxx8dbCuwfvGSs0/fFUonrzUT4nkfDYqwqIcZUPePGxXng5j7t X-Received: by 2002:a17:902:a508:b0:181:2875:3b09 with SMTP id s8-20020a170902a50800b0018128753b09mr22745188plq.45.1666427235046; Sat, 22 Oct 2022 01:27:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666427235; cv=none; d=google.com; s=arc-20160816; b=WHC+LbNpktAvpAUAJa7Fprz+6RED1nulbmjAf4TN+bUbsKTsIS8FaOc471N4a+SSBQ AEc5zRE5iLcZ9sYHCNWzfbZSZe4fO8z7sAxXIrbMccP5Jh7VdTUY+vB9z3cPgo9Z/2WZ Pam0fXHTt/7M5L3PFJCxso5V/J3JpQafx8O3mr33ao6JJZI3TRNdzQVKRZR2i0gg9spe q/OPb0kMhxq0a9zvqNpyiFvWKhfqS2HjGLECxI2AIEyitjOb+uMqtcpsVCBd3dmtERtu ZMVu/8g8/Joy8W3bqi2QL5j2j0w0m+pgLeuhPj2zFUdCNAC+ZKHqk3UUtyLMoukFTzEx /iSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=FAGnRwHG0pysocHU3NR6q1lqf1Nu6WgLzrgc1rKE49Y=; b=Y8oVeUVQH61ASRW+0qGqkbdmO550tZRYSNpHLp2vkkTQ2/sxOa9BKcZqzkqB7/OR9g za5oAP4SqJNJckuzLSyCmDCGesCjeUqiyMC6pw9lbCcG68LcQES6uuQUKC013QByDT7y oCaLIuy/hDfY0pBtgUb+sGkqQDo34WHVSmL+TfVgd7tzLbiubtnU05tM19rSkIUhmZkr W8oulfbA14UxdGQIihetlfO9VKFWna9wsZ54V1GsZp3aUAm5Uj4g/wi5/Zc71qrjdwmx xHL23dDQy6vLw5VXtBI+cs/r08zxAK2qZ+9Solu5iBZeZdNt47rFmNt59JVNdOpXyuLB euXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TSCZeDdy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j189-20020a6380c6000000b0046ec3bdd048si3834699pgd.484.2022.10.22.01.27.03; Sat, 22 Oct 2022 01:27:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TSCZeDdy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231696AbiJVHu0 (ORCPT + 99 others); Sat, 22 Oct 2022 03:50:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231654AbiJVHs4 (ORCPT ); Sat, 22 Oct 2022 03:48:56 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F37021D8F00; Sat, 22 Oct 2022 00:45:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4135860B89; Sat, 22 Oct 2022 07:41:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52D61C433C1; Sat, 22 Oct 2022 07:41:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424503; bh=8OPJ/E2siWg3Jv6ZeX2QBTMFA8a9q7dFLzoDaRqiBWo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TSCZeDdyCxKvSdRt/BqubYuCIeLsB+Y91R8GluW/oTrt6IomGexr2RhfhA8+yVz9f uF/UFdLypNk+YbQ71s7S9eA1fL4HOUiYzrstDjQ+8+gzv28otNGfObhVGi1sJPI1ps aO0OZJ9MX390ssly80KZYiAi+b9QD2JOpW3fiXG0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Jim Mattson , Maxim Levitsky , Paolo Bonzini Subject: [PATCH 5.19 163/717] KVM: VMX: Drop bits 31:16 when shoving exception error code into VMCS Date: Sat, 22 Oct 2022 09:20:42 +0200 Message-Id: <20221022072444.312351160@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit eba9799b5a6efe2993cf92529608e4aa8163d73b upstream. Deliberately truncate the exception error code when shoving it into the VMCS (VM-Entry field for vmcs01 and vmcs02, VM-Exit field for vmcs12). Intel CPUs are incapable of handling 32-bit error codes and will never generate an error code with bits 31:16, but userspace can provide an arbitrary error code via KVM_SET_VCPU_EVENTS. Failure to drop the bits on exception injection results in failed VM-Entry, as VMX disallows setting bits 31:16. Setting the bits on VM-Exit would at best confuse L1, and at worse induce a nested VM-Entry failure, e.g. if L1 decided to reinject the exception back into L2. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Reviewed-by: Jim Mattson Reviewed-by: Maxim Levitsky Link: https://lore.kernel.org/r/20220830231614.3580124-3-seanjc@google.com Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/vmx/nested.c | 11 ++++++++++- arch/x86/kvm/vmx/vmx.c | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3839,7 +3839,16 @@ static void nested_vmx_inject_exception_ u32 intr_info = nr | INTR_INFO_VALID_MASK; if (vcpu->arch.exception.has_error_code) { - vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; + /* + * Intel CPUs do not generate error codes with bits 31:16 set, + * and more importantly VMX disallows setting bits 31:16 in the + * injected error code for VM-Entry. Drop the bits to mimic + * hardware and avoid inducing failure on nested VM-Entry if L1 + * chooses to inject the exception back to L2. AMD CPUs _do_ + * generate "full" 32-bit error codes, so KVM allows userspace + * to inject exception error codes with bits 31:16 set. + */ + vmcs12->vm_exit_intr_error_code = (u16)vcpu->arch.exception.error_code; intr_info |= INTR_INFO_DELIVER_CODE_MASK; } --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1687,7 +1687,17 @@ static void vmx_queue_exception(struct k kvm_deliver_exception_payload(vcpu); if (has_error_code) { - vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); + /* + * Despite the error code being architecturally defined as 32 + * bits, and the VMCS field being 32 bits, Intel CPUs and thus + * VMX don't actually supporting setting bits 31:16. Hardware + * will (should) never provide a bogus error code, but AMD CPUs + * do generate error codes with bits 31:16 set, and so KVM's + * ABI lets userspace shove in arbitrary 32-bit values. Drop + * the upper bits to avoid VM-Fail, losing information that + * does't really exist is preferable to killing the VM. + */ + vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, (u16)error_code); intr_info |= INTR_INFO_DELIVER_CODE_MASK; }