Skip to content

Commit aacd149

Browse files
ardbiesheuvelwilldeacon
authored andcommitted
arm64: head: avoid relocating the kernel twice for KASLR
Currently, when KASLR is in effect, we set up the kernel virtual address space twice: the first time, the KASLR seed is looked up in the device tree, and the kernel virtual mapping is torn down and recreated again, after which the relocations are applied a second time. The latter step means that statically initialized global pointer variables will be reset to their initial values, and to ensure that BSS variables are not set to values based on the initial translation, they are cleared again as well. All of this is needed because we need the command line (taken from the DT) to tell us whether or not to randomize the virtual address space before entering the kernel proper. However, this code has expanded little by little and now creates global state unrelated to the virtual randomization of the kernel before the mapping is torn down and set up again, and the BSS cleared for a second time. This has created some issues in the past, and it would be better to avoid this little dance if possible. So instead, let's use the temporary mapping of the device tree, and execute the bare minimum of code to decide whether or not KASLR should be enabled, and what the seed is. Only then, create the virtual kernel mapping, clear BSS, etc and proceed as normal. This avoids the issues around inconsistent global state due to BSS being cleared twice, and is generally more maintainable, as it permits us to defer all the remaining DT parsing and KASLR initialization to a later time. This means the relocation fixup code runs only a single time as well, allowing us to simplify the RELR handling code too, which is not idempotent and was therefore required to keep track of the offset that was applied the first time around. Note that this means we have to clone a pair of FDT library objects, so that we can control how they are built - we need the stack protector and other instrumentation disabled so that the code can tolerate being called this early. Note that only the kernel page tables and the temporary stack are mapped read-write at this point, which ensures that the early code does not modify any global state inadvertently. Signed-off-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent fc5a89f commit aacd149

File tree

6 files changed

+171
-140
lines changed

6 files changed

+171
-140
lines changed

arch/arm64/kernel/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ obj-$(CONFIG_ACPI) += acpi.o
5959
obj-$(CONFIG_ACPI_NUMA) += acpi_numa.o
6060
obj-$(CONFIG_ARM64_ACPI_PARKING_PROTOCOL) += acpi_parking_protocol.o
6161
obj-$(CONFIG_PARAVIRT) += paravirt.o
62-
obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
62+
obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o pi/
6363
obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o
6464
obj-$(CONFIG_ELF_CORE) += elfcore.o
6565
obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \

arch/arm64/kernel/head.S

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,13 @@
8686
* x21 primary_entry() .. start_kernel() FDT pointer passed at boot in x0
8787
* x22 create_idmap() .. start_kernel() ID map VA of the DT blob
8888
* x23 primary_entry() .. start_kernel() physical misalignment/KASLR offset
89-
* x24 __primary_switch() .. relocate_kernel() current RELR displacement
89+
* x24 __primary_switch() linear map KASLR seed
9090
* x28 create_idmap() callee preserved temp register
9191
*/
9292
SYM_CODE_START(primary_entry)
9393
bl preserve_boot_args
9494
bl init_kernel_el // w0=cpu_boot_mode
9595
mov x20, x0
96-
adrp x23, __PHYS_OFFSET
97-
and x23, x23, MIN_KIMG_ALIGN - 1 // KASLR offset, defaults to 0
9896
bl create_idmap
9997

10098
/*
@@ -441,23 +439,17 @@ SYM_FUNC_START_LOCAL(__primary_switched)
441439
bl __pi_memset
442440
dsb ishst // Make zero page visible to PTW
443441

442+
#ifdef CONFIG_RANDOMIZE_BASE
443+
adrp x5, memstart_offset_seed // Save KASLR linear map seed
444+
strh w24, [x5, :lo12:memstart_offset_seed]
445+
#endif
444446
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
445447
bl kasan_early_init
446448
#endif
447449
mov x0, x21 // pass FDT address in x0
448450
bl early_fdt_map // Try mapping the FDT early
449451
mov x0, x22 // pass FDT address in x0
450452
bl init_feature_override // Parse cpu feature overrides
451-
#ifdef CONFIG_RANDOMIZE_BASE
452-
tst x23, ~(MIN_KIMG_ALIGN - 1) // already running randomized?
453-
b.ne 0f
454-
bl kaslr_early_init // parse FDT for KASLR options
455-
cbz x0, 0f // KASLR disabled? just proceed
456-
orr x23, x23, x0 // record KASLR offset
457-
ldp x29, x30, [sp], #16 // we must enable KASLR, return
458-
ret // to __primary_switch()
459-
0:
460-
#endif
461453
mov x0, x20
462454
bl switch_to_vhe // Prefer VHE if possible
463455
ldp x29, x30, [sp], #16
@@ -759,27 +751,17 @@ SYM_FUNC_START_LOCAL(__relocate_kernel)
759751
* entry in x9, the address being relocated by the current address or
760752
* bitmap entry in x13 and the address being relocated by the current
761753
* bit in x14.
762-
*
763-
* Because addends are stored in place in the binary, RELR relocations
764-
* cannot be applied idempotently. We use x24 to keep track of the
765-
* currently applied displacement so that we can correctly relocate if
766-
* __relocate_kernel is called twice with non-zero displacements (i.e.
767-
* if there is both a physical misalignment and a KASLR displacement).
768754
*/
769755
adr_l x9, __relr_start
770756
adr_l x10, __relr_end
771757

