blob: b3d8eb154f59fe56dacb896b8471f3d5dc0f8886 [file] [log] [blame]
Andrey Konovalove886bf92018-12-28 00:31:14 -08001// SPDX-License-Identifier: GPL-2.0
Andrey Konovalovbffa9862018-12-28 00:29:45 -08002/*
Andrey Konovalovbb359db2020-12-22 12:00:32 -08003 * This file contains common KASAN code.
Andrey Konovalovbffa9862018-12-28 00:29:45 -08004 *
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>
Andrey Konovalovbffa9862018-12-28 00:29:45 -080010 */
11
12#include <linux/export.h>
Andrey Konovalovbffa9862018-12-28 00:29:45 -080013#include <linux/init.h>
14#include <linux/kasan.h>
15#include <linux/kernel.h>
Andrey Konovalovbffa9862018-12-28 00:29:45 -080016#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>
Andrey Konovalovbffa9862018-12-28 00:29:45 -080028#include <linux/bug.h>
29
30#include "kasan.h"
31#include "../slab.h"
32
Walter Wu26e760c2020-08-06 23:24:35 -070033depot_stack_handle_t kasan_save_stack(gfp_t flags)
Andrey Konovalovbffa9862018-12-28 00:29:45 -080034{
35 unsigned long entries[KASAN_STACK_DEPTH];
Thomas Gleixner880e0492019-04-25 11:45:02 +020036 unsigned int nr_entries;
Andrey Konovalovbffa9862018-12-28 00:29:45 -080037
Thomas Gleixner880e0492019-04-25 11:45:02 +020038 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);
Andrey Konovalovbffa9862018-12-28 00:29:45 -080041}
42
Walter Wue4b78182020-08-06 23:24:39 -070043void kasan_set_track(struct kasan_track *track, gfp_t flags)
Andrey Konovalovbffa9862018-12-28 00:29:45 -080044{
45 track->pid = current->pid;
Walter Wu26e760c2020-08-06 23:24:35 -070046 track->stack = kasan_save_stack(flags);
Andrey Konovalovbffa9862018-12-28 00:29:45 -080047}
48
Andrey Konovalovd73b4932020-12-22 12:00:56 -080049#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
Andrey Konovalovbffa9862018-12-28 00:29:45 -080050void kasan_enable_current(void)
51{
52 current->kasan_depth++;
53}
54
55void kasan_disable_current(void)
56{
57 current->kasan_depth--;
58}
Andrey Konovalovd73b4932020-12-22 12:00:56 -080059#endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
Andrey Konovalovbffa9862018-12-28 00:29:45 -080060
Andrey Konovalov34303242020-12-22 12:03:10 -080061void __kasan_unpoison_range(const void *address, size_t size)
Andrey Konovalovcebd0eb2020-12-22 12:00:21 -080062{
Andrey Konovalovaa5c2192021-04-29 22:59:59 -070063 kasan_unpoison(address, size, false);
Andrey Konovalovcebd0eb2020-12-22 12:00:21 -080064}
65
Walter Wu02c58772021-04-16 15:46:00 -070066#ifdef CONFIG_KASAN_STACK
Andrey Konovalovbffa9862018-12-28 00:29:45 -080067/* Unpoison the entire stack for a task. */
68void kasan_unpoison_task_stack(struct task_struct *task)
69{
Andrey Konovalov77f57c92020-12-22 12:02:49 -080070 void *base = task_stack_page(task);
71
Andrey Konovalovaa5c2192021-04-29 22:59:59 -070072 kasan_unpoison(base, THREAD_SIZE, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -080073}
74
75/* Unpoison the stack for the current task beyond a watermark sp value. */
76asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
77{
78 /*
79 * Calculate the task stack base address. Avoid using 'current'
80 * because this function is called by early resume code which hasn't
81 * yet set up the percpu register (%gs).
82 */
83 void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
84
Andrey Konovalovaa5c2192021-04-29 22:59:59 -070085 kasan_unpoison(base, watermark - base, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -080086}
Andrey Konovalovd56a9ef2020-12-22 12:02:42 -080087#endif /* CONFIG_KASAN_STACK */
Andrey Konovalovbffa9862018-12-28 00:29:45 -080088
Andrey Konovalove86f8b092020-12-22 12:03:31 -080089/*
90 * Only allow cache merging when stack collection is disabled and no metadata
91 * is present.
92 */
93slab_flags_t __kasan_never_merge(void)
94{
95 if (kasan_stack_collection_enabled())
96 return SLAB_KASAN;
97 return 0;
98}
99
Andrey Konovalov34303242020-12-22 12:03:10 -0800100void __kasan_alloc_pages(struct page *page, unsigned int order)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800101{
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800102 u8 tag;
103 unsigned long i;
104
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800105 if (unlikely(PageHighMem(page)))
106 return;
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800107
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800108 tag = kasan_random_tag();
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800109 for (i = 0; i < (1 << order); i++)
110 page_kasan_tag_set(page + i, tag);
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700111 kasan_unpoison(page_address(page), PAGE_SIZE << order, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800112}
113
Andrey Konovalov34303242020-12-22 12:03:10 -0800114void __kasan_free_pages(struct page *page, unsigned int order)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800115{
116 if (likely(!PageHighMem(page)))
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800117 kasan_poison(page_address(page), PAGE_SIZE << order,
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700118 KASAN_FREE_PAGE, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800119}
120
121/*
122 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
123 * For larger allocations larger redzones are used.
124 */
125static inline unsigned int optimal_redzone(unsigned int object_size)
126{
127 return
128 object_size <= 64 - 16 ? 16 :
129 object_size <= 128 - 32 ? 32 :
130 object_size <= 512 - 64 ? 64 :
131 object_size <= 4096 - 128 ? 128 :
132 object_size <= (1 << 14) - 256 ? 256 :
133 object_size <= (1 << 15) - 512 ? 512 :
134 object_size <= (1 << 16) - 1024 ? 1024 : 2048;
135}
136
Andrey Konovalov34303242020-12-22 12:03:10 -0800137void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
138 slab_flags_t *flags)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800139{
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800140 unsigned int ok_size;
141 unsigned int optimal_size;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800142
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800143 /*
144 * SLAB_KASAN is used to mark caches as ones that are sanitized by
145 * KASAN. Currently this flag is used in two places:
146 * 1. In slab_ksize() when calculating the size of the accessible
147 * memory within the object.
148 * 2. In slab_common.c to prevent merging of sanitized caches.
149 */
150 *flags |= SLAB_KASAN;
151
152 if (!kasan_stack_collection_enabled())
Andrey Konovalov8028caa2020-12-22 12:03:06 -0800153 return;
Andrey Konovalov8028caa2020-12-22 12:03:06 -0800154
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800155 ok_size = *size;
156
157 /* Add alloc meta into redzone. */
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800158 cache->kasan_info.alloc_meta_offset = *size;
159 *size += sizeof(struct kasan_alloc_meta);
160
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800161 /*
162 * If alloc meta doesn't fit, don't add it.
163 * This can only happen with SLAB, as it has KMALLOC_MAX_SIZE equal
164 * to KMALLOC_MAX_CACHE_SIZE and doesn't fall back to page_alloc for
165 * larger sizes.
166 */
167 if (*size > KMALLOC_MAX_SIZE) {
168 cache->kasan_info.alloc_meta_offset = 0;
169 *size = ok_size;
170 /* Continue, since free meta might still fit. */
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800171 }
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800172
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800173 /* Only the generic mode uses free meta or flexible redzones. */
174 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
175 cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800176 return;
177 }
178
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800179 /*
180 * Add free meta into redzone when it's not possible to store
181 * it in the object. This is the case when:
182 * 1. Object is SLAB_TYPESAFE_BY_RCU, which means that it can
183 * be touched after it was freed, or
184 * 2. Object has a constructor, which means it's expected to
185 * retain its content until the next allocation, or
186 * 3. Object is too small.
187 * Otherwise cache->kasan_info.free_meta_offset = 0 is implied.
188 */
189 if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor ||
190 cache->object_size < sizeof(struct kasan_free_meta)) {
191 ok_size = *size;
192
193 cache->kasan_info.free_meta_offset = *size;
194 *size += sizeof(struct kasan_free_meta);
195
196 /* If free meta doesn't fit, don't add it. */
197 if (*size > KMALLOC_MAX_SIZE) {
198 cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META;
199 *size = ok_size;
200 }
201 }
202
203 /* Calculate size with optimal redzone. */
204 optimal_size = cache->object_size + optimal_redzone(cache->object_size);
205 /* Limit it with KMALLOC_MAX_SIZE (relevant for SLAB only). */
206 if (optimal_size > KMALLOC_MAX_SIZE)
207 optimal_size = KMALLOC_MAX_SIZE;
208 /* Use optimal size if the size with added metas is not large enough. */
209 if (*size < optimal_size)
210 *size = optimal_size;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800211}
212
Andrey Konovalov92850132021-02-25 17:19:55 -0800213void __kasan_cache_create_kmalloc(struct kmem_cache *cache)
214{
215 cache->kasan_info.is_kmalloc = true;
216}
217
Andrey Konovalov34303242020-12-22 12:03:10 -0800218size_t __kasan_metadata_size(struct kmem_cache *cache)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800219{
Andrey Konovalov8028caa2020-12-22 12:03:06 -0800220 if (!kasan_stack_collection_enabled())
221 return 0;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800222 return (cache->kasan_info.alloc_meta_offset ?
223 sizeof(struct kasan_alloc_meta) : 0) +
224 (cache->kasan_info.free_meta_offset ?
225 sizeof(struct kasan_free_meta) : 0);
226}
227
Andrey Konovalov64767922020-12-22 12:02:34 -0800228struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache,
229 const void *object)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800230{
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800231 if (!cache->kasan_info.alloc_meta_offset)
232 return NULL;
Andrey Konovalovc0054c52020-12-22 12:02:52 -0800233 return kasan_reset_tag(object) + cache->kasan_info.alloc_meta_offset;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800234}
235
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800236#ifdef CONFIG_KASAN_GENERIC
Andrey Konovalov64767922020-12-22 12:02:34 -0800237struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache,
238 const void *object)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800239{
240 BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800241 if (cache->kasan_info.free_meta_offset == KASAN_NO_FREE_META)
242 return NULL;
Andrey Konovalovc0054c52020-12-22 12:02:52 -0800243 return kasan_reset_tag(object) + cache->kasan_info.free_meta_offset;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800244}
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800245#endif
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800246
Andrey Konovalov34303242020-12-22 12:03:10 -0800247void __kasan_poison_slab(struct page *page)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800248{
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800249 unsigned long i;
250
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700251 for (i = 0; i < compound_nr(page); i++)
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800252 page_kasan_tag_reset(page + i);
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800253 kasan_poison(page_address(page), page_size(page),
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700254 KASAN_KMALLOC_REDZONE, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800255}
256
Andrey Konovalov34303242020-12-22 12:03:10 -0800257void __kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800258{
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700259 kasan_unpoison(object, cache->object_size, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800260}
261
Andrey Konovalov34303242020-12-22 12:03:10 -0800262void __kasan_poison_object_data(struct kmem_cache *cache, void *object)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800263{
Andrey Konovalovcde8a7e2021-02-25 17:20:27 -0800264 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE),
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700265 KASAN_KMALLOC_REDZONE, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800266}
267
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800268/*
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800269 * This function assigns a tag to an object considering the following:
270 * 1. A cache might have a constructor, which might save a pointer to a slab
271 * object somewhere (e.g. in the object itself). We preassign a tag for
272 * each object in caches with constructors during slab creation and reuse
273 * the same tag each time a particular object is allocated.
274 * 2. A cache might be SLAB_TYPESAFE_BY_RCU, which means objects can be
275 * accessed after being freed. We preassign tags for objects in these
276 * caches as well.
277 * 3. For SLAB allocator we can't preassign tags randomly since the freelist
278 * is stored as an array of indexes instead of a linked list. Assign tags
279 * based on objects indexes, so that objects that are next to each other
280 * get different tags.
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800281 */
Andrey Konovalovc80a0362021-02-25 17:20:35 -0800282static inline u8 assign_tag(struct kmem_cache *cache,
283 const void *object, bool init)
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800284{
Andrey Konovalov1ef31332020-12-22 12:03:20 -0800285 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
286 return 0xff;
287
Andrey Konovalove1db95b2019-02-20 22:19:01 -0800288 /*
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800289 * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU
290 * set, assign a tag when the object is being allocated (init == false).
291 */
292 if (!cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU))
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800293 return init ? KASAN_TAG_KERNEL : kasan_random_tag();
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800294
295 /* For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU: */
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800296#ifdef CONFIG_SLAB
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800297 /* For SLAB assign tags based on the object index in the freelist. */
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800298 return (u8)obj_to_index(cache, virt_to_page(object), (void *)object);
299#else
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800300 /*
301 * For SLUB assign a random tag during slab creation, otherwise reuse
302 * the already assigned tag.
303 */
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800304 return init ? kasan_random_tag() : get_tag(object);
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800305#endif
306}
307
Andrey Konovalov34303242020-12-22 12:03:10 -0800308void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
Andrey Konovalov66afc7f2018-12-28 00:31:01 -0800309 const void *object)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800310{
Andrey Konovalov64767922020-12-22 12:02:34 -0800311 struct kasan_alloc_meta *alloc_meta;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800312
Andrey Konovalov8028caa2020-12-22 12:03:06 -0800313 if (kasan_stack_collection_enabled()) {
Andrey Konovalov8028caa2020-12-22 12:03:06 -0800314 alloc_meta = kasan_get_alloc_meta(cache, object);
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800315 if (alloc_meta)
316 __memset(alloc_meta, 0, sizeof(*alloc_meta));
Andrey Konovalov8028caa2020-12-22 12:03:06 -0800317 }
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800318
Andrey Konovalov1ef31332020-12-22 12:03:20 -0800319 /* Tag is ignored in set_tag() without CONFIG_KASAN_SW/HW_TAGS */
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800320 object = set_tag(object, assign_tag(cache, object, true));
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800321
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800322 return (void *)object;
323}
324
Andrey Konovalovc80a0362021-02-25 17:20:35 -0800325static inline bool ____kasan_slab_free(struct kmem_cache *cache,
326 void *object, unsigned long ip, bool quarantine)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800327{
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800328 u8 tag;
329 void *tagged_object;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800330
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800331 tag = get_tag(object);
332 tagged_object = object;
Andrey Konovalovc0054c52020-12-22 12:02:52 -0800333 object = kasan_reset_tag(object);
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800334
Alexander Potapenko2b830522021-02-25 17:19:21 -0800335 if (is_kfence_address(object))
336 return false;
337
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800338 if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) !=
339 object)) {
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800340 kasan_report_invalid_free(tagged_object, ip);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800341 return true;
342 }
343
344 /* RCU slabs could be legally used after free within the RCU period */
345 if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
346 return false;
347
Andrey Konovalov611806b2021-02-24 12:05:50 -0800348 if (!kasan_byte_accessible(tagged_object)) {
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800349 kasan_report_invalid_free(tagged_object, ip);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800350 return true;
351 }
352
Andrey Konovalovcde8a7e2021-02-25 17:20:27 -0800353 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE),
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700354 KASAN_KMALLOC_FREE, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800355
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800356 if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine))
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800357 return false;
358
Andrey Konovalovdf54b382021-02-25 17:20:07 -0800359 if (kasan_stack_collection_enabled())
360 kasan_set_free_info(cache, object, tag);
Walter Wuae8f06b2019-09-23 15:34:13 -0700361
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800362 return kasan_quarantine_put(cache, object);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800363}
364
Andrey Konovalov34303242020-12-22 12:03:10 -0800365bool __kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800366{
Andrey Konovalov34303242020-12-22 12:03:10 -0800367 return ____kasan_slab_free(cache, object, ip, true);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800368}
369
Andrey Konovalovc80a0362021-02-25 17:20:35 -0800370static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip)
Andrey Konovalov200072c2021-02-25 17:20:11 -0800371{
372 if (ptr != page_address(virt_to_head_page(ptr))) {
373 kasan_report_invalid_free(ptr, ip);
374 return true;
375 }
376
377 if (!kasan_byte_accessible(ptr)) {
378 kasan_report_invalid_free(ptr, ip);
379 return true;
380 }
381
382 /*
383 * The object will be poisoned by kasan_free_pages() or
384 * kasan_slab_free_mempool().
385 */
386
387 return false;
388}
389
390void __kasan_kfree_large(void *ptr, unsigned long ip)
391{
392 ____kasan_kfree_large(ptr, ip);
393}
394
Andrey Konovaloveeb31602020-12-22 12:03:13 -0800395void __kasan_slab_free_mempool(void *ptr, unsigned long ip)
396{
397 struct page *page;
398
399 page = virt_to_head_page(ptr);
400
401 /*
402 * Even though this function is only called for kmem_cache_alloc and
403 * kmalloc backed mempool allocations, those allocations can still be
404 * !PageSlab() when the size provided to kmalloc is larger than
405 * KMALLOC_MAX_SIZE, and kmalloc falls back onto page_alloc.
406 */
407 if (unlikely(!PageSlab(page))) {
Andrey Konovalov200072c2021-02-25 17:20:11 -0800408 if (____kasan_kfree_large(ptr, ip))
Andrey Konovaloveeb31602020-12-22 12:03:13 -0800409 return;
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700410 kasan_poison(ptr, page_size(page), KASAN_FREE_PAGE, false);
Andrey Konovaloveeb31602020-12-22 12:03:13 -0800411 } else {
412 ____kasan_slab_free(page->slab_cache, ptr, ip, false);
413 }
414}
415
Andrey Konovalov92850132021-02-25 17:19:55 -0800416static void set_alloc_info(struct kmem_cache *cache, void *object,
417 gfp_t flags, bool is_kmalloc)
Andrey Konovalov8bb00092020-12-22 12:02:38 -0800418{
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800419 struct kasan_alloc_meta *alloc_meta;
420
Andrey Konovalov92850132021-02-25 17:19:55 -0800421 /* Don't save alloc info for kmalloc caches in kasan_slab_alloc(). */
422 if (cache->kasan_info.is_kmalloc && !is_kmalloc)
423 return;
424
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800425 alloc_meta = kasan_get_alloc_meta(cache, object);
426 if (alloc_meta)
427 kasan_set_track(&alloc_meta->alloc_track, flags);
Andrey Konovalov8bb00092020-12-22 12:02:38 -0800428}
429
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800430void * __must_check __kasan_slab_alloc(struct kmem_cache *cache,
431 void *object, gfp_t flags)
432{
433 u8 tag;
434 void *tagged_object;
435
436 if (gfpflags_allow_blocking(flags))
437 kasan_quarantine_reduce();
438
439 if (unlikely(object == NULL))
440 return NULL;
441
442 if (is_kfence_address(object))
443 return (void *)object;
444
445 /*
446 * Generate and assign random tag for tag-based modes.
447 * Tag is ignored in set_tag() for the generic mode.
448 */
449 tag = assign_tag(cache, object, false);
450 tagged_object = set_tag(object, tag);
451
452 /*
453 * Unpoison the whole object.
454 * For kmalloc() allocations, kasan_kmalloc() will do precise poisoning.
455 */
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700456 kasan_unpoison(tagged_object, cache->object_size, false);
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800457
458 /* Save alloc info (if possible) for non-kmalloc() allocations. */
459 if (kasan_stack_collection_enabled())
460 set_alloc_info(cache, (void *)object, flags, false);
461
462 return tagged_object;
463}
464
Andrey Konovalovc80a0362021-02-25 17:20:35 -0800465static inline void *____kasan_kmalloc(struct kmem_cache *cache,
466 const void *object, size_t size, gfp_t flags)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800467{
468 unsigned long redzone_start;
469 unsigned long redzone_end;
470
471 if (gfpflags_allow_blocking(flags))
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800472 kasan_quarantine_reduce();
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800473
474 if (unlikely(object == NULL))
475 return NULL;
476
Alexander Potapenko2b830522021-02-25 17:19:21 -0800477 if (is_kfence_address(kasan_reset_tag(object)))
478 return (void *)object;
479
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800480 /*
481 * The object has already been unpoisoned by kasan_slab_alloc() for
Andrey Konovalovd12d9ad2021-02-25 17:20:23 -0800482 * kmalloc() or by kasan_krealloc() for krealloc().
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800483 */
484
485 /*
486 * The redzone has byte-level precision for the generic mode.
487 * Partially poison the last object granule to cover the unaligned
488 * part of the redzone.
489 */
490 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
491 kasan_poison_last_granule((void *)object, size);
492
493 /* Poison the aligned part of the redzone. */
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800494 redzone_start = round_up((unsigned long)(object + size),
Andrey Konovalov1f600622020-12-22 12:00:24 -0800495 KASAN_GRANULE_SIZE);
Andrey Konovalovcde8a7e2021-02-25 17:20:27 -0800496 redzone_end = round_up((unsigned long)(object + cache->object_size),
497 KASAN_GRANULE_SIZE);
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800498 kasan_poison((void *)redzone_start, redzone_end - redzone_start,
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700499 KASAN_KMALLOC_REDZONE, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800500
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800501 /*
502 * Save alloc info (if possible) for kmalloc() allocations.
503 * This also rewrites the alloc info when called from kasan_krealloc().
504 */
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800505 if (kasan_stack_collection_enabled())
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800506 set_alloc_info(cache, (void *)object, flags, true);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800507
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800508 /* Keep the tag that was set by kasan_slab_alloc(). */
509 return (void *)object;
Andrey Konovalove1db95b2019-02-20 22:19:01 -0800510}
511
Andrey Konovalov34303242020-12-22 12:03:10 -0800512void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object,
513 size_t size, gfp_t flags)
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800514{
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800515 return ____kasan_kmalloc(cache, object, size, flags);
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800516}
Andrey Konovalov34303242020-12-22 12:03:10 -0800517EXPORT_SYMBOL(__kasan_kmalloc);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800518
Andrey Konovalov34303242020-12-22 12:03:10 -0800519void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size,
Andrey Konovalov66afc7f2018-12-28 00:31:01 -0800520 gfp_t flags)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800521{
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800522 unsigned long redzone_start;
523 unsigned long redzone_end;
524
525 if (gfpflags_allow_blocking(flags))
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800526 kasan_quarantine_reduce();
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800527
528 if (unlikely(ptr == NULL))
529 return NULL;
530
Andrey Konovalov43a219c2021-02-25 17:20:03 -0800531 /*
532 * The object has already been unpoisoned by kasan_alloc_pages() for
Andrey Konovalovd12d9ad2021-02-25 17:20:23 -0800533 * alloc_pages() or by kasan_krealloc() for krealloc().
Andrey Konovalov43a219c2021-02-25 17:20:03 -0800534 */
535
536 /*
537 * The redzone has byte-level precision for the generic mode.
538 * Partially poison the last object granule to cover the unaligned
539 * part of the redzone.
540 */
541 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
542 kasan_poison_last_granule(ptr, size);
543
544 /* Poison the aligned part of the redzone. */
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800545 redzone_start = round_up((unsigned long)(ptr + size),
Andrey Konovalov1f600622020-12-22 12:00:24 -0800546 KASAN_GRANULE_SIZE);
Andrey Konovalov43a219c2021-02-25 17:20:03 -0800547 redzone_end = (unsigned long)ptr + page_size(virt_to_page(ptr));
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800548 kasan_poison((void *)redzone_start, redzone_end - redzone_start,
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700549 KASAN_PAGE_REDZONE, false);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800550
551 return (void *)ptr;
552}
553
Andrey Konovalov34303242020-12-22 12:03:10 -0800554void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flags)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800555{
556 struct page *page;
557
558 if (unlikely(object == ZERO_SIZE_PTR))
559 return (void *)object;
560
Andrey Konovalovd12d9ad2021-02-25 17:20:23 -0800561 /*
562 * Unpoison the object's data.
563 * Part of it might already have been unpoisoned, but it's unknown
564 * how big that part is.
565 */
Andrey Konovalovaa5c2192021-04-29 22:59:59 -0700566 kasan_unpoison(object, size, false);
Andrey Konovalovd12d9ad2021-02-25 17:20:23 -0800567
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800568 page = virt_to_head_page(object);
569
Andrey Konovalovd12d9ad2021-02-25 17:20:23 -0800570 /* Piggy-back on kmalloc() instrumentation to poison the redzone. */
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800571 if (unlikely(!PageSlab(page)))
Andrey Konovalov34303242020-12-22 12:03:10 -0800572 return __kasan_kmalloc_large(object, size, flags);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800573 else
Andrey Konovalove2db1a92021-02-25 17:19:59 -0800574 return ____kasan_kmalloc(page->slab_cache, object, size, flags);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800575}
576
Andrey Konovalov611806b2021-02-24 12:05:50 -0800577bool __kasan_check_byte(const void *address, unsigned long ip)
578{
579 if (!kasan_byte_accessible(address)) {
580 kasan_report((unsigned long)address, 1, false, ip);
581 return false;
582 }
583 return true;
584}