xref: /xnu-8796.121.2/san/memory/kasan-classic.h (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1 /*
2  * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef _KASAN_CLASSIC_H_
30 #define _KASAN_CLASSIC_H_
31 
32 #include <mach/mach_types.h>
33 
34 /* Catch obvious mismatches */
35 #if KASAN && !__has_feature(address_sanitizer)
36 #error "KASAN selected, but not enabled in compiler"
37 #endif
38 
39 #if !KASAN && __has_feature(address_sanitizer)
40 #error "ASAN enabled in compiler, but kernel is not configured for KASAN"
41 #endif
42 
43 /* Granularity is 8 bytes */
44 #define KASAN_SIZE_ALIGNMENT    0x7UL
45 
46 typedef uintptr_t uptr;
47 
48 #define KASAN_DEBUG  0
49 #define KASAN_DYNAMIC_BLACKLIST 1
50 #define KASAN_FAKESTACK 1
51 /*
52  * KASAN features and config
53  */
54 #define FAKESTACK_QUARANTINE (1 && KASAN_FAKESTACK)
55 #define QUARANTINE_ENTRIES 5000
56 #define QUARANTINE_MAXSIZE MiB(4)
57 
58 /*
59  * KASAN-CLASSIC shadow table entry values.
60  *  - 0:                the full 8 bytes are addressable
61  *  - [1,7]:            the byte is partially addressable (as many valid bytes
62  *                      as specified)
63  *  - 0xFx, 0xAC, 0xE9: byte is not addressable and poisoned somehow.
64  */
65 #define ASAN_VALID          0x00
66 #define ASAN_PARTIAL1       0x01
67 #define ASAN_PARTIAL2       0x02
68 #define ASAN_PARTIAL3       0x03
69 #define ASAN_PARTIAL4       0x04
70 #define ASAN_PARTIAL5       0x05
71 #define ASAN_PARTIAL6       0x06
72 #define ASAN_PARTIAL7       0x07
73 #define ASAN_ARRAY_COOKIE   0xac // kAsanArrayCookieMagic
74 #define ASAN_STACK_RZ       0xf0 // XNU only
75 #define ASAN_STACK_LEFT_RZ  0xf1 // kAsanStackLeftRedzoneMagic
76 #define ASAN_STACK_MID_RZ   0xf2 // kAsanStackMidRedzoneMagic
77 #define ASAN_STACK_RIGHT_RZ 0xf3 // kAsanStackRightRedzoneMagic
78 #define ASAN_STACK_FREED    0xf5 // kAsanStackAfterReturnMagic
79 //                          0xf6 // kAsanInitializationOrderMagic
80 //                          0xf7 // kAsanUserPoisonedMemoryMagic
81 #define ASAN_STACK_OOSCOPE  0xf8 // kAsanStackUseAfterScopeMagic
82 #define ASAN_GLOBAL_RZ      0xf9 // kAsanGlobalRedzoneMagic
83 #define ASAN_HEAP_RZ        0xe9 // XNU only, not used in shadow
84 #define ASAN_HEAP_LEFT_RZ   0xfa // kAsanHeapLeftRedzoneMagic
85 #define ASAN_HEAP_RIGHT_RZ  0xfb // XNU only
86 //                          0xfc // kAsanContiguousContainerOOBMagic
87 #define ASAN_HEAP_FREED     0xfd // kAsanHeapFreeMagic
88 //                          0xfe // kAsanInternalHeapMagic
89 
90 #define KASAN_GUARD_SIZE (16)
91 #define KASAN_GUARD_PAD  (KASAN_GUARD_SIZE * 2)
92 
93 #define KASAN_HEAP_ZALLOC    0
94 #define KASAN_HEAP_FAKESTACK 1
95 #define KASAN_HEAP_TYPES     2
96 
97 __BEGIN_DECLS
98 
99 /* KASAN-CLASSIC zalloc hooks */
100 
101 extern void kasan_zmem_add(
102 	vm_address_t            addr,
103 	vm_size_t               size,
104 	vm_offset_t             esize,
105 	vm_offset_t             offs,
106 	vm_offset_t             rzsize);
107 
108 extern void kasan_zmem_remove(
109 	vm_address_t            addr,
110 	vm_size_t               size,
111 	vm_offset_t             esize,
112 	vm_offset_t             offs,
113 	vm_offset_t             rzsize);
114 
115 extern void kasan_alloc(
116 	vm_address_t            addr,
117 	vm_size_t               size,
118 	vm_size_t               usize,
119 	vm_size_t               rzsize,
120 	bool                    percpu,
121 	void                   *fp);
122 
123 extern void kasan_free(
124 	vm_address_t            addr,
125 	vm_size_t               size,
126 	vm_size_t               usize,
127 	vm_size_t               rzsize,
128 	bool                    percpu,
129 	void                   *fp);
130 
131 extern void kasan_alloc_large(
132 	vm_address_t            addr,
133 	vm_size_t               req_size);
134 
135 extern vm_size_t kasan_user_size(
136 	vm_address_t            addr);
137 
138 extern void kasan_check_alloc(
139 	vm_address_t            addr,
140 	vm_size_t               size,
141 	vm_size_t               usize);
142 
143 struct kasan_quarantine_result {
144 	vm_address_t            addr;
145 	struct zone            *zone;
146 };
147 
148 extern struct kasan_quarantine_result kasan_quarantine(
149 	vm_address_t            addr,
150 	vm_size_t               size);
151 
152 /* in zalloc.c */
153 extern vm_size_t kasan_quarantine_resolve(
154 	vm_address_t            addr,
155 	struct zone           **zonep);
156 
157 __END_DECLS
158 
159 #endif /* _KASAN_CLASSIC_H_ */
160