772-
sub x15, x23, x24 // delta from previous offset
773-
cbz x15, 7f // nothing to do if unchanged
774-
mov x24, x23 // save new offset
775-
776758
2: cmp x9, x10
777759
b.hs 7f
778760
ldr x11, [x9], #8
779761
tbnz x11, #0, 3f // branch to handle bitmaps
780762
add x13, x11, x23
781763
ldr x12, [x13] // relocate address entry
782-
add x12, x12, x15
764+
add x12, x12, x23
783765
str x12, [x13], #8 // adjust to start of bitmap
784766
b 2b
785767

@@ -788,7 +770,7 @@ SYM_FUNC_START_LOCAL(__relocate_kernel)
788770
cbz x11, 6f
789771
tbz x11, #0, 5f // skip bit if not set
790772
ldr x12, [x14] // relocate bit
791-
add x12, x12, x15
773+
add x12, x12, x23
792774
str x12, [x14]
793775

794776
5: add x14, x14, #8 // move to next bit's address
@@ -812,40 +794,27 @@ SYM_FUNC_START_LOCAL(__primary_switch)
812794
adrp x1, reserved_pg_dir
813795
adrp x2, init_idmap_pg_dir
814796
bl __enable_mmu
815-
797+
#ifdef CONFIG_RELOCATABLE
798+
adrp x23, __PHYS_OFFSET
799+
and x23, x23, MIN_KIMG_ALIGN - 1
800+
#ifdef CONFIG_RANDOMIZE_BASE
801+
mov x0, x22
802+
adrp x1, init_pg_end
803+
mov sp, x1
804+
mov x29, xzr
805+
bl __pi_kaslr_early_init
806+
and x24, x0, #SZ_2M - 1 // capture memstart offset seed
807+
bic x0, x0, #SZ_2M - 1
808+
orr x23, x23, x0 // record kernel offset
809+
#endif
810+
#endif
816811
bl clear_page_tables
817812
bl create_kernel_mapping
818813

819814
adrp x1, init_pg_dir
820815
load_ttbr1 x1, x1, x2
821816
#ifdef CONFIG_RELOCATABLE
822-
#ifdef CONFIG_RELR
823-
mov x24, #0 // no RELR displacement yet
824-
#endif
825817
bl __relocate_kernel
826-
#ifdef CONFIG_RANDOMIZE_BASE
827-
ldr x8, =__primary_switched
828-
adrp x0, __PHYS_OFFSET
829-
blr x8
830-
831-
/*
832-
* If we return here, we have a KASLR displacement in x23 which we need
833-
* to take into account by discarding the current kernel mapping and
834-
* creating a new one.
835-
*/
836-
adrp x1, reserved_pg_dir // Disable translations via TTBR1
837-
load_ttbr1 x1, x1, x2
838-
bl clear_page_tables
839-
bl create_kernel_mapping // Recreate kernel mapping
840-
841-
tlbi vmalle1 // Remove any stale TLB entries
842-
dsb nsh
843-
isb
844-
845-
adrp x1, init_pg_dir // Re-enable translations via TTBR1
846-
load_ttbr1 x1, x1, x2
847-
bl __relocate_kernel
848-
#endif
849818
#endif
850819
ldr x8, =__primary_switched
851820
adrp x0, __PHYS_OFFSET

arch/arm64/kernel/image-vars.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ __efistub_dcache_clean_poc = __pi_dcache_clean_poc;
4141
__efistub___memcpy = __pi_memcpy;
4242
__efistub___memmove = __pi_memmove;
4343
__efistub___memset = __pi_memset;
44+
45+
__pi___memcpy = __pi_memcpy;
46+
__pi___memmove = __pi_memmove;
47+
__pi___memset = __pi_memset;
4448
#endif
4549

4650
__efistub__text = _text;

