arm64: Unmap linear alias of kernel data/bss
Subsystems (7)
v7 cover arm64: Unmap linear alias of kernel data/bss
From: Ard Biesheuvel <[email protected]>
One of the reasons the lack of randomization of the linear map on arm64 is considered problematic is the fact that bootloaders adhering to the original arm64 boot protocol (i.e., a substantial fraction of all Android phones) may place the kernel at the base of DRAM, and therefore at the base of the non-randomized linear map. This puts a writable alias of the kernel's data and bss regions at a predictable location, removing the need for an attacker to guess where KASLR mapped the kernel.
Let's unmap this linear, writable alias entirely, so that knowing the location of the linear alias does not give write access to the kernel's data and bss regions.
Changes since v6:
- Improve commits logs and comments
- Add acks from Kevin
- Reorder patches so remapping data/bss R/O occurs after moving the zero page into .rodata
- Drop zero page cache flush from SuperH rather than casting away the constness
- Map kfence pool with NO_EXEC_MAPPINGS
Note that Sashiko had some comments on patch 15/15 [1] but none of those seem accurate. (I have tested both suspend/resume and hibernate under QEMU and both work as expected)
Changes since v5:
- Reorder series in ascending order of impact, so that the first few can be merged earlier if desired. This also makes the patch that remaps the data/bss linear alias as tagged redundant, which is therefore dropped.
- Add patch #3 to address an existing issue spotted by Sashiko
- Fix thinko in contiguous region check (#5), where the whole region needs to be considered and not only the first entry (dropped Rb as well) - this addresses the kfence issue Sashiko reported on v5 [0]
- Update commit log on #6 to clarify that changing permission bits on PTE_CONT entries is safe as long as PTE_CONT itself does not change
- Likewise, drop hunk that adds the PTE_CONT bit to the 'permitted' mask in pgattr_change_is_safe(), as changing it is not safe. (#8)
- Move kasan's additional page table to pgdir BSS as well
- Use (NOLOAD) on the .pgdir.bss section so it does not get emitted into vmlinux
- Add powerpc and SuperH patches to deal with empty_zero_page[] being made const
Changes since v4:
- Update the correct [early] mapping in patch #1
- Make empty_zero_page[] const instead of __ro_after_init
- Drop patches that remap the fixmap page tables r/o for now
- Don't force page mappings for the data/bss linear alias, as it is no longer needed for set_memory_valid()
- Add acks
Changes since v3:
- Drop bogus patch adding hierarchical PXN to the fixmap mapping, which breaks the KPTI trampoline (thanks to Sashiko)
- Add generic patch to move the empty_zero_page to __ro_after_init, as it now lives in generic code.
- Add patches to remap the linear aliases of the fixmap page tables read-only too - these live at an a priori known offset in the linear map if physical KASLR was omitted, and control a priori known addresses in the virtual kernel space.
- Rebase onto v7.1-rc1
Changes since v2:
- Keep bm_pte[] in the region that is remapped r/o or unmapped, as it is only manipulated via its kernel alias
- Drop check that prohibits any manipulation of descriptors with the CONT bit set
- Add Ryan's ack to a couple of patches
- Rebase onto v7.0-rc4
Changes since v1:
- Put zero page patch at the start of the series
- Tweak __map_memblock() API to respect existing table and contiguous mappings, so that the logic to map the kernel alias can be simplified
- Stop abusing the MEMBLOCK_NOMAP flag to initially omit the kernel linear alias from the linear map
- Some additional cleanup patches
- Use proper API [set_memory_valid()] to (un)map the linear alias of data/bss.
[0] https://sashiko.dev/#/patchset/20260519151616.2557018-15-ardb%2Bgit%40google.com
[1] https://sashiko.dev/#/patchset/20260526175846.2694125-17-ardb%2Bgit%40google.com
Ard Biesheuvel (15):
arm64: mm: Remove bogus stop condition from map_mem() loop
arm64: mm: Drop redundant pgd_t* argument from map_mem()
arm64: mm: Check for pud_/pmd_set_huge() failures on kernel mappings
arm64: mm: Preserve existing table mappings when mapping DRAM
arm64: mm: Preserve non-contiguous descriptors when mapping DRAM
arm64: mm: Permit contiguous descriptors to be manipulated
arm64: kfence: Avoid NOMAP tricks when mapping the early pool
arm64: mm: Permit contiguous attribute for preliminary mappings
arm64: Move fixmap and kasan page tables to end of kernel image
arm64: mm: Don't abuse memblock NOMAP to check for overlaps powerpc/code-patching: Avoid r/w mapping of the zero page
sh: Drop cache flush of the zero page at boot
mm: Make empty_zero_page[] const
arm64: mm: Map the kernel data/bss read-only in the linear map
arm64: mm: Unmap kernel data/bss entirely from the linear map
arch/arm64/include/asm/mmu.h | 2 + arch/arm64/include/asm/pgtable.h | 4 + arch/arm64/kernel/vmlinux.lds.S | 8 +- arch/arm64/mm/fixmap.c | 6 +- arch/arm64/mm/kasan_init.c | 2 +- arch/arm64/mm/mmu.c | 164 ++++++++++++-------- arch/powerpc/lib/code-patching.c | 52 +------ arch/sh/mm/init.c | 3 - include/linux/pgtable.h | 2 +- mm/mm_init.c | 2 +- 10 files changed, 121 insertions(+), 124 deletions(-)
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 1/15 arm64: mm: Remove bogus stop condition from map_mem() loop
From: Ard Biesheuvel <[email protected]>
The memblock API guarantees that start is not greater than or equal to end, so there is no need to test it. And if it were, it is doubtful that breaking out of the loop would be a reasonable course of action here (rather than attempting to map the remaining regions)
So let's drop this check.
--- arch/arm64/mm/mmu.c | 2 -- 1 file changed, 2 deletions(-)
arch/arm64/mm/mmu.c +0 -2
@@ -1173,8 +1173,6 @@ static void __init map_mem(pgd_t *pgdp)/* map all the memory banks */ for_each_mem_range (i, &start, &end) { -if (start >= end) -break ;/* * The linear map must allow allocation tags reading/writing * if MTE is present. Otherwise, it has the same attributes as
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 2/15 arm64: mm: Drop redundant pgd_t* argument from map_mem()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
__map_memblock() and map_mem() always operate on swapper_pg_dir, so there is no need to pass around a pgd_t pointer between them.
--- arch/arm64/mm/mmu.c | 25 ++++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-)
arch/arm64/mm/mmu.c +12 -13
@@ -1035,11 +1035,11 @@ static void update_mapping_prot(phys_addr_t phys, unsigned long virt,flush_tlb_kernel_range (virt, virt + size); } -static void __init __map_memblock (pgd_t *pgdp,phys_addr_t start, -phys_addr_t end,pgprot_t prot,int flags) +static void __init __map_memblock (phys_addr_t start,phys_addr_t end, +pgprot_t prot,int flags) { -early_create_pgd_mapping (pgdp, start,__phys_to_virt (start), end - start, - prot, early_pgtable_alloc, flags); +early_create_pgd_mapping (swapper_pg_dir, start,__phys_to_virt (start), + end - start, prot, early_pgtable_alloc, flags); }void __init mark_linear_text_alias_ro (void )
@@ -1087,13 +1087,13 @@ static phys_addr_t __init arm64_kfence_alloc_pool(void)return kfence_pool; } -static void __init arm64_kfence_map_pool (phys_addr_t kfence_pool,pgd_t *pgdp) +static void __init arm64_kfence_map_pool (phys_addr_t kfence_pool) {if (!kfence_pool)return ;/* KFENCE pool needs page-level mapping. */ -__map_memblock (pgdp, kfence_pool, kfence_pool +KFENCE_POOL_SIZE , +__map_memblock (kfence_pool, kfence_pool +KFENCE_POOL_SIZE ,pgprot_tagged (PAGE_KERNEL ),NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS );memblock_clear_nomap (kfence_pool,KFENCE_POOL_SIZE );
@@ -1129,11 +1129,11 @@ bool arch_kfence_init_pool(void)#else /* CONFIG_KFENCE */ static inline phys_addr_t arm64_kfence_alloc_pool (void ) {return 0 ; } -static inline void arm64_kfence_map_pool (phys_addr_t kfence_pool,pgd_t *pgdp) { } +static inline void arm64_kfence_map_pool (phys_addr_t kfence_pool) { }#endif /* CONFIG_KFENCE */ -static void __init map_mem (pgd_t *pgdp) +static void __init map_mem (void ) {static const u64 direct_map_end =_PAGE_END (VA_BITS_MIN );phys_addr_t kernel_start =__pa_symbol (_text);
@@ -1178,7 +1178,7 @@ static void __init map_mem(pgd_t *pgdp)* if MTE is present. Otherwise, it has the same attributes as * PAGE_KERNEL. */ -__map_memblock (pgdp, start, end,pgprot_tagged (PAGE_KERNEL ), +__map_memblock (start, end,pgprot_tagged (PAGE_KERNEL ), flags); }
@@ -1192,10 +1192,9 @@ static void __init map_mem(pgd_t *pgdp)* Note that contiguous mappings cannot be remapped in this way, * so we should avoid them here. */ -__map_memblock (pgdp, kernel_start, kernel_end, -PAGE_KERNEL ,NO_CONT_MAPPINGS ); +__map_memblock (kernel_start, kernel_end,PAGE_KERNEL ,NO_CONT_MAPPINGS );memblock_clear_nomap (kernel_start, kernel_end - kernel_start); -arm64_kfence_map_pool (early_kfence_pool, pgdp); +arm64_kfence_map_pool (early_kfence_pool); }void mark_rodata_ro (void )
@@ -1417,7 +1416,7 @@ static void __init create_idmap(void)void __init paging_init (void ) { -map_mem (swapper_pg_dir); +map_mem ();memblock_allow_resize ();
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 3/15 arm64: mm: Check for pud_/pmd_set_huge() failures on kernel mappings
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Sashiko reports:
| If pmd_set_huge() rejects an unsafe page table transition (such as | mapping a different physical address over an existing block mapping), | it returns 0 and leaves the page table entry unmodified. | | Because *pmdp remains unmodified, READ_ONCE(pmd_val(*pmdp)) will equal | pmd_val(old_pmd). The transition from old_pmd to old_pmd is evaluated | as safe by pgattr_change_is_safe(), so the BUG_ON never triggers. | | This allows invalid and unsafe mapping updates to be silently dropped | instead of panicking, leaving stale memory mappings active while the | caller assumes the update was successful.
The same applies to pud_set_huge() in alloc_init_pud().
Given how it is generally preferred to limp on rather than blow up the system if an unexpected condition such as this one occurs, and the fact that there are no known cases where this disparity results in real problems, let's WARN on these failures rather than BUG, allowing the system to survive to the point where it can actually report them.
--- arch/arm64/mm/mmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/mm/mmu.c +2 -2
@@ -257,7 +257,7 @@ static int init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,/* try section mapping first */ if (((addr | next | phys) & ~PMD_MASK ) ==0 && (flags &NO_BLOCK_MAPPINGS ) ==0 ) { -pmd_set_huge (pmdp, phys, prot); +WARN_ON (!pmd_set_huge (pmdp, phys, prot));/* * After the PMD entry has been populated once, we
@@ -380,7 +380,7 @@ static int alloc_init_pud(p4d_t *p4dp, unsigned long addr, unsigned long end,if (pud_sect_supported () && ((addr | next | phys) & ~PUD_MASK ) ==0 && (flags &NO_BLOCK_MAPPINGS ) ==0 ) { -pud_set_huge (pudp, phys, prot); +WARN_ON (!pud_set_huge (pudp, phys, prot));/* * After the PUD entry has been populated once, we
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 4/15 arm64: mm: Preserve existing table mappings when mapping DRAM
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Instead of blindly overwriting an existing table entry when mapping DRAM regions, take care not to replace a pre-existing table entry with a block entry. This permits the logic of mapping the kernel's linear alias to be simplified in a subsequent patch.
--- arch/arm64/mm/mmu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/mm/mmu.c +4 -2
@@ -256,7 +256,8 @@ static int init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,/* try section mapping first */ if (((addr | next | phys) & ~PMD_MASK ) ==0 && - (flags &NO_BLOCK_MAPPINGS ) ==0 ) { + (flags &NO_BLOCK_MAPPINGS ) ==0 && + !pmd_table (old_pmd)) {WARN_ON (!pmd_set_huge (pmdp, phys, prot));/*
@@ -379,7 +380,8 @@ static int alloc_init_pud(p4d_t *p4dp, unsigned long addr, unsigned long end,*/ if (pud_sect_supported () && ((addr | next | phys) & ~PUD_MASK ) ==0 && - (flags &NO_BLOCK_MAPPINGS ) ==0 ) { + (flags &NO_BLOCK_MAPPINGS ) ==0 && + !pud_table (old_pud)) {WARN_ON (!pud_set_huge (pudp, phys, prot));/*
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 5/15 arm64: mm: Preserve non-contiguous descriptors when mapping DRAM
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Instead of blindly overwriting existing live entries regardless of the value of their contiguous bit when mapping DRAM regions at contiguous-hint granularity, check whether the contiguous region in question contains any valid descriptors that have the contiguous bit cleared, and in that case, leave the contiguous bit unset on the entire region. This permits the logic of mapping the kernel's linear alias to be simplified in a subsequent patch.
Note that this can only result in a misprogrammed contiguous bit (as per ARM ARM RNGLXZ) if the region in question already contains a mix of valid contiguous and valid non-contiguous descriptors, in which case it was already misprogrammed to begin with.
--- arch/arm64/include/asm/pgtable.h | 4 ++++ arch/arm64/mm/mmu.c | 22 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-)
arch/arm64/include/asm/pgtable.h +4 -0
@@ -181,6 +181,10 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)* Returns true if the pte is valid and has the contiguous bit set. */ #define pte_valid_cont (pte) (pte_valid(pte) && pte_cont(pte)) +/* +* Returns true if the pte is valid and has the contiguous bit cleared. +*/ +#define pte_valid_noncont (pte) (pte_valid(pte) && !pte_cont(pte))/* * Could the pte be present in the TLB? We must check mm_tlb_flush_pending * so that we don't erroneously return false for pages that have been
arch/arm64/mm/mmu.c +20 -2
@@ -187,6 +187,14 @@ static void init_pte(pte_t *ptep, unsigned long addr, unsigned long end, }while (ptep++, addr +=PAGE_SIZE , addr != end); } +static bool pte_range_has_valid_noncont (pte_t *ptep) +{ +for (int i =0 ; i <CONT_PTES ; i++) +if (pte_valid_noncont (__ptep_get (&ptep[i]))) +return true; +return false; +} + static int alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, phys_addr_t phys, pgprot_t prot,
@@ -224,7 +232,8 @@ static int alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,/* use a contiguous mapping if the range is suitably aligned */ if ((((addr | next | phys) & ~CONT_PTE_MASK ) ==0 ) && - (flags &NO_CONT_MAPPINGS ) ==0 ) + (flags &NO_CONT_MAPPINGS ) ==0 && + !pte_range_has_valid_noncont (ptep)) __prot =__pgprot (pgprot_val (prot) |PTE_CONT );init_pte (ptep, addr, next, phys, __prot);
@@ -283,6 +292,14 @@ static int init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,return 0 ; } +static bool pmd_range_has_valid_noncont (pmd_t *pmdp) +{ +for (int i =0 ; i <CONT_PMDS ; i++) +if (pte_valid_noncont (pmd_pte (READ_ONCE (pmdp[i])))) +return true; +return false; +} + static int alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, unsigned long end, phys_addr_t phys, pgprot_t prot,
@@ -324,7 +341,8 @@ static int alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,/* use a contiguous mapping if the range is suitably aligned */ if ((((addr | next | phys) & ~CONT_PMD_MASK ) ==0 ) && - (flags &NO_CONT_MAPPINGS ) ==0 ) + (flags &NO_CONT_MAPPINGS ) ==0 && + !pmd_range_has_valid_noncont (pmdp)) __prot =__pgprot (pgprot_val (prot) |PTE_CONT ); ret =init_pmd (pmdp, addr, next, phys, __prot, pgtable_alloc, flags);
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 6/15 arm64: mm: Permit contiguous descriptors to be manipulated
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Currently, pgattr_change_is_safe() is overly pedantic when it comes to descriptors with the contiguous hint attribute set, as it rejects assignments even if the old and the new value are the same.
In fact, as per ARM ARM RJQQTC, manipulating descriptors with the contiguous bit set is safe as long as the bit itself does not change value, in the sense that no TLB conflict aborts or other exceptions may be raised as a result. Inconsistent permission attributes within the contiguous region may result in any of the alternatives to be taken to apply to the entire region, which might be a programming error, but it does not constitute an unsafe manipulation in terms of what pgattr_change_is_safe() is intended to detect.
So drop the special PTE_CONT check, but still omit PTE_CONT from 'mask' so that modifying the bit is still regarded as unsafe.
--- arch/arm64/mm/mmu.c | 4 ---- 1 file changed, 4 deletions(-)
arch/arm64/mm/mmu.c +0 -4
@@ -134,10 +134,6 @@ bool pgattr_change_is_safe(pteval_t old, pteval_t new)if (pte_pfn (__pte (old)) !=pte_pfn (__pte (new)))return false; -/* live contiguous mappings may not be manipulated at all */ -if ((old | new) &PTE_CONT ) -return false; -/* Transitioning from Non-Global to Global is unsafe */ if (old & ~new &PTE_NG )return false;
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 7/15 arm64: kfence: Avoid NOMAP tricks when mapping the early pool
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Now that the map_mem() routines respect existing page mappings and contiguous granule sized blocks with the contiguous bit cleared, there is no longer a reason to play tricks with the memblock NOMAP attribute.
Instead, the kfence pool can be allocated and mapped with page granularity first, and this granularity will be respected when the rest of DRAM is mapped later, even if block and contiguous mappings are allowed for the remainder of those mappings.
Add the NO_EXEC_MAPPINGS flag to ensure that hierarchical XN attributes are set on the intermediate page tables that are allocated when mapping the pool.
--- arch/arm64/mm/mmu.c | 27 +++++--------------- 1 file changed, 6 insertions(+), 21 deletions(-)
arch/arm64/mm/mmu.c +6 -21
@@ -1083,36 +1083,24 @@ static int __init parse_kfence_early_init(char *arg) }early_param ("kfence.sample_interval" , parse_kfence_early_init); -static phys_addr_t __init arm64_kfence_alloc_pool (void ) +static void __init arm64_kfence_map_pool (void ) {phys_addr_t kfence_pool;if (!kfence_early_init) -return 0 ; +return ; kfence_pool =memblock_phys_alloc (KFENCE_POOL_SIZE ,PAGE_SIZE );if (!kfence_pool) {pr_err ("failed to allocate kfence pool\n" ); kfence_early_init = false; -return 0 ; - } - -/* Temporarily mark as NOMAP. */ -memblock_mark_nomap (kfence_pool,KFENCE_POOL_SIZE ); - -return kfence_pool; -} - -static void __init arm64_kfence_map_pool (phys_addr_t kfence_pool) -{ -if (!kfence_pool)return ; + }/* KFENCE pool needs page-level mapping. */ __map_memblock (kfence_pool, kfence_pool +KFENCE_POOL_SIZE ,pgprot_tagged (PAGE_KERNEL ), -NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS ); -memblock_clear_nomap (kfence_pool,KFENCE_POOL_SIZE ); +NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS |NO_EXEC_MAPPINGS ); __kfence_pool =phys_to_virt (kfence_pool); }
@@ -1144,8 +1132,7 @@ bool arch_kfence_init_pool(void) }#else /* CONFIG_KFENCE */ -static inline phys_addr_t arm64_kfence_alloc_pool (void ) {return 0 ; } -static inline void arm64_kfence_map_pool (phys_addr_t kfence_pool) { } +static inline void arm64_kfence_map_pool (void ) { }#endif /* CONFIG_KFENCE */
@@ -1155,7 +1142,6 @@ static void __init map_mem(void)phys_addr_t kernel_start =__pa_symbol (_text);phys_addr_t kernel_end =__pa_symbol (__init_begin);phys_addr_t start, end; -phys_addr_t early_kfence_pool;int flags =NO_EXEC_MAPPINGS ;u64 i;
@@ -1172,7 +1158,7 @@ static void __init map_mem(void)BUILD_BUG_ON (pgd_index (direct_map_end -1 ) ==pgd_index (direct_map_end) &&pgd_index (_PAGE_OFFSET (VA_BITS_MIN )) !=PTRS_PER_PGD -1 ); - early_kfence_pool =arm64_kfence_alloc_pool (); +arm64_kfence_map_pool (); linear_map_requires_bbml2 = !force_pte_mapping () &&can_set_direct_map ();
@@ -1210,7 +1196,6 @@ static void __init map_mem(void)*/ __map_memblock (kernel_start, kernel_end,PAGE_KERNEL ,NO_CONT_MAPPINGS );memblock_clear_nomap (kernel_start, kernel_end - kernel_start); -arm64_kfence_map_pool (early_kfence_pool); }void mark_rodata_ro (void )
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Re: [PATCH v7 07/15] arm64: kfence: Avoid NOMAP tricks when mapping the early pool
Quoted 11 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Now that the
map_mem()routines respect existing page mappings and contiguous granule sized blocks with the contiguous bit cleared, there is no longer a reason to play tricks with the memblock NOMAP attribute.Instead, the kfence pool can be allocated and mapped with page granularity first, and this granularity will be respected when the rest of DRAM is mapped later, even if block and contiguous mappings are allowed for the remainder of those mappings.
Quoted 5 lines
Add the
NO_EXEC_MAPPINGSflag to ensure that hierarchical XN attributes are set on the intermediate page tables that are allocated when mapping the pool.Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 84 lines
--- arch/arm64/mm/mmu.c | 27 +++++--------------- 1 file changed, 6 insertions(+), 21 deletions(-)arch/arm64/mm/mmu.c +6
-21@@ -1083,36 +1083,24 @@ static int __init parse_kfence_early_init(char *arg) }early_param ("kfence.sample_interval" , parse_kfence_early_init); -static phys_addr_t __init arm64_kfence_alloc_pool (void ) +static void __init arm64_kfence_map_pool (void ) {phys_addr_t kfence_pool;if (!kfence_early_init) -return 0 ; +return ; kfence_pool =memblock_phys_alloc (KFENCE_POOL_SIZE ,PAGE_SIZE );if (!kfence_pool) {pr_err ("failed to allocate kfence pool\n" ); kfence_early_init = false; -return 0 ; - } - -/* Temporarily mark as NOMAP. */ -memblock_mark_nomap (kfence_pool,KFENCE_POOL_SIZE ); - -return kfence_pool; -} - -static void __init arm64_kfence_map_pool (phys_addr_t kfence_pool) -{ -if (!kfence_pool)return ; + }/* KFENCE pool needs page-level mapping. */ __map_memblock (kfence_pool, kfence_pool +KFENCE_POOL_SIZE ,pgprot_tagged (PAGE_KERNEL ), -NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS ); -memblock_clear_nomap (kfence_pool,KFENCE_POOL_SIZE ); +NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS |NO_EXEC_MAPPINGS ); __kfence_pool =phys_to_virt (kfence_pool); }@@ -1144,8 +1132,7 @@ bool arch_kfence_init_pool(void) }#else /* CONFIG_KFENCE */ -static inline phys_addr_t arm64_kfence_alloc_pool (void ) {return 0 ; } -static inline void arm64_kfence_map_pool (phys_addr_t kfence_pool) { } +static inline void arm64_kfence_map_pool (void ) { }#endif /* CONFIG_KFENCE */ @@ -1155,7 +1142,6 @@ static void __init map_mem(void)phys_addr_t kernel_start =__pa_symbol (_text);phys_addr_t kernel_end =__pa_symbol (__init_begin);phys_addr_t start, end; -phys_addr_t early_kfence_pool;int flags =NO_EXEC_MAPPINGS ;u64 i;@@ -1172,7 +1158,7 @@ static void __init map_mem(void)BUILD_BUG_ON (pgd_index (direct_map_end -1 ) ==pgd_index (direct_map_end) &&pgd_index (_PAGE_OFFSET (VA_BITS_MIN )) !=PTRS_PER_PGD -1 ); - early_kfence_pool =arm64_kfence_alloc_pool (); +arm64_kfence_map_pool (); linear_map_requires_bbml2 = !force_pte_mapping () &&can_set_direct_map ();@@ -1210,7 +1196,6 @@ static void __init map_mem(void)*/ __map_memblock (kernel_start, kernel_end,PAGE_KERNEL ,NO_CONT_MAPPINGS );memblock_clear_nomap (kernel_start, kernel_end - kernel_start); -arm64_kfence_map_pool (early_kfence_pool); }void mark_rodata_ro (void )
PATCH v7 8/15 arm64: mm: Permit contiguous attribute for preliminary mappings
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
There are a few cases where we omit the contiguous hint for mappings that start out as read-write and are remapped read-only later, on the basis that manipulating live descriptors with the PTE_CONT attribute set is unsafe. When support for the contiguous hint was added to the code, the ARM ARM was ambiguous about this, and so we erred on the side of caution.
In the meantime, this has been clarified [0], and regions that will be remapped in their entirety, retaining the contiguous bit on all entries, can use the contiguous hint both in the initial mapping as well as the one that replaces it. Note that this requires that the logic that may be called to remap overlapping regions respects existing valid descriptors that have the contiguous bit cleared.
So omit the NO_CONT_MAPPINGS flag in places where it is unneeded.
[0] RJQQTC
For a TLB lookup in a contiguous region mapped by translation table entries that have consistent values for the Contiguous bit, but have the OA, attributes, or permissions misprogrammed, that TLB lookup is permitted to produce an OA, access permissions, and memory attributes that are consistent with any one of the programmed translation table values.
--- arch/arm64/mm/mmu.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
arch/arm64/mm/mmu.c +3 -7
@@ -1016,8 +1016,7 @@ void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt, &phys, virt);return ; } -early_create_pgd_mapping (init_mm.pgd, phys, virt, size, prot,NULL , -NO_CONT_MAPPINGS ); +early_create_pgd_mapping (init_mm.pgd, phys, virt, size, prot,NULL ,0 ); } void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
@@ -1044,8 +1043,7 @@ static void update_mapping_prot(phys_addr_t phys, unsigned long virt,return ; } -early_create_pgd_mapping (init_mm.pgd, phys, virt, size, prot,NULL , -NO_CONT_MAPPINGS ); +early_create_pgd_mapping (init_mm.pgd, phys, virt, size, prot,NULL ,0 );/* flush the TLBs after updating live kernel mappings */ flush_tlb_kernel_range (virt, virt + size);
@@ -1191,10 +1189,8 @@ static void __init map_mem(void)* alternative patching has completed). This makes the contents * of the region accessible to subsystems such as hibernate, * but protects it from inadvertent modification or execution. -* Note that contiguous mappings cannot be remapped in this way, -* so we should avoid them here. */ -__map_memblock (kernel_start, kernel_end,PAGE_KERNEL ,NO_CONT_MAPPINGS ); +__map_memblock (kernel_start, kernel_end,PAGE_KERNEL ,0 );memblock_clear_nomap (kernel_start, kernel_end - kernel_start); }
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 9/15 arm64: Move fixmap and kasan page tables to end of kernel image
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Move the fixmap and kasan page tables out of the BSS section, and place them at the end of the image, right before the init_pg_dir section where some of the other statically allocated page tables live.
These page tables are currently the only data objects in vmlinux that are meant to be accessed via the kernel image's linear alias, and so placing them together allows the remainder of the data/bss section to be remapped read-only or unmapped entirely.
--- arch/arm64/include/asm/mmu.h | 2 ++ arch/arm64/kernel/vmlinux.lds.S | 8 +++++++- arch/arm64/mm/fixmap.c | 6 +++--- arch/arm64/mm/kasan_init.c | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-)
arch/arm64/include/asm/mmu.h +2 -0
@@ -13,6 +13,8 @@#ifndef __ASSEMBLER__ +#define __pgtbl_bss __section(".pgdir.bss") __aligned(PAGE_SIZE) +#include <linux/refcount.h> #include <asm/cpufeature.h>
arch/arm64/kernel/vmlinux.lds.S +7 -1
@@ -349,9 +349,15 @@ SECTIONS _edata = .; /* start of zero-init region */ - BSS_SECTION(SBSS_ALIGN, 0, 0) + BSS_SECTION(SBSS_ALIGN, 0, PAGE_SIZE) __pi___bss_start = __bss_start; + /* fixmap BSS starts here - preceding data/BSS is omitted from the linear map */ + .pgdir.bss (NOLOAD) : ALIGN(PAGE_SIZE) { + *(.pgdir.bss) + } + ASSERT(ADDR(.pgdir.bss) == __bss_stop, ".pgdir.bss must follow BSS") + . = ALIGN(PAGE_SIZE); __pi_init_pg_dir = .; . += INIT_DIR_SIZE;
arch/arm64/mm/fixmap.c +3 -3
@@ -31,9 +31,9 @@ static_assert(NR_BM_PMD_TABLES == 1);#define BM_PTE_TABLE_IDX (addr) __BM_TABLE_IDX(addr, PMD_SHIFT) -static pte_t bm_pte[NR_BM_PTE_TABLES][PTRS_PER_PTE] __page_aligned_bss; -static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; -static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused; +static pte_t bm_pte[NR_BM_PTE_TABLES][PTRS_PER_PTE] __pgtbl_bss; +static pmd_t bm_pmd[PTRS_PER_PMD] __pgtbl_bss __maybe_unused; +static pud_t bm_pud[PTRS_PER_PUD] __pgtbl_bss __maybe_unused;static inline pte_t *fixmap_pte (unsigned long addr) {
arch/arm64/mm/kasan_init.c +1 -1
@@ -214,7 +214,7 @@ asmlinkage void __init kasan_early_init(void)* shadow pud_t[]/p4d_t[], which could end up getting corrupted * when the linear region is mapped. */ - static pte_t tbl[PTRS_PER_PTE] __page_aligned_bss; + static pte_t tbl[PTRS_PER_PTE] __pgtbl_bss;pgd_t *pgdp =pgd_offset_k (KASAN_SHADOW_START );set_pgd (pgdp,__pgd (__pa_symbol (tbl) |PGD_TYPE_TABLE ));
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 10/15 arm64: mm: Don't abuse memblock NOMAP to check for overlaps
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Now that the linear region mapping routines respect existing table mappings and contiguous block and page mappings, it is no longer needed to fiddle with the memblock tables to set and clear the NOMAP attribute in order to omit text and rodata when creating the linear map.
Instead, map the kernel text and rodata alias first with the desired initial attributes and granularity, so that the loop iterating over the memblocks will not remap it in a manner that prevents it from being remapped with updated attributes later.
--- arch/arm64/mm/mmu.c | 26 ++++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-)
arch/arm64/mm/mmu.c +10 -16
@@ -1164,12 +1164,17 @@ static void __init map_mem(void) flags |=NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS ;/* -* Take care not to create a writable alias for the -* read-only text and rodata sections of the kernel image. -* So temporarily mark them as NOMAP to skip mappings in -* the following for-loop +* Map the linear alias of the [_text, __init_begin) interval first +* so that its write permissions can be removed later without the need +* to split any block mappings created by the loop below. +* +* Write permissions are needed for alternatives patching, and will be +* removed later by mark_linear_text_alias_ro() above. This makes the +* contents of the region accessible to subsystems such as hibernate, +* but protects it from inadvertent modification or execution. */ -memblock_mark_nomap (kernel_start, kernel_end - kernel_start); +__map_memblock (kernel_start, kernel_end,pgprot_tagged (PAGE_KERNEL ), + flags);/* map all the memory banks */ for_each_mem_range (i, &start, &end) {
@@ -1181,17 +1186,6 @@ static void __init map_mem(void)__map_memblock (start, end,pgprot_tagged (PAGE_KERNEL ), flags); } - -/* -* Map the linear alias of the [_text, __init_begin) interval -* as non-executable now, and remove the write permission in -* mark_linear_text_alias_ro() below (which will be called after -* alternative patching has completed). This makes the contents -* of the region accessible to subsystems such as hibernate, -* but protects it from inadvertent modification or execution. -*/ -__map_memblock (kernel_start, kernel_end,PAGE_KERNEL ,0 ); -memblock_clear_nomap (kernel_start, kernel_end - kernel_start); }void mark_rodata_ro (void )
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Re: [PATCH v7 10/15] arm64: mm: Don't abuse memblock NOMAP to check for overlaps
Quoted 7 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Now that the linear region mapping routines respect existing table mappings and contiguous block and page mappings, it is no longer needed to fiddle with the memblock tables to set and clear the NOMAP attribute in order to omit text and rodata when creating the linear map.
Quoted 6 lines
Instead, map the kernel text and rodata alias first with the desired initial attributes and granularity, so that the loop iterating over the memblocks will not remap it in a manner that prevents it from being remapped with updated attributes later.
Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 49 lines
--- arch/arm64/mm/mmu.c | 26 ++++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-)arch/arm64/mm/mmu.c +10
-16@@ -1164,12 +1164,17 @@ static void __init map_mem(void) flags |=NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS ;/* -* Take care not to create a writable alias for the -* read-only text and rodata sections of the kernel image. -* So temporarily mark them as NOMAP to skip mappings in -* the following for-loop +* Map the linear alias of the [_text, __init_begin) interval first +* so that its write permissions can be removed later without the need +* to split any block mappings created by the loop below. +* +* Write permissions are needed for alternatives patching, and will be +* removed later by mark_linear_text_alias_ro() above. This makes the +* contents of the region accessible to subsystems such as hibernate, +* but protects it from inadvertent modification or execution. */ -memblock_mark_nomap (kernel_start, kernel_end - kernel_start); +__map_memblock (kernel_start, kernel_end,pgprot_tagged (PAGE_KERNEL ), + flags);/* map all the memory banks */ for_each_mem_range (i, &start, &end) {@@ -1181,17 +1186,6 @@ static void __init map_mem(void)__map_memblock (start, end,pgprot_tagged (PAGE_KERNEL ), flags); } - -/* -* Map the linear alias of the [_text, __init_begin) interval -* as non-executable now, and remove the write permission in -* mark_linear_text_alias_ro() below (which will be called after -* alternative patching has completed). This makes the contents -* of the region accessible to subsystems such as hibernate, -* but protects it from inadvertent modification or execution. -*/ -__map_memblock (kernel_start, kernel_end,PAGE_KERNEL ,0 ); -memblock_clear_nomap (kernel_start, kernel_end - kernel_start); }void mark_rodata_ro (void )
PATCH v7 11/15 powerpc/code-patching: Avoid r/w mapping of the zero page
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The only remaining use of map_patch_area() is mapping the zero page, and immediately unmapping it again so that the intermediate page table levels are all guaranteed to be populated.
The use of the zero page here is completely arbitrary, and not harmful per se, but currently, it creates a writable mapping, and does so in a manner that requires that the empty_zero_page[] symbol is not const-qualified.
Given that this is about to change, and that map_patch_area() now never maps anything other than the zero page, let's simplify the code and
- remove the helpers and call [un]map_kernel_page() directly
- take the PA of empty_zero_page directly
- create a read-only temporary mapping.
This allows empty_zero_page[] to be repainted as const u8[] in a subsequent patch, without making substantial changes to this code patching logic.
--- arch/powerpc/lib/code-patching.c | 52 +------------------- 1 file changed, 2 insertions(+), 50 deletions(-)
arch/powerpc/lib/code-patching.c +2 -50
@@ -60,9 +60,6 @@ struct patch_context { static DEFINE_PER_CPU(struct patch_context, cpu_patching_context); -static int map_patch_area (void *addr,unsigned long text_poke_addr); -static void unmap_patch_area (unsigned long addr); -static bool mm_patch_enabled (void ) {return IS_ENABLED (CONFIG_SMP ) &&radix_enabled ();
@@ -117,11 +114,11 @@ static int text_area_cpu_up(unsigned int cpu)// Map/unmap the area to ensure all page tables are pre-allocated addr = (unsigned long )area->addr; - err =map_patch_area (empty_zero_page, addr); + err =map_kernel_page (addr,__pa_symbol (empty_zero_page),PAGE_KERNEL_RO );if (err)return err; -unmap_patch_area (addr); +unmap_kernel_page (addr);this_cpu_write (cpu_patching_context.area, area);this_cpu_write (cpu_patching_context.addr, addr);
@@ -233,51 +230,6 @@ static unsigned long get_patch_pfn(void *addr)return __pa_symbol (addr) >>PAGE_SHIFT ; } -/* -* This can be called for kernel text or a module. -*/ -static int map_patch_area (void *addr,unsigned long text_poke_addr) -{ -unsigned long pfn =get_patch_pfn (addr); - -return map_kernel_page (text_poke_addr, (pfn <<PAGE_SHIFT ),PAGE_KERNEL ); -} - -static void unmap_patch_area (unsigned long addr) -{ -pte_t *ptep; -pmd_t *pmdp; -pud_t *pudp; -p4d_t *p4dp; -pgd_t *pgdp; - - pgdp =pgd_offset_k (addr); -if (WARN_ON (pgd_none (*pgdp))) -return ; - - p4dp =p4d_offset (pgdp, addr); -if (WARN_ON (p4d_none (*p4dp))) -return ; - - pudp =pud_offset (p4dp, addr); -if (WARN_ON (pud_none (*pudp))) -return ; - - pmdp =pmd_offset (pudp, addr); -if (WARN_ON (pmd_none (*pmdp))) -return ; - - ptep =pte_offset_kernel (pmdp, addr); -if (WARN_ON (pte_none (*ptep))) -return ; - -/* -* In hash, pte_clear flushes the tlb, in radix, we have to -*/ -pte_clear (&init_mm, addr, ptep); -flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); -} -static int __do_patch_mem_mm (void *addr,unsigned long val,bool is_dword) {int err;
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Re: [PATCH v7 11/15] powerpc/code-patching: Avoid r/w mapping of the zero page
Quoted 114 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The only remaining use of
map_patch_area()is mapping the zero page, and immediately unmapping it again so that the intermediate page table levels are all guaranteed to be populated.The use of the zero page here is completely arbitrary, and not harmful per se, but currently, it creates a writable mapping, and does so in a manner that requires that the empty_zero_page[] symbol is not const-qualified.
Given that this is about to change, and that
map_patch_area()now never maps anything other than the zero page, let's simplify the code and
- remove the helpers and call [un]map_kernel_page() directly
- take the PA ofempty_zero_pagedirectly
- create a read-only temporary mapping.This allows empty_zero_page[] to be repainted as const u8[] in a subsequent patch, without making substantial changes to this code patching logic.
Cc Madhavan Srinivasan <[email protected]>Cc Michael Ellerman <[email protected]>Cc Nicholas Piggin <[email protected]>Cc "Christophe Leroy (CS GROUP)" <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/powerpc/lib/code-patching.c | 52 +------------------- 1 file changed, 2 insertions(+), 50 deletions(-)arch/powerpc/lib/code-patching.c +2
-50@@ -60,9 +60,6 @@ struct patch_context { static DEFINE_PER_CPU(struct patch_context, cpu_patching_context); -static int map_patch_area (void *addr,unsigned long text_poke_addr); -static void unmap_patch_area (unsigned long addr); -static bool mm_patch_enabled (void ) {return IS_ENABLED (CONFIG_SMP ) &&radix_enabled ();@@ -117,11 +114,11 @@ static int text_area_cpu_up(unsigned int cpu)// Map/unmap the area to ensure all page tables are pre-allocated addr = (unsigned long )area->addr; - err =map_patch_area (empty_zero_page, addr); + err =map_kernel_page (addr,__pa_symbol (empty_zero_page),PAGE_KERNEL_RO );if (err)return err; -unmap_patch_area (addr); +unmap_kernel_page (addr);this_cpu_write (cpu_patching_context.area, area);this_cpu_write (cpu_patching_context.addr, addr);@@ -233,51 +230,6 @@ static unsigned long get_patch_pfn(void *addr)return __pa_symbol (addr) >>PAGE_SHIFT ; } -/* -* This can be called for kernel text or a module. -*/ -static int map_patch_area (void *addr,unsigned long text_poke_addr) -{ -unsigned long pfn =get_patch_pfn (addr); - -return map_kernel_page (text_poke_addr, (pfn <<PAGE_SHIFT ),PAGE_KERNEL ); -} - -static void unmap_patch_area (unsigned long addr) -{ -pte_t *ptep; -pmd_t *pmdp; -pud_t *pudp; -p4d_t *p4dp; -pgd_t *pgdp; - - pgdp =pgd_offset_k (addr); -if (WARN_ON (pgd_none (*pgdp))) -return ; - - p4dp =p4d_offset (pgdp, addr); -if (WARN_ON (p4d_none (*p4dp))) -return ; - - pudp =pud_offset (p4dp, addr); -if (WARN_ON (pud_none (*pudp))) -return ; - - pmdp =pmd_offset (pudp, addr); -if (WARN_ON (pmd_none (*pmdp))) -return ; - - ptep =pte_offset_kernel (pmdp, addr); -if (WARN_ON (pte_none (*ptep))) -return ; - -/* -* In hash, pte_clear flushes the tlb, in radix, we have to -*/ -pte_clear (&init_mm, addr, ptep); -flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); -} -static int __do_patch_mem_mm (void *addr,unsigned long val,bool is_dword) {int err;Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
I don't see any functional change.
Re: [PATCH v7 11/15] powerpc/code-patching: Avoid r/w mapping of the zero page
In reply to Ard BiesheuvelLe 29/05/2026 à 17:02, Ard Biesheuvel a écrit :
Quoted 21 lines
From: Ard Biesheuvel <[email protected]>
The only remaining use of
map_patch_area()is mapping the zero page, and immediately unmapping it again so that the intermediate page table levels are all guaranteed to be populated.The use of the zero page here is completely arbitrary, and not harmful per se, but currently, it creates a writable mapping, and does so in a manner that requires that the empty_zero_page[] symbol is not const-qualified.
Given that this is about to change, and that
map_patch_area()now never maps anything other than the zero page, let's simplify the code and
- remove the helpers and call [un]map_kernel_page() directly
- take the PA ofempty_zero_pagedirectly
- create a read-only temporary mapping.This allows empty_zero_page[] to be repainted as const u8[] in a subsequent patch, without making substantial changes to this code patching logic.
Quoted 6 lines
Cc Madhavan Srinivasan <[email protected]>Cc Michael Ellerman <[email protected]>Cc Nicholas Piggin <[email protected]>Cc "Christophe Leroy (CS GROUP)" <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 84 lines
--- arch/powerpc/lib/code-patching.c | 52 +------------------- 1 file changed, 2 insertions(+), 50 deletions(-)arch/powerpc/lib/code-patching.c +2
-50@@ -60,9 +60,6 @@ struct patch_context { static DEFINE_PER_CPU(struct patch_context, cpu_patching_context); -static int map_patch_area (void *addr,unsigned long text_poke_addr); -static void unmap_patch_area (unsigned long addr); -static bool mm_patch_enabled (void ) {return IS_ENABLED (CONFIG_SMP ) &&radix_enabled ();@@ -117,11 +114,11 @@ static int text_area_cpu_up(unsigned int cpu)// Map/unmap the area to ensure all page tables are pre-allocated addr = (unsigned long )area->addr; - err =map_patch_area (empty_zero_page, addr); + err =map_kernel_page (addr,__pa_symbol (empty_zero_page),PAGE_KERNEL_RO );if (err)return err; -unmap_patch_area (addr); +unmap_kernel_page (addr);this_cpu_write (cpu_patching_context.area, area);this_cpu_write (cpu_patching_context.addr, addr);@@ -233,51 +230,6 @@ static unsigned long get_patch_pfn(void *addr)return __pa_symbol (addr) >>PAGE_SHIFT ; } -/* -* This can be called for kernel text or a module. -*/ -static int map_patch_area (void *addr,unsigned long text_poke_addr) -{ -unsigned long pfn =get_patch_pfn (addr); - -return map_kernel_page (text_poke_addr, (pfn <<PAGE_SHIFT ),PAGE_KERNEL ); -} - -static void unmap_patch_area (unsigned long addr) -{ -pte_t *ptep; -pmd_t *pmdp; -pud_t *pudp; -p4d_t *p4dp; -pgd_t *pgdp; - - pgdp =pgd_offset_k (addr); -if (WARN_ON (pgd_none (*pgdp))) -return ; - - p4dp =p4d_offset (pgdp, addr); -if (WARN_ON (p4d_none (*p4dp))) -return ; - - pudp =pud_offset (p4dp, addr); -if (WARN_ON (pud_none (*pudp))) -return ; - - pmdp =pmd_offset (pudp, addr); -if (WARN_ON (pmd_none (*pmdp))) -return ; - - ptep =pte_offset_kernel (pmdp, addr); -if (WARN_ON (pte_none (*ptep))) -return ; - -/* -* In hash, pte_clear flushes the tlb, in radix, we have to -*/ -pte_clear (&init_mm, addr, ptep); -flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); -} -static int __do_patch_mem_mm (void *addr,unsigned long val,bool is_dword) {int err;
PATCH v7 12/15 sh: Drop cache flush of the zero page at boot
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
SuperH performs cache maintenance on the zero page during boot, presumably because before commit
6215d9f4470f ("arch, mm: consolidate empty_zero_page")
the zero page did double duty as a boot params region, and was cleared separately, as it was not part of BSS. The memset() in question was dropped by that commit, but the __flush_wback_region() call remained.
As empty_zero_page[] has been moved to BSS, it can be treated as any other BSS memory, and so the cache flush can be dropped.
--- arch/sh/mm/init.c | 3 --- 1 file changed, 3 deletions(-)
arch/sh/mm/init.c +0 -3
@@ -331,9 +331,6 @@ void __init mem_init(void)/* Set this up early, so we can take care of the zero page */ cpu_cache_init (); -/* clear the zero-page */ -__flush_wback_region (empty_zero_page,PAGE_SIZE ); -vsyscall_init (); pr_info("virtual kernel memory layout:\n"
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Re: [PATCH v7 12/15] sh: Drop cache flush of the zero page at boot
Quoted 14 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
SuperH performs cache maintenance on the zero page during boot, presumably because before commit
6215d9f4470f("arch, mm: consolidateempty_zero_page")the zero page did double duty as a boot params region, and was cleared separately, as it was not part of BSS. The
memset()in question was dropped by that commit, but the__flush_wback_region()call remained.As empty_zero_page[] has been moved to BSS, it can be treated as any other BSS memory, and so the cache flush can be dropped.
Quoted 6 lines
Cc Yoshinori Sato <[email protected]>Cc Rich Felker <[email protected]>Cc John Paul Adrian Glaubitz <[email protected]>Cc Mike Rapoport <[email protected]>Cc Geert Uytterhoeven <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 3 lines
--- arch/sh/mm/init.c | 3 --- 1 file changed, 3 deletions(-)
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v7 12/15] sh: Drop cache flush of the zero page at boot
In reply to Ard BiesheuvelQuoted 14 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
SuperH performs cache maintenance on the zero page during boot, presumably because before commit
6215d9f4470f("arch, mm: consolidateempty_zero_page")the zero page did double duty as a boot params region, and was cleared separately, as it was not part of BSS. The
memset()in question was dropped by that commit, but the__flush_wback_region()call remained.As empty_zero_page[] has been moved to BSS, it can be treated as any other BSS memory, and so the cache flush can be dropped.
Quoted 6 lines
Cc Yoshinori Sato <[email protected]>Cc Rich Felker <[email protected]>Cc John Paul Adrian Glaubitz <[email protected]>Cc Mike Rapoport <[email protected]>Cc Geert Uytterhoeven <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
PATCH v7 13/15 mm: Make empty_zero_page[] const
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The empty zero page is used to back any kernel or user space mapping that is supposed to remain cleared, and so the page itself is never supposed to be modified.
So mark it as const, which moves it into .rodata rather than .bss: on most architectures, this ensures that both the kernel's mapping of it and any aliases that are accessible via the kernel direct (linear) map are mapped read-only, and cannot be used (inadvertently or maliciously) to corrupt the contents of the zero page.
--- include/linux/pgtable.h | 2 +- mm/mm_init.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/pgtable.h +1 -1
@@ -1993,7 +1993,7 @@ static inline unsigned long zero_pfn(unsigned long addr)return zero_page_pfn; } -extern uint8_t empty_zero_page[PAGE_SIZE ]; +extern const uint8_t empty_zero_page[PAGE_SIZE ];extern struct page *__zero_page;static inline struct page *_zero_page (unsigned long addr)
mm/mm_init.c +1 -1
@@ -57,7 +57,7 @@ unsigned long zero_page_pfn __ro_after_init;EXPORT_SYMBOL (zero_page_pfn);#ifndef __HAVE_COLOR_ZERO_PAGE -uint8_t empty_zero_page[PAGE_SIZE] __page_aligned_bss; +const uint8_t empty_zero_page[PAGE_SIZE ]__aligned(PAGE_SIZE) ;EXPORT_SYMBOL (empty_zero_page);struct page *__zero_page__ro_after_init ;
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 14/15 arm64: mm: Map the kernel data/bss read-only in the linear map
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
On systems where the bootloader adheres to the original arm64 boot protocol, the placement of the kernel in the physical address space is highly predictable, and this makes the placement of its linear alias in the kernel virtual address space equally predictable, given the lack of randomization of the linear map.
The linear aliases of the kernel text and rodata regions are already mapped read-only, but the kernel data and bss are mapped read-write in this region. This is not needed, so map them read-only as well.
Note that the statically allocated kernel page tables do need to be modifiable via the linear map, so leave these mapped read-write.
--- arch/arm64/mm/mmu.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
arch/arm64/mm/mmu.c +13 -2
@@ -1138,7 +1138,9 @@ static void __init map_mem(void) {static const u64 direct_map_end =_PAGE_END (VA_BITS_MIN );phys_addr_t kernel_start =__pa_symbol (_text); -phys_addr_t kernel_end =__pa_symbol (__init_begin); +phys_addr_t init_begin =__pa_symbol (__init_begin); +phys_addr_t init_end =__pa_symbol (__init_end); +phys_addr_t kernel_end =__pa_symbol (__bss_stop);phys_addr_t start, end;int flags =NO_EXEC_MAPPINGS ;u64 i;
@@ -1173,7 +1175,11 @@ static void __init map_mem(void)* contents of the region accessible to subsystems such as hibernate, * but protects it from inadvertent modification or execution. */ -__map_memblock (kernel_start, kernel_end,pgprot_tagged (PAGE_KERNEL ), +__map_memblock (kernel_start, init_begin,pgprot_tagged (PAGE_KERNEL ), + flags); + +/* Map the kernel data/bss so it can be remapped later */ +__map_memblock (init_end, kernel_end,pgprot_tagged (PAGE_KERNEL ), flags);/* map all the memory banks */
@@ -1186,6 +1192,11 @@ static void __init map_mem(void)__map_memblock (start, end,pgprot_tagged (PAGE_KERNEL ), flags); } + +/* Map the kernel data/bss read-only in the linear map */ +__map_memblock (init_end, kernel_end,PAGE_KERNEL_RO , flags); +flush_tlb_kernel_range ((unsigned long )lm_alias (__init_end), + (unsigned long )lm_alias (__bss_stop)); }void mark_rodata_ro (void )
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH v7 15/15 arm64: mm: Unmap kernel data/bss entirely from the linear map
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The linear aliases of the kernel text and rodata are also mapped read-only in the linear map. Given that the contents of these regions are mostly identical to the version in the loadable image, mapping them read-only and leaving their contents visible is a reasonable hardening measure.
Data and bss, however, are now also mapped read-only but the contents of these regions are more likely to contain data that we'd rather not leak. So let's unmap these entirely in the linear map when the kernel is running normally.
When going into hibernation or waking up from it, these regions need to be mapped, so map the region initially, and toggle the valid bit so map/unmap the region as needed.
Doing so is required because pages covering the kernel image are marked as PageReserved, and therefore disregarded for snapshotting by the hibernate logic unless they are mapped.
--- arch/arm64/mm/mmu.c | 45 ++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-)
arch/arm64/mm/mmu.c +41 -4
@@ -24,6 +24,7 @@#include <linux/mm.h> #include <linux/vmalloc.h> #include <linux/set_memory.h> +#include <linux/suspend.h> #include <linux/kfence.h> #include <linux/pkeys.h> #include <linux/mm_inline.h>
@@ -1056,6 +1057,29 @@ static void __init __map_memblock(phys_addr_t start, phys_addr_t end, end - start, prot, early_pgtable_alloc, flags); } +static void mark_linear_data_alias_valid (bool valid) +{ +set_memory_valid ((unsigned long )lm_alias (__init_end), + (unsigned long )(__bss_stop - __init_end) /PAGE_SIZE , + valid); +} + +static int arm64_hibernate_pm_notify (struct notifier_block *nb, +unsigned long mode,void *unused) +{ +switch (mode) { +default : +break ; +case PM_POST_HIBERNATION : +mark_linear_data_alias_valid (false); +break ; +case PM_HIBERNATION_PREPARE : +mark_linear_data_alias_valid (true); +break ; + } +return 0 ; +} +void __init mark_linear_text_alias_ro (void ) {/*
@@ -1064,6 +1088,21 @@ void __init mark_linear_text_alias_ro(void)update_mapping_prot (__pa_symbol (_text), (unsigned long )lm_alias (_text), (unsigned long )__init_begin - (unsigned long )_text,PAGE_KERNEL_RO ); + +/* +* Register a PM notifier to remap the linear alias of data/bss as +* valid read-only before hibernation. This is needed because the +* snapshot logic disregards PageReserved pages (such as the ones +* covering the kernel image) unless they are mapped in the linear +* map. +*/ +if (IS_ENABLED (CONFIG_HIBERNATION )) { +static struct notifier_block nb = { + .notifier_call = arm64_hibernate_pm_notify + }; + +register_pm_notifier (&nb); + } }#ifdef CONFIG_KFENCE
@@ -1193,10 +1232,8 @@ static void __init map_mem(void) flags); } -/* Map the kernel data/bss read-only in the linear map */ -__map_memblock (init_end, kernel_end,PAGE_KERNEL_RO , flags); -flush_tlb_kernel_range ((unsigned long )lm_alias (__init_end), - (unsigned long )lm_alias (__bss_stop)); +/* Map the kernel data/bss as invalid in the linear map */ +mark_linear_data_alias_valid (false); }void mark_rodata_ro (void )
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Re: [PATCH v7 15/15] arm64: mm: Unmap kernel data/bss entirely from the linear map
Quoted 8 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The linear aliases of the kernel text and rodata are also mapped read-only in the linear map. Given that the contents of these regions are mostly identical to the version in the loadable image, mapping them read-only and leaving their contents visible is a reasonable hardening measure.
Quoted 8 lines
Data and bss, however, are now also mapped read-only but the contents of these regions are more likely to contain data that we'd rather not leak. So let's unmap these entirely in the linear map when the kernel is running normally.
When going into hibernation or waking up from it, these regions need to be mapped, so map the region initially, and toggle the valid bit so map/unmap the region as needed.
s/so map/to map/? Also not sure what "initially" is referring to here.
Otherwise:
I don't know much about hibernation though, would be good for someone knowledgeable to have a look.
- Kevin
Quoted 86 lines
Doing so is required because pages covering the kernel image are marked as PageReserved, and therefore disregarded for snapshotting by the hibernate logic unless they are mapped.
Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/mm/mmu.c | 45 ++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-)arch/arm64/mm/mmu.c +41
-4@@ -24,6 +24,7 @@#include <linux/mm.h> #include <linux/vmalloc.h> #include <linux/set_memory.h> +#include <linux/suspend.h> #include <linux/kfence.h> #include <linux/pkeys.h> #include <linux/mm_inline.h> @@ -1056,6 +1057,29 @@ static void __init __map_memblock(phys_addr_t start, phys_addr_t end, end - start, prot, early_pgtable_alloc, flags); } +static void mark_linear_data_alias_valid (bool valid) +{ +set_memory_valid ((unsigned long )lm_alias (__init_end), + (unsigned long )(__bss_stop - __init_end) /PAGE_SIZE , + valid); +} + +static int arm64_hibernate_pm_notify (struct notifier_block *nb, +unsigned long mode,void *unused) +{ +switch (mode) { +default : +break ; +case PM_POST_HIBERNATION : +mark_linear_data_alias_valid (false); +break ; +case PM_HIBERNATION_PREPARE : +mark_linear_data_alias_valid (true); +break ; + } +return 0 ; +} +void __init mark_linear_text_alias_ro (void ) {/* @@ -1064,6 +1088,21 @@ void __init mark_linear_text_alias_ro(void)update_mapping_prot (__pa_symbol (_text), (unsigned long )lm_alias (_text), (unsigned long )__init_begin - (unsigned long )_text,PAGE_KERNEL_RO ); + +/* +* Register a PM notifier to remap the linear alias of data/bss as +* valid read-only before hibernation. This is needed because the +* snapshot logic disregards PageReserved pages (such as the ones +* covering the kernel image) unless they are mapped in the linear +* map. +*/ +if (IS_ENABLED (CONFIG_HIBERNATION )) { +static struct notifier_block nb = { + .notifier_call = arm64_hibernate_pm_notify + }; + +register_pm_notifier (&nb); + } }#ifdef CONFIG_KFENCE @@ -1193,10 +1232,8 @@ static void __init map_mem(void) flags); } -/* Map the kernel data/bss read-only in the linear map */ -__map_memblock (init_end, kernel_end,PAGE_KERNEL_RO , flags); -flush_tlb_kernel_range ((unsigned long )lm_alias (__init_end), - (unsigned long )lm_alias (__bss_stop)); +/* Map the kernel data/bss as invalid in the linear map */ +mark_linear_data_alias_valid (false); }void mark_rodata_ro (void )
Re: [PATCH v7 15/15] arm64: mm: Unmap kernel data/bss entirely from the linear map
In reply to Ard BiesheuvelDear All,
Quoted 17 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The linear aliases of the kernel text and rodata are also mapped read-only in the linear map. Given that the contents of these regions are mostly identical to the version in the loadable image, mapping them read-only and leaving their contents visible is a reasonable hardening measure.
Data and bss, however, are now also mapped read-only but the contents of these regions are more likely to contain data that we'd rather not leak. So let's unmap these entirely in the linear map when the kernel is running normally.
When going into hibernation or waking up from it, these regions need to be mapped, so map the region initially, and toggle the valid bit so map/unmap the region as needed.
Quoted 5 lines
Doing so is required because pages covering the kernel image are marked as PageReserved, and therefore disregarded for snapshotting by the hibernate logic unless they are mapped.
Signed-off-by Ard Biesheuvel <[email protected]>
This commit landed in yesterday's linux-next as commit 63e0b6a5b693 ("arm64: mm: Unmap kernel data/bss entirely from the linear map"). In my tests I found that it breaks booting of RaspberryPi3 and RaspberryPi4 boards with the following kernel panic:
kvm [1]: nv: 570 coarse grained trap handlers
kvm [1]: nv: 710 fine grained trap handlers
kvm [1]: IPA Size Limit: 40 bits
Unable to handle kernel paging request at virtual address ffff000003a23000
Mem abort info:
ESR = 0x0000000096000147
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x07: level 3 translation fault
Data abort info:
ISV = 0, ISS = 0x00000147, ISS2 = 0x00000000
CM = 1, WnR = 1, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
swapper pgtable: 4k pages, 48-bit VAs, pgdp=0000000002609000
[ffff000003a23000] pgd=0000000000000000, p4d=180000003b3ff403, pud=180000003b3fe403, pmd=180000003b3e6403, pte=00e8000003a23f06
Internal error: Oops: 0000000096000147 [#1] SMP Modules linked in: CPU: 3 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.1.0-rc1+ #16768 PREEMPT Hardware name: Raspberry Pi 3 Model B (DT) pstate: 80000005 (Nzcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : dcache_clean_inval_poc+0x24/0x48 lr : kvm_arm_init+0xa8c/0x165c sp : ffff8000844bbd00 ... Call trace: dcache_clean_inval_poc+0x24/0x48 (P) do_one_initcall+0x68/0x4f4 kernel_init_freeable+0x24c/0x360 kernel_init+0x24/0x1dc ret_from_fork+0x10/0x20 Code: 9ac32042 d1000443 8a230000 d503201f (d50b7e20) ---[ end trace 0000000000000000 ]--- Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b
SMP: stopping secondary CPUs
Kernel Offset: disabled
CPU features: 0x00000000,03000008,00040000,0400421b Memory Limit: none
---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b ]---
Quoted 71 lines
--- arch/arm64/mm/mmu.c | 45 ++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-)arch/arm64/mm/mmu.c +39
-0@@ -24,6 +24,7 @@#include <linux/mm.h> #include <linux/vmalloc.h> #include <linux/set_memory.h> +#include <linux/suspend.h> #include <linux/kfence.h> #include <linux/pkeys.h> #include <linux/mm_inline.h> @@ -1056,6 +1057,29 @@ static void __init __map_memblock(phys_addr_t start, phys_addr_t end, end - start, prot, early_pgtable_alloc, flags); } +static void mark_linear_data_alias_valid (bool valid) +{ +set_memory_valid ((unsigned long )lm_alias (__init_end), + (unsigned long )(__bss_stop - __init_end) /PAGE_SIZE , + valid); +} + +static int arm64_hibernate_pm_notify (struct notifier_block *nb, +unsigned long mode,void *unused) +{ +switch (mode) { +default : +break ; +case PM_POST_HIBERNATION : +mark_linear_data_alias_valid (false); +break ; +case PM_HIBERNATION_PREPARE : +mark_linear_data_alias_valid (true); +break ; + } +return 0 ; +} +void __init mark_linear_text_alias_ro (void ) {/* @@ -1064,6 +1088,21 @@ void __init mark_linear_text_alias_ro(void)update_mapping_prot (__pa_symbol (_text), (unsigned long )lm_alias (_text), (unsigned long )__init_begin - (unsigned long )_text,PAGE_KERNEL_RO ); + +/* +* Register a PM notifier to remap the linear alias of data/bss as +* valid read-only before hibernation. This is needed because the +* snapshot logic disregards PageReserved pages (such as the ones +* covering the kernel image) unless they are mapped in the linear +* map. +*/ +if (IS_ENABLED (CONFIG_HIBERNATION )) { +static struct notifier_block nb = { + .notifier_call = arm64_hibernate_pm_notify + }; + +register_pm_notifier (&nb); + } }#ifdef CONFIG_KFENCE @@ -1193,10 +1232,8 @@ static void __init map_mem(void) flags); }
arch/arm64/mm/mmu.c static void __init map_mem(void) · line 1239
Quoted 10 lines
arch/arm64/mm/mmu.c +2
-4-/* Map the kernel data/bss read-only in the linear map */ -__map_memblock (init_end, kernel_end,PAGE_KERNEL_RO , flags); -flush_tlb_kernel_range ((unsigned long )lm_alias (__init_end), - (unsigned long )lm_alias (__bss_stop)); +/* Map the kernel data/bss as invalid in the linear map */ +mark_linear_data_alias_valid (false); }void mark_rodata_ro (void )
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Re: [PATCH v7 15/15] arm64: mm: Unmap kernel data/bss entirely from the linear map
Quoted 22 lines · Marek Szyprowski
Quoted 21 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The linear aliases of the kernel text and rodata are also mapped read-only in the linear map. Given that the contents of these regions are mostly identical to the version in the loadable image, mapping them read-only and leaving their contents visible is a reasonable hardening measure.
Data and bss, however, are now also mapped read-only but the contents of these regions are more likely to contain data that we'd rather not leak. So let's unmap these entirely in the linear map when the kernel is running normally.
When going into hibernation or waking up from it, these regions need to be mapped, so map the region initially, and toggle the valid bit so map/unmap the region as needed.
Doing so is required because pages covering the kernel image are marked as PageReserved, and therefore disregarded for snapshotting by the hibernate logic unless they are mapped.
Quoted 5 lines
Quoted 1 line
Signed-off-by Ard Biesheuvel <[email protected]>This commit landed in yesterday's linux-next as commit
63e0b6a5b693("arm64: mm: Unmap kernel data/bss entirely from the linear map"). In my tests I found that it breaks booting of RaspberryPi3 and RaspberryPi4 boards with the following kernel panic:
One more comment - reverting 63e0b6a5b693 and 53205d56212c (dependent change) on top of next-20260608 fixes this issue.
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Re: [PATCH v7 15/15] arm64: mm: Unmap kernel data/bss entirely from the linear map
Quoted 23 lines · Marek Szyprowski
Quoted 22 lines · Marek Szyprowski
Quoted 21 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The linear aliases of the kernel text and rodata are also mapped read-only in the linear map. Given that the contents of these regions are mostly identical to the version in the loadable image, mapping them read-only and leaving their contents visible is a reasonable hardening measure.
Data and bss, however, are now also mapped read-only but the contents of these regions are more likely to contain data that we'd rather not leak. So let's unmap these entirely in the linear map when the kernel is running normally.
When going into hibernation or waking up from it, these regions need to be mapped, so map the region initially, and toggle the valid bit so map/unmap the region as needed.
Doing so is required because pages covering the kernel image are marked as PageReserved, and therefore disregarded for snapshotting by the hibernate logic unless they are mapped.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Ard Biesheuvel <[email protected]>This commit landed in yesterday's linux-next as commit
63e0b6a5b693("arm64: mm: Unmap kernel data/bss entirely from the linear map"). In my tests I found that it breaks booting of RaspberryPi3 and RaspberryPi4 boards with the following kernel panic:One more comment - reverting 63e0b6a5b693 and
53205d56212c(dependent change) on top of next-20260608 fixes this issue.
Thanks for the report, and for the confirmation that those reverts fix the issue - this was reported here as well:
Re: [PATCH v7 15/15] arm64: mm: Unmap kernel data/bss entirely from the linear map
In reply to Marek SzyprowskiHi,
Quoted 23 lines · Marek Szyprowski
Quoted 22 lines · Marek Szyprowski
Quoted 21 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The linear aliases of the kernel text and rodata are also mapped read-only in the linear map. Given that the contents of these regions are mostly identical to the version in the loadable image, mapping them read-only and leaving their contents visible is a reasonable hardening measure.
Data and bss, however, are now also mapped read-only but the contents of these regions are more likely to contain data that we'd rather not leak. So let's unmap these entirely in the linear map when the kernel is running normally.
When going into hibernation or waking up from it, these regions need to be mapped, so map the region initially, and toggle the valid bit so map/unmap the region as needed.
Doing so is required because pages covering the kernel image are marked as PageReserved, and therefore disregarded for snapshotting by the hibernate logic unless they are mapped.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Ard Biesheuvel <[email protected]>This commit landed in yesterday's linux-next as commit
63e0b6a5b693("arm64: mm: Unmap kernel data/bss entirely from the linear map"). In my tests I found that it breaks booting of RaspberryPi3 and RaspberryPi4 boards with the following kernel panic:One more comment - reverting 63e0b6a5b693 and
53205d56212c(dependent change) on top of next-20260608 fixes this issue.
Thanks for report! It seems it already has been reported and discussed in another thread [1].
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Cheers
Vladimir
Quoted 3 lines
Best regards
-- Marek Szyprowski, PhD Samsung R&D Institute Poland
Re: [PATCH v7 15/15] arm64: mm: Unmap kernel data/bss entirely from the linear map
In reply to Marek SzyprowskiQuoted 23 lines · Marek Szyprowski
Quoted 22 lines · Marek Szyprowski
Quoted 21 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The linear aliases of the kernel text and rodata are also mapped read-only in the linear map. Given that the contents of these regions are mostly identical to the version in the loadable image, mapping them read-only and leaving their contents visible is a reasonable hardening measure.
Data and bss, however, are now also mapped read-only but the contents of these regions are more likely to contain data that we'd rather not leak. So let's unmap these entirely in the linear map when the kernel is running normally.
When going into hibernation or waking up from it, these regions need to be mapped, so map the region initially, and toggle the valid bit so map/unmap the region as needed.
Doing so is required because pages covering the kernel image are marked as PageReserved, and therefore disregarded for snapshotting by the hibernate logic unless they are mapped.
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Ard Biesheuvel <[email protected]>This commit landed in yesterday's linux-next as commit
63e0b6a5b693("arm64: mm: Unmap kernel data/bss entirely from the linear map"). In my tests I found that it breaks booting of RaspberryPi3 and RaspberryPi4 boards with the following kernel panic:
Seeing the same panic on R-Car H3 ES2.0 (Cortex A57/A53), but not on R-Car V4M (Cortex A76).
Quoted 2 lines
One more comment - reverting
63e0b6a5b693and53205d56212c(dependent change) on top of next-20260608 fixes this issue.
Confirmed, too.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 9 lines · Ard Biesheuvel
One of the reasons the lack of randomization of the linear map on arm64 is considered problematic is the fact that bootloaders adhering to the original arm64 boot protocol (i.e., a substantial fraction of all Android phones) may place the kernel at the base of DRAM, and therefore at the base of the non-randomized linear map. This puts a writable alias of the kernel's data and bss regions at a predictable location, removing the need for an attacker to guess where KASLR mapped the kernel.
[...]
It would've been nice to hear from the ppc folks on patch 11, but I've picked it up on the assumption that they'll love the negative diff stat. Worst case, we can drop/revert stuff if they have late objections.
Applied to arm64 (for-next/mm), thanks!
[01/15] arm64: mm: Remove bogus stop condition from map_mem() loop
https://git.kernel.org/arm64/c/36ca7f4be809 [02/15] arm64: mm: Drop redundant pgd_t* argument from map_mem() https://git.kernel.org/arm64/c/2e527667a3b9 [03/15] arm64: mm: Check for pud_/pmd_set_huge() failures on kernel mappings https://git.kernel.org/arm64/c/8dd640d9233d [04/15] arm64: mm: Preserve existing table mappings when mapping DRAM https://git.kernel.org/arm64/c/a64293e993f6 [05/15] arm64: mm: Preserve non-contiguous descriptors when mapping DRAM https://git.kernel.org/arm64/c/ecda73ae92ca [06/15] arm64: mm: Permit contiguous descriptors to be manipulated https://git.kernel.org/arm64/c/05c5c31e9d8d [07/15] arm64: kfence: Avoid NOMAP tricks when mapping the early pool https://git.kernel.org/arm64/c/dfd73e574d38 [08/15] arm64: mm: Permit contiguous attribute for preliminary mappings https://git.kernel.org/arm64/c/28becb2c1d74 [09/15] arm64: Move fixmap and kasan page tables to end of kernel image https://git.kernel.org/arm64/c/382a03e12eba [10/15] arm64: mm: Don't abuse memblock NOMAP to check for overlaps https://git.kernel.org/arm64/c/d672a4b72c95 [11/15] powerpc/code-patching: Avoid r/w mapping of the zero page https://git.kernel.org/arm64/c/c0693153fb17 [12/15] sh: Drop cache flush of the zero page at boot https://git.kernel.org/arm64/c/99bad3e992e2 [13/15] mm: Make empty_zero_page[] const https://git.kernel.org/arm64/c/0aae825f1ed7 [14/15] arm64: mm: Map the kernel data/bss read-only in the linear map https://git.kernel.org/arm64/c/f2ba877402e5 [15/15] arm64: mm: Unmap kernel data/bss entirely from the linear map https://git.kernel.org/arm64/c/63e0b6a5b693
Cheers,
Signature · 5 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev
(cc Marc)
Quoted 9 lines · Will Deacon
Quoted 8 lines · Ard Biesheuvel
One of the reasons the lack of randomization of the linear map on arm64 is considered problematic is the fact that bootloaders adhering to the original arm64 boot protocol (i.e., a substantial fraction of all Android phones) may place the kernel at the base of DRAM, and therefore at the base of the non-randomized linear map. This puts a writable alias of the kernel's data and bss regions at a predictable location, removing the need for an attacker to guess where KASLR mapped the kernel.
Quoted 6 lines
Quoted 1 line
[...]
It would've been nice to hear from the ppc folks on patch 11, but I've picked it up on the assumption that they'll love the negative diff stat. Worst case, we can drop/revert stuff if they have late objections.
Thanks.
There is a de facto ack from Michael Ellerman in the Link:, which is why I included it.
Note that Sashiko found an issue with KVM+MTE, where a read-only mapping of the zero page in the linear map may result in issues:
"""
Does moving the zero page to .rodata (or unmapping/read-only mapping its linear alias) expose a guest-to-host denial of service with KVM and MTE? When an MTE-enabled KVM guest reads an unmapped memory address, KVM handles the stage-2 fault by mapping the host's shared zero page. KVM will then call sanitise_mte_tags() in arch/arm64/kvm/mmu.c.
Since the PG_mte_tagged flag is never set on the zero page, KVM's try_page_mte_tagging() succeeds, and it calls mte_clear_page_tags(). This executes the STGM instruction using the zero page's linear map alias. If this alias is read-only or unmapped, won't the STGM instruction trigger a synchronous permission fault or translation fault in EL1, causing a host kernel panic?
"""
Marc seems to think it is legit, so I came up with the following (I'll send it out separately with another pair of tweaks):
-------8<------------
From: Ard Biesheuvel <[email protected]>
Subject: [PATCH] arm64: mte: Disregard the zero page explicitly for
manipulating tags
The zero page is conceptually immutable, and will be moved into .rodata to prevent inadvertent corruption.
Prepare the MTE code for this, by ensuring that the zero page is never taken into account for tag manipulation, given that those actions will no longer be permitted on the read-only alias of .rodata in the linear map.
arch/arm64/include/asm/mte.h +5 -0
@@ -80,6 +80,11 @@ static inline bool page_mte_tagged(struct page *page)*/ static inline bool try_page_mte_tagging (struct page *page) { +extern struct page *__zero_page; + +if (page == __zero_page) +return false; +VM_WARN_ON_ONCE (folio_test_hugetlb (page_folio (page)));if (!test_and_set_bit (PG_mte_lock, &page->flags.f))
Will Deacon
maintainerQuoted 41 lines · Ard Biesheuvel
(cc Marc)
Quoted 9 lines · Will Deacon
Quoted 8 lines · Ard Biesheuvel
One of the reasons the lack of randomization of the linear map on arm64 is considered problematic is the fact that bootloaders adhering to the original arm64 boot protocol (i.e., a substantial fraction of all Android phones) may place the kernel at the base of DRAM, and therefore at the base of the non-randomized linear map. This puts a writable alias of the kernel's data and bss regions at a predictable location, removing the need for an attacker to guess where KASLR mapped the kernel.
Quoted 6 lines
Quoted 1 line
[...]
It would've been nice to hear from the ppc folks on patch 11, but I've picked it up on the assumption that they'll love the negative diff stat. Worst case, we can drop/revert stuff if they have late objections.
Thanks.
There is a de facto ack from Michael Ellerman in the Link:, which is why I included it.
Note that Sashiko found an issue with KVM+MTE, where a read-only mapping of the zero page in the linear map may result in issues:
"""
Does moving the zero page to .rodata (or unmapping/read-only mapping its linear alias) expose a guest-to-host denial of service with KVM and MTE? When an MTE-enabled KVM guest reads an unmapped memory address, KVM handles the stage-2 fault by mapping the host's shared zero page. KVM will then callsanitise_mte_tags()inarch/arm64/kvm/mmu.c.
Since thePG_mte_taggedflag is never set on the zero page, KVM'stry_page_mte_tagging()succeeds, and it callsmte_clear_page_tags(). This executes the STGM instruction using the zero page's linear map alias. If this alias is read-only or unmapped, won't the STGM instruction trigger a synchronous permission fault or translation fault in EL1, causing a host kernel panic?
"""
Quoted 2 lines
Marc seems to think it is legit, so I came up with the following (I'll send it out separately with another pair of tweaks):
Thanks, it also looks like we're getting some early WARN_ON()s firing in CI from split_kernel_leaf_mapping() after applying your changes:
Will
Quoted 42 lines · Will Deacon
Quoted 41 lines · Ard Biesheuvel
(cc Marc)
Quoted 9 lines · Will Deacon
Quoted 8 lines · Ard Biesheuvel
One of the reasons the lack of randomization of the linear map on arm64 is considered problematic is the fact that bootloaders adhering to the original arm64 boot protocol (i.e., a substantial fraction of all Android phones) may place the kernel at the base of DRAM, and therefore at the base of the non-randomized linear map. This puts a writable alias of the kernel's data and bss regions at a predictable location, removing the need for an attacker to guess where KASLR mapped the kernel.
Quoted 6 lines
Quoted 1 line
[...]
It would've been nice to hear from the ppc folks on patch 11, but I've picked it up on the assumption that they'll love the negative diff stat. Worst case, we can drop/revert stuff if they have late objections.
Thanks.
There is a de facto ack from Michael Ellerman in the Link:, which is why I included it.
Note that Sashiko found an issue with KVM+MTE, where a read-only mapping of the zero page in the linear map may result in issues:
"""
Does moving the zero page to .rodata (or unmapping/read-only mapping its linear alias) expose a guest-to-host denial of service with KVM and MTE? When an MTE-enabled KVM guest reads an unmapped memory address, KVM handles the stage-2 fault by mapping the host's shared zero page. KVM will then callsanitise_mte_tags()inarch/arm64/kvm/mmu.c.
Since thePG_mte_taggedflag is never set on the zero page, KVM'stry_page_mte_tagging()succeeds, and it callsmte_clear_page_tags(). This executes the STGM instruction using the zero page's linear map alias. If this alias is read-only or unmapped, won't the STGM instruction trigger a synchronous permission fault or translation fault in EL1, causing a host kernel panic?
"""
Quoted 8 lines
Quoted 2 lines
Marc seems to think it is legit, so I came up with the following (I'll send it out separately with another pair of tweaks):
Thanks, it also looks like we're getting some early WARN_ON()s firing in CI from
split_kernel_leaf_mapping()after applying your changes:
OK I'll investigate