Skip to content

Commit d512d4a

Browse files
committed
arm64: efi: Execute runtime services from a dedicated stack
jira LE-1907 cve CVE-2023-21102 Rebuild_History Non-Buildable kernel-5.14.0-284.30.1.el9_2 commit-author Ard Biesheuvel <[email protected]> commit ff7a167 With the introduction of PRMT in the ACPI subsystem, the EFI rts workqueue is no longer the only caller of efi_call_virt_pointer() in the kernel. This means the EFI runtime services lock is no longer sufficient to manage concurrent calls into firmware, but also that firmware calls may occur that are not marshalled via the workqueue mechanism, but originate directly from the caller context. For added robustness, and to ensure that the runtime services have 8 KiB of stack space available as per the EFI spec, introduce a spinlock protected EFI runtime stack of 8 KiB, where the spinlock also ensures serialization between the EFI rts workqueue (which itself serializes EFI runtime calls) and other callers of efi_call_virt_pointer(). While at it, use the stack pivot to avoid reloading the shadow call stack pointer from the ordinary stack, as doing so could produce a gadget to defeat it. Signed-off-by: Ard Biesheuvel <[email protected]> (cherry picked from commit ff7a167) Signed-off-by: Jonathan Maple <[email protected]>
1 parent 9effbed commit d512d4a

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

arch/arm64/include/asm/efi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md);
2525
({ \
2626
efi_virtmap_load(); \
2727
__efi_fpsimd_begin(); \
28+
spin_lock(&efi_rt_lock); \
2829
})
2930

3031
#undef arch_efi_call_virt
@@ -33,10 +34,12 @@ int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md);
3334

3435
#define arch_efi_call_virt_teardown() \
3536
({ \
37+
spin_unlock(&efi_rt_lock); \
3638
__efi_fpsimd_end(); \
3739
efi_virtmap_unload(); \
3840
})
3941

42+
extern spinlock_t efi_rt_lock;
4043
efi_status_t __efi_rt_asm_wrapper(void *, const char *, ...);
4144

4245
#define ARCH_EFI_IRQ_FLAGS_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)

arch/arm64/kernel/efi-rt-wrapper.S

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
1616
*/
1717
stp x1, x18, [sp, #16]
1818

19+
ldr_l x16, efi_rt_stack_top
20+
mov sp, x16
21+
#ifdef CONFIG_SHADOW_CALL_STACK
22+
str x18, [sp, #-16]!
23+
#endif
24+
1925
/*
2026
* We are lucky enough that no EFI runtime services take more than
2127
* 5 arguments, so all are passed in registers rather than via the
@@ -29,6 +35,7 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
2935
mov x4, x6
3036
blr x8
3137

38+
mov sp, x29
3239
ldp x1, x2, [sp, #16]
3340
cmp x2, x18
3441
ldp x29, x30, [sp], #32
@@ -42,6 +49,10 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
4249
* called with preemption disabled and a separate shadow stack is used
4350
* for interrupts.
4451
*/
45-
mov x18, x2
52+
#ifdef CONFIG_SHADOW_CALL_STACK
53+
ldr_l x18, efi_rt_stack_top
54+
ldr x18, [x18, #-16]
55+
#endif
56+
4657
b efi_handle_corrupted_x18 // tail call
4758
SYM_FUNC_END(__efi_rt_asm_wrapper)

arch/arm64/kernel/efi.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,30 @@ asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
128128
pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
129129
return s;
130130
}
131+
132+
DEFINE_SPINLOCK(efi_rt_lock);
133+
134+
asmlinkage u64 *efi_rt_stack_top __ro_after_init;
135+
136+
/* EFI requires 8 KiB of stack space for runtime services */
137+
static_assert(THREAD_SIZE >= SZ_8K);
138+
139+
static int __init arm64_efi_rt_init(void)
140+
{
141+
void *p;
142+
143+
if (!efi_enabled(EFI_RUNTIME_SERVICES))
144+
return 0;
145+
146+
p = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, GFP_KERNEL,
147+
NUMA_NO_NODE, &&l);
148+
l: if (!p) {
149+
pr_warn("Failed to allocate EFI runtime stack\n");
150+
clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
151+
return -ENOMEM;
152+
}
153+
154+
efi_rt_stack_top = p + THREAD_SIZE;
155+
return 0;
156+
}
157+
core_initcall(arm64_efi_rt_init);

0 commit comments

Comments
 (0)