xref: /xnu-8792.41.9/san/memory/kasan-tbi.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
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_KALLOC 0
46 #define KASAN_ZALLOC 0
47 #define KASAN_DYNAMIC_BLACKLIST 1
48 #define KASAN_FAKESTACK 0
49 
50 /* Granularity is 16 bytes */
51 #define KASAN_SIZE_ALIGNMENT        0xFUL
52 /* TBI reserves the topmost 8 bits, we use the lower 4 of this reserved range */
53 #define KASAN_TBI_ADDR_SIZE         56
54 #define KASAN_TBI_TAG_SIZE          4
55 #define KASAN_TBI_PAD_SIZE          4
56 
57 union kasan_tbi_ptr {
58 	long value;
59 
60 	struct {
61 		long ptr_no_tbi: KASAN_TBI_ADDR_SIZE;
62 		uint8_t ptr_tag:    KASAN_TBI_TAG_SIZE;
63 		long ptr_pad:    KASAN_TBI_PAD_SIZE;
64 	};
65 };
66 
67 static inline long
kasan_tbi_tag_ptr(long ptr,uint8_t tag)68 kasan_tbi_tag_ptr(long ptr, uint8_t tag)
69 {
70 	union kasan_tbi_ptr p = {
71 		.value = ptr,
72 	};
73 
74 	p.ptr_tag = tag;
75 	return p.value;
76 }
77 
78 static inline uint8_t
kasan_tbi_get_tag(long ptr)79 kasan_tbi_get_tag(long ptr)
80 {
81 	union kasan_tbi_ptr p = {
82 		.value = ptr,
83 	};
84 
85 	return p.ptr_tag;
86 }
87 
88 /*
89  * KASAN_TBI inline insturmentation emits a brk instruction as a violation
90  * report. The ESR value encodes both the access type and size.
91  * osfmk/arm64/sleh.c needs to now the right ranges to proxy this information
92  * back to the kasan runtime.
93  */
94 #define KASAN_TBI_ESR_BASE          (0x900)
95 #define KASAN_TBI_ESR_WRITE         (0x10)
96 #define KASAN_TBI_ESR_IGNORE        (0x20)
97 #define KASAN_TBI_ESR_SIZE_MASK     (0xF)
98 #define KASAN_TBI_ESR_TOP           (KASAN_TBI_ESR_BASE | KASAN_TBI_ESR_WRITE |     \
99 	                            KASAN_TBI_ESR_IGNORE | KASAN_TBI_ESR_SIZE_MASK)
100 #define KASAN_TBI_GET_SIZE(x)       (1 << ((x) & KASAN_TBI_ESR_SIZE_MASK))
101 
102 uint8_t kasan_tbi_get_memory_tag(vm_offset_t);
103 uint8_t *kasan_tbi_get_tag_address(vm_offset_t);
104 void kasan_tbi_copy_tags(vm_offset_t, vm_offset_t, vm_size_t);
105 vm_offset_t kasan_tbi_tag_zalloc(vm_offset_t, vm_size_t, vm_size_t, boolean_t);
106 vm_offset_t kasan_tbi_tag_zfree(vm_offset_t, vm_offset_t, boolean_t);
107 vm_offset_t kasan_tbi_tag_zalloc_default(vm_offset_t, vm_size_t, boolean_t);
108 vm_offset_t kasan_tbi_fix_address_tag(vm_offset_t);
109 vm_offset_t kasan_tbi_tag_large_alloc(vm_offset_t, vm_size_t, vm_size_t);
110 vm_offset_t kasan_tbi_tag_large_free(vm_offset_t, vm_size_t);
111 
112 void kasan_handle_brk_failure(vm_offset_t, uint16_t);
113 
114 #endif /* _KASAN_TBI_H_ */
115