Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * This file contains common generic and tag-based KASAN code.
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7 *
8 * Some code borrowed from https://github.com/xairy/kasan-prototype by
9 * Andrey Konovalov <andreyknvl@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
17#include <linux/export.h>
18#include <linux/init.h>
19#include <linux/kasan.h>
20#include <linux/kernel.h>
21#include <linux/kmemleak.h>
22#include <linux/linkage.h>
23#include <linux/memblock.h>
24#include <linux/memory.h>
25#include <linux/mm.h>
26#include <linux/module.h>
27#include <linux/printk.h>
28#include <linux/sched.h>
29#include <linux/sched/task_stack.h>
30#include <linux/slab.h>
31#include <linux/stacktrace.h>
32#include <linux/string.h>
33#include <linux/types.h>
34#include <linux/vmalloc.h>
35#include <linux/bug.h>
36
37#include <asm/cacheflush.h>
38#include <asm/tlbflush.h>
39
40#include "kasan.h"
41#include "../slab.h"
42
43depot_stack_handle_t kasan_save_stack(gfp_t flags)
44{
45 unsigned long entries[KASAN_STACK_DEPTH];
46 unsigned int nr_entries;
47
48 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
49 nr_entries = filter_irq_stacks(entries, nr_entries);
50 return stack_depot_save(entries, nr_entries, flags);
51}
52
53void kasan_set_track(struct kasan_track *track, gfp_t flags)
54{
55 track->pid = current->pid;
56 track->stack = kasan_save_stack(flags);
57}
58
59void kasan_enable_current(void)
60{
61 current->kasan_depth++;
62}
63
64void kasan_disable_current(void)
65{
66 current->kasan_depth--;
67}
68
69bool __kasan_check_read(const volatile void *p, unsigned int size)
70{
71 return check_memory_region((unsigned long)p, size, false, _RET_IP_);
72}
73EXPORT_SYMBOL(__kasan_check_read);
74
75bool __kasan_check_write(const volatile void *p, unsigned int size)
76{
77 return check_memory_region((unsigned long)p, size, true, _RET_IP_);
78}
79EXPORT_SYMBOL(__kasan_check_write);
80
81#undef memset
82void *memset(void *addr, int c, size_t len)
83{
84 if (!check_memory_region((unsigned long)addr, len, true, _RET_IP_))
85 return NULL;
86
87 return __memset(addr, c, len);
88}
89
90#ifdef __HAVE_ARCH_MEMMOVE
91#undef memmove
92void *memmove(void *dest, const void *src, size_t len)
93{
94 if (!check_memory_region((unsigned long)src, len, false, _RET_IP_) ||
95 !check_memory_region((unsigned long)dest, len, true, _RET_IP_))
96 return NULL;
97
98 return __memmove(dest, src, len);
99}
100#endif
101
102#undef memcpy
103void *memcpy(void *dest, const void *src, size_t len)
104{
105 if (!check_memory_region((unsigned long)src, len, false, _RET_IP_) ||
106 !check_memory_region((unsigned long)dest, len, true, _RET_IP_))
107 return NULL;
108
109 return __memcpy(dest, src, len);
110}
111
112/*
113 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
114 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
115 */
116void kasan_poison_shadow(const void *address, size_t size, u8 value)
117{
118 void *shadow_start, *shadow_end;
119
120 /*
121 * Perform shadow offset calculation based on untagged address, as
122 * some of the callers (e.g. kasan_poison_object_data) pass tagged
123 * addresses to this function.
124 */
125 address = reset_tag(address);
126
127 shadow_start = kasan_mem_to_shadow(address);
128 shadow_end = kasan_mem_to_shadow(address + size);
129
130 __memset(shadow_start, value, shadow_end - shadow_start);
131}
132
133void kasan_unpoison_shadow(const void *address, size_t size)
134{
135 u8 tag = get_tag(address);
136
137 /*
138 * Perform shadow offset calculation based on untagged address, as
139 * some of the callers (e.g. kasan_unpoison_object_data) pass tagged
140 * addresses to this function.
141 */
142 address = reset_tag(address);
143
144 kasan_poison_shadow(address, size, tag);
145
146 if (size & KASAN_SHADOW_MASK) {
147 u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
148
149 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
150 *shadow = tag;
151 else
152 *shadow = size & KASAN_SHADOW_MASK;
153 }
154}
155
156static void __kasan_unpoison_stack(struct task_struct *task, const void *sp)
157{
158 void *base = task_stack_page(task);
159 size_t size = sp - base;
160
161 kasan_unpoison_shadow(base, size);
162}
163
164/* Unpoison the entire stack for a task. */
165void kasan_unpoison_task_stack(struct task_struct *task)
166{
167 __kasan_unpoison_stack(task, task_stack_page(task) + THREAD_SIZE);
168}
169
170/* Unpoison the stack for the current task beyond a watermark sp value. */
171asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
172{
173 /*
174 * Calculate the task stack base address. Avoid using 'current'
175 * because this function is called by early resume code which hasn't
176 * yet set up the percpu register (%gs).
177 */
178 void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
179
180 kasan_unpoison_shadow(base, watermark - base);
181}
182
183void kasan_alloc_pages(struct page *page, unsigned int order)
184{
185 u8 tag;
186 unsigned long i;
187
188 if (unlikely(PageHighMem(page)))
189 return;
190
191 tag = random_tag();
192 for (i = 0; i < (1 << order); i++)
193 page_kasan_tag_set(page + i, tag);
194 kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
195}
196
197void kasan_free_pages(struct page *page, unsigned int order)
198{
199 if (likely(!PageHighMem(page)))
200 kasan_poison_shadow(page_address(page),
201 PAGE_SIZE << order,
202 KASAN_FREE_PAGE);
203}
204
205/*
206 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
207 * For larger allocations larger redzones are used.
208 */
209static inline unsigned int optimal_redzone(unsigned int object_size)
210{
211 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
212 return 0;
213
214 return
215 object_size <= 64 - 16 ? 16 :
216 object_size <= 128 - 32 ? 32 :
217 object_size <= 512 - 64 ? 64 :
218 object_size <= 4096 - 128 ? 128 :
219 object_size <= (1 << 14) - 256 ? 256 :
220 object_size <= (1 << 15) - 512 ? 512 :
221 object_size <= (1 << 16) - 1024 ? 1024 : 2048;
222}
223
224void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
225 slab_flags_t *flags)
226{
227 unsigned int orig_size = *size;
228 unsigned int redzone_size;
229 int redzone_adjust;
230
231 /* Add alloc meta. */
232 cache->kasan_info.alloc_meta_offset = *size;
233 *size += sizeof(struct kasan_alloc_meta);
234
235 /* Add free meta. */
236 if (IS_ENABLED(CONFIG_KASAN_GENERIC) &&
237 (cache->flags & SLAB_TYPESAFE_BY_RCU || cache->ctor ||
238 cache->object_size < sizeof(struct kasan_free_meta))) {
239 cache->kasan_info.free_meta_offset = *size;
240 *size += sizeof(struct kasan_free_meta);
241 }
242
243 redzone_size = optimal_redzone(cache->object_size);
244 redzone_adjust = redzone_size - (*size - cache->object_size);
245 if (redzone_adjust > 0)
246 *size += redzone_adjust;
247
248 *size = min_t(unsigned int, KMALLOC_MAX_SIZE,
249 max(*size, cache->object_size + redzone_size));
250
251 /*
252 * If the metadata doesn't fit, don't enable KASAN at all.
253 */
254 if (*size <= cache->kasan_info.alloc_meta_offset ||
255 *size <= cache->kasan_info.free_meta_offset) {
256 cache->kasan_info.alloc_meta_offset = 0;
257 cache->kasan_info.free_meta_offset = 0;
258 *size = orig_size;
259 return;
260 }
261
262 *flags |= SLAB_KASAN;
263}
264
265size_t kasan_metadata_size(struct kmem_cache *cache)
266{
267 return (cache->kasan_info.alloc_meta_offset ?
268 sizeof(struct kasan_alloc_meta) : 0) +
269 (cache->kasan_info.free_meta_offset ?
270 sizeof(struct kasan_free_meta) : 0);
271}
272
273struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
274 const void *object)
275{
276 return (void *)object + cache->kasan_info.alloc_meta_offset;
277}
278
279struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
280 const void *object)
281{
282 BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
283 return (void *)object + cache->kasan_info.free_meta_offset;
284}
285
286void kasan_poison_slab(struct page *page)
287{
288 unsigned long i;
289
290 for (i = 0; i < compound_nr(page); i++)
291 page_kasan_tag_reset(page + i);
292 kasan_poison_shadow(page_address(page), page_size(page),
293 KASAN_KMALLOC_REDZONE);
294}
295
296void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
297{
298 kasan_unpoison_shadow(object, cache->object_size);
299}
300
301void kasan_poison_object_data(struct kmem_cache *cache, void *object)
302{
303 kasan_poison_shadow(object,
304 round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
305 KASAN_KMALLOC_REDZONE);
306}
307
308/*
309 * This function assigns a tag to an object considering the following:
310 * 1. A cache might have a constructor, which might save a pointer to a slab
311 * object somewhere (e.g. in the object itself). We preassign a tag for
312 * each object in caches with constructors during slab creation and reuse
313 * the same tag each time a particular object is allocated.
314 * 2. A cache might be SLAB_TYPESAFE_BY_RCU, which means objects can be
315 * accessed after being freed. We preassign tags for objects in these
316 * caches as well.
317 * 3. For SLAB allocator we can't preassign tags randomly since the freelist
318 * is stored as an array of indexes instead of a linked list. Assign tags
319 * based on objects indexes, so that objects that are next to each other
320 * get different tags.
321 */
322static u8 assign_tag(struct kmem_cache *cache, const void *object,
323 bool init, bool keep_tag)
324{
325 /*
326 * 1. When an object is kmalloc()'ed, two hooks are called:
327 * kasan_slab_alloc() and kasan_kmalloc(). We assign the
328 * tag only in the first one.
329 * 2. We reuse the same tag for krealloc'ed objects.
330 */
331 if (keep_tag)
332 return get_tag(object);
333
334 /*
335 * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU
336 * set, assign a tag when the object is being allocated (init == false).
337 */
338 if (!cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU))
339 return init ? KASAN_TAG_KERNEL : random_tag();
340
341 /* For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU: */
342#ifdef CONFIG_SLAB
343 /* For SLAB assign tags based on the object index in the freelist. */
344 return (u8)obj_to_index(cache, virt_to_page(object), (void *)object);
345#else
346 /*
347 * For SLUB assign a random tag during slab creation, otherwise reuse
348 * the already assigned tag.
349 */
350 return init ? random_tag() : get_tag(object);
351#endif
352}
353
354void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
355 const void *object)
356{
357 struct kasan_alloc_meta *alloc_info;
358
359 if (!(cache->flags & SLAB_KASAN))
360 return (void *)object;
361
362 alloc_info = get_alloc_info(cache, object);
363 __memset(alloc_info, 0, sizeof(*alloc_info));
364
365 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
366 object = set_tag(object,
367 assign_tag(cache, object, true, false));
368
369 return (void *)object;
370}
371
372static inline bool shadow_invalid(u8 tag, s8 shadow_byte)
373{
374 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
375 return shadow_byte < 0 ||
376 shadow_byte >= KASAN_SHADOW_SCALE_SIZE;
377
378 /* else CONFIG_KASAN_SW_TAGS: */
379 if ((u8)shadow_byte == KASAN_TAG_INVALID)
380 return true;
381 if ((tag != KASAN_TAG_KERNEL) && (tag != (u8)shadow_byte))
382 return true;
383
384 return false;
385}
386
387static bool __kasan_slab_free(struct kmem_cache *cache, void *object,
388 unsigned long ip, bool quarantine)
389{
390 s8 shadow_byte;
391 u8 tag;
392 void *tagged_object;
393 unsigned long rounded_up_size;
394
395 tag = get_tag(object);
396 tagged_object = object;
397 object = reset_tag(object);
398
399 if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) !=
400 object)) {
401 kasan_report_invalid_free(tagged_object, ip);
402 return true;
403 }
404
405 /* RCU slabs could be legally used after free within the RCU period */
406 if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
407 return false;
408
409 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object));
410 if (shadow_invalid(tag, shadow_byte)) {
411 kasan_report_invalid_free(tagged_object, ip);
412 return true;
413 }
414
415 rounded_up_size = round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE);
416 kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
417
418 if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine) ||
419 unlikely(!(cache->flags & SLAB_KASAN)))
420 return false;
421
422 kasan_set_free_info(cache, object, tag);
423
424 quarantine_put(get_free_info(cache, object), cache);
425
426 return IS_ENABLED(CONFIG_KASAN_GENERIC);
427}
428
429bool kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip)
430{
431 return __kasan_slab_free(cache, object, ip, true);
432}
433
434static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object,
435 size_t size, gfp_t flags, bool keep_tag)
436{
437 unsigned long redzone_start;
438 unsigned long redzone_end;
439 u8 tag = 0xff;
440
441 if (gfpflags_allow_blocking(flags))
442 quarantine_reduce();
443
444 if (unlikely(object == NULL))
445 return NULL;
446
447 redzone_start = round_up((unsigned long)(object + size),
448 KASAN_SHADOW_SCALE_SIZE);
449 redzone_end = round_up((unsigned long)object + cache->object_size,
450 KASAN_SHADOW_SCALE_SIZE);
451
452 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
453 tag = assign_tag(cache, object, false, keep_tag);
454
455 /* Tag is ignored in set_tag without CONFIG_KASAN_SW_TAGS */
456 kasan_unpoison_shadow(set_tag(object, tag), size);
457 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
458 KASAN_KMALLOC_REDZONE);
459
460 if (cache->flags & SLAB_KASAN)
461 kasan_set_track(&get_alloc_info(cache, object)->alloc_track, flags);
462
463 return set_tag(object, tag);
464}
465
466void * __must_check kasan_slab_alloc(struct kmem_cache *cache, void *object,
467 gfp_t flags)
468{
469 return __kasan_kmalloc(cache, object, cache->object_size, flags, false);
470}
471
472void * __must_check kasan_kmalloc(struct kmem_cache *cache, const void *object,
473 size_t size, gfp_t flags)
474{
475 return __kasan_kmalloc(cache, object, size, flags, true);
476}
477EXPORT_SYMBOL(kasan_kmalloc);
478
479void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
480 gfp_t flags)
481{
482 struct page *page;
483 unsigned long redzone_start;
484 unsigned long redzone_end;
485
486 if (gfpflags_allow_blocking(flags))
487 quarantine_reduce();
488
489 if (unlikely(ptr == NULL))
490 return NULL;
491
492 page = virt_to_page(ptr);
493 redzone_start = round_up((unsigned long)(ptr + size),
494 KASAN_SHADOW_SCALE_SIZE);
495 redzone_end = (unsigned long)ptr + page_size(page);
496
497 kasan_unpoison_shadow(ptr, size);
498 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
499 KASAN_PAGE_REDZONE);
500
501 return (void *)ptr;
502}
503
504void * __must_check kasan_krealloc(const void *object, size_t size, gfp_t flags)
505{
506 struct page *page;
507
508 if (unlikely(object == ZERO_SIZE_PTR))
509 return (void *)object;
510
511 page = virt_to_head_page(object);
512
513 if (unlikely(!PageSlab(page)))
514 return kasan_kmalloc_large(object, size, flags);
515 else
516 return __kasan_kmalloc(page->slab_cache, object, size,
517 flags, true);
518}
519
520void kasan_poison_kfree(void *ptr, unsigned long ip)
521{
522 struct page *page;
523
524 page = virt_to_head_page(ptr);
525
526 if (unlikely(!PageSlab(page))) {
527 if (ptr != page_address(page)) {
528 kasan_report_invalid_free(ptr, ip);
529 return;
530 }
531 kasan_poison_shadow(ptr, page_size(page), KASAN_FREE_PAGE);
532 } else {
533 __kasan_slab_free(page->slab_cache, ptr, ip, false);
534 }
535}
536
537void kasan_kfree_large(void *ptr, unsigned long ip)
538{
539 if (ptr != page_address(virt_to_head_page(ptr)))
540 kasan_report_invalid_free(ptr, ip);
541 /* The object will be poisoned by page_alloc. */
542}
543
544#ifndef CONFIG_KASAN_VMALLOC
545int kasan_module_alloc(void *addr, size_t size)
546{
547 void *ret;
548 size_t scaled_size;
549 size_t shadow_size;
550 unsigned long shadow_start;
551
552 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
553 scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT;
554 shadow_size = round_up(scaled_size, PAGE_SIZE);
555
556 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
557 return -EINVAL;
558
559 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
560 shadow_start + shadow_size,
561 GFP_KERNEL,
562 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
563 __builtin_return_address(0));
564
565 if (ret) {
566 __memset(ret, KASAN_SHADOW_INIT, shadow_size);
567 find_vm_area(addr)->flags |= VM_KASAN;
568 kmemleak_ignore(ret);
569 return 0;
570 }
571
572 return -ENOMEM;
573}
574
575void kasan_free_shadow(const struct vm_struct *vm)
576{
577 if (vm->flags & VM_KASAN)
578 vfree(kasan_mem_to_shadow(vm->addr));
579}
580#endif
581
582#ifdef CONFIG_MEMORY_HOTPLUG
583static bool shadow_mapped(unsigned long addr)
584{
585 pgd_t *pgd = pgd_offset_k(addr);
586 p4d_t *p4d;
587 pud_t *pud;
588 pmd_t *pmd;
589 pte_t *pte;
590
591 if (pgd_none(*pgd))
592 return false;
593 p4d = p4d_offset(pgd, addr);
594 if (p4d_none(*p4d))
595 return false;
596 pud = pud_offset(p4d, addr);
597 if (pud_none(*pud))
598 return false;
599
600 /*
601 * We can't use pud_large() or pud_huge(), the first one is
602 * arch-specific, the last one depends on HUGETLB_PAGE. So let's abuse
603 * pud_bad(), if pud is bad then it's bad because it's huge.
604 */
605 if (pud_bad(*pud))
606 return true;
607 pmd = pmd_offset(pud, addr);
608 if (pmd_none(*pmd))
609 return false;
610
611 if (pmd_bad(*pmd))
612 return true;
613 pte = pte_offset_kernel(pmd, addr);
614 return !pte_none(*pte);
615}
616
617static int __meminit kasan_mem_notifier(struct notifier_block *nb,
618 unsigned long action, void *data)
619{
620 struct memory_notify *mem_data = data;
621 unsigned long nr_shadow_pages, start_kaddr, shadow_start;
622 unsigned long shadow_end, shadow_size;
623
624 nr_shadow_pages = mem_data->nr_pages >> KASAN_SHADOW_SCALE_SHIFT;
625 start_kaddr = (unsigned long)pfn_to_kaddr(mem_data->start_pfn);
626 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)start_kaddr);
627 shadow_size = nr_shadow_pages << PAGE_SHIFT;
628 shadow_end = shadow_start + shadow_size;
629
630 if (WARN_ON(mem_data->nr_pages % KASAN_SHADOW_SCALE_SIZE) ||
631 WARN_ON(start_kaddr % (KASAN_SHADOW_SCALE_SIZE << PAGE_SHIFT)))
632 return NOTIFY_BAD;
633
634 switch (action) {
635 case MEM_GOING_ONLINE: {
636 void *ret;
637
638 /*
639 * If shadow is mapped already than it must have been mapped
640 * during the boot. This could happen if we onlining previously
641 * offlined memory.
642 */
643 if (shadow_mapped(shadow_start))
644 return NOTIFY_OK;
645
646 ret = __vmalloc_node_range(shadow_size, PAGE_SIZE, shadow_start,
647 shadow_end, GFP_KERNEL,
648 PAGE_KERNEL, VM_NO_GUARD,
649 pfn_to_nid(mem_data->start_pfn),
650 __builtin_return_address(0));
651 if (!ret)
652 return NOTIFY_BAD;
653
654 kmemleak_ignore(ret);
655 return NOTIFY_OK;
656 }
657 case MEM_CANCEL_ONLINE:
658 case MEM_OFFLINE: {
659 struct vm_struct *vm;
660
661 /*
662 * shadow_start was either mapped during boot by kasan_init()
663 * or during memory online by __vmalloc_node_range().
664 * In the latter case we can use vfree() to free shadow.
665 * Non-NULL result of the find_vm_area() will tell us if
666 * that was the second case.
667 *
668 * Currently it's not possible to free shadow mapped
669 * during boot by kasan_init(). It's because the code
670 * to do that hasn't been written yet. So we'll just
671 * leak the memory.
672 */
673 vm = find_vm_area((void *)shadow_start);
674 if (vm)
675 vfree((void *)shadow_start);
676 }
677 }
678
679 return NOTIFY_OK;
680}
681
682static int __init kasan_memhotplug_init(void)
683{
684 hotplug_memory_notifier(kasan_mem_notifier, 0);
685
686 return 0;
687}
688
689core_initcall(kasan_memhotplug_init);
690#endif
691
692#ifdef CONFIG_KASAN_VMALLOC
693static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr,
694 void *unused)
695{
696 unsigned long page;
697 pte_t pte;
698
699 if (likely(!pte_none(*ptep)))
700 return 0;
701
702 page = __get_free_page(GFP_KERNEL);
703 if (!page)
704 return -ENOMEM;
705
706 memset((void *)page, KASAN_VMALLOC_INVALID, PAGE_SIZE);
707 pte = pfn_pte(PFN_DOWN(__pa(page)), PAGE_KERNEL);
708
709 spin_lock(&init_mm.page_table_lock);
710 if (likely(pte_none(*ptep))) {
711 set_pte_at(&init_mm, addr, ptep, pte);
712 page = 0;
713 }
714 spin_unlock(&init_mm.page_table_lock);
715 if (page)
716 free_page(page);
717 return 0;
718}
719
720int kasan_populate_vmalloc(unsigned long addr, unsigned long size)
721{
722 unsigned long shadow_start, shadow_end;
723 int ret;
724
725 if (!is_vmalloc_or_module_addr((void *)addr))
726 return 0;
727
728 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)addr);
729 shadow_start = ALIGN_DOWN(shadow_start, PAGE_SIZE);
730 shadow_end = (unsigned long)kasan_mem_to_shadow((void *)addr + size);
731 shadow_end = ALIGN(shadow_end, PAGE_SIZE);
732
733 ret = apply_to_page_range(&init_mm, shadow_start,
734 shadow_end - shadow_start,
735 kasan_populate_vmalloc_pte, NULL);
736 if (ret)
737 return ret;
738
739 flush_cache_vmap(shadow_start, shadow_end);
740
741 /*
742 * We need to be careful about inter-cpu effects here. Consider:
743 *
744 * CPU#0 CPU#1
745 * WRITE_ONCE(p, vmalloc(100)); while (x = READ_ONCE(p)) ;
746 * p[99] = 1;
747 *
748 * With compiler instrumentation, that ends up looking like this:
749 *
750 * CPU#0 CPU#1
751 * // vmalloc() allocates memory
752 * // let a = area->addr
753 * // we reach kasan_populate_vmalloc
754 * // and call kasan_unpoison_shadow:
755 * STORE shadow(a), unpoison_val
756 * ...
757 * STORE shadow(a+99), unpoison_val x = LOAD p
758 * // rest of vmalloc process <data dependency>
759 * STORE p, a LOAD shadow(x+99)
760 *
761 * If there is no barrier between the end of unpoisioning the shadow
762 * and the store of the result to p, the stores could be committed
763 * in a different order by CPU#0, and CPU#1 could erroneously observe
764 * poison in the shadow.
765 *
766 * We need some sort of barrier between the stores.
767 *
768 * In the vmalloc() case, this is provided by a smp_wmb() in
769 * clear_vm_uninitialized_flag(). In the per-cpu allocator and in
770 * get_vm_area() and friends, the caller gets shadow allocated but
771 * doesn't have any pages mapped into the virtual address space that
772 * has been reserved. Mapping those pages in will involve taking and
773 * releasing a page-table lock, which will provide the barrier.
774 */
775
776 return 0;
777}
778
779/*
780 * Poison the shadow for a vmalloc region. Called as part of the
781 * freeing process at the time the region is freed.
782 */
783void kasan_poison_vmalloc(const void *start, unsigned long size)
784{
785 if (!is_vmalloc_or_module_addr(start))
786 return;
787
788 size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
789 kasan_poison_shadow(start, size, KASAN_VMALLOC_INVALID);
790}
791
792void kasan_unpoison_vmalloc(const void *start, unsigned long size)
793{
794 if (!is_vmalloc_or_module_addr(start))
795 return;
796
797 kasan_unpoison_shadow(start, size);
798}
799
800static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr,
801 void *unused)
802{
803 unsigned long page;
804
805 page = (unsigned long)__va(pte_pfn(*ptep) << PAGE_SHIFT);
806
807 spin_lock(&init_mm.page_table_lock);
808
809 if (likely(!pte_none(*ptep))) {
810 pte_clear(&init_mm, addr, ptep);
811 free_page(page);
812 }
813 spin_unlock(&init_mm.page_table_lock);
814
815 return 0;
816}
817
818/*
819 * Release the backing for the vmalloc region [start, end), which
820 * lies within the free region [free_region_start, free_region_end).
821 *
822 * This can be run lazily, long after the region was freed. It runs
823 * under vmap_area_lock, so it's not safe to interact with the vmalloc/vmap
824 * infrastructure.
825 *
826 * How does this work?
827 * -------------------
828 *
829 * We have a region that is page aligned, labelled as A.
830 * That might not map onto the shadow in a way that is page-aligned:
831 *
832 * start end
833 * v v
834 * |????????|????????|AAAAAAAA|AA....AA|AAAAAAAA|????????| < vmalloc
835 * -------- -------- -------- -------- --------
836 * | | | | |
837 * | | | /-------/ |
838 * \-------\|/------/ |/---------------/
839 * ||| ||
840 * |??AAAAAA|AAAAAAAA|AA??????| < shadow
841 * (1) (2) (3)
842 *
843 * First we align the start upwards and the end downwards, so that the
844 * shadow of the region aligns with shadow page boundaries. In the
845 * example, this gives us the shadow page (2). This is the shadow entirely
846 * covered by this allocation.
847 *
848 * Then we have the tricky bits. We want to know if we can free the
849 * partially covered shadow pages - (1) and (3) in the example. For this,
850 * we are given the start and end of the free region that contains this
851 * allocation. Extending our previous example, we could have:
852 *
853 * free_region_start free_region_end
854 * | start end |
855 * v v v v
856 * |FFFFFFFF|FFFFFFFF|AAAAAAAA|AA....AA|AAAAAAAA|FFFFFFFF| < vmalloc
857 * -------- -------- -------- -------- --------
858 * | | | | |
859 * | | | /-------/ |
860 * \-------\|/------/ |/---------------/
861 * ||| ||
862 * |FFAAAAAA|AAAAAAAA|AAF?????| < shadow
863 * (1) (2) (3)
864 *
865 * Once again, we align the start of the free region up, and the end of
866 * the free region down so that the shadow is page aligned. So we can free
867 * page (1) - we know no allocation currently uses anything in that page,
868 * because all of it is in the vmalloc free region. But we cannot free
869 * page (3), because we can't be sure that the rest of it is unused.
870 *
871 * We only consider pages that contain part of the original region for
872 * freeing: we don't try to free other pages from the free region or we'd
873 * end up trying to free huge chunks of virtual address space.
874 *
875 * Concurrency
876 * -----------
877 *
878 * How do we know that we're not freeing a page that is simultaneously
879 * being used for a fresh allocation in kasan_populate_vmalloc(_pte)?
880 *
881 * We _can_ have kasan_release_vmalloc and kasan_populate_vmalloc running
882 * at the same time. While we run under free_vmap_area_lock, the population
883 * code does not.
884 *
885 * free_vmap_area_lock instead operates to ensure that the larger range
886 * [free_region_start, free_region_end) is safe: because __alloc_vmap_area and
887 * the per-cpu region-finding algorithm both run under free_vmap_area_lock,
888 * no space identified as free will become used while we are running. This
889 * means that so long as we are careful with alignment and only free shadow
890 * pages entirely covered by the free region, we will not run in to any
891 * trouble - any simultaneous allocations will be for disjoint regions.
892 */
893void kasan_release_vmalloc(unsigned long start, unsigned long end,
894 unsigned long free_region_start,
895 unsigned long free_region_end)
896{
897 void *shadow_start, *shadow_end;
898 unsigned long region_start, region_end;
899 unsigned long size;
900
901 region_start = ALIGN(start, PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
902 region_end = ALIGN_DOWN(end, PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
903
904 free_region_start = ALIGN(free_region_start,
905 PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
906
907 if (start != region_start &&
908 free_region_start < region_start)
909 region_start -= PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE;
910
911 free_region_end = ALIGN_DOWN(free_region_end,
912 PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
913
914 if (end != region_end &&
915 free_region_end > region_end)
916 region_end += PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE;
917
918 shadow_start = kasan_mem_to_shadow((void *)region_start);
919 shadow_end = kasan_mem_to_shadow((void *)region_end);
920
921 if (shadow_end > shadow_start) {
922 size = shadow_end - shadow_start;
923 apply_to_existing_page_range(&init_mm,
924 (unsigned long)shadow_start,
925 size, kasan_depopulate_vmalloc_pte,
926 NULL);
927 flush_tlb_kernel_range((unsigned long)shadow_start,
928 (unsigned long)shadow_end);
929 }
930}
931#endif
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * This file contains common KASAN code.
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7 *
8 * Some code borrowed from https://github.com/xairy/kasan-prototype by
9 * Andrey Konovalov <andreyknvl@gmail.com>
10 */
11
12#include <linux/export.h>
13#include <linux/init.h>
14#include <linux/kasan.h>
15#include <linux/kernel.h>
16#include <linux/linkage.h>
17#include <linux/memblock.h>
18#include <linux/memory.h>
19#include <linux/mm.h>
20#include <linux/module.h>
21#include <linux/printk.h>
22#include <linux/sched.h>
23#include <linux/sched/task_stack.h>
24#include <linux/slab.h>
25#include <linux/stacktrace.h>
26#include <linux/string.h>
27#include <linux/types.h>
28#include <linux/bug.h>
29
30#include "kasan.h"
31#include "../slab.h"
32
33depot_stack_handle_t kasan_save_stack(gfp_t flags)
34{
35 unsigned long entries[KASAN_STACK_DEPTH];
36 unsigned int nr_entries;
37
38 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
39 nr_entries = filter_irq_stacks(entries, nr_entries);
40 return stack_depot_save(entries, nr_entries, flags);
41}
42
43void kasan_set_track(struct kasan_track *track, gfp_t flags)
44{
45 track->pid = current->pid;
46 track->stack = kasan_save_stack(flags);
47}
48
49#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
50void kasan_enable_current(void)
51{
52 current->kasan_depth++;
53}
54EXPORT_SYMBOL(kasan_enable_current);
55
56void kasan_disable_current(void)
57{
58 current->kasan_depth--;
59}
60EXPORT_SYMBOL(kasan_disable_current);
61
62#endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
63
64void __kasan_unpoison_range(const void *address, size_t size)
65{
66 kasan_unpoison(address, size, false);
67}
68
69#ifdef CONFIG_KASAN_STACK
70/* Unpoison the entire stack for a task. */
71void kasan_unpoison_task_stack(struct task_struct *task)
72{
73 void *base = task_stack_page(task);
74
75 kasan_unpoison(base, THREAD_SIZE, false);
76}
77
78/* Unpoison the stack for the current task beyond a watermark sp value. */
79asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
80{
81 /*
82 * Calculate the task stack base address. Avoid using 'current'
83 * because this function is called by early resume code which hasn't
84 * yet set up the percpu register (%gs).
85 */
86 void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
87
88 kasan_unpoison(base, watermark - base, false);
89}
90#endif /* CONFIG_KASAN_STACK */
91
92/*
93 * Only allow cache merging when stack collection is disabled and no metadata
94 * is present.
95 */
96slab_flags_t __kasan_never_merge(void)
97{
98 if (kasan_stack_collection_enabled())
99 return SLAB_KASAN;
100 return 0;
101}
102
103void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init)
104{
105 u8 tag;
106 unsigned long i;
107
108 if (unlikely(PageHighMem(page)))
109 return;
110
111 tag = kasan_random_tag();
112 for (i = 0; i < (1 << order); i++)
113 page_kasan_tag_set(page + i, tag);
114 kasan_unpoison(page_address(page), PAGE_SIZE << order, init);
115}
116
117void __kasan_poison_pages(struct page *page, unsigned int order, bool init)
118{
119 if (likely(!PageHighMem(page)))
120 kasan_poison(page_address(page), PAGE_SIZE << order,
121 KASAN_FREE_PAGE, init);
122}
123
124/*
125 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
126 * For larger allocations larger redzones are used.
127 */
128static inline unsigned int optimal_redzone(unsigned int object_size)
129{
130 return
131 object_size <= 64 - 16 ? 16 :
132 object_size <= 128 - 32 ? 32 :
133 object_size <= 512 - 64 ? 64 :
134 object_size <= 4096 - 128 ? 128 :
135 object_size <= (1 << 14) - 256 ? 256 :
136 object_size <= (1 << 15) - 512 ? 512 :
137 object_size <= (1 << 16) - 1024 ? 1024 : 2048;
138}
139
140void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
141 slab_flags_t *flags)
142{
143 unsigned int ok_size;
144 unsigned int optimal_size;
145
146 /*
147 * SLAB_KASAN is used to mark caches as ones that are sanitized by
148 * KASAN. Currently this flag is used in two places:
149 * 1. In slab_ksize() when calculating the size of the accessible
150 * memory within the object.
151 * 2. In slab_common.c to prevent merging of sanitized caches.
152 */
153 *flags |= SLAB_KASAN;
154
155 if (!kasan_stack_collection_enabled())
156 return;
157
158 ok_size = *size;
159
160 /* Add alloc meta into redzone. */
161 cache->kasan_info.alloc_meta_offset = *size;
162 *size += sizeof(struct kasan_alloc_meta);
163
164 /*
165 * If alloc meta doesn't fit, don't add it.
166 * This can only happen with SLAB, as it has KMALLOC_MAX_SIZE equal
167 * to KMALLOC_MAX_CACHE_SIZE and doesn't fall back to page_alloc for
168 * larger sizes.
169 */
170 if (*size > KMALLOC_MAX_SIZE) {
171 cache->kasan_info.alloc_meta_offset = 0;
172 *size = ok_size;
173 /* Continue, since free meta might still fit. */
174 }
175
176 /* Only the generic mode uses free meta or flexible redzones. */
177 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
178 cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META;
179 return;
180 }
181
182 /*
183 * Add free meta into redzone when it's not possible to store
184 * it in the object. This is the case when:
185 * 1. Object is SLAB_TYPESAFE_BY_RCU, which means that it can
186 * be touched after it was freed, or
187 * 2. Object has a constructor, which means it's expected to
188 * retain its content until the next allocation, or
189 * 3. Object is too small.
190 * Otherwise cache->kasan_info.free_meta_offset = 0 is implied.
191 */
192 if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor ||
193 cache->object_size < sizeof(struct kasan_free_meta)) {
194 ok_size = *size;
195
196 cache->kasan_info.free_meta_offset = *size;
197 *size += sizeof(struct kasan_free_meta);
198
199 /* If free meta doesn't fit, don't add it. */
200 if (*size > KMALLOC_MAX_SIZE) {
201 cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META;
202 *size = ok_size;
203 }
204 }
205
206 /* Calculate size with optimal redzone. */
207 optimal_size = cache->object_size + optimal_redzone(cache->object_size);
208 /* Limit it with KMALLOC_MAX_SIZE (relevant for SLAB only). */
209 if (optimal_size > KMALLOC_MAX_SIZE)
210 optimal_size = KMALLOC_MAX_SIZE;
211 /* Use optimal size if the size with added metas is not large enough. */
212 if (*size < optimal_size)
213 *size = optimal_size;
214}
215
216void __kasan_cache_create_kmalloc(struct kmem_cache *cache)
217{
218 cache->kasan_info.is_kmalloc = true;
219}
220
221size_t __kasan_metadata_size(struct kmem_cache *cache)
222{
223 if (!kasan_stack_collection_enabled())
224 return 0;
225 return (cache->kasan_info.alloc_meta_offset ?
226 sizeof(struct kasan_alloc_meta) : 0) +
227 (cache->kasan_info.free_meta_offset ?
228 sizeof(struct kasan_free_meta) : 0);
229}
230
231struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache,
232 const void *object)
233{
234 if (!cache->kasan_info.alloc_meta_offset)
235 return NULL;
236 return kasan_reset_tag(object) + cache->kasan_info.alloc_meta_offset;
237}
238
239#ifdef CONFIG_KASAN_GENERIC
240struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache,
241 const void *object)
242{
243 BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
244 if (cache->kasan_info.free_meta_offset == KASAN_NO_FREE_META)
245 return NULL;
246 return kasan_reset_tag(object) + cache->kasan_info.free_meta_offset;
247}
248#endif
249
250void __kasan_poison_slab(struct page *page)
251{
252 unsigned long i;
253
254 for (i = 0; i < compound_nr(page); i++)
255 page_kasan_tag_reset(page + i);
256 kasan_poison(page_address(page), page_size(page),
257 KASAN_KMALLOC_REDZONE, false);
258}
259
260void __kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
261{
262 kasan_unpoison(object, cache->object_size, false);
263}
264
265void __kasan_poison_object_data(struct kmem_cache *cache, void *object)
266{
267 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE),
268 KASAN_KMALLOC_REDZONE, false);
269}
270
271/*
272 * This function assigns a tag to an object considering the following:
273 * 1. A cache might have a constructor, which might save a pointer to a slab
274 * object somewhere (e.g. in the object itself). We preassign a tag for
275 * each object in caches with constructors during slab creation and reuse
276 * the same tag each time a particular object is allocated.
277 * 2. A cache might be SLAB_TYPESAFE_BY_RCU, which means objects can be
278 * accessed after being freed. We preassign tags for objects in these
279 * caches as well.
280 * 3. For SLAB allocator we can't preassign tags randomly since the freelist
281 * is stored as an array of indexes instead of a linked list. Assign tags
282 * based on objects indexes, so that objects that are next to each other
283 * get different tags.
284 */
285static inline u8 assign_tag(struct kmem_cache *cache,
286 const void *object, bool init)
287{
288 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
289 return 0xff;
290
291 /*
292 * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU
293 * set, assign a tag when the object is being allocated (init == false).
294 */
295 if (!cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU))
296 return init ? KASAN_TAG_KERNEL : kasan_random_tag();
297
298 /* For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU: */
299#ifdef CONFIG_SLAB
300 /* For SLAB assign tags based on the object index in the freelist. */
301 return (u8)obj_to_index(cache, virt_to_page(object), (void *)object);
302#else
303 /*
304 * For SLUB assign a random tag during slab creation, otherwise reuse
305 * the already assigned tag.
306 */
307 return init ? kasan_random_tag() : get_tag(object);
308#endif
309}
310
311void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
312 const void *object)
313{
314 struct kasan_alloc_meta *alloc_meta;
315
316 if (kasan_stack_collection_enabled()) {
317 alloc_meta = kasan_get_alloc_meta(cache, object);
318 if (alloc_meta)
319 __memset(alloc_meta, 0, sizeof(*alloc_meta));
320 }
321
322 /* Tag is ignored in set_tag() without CONFIG_KASAN_SW/HW_TAGS */
323 object = set_tag(object, assign_tag(cache, object, true));
324
325 return (void *)object;
326}
327
328static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object,
329 unsigned long ip, bool quarantine, bool init)
330{
331 u8 tag;
332 void *tagged_object;
333
334 if (!kasan_arch_is_ready())
335 return false;
336
337 tag = get_tag(object);
338 tagged_object = object;
339 object = kasan_reset_tag(object);
340
341 if (is_kfence_address(object))
342 return false;
343
344 if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) !=
345 object)) {
346 kasan_report_invalid_free(tagged_object, ip);
347 return true;
348 }
349
350 /* RCU slabs could be legally used after free within the RCU period */
351 if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
352 return false;
353
354 if (!kasan_byte_accessible(tagged_object)) {
355 kasan_report_invalid_free(tagged_object, ip);
356 return true;
357 }
358
359 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE),
360 KASAN_KMALLOC_FREE, init);
361
362 if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine))
363 return false;
364
365 if (kasan_stack_collection_enabled())
366 kasan_set_free_info(cache, object, tag);
367
368 return kasan_quarantine_put(cache, object);
369}
370
371bool __kasan_slab_free(struct kmem_cache *cache, void *object,
372 unsigned long ip, bool init)
373{
374 return ____kasan_slab_free(cache, object, ip, true, init);
375}
376
377static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip)
378{
379 if (ptr != page_address(virt_to_head_page(ptr))) {
380 kasan_report_invalid_free(ptr, ip);
381 return true;
382 }
383
384 if (!kasan_byte_accessible(ptr)) {
385 kasan_report_invalid_free(ptr, ip);
386 return true;
387 }
388
389 /*
390 * The object will be poisoned by kasan_free_pages() or
391 * kasan_slab_free_mempool().
392 */
393
394 return false;
395}
396
397void __kasan_kfree_large(void *ptr, unsigned long ip)
398{
399 ____kasan_kfree_large(ptr, ip);
400}
401
402void __kasan_slab_free_mempool(void *ptr, unsigned long ip)
403{
404 struct page *page;
405
406 page = virt_to_head_page(ptr);
407
408 /*
409 * Even though this function is only called for kmem_cache_alloc and
410 * kmalloc backed mempool allocations, those allocations can still be
411 * !PageSlab() when the size provided to kmalloc is larger than
412 * KMALLOC_MAX_SIZE, and kmalloc falls back onto page_alloc.
413 */
414 if (unlikely(!PageSlab(page))) {
415 if (____kasan_kfree_large(ptr, ip))
416 return;
417 kasan_poison(ptr, page_size(page), KASAN_FREE_PAGE, false);
418 } else {
419 ____kasan_slab_free(page->slab_cache, ptr, ip, false, false);
420 }
421}
422
423static void set_alloc_info(struct kmem_cache *cache, void *object,
424 gfp_t flags, bool is_kmalloc)
425{
426 struct kasan_alloc_meta *alloc_meta;
427
428 /* Don't save alloc info for kmalloc caches in kasan_slab_alloc(). */
429 if (cache->kasan_info.is_kmalloc && !is_kmalloc)
430 return;
431
432 alloc_meta = kasan_get_alloc_meta(cache, object);
433 if (alloc_meta)
434 kasan_set_track(&alloc_meta->alloc_track, flags);
435}
436
437void * __must_check __kasan_slab_alloc(struct kmem_cache *cache,
438 void *object, gfp_t flags, bool init)
439{
440 u8 tag;
441 void *tagged_object;
442
443 if (gfpflags_allow_blocking(flags))
444 kasan_quarantine_reduce();
445
446 if (unlikely(object == NULL))
447 return NULL;
448
449 if (is_kfence_address(object))
450 return (void *)object;
451
452 /*
453 * Generate and assign random tag for tag-based modes.
454 * Tag is ignored in set_tag() for the generic mode.
455 */
456 tag = assign_tag(cache, object, false);
457 tagged_object = set_tag(object, tag);
458
459 /*
460 * Unpoison the whole object.
461 * For kmalloc() allocations, kasan_kmalloc() will do precise poisoning.
462 */
463 kasan_unpoison(tagged_object, cache->object_size, init);
464
465 /* Save alloc info (if possible) for non-kmalloc() allocations. */
466 if (kasan_stack_collection_enabled())
467 set_alloc_info(cache, (void *)object, flags, false);
468
469 return tagged_object;
470}
471
472static inline void *____kasan_kmalloc(struct kmem_cache *cache,
473 const void *object, size_t size, gfp_t flags)
474{
475 unsigned long redzone_start;
476 unsigned long redzone_end;
477
478 if (gfpflags_allow_blocking(flags))
479 kasan_quarantine_reduce();
480
481 if (unlikely(object == NULL))
482 return NULL;
483
484 if (is_kfence_address(kasan_reset_tag(object)))
485 return (void *)object;
486
487 /*
488 * The object has already been unpoisoned by kasan_slab_alloc() for
489 * kmalloc() or by kasan_krealloc() for krealloc().
490 */
491
492 /*
493 * The redzone has byte-level precision for the generic mode.
494 * Partially poison the last object granule to cover the unaligned
495 * part of the redzone.
496 */
497 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
498 kasan_poison_last_granule((void *)object, size);
499
500 /* Poison the aligned part of the redzone. */
501 redzone_start = round_up((unsigned long)(object + size),
502 KASAN_GRANULE_SIZE);
503 redzone_end = round_up((unsigned long)(object + cache->object_size),
504 KASAN_GRANULE_SIZE);
505 kasan_poison((void *)redzone_start, redzone_end - redzone_start,
506 KASAN_KMALLOC_REDZONE, false);
507
508 /*
509 * Save alloc info (if possible) for kmalloc() allocations.
510 * This also rewrites the alloc info when called from kasan_krealloc().
511 */
512 if (kasan_stack_collection_enabled())
513 set_alloc_info(cache, (void *)object, flags, true);
514
515 /* Keep the tag that was set by kasan_slab_alloc(). */
516 return (void *)object;
517}
518
519void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object,
520 size_t size, gfp_t flags)
521{
522 return ____kasan_kmalloc(cache, object, size, flags);
523}
524EXPORT_SYMBOL(__kasan_kmalloc);
525
526void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size,
527 gfp_t flags)
528{
529 unsigned long redzone_start;
530 unsigned long redzone_end;
531
532 if (gfpflags_allow_blocking(flags))
533 kasan_quarantine_reduce();
534
535 if (unlikely(ptr == NULL))
536 return NULL;
537
538 /*
539 * The object has already been unpoisoned by kasan_alloc_pages() for
540 * alloc_pages() or by kasan_krealloc() for krealloc().
541 */
542
543 /*
544 * The redzone has byte-level precision for the generic mode.
545 * Partially poison the last object granule to cover the unaligned
546 * part of the redzone.
547 */
548 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
549 kasan_poison_last_granule(ptr, size);
550
551 /* Poison the aligned part of the redzone. */
552 redzone_start = round_up((unsigned long)(ptr + size),
553 KASAN_GRANULE_SIZE);
554 redzone_end = (unsigned long)ptr + page_size(virt_to_page(ptr));
555 kasan_poison((void *)redzone_start, redzone_end - redzone_start,
556 KASAN_PAGE_REDZONE, false);
557
558 return (void *)ptr;
559}
560
561void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flags)
562{
563 struct page *page;
564
565 if (unlikely(object == ZERO_SIZE_PTR))
566 return (void *)object;
567
568 /*
569 * Unpoison the object's data.
570 * Part of it might already have been unpoisoned, but it's unknown
571 * how big that part is.
572 */
573 kasan_unpoison(object, size, false);
574
575 page = virt_to_head_page(object);
576
577 /* Piggy-back on kmalloc() instrumentation to poison the redzone. */
578 if (unlikely(!PageSlab(page)))
579 return __kasan_kmalloc_large(object, size, flags);
580 else
581 return ____kasan_kmalloc(page->slab_cache, object, size, flags);
582}
583
584bool __kasan_check_byte(const void *address, unsigned long ip)
585{
586 if (!kasan_byte_accessible(address)) {
587 kasan_report((unsigned long)address, 1, false, ip);
588 return false;
589 }
590 return true;
591}