Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp907022pxb; Tue, 8 Feb 2022 05:19:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJyqsGaGTyDjrrI+cWQX4HqemyWUAoJ0QiQmOAQ+CIoVK7/wO1nCIKo/zdV4/Yc+8u6A/3xr X-Received: by 2002:aa7:8241:: with SMTP id e1mr1402414pfn.86.1644326344774; Tue, 08 Feb 2022 05:19:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644326344; cv=none; d=google.com; s=arc-20160816; b=i7JFUHwPQ/v5QXc51cMQPgOW4n0/aa6tHHkSVNxeoXKnFg8VW4FkQwD3R8vuRiXWil 5z2PAviRpcvREoj4MVre9oNsD5zOdP/5StHZeKJiKZvgKlVv+/mvmGSjMyPCnFoGG41X BExSuYSrOm9+3PFpFNxVOSn9kF7JwiEcmlbOL7Fq6nfMfcDW/aCjiTia/AicD3xLYPbs H/i7axiGJdjMbthiD1EVnNkhOd6i6ZkfYmaDLKQCsV9vsJE+iVqzL5aB8DIE3FPYu4eK Jpj8DqxUgCPDiocAnQy5PebZ2JwKAMY3qawRg6xcnBYCkUUaJoxrWcHZdkFADhKj+Rik Vv+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=vzlzaB5S/veay9fPsYouwaa7Ye2duYzjZ7MsDyh1dr4=; b=tu1ZRiJODH81uJq1htAFTUf6+x98P0+jdfbSEfrGCP4+wQqNtomIb8O5OP6jQI1S2B ruma51hWevqRP8oofsV0DTzkW1pJiD26mozWBEbnCJtd7xQUBK/3BZCwx+tFC9qVztiD xLni35scVumrhYuvJT/P9dbTC12rsu/WUBi7pMPdHUgT/B1NjpjP+9GP/UhB0jJGevv2 EKrT925Lpjq/mlv1XlWXqKEB/k+oIMDjrlGkjTTwIWMjELIi6RmayhXOM4cTDpPvACou Td6BkM/M93LqX5vFht3vTx59SBJxuHOLJvKhtrmlBJMCqcjXiHbCN6YxeooxjOvKcGR4 sj4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DWvVdPKo; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m9si11574245pll.287.2022.02.08.05.18.48; Tue, 08 Feb 2022 05:19:04 -0800 (PST) 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=@google.com header.s=20210112 header.b=DWvVdPKo; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236924AbiBGT4Z (ORCPT + 99 others); Mon, 7 Feb 2022 14:56:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241660AbiBGTvK (ORCPT ); Mon, 7 Feb 2022 14:51:10 -0500 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4695C0401E1 for ; Mon, 7 Feb 2022 11:51:09 -0800 (PST) Received: by mail-pf1-x42a.google.com with SMTP id n32so14811835pfv.11 for ; Mon, 07 Feb 2022 11:51:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=vzlzaB5S/veay9fPsYouwaa7Ye2duYzjZ7MsDyh1dr4=; b=DWvVdPKojL+uX+8p3r/iCjkHgAERiMri1PzoB/Zo0kSnzI46zwpN2j6TOjDnLPvwVf hAbDf2McKmU2wDKHc+DePunkStheTMA5WRL1xC6HZRU5UBDR2TbsJO1UPDT1jxnBUXMV WhX37VktCtbWWXdlT+u9fYjz806AJFbK+zXU/1L6ZZeHcj0C/iES3inbmXTCs+1MTMjP XZk9QIg6fRn7NLS2LC5rdI80mmJXYEW5tlbZbrl99GdkVMk8BOgyhLlPPL2DSq0BCxIN yiN8vAxFu2y8FTQtM7uDKGpGPF0cehnZuJEoyK5Qk+/nuFvzXpsVX5Ozn1xE1xE7acTH fPcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=vzlzaB5S/veay9fPsYouwaa7Ye2duYzjZ7MsDyh1dr4=; b=hjk7eLMuxrQjkZVsCdNADupMJXZ7zbgA5oHUhnPTRu4eqtITgGToaFdhMRDmvxQcIa hEevBaV6EROLd3ZSlPLJ+qGs7AHYspwWMwX8WfmvjBg4M78Mulco5nBGjFN8GmXU2jrL j0yk8k/+DjeNto3+VPwSvKokN3FbKnZuCWhBBXknM9ZNOvDyW2894geryW+m3caqU4D7 gOzSj1Lh5D0FqpzJdvMRxkjib9RUnt/JW97s1wGM338LYoygeeoKu5WXJfmctUYW72Rz +4jmPSD0eOKEnkaoAqOqqoXfJ0f6czcZAYSznDZ0Z8yM2HcClPDxl1NUfjcMcj92BKKt Buhg== X-Gm-Message-State: AOAM5324GtadnDXNzCFfQV4NsiLbFowW9BhoS5jfCajI9Sd6GP2BVIL1 +T5DAzFMxxI5unDiYiq8TSb/gA== X-Received: by 2002:a05:6a00:244e:: with SMTP id d14mr968685pfj.45.1644263469165; Mon, 07 Feb 2022 11:51:09 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id c18sm12636184pfp.181.2022.02.07.11.51.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 11:51:08 -0800 (PST) Date: Mon, 7 Feb 2022 19:51:05 +0000 From: Sean Christopherson To: Hou Wenlong Cc: kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] KVM: x86: Fix wrong privilege check for code segment in __load_segment_descriptor() Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 On Thu, Jan 20, 2022, Hou Wenlong wrote: > Code segment descriptor can be loaded by jmp/call/ret, iret > and int. The privilege checks are different between those > instructions above realmode. Although, the emulator has > use x86_transfer_type enumerate to differentiate them, but > it is not really used in __load_segment_descriptor(). Note, > far jump/call to call gate, task gate or task state segment > are not implemented in emulator. > > As for far jump/call to code segment, if DPL > CPL for conforming > code or (RPL > CPL or DPL != CPL) for non-conforming code, it > should trigger #GP. The current checks are ok. > > As for far return, if RPL < CPL or DPL > RPL for conforming > code or DPL != RPL for non-conforming code, it should trigger #GP. > Outer level return is not implemented above virtual-8086 mode in > emulator. So it implies that RPL <= CPL, but the current checks > wouldn't trigger #GP if RPL < CPL. > > As for code segment loading in task switch, if DPL > RPL for conforming > code or DPL != RPL for non-conforming code, it should trigger #TS. Since > segment selector is loaded before segment descriptor when load state from > tss, it implies that RPL = CPL, so the current checks are ok. > > The only problem in current implementation is mssing RPL < CPL check for > far return. However, change code to follow the manual is better. > > Signed-off-by: Hou Wenlong > --- Reviewed-by: Sean Christopherson > arch/x86/kvm/emulate.c | 30 ++++++++++++++++++++++-------- > 1 file changed, 22 insertions(+), 8 deletions(-) > > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c > index 864db6fbe8db..b7ce2a85e58e 100644 > --- a/arch/x86/kvm/emulate.c > +++ b/arch/x86/kvm/emulate.c > @@ -1631,14 +1631,28 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, > if (!(seg_desc.type & 8)) > goto exception; > > - if (seg_desc.type & 4) { > - /* conforming */ > - if (dpl > cpl) > - goto exception; > - } else { > - /* nonconforming */ > - if (rpl > cpl || dpl != cpl) > - goto exception; A comment here would be mildly helpful, e.g. /* RET can never return to an inner privilege level. */ > + if (transfer == X86_TRANSFER_RET && rpl < cpl) > + goto exception; And then as a follow-up patch, I think we can/should move the unhandled outer privilege level logic here to make it easier to understand why the checks for RET are incomplete, e.g. diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index a885b53dc7cc..a7cecd7beb91 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -1631,8 +1631,15 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, if (!(seg_desc.type & 8)) goto exception; - if (transfer == X86_TRANSFER_RET && rpl < cpl) - goto exception; + if (transfer == X86_TRANSFER_RET) { + /* RET can never return to an inner privilege level. */ + if (rpl < cpl) + goto exception; + /* Outer-privilege level return is not implemented */ + if (rpl > cpl) + return X86EMUL_UNHANDLEABLE; + } + if (transfer == X86_TRANSFER_RET || X86_TRANSFER_TASK_SWITCH) { if (seg_desc.type & 4) { /* conforming */ @@ -2227,9 +2234,6 @@ static int em_ret_far(struct x86_emulate_ctxt *ctxt) rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); if (rc != X86EMUL_CONTINUE) return rc; - /* Outer-privilege level return is not implemented */ - if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl) - return X86EMUL_UNHANDLEABLE; rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl, X86_TRANSFER_RET, &new_desc);