Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp418276rdb; Tue, 31 Oct 2023 10:57:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHJLaRz6Ek/mHYkNKGfDT0FzIsAYBtgprIMGnuWXJzfO+Avod35j+3zVfEAngrVtU+5g5J1 X-Received: by 2002:a17:902:ab56:b0:1c0:bcbc:d5d with SMTP id ij22-20020a170902ab5600b001c0bcbc0d5dmr10724611plb.61.1698775038943; Tue, 31 Oct 2023 10:57:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698775038; cv=none; d=google.com; s=arc-20160816; b=NcRD6nZyb8AxdFxHV9AldAnEUeANuPJ8BpRZEivn1e1rsat/BqG8xbttF/PT2fuayL m191R08BfUQnEWS+6pO8UrgV0j7e+mwFroFFDgzXpSVFFumZOFXECz1mQqAyM9E6XsQT HNGVh8QJ/FN66NQwiIZKgZS5r8klm4ySkEOHTKIypE20Z9iNCQzXYKZigyyMWieOG1vN 4nMpQGAJh2a1py9hJHNtkBcImpT/Khs2C9r6PthiiO5Ub6fkE6WYJzeWO4xGUM9Qe8dS cAi8qUh7wUeXlJH+osnaEy3N6sCd8EmiPuO7iDvjp8bxrJt1BpITEv49g66PQuU8ES4t bSqA== 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:date:cc:to:from:subject :message-id:dkim-signature; bh=WgMjQYwxlUZjBnG/eePgFyOUyxWaXqAMDNc4VKXZvIw=; fh=+Fohcjfhju91Ks65dYUxnAk8Iqhjo5nHmNS78Sf+bHk=; b=tbiV/avKsQpdG4pr98K/TS+HLGq2kpdon2Ik0t5ybiQZ9+H7xPLnnlceJGBTfrBo0R 5pBwQtpZmPPzwTuam/sxIvKkTy8xAUoA3itLaUZQV+k6QD6+rvCj9XK7m/+Xl1c2UIBk OHaOCULdLtPtprU463iQk+SkYWKVnPzM1oOCF4luxXD/BsOP3o7PUdxLxQmnrnHkVtdr CiHGElKpHXdqwUWy31XZwC1PmGdkbhFQzVcmp51ACzfZolRtx47qmM/Htr7fwS6CU6l0 WuQYYs4nuZVzUA6ZoA7Yx34p2qRjIqayXYq6uzgyiIYZpxSJysZZcQQfn3/Al1/LxLA3 WFTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XnOzbLR7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id lf12-20020a170902fb4c00b001bb3bcd05bbsi1224802plb.471.2023.10.31.10.57.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 10:57:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XnOzbLR7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id D921D8044375; Tue, 31 Oct 2023 10:57:15 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376338AbjJaR5J (ORCPT + 99 others); Tue, 31 Oct 2023 13:57:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376257AbjJaR5H (ORCPT ); Tue, 31 Oct 2023 13:57:07 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2762101 for ; Tue, 31 Oct 2023 10:56:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698774981; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WgMjQYwxlUZjBnG/eePgFyOUyxWaXqAMDNc4VKXZvIw=; b=XnOzbLR77ynLxpOQuH+0XY8ocHhLWKa6tlG635tKW3CqpGzM4I92emDdWiKfZ6cZn1aDeq WdyJCdvePV9q7Z4DxaRrgJXgPyASMzFJjUtmH+U8iKvutL6mE0jmVynpvX2ky5fbGf1rY7 6JNtoP6hfenEz23+zlb7B9J51pmv5OY= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-216-hSCNxFloNk2VAaqjehENCQ-1; Tue, 31 Oct 2023 13:56:19 -0400 X-MC-Unique: hSCNxFloNk2VAaqjehENCQ-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4083a670d25so43075405e9.0 for ; Tue, 31 Oct 2023 10:56:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698774978; x=1699379778; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=WgMjQYwxlUZjBnG/eePgFyOUyxWaXqAMDNc4VKXZvIw=; b=M16ywObyvPFWhkUtr9lGn6+69aETZGcyAjgjw9xIYhz0psMw0uDsDUQoRtz+O181ny ti0qGnC8xUTJkJnx9+CVZGXgNgRC6qDX1adu+jrPdM+V9aMXnnTajo5qZmPpTxV4JpLm VKaTSP6BwLvDf2C9R+l8ww47QNRvI7oib6e4SBiGvS9iSEbyN5sRj8cBmZB61vXco1SR jrgbFe7hNgyl9GH1Rq8bWJj8h0giHSvQUdO3nipIiAAB/9lMN0AX18QV9+YJalWtynp2 Xe70YBOpoZ2fUsviWh0H2f6K1pJBnuHSLtBd0GEuGzwGe4+wtQtXiuz8/RAqTojzxAe3 tB8A== X-Gm-Message-State: AOJu0YyUMwb3HWJs/MmZjB02s1z6ufvmH266phe/drDyXSQxyQL7uW/c AYVmGYR0ZCiNNEQx1Bw62xXfkh6TcsOBkVeIl5h/NN866yIGrG4S2pmgOcOewupOC12eP3cF3wN BhLVrynyz/7AKggNGHBrG4FI4 X-Received: by 2002:a05:600c:4748:b0:409:325:e499 with SMTP id w8-20020a05600c474800b004090325e499mr10664054wmo.32.1698774978329; Tue, 31 Oct 2023 10:56:18 -0700 (PDT) X-Received: by 2002:a05:600c:4748:b0:409:325:e499 with SMTP id w8-20020a05600c474800b004090325e499mr10664035wmo.32.1698774978006; Tue, 31 Oct 2023 10:56:18 -0700 (PDT) Received: from starship ([89.237.100.246]) by smtp.gmail.com with ESMTPSA id j26-20020a05600c1c1a00b003fefb94ccc9sm2458756wms.11.2023.10.31.10.56.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 10:56:17 -0700 (PDT) Message-ID: <65b52f509d7f9147917f85c7db7de8fd3fe8fb44.camel@redhat.com> Subject: Re: [PATCH v6 21/25] KVM: VMX: Set up interception for CET MSRs From: Maxim Levitsky To: Yang Weijiang , seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: dave.hansen@intel.com, peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, john.allen@amd.com Date: Tue, 31 Oct 2023 19:56:15 +0200 In-Reply-To: <20230914063325.85503-22-weijiang.yang@intel.com> References: <20230914063325.85503-1-weijiang.yang@intel.com> <20230914063325.85503-22-weijiang.yang@intel.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 31 Oct 2023 10:57:16 -0700 (PDT) On Thu, 2023-09-14 at 02:33 -0400, Yang Weijiang wrote: > Enable/disable CET MSRs interception per associated feature configuration. > Shadow Stack feature requires all CET MSRs passed through to guest to make > it supported in user and supervisor mode while IBT feature only depends on > MSR_IA32_{U,S}_CETS_CET to enable user and supervisor IBT. I don't think that this statement is 100% true. KVM can still technically intercept wrmsr/rdmsr access to all CET msrs because they should not be used often by the guest and this way allow to show the guest different values than what the actual hardware values are. For example KVM can hide (and maybe it should) indirect branch tracking bits in the MSR_IA32_S_CET if only the shadow stack is enabled and indirect branch tracking is disabled. The real problem is that MSR_IA32_U_CET is indirectly allowed to be read/written unintercepted, because of XSAVES (CET_U state component 11). Note that on the other hand the MSR_IA32_S_CET is not saved/restored by XSAVES. So this is what I think would be the best effort that KVM can do to separate the two features: 1. If support state of shadow stack and indirect branch tracking matches the host (the common case) then it is simple: - allow both CET_S and CET_U XSAVES components - allow unintercepted access to all CET msrs 2. If only indirect branch is enabled in the guest CPUID, but *host also supports shadow stacks*: - don't expose to the guest either the CET_S nor CET_U XSAVES components. - only support IA32_S_CET/IA32_U_CET msrs, intercept them, and hide the shadow stack bits from the guest. 3. If only shadow stacks are enabled in the guest CPUID but the *host also supports indirect branch tracking*: - intercept access to IA32_S_CET and IA32_U_CET and disallow indirect branch tracking bits to be set there. - for the sake of performance allow both CET_S and CET_U XSAVES components, and accept the fact that these instructions can enable the hidden indirect branch tracking bits there (this causes no harm to the host, and will likely let the guest keep both pieces, fair for using undocumented features). -or- don't enable CET_U XSAVES component and hope that the guest can cope with this by context switching the msrs instead. Yet another solution is to enable the intercept of the XSAVES, and adjust the saved/restored bits of CET_U msrs in the image after its emulation/execution. (This can't be done on AMD, but at least this can be done on Intel, and AMD so far doesn't support the indirect branch tracking at all). Another, much simpler option is to fail the guest creation if the shadow stack + indirect branch tracking state differs between host and the guest, unless both are disabled in the guest. (in essence don't let the guest be created if (2) or (3) happen) Best regards, Maxim Levitsky > > Note, this MSR design introduced an architectual limitation of SHSTK and > IBT control for guest, i.e., when SHSTK is exposed, IBT is also available > to guest from architectual perspective since IBT relies on subset of SHSTK > relevant MSRs. > > Signed-off-by: Yang Weijiang > --- > arch/x86/kvm/vmx/vmx.c | 42 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 42 insertions(+) > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 9f4b56337251..30373258573d 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -699,6 +699,10 @@ static bool is_valid_passthrough_msr(u32 msr) > case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8: > /* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */ > return true; > + case MSR_IA32_U_CET: > + case MSR_IA32_S_CET: > + case MSR_IA32_PL0_SSP ... MSR_IA32_INT_SSP_TAB: > + return true; > } > > r = possible_passthrough_msr_slot(msr) != -ENOENT; > @@ -7769,6 +7773,42 @@ static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) > vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4)); > } > > +static void vmx_update_intercept_for_cet_msr(struct kvm_vcpu *vcpu) > +{ > + bool incpt; > + > + if (kvm_cpu_cap_has(X86_FEATURE_SHSTK)) { > + incpt = !guest_cpuid_has(vcpu, X86_FEATURE_SHSTK); > + > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_U_CET, > + MSR_TYPE_RW, incpt); > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_S_CET, > + MSR_TYPE_RW, incpt); > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_PL0_SSP, > + MSR_TYPE_RW, incpt); > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_PL1_SSP, > + MSR_TYPE_RW, incpt); > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_PL2_SSP, > + MSR_TYPE_RW, incpt); > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_PL3_SSP, > + MSR_TYPE_RW, incpt); > + if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_INT_SSP_TAB, > + MSR_TYPE_RW, incpt); > + if (!incpt) > + return; > + } > + > + if (kvm_cpu_cap_has(X86_FEATURE_IBT)) { > + incpt = !guest_cpuid_has(vcpu, X86_FEATURE_IBT); > + > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_U_CET, > + MSR_TYPE_RW, incpt); > + vmx_set_intercept_for_msr(vcpu, MSR_IA32_S_CET, > + MSR_TYPE_RW, incpt); > + } > +} > + > static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) > { > struct vcpu_vmx *vmx = to_vmx(vcpu); > @@ -7846,6 +7886,8 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) > > /* Refresh #PF interception to account for MAXPHYADDR changes. */ > vmx_update_exception_bitmap(vcpu); > + > + vmx_update_intercept_for_cet_msr(vcpu); > } > > static u64 vmx_get_perf_capabilities(void)