Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp4890220rwr; Mon, 8 May 2023 14:25:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6RDvupubcGcsHUtXTn4ZjBrv3uNXRqsFYu3iPs23gIUfbNDmaxR/Gb/JPEMWw3AeWdLrUe X-Received: by 2002:a17:90a:9c07:b0:250:1961:f6b0 with SMTP id h7-20020a17090a9c0700b002501961f6b0mr11906715pjp.32.1683581109147; Mon, 08 May 2023 14:25:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683581109; cv=none; d=google.com; s=arc-20160816; b=ur0xMc8iID4MEQpb5ok9kP5f2ex9NXFB77HlwP7hqb5FtwvoWWwhAqSJ3N0XfrL/2h gNU9+muDjd3mhje0upufdJ5IxdLS6SFz5SWoN4i8FJxld31upMBitDIoVbCnCT45gNg+ elXrIw1eutJvAg54n++8ALmvPJdxNiOdjfxbKC2c8fkt9u30FcSO2aJIlMKShWIjkTL2 Aj1fKPYzs2rHYGubmJw3CEttWEESd9Dt6+rPEgkDl6rEOT3Apsndcxmqp+jojwxiI7Ad 5Oi3mLzF3gVc3hxEt6FNKj8mZOH2NjAdRzTjWHC10DBmqp9c8qIrOhyh1+xzB52xEbvx in9Q== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=NFnNeAlUj9C+huN2rBEcxkVDG8OipZYXhgLegmZBDMk=; b=JeQ6nI1nsH9FcsmXQ7oLAvizXj6s9Nuz6mkqIZLVS1CCUZ7plrF1CfN4In+KRahqDk Ea0KJ/QFccOyL9TPxWtTSj3ogmuH2Ls/Z1Xya3WK/2qcG7JxxpzUgiYZiRNq+bRO8m0f LGkbnYS36gYUG88MWWnVUSEUR1Zh3zHE4gUhWU2TznrpUt7uC3OSlHq/+lvuZ88sb3Ny Nineul0Nsgy9OqcMbqvjDUmUUSruhjYQr368vSx+kMhZcjoMr0buXZrWMeJIItffhrtk W8o6E7zstTK+TFyA4bHw0iVrUzunKh3Y3BvlIUe/AOmicm2/7mmXr5qGQcd4B8lN8mmq EP4A== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s14-20020a17090aad8e00b0024df2dbc7afsi20184315pjq.52.2023.05.08.14.24.54; Mon, 08 May 2023 14:25:09 -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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232167AbjEHVLx (ORCPT + 99 others); Mon, 8 May 2023 17:11:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229486AbjEHVLw (ORCPT ); Mon, 8 May 2023 17:11:52 -0400 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 618E04220; Mon, 8 May 2023 14:11:51 -0700 (PDT) Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-51b4ef5378bso4569141a12.1; Mon, 08 May 2023 14:11:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683580311; x=1686172311; h=in-reply-to:content-transfer-encoding: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=NFnNeAlUj9C+huN2rBEcxkVDG8OipZYXhgLegmZBDMk=; b=S9UF4HLG98keATLrTaHybz4WUFa5bF0m5spBkllhG7MJveWKjBgz+fjTKfueRjX8LO YUz/PdBKVrT0jvYlv+Yc53iOIPt/ZRXnvYdaOpT9ZR7mjU2GE4hMOHBL3a/3jxD1SaPn 2utFB62PsvOZtp3Wwq1dBsM0pArQV68Grvxxh13mqaax040tfLT94cBy534+4u5IjyOR I9xh7S7YBSJOEjJpDW9QIEnrEotSvaAeoaJC8D+LfiZ82M5TCrNCt4NPG84P/VRLo/Au cOj/icR180Vw6mDBLiwUTtF0FusiKdmQJUpkhZqq7G7W6BZOTD1cSmqin+wOdFJe6jfd r2Kg== X-Gm-Message-State: AC+VfDzGCIt1Qy19WlXYsaaDcE5/qwuTQNnYTJrceGpTKsespQHqhGAr 7ucAH7lDqHSxSTVHpATGtcU= X-Received: by 2002:a17:90a:65cb:b0:248:8399:1f7c with SMTP id i11-20020a17090a65cb00b0024883991f7cmr11239406pjs.38.1683580310757; Mon, 08 May 2023 14:11:50 -0700 (PDT) Received: from liuwe-devbox-debian-v2 ([20.69.120.36]) by smtp.gmail.com with ESMTPSA id k14-20020a170902760e00b0019aeddce6casm7648553pll.205.2023.05.08.14.11.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 May 2023 14:11:50 -0700 (PDT) Date: Mon, 8 May 2023 21:11:48 +0000 From: Wei Liu To: =?iso-8859-1?Q?Micka=EBl_Sala=FCn?= Cc: Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Ingo Molnar , Kees Cook , Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li , Alexander Graf , Forrest Yuan Yu , James Morris , John Andersen , Liran Alon , "Madhavan T . Venkataraman" , Marian Rotariu , Mihai =?utf-8?B?RG9uyJt1?= , =?utf-8?B?TmljdciZb3IgQ8OuyJt1?= , Rick Edgecombe , Thara Gopinath , Will Deacon , Zahra Tarkhani , =?utf-8?Q?=C8=98tefan_=C8=98icleru?= , dev@lists.cloudhypervisor.org, kvm@vger.kernel.org, linux-hardening@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, qemu-devel@nongnu.org, virtualization@lists.linux-foundation.org, x86@kernel.org, xen-devel@lists.xenproject.org, Wei Liu Subject: Re: [PATCH v1 5/9] KVM: x86: Add new hypercall to lock control registers Message-ID: References: <20230505152046.6575-1-mic@digikod.net> <20230505152046.6575-6-mic@digikod.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230505152046.6575-6-mic@digikod.net> X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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 Fri, May 05, 2023 at 05:20:42PM +0200, Micka?l Sala?n wrote: > This enables guests to lock their CR0 and CR4 registers with a subset of > X86_CR0_WP, X86_CR4_SMEP, X86_CR4_SMAP, X86_CR4_UMIP, X86_CR4_FSGSBASE > and X86_CR4_CET flags. > > The new KVM_HC_LOCK_CR_UPDATE hypercall takes two arguments. The first > is to identify the control register, and the second is a bit mask to > pin (i.e. mark as read-only). > > These register flags should already be pinned by Linux guests, but once > compromised, this self-protection mechanism could be disabled, which is > not the case with this dedicated hypercall. > > Cc: Borislav Petkov > Cc: Dave Hansen > Cc: H. Peter Anvin > Cc: Ingo Molnar > Cc: Kees Cook > Cc: Madhavan T. Venkataraman > Cc: Paolo Bonzini > Cc: Sean Christopherson > Cc: Thomas Gleixner > Cc: Vitaly Kuznetsov > Cc: Wanpeng Li > Signed-off-by: Micka?l Sala?n > Link: https://lore.kernel.org/r/20230505152046.6575-6-mic@digikod.net [...] > hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); > if (is_unrestricted_guest(vcpu)) > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index ffab64d08de3..a529455359ac 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -7927,11 +7927,77 @@ static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr) > return value; > } > > +#ifdef CONFIG_HEKI > + > +extern unsigned long cr4_pinned_mask; > + Can this be moved to a header file? > +static int heki_lock_cr(struct kvm *const kvm, const unsigned long cr, > + unsigned long pin) > +{ > + if (!pin) > + return -KVM_EINVAL; > + > + switch (cr) { > + case 0: > + /* Cf. arch/x86/kernel/cpu/common.c */ > + if (!(pin & X86_CR0_WP)) > + return -KVM_EINVAL; > + > + if ((read_cr0() & pin) != pin) > + return -KVM_EINVAL; > + > + atomic_long_or(pin, &kvm->heki_pinned_cr0); > + return 0; > + case 4: > + /* Checks for irrelevant bits. */ > + if ((pin & cr4_pinned_mask) != pin) > + return -KVM_EINVAL; > + It is enforcing the host mask on the guest, right? If the guest's set is a super set of the host's then it will get rejected. > + /* Ignores bits not present in host. */ > + pin &= __read_cr4(); > + atomic_long_or(pin, &kvm->heki_pinned_cr4); > + return 0; > + } > + return -KVM_EINVAL; > +} > + > +int heki_check_cr(const struct kvm *const kvm, const unsigned long cr, > + const unsigned long val) > +{ > + unsigned long pinned; > + > + switch (cr) { > + case 0: > + pinned = atomic_long_read(&kvm->heki_pinned_cr0); > + if ((val & pinned) != pinned) { > + pr_warn_ratelimited( > + "heki-kvm: Blocked CR0 update: 0x%lx\n", val); I think if the message contains the VM and VCPU identifier it will become more useful. Thanks, Wei.