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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 16426CD98E4 for ; Tue, 16 Jun 2026 15:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=r4HeNsdpZXcuGDpdJwc1l324i0d9msH0jiY923pAvrc=; b=CDZxJk6TCegQVVNmiKrJZq8nJt NrZzXvVZ3NyBDk0FYPINYqi5g0rLBvdHIwl0Q9+M2GIeNtEqyFfrb7GyVRl/FEhUY6z9OwtLPF5c1 GRSd8bWEtVi322byR7GrLIJDFepxr2kpbZSyH6tcbXRXBu4XenRJT84OSN92m+YK5RMGDnHaVXmq7 2RIh9qk82c4yHnAxtNcp82cARw/m/OhBT3WJaDXj9aIwWu1zMQSVUAyjBPO88EzaccNmrhP+wVzx4 N1j60nVrcOxM7nVjWJ4Uko+Vrw9wOy953LyT9ab1yQJtMlcWRrB2cQtNPtc92wiUP0XjDDxH2Yu8U orONXbZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wZVZc-0000000Fzpd-2OUy; Tue, 16 Jun 2026 15:19:32 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wZVZa-0000000FzpV-3yYF for linux-arm-kernel@lists.infradead.org; Tue, 16 Jun 2026 15:19:31 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id E6F116012A; Tue, 16 Jun 2026 15:19:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15C961F00A3A; Tue, 16 Jun 2026 15:19:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1781623169; bh=r4HeNsdpZXcuGDpdJwc1l324i0d9msH0jiY923pAvrc=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=Ck1DRL22ljLe93iK/7HgtsYxeBKDxBCFPWNpfgWDBiVxkCWICUXwCIPlsaTKuJuhs biK6K8nc9I2zI8He0La3nV8IU8ak0Atra2CONHCbod28hTjnrKvyEfkj4VfeW53F1f GHQ3vk6KZDZD3RaqEf1baP32aHjSwcsvZMgaYvKICu+3us71lDvpfucBjbXqNpMP3t /QHtweoSA0bIaoRc6cbNqeqGC2oiiptTvR0x2HTdG2GRYQWsjg3jaaiy2CrPP2v0Sn hMxk+u60vSWW3+yjH/D02PCNUyXJ2gH95+b58HXM4lHcsFY1TOkWbafCI2y4I75VyO 1xBe5AlPkpqTw== Message-ID: <70f4dcdf-c45f-47d3-91df-a7897bd86ff4@kernel.org> Date: Tue, 16 Jun 2026 17:19:22 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH RFC v8 01/24] mm: Introduce kpkeys To: Kevin Brodsky , linux-hardening@vger.kernel.org Cc: Andrew Morton , Andy Lutomirski , Catalin Marinas , Dave Hansen , Ira Weiny , Jann Horn , Jeff Xu , Joey Gouly , Kees Cook , Linus Walleij , Marc Zyngier , Mark Brown , Matthew Wilcox , Maxwell Bland , "Mike Rapoport (IBM)" , Peter Zijlstra , Pierre Langlois , Quentin Perret , Rick Edgecombe , Ryan Roberts , Vlastimil Babka , Will Deacon , Yang Shi , Yeoreum Yun , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, x86@kernel.org, Lorenzo Stoakes , Thomas Gleixner References: <20260526-kpkeys-v8-0-eaaacdacc67c@arm.com> <20260526-kpkeys-v8-1-eaaacdacc67c@arm.com> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzS5EYXZpZCBIaWxk ZW5icmFuZCAoQ3VycmVudCkgPGRhdmlkQGtlcm5lbC5vcmc+wsGQBBMBCAA6AhsDBQkmWAik AgsJBBUKCQgCFgICHgUCF4AWIQQb2cqtc1xMOkYN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CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: <20260526-kpkeys-v8-1-eaaacdacc67c@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 5/26/26 13:15, Kevin Brodsky wrote: > kpkeys is a simple framework to enable the use of protection keys > (pkeys) to harden the kernel itself. This patch introduces the basic > API in : a couple of functions to set and restore > the pkey register and macros to define guard objects. > > kpkeys introduces a new concept on top of pkeys: the kpkeys context. > Each context is associated to a set of permissions for the pkeys > managed by the kpkeys framework. kpkeys_set_context(ctx) sets those > permissions according to ctx, and returns the original pkey > register, to be later restored by kpkeys_restore_pkey_reg(). To > start with, only KPKEYS_CTX_DEFAULT is available, which is meant to > grant RW access to KPKEYS_PKEY_DEFAULT (i.e. all memory since this > is the only available pkey for now). > > Because each architecture implementing pkeys uses a different > representation for the pkey register, and may reserve certain pkeys > for specific uses, support for kpkeys must be explicitly indicated > by selecting ARCH_HAS_KPKEYS and defining the following functions in > , in addition to the macros provided in > : > > - arch_kpkeys_set_context() > - arch_kpkeys_restore_pkey_reg() Looking at this, and wondering about "why do we get registers involved in this API" I would probably have an interface like: arch_kpkeys_enter_context() arch_kpkeys_leave_context() Whereby you return a "struct kpkeys_state" or sth like that. You could either let the architecture define what's in the state, or alternatively store some generic data in there as well. struct kpkeys_state { bool entered_context; struct arch_pkey_state arch; }; Maybe the "entered_context" or however you would want to call it could avoid the KPKEYS_PKEY_REG_INVAL (which confuses me ;) )? But the KPKEYS_PKEY_REG_INVAL usage confuses me. I understand the KPKEYS_GUARD_COND + kpkeys_restore_pkey_reg() one, but not the one where arch_kpkeys_set_context() would return that value. > > Signed-off-by: Kevin Brodsky > --- > include/asm-generic/kpkeys.h | 17 ++++++ > include/linux/kpkeys.h | 122 +++++++++++++++++++++++++++++++++++++++++++ > mm/Kconfig | 2 + > 3 files changed, 141 insertions(+) > > diff --git a/include/asm-generic/kpkeys.h b/include/asm-generic/kpkeys.h > new file mode 100644 > index 000000000000..ab819f157d6a > --- /dev/null > +++ b/include/asm-generic/kpkeys.h > @@ -0,0 +1,17 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > +#ifndef __ASM_GENERIC_KPKEYS_H > +#define __ASM_GENERIC_KPKEYS_H > + > +#ifndef KPKEYS_PKEY_DEFAULT > +#define KPKEYS_PKEY_DEFAULT 0 > +#endif Do we currently expect an architecture to overwrite this? How does this interact with KPKEYS_CTX_DEFAULT? Nobody in this patch uses it, so maybe it should be added where actually needed. > + > +/* > + * Represents a pkey register value that cannot be used, typically disabling > + * access to all keys. > + */ > +#ifndef KPKEYS_PKEY_REG_INVAL > +#define KPKEYS_PKEY_REG_INVAL 0 > +#endif > + > +#endif /* __ASM_GENERIC_KPKEYS_H */ > diff --git a/include/linux/kpkeys.h b/include/linux/kpkeys.h > new file mode 100644 > index 000000000000..0ce0db80b4ee > --- /dev/null > +++ b/include/linux/kpkeys.h > @@ -0,0 +1,122 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > +#ifndef _LINUX_KPKEYS_H > +#define _LINUX_KPKEYS_H > + > +#include > +#include > + > +#define KPKEYS_CTX_DEFAULT 0 Agreed with enum. > + > +#define KPKEYS_CTX_MIN KPKEYS_CTX_DEFAULT > +#define KPKEYS_CTX_MAX KPKEYS_CTX_DEFAULT > + > +/* > + * ... is used to discard extra arguments - this allows users of this macro > + * to have set_arg default to void. > + */ > +#define __KPKEYS_GUARD(name, set_context, restore_pkey_reg, set_arg, ...) \ > + __DEFINE_CLASS_IS_CONDITIONAL(name, false); \ > + DEFINE_CLASS(name, u64, \ > + restore_pkey_reg, set_context, set_arg); \ > + static inline void *class_##name##_lock_ptr(u64 *_T) \ > + { return _T; } > + > +/** > + * KPKEYS_GUARD_NOOP() - define a guard type that does nothing > + * @name: the name of the guard type > + * @cond_arg: an argument specification (optional) > + * > + * Define a guard type that does nothing, useful to match a real guard type > + * that is defined under an #ifdef. @cond_arg may optionally be passed to match > + * a guard defined using KPKEYS_GUARD_COND(). > + */ > +#define KPKEYS_GUARD_NOOP(name, ...) \ > + __KPKEYS_GUARD(name, 0, (void)_T, ##__VA_ARGS__, void) > + > +#ifdef CONFIG_ARCH_HAS_KPKEYS > + > +#include > + > +/** > + * KPKEYS_GUARD_COND() - define a guard type that conditionally switches to > + * a given kpkeys context > + * @name: the name of the guard type > + * @ctx: the kpkeys context to switch to > + * @cond: an expression that is evaluated as condition > + * @cond_arg: an argument specification for the condition (optional) > + * > + * Define a guard type that switches to @ctx if @cond evaluates to true, > + * and does nothing otherwise. @cond_arg may be specified to give access to a > + * caller-defined argument to @cond. > + */ > +#define KPKEYS_GUARD_COND(name, ctx, cond, ...) \ > + __KPKEYS_GUARD(name, \ > + (cond) ? kpkeys_set_context(ctx) \ > + : KPKEYS_PKEY_REG_INVAL, \ > + kpkeys_restore_pkey_reg(_T), \ > + ##__VA_ARGS__, void) > + > +/** > + * KPKEYS_GUARD() - define a guard type that switches to a given kpkeys context > + * if kpkeys are enabled > + * @name: the name of the guard type > + * @ctx: the kpkeys context to switch to > + * > + * Define a guard type that switches to @ctx if the system supports kpkeys. > + */ > +#define KPKEYS_GUARD(name, ctx) \ > + KPKEYS_GUARD_COND(name, ctx, kpkeys_enabled()) > + > +/** > + * kpkeys_set_context() - switch kpkeys context > + * @ctx: the context to switch to > + * > + * Switches to specified kpkeys context. @ctx must be a compile-time > + * constant. The arch-specific pkey register will be updated accordingly, and > + * the original value returned. Are these arch details and registers relevant? Ideally, we'd keep it very simple here ... > + * > + * Return: the original pkey register value if the register was written to, or > + * KPKEYS_PKEY_REG_INVAL otherwise (no write to the register was > + * required). ... and here. Not sure if any caller cares about these details. Again, with some abstract state we could maybe handle that internally. "Return: the pkey state to pass to kpkeys_restore_pkey_reg" (or however that function will be called) > + */ > +static __always_inline u64 kpkeys_set_context(int ctx) > +{ > + BUILD_BUG_ON_MSG(!__builtin_constant_p(ctx), > + "kpkeys_set_context() only takes constant values"); > + BUILD_BUG_ON_MSG(ctx < KPKEYS_CTX_MIN || ctx > KPKEYS_CTX_MAX, > + "Invalid value passed to kpkeys_set_context()"); > + > + return arch_kpkeys_set_context(ctx); > +} > + > +/** > + * kpkeys_restore_pkey_reg() - restores a pkey register value > + * @pkey_reg: the pkey register value to restore > + * > + * This function is meant to be passed the value returned by > + * kpkeys_set_context(), in order to restore the pkey register to its original > + * value (thus restoring the original kpkeys context). > + */ > +static __always_inline void kpkeys_restore_pkey_reg(u64 pkey_reg) > +{ > + if (pkey_reg != KPKEYS_PKEY_REG_INVAL) > + arch_kpkeys_restore_pkey_reg(pkey_reg); > +} > + > +static inline bool kpkeys_enabled(void) Is the enabled vs. supported intentional? > +{ > + return arch_supports_kpkeys(); > +} > + -- Cheers, David