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