1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * KFENCE reporting.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2020, Google LLC.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <stdarg.h>
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/lockdep.h>
12*4882a593Smuzhiyun #include <linux/printk.h>
13*4882a593Smuzhiyun #include <linux/sched/debug.h>
14*4882a593Smuzhiyun #include <linux/seq_file.h>
15*4882a593Smuzhiyun #include <linux/stacktrace.h>
16*4882a593Smuzhiyun #include <linux/string.h>
17*4882a593Smuzhiyun #include <trace/events/error_report.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <asm/kfence.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include "kfence.h"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* May be overridden by <asm/kfence.h>. */
24*4882a593Smuzhiyun #ifndef ARCH_FUNC_PREFIX
25*4882a593Smuzhiyun #define ARCH_FUNC_PREFIX ""
26*4882a593Smuzhiyun #endif
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun extern bool no_hash_pointers;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* Helper function to either print to a seq_file or to console. */
31*4882a593Smuzhiyun __printf(2, 3)
seq_con_printf(struct seq_file * seq,const char * fmt,...)32*4882a593Smuzhiyun static void seq_con_printf(struct seq_file *seq, const char *fmt, ...)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun va_list args;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun va_start(args, fmt);
37*4882a593Smuzhiyun if (seq)
38*4882a593Smuzhiyun seq_vprintf(seq, fmt, args);
39*4882a593Smuzhiyun else
40*4882a593Smuzhiyun vprintk(fmt, args);
41*4882a593Smuzhiyun va_end(args);
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun * Get the number of stack entries to skip to get out of MM internals. @type is
46*4882a593Smuzhiyun * optional, and if set to NULL, assumes an allocation or free stack.
47*4882a593Smuzhiyun */
get_stack_skipnr(const unsigned long stack_entries[],int num_entries,const enum kfence_error_type * type)48*4882a593Smuzhiyun static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries,
49*4882a593Smuzhiyun const enum kfence_error_type *type)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun char buf[64];
52*4882a593Smuzhiyun int skipnr, fallback = 0;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (type) {
55*4882a593Smuzhiyun /* Depending on error type, find different stack entries. */
56*4882a593Smuzhiyun switch (*type) {
57*4882a593Smuzhiyun case KFENCE_ERROR_UAF:
58*4882a593Smuzhiyun case KFENCE_ERROR_OOB:
59*4882a593Smuzhiyun case KFENCE_ERROR_INVALID:
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * kfence_handle_page_fault() may be called with pt_regs
62*4882a593Smuzhiyun * set to NULL; in that case we'll simply show the full
63*4882a593Smuzhiyun * stack trace.
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun return 0;
66*4882a593Smuzhiyun case KFENCE_ERROR_CORRUPTION:
67*4882a593Smuzhiyun case KFENCE_ERROR_INVALID_FREE:
68*4882a593Smuzhiyun break;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun for (skipnr = 0; skipnr < num_entries; skipnr++) {
73*4882a593Smuzhiyun int len = scnprintf(buf, sizeof(buf), "%ps", (void *)stack_entries[skipnr]);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun if (str_has_prefix(buf, ARCH_FUNC_PREFIX "kfence_") ||
76*4882a593Smuzhiyun str_has_prefix(buf, ARCH_FUNC_PREFIX "__kfence_") ||
77*4882a593Smuzhiyun !strncmp(buf, ARCH_FUNC_PREFIX "__slab_free", len)) {
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * In case of tail calls from any of the below
80*4882a593Smuzhiyun * to any of the above.
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun fallback = skipnr + 1;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* Also the *_bulk() variants by only checking prefixes. */
86*4882a593Smuzhiyun if (str_has_prefix(buf, ARCH_FUNC_PREFIX "kfree") ||
87*4882a593Smuzhiyun str_has_prefix(buf, ARCH_FUNC_PREFIX "kmem_cache_free") ||
88*4882a593Smuzhiyun str_has_prefix(buf, ARCH_FUNC_PREFIX "__kmalloc") ||
89*4882a593Smuzhiyun str_has_prefix(buf, ARCH_FUNC_PREFIX "kmem_cache_alloc"))
90*4882a593Smuzhiyun goto found;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun if (fallback < num_entries)
93*4882a593Smuzhiyun return fallback;
94*4882a593Smuzhiyun found:
95*4882a593Smuzhiyun skipnr++;
96*4882a593Smuzhiyun return skipnr < num_entries ? skipnr : 0;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
kfence_print_stack(struct seq_file * seq,const struct kfence_metadata * meta,bool show_alloc)99*4882a593Smuzhiyun static void kfence_print_stack(struct seq_file *seq, const struct kfence_metadata *meta,
100*4882a593Smuzhiyun bool show_alloc)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun const struct kfence_track *track = show_alloc ? &meta->alloc_track : &meta->free_track;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun if (track->num_stack_entries) {
105*4882a593Smuzhiyun /* Skip allocation/free internals stack. */
106*4882a593Smuzhiyun int i = get_stack_skipnr(track->stack_entries, track->num_stack_entries, NULL);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* stack_trace_seq_print() does not exist; open code our own. */
109*4882a593Smuzhiyun for (; i < track->num_stack_entries; i++)
110*4882a593Smuzhiyun seq_con_printf(seq, " %pS\n", (void *)track->stack_entries[i]);
111*4882a593Smuzhiyun } else {
112*4882a593Smuzhiyun seq_con_printf(seq, " no %s stack\n", show_alloc ? "allocation" : "deallocation");
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
kfence_print_object(struct seq_file * seq,const struct kfence_metadata * meta)116*4882a593Smuzhiyun void kfence_print_object(struct seq_file *seq, const struct kfence_metadata *meta)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun const int size = abs(meta->size);
119*4882a593Smuzhiyun const unsigned long start = meta->addr;
120*4882a593Smuzhiyun const struct kmem_cache *const cache = meta->cache;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun lockdep_assert_held(&meta->lock);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun if (meta->state == KFENCE_OBJECT_UNUSED) {
125*4882a593Smuzhiyun seq_con_printf(seq, "kfence-#%td unused\n", meta - kfence_metadata);
126*4882a593Smuzhiyun return;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun seq_con_printf(seq,
130*4882a593Smuzhiyun "kfence-#%td [0x%p-0x%p"
131*4882a593Smuzhiyun ", size=%d, cache=%s] allocated by task %d:\n",
132*4882a593Smuzhiyun meta - kfence_metadata, (void *)start, (void *)(start + size - 1), size,
133*4882a593Smuzhiyun (cache && cache->name) ? cache->name : "<destroyed>", meta->alloc_track.pid);
134*4882a593Smuzhiyun kfence_print_stack(seq, meta, true);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun if (meta->state == KFENCE_OBJECT_FREED) {
137*4882a593Smuzhiyun seq_con_printf(seq, "\nfreed by task %d:\n", meta->free_track.pid);
138*4882a593Smuzhiyun kfence_print_stack(seq, meta, false);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun * Show bytes at @addr that are different from the expected canary values, up to
144*4882a593Smuzhiyun * @max_bytes.
145*4882a593Smuzhiyun */
print_diff_canary(unsigned long address,size_t bytes_to_show,const struct kfence_metadata * meta)146*4882a593Smuzhiyun static void print_diff_canary(unsigned long address, size_t bytes_to_show,
147*4882a593Smuzhiyun const struct kfence_metadata *meta)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun const unsigned long show_until_addr = address + bytes_to_show;
150*4882a593Smuzhiyun const u8 *cur, *end;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* Do not show contents of object nor read into following guard page. */
153*4882a593Smuzhiyun end = (const u8 *)(address < meta->addr ? min(show_until_addr, meta->addr)
154*4882a593Smuzhiyun : min(show_until_addr, PAGE_ALIGN(address)));
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun pr_cont("[");
157*4882a593Smuzhiyun for (cur = (const u8 *)address; cur < end; cur++) {
158*4882a593Smuzhiyun if (*cur == KFENCE_CANARY_PATTERN(cur))
159*4882a593Smuzhiyun pr_cont(" .");
160*4882a593Smuzhiyun else if (no_hash_pointers)
161*4882a593Smuzhiyun pr_cont(" 0x%02x", *cur);
162*4882a593Smuzhiyun else /* Do not leak kernel memory in non-debug builds. */
163*4882a593Smuzhiyun pr_cont(" !");
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun pr_cont(" ]");
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
get_access_type(bool is_write)168*4882a593Smuzhiyun static const char *get_access_type(bool is_write)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun return is_write ? "write" : "read";
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
kfence_report_error(unsigned long address,bool is_write,struct pt_regs * regs,const struct kfence_metadata * meta,enum kfence_error_type type)173*4882a593Smuzhiyun void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *regs,
174*4882a593Smuzhiyun const struct kfence_metadata *meta, enum kfence_error_type type)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun unsigned long stack_entries[KFENCE_STACK_DEPTH] = { 0 };
177*4882a593Smuzhiyun const ptrdiff_t object_index = meta ? meta - kfence_metadata : -1;
178*4882a593Smuzhiyun int num_stack_entries;
179*4882a593Smuzhiyun int skipnr = 0;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun if (regs) {
182*4882a593Smuzhiyun num_stack_entries = stack_trace_save_regs(regs, stack_entries, KFENCE_STACK_DEPTH, 0);
183*4882a593Smuzhiyun } else {
184*4882a593Smuzhiyun num_stack_entries = stack_trace_save(stack_entries, KFENCE_STACK_DEPTH, 1);
185*4882a593Smuzhiyun skipnr = get_stack_skipnr(stack_entries, num_stack_entries, &type);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* Require non-NULL meta, except if KFENCE_ERROR_INVALID. */
189*4882a593Smuzhiyun if (WARN_ON(type != KFENCE_ERROR_INVALID && !meta))
190*4882a593Smuzhiyun return;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun if (meta)
193*4882a593Smuzhiyun lockdep_assert_held(&meta->lock);
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun * Because we may generate reports in printk-unfriendly parts of the
196*4882a593Smuzhiyun * kernel, such as scheduler code, the use of printk() could deadlock.
197*4882a593Smuzhiyun * Until such time that all printing code here is safe in all parts of
198*4882a593Smuzhiyun * the kernel, accept the risk, and just get our message out (given the
199*4882a593Smuzhiyun * system might already behave unpredictably due to the memory error).
200*4882a593Smuzhiyun * As such, also disable lockdep to hide warnings, and avoid disabling
201*4882a593Smuzhiyun * lockdep for the rest of the kernel.
202*4882a593Smuzhiyun */
203*4882a593Smuzhiyun lockdep_off();
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun pr_err("==================================================================\n");
206*4882a593Smuzhiyun /* Print report header. */
207*4882a593Smuzhiyun switch (type) {
208*4882a593Smuzhiyun case KFENCE_ERROR_OOB: {
209*4882a593Smuzhiyun const bool left_of_object = address < meta->addr;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun pr_err("BUG: KFENCE: out-of-bounds %s in %pS\n\n", get_access_type(is_write),
212*4882a593Smuzhiyun (void *)stack_entries[skipnr]);
213*4882a593Smuzhiyun pr_err("Out-of-bounds %s at 0x%p (%luB %s of kfence-#%td):\n",
214*4882a593Smuzhiyun get_access_type(is_write), (void *)address,
215*4882a593Smuzhiyun left_of_object ? meta->addr - address : address - meta->addr,
216*4882a593Smuzhiyun left_of_object ? "left" : "right", object_index);
217*4882a593Smuzhiyun break;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun case KFENCE_ERROR_UAF:
220*4882a593Smuzhiyun pr_err("BUG: KFENCE: use-after-free %s in %pS\n\n", get_access_type(is_write),
221*4882a593Smuzhiyun (void *)stack_entries[skipnr]);
222*4882a593Smuzhiyun pr_err("Use-after-free %s at 0x%p (in kfence-#%td):\n",
223*4882a593Smuzhiyun get_access_type(is_write), (void *)address, object_index);
224*4882a593Smuzhiyun break;
225*4882a593Smuzhiyun case KFENCE_ERROR_CORRUPTION:
226*4882a593Smuzhiyun pr_err("BUG: KFENCE: memory corruption in %pS\n\n", (void *)stack_entries[skipnr]);
227*4882a593Smuzhiyun pr_err("Corrupted memory at 0x%p ", (void *)address);
228*4882a593Smuzhiyun print_diff_canary(address, 16, meta);
229*4882a593Smuzhiyun pr_cont(" (in kfence-#%td):\n", object_index);
230*4882a593Smuzhiyun break;
231*4882a593Smuzhiyun case KFENCE_ERROR_INVALID:
232*4882a593Smuzhiyun pr_err("BUG: KFENCE: invalid %s in %pS\n\n", get_access_type(is_write),
233*4882a593Smuzhiyun (void *)stack_entries[skipnr]);
234*4882a593Smuzhiyun pr_err("Invalid %s at 0x%p:\n", get_access_type(is_write),
235*4882a593Smuzhiyun (void *)address);
236*4882a593Smuzhiyun break;
237*4882a593Smuzhiyun case KFENCE_ERROR_INVALID_FREE:
238*4882a593Smuzhiyun pr_err("BUG: KFENCE: invalid free in %pS\n\n", (void *)stack_entries[skipnr]);
239*4882a593Smuzhiyun pr_err("Invalid free of 0x%p (in kfence-#%td):\n", (void *)address,
240*4882a593Smuzhiyun object_index);
241*4882a593Smuzhiyun break;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun /* Print stack trace and object info. */
245*4882a593Smuzhiyun stack_trace_print(stack_entries + skipnr, num_stack_entries - skipnr, 0);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun if (meta) {
248*4882a593Smuzhiyun pr_err("\n");
249*4882a593Smuzhiyun kfence_print_object(NULL, meta);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* Print report footer. */
253*4882a593Smuzhiyun pr_err("\n");
254*4882a593Smuzhiyun if (no_hash_pointers && regs)
255*4882a593Smuzhiyun show_regs(regs);
256*4882a593Smuzhiyun else
257*4882a593Smuzhiyun dump_stack_print_info(KERN_ERR);
258*4882a593Smuzhiyun trace_error_report_end(ERROR_DETECTOR_KFENCE, address);
259*4882a593Smuzhiyun pr_err("==================================================================\n");
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun lockdep_on();
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (panic_on_warn)
264*4882a593Smuzhiyun panic("panic_on_warn set ...\n");
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* We encountered a memory unsafety error, taint the kernel! */
267*4882a593Smuzhiyun add_taint(TAINT_BAD_PAGE, LOCKDEP_STILL_OK);
268*4882a593Smuzhiyun }
269