pkeys-based page table hardening
Subsystems (6)
v10 cover pkeys-based page table hardening
NEW in RFC v10: less invasive approach to protect early page tables (see section "Page table mappings")
This is a proposal to leverage protection keys (pkeys) to harden critical kernel data, by making it mostly read-only. The series includes a simple framework called "kpkeys" to manipulate pkeys for in-kernel use, as well as a page table hardening feature based on that framework, "kpkeys_hardened_pgtables". Both are implemented on arm64 as a proof of concept, but they are designed to be compatible with any architecture that supports pkeys.
The proposed approach is a typical use of pkeys: the data to protect is mapped with a given pkey P, and the pkeys register is initially configured to grant read-only access to P. Where the protected data needs to be written to, the pkeys register is temporarily switched to grant write access to P on the current CPU.
The key fact this approach relies on is that the target data is only written to via a limited and well-defined API. This makes it possible to explicitly switch the pkeys register where needed, without introducing excessively invasive changes, and only for a small amount of trusted code.
Page tables are chosen as an initial target because of their especially critical nature - a single write may result in arbitrary pages becoming accessible to any context (including userspace). In order to keep the series digestible for reviewers, this RFC focuses on functionality rather than performance, making it most suitable as a debug feature. The key trade-off is the requirement to PTE-map the direct map - see section "Page table mappings" for details.
This series has similarities with the "PKS write protected page tables" series posted by Rick Edgecombe a few years ago [1] but it is not specific to x86/PKS - the approach is meant to be generic.
This proposal (as of RFC v5) was presented at Linux Security Summit Europe 2025 [2].
[Table of contents]
* kpkeys
- pkeys register management
* kpkeys_hardened_pgtables
- Page table mappings
- kpkeys context switching
- Performance
* This series
- Branches
* Threat model
* Further use-cases
* Limitations/Known issues
kpkeys
======
kpkeys is a simple API meant to manage a subset of the protection keys available to the kernel. It is based on the notion of "kpkeys context": a simple integer that defines the permissions for every pkey managed by kpkeys. Other pkeys are unaffected by calls to the API (i.e. their permissions remain unchanged).
The use of pkeys involves two separate mechanisms: assigning a pkey to pages, and defining the pkeys -> permissions mapping via the pkey register. This is implemented through the following interface:
- Pages are assigned a pkey in the direct map using set_direct_map_pkey(). This is sufficient for this series, but it is also plausible for higher-level allocators to support marking allocations with a given pkey.
- The pkeys register is configured when switching to a kpkeys context. kpkeys contexts are associated with a permission set for every managed pkey, for instance:
KPKEYS_CTX_DEFAULT:
RW access to pkey 0
RO access to any other KPKEYS_PKEY_*
KPKEYS_CTX_<FEAT>:
RW access to pkey 0
RW access to KPKEYS_PKEY_<FEAT>
RO access to any other KPKEYS_PKEY_*
Only pkeys that are managed by the kpkeys framework are impacted; permissions for other pkeys are left unchanged (this allows for other schemes using pkeys to be used in parallel, and arch-specific use of certain pkeys).
The current kpkeys context is changed by calling kpkeys_enter_context(), which sets the pkeys register accordingly and returns the original state. A subsequent call to kpkeys_leave_context() restores the original state (and thus the original kpkeys context). The numeric value of KPKEYS_CTX_* (kpkeys context) is purely symbolic and thus generic, however each architecture is free to define non-default pkeys values (KPKEYS_PKEY_*).
Note: the nested use of kpkeys contexts has to be considered carefully. That is, while running under KPKEYS_CTX_FEAT1, switching to KPKEYS_CTX_FEAT2 may result in certain pkeys no longer being accessible. If need be (for a potential future use-case), the API could be extended to allow certain permissions to be inherited rather than overwritten on a kpkeys context switch.
pkeys register management
-------------------------
The kpkeys model relies on the kernel pkeys register being set to a specific value for the duration of a relatively small section of code, and otherwise to the default value. Two aspects should be considered:
1. Exception entry/return
2. Thread context-switch
Ideally, an implementation would switch the pkeys register in both
cases: 1. reset it to a fixed state on exception entry, and 2. context-switch it along with the other thread registers.
In this series, the arm64 implementation only performs 2. i.e. context-switching POR_EL1 per-thread. This ensures that each thread's pkeys register is preserved no matter how a thread is rescheduled (voluntarily or not).
Resetting POR_EL1 on exception entry proved problematic for the kpkeys_hardened_pgtables feature with the lazy MMU optimisation (see next section), because page table setters may be used immediately when a thread resumes after being rescheduled on irqexit, before exception return. If POR_EL1 is reset on exception entry, we may end up in an inconsistent state where lazy MMU mode may be enabled but POR_EL1 does not allow writing to page tables until exception return. For more details, see [3].
Having the entire exception handling inherit the interrupted POR_EL1 is not ideal as it increases the amount of code run with a potentially privileged kpkeys state. It may be possible to pause the lazy MMU mode to avoid the issue described above, but it isn't clear whether this is a general enough solution.
An important assumption is that all kpkeys contexts allow RW access to the default pkey (0). Context-switching and saving/restoring POR_EL1 on exception entry/return becomes significantly more complex otherwise.
kpkeys_hardened_pgtables
========================
The kpkeys_hardened_pgtables feature uses the interface above to make the (kernel and user) page tables read-only by default, enabling write access only in helpers such as set_pte().
Page table mappings
-------------------
Kernel page tables are required as soon as the MMU is turned on, i.e. extremely early. Different allocators are therefore used to obtain page table pages (PTPs), depending on where we are in the boot sequence. In chronological order:
1. Static pools (in the kernel image itself). Typically used to map the kernel image. By definition these pools have a fixed size.
2. memblock. Mainly used to create the direct map and sparse-vmemmap page tables.
3. Buddy allocator, via pagetable_alloc(). Used for everything else, including user page tables, once available.
No matter which allocator is used, this feature relies on the direct map being modified to map PTPs with KPKEYS_PKEY_PGTABLES. If large block mappings are used to create the direct map, blocks may need to be split when setting the pkey for individual PTPs, which in turn requires a new PTP to be allocated. RFC v6 introduced a PTP allocator that both handles splitting and minimises it. This fairly complex component was removed in RFC v7 so that reviewers can focus on the core logic of the feature.
The present series therefore requires the direct map to be fully PTE-mapped, so that PTPs can be individually protected without worrying about block splitting.
The different sources of PTPs listed above are protected as follows:
1. and 2. Early page tables: when the feature is turned on, the kernel page tables are walked and all backing pages are remapped with the appropriate pkey. This ensures that all installed page tables (for the kernel image, the direct map itself, sparse-vmemmap, etc.) are protected. This simple approach is safe because we know the direct map is PTE-mapped (no splitting can occur while walking the page tables).
3. Regular page tables: directly obtained from the buddy allocator and their pkey set right away.
All this infrastructure is generic. This series only includes the required plumbing for arm64, but it is meant to be plumbed into x86 in a similar manner.
kpkeys context switching
------------------------
Page tables are only supposed to be written via a fairly small set of helpers, so only a few switches to a privileged kpkeys context need to be introduced.
However, a naive implementation is very inefficient when many PTEs are changed at once, as the kpkeys context is switched twice for every PTE. On arm64, this means two barriers (ISBs) per PTE. This is optimised by making use of the lazy MMU mode to batch those switches: 1. switch to KPKEYS_CTX_PGTABLES on arch_enter_lazy_mmu_mode(), 2. skip any kpkeys switch while in that section, and 3. restore the kpkeys context on arch_leave_lazy_mmu_mode(). When that last function already issues an ISB (when updating kernel page tables), we get a further optimisation as we can skip the ISB when restoring the kpkeys context.
The lazy MMU mode may be enabled/disabled in a nested manner. This is handled thanks to [5] - if nesting occurs, only the outer section will switch kpkeys context.
Performance
-----------
No arm64 hardware currently implements POE. Benchmarking has however been performed on a mock implementation, which should be fairly representative. On a "real-world" and fork-heavy workload like kernbench, the estimated overhead is around 2% in system time, while the real time overhead is negligible. Microbenchmarks focused on creating/destroying mappings yield overheads up to 15%, but typically much smaller once batching kicks in (when modifying many PTEs at once).
For more details, see this section in RFC v5 and v6 (linked in the changelog).
This series
===========
The series is composed of two parts:
- The kpkeys framework (patch 1-10). The main API is introduced in <linux/kpkeys.h>, and it is implemented on arm64 using the POE (Permission Overlay Extension) feature.
- The kpkeys_hardened_pgtables feature (patch 11-22). <linux/kpkeys.h> is extended with an API to protect page table pages and a guard object to switch kpkeys context accordingly, both gated on a static key. This is then used in generic and arm64 pgtable handling code as needed. Finally a simple KUnit-based test suite is added to demonstrate the page table protection.
Branches
--------
To make reviewing and testing easier, this series is available here:
https://gitlab.arm.com/linux-arm/linux-kb
The following branches are available:
- kpkeys/rfc-v10 - this series, as posted
Threat model
============
The proposed scheme aims at mitigating data-only attacks (e.g. use-after-free/cross-cache attacks). In other words, it is assumed that control flow is not corrupted, and that the attacker does not achieve arbitrary code execution. Nothing prevents the pkeys register from being set to its most permissive state - the assumption is that the register is only modified on legitimate code paths.
A few related notes:
- Functions that set the pkeys register are all implemented inline. Besides performance considerations, this is meant to avoid creating a function that can be used as a straightforward gadget to set the pkeys register to an arbitrary value.
- kpkeys_enter_context() only accepts a compile-time constant as argument, as a variable could be manipulated by an attacker. This could be relaxed but it seems unlikely that a variable kpkeys context would be needed in practice.
Further use-cases
=================
It should be possible to harden various targets using kpkeys, including:
- struct cred - proposal posted as a separate series [6]
- eBPF programs (preventing direct W/X to core kernel code/data) [7]
- SELinux state (e.g. struct selinux_state::initialized)
... and many others.
kpkeys could also be used to strengthen the confidentiality of secret data by making it completely inaccessible by default, and granting read-only or read-write access as needed. This requires such data to be rarely accessed (or via a limited interface only). One example on arm64 is the pointer authentication keys in thread_struct, whose leakage to userspace would lead to pointer authentication being easily defeated.
Limitations/Known issues
========================
- The direct map is forced to be PTE-mapped; large block support requires significantly more logic. See section "Protected page table allocation" and RFC v6 for a possible implementation.
- The fixmap page tables are not fully protected. Walking the early kernel page tables ensures that their direct map alias is protected, but they remain writable in the kernel image. bm_pte is normally written via the kimg alias, so we should change its pkey as well. See also Ard's series protecting other levels without kpkeys. [15]
- This series hasn't been tested with KASAN. When enabled, the kernel page table walk in patch 13 becomes very inefficient because page table pages get reused by shadow mappings; these additional mappings should be skipped (like in ptdump).
- This series has only been tested with 4K pages and 48-bit VAs. Other configurations should work just fine, but the tests make too many assumptions (as reported by Sashiko).
Any comment or feedback is highly appreciated, be it on the high-level approach or implementation choices!
An LLM (Codex) was used to generate some portions of the code and commit messages; I understand and carefully reviewed all generated contents.
---
Changelog
RFC v9..v10:
- Reworked early page table protection: walk the installed kernel page tables instead of modifying early allocators. This approach has several limitations (especially if block mappings are to be supported in the future), but it significantly simplifies the series. [David Hildenbrand]
* Patch 13 implements this approach using the pagewalk API, protecting both statically allocated and memblock-allocated page tables when enabling kpkeys_hardened_pgtables. (The same approach was used until RFC v5, see RFC v5 patch 10.)
* Dropped RFC v9 patches 10, 13 and 19, which introduced and used the dedicated early page table allocator, and patches 15 and 21, which introduced and implemented the static page table protection hook.
* Patch 14 no longer handles early-allocated vmemmap page tables, as the page table walk handles them too.
- Patch 1, 4: the architecture hooks now only handle struct arch_kpkeys_state; enter() returns whether the context was actually modified. The generic API manages the generic part of struct kpkeys_state (.entered_context). [David Hildenbrand]
- Patch 2: renamed set_memory_pkey() to set_direct_map_pkey(), in line with other functions that are restricted to modifying the direct map.
- Patch 3: use field_prep() to encode permissions in por_elx_set_pkey_perms(). [Linu Cherian]
- Patch 4: added compiler barriers around POR_EL1 writes to prevent memory accesses from being reordered across kpkeys context switches. [Sashiko]
- Patch 5: check can_set_direct_map() [Sashiko]
- Added patch 6 to disable EL2 trapping of POR_EL1 accesses.
- Patch 11:
* Call kpkeys_hardened_pgtables_init() directly from mm_core_init(), rather than forcing every supported architecture to call it from mem_init(). [Mike Rapoport]
* Require ARCH_HAS_SET_DIRECT_MAP. [Sashiko]
- Patch 18: added missing kpkeys context switch in ptep_try_set(). [Sashiko]
- Improved the cover letter. [Linu]
- Collected R-b's from Linu and Yeoreum (thanks!).
- Rebased on v7.4-rc2.
RFC v9: https://lore.kernel.org/all/[email protected]/
RFC v8..v9:
- Patch 1 [David Hildenbrand, Linus Walleij]:
* Abstracted the pkey register using a new struct arch_kpkeys_state, itself contained in struct kpkeys_state. The main interface is now kpkeys_{enter,leave}_context().
* A bool in struct kpkeys_state is now used to skip the context restore, instead of using a magic pkey register value (KPKEYS_PKEY_REG_INVAL).
* Context values are now defined as enum kpkeys_ctx.
* All types are defined in a separate kpkeys_types.h to avoid circular dependencies. This also replaces <asm-generic/kpkeys.h>.
* Renamed kpkeys_enabled() to kpkeys_supported().
* Replaced KPKEYS_PKEY_DEFAULT with hardcoded 0 (this is more consistent with user pkeys, and that value cannot easily be changed).
- Patch 4: changed type of POE permissions to u8, clarified commit message. [David H]
- Patch 5:
* Aligned with new API in patch 1.
* Added check to skip POR_EL1 updates if we have already entered the requested context (no value change).
* Made prev_por/new_por const. [David H]
- Added patch 20 to make the kernel image alias of init_pg_dir read-only. [Sashiko]
- Patch 23:
* Aligned with patch 1 by storing a full struct kpkeys_state in thread_struct.
* Made kpkeys_lazy_mmu_{enter,exit} static inline. [Sashiko]
- Rebased on v7.2.
RFC v8: https://lore.kernel.org/all/[email protected]/
RFC v7..v8:
[Changes below are addressing issues reported by Sashiko [13]]
- Patch 3: do not enable overlays for user memory types in POR_EL1 - this breaks the atomic futex uaccess operations. Added some comment and test in a separate series [14] to clarify this.
- Patch 5: fix return type of arch_kpkeys_set_context()
- Patch 7: reset POR_EL1 to POR_EL1_INIT when cloning a thread. The pkey register value is tied to the context of the original thread (e.g. being in lazy MMU mode) and should not be inherited by the new thread in the general case.
- Patch 11: make ARCH_HAS_KPKEYS_HARDENED_PGTABLES explicitly depend on ARCH_HAS_KPKEYS (not sure why I didn't do that to start with...)
- Patch 14:
* GFP_PGTABLE_KERNEL is not defined on all arch's, use GFP_KERNEL | __GFP_ZERO instead [reported by kernel CI]
* Amend the arm64 hotplug code to use pagetable_free(), ensuring that the pkey is reset when vmemmap page tables are freed (this may belong to a separate patch, surprisingly x86 already uses pagetable_free() for this purpose)
* Fix new caller of vmemmap_alloc_block_zero(), call memset() directly since that's the only caller left [also reported by kernel CI]
* Added a note to the commit message explaining why __GFP_RETRY_MAYFAIL is not used
- Patch 21: reduced scope of guard in __ptep_set_access_flags_anysz()
- Patch 24: write to P4D/PUD if not folded in write_direct_map_pggtables()
- Other minor improvements
RFC v7: https://lore.kernel.org/all/[email protected]/
RFC v6..v7:
- Rebased on v7.1-rc2
- Removed large block mapping support to ease reviewing and focus on the core changes. [David Hildenbrand] Accordingly, patched 19 was modified to force PTE mappings if kpkeys_hardened_pgtables is enabled.
- Added patch 14 to protect the sparse-vmemmap page tables.
- Dropped patch "arm64: Reset POR_EL1 on exception entry" as it may cause a crash when a thread with lazy MMU mode enabled is resumed on the irqexit path. See [3] and section "pkey register management" for details. The POR_EL1_INIT bits were squashed into patch 5 (they are still needed for patch 8).
- Renamed "kpkeys level" to "kpkeys context" as there is no longer a notion of ordering - each "context" may be given access to any set of pkeys. [David H]
- Re-organised *_enabled() functions [partially suggested by David H]. The interface is now fully generic and defined in <linux/kpkeys.h>:
- kpkeys_enabled() -> arch_supports_kpkeys()
- kpkeys_hardened_pgtables_enabled() -> static key
- kpkeys_hardened_pgtables_early_enabled() -> arch_supports_kpkeys_early()
Each arch then implements (without #ifdef'ing required):
- arch_supports_kpkeys()
- arch_supports_kpkeys_early()
See patch 1, 5, 11, 16 for details.
- Patch 2: remove KASAN tag when checking if the address falls in the linear map [reported by Yeoreum Yun, thanks!]
- Patch 11: export symbol for the lazy MMU KUnit tests (reported by build CI)
- Patch 24: added test for the sparse-vmemmap page tables
RFC v6: https://lore.kernel.org/linux-hardening/[email protected]/
RFC v5..v6:
- Rebased on v7.0-rc1 (includes support for splitting large block mappings with BBML2-noabort [4] and nested lazy MMU sections [5])
- Completely new approach for allocating protected page tables, see section "Protected page table allocation". Patch 11-26 are mostly new.
- Patch 6: check arch_kpkeys_enabled() to make sure that page table attributes are still changed when using the mock implementation
- Patch 9: new patch (thank you Yeoreum!)
- Patch 28: minor changes following the merging of the nested lazy MMU series [5]
- Patch 30:
* Many tests added to provide better coverage of the various page table allocation paths
* Require that the tests are built-in (many referenced symbols are not exported)
* Some refactoring to reduce duplication and add logging
RFC v5: https://lore.kernel.org/linux-hardening/[email protected]/
RFC v4..v5:
- Rebased on v6.17-rc1.
- Cover letter: re-ran benchmarks on top of v6.17-rc1, made various small improvements especially to the "Performance" section.
- Patch 18: disable batching while in interrupt, since POR_EL1 is reset on exception entry, making the TIF_LAZY_MMU flag meaningless. This fixes a crash that may occur when a page table page is freed while in interrupt context.
- Patch 17: ensure that the target kernel address is actually PTE-mapped. Certain mappings (e.g. code) may be PMD-mapped instead - this explains why the change made in v4 was required.
RFC v4: https://lore.kernel.org/linux-mm/[email protected]/
RFC v3..v4:
- Added appropriate handling of the arm64 pkey register (POR_EL1):
context-switching between threads and resetting on exception entry (patch 7 and 8). See section "pkey register management" above for more details. A new POR_EL1_INIT macro is introduced to make the default value available to assembly (where POR_EL1 is reset on exception entry); it is updated in each patch allocating new keys.
- Added patch 18 making use of the lazy_mmu mode to batch switches to KPKEYS_LVL_PGTABLES - just once per lazy_mmu section rather than on every pgtable write. See section "Performance" for details.
- Rebased on top of [8]. No direct impact on the patches, but it ensures that the ctor/dtor is always called for kernel pgtables. This is an important fix as kernel PTEs allocated after boot were not protected by kpkeys_hardened_pgtables in v3 - a new test was added to patch 17 to ensure that pgtables created by vmalloc are protected too.
- Rebased on top of [9]. The batching of kpkeys level switches in patch 18 relies on the last patch in [9].
- Moved kpkeys guard definitions out of <linux/kpkeys.h> and to a relevant header for each subsystem (e.g. <asm/pgtable.h> for the kpkeys_hardened_pgtables guard).
- Patch 1,5: marked kpkeys_{set_level,restore_pkey_reg} as __always_inline to ensure that no callable gadget is created. [Maxwell Bland's suggestion]
- Patch 5: added helper __kpkeys_set_pkey_reg_nosync().
- Patch 10: marked kernel_pgtables_set_pkey() and related helpers as __init. [Linus Walleij's suggestion]
- Patch 11: added helper kpkeys_hardened_pgtables_enabled(), renamed the static key to kpkeys_hardened_pgtables_key.
- Patch 17: followed the KUnit conventions more closely. [Kees Cook's suggestion]
- Patch 17: changed the address used in the write_linear_map_pte() test. It seems that the PTEs that map some functions are allocated in ZONE_DMA and read-only (unclear why exactly). This doesn't seem to occur for global variables.
- Various minor fixes/improvements.
- Rebased on v6.15-rc1. This includes [10], which renames a few POE
symbols: s/POE_RXW/POE_RWX/ and s/por_set_pkey_perms/por_elx_set_pkey_perms/
RFC v3: https://lore.kernel.org/linux-hardening/[email protected]/
RFC v2..v3:
- Patch 1: kpkeys_set_level() may now return KPKEYS_PKEY_REG_INVAL to indicate that the pkey register wasn't written to, and as a result that kpkeys_restore_pkey_reg() should do nothing. This simplifies the conditional guard macro and also allows architectures to skip writes to the pkey register if the target value is the same as the current one.
- Patch 1: introduced additional KPKEYS_GUARD* macros to cover more use-cases and reduce duplication.
- Patch 6: reject pkey value above arch_max_pkey().
- Patch 13: added missing guard(kpkeys_hardened_pgtables) in __clear_young_dirty_pte().
- Rebased on v6.14-rc1.
RFC v2: https://lore.kernel.org/linux-hardening/[email protected]/
RFC v1..v2:
- A new approach is used to set the pkey of page table pages. Thanks to Qi Zheng's and my own series [11][12], pagetable_*_ctor is systematically called when a PTP is allocated at any level (PTE to PGD), and pagetable_*_dtor when it is freed, on all architectures. Patch 11 makes use of this to call kpkeys_{,un}protect_pgtable_memory from the common ctor/dtor helper. The arm64 patches from v1 (patch 12 and 13) are dropped as they are no longer needed. Patch 10 is introduced to allow pagetable_*_ctor to fail at all levels, since kpkeys_protect_pgtable_memory may itself fail.
[Original suggestion by Peter Zijlstra]
- Changed the prototype of kpkeys_{,un}protect_pgtable_memory in patch 9 to take a struct folio * for more convenience, and implemented them out-of-line to avoid a circular dependency with <linux/mm.h>.
- Rebased on next-20250107, which includes [11] and [12].
- Added locking in patch 8. [Peter Zijlstra's suggestion]
RFC v1: https://lore.kernel.org/linux-hardening/[email protected]/
---
References
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lsseu2025.sched.com/event/25GEE/kernel-hardening-with-protection-keys-kevin-brodsky-arm
[3] https://lore.kernel.org/linux-hardening/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/linux-mm/[email protected]/
[7] https://lore.kernel.org/all/[email protected]/
[8] https://lore.kernel.org/linux-mm/[email protected]/
[9] https://lore.kernel.org/linux-mm/[email protected]/
[10] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[11] https://lore.kernel.org/linux-mm/[email protected]/
[12] https://lore.kernel.org/linux-mm/[email protected]/
[13] https://sashiko.dev/#/patchset/20260505-kpkeys-v7-0-20c0bdd97197%40arm.com
[14] https://lore.kernel.org/all/[email protected]/
[15] https://lore.kernel.org/all/[email protected]/
---
To: [email protected]
Cc: Andrew Morton <[email protected]>
Cc: Ard Biesheuvel <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: David Hildenbrand (Arm) <[email protected]>
Cc: Ira Weiny <[email protected]>
Cc: Jann Horn <[email protected]>
Cc: Jeff Xu <[email protected]>
Cc: Joey Gouly <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Linu Cherian <[email protected]>
Cc: Linus Walleij <[email protected]>
Cc: Lorenzo Stoakes <[email protected]>
Cc: Marc Zyngier <[email protected]>
Cc: Mark Brown <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Maxwell Bland <[email protected]>
Cc: "Mike Rapoport (IBM)" <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Pierre Langlois <[email protected]>
Cc: Pierre-Clément Tosi <[email protected]>
Cc: Quentin Perret <[email protected]>
Cc: Rick Edgecombe <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Yang Shi <[email protected]>
Cc: Yeoreum Yun <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Kevin Brodsky (21):
mm: Introduce kpkeys
set_memory: Introduce set_direct_map_pkey() stub
arm64: Introduce por_elx_set_pkey_perms() helper
arm64: Implement asm/kpkeys.h using POE
arm64: set_memory: Implement set_direct_map_pkey()
arm64: Disable EL2 trapping of POR_EL1
arm64: Context-switch POR_EL1
arm64: mm: Enable overlays for all EL1 indirect permissions
arm64: Enable kpkeys
mm: kpkeys: Introduce kpkeys_hardened_pgtables feature
mm: kpkeys: Protect regular page tables
mm: kpkeys: Protect early kernel page tables
mm: kpkeys: Protect vmemmap page tables
arm64: kpkeys: Implement arch_supports_kpkeys_early()
arm64: kpkeys: Support KPKEYS_CTX_PGTABLES
arm64: kpkeys: Ensure the linear map can be modified
arm64: kpkeys: Guard page table writes
arm64: kpkeys: Batch KPKEYS_CTX_PGTABLES switches
arm64: kpkeys: Enable kpkeys_hardened_pgtables support
arm64: mm: Map kernel image alias of init_pg_dir read-only
mm: Add basic tests for kpkeys_hardened_pgtables
Yeoreum Yun (1):
arm64: Initialize POR_EL1 register on cpu_resume()
arch/arm64/Kconfig | 2 + arch/arm64/include/asm/cpufeature.h | 12 ++ arch/arm64/include/asm/el2_setup.h | 3 +- arch/arm64/include/asm/kpkeys.h | 83 +++++++++++++ arch/arm64/include/asm/kpkeys_types.h | 15 +++ arch/arm64/include/asm/pgtable-prot.h | 8 +- arch/arm64/include/asm/pgtable.h | 67 +++++++++- arch/arm64/include/asm/por.h | 11 ++ arch/arm64/include/asm/processor.h | 5 + arch/arm64/include/asm/sections.h | 1 + arch/arm64/include/asm/set_memory.h | 4 + arch/arm64/kernel/cpufeature.c | 5 +- arch/arm64/kernel/pi/map_kernel.c | 9 +- arch/arm64/kernel/process.c | 10 ++ arch/arm64/kernel/sleep.S | 12 ++ arch/arm64/mm/fault.c | 17 +-- arch/arm64/mm/mmu.c | 38 +++--- arch/arm64/mm/pageattr.c | 29 ++++- include/linux/kpkeys.h | 150 ++++++++++++++++++++++ include/linux/kpkeys_types.h | 30 +++++ include/linux/mm.h | 14 ++- include/linux/set_memory.h | 7 ++ mm/Kconfig | 5 + mm/Makefile | 2 + mm/kpkeys_hardened_pgtables.c | 185 +++++++++++++++++++++++++++ mm/mm_init.c | 2 + mm/sparse-vmemmap.c | 23 ++-- mm/tests/kpkeys_hardened_pgtables_kunit.c | 200 ++++++++++++++++++++++++++++++ security/Kconfig.hardening | 24 ++++ 29 files changed, 928 insertions(+), 45 deletions(-)
---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20260428-kpkeys-e165645122c1
RFC PATCH v10 1/22 mm: Introduce kpkeys
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 <linux/kpkeys.h>: a pair of functions to enter/leave a kpkeys context, and macros to define guard objects.
Each kpkeys context specifies permissions for the pkeys managed by the framework. kpkeys_enter_context() applies those permissions and returns a state for kpkeys_leave_context() to restore.
The saved state is represented by struct kpkeys_state, with one field for the arch state (typically the saved pkeys register value). The other field is used to skip the restore step in case we were already in the context passed to enter_context().
Support for kpkeys must be indicated by selecting ARCH_HAS_KPKEYS. The architecture must then provide:
* struct arch_kpkeys_state in <asm/kpkeys_types.h>
* arch_kpkeys_enter_context() in <asm/kpkeys.h>:
if needed, sets the new state and saves the original one in arch_kpkeys_state; reports whether the context was actually changed
* arch_kpkeys_leave_context() in <asm/kpkeys.h>:
restores the original state
* arch_supports_kpkeys() in <asm/kpkeys.h>:
returns whether the system has an appropriate hardware feature to support kpkeys
To start with, only KPKEYS_CTX_DEFAULT is available, granting RW access to the default pkey (0).
--- include/linux/kpkeys.h | 107 +++++++++++++++++++++++++++++++++++++++++++ include/linux/kpkeys_types.h | 25 ++++++++++ mm/Kconfig | 2 + 3 files changed, 134 insertions(+)
include/linux/kpkeys.h new file +107 -0
@@ -0,0 +1,107 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_KPKEYS_H +#define _LINUX_KPKEYS_H + +#include <linux/bug.h> +#include <linux/cleanup.h> +#include <linux/kpkeys_types.h> + +/** +* KPKEYS_GUARD_NOOP() - helper to define a guard type that does nothing +* @name: the name of the guard type +* +* Define a guard type that does nothing, useful to match a real guard type +* that is defined under an #ifdef. +*/ +#define KPKEYS_GUARD_NOOP (name) \ + __DEFINE_CLASS_IS_CONDITIONAL(name, false); \ + DEFINE_CLASS(name, bool, (void)_T, false, void); \ + static inline void *class_##name##_lock_ptr(bool *_T) \ + { return _T; } + +#ifdef CONFIG_ARCH_HAS_KPKEYS + +#include <asm/kpkeys.h> + +/** +* KPKEYS_GUARD_COND() - helper to 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 +* +* Define a guard type that switches to @ctx if @cond evaluates to true, +* and does nothing otherwise. +*/ +#define KPKEYS_GUARD_COND (name, ctx, cond) \ + __DEFINE_CLASS_IS_CONDITIONAL(name, false); \ + DEFINE_CLASS(name, struct kpkeys_state, \ + kpkeys_leave_context(&_T), \ + (cond) ? kpkeys_enter_context(ctx) : \ + (struct kpkeys_state) {}, void); \ + static inline \ + void *class_##name##_lock_ptr(struct kpkeys_state *_T) \ + { return _T; } + +/** +* KPKEYS_GUARD() - helper to define a guard type that switches to a given +* kpkeys context if kpkeys are supported +* @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_supported()) + +/** +* kpkeys_enter_context() - enter a kpkeys context +* @ctx: the context to switch to +* +* Enters the specified kpkeys context. @ctx must be a compile-time constant. +* +* Return: state to be passed to kpkeys_leave_context(). +*/ +static __always_inline +struct kpkeys_state kpkeys_enter_context(enum kpkeys_ctx ctx) +{ +struct kpkeys_state state = {}; + +BUILD_BUG_ON_MSG (!__builtin_constant_p (ctx), +"kpkeys_enter_context() only takes constant values" ); +BUILD_BUG_ON_MSG (ctx <0 || ctx >=KPKEYS_CTX_COUNT , +"Invalid value passed to kpkeys_enter_context()" ); + + state.entered_context =arch_kpkeys_enter_context (ctx, &state.arch_state); +return state; +} + +/** +* kpkeys_leave_context() - leave a kpkeys context +* @state: state returned by kpkeys_enter_context() +* +* Restores the state saved when entering a kpkeys context. If no context was +* entered, this function does nothing. +*/ +static __always_inline +void kpkeys_leave_context(const struct kpkeys_state *state) +{ +if (state->entered_context) +arch_kpkeys_leave_context (&state->arch_state); +} + +static inline bool kpkeys_supported (void ) +{ +return arch_supports_kpkeys (); +} + +#else /* CONFIG_ARCH_HAS_KPKEYS */ + +static inline bool kpkeys_supported (void ) +{ +return false; +} + +#endif /* CONFIG_ARCH_HAS_KPKEYS */ + +#endif /* _LINUX_KPKEYS_H */
include/linux/kpkeys_types.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_KPKEYS_TYPES_H +#define _LINUX_KPKEYS_TYPES_H + +#include <linux/types.h> + +#ifdef CONFIG_ARCH_HAS_KPKEYS +#include <asm/kpkeys_types.h> +#endif + +#if defined(CONFIG_ARCH_HAS_KPKEYS ) && !defined(__ASSEMBLY__) + +enum kpkeys_ctx { +KPKEYS_CTX_DEFAULT =0 , +KPKEYS_CTX_COUNT , +}; + +struct kpkeys_state { +bool entered_context; +struct arch_kpkeys_state arch_state; +}; + +#endif /* CONFIG_ARCH_HAS_KPKEYS && !__ASSEMBLY__ */ + +#endif /* _LINUX_KPKEYS_TYPES_H */
mm/Kconfig +2 -0
@@ -1265,6 +1265,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
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v10 01/22] mm: Introduce kpkeys
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mm: kpkeys: use DEFINE_CLASS_IS_UNCONDITIONAL() in KPKEYS_GUARD macros --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm: kpkeys: use DEFINE_CLASS_IS_UNCONDITIONAL() in KPKEYS_GUARD macros
--
commit fedb14faa9a0bdbbf2b6d66e78122c92692259b9
Author: Kevin Brodsky <[email protected]>
mm: Introduce kpkeys
This commit introduces the kpkeys header API, context switching helpers, and guard class macros for kernel protection keys.
Quoted 6 lines
include/linux/kpkeys.h new file +0
-0@@ -0,0 +1,107 @@
[ ... ]
Quoted 19 lines
include/linux/kpkeys.h new file +19
-0+#define KPKEYS_GUARD_NOOP (name) \ + __DEFINE_CLASS_IS_CONDITIONAL(name, false); \ + DEFINE_CLASS(name, bool, (void)_T, false, void); \ + static inline void *class_##name##_lock_ptr(bool *_T) \ + { return _T; } + +#ifdef CONFIG_ARCH_HAS_KPKEYS + +#include <asm/kpkeys.h> + +/** +* KPKEYS_GUARD_COND() - helper to 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 +* +* Define a guard type that switches to @ctx if @cond evaluates to true, +* and does nothing otherwise.
include/linux/kpkeys.h line 44
Quoted 10 lines
include/linux/kpkeys.h new file +10
-0+*/ +#define KPKEYS_GUARD_COND (name, ctx, cond) \ + __DEFINE_CLASS_IS_CONDITIONAL(name, false); \ + DEFINE_CLASS(name, struct kpkeys_state, \ + kpkeys_leave_context(&_T), \ + (cond) ? kpkeys_enter_context(ctx) : \ + (struct kpkeys_state) {}, void); \ + static inline \ + void *class_##name##_lock_ptr(struct kpkeys_state *_T) \ + { return _T; }
[Severity: Low]
Could KPKEYS_GUARD_NOOP() and KPKEYS_GUARD_COND() use DEFINE_CLASS_IS_UNCONDITIONAL(name) after DEFINE_CLASS() instead of open-coding __DEFINE_CLASS_IS_CONDITIONAL() and class_##name##_lock_ptr()? include/linux/cleanup.h already provides DEFINE_CLASS_IS_UNCONDITIONAL() for this purpose, avoiding reliance on internal cleanup.h helpers.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH RFC v10 01/22] mm: Introduce kpkeys
Quoted 19 lines · On 10/11/2026 17:02, [email protected] wrote:
Quoted 19 lines
[...]
include/linux/kpkeys.h new file +17
-0+/** +* KPKEYS_GUARD_COND() - helper to 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 +* +* Define a guard type that switches to @ctx if @cond evaluates to true, +* and does nothing otherwise. +*/ +#define KPKEYS_GUARD_COND (name, ctx, cond) \ + __DEFINE_CLASS_IS_CONDITIONAL(name, false); \ + DEFINE_CLASS(name, struct kpkeys_state, \ + kpkeys_leave_context(&_T), \ + (cond) ? kpkeys_enter_context(ctx) : \ + (struct kpkeys_state) {}, void); \ + static inline \
Quoted 8 lines
include/linux/kpkeys.h line 44
Quoted 2 lines
include/linux/kpkeys.h new file +2
-0+ void *class_##name##_lock_ptr(struct kpkeys_state *_T) \ + { return _T; }[Severity: Low]
CouldKPKEYS_GUARD_NOOP()andKPKEYS_GUARD_COND()useDEFINE_CLASS_IS_UNCONDITIONAL(name)afterDEFINE_CLASS()instead of open-coding__DEFINE_CLASS_IS_CONDITIONAL()and class_##name##_lock_ptr()?include/linux/cleanup.halready providesDEFINE_CLASS_IS_UNCONDITIONAL()for this purpose, avoiding reliance on internalcleanup.hhelpers.
Agreed, that's the helper I was looking for but it didn't exist when I first wrote this code!
- Kevin
RFC PATCH v10 2/22 set_memory: Introduce set_direct_map_pkey() stub
In reply to Kevin BrodskyIntroduce a new function, set_direct_map_pkey(), which sets the protection key (pkey) of pages in the specified direct map range. Architectures implementing kernel pkeys (kpkeys) must provide a suitable implementation; an empty stub is added as fallback.
--- include/linux/set_memory.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/linux/set_memory.h +7 -0
@@ -190,4 +190,11 @@ static inline int set_memory_decrypted(unsigned long addr, int numpages) }#endif /* CONFIG_ARCH_HAS_MEM_ENCRYPT */ +#ifndef CONFIG_ARCH_HAS_KPKEYS +static inline int set_direct_map_pkey (unsigned long addr,int numpages,int pkey) +{ +return 0 ; +} +#endif +#endif /* _LINUX_SET_MEMORY_H_ */
Signature · 1 line
2.56.0
RFC PATCH v10 3/22 arm64: Introduce por_elx_set_pkey_perms() helper
In reply to Kevin BrodskyIntroduce a helper that sets the permissions of a given pkey (POIndex) in the POR_ELx format, and make use of it in arch_set_user_pkey_access(). Use field_prep() to encode the new permissions because the field mask depends on the pkey index.
new_por now represents the permissions of a single pkey, so rename it to new_perms and change its type accordingly.
--- arch/arm64/include/asm/por.h | 7 +++++++ arch/arm64/mm/mmu.c | 26 ++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-)
arch/arm64/include/asm/por.h +7 -0
@@ -31,4 +31,11 @@ static inline bool por_elx_allows_exec(u64 por, u8 pkey) return perm & POE_X; } +static inline u64 por_elx_set_pkey_perms (u64 por,u8 pkey,u8 perms) +{ +u64 mask =POE_MASK <<POR_ELx_PERM_SHIFT (pkey); + +return (por & ~mask) |field_prep (mask, perms); +} +#endif /* _ASM_ARM64_POR_H */
arch/arm64/mm/mmu.c +10 -16
@@ -2355,8 +2355,8 @@ void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp)#ifdef CONFIG_ARCH_HAS_PKEYS int arch_set_user_pkey_access (int pkey,unsigned long init_val) { -u64 new_por; -u64 old_por; +u8 new_perms; +u64 por;if (!system_supports_poe ())return -ENOSPC ;
@@ -2370,25 +2370,19 @@ int arch_set_user_pkey_access(int pkey, unsigned long init_val)return -EINVAL ;/* Set the bits we need in POR: */ - new_por =POE_RWX ; + new_perms =POE_RWX ;if (init_val &PKEY_DISABLE_WRITE ) - new_por &= ~POE_W ; + new_perms &= ~POE_W ;if (init_val &PKEY_DISABLE_ACCESS ) - new_por &= ~POE_RW ; + new_perms &= ~POE_RW ;if (init_val &PKEY_DISABLE_READ ) - new_por &= ~POE_R ; + new_perms &= ~POE_R ;if (init_val &PKEY_DISABLE_EXECUTE ) - new_por &= ~POE_X ; + new_perms &= ~POE_X ; -/* Shift the bits in to the correct place in POR for pkey: */ - new_por =POR_ELx_PERM_PREP (pkey, new_por); - -/* Get old POR and mask off any old bits in place: */ - old_por =read_sysreg_s (SYS_POR_EL0 ); - old_por &= ~(POE_MASK <<POR_ELx_PERM_SHIFT (pkey)); - -/* Write old part along with new part: */ -write_sysreg_s (old_por | new_por,SYS_POR_EL0 ); + por =read_sysreg_s (SYS_POR_EL0 ); + por =por_elx_set_pkey_perms (por, pkey, new_perms); +write_sysreg_s (por,SYS_POR_EL0 );return 0 ; }
Signature · 1 line
2.56.0
RFC PATCH v10 4/22 arm64: Implement asm/kpkeys.h using POE
In reply to Kevin BrodskyImplement the kpkeys interface if CONFIG_ARM64_POE is enabled. The permissions for pkey 0 are set to RWX as this pkey is also used for code mappings.
arch_kpkeys_enter_context() returns whether it modified POR_EL1, and saves its old value when it does. arch_kpkeys_leave_context() is only called if POR_EL1 actually needs to be restored.
To allow <asm/kpkeys.h> to be included from assembly, also add appropriate #ifdefs to <asm/por.h>.
--- arch/arm64/include/asm/kpkeys.h | 73 +++++++++++++++++++++++++++++++++++ arch/arm64/include/asm/kpkeys_types.h | 15 +++++++ arch/arm64/include/asm/por.h | 4 ++ 3 files changed, 92 insertions(+)
arch/arm64/include/asm/kpkeys.h new file +73 -0
@@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_KPKEYS_H +#define __ASM_KPKEYS_H + +#include <linux/kpkeys_types.h> + +#include <asm/barrier.h> +#include <asm/cpufeature.h> +#include <asm/por.h> + +/* +* Equivalent to por_set_kpkeys_context(0, KPKEYS_CTX_DEFAULT), but can also be +* used in assembly. +*/ +#define POR_EL1_INIT POR_ELx_PERM_PREP(0, POE_RWX) + +#ifndef __ASSEMBLY__ + +static inline bool arch_supports_kpkeys (void ) +{ +return system_supports_poe (); +} + +#ifdef CONFIG_ARM64_POE + +static inline u64 por_set_kpkeys_context (u64 por,enum kpkeys_ctx ctx) +{ + por =por_elx_set_pkey_perms (por,0 ,POE_RWX ); + +return por; +} + +static __always_inline void __kpkeys_set_pkey_reg_nosync(u64 pkey_reg) +{ +/* +* Writing to POR_EL1 potentially affects all memory accesses, so +* use barrier() to ensure load/store instructions cannot be reordered +* past the write. +*/ +barrier (); +write_sysreg_s (pkey_reg,SYS_POR_EL1 ); +barrier (); +} + +static __always_inline +bool arch_kpkeys_enter_context(enum kpkeys_ctx ctx, +struct arch_kpkeys_state *state) +{ +const u64 prev_por =read_sysreg_s (SYS_POR_EL1 ); +const u64 new_por =por_set_kpkeys_context (prev_por, ctx); + +if (prev_por == new_por) +return false; + + state->por = prev_por; +__kpkeys_set_pkey_reg_nosync (new_por); +isb (); + +return true; +} + +static __always_inline +void arch_kpkeys_leave_context(const struct arch_kpkeys_state *state) +{ +__kpkeys_set_pkey_reg_nosync (state->por); +isb (); +} + +#endif /* CONFIG_ARM64_POE */ + +#endif /* __ASSEMBLY__ */ + +#endif /* __ASM_KPKEYS_H */
arch/arm64/include/asm/kpkeys_types.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_KPKEYS_TYPES_H +#define __ASM_KPKEYS_TYPES_H + +#include <linux/types.h> + +#ifndef __ASSEMBLY__ + +struct arch_kpkeys_state { +u64 por; +}; + +#endif /* __ASSEMBLY__ */ + +#endif /* __ASM_KPKEYS_TYPES_H */
arch/arm64/include/asm/por.h +4 -0
@@ -10,6 +10,8 @@#define POR_EL0_INIT POR_ELx_PERM_PREP(0, POE_RWX) +#ifndef __ASSEMBLY__ +static inline bool por_elx_allows_read (u64 por,u8 pkey) {u8 perm =POR_ELx_PERM_GET (pkey, por);
@@ -38,4 +40,6 @@ static inline u64 por_elx_set_pkey_perms(u64 por, u8 pkey, u8 perms)return (por & ~mask) |field_prep (mask, perms); } +#endif /* __ASSEMBLY__ */ +#endif /* _ASM_ARM64_POR_H */
Signature · 1 line
2.56.0
Re: [PATCH RFC v10 04/22] arm64: Implement asm/kpkeys.h using POE
arch/arm64/include/asm/kpkeys.h line 17
Quoted 3 lines · Kevin Brodsky
arch/arm64/include/asm/kpkeys.h new file +3
-0+#define POR_EL1_INIT POR_ELx_PERM_PREP(0, POE_RWX) + +#ifndef __ASSEMBLY__
Everything else in arch/arm64 was converted to __ASSEMBLER__.
Re: [PATCH RFC v10 04/22] arm64: Implement asm/kpkeys.h using POE
Quoted 6 lines · Mark Brown
arch/arm64/include/asm/kpkeys.h line 17
Quoted 3 lines · Kevin Brodsky
arch/arm64/include/asm/kpkeys.h new file +3
-0+#define POR_EL1_INIT POR_ELx_PERM_PREP(0, POE_RWX) + +#ifndef __ASSEMBLY__Everything else in
arch/arm64was converted to__ASSEMBLER__.
Good catch, copy/pasted the wrong one when I first wrote this!
- Kevin
[email protected]
AI reviewRe: [PATCH RFC v10 04/22] arm64: Implement asm/kpkeys.h using POE
In reply to Kevin BrodskyThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] arm64: kpkeys: mismatched CONFIG_ARM64_POE guard in asm/kpkeys.h --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] arm64: kpkeys: mismatched CONFIG_ARM64_POE guard in asm/kpkeys.h
--
commit 6f45a834f4b090307d508db166b798a5f825afea
Author: Kevin Brodsky <[email protected]>
arm64: Implement asm/kpkeys.h using POE
This commit implements the arm64 kpkeys headers and context-switching helpers using the Permission Overlay Extension (POE) and POR_EL1.
Quoted 51 lines
arch/arm64/include/asm/kpkeys.h new file +45
-0@@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_KPKEYS_H +#define __ASM_KPKEYS_H + +#include <linux/kpkeys_types.h> + +#include <asm/barrier.h> +#include <asm/cpufeature.h> +#include <asm/por.h> + +/* +* Equivalent to por_set_kpkeys_context(0, KPKEYS_CTX_DEFAULT), but can also be +* used in assembly. +*/ +#define POR_EL1_INIT POR_ELx_PERM_PREP(0, POE_RWX) + +#ifndef __ASSEMBLY__ + +static inline bool arch_supports_kpkeys (void ) +{ +return system_supports_poe (); +} + +#ifdef CONFIG_ARM64_POE + +static inline u64 por_set_kpkeys_context (u64 por,enum kpkeys_ctx ctx) +{ + por =por_elx_set_pkey_perms (por,0 ,POE_RWX ); + +return por; +} + +static __always_inline void __kpkeys_set_pkey_reg_nosync(u64 pkey_reg) +{ +/* +* Writing to POR_EL1 potentially affects all memory accesses, so +* use barrier() to ensure load/store instructions cannot be reordered +* past the write. +*/ +barrier (); +write_sysreg_s (pkey_reg,SYS_POR_EL1 ); +barrier (); +} + +static __always_inline
arch/arm64/include/asm/kpkeys.h line 55
Quoted 10 lines
arch/arm64/include/asm/kpkeys.h new file +10
-0+bool arch_kpkeys_enter_context (enum kpkeys_ctx ctx, +struct arch_kpkeys_state *state) +{ +const u64 prev_por =read_sysreg_s (SYS_POR_EL1 ); +const u64 new_por =por_set_kpkeys_context (prev_por, ctx); + +if (prev_por == new_por) +return false; + + state->por = prev_por;
[Severity: Medium]
Should this block be guarded by CONFIG_ARCH_HAS_KPKEYS instead of CONFIG_ARM64_POE?
include/linux/kpkeys_types.h only includes <asm/kpkeys_types.h> (for struct arch_kpkeys_state) and defines enum kpkeys_ctx when CONFIG_ARCH_HAS_KPKEYS is enabled:
include/linux/kpkeys_types.h:
#ifdef CONFIG_ARCH_HAS_KPKEYS
#include <asm/kpkeys_types.h>
#endif
#if defined(CONFIG_ARCH_HAS_KPKEYS) && !defined(__ASSEMBLY__)
enum kpkeys_ctx {
KPKEYS_CTX_DEFAULT = 0,
KPKEYS_CTX_COUNT,
};Because CONFIG_ARM64_POE is enabled by default on arm64 and does not select ARCH_HAS_KPKEYS until the later commit "arm64: Enable kpkeys", when commit "arm64: Context-switch POR_EL1" includes <asm/kpkeys.h> in arch/arm64/kernel/process.c, the build fails with incomplete type errors for enum kpkeys_ctx and struct arch_kpkeys_state, breaking bisection.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH RFC v10 04/22] arm64: Implement asm/kpkeys.h using POE
Quoted 54 lines · On 10/11/2026 17:02, [email protected] wrote:
Quoted 24 lines
[...]
arch/arm64/include/asm/kpkeys.h new file +22
-0+#ifdef CONFIG_ARM64_POE + +static inline u64 por_set_kpkeys_context (u64 por,enum kpkeys_ctx ctx) +{ + por =por_elx_set_pkey_perms (por,0 ,POE_RWX ); + +return por; +} + +static __always_inline void __kpkeys_set_pkey_reg_nosync(u64 pkey_reg) +{ +/* +* Writing to POR_EL1 potentially affects all memory accesses, so +* use barrier() to ensure load/store instructions cannot be reordered +* past the write. +*/ +barrier (); +write_sysreg_s (pkey_reg,SYS_POR_EL1 ); +barrier (); +} + +static __always_inlinearch/arm64/include/asm/kpkeys.h line 55
Quoted 10 lines
arch/arm64/include/asm/kpkeys.h new file +10
-0+bool arch_kpkeys_enter_context (enum kpkeys_ctx ctx, +struct arch_kpkeys_state *state) +{ +const u64 prev_por =read_sysreg_s (SYS_POR_EL1 ); +const u64 new_por =por_set_kpkeys_context (prev_por, ctx); + +if (prev_por == new_por) +return false; + + state->por = prev_por;[Severity: Medium]
Should this block be guarded byCONFIG_ARCH_HAS_KPKEYSinstead ofCONFIG_ARM64_POE?
include/linux/kpkeys_types.honly includes <asm/kpkeys_types.h> (for structarch_kpkeys_state) and defines enumkpkeys_ctxwhenCONFIG_ARCH_HAS_KPKEYSis enabled:
include/linux/kpkeys_types.h:#ifdef CONFIG_ARCH_HAS_KPKEYS #include <asm/kpkeys_types.h> #endif #if defined(CONFIG_ARCH_HAS_KPKEYS) && !defined(__ASSEMBLY__) enum kpkeys_ctx { KPKEYS_CTX_DEFAULT = 0, KPKEYS_CTX_COUNT, };
Quoted 5 lines
Because
CONFIG_ARM64_POEis enabled by default on arm64 and does not selectARCH_HAS_KPKEYSuntil the later commit "arm64: Enable kpkeys", when commit "arm64: Context-switchPOR_EL1" includes <asm/kpkeys.h> inarch/arm64/kernel/process.c, the build fails with incomplete type errors for enumkpkeys_ctxand structarch_kpkeys_state, breaking bisection.
Will use CONFIG_ARCH_HAS_KPKEYS instead. The idea was that this implementation is POE-specific, but in fact another implementation would likely be runtime-selected so using the generic #ifdef makes more sense.
- Kevin
RFC PATCH v10 5/22 arm64: set_memory: Implement set_direct_map_pkey()
In reply to Kevin BrodskyImplement set_direct_map_pkey() using POE.
--- arch/arm64/include/asm/set_memory.h | 4 ++++ arch/arm64/mm/pageattr.c | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
arch/arm64/include/asm/set_memory.h +4 -0
@@ -18,4 +18,8 @@ bool kernel_page_present(struct page *page);int set_memory_encrypted (unsigned long addr,int numpages);int set_memory_decrypted (unsigned long addr,int numpages); +#ifdef CONFIG_ARCH_HAS_KPKEYS +int set_direct_map_pkey (unsigned long addr,int numpages,int pkey); +#endif +#endif /* _ASM_ARM64_SET_MEMORY_H */
arch/arm64/mm/pageattr.c +26 -0
@@ -9,6 +9,8 @@#include <linux/sched.h> #include <linux/vmalloc.h> #include <linux/pagewalk.h> +#include <linux/pkeys.h> +#include <linux/kpkeys.h> #include <asm/cacheflush.h> #include <asm/pgtable-prot.h>
@@ -319,6 +321,30 @@ int __set_memory_enc_dec(unsigned long addr, int numpages, bool encrypt) __pgprot(PTE_PRESENT_INVALID)); } +#ifdef CONFIG_ARCH_HAS_KPKEYS +int set_direct_map_pkey (unsigned long addr,int numpages,int pkey) +{ +unsigned long set_prot =0 ; + +if (!kpkeys_supported () || !can_set_direct_map ()) +return 0 ; + +if (!__is_lm_address (kasan_reset_tag ((void *)addr))) +return -EINVAL ; + +if (pkey <0 || pkey >=arch_max_pkey ()) +return -EINVAL ; + + set_prot |= pkey &BIT (0 ) ?PTE_PO_IDX_0 :0 ; + set_prot |= pkey &BIT (1 ) ?PTE_PO_IDX_1 :0 ; + set_prot |= pkey &BIT (2 ) ?PTE_PO_IDX_2 :0 ; + +return __change_memory_common (addr,PAGE_SIZE * numpages, +__pgprot (set_prot), +__pgprot (PTE_PO_IDX_MASK )); +} +#endif +#ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages (struct page *page,int numpages,int enable) {
Signature · 1 line
2.56.0
RFC PATCH v10 6/22 arm64: Disable EL2 trapping of POR_EL1
In reply to Kevin BrodskyMake sure EL2 doesn't trap kernel accesses to POR_EL1 by setting the nPOR_EL1 bit in HFGRT{R,W}_EL2.
--- arch/arm64/include/asm/el2_setup.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/el2_setup.h +2 -1
@@ -380,7 +380,8 @@ ubfx x1, x1, #ID_AA64MMFR3_EL1_S1POE_SHIFT, #4 cbz x1, .Lskip_poe_fgt_\@ -/* Disable trapping of POR_EL0 */ +/* Disable trapping of POR_ELx */ + orr x0, x0, #HFGRTR_EL2_nPOR_EL1 orr x0, x0, #HFGRTR_EL2_nPOR_EL0 .Lskip_poe_fgt_\@:
Signature · 1 line
2.56.0
Re: [PATCH RFC v10 06/22] arm64: Disable EL2 trapping of POR_EL1
Quoted 2 lines · Kevin Brodsky
Make sure EL2 doesn't trap kernel accesses to
POR_EL1by setting thenPOR_EL1bit in HFGRT{R,W}_EL2.
This wants a matching update in booting.rst as well for when the kernel is booted at EL1.
Re: [PATCH RFC v10 06/22] arm64: Disable EL2 trapping of POR_EL1
Quoted 6 lines · Mark Brown
Quoted 2 lines · Kevin Brodsky
Make sure EL2 doesn't trap kernel accesses to
POR_EL1by setting thenPOR_EL1bit in HFGRT{R,W}_EL2.This wants a matching update in
booting.rstas well for when the kernel is booted at EL1.
Good catch, in fact POE requirements are completely missing from booting.rst. Will post a separate patch for what we already need upstream (i.e. POE at EL0), and then expand the section in this patch.
- Kevin
RFC PATCH v10 7/22 arm64: Initialize POR_EL1 register on cpu_resume()
In reply to Kevin BrodskyFrom: Yeoreum Yun <[email protected]>
The POR_EL1 register is reset to an unknown value after cpu_suspend(). Since POR_EL1 always holds POR_EL1_INIT when entering cpu_suspend(), initialize POR_EL1 with POR_EL1_INIT before cpu_do_resume().
--- arch/arm64/kernel/sleep.S | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/arm64/kernel/sleep.S +12 -0
@@ -3,6 +3,7 @@ #include <linux/linkage.h> #include <asm/asm-offsets.h> #include <asm/assembler.h> +#include <asm/kpkeys.h> #include <asm/smp.h> .text
@@ -134,6 +135,17 @@ SYM_FUNC_START(_cpu_resume) /* load sp from context */ ldr x2, [x0, #CPU_CTX_SP] mov sp, x2 + +#ifdef CONFIG_ARM64_POE +alternative_if_not ARM64_HAS_S1POE + b .Lskip_por_set +alternative_else_nop_endif + mov_q x2, POR_EL1_INIT + msr_s SYS_POR_EL1, x2 + /* isb can be skipped since cpu_do_resume() will do it. */ +.Lskip_por_set: +#endif /* CONFIG_ARM64_POE */ + /* * cpu_do_resume expects x0 to contain context address pointer */
Signature · 1 line
2.56.0
Re: [PATCH RFC v10 07/22] arm64: Initialize POR_EL1 register on cpu_resume()
Quoted 6 lines · Kevin Brodsky
From: Yeoreum Yun <[email protected]>
The
POR_EL1register is reset to an unknown value aftercpu_suspend(). SincePOR_EL1always holdsPOR_EL1_INITwhen enteringcpu_suspend(), initializePOR_EL1withPOR_EL1_INITbeforecpu_do_resume().
arch/arm64/kernel/sleep.S SYM_FUNC_START(_cpu_resume) · line 147
Quoted 9 lines
arch/arm64/kernel/sleep.S +9
-0+#ifdef CONFIG_ARM64_POE +alternative_if_not ARM64_HAS_S1POE + b .Lskip_por_set +alternative_else_nop_endif + mov_q x2, POR_EL1_INIT + msr_s SYS_POR_EL1, x2 + /* isb can be skipped since cpu_do_resume() will do it. */ +.Lskip_por_set: +#endif /* CONFIG_ARM64_POE */
cpu_do_resume() will update TCR2_EL1 to enable POE again, a sufficiently unhelpful implementation could potentially decide to do that before this POR_EL1 value takes effect AFAICT given that there's no barriers.
Re: [PATCH RFC v10 07/22] arm64: Initialize POR_EL1 register on cpu_resume()
Quoted 7 lines · Mark Brown
Quoted 6 lines · Kevin Brodsky
From: Yeoreum Yun <[email protected]>
The
POR_EL1register is reset to an unknown value aftercpu_suspend(). SincePOR_EL1always holdsPOR_EL1_INITwhen enteringcpu_suspend(), initializePOR_EL1withPOR_EL1_INITbeforecpu_do_resume().Quoted 1 line
+#ifdef
CONFIG_ARM64_POE
Quoted 12 lines
arch/arm64/kernel/sleep.S SYM_FUNC_START(_cpu_resume) · line 147
Quoted 8 lines
arch/arm64/kernel/sleep.S +8
-0+alternative_if_not ARM64_HAS_S1POE + b .Lskip_por_set +alternative_else_nop_endif + mov_q x2, POR_EL1_INIT + msr_s SYS_POR_EL1, x2 + /* isb can be skipped since cpu_do_resume() will do it. */ +.Lskip_por_set: +#endif /* CONFIG_ARM64_POE */
cpu_do_resume()will updateTCR2_EL1to enable POE again, a sufficiently unhelpful implementation could potentially decide to do that before thisPOR_EL1value takes effect AFAICT given that there's no barriers.
Thanks. seems required to isb() after initialising SYS_POR_EL1.
Signature · 2 lines
Sincerely, Yeoreum Yun
Re: [PATCH RFC v10 07/22] arm64: Initialize POR_EL1 register on cpu_resume()
Quoted 19 lines · Yeoreum Yun
Quoted 7 lines · Mark Brown
Quoted 6 lines · Kevin Brodsky
From: Yeoreum Yun <[email protected]>
The
POR_EL1register is reset to an unknown value aftercpu_suspend(). SincePOR_EL1always holdsPOR_EL1_INITwhen enteringcpu_suspend(), initializePOR_EL1withPOR_EL1_INITbeforecpu_do_resume().Quoted 1 line
+#ifdef
CONFIG_ARM64_POEQuoted 11 lines
arch/arm64/kernel/sleep.S SYM_FUNC_START(_cpu_resume) · line 147
Quoted 8 lines
arch/arm64/kernel/sleep.S +8
-0+alternative_if_not ARM64_HAS_S1POE + b .Lskip_por_set +alternative_else_nop_endif + mov_q x2, POR_EL1_INIT + msr_s SYS_POR_EL1, x2 + /* isb can be skipped since cpu_do_resume() will do it. */ +.Lskip_por_set: +#endif /* CONFIG_ARM64_POE */
cpu_do_resume()will updateTCR2_EL1to enable POE again, a sufficiently unhelpful implementation could potentially decide to do that before thisPOR_EL1value takes effect AFAICT given that there's no barriers.
Quoted 1 line
Thanks. seems required to
isb()after initialisingSYS_POR_EL1.
Agreed (as Sashiko reported as well). Presumably that means we also need an ISB after writing to POR_EL1 in cpu_enable_poe(), as that's basically the same situation.
- Kevin
Re: [PATCH RFC v10 07/22] arm64: Initialize POR_EL1 register on cpu_resume()
Quoted 7 lines · Kevin Brodsky
Quoted 1 line · Yeoreum Yun
Thanks. seems required to
isb()after initialisingSYS_POR_EL1.Agreed (as Sashiko reported as well). Presumably that means we also need an ISB after writing to
POR_EL1incpu_enable_poe(), as that's basically the same situation.
Possibly just have one before writing TCR2_EL1 rather than for each of the indvididual features, that's harder to mess up?
Re: [PATCH RFC v10 07/22] arm64: Initialize POR_EL1 register on cpu_resume()
Quoted 11 lines · Mark Brown
Quoted 7 lines · Kevin Brodsky
Quoted 1 line · Yeoreum Yun
Thanks. seems required to
isb()after initialisingSYS_POR_EL1.Agreed (as Sashiko reported as well). Presumably that means we also need an ISB after writing to
POR_EL1incpu_enable_poe(), as that's basically the same situation.Possibly just have one before writing
TCR2_EL1rather than for each of the indvididual features, that's harder to mess up?
I'm not sure that's necessarily better, as we'd end up executing an isb() even when POE or other features aren't enabled.
Wouldn't it be better to execute an isb() only when needed?
Signature · 2 lines
Sincerely, Yeoreum Yun
Re: [PATCH RFC v10 07/22] arm64: Initialize POR_EL1 register on cpu_resume()
Quoted 8 lines · Yeoreum Yun
Quoted 2 lines · Mark Brown
Possibly just have one before writing
TCR2_EL1rather than for each of the indvididual features, that's harder to mess up?I'm not sure that's necessarily better, as we'd end up executing an
isb()even when POE or other features aren't enabled.
Wouldn't it be better to execute anisb()only when needed?
OTOH it seemed like we'd repeatedly omitted them where we needed them, and we'd end up issuing multiple barriers this way. But Kevin pointed out that the second one needs to be where it is anyway so it doesn't matter.
Re: [PATCH RFC v10 07/22] arm64: Initialize POR_EL1 register on cpu_resume()
In reply to Mark BrownQuoted 5 lines · Mark Brown
Quoted 3 lines
Agreed (as Sashiko reported as well). Presumably that means we also need an ISB after writing to
POR_EL1incpu_enable_poe(), as that's basically the same situation.Possibly just have one before writing
TCR2_EL1rather than for each of the indvididual features, that's harder to mess up?
cpu_enable_poe() directly writes to TCR2_EL1 so surely the extra ISB has to be there (between writing to POR_EL1 and writing to TCR2_EL1)?
In _cpu_resume for now I'd keep the ISB directly after writing to POR_EL1, as I'm not sure the ordering is needed for anything but POE and it seems easier to follow with the ISB right after POR_EL1.
- Kevin
[email protected]
AI reviewRe: [PATCH RFC v10 07/22] arm64: Initialize POR_EL1 register on cpu_resume()
In reply to Kevin BrodskyThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] arm64: unconfirmed ordering of SYS_POR_EL1 and REG_TCR2_EL1 in _cpu_resume() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] arm64: unconfirmed ordering of SYS_POR_EL1 and REG_TCR2_EL1 in _cpu_resume()
--
commit 5fdafa429ecfc231981525c4af268a477f615016
Author: Yeoreum Yun <[email protected]>
arm64: Initialize POR_EL1 register on cpu_resume()
This commit initializes POR_EL1 to POR_EL1_INIT in _cpu_resume() before calling cpu_do_resume() since POR_EL1 resets to an unknown value across cpu_suspend().
Quoted 4 lines
arch/arm64/kernel/sleep.S +0
-0
[ ... ]
arch/arm64/kernel/sleep.S SYM_FUNC_START(_cpu_resume) · line 145
Quoted 12 lines
arch/arm64/kernel/sleep.S +8
-0@@ -134,6 +135,17 @@ SYM_FUNC_START(_cpu_resume) /* load sp from context */ ldr x2, [x0, #CPU_CTX_SP] mov sp, x2 + +#ifdef CONFIG_ARM64_POE +alternative_if_not ARM64_HAS_S1POE + b .Lskip_por_set +alternative_else_nop_endif + mov_q x2, POR_EL1_INIT + msr_s SYS_POR_EL1, x2 + /* isb can be skipped since cpu_do_resume() will do it. */
[Severity: Low]
Can the indirect effect of writing REG_TCR2_EL1 in cpu_do_resume() take effect before this unsynchronized write to SYS_POR_EL1 when no isb separates the two system register writes?
This rests on an ARM architecture detail that the review could not confirm from the kernel source. The MMU is already enabled when _cpu_resume() runs, and cpu_do_resume() restores REG_TCR2_EL1 (with TCR2_EL1_POE set once commit "arm64: Enable kpkeys" enables stage-1 POE at EL1) and performs further instruction fetches and memory loads before the final isb:
arch/arm64/mm/proc.S:cpu_do_resume() {
...
alternative_if ARM64_HAS_TCR2
ldr x2, [x0, #104]
msr REG_TCR2_EL1, x2
alternative_else_nop_endif
alternative_if ARM64_SME
ldr x2, [x0, #112]
msr_s SYS_TPIDR2_EL0, x2alternative_else_nop_endif
...
ptrauth_keys_install_kernel_nosync x14, x1, x2, x3 isb
ret
}
If the architecture permits enabling stage-1 permission overlay in REG_TCR2_EL1 to take effect on those intervening fetches or loads before the write to SYS_POR_EL1 takes effect, the CPU could evaluate kernel permission overlays against the unknown reset value of POR_EL1 and trigger a fatal EL1 permission fault during resume.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
RFC PATCH v10 8/22 arm64: Context-switch POR_EL1
In reply to Kevin BrodskyPOR_EL1 is about to be used by the kpkeys framework, modifying it for (typically small) sections of code. If an exception occurs during that window and scheduling occurs, we must ensure that POR_EL1 is context-switched as needed (saving the old value and restoring the new one). An ISB is needed to ensure the write takes effect, so we skip it if the new value is the same as the old, like for POR_EL0.
--- arch/arm64/include/asm/processor.h | 1 + arch/arm64/kernel/process.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
arch/arm64/include/asm/processor.h +1 -0
@@ -195,6 +195,7 @@ struct thread_struct {u64 svcr;u64 tpidr2_el0;u64 por_el0; +u64 por_el1;#ifdef CONFIG_ARM64_GCS unsigned int gcs_el0_mode;unsigned int gcs_el0_locked;
arch/arm64/kernel/process.c +10 -0
@@ -51,6 +51,7 @@#include <asm/exec.h> #include <asm/fpsimd.h> #include <asm/gcs.h> +#include <asm/kpkeys.h> #include <asm/mmu_context.h> #include <asm/mpam.h> #include <asm/mte.h>
@@ -446,6 +447,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)ptrauth_thread_init_kernel (p); +if (system_supports_poe ()) + p->thread.por_el1 =POR_EL1_INIT ; +if (likely (!args->fn)) { *childregs = *current_pt_regs (); childregs->regs[0 ] =0 ;
@@ -698,6 +702,12 @@ static void permission_overlay_switch(struct task_struct *next)* of POR_EL0. */ } + + current->thread.por_el1 =read_sysreg_s (SYS_POR_EL1 ); +if (current->thread.por_el1 != next->thread.por_el1) { +write_sysreg_s (next->thread.por_el1,SYS_POR_EL1 ); +isb (); + } }/*
Signature · 1 line
2.56.0
RFC PATCH v10 9/22 arm64: mm: Enable overlays for all EL1 indirect permissions
In reply to Kevin BrodskyIn preparation of using POE inside the kernel, enable "Overlay applied" for kernel memory types in PIR_EL1. This ensures that the permissions set in POR_EL1 affect all kernel mappings.
User memory types must be left untouched (overlays not applied) because any privileged access to user memory (e.g. futex atomic without FEAT_LSUI) would then be mistakenly checked against POR_EL1.
--- arch/arm64/include/asm/pgtable-prot.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable-prot.h +4 -4
@@ -183,9 +183,9 @@ static inline bool __pure lpa2_is_enabled(void)PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_SHARED_EXEC),PIE_RW ) | \PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_READONLY),PIE_R ) | \PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_SHARED),PIE_RW ) | \ -PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_KERNEL_ROX),PIE_RX ) | \ -PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_KERNEL_EXEC),PIE_RWX ) | \ -PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_KERNEL_RO),PIE_R ) | \ - PIRx_ELx_PERM_PREP(pte_pi_index(_PAGE_KERNEL), PIE_RW)) +PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_KERNEL_ROX),PIE_RX_O ) | \ +PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_KERNEL_EXEC),PIE_RWX_O ) | \ +PIRx_ELx_PERM_PREP (pte_pi_index (_PAGE_KERNEL_RO),PIE_R_O ) | \ + PIRx_ELx_PERM_PREP(pte_pi_index(_PAGE_KERNEL), PIE_RW_O)) #endif /* __ASM_PGTABLE_PROT_H */
Signature · 1 line
2.56.0
RFC PATCH v10 10/22 arm64: Enable kpkeys
In reply to Kevin BrodskyThis is the final step to enable kpkeys on arm64. We enable POE at EL1 by setting TCR2_EL1.POE, and initialise POR_EL1 to the default value, enabling access to the default pkey/POIndex (0). An ISB is added so that POE restrictions are enforced immediately.
Having done this, we can now select ARCH_HAS_KPKEYS if ARM64_POE is enabled.
--- arch/arm64/Kconfig | 1 + arch/arm64/kernel/cpufeature.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +1 -0
@@ -2300,6 +2300,7 @@ config ARM64_POE def_bool y select ARCH_USES_HIGH_VMA_FLAGS select ARCH_HAS_PKEYS + select ARCH_HAS_KPKEYS help The Permission Overlay Extension is used to implement Memory Protection Keys. Memory Protection Keys provides a mechanism for
arch/arm64/kernel/cpufeature.c +4 -1
@@ -76,6 +76,7 @@#include <linux/kasan.h> #include <linux/percpu.h> #include <linux/sched/isolation.h> +#include <linux/kpkeys.h> #include <asm/arm_pmuv3.h> #include <asm/cpu.h>
@@ -2334,8 +2335,10 @@ static void cpu_enable_mops(const struct arm64_cpu_capabilities *__unused)#ifdef CONFIG_ARM64_POE static void cpu_enable_poe (const struct arm64_cpu_capabilities *__unused) { -sysreg_clear_set (REG_TCR2_EL1 ,0 ,TCR2_EL1_E0POE ); +write_sysreg_s (POR_EL1_INIT ,SYS_POR_EL1 ); +sysreg_clear_set (REG_TCR2_EL1 ,0 ,TCR2_EL1_E0POE |TCR2_EL1_POE );sysreg_clear_set (CPACR_EL1 ,0 ,CPACR_EL1_E0POE ); +isb (); }#endif
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v10 10/22] arm64: Enable kpkeys
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] arm64: cpufeature: cpu_enable_poe() enables TCR2_EL1_POE without S1PIE --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] arm64: cpufeature: cpu_enable_poe() enables TCR2_EL1_POE without S1PIE
--
commit 916b4efd6b8494c5d63fd0effff9032def1dba2d
Author: Kevin Brodsky <[email protected]>
arm64: Enable kpkeys
This commit enables POE at EL1 by initializing POR_EL1, setting TCR2_EL1.POE in cpu_enable_poe(), and selecting ARCH_HAS_KPKEYS under CONFIG_ARM64_POE.
Quoted 4 lines
arch/arm64/kernel/cpufeature.c +0
-0
[ ... ]
arch/arm64/kernel/cpufeature.c static void cpu_enable_mops(const struct arm64_cpu_capabilities *__unused) · line 2343
Quoted 11 lines
arch/arm64/kernel/cpufeature.c +3
-1@@ -2334,8 +2335,10 @@ static void cpu_enable_mops(const struct arm64_cpu_capabilities *__unused)#ifdef CONFIG_ARM64_POE static void cpu_enable_poe (const struct arm64_cpu_capabilities *__unused) { -sysreg_clear_set (REG_TCR2_EL1 ,0 ,TCR2_EL1_E0POE ); +write_sysreg_s (POR_EL1_INIT ,SYS_POR_EL1 ); +sysreg_clear_set (REG_TCR2_EL1 ,0 ,TCR2_EL1_E0POE |TCR2_EL1_POE );sysreg_clear_set (CPACR_EL1 ,0 ,CPACR_EL1_E0POE ); +isb (); }#endif
[Severity: Medium]
Does enabling TCR2_EL1_POE here break privileged EL1 accesses to user memory when S1POE is present without S1PIE (for example, in a KVM guest configured with FEAT_S1POE=1 and FEAT_S1PIE=0)?
Commit "arm64: mm: Enable overlays for all EL1 indirect permissions" relies on PIR_EL1 (PIE_E1) leaving user memory types without overlay (_O) so that privileged EL1 accesses to user pages (such as futex_atomic_op_inuser() using uaccess_enable_privileged() when FEAT_LSUI is absent) are not checked against POR_EL1. When S1PIE is absent, stage-1 translations use direct permissions and TCR2_EL1_POE applies POR_EL1 to all privileged accesses, including TTBR0 user mappings.
Because POR_EL1_INIT only permits pkey 0, a user mapping with pkey 1..7 causes futex_atomic_op_inuser() to fault against POR_EL1 while fault_in_user_writeable() succeeds with unprivileged sttr against POR_EL0, resulting in an unbounded retry loop in futex_wake_op().
Should TCR2_EL1_POE and arch_supports_kpkeys() (or ARM64_HAS_S1POE itself) require S1PIE to be supported and enabled?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH RFC v10 10/22] arm64: Enable kpkeys
Quoted 7 lines · On 10/11/2026 17:02, [email protected] wrote:
Quoted 7 lines
arch/arm64/kernel/cpufeature.c +2
-1@@ -2334,8 +2335,10 @@ static void cpu_enable_mops(const struct arm64_cpu_capabilities *__unused)#ifdef CONFIG_ARM64_POE static void cpu_enable_poe (const struct arm64_cpu_capabilities *__unused) { -sysreg_clear_set (REG_TCR2_EL1 ,0 ,TCR2_EL1_E0POE ); +write_sysreg_s (POR_EL1_INIT ,SYS_POR_EL1 ); +sysreg_clear_set (REG_TCR2_EL1 ,0 ,TCR2_EL1_E0POE |TCR2_EL1_POE );
Quoted 8 lines
arch/arm64/kernel/cpufeature.c static void cpu_enable_mops(const struct arm64_cpu_capabilities *__unused) · line 2343
Quoted 4 lines
arch/arm64/kernel/cpufeature.c +1
-0sysreg_clear_set (CPACR_EL1 ,0 ,CPACR_EL1_E0POE ); + isb(); }#endif [Severity: Medium]
Does enablingTCR2_EL1_POEhere break privileged EL1 accesses to user memory when S1POE is present without S1PIE (for example, in a KVM guest configured with FEAT_S1POE=1 and FEAT_S1PIE=0)?
That one is a very good point. FEAT_S1POE does not require FEAT_S1PIE, so it is technically possible that an implementation has POE but not PIE, and in that case things will go wrong as described below (confirmed on FVP).
There are ways we could deal with this, but they are more invasive as, without PIE, POR_EL1 restricts all accesses via TTBR1. Without evidence that POE would ever be implemented without PIE, I think it is reasonable to remove this possibility by requiring PIE when detecting POE.
- Kevin
Quoted 15 lines
Commit "arm64: mm: Enable overlays for all EL1 indirect permissions" relies on
PIR_EL1(PIE_E1) leaving user memory types without overlay (_O) so that privileged EL1 accesses to user pages (such asfutex_atomic_op_inuser()usinguaccess_enable_privileged()whenFEAT_LSUIis absent) are not checked againstPOR_EL1. When S1PIE is absent, stage-1 translations use direct permissions andTCR2_EL1_POEappliesPOR_EL1to all privileged accesses, including TTBR0 user mappings.Because
POR_EL1_INITonly permits pkey 0, a user mapping with pkey 1..7 causesfutex_atomic_op_inuser()to fault againstPOR_EL1whilefault_in_user_writeable()succeeds with unprivileged sttr againstPOR_EL0, resulting in an unbounded retry loop infutex_wake_op().Should
TCR2_EL1_POEandarch_supports_kpkeys()(orARM64_HAS_S1POEitself) require S1PIE to be supported and enabled?
RFC PATCH v10 11/22 mm: kpkeys: Introduce kpkeys_hardened_pgtables feature
In reply to Kevin Brodskykpkeys_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 <asm/kpkeys.h>. 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.
--- 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(+)
include/linux/kpkeys.h +33 -0
@@ -4,6 +4,7 @@#include <linux/bug.h> #include <linux/cleanup.h> +#include <linux/jump_label.h> #include <linux/kpkeys_types.h> /**
@@ -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 */
include/linux/kpkeys_types.h +5 -0
@@ -8,10 +8,15 @@#include <asm/kpkeys_types.h> #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 , };
mm/Kconfig +3 -0
@@ -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
mm/Makefile +1 -0
@@ -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
mm/kpkeys_hardened_pgtables.c new file +16 -0
@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/kpkeys.h> +#include <linux/mm.h> + +#include <kunit/visibility.h> + +__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); +}
mm/mm_init.c +2 -0
@@ -21,6 +21,7 @@#include <linux/buffer_head.h> #include <linux/kmemleak.h> #include <linux/kfence.h> +#include <linux/kpkeys.h> #include <linux/page_ext.h> #include <linux/pti.h> #include <linux/pgtable.h>
@@ -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 */
security/Kconfig.hardening +12 -0
@@ -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
Signature · 1 line
2.56.0
RFC PATCH v10 12/22 mm: kpkeys: Protect regular page tables
In reply to Kevin BrodskyIf the kpkeys_hardened_pgtables feature is enabled, page table pages (PTPs) should be protected by modifying the linear mapping to map them with a privileged pkey (KPKEYS_PKEY_PGTABLES). This patch introduces a new page allocator for that purpose:
* kpkeys_pgtable_alloc() allocates a new PTP and sets the linear mapping to KPKEYS_PKEY_PGTABLES for that page
* kpkeys_pgtable_free() frees such a PTP and restores the linear mapping to the default pkey
This interface is then hooked into pagetable_alloc() and pagetable_free(), protecting all page tables created once the buddy allocator is available. Early page tables are allocated in other ways and will be protected in subsequent patches.
This implementation of kpkeys_pgtable_{alloc,free}() is minimal and relies on the linear map being fully PTE-mapped - otherwise calling set_direct_map_pkey() on a single page may result in splitting a block mapping, which in turn requires allocating a new PTP. A more elaborate implementation could be added later to handle this situation.
--- include/linux/kpkeys.h | 10 +++++++++ include/linux/mm.h | 14 +++++++++++-- mm/kpkeys_hardened_pgtables.c | 47 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-)
include/linux/kpkeys.h +10 -0
@@ -119,6 +119,9 @@ static inline bool kpkeys_hardened_pgtables_early_enabled(void) return arch_supports_kpkeys_early(); } +struct page *kpkeys_pgtable_alloc (gfp_t gfp,unsigned int order); +void kpkeys_pgtable_free (struct page *page,unsigned int order); +void kpkeys_hardened_pgtables_init (void );#else /* CONFIG_KPKEYS_HARDENED_PGTABLES */
@@ -133,6 +136,13 @@ static inline bool kpkeys_hardened_pgtables_early_enabled(void) return false; } +static inline struct page *kpkeys_pgtable_alloc (gfp_t gfp,unsigned int order) +{ +return NULL ; +} + +static inline void kpkeys_pgtable_free (struct page *page,unsigned int order) {} +static inline void kpkeys_hardened_pgtables_init (void ) {}#endif /* CONFIG_KPKEYS_HARDENED_PGTABLES */
include/linux/mm.h +12 -2
@@ -39,6 +39,7 @@#include <linux/iommu-debug-pagealloc.h> #include <linux/kcsan-checks.h> #include <linux/vmemmap-optimization.h> +#include <linux/kpkeys.h> struct mempolicy ;struct anon_vma ;
@@ -3950,7 +3951,12 @@ static inline bool ptdesc_test_kernel(const struct ptdesc *ptdesc)*/ static inline struct ptdesc *pagetable_alloc_noprof (gfp_t gfp,unsigned int order) { -struct page *page =alloc_pages_noprof (gfp | __GFP_COMP, order); +struct page *page; + +if (kpkeys_hardened_pgtables_enabled ()) + page =kpkeys_pgtable_alloc (gfp | __GFP_COMP, order); +else + page =alloc_pages_noprof (gfp | __GFP_COMP, order);return page_ptdesc (page); }
@@ -3959,8 +3965,12 @@ static inline struct ptdesc *pagetable_alloc_noprof(gfp_t gfp, unsigned int ordestatic inline void __pagetable_free (struct ptdesc *pt) {struct page *page =ptdesc_page (pt); +unsigned int order =compound_order (page); -__free_pages (page,compound_order (page)); +if (kpkeys_hardened_pgtables_enabled ()) +kpkeys_pgtable_free (page, order); +else +__free_pages (page, order); }#ifdef CONFIG_ASYNC_KERNEL_PGTABLE_FREE
mm/kpkeys_hardened_pgtables.c +47 -0
59 lines not on this page · raw message
Signature · 1 line
2.56.0
RFC PATCH v10 13/22 mm: kpkeys: Protect early kernel page tables
In reply to Kevin BrodskyInitial kernel page tables (kernel image, direct map, vmemmap, etc.) are not allocated using pagetable_alloc() - the buddy allocator isn't ready at that point. We need to use a different approach to protect them.
This patch takes the "brute force" route: when kpkeys_hardened_pgtables is enabled, walk all installed kernel page table, setting the pkey of their backing pages in the direct map along the way.
The standard pagewalk API is used for that purpose, albeit in a slightly unusual way:
* Visiting an entry is used to set the pkey of the table it points to, hence: .pmd_entry -> protect_pte_table
* PTE entries are always leaves, so no need to visit them (no .pte_entry callback)
* The range is meant to capture all installed kernel page tables. There doesn't seem to be a generic way to express this; [PAGE_OFFSET, ULONG_MAX) seems to be good enough on arm64/x86.
Locking probably isn't necessary so early during boot, but taking the mmap_write_lock shouldn't hurt either. We also assume that all page tables installed after that point are allocated using pagetable_alloc(), which seems to be the case, at least on arm64. There are some more assumptions - see comment above protect_early_kernel_pgtables().
This simple approach should reliably protect all early kernel page tables without invasive changes. However, adding support for large blocks (PMD/PUD) is likely to be difficult and/or inefficient as splitting will ocur when protecting individual page table pages. Modifying the early page table allocators directly may be preferable.
--- mm/kpkeys_hardened_pgtables.c | 122 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+)
mm/kpkeys_hardened_pgtables.c +122 -0
@@ -1,6 +1,8 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/kpkeys.h> #include <linux/mm.h> +#include <linux/pagewalk.h> +#include <linux/pgtable.h> #include <linux/set_memory.h> #include <kunit/visibility.h>
@@ -54,10 +56,130 @@ void kpkeys_pgtable_free(struct page *page, unsigned int order)__free_pages (page, order); } +static void __init protect_pgtable_page (void *page) +{ +int ret; + + ret =set_direct_map_pkey ((unsigned long )page,1 ,KPKEYS_PKEY_PGTABLES ); +WARN_ON_ONCE (ret); +} + +static int __init protect_pte_table (pmd_t *pmd,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +pmd_t entry =pmdp_get (pmd); + +if (!pmd_present (entry) ||pmd_leaf (entry)) +return 0 ; + +protect_pgtable_page (pte_offset_kernel (pmd,0 )); + +return 0 ; +} + +static int __init protect_pmd_table (pud_t *pud,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +pud_t entry; + +if (mm_pmd_folded (walk->mm)) +return 0 ; + + entry =pudp_get (pud); +if (!pud_present (entry) ||pud_leaf (entry)) +return 0 ; + +protect_pgtable_page (pmd_offset (pud,0 )); + +return 0 ; +} + +static int __init protect_pud_table (p4d_t *p4d,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +p4d_t entry; + +if (mm_pud_folded (walk->mm)) +return 0 ; + + entry =p4dp_get (p4d); +if (!p4d_present (entry) ||p4d_leaf (entry)) +return 0 ; + +protect_pgtable_page (pud_offset (p4d,0 )); + +return 0 ; +} + +static int __init protect_p4d_table (pgd_t *pgd,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +pgd_t entry; + +if (mm_p4d_folded (walk->mm)) +return 0 ; + + entry =pgdp_get (pgd); +if (!pgd_present (entry) ||pgd_leaf (entry)) +return 0 ; + +protect_pgtable_page (p4d_offset (pgd,0 )); + +return 0 ; +} + +static const struct mm_walk_ops protect_pgtable_ops__initconst = { + .pmd_entry = protect_pte_table, + .pud_entry = protect_pmd_table, + .p4d_entry = protect_pud_table, + .pgd_entry = protect_p4d_table, +}; + +/* +* Protect all installed kernel page tables by walking them and setting the +* direct mapping of all backing pages to pkey KPKEYS_PKEY_PGTABLES. We assume +* all non-root levels (PTE to P4D) are page-sized (asserts below). +* +* The root PGD page is left untouched; it is normally accessed via the kimg +* mapping only. On arm64 it cannot be written directly via any mapping. +* +* This approach is unsafe if set_direct_map_pkey() may split blocks in the +* direct map; we rely on the direct map being fully PTE-mapped. +*/ +static void __init protect_early_kernel_pgtables (void ) +{ +int ret; + +mmap_write_lock (&init_mm); +/* +* [PAGE_OFFSET, ULONG_MAX) covers all early kernel mappings on arm64 +* and x86 (at least). +*/ + ret =walk_kernel_page_table_range (PAGE_OFFSET ,ULONG_MAX , + &protect_pgtable_ops,NULL ,NULL ); +mmap_write_unlock (&init_mm); +WARN_ON_ONCE (ret); +} + +static_assert (MAX_PTRS_PER_PTE *sizeof (pte_t) ==PAGE_SIZE ); + +#ifndef __PAGETABLE_PMD_FOLDED +static_assert (MAX_PTRS_PER_PMD *sizeof (pmd_t) ==PAGE_SIZE ); +#endif + +#ifndef __PAGETABLE_PUD_FOLDED +static_assert (MAX_PTRS_PER_PUD *sizeof (pud_t) ==PAGE_SIZE ); +#endif + +#ifndef __PAGETABLE_P4D_FOLDED +static_assert (MAX_PTRS_PER_P4D *sizeof (p4d_t) ==PAGE_SIZE ); +#endif +void __init kpkeys_hardened_pgtables_init (void ) {if (!kpkeys_supported ())return ;static_branch_enable (&kpkeys_hardened_pgtables_key); +protect_early_kernel_pgtables (); }
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v10 13/22] mm: kpkeys: Protect early kernel page tables
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mm: kpkeys: redundant KASAN shadow walks in protect_early_kernel_pgtables() - [Medium] mm: kpkeys…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mm: kpkeys: redundant KASAN shadow walks in protect_early_kernel_pgtables()
- [Medium] mm: kpkeys: repeated swapper_pg_dir walks in protect_early_kernel_pgtables()
--
commit 9b67b4dcc54e06a0681d8d0ff9c11cb3015125f0
Author: Kevin Brodsky <[email protected]>
mm: kpkeys: Protect early kernel page tables
This commit walks the installed kernel page tables during boot and sets the direct-map pkey of their backing pages to KPKEYS_PKEY_PGTABLES when kpkeys_hardened_pgtables is enabled.
Quoted 69 lines
mm/kpkeys_hardened_pgtables.c +61
-0@@ -54,10 +56,130 @@ void kpkeys_pgtable_free(struct page *page, unsigned int order)__free_pages (page, order); } +static void __init protect_pgtable_page (void *page) +{ +int ret; + + ret =set_direct_map_pkey ((unsigned long )page,1 ,KPKEYS_PKEY_PGTABLES ); +WARN_ON_ONCE (ret); +} + +static int __init protect_pte_table (pmd_t *pmd,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +pmd_t entry =pmdp_get (pmd); + +if (!pmd_present (entry) ||pmd_leaf (entry)) +return 0 ; + +protect_pgtable_page (pte_offset_kernel (pmd,0 )); + +return 0 ; +} + +static int __init protect_pmd_table (pud_t *pud,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +pud_t entry; + +if (mm_pmd_folded (walk->mm)) +return 0 ; + + entry =pudp_get (pud); +if (!pud_present (entry) ||pud_leaf (entry)) +return 0 ; + +protect_pgtable_page (pmd_offset (pud,0 )); + +return 0 ; +} + +static int __init protect_pud_table (p4d_t *p4d,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +p4d_t entry; + +if (mm_pud_folded (walk->mm)) +return 0 ; + + entry =p4dp_get (p4d); +if (!p4d_present (entry) ||p4d_leaf (entry)) +return 0 ; + +protect_pgtable_page (pud_offset (p4d,0 )); + +return 0 ; +} + +static int __init protect_p4d_table (pgd_t *pgd,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +pgd_t entry; + +if (mm_p4d_folded (walk->mm))
mm/kpkeys_hardened_pgtables.c void kpkeys_pgtable_free(struct page *page, unsigned int order) · line 129
Quoted 10 lines
mm/kpkeys_hardened_pgtables.c +10
-0+return 0 ; + + entry =pgdp_get (pgd); +if (!pgd_present (entry) ||pgd_leaf (entry)) +return 0 ; + +protect_pgtable_page (p4d_offset (pgd,0 )); + +return 0 ; +}
[Severity: Medium]
When CONFIG_KASAN_GENERIC or CONFIG_KASAN_SW_TAGS is enabled, does this walk and call set_direct_map_pkey() millions of times on the shared kasan_early_shadow_{p4d,pud,pmd,pte} tables?
kasan_init() runs in setup_arch() before mm_core_init() calls kpkeys_hardened_pgtables_init(), populating unbacked shadow ranges in [PAGE_OFFSET, ULONG_MAX) with shared static tables where all 512 entries at each level point to the same next-level kasan_early_shadow_* page. Unlike mm/ptdump.c, which detects those tables and sets walk->action = ACTION_CONTINUE, the callbacks here descend through every entry and invoke set_direct_map_pkey() (including a direct-map page table walk and broadcast TLB flush) for every 2 MiB PMD slot across the multi-terabyte shadow range, stalling boot.
[ ... ]
Quoted 4 lines
mm/kpkeys_hardened_pgtables.c +4
-0+static void __init protect_early_kernel_pgtables (void ) +{ +int ret; +
mm/kpkeys_hardened_pgtables.c void kpkeys_pgtable_free(struct page *page, unsigned int order) · line 162
Quoted 10 lines
mm/kpkeys_hardened_pgtables.c +10
-0+mmap_write_lock (&init_mm); +/* +* [PAGE_OFFSET, ULONG_MAX) covers all early kernel mappings on arm64 +* and x86 (at least). +*/ + ret =walk_kernel_page_table_range (PAGE_OFFSET ,ULONG_MAX , + &protect_pgtable_ops,NULL ,NULL ); +mmap_write_unlock (&init_mm); +WARN_ON_ONCE (ret); +}
[Severity: Medium]
On arm64 with CONFIG_ARM64_LPA2=y running on hardware where the top page table level is dynamically folded (vabits_actual < VA_BITS), does starting at PAGE_OFFSET walk and re-protect swapper_pg_dir 16 or 32 times?
On arm64, PAGE_OFFSET is the compile-time _PAGE_OFFSET(VA_BITS) rather than _PAGE_OFFSET(vabits_actual). When !pgtable_l5_enabled() on 4K pages (or !pgtable_l4_enabled() on 16K pages), pgd_none() is always false and pgd_to_folded_p4d() (or p4d_to_folded_pud()) wraps each top-level step in [PAGE_OFFSET, ULONG_MAX) back to the start of swapper_pg_dir:
arch/arm64/include/asm/pgtable.h:pgd_to_folded_p4d() {
return (p4d_t *)PTR_ALIGN_DOWN(pgdp, PAGE_SIZE) + p4d_index(addr);
}As a result, walk_kernel_page_table_range() traverses the entire kernel page table hierarchy and flushes TLBs across all page table pages 16 times on 4K pages (or 32 times on 16K pages) during boot.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Re: [PATCH RFC v10 13/22] mm: kpkeys: Protect early kernel page tables
Quoted 12 lines · On 10/11/2026 17:02, [email protected] wrote:
Quoted 12 lines
mm/kpkeys_hardened_pgtables.c +12
-0+static int __init protect_p4d_table (pgd_t *pgd,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +pgd_t entry; + +if (mm_p4d_folded (walk->mm)) +return 0 ; + + entry =pgdp_get (pgd); +if (!pgd_present (entry) ||pgd_leaf (entry)) +return 0 ; +
Quoted 8 lines
mm/kpkeys_hardened_pgtables.c void kpkeys_pgtable_free(struct page *page, unsigned int order) · line 129
Quoted 4 lines
mm/kpkeys_hardened_pgtables.c +4
-0+protect_pgtable_page (p4d_offset (pgd,0 )); + +return 0 ; +}[Severity: Medium]
WhenCONFIG_KASAN_GENERICorCONFIG_KASAN_SW_TAGSis enabled, does this walk and callset_direct_map_pkey()millions of times on the shared kasan_early_shadow_{p4d,pud,pmd,pte} tables?
Yes, KASAN support needs to be addressed, as mentioned in the cover letter.
Quoted 37 lines
kasan_init()runs insetup_arch()beforemm_core_init()callskpkeys_hardened_pgtables_init(), populating unbacked shadow ranges in [PAGE_OFFSET,ULONG_MAX) with shared static tables where all 512 entries at each level point to the same next-level kasan_early_shadow_* page. Unlikemm/ptdump.c, which detects those tables and setswalk->action=ACTION_CONTINUE, the callbacks here descend through every entry and invokeset_direct_map_pkey()(including a direct-map page table walk and broadcast TLB flush) for every 2 MiB PMD slot across the multi-terabyte shadow range, stalling boot.[ ... ]
Quoted 4 lines
mm/kpkeys_hardened_pgtables.c +4
-0+static void __init protect_early_kernel_pgtables (void ) +{ +int ret; +mm/kpkeys_hardened_pgtables.c void kpkeys_pgtable_free(struct page *page, unsigned int order) · line 162
Quoted 10 lines
mm/kpkeys_hardened_pgtables.c +10
-0+mmap_write_lock (&init_mm); +/* +* [PAGE_OFFSET, ULONG_MAX) covers all early kernel mappings on arm64 +* and x86 (at least). +*/ + ret =walk_kernel_page_table_range (PAGE_OFFSET ,ULONG_MAX , + &protect_pgtable_ops,NULL ,NULL ); +mmap_write_unlock (&init_mm); +WARN_ON_ONCE (ret); +}[Severity: Medium]
On arm64 withCONFIG_ARM64_LPA2=yrunning on hardware where the top page table level is dynamically folded (vabits_actual<VA_BITS), does starting atPAGE_OFFSETwalk and re-protectswapper_pg_dir16 or 32 times?On arm64,
PAGE_OFFSETis the compile-time_PAGE_OFFSET(VA_BITS)rather than_PAGE_OFFSET(vabits_actual). When !pgtable_l5_enabled() on 4K pages (or !pgtable_l4_enabled() on 16K pages),pgd_none()is always false andpgd_to_folded_p4d()(orp4d_to_folded_pud()) wraps each top-level step in [PAGE_OFFSET,ULONG_MAX) back to the start ofswapper_pg_dir:
Quoted 7 lines
arch/arm64/include/asm/pgtable.h:pgd_to_folded_p4d() { return (p4d_t *)PTR_ALIGN_DOWN(pgdp, PAGE_SIZE) + p4d_index(addr); }As a result,
walk_kernel_page_table_range()traverses the entire kernel page table hierarchy and flushes TLBs across all page table pages 16 times on 4K pages (or 32 times on 16K pages) during boot.
Right. Addresses don't really matter here, so for the next version I'm planning to use the synthetic range [0, SZ) where SZ is PTRS_PER_PXX * PXX_SIZE and PXX is the highest non-folded level (PGD if there is no folding).
- Kevin
RFC PATCH v10 14/22 mm: kpkeys: Protect vmemmap page tables
In reply to Kevin BrodskyThe kpkeys_hardened_pgtables feature automatically protect early vmemmap page tables (covering standard boot memory) when it is enabled.
In order to protect late vmemmap page tables as well, allocate them like regular page tables using pagetable_alloc().
pagetable_alloc() does the right thing to protect the backing pages using kpkeys, but isn't NUMA-aware, so the pages may be allocated on any node. This could potentially incur some overhead on large NUMA systems that make heavy use of memory hotplug.
The arm64 hotplug code is also amended to use a matching pagetable_free(), ensuring that the pkey is reset when the page tables are freed. x86 already uses pagetable_free() on that path.
Unlike in vmemmap_alloc_block(), __GFP_RETRY_MAYFAIL is not used as it isn't justified for allocating page tables - this disables the OOM and we do not have a fallback if we fail to allocate page tables. See previous discussion linked below.
---
This is a minimal patch to protect vmemmmap page tables. More work may be needed here:
* Restoring NUMA awareness
* Moving the arm64 change to a separate commit?
* General refactoring of how these page tables are allocated: since we are not using the standard per-level functions (e.g. pmd_alloc()), we are not calling pagetable_*_ctor() or ptdesc_set_kernel(). I sent a separate series [1] that partially addresses this, but more work is still needed.
* x86 allows boot memory to be hot-removed in some circumstances. free_pagetable() should handle this situation by resetting the direct map pkey of reserved pages.
[1] https://lore.kernel.org/all/[email protected]/
--- arch/arm64/mm/mmu.c | 2 +- mm/sparse-vmemmap.c | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-)
arch/arm64/mm/mmu.c +1 -1
@@ -1496,7 +1496,7 @@ static void free_hotplug_page_range(struct page *page, size_t size,static void free_hotplug_pgtable_page (struct page *page) {pagetable_dtor (page_ptdesc (page)); -free_hotplug_page_range (page,PAGE_SIZE ,NULL ); +pagetable_free (page_ptdesc (page)); } static bool pgtable_range_aligned(unsigned long start, unsigned long end,
mm/sparse-vmemmap.c +16 -7
@@ -150,13 +150,22 @@ int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pagesreturn 0 ; } -static void *__meminit vmemmap_alloc_block_zero (unsigned long size,int node) +static void *__meminit vmemmap_alloc_pgtable (int node) { -void *p =vmemmap_alloc_block (size, node); +void *p; + +if (slab_is_available ()) { +gfp_t gfp =GFP_KERNEL | __GFP_ZERO; +struct ptdesc *ptdesc =pagetable_alloc (gfp,0 ); + +return ptdesc ?ptdesc_address (ptdesc) :NULL ; + } + + p =vmemmap_alloc_block (PAGE_SIZE , node);if (!p)return NULL ; -memset (p,0 , size); +memset (p,0 ,PAGE_SIZE );return p; }
@@ -305,7 +314,7 @@ static pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, in {pmd_t *pmd =pmd_offset (pud, addr);if (pmd_none (*pmd)) { -void *p =vmemmap_alloc_block_zero (PAGE_SIZE , node); +void *p =vmemmap_alloc_pgtable (node);if (!p)return NULL ;kernel_pte_init (p);
@@ -318,7 +327,7 @@ static pud_t * __meminit vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, in {pud_t *pud =pud_offset (p4d, addr);if (pud_none (*pud)) { -void *p =vmemmap_alloc_block_zero (PAGE_SIZE , node); +void *p =vmemmap_alloc_pgtable (node);if (!p)return NULL ;pmd_init (p);
@@ -331,7 +340,7 @@ static p4d_t * __meminit vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, in {p4d_t *p4d =p4d_offset (pgd, addr);if (p4d_none (*p4d)) { -void *p =vmemmap_alloc_block_zero (PAGE_SIZE , node); +void *p =vmemmap_alloc_pgtable (node);if (!p)return NULL ;pud_init (p);
@@ -344,7 +353,7 @@ static pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node) {pgd_t *pgd =pgd_offset_k (addr);if (pgd_none (*pgd)) { -void *p =vmemmap_alloc_block_zero (PAGE_SIZE , node); +void *p =vmemmap_alloc_pgtable (node);if (!p)return NULL ;pgd_populate_kernel (addr, pgd, p);
Signature · 1 line
2.56.0
RFC PATCH v10 15/22 arm64: kpkeys: Implement arch_supports_kpkeys_early()
In reply to Kevin BrodskyWe need to check if the kpkeys_hardened_pgtables feature is going to be enabled very early during boot, to decide how to set up the linear map and how to allocate early page tables. This happens even before boot CPU features are detected.
Implement the arch_supports_kpkeys_early() helper by directly checking if the boot CPU supports POE, if it is called before boot CPU features are detected. It may also be called later, in which case we simply check the POE feature.
--- arch/arm64/include/asm/cpufeature.h | 12 ++++++++++++ arch/arm64/include/asm/kpkeys.h | 7 +++++++ 2 files changed, 19 insertions(+)
arch/arm64/include/asm/cpufeature.h +12 -0
@@ -1114,6 +1114,18 @@ static inline bool cpu_has_lpa2(void)#endif } +static inline bool cpu_has_poe (void ) +{ +u64 mmfr3; + +if (!IS_ENABLED (CONFIG_ARM64_POE )) +return false; + + mmfr3 =read_sysreg_s (SYS_ID_AA64MMFR3_EL1 ); +return cpuid_feature_extract_unsigned_field (mmfr3, +ID_AA64MMFR3_EL1_S1POE_SHIFT ); +} +#endif /* __ASSEMBLER__ */ #endif
arch/arm64/include/asm/kpkeys.h +7 -0
@@ -21,6 +21,13 @@ static inline bool arch_supports_kpkeys(void)return system_supports_poe (); } +static inline bool arch_supports_kpkeys_early (void ) +{ +/* POE is a boot feature */ +return boot_capabilities_finalized () ? +system_supports_poe () :cpu_has_poe (); +} +#ifdef CONFIG_ARM64_POE static inline u64 por_set_kpkeys_context (u64 por,enum kpkeys_ctx ctx)
Signature · 1 line
2.56.0
RFC PATCH v10 16/22 arm64: kpkeys: Support KPKEYS_CTX_PGTABLES
In reply to Kevin BrodskyEnable RW access to KPKEYS_PKEY_PGTABLES (used to map page table pages) if switching to KPKEYS_CTX_PGTABLES, otherwise only grant RO access.
--- arch/arm64/include/asm/kpkeys.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kpkeys.h +4 -1
@@ -12,7 +12,8 @@* Equivalent to por_set_kpkeys_context(0, KPKEYS_CTX_DEFAULT), but can also be * used in assembly. */ -#define POR_EL1_INIT POR_ELx_PERM_PREP(0, POE_RWX) +#define POR_EL1_INIT (POR_ELx_PERM_PREP(0, POE_RWX) | \ + POR_ELx_PERM_PREP(KPKEYS_PKEY_PGTABLES, POE_R))#ifndef __ASSEMBLY__
@@ -33,6 +34,8 @@ static inline bool arch_supports_kpkeys_early(void)static inline u64 por_set_kpkeys_context (u64 por,enum kpkeys_ctx ctx) { por =por_elx_set_pkey_perms (por,0 ,POE_RWX ); + por =por_elx_set_pkey_perms (por,KPKEYS_PKEY_PGTABLES , + ctx ==KPKEYS_CTX_PGTABLES ?POE_RW :POE_R );return por; }
Signature · 1 line
2.56.0
RFC PATCH v10 17/22 arm64: kpkeys: Ensure the linear map can be modified
In reply to Kevin BrodskyWhen the kpkeys_hardened_pgtables feature is enabled, we need to be able to modify attributes (specifically the pkey/POIndex) in the linear map at page granularity.
Add the appropriate check to can_set_direct_map() on the same principle as rodata_full and other features.
kpkeys_hardened_pgtables currently requires the linear map to be fully PTE-mapped, so we also need to ensure that force_pte_mapping() returns true, like for DEBUG_PAGEALLOC.
Both functions can be called very early, before POE is actually detected, so the early_enabled() helper is used.
--- arch/arm64/mm/mmu.c | 2 +- arch/arm64/mm/pageattr.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
arch/arm64/mm/mmu.c +1 -1
@@ -781,7 +781,7 @@ static inline bool force_pte_mapping(void)const bool bbml3 =system_capabilities_finalized () ?system_supports_bbml3 () :cpu_supports_bbml3 (); -if (debug_pagealloc_enabled ()) +if (debug_pagealloc_enabled () ||kpkeys_hardened_pgtables_early_enabled ())return true;if (bbml3)return false;
arch/arm64/mm/pageattr.c +2 -1
@@ -101,7 +101,8 @@ bool can_set_direct_map(void)* Realms need to make pages shared/protected at page granularity. */ return rodata_full ||debug_pagealloc_enabled () || -arm64_kfence_can_set_direct_map () ||is_realm_world (); +arm64_kfence_can_set_direct_map () ||is_realm_world () || +kpkeys_hardened_pgtables_early_enabled (); } static int update_range_prot(unsigned long start, unsigned long size,
Signature · 1 line
2.56.0
RFC PATCH v10 18/22 arm64: kpkeys: Guard page table writes
In reply to Kevin BrodskyWhen CONFIG_KPKEYS_HARDENED_PGTABLES is enabled, page tables (both user and kernel) are mapped with a privileged pkey in the linear mapping. As a result, they can only be written in a privileged kpkeys context.
Introduce a kpkeys guard that sets POR_EL1 appropriately to allow writing to page tables, and use this guard wherever necessary. The scope is kept as small as possible, so that POR_EL1 is quickly reset to its default value. Where atomics are involved, the guard's scope encompasses the whole loop to avoid switching POR_EL1 unnecessarily.
This patch is a no-op if CONFIG_KPKEYS_HARDENED_PGTABLES is disabled (default).
--- arch/arm64/include/asm/pgtable.h | 23 ++++++++++++++++++++++- arch/arm64/mm/fault.c | 17 ++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-)
arch/arm64/include/asm/pgtable.h +22 -1
@@ -39,6 +39,14 @@#include <linux/mm_types.h> #include <linux/sched.h> #include <linux/page_table_check.h> +#include <linux/kpkeys.h> + +#ifdef CONFIG_KPKEYS_HARDENED_PGTABLES +KPKEYS_GUARD_COND (kpkeys_hardened_pgtables,KPKEYS_CTX_PGTABLES , + kpkeys_hardened_pgtables_enabled()) +#else +KPKEYS_GUARD_NOOP(kpkeys_hardened_pgtables) +#endif static inline void emit_pte_barriers (void ) {
@@ -360,6 +368,7 @@ static inline pte_t pte_clear_uffd(pte_t pte)static inline void __set_pte_nosync (pte_t *ptep,pte_t pte) { +guard (kpkeys_hardened_pgtables)();WRITE_ONCE (*ptep, pte); }
@@ -789,6 +798,7 @@ static inline void set_pmd(pmd_t *pmdp, pmd_t pmd) }#endif /* __PAGETABLE_PMD_FOLDED */ +guard (kpkeys_hardened_pgtables)();WRITE_ONCE (*pmdp, pmd);if (pmd_valid (pmd))
@@ -850,6 +860,7 @@ static inline void set_pud(pud_t *pudp, pud_t pud)return ; } +guard (kpkeys_hardened_pgtables)();WRITE_ONCE (*pudp, pud);if (pud_valid (pud))
@@ -928,6 +939,7 @@ static inline void set_p4d(p4d_t *p4dp, p4d_t p4d)return ; } +guard (kpkeys_hardened_pgtables)();WRITE_ONCE (*p4dp, p4d);queue_pte_barriers (); }
@@ -1053,6 +1065,7 @@ static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)return ; } +guard (kpkeys_hardened_pgtables)();WRITE_ONCE (*pgdp, pgd);queue_pte_barriers (); }
@@ -1254,6 +1267,7 @@ static inline bool __ptep_test_and_clear_young(struct vm_area_struct *vma, {pte_t old_pte, pte; +guard (kpkeys_hardened_pgtables)(); pte =__ptep_get (ptep); do { old_pte = pte;
@@ -1301,7 +1315,10 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm, pte_t *ptep, unsigned long pgsize) { -pte_t pte =__pte (xchg_relaxed (&pte_val (*ptep),0 )); +pte_t pte; + +scoped_guard (kpkeys_hardened_pgtables) + pte =__pte (xchg_relaxed (&pte_val (*ptep),0 ));switch (pgsize) {case PAGE_SIZE :
@@ -1374,6 +1391,7 @@ static inline void ___ptep_set_wrprotect(struct mm_struct *mm, {pte_t old_pte; +guard (kpkeys_hardened_pgtables)(); do { old_pte = pte; pte = pte_wrprotect(pte);
@@ -1407,6 +1425,7 @@ static inline void __clear_young_dirty_pte(struct vm_area_struct *vma, {pte_t old_pte; +guard (kpkeys_hardened_pgtables)(); do { old_pte = pte;
@@ -1454,6 +1473,7 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp, pmd_t pmd) { page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd); +guard (kpkeys_hardened_pgtables)();return __pmd (xchg_relaxed (&pmd_val (*pmdp),pmd_val (pmd))); }#endif
@@ -1784,6 +1804,7 @@ static inline bool ptep_try_set(pte_t *ptep, pte_t new_pte) {pteval_t old =0 ; +guard (kpkeys_hardened_pgtables)();if (!try_cmpxchg (&pte_val (*ptep), &old,pte_val (new_pte)))return false;
arch/arm64/mm/fault.c +10 -7
@@ -239,13 +239,16 @@ int __ptep_set_access_flags_anysz(struct vm_area_struct *vma,*/ pte_val (entry) ^=PTE_RDONLY ; pteval =pte_val (pte); -do { - old_pteval = pteval; - pteval ^=PTE_RDONLY ; - pteval |=pte_val (entry); - pteval ^=PTE_RDONLY ; - pteval =cmpxchg_relaxed (&pte_val (*ptep), old_pteval, pteval); - }while (pteval != old_pteval); +scoped_guard (kpkeys_hardened_pgtables) { +do { + old_pteval = pteval; + pteval ^=PTE_RDONLY ; + pteval |=pte_val (entry); + pteval ^=PTE_RDONLY ; + pteval =cmpxchg_relaxed (&pte_val (*ptep), old_pteval, + pteval); + }while (pteval != old_pteval); + }/* * Invalidate the local stale read-only entry. Remote stale entries
Signature · 1 line
2.56.0
RFC PATCH v10 19/22 arm64: kpkeys: Batch KPKEYS_CTX_PGTABLES switches
In reply to Kevin BrodskyThe kpkeys_hardened_pgtables feature currently switches kpkeys context in every helper that writes to page tables, such as set_pte(). With kpkeys implemented using POE, this entails a pair of ISBs whenever such helper is called.
A simple way to reduce this overhead is to make use of the lazy MMU mode. We amend the kpkeys_hardened_pgtables guard so that no kpkeys context switch (i.e. POR_EL1 update) is issued while the lazy MMU mode is active. Instead, we switch to KPKEYS_CTX_PGTABLES when entering the lazy MMU mode, and restore the previous context when exiting it.
Restoring the previous kpkeys context requires storing struct kpkeys_state somewhere. POR_EL1 is a full 64-bit value so we cannot simply use a TIF flag. There is no straightforward way to reuse current->thread.por_el1 for that purpose - this is where the current value of POR_EL1 is stored on a context switch, i.e. the value corresponding to KPKEYS_CTX_PGTABLES inside a lazy_mmu section. Instead, we add a new member to thread_struct to hold that state temporarily. This isn't optimal as that member is unused outside of lazy MMU sections, but it is the simplest option. Nesting of sections is not a concern as arch_{enter,leave}_lazy_mmu_mode() are not called in inner lazy MMU sections (nor do we need to do anything there).
A further optimisation this patch makes is to merge the ISBs when exiting lazy_mmu mode. That is, if an ISB is going to be issued by emit_pte_barriers() because kernel pgtables were modified in the lazy MMU section, we skip the ISB after restoring POR_EL1. This is done by checking TIF_LAZY_MMU_PENDING and ensuring that POR_EL1 is restored before emit_pte_barriers() is called.
--- arch/arm64/include/asm/pgtable.h | 50 +++++++++++++++++++++++++++++++++++--- arch/arm64/include/asm/processor.h | 4 +++ 2 files changed, 50 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +46 -4
@@ -43,10 +43,44 @@#ifdef CONFIG_KPKEYS_HARDENED_PGTABLES KPKEYS_GUARD_COND (kpkeys_hardened_pgtables,KPKEYS_CTX_PGTABLES , - kpkeys_hardened_pgtables_enabled()) -#else +kpkeys_hardened_pgtables_enabled () && + !is_lazy_mmu_mode_active()) + +static inline void kpkeys_lazy_mmu_enter (void ) +{ +if (!kpkeys_hardened_pgtables_enabled ()) +return ; + + current->thread.kpkeys_state_lazy_mmu = +kpkeys_enter_context (KPKEYS_CTX_PGTABLES ); +} + +static inline void kpkeys_lazy_mmu_exit (void ) +{ +const struct kpkeys_state *state = + ¤t->thread.kpkeys_state_lazy_mmu; + +if (!kpkeys_hardened_pgtables_enabled ()) +return ; + +/* +* We skip any barrier if TIF_LAZY_MMU_PENDING is set: +* emit_pte_barriers() will issue an ISB just after this function +* returns. +*/ +if (test_thread_flag (TIF_LAZY_MMU_PENDING ) && state->entered_context) +__kpkeys_set_pkey_reg_nosync (state->arch_state.por); +else +kpkeys_leave_context (state); +} +#else /* CONFIG_KPKEYS_HARDENED_PGTABLES */ KPKEYS_GUARD_NOOP (kpkeys_hardened_pgtables) -#endif + +static inline void kpkeys_lazy_mmu_enter(void) {} +static inline void kpkeys_lazy_mmu_exit (void ) {} +#endif /* CONFIG_KPKEYS_HARDENED_PGTABLES */ + +static inline void emit_pte_barriers (void ) {
@@ -79,7 +113,10 @@ static inline void queue_pte_barriers(void) } } -static inline void arch_enter_lazy_mmu_mode (void ) {} +static inline void arch_enter_lazy_mmu_mode (void ) +{ +kpkeys_lazy_mmu_enter (); +}static inline void arch_flush_lazy_mmu_mode (void ) {
@@ -89,6 +126,11 @@ static inline void arch_flush_lazy_mmu_mode(void)static inline void arch_leave_lazy_mmu_mode (void ) { +/* +* The ordering should be preserved to allow kpkeys_lazy_mmu_exit() +* to skip any barrier when TIF_LAZY_MMU_PENDING is set. +*/ +kpkeys_lazy_mmu_exit ();arch_flush_lazy_mmu_mode (); }
arch/arm64/include/asm/processor.h +4 -0
@@ -30,6 +30,7 @@#include <linux/build_bug.h> #include <linux/cache.h> #include <linux/init.h> +#include <linux/kpkeys_types.h> #include <linux/stddef.h> #include <linux/string.h> #include <linux/thread_info.h>
@@ -196,6 +197,9 @@ struct thread_struct {u64 tpidr2_el0;u64 por_el0;u64 por_el1; +#ifdef CONFIG_KPKEYS_HARDENED_PGTABLES +struct kpkeys_state kpkeys_state_lazy_mmu; +#endif #ifdef CONFIG_ARM64_GCS unsigned int gcs_el0_mode;unsigned int gcs_el0_locked;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v10 19/22] arm64: kpkeys: Batch KPKEYS_CTX_PGTABLES switches
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] arm64: kpkeys: POR_EL1 desync with is_lazy_mmu_mode_active() on entry/exit --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] arm64: kpkeys: POR_EL1 desync with is_lazy_mmu_mode_active() on entry/exit
--
commit 1b260fb8242fe345fd5e4bdc00ba9a4272383991
Author: Kevin Brodsky <[email protected]>
arm64: kpkeys: Batch KPKEYS_CTX_PGTABLES switches
This commit batches KPKEYS_CTX_PGTABLES context switches across lazy MMU sections by switching POR_EL1 when entering and leaving lazy MMU mode and skipping per-write guard switches while lazy MMU mode is active.
Quoted 11 lines
arch/arm64/include/asm/pgtable.h +1
-2@@ -43,10 +43,44 @@#ifdef CONFIG_KPKEYS_HARDENED_PGTABLES KPKEYS_GUARD_COND(kpkeys_hardened_pgtables, KPKEYS_CTX_PGTABLES, -kpkeys_hardened_pgtables_enabled ()) -#else + kpkeys_hardened_pgtables_enabled() &&
arch/arm64/include/asm/pgtable.h line 56
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +10
-0+ !is_lazy_mmu_mode_active()) + +static inline void kpkeys_lazy_mmu_enter (void ) +{ +if (!kpkeys_hardened_pgtables_enabled ()) +return ; + + current->thread.kpkeys_state_lazy_mmu = +kpkeys_enter_context (KPKEYS_CTX_PGTABLES ); +}
[Severity: Medium]
Can a synchronous exception between updating current->lazy_mmu_state and switching SYS_POR_EL1 cause a fatal permission fault when writing to page tables?
lazy_mmu_mode_enable() and __task_lazy_mmu_mode_resume() mark current->lazy_mmu_state active before calling arch_enter_lazy_mmu_mode():
include/linux/pgtable.h:lazy_mmu_mode_enable() {
...
if (state->enable_count++ == 0)
arch_enter_lazy_mmu_mode();
}On arm64, synchronous kernel exceptions (such as kprobes or KFENCE page faults) do not set NMI, hardirq, or softirq bits in preempt_count(), so in_interrupt() remains false and is_lazy_mmu_mode_active() returns true:
lazy_mmu_mode_enable()
state->enable_count++ (0 -> 1)
<synchronous exception, e.g. kprobe / KFENCE fault>
...
__set_pte_nosync()
guard(kpkeys_hardened_pgtables)()
is_lazy_mmu_mode_active() == true <-- skips POR_EL1 switch
WRITE_ONCE(*ptep, pte) <-- unhandled EL1 permission fault arch_enter_lazy_mmu_mode()
kpkeys_lazy_mmu_enter()
Because is_lazy_mmu_mode_active() is already true while SYS_POR_EL1 still maps KPKEYS_PKEY_PGTABLES as read-only, both a nested lazy_mmu_mode_enable() and guard(kpkeys_hardened_pgtables)() skip switching SYS_POR_EL1, and the page table write faults.
A symmetric window exists in lazy_mmu_mode_disable() and __task_lazy_mmu_mode_pause(), where enable_count or pause_count is updated before arch_leave_lazy_mmu_mode() restores SYS_POR_EL1. A synchronous exception in that window that enters a nested lazy_mmu_mode_enable() sees SYS_POR_EL1 already set to KPKEYS_CTX_PGTABLES and overwrites kpkeys_state_lazy_mmu.entered_context with false, causing the outer kpkeys_lazy_mmu_exit() to leave SYS_POR_EL1 writable.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
Re: [PATCH RFC v10 19/22] arm64: kpkeys: Batch KPKEYS_CTX_PGTABLES switches
Quoted 32 lines · On 10/11/2026 17:02, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] arm64: kpkeys:POR_EL1desync withis_lazy_mmu_mode_active()on entry/exit
--commit
1b260fb8242fe345fd5e4bdc00ba9a4272383991
Author: Kevin Brodsky <[email protected]>arm64: kpkeys: Batch
KPKEYS_CTX_PGTABLESswitchesThis commit batches
KPKEYS_CTX_PGTABLEScontext switches across lazy MMU sections by switchingPOR_EL1when entering and leaving lazy MMU mode and skipping per-write guard switches while lazy MMU mode is active.Quoted 17 lines
arch/arm64/include/asm/pgtable.h +7
-2@@ -43,10 +43,44 @@#ifdef CONFIG_KPKEYS_HARDENED_PGTABLES KPKEYS_GUARD_COND (kpkeys_hardened_pgtables,KPKEYS_CTX_PGTABLES , -kpkeys_hardened_pgtables_enabled ()) -#else +kpkeys_hardened_pgtables_enabled () && + !is_lazy_mmu_mode_active()) + +static inline void kpkeys_lazy_mmu_enter (void ) +{ +if (!kpkeys_hardened_pgtables_enabled ()) +return ;
Quoted 8 lines
arch/arm64/include/asm/pgtable.h line 56
Quoted 4 lines
arch/arm64/include/asm/pgtable.h +4
-0+ + current->thread.kpkeys_state_lazy_mmu = +kpkeys_enter_context (KPKEYS_CTX_PGTABLES ); +}[Severity: Medium]
Can a synchronous exception between updatingcurrent->lazy_mmu_stateand switchingSYS_POR_EL1cause a fatal permission fault when writing to page tables?
Sashiko already reported this on RFC v9. What I concluded is that this is very unlikely - we shouldn't get a (non-fatal) synchronous exception in that window. The one plausible case is kprobes, and I guess the answer to that one is: don't put a kprobe there :) (We can't easily enforce this though as NOKPROBE_SYMBOL() doesn't work for inlined functions.)
A more serious concern is that since RFC v7, POR_EL1 is no longer reset on exception entry, because that doesn't play well with lazy MMU mode either [1]. If we found a way for lazy MMU mode to be fully disabled while exceptions are handled, then it should address both concerns at the same time.
- Kevin
[1]
https://lore.kernel.org/linux-hardening/[email protected]/
Quoted 36 lines
lazy_mmu_mode_enable()and__task_lazy_mmu_mode_resume()markcurrent->lazy_mmu_stateactive before callingarch_enter_lazy_mmu_mode():include/linux/pgtable.h:lazy_mmu_mode_enable() { ... if (state->enable_count++ == 0) arch_enter_lazy_mmu_mode(); }On arm64, synchronous kernel exceptions (such as kprobes or KFENCE page faults) do not set NMI, hardirq, or softirq bits in
preempt_count(), soin_interrupt()remains false andis_lazy_mmu_mode_active()returns true:
lazy_mmu_mode_enable()
state->enable_count++ (0 -> 1)
<synchronous exception, e.g. kprobe / KFENCE fault>... __set_pte_nosync() guard(kpkeys_hardened_pgtables)() is_lazy_mmu_mode_active() == true <-- skips POR_EL1 switch WRITE_ONCE(*ptep, pte) <-- unhandled EL1 permission fault
arch_enter_lazy_mmu_mode()
kpkeys_lazy_mmu_enter()Because
is_lazy_mmu_mode_active()is already true whileSYS_POR_EL1still mapsKPKEYS_PKEY_PGTABLESas read-only, both a nestedlazy_mmu_mode_enable()and guard(kpkeys_hardened_pgtables)() skip switchingSYS_POR_EL1, and the page table write faults.A symmetric window exists in
lazy_mmu_mode_disable()and__task_lazy_mmu_mode_pause(), whereenable_countorpause_countis updated beforearch_leave_lazy_mmu_mode()restoresSYS_POR_EL1. A synchronous exception in that window that enters a nestedlazy_mmu_mode_enable()seesSYS_POR_EL1already set toKPKEYS_CTX_PGTABLESand overwrites kpkeys_state_lazy_mmu.entered_context with false, causing the outerkpkeys_lazy_mmu_exit()to leaveSYS_POR_EL1writable.
RFC PATCH v10 20/22 arm64: kpkeys: Enable kpkeys_hardened_pgtables support
In reply to Kevin BrodskyAll the pieces are in place to advertise support for kpkeys_hardened_pgtables. Do it by selecting ARCH_HAS_KPKEYS_HARDENED_PGTABLES if ARM64_POE is enabled.
--- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
@@ -2301,6 +2301,7 @@ config ARM64_POE select ARCH_USES_HIGH_VMA_FLAGS select ARCH_HAS_PKEYS select ARCH_HAS_KPKEYS + select ARCH_HAS_KPKEYS_HARDENED_PGTABLES help The Permission Overlay Extension is used to implement Memory Protection Keys. Memory Protection Keys provides a mechanism for
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v10 20/22] arm64: kpkeys: Enable kpkeys_hardened_pgtables support
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] arm64: mm: preserved pkey in trans_pgd_create_copy() faults on kexec/hibernate - [Medium] mm…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] arm64: mm: preserved pkey in trans_pgd_create_copy() faults on kexec/hibernate
- [Medium] mm: kpkeys: redundant KASAN shadow walks in protect_early_kernel_pgtables()
--
commit 974504a027662a7ee9def760c6976edfcfbc634c
Author: Kevin Brodsky <[email protected]>
arm64: kpkeys: Enable kpkeys_hardened_pgtables support
This commit selects ARCH_HAS_KPKEYS_HARDENED_PGTABLES under ARM64_POE to enable kpkeys_hardened_pgtables support on arm64.
arch/arm64/Kconfig config ARM64_POE · line 2304
Quoted 9 lines
arch/arm64/Kconfig +1
-0@@ -2301,6 +2301,7 @@ config ARM64_POE select ARCH_USES_HIGH_VMA_FLAGS select ARCH_HAS_PKEYS select ARCH_HAS_KPKEYS + select ARCH_HAS_KPKEYS_HARDENED_PGTABLES
[Severity: High]
Does enabling CONFIG_KPKEYS_HARDENED_PGTABLES cause hibernation resume and kexec relocation to fault when writing through the transitional linear map?
When kpkeys_hardened_pgtables is active, linear-map PTEs for page table pages carry KPKEYS_PKEY_PGTABLES, which is read-only under POR_EL1_INIT. Both swsusp_arch_resume() and machine_kexec_post_load() call trans_pgd_create_copy() to build a writable copy of the linear map so swsusp_arch_suspend_exit() and arm64_relocate_new_kernel() can overwrite existing kernel memory, including page tables:
arch/arm64/mm/trans_pgd.c:copy_pte() {
...
__set_pte(dst_ptep, pte_mkvalid_k(pte_mkwrite_novma(pte)));
...
}Neither pte_mkwrite_novma() nor pte_mkvalid_k() clears PTE_PO_IDX_MASK, and POR_EL1 remains set to POR_EL1_INIT during the copy. Any store in swsusp_arch_suspend_exit() or arm64_relocate_new_kernel() to a destination page that was a page table when trans_pgd_create_copy() ran will trigger a fatal synchronous EL1 Permission Overlay fault with DAIF masked. Clearing PTE_PO_IDX_MASK (or setting the default pkey) in copy_pte() would avoid preserving the read-only overlay in the transitional page tables.
[Severity: Medium]
Can protect_early_kernel_pgtables() hang or trigger a soft lockup at boot when KASAN or runtime-folded LPA2 is enabled?
On arm64 with CONFIG_KASAN_GENERIC or CONFIG_KASAN_SW_TAGS, kasan_init_shadow() populates multi-terabyte shadow regions before mm_core_init() using the shared kasan_early_shadow_{p4d,pud,pmd,pte} tables. When kpkeys_hardened_pgtables_init() calls protect_early_kernel_pgtables(), walk_kernel_page_table_range(PAGE_OFFSET, ULONG_MAX, ...) does not skip these shared KASAN tables, so protect_pte_table() calls set_direct_map_pkey() and broadcasts a TLB invalidation on the same kasan_early_shadow_pte page for every 2 MiB PMD slot across the shadow range.
In addition, when CONFIG_ARM64_VA_BITS_52 is enabled on hardware that falls back to 48-bit or 47-bit VA at runtime, PAGE_OFFSET is _PAGE_OFFSET(52) rather than _PAGE_OFFSET(vabits_actual). Because pgd_none() returns false when folded and pgd_to_folded_p4d() / p4d_to_folded_pud() aligns down to the start of swapper_pg_dir, walking from PAGE_OFFSET re-walks and flushes the entire swapper_pg_dir 16 times on 4K pages or 32 times on 16K pages.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
Re: [PATCH RFC v10 20/22] arm64: kpkeys: Enable kpkeys_hardened_pgtables support
Quoted 17 lines · On 10/11/2026 17:02, [email protected] wrote:
[Severity: High]
Does enablingCONFIG_KPKEYS_HARDENED_PGTABLEScause hibernation resume and kexec relocation to fault when writing through the transitional linear map?When
kpkeys_hardened_pgtablesis active, linear-map PTEs for page table pages carryKPKEYS_PKEY_PGTABLES, which is read-only underPOR_EL1_INIT. Bothswsusp_arch_resume()andmachine_kexec_post_load()calltrans_pgd_create_copy()to build a writable copy of the linear map soswsusp_arch_suspend_exit()andarm64_relocate_new_kernel()can overwrite existing kernel memory, including page tables:arch/arm64/mm/trans_pgd.c:copy_pte() { ... __set_pte(dst_ptep, pte_mkvalid_k(pte_mkwrite_novma(pte))); ... }
Quoted 7 lines
Neither
pte_mkwrite_novma()norpte_mkvalid_k()clearsPTE_PO_IDX_MASK, andPOR_EL1remains set toPOR_EL1_INITduring the copy. Any store inswsusp_arch_suspend_exit()orarm64_relocate_new_kernel()to a destination page that was a page table whentrans_pgd_create_copy()ran will trigger a fatal synchronous EL1 Permission Overlay fault with DAIF masked. ClearingPTE_PO_IDX_MASK(or setting the default pkey) incopy_pte()would avoid preserving the read-only overlay in the transitional page tables.
Confirmed that issue by getting kexec to reuse the running kernel's physical pages when loading the new image:
$ kexec -l Image -c --mem-min <addr>
where <addr> is the physical address of the running kernel as reported by /proc/iomem. This causes a crash as some of the static page table pages in the kernel's data segment are remapped with POIndex 1 when walking early page tables (patch 13), and the transitional linear map inherits the POIndex, preventing writes to these pages with the default value of POR_EL1.
Agreed that clearing the POIndex in copy_pte() is the right thing to do. We already make all mappings present and writeable; resetting their POIndex is another required step to ensure the mapped memory can be written. Higher level tables should be handled in the same way, in case we protect whole blocks in the future.
- Kevin
RFC PATCH v10 21/22 arm64: mm: Map kernel image alias of init_pg_dir read-only
In reply to Kevin BrodskyPage tables in init_pg_dir are always modified via the linear map. kpkeys_hardened_pgtables now prevents arbitrary code from writing page tables via the linear map; map the kernel image alias of init_pg_dir read-only to ensure that the kpkeys protection cannot be bypassed.
--- arch/arm64/include/asm/sections.h | 1 + arch/arm64/kernel/pi/map_kernel.c | 9 +++++++-- arch/arm64/mm/mmu.c | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-)
arch/arm64/include/asm/sections.h +1 -0
@@ -23,6 +23,7 @@ extern char __irqentry_text_start[], __irqentry_text_end[];extern char __mmuoff_data_start[], __mmuoff_data_end[];extern char __entry_tramp_text_start[], __entry_tramp_text_end[];extern char __relocate_new_kernel_start[], __relocate_new_kernel_end[]; +extern char __pi_init_pg_dir[], __pi_init_pg_end[];static inline size_t entry_tramp_text_size (void ) {
arch/arm64/kernel/pi/map_kernel.c +7 -2
@@ -92,8 +92,13 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level) __inittext_end, prot, false, root_level);map_segment (init_pg_dir, &pgdp, va_offset, __initdata_begin, __initdata_end, data_prot, false, root_level); -map_segment (init_pg_dir, &pgdp, va_offset, _data, _end, data_prot, - true, root_level); +map_segment (init_pg_dir, &pgdp, va_offset, _data, init_pg_dir, + data_prot, true, root_level); +/* Map init_pg_dir separately as we will remap it read-only later on */ +map_segment (init_pg_dir, &pgdp, va_offset, init_pg_dir, init_pg_end, + data_prot, true, root_level); +map_segment (init_pg_dir, &pgdp, va_offset, init_pg_end, _end, + data_prot, true, root_level);dsb (ishst);idmap_cpu_replace_ttbr1 ((phys_addr_t )init_pg_dir);
arch/arm64/mm/mmu.c +8 -0
@@ -1267,6 +1267,14 @@ void mark_rodata_ro(void)update_mapping_prot (__pa_symbol (_text), (unsigned long )_text, (unsigned long )_stext - (unsigned long )_text,PAGE_KERNEL_RO ); +/* +* Map the kernel image mapping of init_pg_dir read-only; it should +* only be written via the linear map. +*/ + section_size = (unsigned long )__pi_init_pg_end - (unsigned long )__pi_init_pg_dir; +update_mapping_prot (__pa_symbol (__pi_init_pg_dir), + (unsigned long )__pi_init_pg_dir, + section_size,PAGE_KERNEL_RO );/* Map the kernel data/bss as invalid in the linear map */ mark_linear_data_alias_valid (false);
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v10 21/22] arm64: mm: Map kernel image alias of init_pg_dir read-only
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] arm64: mm: init_pg_dir split in map_kernel() overflows INIT_DIR_SIZE --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] arm64: mm: init_pg_dir split in map_kernel() overflows INIT_DIR_SIZE
--
commit 3e7fce4327f018b992b775d63738a09b4b86a015
Author: Kevin Brodsky <[email protected]>
arm64: mm: Map kernel image alias of init_pg_dir read-only
This commit splits the early mapping of [init_pg_dir, init_pg_end) from [_data, _end) in map_kernel() and remaps the kernel image alias of init_pg_dir read-only in mark_rodata_ro().
[ ... ]
Quoted 10 lines
arch/arm64/kernel/pi/map_kernel.c +0
-2@@ -92,8 +92,13 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level) __inittext_end, prot, false, root_level);map_segment (init_pg_dir, &pgdp, va_offset, __initdata_begin, __initdata_end, data_prot, false, root_level); -map_segment (init_pg_dir, &pgdp, va_offset, _data, _end, data_prot, - true, root_level);
arch/arm64/kernel/pi/map_kernel.c static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level) · line 104
Quoted 10 lines
arch/arm64/kernel/pi/map_kernel.c +7
-0+map_segment (init_pg_dir, &pgdp, va_offset, _data, init_pg_dir, + data_prot, true, root_level); +/* Map init_pg_dir separately as we will remap it read-only later on */ +map_segment (init_pg_dir, &pgdp, va_offset, init_pg_dir, init_pg_end, + data_prot, true, root_level); +map_segment (init_pg_dir, &pgdp, va_offset, init_pg_end, _end, + data_prot, true, root_level);dsb (ishst);idmap_cpu_replace_ttbr1 ((phys_addr_t )init_pg_dir);
[Severity: High]
Can splitting [_data, _end) at init_pg_dir and init_pg_end cause pgdp to overflow INIT_DIR_SIZE on CONFIG_ARM64_4K_PAGES kernels?
With 4K pages, map_range() uses 2MB PMD block mappings where possible and allocates a PTE table from pgdp whenever a segment boundary falls inside a 2MB block. EARLY_SEGMENT_EXTRA_PAGES only reserves PTE tables for KERNEL_SEGMENT_COUNT plus the [_text, _stext) split added in 4d8e74ad4585 ("arm64: Reserve an extra page for early kernel mapping"):
arch/arm64/include/asm/kernel-pgtable.h:
/*
* KERNEL_SEGMENT_COUNT counts the permanent kernel VMAs. The early mapping
* has one additional split, [_text, _stext). Reserve one more page for the
* SWAPPER_BLOCK_SIZE-unaligned boundaries.
*/
#define EARLY_SEGMENT_EXTRA_PAGES (KERNEL_SEGMENT_COUNT + 2)
When _data lies in an earlier 2MB block and a 2MB boundary falls between init_pg_dir and _end, splitting [init_pg_dir, init_pg_end) out of [_data, _end) forces map_range() to allocate an extra PTE table for the 2MB block containing init_pg_dir. That advances pgdp past __pi_init_pg_end into the adjacent early_init_stack page, corrupting the active early boot stack and placing the new PTE table outside the [__pi_init_pg_dir, __pi_init_pg_end) range that mark_rodata_ro() remaps read-only.
EARLY_SEGMENT_EXTRA_PAGES should be increased to account for the extra split.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
RFC PATCH v10 22/22 mm: Add basic tests for kpkeys_hardened_pgtables
In reply to Kevin BrodskyAdd basic tests for the kpkeys_hardened_pgtables feature: try to perform direct writes to kernel and user page table entries and ensure they fail.
Multiple cases are considered for kernel page tables, as early page tables are allocated and/or protected in a different way.
The tests are builtin (cannot be built as a module) because they refer to multiple symbols that are not exported (e.g. copy_to_kernel_nofault()).
--- mm/Makefile | 1 + mm/tests/kpkeys_hardened_pgtables_kunit.c | 200 ++++++++++++++++++++++++++++++ security/Kconfig.hardening | 12 ++ 3 files changed, 213 insertions(+)
mm/Makefile +1 -0
@@ -150,3 +150,4 @@ 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 +obj-$(CONFIG_KPKEYS_HARDENED_PGTABLES_KUNIT_TEST) += tests/kpkeys_hardened_pgtables_kunit.o
mm/tests/kpkeys_hardened_pgtables_kunit.c new file +200 -0
200 lines not on this page · raw message
security/Kconfig.hardening +12 -0
@@ -283,6 +283,18 @@ config KPKEYS_HARDENED_PGTABLES This option has no effect if the system does not support kernel pkeys. +config KPKEYS_HARDENED_PGTABLES_KUNIT_TEST + bool "KUnit tests for kpkeys_hardened_pgtables" if !KUNIT_ALL_TESTS + depends on KPKEYS_HARDENED_PGTABLES + depends on KUNIT=y + default KUNIT_ALL_TESTS + help + Enable this option to check that the kpkeys_hardened_pgtables feature + functions as intended, i.e. prevents arbitrary writes to user and + kernel page tables. + + If unsure, say N. + endmenu config CC_HAS_RANDSTRUCT
Signature · 1 line
2.56.0
Re: [PATCH RFC v10 22/22] mm: Add basic tests for kpkeys_hardened_pgtables
security/Kconfig.hardening config KPKEYS_HARDENED_PGTABLES · line 290
Quoted 11 lines · Kevin Brodsky
--- mm/Makefile | 1 + mm/tests/kpkeys_hardened_pgtables_kunit.c | 200 ++++++++++++++++++++++++++++++ security/Kconfig.hardening | 12 ++ 3 files changed, 213 insertions(+)security/Kconfig.hardening +5
-0+config KPKEYS_HARDENED_PGTABLES_KUNIT_TEST + bool "KUnit tests for kpkeys_hardened_pgtables" if !KUNIT_ALL_TESTS + depends on KPKEYS_HARDENED_PGTABLES + depends on KUNIT=y + default KUNIT_ALL_TESTS
I'd also expect to see this added to one of the KUnit config fragments somewhere so it gets run as standard, though that's a little awkward given that KPKEYS_HARDENED_PGTABLES isn't universally available.
Re: [PATCH RFC v10 22/22] mm: Add basic tests for kpkeys_hardened_pgtables
Quoted 15 lines · Mark Brown
security/Kconfig.hardening config KPKEYS_HARDENED_PGTABLES · line 290
Quoted 10 lines · Kevin Brodsky
--- mm/Makefile | 1 + mm/tests/kpkeys_hardened_pgtables_kunit.c | 200 ++++++++++++++++++++++++++++++ security/Kconfig.hardening | 12 ++ 3 files changed, 213 insertions(+)security/Kconfig.hardening +5
-0+config KPKEYS_HARDENED_PGTABLES_KUNIT_TEST + bool "KUnit tests for kpkeys_hardened_pgtables" if !KUNIT_ALL_TESTS + depends on KPKEYS_HARDENED_PGTABLES + depends on KUNIT=y + default KUNIT_ALL_TESTSI'd also expect to see this added to one of the KUnit config fragments somewhere so it gets run as standard, though that's a little awkward given that
KPKEYS_HARDENED_PGTABLESisn't universally available.
Do you mean adding the dependency KPKEYS_HARDENED_PGTABLES=y to tools/testing/kunit/configs/all_tests.config? Indeed that would only be meaningful on arm64 (for now). That said it wouldn't hurt other architectures, so happy to add it there if you think that's worth it.
- Kevin
Re: [PATCH RFC v10 22/22] mm: Add basic tests for kpkeys_hardened_pgtables
Quoted 4 lines · Kevin Brodsky
Do you mean adding the dependency KPKEYS_HARDENED_PGTABLES=y to
tools/testing/kunit/configs/all_tests.config? Indeed that would only be meaningful on arm64 (for now). That said it wouldn't hurt other architectures, so happy to add it there if you think that's worth it.
Yes, or something like that - unfortunately KUnit checks to see if the config options appear in the configuration so something that vanishes on other architectures causes issues. Perhaps the default for the config option ought to depend on KUNIT_ALL_TESTS?
Re: [PATCH RFC v10 22/22] mm: Add basic tests for kpkeys_hardened_pgtables
Quoted 9 lines · Mark Brown
Quoted 4 lines · Kevin Brodsky
Do you mean adding the dependency KPKEYS_HARDENED_PGTABLES=y to
tools/testing/kunit/configs/all_tests.config? Indeed that would only be meaningful on arm64 (for now). That said it wouldn't hurt other architectures, so happy to add it there if you think that's worth it.Yes, or something like that - unfortunately KUnit checks to see if the config options appear in the configuration so something that vanishes on other architectures causes issues.
Alright, I guess there's not much we can do then, short of creating arch-specific fragments.
Quoted 2 lines
Perhaps the default for the config
option ought to depend onKUNIT_ALL_TESTS?
Not sure I'm following, the config already has default KUNIT_ALL_TESTS?
- Kevin
Re: [PATCH RFC v10 22/22] mm: Add basic tests for kpkeys_hardened_pgtables
Quoted 11 lines · Kevin Brodsky
Quoted 7 lines · Mark Brown
Quoted 1 line · Kevin Brodsky
Do you mean adding the dependency KPKEYS_HARDENED_PGTABLES=y to
Yes, or something like that - unfortunately KUnit checks to see if the config options appear in the configuration so something that vanishes on other architectures causes issues.
Alright, I guess there's not much we can do then, short of creating arch-specific fragments.
Or my suggestion below, or come up with some generic option that could always be there and would in turn select hardening things. There's probably a case for having something given that the hardening features will tend to catch some kinds of test error but that feels more config fragment like. Another idea would be to teach KUnit about the idea that it should try turning on some config options but not worry if they're not selected.
Quoted 4 lines
Quoted 2 lines
Perhaps the default for the config
option ought to depend onKUNIT_ALL_TESTS?Not sure I'm following, the config already has default
KUNIT_ALL_TESTS?
The config for the test does, but KPKEYS_HARDENED_PGTABLES which the test depends on does not so unless the underlying defconfig turns that on the test won't be enabled. I was suggesting also adding a default for testing.
Re: [PATCH RFC v10 22/22] mm: Add basic tests for kpkeys_hardened_pgtables
Quoted 13 lines · Mark Brown
Quoted 5 lines
Quoted 3 lines
Yes, or something like that - unfortunately KUnit checks to see if the config options appear in the configuration so something that vanishes on other architectures causes issues.
Alright, I guess there's not much we can do then, short of creating arch-specific fragments.
Or my suggestion below, or come up with some generic option that could always be there and would in turn select hardening things. There's probably a case for having something given that the hardening features will tend to catch some kinds of test error but that feels more config fragment like. Another idea would be to teach KUnit about the idea that it should try turning on some config options but not worry if they're not selected.
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines
Perhaps the default for the config
option ought to depend onKUNIT_ALL_TESTS?Not sure I'm following, the config already has default
KUNIT_ALL_TESTS?The config for the test does, but
KPKEYS_HARDENED_PGTABLESwhich the test depends on does not so unless the underlying defconfig turns that on the test won't be enabled. I was suggesting also adding a default for testing.
Ah got it. It doesn't look like there's a precedent for KUNIT_ALL_TESTS having that kind of side effect though.
A separate fragment for hardening options where KUnit ignores options not getting selected seems like a reasonable approach.
- Kevin
[email protected]
AI reviewRe: [PATCH RFC v10 22/22] mm: Add basic tests for kpkeys_hardened_pgtables
In reply to Kevin BrodskyThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm: missing folded PUD and PMD checks in kpkeys_hardened_pgtables KUnit tests --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm: missing folded PUD and PMD checks in kpkeys_hardened_pgtables KUnit tests
--
commit 2fe47b9a3fc12c652abdd8342ffe143ffdaeb477
Author: Kevin Brodsky <[email protected]>
mm: Add basic tests for kpkeys_hardened_pgtables
This commit adds built-in KUnit tests for kpkeys_hardened_pgtables that verify direct writes to kernel and user page table entries fail with -EFAULT.
Quoted 5 lines
mm/tests/kpkeys_hardened_pgtables_kunit.c new file +0
-0
[ ... ]
Quoted 23 lines
mm/tests/kpkeys_hardened_pgtables_kunit.c new file +23
-0+ p4dp =p4d_offset (pgdp, addr); +KUNIT_ASSERT_NOT_NULL_MSG (test, p4dp,"Failed to get P4D" ); +if (!mm_p4d_folded (&init_mm)) +write_pgtable (p4d, p4dp); + + pudp =pud_offset (p4dp, addr); +KUNIT_ASSERT_NOT_NULL_MSG (test, pudp,"Failed to get PUD" ); +if (!mm_pud_folded (&init_mm)) +write_pgtable (pud, pudp); + + pmdp =pmd_offset (pudp, addr); +KUNIT_ASSERT_NOT_NULL_MSG (test, pmdp,"Failed to get PMD" ); +write_pgtable (pmd, pmdp); + + ptep =pte_offset_kernel (pmdp, addr); +KUNIT_ASSERT_NOT_NULL_MSG (test, ptep,"Failed to get PTE" ); +write_pgtable (pte, ptep); +} + +/* Worth checking since the kernel image is mapped with static page tables */ +static void write_kernel_image_pud (struct kunit *test) +{ +pud_t *pudp;
mm/tests/kpkeys_hardened_pgtables_kunit.c line 120
Quoted 10 lines
mm/tests/kpkeys_hardened_pgtables_kunit.c new file +10
-0+ +if (!kpkeys_supported ()) +kunit_skip (test,"kpkeys are not supported" ); + +/* The kernel is probably block-mapped, check the PUD to be safe */ + pudp =pud_off_k ((unsigned long )&init_mm); +KUNIT_ASSERT_NOT_NULL_MSG (test, pudp,"Failed to get PUD" ); + +write_pgtable (pud, pudp); +}
[Severity: Medium]
Will write_kernel_image_pud() fail on configurations where the PUD level is folded (such as 3-level page tables on arm64, or 16K pages with 52-bit VA without FEAT_LPA2)?
When mm_pud_folded(&init_mm) is true, pud_off_k() resolves to an entry in swapper_pg_dir, which is not protected by kpkeys_hardened_pgtables and is still writable when built-in KUnit tests run before mark_rodata_ro(), so copy_to_kernel_nofault() succeeds instead of returning -EFAULT.
Similarly, on 2-level page table configurations where mm_pmd_folded() is true, the unguarded PMD writes in write_direct_map_pgtables() and write_vmemmap_pmd() resolve into swapper_pg_dir, and pmd_off() in write_user_pmd() resolves into current->mm->pgd (which is slab-allocated from pgd_cache rather than kpkeys_pgtable_alloc() when PGD_SIZE != PAGE_SIZE).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22