blob: b7e1b096d90601bfb2dc6a2a3646c2928d22dc1d [file] [log] [blame]
Sami Tolvanend08b9f02020-04-27 09:00:07 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shadow Call Stack support.
4 *
5 * Copyright (C) 2019 Google LLC
6 */
7
Sami Tolvanena2abe7c2020-11-30 15:34:41 -08008#include <linux/cpuhotplug.h>
Sami Tolvanend08b9f02020-04-27 09:00:07 -07009#include <linux/kasan.h>
Sami Tolvanen628d06a2020-04-27 09:00:08 -070010#include <linux/mm.h>
Sami Tolvanend08b9f02020-04-27 09:00:07 -070011#include <linux/scs.h>
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080012#include <linux/vmalloc.h>
Sami Tolvanen628d06a2020-04-27 09:00:08 -070013#include <linux/vmstat.h>
Sami Tolvanend08b9f02020-04-27 09:00:07 -070014
Will Deaconbee348f2020-05-15 14:43:11 +010015static void __scs_account(void *s, int account)
16{
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080017 struct page *scs_page = vmalloc_to_page(s);
Will Deaconbee348f2020-05-15 14:43:11 +010018
Shakeel Butt991e7672020-08-06 23:21:37 -070019 mod_node_page_state(page_pgdat(scs_page), NR_KERNEL_SCS_KB,
Will Deaconbee348f2020-05-15 14:43:11 +010020 account * (SCS_SIZE / SZ_1K));
21}
22
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080023/* Matches NR_CACHED_STACKS for VMAP_STACK */
24#define NR_CACHED_SCS 2
25static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]);
Sami Tolvanend08b9f02020-04-27 09:00:07 -070026
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080027static 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) {
Andrey Konovalovf6e39792022-03-24 18:11:35 -070035 s = kasan_unpoison_vmalloc(s, SCS_SIZE,
36 KASAN_VMALLOC_PROT_NORMAL);
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080037 memset(s, 0, SCS_SIZE);
Andrey Konovalovf6e39792022-03-24 18:11:35 -070038 goto out;
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080039 }
40 }
41
Andrey Konovalovf6e39792022-03-24 18:11:35 -070042 s = __vmalloc_node_range(SCS_SIZE, 1, VMALLOC_START, VMALLOC_END,
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080043 GFP_SCS, PAGE_KERNEL, 0, node,
44 __builtin_return_address(0));
Andrey Konovalovf6e39792022-03-24 18:11:35 -070045
46out:
47 return kasan_reset_tag(s);
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080048}
49
50void *scs_alloc(int node)
51{
52 void *s;
53
54 s = __scs_alloc(node);
Will Deaconbee348f2020-05-15 14:43:11 +010055 if (!s)
56 return NULL;
Sami Tolvanend08b9f02020-04-27 09:00:07 -070057
Will Deaconbee348f2020-05-15 14:43:11 +010058 *__scs_magic(s) = SCS_END_MAGIC;
59
60 /*
61 * Poison the allocation to catch unintentional accesses to
62 * the shadow stack when KASAN is enabled.
63 */
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080064 kasan_poison_vmalloc(s, SCS_SIZE);
Will Deaconbee348f2020-05-15 14:43:11 +010065 __scs_account(s, 1);
Sami Tolvanend08b9f02020-04-27 09:00:07 -070066 return s;
67}
68
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080069void scs_free(void *s)
Sami Tolvanend08b9f02020-04-27 09:00:07 -070070{
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080071 int i;
72
Will Deaconbee348f2020-05-15 14:43:11 +010073 __scs_account(s, -1);
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080074
75 /*
76 * We cannot sleep as this can be called in interrupt context,
77 * so use this_cpu_cmpxchg to update the cache, and vfree_atomic
78 * to free the stack.
79 */
80
81 for (i = 0; i < NR_CACHED_SCS; i++)
82 if (this_cpu_cmpxchg(scs_cache[i], 0, s) == NULL)
83 return;
84
Andrey Konovalovf6e39792022-03-24 18:11:35 -070085 kasan_unpoison_vmalloc(s, SCS_SIZE, KASAN_VMALLOC_PROT_NORMAL);
Sami Tolvanena2abe7c2020-11-30 15:34:41 -080086 vfree_atomic(s);
87}
88
89static int scs_cleanup(unsigned int cpu)
90{
91 int i;
92 void **cache = per_cpu_ptr(scs_cache, cpu);
93
94 for (i = 0; i < NR_CACHED_SCS; i++) {
95 vfree(cache[i]);
96 cache[i] = NULL;
97 }
98
99 return 0;
Sami Tolvanend08b9f02020-04-27 09:00:07 -0700100}
101
102void __init scs_init(void)
103{
Sami Tolvanena2abe7c2020-11-30 15:34:41 -0800104 cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "scs:scs_cache", NULL,
105 scs_cleanup);
Sami Tolvanend08b9f02020-04-27 09:00:07 -0700106}
107
108int scs_prepare(struct task_struct *tsk, int node)
109{
110 void *s = scs_alloc(node);
111
112 if (!s)
113 return -ENOMEM;
114
Will Deacon51189c72020-05-15 14:11:05 +0100115 task_scs(tsk) = task_scs_sp(tsk) = s;
Sami Tolvanend08b9f02020-04-27 09:00:07 -0700116 return 0;
117}
118
Sami Tolvanen5bbaf9d2020-04-27 09:00:09 -0700119static void scs_check_usage(struct task_struct *tsk)
120{
121 static unsigned long highest;
122
123 unsigned long *p, prev, curr = highest, used = 0;
124
125 if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
126 return;
127
128 for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
129 if (!READ_ONCE_NOCHECK(*p))
130 break;
Will Deacon333ed742020-06-03 12:16:37 +0100131 used += sizeof(*p);
Sami Tolvanen5bbaf9d2020-04-27 09:00:09 -0700132 }
133
134 while (used > curr) {
135 prev = cmpxchg_relaxed(&highest, curr, used);
136
137 if (prev == curr) {
138 pr_info("%s (%d): highest shadow stack usage: %lu bytes\n",
139 tsk->comm, task_pid_nr(tsk), used);
140 break;
141 }
142
143 curr = prev;
144 }
145}
146
Sami Tolvanend08b9f02020-04-27 09:00:07 -0700147void scs_release(struct task_struct *tsk)
148{
149 void *s = task_scs(tsk);
150
151 if (!s)
152 return;
153
Will Deacon88485be2020-05-15 14:56:05 +0100154 WARN(task_scs_end_corrupted(tsk),
155 "corrupted shadow stack detected when freeing task\n");
Sami Tolvanen5bbaf9d2020-04-27 09:00:09 -0700156 scs_check_usage(tsk);
Sami Tolvanend08b9f02020-04-27 09:00:07 -0700157 scs_free(s);
158}