Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp7109481rwi; Mon, 24 Oct 2022 09:57:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Cz1cvvGituQqK0ZKWtEgzUCKw+bjPXycx5tXfHliOnWwH6z3yJPoN0CFPyXK5WW1g4WXH X-Received: by 2002:a17:90b:1808:b0:20c:8409:2007 with SMTP id lw8-20020a17090b180800b0020c84092007mr39974296pjb.226.1666630611295; Mon, 24 Oct 2022 09:56:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666630611; cv=none; d=google.com; s=arc-20160816; b=wvlRTOG+r7tX/vg+2lAiPIu3b8ct2zo8WAtdatDKsxKfZvIiC1YevTou8qhXys14LD H7IF5F7R0tL8fOHcOud3jZRApNR0QRuE0Xd932v1ZYWT2i0q18rBaVtGScr2KQ298VlB Ze7wpCDncu3UNwxdRnaBpUUk/N5L4cPe6phpjM75VUhJulBopeZe9caU+Y+26Fm3cxbF 8O5ae9yq0JWRkz6fQER3vyW+OsxKjQp2skMZSYM/VJdcke7koSkgJPaR71iM04KUuLZF a+y2ZIXWlbXA2IWKfbDr2DLN3gKK9O6BZgqL0gYvbEtLbKeUf9cYAKx5ylQMOUBpqQn3 IAfw== 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=ctV7+rZPsGR/QnD/z+WRef4yOR8lVXODY8fGVmYaPK4=; b=AJ5nJClIs+5ujwdI+qt8MhzhFr0+WIubhV3SW8oohfUhcKSCeIjYxLjKC8Oyb22qEW AB2bc1LMZk7zTyjB5yN0AL2AyCZpc6Pzt2xQ+R/bgSdfyUyG2x3VPH6orWiAXUQxnG7A q3SRsEmRvUdZsVHcGHHDv4Jd1YUhik40BcQhtQ4ZVXvElYaL8jD6WLiWRqurOFqPNYS3 wRnRHP00R51zbIBGUBgyzlbxi5uILyM/yb9GNoy82kbmTcKqZZO5gCv7IWZIrKPBngIi h+/mkHRS4Epzw/vr1rdKAn8sYpFbeZ3dBMbbWuQ6cFjmUbtbRLZ1pzyaoFscg1H7Hj3r VioA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ht59Pz9U; 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 t19-20020a63d253000000b0042c3a5b699asi16885091pgi.783.2022.10.24.09.56.32; Mon, 24 Oct 2022 09:56:51 -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=ht59Pz9U; 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 S235007AbiJXQre (ORCPT + 99 others); Mon, 24 Oct 2022 12:47:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234998AbiJXQqb (ORCPT ); Mon, 24 Oct 2022 12:46:31 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1EE5A1A991C; Mon, 24 Oct 2022 08:31:28 -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 ams.source.kernel.org (Postfix) with ESMTPS id 088E8B818E7; Mon, 24 Oct 2022 12:38:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CE88C433B5; Mon, 24 Oct 2022 12:38:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666615114; bh=XH7eVddOd2TaoGdpln6E2fmn2Bqam6nMYnW2HyEEwso=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ht59Pz9UY3xYsrryrhflqB8PIZi4wXpvSsLKIe1sPJOx1dMQ1+EasKUWMlbhmkDTz IRAcFgjbm1ElO4EBHH043ZwJLCeoRWGdkzYiAkb1NF1bHAu3+HZKQY3Ajk86lVPndH gX/UoE3Yz/GVAPZm6q/XozSarenw0LDnjhwVCYQc= 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.15 126/530] KVM: VMX: Drop bits 31:16 when shoving exception error code into VMCS Date: Mon, 24 Oct 2022 13:27:50 +0200 Message-Id: <20221024113050.768069992@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@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.6 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 @@ -3826,7 +3826,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 @@ -1676,7 +1676,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; }