From patchwork Sat Oct 22 07:20:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7257 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1091599wrr; Sat, 22 Oct 2022 00:51:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7t+mYPlZf0U9omvaST8g8DqVTmufndxgyv+vlSS32dRxLdT+IymqFBXm634EVb2ZwyGTq+ X-Received: by 2002:a05:6a00:1349:b0:563:654d:ce3f with SMTP id k9-20020a056a00134900b00563654dce3fmr23449832pfu.32.1666425074292; Sat, 22 Oct 2022 00:51:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666425074; cv=none; d=google.com; s=arc-20160816; b=VaWAJNuIwbEY1CzFOfpjyqg8kvI8bdGTVCTbq7mWNbwShYxlSBxljSFOJthi2O7RqG c/EffSE+rXK0Tu5DGilC0L8EKzXh8v1Zybw1zze8BuGimjouqpz70VugEg0rUdDVTB/d SMujkRbnqZEMow05JeTf6wCm1YoeDJyJ90BFrkKbsZJ88CXvp4FVTnp66Ayvh9nw9r/x Ro6skkC3qSHHZLeVNQ8GKmPF/hTkjvRJ9u0xwLITnaaz17f+xK3CG9J18vvMn8PaEp2Q f++3UyXhoIDMv8HKghfqPmbz6TUZilMp9Aj5GA9zi0TXX6b0vJguPX59iL9jUAOiMcIf C7Ag== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s33-20020a63ff61000000b0042be0a584cfsi3482690pgk.698.2022.10.22.00.51.01; Sat, 22 Oct 2022 00:51:14 -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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747373338951448192?= X-GMAIL-MSGID: =?utf-8?q?1747373338951448192?= 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; }