1 /*
2 * Copyright (c) 2000-2020 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_TBI_H_
30 #define _KASAN_TBI_H_
31
32 #include <mach/mach_types.h>
33
34 /* Catch obvious mismatches */
35 #if KASAN && !__has_feature(hwaddress_sanitizer)
36 #error "KASAN selected, but not enabled in compiler"
37 #endif
38
39 #if !KASAN && __has_feature(hwaddress_sanitizer)
40 #error "hwaddress_sanitizer enabled in compiler, but kernel is not configured for KASAN"
41 #endif
42
43 /* old-style configs. */
44 #define KASAN_DEBUG 0
45 #define KASAN_DYNAMIC_BLACKLIST 1
46 #define KASAN_FAKESTACK 0
47
48 /* Granularity is 16 bytes */
49 #define KASAN_SIZE_ALIGNMENT 0xFUL
50 /* TBI reserves the topmost 8 bits, we use the lower 4 of this reserved range */
51 #define KASAN_TBI_ADDR_SIZE 56
52 #define KASAN_TBI_TAG_SIZE 4
53 #define KASAN_TBI_PAD_SIZE 4
54
55 union kasan_tbi_ptr {
56 long value;
57
58 struct {
59 long ptr_no_tbi: KASAN_TBI_ADDR_SIZE;
60 uint8_t ptr_tag: KASAN_TBI_TAG_SIZE;
61 long ptr_pad: KASAN_TBI_PAD_SIZE;
62 };
63 };
64
65 static inline long
kasan_tbi_tag_ptr(long ptr,uint8_t tag)66 kasan_tbi_tag_ptr(long ptr, uint8_t tag)
67 {
68 union kasan_tbi_ptr p = {
69 .value = ptr,
70 };
71
72 p.ptr_tag = tag;
73 return p.value;
74 }
75
76 static inline uint8_t
kasan_tbi_get_tag(long ptr)77 kasan_tbi_get_tag(long ptr)
78 {
79 union kasan_tbi_ptr p = {
80 .value = ptr,
81 };
82
83 return p.ptr_tag;
84 }
85
86 /*
87 * KASAN_TBI inline insturmentation emits a brk instruction as a violation
88 * report. The ESR value encodes both the access type and size.
89 * osfmk/arm64/sleh.c needs to now the right ranges to proxy this information
90 * back to the kasan runtime.
91 */
92 #define KASAN_TBI_ESR_BASE (0x900)
93 #define KASAN_TBI_ESR_WRITE (0x10)
94 #define KASAN_TBI_ESR_IGNORE (0x20)
95 #define KASAN_TBI_ESR_SIZE_MASK (0xF)
96 #define KASAN_TBI_ESR_TOP (KASAN_TBI_ESR_BASE | KASAN_TBI_ESR_WRITE | \
97 KASAN_TBI_ESR_IGNORE | KASAN_TBI_ESR_SIZE_MASK)
98 #define KASAN_TBI_GET_SIZE(x) (1 << ((x) & KASAN_TBI_ESR_SIZE_MASK))
99
100 uint8_t kasan_tbi_get_memory_tag(vm_offset_t);
101 uint8_t *kasan_tbi_get_tag_address(vm_offset_t);
102 void kasan_tbi_copy_tags(vm_offset_t, vm_offset_t, vm_size_t);
103 vm_offset_t kasan_tbi_tag_zalloc(vm_offset_t, vm_size_t, vm_size_t, boolean_t);
104 vm_offset_t kasan_tbi_tag_zfree(vm_offset_t, vm_offset_t, boolean_t);
105 vm_offset_t kasan_tbi_tag_zalloc_default(vm_offset_t, vm_size_t, boolean_t);
106 vm_offset_t kasan_tbi_fix_address_tag(vm_offset_t);
107 vm_offset_t kasan_tbi_tag_large_alloc(vm_offset_t, vm_size_t, vm_size_t);
108 vm_offset_t kasan_tbi_tag_large_free(vm_offset_t, vm_size_t);
109
110 void kasan_handle_brk_failure(vm_offset_t, uint16_t);
111
112 #endif /* _KASAN_TBI_H_ */
113