From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5350DC433FE for ; Mon, 7 Feb 2022 19:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240078AbiBGT4Z (ORCPT ); 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-Google-Smtp-Source: ABdhPJxckrgaXyzE7hXmoCqcM4bymTTt+24ecIBSNEEEv8MA5Bd9AJg0cUoNit9K9VB1eF832B7YIQ== 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: 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);