Skip to content

Commit 5287569

Browse files
samitolvanenwilldeacon
authored andcommitted
arm64: Implement Shadow Call Stack
This change implements shadow stack switching, initial SCS set-up, and interrupt shadow stacks for arm64. Signed-off-by: Sami Tolvanen <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent 9654736 commit 5287569

File tree

9 files changed

+114
-2
lines changed

9 files changed

+114
-2
lines changed

arch/arm64/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ config ARM64
6464
select ARCH_USE_QUEUED_RWLOCKS
6565
select ARCH_USE_QUEUED_SPINLOCKS
6666
select ARCH_SUPPORTS_MEMORY_FAILURE
67+
select ARCH_SUPPORTS_SHADOW_CALL_STACK if CC_HAVE_SHADOW_CALL_STACK
6768
select ARCH_SUPPORTS_ATOMIC_RMW
6869
select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 && (GCC_VERSION >= 50000 || CC_IS_CLANG)
6970
select ARCH_SUPPORTS_NUMA_BALANCING
@@ -1025,6 +1026,10 @@ config ARCH_HAS_CACHE_LINE_SIZE
10251026
config ARCH_ENABLE_SPLIT_PMD_PTLOCK
10261027
def_bool y if PGTABLE_LEVELS > 2
10271028

1029+
# Supported by clang >= 7.0
1030+
config CC_HAVE_SHADOW_CALL_STACK
1031+
def_bool $(cc-option, -fsanitize=shadow-call-stack -ffixed-x18)
1032+
10281033
config SECCOMP
10291034
bool "Enable seccomp to safely compute untrusted bytecode"
10301035
---help---

arch/arm64/include/asm/scs.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_SCS_H
3+
#define _ASM_SCS_H
4+
5+
#ifdef __ASSEMBLY__
6+
7+
#ifdef CONFIG_SHADOW_CALL_STACK
8+
.macro scs_load tsk, tmp
9+
ldp x18, \tmp, [\tsk, #TSK_TI_SCS_BASE]
10+
add x18, x18, \tmp
11+
.endm
12+
13+
.macro scs_save tsk, tmp
14+
ldr \tmp, [\tsk, #TSK_TI_SCS_BASE]
15+
sub \tmp, x18, \tmp
16+
str \tmp, [\tsk, #TSK_TI_SCS_OFFSET]
17+
.endm
18+
#else
19+
.macro scs_load tsk, tmp
20+
.endm
21+
22+
.macro scs_save tsk, tmp
23+
.endm
24+
#endif /* CONFIG_SHADOW_CALL_STACK */
25+
26+
#else /* __ASSEMBLY__ */
27+
28+
#include <linux/scs.h>
29+
30+
#ifdef CONFIG_SHADOW_CALL_STACK
31+
32+
static inline void scs_overflow_check(struct task_struct *tsk)
33+
{
34+
if (unlikely(scs_corrupted(tsk)))
35+
panic("corrupted shadow stack detected inside scheduler\n");
36+
}
37+
38+
#else /* CONFIG_SHADOW_CALL_STACK */
39+
40+
static inline void scs_overflow_check(struct task_struct *tsk) {}
41+
42+
#endif /* CONFIG_SHADOW_CALL_STACK */
43+
44+
#endif /* __ASSEMBLY __ */
45+
46+
#endif /* _ASM_SCS_H */

arch/arm64/include/asm/thread_info.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ struct thread_info {
4141
#endif
4242
} preempt;
4343
};
44+
#ifdef CONFIG_SHADOW_CALL_STACK
45+
void *scs_base;
46+
unsigned long scs_offset;
47+
#endif
4448
};
4549

4650
#define thread_saved_pc(tsk) \
@@ -100,11 +104,20 @@ void arch_release_task_struct(struct task_struct *tsk);
100104
_TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
101105
_TIF_SYSCALL_EMU)
102106

107+
#ifdef CONFIG_SHADOW_CALL_STACK
108+
#define INIT_SCS \
109+
.scs_base = init_shadow_call_stack, \
110+
.scs_offset = 0,
111+
#else
112+
#define INIT_SCS
113+
#endif
114+
103115
#define INIT_THREAD_INFO(tsk) \
104116
{ \
105117
.flags = _TIF_FOREIGN_FPSTATE, \
106118
.preempt_count = INIT_PREEMPT_COUNT, \
107119
.addr_limit = KERNEL_DS, \
120+
INIT_SCS \
108121
}
109122

110123
#endif /* __ASM_THREAD_INFO_H */

arch/arm64/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ obj-$(CONFIG_CRASH_CORE) += crash_core.o
6363
obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o
6464
obj-$(CONFIG_ARM64_SSBD) += ssbd.o
6565
obj-$(CONFIG_ARM64_PTR_AUTH) += pointer_auth.o
66+
obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o
6667

6768
obj-y += vdso/ probes/
6869
obj-$(CONFIG_COMPAT_VDSO) += vdso32/

arch/arm64/kernel/asm-offsets.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ int main(void)
3333
DEFINE(TSK_TI_ADDR_LIMIT, offsetof(struct task_struct, thread_info.addr_limit));
3434
#ifdef CONFIG_ARM64_SW_TTBR0_PAN
3535
DEFINE(TSK_TI_TTBR0, offsetof(struct task_struct, thread_info.ttbr0));
36+
#endif
37+
#ifdef CONFIG_SHADOW_CALL_STACK
38+
DEFINE(TSK_TI_SCS_BASE, offsetof(struct task_struct, thread_info.scs_base));
39+
DEFINE(TSK_TI_SCS_OFFSET, offsetof(struct task_struct, thread_info.scs_offset));
3640
#endif
3741
DEFINE(TSK_STACK, offsetof(struct task_struct, stack));
3842
#ifdef CONFIG_STACKPROTECTOR

