xref: /xnu-8020.101.4/san/memory/kasan-classic.h (revision e7776783b89a353188416a9a346c6cdb4928faad)
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_KALLOC 1
50 #define KASAN_ZALLOC 1
51 #define KASAN_DYNAMIC_BLACKLIST 1
52 /*
53  * KASAN features and config
54  */
55 #define FAKESTACK     1
56 /* KASAN_KALLOC defined in kasan.h */
57 /* KASAN_ZALLOC defined in kasan.h */
58 #define FAKESTACK_QUARANTINE (1 && FAKESTACK)
59 
60 #define QUARANTINE_ENTRIES 5000
61 #define QUARANTINE_MAXSIZE MiB(10)
62 
63 /*
64  * KASAN-CLASSIC shadow table entry values.
65  *  - 0:                the full 8 bytes are addressable
66  *  - [1,7]:            the byte is partially addressable (as many valid bytes
67  *                      as specified)
68  *  - 0xFx, 0xAC, 0xE9: byte is not addressable and poisoned somehow.
69  */
70 #define ASAN_VALID          0x00
71 #define ASAN_PARTIAL1       0x01
72 #define ASAN_PARTIAL2       0x02
73 #define ASAN_PARTIAL3       0x03
74 #define ASAN_PARTIAL4       0x04
75 #define ASAN_PARTIAL5       0x05
76 #define ASAN_PARTIAL6       0x06
77 #define ASAN_PARTIAL7       0x07
78 #define ASAN_ARRAY_COOKIE   0xac
79 #define ASAN_STACK_RZ       0xf0
80 #define ASAN_STACK_LEFT_RZ  0xf1
81 #define ASAN_STACK_MID_RZ   0xf2
82 #define ASAN_STACK_RIGHT_RZ 0xf3
83 #define ASAN_STACK_FREED    0xf5
84 #define ASAN_STACK_OOSCOPE  0xf8
85 #define ASAN_GLOBAL_RZ      0xf9
86 #define ASAN_HEAP_RZ        0xe9
87 #define ASAN_HEAP_LEFT_RZ   0xfa
88 #define ASAN_HEAP_RIGHT_RZ  0xfb
89 #define ASAN_HEAP_FREED     0xfd
90 
91 #define KASAN_GUARD_SIZE (16)
92 #define KASAN_GUARD_PAD  (KASAN_GUARD_SIZE * 2)
93 
94 #define KASAN_HEAP_ZALLOC    0
95 #define KASAN_HEAP_KALLOC    1
96 #define KASAN_HEAP_FAKESTACK 2
97 #define KASAN_HEAP_TYPES     3
98 
99 __BEGIN_DECLS
100 /* KASAN-CLASSIC zalloc hooks */
101 vm_size_t kasan_alloc_resize(vm_size_t);
102 vm_address_t kasan_alloc(vm_offset_t, vm_size_t, vm_size_t, vm_size_t);
103 vm_address_t kasan_realloc(vm_offset_t, vm_size_t, vm_size_t, vm_size_t);
104 vm_address_t kasan_dealloc(vm_offset_t, vm_size_t *);
105 vm_size_t kasan_user_size(vm_offset_t);
106 void kasan_check_free(vm_offset_t, vm_size_t, unsigned);
107 void kasan_free(void **, vm_size_t *, int, zone_t *, vm_size_t, bool);
108 
109 /* KASAN-CLASSIC Quarantine (zalloc) hooks */
110 void kasan_free(void **, vm_size_t *, int, zone_t *, vm_size_t, bool);
111 void __asan_poison_cxx_array_cookie(uptr);
112 uptr __asan_load_cxx_array_cookie(uptr *);
113 void kasan_unpoison_cxx_array_cookie(void *);
114 
115 __END_DECLS
116 #endif /* _KASAN_CLASSIC_H_ */
117