scs: switch to vmapped shadow stacks
cover scs: switch to vmapped shadow stacks
As discussed a few months ago [1][2], virtually mapped shadow call stacks are better for safety and robustness. This series dusts off the VMAP option from the original SCS patch series and switches the kernel to use virtually mapped shadow stacks unconditionally when SCS is enabled.
[1] https://lore.kernel.org/lkml/20200515172355.GD23334@willie-the-truck/
[2] https://lore.kernel.org/lkml/[email protected]/
Sami Tolvanen (2):
scs: switch to vmapped shadow stacks
arm64: scs: use vmapped IRQ and SDEI shadow stacks
arch/arm64/include/asm/scs.h | 21 ++++++++++- arch/arm64/kernel/entry.S | 6 ++-- arch/arm64/kernel/irq.c | 2 ++ arch/arm64/kernel/scs.c | 67 +++++++++++++++++++++++++++++++++--- arch/arm64/kernel/sdei.c | 7 ++++ include/linux/scs.h | 15 +++----- kernel/scs.c | 67 ++++++++++++++++++++++++++++++------ 7 files changed, 156 insertions(+), 29 deletions(-)
base-commit: 96485e4462604744d66bf4301557d996d80b85eb
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
PATCH 1/2 scs: switch to vmapped shadow stacks
The kernel currently uses kmem_cache to allocate shadow call stacks, which means an overflow may not be immediately detected and can potentially result in another task's shadow stack to be overwritten.
This change switches SCS to use virtually mapped shadow stacks, which increases shadow stack size to a full page and provides more robust overflow detection similarly to VMAP_STACK.
--- include/linux/scs.h | 7 +---- kernel/scs.c | 63 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 15 deletions(-)
include/linux/scs.h +1 -6
@@ -15,12 +15,7 @@#ifdef CONFIG_SHADOW_CALL_STACK -/* -* In testing, 1 KiB shadow stack size (i.e. 128 stack frames on a 64-bit -* architecture) provided ~40% safety margin on stack usage while keeping -* memory allocation overhead reasonable. -*/ -#define SCS_SIZE SZ_1K +#define SCS_SIZE PAGE_SIZE#define GFP_SCS (GFP_KERNEL | __GFP_ZERO)/* An illegal pointer value to mark the end of the shadow stack. */
kernel/scs.c +54 -9
104 lines not on this page · raw message
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
PATCH 2/2 arm64: scs: use vmapped IRQ and SDEI shadow stacks
In reply to Sami TolvanenUse scs_alloc() to allocate also IRQ and SDEI shadow stacks instead of using statically allocated stacks.
--- arch/arm64/include/asm/scs.h | 21 ++++++++++- arch/arm64/kernel/entry.S | 6 ++-- arch/arm64/kernel/irq.c | 2 ++ arch/arm64/kernel/scs.c | 67 +++++++++++++++++++++++++++++++++--- arch/arm64/kernel/sdei.c | 7 ++++ include/linux/scs.h | 8 ++--- kernel/scs.c | 4 +-- 7 files changed, 101 insertions(+), 14 deletions(-)
arch/arm64/include/asm/scs.h +20 -1
@@ -24,6 +24,25 @@ .endm#endif /* CONFIG_SHADOW_CALL_STACK */ -#endif /* __ASSEMBLY __ */ +#else /* __ASSEMBLY__ */ + +#include <linux/scs.h> + +#ifdef CONFIG_SHADOW_CALL_STACK + +extern void scs_init_irq (void ); + +extern void scs_free_sdei (void ); +extern int scs_init_sdei (void ); + +#else + +static inline void scs_init_irq (void ) {} +static inline void scs_free_sdei (void ) {} +static inline int scs_init_sdei (void ) {return -EOPNOTSUPP ; } + +#endif + +#endif /* __ASSEMBLY__ */ #endif /* _ASM_SCS_H */
arch/arm64/kernel/entry.S +3 -3
@@ -438,7 +438,7 @@ SYM_CODE_END(__swpan_exit_el0) #ifdef CONFIG_SHADOW_CALL_STACK /* also switch to the irq shadow stack */ - adr_this_cpu scs_sp, irq_shadow_call_stack, x26 + ldr_this_cpu scs_sp, irq_shadow_call_stack_ptr, x26 #endif 9998:
@@ -1094,9 +1094,9 @@ SYM_CODE_START(__sdei_asm_handler) #ifdef CONFIG_SHADOW_CALL_STACK /* Use a separate shadow call stack for normal and critical events */ cbnz w4, 3f - adr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal, tmp=x6 + ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal_ptr, tmp=x6 b 4f -3: adr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical, tmp=x6 +3: ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical_ptr, tmp=x6 4: #endif
arch/arm64/kernel/irq.c +2 -0
@@ -20,6 +20,7 @@#include <linux/seq_file.h> #include <linux/vmalloc.h> #include <asm/daifflags.h> +#include <asm/scs.h> #include <asm/vmap_stack.h> /* Only access this in an NMI enter/exit */
@@ -54,6 +55,7 @@ static void init_irq_stacks(void)void __init init_IRQ (void ) {init_irq_stacks (); +scs_init_irq ();irqchip_init ();if (!handle_arch_irq)panic ("No interrupt controller found." );
arch/arm64/kernel/scs.c +63 -4
74 lines not on this page · raw message
arch/arm64/kernel/sdei.c +7 -0
@@ -13,6 +13,7 @@#include <asm/kprobes.h> #include <asm/mmu.h> #include <asm/ptrace.h> +#include <asm/scs.h> #include <asm/sections.h> #include <asm/stacktrace.h> #include <asm/sysreg.h>
@@ -138,6 +139,12 @@ unsigned long sdei_arch_get_entry_point(int conduit)return 0 ; } +if (scs_init_sdei ()) { +if (IS_ENABLED (CONFIG_VMAP_STACK )) +free_sdei_stacks (); +return 0 ; + } + sdei_exit_mode = (conduit ==SMCCC_CONDUIT_HVC ) ?SDEI_EXIT_HVC :SDEI_EXIT_SMC ;#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
include/linux/scs.h +4 -4
@@ -21,13 +21,11 @@/* An illegal pointer value to mark the end of the shadow stack. */ #define SCS_END_MAGIC (0x5f6UL + POISON_POINTER_DELTA) -/* Allocate a static per-CPU shadow stack */ -#define DEFINE_SCS (name) \ - DEFINE_PER_CPU(unsigned long [SCS_SIZE/sizeof(long)], name) \ -#define task_scs (tsk) (task_thread_info(tsk)->scs_base)#define task_scs_sp (tsk) (task_thread_info(tsk)->scs_sp) +void *scs_alloc (int node); +void scs_free (void *s);void scs_init (void );int scs_prepare (struct task_struct *tsk,int node);void scs_release (struct task_struct *tsk);
@@ -56,6 +54,8 @@ static inline bool task_scs_end_corrupted(struct task_struct *tsk)#else /* CONFIG_SHADOW_CALL_STACK */ +static inline void *scs_alloc (int node) {return NULL ; } +static inline void scs_free (void *s) {}static inline void scs_init (void ) {}static inline void scs_task_reset (struct task_struct *tsk) {}static inline int scs_prepare (struct task_struct *tsk,int node) {return 0 ; }
kernel/scs.c +2 -2
@@ -24,7 +24,7 @@ static void __scs_account(void *s, int account)#define NR_CACHED_SCS 2 static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]); -static void *scs_alloc (int node) +void *scs_alloc (int node) {int i;void *s;
@@ -63,7 +63,7 @@ static void *scs_alloc(int node)return s; } -static void scs_free (void *s) +void scs_free (void *s) {int i;
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
Quoted 9 lines · Sami Tolvanen
The kernel currently uses
kmem_cacheto allocate shadow call stacks, which means an overflow may not be immediately detected and can potentially result in another task's shadow stack to be overwritten.This change switches SCS to use virtually mapped shadow stacks, which increases shadow stack size to a full page and provides more robust overflow detection similarly to
VMAP_STACK.Signed-off-by Sami Tolvanen <[email protected]>
Thanks! I much prefer this to kmem. :)
Signature · 1 line
Kees Cook
Re: [PATCH 2/2] arm64: scs: use vmapped IRQ and SDEI shadow stacks
In reply to Sami TolvanenQuoted 4 lines · Sami Tolvanen
Use
scs_alloc()to allocate also IRQ and SDEI shadow stacks instead of using statically allocated stacks.Signed-off-by Sami Tolvanen <[email protected]>
Signature · 1 line
Kees Cook
Catalin Marinas
maintainerQuoted 5 lines · Sami Tolvanen
As discussed a few months ago [1][2], virtually mapped shadow call stacks are better for safety and robustness. This series dusts off the VMAP option from the original SCS patch series and switches the kernel to use virtually mapped shadow stacks unconditionally when SCS is enabled.
Quoted 7 lines
[1] https://lore.kernel.org/lkml/20200515172355.GD23334@willie-the-truck/
[2] https://lore.kernel.org/lkml/[email protected]/Sami Tolvanen (2):
scs: switch to vmapped shadow stacks
arm64: scs: use vmapped IRQ and SDEI shadow stacks
Will, Mark. Any objections to this series? If not, I can queue it for 5.11 via the arm64 tree.
Thanks.
Signature · 1 line
Catalin
Will Deacon
maintainerQuoted 10 lines · Catalin Marinas
Quoted 9 lines · Sami Tolvanen
As discussed a few months ago [1][2], virtually mapped shadow call stacks are better for safety and robustness. This series dusts off the VMAP option from the original SCS patch series and switches the kernel to use virtually mapped shadow stacks unconditionally when SCS is enabled.
[1] https://lore.kernel.org/lkml/20200515172355.GD23334@willie-the-truck/
[2] https://lore.kernel.org/lkml/[email protected]/
Quoted 6 lines
Quoted 3 lines
Sami Tolvanen (2):
scs: switch to vmapped shadow stacks
arm64: scs: use vmapped IRQ and SDEI shadow stacksWill, Mark. Any objections to this series? If not, I can queue it for 5.11 via the arm64 tree.
It's on my list to review, but I have a tonne of other things I have to get to first.
Will
Will Deacon
maintainerHi Sami,
Quoted 8 lines · Sami Tolvanen
The kernel currently uses
kmem_cacheto allocate shadow call stacks, which means an overflow may not be immediately detected and can potentially result in another task's shadow stack to be overwritten.This change switches SCS to use virtually mapped shadow stacks, which increases shadow stack size to a full page and provides more robust overflow detection similarly to
VMAP_STACK.
Quoted 5 lines
Signed-off-by Sami Tolvanen <[email protected]>--- include/linux/scs.h | 7 +---- kernel/scs.c | 63 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 15 deletions(-)
Cheers for posting this. I _much_ prefer handling the SCS this way, but I have some comments on the implementation below.
Quoted 5 lines
include/linux/scs.h +0
-0
include/linux/scs.h line 18
Quoted 10 lines
include/linux/scs.h +1
-6@@ -15,12 +15,7 @@#ifdef CONFIG_SHADOW_CALL_STACK -/* -* In testing, 1 KiB shadow stack size (i.e. 128 stack frames on a 64-bit -* architecture) provided ~40% safety margin on stack usage while keeping -* memory allocation overhead reasonable. -*/ -#define SCS_SIZE SZ_1K +#define SCS_SIZE PAGE_SIZE
We could make this SCS_ORDER and then forget about alignment etc.
Quoted 43 lines
include/linux/scs.h +0
-0#define GFP_SCS (GFP_KERNEL | __GFP_ZERO)/* An illegal pointer value to mark the end of the shadow stack. */ kernel/scs.c +13
-5@@ -5,50 +5,95 @@* Copyright (C) 2019 Google LLC */ +#include <linux/cpuhotplug.h> #include <linux/kasan.h> #include <linux/mm.h> #include <linux/scs.h> -#include <linux/slab.h> +#include <linux/vmalloc.h> #include <linux/vmstat.h> -static struct kmem_cache *scs_cache; -static void __scs_account (void *s,int account) { -struct page *scs_page =virt_to_page (s); +struct page *scs_page =vmalloc_to_page (s);mod_node_page_state (page_pgdat (scs_page),NR_KERNEL_SCS_KB , account * (SCS_SIZE /SZ_1K )); } +/* Matches NR_CACHED_STACKS for VMAP_STACK */ +#define NR_CACHED_SCS 2 +static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]); +static void *scs_alloc (int node) { -void *s =kmem_cache_alloc_node (scs_cache,GFP_SCS , node); +int i; +void *s; + +for (i =0 ; i <NR_CACHED_SCS ; i++) { + s =this_cpu_xchg (scs_cache[i],NULL ); +if (s) {
kernel/scs.c line 44
Quoted 10 lines
kernel/scs.c +10
-0+memset (s,0 ,SCS_SIZE ); + goto out; + } + } + +/* +* We allocate a full page for the shadow stack, which should be +* more than we need. Check the assumption nevertheless. +*/ +BUILD_BUG_ON (SCS_SIZE >PAGE_SIZE );i
With SCS_ORDER, you can drop this.
kernel/scs.c line 49
Quoted 5 lines
kernel/scs.c +5
-0+ + s =__vmalloc_node_range (PAGE_SIZE ,SCS_SIZE , +VMALLOC_START ,VMALLOC_END , +GFP_SCS ,PAGE_KERNEL ,0 , + node,__builtin_return_address (0 ));
Do we actually need vmalloc here? If we used alloc_pages() + vmap() instead, then we could avoid the expensive call to vmalloc_to_page() in __scs_account().
Quoted 15 lines
kernel/scs.c +2
-1if (!s)return NULL ; +out: *__scs_magic (s) =SCS_END_MAGIC ;/* * Poison the allocation to catch unintentional accesses to * the shadow stack when KASAN is enabled. */ -kasan_poison_object_data (scs_cache, s); +kasan_poison_vmalloc (s,SCS_SIZE );__scs_account (s,1 );return s;
kernel/scs.c line 71
Quoted 10 lines
kernel/scs.c +3
-2}static void scs_free (void *s) { +int i; +__scs_account (s,-1 ); -kasan_unpoison_object_data (scs_cache, s); -kmem_cache_free (scs_cache, s); +kasan_unpoison_vmalloc (s,SCS_SIZE );
I don't see the point in unpoisoning here tbh; vfree_atomic() re-poisons almost immediately, so we should probably defer this to scs_alloc() and only when picking the stack out of the cache.
kernel/scs.c line 72
Quoted 2 lines
kernel/scs.c +2
-0+ +for (i =0 ; i <NR_CACHED_SCS ; i++)
Can you add a comment about the re-entrancy here and why we're using this_cpu_cmpxchg() please?
Tnanks,
Will
Will Deacon
maintainerRe: [PATCH 2/2] arm64: scs: use vmapped IRQ and SDEI shadow stacks
In reply to Sami TolvanenQuoted 22 lines · Sami Tolvanen
Use
scs_alloc()to allocate also IRQ and SDEI shadow stacks instead of using statically allocated stacks.Signed-off-by Sami Tolvanen <[email protected]>--- arch/arm64/include/asm/scs.h | 21 ++++++++++- arch/arm64/kernel/entry.S | 6 ++-- arch/arm64/kernel/irq.c | 2 ++ arch/arm64/kernel/scs.c | 67 +++++++++++++++++++++++++++++++++--- arch/arm64/kernel/sdei.c | 7 ++++ include/linux/scs.h | 8 ++--- kernel/scs.c | 4 +-- 7 files changed, 101 insertions(+), 14 deletions(-)arch/arm64/include/asm/scs.h +0
-0@@ -24,6 +24,25 @@ .endm#endif /* CONFIG_SHADOW_CALL_STACK */
arch/arm64/include/asm/scs.h line 35
Quoted 10 lines
arch/arm64/include/asm/scs.h +9
-1-#endif /* __ASSEMBLY __ */ +#else /* __ASSEMBLY__ */ + +#include <linux/scs.h> + +#ifdef CONFIG_SHADOW_CALL_STACK + +extern void scs_init_irq (void ); + +extern void scs_free_sdei (void );
This is only called on the scs_init_sdei() failure path, so it can be static. But see below, because I think we should move all of these functions out of scs.c anyway.
Quoted 13 lines
arch/arm64/kernel/irq.c +1
-0@@ -20,6 +20,7 @@#include <linux/seq_file.h> #include <linux/vmalloc.h> #include <asm/daifflags.h> +#include <asm/scs.h> #include <asm/vmap_stack.h> /* Only access this in an NMI enter/exit */
arch/arm64/kernel/irq.c static void init_irq_stacks(void) · line 58
Quoted 4 lines
arch/arm64/kernel/irq.c +1
-0@@ -54,6 +55,7 @@ static void init_irq_stacks(void)void __init init_IRQ (void ) {init_irq_stacks (); +scs_init_irq ();
If we internalise this in init_irq_stacks()...
Quoted 13 lines
arch/arm64/kernel/sdei.c +1
-0@@ -13,6 +13,7 @@#include <asm/kprobes.h> #include <asm/mmu.h> #include <asm/ptrace.h> +#include <asm/scs.h> #include <asm/sections.h> #include <asm/stacktrace.h> #include <asm/sysreg.h>
arch/arm64/kernel/sdei.c unsigned long sdei_arch_get_entry_point(int conduit) · line 142
Quoted 4 lines
arch/arm64/kernel/sdei.c +1
-0@@ -138,6 +139,12 @@ unsigned long sdei_arch_get_entry_point(int conduit)return 0 ; } +if (scs_init_sdei ()) {
... and this in init_sdei_stacks(), then I think we remove all of the code from arch/arm64/kernel/scs.c.
Quoted 43 lines
include/linux/scs.h +4
-4@@ -21,13 +21,11 @@/* An illegal pointer value to mark the end of the shadow stack. */ #define SCS_END_MAGIC (0x5f6UL + POISON_POINTER_DELTA) -/* Allocate a static per-CPU shadow stack */ -#define DEFINE_SCS (name) \ - DEFINE_PER_CPU(unsigned long [SCS_SIZE/sizeof(long)], name) \ -#define task_scs (tsk) (task_thread_info(tsk)->scs_base)#define task_scs_sp (tsk) (task_thread_info(tsk)->scs_sp) +void *scs_alloc (int node); +void scs_free (void *s);void scs_init (void );int scs_prepare (struct task_struct *tsk,int node);void scs_release (struct task_struct *tsk);@@ -56,6 +54,8 @@ static inline bool task_scs_end_corrupted(struct task_struct *tsk)#else /* CONFIG_SHADOW_CALL_STACK */ +static inline void *scs_alloc (int node) {return NULL ; } +static inline void scs_free (void *s) {}static inline void scs_init (void ) {}static inline void scs_task_reset (struct task_struct *tsk) {}static inline int scs_prepare (struct task_struct *tsk,int node) {return 0 ; }kernel/scs.c +1
-1@@ -24,7 +24,7 @@ static void __scs_account(void *s, int account)#define NR_CACHED_SCS 2 static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]); -static void *scs_alloc (int node) +void *scs_alloc (int node) {int i;void *s;
kernel/scs.c static void *scs_alloc(int node) · line 66
Quoted 5 lines
kernel/scs.c +1
-1@@ -63,7 +63,7 @@ static void *scs_alloc(int node)return s; } -static void scs_free (void *s) +void scs_free (void *s)
Should be part of the first patch?
Will
Quoted 29 lines · Will Deacon
Hi Sami,
Quoted 8 lines · Sami Tolvanen
The kernel currently uses
kmem_cacheto allocate shadow call stacks, which means an overflow may not be immediately detected and can potentially result in another task's shadow stack to be overwritten.This change switches SCS to use virtually mapped shadow stacks, which increases shadow stack size to a full page and provides more robust overflow detection similarly to
VMAP_STACK.Quoted 5 lines
Signed-off-by Sami Tolvanen <[email protected]>--- include/linux/scs.h | 7 +---- kernel/scs.c | 63 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 15 deletions(-)Cheers for posting this. I _much_ prefer handling the SCS this way, but I have some comments on the implementation below.
Quoted 8 lines
include/linux/scs.h +0
-0@@ -15,12 +15,7 @@#ifdef CONFIG_SHADOW_CALL_STACK
Quoted 9 lines
include/linux/scs.h line 18
Quoted 7 lines
include/linux/scs.h +1
-6-/* -* In testing, 1 KiB shadow stack size (i.e. 128 stack frames on a 64-bit -* architecture) provided ~40% safety margin on stack usage while keeping -* memory allocation overhead reasonable. -*/ -#define SCS_SIZE SZ_1K +#define SCS_SIZE PAGE_SIZEWe could make this
SCS_ORDERand then forget about alignment etc.
It's still convenient to have SCS_SIZE defined, I think. I can certainly define SCS_ORDER and use that to define SCS_SIZE, but do you think we'll need an order >0 here at some point in future?
Quoted 56 lines
Quoted 43 lines
include/linux/scs.h +0
-0#define GFP_SCS (GFP_KERNEL | __GFP_ZERO)/* An illegal pointer value to mark the end of the shadow stack. */ kernel/scs.c +13
-5@@ -5,50 +5,95 @@* Copyright (C) 2019 Google LLC */ +#include <linux/cpuhotplug.h> #include <linux/kasan.h> #include <linux/mm.h> #include <linux/scs.h> -#include <linux/slab.h> +#include <linux/vmalloc.h> #include <linux/vmstat.h> -static struct kmem_cache *scs_cache; -static void __scs_account (void *s,int account) { -struct page *scs_page =virt_to_page (s); +struct page *scs_page =vmalloc_to_page (s);mod_node_page_state (page_pgdat (scs_page),NR_KERNEL_SCS_KB , account * (SCS_SIZE /SZ_1K )); } +/* Matches NR_CACHED_STACKS for VMAP_STACK */ +#define NR_CACHED_SCS 2 +static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]); +static void *scs_alloc (int node) { -void *s =kmem_cache_alloc_node (scs_cache,GFP_SCS , node); +int i; +void *s; + +for (i =0 ; i <NR_CACHED_SCS ; i++) { + s =this_cpu_xchg (scs_cache[i],NULL ); +if (s) {kernel/scs.c line 44
Quoted 10 lines
kernel/scs.c +10
-0+memset (s,0 ,SCS_SIZE ); + goto out; + } + } + +/* +* We allocate a full page for the shadow stack, which should be +* more than we need. Check the assumption nevertheless. +*/ +BUILD_BUG_ON (SCS_SIZE >PAGE_SIZE );iWith
SCS_ORDER, you can drop this.
Quoted 7 lines
kernel/scs.c line 49
Quoted 5 lines
kernel/scs.c +5
-0+ + s =__vmalloc_node_range (PAGE_SIZE ,SCS_SIZE , +VMALLOC_START ,VMALLOC_END , +GFP_SCS ,PAGE_KERNEL ,0 , + node,__builtin_return_address (0 ));Do we actually need vmalloc here? If we used
alloc_pages()+vmap()
Does it matter that vmap() always uses NUMA_NO_NODE? We'll also lose the ability to use vfree_atomic() in scs_release() unless we use VM_MAP_PUT_PAGES and allocate the page array passed to vmap() with kvmalloc(), which I think we need to do to avoid sleeping in scs_free().
Quoted 2 lines
instead, then we could avoid the expensive call to
vmalloc_to_page()in__scs_account().
We still need vmalloc_to_page() in scs_release(). I suppose we could alternatively follow the example in kernel/fork.c and cache the vm_struct from find_vm_area() and use vm->pages[0] for the accounting. Thoughts?
Quoted 18 lines
Quoted 18 lines
kernel/scs.c +2
-1if (!s)return NULL ; +out: *__scs_magic (s) =SCS_END_MAGIC ;/* * Poison the allocation to catch unintentional accesses to * the shadow stack when KASAN is enabled. */ -kasan_poison_object_data (scs_cache, s); +kasan_poison_vmalloc (s,SCS_SIZE );__scs_account (s,1 );return s; }
Quoted 12 lines
kernel/scs.c line 71
Quoted 8 lines
kernel/scs.c +3
-2static void scs_free (void *s) { +int i; +__scs_account (s,-1 ); -kasan_unpoison_object_data (scs_cache, s); -kmem_cache_free (scs_cache, s); +kasan_unpoison_vmalloc (s,SCS_SIZE );I don't see the point in unpoisoning here tbh;
vfree_atomic()re-poisons almost immediately, so we should probably defer this toscs_alloc()and only when picking the stack out of the cache.
Sure, I'll change this in v2.
Quoted 6 lines
kernel/scs.c line 72
Quoted 3 lines
kernel/scs.c +2
-0+ +for (i =0 ; i <NR_CACHED_SCS ; i++)Can you add a comment about the re-entrancy here and why we're using
this_cpu_cmpxchg()please?
I'll add a comment.
Sami
Will Deacon
maintainerHi Sami,
Quoted 37 lines · Sami Tolvanen
Quoted 26 lines · Will Deacon
Quoted 8 lines · Sami Tolvanen
The kernel currently uses
kmem_cacheto allocate shadow call stacks, which means an overflow may not be immediately detected and can potentially result in another task's shadow stack to be overwritten.This change switches SCS to use virtually mapped shadow stacks, which increases shadow stack size to a full page and provides more robust overflow detection similarly to
VMAP_STACK.Quoted 5 lines
Signed-off-by Sami Tolvanen <[email protected]>--- include/linux/scs.h | 7 +---- kernel/scs.c | 63 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 15 deletions(-)Cheers for posting this. I _much_ prefer handling the SCS this way, but I have some comments on the implementation below.
Quoted 8 lines
include/linux/scs.h +0
-0@@ -15,12 +15,7 @@#ifdef CONFIG_SHADOW_CALL_STACK Quoted 9 lines
include/linux/scs.h line 18
Quoted 7 lines
include/linux/scs.h +1
-6-/* -* In testing, 1 KiB shadow stack size (i.e. 128 stack frames on a 64-bit -* architecture) provided ~40% safety margin on stack usage while keeping -* memory allocation overhead reasonable. -*/ -#define SCS_SIZE SZ_1K +#define SCS_SIZE PAGE_SIZEWe could make this
SCS_ORDERand then forget about alignment etc.
Quoted 3 lines
It's still convenient to have
SCS_SIZEdefined, I think. I can certainly defineSCS_ORDERand use that to defineSCS_SIZE, but do you think we'll need an order >0 here at some point in future?
I'm not daft enough to comment on SCS size again ;) Let's define SCS_ORDER 0 and then SCS_SIZE in terms of that.
Quoted 63 lines
Quoted 63 lines
Quoted 44 lines
include/linux/scs.h +0
-0#define GFP_SCS (GFP_KERNEL | __GFP_ZERO)/* An illegal pointer value to mark the end of the shadow stack. */ kernel/scs.c +13
-5@@ -5,50 +5,95 @@* Copyright (C) 2019 Google LLC */ +#include <linux/cpuhotplug.h> #include <linux/kasan.h> #include <linux/mm.h> #include <linux/scs.h> -#include <linux/slab.h> +#include <linux/vmalloc.h> #include <linux/vmstat.h> -static struct kmem_cache *scs_cache; -static void __scs_account (void *s,int account) { -struct page *scs_page =virt_to_page (s); +struct page *scs_page =vmalloc_to_page (s);mod_node_page_state (page_pgdat (scs_page),NR_KERNEL_SCS_KB , account * (SCS_SIZE /SZ_1K )); } +/* Matches NR_CACHED_STACKS for VMAP_STACK */ +#define NR_CACHED_SCS 2 +static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]); +static void *scs_alloc (int node) { -void *s =kmem_cache_alloc_node (scs_cache,GFP_SCS , node); +int i; +void *s; + +for (i =0 ; i <NR_CACHED_SCS ; i++) { + s =this_cpu_xchg (scs_cache[i],NULL ); +if (s) {kernel/scs.c line 44
Quoted 10 lines
kernel/scs.c +10
-0+memset (s,0 ,SCS_SIZE ); + goto out; + } + } + +/* +* We allocate a full page for the shadow stack, which should be +* more than we need. Check the assumption nevertheless. +*/ +BUILD_BUG_ON (SCS_SIZE >PAGE_SIZE );iWith
SCS_ORDER, you can drop this.Quoted 5 lines
kernel/scs.c +5
-0+ + s =__vmalloc_node_range (PAGE_SIZE ,SCS_SIZE , +VMALLOC_START ,VMALLOC_END , +GFP_SCS ,PAGE_KERNEL ,0 , + node,__builtin_return_address (0 ));
Quoted 7 lines
Quoted 1 line
Do we actually need vmalloc here? If we used
alloc_pages()+vmap()Does it matter that
vmap()always usesNUMA_NO_NODE? We'll also lose the ability to usevfree_atomic()inscs_release()unless we useVM_MAP_PUT_PAGESand allocate the page array passed tovmap()withkvmalloc(), which I think we need to do to avoid sleeping inscs_free().
Huh, I didn't realise we didn't have vunmap_atomic(). In which case, I take that back -- let's stick with vmalloc() for now.
Cheers,
Will