arch/arm64/kernel/entry.S

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <asm/mmu.h>
2424
#include <asm/processor.h>
2525
#include <asm/ptrace.h>
26+
#include <asm/scs.h>
2627
#include <asm/thread_info.h>
2728
#include <asm/asm-uaccess.h>
2829
#include <asm/unistd.h>
@@ -179,6 +180,8 @@ alternative_cb_end
179180
apply_ssbd 1, x22, x23
180181

181182
ptrauth_keys_install_kernel tsk, 1, x20, x22, x23
183+
184+
scs_load tsk, x20
182185
.else
183186
add x21, sp, #S_FRAME_SIZE
184187
get_current_task tsk
@@ -343,6 +346,8 @@ alternative_else_nop_endif
343346
msr cntkctl_el1, x1
344347
4:
345348
#endif
349+
scs_save tsk, x0
350+
346351
/* No kernel C function calls after this as user keys are set. */
347352
ptrauth_keys_install_user tsk, x0, x1, x2
348353

@@ -388,6 +393,9 @@ alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
388393

389394
.macro irq_stack_entry
390395
mov x19, sp // preserve the original sp
396+
#ifdef CONFIG_SHADOW_CALL_STACK
397+
mov x24, x18 // preserve the original shadow stack
398+
#endif
391399

392400
/*
393401
* Compare sp with the base of the task stack.
@@ -405,15 +413,25 @@ alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
405413

406414
/* switch to the irq stack */
407415
mov sp, x26
416+
417+
#ifdef CONFIG_SHADOW_CALL_STACK
418+
/* also switch to the irq shadow stack */
419+
adr_this_cpu x18, irq_shadow_call_stack, x26
420+
#endif
421+
408422
9998:
409423
.endm
410424

411425
/*
412-
* x19 should be preserved between irq_stack_entry and
413-
* irq_stack_exit.
426+
* The callee-saved regs (x19-x29) should be preserved between
427+
* irq_stack_entry and irq_stack_exit, but note that kernel_entry
428+
* uses x20-x23 to store data for later use.
414429
*/
415430
.macro irq_stack_exit
416431
mov sp, x19
432+
#ifdef CONFIG_SHADOW_CALL_STACK
433+
mov x18, x24
434+
#endif
417435
.endm
418436

419437
/* GPRs used by entry code */
@@ -901,6 +919,8 @@ SYM_FUNC_START(cpu_switch_to)
901919
mov sp, x9
902920
msr sp_el0, x1
903921
ptrauth_keys_install_kernel x1, 1, x8, x9, x10
922+
scs_save x0, x8
923+
scs_load x1, x8
904924
ret
905925
SYM_FUNC_END(cpu_switch_to)
906926
NOKPROBE(cpu_switch_to)

arch/arm64/kernel/head.S

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <asm/pgtable-hwdef.h>
2828
#include <asm/pgtable.h>
2929
#include <asm/page.h>
30+
#include <asm/scs.h>
3031
#include <asm/smp.h>
3132
#include <asm/sysreg.h>
3233
#include <asm/thread_info.h>
@@ -424,6 +425,10 @@ SYM_FUNC_START_LOCAL(__primary_switched)
424425
stp xzr, x30, [sp, #-16]!
425426
mov x29, sp
426427

428+
#ifdef CONFIG_SHADOW_CALL_STACK
429+
adr_l x18, init_shadow_call_stack // Set shadow call stack
430+
#endif
431+
427432
str_l x21, __fdt_pointer, x5 // Save FDT pointer
428433

429434
ldr_l x4, kimage_vaddr // Save the offset between
@@ -737,6 +742,7 @@ SYM_FUNC_START_LOCAL(__secondary_switched)
737742
ldr x2, [x0, #CPU_BOOT_TASK]
738743
cbz x2, __secondary_too_slow
739744
msr sp_el0, x2
745+
scs_load x2, x3
740746
mov x29, #0
741747
mov x30, #0
742748
b secondary_start_kernel

arch/arm64/kernel/process.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <asm/mmu_context.h>
5353
#include <asm/processor.h>
5454
#include <asm/pointer_auth.h>
55+
#include <asm/scs.h>
5556
#include <asm/stacktrace.h>
5657

5758
#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
@@ -515,6 +516,7 @@ __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
515516
entry_task_switch(next);
516517
uao_thread_switch(next);
517518
ssbs_thread_switch(next);
519+
scs_overflow_check(next);
518520

519521
/*
520522
* Complete any pending TLB or cache maintenance on this CPU in case

arch/arm64/kernel/scs.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Shadow Call Stack support.
4+
*
5+
* Copyright (C) 2019 Google LLC
6+
*/
7+
8+
#include <linux/percpu.h>
9+
#include <asm/scs.h>
10+
11+
/* Allocate a static per-CPU shadow stack */
12+
#define DEFINE_SCS(name) \
13+
DEFINE_PER_CPU(unsigned long [SCS_SIZE/sizeof(long)], name) \
14+
15+
DEFINE_SCS(irq_shadow_call_stack);

0 commit comments

Comments
 (0)