xref: /linux/mm/kmsan/core.c (revision 7a92fc8b4d20680e4c20289a670d8fca2d1f2c1b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KMSAN runtime library.
4  *
5  * Copyright (C) 2017-2022 Google LLC
6  * Author: Alexander Potapenko <glider@google.com>
7  *
8  */
9 
10 #include <asm/page.h>
11 #include <linux/compiler.h>
12 #include <linux/export.h>
13 #include <linux/highmem.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/kmsan_types.h>
17 #include <linux/memory.h>
18 #include <linux/mm.h>
19 #include <linux/mm_types.h>
20 #include <linux/mmzone.h>
21 #include <linux/percpu-defs.h>
22 #include <linux/preempt.h>
23 #include <linux/slab.h>
24 #include <linux/stackdepot.h>
25 #include <linux/stacktrace.h>
26 #include <linux/types.h>
27 #include <linux/vmalloc.h>
28 
29 #include "../slab.h"
30 #include "kmsan.h"
31 
32 bool kmsan_enabled __read_mostly;
33 
34 /*
35  * Per-CPU KMSAN context to be used in interrupts, where current->kmsan is
36  * unavaliable.
37  */
38 DEFINE_PER_CPU(struct kmsan_ctx, kmsan_percpu_ctx);
39 
40 void kmsan_internal_task_create(struct task_struct *task)
41 {
42 	struct kmsan_ctx *ctx = &task->kmsan_ctx;
43 	struct thread_info *info = current_thread_info();
44 
45 	__memset(ctx, 0, sizeof(*ctx));
46 	ctx->allow_reporting = true;
47 	kmsan_internal_unpoison_memory(info, sizeof(*info), false);
48 }
49 
50 void kmsan_internal_poison_memory(void *address, size_t size, gfp_t flags,
51 				  unsigned int poison_flags)
52 {
53 	u32 extra_bits =
54 		kmsan_extra_bits(/*depth*/ 0, poison_flags & KMSAN_POISON_FREE);
55 	bool checked = poison_flags & KMSAN_POISON_CHECK;
56 	depot_stack_handle_t handle;
57 
58 	handle = kmsan_save_stack_with_flags(flags, extra_bits);
59 	kmsan_internal_set_shadow_origin(address, size, -1, handle, checked);
60 }
61 
62 void kmsan_internal_unpoison_memory(void *address, size_t size, bool checked)
63 {
64 	kmsan_internal_set_shadow_origin(address, size, 0, 0, checked);
65 }
66 
67 depot_stack_handle_t kmsan_save_stack_with_flags(gfp_t flags,
68 						 unsigned int extra)
69 {
70 	unsigned long entries[KMSAN_STACK_DEPTH];
71 	unsigned int nr_entries;
72 	depot_stack_handle_t handle;
73 
74 	nr_entries = stack_trace_save(entries, KMSAN_STACK_DEPTH, 0);
75 
76 	/* Don't sleep. */
77 	flags &= ~(__GFP_DIRECT_RECLAIM | __GFP_KSWAPD_RECLAIM);
78 
79 	handle = __stack_depot_save(entries, nr_entries, flags, true);
80 	return stack_depot_set_extra_bits(handle, extra);
81 }
82 
83 /* Copy the metadata following the memmove() behavior. */
84 void kmsan_internal_memmove_metadata(void *dst, void *src, size_t n)
85 {
86 	depot_stack_handle_t prev_old_origin = 0, prev_new_origin = 0;
87 	int i, iter, step, src_off, dst_off, oiter_src, oiter_dst;
88 	depot_stack_handle_t old_origin = 0, new_origin = 0;
89 	depot_stack_handle_t *origin_src, *origin_dst;
90 	u8 *shadow_src, *shadow_dst;
91 	u32 *align_shadow_dst;
92 	bool backwards;
93 
94 	shadow_dst = kmsan_get_metadata(dst, KMSAN_META_SHADOW);
95 	if (!shadow_dst)
96 		return;
97 	KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(dst, n));
98 	align_shadow_dst =
99 		(u32 *)ALIGN_DOWN((u64)shadow_dst, KMSAN_ORIGIN_SIZE);
100 
101 	shadow_src = kmsan_get_metadata(src, KMSAN_META_SHADOW);
102 	if (!shadow_src) {
103 		/* @src is untracked: mark @dst as initialized. */
104 		kmsan_internal_unpoison_memory(dst, n, /*checked*/ false);
105 		return;
106 	}
107 	KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(src, n));
108 
109 	origin_dst = kmsan_get_metadata(dst, KMSAN_META_ORIGIN);
110 	origin_src = kmsan_get_metadata(src, KMSAN_META_ORIGIN);
111 	KMSAN_WARN_ON(!origin_dst || !origin_src);
112 
113 	backwards = dst > src;
114 	step = backwards ? -1 : 1;
115 	iter = backwards ? n - 1 : 0;
116 	src_off = (u64)src % KMSAN_ORIGIN_SIZE;
117 	dst_off = (u64)dst % KMSAN_ORIGIN_SIZE;
118 
119 	/* Copy shadow bytes one by one, updating the origins if necessary. */
120 	for (i = 0; i < n; i++, iter += step) {
121 		oiter_src = (iter + src_off) / KMSAN_ORIGIN_SIZE;
122 		oiter_dst = (iter + dst_off) / KMSAN_ORIGIN_SIZE;
123 		if (!shadow_src[iter]) {
124 			shadow_dst[iter] = 0;
125 			if (!align_shadow_dst[oiter_dst])
126 				origin_dst[oiter_dst] = 0;
127 			continue;
128 		}
129 		shadow_dst[iter] = shadow_src[iter];
130 		old_origin = origin_src[oiter_src];
131 		if (old_origin == prev_old_origin)
132 			new_origin = prev_new_origin;
133 		else {
134 			/*
135 			 * kmsan_internal_chain_origin() may return
136 			 * NULL, but we don't want to lose the previous
137 			 * origin value.
138 			 */
139 			new_origin = kmsan_internal_chain_origin(old_origin);
140 			if (!new_origin)
141 				new_origin = old_origin;
142 		}
143 		origin_dst[oiter_dst] = new_origin;
144 		prev_new_origin = new_origin;
145 		prev_old_origin = old_origin;
146 	}
147 }
148 
149 depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id)
150 {
151 	unsigned long entries[3];
152 	u32 extra_bits;
153 	int depth;
154 	bool uaf;
155 	depot_stack_handle_t handle;
156 
157 	if (!id)
158 		return id;
159 	/*
160 	 * Make sure we have enough spare bits in @id to hold the UAF bit and
161 	 * the chain depth.
162 	 */
163 	BUILD_BUG_ON(
164 		(1 << STACK_DEPOT_EXTRA_BITS) <= (KMSAN_MAX_ORIGIN_DEPTH << 1));
165 
166 	extra_bits = stack_depot_get_extra_bits(id);
167 	depth = kmsan_depth_from_eb(extra_bits);
168 	uaf = kmsan_uaf_from_eb(extra_bits);
169 
170 	/*
171 	 * Stop chaining origins once the depth reached KMSAN_MAX_ORIGIN_DEPTH.
172 	 * This mostly happens in the case structures with uninitialized padding
173 	 * are copied around many times. Origin chains for such structures are
174 	 * usually periodic, and it does not make sense to fully store them.
175 	 */
176 	if (depth == KMSAN_MAX_ORIGIN_DEPTH)
177 		return id;
178 
179 	depth++;
180 	extra_bits = kmsan_extra_bits(depth, uaf);
181 
182 	entries[0] = KMSAN_CHAIN_MAGIC_ORIGIN;
183 	entries[1] = kmsan_save_stack_with_flags(__GFP_HIGH, 0);
184 	entries[2] = id;
185 	/*
186 	 * @entries is a local var in non-instrumented code, so KMSAN does not
187 	 * know it is initialized. Explicitly unpoison it to avoid false
188 	 * positives when __stack_depot_save() passes it to instrumented code.
189 	 */
190 	kmsan_internal_unpoison_memory(entries, sizeof(entries), false);
191 	handle = __stack_depot_save(entries, ARRAY_SIZE(entries), __GFP_HIGH,
192 				    true);
193 	return stack_depot_set_extra_bits(handle, extra_bits);
194 }
195 
196 void kmsan_internal_set_shadow_origin(void *addr, size_t size, int b,
197 				      u32 origin, bool checked)
198 {
199 	u64 address = (u64)addr;
200 	void *shadow_start;
201 	u32 *origin_start;
202 	size_t pad = 0;
203 
204 	KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(addr, size));
205 	shadow_start = kmsan_get_metadata(addr, KMSAN_META_SHADOW);
206 	if (!shadow_start) {
207 		/*
208 		 * kmsan_metadata_is_contiguous() is true, so either all shadow
209 		 * and origin pages are NULL, or all are non-NULL.
210 		 */
211 		if (checked) {
212 			pr_err("%s: not memsetting %ld bytes starting at %px, because the shadow is NULL\n",
213 			       __func__, size, addr);
214 			KMSAN_WARN_ON(true);
215 		}
216 		return;
217 	}
218 	__memset(shadow_start, b, size);
219 
220 	if (!IS_ALIGNED(address, KMSAN_ORIGIN_SIZE)) {
221 		pad = address % KMSAN_ORIGIN_SIZE;
222 		address -= pad;
223 		size += pad;
224 	}
225 	size = ALIGN(size, KMSAN_ORIGIN_SIZE);
226 	origin_start =
227 		(u32 *)kmsan_get_metadata((void *)address, KMSAN_META_ORIGIN);
228 
229 	for (int i = 0; i < size / KMSAN_ORIGIN_SIZE; i++)
230 		origin_start[i] = origin;
231 }
232 
233 struct page *kmsan_vmalloc_to_page_or_null(void *vaddr)
234 {
235 	struct page *page;
236 
237 	if (!kmsan_internal_is_vmalloc_addr(vaddr) &&
238 	    !kmsan_internal_is_module_addr(vaddr))
239 		return NULL;
240 	page = vmalloc_to_page(vaddr);
241 	if (pfn_valid(page_to_pfn(page)))
242 		return page;
243 	else
244 		return NULL;
245 }
246 
247 void kmsan_internal_check_memory(void *addr, size_t size, const void *user_addr,
248 				 int reason)
249 {
250 	depot_stack_handle_t cur_origin = 0, new_origin = 0;
251 	unsigned long addr64 = (unsigned long)addr;
252 	depot_stack_handle_t *origin = NULL;
253 	unsigned char *shadow = NULL;
254 	int cur_off_start = -1;
255 	int chunk_size;
256 	size_t pos = 0;
257 
258 	if (!size)
259 		return;
260 	KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(addr, size));
261 	while (pos < size) {
262 		chunk_size = min(size - pos,
263 				 PAGE_SIZE - ((addr64 + pos) % PAGE_SIZE));
264 		shadow = kmsan_get_metadata((void *)(addr64 + pos),
265 					    KMSAN_META_SHADOW);
266 		if (!shadow) {
267 			/*
268 			 * This page is untracked. If there were uninitialized
269 			 * bytes before, report them.
270 			 */
271 			if (cur_origin) {
272 				kmsan_enter_runtime();
273 				kmsan_report(cur_origin, addr, size,
274 					     cur_off_start, pos - 1, user_addr,
275 					     reason);
276 				kmsan_leave_runtime();
277 			}
278 			cur_origin = 0;
279 			cur_off_start = -1;
280 			pos += chunk_size;
281 			continue;
282 		}
283 		for (int i = 0; i < chunk_size; i++) {
284 			if (!shadow[i]) {
285 				/*
286 				 * This byte is unpoisoned. If there were
287 				 * poisoned bytes before, report them.
288 				 */
289 				if (cur_origin) {
290 					kmsan_enter_runtime();
291 					kmsan_report(cur_origin, addr, size,
292 						     cur_off_start, pos + i - 1,
293 						     user_addr, reason);
294 					kmsan_leave_runtime();
295 				}
296 				cur_origin = 0;
297 				cur_off_start = -1;
298 				continue;
299 			}
300 			origin = kmsan_get_metadata((void *)(addr64 + pos + i),
301 						    KMSAN_META_ORIGIN);
302 			KMSAN_WARN_ON(!origin);
303 			new_origin = *origin;
304 			/*
305 			 * Encountered new origin - report the previous
306 			 * uninitialized range.
307 			 */
308 			if (cur_origin != new_origin) {
309 				if (cur_origin) {
310 					kmsan_enter_runtime();
311 					kmsan_report(cur_origin, addr, size,
312 						     cur_off_start, pos + i - 1,
313 						     user_addr, reason);
314 					kmsan_leave_runtime();
315 				}
316 				cur_origin = new_origin;
317 				cur_off_start = pos + i;
318 			}
319 		}
320 		pos += chunk_size;
321 	}
322 	KMSAN_WARN_ON(pos != size);
323 	if (cur_origin) {
324 		kmsan_enter_runtime();
325 		kmsan_report(cur_origin, addr, size, cur_off_start, pos - 1,
326 			     user_addr, reason);
327 		kmsan_leave_runtime();
328 	}
329 }
330 
331 bool kmsan_metadata_is_contiguous(void *addr, size_t size)
332 {
333 	char *cur_shadow = NULL, *next_shadow = NULL, *cur_origin = NULL,
334 	     *next_origin = NULL;
335 	u64 cur_addr = (u64)addr, next_addr = cur_addr + PAGE_SIZE;
336 	depot_stack_handle_t *origin_p;
337 	bool all_untracked = false;
338 
339 	if (!size)
340 		return true;
341 
342 	/* The whole range belongs to the same page. */
343 	if (ALIGN_DOWN(cur_addr + size - 1, PAGE_SIZE) ==
344 	    ALIGN_DOWN(cur_addr, PAGE_SIZE))
345 		return true;
346 
347 	cur_shadow = kmsan_get_metadata((void *)cur_addr, /*is_origin*/ false);
348 	if (!cur_shadow)
349 		all_untracked = true;
350 	cur_origin = kmsan_get_metadata((void *)cur_addr, /*is_origin*/ true);
351 	if (all_untracked && cur_origin)
352 		goto report;
353 
354 	for (; next_addr < (u64)addr + size;
355 	     cur_addr = next_addr, cur_shadow = next_shadow,
356 	     cur_origin = next_origin, next_addr += PAGE_SIZE) {
357 		next_shadow = kmsan_get_metadata((void *)next_addr, false);
358 		next_origin = kmsan_get_metadata((void *)next_addr, true);
359 		if (all_untracked) {
360 			if (next_shadow || next_origin)
361 				goto report;
362 			if (!next_shadow && !next_origin)
363 				continue;
364 		}
365 		if (((u64)cur_shadow == ((u64)next_shadow - PAGE_SIZE)) &&
366 		    ((u64)cur_origin == ((u64)next_origin - PAGE_SIZE)))
367 			continue;
368 		goto report;
369 	}
370 	return true;
371 
372 report:
373 	pr_err("%s: attempting to access two shadow page ranges.\n", __func__);
374 	pr_err("Access of size %ld at %px.\n", size, addr);
375 	pr_err("Addresses belonging to different ranges: %px and %px\n",
376 	       (void *)cur_addr, (void *)next_addr);
377 	pr_err("page[0].shadow: %px, page[1].shadow: %px\n", cur_shadow,
378 	       next_shadow);
379 	pr_err("page[0].origin: %px, page[1].origin: %px\n", cur_origin,
380 	       next_origin);
381 	origin_p = kmsan_get_metadata(addr, KMSAN_META_ORIGIN);
382 	if (origin_p) {
383 		pr_err("Origin: %08x\n", *origin_p);
384 		kmsan_print_origin(*origin_p);
385 	} else {
386 		pr_err("Origin: unavailable\n");
387 	}
388 	return false;
389 }
390