forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kasan-fakestack.c
339 lines (282 loc) · 8.52 KB
/
kasan-fakestack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (c) 2016 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
#include <stdint.h>
#include <stdbool.h>
#include <kern/assert.h>
#include <kern/zalloc.h>
#include <mach/mach_vm.h>
#include <mach/vm_param.h>
#include <libkern/libkern.h>
#include <libkern/OSAtomic.h>
#include <sys/queue.h>
#include <kern/thread.h>
#include <kern/debug.h>
#include <kasan.h>
#include <kasan_internal.h>
int __asan_option_detect_stack_use_after_return = 0;
#define FAKESTACK_HEADER_SZ 64
#define FAKESTACK_NUM_SZCLASS 7
#define FAKESTACK_FREED 0 /* forced by clang */
#define FAKESTACK_ALLOCATED 1
#if FAKESTACK
struct fakestack_header {
LIST_ENTRY(fakestack_header) list;
void *site; /* allocation site */
struct {
uint8_t flag;
vm_size_t realsz : 52;
vm_size_t sz_class : 4;
};
uint64_t __pad0;
};
_Static_assert(sizeof(struct fakestack_header) <= FAKESTACK_HEADER_SZ, "fakestack_header size mismatch");
static zone_t fakestack_zones[FAKESTACK_NUM_SZCLASS];
static char fakestack_names[FAKESTACK_NUM_SZCLASS][16];
static const unsigned long fakestack_min = 1 << 6;
static const unsigned long __unused fakestack_max = 1 << 16;
/*
* Mark the current thread as being in a fakestack operation, to avoid reentrancy
* issues. If set, disable fakestack allocation.
*/
static boolean_t
thread_enter_fakestack(void)
{
thread_t thread = current_thread();
if (thread) {
return OSIncrementAtomic(&kasan_get_thread_data(current_thread())->in_fakestack);
} else {
return 0;
}
}
static boolean_t
thread_exit_fakestack(void)
{
thread_t thread = current_thread();
if (thread) {
return OSDecrementAtomic(&kasan_get_thread_data(current_thread())->in_fakestack);
} else {
return 0;
}
}
static bool
ptr_is_on_stack(uptr ptr)
{
vm_offset_t base = dtrace_get_kernel_stack(current_thread());
if (ptr >= base && ptr < (base + kernel_stack_size)) {
return true;
} else {
return false;
}
}
/* free all unused fakestack entries */
static void NOINLINE
kasan_fakestack_gc(thread_t thread)
{
struct fakestack_header *cur, *tmp;
LIST_HEAD(, fakestack_header) tofree = LIST_HEAD_INITIALIZER(tofree);
/* move all the freed elements off the main list */
struct fakestack_header_list *head = &kasan_get_thread_data(thread)->fakestack_head;
LIST_FOREACH_SAFE(cur, head, list, tmp) {
if (cur->flag == FAKESTACK_FREED) {
LIST_REMOVE(cur, list);
LIST_INSERT_HEAD(&tofree, cur, list);
}
}
/* ... then actually free them */
LIST_FOREACH_SAFE(cur, &tofree, list, tmp) {
zone_t zone = fakestack_zones[cur->sz_class];
size_t sz = (fakestack_min << cur->sz_class) + FAKESTACK_HEADER_SZ;
LIST_REMOVE(cur, list);
void *ptr = (void *)cur;
kasan_free_internal(&ptr, &sz, KASAN_HEAP_FAKESTACK, &zone, cur->realsz, 1, FAKESTACK_QUARANTINE);
if (ptr) {
zfree(zone, ptr);
}
}
}
static uint8_t **
fakestack_flag_ptr(vm_offset_t ptr, vm_size_t sz)
{
uint8_t **x = (uint8_t **)ptr;
size_t idx = sz / 8;
return &x[idx - 1];
}
static uptr ALWAYS_INLINE
kasan_fakestack_alloc(int sz_class, size_t realsz)
{
if (!__asan_option_detect_stack_use_after_return) {
return 0;
}
if (sz_class >= FAKESTACK_NUM_SZCLASS) {
return 0;
}
boolean_t flags;
uptr ret = 0;
size_t sz = fakestack_min << sz_class;
assert(realsz <= sz);
assert(sz <= fakestack_max);
zone_t zone = fakestack_zones[sz_class];
if (thread_enter_fakestack()) {
return 0;
}
kasan_lock(&flags);
kasan_fakestack_gc(current_thread()); /* XXX: optimal? */
ret = (uptr)zget(zone);
thread_exit_fakestack();
if (ret) {
size_t leftrz = 32 + FAKESTACK_HEADER_SZ;
size_t validsz = realsz - 32 - 16; /* remove redzones */
size_t rightrz = sz - validsz - 32; /* 16 bytes, plus whatever is left over */
struct fakestack_header *hdr = (struct fakestack_header *)ret;
kasan_poison(ret, validsz, leftrz, rightrz, ASAN_STACK_RZ);
hdr->site = __builtin_return_address(0);
hdr->realsz = realsz;
hdr->sz_class = sz_class;
hdr->flag = FAKESTACK_ALLOCATED;
ret += FAKESTACK_HEADER_SZ;
*fakestack_flag_ptr(ret, sz) = &hdr->flag; /* back ptr to the slot */
struct fakestack_header_list *head = &kasan_get_thread_data(current_thread())->fakestack_head;
LIST_INSERT_HEAD(head, hdr, list);
}
kasan_unlock(flags);
return ret;
}
static void NOINLINE
kasan_fakestack_free(int sz_class, uptr dst, size_t realsz)
{
if (ptr_is_on_stack(dst)) {
return;
}
assert(realsz <= (fakestack_min << sz_class));
assert(__asan_option_detect_stack_use_after_return);
vm_size_t sz = fakestack_min << sz_class;
zone_t zone = fakestack_zones[sz_class];
assert(zone);
/* TODO: check the magic? */
dst -= FAKESTACK_HEADER_SZ;
sz += FAKESTACK_HEADER_SZ;
struct fakestack_header *hdr = (struct fakestack_header *)dst;
assert(hdr->sz_class == sz_class);
boolean_t flags;
kasan_lock(&flags);
LIST_REMOVE(hdr, list);
kasan_free_internal((void **)&dst, &sz, KASAN_HEAP_FAKESTACK, &zone, realsz, 1, FAKESTACK_QUARANTINE);
if (dst) {
zfree(zone, (void *)dst);
}
kasan_unlock(flags);
}
void NOINLINE
kasan_unpoison_fakestack(thread_t thread)
{
if (!__asan_option_detect_stack_use_after_return) {
return;
}
boolean_t flags;
kasan_lock(&flags);
thread_enter_fakestack();
struct fakestack_header_list *head = &kasan_get_thread_data(thread)->fakestack_head;
struct fakestack_header *cur;
LIST_FOREACH(cur, head, list) {
if (cur->flag == FAKESTACK_ALLOCATED) {
cur->flag = FAKESTACK_FREED;
}
}
kasan_fakestack_gc(thread);
thread_exit_fakestack();
kasan_unlock(flags);
}
void NOINLINE
kasan_init_fakestack(void)
{
/* allocate the fakestack zones */
for (int i = 0; i < FAKESTACK_NUM_SZCLASS; i++) {
zone_t z;
unsigned long sz = (fakestack_min << i) + FAKESTACK_HEADER_SZ;
size_t maxsz = 256UL * 1024;
if (i <= 3) {
/* size classes 0..3 are much more common */
maxsz *= 4;
}
snprintf(fakestack_names[i], 16, "fakestack.%d", i);
z = zinit(sz, maxsz, sz, fakestack_names[i]);
assert(z);
zone_change(z, Z_NOCALLOUT, TRUE);
zone_change(z, Z_EXHAUST, TRUE);
zone_change(z, Z_EXPAND, FALSE);
zone_change(z, Z_COLLECT, FALSE);
zone_change(z, Z_KASAN_QUARANTINE, FALSE);
zfill(z, maxsz / sz);
fakestack_zones[i] = z;
}
/* globally enable */
__asan_option_detect_stack_use_after_return = 1;
}
#else /* FAKESTACK */
void
kasan_init_fakestack(void)
{
assert(__asan_option_detect_stack_use_after_return == 0);
}
void
kasan_unpoison_fakestack(thread_t __unused thread)
{
assert(__asan_option_detect_stack_use_after_return == 0);
}
static uptr
kasan_fakestack_alloc(int __unused sz_class, size_t __unused realsz)
{
assert(__asan_option_detect_stack_use_after_return == 0);
return 0;
}
static void
kasan_fakestack_free(int __unused sz_class, uptr __unused dst, size_t __unused realsz)
{
assert(__asan_option_detect_stack_use_after_return == 0);
panic("fakestack_free called on non-FAKESTACK config\n");
}
#endif
void kasan_init_thread(struct kasan_thread_data *td)
{
td->in_fakestack = 0;
LIST_INIT(&td->fakestack_head);
}
#define FAKESTACK_DECLARE(szclass) \
uptr __asan_stack_malloc_##szclass(size_t sz) { return kasan_fakestack_alloc(szclass, sz); } \
void __asan_stack_free_##szclass(uptr dst, size_t sz) { kasan_fakestack_free(szclass, dst, sz); }
FAKESTACK_DECLARE(0)
FAKESTACK_DECLARE(1)
FAKESTACK_DECLARE(2)
FAKESTACK_DECLARE(3)
FAKESTACK_DECLARE(4)
FAKESTACK_DECLARE(5)
FAKESTACK_DECLARE(6)
FAKESTACK_DECLARE(7)
FAKESTACK_DECLARE(8)
FAKESTACK_DECLARE(9)
FAKESTACK_DECLARE(10)