]> git.saurik.com Git - apple/xnu.git/blob - san/kasan_internal.h
xnu-4570.41.2.tar.gz
[apple/xnu.git] / san / kasan_internal.h
1 /*
2 * Copyright (c) 2000-2014 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_INTERNAL_H_
30 #define _KASAN_INTERNAL_H_
31
32 #include <stdbool.h>
33 #include <mach/mach_vm.h>
34 #include <kern/zalloc.h>
35
36 typedef uintptr_t uptr;
37
38 #define MiB(x) ((x) * 1024UL * 1024)
39
40 /*
41 * KASAN features and config
42 */
43 #define KASAN_DEBUG 1
44 #define FAKESTACK 1
45 #define MEMINTRINSICS 1
46 /* KASAN_KALLOC defined in kasan.h */
47 /* KASAN_ZALLOC defined in kasan.h */
48 #define FAKESTACK_QUARANTINE (1 && FAKESTACK)
49
50 #define QUARANTINE_ENTRIES 5000
51 #define QUARANTINE_MAXSIZE MiB(10)
52
53 /*
54 * The amount of physical memory stolen by KASan at boot to back the shadow memory
55 * and page tables. Larger memory systems need to steal proportionally less.
56 */
57 #ifdef __arm64__
58 /* Works out at about 25% of 512 MiB and 15% of 3GiB system */
59 # define STOLEN_MEM_PERCENT 13UL
60 # define STOLEN_MEM_BYTES MiB(62)
61 #else
62 # define STOLEN_MEM_PERCENT 25UL
63 # define STOLEN_MEM_BYTES 0
64 #endif
65
66 #ifndef KASAN
67 # error KASAN undefined
68 #endif
69
70 #ifndef KASAN_SHIFT
71 # error KASAN_SHIFT undefined
72 #endif
73
74 #define ADDRESS_FOR_SHADOW(x) (((x) - KASAN_SHIFT) << 3)
75 #define SHADOW_FOR_ADDRESS(x) (uint8_t *)(((x) >> 3) + KASAN_SHIFT)
76
77 #define NOINLINE __attribute__ ((noinline))
78 #define ALWAYS_INLINE inline __attribute__((always_inline))
79
80 #define CLANG_MIN_VERSION(x) (defined(__apple_build_version__) && (__apple_build_version__ >= (x)))
81
82 #define BIT(x) (1U << (x))
83
84 enum kasan_access_type {
85 /* exactly one of these bits must be set */
86 TYPE_LOAD = BIT(0),
87 TYPE_STORE = BIT(1),
88 TYPE_KFREE = BIT(2),
89 TYPE_ZFREE = BIT(3),
90 TYPE_FSFREE = BIT(4), /* fakestack free */
91 TYPE_MEMLD = BIT(5), /* memory intrinsic - load */
92 TYPE_MEMSTR = BIT(6), /* memory intrinsic - store */
93 TYPE_STRINGLD = BIT(7), /* string intrinsic - load */
94 TYPE_STRINGSTR = BIT(8), /* string intrinsic - store */
95 TYPE_TEST = BIT(15),
96
97 /* masks */
98 TYPE_LDSTR = TYPE_LOAD|TYPE_STORE, /* regular loads and stores */
99 TYPE_FREE = TYPE_KFREE|TYPE_ZFREE|TYPE_FSFREE,
100 TYPE_MEM = TYPE_MEMLD|TYPE_MEMSTR,
101 TYPE_STRING = TYPE_STRINGLD|TYPE_STRINGSTR,
102 TYPE_LOAD_ALL = TYPE_LOAD|TYPE_MEMLD|TYPE_STRINGLD,
103 TYPE_STORE_ALL = TYPE_STORE|TYPE_MEMSTR|TYPE_STRINGSTR,
104 TYPE_ALL = ~0U
105 };
106
107 bool kasan_range_poisoned(vm_offset_t base, vm_size_t size, vm_offset_t *first_invalid);
108 void kasan_check_range(const void *x, size_t sz, unsigned access_type);
109 void kasan_test(int testno, int fail);
110 void kasan_handle_test(void);
111 void kasan_unpoison_curstack(void);
112 void kasan_free_internal(void **addrp, vm_size_t *sizep, int type, zone_t *, vm_size_t user_size, int locked, bool doquarantine);
113 void kasan_poison(vm_offset_t base, vm_size_t size, vm_size_t leftrz, vm_size_t rightrz, uint8_t flags);
114 void kasan_unpoison(void *base, vm_size_t size);
115 void kasan_lock(boolean_t *b);
116 void kasan_unlock(boolean_t b);
117 void kasan_init_fakestack(void);
118
119 /* dynamic blacklist */
120 void kasan_init_dybl(void);
121 bool kasan_is_blacklisted(unsigned type);
122 void kasan_dybl_load_kext(uintptr_t addr, const char *kextname);
123 void kasan_dybl_unload_kext(uintptr_t addr);
124
125 /* arch-specific interface */
126 void kasan_arch_init(void);
127
128 extern vm_address_t kernel_vbase;
129 extern vm_address_t kernel_vtop;
130
131 extern long shadow_pages_used;
132
133 /* Describes the source location where a global is defined. */
134 struct asan_global_source_location {
135 const char *filename;
136 int line_no;
137 int column_no;
138 };
139
140 /* Describes an instrumented global variable. */
141 struct asan_global {
142 uptr addr;
143 uptr size;
144 uptr size_with_redzone;
145 const char *name;
146 const char *module;
147 uptr has_dynamic_init;
148 struct asan_global_source_location *location;
149 #if CLANG_MIN_VERSION(8020000)
150 uptr odr_indicator;
151 #endif
152 };
153
154 #if defined(__x86_64__)
155 # define _JBLEN ((9 * 2) + 3 + 16)
156 #elif defined(__arm64__)
157 # define _JBLEN ((14 + 8 + 2) * 2)
158 #else
159 # error "Unknown arch"
160 #endif
161
162 typedef int jmp_buf[_JBLEN];
163 void _longjmp(jmp_buf env, int val);
164 int _setjmp(jmp_buf env);
165
166 #endif /* _KASAN_INTERNAL_H_ */