Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp24860pxb; Wed, 20 Apr 2022 15:04:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzLPY8Iozm4TPe/DWXmoC2aMywYzojIGHKyZ3x/AlWC0ZWngvZJ9xX+oBx10K5wF8d0hs+h X-Received: by 2002:a17:906:8a4e:b0:6e8:9109:cc01 with SMTP id gx14-20020a1709068a4e00b006e89109cc01mr19767919ejc.80.1650492292378; Wed, 20 Apr 2022 15:04:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650492292; cv=none; d=google.com; s=arc-20160816; b=s+VIQ/MN+/L0Pj4ep1wG/clE30hc0OqfMYKgjUzMyEmcu78zN3fWE2nF1vs04KR2SW 2fu7uNI4qDHSGNjSW5k7ZVeZ7azHBt6DlNFqM/kJk3kpzgOQ2X+hHo+3aovy7UL+DOex dnOy0gVoH61d7uA9rXetqX4as5zo7/Cy89aDdWkp8WXPthXZfR0JC+IphWB+QUcC5yn1 G1uYrYSsVgwiHA6UNSTsAnnancA2wtmybt5c3lyOUQKCyKXP0P6PHk+41isGalbEzRS1 OM6+MpF3D1eQlEiB85U6ZS+I+Cc25lfC64BsWiecvNuDQHffAItgpEf3jEUaW5VYSqss 0wWA== 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=Y5pKKgQt9cD9lanUtZDmztFaGnE0InfLW8DospAV9Lc=; b=I6slX6FFvgniM3j8AP4b1uKFjH4/GPvCfjbaCu9bggBEJJjlOCe313YN8EAHXVj16F //Els+aCjjQ8GpUPISQlY9GGmROgqXGerNesMThPKuANMOXLM/BMsGASd6FVM88fYHex rASai7vz+Jpavhh8kH3eVFo3T2YyQ5BfvwO1dzMvQllTEtg32qT+yL4IMwtSwWyABkXo H/jNcgX6w61BTS/2u3BL/TByBCKn64ohdYs6m2FcTJ0oK9gIMsSQmJRWP6HSF22VPTAE FXXceVugASNyQll35qHODIp+dCpMdsobCdeNO1cm3nDRff/c48nY1yT6cW17VycM99F1 olzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KeTAKfat; 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 q26-20020a056402033a00b00423a9302e02si2219872edw.258.2022.04.20.15.04.17; Wed, 20 Apr 2022 15:04:52 -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=KeTAKfat; 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 S241457AbiDRNpw (ORCPT + 99 others); Mon, 18 Apr 2022 09:45:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241883AbiDRNZl (ORCPT ); Mon, 18 Apr 2022 09:25:41 -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 3B1E115819; Mon, 18 Apr 2022 05:52:58 -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 DE5D4612B5; Mon, 18 Apr 2022 12:52:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCCD0C385A1; Mon, 18 Apr 2022 12:52:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286375; bh=XHhJvtTAVSezNouZIbqSrgobfO0e7Br0xeWb1Sc4/XM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KeTAKfat5jtd0jDaTkxyVQ1fSTWVdq1J8mBUH/fiUnj2mKtAoUNSAGEeM9OAdi1Qg 1DlYqq90qhtY9Rkz7uaB6pL/+XUQa3CIyDvJxX+9CBOa73Ro9A7Gu6Ei4xEV8HeS44 83GRZ/kZ66KL/YeFgvx4lIYc33us6ujLs5E1XKL4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Hou Wenlong , Paolo Bonzini , Sasha Levin Subject: [PATCH 4.14 114/284] KVM: x86/emulator: Defer not-present segment check in __load_segment_descriptor() Date: Mon, 18 Apr 2022 14:11:35 +0200 Message-Id: <20220418121214.252056985@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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,T_SCC_BODY_TEXT_LINE 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: Hou Wenlong [ Upstream commit ca85f002258fdac3762c57d12d5e6e401b6a41af ] Per Intel's SDM on the "Instruction Set Reference", when loading segment descriptor, not-present segment check should be after all type and privilege checks. But the emulator checks it first, then #NP is triggered instead of #GP if privilege fails and segment is not present. Put not-present segment check after type and privilege checks in __load_segment_descriptor(). Fixes: 38ba30ba51a00 (KVM: x86 emulator: Emulate task switch in emulator.c) Reviewed-by: Sean Christopherson Signed-off-by: Hou Wenlong Message-Id: <52573c01d369f506cadcf7233812427cf7db81a7.1644292363.git.houwenlong.hwl@antgroup.com> Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin --- arch/x86/kvm/emulate.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 23d3329e1c73..c5f2a72343e3 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -1682,11 +1682,6 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, goto exception; } - if (!seg_desc.p) { - err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; - goto exception; - } - dpl = seg_desc.dpl; switch (seg) { @@ -1726,6 +1721,10 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, case VCPU_SREG_TR: if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) goto exception; + if (!seg_desc.p) { + err_vec = NP_VECTOR; + goto exception; + } old_desc = seg_desc; seg_desc.type |= 2; /* busy */ ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, @@ -1750,6 +1749,11 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, break; } + if (!seg_desc.p) { + err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; + goto exception; + } + if (seg_desc.s) { /* mark segment as accessed */ if (!(seg_desc.type & 1)) { -- 2.34.1