From patchwork Mon Feb 3 10:18:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Brodsky X-Patchwork-Id: 13957184 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01B71C02192 for ; Mon, 3 Feb 2025 10:19:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B2F4280010; Mon, 3 Feb 2025 05:19:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 86434280002; Mon, 3 Feb 2025 05:19:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70351280010; Mon, 3 Feb 2025 05:19:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 51A87280002 for ; Mon, 3 Feb 2025 05:19:56 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 444484C821 for ; Mon, 3 Feb 2025 10:19:48 +0000 (UTC) X-FDA: 83078237256.25.D077614 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf15.hostedemail.com (Postfix) with ESMTP id A7B2CA0003 for ; Mon, 3 Feb 2025 10:19:46 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf15.hostedemail.com: domain of kevin.brodsky@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=kevin.brodsky@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738577986; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fSKs8pcmNTnnytgS8+O5jxAzIy+XDlinRu8X5YqLwhU=; b=IcYjWHZLS1/O6G2QbMo/eCdiuRciy+Jy/Ypsfh5KM7ZCDJ/+Za+FFlRuhGQu8Ky7+B0BFG TJIl81Zg2eO2z1oLpVqy3td3qWPfjunJlXzjlXUCi0xiPEFqRHi6Xfax+vh1IN3rm7FxRr HpsV3il5wF2Qai0czM8p/rHDCBazdBU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738577986; a=rsa-sha256; cv=none; b=kjCMHYc9mk1rbUbsx5i0Mrwu6CNCnReiR5CQ23gqyVGkiX0KKjjBBDJHUidocPmP27uFib PQPW3shrHO42xkLaTuFy+bz89Rn2YBHgydofgEVyMt9XPrioL8qcu2FK3OQoPqykXyjyH8 MkpUBVw+4FokzKKrn8QUq4yXwwfvclk= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf15.hostedemail.com: domain of kevin.brodsky@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=kevin.brodsky@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 50A281476; Mon, 3 Feb 2025 02:20:10 -0800 (PST) Received: from e123572-lin.arm.com (e123572-lin.cambridge.arm.com [10.1.194.54]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5E69A3F63F; Mon, 3 Feb 2025 02:19:42 -0800 (PST) From: Kevin Brodsky To: linux-hardening@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Kevin Brodsky , Andrew Morton , Mark Brown , Catalin Marinas , Dave Hansen , Jann Horn , Jeff Xu , Joey Gouly , Kees Cook , Linus Walleij , Andy Lutomirski , Marc Zyngier , Peter Zijlstra , Pierre Langlois , Quentin Perret , "Mike Rapoport (IBM)" , Ryan Roberts , Thomas Gleixner , Will Deacon , Matthew Wilcox , Qi Zheng , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, x86@kernel.org Subject: [RFC PATCH v3 01/15] mm: Introduce kpkeys Date: Mon, 3 Feb 2025 10:18:25 +0000 Message-ID: <20250203101839.1223008-2-kevin.brodsky@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250203101839.1223008-1-kevin.brodsky@arm.com> References: <20250203101839.1223008-1-kevin.brodsky@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A7B2CA0003 X-Stat-Signature: wmn4cmxoez97t8zwcwip6ei96d7j3oga X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1738577986-857202 X-HE-Meta: U2FsdGVkX1+AR/Sbsj/pjmXEw7/yxEIHsQXnY9EL2DSr72pd7EQc8IJG6PHq9g9AqPPW+9f5Zx++dC5rireJaW32sfPxtFy56SdsseNQf4yODeQT4CNIhvCQ8ZOnvm2MYxeqOaiZQy5KRUrYySK89j3IdDfrudhO3wbIyCcjdZAASWxW/5XEy1oE3Wg57xbGPDHOYs8NM5VgIAeiPMvNgKxP/eVM3xXVojqDJ7dAijQci6P3U6rRJkNVreGSlNTN6lQ4d/4gNbj0RFml58Ql0sFwmQxkh5N4g0nhJnEsdgzlOAPC++a5l79AEocjJuXqhqaKJEjRc9ePLXWdc78JSDwDQtlWWXuYqcVO/ss8aJSGtwN5XI/mmSm6Ys6H+8i3zE9hNiHQHXqIJ9jFxNKF3uxYSZPzkIUZJnr30H+e2Yjq/Nl/ABlzV8clu2fA8x1AOnkDhmaMvJWbPPypp1JGEgMOFRppFyt0zUjR5DJoucbkN8zk343vxGBMCMsqMcKdAitC3cRFIv+jD8VLcV/27zI07ElaVawBQFyQwBIGWWuLocHokoNJKJYYrqh3DnEBj0InzN9J2SjBJz0P/oRgjgzlk3xkXZJGo/2/4LcoVy2zqDo3EsAGAkJ7gN19Bd8nweO81zrWUty3CVOu5djbriQ0o9XCKlLHBWx5orjHMnBKnckEBWBIJ1txgTziIjf7wQgHCzCi4RL7OHCMgjm6YOSIMVU7oxDiK1pPHFGJSwvq6jTm6EIop9tyH/94TIODJfKVJo0EZYdPpPOEeyEgBoUUOsVkIfKnLQjcQyWIG+OQobpfFzrfTrT1pUu29f7TzyU0gxZhE6CJ2VYTP0fouW8Ji+r7nxGUWpwgI967Pl6vT+75ibFupKePui9r+NCIOP3J+es1PeO1DbGn3KmjU6mUAcdkcbN61nHgS8TVubv5lJpoe0QsBsFwQiuapccpiMfvXlaMFCAGlVoEnDb we3JQ0O8 /NSW5OEwasgJM76qvMfNcQhj70gzO96h7vdnIBhj3TLwiG+mSUQvKhIMd3KrEPx7bymqb6gpDr25nlEJCf7DZFrsITn6m5P32sgPjUnAwkdoVcsHIumu8jBwjtIw6PU1vvt6PmTFO7rKCWigBSQZhTfoD3xG/MP1UMD/vAhBXZR0NRI0SO7T5z1CiriPchteoPtcQ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 level. Each level is associated to a set of permissions for the pkeys managed by the kpkeys framework. kpkeys_set_level(lvl) sets those permissions according to lvl, and returns the original pkey register, to be later restored by kpkeys_restore_pkey_reg(). To start with, only KPKEYS_LVL_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_level() - arch_kpkeys_restore_pkey_reg() - arch_kpkeys_enabled() Signed-off-by: Kevin Brodsky --- include/asm-generic/kpkeys.h | 17 ++++++ include/linux/kpkeys.h | 113 +++++++++++++++++++++++++++++++++++ mm/Kconfig | 2 + 3 files changed, 132 insertions(+) create mode 100644 include/asm-generic/kpkeys.h create mode 100644 include/linux/kpkeys.h 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 + +/* + * 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..62f897c65658 --- /dev/null +++ b/include/linux/kpkeys.h @@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_KPKEYS_H +#define _LINUX_KPKEYS_H + +#include +#include + +#define KPKEYS_LVL_DEFAULT 0 + +#define KPKEYS_LVL_MIN KPKEYS_LVL_DEFAULT +#define KPKEYS_LVL_MAX KPKEYS_LVL_DEFAULT + +#define __KPKEYS_GUARD(name, set_level, restore_pkey_reg, set_arg, ...) \ + __DEFINE_CLASS_IS_CONDITIONAL(name, false); \ + DEFINE_CLASS(name, u64, \ + restore_pkey_reg, set_level, 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 level + * @name: the name of the guard type + * @level: the kpkeys level 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 @level 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, level, cond, ...) \ + __KPKEYS_GUARD(name, \ + cond ? kpkeys_set_level(level) \ + : KPKEYS_PKEY_REG_INVAL, \ + kpkeys_restore_pkey_reg(_T), \ + ##__VA_ARGS__, void) + +/** + * KPKEYS_GUARD() - define a guard type that switches to a given kpkeys level + * if kpkeys are enabled + * @name: the name of the guard type + * @level: the kpkeys level to switch to + * + * Define a guard type that switches to @level if the system supports kpkeys. + */ +#define KPKEYS_GUARD(name, level) \ + KPKEYS_GUARD_COND(name, level, arch_kpkeys_enabled()) + +/** + * kpkeys_set_level() - switch kpkeys level + * @level: the level to switch to + * + * Switches the kpkeys level to the specified value. @level must be a + * compile-time constant. The arch-specific pkey register will be updated + * accordingly, and the original value returned. + * + * 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). + */ +static inline u64 kpkeys_set_level(int level) +{ + BUILD_BUG_ON_MSG(!__builtin_constant_p(level), + "kpkeys_set_level() only takes constant levels"); + BUILD_BUG_ON_MSG(level < KPKEYS_LVL_MIN || level > KPKEYS_LVL_MAX, + "Invalid level passed to kpkeys_set_level()"); + + return arch_kpkeys_set_level(level); +} + +/** + * 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_level(), + * in order to restore the pkey register to its original value (thus restoring + * the original kpkeys level). + */ +static inline void kpkeys_restore_pkey_reg(u64 pkey_reg) +{ + if (pkey_reg != KPKEYS_PKEY_REG_INVAL) + arch_kpkeys_restore_pkey_reg(pkey_reg); +} + +#else /* CONFIG_ARCH_HAS_KPKEYS */ + +#include + +static inline bool arch_kpkeys_enabled(void) +{ + return false; +} + +#endif /* CONFIG_ARCH_HAS_KPKEYS */ + +#endif /* _LINUX_KPKEYS_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 1b501db06417..71edc478f111 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1147,6 +1147,8 @@ config ARCH_USES_HIGH_VMA_FLAGS bool config ARCH_HAS_PKEYS bool +config ARCH_HAS_KPKEYS + bool config ARCH_USES_PG_ARCH_2 bool