1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __MM_KASAN_KASAN_H
3 #define __MM_KASAN_KASAN_H
5 #include <linux/kasan.h>
6 #include <linux/stackdepot.h>
8 #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
9 #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
11 #define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */
12 #define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */
13 #define KASAN_TAG_MAX 0xFD /* maximum value for random tags */
15 #define KASAN_FREE_PAGE 0xFF /* page was freed */
16 #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */
17 #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */
18 #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */
19 #define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */
22 * Stack redzone shadow values
23 * (Those are compiler's ABI, don't change them)
25 #define KASAN_STACK_LEFT 0xF1
26 #define KASAN_STACK_MID 0xF2
27 #define KASAN_STACK_RIGHT 0xF3
28 #define KASAN_STACK_PARTIAL 0xF4
29 #define KASAN_USE_AFTER_SCOPE 0xF8
32 * alloca redzone shadow values
34 #define KASAN_ALLOCA_LEFT 0xCA
35 #define KASAN_ALLOCA_RIGHT 0xCB
37 #define KASAN_ALLOCA_REDZONE_SIZE 32
39 /* Don't break randconfig/all*config builds */
40 #ifndef KASAN_ABI_VERSION
41 #define KASAN_ABI_VERSION 1
44 struct kasan_access_info
{
45 const void *access_addr
;
46 const void *first_bad_addr
;
52 /* The layout of struct dictated by compiler */
53 struct kasan_source_location
{
59 /* The layout of struct dictated by compiler */
61 const void *beg
; /* Address of the beginning of the global variable. */
62 size_t size
; /* Size of the global variable. */
63 size_t size_with_redzone
; /* Size of the variable + size of the red zone. 32 bytes aligned */
65 const void *module_name
; /* Name of the module where the global variable is declared. */
66 unsigned long has_dynamic_init
; /* This needed for C++ */
67 #if KASAN_ABI_VERSION >= 4
68 struct kasan_source_location
*location
;
70 #if KASAN_ABI_VERSION >= 5
76 * Structures to keep alloc and free tracks *
79 #define KASAN_STACK_DEPTH 64
83 depot_stack_handle_t stack
;
86 struct kasan_alloc_meta
{
87 struct kasan_track alloc_track
;
88 struct kasan_track free_track
;
92 struct qlist_node
*next
;
94 struct kasan_free_meta
{
95 /* This field is used while the object is in the quarantine.
96 * Otherwise it might be used for the allocator freelist.
98 struct qlist_node quarantine_link
;
101 struct kasan_alloc_meta
*get_alloc_info(struct kmem_cache
*cache
,
103 struct kasan_free_meta
*get_free_info(struct kmem_cache
*cache
,
106 static inline const void *kasan_shadow_to_mem(const void *shadow_addr
)
108 return (void *)(((unsigned long)shadow_addr
- KASAN_SHADOW_OFFSET
)
109 << KASAN_SHADOW_SCALE_SHIFT
);
112 void kasan_poison_shadow(const void *address
, size_t size
, u8 value
);
114 void check_memory_region(unsigned long addr
, size_t size
, bool write
,
115 unsigned long ret_ip
);
117 void kasan_report(unsigned long addr
, size_t size
,
118 bool is_write
, unsigned long ip
);
119 void kasan_report_invalid_free(void *object
, unsigned long ip
);
121 #if defined(CONFIG_KASAN_GENERIC) && \
122 (defined(CONFIG_SLAB) || defined(CONFIG_SLUB))
123 void quarantine_put(struct kasan_free_meta
*info
, struct kmem_cache
*cache
);
124 void quarantine_reduce(void);
125 void quarantine_remove_cache(struct kmem_cache
*cache
);
127 static inline void quarantine_put(struct kasan_free_meta
*info
,
128 struct kmem_cache
*cache
) { }
129 static inline void quarantine_reduce(void) { }
130 static inline void quarantine_remove_cache(struct kmem_cache
*cache
) { }
133 #ifdef CONFIG_KASAN_SW_TAGS
139 static inline u8
random_tag(void)
146 #ifndef arch_kasan_set_tag
147 #define arch_kasan_set_tag(addr, tag) ((void *)(addr))
149 #ifndef arch_kasan_reset_tag
150 #define arch_kasan_reset_tag(addr) ((void *)(addr))
152 #ifndef arch_kasan_get_tag
153 #define arch_kasan_get_tag(addr) 0
156 #define set_tag(addr, tag) ((void *)arch_kasan_set_tag((addr), (tag)))
157 #define reset_tag(addr) ((void *)arch_kasan_reset_tag(addr))
158 #define get_tag(addr) arch_kasan_get_tag(addr)
161 * Exported functions for interfaces called from assembly or from generated
162 * code. Declarations here to avoid warning about missing declarations.
164 asmlinkage
void kasan_unpoison_task_stack_below(const void *watermark
);
165 void __asan_register_globals(struct kasan_global
*globals
, size_t size
);
166 void __asan_unregister_globals(struct kasan_global
*globals
, size_t size
);
167 void __asan_loadN(unsigned long addr
, size_t size
);
168 void __asan_storeN(unsigned long addr
, size_t size
);
169 void __asan_handle_no_return(void);
170 void __asan_poison_stack_memory(const void *addr
, size_t size
);
171 void __asan_unpoison_stack_memory(const void *addr
, size_t size
);
172 void __asan_alloca_poison(unsigned long addr
, size_t size
);
173 void __asan_allocas_unpoison(const void *stack_top
, const void *stack_bottom
);
175 void __asan_load1(unsigned long addr
);
176 void __asan_store1(unsigned long addr
);
177 void __asan_load2(unsigned long addr
);
178 void __asan_store2(unsigned long addr
);
179 void __asan_load4(unsigned long addr
);
180 void __asan_store4(unsigned long addr
);
181 void __asan_load8(unsigned long addr
);
182 void __asan_store8(unsigned long addr
);
183 void __asan_load16(unsigned long addr
);
184 void __asan_store16(unsigned long addr
);
186 void __asan_load1_noabort(unsigned long addr
);
187 void __asan_store1_noabort(unsigned long addr
);
188 void __asan_load2_noabort(unsigned long addr
);
189 void __asan_store2_noabort(unsigned long addr
);
190 void __asan_load4_noabort(unsigned long addr
);
191 void __asan_store4_noabort(unsigned long addr
);
192 void __asan_load8_noabort(unsigned long addr
);
193 void __asan_store8_noabort(unsigned long addr
);
194 void __asan_load16_noabort(unsigned long addr
);
195 void __asan_store16_noabort(unsigned long addr
);
197 void __asan_set_shadow_00(const void *addr
, size_t size
);
198 void __asan_set_shadow_f1(const void *addr
, size_t size
);
199 void __asan_set_shadow_f2(const void *addr
, size_t size
);
200 void __asan_set_shadow_f3(const void *addr
, size_t size
);
201 void __asan_set_shadow_f5(const void *addr
, size_t size
);
202 void __asan_set_shadow_f8(const void *addr
, size_t size
);