Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3360308rdh; Mon, 27 Nov 2023 12:09:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IFH5tDdFzKXLwEn+75eFiQcm372N1EUEzq14JXiFwu27KekmjyNE//Vco0a8HyLX+UFSr/b X-Received: by 2002:a17:902:b488:b0:1cc:5aef:f2c1 with SMTP id y8-20020a170902b48800b001cc5aeff2c1mr11601650plr.33.1701115783630; Mon, 27 Nov 2023 12:09:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701115783; cv=none; d=google.com; s=arc-20160816; b=jUhMWJzIxccptjedJHhtWAtj1Z7LvC5b7DF+I+DaXyyMiOyWQLCsHopvnBO2yYYqem b1ZZ3aQxdS1wF6k6JGz1eHjSMKCRlTxOgULupg04pSRUxQSGN6mq3+tmYnttMIwwkaMX kGz1sR+8OzWtjgGO8MJYHRIF45JQInMSlQ90I5jR0odALLd7cQmht1zgpkVtYeLJV59S aC7lW6JAlxU3YqAGjn1DVhBk/3tIGIXptSZhrJlX5kwjxZy1CnCTvFpmDoYh3FyAwNNA I4pbeuKVuCQnjRpWTlkYms+lGI7h/7jSBGgYWqK4tmmKHN3JDAEg762FUGCv8ZfCJ1Hi Ksaw== 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:dkim-signature; bh=0g8rmOmWV3gIvWc7PsUFJZCK/mjDmfDlEjIfQZ9fLpw=; fh=3kfo7Wu3hVD31NLakUKcX3QFJbnxT7NDSOGAOu/oyBo=; b=PchnkEY3R+00vXczaVQxPbFu/26H54B7h6bmz+H9zUmPMS1K6dn8SBMxKSpmK1s2jI TN4+nD2/TshKLHPtM1gr3kqmsZoIf7O5qhP4//dyBxBZ3VfLqdMz7JsyMgn77iZyQtp7 WixNbYQhNu+2vmt2P4fPaUa2aLwrM8TvalS+3VHNHVgFoAdph/SR7prGD0Va7Jb/2sAF U/Tx4AckR2giXQYb5bJl3RiXI+aB7zyvE/DWFcTRapwlF/nohFte/tUpazfQHzlaAMem HsMGJPT8LTPeM6rGRwqJAyObZHpMw5rqpiULOUEfk2k9WRgDyyskeiSso64myqYq0md2 +Dhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=eDWXIpgR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id c9-20020a170903234900b001cfb4a3adadsi6159123plh.344.2023.11.27.12.09.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 12:09:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=eDWXIpgR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C79348068115; Mon, 27 Nov 2023 12:09:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233039AbjK0UJ0 (ORCPT + 99 others); Mon, 27 Nov 2023 15:09:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229527AbjK0UJY (ORCPT ); Mon, 27 Nov 2023 15:09:24 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0905C2; Mon, 27 Nov 2023 12:09:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=0g8rmOmWV3gIvWc7PsUFJZCK/mjDmfDlEjIfQZ9fLpw=; b=eDWXIpgRHK0l6Kp1zvM8AozfOF C6Od0RZKcBiM1TpdWu2XQUTQBZigv/lVFcp4wmzQVJpUa30ta0VAtD86zKeO0v7pZOLiVWQRr4KN8 hk4FOzx8yGdiIasjtANlRLndmWRJMvBdSEMGCKmqSYLag0ay+cj9SgXrs1vld5SS+8dkv6bWq3SV5 MDYEEFDnBglQijN9WYMxBEuwTpC4pfbqpYLSGbfh3mrMun861qNxt0UbAxZ6eZUufig+63zyL2qSO J7adiJq4wNyrGWs8soFqK96nVJbz6xxEW1NnrEo4DM5u7kBaGizRWOoC4Gn534kdhjeQxjkmw2wur fASu7jhg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1r7huM-00Bhd4-E7; Mon, 27 Nov 2023 20:08:42 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 12A193002F1; Mon, 27 Nov 2023 21:08:42 +0100 (CET) Date: Mon, 27 Nov 2023 21:08:41 +0100 From: Peter Zijlstra To: "Madhavan T. Venkataraman" Cc: =?iso-8859-1?Q?Micka=EBl_Sala=FCn?= , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Ingo Molnar , Kees Cook , Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li , Alexander Graf , Chao Peng , "Edgecombe, Rick P" , Forrest Yuan Yu , James Gowans , James Morris , John Andersen , Marian Rotariu , Mihai =?utf-8?B?RG9uyJt1?= , =?utf-8?B?TmljdciZb3IgQ8OuyJt1?= , Thara Gopinath , Trilok Soni , Wei Liu , Will Deacon , Yu Zhang , 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 Subject: Re: [RFC PATCH v2 17/19] heki: x86: Update permissions counters during text patching Message-ID: <20231127200841.GZ3818@noisy.programming.kicks-ass.net> References: <20231113022326.24388-1-mic@digikod.net> <20231113022326.24388-18-mic@digikod.net> <20231113081929.GA16138@noisy.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-0.9 required=5.0 tests=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 fry.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 (fry.vger.email [0.0.0.0]); Mon, 27 Nov 2023 12:09:41 -0800 (PST) On Mon, Nov 27, 2023 at 10:48:29AM -0600, Madhavan T. Venkataraman wrote: > Apologies for the late reply. I was on vacation. Please see my response below: > > On 11/13/23 02:19, Peter Zijlstra wrote: > > On Sun, Nov 12, 2023 at 09:23:24PM -0500, Micka?l Sala?n wrote: > >> From: Madhavan T. Venkataraman > >> > >> X86 uses a function called __text_poke() to modify executable code. This > >> patching function is used by many features such as KProbes and FTrace. > >> > >> Update the permissions counters for the text page so that write > >> permissions can be temporarily established in the EPT to modify the > >> instructions in that page. > >> > >> Cc: Borislav Petkov > >> Cc: Dave Hansen > >> Cc: H. Peter Anvin > >> Cc: Ingo Molnar > >> Cc: Kees Cook > >> Cc: Madhavan T. Venkataraman > >> Cc: Micka?l Sala?n > >> Cc: Paolo Bonzini > >> Cc: Sean Christopherson > >> Cc: Thomas Gleixner > >> Cc: Vitaly Kuznetsov > >> Cc: Wanpeng Li > >> Signed-off-by: Madhavan T. Venkataraman > >> --- > >> > >> Changes since v1: > >> * New patch > >> --- > >> arch/x86/kernel/alternative.c | 5 ++++ > >> arch/x86/mm/heki.c | 49 +++++++++++++++++++++++++++++++++++ > >> include/linux/heki.h | 14 ++++++++++ > >> 3 files changed, 68 insertions(+) > >> > >> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c > >> index 517ee01503be..64fd8757ba5c 100644 > >> --- a/arch/x86/kernel/alternative.c > >> +++ b/arch/x86/kernel/alternative.c > >> @@ -18,6 +18,7 @@ > >> #include > >> #include > >> #include > >> +#include > >> #include > >> #include > >> #include > >> @@ -1801,6 +1802,7 @@ static void *__text_poke(text_poke_f func, void *addr, const void *src, size_t l > >> */ > >> pgprot = __pgprot(pgprot_val(PAGE_KERNEL) & ~_PAGE_GLOBAL); > >> > >> + heki_text_poke_start(pages, cross_page_boundary ? 2 : 1, pgprot); > >> /* > >> * The lock is not really needed, but this allows to avoid open-coding. > >> */ > >> @@ -1865,7 +1867,10 @@ static void *__text_poke(text_poke_f func, void *addr, const void *src, size_t l > >> } > >> > >> local_irq_restore(flags); > >> + > >> pte_unmap_unlock(ptep, ptl); > >> + heki_text_poke_end(pages, cross_page_boundary ? 2 : 1, pgprot); > >> + > >> return addr; > >> } > > > > This makes no sense, we already use a custom CR3 with userspace alias > > for the actual pages to write to, why are you then frobbing permissions > > on that *again* ? > > Today, the permissions for a guest page in the extended page table > (EPT) are RWX (unless permissions are restricted for some specific > reason like for shadow page table pages). In this Heki feature, we > don't allow RWX by default in the EPT. We only allow those permissions > in the EPT that the guest page actually needs. E.g., for a text page, > it is R_X in both the guest page table and the EPT. To what end? If you always mirror what the guest does, you've not actually gained anything. > For text patching, the above code establishes an alternate mapping in > the guest page table that is RW_ so that the text can be patched. That > needs to be reflected in the EPT so that the EPT permissions will > change from R_X to RWX. In other words, RWX is allowed only as > necessary. At the end of patching, the EPT permissions are restored to > R_X. > > Does that address your comment? No, if you want to mirror the native PTEs why don't you hook into the paravirt page-table muck and get all that for free? Also, this is the user range, are you saying you're also playing these daft games with user maps?