Skip to content

Commit a22fc8e

Browse files
ardbiesheuvelwilldeacon
authored andcommitted
arm64: mm: Take potential load offset into account when KASLR is off
We enable CONFIG_RELOCATABLE even when CONFIG_RANDOMIZE_BASE is disabled, and this permits the loader (i.e., EFI) to place the kernel anywhere in physical memory as long as the base address is 64k aligned. This means that the 'KASLR' case described in the header that defines the size of the statically allocated page tables could take effect even when CONFIG_RANDMIZE_BASE=n. So check for CONFIG_RELOCATABLE instead. Signed-off-by: Ard Biesheuvel <[email protected]> Reviewed-by: Anshuman Khandual <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent 3dfdc27 commit a22fc8e

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

arch/arm64/include/asm/kernel-pgtable.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,12 @@
3737

3838

3939
/*
40-
* If KASLR is enabled, then an offset K is added to the kernel address
41-
* space. The bottom 21 bits of this offset are zero to guarantee 2MB
42-
* alignment for PA and VA.
43-
*
44-
* For each pagetable level of the swapper, we know that the shift will
45-
* be larger than 21 (for the 4KB granule case we use section maps thus
46-
* the smallest shift is actually 30) thus there is the possibility that
47-
* KASLR can increase the number of pagetable entries by 1, so we make
48-
* room for this extra entry.
49-
*
50-
* Note KASLR cannot increase the number of required entries for a level
51-
* by more than one because it increments both the virtual start and end
52-
* addresses equally (the extra entry comes from the case where the end
53-
* address is just pushed over a boundary and the start address isn't).
40+
* A relocatable kernel may execute from an address that differs from the one at
41+
* which it was linked. In the worst case, its runtime placement may intersect
42+
* with two adjacent PGDIR entries, which means that an additional page table
43+
* may be needed at each subordinate level.
5444
*/
55-
56-
#ifdef CONFIG_RANDOMIZE_BASE
57-
#define EARLY_KASLR (1)
58-
#else
59-
#define EARLY_KASLR (0)
60-
#endif
45+
#define EXTRA_PAGE __is_defined(CONFIG_RELOCATABLE)
6146

6247
#define SPAN_NR_ENTRIES(vstart, vend, shift) \
6348
((((vend) - 1) >> (shift)) - ((vstart) >> (shift)) + 1)
@@ -83,7 +68,7 @@
8368
+ EARLY_PGDS((vstart), (vend), add) /* each PGDIR needs a next level page table */ \
8469
+ EARLY_PUDS((vstart), (vend), add) /* each PUD needs a next level page table */ \
8570
+ EARLY_PMDS((vstart), (vend), add)) /* each PMD needs a next level page table */
86-
#define INIT_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR, _end, EARLY_KASLR))
71+
#define INIT_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR, _end, EXTRA_PAGE))
8772

8873
/* the initial ID map may need two extra pages if it needs to be extended */
8974
#if VA_BITS < 48

0 commit comments

Comments
 (0)