arch/arm64/kernel/kaslr.c

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -23,95 +23,8 @@
2323
u64 __ro_after_init module_alloc_base;
2424
u16 __initdata memstart_offset_seed;
2525

26-
static __init u64 get_kaslr_seed(void *fdt)
27-
{
28-
int node, len;
29-
fdt64_t *prop;
30-
u64 ret;
31-
32-
node = fdt_path_offset(fdt, "/chosen");
33-
if (node < 0)
34-
return 0;
35-
36-
prop = fdt_getprop_w(fdt, node, "kaslr-seed", &len);
37-
if (!prop || len != sizeof(u64))
38-
return 0;
39-
40-
ret = fdt64_to_cpu(*prop);
41-
*prop = 0;
42-
return ret;
43-
}
44-
4526
struct arm64_ftr_override kaslr_feature_override __initdata;
4627

47-
/*
48-
* This routine will be executed with the kernel mapped at its default virtual
49-
* address, and if it returns successfully, the kernel will be remapped, and
50-
* start_kernel() will be executed from a randomized virtual offset. The
51-
* relocation will result in all absolute references (e.g., static variables
52-
* containing function pointers) to be reinitialized, and zero-initialized
53-
* .bss variables will be reset to 0.
54-
*/
55-
u64 __init kaslr_early_init(void)
56-
{
57-
void *fdt;
58-
u64 seed, offset, mask;
59-
unsigned long raw;
60-
61-
/*
62-
* Try to map the FDT early. If this fails, we simply bail,
63-
* and proceed with KASLR disabled. We will make another
64-
* attempt at mapping the FDT in setup_machine()
65-
*/
66-
fdt = get_early_fdt_ptr();
67-
if (!fdt) {
68-
return 0;
69-
}
70-
71-
/*
72-
* Retrieve (and wipe) the seed from the FDT
73-
*/
74-
seed = get_kaslr_seed(fdt);
75-
76-
/*
77-
* Check if 'nokaslr' appears on the command line, and
78-
* return 0 if that is the case.
79-
*/
80-
if (kaslr_feature_override.val & kaslr_feature_override.mask & 0xf) {
81-
return 0;
82-
}
83-
84-
/*
85-
* Mix in any entropy obtainable architecturally if enabled
86-
* and supported.
87-
*/
88-
89-
if (arch_get_random_seed_long_early(&raw))
90-
seed ^= raw;
91-
92-
if (!seed) {
93-
return 0;
94-
}
95-
96-
/*
97-
* OK, so we are proceeding with KASLR enabled. Calculate a suitable
98-
* kernel image offset from the seed. Let's place the kernel in the
99-
* middle half of the VMALLOC area (VA_BITS_MIN - 2), and stay clear of
100-
* the lower and upper quarters to avoid colliding with other
101-
* allocations.
102-
* Even if we could randomize at page granularity for 16k and 64k pages,
103-
* let's always round to 2 MB so we don't interfere with the ability to
104-
* map using contiguous PTEs
105-
*/
106-
mask = ((1UL << (VA_BITS_MIN - 2)) - 1) & ~(SZ_2M - 1);
107-
offset = BIT(VA_BITS_MIN - 3) + (seed & mask);
108-
109-
/* use the top 16 bits to randomize the linear region */
110-
memstart_offset_seed = seed >> 48;
111-
112-
return offset;
113-
}
114-
11528
static int __init kaslr_init(void)
11629
{
11730
u64 module_range;

arch/arm64/kernel/pi/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# Copyright 2022 Google LLC
3+
4+
KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \
5+
-Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
6+
$(call cc-option,-mbranch-protection=none) \
7+
-I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
8+
-include $(srctree)/include/linux/hidden.h \
9+
-D__DISABLE_EXPORTS -ffreestanding -D__NO_FORTIFY \
10+
$(call cc-option,-fno-addrsig)
11+
12+
# remove SCS flags from all objects in this directory
13+
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
14+
# disable LTO
15+
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
16+
17+
GCOV_PROFILE := n
18+
KASAN_SANITIZE := n
19+
KCSAN_SANITIZE := n
20+
UBSAN_SANITIZE := n
21+
KCOV_INSTRUMENT := n
22+
23+
$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ \
24+
--remove-section=.note.gnu.property \
25+
--prefix-alloc-sections=.init
26+
$(obj)/%.pi.o: $(obj)/%.o FORCE
27+
$(call if_changed,objcopy)
28+
29+
$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
30+
$(call if_changed_rule,cc_o_c)
31+
32+
obj-y := kaslr_early.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
33+
extra-y := $(patsubst %.pi.o,%.o,$(obj-y))

0 commit comments

Comments
 (0)