Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp4081571rwi; Wed, 12 Oct 2022 10:18:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5SwdZxSoMZ0QrmlQ5JdB+FpNntfVXcSJ2fRdELalXMFJ3UQzRjQAcjal33t7BIjrgr3w3K X-Received: by 2002:a05:6402:428a:b0:42e:8f7e:1638 with SMTP id g10-20020a056402428a00b0042e8f7e1638mr28634023edc.228.1665595100285; Wed, 12 Oct 2022 10:18:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665595100; cv=none; d=google.com; s=arc-20160816; b=c1vLcaQRxjUrM76klG4aiUq5noS+3BXtRaiFMmp5nKnJCUUNYo/G/83eq9V1/RqQfh 38IgIZt3WxVp5Y83N+SF2SwAy+7y0a4o8awUH2grqvDdpGQntjB+NNinlw+4OH+5ekoZ 0OuMois+4kpPFWO4GrI4p7d+zwqkQmXFUk0griLv/K4rZlsIvIGjsigo+CjPN4BkyAvH O+rbvBXVss5dJ1woaUKmUBS60CjdYO+1A6Pc4xwvEFJofthkkJzNFp9ThtG6deQiiYLK q4P1YKISjv4kOYk+c26qnVUBJzQlHmDHvIsbTNodoQecIUbjewrz2DccWzLtVQ+trlQB V93g== 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=Det2ITA/n+WXH8RhBcuNargdlyOW5bs99Gk+aJ7/qCQ=; b=AYNj2mhGkAbqlPzznTWY0OGdq+tZrmpNbvYyneCQTxj1fWCG3FbV8BDOjABOW1Xaur ht4QM7z9nB88hfSdcdyoYl9aaVqINxGaHtaSagPLMAmFOQoSCnnvoo96u/gw+Pv1Ifaw YMu+YSfETovgx8lSxoWxNyl2yGlJ5WVU0IPo+189g6JPguu+RwbdCzG3/9tjXEOLS8xN wn6Vdn33IgMMB1hjre2dcLRdiSmduMNpBJnUuDOj+7bePAbjMpn6x8G3PwazefTg3jiT wY3jwqlQn6/BFBVh2Y9tYqthXWq+pUnnx46DZX9ErvIBc+B1+UtOE/I8tG/px0pHGi8A T4Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=QMdpGzfB; 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 j10-20020a05640211ca00b0045ca331c2ebsi2743576edw.232.2022.10.12.10.17.51; Wed, 12 Oct 2022 10:18:20 -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=@google.com header.s=20210112 header.b=QMdpGzfB; 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 S229686AbiJLQfK (ORCPT + 99 others); Wed, 12 Oct 2022 12:35:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229734AbiJLQfG (ORCPT ); Wed, 12 Oct 2022 12:35:06 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0618D2FC0D for ; Wed, 12 Oct 2022 09:35:03 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id l1-20020a17090a72c100b0020a6949a66aso2573775pjk.1 for ; Wed, 12 Oct 2022 09:35:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Det2ITA/n+WXH8RhBcuNargdlyOW5bs99Gk+aJ7/qCQ=; b=QMdpGzfBf0NldWY/bD/I/AQC+34DaQ6T41DAl98Jbq3s4BBRU97wbxp6mrDPsK+Ajy 2edzfjH61I/TPtM4Ld3Sg286tM7g4/bes7Zj7CMqf7PHJuiZGAuKkb6vrw7R0AqElJ9g q5JEZvX/mhYNsoqrl/mrS7yh+EMgeDi+PhabUfW+c9hkX5GBav4GP492SXT4icsqZZ/V eezHq/vlZYz/Pecf8ouJtRivFcbERqR8LAZMKRKHEx10V7KKojX63yeROb8OYzdtW9eV xcjCMO/aCFW0H9FYI8asOxTjzUTXU1kGeFnALca+5xVmM5t5zT1ohcqLE2wChC6vaWJL 3d2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Det2ITA/n+WXH8RhBcuNargdlyOW5bs99Gk+aJ7/qCQ=; b=Xmp4qI9HyQIJd0jDt0IhBJdZ7vVDHIF0W7krC5X3psTLTvvXqXoeZXwKPnxqZWo36j oqTR1qIM5IWpIY9Bq90+Gn1/pkjg+66cl4bLNvKCKMQNztIQBSeSG71eelQD1cfSfmVC pkxF7+kQZqjC1lu8nINO30P8pDk8+LorZ733PgF8oXV4wpfC8SBNIDzan+eyLGvL42VG Y1sAoLwEf3u464po26SiFDJiOupiBZGWzwp2/Xf5RlTmMAdAa1bcOW+bq14rnHwUeVWc RXuPVNn4W6yW9WpO+YpqP8l4K7kx4vzWRxdGisKQl4XUysrnlYaueMexbCyGnj0YdFT4 HGbg== X-Gm-Message-State: ACrzQf0nj1YJe5QJHQnbc+AZTXzPbhEQI3jWBNXrYIEoath6Pp4TgKuW VfgzNhKdJfWRlDo2L7Md+YkS6A== X-Received: by 2002:a17:90b:1e4b:b0:20d:2ad0:ce00 with SMTP id pi11-20020a17090b1e4b00b0020d2ad0ce00mr6388449pjb.72.1665592502345; Wed, 12 Oct 2022 09:35:02 -0700 (PDT) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id q2-20020a170902f34200b0017a1145eec7sm10799750ple.157.2022.10.12.09.35.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Oct 2022 09:35:01 -0700 (PDT) Date: Wed, 12 Oct 2022 16:34:57 +0000 From: Sean Christopherson To: Hou Wenlong Cc: kvm@vger.kernel.org, Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: x86: Mark transfer type as X86_TRANSFER_RET when loading CS in iret emulation 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, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 Wed, Oct 12, 2022, Hou Wenlong wrote: > When loading code segment descriptor in iret instruction emulation, the > checks are same as far return instruction emulation, so transfer type > should be X86_TRANSFER_RET in __load_segment_descriptor(). Although, > only iret in real mode is implemented now, and no checks are actually > needed for real mode, it would still be better to mark transfer type as > X86_TRANSFER_RET. It's not strictly a RET though. The RPL vs. DPL checks in __load_segment_descriptor() might do the right thing, but there's a rather large pile of stuff IRET can do that RET can't (ignoring the fact that KVM doesn't even emulate FAR RET to outer privilege levels). And __emulate_int_real() also loads CS with X86_TRANSFER_NONE, i.e. KVM still has a weird path to worry about. Rather than make the IRET case slightly less wrong, what about adding a sanity check in __load_segment_descriptor() that KVM doesn't attempt to load CS in Protected Mode with X86_TRANSFER_NONE? diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 3b27622d4642..fe735e18c419 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -1641,6 +1641,14 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, goto exception; break; case VCPU_SREG_CS: + /* + * KVM uses "none" when loading CS as part of emulating Real + * Mode exceptions and IRET (handled above). In all other + * cases, loading CS without a control transfer is a KVM bug. + */ + if (WARN_ON_ONCE(transfer == X86_TRANSFER_NONE)) + goto exception; + if (!(seg_desc.type & 8)) goto exception; > > No functional change intended. > > Signed-off-by: Hou Wenlong > --- > arch/x86/kvm/emulate.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c > index 3b27622d4642..5052eb480068 100644 > --- a/arch/x86/kvm/emulate.c > +++ b/arch/x86/kvm/emulate.c > @@ -2100,6 +2100,7 @@ static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) > X86_EFLAGS_FIXED; > unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF | > X86_EFLAGS_VIP; > + u8 cpl = ctxt->ops->cpl(ctxt); > > /* TODO: Add stack limit check */ > > @@ -2121,7 +2122,8 @@ static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) > if (rc != X86EMUL_CONTINUE) > return rc; > > - rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); > + rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl, > + X86_TRANSFER_RET, NULL); > > if (rc != X86EMUL_CONTINUE) > return rc; > -- > 2.31.1 >