1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __MM_KASAN_KASAN_H
3 #define __MM_KASAN_KASAN_H
5 #include <linux/atomic.h>
6 #include <linux/kasan.h>
7 #include <linux/kasan-tags.h>
8 #include <linux/kfence.h>
9 #include <linux/stackdepot.h>
11 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
13 #include <linux/static_key.h>
15 DECLARE_STATIC_KEY_TRUE(kasan_flag_stacktrace);
17 static inline bool kasan_stack_collection_enabled(void)
19 return static_branch_unlikely(&kasan_flag_stacktrace);
22 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
24 static inline bool kasan_stack_collection_enabled(void)
29 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
31 #ifdef CONFIG_KASAN_HW_TAGS
35 DECLARE_STATIC_KEY_TRUE(kasan_flag_vmalloc);
43 extern enum kasan_mode kasan_mode __ro_after_init;
45 extern unsigned long kasan_page_alloc_sample;
46 extern unsigned int kasan_page_alloc_sample_order;
47 DECLARE_PER_CPU(long, kasan_page_alloc_skip);
49 static inline bool kasan_vmalloc_enabled(void)
51 /* Static branch is never enabled with CONFIG_KASAN_VMALLOC disabled. */
52 return static_branch_likely(&kasan_flag_vmalloc);
55 static inline bool kasan_async_fault_possible(void)
57 return kasan_mode == KASAN_MODE_ASYNC || kasan_mode == KASAN_MODE_ASYMM;
60 static inline bool kasan_sync_fault_possible(void)
62 return kasan_mode == KASAN_MODE_SYNC || kasan_mode == KASAN_MODE_ASYMM;
65 static inline bool kasan_sample_page_alloc(unsigned int order)
67 /* Fast-path for when sampling is disabled. */
68 if (kasan_page_alloc_sample == 1)
71 if (order < kasan_page_alloc_sample_order)
74 if (this_cpu_dec_return(kasan_page_alloc_skip) < 0) {
75 this_cpu_write(kasan_page_alloc_skip,
76 kasan_page_alloc_sample - 1);
83 #else /* CONFIG_KASAN_HW_TAGS */
85 static inline bool kasan_vmalloc_enabled(void)
87 return IS_ENABLED(CONFIG_KASAN_VMALLOC);
90 static inline bool kasan_async_fault_possible(void)
95 static inline bool kasan_sync_fault_possible(void)
100 static inline bool kasan_sample_page_alloc(unsigned int order)
105 #endif /* CONFIG_KASAN_HW_TAGS */
107 #ifdef CONFIG_KASAN_GENERIC
110 * Generic KASAN uses per-object metadata to store alloc and free stack traces
111 * and the quarantine link.
113 static inline bool kasan_requires_meta(void)
118 #else /* CONFIG_KASAN_GENERIC */
121 * Tag-based KASAN modes do not use per-object metadata: they use the stack
122 * ring to store alloc and free stack traces and do not use qurantine.
124 static inline bool kasan_requires_meta(void)
129 #endif /* CONFIG_KASAN_GENERIC */
131 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
132 #define KASAN_GRANULE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
134 #include <asm/mte-kasan.h>
135 #define KASAN_GRANULE_SIZE MTE_GRANULE_SIZE
138 #define KASAN_GRANULE_MASK (KASAN_GRANULE_SIZE - 1)
140 #define KASAN_MEMORY_PER_SHADOW_PAGE (KASAN_GRANULE_SIZE << PAGE_SHIFT)
142 #ifdef CONFIG_KASAN_GENERIC
143 #define KASAN_PAGE_FREE 0xFF /* freed page */
144 #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocation */
145 #define KASAN_SLAB_REDZONE 0xFC /* redzone for slab object */
146 #define KASAN_SLAB_FREE 0xFB /* freed slab object */
147 #define KASAN_VMALLOC_INVALID 0xF8 /* inaccessible space in vmap area */
149 #define KASAN_PAGE_FREE KASAN_TAG_INVALID
150 #define KASAN_PAGE_REDZONE KASAN_TAG_INVALID
151 #define KASAN_SLAB_REDZONE KASAN_TAG_INVALID
152 #define KASAN_SLAB_FREE KASAN_TAG_INVALID
153 #define KASAN_VMALLOC_INVALID KASAN_TAG_INVALID /* only used for SW_TAGS */
156 #ifdef CONFIG_KASAN_GENERIC
158 #define KASAN_SLAB_FREE_META 0xFA /* freed slab object with free meta */
159 #define KASAN_GLOBAL_REDZONE 0xF9 /* redzone for global variable */
161 /* Stack redzone shadow values. Compiler ABI, do not change. */
162 #define KASAN_STACK_LEFT 0xF1
163 #define KASAN_STACK_MID 0xF2
164 #define KASAN_STACK_RIGHT 0xF3
165 #define KASAN_STACK_PARTIAL 0xF4
167 /* alloca redzone shadow values. */
168 #define KASAN_ALLOCA_LEFT 0xCA
169 #define KASAN_ALLOCA_RIGHT 0xCB
171 /* alloca redzone size. Compiler ABI, do not change. */
172 #define KASAN_ALLOCA_REDZONE_SIZE 32
174 /* Stack frame marker. Compiler ABI, do not change. */
175 #define KASAN_CURRENT_STACK_FRAME_MAGIC 0x41B58AB3
177 /* Dummy value to avoid breaking randconfig/all*config builds. */
178 #ifndef KASAN_ABI_VERSION
179 #define KASAN_ABI_VERSION 1
182 #endif /* CONFIG_KASAN_GENERIC */
184 /* Metadata layout customization. */
185 #define META_BYTES_PER_BLOCK 1
186 #define META_BLOCKS_PER_ROW 16
187 #define META_BYTES_PER_ROW (META_BLOCKS_PER_ROW * META_BYTES_PER_BLOCK)
188 #define META_MEM_BYTES_PER_ROW (META_BYTES_PER_ROW * KASAN_GRANULE_SIZE)
189 #define META_ROWS_AROUND_ADDR 2
191 #define KASAN_STACK_DEPTH 64
195 depot_stack_handle_t stack;
196 #ifdef CONFIG_KASAN_EXTRA_INFO
199 #endif /* CONFIG_KASAN_EXTRA_INFO */
202 enum kasan_report_type {
204 KASAN_REPORT_INVALID_FREE,
205 KASAN_REPORT_DOUBLE_FREE,
208 struct kasan_report_info {
209 /* Filled in by kasan_report_*(). */
210 enum kasan_report_type type;
211 const void *access_addr;
216 /* Filled in by the common reporting code. */
217 const void *first_bad_addr;
218 struct kmem_cache *cache;
222 /* Filled in by the mode-specific reporting code. */
223 const char *bug_type;
224 struct kasan_track alloc_track;
225 struct kasan_track free_track;
228 /* Do not change the struct layout: compiler ABI. */
229 struct kasan_source_location {
230 const char *filename;
235 /* Do not change the struct layout: compiler ABI. */
236 struct kasan_global {
237 const void *beg; /* Address of the beginning of the global variable. */
238 size_t size; /* Size of the global variable. */
239 size_t size_with_redzone; /* Size of the variable + size of the redzone. 32 bytes aligned. */
241 const void *module_name; /* Name of the module where the global variable is declared. */
242 unsigned long has_dynamic_init; /* This is needed for C++. */
243 #if KASAN_ABI_VERSION >= 4
244 struct kasan_source_location *location;
246 #if KASAN_ABI_VERSION >= 5
251 /* Structures for keeping alloc and free meta. */
253 #ifdef CONFIG_KASAN_GENERIC
256 * Alloc meta contains the allocation-related information about a slab object.
257 * Alloc meta is saved when an object is allocated and is kept until either the
258 * object returns to the slab freelist (leaves quarantine for quarantined
259 * objects or gets freed for the non-quarantined ones) or reallocated via
260 * krealloc or through a mempool.
261 * Alloc meta is stored inside of the object's redzone.
262 * Alloc meta is considered valid whenever it contains non-zero data.
264 struct kasan_alloc_meta {
265 struct kasan_track alloc_track;
266 /* Free track is stored in kasan_free_meta. */
267 depot_stack_handle_t aux_stack[2];
271 struct qlist_node *next;
275 * Free meta is stored either in the object itself or in the redzone after the
276 * object. In the former case, free meta offset is 0. In the latter case, the
277 * offset is between 0 and INT_MAX. INT_MAX marks that free meta is not present.
279 #define KASAN_NO_FREE_META INT_MAX
282 * Free meta contains the freeing-related information about a slab object.
283 * Free meta is only kept for quarantined objects and for mempool objects until
284 * the object gets allocated again.
285 * Free meta is stored within the object's memory.
286 * Free meta is considered valid whenever the value of the shadow byte that
287 * corresponds to the first 8 bytes of the object is KASAN_SLAB_FREE_META.
289 struct kasan_free_meta {
290 struct qlist_node quarantine_link;
291 struct kasan_track free_track;
294 #endif /* CONFIG_KASAN_GENERIC */
296 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
298 struct kasan_stack_ring_entry {
301 struct kasan_track track;
305 struct kasan_stack_ring {
309 struct kasan_stack_ring_entry *entries;
312 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
314 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
316 static __always_inline bool addr_in_shadow(const void *addr)
318 return addr >= (void *)KASAN_SHADOW_START &&
319 addr < (void *)KASAN_SHADOW_END;
322 #ifndef kasan_shadow_to_mem
323 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
325 return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
326 << KASAN_SHADOW_SCALE_SHIFT);
330 #ifndef addr_has_metadata
331 static __always_inline bool addr_has_metadata(const void *addr)
333 return (kasan_reset_tag(addr) >=
334 kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
339 * kasan_check_range - Check memory region, and report if invalid access.
340 * @addr: the accessed address
341 * @size: the accessed size
342 * @write: true if access is a write access
343 * @ret_ip: return address
344 * @return: true if access was valid, false if invalid
346 bool kasan_check_range(const void *addr, size_t size, bool write,
347 unsigned long ret_ip);
349 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
351 static __always_inline bool addr_has_metadata(const void *addr)
353 return (is_vmalloc_addr(addr) || virt_addr_valid(addr));
356 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
358 const void *kasan_find_first_bad_addr(const void *addr, size_t size);
359 size_t kasan_get_alloc_size(void *object, struct kmem_cache *cache);
360 void kasan_complete_mode_report_info(struct kasan_report_info *info);
361 void kasan_metadata_fetch_row(char *buffer, void *row);
363 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
364 void kasan_print_tags(u8 addr_tag, const void *addr);
366 static inline void kasan_print_tags(u8 addr_tag, const void *addr) { }
369 #if defined(CONFIG_KASAN_STACK)
370 void kasan_print_address_stack_frame(const void *addr);
372 static inline void kasan_print_address_stack_frame(const void *addr) { }
375 #ifdef CONFIG_KASAN_GENERIC
376 void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object);
378 static inline void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object) { }
381 bool kasan_report(const void *addr, size_t size,
382 bool is_write, unsigned long ip);
383 void kasan_report_invalid_free(void *object, unsigned long ip, enum kasan_report_type type);
385 struct slab *kasan_addr_to_slab(const void *addr);
387 #ifdef CONFIG_KASAN_GENERIC
388 struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache,
390 struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache,
392 void kasan_init_object_meta(struct kmem_cache *cache, const void *object);
394 static inline void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { }
397 depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags);
398 void kasan_set_track(struct kasan_track *track, depot_stack_handle_t stack);
399 void kasan_save_track(struct kasan_track *track, gfp_t flags);
400 void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags);
401 void kasan_save_free_info(struct kmem_cache *cache, void *object);
403 #ifdef CONFIG_KASAN_GENERIC
404 bool kasan_quarantine_put(struct kmem_cache *cache, void *object);
405 void kasan_quarantine_reduce(void);
406 void kasan_quarantine_remove_cache(struct kmem_cache *cache);
408 static inline bool kasan_quarantine_put(struct kmem_cache *cache, void *object) { return false; }
409 static inline void kasan_quarantine_reduce(void) { }
410 static inline void kasan_quarantine_remove_cache(struct kmem_cache *cache) { }
413 #ifndef arch_kasan_set_tag
414 static inline const void *arch_kasan_set_tag(const void *addr, u8 tag)
419 #ifndef arch_kasan_get_tag
420 #define arch_kasan_get_tag(addr) 0
423 #define set_tag(addr, tag) ((void *)arch_kasan_set_tag((addr), (tag)))
424 #define get_tag(addr) arch_kasan_get_tag(addr)
426 #ifdef CONFIG_KASAN_HW_TAGS
428 #define hw_enable_tag_checks_sync() arch_enable_tag_checks_sync()
429 #define hw_enable_tag_checks_async() arch_enable_tag_checks_async()
430 #define hw_enable_tag_checks_asymm() arch_enable_tag_checks_asymm()
431 #define hw_suppress_tag_checks_start() arch_suppress_tag_checks_start()
432 #define hw_suppress_tag_checks_stop() arch_suppress_tag_checks_stop()
433 #define hw_force_async_tag_fault() arch_force_async_tag_fault()
434 #define hw_get_random_tag() arch_get_random_tag()
435 #define hw_get_mem_tag(addr) arch_get_mem_tag(addr)
436 #define hw_set_mem_tag_range(addr, size, tag, init) \
437 arch_set_mem_tag_range((addr), (size), (tag), (init))
439 void kasan_enable_hw_tags(void);
441 #else /* CONFIG_KASAN_HW_TAGS */
443 static inline void kasan_enable_hw_tags(void) { }
445 #endif /* CONFIG_KASAN_HW_TAGS */
447 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
448 void __init kasan_init_tags(void);
449 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
451 #if defined(CONFIG_KASAN_HW_TAGS) && IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
453 void kasan_force_async_fault(void);
455 #else /* CONFIG_KASAN_HW_TAGS && CONFIG_KASAN_KUNIT_TEST */
457 static inline void kasan_force_async_fault(void) { }
459 #endif /* CONFIG_KASAN_HW_TAGS && CONFIG_KASAN_KUNIT_TEST */
461 #ifdef CONFIG_KASAN_SW_TAGS
462 u8 kasan_random_tag(void);
463 #elif defined(CONFIG_KASAN_HW_TAGS)
464 static inline u8 kasan_random_tag(void) { return hw_get_random_tag(); }
466 static inline u8 kasan_random_tag(void) { return 0; }
469 #ifdef CONFIG_KASAN_HW_TAGS
471 static inline void kasan_poison(const void *addr, size_t size, u8 value, bool init)
473 if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
475 if (WARN_ON(size & KASAN_GRANULE_MASK))
478 hw_set_mem_tag_range(kasan_reset_tag(addr), size, value, init);
481 static inline void kasan_unpoison(const void *addr, size_t size, bool init)
483 u8 tag = get_tag(addr);
485 if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
487 size = round_up(size, KASAN_GRANULE_SIZE);
489 hw_set_mem_tag_range(kasan_reset_tag(addr), size, tag, init);
492 static inline bool kasan_byte_accessible(const void *addr)
494 u8 ptr_tag = get_tag(addr);
495 u8 mem_tag = hw_get_mem_tag((void *)addr);
497 return ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag;
500 #else /* CONFIG_KASAN_HW_TAGS */
503 * kasan_poison - mark the memory range as inaccessible
504 * @addr - range start address, must be aligned to KASAN_GRANULE_SIZE
505 * @size - range size, must be aligned to KASAN_GRANULE_SIZE
506 * @value - value that's written to metadata for the range
507 * @init - whether to initialize the memory range (only for hardware tag-based)
509 void kasan_poison(const void *addr, size_t size, u8 value, bool init);
512 * kasan_unpoison - mark the memory range as accessible
513 * @addr - range start address, must be aligned to KASAN_GRANULE_SIZE
514 * @size - range size, can be unaligned
515 * @init - whether to initialize the memory range (only for hardware tag-based)
517 * For the tag-based modes, the @size gets aligned to KASAN_GRANULE_SIZE before
519 * For the generic mode, the last granule of the memory range gets partially
520 * unpoisoned based on the @size.
522 void kasan_unpoison(const void *addr, size_t size, bool init);
524 bool kasan_byte_accessible(const void *addr);
526 #endif /* CONFIG_KASAN_HW_TAGS */
528 #ifdef CONFIG_KASAN_GENERIC
531 * kasan_poison_last_granule - mark the last granule of the memory range as
533 * @addr - range start address, must be aligned to KASAN_GRANULE_SIZE
536 * This function is only available for the generic mode, as it's the only mode
537 * that has partially poisoned memory granules.
539 void kasan_poison_last_granule(const void *address, size_t size);
541 #else /* CONFIG_KASAN_GENERIC */
543 static inline void kasan_poison_last_granule(const void *address, size_t size) { }
545 #endif /* CONFIG_KASAN_GENERIC */
547 #ifndef kasan_arch_is_ready
548 static inline bool kasan_arch_is_ready(void) { return true; }
549 #elif !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE)
550 #error kasan_arch_is_ready only works in KASAN generic outline mode!
553 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
555 void kasan_kunit_test_suite_start(void);
556 void kasan_kunit_test_suite_end(void);
558 #else /* CONFIG_KASAN_KUNIT_TEST */
560 static inline void kasan_kunit_test_suite_start(void) { }
561 static inline void kasan_kunit_test_suite_end(void) { }
563 #endif /* CONFIG_KASAN_KUNIT_TEST */
565 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST)
567 bool kasan_save_enable_multi_shot(void);
568 void kasan_restore_multi_shot(bool enabled);
573 * Exported functions for interfaces called from assembly or from generated
574 * code. Declared here to avoid warnings about missing declarations.
577 void __asan_register_globals(void *globals, ssize_t size);
578 void __asan_unregister_globals(void *globals, ssize_t size);
579 void __asan_handle_no_return(void);
580 void __asan_alloca_poison(void *, ssize_t size);
581 void __asan_allocas_unpoison(void *stack_top, ssize_t stack_bottom);
583 void __asan_load1(void *);
584 void __asan_store1(void *);
585 void __asan_load2(void *);
586 void __asan_store2(void *);
587 void __asan_load4(void *);
588 void __asan_store4(void *);
589 void __asan_load8(void *);
590 void __asan_store8(void *);
591 void __asan_load16(void *);
592 void __asan_store16(void *);
593 void __asan_loadN(void *, ssize_t size);
594 void __asan_storeN(void *, ssize_t size);
596 void __asan_load1_noabort(void *);
597 void __asan_store1_noabort(void *);
598 void __asan_load2_noabort(void *);
599 void __asan_store2_noabort(void *);
600 void __asan_load4_noabort(void *);
601 void __asan_store4_noabort(void *);
602 void __asan_load8_noabort(void *);
603 void __asan_store8_noabort(void *);
604 void __asan_load16_noabort(void *);
605 void __asan_store16_noabort(void *);
606 void __asan_loadN_noabort(void *, ssize_t size);
607 void __asan_storeN_noabort(void *, ssize_t size);
609 void __asan_report_load1_noabort(void *);
610 void __asan_report_store1_noabort(void *);
611 void __asan_report_load2_noabort(void *);
612 void __asan_report_store2_noabort(void *);
613 void __asan_report_load4_noabort(void *);
614 void __asan_report_store4_noabort(void *);
615 void __asan_report_load8_noabort(void *);
616 void __asan_report_store8_noabort(void *);
617 void __asan_report_load16_noabort(void *);
618 void __asan_report_store16_noabort(void *);
619 void __asan_report_load_n_noabort(void *, ssize_t size);
620 void __asan_report_store_n_noabort(void *, ssize_t size);
622 void __asan_set_shadow_00(const void *addr, ssize_t size);
623 void __asan_set_shadow_f1(const void *addr, ssize_t size);
624 void __asan_set_shadow_f2(const void *addr, ssize_t size);
625 void __asan_set_shadow_f3(const void *addr, ssize_t size);
626 void __asan_set_shadow_f5(const void *addr, ssize_t size);
627 void __asan_set_shadow_f8(const void *addr, ssize_t size);
629 void *__asan_memset(void *addr, int c, ssize_t len);
630 void *__asan_memmove(void *dest, const void *src, ssize_t len);
631 void *__asan_memcpy(void *dest, const void *src, ssize_t len);
633 void __hwasan_load1_noabort(void *);
634 void __hwasan_store1_noabort(void *);
635 void __hwasan_load2_noabort(void *);
636 void __hwasan_store2_noabort(void *);
637 void __hwasan_load4_noabort(void *);
638 void __hwasan_store4_noabort(void *);
639 void __hwasan_load8_noabort(void *);
640 void __hwasan_store8_noabort(void *);
641 void __hwasan_load16_noabort(void *);
642 void __hwasan_store16_noabort(void *);
643 void __hwasan_loadN_noabort(void *, ssize_t size);
644 void __hwasan_storeN_noabort(void *, ssize_t size);
646 void __hwasan_tag_memory(void *, u8 tag, ssize_t size);
648 void *__hwasan_memset(void *addr, int c, ssize_t len);
649 void *__hwasan_memmove(void *dest, const void *src, ssize_t len);
650 void *__hwasan_memcpy(void *dest, const void *src, ssize_t len);
652 void kasan_tag_mismatch(void *addr, unsigned long access_info,
653 unsigned long ret_ip);
655 #endif /* __MM_KASAN_KASAN_H */