subr_asan.c revision 1.26 1 1.26 maxv /* $NetBSD: subr_asan.c,v 1.26 2020/09/10 14:10:46 maxv Exp $ */
2 1.1 maxv
3 1.1 maxv /*
4 1.26 maxv * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
5 1.1 maxv * All rights reserved.
6 1.1 maxv *
7 1.26 maxv * This code is part of the KASAN subsystem of the NetBSD kernel.
8 1.1 maxv *
9 1.1 maxv * Redistribution and use in source and binary forms, with or without
10 1.1 maxv * modification, are permitted provided that the following conditions
11 1.1 maxv * are met:
12 1.1 maxv * 1. Redistributions of source code must retain the above copyright
13 1.1 maxv * notice, this list of conditions and the following disclaimer.
14 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 maxv * notice, this list of conditions and the following disclaimer in the
16 1.1 maxv * documentation and/or other materials provided with the distribution.
17 1.1 maxv *
18 1.26 maxv * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.26 maxv * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.26 maxv * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.26 maxv * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.26 maxv * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.26 maxv * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.26 maxv * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.26 maxv * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.26 maxv * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.26 maxv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.26 maxv * SUCH DAMAGE.
29 1.1 maxv */
30 1.1 maxv
31 1.1 maxv #include <sys/cdefs.h>
32 1.26 maxv __KERNEL_RCSID(0, "$NetBSD: subr_asan.c,v 1.26 2020/09/10 14:10:46 maxv Exp $");
33 1.1 maxv
34 1.1 maxv #include <sys/param.h>
35 1.1 maxv #include <sys/device.h>
36 1.1 maxv #include <sys/kernel.h>
37 1.1 maxv #include <sys/param.h>
38 1.1 maxv #include <sys/conf.h>
39 1.1 maxv #include <sys/systm.h>
40 1.1 maxv #include <sys/types.h>
41 1.1 maxv #include <sys/asan.h>
42 1.1 maxv
43 1.25 riastrad #include <uvm/uvm_extern.h>
44 1.1 maxv
45 1.10 maxv #ifdef KASAN_PANIC
46 1.10 maxv #define REPORT panic
47 1.10 maxv #else
48 1.10 maxv #define REPORT printf
49 1.10 maxv #endif
50 1.10 maxv
51 1.1 maxv /* ASAN constants. Part of the compiler ABI. */
52 1.1 maxv #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
53 1.1 maxv #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
54 1.19 maxv #define KASAN_ALLOCA_SCALE_SIZE 32
55 1.1 maxv
56 1.1 maxv /* The MD code. */
57 1.1 maxv #include <machine/asan.h>
58 1.1 maxv
59 1.1 maxv /* ASAN ABI version. */
60 1.1 maxv #if defined(__clang__) && (__clang_major__ - 0 >= 6)
61 1.1 maxv #define ASAN_ABI_VERSION 8
62 1.1 maxv #elif __GNUC_PREREQ__(7, 1) && !defined(__clang__)
63 1.1 maxv #define ASAN_ABI_VERSION 8
64 1.1 maxv #elif __GNUC_PREREQ__(6, 1) && !defined(__clang__)
65 1.1 maxv #define ASAN_ABI_VERSION 6
66 1.1 maxv #else
67 1.1 maxv #error "Unsupported compiler version"
68 1.1 maxv #endif
69 1.1 maxv
70 1.1 maxv #define __RET_ADDR (unsigned long)__builtin_return_address(0)
71 1.1 maxv
72 1.1 maxv /* Global variable descriptor. Part of the compiler ABI. */
73 1.1 maxv struct __asan_global_source_location {
74 1.1 maxv const char *filename;
75 1.1 maxv int line_no;
76 1.1 maxv int column_no;
77 1.1 maxv };
78 1.1 maxv struct __asan_global {
79 1.1 maxv const void *beg; /* address of the global variable */
80 1.1 maxv size_t size; /* size of the global variable */
81 1.1 maxv size_t size_with_redzone; /* size with the redzone */
82 1.1 maxv const void *name; /* name of the variable */
83 1.1 maxv const void *module_name; /* name of the module where the var is declared */
84 1.1 maxv unsigned long has_dynamic_init; /* the var has dyn initializer (c++) */
85 1.1 maxv struct __asan_global_source_location *location;
86 1.1 maxv #if ASAN_ABI_VERSION >= 7
87 1.1 maxv uintptr_t odr_indicator; /* the address of the ODR indicator symbol */
88 1.1 maxv #endif
89 1.1 maxv };
90 1.1 maxv
91 1.1 maxv static bool kasan_enabled __read_mostly = false;
92 1.1 maxv
93 1.1 maxv /* -------------------------------------------------------------------------- */
94 1.1 maxv
95 1.1 maxv void
96 1.1 maxv kasan_shadow_map(void *addr, size_t size)
97 1.1 maxv {
98 1.1 maxv size_t sz, npages, i;
99 1.1 maxv vaddr_t sva, eva;
100 1.1 maxv
101 1.1 maxv KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0);
102 1.1 maxv
103 1.1 maxv sz = roundup(size, KASAN_SHADOW_SCALE_SIZE) / KASAN_SHADOW_SCALE_SIZE;
104 1.1 maxv
105 1.1 maxv sva = (vaddr_t)kasan_md_addr_to_shad(addr);
106 1.1 maxv eva = (vaddr_t)kasan_md_addr_to_shad(addr) + sz;
107 1.1 maxv
108 1.1 maxv sva = rounddown(sva, PAGE_SIZE);
109 1.1 maxv eva = roundup(eva, PAGE_SIZE);
110 1.1 maxv
111 1.1 maxv npages = (eva - sva) / PAGE_SIZE;
112 1.1 maxv
113 1.1 maxv KASSERT(sva >= KASAN_MD_SHADOW_START && eva < KASAN_MD_SHADOW_END);
114 1.1 maxv
115 1.1 maxv for (i = 0; i < npages; i++) {
116 1.1 maxv kasan_md_shadow_map_page(sva + i * PAGE_SIZE);
117 1.1 maxv }
118 1.1 maxv }
119 1.1 maxv
120 1.1 maxv static void
121 1.1 maxv kasan_ctors(void)
122 1.1 maxv {
123 1.23 skrll extern Elf_Addr __CTOR_LIST__, __CTOR_END__;
124 1.1 maxv size_t nentries, i;
125 1.23 skrll Elf_Addr *ptr;
126 1.1 maxv
127 1.1 maxv nentries = ((size_t)&__CTOR_END__ - (size_t)&__CTOR_LIST__) /
128 1.1 maxv sizeof(uintptr_t);
129 1.1 maxv
130 1.1 maxv ptr = &__CTOR_LIST__;
131 1.1 maxv for (i = 0; i < nentries; i++) {
132 1.1 maxv void (*func)(void);
133 1.1 maxv
134 1.1 maxv func = (void *)(*ptr);
135 1.1 maxv (*func)();
136 1.1 maxv
137 1.1 maxv ptr++;
138 1.1 maxv }
139 1.1 maxv }
140 1.1 maxv
141 1.1 maxv void
142 1.1 maxv kasan_early_init(void *stack)
143 1.1 maxv {
144 1.1 maxv kasan_md_early_init(stack);
145 1.1 maxv }
146 1.1 maxv
147 1.1 maxv void
148 1.1 maxv kasan_init(void)
149 1.1 maxv {
150 1.1 maxv /* MD initialization. */
151 1.1 maxv kasan_md_init();
152 1.1 maxv
153 1.1 maxv /* Now officially enabled. */
154 1.1 maxv kasan_enabled = true;
155 1.1 maxv
156 1.1 maxv /* Call the ASAN constructors. */
157 1.1 maxv kasan_ctors();
158 1.1 maxv }
159 1.1 maxv
160 1.5 maxv static inline const char *
161 1.5 maxv kasan_code_name(uint8_t code)
162 1.5 maxv {
163 1.5 maxv switch (code) {
164 1.6 maxv case KASAN_GENERIC_REDZONE:
165 1.6 maxv return "GenericRedZone";
166 1.6 maxv case KASAN_MALLOC_REDZONE:
167 1.6 maxv return "MallocRedZone";
168 1.6 maxv case KASAN_KMEM_REDZONE:
169 1.6 maxv return "KmemRedZone";
170 1.6 maxv case KASAN_POOL_REDZONE:
171 1.6 maxv return "PoolRedZone";
172 1.6 maxv case KASAN_POOL_FREED:
173 1.6 maxv return "PoolUseAfterFree";
174 1.5 maxv case 1 ... 7:
175 1.5 maxv return "RedZonePartial";
176 1.5 maxv case KASAN_STACK_LEFT:
177 1.5 maxv return "StackLeft";
178 1.18 maxv case KASAN_STACK_MID:
179 1.18 maxv return "StackMiddle";
180 1.5 maxv case KASAN_STACK_RIGHT:
181 1.5 maxv return "StackRight";
182 1.18 maxv case KASAN_USE_AFTER_RET:
183 1.18 maxv return "UseAfterRet";
184 1.5 maxv case KASAN_USE_AFTER_SCOPE:
185 1.5 maxv return "UseAfterScope";
186 1.5 maxv default:
187 1.5 maxv return "Unknown";
188 1.5 maxv }
189 1.5 maxv }
190 1.5 maxv
191 1.1 maxv static void
192 1.5 maxv kasan_report(unsigned long addr, size_t size, bool write, unsigned long pc,
193 1.5 maxv uint8_t code)
194 1.1 maxv {
195 1.10 maxv REPORT("ASan: Unauthorized Access In %p: Addr %p [%zu byte%s, %s,"
196 1.5 maxv " %s]\n",
197 1.1 maxv (void *)pc, (void *)addr, size, (size > 1 ? "s" : ""),
198 1.5 maxv (write ? "write" : "read"), kasan_code_name(code));
199 1.1 maxv kasan_md_unwind();
200 1.1 maxv }
201 1.1 maxv
202 1.1 maxv static __always_inline void
203 1.1 maxv kasan_shadow_1byte_markvalid(unsigned long addr)
204 1.1 maxv {
205 1.1 maxv int8_t *byte = kasan_md_addr_to_shad((void *)addr);
206 1.1 maxv int8_t last = (addr & KASAN_SHADOW_MASK) + 1;
207 1.1 maxv
208 1.1 maxv *byte = last;
209 1.1 maxv }
210 1.1 maxv
211 1.1 maxv static __always_inline void
212 1.4 maxv kasan_shadow_Nbyte_markvalid(const void *addr, size_t size)
213 1.4 maxv {
214 1.4 maxv size_t i;
215 1.4 maxv
216 1.4 maxv for (i = 0; i < size; i++) {
217 1.4 maxv kasan_shadow_1byte_markvalid((unsigned long)addr+i);
218 1.4 maxv }
219 1.4 maxv }
220 1.4 maxv
221 1.4 maxv static __always_inline void
222 1.6 maxv kasan_shadow_Nbyte_fill(const void *addr, size_t size, uint8_t code)
223 1.1 maxv {
224 1.1 maxv void *shad;
225 1.1 maxv
226 1.1 maxv if (__predict_false(size == 0))
227 1.1 maxv return;
228 1.1 maxv if (__predict_false(kasan_md_unsupported((vaddr_t)addr)))
229 1.1 maxv return;
230 1.1 maxv
231 1.1 maxv KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0);
232 1.1 maxv KASSERT(size % KASAN_SHADOW_SCALE_SIZE == 0);
233 1.1 maxv
234 1.1 maxv shad = (void *)kasan_md_addr_to_shad(addr);
235 1.1 maxv size = size >> KASAN_SHADOW_SCALE_SHIFT;
236 1.1 maxv
237 1.6 maxv __builtin_memset(shad, code, size);
238 1.1 maxv }
239 1.1 maxv
240 1.1 maxv void
241 1.1 maxv kasan_add_redzone(size_t *size)
242 1.1 maxv {
243 1.1 maxv *size = roundup(*size, KASAN_SHADOW_SCALE_SIZE);
244 1.1 maxv *size += KASAN_SHADOW_SCALE_SIZE;
245 1.1 maxv }
246 1.1 maxv
247 1.1 maxv void
248 1.1 maxv kasan_softint(struct lwp *l)
249 1.1 maxv {
250 1.1 maxv const void *stk = (const void *)uvm_lwp_getuarea(l);
251 1.1 maxv
252 1.1 maxv kasan_shadow_Nbyte_fill(stk, USPACE, 0);
253 1.1 maxv }
254 1.1 maxv
255 1.2 maxv /*
256 1.2 maxv * In an area of size 'sz_with_redz', mark the 'size' first bytes as valid,
257 1.2 maxv * and the rest as invalid. There are generally two use cases:
258 1.2 maxv *
259 1.6 maxv * o kasan_mark(addr, origsize, size, code), with origsize < size. This marks
260 1.6 maxv * the redzone at the end of the buffer as invalid.
261 1.2 maxv *
262 1.6 maxv * o kasan_mark(addr, size, size, 0). This marks the entire buffer as valid.
263 1.2 maxv */
264 1.1 maxv void
265 1.6 maxv kasan_mark(const void *addr, size_t size, size_t sz_with_redz, uint8_t code)
266 1.1 maxv {
267 1.9 maxv size_t i, n, redz;
268 1.9 maxv int8_t *shad;
269 1.9 maxv
270 1.9 maxv KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0);
271 1.9 maxv redz = sz_with_redz - roundup(size, KASAN_SHADOW_SCALE_SIZE);
272 1.9 maxv KASSERT(redz % KASAN_SHADOW_SCALE_SIZE == 0);
273 1.9 maxv shad = kasan_md_addr_to_shad(addr);
274 1.9 maxv
275 1.9 maxv /* Chunks of 8 bytes, valid. */
276 1.9 maxv n = size / KASAN_SHADOW_SCALE_SIZE;
277 1.9 maxv for (i = 0; i < n; i++) {
278 1.9 maxv *shad++ = 0;
279 1.9 maxv }
280 1.9 maxv
281 1.9 maxv /* Possibly one chunk, mid. */
282 1.9 maxv if ((size & KASAN_SHADOW_MASK) != 0) {
283 1.9 maxv *shad++ = (size & KASAN_SHADOW_MASK);
284 1.9 maxv }
285 1.9 maxv
286 1.9 maxv /* Chunks of 8 bytes, invalid. */
287 1.9 maxv n = redz / KASAN_SHADOW_SCALE_SIZE;
288 1.9 maxv for (i = 0; i < n; i++) {
289 1.9 maxv *shad++ = code;
290 1.9 maxv }
291 1.1 maxv }
292 1.1 maxv
293 1.1 maxv /* -------------------------------------------------------------------------- */
294 1.1 maxv
295 1.1 maxv #define ADDR_CROSSES_SCALE_BOUNDARY(addr, size) \
296 1.1 maxv (addr >> KASAN_SHADOW_SCALE_SHIFT) != \
297 1.1 maxv ((addr + size - 1) >> KASAN_SHADOW_SCALE_SHIFT)
298 1.1 maxv
299 1.1 maxv static __always_inline bool
300 1.5 maxv kasan_shadow_1byte_isvalid(unsigned long addr, uint8_t *code)
301 1.1 maxv {
302 1.1 maxv int8_t *byte = kasan_md_addr_to_shad((void *)addr);
303 1.1 maxv int8_t last = (addr & KASAN_SHADOW_MASK) + 1;
304 1.1 maxv
305 1.5 maxv if (__predict_true(*byte == 0 || last <= *byte)) {
306 1.5 maxv return true;
307 1.5 maxv }
308 1.5 maxv *code = *byte;
309 1.5 maxv return false;
310 1.1 maxv }
311 1.1 maxv
312 1.1 maxv static __always_inline bool
313 1.5 maxv kasan_shadow_2byte_isvalid(unsigned long addr, uint8_t *code)
314 1.1 maxv {
315 1.1 maxv int8_t *byte, last;
316 1.1 maxv
317 1.1 maxv if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 2)) {
318 1.5 maxv return (kasan_shadow_1byte_isvalid(addr, code) &&
319 1.5 maxv kasan_shadow_1byte_isvalid(addr+1, code));
320 1.1 maxv }
321 1.1 maxv
322 1.1 maxv byte = kasan_md_addr_to_shad((void *)addr);
323 1.1 maxv last = ((addr + 1) & KASAN_SHADOW_MASK) + 1;
324 1.1 maxv
325 1.5 maxv if (__predict_true(*byte == 0 || last <= *byte)) {
326 1.5 maxv return true;
327 1.5 maxv }
328 1.5 maxv *code = *byte;
329 1.5 maxv return false;
330 1.1 maxv }
331 1.1 maxv
332 1.1 maxv static __always_inline bool
333 1.5 maxv kasan_shadow_4byte_isvalid(unsigned long addr, uint8_t *code)
334 1.1 maxv {
335 1.1 maxv int8_t *byte, last;
336 1.1 maxv
337 1.1 maxv if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 4)) {
338 1.5 maxv return (kasan_shadow_2byte_isvalid(addr, code) &&
339 1.5 maxv kasan_shadow_2byte_isvalid(addr+2, code));
340 1.1 maxv }
341 1.1 maxv
342 1.1 maxv byte = kasan_md_addr_to_shad((void *)addr);
343 1.1 maxv last = ((addr + 3) & KASAN_SHADOW_MASK) + 1;
344 1.1 maxv
345 1.5 maxv if (__predict_true(*byte == 0 || last <= *byte)) {
346 1.5 maxv return true;
347 1.5 maxv }
348 1.5 maxv *code = *byte;
349 1.5 maxv return false;
350 1.1 maxv }
351 1.1 maxv
352 1.1 maxv static __always_inline bool
353 1.5 maxv kasan_shadow_8byte_isvalid(unsigned long addr, uint8_t *code)
354 1.1 maxv {
355 1.1 maxv int8_t *byte, last;
356 1.1 maxv
357 1.1 maxv if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 8)) {
358 1.5 maxv return (kasan_shadow_4byte_isvalid(addr, code) &&
359 1.5 maxv kasan_shadow_4byte_isvalid(addr+4, code));
360 1.1 maxv }
361 1.1 maxv
362 1.1 maxv byte = kasan_md_addr_to_shad((void *)addr);
363 1.1 maxv last = ((addr + 7) & KASAN_SHADOW_MASK) + 1;
364 1.1 maxv
365 1.5 maxv if (__predict_true(*byte == 0 || last <= *byte)) {
366 1.5 maxv return true;
367 1.5 maxv }
368 1.5 maxv *code = *byte;
369 1.5 maxv return false;
370 1.1 maxv }
371 1.1 maxv
372 1.1 maxv static __always_inline bool
373 1.5 maxv kasan_shadow_Nbyte_isvalid(unsigned long addr, size_t size, uint8_t *code)
374 1.1 maxv {
375 1.1 maxv size_t i;
376 1.1 maxv
377 1.1 maxv for (i = 0; i < size; i++) {
378 1.5 maxv if (!kasan_shadow_1byte_isvalid(addr+i, code))
379 1.1 maxv return false;
380 1.1 maxv }
381 1.1 maxv
382 1.1 maxv return true;
383 1.1 maxv }
384 1.1 maxv
385 1.1 maxv static __always_inline void
386 1.1 maxv kasan_shadow_check(unsigned long addr, size_t size, bool write,
387 1.1 maxv unsigned long retaddr)
388 1.1 maxv {
389 1.5 maxv uint8_t code;
390 1.1 maxv bool valid;
391 1.1 maxv
392 1.1 maxv if (__predict_false(!kasan_enabled))
393 1.1 maxv return;
394 1.1 maxv if (__predict_false(size == 0))
395 1.1 maxv return;
396 1.1 maxv if (__predict_false(kasan_md_unsupported(addr)))
397 1.1 maxv return;
398 1.1 maxv
399 1.1 maxv if (__builtin_constant_p(size)) {
400 1.1 maxv switch (size) {
401 1.1 maxv case 1:
402 1.5 maxv valid = kasan_shadow_1byte_isvalid(addr, &code);
403 1.1 maxv break;
404 1.1 maxv case 2:
405 1.5 maxv valid = kasan_shadow_2byte_isvalid(addr, &code);
406 1.1 maxv break;
407 1.1 maxv case 4:
408 1.5 maxv valid = kasan_shadow_4byte_isvalid(addr, &code);
409 1.1 maxv break;
410 1.1 maxv case 8:
411 1.5 maxv valid = kasan_shadow_8byte_isvalid(addr, &code);
412 1.1 maxv break;
413 1.1 maxv default:
414 1.5 maxv valid = kasan_shadow_Nbyte_isvalid(addr, size, &code);
415 1.1 maxv break;
416 1.1 maxv }
417 1.1 maxv } else {
418 1.5 maxv valid = kasan_shadow_Nbyte_isvalid(addr, size, &code);
419 1.1 maxv }
420 1.1 maxv
421 1.1 maxv if (__predict_false(!valid)) {
422 1.5 maxv kasan_report(addr, size, write, retaddr, code);
423 1.1 maxv }
424 1.1 maxv }
425 1.1 maxv
426 1.1 maxv /* -------------------------------------------------------------------------- */
427 1.1 maxv
428 1.1 maxv void *
429 1.1 maxv kasan_memcpy(void *dst, const void *src, size_t len)
430 1.1 maxv {
431 1.1 maxv kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
432 1.1 maxv kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
433 1.1 maxv return __builtin_memcpy(dst, src, len);
434 1.1 maxv }
435 1.1 maxv
436 1.1 maxv int
437 1.1 maxv kasan_memcmp(const void *b1, const void *b2, size_t len)
438 1.1 maxv {
439 1.1 maxv kasan_shadow_check((unsigned long)b1, len, false, __RET_ADDR);
440 1.1 maxv kasan_shadow_check((unsigned long)b2, len, false, __RET_ADDR);
441 1.1 maxv return __builtin_memcmp(b1, b2, len);
442 1.1 maxv }
443 1.1 maxv
444 1.1 maxv void *
445 1.1 maxv kasan_memset(void *b, int c, size_t len)
446 1.1 maxv {
447 1.1 maxv kasan_shadow_check((unsigned long)b, len, true, __RET_ADDR);
448 1.1 maxv return __builtin_memset(b, c, len);
449 1.1 maxv }
450 1.1 maxv
451 1.12 maxv void *
452 1.12 maxv kasan_memmove(void *dst, const void *src, size_t len)
453 1.12 maxv {
454 1.12 maxv kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
455 1.12 maxv kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
456 1.12 maxv return __builtin_memmove(dst, src, len);
457 1.12 maxv }
458 1.12 maxv
459 1.1 maxv char *
460 1.1 maxv kasan_strcpy(char *dst, const char *src)
461 1.1 maxv {
462 1.1 maxv char *save = dst;
463 1.1 maxv
464 1.1 maxv while (1) {
465 1.1 maxv kasan_shadow_check((unsigned long)src, 1, false, __RET_ADDR);
466 1.1 maxv kasan_shadow_check((unsigned long)dst, 1, true, __RET_ADDR);
467 1.1 maxv *dst = *src;
468 1.1 maxv if (*src == '\0')
469 1.1 maxv break;
470 1.1 maxv src++, dst++;
471 1.1 maxv }
472 1.1 maxv
473 1.1 maxv return save;
474 1.1 maxv }
475 1.1 maxv
476 1.1 maxv int
477 1.1 maxv kasan_strcmp(const char *s1, const char *s2)
478 1.1 maxv {
479 1.1 maxv while (1) {
480 1.1 maxv kasan_shadow_check((unsigned long)s1, 1, false, __RET_ADDR);
481 1.1 maxv kasan_shadow_check((unsigned long)s2, 1, false, __RET_ADDR);
482 1.1 maxv if (*s1 != *s2)
483 1.1 maxv break;
484 1.1 maxv if (*s1 == '\0')
485 1.1 maxv return 0;
486 1.1 maxv s1++, s2++;
487 1.1 maxv }
488 1.1 maxv
489 1.1 maxv return (*(const unsigned char *)s1 - *(const unsigned char *)s2);
490 1.1 maxv }
491 1.1 maxv
492 1.1 maxv size_t
493 1.1 maxv kasan_strlen(const char *str)
494 1.1 maxv {
495 1.1 maxv const char *s;
496 1.1 maxv
497 1.1 maxv s = str;
498 1.1 maxv while (1) {
499 1.1 maxv kasan_shadow_check((unsigned long)s, 1, false, __RET_ADDR);
500 1.1 maxv if (*s == '\0')
501 1.1 maxv break;
502 1.1 maxv s++;
503 1.1 maxv }
504 1.1 maxv
505 1.1 maxv return (s - str);
506 1.1 maxv }
507 1.1 maxv
508 1.20 maxv char *
509 1.20 maxv kasan_strcat(char *dst, const char *src)
510 1.20 maxv {
511 1.20 maxv size_t ldst, lsrc;
512 1.20 maxv
513 1.20 maxv ldst = __builtin_strlen(dst);
514 1.20 maxv lsrc = __builtin_strlen(src);
515 1.20 maxv kasan_shadow_check((unsigned long)dst, ldst + lsrc + 1, true,
516 1.20 maxv __RET_ADDR);
517 1.20 maxv kasan_shadow_check((unsigned long)src, lsrc + 1, false,
518 1.20 maxv __RET_ADDR);
519 1.20 maxv
520 1.20 maxv return __builtin_strcat(dst, src);
521 1.20 maxv }
522 1.20 maxv
523 1.20 maxv char *
524 1.20 maxv kasan_strchr(const char *s, int c)
525 1.20 maxv {
526 1.20 maxv kasan_shadow_check((unsigned long)s, __builtin_strlen(s) + 1, false,
527 1.20 maxv __RET_ADDR);
528 1.20 maxv return __builtin_strchr(s, c);
529 1.20 maxv }
530 1.20 maxv
531 1.20 maxv char *
532 1.20 maxv kasan_strrchr(const char *s, int c)
533 1.20 maxv {
534 1.20 maxv kasan_shadow_check((unsigned long)s, __builtin_strlen(s) + 1, false,
535 1.20 maxv __RET_ADDR);
536 1.20 maxv return __builtin_strrchr(s, c);
537 1.20 maxv }
538 1.20 maxv
539 1.8 maxv #undef kcopy
540 1.7 maxv #undef copyinstr
541 1.7 maxv #undef copyoutstr
542 1.7 maxv #undef copyin
543 1.7 maxv
544 1.8 maxv int kasan_kcopy(const void *, void *, size_t);
545 1.7 maxv int kasan_copyinstr(const void *, void *, size_t, size_t *);
546 1.7 maxv int kasan_copyoutstr(const void *, void *, size_t, size_t *);
547 1.7 maxv int kasan_copyin(const void *, void *, size_t);
548 1.8 maxv int kcopy(const void *, void *, size_t);
549 1.7 maxv int copyinstr(const void *, void *, size_t, size_t *);
550 1.7 maxv int copyoutstr(const void *, void *, size_t, size_t *);
551 1.7 maxv int copyin(const void *, void *, size_t);
552 1.7 maxv
553 1.7 maxv int
554 1.8 maxv kasan_kcopy(const void *src, void *dst, size_t len)
555 1.8 maxv {
556 1.8 maxv kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
557 1.8 maxv kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
558 1.8 maxv return kcopy(src, dst, len);
559 1.8 maxv }
560 1.8 maxv
561 1.8 maxv int
562 1.7 maxv kasan_copyin(const void *uaddr, void *kaddr, size_t len)
563 1.7 maxv {
564 1.7 maxv kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
565 1.7 maxv return copyin(uaddr, kaddr, len);
566 1.7 maxv }
567 1.7 maxv
568 1.7 maxv int
569 1.7 maxv kasan_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
570 1.7 maxv {
571 1.7 maxv kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
572 1.7 maxv return copyinstr(uaddr, kaddr, len, done);
573 1.7 maxv }
574 1.7 maxv
575 1.7 maxv int
576 1.7 maxv kasan_copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
577 1.7 maxv {
578 1.7 maxv kasan_shadow_check((unsigned long)kaddr, len, false, __RET_ADDR);
579 1.7 maxv return copyoutstr(kaddr, uaddr, len, done);
580 1.7 maxv }
581 1.7 maxv
582 1.1 maxv /* -------------------------------------------------------------------------- */
583 1.1 maxv
584 1.16 maxv #undef _ucas_32
585 1.16 maxv #undef _ucas_32_mp
586 1.16 maxv #undef _ucas_64
587 1.16 maxv #undef _ucas_64_mp
588 1.16 maxv #undef _ufetch_8
589 1.16 maxv #undef _ufetch_16
590 1.16 maxv #undef _ufetch_32
591 1.16 maxv #undef _ufetch_64
592 1.16 maxv
593 1.16 maxv int _ucas_32(volatile uint32_t *, uint32_t, uint32_t, uint32_t *);
594 1.16 maxv int kasan__ucas_32(volatile uint32_t *, uint32_t, uint32_t, uint32_t *);
595 1.16 maxv int
596 1.16 maxv kasan__ucas_32(volatile uint32_t *uaddr, uint32_t old, uint32_t new,
597 1.16 maxv uint32_t *ret)
598 1.16 maxv {
599 1.16 maxv kasan_shadow_check((unsigned long)ret, sizeof(*ret), true,
600 1.16 maxv __RET_ADDR);
601 1.16 maxv return _ucas_32(uaddr, old, new, ret);
602 1.16 maxv }
603 1.16 maxv
604 1.16 maxv #ifdef __HAVE_UCAS_MP
605 1.16 maxv int _ucas_32_mp(volatile uint32_t *, uint32_t, uint32_t, uint32_t *);
606 1.16 maxv int kasan__ucas_32_mp(volatile uint32_t *, uint32_t, uint32_t, uint32_t *);
607 1.16 maxv int
608 1.16 maxv kasan__ucas_32_mp(volatile uint32_t *uaddr, uint32_t old, uint32_t new,
609 1.16 maxv uint32_t *ret)
610 1.16 maxv {
611 1.16 maxv kasan_shadow_check((unsigned long)ret, sizeof(*ret), true,
612 1.16 maxv __RET_ADDR);
613 1.16 maxv return _ucas_32_mp(uaddr, old, new, ret);
614 1.16 maxv }
615 1.16 maxv #endif
616 1.16 maxv
617 1.16 maxv #ifdef _LP64
618 1.16 maxv int _ucas_64(volatile uint64_t *, uint64_t, uint64_t, uint64_t *);
619 1.16 maxv int kasan__ucas_64(volatile uint64_t *, uint64_t, uint64_t, uint64_t *);
620 1.16 maxv int
621 1.16 maxv kasan__ucas_64(volatile uint64_t *uaddr, uint64_t old, uint64_t new,
622 1.16 maxv uint64_t *ret)
623 1.16 maxv {
624 1.16 maxv kasan_shadow_check((unsigned long)ret, sizeof(*ret), true,
625 1.16 maxv __RET_ADDR);
626 1.16 maxv return _ucas_64(uaddr, old, new, ret);
627 1.16 maxv }
628 1.16 maxv
629 1.16 maxv #ifdef __HAVE_UCAS_MP
630 1.16 maxv int _ucas_64_mp(volatile uint64_t *, uint64_t, uint64_t, uint64_t *);
631 1.16 maxv int kasan__ucas_64_mp(volatile uint64_t *, uint64_t, uint64_t, uint64_t *);
632 1.16 maxv int
633 1.16 maxv kasan__ucas_64_mp(volatile uint64_t *uaddr, uint64_t old, uint64_t new,
634 1.16 maxv uint64_t *ret)
635 1.16 maxv {
636 1.16 maxv kasan_shadow_check((unsigned long)ret, sizeof(*ret), true,
637 1.16 maxv __RET_ADDR);
638 1.16 maxv return _ucas_64_mp(uaddr, old, new, ret);
639 1.16 maxv }
640 1.16 maxv #endif
641 1.16 maxv #endif
642 1.16 maxv
643 1.16 maxv int _ufetch_8(const uint8_t *, uint8_t *);
644 1.16 maxv int kasan__ufetch_8(const uint8_t *, uint8_t *);
645 1.16 maxv int
646 1.16 maxv kasan__ufetch_8(const uint8_t *uaddr, uint8_t *valp)
647 1.16 maxv {
648 1.16 maxv kasan_shadow_check((unsigned long)valp, sizeof(*valp), true,
649 1.16 maxv __RET_ADDR);
650 1.16 maxv return _ufetch_8(uaddr, valp);
651 1.16 maxv }
652 1.16 maxv
653 1.16 maxv int _ufetch_16(const uint16_t *, uint16_t *);
654 1.16 maxv int kasan__ufetch_16(const uint16_t *, uint16_t *);
655 1.16 maxv int
656 1.16 maxv kasan__ufetch_16(const uint16_t *uaddr, uint16_t *valp)
657 1.16 maxv {
658 1.16 maxv kasan_shadow_check((unsigned long)valp, sizeof(*valp), true,
659 1.16 maxv __RET_ADDR);
660 1.16 maxv return _ufetch_16(uaddr, valp);
661 1.16 maxv }
662 1.16 maxv
663 1.16 maxv int _ufetch_32(const uint32_t *, uint32_t *);
664 1.16 maxv int kasan__ufetch_32(const uint32_t *, uint32_t *);
665 1.16 maxv int
666 1.16 maxv kasan__ufetch_32(const uint32_t *uaddr, uint32_t *valp)
667 1.16 maxv {
668 1.16 maxv kasan_shadow_check((unsigned long)valp, sizeof(*valp), true,
669 1.16 maxv __RET_ADDR);
670 1.16 maxv return _ufetch_32(uaddr, valp);
671 1.16 maxv }
672 1.16 maxv
673 1.16 maxv #ifdef _LP64
674 1.16 maxv int _ufetch_64(const uint64_t *, uint64_t *);
675 1.16 maxv int kasan__ufetch_64(const uint64_t *, uint64_t *);
676 1.16 maxv int
677 1.16 maxv kasan__ufetch_64(const uint64_t *uaddr, uint64_t *valp)
678 1.16 maxv {
679 1.16 maxv kasan_shadow_check((unsigned long)valp, sizeof(*valp), true,
680 1.16 maxv __RET_ADDR);
681 1.16 maxv return _ufetch_64(uaddr, valp);
682 1.16 maxv }
683 1.16 maxv #endif
684 1.16 maxv
685 1.16 maxv /* -------------------------------------------------------------------------- */
686 1.16 maxv
687 1.11 maxv #undef atomic_add_32
688 1.11 maxv #undef atomic_add_int
689 1.11 maxv #undef atomic_add_long
690 1.11 maxv #undef atomic_add_ptr
691 1.11 maxv #undef atomic_add_64
692 1.11 maxv #undef atomic_add_32_nv
693 1.11 maxv #undef atomic_add_int_nv
694 1.11 maxv #undef atomic_add_long_nv
695 1.11 maxv #undef atomic_add_ptr_nv
696 1.11 maxv #undef atomic_add_64_nv
697 1.11 maxv #undef atomic_and_32
698 1.11 maxv #undef atomic_and_uint
699 1.11 maxv #undef atomic_and_ulong
700 1.11 maxv #undef atomic_and_64
701 1.11 maxv #undef atomic_and_32_nv
702 1.11 maxv #undef atomic_and_uint_nv
703 1.11 maxv #undef atomic_and_ulong_nv
704 1.11 maxv #undef atomic_and_64_nv
705 1.11 maxv #undef atomic_or_32
706 1.11 maxv #undef atomic_or_uint
707 1.11 maxv #undef atomic_or_ulong
708 1.11 maxv #undef atomic_or_64
709 1.11 maxv #undef atomic_or_32_nv
710 1.11 maxv #undef atomic_or_uint_nv
711 1.11 maxv #undef atomic_or_ulong_nv
712 1.11 maxv #undef atomic_or_64_nv
713 1.11 maxv #undef atomic_cas_32
714 1.11 maxv #undef atomic_cas_uint
715 1.11 maxv #undef atomic_cas_ulong
716 1.11 maxv #undef atomic_cas_ptr
717 1.11 maxv #undef atomic_cas_64
718 1.11 maxv #undef atomic_cas_32_ni
719 1.11 maxv #undef atomic_cas_uint_ni
720 1.11 maxv #undef atomic_cas_ulong_ni
721 1.11 maxv #undef atomic_cas_ptr_ni
722 1.11 maxv #undef atomic_cas_64_ni
723 1.11 maxv #undef atomic_swap_32
724 1.11 maxv #undef atomic_swap_uint
725 1.11 maxv #undef atomic_swap_ulong
726 1.11 maxv #undef atomic_swap_ptr
727 1.11 maxv #undef atomic_swap_64
728 1.11 maxv #undef atomic_dec_32
729 1.11 maxv #undef atomic_dec_uint
730 1.11 maxv #undef atomic_dec_ulong
731 1.11 maxv #undef atomic_dec_ptr
732 1.11 maxv #undef atomic_dec_64
733 1.11 maxv #undef atomic_dec_32_nv
734 1.11 maxv #undef atomic_dec_uint_nv
735 1.11 maxv #undef atomic_dec_ulong_nv
736 1.11 maxv #undef atomic_dec_ptr_nv
737 1.11 maxv #undef atomic_dec_64_nv
738 1.11 maxv #undef atomic_inc_32
739 1.11 maxv #undef atomic_inc_uint
740 1.11 maxv #undef atomic_inc_ulong
741 1.11 maxv #undef atomic_inc_ptr
742 1.11 maxv #undef atomic_inc_64
743 1.11 maxv #undef atomic_inc_32_nv
744 1.11 maxv #undef atomic_inc_uint_nv
745 1.11 maxv #undef atomic_inc_ulong_nv
746 1.11 maxv #undef atomic_inc_ptr_nv
747 1.11 maxv #undef atomic_inc_64_nv
748 1.11 maxv
749 1.11 maxv #define ASAN_ATOMIC_FUNC_ADD(name, tret, targ1, targ2) \
750 1.11 maxv void atomic_add_##name(volatile targ1 *, targ2); \
751 1.11 maxv void kasan_atomic_add_##name(volatile targ1 *, targ2); \
752 1.11 maxv void kasan_atomic_add_##name(volatile targ1 *ptr, targ2 val) \
753 1.11 maxv { \
754 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
755 1.11 maxv __RET_ADDR); \
756 1.11 maxv atomic_add_##name(ptr, val); \
757 1.11 maxv } \
758 1.11 maxv tret atomic_add_##name##_nv(volatile targ1 *, targ2); \
759 1.11 maxv tret kasan_atomic_add_##name##_nv(volatile targ1 *, targ2); \
760 1.11 maxv tret kasan_atomic_add_##name##_nv(volatile targ1 *ptr, targ2 val) \
761 1.11 maxv { \
762 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
763 1.11 maxv __RET_ADDR); \
764 1.11 maxv return atomic_add_##name##_nv(ptr, val); \
765 1.11 maxv }
766 1.11 maxv
767 1.11 maxv #define ASAN_ATOMIC_FUNC_AND(name, tret, targ1, targ2) \
768 1.11 maxv void atomic_and_##name(volatile targ1 *, targ2); \
769 1.11 maxv void kasan_atomic_and_##name(volatile targ1 *, targ2); \
770 1.11 maxv void kasan_atomic_and_##name(volatile targ1 *ptr, targ2 val) \
771 1.11 maxv { \
772 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
773 1.11 maxv __RET_ADDR); \
774 1.11 maxv atomic_and_##name(ptr, val); \
775 1.11 maxv } \
776 1.11 maxv tret atomic_and_##name##_nv(volatile targ1 *, targ2); \
777 1.11 maxv tret kasan_atomic_and_##name##_nv(volatile targ1 *, targ2); \
778 1.11 maxv tret kasan_atomic_and_##name##_nv(volatile targ1 *ptr, targ2 val) \
779 1.11 maxv { \
780 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
781 1.11 maxv __RET_ADDR); \
782 1.11 maxv return atomic_and_##name##_nv(ptr, val); \
783 1.11 maxv }
784 1.11 maxv
785 1.11 maxv #define ASAN_ATOMIC_FUNC_OR(name, tret, targ1, targ2) \
786 1.11 maxv void atomic_or_##name(volatile targ1 *, targ2); \
787 1.11 maxv void kasan_atomic_or_##name(volatile targ1 *, targ2); \
788 1.11 maxv void kasan_atomic_or_##name(volatile targ1 *ptr, targ2 val) \
789 1.11 maxv { \
790 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
791 1.11 maxv __RET_ADDR); \
792 1.11 maxv atomic_or_##name(ptr, val); \
793 1.11 maxv } \
794 1.11 maxv tret atomic_or_##name##_nv(volatile targ1 *, targ2); \
795 1.11 maxv tret kasan_atomic_or_##name##_nv(volatile targ1 *, targ2); \
796 1.11 maxv tret kasan_atomic_or_##name##_nv(volatile targ1 *ptr, targ2 val) \
797 1.11 maxv { \
798 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
799 1.11 maxv __RET_ADDR); \
800 1.11 maxv return atomic_or_##name##_nv(ptr, val); \
801 1.11 maxv }
802 1.11 maxv
803 1.11 maxv #define ASAN_ATOMIC_FUNC_CAS(name, tret, targ1, targ2) \
804 1.11 maxv tret atomic_cas_##name(volatile targ1 *, targ2, targ2); \
805 1.11 maxv tret kasan_atomic_cas_##name(volatile targ1 *, targ2, targ2); \
806 1.11 maxv tret kasan_atomic_cas_##name(volatile targ1 *ptr, targ2 exp, targ2 new) \
807 1.11 maxv { \
808 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
809 1.11 maxv __RET_ADDR); \
810 1.11 maxv return atomic_cas_##name(ptr, exp, new); \
811 1.11 maxv } \
812 1.11 maxv tret atomic_cas_##name##_ni(volatile targ1 *, targ2, targ2); \
813 1.11 maxv tret kasan_atomic_cas_##name##_ni(volatile targ1 *, targ2, targ2); \
814 1.11 maxv tret kasan_atomic_cas_##name##_ni(volatile targ1 *ptr, targ2 exp, targ2 new) \
815 1.11 maxv { \
816 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
817 1.11 maxv __RET_ADDR); \
818 1.11 maxv return atomic_cas_##name##_ni(ptr, exp, new); \
819 1.11 maxv }
820 1.11 maxv
821 1.11 maxv #define ASAN_ATOMIC_FUNC_SWAP(name, tret, targ1, targ2) \
822 1.11 maxv tret atomic_swap_##name(volatile targ1 *, targ2); \
823 1.11 maxv tret kasan_atomic_swap_##name(volatile targ1 *, targ2); \
824 1.11 maxv tret kasan_atomic_swap_##name(volatile targ1 *ptr, targ2 val) \
825 1.11 maxv { \
826 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
827 1.11 maxv __RET_ADDR); \
828 1.11 maxv return atomic_swap_##name(ptr, val); \
829 1.11 maxv }
830 1.11 maxv
831 1.11 maxv #define ASAN_ATOMIC_FUNC_DEC(name, tret, targ1) \
832 1.11 maxv void atomic_dec_##name(volatile targ1 *); \
833 1.11 maxv void kasan_atomic_dec_##name(volatile targ1 *); \
834 1.11 maxv void kasan_atomic_dec_##name(volatile targ1 *ptr) \
835 1.11 maxv { \
836 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
837 1.11 maxv __RET_ADDR); \
838 1.11 maxv atomic_dec_##name(ptr); \
839 1.11 maxv } \
840 1.11 maxv tret atomic_dec_##name##_nv(volatile targ1 *); \
841 1.11 maxv tret kasan_atomic_dec_##name##_nv(volatile targ1 *); \
842 1.11 maxv tret kasan_atomic_dec_##name##_nv(volatile targ1 *ptr) \
843 1.11 maxv { \
844 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
845 1.11 maxv __RET_ADDR); \
846 1.11 maxv return atomic_dec_##name##_nv(ptr); \
847 1.11 maxv }
848 1.11 maxv
849 1.11 maxv #define ASAN_ATOMIC_FUNC_INC(name, tret, targ1) \
850 1.11 maxv void atomic_inc_##name(volatile targ1 *); \
851 1.11 maxv void kasan_atomic_inc_##name(volatile targ1 *); \
852 1.11 maxv void kasan_atomic_inc_##name(volatile targ1 *ptr) \
853 1.11 maxv { \
854 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
855 1.11 maxv __RET_ADDR); \
856 1.11 maxv atomic_inc_##name(ptr); \
857 1.11 maxv } \
858 1.11 maxv tret atomic_inc_##name##_nv(volatile targ1 *); \
859 1.11 maxv tret kasan_atomic_inc_##name##_nv(volatile targ1 *); \
860 1.11 maxv tret kasan_atomic_inc_##name##_nv(volatile targ1 *ptr) \
861 1.11 maxv { \
862 1.11 maxv kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
863 1.11 maxv __RET_ADDR); \
864 1.11 maxv return atomic_inc_##name##_nv(ptr); \
865 1.11 maxv }
866 1.11 maxv
867 1.11 maxv ASAN_ATOMIC_FUNC_ADD(32, uint32_t, uint32_t, int32_t);
868 1.11 maxv ASAN_ATOMIC_FUNC_ADD(64, uint64_t, uint64_t, int64_t);
869 1.11 maxv ASAN_ATOMIC_FUNC_ADD(int, unsigned int, unsigned int, int);
870 1.11 maxv ASAN_ATOMIC_FUNC_ADD(long, unsigned long, unsigned long, long);
871 1.11 maxv ASAN_ATOMIC_FUNC_ADD(ptr, void *, void, ssize_t);
872 1.11 maxv
873 1.11 maxv ASAN_ATOMIC_FUNC_AND(32, uint32_t, uint32_t, uint32_t);
874 1.11 maxv ASAN_ATOMIC_FUNC_AND(64, uint64_t, uint64_t, uint64_t);
875 1.11 maxv ASAN_ATOMIC_FUNC_AND(uint, unsigned int, unsigned int, unsigned int);
876 1.11 maxv ASAN_ATOMIC_FUNC_AND(ulong, unsigned long, unsigned long, unsigned long);
877 1.11 maxv
878 1.11 maxv ASAN_ATOMIC_FUNC_OR(32, uint32_t, uint32_t, uint32_t);
879 1.11 maxv ASAN_ATOMIC_FUNC_OR(64, uint64_t, uint64_t, uint64_t);
880 1.11 maxv ASAN_ATOMIC_FUNC_OR(uint, unsigned int, unsigned int, unsigned int);
881 1.11 maxv ASAN_ATOMIC_FUNC_OR(ulong, unsigned long, unsigned long, unsigned long);
882 1.11 maxv
883 1.11 maxv ASAN_ATOMIC_FUNC_CAS(32, uint32_t, uint32_t, uint32_t);
884 1.11 maxv ASAN_ATOMIC_FUNC_CAS(64, uint64_t, uint64_t, uint64_t);
885 1.11 maxv ASAN_ATOMIC_FUNC_CAS(uint, unsigned int, unsigned int, unsigned int);
886 1.11 maxv ASAN_ATOMIC_FUNC_CAS(ulong, unsigned long, unsigned long, unsigned long);
887 1.11 maxv ASAN_ATOMIC_FUNC_CAS(ptr, void *, void, void *);
888 1.11 maxv
889 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(32, uint32_t, uint32_t, uint32_t);
890 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(64, uint64_t, uint64_t, uint64_t);
891 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(uint, unsigned int, unsigned int, unsigned int);
892 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(ulong, unsigned long, unsigned long, unsigned long);
893 1.11 maxv ASAN_ATOMIC_FUNC_SWAP(ptr, void *, void, void *);
894 1.11 maxv
895 1.11 maxv ASAN_ATOMIC_FUNC_DEC(32, uint32_t, uint32_t)
896 1.11 maxv ASAN_ATOMIC_FUNC_DEC(64, uint64_t, uint64_t)
897 1.11 maxv ASAN_ATOMIC_FUNC_DEC(uint, unsigned int, unsigned int);
898 1.11 maxv ASAN_ATOMIC_FUNC_DEC(ulong, unsigned long, unsigned long);
899 1.11 maxv ASAN_ATOMIC_FUNC_DEC(ptr, void *, void);
900 1.11 maxv
901 1.11 maxv ASAN_ATOMIC_FUNC_INC(32, uint32_t, uint32_t)
902 1.11 maxv ASAN_ATOMIC_FUNC_INC(64, uint64_t, uint64_t)
903 1.11 maxv ASAN_ATOMIC_FUNC_INC(uint, unsigned int, unsigned int);
904 1.11 maxv ASAN_ATOMIC_FUNC_INC(ulong, unsigned long, unsigned long);
905 1.11 maxv ASAN_ATOMIC_FUNC_INC(ptr, void *, void);
906 1.11 maxv
907 1.11 maxv /* -------------------------------------------------------------------------- */
908 1.11 maxv
909 1.14 maxv #ifdef __HAVE_KASAN_INSTR_BUS
910 1.14 maxv
911 1.13 maxv #include <sys/bus.h>
912 1.13 maxv
913 1.13 maxv #undef bus_space_read_multi_1
914 1.13 maxv #undef bus_space_read_multi_2
915 1.13 maxv #undef bus_space_read_multi_4
916 1.13 maxv #undef bus_space_read_multi_8
917 1.13 maxv #undef bus_space_read_multi_stream_1
918 1.13 maxv #undef bus_space_read_multi_stream_2
919 1.13 maxv #undef bus_space_read_multi_stream_4
920 1.13 maxv #undef bus_space_read_multi_stream_8
921 1.13 maxv #undef bus_space_read_region_1
922 1.13 maxv #undef bus_space_read_region_2
923 1.13 maxv #undef bus_space_read_region_4
924 1.13 maxv #undef bus_space_read_region_8
925 1.13 maxv #undef bus_space_read_region_stream_1
926 1.13 maxv #undef bus_space_read_region_stream_2
927 1.13 maxv #undef bus_space_read_region_stream_4
928 1.13 maxv #undef bus_space_read_region_stream_8
929 1.13 maxv #undef bus_space_write_multi_1
930 1.13 maxv #undef bus_space_write_multi_2
931 1.13 maxv #undef bus_space_write_multi_4
932 1.13 maxv #undef bus_space_write_multi_8
933 1.13 maxv #undef bus_space_write_multi_stream_1
934 1.13 maxv #undef bus_space_write_multi_stream_2
935 1.13 maxv #undef bus_space_write_multi_stream_4
936 1.13 maxv #undef bus_space_write_multi_stream_8
937 1.13 maxv #undef bus_space_write_region_1
938 1.13 maxv #undef bus_space_write_region_2
939 1.13 maxv #undef bus_space_write_region_4
940 1.13 maxv #undef bus_space_write_region_8
941 1.13 maxv #undef bus_space_write_region_stream_1
942 1.13 maxv #undef bus_space_write_region_stream_2
943 1.13 maxv #undef bus_space_write_region_stream_4
944 1.13 maxv #undef bus_space_write_region_stream_8
945 1.13 maxv
946 1.13 maxv #define ASAN_BUS_READ_FUNC(bytes, bits) \
947 1.13 maxv void bus_space_read_multi_##bytes(bus_space_tag_t, bus_space_handle_t, \
948 1.13 maxv bus_size_t, uint##bits##_t *, bus_size_t); \
949 1.13 maxv void kasan_bus_space_read_multi_##bytes(bus_space_tag_t, \
950 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
951 1.13 maxv void kasan_bus_space_read_multi_##bytes(bus_space_tag_t tag, \
952 1.13 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \
953 1.13 maxv bus_size_t count) \
954 1.13 maxv { \
955 1.13 maxv kasan_shadow_check((uintptr_t)buf, \
956 1.13 maxv sizeof(uint##bits##_t) * count, false, __RET_ADDR); \
957 1.13 maxv bus_space_read_multi_##bytes(tag, hnd, size, buf, count); \
958 1.13 maxv } \
959 1.13 maxv void bus_space_read_multi_stream_##bytes(bus_space_tag_t, \
960 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
961 1.13 maxv void kasan_bus_space_read_multi_stream_##bytes(bus_space_tag_t, \
962 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
963 1.13 maxv void kasan_bus_space_read_multi_stream_##bytes(bus_space_tag_t tag, \
964 1.13 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \
965 1.13 maxv bus_size_t count) \
966 1.13 maxv { \
967 1.13 maxv kasan_shadow_check((uintptr_t)buf, \
968 1.13 maxv sizeof(uint##bits##_t) * count, false, __RET_ADDR); \
969 1.13 maxv bus_space_read_multi_stream_##bytes(tag, hnd, size, buf, count);\
970 1.13 maxv } \
971 1.13 maxv void bus_space_read_region_##bytes(bus_space_tag_t, bus_space_handle_t, \
972 1.13 maxv bus_size_t, uint##bits##_t *, bus_size_t); \
973 1.13 maxv void kasan_bus_space_read_region_##bytes(bus_space_tag_t, \
974 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
975 1.13 maxv void kasan_bus_space_read_region_##bytes(bus_space_tag_t tag, \
976 1.13 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \
977 1.13 maxv bus_size_t count) \
978 1.13 maxv { \
979 1.13 maxv kasan_shadow_check((uintptr_t)buf, \
980 1.13 maxv sizeof(uint##bits##_t) * count, false, __RET_ADDR); \
981 1.13 maxv bus_space_read_region_##bytes(tag, hnd, size, buf, count); \
982 1.13 maxv } \
983 1.13 maxv void bus_space_read_region_stream_##bytes(bus_space_tag_t, \
984 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
985 1.13 maxv void kasan_bus_space_read_region_stream_##bytes(bus_space_tag_t, \
986 1.13 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
987 1.13 maxv void kasan_bus_space_read_region_stream_##bytes(bus_space_tag_t tag, \
988 1.13 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \
989 1.13 maxv bus_size_t count) \
990 1.13 maxv { \
991 1.13 maxv kasan_shadow_check((uintptr_t)buf, \
992 1.13 maxv sizeof(uint##bits##_t) * count, false, __RET_ADDR); \
993 1.13 maxv bus_space_read_region_stream_##bytes(tag, hnd, size, buf, count);\
994 1.13 maxv }
995 1.13 maxv
996 1.13 maxv #define ASAN_BUS_WRITE_FUNC(bytes, bits) \
997 1.13 maxv void bus_space_write_multi_##bytes(bus_space_tag_t, bus_space_handle_t, \
998 1.13 maxv bus_size_t, const uint##bits##_t *, bus_size_t); \
999 1.13 maxv void kasan_bus_space_write_multi_##bytes(bus_space_tag_t, \
1000 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
1001 1.13 maxv void kasan_bus_space_write_multi_##bytes(bus_space_tag_t tag, \
1002 1.13 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \
1003 1.13 maxv bus_size_t count) \
1004 1.13 maxv { \
1005 1.13 maxv kasan_shadow_check((uintptr_t)buf, \
1006 1.13 maxv sizeof(uint##bits##_t) * count, true, __RET_ADDR); \
1007 1.13 maxv bus_space_write_multi_##bytes(tag, hnd, size, buf, count); \
1008 1.13 maxv } \
1009 1.13 maxv void bus_space_write_multi_stream_##bytes(bus_space_tag_t, \
1010 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
1011 1.13 maxv void kasan_bus_space_write_multi_stream_##bytes(bus_space_tag_t, \
1012 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
1013 1.13 maxv void kasan_bus_space_write_multi_stream_##bytes(bus_space_tag_t tag, \
1014 1.13 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \
1015 1.13 maxv bus_size_t count) \
1016 1.13 maxv { \
1017 1.13 maxv kasan_shadow_check((uintptr_t)buf, \
1018 1.13 maxv sizeof(uint##bits##_t) * count, true, __RET_ADDR); \
1019 1.13 maxv bus_space_write_multi_stream_##bytes(tag, hnd, size, buf, count);\
1020 1.13 maxv } \
1021 1.13 maxv void bus_space_write_region_##bytes(bus_space_tag_t, bus_space_handle_t,\
1022 1.13 maxv bus_size_t, const uint##bits##_t *, bus_size_t); \
1023 1.13 maxv void kasan_bus_space_write_region_##bytes(bus_space_tag_t, \
1024 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
1025 1.13 maxv void kasan_bus_space_write_region_##bytes(bus_space_tag_t tag, \
1026 1.13 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \
1027 1.13 maxv bus_size_t count) \
1028 1.13 maxv { \
1029 1.13 maxv kasan_shadow_check((uintptr_t)buf, \
1030 1.13 maxv sizeof(uint##bits##_t) * count, true, __RET_ADDR); \
1031 1.13 maxv bus_space_write_region_##bytes(tag, hnd, size, buf, count); \
1032 1.13 maxv } \
1033 1.13 maxv void bus_space_write_region_stream_##bytes(bus_space_tag_t, \
1034 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
1035 1.13 maxv void kasan_bus_space_write_region_stream_##bytes(bus_space_tag_t, \
1036 1.13 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
1037 1.13 maxv void kasan_bus_space_write_region_stream_##bytes(bus_space_tag_t tag, \
1038 1.13 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \
1039 1.13 maxv bus_size_t count) \
1040 1.13 maxv { \
1041 1.13 maxv kasan_shadow_check((uintptr_t)buf, \
1042 1.13 maxv sizeof(uint##bits##_t) * count, true, __RET_ADDR); \
1043 1.13 maxv bus_space_write_region_stream_##bytes(tag, hnd, size, buf, count);\
1044 1.13 maxv }
1045 1.13 maxv
1046 1.13 maxv ASAN_BUS_READ_FUNC(1, 8)
1047 1.13 maxv ASAN_BUS_READ_FUNC(2, 16)
1048 1.13 maxv ASAN_BUS_READ_FUNC(4, 32)
1049 1.13 maxv ASAN_BUS_READ_FUNC(8, 64)
1050 1.13 maxv
1051 1.13 maxv ASAN_BUS_WRITE_FUNC(1, 8)
1052 1.13 maxv ASAN_BUS_WRITE_FUNC(2, 16)
1053 1.13 maxv ASAN_BUS_WRITE_FUNC(4, 32)
1054 1.13 maxv ASAN_BUS_WRITE_FUNC(8, 64)
1055 1.13 maxv
1056 1.14 maxv #endif /* __HAVE_KASAN_INSTR_BUS */
1057 1.14 maxv
1058 1.13 maxv /* -------------------------------------------------------------------------- */
1059 1.13 maxv
1060 1.15 maxv #include <sys/mbuf.h>
1061 1.15 maxv
1062 1.15 maxv static void
1063 1.15 maxv kasan_dma_sync_linear(uint8_t *buf, bus_addr_t offset, bus_size_t len,
1064 1.15 maxv bool write, uintptr_t pc)
1065 1.15 maxv {
1066 1.15 maxv kasan_shadow_check((uintptr_t)(buf + offset), len, write, pc);
1067 1.15 maxv }
1068 1.15 maxv
1069 1.15 maxv static void
1070 1.15 maxv kasan_dma_sync_mbuf(struct mbuf *m, bus_addr_t offset, bus_size_t len,
1071 1.15 maxv bool write, uintptr_t pc)
1072 1.15 maxv {
1073 1.15 maxv bus_addr_t minlen;
1074 1.15 maxv
1075 1.15 maxv for (; m != NULL && len != 0; m = m->m_next) {
1076 1.15 maxv kasan_shadow_check((uintptr_t)m, sizeof(*m), false, pc);
1077 1.15 maxv
1078 1.15 maxv if (offset >= m->m_len) {
1079 1.15 maxv offset -= m->m_len;
1080 1.15 maxv continue;
1081 1.15 maxv }
1082 1.15 maxv
1083 1.15 maxv minlen = MIN(len, m->m_len - offset);
1084 1.15 maxv kasan_shadow_check((uintptr_t)(mtod(m, char *) + offset),
1085 1.15 maxv minlen, write, pc);
1086 1.15 maxv
1087 1.15 maxv offset = 0;
1088 1.15 maxv len -= minlen;
1089 1.15 maxv }
1090 1.15 maxv }
1091 1.15 maxv
1092 1.15 maxv static void
1093 1.15 maxv kasan_dma_sync_uio(struct uio *uio, bus_addr_t offset, bus_size_t len,
1094 1.15 maxv bool write, uintptr_t pc)
1095 1.15 maxv {
1096 1.15 maxv bus_size_t minlen, resid;
1097 1.15 maxv struct iovec *iov;
1098 1.15 maxv int i;
1099 1.15 maxv
1100 1.17 maxv kasan_shadow_check((uintptr_t)uio, sizeof(struct uio), false, pc);
1101 1.17 maxv
1102 1.17 maxv if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace))
1103 1.15 maxv return;
1104 1.15 maxv
1105 1.15 maxv resid = uio->uio_resid;
1106 1.15 maxv iov = uio->uio_iov;
1107 1.15 maxv
1108 1.15 maxv for (i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
1109 1.15 maxv kasan_shadow_check((uintptr_t)&iov[i], sizeof(iov[i]),
1110 1.15 maxv false, pc);
1111 1.15 maxv minlen = MIN(resid, iov[i].iov_len);
1112 1.15 maxv kasan_shadow_check((uintptr_t)iov[i].iov_base, minlen,
1113 1.15 maxv write, pc);
1114 1.15 maxv resid -= minlen;
1115 1.15 maxv }
1116 1.15 maxv }
1117 1.15 maxv
1118 1.15 maxv void
1119 1.15 maxv kasan_dma_sync(bus_dmamap_t map, bus_addr_t offset, bus_size_t len, int ops)
1120 1.15 maxv {
1121 1.15 maxv bool write = (ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTWRITE)) != 0;
1122 1.15 maxv
1123 1.15 maxv switch (map->dm_buftype) {
1124 1.15 maxv case KASAN_DMA_LINEAR:
1125 1.15 maxv kasan_dma_sync_linear(map->dm_buf, offset, len, write,
1126 1.15 maxv __RET_ADDR);
1127 1.15 maxv break;
1128 1.15 maxv case KASAN_DMA_MBUF:
1129 1.15 maxv kasan_dma_sync_mbuf(map->dm_buf, offset, len, write,
1130 1.15 maxv __RET_ADDR);
1131 1.15 maxv break;
1132 1.15 maxv case KASAN_DMA_UIO:
1133 1.15 maxv kasan_dma_sync_uio(map->dm_buf, offset, len, write,
1134 1.15 maxv __RET_ADDR);
1135 1.15 maxv break;
1136 1.15 maxv case KASAN_DMA_RAW:
1137 1.15 maxv break;
1138 1.15 maxv default:
1139 1.15 maxv panic("%s: impossible", __func__);
1140 1.15 maxv }
1141 1.15 maxv }
1142 1.15 maxv
1143 1.15 maxv void
1144 1.15 maxv kasan_dma_load(bus_dmamap_t map, void *buf, bus_size_t buflen, int type)
1145 1.15 maxv {
1146 1.15 maxv map->dm_buf = buf;
1147 1.15 maxv map->dm_buflen = buflen;
1148 1.15 maxv map->dm_buftype = type;
1149 1.15 maxv }
1150 1.15 maxv
1151 1.15 maxv /* -------------------------------------------------------------------------- */
1152 1.15 maxv
1153 1.1 maxv void __asan_register_globals(struct __asan_global *, size_t);
1154 1.1 maxv void __asan_unregister_globals(struct __asan_global *, size_t);
1155 1.1 maxv
1156 1.1 maxv void
1157 1.1 maxv __asan_register_globals(struct __asan_global *globals, size_t n)
1158 1.1 maxv {
1159 1.1 maxv size_t i;
1160 1.1 maxv
1161 1.1 maxv for (i = 0; i < n; i++) {
1162 1.2 maxv kasan_mark(globals[i].beg, globals[i].size,
1163 1.6 maxv globals[i].size_with_redzone, KASAN_GENERIC_REDZONE);
1164 1.1 maxv }
1165 1.1 maxv }
1166 1.1 maxv
1167 1.1 maxv void
1168 1.1 maxv __asan_unregister_globals(struct __asan_global *globals, size_t n)
1169 1.1 maxv {
1170 1.1 maxv /* never called */
1171 1.1 maxv }
1172 1.1 maxv
1173 1.1 maxv #define ASAN_LOAD_STORE(size) \
1174 1.1 maxv void __asan_load##size(unsigned long); \
1175 1.1 maxv void __asan_load##size(unsigned long addr) \
1176 1.1 maxv { \
1177 1.1 maxv kasan_shadow_check(addr, size, false, __RET_ADDR);\
1178 1.1 maxv } \
1179 1.1 maxv void __asan_load##size##_noabort(unsigned long); \
1180 1.1 maxv void __asan_load##size##_noabort(unsigned long addr) \
1181 1.1 maxv { \
1182 1.1 maxv kasan_shadow_check(addr, size, false, __RET_ADDR);\
1183 1.1 maxv } \
1184 1.1 maxv void __asan_store##size(unsigned long); \
1185 1.1 maxv void __asan_store##size(unsigned long addr) \
1186 1.1 maxv { \
1187 1.1 maxv kasan_shadow_check(addr, size, true, __RET_ADDR);\
1188 1.1 maxv } \
1189 1.1 maxv void __asan_store##size##_noabort(unsigned long); \
1190 1.1 maxv void __asan_store##size##_noabort(unsigned long addr) \
1191 1.1 maxv { \
1192 1.1 maxv kasan_shadow_check(addr, size, true, __RET_ADDR);\
1193 1.1 maxv }
1194 1.1 maxv
1195 1.1 maxv ASAN_LOAD_STORE(1);
1196 1.1 maxv ASAN_LOAD_STORE(2);
1197 1.1 maxv ASAN_LOAD_STORE(4);
1198 1.1 maxv ASAN_LOAD_STORE(8);
1199 1.1 maxv ASAN_LOAD_STORE(16);
1200 1.1 maxv
1201 1.1 maxv void __asan_loadN(unsigned long, size_t);
1202 1.1 maxv void __asan_loadN_noabort(unsigned long, size_t);
1203 1.1 maxv void __asan_storeN(unsigned long, size_t);
1204 1.1 maxv void __asan_storeN_noabort(unsigned long, size_t);
1205 1.1 maxv void __asan_handle_no_return(void);
1206 1.1 maxv
1207 1.1 maxv void
1208 1.1 maxv __asan_loadN(unsigned long addr, size_t size)
1209 1.1 maxv {
1210 1.1 maxv kasan_shadow_check(addr, size, false, __RET_ADDR);
1211 1.1 maxv }
1212 1.1 maxv
1213 1.1 maxv void
1214 1.1 maxv __asan_loadN_noabort(unsigned long addr, size_t size)
1215 1.1 maxv {
1216 1.1 maxv kasan_shadow_check(addr, size, false, __RET_ADDR);
1217 1.1 maxv }
1218 1.1 maxv
1219 1.1 maxv void
1220 1.1 maxv __asan_storeN(unsigned long addr, size_t size)
1221 1.1 maxv {
1222 1.1 maxv kasan_shadow_check(addr, size, true, __RET_ADDR);
1223 1.1 maxv }
1224 1.1 maxv
1225 1.1 maxv void
1226 1.1 maxv __asan_storeN_noabort(unsigned long addr, size_t size)
1227 1.1 maxv {
1228 1.1 maxv kasan_shadow_check(addr, size, true, __RET_ADDR);
1229 1.1 maxv }
1230 1.1 maxv
1231 1.1 maxv void
1232 1.1 maxv __asan_handle_no_return(void)
1233 1.1 maxv {
1234 1.1 maxv /* nothing */
1235 1.1 maxv }
1236 1.1 maxv
1237 1.1 maxv #define ASAN_SET_SHADOW(byte) \
1238 1.1 maxv void __asan_set_shadow_##byte(void *, size_t); \
1239 1.1 maxv void __asan_set_shadow_##byte(void *addr, size_t size) \
1240 1.1 maxv { \
1241 1.1 maxv __builtin_memset((void *)addr, 0x##byte, size); \
1242 1.1 maxv }
1243 1.1 maxv
1244 1.1 maxv ASAN_SET_SHADOW(00);
1245 1.1 maxv ASAN_SET_SHADOW(f1);
1246 1.1 maxv ASAN_SET_SHADOW(f2);
1247 1.1 maxv ASAN_SET_SHADOW(f3);
1248 1.1 maxv ASAN_SET_SHADOW(f5);
1249 1.1 maxv ASAN_SET_SHADOW(f8);
1250 1.4 maxv
1251 1.4 maxv void __asan_poison_stack_memory(const void *, size_t);
1252 1.4 maxv void __asan_unpoison_stack_memory(const void *, size_t);
1253 1.4 maxv
1254 1.17 maxv void
1255 1.17 maxv __asan_poison_stack_memory(const void *addr, size_t size)
1256 1.4 maxv {
1257 1.4 maxv size = roundup(size, KASAN_SHADOW_SCALE_SIZE);
1258 1.4 maxv kasan_shadow_Nbyte_fill(addr, size, KASAN_USE_AFTER_SCOPE);
1259 1.4 maxv }
1260 1.4 maxv
1261 1.17 maxv void
1262 1.17 maxv __asan_unpoison_stack_memory(const void *addr, size_t size)
1263 1.4 maxv {
1264 1.4 maxv kasan_shadow_Nbyte_markvalid(addr, size);
1265 1.4 maxv }
1266 1.19 maxv
1267 1.19 maxv void __asan_alloca_poison(const void *, size_t);
1268 1.19 maxv void __asan_allocas_unpoison(const void *, const void *);
1269 1.19 maxv
1270 1.19 maxv void __asan_alloca_poison(const void *addr, size_t size)
1271 1.19 maxv {
1272 1.19 maxv const void *l, *r;
1273 1.19 maxv
1274 1.19 maxv KASSERT((vaddr_t)addr % KASAN_ALLOCA_SCALE_SIZE == 0);
1275 1.19 maxv
1276 1.19 maxv l = (const uint8_t *)addr - KASAN_ALLOCA_SCALE_SIZE;
1277 1.19 maxv r = (const uint8_t *)addr + roundup(size, KASAN_ALLOCA_SCALE_SIZE);
1278 1.19 maxv
1279 1.19 maxv kasan_shadow_Nbyte_fill(l, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_LEFT);
1280 1.19 maxv kasan_mark(addr, size, roundup(size, KASAN_ALLOCA_SCALE_SIZE),
1281 1.19 maxv KASAN_STACK_MID);
1282 1.19 maxv kasan_shadow_Nbyte_fill(r, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_RIGHT);
1283 1.19 maxv }
1284 1.19 maxv
1285 1.19 maxv void __asan_allocas_unpoison(const void *stkbegin, const void *stkend)
1286 1.19 maxv {
1287 1.19 maxv size_t size;
1288 1.19 maxv
1289 1.19 maxv if (__predict_false(!stkbegin))
1290 1.19 maxv return;
1291 1.19 maxv if (__predict_false((uintptr_t)stkbegin > (uintptr_t)stkend))
1292 1.19 maxv return;
1293 1.19 maxv size = (uintptr_t)stkend - (uintptr_t)stkbegin;
1294 1.19 maxv
1295 1.19 maxv kasan_shadow_Nbyte_fill(stkbegin, size, 0);
1296 1.19 maxv }
1297