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 (!kasan_enabled())
309 return false;
310
311 if (ptr != page_address(virt_to_head_page(ptr))) {
312 kasan_report_invalid_free(ptr, ip, KASAN_REPORT_INVALID_FREE);
313 return true;
314 }
315
316 if (!kasan_byte_accessible(ptr)) {
317 kasan_report_invalid_free(ptr, ip, KASAN_REPORT_DOUBLE_FREE);
318 return true;
319 }
320
321 return false;
322 }
323
__kasan_kfree_large(void * ptr,unsigned long ip)324 void __kasan_kfree_large(void *ptr, unsigned long ip)
325 {
326 check_page_allocation(ptr, ip);
327
328 /* The object will be poisoned by kasan_poison_pages(). */
329 }
330
unpoison_slab_object(struct kmem_cache * cache,void * object,gfp_t flags,bool init)331 static inline void unpoison_slab_object(struct kmem_cache *cache, void *object,
332 gfp_t flags, bool init)
333 {
334 /*
335 * Unpoison the whole object. For kmalloc() allocations,
336 * poison_kmalloc_redzone() will do precise poisoning.
337 */
338 kasan_unpoison(object, cache->object_size, init);
339
340 /* Save alloc info (if possible) for non-kmalloc() allocations. */
341 if (kasan_stack_collection_enabled() && !is_kmalloc_cache(cache))
342 kasan_save_alloc_info(cache, object, flags);
343 }
344
__kasan_slab_alloc(struct kmem_cache * cache,void * object,gfp_t flags,bool init)345 void * __must_check __kasan_slab_alloc(struct kmem_cache *cache,
346 void *object, gfp_t flags, bool init)
347 {
348 u8 tag;
349 void *tagged_object;
350
351 if (gfpflags_allow_blocking(flags))
352 kasan_quarantine_reduce();
353
354 if (unlikely(object == NULL))
355 return NULL;
356
357 if (is_kfence_address(object))
358 return (void *)object;
359
360 /*
361 * Generate and assign random tag for tag-based modes.
362 * Tag is ignored in set_tag() for the generic mode.
363 */
364 tag = assign_tag(cache, object, false);
365 tagged_object = set_tag(object, tag);
366
367 /* Unpoison the object and save alloc info for non-kmalloc() allocations. */
368 unpoison_slab_object(cache, tagged_object, flags, init);
369
370 return tagged_object;
371 }
372
poison_kmalloc_redzone(struct kmem_cache * cache,const void * object,size_t size,gfp_t flags)373 static inline void poison_kmalloc_redzone(struct kmem_cache *cache,
374 const void *object, size_t size, gfp_t flags)
375 {
376 unsigned long redzone_start;
377 unsigned long redzone_end;
378
379 /*
380 * The redzone has byte-level precision for the generic mode.
381 * Partially poison the last object granule to cover the unaligned
382 * part of the redzone.
383 */
384 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
385 kasan_poison_last_granule((void *)object, size);
386
387 /* Poison the aligned part of the redzone. */
388 redzone_start = round_up((unsigned long)(object + size),
389 KASAN_GRANULE_SIZE);
390 redzone_end = round_up((unsigned long)(object + cache->object_size),
391 KASAN_GRANULE_SIZE);
392 kasan_poison((void *)redzone_start, redzone_end - redzone_start,
393 KASAN_SLAB_REDZONE, false);
394
395 /*
396 * Save alloc info (if possible) for kmalloc() allocations.
397 * This also rewrites the alloc info when called from kasan_krealloc().
398 */
399 if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache))
400 kasan_save_alloc_info(cache, (void *)object, flags);
401
402 }
403
__kasan_kmalloc(struct kmem_cache * cache,const void * object,size_t size,gfp_t flags)404 void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object,
405 size_t size, gfp_t flags)
406 {
407 if (gfpflags_allow_blocking(flags))
408 kasan_quarantine_reduce();
409
410 if (unlikely(object == NULL))
411 return NULL;
412
413 if (is_kfence_address(object))
414 return (void *)object;
415
416 /* The object has already been unpoisoned by kasan_slab_alloc(). */
417 poison_kmalloc_redzone(cache, object, size, flags);
418
419 /* Keep the tag that was set by kasan_slab_alloc(). */
420 return (void *)object;
421 }
422 EXPORT_SYMBOL(__kasan_kmalloc);
423
poison_kmalloc_large_redzone(const void * ptr,size_t size,gfp_t flags)424 static inline void poison_kmalloc_large_redzone(const void *ptr, size_t size,
425 gfp_t flags)
426 {
427 unsigned long redzone_start;
428 unsigned long redzone_end;
429
430 /*
431 * The redzone has byte-level precision for the generic mode.
432 * Partially poison the last object granule to cover the unaligned
433 * part of the redzone.
434 */
435 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
436 kasan_poison_last_granule(ptr, size);
437
438 /* Poison the aligned part of the redzone. */
439 redzone_start = round_up((unsigned long)(ptr + size), KASAN_GRANULE_SIZE);
440 redzone_end = (unsigned long)ptr + page_size(virt_to_page(ptr));
441 kasan_poison((void *)redzone_start, redzone_end - redzone_start,
442 KASAN_PAGE_REDZONE, false);
443 }
444
__kasan_kmalloc_large(const void * ptr,size_t size,gfp_t flags)445 void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size,
446 gfp_t flags)
447 {
448 if (gfpflags_allow_blocking(flags))
449 kasan_quarantine_reduce();
450
451 if (unlikely(ptr == NULL))
452 return NULL;
453
454 /* The object has already been unpoisoned by kasan_unpoison_pages(). */
455 poison_kmalloc_large_redzone(ptr, size, flags);
456
457 /* Keep the tag that was set by alloc_pages(). */
458 return (void *)ptr;
459 }
460
__kasan_krealloc(const void * object,size_t size,gfp_t flags)461 void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flags)
462 {
463 struct slab *slab;
464
465 if (gfpflags_allow_blocking(flags))
466 kasan_quarantine_reduce();
467
468 if (unlikely(object == ZERO_SIZE_PTR))
469 return (void *)object;
470
471 if (is_kfence_address(object))
472 return (void *)object;
473
474 /*
475 * Unpoison the object's data.
476 * Part of it might already have been unpoisoned, but it's unknown
477 * how big that part is.
478 */
479 kasan_unpoison(object, size, false);
480
481 slab = virt_to_slab(object);
482
483 /* Piggy-back on kmalloc() instrumentation to poison the redzone. */
484 if (unlikely(!slab))
485 poison_kmalloc_large_redzone(object, size, flags);
486 else
487 poison_kmalloc_redzone(slab->slab_cache, object, size, flags);
488
489 return (void *)object;
490 }
491
__kasan_mempool_poison_pages(struct page * page,unsigned int order,unsigned long ip)492 bool __kasan_mempool_poison_pages(struct page *page, unsigned int order,
493 unsigned long ip)
494 {
495 unsigned long *ptr;
496
497 if (unlikely(PageHighMem(page)))
498 return true;
499
500 /* Bail out if allocation was excluded due to sampling. */
501 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
502 page_kasan_tag(page) == KASAN_TAG_KERNEL)
503 return true;
504
505 ptr = page_address(page);
506
507 if (check_page_allocation(ptr, ip))
508 return false;
509
510 kasan_poison(ptr, PAGE_SIZE << order, KASAN_PAGE_FREE, false);
511
512 return true;
513 }
514
__kasan_mempool_unpoison_pages(struct page * page,unsigned int order,unsigned long ip)515 void __kasan_mempool_unpoison_pages(struct page *page, unsigned int order,
516 unsigned long ip)
517 {
518 __kasan_unpoison_pages(page, order, false);
519 }
520
__kasan_mempool_poison_object(void * ptr,unsigned long ip)521 bool __kasan_mempool_poison_object(void *ptr, unsigned long ip)
522 {
523 struct folio *folio = virt_to_folio(ptr);
524 struct slab *slab;
525
526 /*
527 * This function can be called for large kmalloc allocation that get
528 * their memory from page_alloc. Thus, the folio might not be a slab.
529 */
530 if (unlikely(!folio_test_slab(folio))) {
531 if (check_page_allocation(ptr, ip))
532 return false;
533 kasan_poison(ptr, folio_size(folio), KASAN_PAGE_FREE, false);
534 return true;
535 }
536
537 if (is_kfence_address(ptr))
538 return true;
539
540 slab = folio_slab(folio);
541
542 if (check_slab_allocation(slab->slab_cache, ptr, ip))
543 return false;
544
545 poison_slab_object(slab->slab_cache, ptr, false);
546 return true;
547 }
548
__kasan_mempool_unpoison_object(void * ptr,size_t size,unsigned long ip)549 void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip)
550 {
551 struct slab *slab;
552 gfp_t flags = 0; /* Might be executing under a lock. */
553
554 slab = virt_to_slab(ptr);
555
556 /*
557 * This function can be called for large kmalloc allocation that get
558 * their memory from page_alloc.
559 */
560 if (unlikely(!slab)) {
561 kasan_unpoison(ptr, size, false);
562 poison_kmalloc_large_redzone(ptr, size, flags);
563 return;
564 }
565
566 if (is_kfence_address(ptr))
567 return;
568
569 /* Unpoison the object and save alloc info for non-kmalloc() allocations. */
570 unpoison_slab_object(slab->slab_cache, ptr, flags, false);
571
572 /* Poison the redzone and save alloc info for kmalloc() allocations. */
573 if (is_kmalloc_cache(slab->slab_cache))
574 poison_kmalloc_redzone(slab->slab_cache, ptr, size, flags);
575 }
576
__kasan_check_byte(const void * address,unsigned long ip)577 bool __kasan_check_byte(const void *address, unsigned long ip)
578 {
579 if (!kasan_byte_accessible(address)) {
580 kasan_report(address, 1, false, ip);
581 return false;
582 }
583 return true;
584 }
585