kpkeys_hardened_pgtables is a hardening feature based on kpkeys. It aims to prevent the corruption of page tables by: 1. mapping all page table pages, both kernel and user, with a privileged pkey (KPKEYS_PKEY_PGTABLES), and 2. granting write access to that pkey only when running at in a privileged kpkeys context (KPKEYS_CTX_PGTABLES). This patch introduces basic infrastructure; the implementation of both aspects follows. The feature is exposed as CONFIG_KPKEYS_HARDENED_PGTABLES; it requires explicit architecture opt-in by selecting ARCH_HAS_KPKEYS_HARDENED_PGTABLES, since much of the page table handling is arch-specific. It also depends on ARCH_HAS_SET_DIRECT_MAP to modify the direct mapping of page tables using set_direct_map_pkey(). Supported architectures must also provide arch_supports_kpkeys_early() in . This will be used during early boot to detect whether kpkeys_hardened_pgtables is going to be enabled (e.g. to decide how to allocate early page tables). Because this feature relies on kpkeys being supported and modifies attributes of the linear map, it must be inactive during early boot. mm_core_init() enables it before any call to pagetable_alloc() is made. Signed-off-by: Kevin Brodsky --- include/linux/kpkeys.h | 33 +++++++++++++++++++++++++++++++++ include/linux/kpkeys_types.h | 5 +++++ mm/Kconfig | 3 +++ mm/Makefile | 1 + mm/kpkeys_hardened_pgtables.c | 16 ++++++++++++++++ mm/mm_init.c | 2 ++ security/Kconfig.hardening | 12 ++++++++++++ 7 files changed, 72 insertions(+) diff --git a/include/linux/kpkeys.h b/include/linux/kpkeys.h index 69ec4808d850..cd3281223778 100644 --- a/include/linux/kpkeys.h +++ b/include/linux/kpkeys.h @@ -4,6 +4,7 @@ #include #include +#include #include /** @@ -104,4 +105,36 @@ static inline bool kpkeys_supported(void) #endif /* CONFIG_ARCH_HAS_KPKEYS */ +#ifdef CONFIG_KPKEYS_HARDENED_PGTABLES + +DECLARE_STATIC_KEY_FALSE(kpkeys_hardened_pgtables_key); + +static inline bool kpkeys_hardened_pgtables_enabled(void) +{ + return static_branch_unlikely(&kpkeys_hardened_pgtables_key); +} + +static inline bool kpkeys_hardened_pgtables_early_enabled(void) +{ + return arch_supports_kpkeys_early(); +} + +void kpkeys_hardened_pgtables_init(void); + +#else /* CONFIG_KPKEYS_HARDENED_PGTABLES */ + +static inline bool kpkeys_hardened_pgtables_enabled(void) +{ + return false; +} + +static inline bool kpkeys_hardened_pgtables_early_enabled(void) +{ + return false; +} + +static inline void kpkeys_hardened_pgtables_init(void) {} + +#endif /* CONFIG_KPKEYS_HARDENED_PGTABLES */ + #endif /* _LINUX_KPKEYS_H */ diff --git a/include/linux/kpkeys_types.h b/include/linux/kpkeys_types.h index 9de59092e9c3..09f15a9565cf 100644 --- a/include/linux/kpkeys_types.h +++ b/include/linux/kpkeys_types.h @@ -8,10 +8,15 @@ #include #endif +#ifndef KPKEYS_PKEY_PGTABLES +#define KPKEYS_PKEY_PGTABLES 1 +#endif + #if defined(CONFIG_ARCH_HAS_KPKEYS) && !defined(__ASSEMBLY__) enum kpkeys_ctx { KPKEYS_CTX_DEFAULT = 0, + KPKEYS_CTX_PGTABLES, KPKEYS_CTX_COUNT, }; diff --git a/mm/Kconfig b/mm/Kconfig index 4635628de622..cf3b6105c4ad 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1267,6 +1267,9 @@ config ARCH_HAS_PKEYS bool config ARCH_HAS_KPKEYS bool +config ARCH_HAS_KPKEYS_HARDENED_PGTABLES + bool + depends on ARCH_HAS_KPKEYS && ARCH_HAS_SET_DIRECT_MAP config ARCH_USES_PG_ARCH_2 bool diff --git a/mm/Makefile b/mm/Makefile index 2a3ec53d62ee..07b67eaa1b3b 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -149,3 +149,4 @@ obj-$(CONFIG_EXECMEM) += execmem.o obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) += tests/lazy_mmu_mode_kunit.o obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o +obj-$(CONFIG_KPKEYS_HARDENED_PGTABLES) += kpkeys_hardened_pgtables.o diff --git a/mm/kpkeys_hardened_pgtables.c b/mm/kpkeys_hardened_pgtables.c new file mode 100644 index 000000000000..4c654e0cf585 --- /dev/null +++ b/mm/kpkeys_hardened_pgtables.c @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include + +#include + +__ro_after_init DEFINE_STATIC_KEY_FALSE(kpkeys_hardened_pgtables_key); +EXPORT_SYMBOL_IF_KUNIT(kpkeys_hardened_pgtables_key); + +void __init kpkeys_hardened_pgtables_init(void) +{ + if (!kpkeys_supported()) + return; + + static_branch_enable(&kpkeys_hardened_pgtables_key); +} diff --git a/mm/mm_init.c b/mm/mm_init.c index d99af80ec752..347246135ce1 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -2733,6 +2734,7 @@ void __init mm_core_init(void) kmemleak_init(); ptlock_cache_init(); pgtable_cache_init(); + kpkeys_hardened_pgtables_init(); debug_objects_mem_init(); vmalloc_init(); /* If no deferred init page_ext now, as vmap is fully initialized */ diff --git a/security/Kconfig.hardening b/security/Kconfig.hardening index 81c81ad983ad..9a6f50103482 100644 --- a/security/Kconfig.hardening +++ b/security/Kconfig.hardening @@ -271,6 +271,18 @@ config BUG_ON_DATA_CORRUPTION If unsure, say N. +config KPKEYS_HARDENED_PGTABLES + bool "Harden page tables using kernel pkeys" + depends on ARCH_HAS_KPKEYS_HARDENED_PGTABLES + help + This option makes all page tables mostly read-only by + allocating them with a non-default protection key (pkey) and + only enabling write access to that pkey in routines that are + expected to write to page table entries. + + This option has no effect if the system does not support + kernel pkeys. + endmenu config CC_HAS_RANDSTRUCT -- 2.56.0