Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shadow Call Stack support. |
| 4 | * |
| 5 | * Copyright (C) 2019 Google LLC |
| 6 | */ |
| 7 | |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 8 | #include <linux/cpuhotplug.h> |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 9 | #include <linux/kasan.h> |
Sami Tolvanen | 628d06a | 2020-04-27 09:00:08 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 11 | #include <linux/scs.h> |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 12 | #include <linux/vmalloc.h> |
Sami Tolvanen | 628d06a | 2020-04-27 09:00:08 -0700 | [diff] [blame] | 13 | #include <linux/vmstat.h> |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 14 | |
Will Deacon | bee348f | 2020-05-15 14:43:11 +0100 | [diff] [blame] | 15 | static void __scs_account(void *s, int account) |
| 16 | { |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 17 | struct page *scs_page = vmalloc_to_page(s); |
Will Deacon | bee348f | 2020-05-15 14:43:11 +0100 | [diff] [blame] | 18 | |
Shakeel Butt | 991e767 | 2020-08-06 23:21:37 -0700 | [diff] [blame] | 19 | mod_node_page_state(page_pgdat(scs_page), NR_KERNEL_SCS_KB, |
Will Deacon | bee348f | 2020-05-15 14:43:11 +0100 | [diff] [blame] | 20 | account * (SCS_SIZE / SZ_1K)); |
| 21 | } |
| 22 | |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 23 | /* Matches NR_CACHED_STACKS for VMAP_STACK */ |
| 24 | #define NR_CACHED_SCS 2 |
| 25 | static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]); |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 26 | |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 27 | static void *__scs_alloc(int node) |
| 28 | { |
| 29 | int i; |
| 30 | void *s; |
| 31 | |
| 32 | for (i = 0; i < NR_CACHED_SCS; i++) { |
| 33 | s = this_cpu_xchg(scs_cache[i], NULL); |
| 34 | if (s) { |
| 35 | kasan_unpoison_vmalloc(s, SCS_SIZE); |
| 36 | memset(s, 0, SCS_SIZE); |
| 37 | return s; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return __vmalloc_node_range(SCS_SIZE, 1, VMALLOC_START, VMALLOC_END, |
| 42 | GFP_SCS, PAGE_KERNEL, 0, node, |
| 43 | __builtin_return_address(0)); |
| 44 | } |
| 45 | |
| 46 | void *scs_alloc(int node) |
| 47 | { |
| 48 | void *s; |
| 49 | |
| 50 | s = __scs_alloc(node); |
Will Deacon | bee348f | 2020-05-15 14:43:11 +0100 | [diff] [blame] | 51 | if (!s) |
| 52 | return NULL; |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 53 | |
Will Deacon | bee348f | 2020-05-15 14:43:11 +0100 | [diff] [blame] | 54 | *__scs_magic(s) = SCS_END_MAGIC; |
| 55 | |
| 56 | /* |
| 57 | * Poison the allocation to catch unintentional accesses to |
| 58 | * the shadow stack when KASAN is enabled. |
| 59 | */ |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 60 | kasan_poison_vmalloc(s, SCS_SIZE); |
Will Deacon | bee348f | 2020-05-15 14:43:11 +0100 | [diff] [blame] | 61 | __scs_account(s, 1); |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 62 | return s; |
| 63 | } |
| 64 | |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 65 | void scs_free(void *s) |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 66 | { |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 67 | int i; |
| 68 | |
Will Deacon | bee348f | 2020-05-15 14:43:11 +0100 | [diff] [blame] | 69 | __scs_account(s, -1); |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * We cannot sleep as this can be called in interrupt context, |
| 73 | * so use this_cpu_cmpxchg to update the cache, and vfree_atomic |
| 74 | * to free the stack. |
| 75 | */ |
| 76 | |
| 77 | for (i = 0; i < NR_CACHED_SCS; i++) |
| 78 | if (this_cpu_cmpxchg(scs_cache[i], 0, s) == NULL) |
| 79 | return; |
| 80 | |
Yee Lee | 528a4ab4 | 2021-09-30 16:16:13 +0800 | [diff] [blame] | 81 | kasan_unpoison_vmalloc(s, SCS_SIZE); |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 82 | vfree_atomic(s); |
| 83 | } |
| 84 | |
| 85 | static int scs_cleanup(unsigned int cpu) |
| 86 | { |
| 87 | int i; |
| 88 | void **cache = per_cpu_ptr(scs_cache, cpu); |
| 89 | |
| 90 | for (i = 0; i < NR_CACHED_SCS; i++) { |
| 91 | vfree(cache[i]); |
| 92 | cache[i] = NULL; |
| 93 | } |
| 94 | |
| 95 | return 0; |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void __init scs_init(void) |
| 99 | { |
Sami Tolvanen | a2abe7c | 2020-11-30 15:34:41 -0800 | [diff] [blame] | 100 | cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "scs:scs_cache", NULL, |
| 101 | scs_cleanup); |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int scs_prepare(struct task_struct *tsk, int node) |
| 105 | { |
| 106 | void *s = scs_alloc(node); |
| 107 | |
| 108 | if (!s) |
| 109 | return -ENOMEM; |
| 110 | |
Will Deacon | 51189c7 | 2020-05-15 14:11:05 +0100 | [diff] [blame] | 111 | task_scs(tsk) = task_scs_sp(tsk) = s; |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Sami Tolvanen | 5bbaf9d | 2020-04-27 09:00:09 -0700 | [diff] [blame] | 115 | static void scs_check_usage(struct task_struct *tsk) |
| 116 | { |
| 117 | static unsigned long highest; |
| 118 | |
| 119 | unsigned long *p, prev, curr = highest, used = 0; |
| 120 | |
| 121 | if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE)) |
| 122 | return; |
| 123 | |
| 124 | for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) { |
| 125 | if (!READ_ONCE_NOCHECK(*p)) |
| 126 | break; |
Will Deacon | 333ed74 | 2020-06-03 12:16:37 +0100 | [diff] [blame] | 127 | used += sizeof(*p); |
Sami Tolvanen | 5bbaf9d | 2020-04-27 09:00:09 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | while (used > curr) { |
| 131 | prev = cmpxchg_relaxed(&highest, curr, used); |
| 132 | |
| 133 | if (prev == curr) { |
| 134 | pr_info("%s (%d): highest shadow stack usage: %lu bytes\n", |
| 135 | tsk->comm, task_pid_nr(tsk), used); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | curr = prev; |
| 140 | } |
| 141 | } |
| 142 | |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 143 | void scs_release(struct task_struct *tsk) |
| 144 | { |
| 145 | void *s = task_scs(tsk); |
| 146 | |
| 147 | if (!s) |
| 148 | return; |
| 149 | |
Will Deacon | 88485be | 2020-05-15 14:56:05 +0100 | [diff] [blame] | 150 | WARN(task_scs_end_corrupted(tsk), |
| 151 | "corrupted shadow stack detected when freeing task\n"); |
Sami Tolvanen | 5bbaf9d | 2020-04-27 09:00:09 -0700 | [diff] [blame] | 152 | scs_check_usage(tsk); |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 153 | scs_free(s); |
| 154 | } |