1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 2021 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions *
15*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions *
18*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions * limitations under the License.
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions */
28*2c2f96dcSApple OSS Distributions
29*2c2f96dcSApple OSS Distributions #include <string.h>
30*2c2f96dcSApple OSS Distributions #include <kern/thread.h>
31*2c2f96dcSApple OSS Distributions #include <mach/mach_vm.h>
32*2c2f96dcSApple OSS Distributions #include <mach/mach_types.h>
33*2c2f96dcSApple OSS Distributions #include <vm/vm_map.h>
34*2c2f96dcSApple OSS Distributions #include <libkern/libkern.h>
35*2c2f96dcSApple OSS Distributions #include <kern/backtrace.h>
36*2c2f96dcSApple OSS Distributions
37*2c2f96dcSApple OSS Distributions #include "kasan_internal.h"
38*2c2f96dcSApple OSS Distributions
39*2c2f96dcSApple OSS Distributions bool report_suppressed_checks = false;
40*2c2f96dcSApple OSS Distributions /*
41*2c2f96dcSApple OSS Distributions * KASAN violation reporting. Decode the access violation and pretty print
42*2c2f96dcSApple OSS Distributions * the violation reason in the panic message.
43*2c2f96dcSApple OSS Distributions */
44*2c2f96dcSApple OSS Distributions #define CRASH_CONTEXT_BEFORE 5
45*2c2f96dcSApple OSS Distributions #define CRASH_CONTEXT_AFTER 5
46*2c2f96dcSApple OSS Distributions #define CONTEXT_BLOCK_SIZE 16
47*2c2f96dcSApple OSS Distributions #define CONTEXT_BLOCK_MASK (CONTEXT_BLOCK_SIZE - 1)
48*2c2f96dcSApple OSS Distributions
49*2c2f96dcSApple OSS Distributions /* Pretty print the shadow table describing memory around the faulting access */
50*2c2f96dcSApple OSS Distributions static size_t
kasan_dump_shadow(uptr p,char * buf,size_t len)51*2c2f96dcSApple OSS Distributions kasan_dump_shadow(uptr p, char *buf, size_t len)
52*2c2f96dcSApple OSS Distributions {
53*2c2f96dcSApple OSS Distributions int i, j;
54*2c2f96dcSApple OSS Distributions size_t n = 0;
55*2c2f96dcSApple OSS Distributions int before = CRASH_CONTEXT_BEFORE;
56*2c2f96dcSApple OSS Distributions int after = CRASH_CONTEXT_AFTER;
57*2c2f96dcSApple OSS Distributions
58*2c2f96dcSApple OSS Distributions uptr shadow = (uptr)SHADOW_FOR_ADDRESS(p);
59*2c2f96dcSApple OSS Distributions uptr shadow_p = shadow;
60*2c2f96dcSApple OSS Distributions uptr shadow_page = vm_map_round_page(shadow_p, PAGE_MASK);
61*2c2f96dcSApple OSS Distributions
62*2c2f96dcSApple OSS Distributions /* rewind to start of context block */
63*2c2f96dcSApple OSS Distributions shadow &= ~((uptr)CONTEXT_BLOCK_MASK);
64*2c2f96dcSApple OSS Distributions shadow -= CONTEXT_BLOCK_SIZE * before;
65*2c2f96dcSApple OSS Distributions
66*2c2f96dcSApple OSS Distributions n += scnprintf(buf + n, len - n,
67*2c2f96dcSApple OSS Distributions " Shadow 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
68*2c2f96dcSApple OSS Distributions
69*2c2f96dcSApple OSS Distributions for (i = 0; i < 1 + before + after; i++, shadow += CONTEXT_BLOCK_SIZE) {
70*2c2f96dcSApple OSS Distributions if ((vm_map_round_page(shadow, PAGE_MASK) != shadow_page) && !kasan_is_shadow_mapped(shadow)) {
71*2c2f96dcSApple OSS Distributions /* avoid unmapped shadow when crossing page boundaries */
72*2c2f96dcSApple OSS Distributions continue;
73*2c2f96dcSApple OSS Distributions }
74*2c2f96dcSApple OSS Distributions
75*2c2f96dcSApple OSS Distributions n += scnprintf(buf + n, len - n, " %16lx:", shadow);
76*2c2f96dcSApple OSS Distributions
77*2c2f96dcSApple OSS Distributions char *left = " ";
78*2c2f96dcSApple OSS Distributions char *right;
79*2c2f96dcSApple OSS Distributions
80*2c2f96dcSApple OSS Distributions for (j = 0; j < CONTEXT_BLOCK_SIZE; j++) {
81*2c2f96dcSApple OSS Distributions uint8_t *x = (uint8_t *)(shadow + j);
82*2c2f96dcSApple OSS Distributions
83*2c2f96dcSApple OSS Distributions right = " ";
84*2c2f96dcSApple OSS Distributions if ((uptr)x == shadow_p) {
85*2c2f96dcSApple OSS Distributions left = "[";
86*2c2f96dcSApple OSS Distributions right = "]";
87*2c2f96dcSApple OSS Distributions } else if ((uptr)(x + 1) == shadow_p) {
88*2c2f96dcSApple OSS Distributions right = "";
89*2c2f96dcSApple OSS Distributions }
90*2c2f96dcSApple OSS Distributions
91*2c2f96dcSApple OSS Distributions n += scnprintf(buf + n, len - n, "%s%02x%s", left, (unsigned)*x, right);
92*2c2f96dcSApple OSS Distributions left = "";
93*2c2f96dcSApple OSS Distributions }
94*2c2f96dcSApple OSS Distributions n += scnprintf(buf + n, len - n, "\n");
95*2c2f96dcSApple OSS Distributions }
96*2c2f96dcSApple OSS Distributions
97*2c2f96dcSApple OSS Distributions n += scnprintf(buf + n, len - n, "\n");
98*2c2f96dcSApple OSS Distributions return n;
99*2c2f96dcSApple OSS Distributions }
100*2c2f96dcSApple OSS Distributions
101*2c2f96dcSApple OSS Distributions #define KASAN_REPORT_BUFSIZE 4096
102*2c2f96dcSApple OSS Distributions static void
kasan_report_internal(uptr p,uptr width,access_t access,violation_t reason,bool dopanic)103*2c2f96dcSApple OSS Distributions kasan_report_internal(uptr p, uptr width, access_t access, violation_t reason, bool dopanic)
104*2c2f96dcSApple OSS Distributions {
105*2c2f96dcSApple OSS Distributions const size_t len = KASAN_REPORT_BUFSIZE;
106*2c2f96dcSApple OSS Distributions static char buf[KASAN_REPORT_BUFSIZE];
107*2c2f96dcSApple OSS Distributions size_t n = 0;
108*2c2f96dcSApple OSS Distributions
109*2c2f96dcSApple OSS Distributions buf[0] = '\0';
110*2c2f96dcSApple OSS Distributions
111*2c2f96dcSApple OSS Distributions n += kasan_impl_decode_issue(buf, len, p, width, access, reason);
112*2c2f96dcSApple OSS Distributions n += kasan_dump_shadow(p, buf + n, len - n);
113*2c2f96dcSApple OSS Distributions
114*2c2f96dcSApple OSS Distributions dopanic ? panic("%s", buf) : printf("%s", buf);
115*2c2f96dcSApple OSS Distributions }
116*2c2f96dcSApple OSS Distributions
117*2c2f96dcSApple OSS Distributions static void
kasan_panic_report_internal(uptr p,uptr width,access_t access,violation_t reason)118*2c2f96dcSApple OSS Distributions kasan_panic_report_internal(uptr p, uptr width, access_t access, violation_t reason)
119*2c2f96dcSApple OSS Distributions {
120*2c2f96dcSApple OSS Distributions kasan_report_internal(p, width, access, reason, true);
121*2c2f96dcSApple OSS Distributions }
122*2c2f96dcSApple OSS Distributions
123*2c2f96dcSApple OSS Distributions static void
kasan_log_report_internal(uptr p,uptr width,access_t access,violation_t reason)124*2c2f96dcSApple OSS Distributions kasan_log_report_internal(uptr p, uptr width, access_t access, violation_t reason)
125*2c2f96dcSApple OSS Distributions {
126*2c2f96dcSApple OSS Distributions kasan_report_internal(p, width, access, reason, false);
127*2c2f96dcSApple OSS Distributions }
128*2c2f96dcSApple OSS Distributions
129*2c2f96dcSApple OSS Distributions
130*2c2f96dcSApple OSS Distributions /* Pretty print a crash report. */
131*2c2f96dcSApple OSS Distributions void NOINLINE OS_NORETURN
kasan_crash_report(uptr p,uptr width,access_t access,violation_t reason)132*2c2f96dcSApple OSS Distributions kasan_crash_report(uptr p, uptr width, access_t access, violation_t reason)
133*2c2f96dcSApple OSS Distributions {
134*2c2f96dcSApple OSS Distributions kasan_handle_test();
135*2c2f96dcSApple OSS Distributions kasan_panic_report_internal(p, width, access, reason);
136*2c2f96dcSApple OSS Distributions __builtin_unreachable(); /* we cant handle this returning anyway */
137*2c2f96dcSApple OSS Distributions }
138*2c2f96dcSApple OSS Distributions
139*2c2f96dcSApple OSS Distributions /* Like kasan_crash_report(), but just log a failure. */
140*2c2f96dcSApple OSS Distributions static void
kasan_log_report(uptr p,uptr width,access_t access,violation_t reason)141*2c2f96dcSApple OSS Distributions kasan_log_report(uptr p, uptr width, access_t access, violation_t reason)
142*2c2f96dcSApple OSS Distributions {
143*2c2f96dcSApple OSS Distributions const size_t len = 256;
144*2c2f96dcSApple OSS Distributions char buf[len];
145*2c2f96dcSApple OSS Distributions size_t l = 0;
146*2c2f96dcSApple OSS Distributions uint32_t nframes = 14;
147*2c2f96dcSApple OSS Distributions uintptr_t frames[nframes];
148*2c2f96dcSApple OSS Distributions uintptr_t *bt = frames;
149*2c2f96dcSApple OSS Distributions
150*2c2f96dcSApple OSS Distributions kasan_log_report_internal(p, width, access, reason);
151*2c2f96dcSApple OSS Distributions
152*2c2f96dcSApple OSS Distributions struct backtrace_control ctl = {
153*2c2f96dcSApple OSS Distributions /* ignore current frame */
154*2c2f96dcSApple OSS Distributions .btc_frame_addr = (uintptr_t)__builtin_frame_address(0),
155*2c2f96dcSApple OSS Distributions };
156*2c2f96dcSApple OSS Distributions nframes = backtrace(bt, nframes, &ctl, NULL);
157*2c2f96dcSApple OSS Distributions
158*2c2f96dcSApple OSS Distributions buf[0] = '\0';
159*2c2f96dcSApple OSS Distributions l += scnprintf(buf + l, len - l, "Backtrace: ");
160*2c2f96dcSApple OSS Distributions for (uint32_t i = 0; i < nframes; i++) {
161*2c2f96dcSApple OSS Distributions l += scnprintf(buf + l, len - l, "%lx,", VM_KERNEL_UNSLIDE(bt[i]));
162*2c2f96dcSApple OSS Distributions }
163*2c2f96dcSApple OSS Distributions l += scnprintf(buf + l, len - l, "\n");
164*2c2f96dcSApple OSS Distributions
165*2c2f96dcSApple OSS Distributions printf("%s", buf);
166*2c2f96dcSApple OSS Distributions }
167*2c2f96dcSApple OSS Distributions
168*2c2f96dcSApple OSS Distributions /*
169*2c2f96dcSApple OSS Distributions * Report a violation that may be disabled and/or blacklisted. This can only be
170*2c2f96dcSApple OSS Distributions * called for dynamic checks (i.e. where the fault is recoverable). Use
171*2c2f96dcSApple OSS Distributions * kasan_crash_report() for static (unrecoverable) violations.
172*2c2f96dcSApple OSS Distributions *
173*2c2f96dcSApple OSS Distributions * access: what we were trying to do when the violation occured
174*2c2f96dcSApple OSS Distributions * reason: what failed about the access
175*2c2f96dcSApple OSS Distributions */
176*2c2f96dcSApple OSS Distributions void
kasan_violation(uintptr_t addr,size_t size,access_t access,violation_t reason)177*2c2f96dcSApple OSS Distributions kasan_violation(uintptr_t addr, size_t size, access_t access, violation_t reason)
178*2c2f96dcSApple OSS Distributions {
179*2c2f96dcSApple OSS Distributions assert(__builtin_popcount(access) == 1);
180*2c2f96dcSApple OSS Distributions if (!kasan_check_enabled(access)) {
181*2c2f96dcSApple OSS Distributions /*
182*2c2f96dcSApple OSS Distributions * A violation happened but the annexed check is disabled. Simply
183*2c2f96dcSApple OSS Distributions * report the issue.
184*2c2f96dcSApple OSS Distributions */
185*2c2f96dcSApple OSS Distributions if (report_suppressed_checks) {
186*2c2f96dcSApple OSS Distributions kasan_log_report(addr, size, access, reason);
187*2c2f96dcSApple OSS Distributions }
188*2c2f96dcSApple OSS Distributions return;
189*2c2f96dcSApple OSS Distributions }
190*2c2f96dcSApple OSS Distributions /* Panic as usual */
191*2c2f96dcSApple OSS Distributions kasan_crash_report(addr, size, access, reason);
192*2c2f96dcSApple OSS Distributions }
193