1 // SPDX-License-Identifier: GPL-2.0
3 * KMSAN hooks for kernel subsystems.
5 * These functions handle creation of KMSAN metadata for memory allocations.
7 * Copyright (C) 2018-2022 Google LLC
8 * Author: Alexander Potapenko <glider@google.com>
12 #include <linux/cacheflush.h>
13 #include <linux/dma-direction.h>
14 #include <linux/gfp.h>
15 #include <linux/kmsan.h>
17 #include <linux/mm_types.h>
18 #include <linux/scatterlist.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/usb.h>
23 #include "../internal.h"
28 * Instrumented functions shouldn't be called under
29 * kmsan_enter_runtime()/kmsan_leave_runtime(), because this will lead to
30 * skipping effects of functions like memset() inside instrumented code.
33 void kmsan_task_create(struct task_struct *task)
35 kmsan_enter_runtime();
36 kmsan_internal_task_create(task);
37 kmsan_leave_runtime();
40 void kmsan_task_exit(struct task_struct *task)
42 struct kmsan_ctx *ctx = &task->kmsan_ctx;
44 if (!kmsan_enabled || kmsan_in_runtime())
47 ctx->allow_reporting = false;
50 void kmsan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags)
52 if (unlikely(object == NULL))
54 if (!kmsan_enabled || kmsan_in_runtime())
57 * There's a ctor or this is an RCU cache - do nothing. The memory
58 * status hasn't changed since last use.
60 if (s->ctor || (s->flags & SLAB_TYPESAFE_BY_RCU))
63 kmsan_enter_runtime();
64 if (flags & __GFP_ZERO)
65 kmsan_internal_unpoison_memory(object, s->object_size,
68 kmsan_internal_poison_memory(object, s->object_size, flags,
70 kmsan_leave_runtime();
73 void kmsan_slab_free(struct kmem_cache *s, void *object)
75 if (!kmsan_enabled || kmsan_in_runtime())
78 /* RCU slabs could be legally used after free within the RCU period */
79 if (unlikely(s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)))
82 * If there's a constructor, freed memory must remain in the same state
83 * until the next allocation. We cannot save its state to detect
84 * use-after-free bugs, instead we just keep it unpoisoned.
88 kmsan_enter_runtime();
89 kmsan_internal_poison_memory(object, s->object_size, GFP_KERNEL,
90 KMSAN_POISON_CHECK | KMSAN_POISON_FREE);
91 kmsan_leave_runtime();
94 void kmsan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
96 if (unlikely(ptr == NULL))
98 if (!kmsan_enabled || kmsan_in_runtime())
100 kmsan_enter_runtime();
101 if (flags & __GFP_ZERO)
102 kmsan_internal_unpoison_memory((void *)ptr, size,
105 kmsan_internal_poison_memory((void *)ptr, size, flags,
107 kmsan_leave_runtime();
110 void kmsan_kfree_large(const void *ptr)
114 if (!kmsan_enabled || kmsan_in_runtime())
116 kmsan_enter_runtime();
117 page = virt_to_head_page((void *)ptr);
118 KMSAN_WARN_ON(ptr != page_address(page));
119 kmsan_internal_poison_memory((void *)ptr,
120 PAGE_SIZE << compound_order(page),
122 KMSAN_POISON_CHECK | KMSAN_POISON_FREE);
123 kmsan_leave_runtime();
126 static unsigned long vmalloc_shadow(unsigned long addr)
128 return (unsigned long)kmsan_get_metadata((void *)addr,
132 static unsigned long vmalloc_origin(unsigned long addr)
134 return (unsigned long)kmsan_get_metadata((void *)addr,
138 void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end)
140 __vunmap_range_noflush(vmalloc_shadow(start), vmalloc_shadow(end));
141 __vunmap_range_noflush(vmalloc_origin(start), vmalloc_origin(end));
142 flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
143 flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
147 * This function creates new shadow/origin pages for the physical pages mapped
148 * into the virtual memory. If those physical pages already had shadow/origin,
151 void kmsan_ioremap_page_range(unsigned long start, unsigned long end,
152 phys_addr_t phys_addr, pgprot_t prot,
153 unsigned int page_shift)
155 gfp_t gfp_mask = GFP_KERNEL | __GFP_ZERO;
156 struct page *shadow, *origin;
157 unsigned long off = 0;
160 if (!kmsan_enabled || kmsan_in_runtime())
163 nr = (end - start) / PAGE_SIZE;
164 kmsan_enter_runtime();
165 for (int i = 0; i < nr; i++, off += PAGE_SIZE) {
166 shadow = alloc_pages(gfp_mask, 1);
167 origin = alloc_pages(gfp_mask, 1);
168 __vmap_pages_range_noflush(
169 vmalloc_shadow(start + off),
170 vmalloc_shadow(start + off + PAGE_SIZE), prot, &shadow,
172 __vmap_pages_range_noflush(
173 vmalloc_origin(start + off),
174 vmalloc_origin(start + off + PAGE_SIZE), prot, &origin,
177 flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
178 flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
179 kmsan_leave_runtime();
182 void kmsan_iounmap_page_range(unsigned long start, unsigned long end)
184 unsigned long v_shadow, v_origin;
185 struct page *shadow, *origin;
188 if (!kmsan_enabled || kmsan_in_runtime())
191 nr = (end - start) / PAGE_SIZE;
192 kmsan_enter_runtime();
193 v_shadow = (unsigned long)vmalloc_shadow(start);
194 v_origin = (unsigned long)vmalloc_origin(start);
195 for (int i = 0; i < nr;
196 i++, v_shadow += PAGE_SIZE, v_origin += PAGE_SIZE) {
197 shadow = kmsan_vmalloc_to_page_or_null((void *)v_shadow);
198 origin = kmsan_vmalloc_to_page_or_null((void *)v_origin);
199 __vunmap_range_noflush(v_shadow, vmalloc_shadow(end));
200 __vunmap_range_noflush(v_origin, vmalloc_origin(end));
202 __free_pages(shadow, 1);
204 __free_pages(origin, 1);
206 flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
207 flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
208 kmsan_leave_runtime();
211 void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy,
214 unsigned long ua_flags;
216 if (!kmsan_enabled || kmsan_in_runtime())
219 * At this point we've copied the memory already. It's hard to check it
220 * before copying, as the size of actually copied buffer is unknown.
223 /* copy_to_user() may copy zero bytes. No need to check. */
226 /* Or maybe copy_to_user() failed to copy anything. */
230 ua_flags = user_access_save();
231 if ((u64)to < TASK_SIZE) {
232 /* This is a user memory access, check it. */
233 kmsan_internal_check_memory((void *)from, to_copy - left, to,
234 REASON_COPY_TO_USER);
236 /* Otherwise this is a kernel memory access. This happens when a
237 * compat syscall passes an argument allocated on the kernel
238 * stack to a real syscall.
239 * Don't check anything, just copy the shadow of the copied
242 kmsan_internal_memmove_metadata((void *)to, (void *)from,
245 user_access_restore(ua_flags);
247 EXPORT_SYMBOL(kmsan_copy_to_user);
249 /* Helper function to check an URB. */
250 void kmsan_handle_urb(const struct urb *urb, bool is_out)
255 kmsan_internal_check_memory(urb->transfer_buffer,
256 urb->transfer_buffer_length,
257 /*user_addr*/ 0, REASON_SUBMIT_URB);
259 kmsan_internal_unpoison_memory(urb->transfer_buffer,
260 urb->transfer_buffer_length,
264 static void kmsan_handle_dma_page(const void *addr, size_t size,
265 enum dma_data_direction dir)
268 case DMA_BIDIRECTIONAL:
269 kmsan_internal_check_memory((void *)addr, size, /*user_addr*/ 0,
271 kmsan_internal_unpoison_memory((void *)addr, size,
275 kmsan_internal_check_memory((void *)addr, size, /*user_addr*/ 0,
278 case DMA_FROM_DEVICE:
279 kmsan_internal_unpoison_memory((void *)addr, size,
287 /* Helper function to handle DMA data transfers. */
288 void kmsan_handle_dma(struct page *page, size_t offset, size_t size,
289 enum dma_data_direction dir)
291 u64 page_offset, to_go, addr;
293 if (PageHighMem(page))
295 addr = (u64)page_address(page) + offset;
297 * The kernel may occasionally give us adjacent DMA pages not belonging
298 * to the same allocation. Process them separately to avoid triggering
299 * internal KMSAN checks.
302 page_offset = addr % PAGE_SIZE;
303 to_go = min(PAGE_SIZE - page_offset, (u64)size);
304 kmsan_handle_dma_page((void *)addr, to_go, dir);
310 void kmsan_handle_dma_sg(struct scatterlist *sg, int nents,
311 enum dma_data_direction dir)
313 struct scatterlist *item;
316 for_each_sg(sg, item, nents, i)
317 kmsan_handle_dma(sg_page(item), item->offset, item->length,
321 /* Functions from kmsan-checks.h follow. */
322 void kmsan_poison_memory(const void *address, size_t size, gfp_t flags)
324 if (!kmsan_enabled || kmsan_in_runtime())
326 kmsan_enter_runtime();
327 /* The users may want to poison/unpoison random memory. */
328 kmsan_internal_poison_memory((void *)address, size, flags,
329 KMSAN_POISON_NOCHECK);
330 kmsan_leave_runtime();
332 EXPORT_SYMBOL(kmsan_poison_memory);
334 void kmsan_unpoison_memory(const void *address, size_t size)
336 unsigned long ua_flags;
338 if (!kmsan_enabled || kmsan_in_runtime())
341 ua_flags = user_access_save();
342 kmsan_enter_runtime();
343 /* The users may want to poison/unpoison random memory. */
344 kmsan_internal_unpoison_memory((void *)address, size,
345 KMSAN_POISON_NOCHECK);
346 kmsan_leave_runtime();
347 user_access_restore(ua_flags);
349 EXPORT_SYMBOL(kmsan_unpoison_memory);
352 * Version of kmsan_unpoison_memory() that can be called from within the KMSAN
355 * Non-instrumented IRQ entry functions receive struct pt_regs from assembly
356 * code. Those regs need to be unpoisoned, otherwise using them will result in
358 * Using kmsan_unpoison_memory() is not an option in entry code, because the
359 * return value of in_task() is inconsistent - as a result, certain calls to
360 * kmsan_unpoison_memory() are ignored. kmsan_unpoison_entry_regs() ensures that
361 * the registers are unpoisoned even if kmsan_in_runtime() is true in the early
364 void kmsan_unpoison_entry_regs(const struct pt_regs *regs)
366 unsigned long ua_flags;
371 ua_flags = user_access_save();
372 kmsan_internal_unpoison_memory((void *)regs, sizeof(*regs),
373 KMSAN_POISON_NOCHECK);
374 user_access_restore(ua_flags);
377 void kmsan_check_memory(const void *addr, size_t size)
381 return kmsan_internal_check_memory((void *)addr, size, /*user_addr*/ 0,
384 EXPORT_SYMBOL(kmsan_check_memory);