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