xref: /xnu-8792.81.2/san/memory/kasan-classic.h (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions /*
2*19c3b8c2SApple OSS Distributions  * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
3*19c3b8c2SApple OSS Distributions  *
4*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*19c3b8c2SApple OSS Distributions  *
6*19c3b8c2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*19c3b8c2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*19c3b8c2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*19c3b8c2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*19c3b8c2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*19c3b8c2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*19c3b8c2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*19c3b8c2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*19c3b8c2SApple OSS Distributions  *
15*19c3b8c2SApple OSS Distributions  * Please obtain a copy of the License at
16*19c3b8c2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*19c3b8c2SApple OSS Distributions  *
18*19c3b8c2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*19c3b8c2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*19c3b8c2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*19c3b8c2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*19c3b8c2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*19c3b8c2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*19c3b8c2SApple OSS Distributions  * limitations under the License.
25*19c3b8c2SApple OSS Distributions  *
26*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*19c3b8c2SApple OSS Distributions  */
28*19c3b8c2SApple OSS Distributions 
29*19c3b8c2SApple OSS Distributions #ifndef _KASAN_CLASSIC_H_
30*19c3b8c2SApple OSS Distributions #define _KASAN_CLASSIC_H_
31*19c3b8c2SApple OSS Distributions 
32*19c3b8c2SApple OSS Distributions #include <mach/mach_types.h>
33*19c3b8c2SApple OSS Distributions 
34*19c3b8c2SApple OSS Distributions /* Catch obvious mismatches */
35*19c3b8c2SApple OSS Distributions #if KASAN && !__has_feature(address_sanitizer)
36*19c3b8c2SApple OSS Distributions #error "KASAN selected, but not enabled in compiler"
37*19c3b8c2SApple OSS Distributions #endif
38*19c3b8c2SApple OSS Distributions 
39*19c3b8c2SApple OSS Distributions #if !KASAN && __has_feature(address_sanitizer)
40*19c3b8c2SApple OSS Distributions #error "ASAN enabled in compiler, but kernel is not configured for KASAN"
41*19c3b8c2SApple OSS Distributions #endif
42*19c3b8c2SApple OSS Distributions 
43*19c3b8c2SApple OSS Distributions /* Granularity is 8 bytes */
44*19c3b8c2SApple OSS Distributions #define KASAN_SIZE_ALIGNMENT    0x7UL
45*19c3b8c2SApple OSS Distributions 
46*19c3b8c2SApple OSS Distributions typedef uintptr_t uptr;
47*19c3b8c2SApple OSS Distributions 
48*19c3b8c2SApple OSS Distributions #define KASAN_DEBUG  0
49*19c3b8c2SApple OSS Distributions #define KASAN_KALLOC 1
50*19c3b8c2SApple OSS Distributions #define KASAN_ZALLOC 1
51*19c3b8c2SApple OSS Distributions #define KASAN_DYNAMIC_BLACKLIST 1
52*19c3b8c2SApple OSS Distributions /*
53*19c3b8c2SApple OSS Distributions  * KASAN features and config
54*19c3b8c2SApple OSS Distributions  */
55*19c3b8c2SApple OSS Distributions #define FAKESTACK     1
56*19c3b8c2SApple OSS Distributions /* KASAN_KALLOC defined in kasan.h */
57*19c3b8c2SApple OSS Distributions /* KASAN_ZALLOC defined in kasan.h */
58*19c3b8c2SApple OSS Distributions #define FAKESTACK_QUARANTINE (1 && FAKESTACK)
59*19c3b8c2SApple OSS Distributions 
60*19c3b8c2SApple OSS Distributions #define QUARANTINE_ENTRIES 5000
61*19c3b8c2SApple OSS Distributions #define QUARANTINE_MAXSIZE MiB(10)
62*19c3b8c2SApple OSS Distributions 
63*19c3b8c2SApple OSS Distributions /*
64*19c3b8c2SApple OSS Distributions  * KASAN-CLASSIC shadow table entry values.
65*19c3b8c2SApple OSS Distributions  *  - 0:                the full 8 bytes are addressable
66*19c3b8c2SApple OSS Distributions  *  - [1,7]:            the byte is partially addressable (as many valid bytes
67*19c3b8c2SApple OSS Distributions  *                      as specified)
68*19c3b8c2SApple OSS Distributions  *  - 0xFx, 0xAC, 0xE9: byte is not addressable and poisoned somehow.
69*19c3b8c2SApple OSS Distributions  */
70*19c3b8c2SApple OSS Distributions #define ASAN_VALID          0x00
71*19c3b8c2SApple OSS Distributions #define ASAN_PARTIAL1       0x01
72*19c3b8c2SApple OSS Distributions #define ASAN_PARTIAL2       0x02
73*19c3b8c2SApple OSS Distributions #define ASAN_PARTIAL3       0x03
74*19c3b8c2SApple OSS Distributions #define ASAN_PARTIAL4       0x04
75*19c3b8c2SApple OSS Distributions #define ASAN_PARTIAL5       0x05
76*19c3b8c2SApple OSS Distributions #define ASAN_PARTIAL6       0x06
77*19c3b8c2SApple OSS Distributions #define ASAN_PARTIAL7       0x07
78*19c3b8c2SApple OSS Distributions #define ASAN_ARRAY_COOKIE   0xac
79*19c3b8c2SApple OSS Distributions #define ASAN_STACK_RZ       0xf0
80*19c3b8c2SApple OSS Distributions #define ASAN_STACK_LEFT_RZ  0xf1
81*19c3b8c2SApple OSS Distributions #define ASAN_STACK_MID_RZ   0xf2
82*19c3b8c2SApple OSS Distributions #define ASAN_STACK_RIGHT_RZ 0xf3
83*19c3b8c2SApple OSS Distributions #define ASAN_STACK_FREED    0xf5
84*19c3b8c2SApple OSS Distributions #define ASAN_STACK_OOSCOPE  0xf8
85*19c3b8c2SApple OSS Distributions #define ASAN_GLOBAL_RZ      0xf9
86*19c3b8c2SApple OSS Distributions #define ASAN_HEAP_RZ        0xe9
87*19c3b8c2SApple OSS Distributions #define ASAN_HEAP_LEFT_RZ   0xfa
88*19c3b8c2SApple OSS Distributions #define ASAN_HEAP_RIGHT_RZ  0xfb
89*19c3b8c2SApple OSS Distributions #define ASAN_HEAP_FREED     0xfd
90*19c3b8c2SApple OSS Distributions 
91*19c3b8c2SApple OSS Distributions #define KASAN_GUARD_SIZE (16)
92*19c3b8c2SApple OSS Distributions #define KASAN_GUARD_PAD  (KASAN_GUARD_SIZE * 2)
93*19c3b8c2SApple OSS Distributions 
94*19c3b8c2SApple OSS Distributions #define KASAN_HEAP_ZALLOC    0
95*19c3b8c2SApple OSS Distributions #define KASAN_HEAP_KALLOC    1
96*19c3b8c2SApple OSS Distributions #define KASAN_HEAP_FAKESTACK 2
97*19c3b8c2SApple OSS Distributions #define KASAN_HEAP_TYPES     3
98*19c3b8c2SApple OSS Distributions 
99*19c3b8c2SApple OSS Distributions __BEGIN_DECLS
100*19c3b8c2SApple OSS Distributions /* KASAN-CLASSIC zalloc hooks */
101*19c3b8c2SApple OSS Distributions vm_size_t kasan_alloc_resize(vm_size_t);
102*19c3b8c2SApple OSS Distributions vm_address_t kasan_alloc(vm_offset_t, vm_size_t, vm_size_t, vm_size_t);
103*19c3b8c2SApple OSS Distributions vm_address_t kasan_realloc(vm_offset_t, vm_size_t, vm_size_t, vm_size_t);
104*19c3b8c2SApple OSS Distributions vm_address_t kasan_dealloc(vm_offset_t, vm_size_t *);
105*19c3b8c2SApple OSS Distributions vm_size_t kasan_user_size(vm_offset_t);
106*19c3b8c2SApple OSS Distributions void kasan_check_free(vm_offset_t, vm_size_t, unsigned);
107*19c3b8c2SApple OSS Distributions 
108*19c3b8c2SApple OSS Distributions /* KASAN-CLASSIC Quarantine (zalloc) hooks */
109*19c3b8c2SApple OSS Distributions void kasan_free(void **, vm_size_t *, int, zone_t *, vm_size_t);
110*19c3b8c2SApple OSS Distributions void __asan_poison_cxx_array_cookie(uptr);
111*19c3b8c2SApple OSS Distributions uptr __asan_load_cxx_array_cookie(uptr *);
112*19c3b8c2SApple OSS Distributions void kasan_unpoison_cxx_array_cookie(void *);
113*19c3b8c2SApple OSS Distributions 
114*19c3b8c2SApple OSS Distributions __END_DECLS
115*19c3b8c2SApple OSS Distributions #endif /* _KASAN_CLASSIC_H_ */
116