From patchwork Wed Oct 19 08:24:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 4583 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp205876wrs; Wed, 19 Oct 2022 01:50:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7W1U831bnMd/Qd6Vxf+dJZg3Jqm7yBGoMlAAyhhy3hDRq5/opGDfazDr/39VKY58WtjqTD X-Received: by 2002:a17:902:b70c:b0:179:eb79:cf9a with SMTP id d12-20020a170902b70c00b00179eb79cf9amr7347851pls.130.1666169453717; Wed, 19 Oct 2022 01:50:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666169453; cv=none; d=google.com; s=arc-20160816; b=SRAliT3KhAxDcQTFopx/kK/XGCdhtaRAqo/YA/61SECGPF6Wh9YqEnB6G0IC05JFQy YE6NHXi3teM806O1lOrk5TZ5JSfzfuuiCdc6JMYQBht1cNDR/XYa52cv8Oh8G+mrceCa gBUqyGvicG+KTjxokon+vBVE5xJIrvV+lQuTm9CkX1/i/Gs1rvUNNtQEWWgobv5i4H8c TATVy2934CYTl1Etv08gUdvl5PKzXhHTMFZL3ECpjtAc+HW95/zLyZ8fMdFylRq+SEFN wgKmaRJeeP+YCMZ+eYD7chMhCPBZ+k8tQO9gMMVZ+eRysynyMfbuYMHiXU2VlL2Zwp/b PT4g== 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=JRpCR5rG+1qR4olGxnoMihKhmN6RI7KC/2EluXG+l2Q=; b=cQchDB0xSYmeVQtjW37HKJBo0KohMDdziQIDGeEGtzasVfJwo+y9vtxOVVSVAbPLhP uCvZBMH5WTSL0w8fX46I/xvnq8Kkzo7J4hzVTC68EEeoq2o5AsrCgip+BdkGT3G5faPO xvLoN2oBe2F2WYAqWq3HAjugbVQeAsbeTvknc8kWbFJ3HQ0XFcrX55qjb2kZg6WEtV0G cd6Q8kTCgvOyugSVmAZbMGbGHhcbznJLC+vIONB0kbjZT2zKcTLh1o3m4JN1L4Qt/CbG BCX9O6G+9rIfVQuoRXW2styph6Da6aO4PaCH4mhoRWAD2mDt35YB56xM3903GX+TJ9rk /YcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zLW9kBAA; 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 s17-20020a632c11000000b0045ffa7279adsi17247578pgs.31.2022.10.19.01.50.40; Wed, 19 Oct 2022 01:50:53 -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=zLW9kBAA; 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 S231644AbiJSItA (ORCPT + 99 others); Wed, 19 Oct 2022 04:49:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230425AbiJSIsQ (ORCPT ); Wed, 19 Oct 2022 04:48:16 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F2628B2D5; Wed, 19 Oct 2022 01:46:08 -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 92F7F61821; Wed, 19 Oct 2022 08:46:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AB037C433B5; Wed, 19 Oct 2022 08:45:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169160; bh=QtnZ6K65aYoHOIJ/VgWVsYFcsTTB18FFcgDX9DHz+pk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zLW9kBAAu6fFfXDMoPtQzKV81omP94RZ5PkcPZPWhlAcEtcKZvQa4Z6NhkiSnHa+0 cr0XYNt42MYSupznZuRX+3hzeHpq91xKA6V92AFE395lFBDA4n996naBFDxlJjDifa Yvkyx8EYgPWIzjtqjIvEf/ZHvINZdZy1tH00spYE= 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 6.0 178/862] KVM: VMX: Drop bits 31:16 when shoving exception error code into VMCS Date: Wed, 19 Oct 2022 10:24:25 +0200 Message-Id: <20221019083257.823613947@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 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?1747105301064903846?= X-GMAIL-MSGID: =?utf-8?q?1747105301064903846?= 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 @@ -3832,7 +3832,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 @@ -1695,7 +1695,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; }