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 if (!kmsan_enabled
|| kmsan_in_runtime())
45 kmsan_disable_current();
48 void kmsan_slab_alloc(struct kmem_cache
*s
, void *object
, gfp_t flags
)
50 if (unlikely(object
== NULL
))
52 if (!kmsan_enabled
|| kmsan_in_runtime())
55 * There's a ctor or this is an RCU cache - do nothing. The memory
56 * status hasn't changed since last use.
58 if (s
->ctor
|| (s
->flags
& SLAB_TYPESAFE_BY_RCU
))
61 kmsan_enter_runtime();
62 if (flags
& __GFP_ZERO
)
63 kmsan_internal_unpoison_memory(object
, s
->object_size
,
66 kmsan_internal_poison_memory(object
, s
->object_size
, flags
,
68 kmsan_leave_runtime();
71 void kmsan_slab_free(struct kmem_cache
*s
, void *object
)
73 if (!kmsan_enabled
|| kmsan_in_runtime())
76 /* RCU slabs could be legally used after free within the RCU period */
77 if (unlikely(s
->flags
& SLAB_TYPESAFE_BY_RCU
))
80 * If there's a constructor, freed memory must remain in the same state
81 * until the next allocation. We cannot save its state to detect
82 * use-after-free bugs, instead we just keep it unpoisoned.
86 kmsan_enter_runtime();
87 kmsan_internal_poison_memory(object
, s
->object_size
, GFP_KERNEL
,
88 KMSAN_POISON_CHECK
| KMSAN_POISON_FREE
);
89 kmsan_leave_runtime();
92 void kmsan_kmalloc_large(const void *ptr
, size_t size
, gfp_t flags
)
94 if (unlikely(ptr
== NULL
))
96 if (!kmsan_enabled
|| kmsan_in_runtime())
98 kmsan_enter_runtime();
99 if (flags
& __GFP_ZERO
)
100 kmsan_internal_unpoison_memory((void *)ptr
, size
,
103 kmsan_internal_poison_memory((void *)ptr
, size
, flags
,
105 kmsan_leave_runtime();
108 void kmsan_kfree_large(const void *ptr
)
112 if (!kmsan_enabled
|| kmsan_in_runtime())
114 kmsan_enter_runtime();
115 page
= virt_to_head_page((void *)ptr
);
116 KMSAN_WARN_ON(ptr
!= page_address(page
));
117 kmsan_internal_poison_memory((void *)ptr
,
120 KMSAN_POISON_CHECK
| KMSAN_POISON_FREE
);
121 kmsan_leave_runtime();
124 static unsigned long vmalloc_shadow(unsigned long addr
)
126 return (unsigned long)kmsan_get_metadata((void *)addr
,
130 static unsigned long vmalloc_origin(unsigned long addr
)
132 return (unsigned long)kmsan_get_metadata((void *)addr
,
136 void kmsan_vunmap_range_noflush(unsigned long start
, unsigned long end
)
138 __vunmap_range_noflush(vmalloc_shadow(start
), vmalloc_shadow(end
));
139 __vunmap_range_noflush(vmalloc_origin(start
), vmalloc_origin(end
));
140 flush_cache_vmap(vmalloc_shadow(start
), vmalloc_shadow(end
));
141 flush_cache_vmap(vmalloc_origin(start
), vmalloc_origin(end
));
145 * This function creates new shadow/origin pages for the physical pages mapped
146 * into the virtual memory. If those physical pages already had shadow/origin,
149 int kmsan_ioremap_page_range(unsigned long start
, unsigned long end
,
150 phys_addr_t phys_addr
, pgprot_t prot
,
151 unsigned int page_shift
)
153 gfp_t gfp_mask
= GFP_KERNEL
| __GFP_ZERO
;
154 struct page
*shadow
, *origin
;
155 unsigned long off
= 0;
156 int nr
, err
= 0, clean
= 0, mapped
;
158 if (!kmsan_enabled
|| kmsan_in_runtime())
161 nr
= (end
- start
) / PAGE_SIZE
;
162 kmsan_enter_runtime();
163 for (int i
= 0; i
< nr
; i
++, off
+= PAGE_SIZE
, clean
= i
) {
164 shadow
= alloc_pages(gfp_mask
, 1);
165 origin
= alloc_pages(gfp_mask
, 1);
166 if (!shadow
|| !origin
) {
170 mapped
= __vmap_pages_range_noflush(
171 vmalloc_shadow(start
+ off
),
172 vmalloc_shadow(start
+ off
+ PAGE_SIZE
), prot
, &shadow
,
179 mapped
= __vmap_pages_range_noflush(
180 vmalloc_origin(start
+ off
),
181 vmalloc_origin(start
+ off
+ PAGE_SIZE
), prot
, &origin
,
184 __vunmap_range_noflush(
185 vmalloc_shadow(start
+ off
),
186 vmalloc_shadow(start
+ off
+ PAGE_SIZE
));
192 /* Page mapping loop finished normally, nothing to clean up. */
198 * Something went wrong. Clean up shadow/origin pages allocated
199 * on the last loop iteration, then delete mappings created
200 * during the previous iterations.
203 __free_pages(shadow
, 1);
205 __free_pages(origin
, 1);
206 __vunmap_range_noflush(
207 vmalloc_shadow(start
),
208 vmalloc_shadow(start
+ clean
* PAGE_SIZE
));
209 __vunmap_range_noflush(
210 vmalloc_origin(start
),
211 vmalloc_origin(start
+ clean
* PAGE_SIZE
));
213 flush_cache_vmap(vmalloc_shadow(start
), vmalloc_shadow(end
));
214 flush_cache_vmap(vmalloc_origin(start
), vmalloc_origin(end
));
215 kmsan_leave_runtime();
219 void kmsan_iounmap_page_range(unsigned long start
, unsigned long end
)
221 unsigned long v_shadow
, v_origin
;
222 struct page
*shadow
, *origin
;
225 if (!kmsan_enabled
|| kmsan_in_runtime())
228 nr
= (end
- start
) / PAGE_SIZE
;
229 kmsan_enter_runtime();
230 v_shadow
= (unsigned long)vmalloc_shadow(start
);
231 v_origin
= (unsigned long)vmalloc_origin(start
);
232 for (int i
= 0; i
< nr
;
233 i
++, v_shadow
+= PAGE_SIZE
, v_origin
+= PAGE_SIZE
) {
234 shadow
= kmsan_vmalloc_to_page_or_null((void *)v_shadow
);
235 origin
= kmsan_vmalloc_to_page_or_null((void *)v_origin
);
236 __vunmap_range_noflush(v_shadow
, vmalloc_shadow(end
));
237 __vunmap_range_noflush(v_origin
, vmalloc_origin(end
));
239 __free_pages(shadow
, 1);
241 __free_pages(origin
, 1);
243 flush_cache_vmap(vmalloc_shadow(start
), vmalloc_shadow(end
));
244 flush_cache_vmap(vmalloc_origin(start
), vmalloc_origin(end
));
245 kmsan_leave_runtime();
248 void kmsan_copy_to_user(void __user
*to
, const void *from
, size_t to_copy
,
251 unsigned long ua_flags
;
253 if (!kmsan_enabled
|| kmsan_in_runtime())
256 * At this point we've copied the memory already. It's hard to check it
257 * before copying, as the size of actually copied buffer is unknown.
260 /* copy_to_user() may copy zero bytes. No need to check. */
263 /* Or maybe copy_to_user() failed to copy anything. */
267 ua_flags
= user_access_save();
268 if (!IS_ENABLED(CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
) ||
269 (u64
)to
< TASK_SIZE
) {
270 /* This is a user memory access, check it. */
271 kmsan_internal_check_memory((void *)from
, to_copy
- left
, to
,
272 REASON_COPY_TO_USER
);
274 /* Otherwise this is a kernel memory access. This happens when a
275 * compat syscall passes an argument allocated on the kernel
276 * stack to a real syscall.
277 * Don't check anything, just copy the shadow of the copied
280 kmsan_internal_memmove_metadata((void *)to
, (void *)from
,
283 user_access_restore(ua_flags
);
285 EXPORT_SYMBOL(kmsan_copy_to_user
);
287 void kmsan_memmove(void *to
, const void *from
, size_t size
)
289 if (!kmsan_enabled
|| kmsan_in_runtime())
292 kmsan_enter_runtime();
293 kmsan_internal_memmove_metadata(to
, (void *)from
, size
);
294 kmsan_leave_runtime();
296 EXPORT_SYMBOL(kmsan_memmove
);
298 /* Helper function to check an URB. */
299 void kmsan_handle_urb(const struct urb
*urb
, bool is_out
)
304 kmsan_internal_check_memory(urb
->transfer_buffer
,
305 urb
->transfer_buffer_length
,
309 kmsan_internal_unpoison_memory(urb
->transfer_buffer
,
310 urb
->transfer_buffer_length
,
313 EXPORT_SYMBOL_GPL(kmsan_handle_urb
);
315 static void kmsan_handle_dma_page(const void *addr
, size_t size
,
316 enum dma_data_direction dir
)
319 case DMA_BIDIRECTIONAL
:
320 kmsan_internal_check_memory((void *)addr
, size
,
321 /*user_addr*/ NULL
, REASON_ANY
);
322 kmsan_internal_unpoison_memory((void *)addr
, size
,
326 kmsan_internal_check_memory((void *)addr
, size
,
327 /*user_addr*/ NULL
, REASON_ANY
);
329 case DMA_FROM_DEVICE
:
330 kmsan_internal_unpoison_memory((void *)addr
, size
,
338 /* Helper function to handle DMA data transfers. */
339 void kmsan_handle_dma(struct page
*page
, size_t offset
, size_t size
,
340 enum dma_data_direction dir
)
342 u64 page_offset
, to_go
, addr
;
344 if (PageHighMem(page
))
346 addr
= (u64
)page_address(page
) + offset
;
348 * The kernel may occasionally give us adjacent DMA pages not belonging
349 * to the same allocation. Process them separately to avoid triggering
350 * internal KMSAN checks.
353 page_offset
= offset_in_page(addr
);
354 to_go
= min(PAGE_SIZE
- page_offset
, (u64
)size
);
355 kmsan_handle_dma_page((void *)addr
, to_go
, dir
);
361 void kmsan_handle_dma_sg(struct scatterlist
*sg
, int nents
,
362 enum dma_data_direction dir
)
364 struct scatterlist
*item
;
367 for_each_sg(sg
, item
, nents
, i
)
368 kmsan_handle_dma(sg_page(item
), item
->offset
, item
->length
,
372 /* Functions from kmsan-checks.h follow. */
375 * To create an origin, kmsan_poison_memory() unwinds the stacks and stores it
376 * into the stack depot. This may cause deadlocks if done from within KMSAN
377 * runtime, therefore we bail out if kmsan_in_runtime().
379 void kmsan_poison_memory(const void *address
, size_t size
, gfp_t flags
)
381 if (!kmsan_enabled
|| kmsan_in_runtime())
383 kmsan_enter_runtime();
384 /* The users may want to poison/unpoison random memory. */
385 kmsan_internal_poison_memory((void *)address
, size
, flags
,
386 KMSAN_POISON_NOCHECK
);
387 kmsan_leave_runtime();
389 EXPORT_SYMBOL(kmsan_poison_memory
);
392 * Unlike kmsan_poison_memory(), this function can be used from within KMSAN
393 * runtime, because it does not trigger allocations or call instrumented code.
395 void kmsan_unpoison_memory(const void *address
, size_t size
)
397 unsigned long ua_flags
;
402 ua_flags
= user_access_save();
403 /* The users may want to poison/unpoison random memory. */
404 kmsan_internal_unpoison_memory((void *)address
, size
,
405 KMSAN_POISON_NOCHECK
);
406 user_access_restore(ua_flags
);
408 EXPORT_SYMBOL(kmsan_unpoison_memory
);
411 * Version of kmsan_unpoison_memory() called from IRQ entry functions.
413 void kmsan_unpoison_entry_regs(const struct pt_regs
*regs
)
415 kmsan_unpoison_memory((void *)regs
, sizeof(*regs
));
418 void kmsan_check_memory(const void *addr
, size_t size
)
422 return kmsan_internal_check_memory((void *)addr
, size
,
423 /*user_addr*/ NULL
, REASON_ANY
);
425 EXPORT_SYMBOL(kmsan_check_memory
);
427 void kmsan_enable_current(void)
429 KMSAN_WARN_ON(current
->kmsan_ctx
.depth
== 0);
430 current
->kmsan_ctx
.depth
--;
432 EXPORT_SYMBOL(kmsan_enable_current
);
434 void kmsan_disable_current(void)
436 current
->kmsan_ctx
.depth
++;
437 KMSAN_WARN_ON(current
->kmsan_ctx
.depth
== 0);
439 EXPORT_SYMBOL(kmsan_disable_current
);