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_INTERNAL_H_ 30 #define _KASAN_INTERNAL_H_ 31 32 #include <stdbool.h> 33 #include <mach/mach_vm.h> 34 #include <kern/zalloc.h> 35 #include <sys/sysctl.h> 36 37 typedef uintptr_t uptr; 38 #define MiB(x) ((x) * 1024UL * 1024) 39 #define BIT(x) (1U << (x)) 40 41 /* Sanity checks */ 42 #ifndef KASAN 43 #error KASAN undefined 44 #endif 45 46 #ifndef KASAN_OFFSET 47 #error KASAN_OFFSET undefined 48 #endif 49 50 #ifndef KASAN_SCALE 51 #error KASAN_SCALE undefined 52 #endif 53 54 #if defined(__x86_64__) 55 # define _JBLEN ((9 * 2) + 3 + 16) 56 #elif defined(__arm64__) 57 # define _JBLEN ((14 + 8 + 2) * 2) 58 #else 59 # error "Unknown arch" 60 #endif 61 62 #if KASAN_DEBUG 63 #define NOINLINE OS_NOINLINE 64 #else 65 #define NOINLINE 66 #endif 67 #define ALWAYS_INLINE inline __attribute__((always_inline)) 68 #define CLANG_MIN_VERSION(x) (defined(__apple_build_version__) && (__apple_build_version__ >= (x))) 69 70 #if KASAN_CLASSIC 71 #define KASAN_MODEL_STR "kasan-classic" 72 #define KASAN_STRIP_ADDR(_x) (_x) 73 #elif KASAN_TBI 74 #define KASAN_MODEL_STR "kasan-tbi" 75 #define KASAN_STRIP_ADDR(_x) (VM_KERNEL_STRIP_UPTR(_x)) 76 #else 77 #error "No kasan model specified" 78 #endif /* KASAN_CLASSIC || KASAN_TBI */ 79 80 extern vm_address_t kernel_vbase; 81 extern vm_address_t kernel_vtop; 82 extern unsigned shadow_pages_used; 83 84 /* boot-arg configurable */ 85 extern unsigned kasan_enabled; 86 extern int fakestack_enabled; 87 extern bool report_suppressed_checks; 88 89 #define KASAN_GRANULE (1UL << KASAN_SCALE) 90 #define KASAN_GRANULE_MASK (KASAN_GRANULE - 1UL) 91 #define kasan_granule_trunc(x) (x & ~KASAN_GRANULE_MASK) 92 #define kasan_granule_round(x) ((x + KASAN_GRANULE_MASK) & ~KASAN_GRANULE_MASK) 93 #define kasan_granule_partial(x) (x & KASAN_GRANULE_MASK) 94 95 #define ADDRESS_FOR_SHADOW(x) (((KASAN_STRIP_ADDR(x)) - KASAN_OFFSET) << KASAN_SCALE) 96 #define SHADOW_FOR_ADDRESS(x) (uint8_t *)(((KASAN_STRIP_ADDR(x)) >> KASAN_SCALE) + KASAN_OFFSET) 97 98 enum __attribute__((flag_enum)) kasan_access_types { 99 /* Common to all KASAN versions */ 100 TYPE_LOAD = BIT(0), /* regular memory load */ 101 TYPE_STORE = BIT(1), /* regular store */ 102 TYPE_MEMR = BIT(2), /* memory intrinsic (read) */ 103 TYPE_MEMW = BIT(3), /* memory intrinsic (write) */ 104 TYPE_STRR = BIT(4), /* string intrinsic (read) */ 105 TYPE_STRW = BIT(5), /* string intrinsic (write) */ 106 107 /* KASAN-classic specific */ 108 TYPE_ZFREE = BIT(6), /* zfree() */ 109 TYPE_FSFREE = BIT(7), /* fakestack free */ 110 111 TYPE_UAF = BIT(12), 112 TYPE_POISON_GLOBAL = BIT(13), 113 TYPE_POISON_HEAP = BIT(14), 114 /* no TYPE_POISON_STACK, because the runtime does not control stack poisoning */ 115 TYPE_TEST = BIT(15), 116 117 /* masks */ 118 TYPE_MEM = TYPE_MEMR | TYPE_MEMW, /* memory intrinsics */ 119 TYPE_STR = TYPE_STRR | TYPE_STRW, /* string intrinsics */ 120 TYPE_READ = TYPE_LOAD | TYPE_MEMR | TYPE_STRR, /* all reads */ 121 TYPE_WRITE = TYPE_STORE | TYPE_MEMW | TYPE_STRW, /* all writes */ 122 TYPE_RW = TYPE_READ | TYPE_WRITE, /* reads and writes */ 123 TYPE_FREE = TYPE_ZFREE | TYPE_FSFREE, 124 TYPE_NORMAL = TYPE_RW | TYPE_FREE, 125 TYPE_DYNAMIC = TYPE_NORMAL | TYPE_UAF, 126 TYPE_POISON = TYPE_POISON_GLOBAL | TYPE_POISON_HEAP, 127 TYPE_ALL = ~0U, 128 }; 129 130 enum kasan_violation_types { 131 REASON_POISONED = 0, /* read or write of poisoned data */ 132 REASON_BAD_METADATA = 1, /* incorrect kasan metadata */ 133 REASON_INVALID_SIZE = 2, /* free size did not match alloc size */ 134 REASON_MOD_AFTER_FREE = 3, /* object modified after free */ 135 REASON_MOD_OOB = 4, /* out of bounds modification of object */ 136 }; 137 138 typedef enum kasan_access_types access_t; 139 typedef enum kasan_violation_types violation_t; 140 141 /* 142 * KASAN may support different shadow table formats and different checking 143 * strategies. _impl functions are called from the format-independent 144 * kasan code to the format dependent implementations. 145 */ 146 void kasan_impl_report_internal(uptr, uptr, access_t, violation_t, bool); 147 void kasan_impl_poison_range(vm_offset_t, vm_size_t, uint8_t); 148 void kasan_impl_kdp_disable(void); 149 void kasan_impl_init(void); 150 void kasan_impl_late_init(void); 151 void kasan_impl_fill_valid_range(uintptr_t, size_t); 152 153 /* 154 * Poisoning comes from KASAN CLASSIC nomenclature. KASAN CLASSIC is based on 155 * identifying valid memory vs poisoned memory (memory that shouldn't be accessed). 156 * This terminology isn't great for KASAN TBI, but is kept for compatibility. 157 */ 158 void kasan_poison(vm_offset_t, vm_size_t, vm_size_t, vm_size_t, uint8_t); 159 160 /* 161 * Runtime checking. kasan_check_range() is consumed by the inlined 162 * instrumentation. See kasan-helper.c 163 */ 164 bool kasan_check_enabled(access_t); 165 bool kasan_impl_check_enabled(access_t); 166 void kasan_check_range(const void *, size_t, access_t); 167 168 /* dynamic blacklist */ 169 void kasan_init_dybl(void); 170 bool kasan_is_blacklisted(access_t); 171 void kasan_dybl_load_kext(uintptr_t, const char *); 172 void kasan_dybl_unload_kext(uintptr_t); 173 174 /* arch-specific interface */ 175 void kasan_arch_init(void); 176 bool kasan_is_shadow_mapped(uintptr_t); 177 178 /* Locking */ 179 void kasan_lock_init(void); 180 void kasan_lock(boolean_t *); 181 void kasan_unlock(boolean_t); 182 bool kasan_lock_held(thread_t); 183 184 /* Subsystem helpers */ 185 void kasan_init_fakestack(void); 186 187 /* 188 * Global variables need to be explicitly handled at runtime, both for xnu 189 * and for KEXTs. 190 */ 191 void kasan_init_globals(vm_offset_t, vm_size_t); 192 193 /* 194 * Handle KASAN detected issues. If modifying kasan_crash_report(), remember 195 * that is called by the instrumentation as well, see kasan-helper.c. 196 */ 197 void kasan_violation(uintptr_t, size_t, access_t, violation_t); 198 size_t kasan_impl_decode_issue(char *, size_t, uptr, uptr, access_t, violation_t); 199 void NOINLINE OS_NORETURN kasan_crash_report(uptr, uptr, access_t, violation_t); 200 201 void kasan_handle_test(void); 202 203 SYSCTL_DECL(kasan); 204 SYSCTL_DECL(_kern_kasan); 205 206 #endif /* _KASAN_INTERNAL_H_ */ 207