1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2020, Google LLC.
8 #include <linux/stdarg.h>
10 #include <linux/kernel.h>
11 #include <linux/lockdep.h>
12 #include <linux/math.h>
13 #include <linux/printk.h>
14 #include <linux/sched/debug.h>
15 #include <linux/seq_file.h>
16 #include <linux/sprintf.h>
17 #include <linux/stacktrace.h>
18 #include <linux/string.h>
19 #include <trace/events/error_report.h>
21 #include <asm/kfence.h>
25 /* May be overridden by <asm/kfence.h>. */
26 #ifndef ARCH_FUNC_PREFIX
27 #define ARCH_FUNC_PREFIX ""
30 /* Helper function to either print to a seq_file or to console. */
32 static void seq_con_printf(struct seq_file *seq, const char *fmt, ...)
38 seq_vprintf(seq, fmt, args);
45 * Get the number of stack entries to skip to get out of MM internals. @type is
46 * optional, and if set to NULL, assumes an allocation or free stack.
48 static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries,
49 const enum kfence_error_type *type)
52 int skipnr, fallback = 0;
55 /* Depending on error type, find different stack entries. */
57 case KFENCE_ERROR_UAF:
58 case KFENCE_ERROR_OOB:
59 case KFENCE_ERROR_INVALID:
61 * kfence_handle_page_fault() may be called with pt_regs
62 * set to NULL; in that case we'll simply show the full
66 case KFENCE_ERROR_CORRUPTION:
67 case KFENCE_ERROR_INVALID_FREE:
72 for (skipnr = 0; skipnr < num_entries; skipnr++) {
73 int len = scnprintf(buf, sizeof(buf), "%ps", (void *)stack_entries[skipnr]);
75 if (str_has_prefix(buf, ARCH_FUNC_PREFIX "kfence_") ||
76 str_has_prefix(buf, ARCH_FUNC_PREFIX "__kfence_") ||
77 str_has_prefix(buf, ARCH_FUNC_PREFIX "__kmem_cache_free") ||
78 !strncmp(buf, ARCH_FUNC_PREFIX "__slab_free", len)) {
80 * In case of tail calls from any of the below to any of
81 * the above, optimized by the compiler such that the
82 * stack trace would omit the initial entry point below.
84 fallback = skipnr + 1;
88 * The below list should only include the initial entry points
89 * into the slab allocators. Includes the *_bulk() variants by
92 if (str_has_prefix(buf, ARCH_FUNC_PREFIX "kfree") ||
93 str_has_prefix(buf, ARCH_FUNC_PREFIX "kmem_cache_free") ||
94 str_has_prefix(buf, ARCH_FUNC_PREFIX "__kmalloc") ||
95 str_has_prefix(buf, ARCH_FUNC_PREFIX "kmem_cache_alloc"))
98 if (fallback < num_entries)
102 return skipnr < num_entries ? skipnr : 0;
105 static void kfence_print_stack(struct seq_file *seq, const struct kfence_metadata *meta,
108 const struct kfence_track *track = show_alloc ? &meta->alloc_track : &meta->free_track;
109 u64 ts_sec = track->ts_nsec;
110 unsigned long rem_nsec = do_div(ts_sec, NSEC_PER_SEC);
112 /* Timestamp matches printk timestamp format. */
113 seq_con_printf(seq, "%s by task %d on cpu %d at %lu.%06lus:\n",
114 show_alloc ? "allocated" : "freed", track->pid,
115 track->cpu, (unsigned long)ts_sec, rem_nsec / 1000);
117 if (track->num_stack_entries) {
118 /* Skip allocation/free internals stack. */
119 int i = get_stack_skipnr(track->stack_entries, track->num_stack_entries, NULL);
121 /* stack_trace_seq_print() does not exist; open code our own. */
122 for (; i < track->num_stack_entries; i++)
123 seq_con_printf(seq, " %pS\n", (void *)track->stack_entries[i]);
125 seq_con_printf(seq, " no %s stack\n", show_alloc ? "allocation" : "deallocation");
129 void kfence_print_object(struct seq_file *seq, const struct kfence_metadata *meta)
131 const int size = abs(meta->size);
132 const unsigned long start = meta->addr;
133 const struct kmem_cache *const cache = meta->cache;
135 lockdep_assert_held(&meta->lock);
137 if (meta->state == KFENCE_OBJECT_UNUSED) {
138 seq_con_printf(seq, "kfence-#%td unused\n", meta - kfence_metadata);
142 seq_con_printf(seq, "kfence-#%td: 0x%p-0x%p, size=%d, cache=%s\n\n",
143 meta - kfence_metadata, (void *)start, (void *)(start + size - 1),
144 size, (cache && cache->name) ? cache->name : "<destroyed>");
146 kfence_print_stack(seq, meta, true);
148 if (meta->state == KFENCE_OBJECT_FREED) {
149 seq_con_printf(seq, "\n");
150 kfence_print_stack(seq, meta, false);
155 * Show bytes at @addr that are different from the expected canary values, up to
158 static void print_diff_canary(unsigned long address, size_t bytes_to_show,
159 const struct kfence_metadata *meta)
161 const unsigned long show_until_addr = address + bytes_to_show;
164 /* Do not show contents of object nor read into following guard page. */
165 end = (const u8 *)(address < meta->addr ? min(show_until_addr, meta->addr)
166 : min(show_until_addr, PAGE_ALIGN(address)));
169 for (cur = (const u8 *)address; cur < end; cur++) {
170 if (*cur == KFENCE_CANARY_PATTERN_U8(cur))
172 else if (no_hash_pointers)
173 pr_cont(" 0x%02x", *cur);
174 else /* Do not leak kernel memory in non-debug builds. */
180 static const char *get_access_type(bool is_write)
182 return is_write ? "write" : "read";
185 void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *regs,
186 const struct kfence_metadata *meta, enum kfence_error_type type)
188 unsigned long stack_entries[KFENCE_STACK_DEPTH] = { 0 };
189 const ptrdiff_t object_index = meta ? meta - kfence_metadata : -1;
190 int num_stack_entries;
194 num_stack_entries = stack_trace_save_regs(regs, stack_entries, KFENCE_STACK_DEPTH, 0);
196 num_stack_entries = stack_trace_save(stack_entries, KFENCE_STACK_DEPTH, 1);
197 skipnr = get_stack_skipnr(stack_entries, num_stack_entries, &type);
200 /* Require non-NULL meta, except if KFENCE_ERROR_INVALID. */
201 if (WARN_ON(type != KFENCE_ERROR_INVALID && !meta))
205 lockdep_assert_held(&meta->lock);
207 * Because we may generate reports in printk-unfriendly parts of the
208 * kernel, such as scheduler code, the use of printk() could deadlock.
209 * Until such time that all printing code here is safe in all parts of
210 * the kernel, accept the risk, and just get our message out (given the
211 * system might already behave unpredictably due to the memory error).
212 * As such, also disable lockdep to hide warnings, and avoid disabling
213 * lockdep for the rest of the kernel.
217 pr_err("==================================================================\n");
218 /* Print report header. */
220 case KFENCE_ERROR_OOB: {
221 const bool left_of_object = address < meta->addr;
223 pr_err("BUG: KFENCE: out-of-bounds %s in %pS\n\n", get_access_type(is_write),
224 (void *)stack_entries[skipnr]);
225 pr_err("Out-of-bounds %s at 0x%p (%luB %s of kfence-#%td):\n",
226 get_access_type(is_write), (void *)address,
227 left_of_object ? meta->addr - address : address - meta->addr,
228 left_of_object ? "left" : "right", object_index);
231 case KFENCE_ERROR_UAF:
232 pr_err("BUG: KFENCE: use-after-free %s in %pS\n\n", get_access_type(is_write),
233 (void *)stack_entries[skipnr]);
234 pr_err("Use-after-free %s at 0x%p (in kfence-#%td):\n",
235 get_access_type(is_write), (void *)address, object_index);
237 case KFENCE_ERROR_CORRUPTION:
238 pr_err("BUG: KFENCE: memory corruption in %pS\n\n", (void *)stack_entries[skipnr]);
239 pr_err("Corrupted memory at 0x%p ", (void *)address);
240 print_diff_canary(address, 16, meta);
241 pr_cont(" (in kfence-#%td):\n", object_index);
243 case KFENCE_ERROR_INVALID:
244 pr_err("BUG: KFENCE: invalid %s in %pS\n\n", get_access_type(is_write),
245 (void *)stack_entries[skipnr]);
246 pr_err("Invalid %s at 0x%p:\n", get_access_type(is_write),
249 case KFENCE_ERROR_INVALID_FREE:
250 pr_err("BUG: KFENCE: invalid free in %pS\n\n", (void *)stack_entries[skipnr]);
251 pr_err("Invalid free of 0x%p (in kfence-#%td):\n", (void *)address,
256 /* Print stack trace and object info. */
257 stack_trace_print(stack_entries + skipnr, num_stack_entries - skipnr, 0);
261 kfence_print_object(NULL, meta);
264 /* Print report footer. */
266 if (no_hash_pointers && regs)
269 dump_stack_print_info(KERN_ERR);
270 trace_error_report_end(ERROR_DETECTOR_KFENCE, address);
271 pr_err("==================================================================\n");
275 check_panic_on_warn("KFENCE");
277 /* We encountered a memory safety error, taint the kernel! */
278 add_taint(TAINT_BAD_PAGE, LOCKDEP_STILL_OK);
282 static void kfence_to_kp_stack(const struct kfence_track *track, void **kp_stack)
286 i = get_stack_skipnr(track->stack_entries, track->num_stack_entries, NULL);
287 for (j = 0; i < track->num_stack_entries && j < KS_ADDRS_COUNT; ++i, ++j)
288 kp_stack[j] = (void *)track->stack_entries[i];
289 if (j < KS_ADDRS_COUNT)
293 bool __kfence_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
295 struct kfence_metadata *meta = addr_to_metadata((unsigned long)object);
302 * If state is UNUSED at least show the pointer requested; the rest
303 * would be garbage data.
305 kpp->kp_ptr = object;
307 /* Requesting info an a never-used object is almost certainly a bug. */
308 if (WARN_ON(meta->state == KFENCE_OBJECT_UNUSED))
311 raw_spin_lock_irqsave(&meta->lock, flags);
314 kpp->kp_slab_cache = meta->cache;
315 kpp->kp_objp = (void *)meta->addr;
316 kfence_to_kp_stack(&meta->alloc_track, kpp->kp_stack);
317 if (meta->state == KFENCE_OBJECT_FREED)
318 kfence_to_kp_stack(&meta->free_track, kpp->kp_free_stack);
319 /* get_stack_skipnr() ensures the first entry is outside allocator. */
320 kpp->kp_ret = kpp->kp_stack[0];
322 raw_spin_unlock_irqrestore(&meta->lock, flags);