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/clock.h>
24 #include <linux/sched/task_stack.h>
25 #include <linux/slab.h>
26 #include <linux/stackdepot.h>
27 #include <linux/stacktrace.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/bug.h>
31
32 #include "kasan.h"
33 #include "../slab.h"
34
35 #if defined(CONFIG_ARCH_DEFER_KASAN) || defined(CONFIG_KASAN_HW_TAGS)
36 /*
37 * Definition of the unified static key declared in kasan-enabled.h.
38 * This provides consistent runtime enable/disable across KASAN modes.
39 */
40 DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled);
41 EXPORT_SYMBOL_GPL(kasan_flag_enabled);
42 #endif
43
kasan_addr_to_slab(const void * addr)44 struct slab *kasan_addr_to_slab(const void *addr)
45 {
46 if (virt_addr_valid(addr))
47 return virt_to_slab(addr);
48 return NULL;
49 }
50
kasan_save_stack(gfp_t flags,depot_flags_t depot_flags)51 depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags)
52 {
53 unsigned long entries[KASAN_STACK_DEPTH];
54 unsigned int nr_entries;
55
56 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
57 return stack_depot_save_flags(entries, nr_entries, flags, depot_flags);
58 }
59
kasan_set_track(struct kasan_track * track,depot_stack_handle_t stack)60 void kasan_set_track(struct kasan_track *track, depot_stack_handle_t stack)
61 {
62 #ifdef CONFIG_KASAN_EXTRA_INFO
63 u32 cpu = raw_smp_processor_id();
64 u64 ts_nsec = local_clock();
65
66 track->cpu = cpu;
67 track->timestamp = ts_nsec >> 9;
68 #endif /* CONFIG_KASAN_EXTRA_INFO */
69 track->pid = current->pid;
70 track->stack = stack;
71 }
72
kasan_save_track(struct kasan_track * track,gfp_t flags)73 void kasan_save_track(struct kasan_track *track, gfp_t flags)
74 {
75 depot_stack_handle_t stack;
76
77 stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC);
78 kasan_set_track(track, stack);
79 }
80
81 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
kasan_enable_current(void)82 void kasan_enable_current(void)
83 {
84 current->kasan_depth++;
85 }
86 EXPORT_SYMBOL(kasan_enable_current);
87
kasan_disable_current(void)88 void kasan_disable_current(void)
89 {
90 current->kasan_depth--;
91 }
92 EXPORT_SYMBOL(kasan_disable_current);
93
94 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
95
__kasan_unpoison_range(const void * address,size_t size)96 void __kasan_unpoison_range(const void *address, size_t size)
97 {
98 if (is_kfence_address(address))
99 return;
100
101 kasan_unpoison(address, size, false);
102 }
103
104 #ifdef CONFIG_KASAN_STACK
105 /* Unpoison the entire stack for a task. */
kasan_unpoison_task_stack(struct task_struct * task)106 void kasan_unpoison_task_stack(struct task_struct *task)
107 {
108 void *base = task_stack_page(task);
109
110 kasan_unpoison(base, THREAD_SIZE, false);
111 }
112
113 /* Unpoison the stack for the current task beyond a watermark sp value. */
kasan_unpoison_task_stack_below(const void * watermark)114 asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
115 {
116 /*
117 * Calculate the task stack base address. Avoid using 'current'
118 * because this function is called by early resume code which hasn't
119 * yet set up the percpu register (%gs).
120 */
121 void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
122
123 kasan_unpoison(base, watermark - base, false);
124 }
125 #endif /* CONFIG_KASAN_STACK */
126
__kasan_unpoison_pages(struct page * page,unsigned int order,bool init)127 bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init)
128 {
129 u8 tag;
130 unsigned long i;
131
132 if (unlikely(PageHighMem(page)))
133 return false;
134
135 if (!kasan_sample_page_alloc(order))
136 return false;
137
138 tag = kasan_random_tag();
139 kasan_unpoison(set_tag(page_address(page), tag),
140 PAGE_SIZE << order, init);
141 for (i = 0; i < (1 << order); i++)
142 page_kasan_tag_set(page + i, tag);
143
144 return true;
145 }
146
__kasan_poison_pages(struct page * page,unsigned int order,bool init)147 void __kasan_poison_pages(struct page *page, unsigned int order, bool init)
148 {
149 if (likely(!PageHighMem(page)))
150 kasan_poison(page_address(page), PAGE_SIZE << order,
151 KASAN_PAGE_FREE, init);
152 }
153
__kasan_poison_slab(struct slab * slab)154 void __kasan_poison_slab(struct slab *slab)
155 {
156 struct page *page = slab_page(slab);
157 unsigned long i;
158
159 for (i = 0; i < compound_nr(page); i++)
160 page_kasan_tag_reset(page + i);
161 kasan_poison(page_address(page), page_size(page),
162 KASAN_SLAB_REDZONE, false);
163 }
164
__kasan_unpoison_new_object(struct kmem_cache * cache,void * object)165 void __kasan_unpoison_new_object(struct kmem_cache *cache, void *object)
166 {
167 kasan_unpoison(object, cache->object_size, false);
168 }
169
__kasan_poison_new_object(struct kmem_cache * cache,void * object)170 void __kasan_poison_new_object(struct kmem_cache *cache, void *object)
171 {
172 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE),
173 KASAN_SLAB_REDZONE, false);
174 }
175
176 /*
177 * This function assigns a tag to an object considering the following:
178 * 1. A cache might have a constructor, which might save a pointer to a slab
179 * object somewhere (e.g. in the object itself). We preassign a tag for
180 * each object in caches with constructors during slab creation and reuse
181 * the same tag each time a particular object is allocated.
182 * 2. A cache might be SLAB_TYPESAFE_BY_RCU, which means objects can be
183 * accessed after being freed. We preassign tags for objects in these
184 * caches as well.
185 */
assign_tag(struct kmem_cache * cache,const void * object,bool init)186 static inline u8 assign_tag(struct kmem_cache *cache,
187 const void *object, bool init)
188 {
189 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
190 return 0xff;
191
192 /*
193 * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU
194 * set, assign a tag when the object is being allocated (init == false).
195 */
196 if (!cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU))
197 return init ? KASAN_TAG_KERNEL : kasan_random_tag();
198
199 /*
200 * For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU,
201 * assign a random tag during slab creation, otherwise reuse
202 * the already assigned tag.
203 */
204 return init ? kasan_random_tag() : get_tag(object);
205 }
206
__kasan_init_slab_obj(struct kmem_cache * cache,const void * object)207 void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
208 const void *object)
209 {
210 /* Initialize per-object metadata if it is present. */
211 if (kasan_requires_meta())
212 kasan_init_object_meta(cache, object);
213
214 /* Tag is ignored in set_tag() without CONFIG_KASAN_SW/HW_TAGS */
215 object = set_tag(object, assign_tag(cache, object, true));
216
217 return (void *)object;
218 }
219
220 /* Returns true when freeing the object is not safe. */
check_slab_allocation(struct kmem_cache * cache,void * object,unsigned long ip)221 static bool check_slab_allocation(struct kmem_cache *cache, void *object,
222 unsigned long ip)
223 {
224 void *tagged_object = object;
225
226 object = kasan_reset_tag(object);
227
228 if (unlikely(nearest_obj(cache, virt_to_slab(object), object) != object)) {
229 kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_INVALID_FREE);
230 return true;
231 }
232
233 if (!kasan_byte_accessible(tagged_object)) {
234 kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_DOUBLE_FREE);
235 return true;
236 }
237
238 return false;
239 }
240
poison_slab_object(struct kmem_cache * cache,void * object,bool init)241 static inline void poison_slab_object(struct kmem_cache *cache, void *object,
242 bool init)
243 {
244 void *tagged_object = object;
245
246 object = kasan_reset_tag(object);
247
248 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE),
249 KASAN_SLAB_FREE, init);
250
251 if (kasan_stack_collection_enabled())
252 kasan_save_free_info(cache, tagged_object);
253 }
254
__kasan_slab_pre_free(struct kmem_cache * cache,void * object,unsigned long ip)255 bool __kasan_slab_pre_free(struct kmem_cache *cache, void *object,
256 unsigned long ip)
257 {
258 if (is_kfence_address(object))
259 return false;
260 return check_slab_allocation(cache, object, ip);
261 }
262
__kasan_slab_free(struct kmem_cache * cache,void * object,bool init,bool still_accessible,bool no_quarantine)263 bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init,
264 bool still_accessible, bool no_quarantine)
265 {
266 if (is_kfence_address(object))
267 return false;
268
269 /*
270 * If this point is reached with an object that must still be
271 * accessible under RCU, we can't poison it; in that case, also skip the
272 * quarantine. This should mostly only happen when CONFIG_SLUB_RCU_DEBUG
273 * has been disabled manually.
274 *
275 * Putting the object on the quarantine wouldn't help catch UAFs (since
276 * we can't poison it here), and it would mask bugs caused by
277 * SLAB_TYPESAFE_BY_RCU users not being careful enough about object
278 * reuse; so overall, putting the object into the quarantine here would
279 * be counterproductive.
280 */
281 if (still_accessible)
282 return false;
283
284 poison_slab_object(cache, object, init);
285
286 if (no_quarantine)
287 return false;
288
289 /*
290 * If the object is put into quarantine, do not let slab put the object
291 * onto the freelist for now. The object's metadata is kept until the
292 * object gets evicted from quarantine.
293 */
294 if (kasan_quarantine_put(cache, object))
295 return true;
296
297 /*
298 * Note: Keep per-object metadata to allow KASAN print stack traces for
299 * use-after-free-before-realloc bugs.
300 */
301
302 /* Let slab put the object onto the freelist. */
303 return false;
304 }
305
check_page_allocation(void * ptr,unsigned long ip)306 static inline bool check_page_allocation(void *ptr, unsigned long ip)
307 {
308 if (ptr != page_address(virt_to_head_page(ptr))) {
309 kasan_report_invalid_free(ptr, ip, KASAN_REPORT_INVALID_FREE);
310 return true;
311 }
312
313 if (!kasan_byte_accessible(ptr)) {
314 kasan_report_invalid_free(ptr, ip, KASAN_REPORT_DOUBLE_FREE);
315 return true;
316 }
317
318 return false;
319 }
320
__kasan_kfree_large(void * ptr,unsigned long ip)321 void __kasan_kfree_large(void *ptr, unsigned long ip)
322 {
323 check_page_allocation(ptr, ip);
324
325 /* The object will be poisoned by kasan_poison_pages(). */
326 }
327
unpoison_slab_object(struct kmem_cache * cache,void * object,gfp_t flags,bool init)328 static inline void unpoison_slab_object(struct kmem_cache *cache, void *object,
329 gfp_t flags, bool init)
330 {
331 /*
332 * Unpoison the whole object. For kmalloc() allocations,
333 * poison_kmalloc_redzone() will do precise poisoning.
334 */
335 kasan_unpoison(object, cache->object_size, init);
336
337 /* Save alloc info (if possible) for non-kmalloc() allocations. */
338 if (kasan_stack_collection_enabled() && !is_kmalloc_cache(cache))
339 kasan_save_alloc_info(cache, object, flags);
340 }
341
__kasan_slab_alloc(struct kmem_cache * cache,void * object,gfp_t flags,bool init)342 void * __must_check __kasan_slab_alloc(struct kmem_cache *cache,
343 void *object, gfp_t flags, bool init)
344 {
345 u8 tag;
346 void *tagged_object;
347
348 if (gfpflags_allow_blocking(flags))
349 kasan_quarantine_reduce();
350
351 if (unlikely(object == NULL))
352 return NULL;
353
354 if (is_kfence_address(object))
355 return (void *)object;
356
357 /*
358 * Generate and assign random tag for tag-based modes.
359 * Tag is ignored in set_tag() for the generic mode.
360 */
361 tag = assign_tag(cache, object, false);
362 tagged_object = set_tag(object, tag);
363
364 /* Unpoison the object and save alloc info for non-kmalloc() allocations. */
365 unpoison_slab_object(cache, tagged_object, flags, init);
366
367 return tagged_object;
368 }
369
poison_kmalloc_redzone(struct kmem_cache * cache,const void * object,size_t size,gfp_t flags)370 static inline void poison_kmalloc_redzone(struct kmem_cache *cache,
371 const void *object, size_t size, gfp_t flags)
372 {
373 unsigned long redzone_start;
374 unsigned long redzone_end;
375
376 /*
377 * The redzone has byte-level precision for the generic mode.
378 * Partially poison the last object granule to cover the unaligned
379 * part of the redzone.
380 */
381 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
382 kasan_poison_last_granule((void *)object, size);
383
384 /* Poison the aligned part of the redzone. */
385 redzone_start = round_up((unsigned long)(object + size),
386 KASAN_GRANULE_SIZE);
387 redzone_end = round_up((unsigned long)(object + cache->object_size),
388 KASAN_GRANULE_SIZE);
389 kasan_poison((void *)redzone_start, redzone_end - redzone_start,
390 KASAN_SLAB_REDZONE, false);
391
392 /*
393 * Save alloc info (if possible) for kmalloc() allocations.
394 * This also rewrites the alloc info when called from kasan_krealloc().
395 */
396 if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache))
397 kasan_save_alloc_info(cache, (void *)object, flags);
398
399 }
400
__kasan_kmalloc(struct kmem_cache * cache,const void * object,size_t size,gfp_t flags)401 void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object,
402 size_t size, gfp_t flags)
403 {
404 if (gfpflags_allow_blocking(flags))
405 kasan_quarantine_reduce();
406
407 if (unlikely(object == NULL))
408 return NULL;
409
410 if (is_kfence_address(object))
411 return (void *)object;
412
413 /* The object has already been unpoisoned by kasan_slab_alloc(). */
414 poison_kmalloc_redzone(cache, object, size, flags);
415
416 /* Keep the tag that was set by kasan_slab_alloc(). */
417 return (void *)object;
418 }
419 EXPORT_SYMBOL(__kasan_kmalloc);
420
poison_kmalloc_large_redzone(const void * ptr,size_t size,gfp_t flags)421 static inline void poison_kmalloc_large_redzone(const void *ptr, size_t size,
422 gfp_t flags)
423 {
424 unsigned long redzone_start;
425 unsigned long redzone_end;
426
427 /*
428 * The redzone has byte-level precision for the generic mode.
429 * Partially poison the last object granule to cover the unaligned
430 * part of the redzone.
431 */
432 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
433 kasan_poison_last_granule(ptr, size);
434
435 /* Poison the aligned part of the redzone. */
436 redzone_start = round_up((unsigned long)(ptr + size), KASAN_GRANULE_SIZE);
437 redzone_end = (unsigned long)ptr + page_size(virt_to_page(ptr));
438 kasan_poison((void *)redzone_start, redzone_end - redzone_start,
439 KASAN_PAGE_REDZONE, false);
440 }
441
__kasan_kmalloc_large(const void * ptr,size_t size,gfp_t flags)442 void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size,
443 gfp_t flags)
444 {
445 if (gfpflags_allow_blocking(flags))
446 kasan_quarantine_reduce();
447
448 if (unlikely(ptr == NULL))
449 return NULL;
450
451 /* The object has already been unpoisoned by kasan_unpoison_pages(). */
452 poison_kmalloc_large_redzone(ptr, size, flags);
453
454 /* Keep the tag that was set by alloc_pages(). */
455 return (void *)ptr;
456 }
457
__kasan_krealloc(const void * object,size_t size,gfp_t flags)458 void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flags)
459 {
460 struct slab *slab;
461
462 if (gfpflags_allow_blocking(flags))
463 kasan_quarantine_reduce();
464
465 if (unlikely(object == ZERO_SIZE_PTR))
466 return (void *)object;
467
468 if (is_kfence_address(object))
469 return (void *)object;
470
471 /*
472 * Unpoison the object's data.
473 * Part of it might already have been unpoisoned, but it's unknown
474 * how big that part is.
475 */
476 kasan_unpoison(object, size, false);
477
478 slab = virt_to_slab(object);
479
480 /* Piggy-back on kmalloc() instrumentation to poison the redzone. */
481 if (unlikely(!slab))
482 poison_kmalloc_large_redzone(object, size, flags);
483 else
484 poison_kmalloc_redzone(slab->slab_cache, object, size, flags);
485
486 return (void *)object;
487 }
488
__kasan_mempool_poison_pages(struct page * page,unsigned int order,unsigned long ip)489 bool __kasan_mempool_poison_pages(struct page *page, unsigned int order,
490 unsigned long ip)
491 {
492 unsigned long *ptr;
493
494 if (unlikely(PageHighMem(page)))
495 return true;
496
497 /* Bail out if allocation was excluded due to sampling. */
498 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
499 page_kasan_tag(page) == KASAN_TAG_KERNEL)
500 return true;
501
502 ptr = page_address(page);
503
504 if (check_page_allocation(ptr, ip))
505 return false;
506
507 kasan_poison(ptr, PAGE_SIZE << order, KASAN_PAGE_FREE, false);
508
509 return true;
510 }
511
__kasan_mempool_unpoison_pages(struct page * page,unsigned int order,unsigned long ip)512 void __kasan_mempool_unpoison_pages(struct page *page, unsigned int order,
513 unsigned long ip)
514 {
515 __kasan_unpoison_pages(page, order, false);
516 }
517
__kasan_mempool_poison_object(void * ptr,unsigned long ip)518 bool __kasan_mempool_poison_object(void *ptr, unsigned long ip)
519 {
520 struct page *page = virt_to_page(ptr);
521 struct slab *slab;
522
523 if (unlikely(PageLargeKmalloc(page))) {
524 if (check_page_allocation(ptr, ip))
525 return false;
526 kasan_poison(ptr, page_size(page), KASAN_PAGE_FREE, false);
527 return true;
528 }
529
530 if (is_kfence_address(ptr))
531 return true;
532
533 slab = page_slab(page);
534
535 if (check_slab_allocation(slab->slab_cache, ptr, ip))
536 return false;
537
538 poison_slab_object(slab->slab_cache, ptr, false);
539 return true;
540 }
541
__kasan_mempool_unpoison_object(void * ptr,size_t size,unsigned long ip)542 void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip)
543 {
544 struct slab *slab;
545 gfp_t flags = 0; /* Might be executing under a lock. */
546
547 slab = virt_to_slab(ptr);
548
549 /*
550 * This function can be called for large kmalloc allocation that get
551 * their memory from page_alloc.
552 */
553 if (unlikely(!slab)) {
554 kasan_unpoison(ptr, size, false);
555 poison_kmalloc_large_redzone(ptr, size, flags);
556 return;
557 }
558
559 if (is_kfence_address(ptr))
560 return;
561
562 /* Unpoison the object and save alloc info for non-kmalloc() allocations. */
563 unpoison_slab_object(slab->slab_cache, ptr, flags, false);
564
565 /* Poison the redzone and save alloc info for kmalloc() allocations. */
566 if (is_kmalloc_cache(slab->slab_cache))
567 poison_kmalloc_redzone(slab->slab_cache, ptr, size, flags);
568 }
569
__kasan_check_byte(const void * address,unsigned long ip)570 bool __kasan_check_byte(const void *address, unsigned long ip)
571 {
572 if (!kasan_byte_accessible(address)) {
573 kasan_report(address, 1, false, ip);
574 return false;
575 }
576 return true;
577 }
578