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