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 #ifdef CONFIG_KASAN_HW_TAGS
9 #include <linux/static_key.h>
10 DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
11 static inline bool kasan_stack_collection_enabled(void)
13 return static_branch_unlikely(&kasan_flag_stacktrace);
16 static inline bool kasan_stack_collection_enabled(void)
22 extern bool kasan_flag_panic __ro_after_init;
24 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
25 #define KASAN_GRANULE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
27 #include <asm/mte-kasan.h>
28 #define KASAN_GRANULE_SIZE MTE_GRANULE_SIZE
31 #define KASAN_GRANULE_MASK (KASAN_GRANULE_SIZE - 1)
33 #define KASAN_MEMORY_PER_SHADOW_PAGE (KASAN_GRANULE_SIZE << PAGE_SHIFT)
35 #define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */
36 #define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */
37 #define KASAN_TAG_MAX 0xFD /* maximum value for random tags */
39 #ifdef CONFIG_KASAN_GENERIC
40 #define KASAN_FREE_PAGE 0xFF /* page was freed */
41 #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */
42 #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */
43 #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */
44 #define KASAN_KMALLOC_FREETRACK 0xFA /* object was freed and has free track set */
46 #define KASAN_FREE_PAGE KASAN_TAG_INVALID
47 #define KASAN_PAGE_REDZONE KASAN_TAG_INVALID
48 #define KASAN_KMALLOC_REDZONE KASAN_TAG_INVALID
49 #define KASAN_KMALLOC_FREE KASAN_TAG_INVALID
50 #define KASAN_KMALLOC_FREETRACK KASAN_TAG_INVALID
53 #define KASAN_GLOBAL_REDZONE 0xF9 /* redzone for global variable */
54 #define KASAN_VMALLOC_INVALID 0xF8 /* unallocated space in vmapped page */
57 * Stack redzone shadow values
58 * (Those are compiler's ABI, don't change them)
60 #define KASAN_STACK_LEFT 0xF1
61 #define KASAN_STACK_MID 0xF2
62 #define KASAN_STACK_RIGHT 0xF3
63 #define KASAN_STACK_PARTIAL 0xF4
66 * alloca redzone shadow values
68 #define KASAN_ALLOCA_LEFT 0xCA
69 #define KASAN_ALLOCA_RIGHT 0xCB
71 #define KASAN_ALLOCA_REDZONE_SIZE 32
74 * Stack frame marker (compiler ABI).
76 #define KASAN_CURRENT_STACK_FRAME_MAGIC 0x41B58AB3
78 /* Don't break randconfig/all*config builds */
79 #ifndef KASAN_ABI_VERSION
80 #define KASAN_ABI_VERSION 1
83 /* Metadata layout customization. */
84 #define META_BYTES_PER_BLOCK 1
85 #define META_BLOCKS_PER_ROW 16
86 #define META_BYTES_PER_ROW (META_BLOCKS_PER_ROW * META_BYTES_PER_BLOCK)
87 #define META_MEM_BYTES_PER_ROW (META_BYTES_PER_ROW * KASAN_GRANULE_SIZE)
88 #define META_ROWS_AROUND_ADDR 2
90 struct kasan_access_info {
91 const void *access_addr;
92 const void *first_bad_addr;
98 /* The layout of struct dictated by compiler */
99 struct kasan_source_location {
100 const char *filename;
105 /* The layout of struct dictated by compiler */
106 struct kasan_global {
107 const void *beg; /* Address of the beginning of the global variable. */
108 size_t size; /* Size of the global variable. */
109 size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */
111 const void *module_name; /* Name of the module where the global variable is declared. */
112 unsigned long has_dynamic_init; /* This needed for C++ */
113 #if KASAN_ABI_VERSION >= 4
114 struct kasan_source_location *location;
116 #if KASAN_ABI_VERSION >= 5
122 * Structures to keep alloc and free tracks *
125 #define KASAN_STACK_DEPTH 64
129 depot_stack_handle_t stack;
132 #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
133 #define KASAN_NR_FREE_STACKS 5
135 #define KASAN_NR_FREE_STACKS 1
138 struct kasan_alloc_meta {
139 struct kasan_track alloc_track;
140 #ifdef CONFIG_KASAN_GENERIC
142 * call_rcu() call stack is stored into struct kasan_alloc_meta.
143 * The free stack is stored into struct kasan_free_meta.
145 depot_stack_handle_t aux_stack[2];
147 struct kasan_track free_track[KASAN_NR_FREE_STACKS];
149 #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
150 u8 free_pointer_tag[KASAN_NR_FREE_STACKS];
156 struct qlist_node *next;
160 * Generic mode either stores free meta in the object itself or in the redzone
161 * after the object. In the former case free meta offset is 0, in the latter
162 * case it has some sane value smaller than INT_MAX. Use INT_MAX as free meta
163 * offset when free meta isn't present.
165 #define KASAN_NO_FREE_META INT_MAX
167 struct kasan_free_meta {
168 #ifdef CONFIG_KASAN_GENERIC
169 /* This field is used while the object is in the quarantine.
170 * Otherwise it might be used for the allocator freelist.
172 struct qlist_node quarantine_link;
173 struct kasan_track free_track;
177 struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache,
179 #ifdef CONFIG_KASAN_GENERIC
180 struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache,
184 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
186 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
188 return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
189 << KASAN_SHADOW_SCALE_SHIFT);
192 static inline bool addr_has_metadata(const void *addr)
194 return (addr >= kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
198 * check_memory_region - Check memory region, and report if invalid access.
199 * @addr: the accessed address
200 * @size: the accessed size
201 * @write: true if access is a write access
202 * @ret_ip: return address
203 * @return: true if access was valid, false if invalid
205 bool check_memory_region(unsigned long addr, size_t size, bool write,
206 unsigned long ret_ip);
208 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
210 static inline bool addr_has_metadata(const void *addr)
215 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
217 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
218 void print_tags(u8 addr_tag, const void *addr);
220 static inline void print_tags(u8 addr_tag, const void *addr) { }
223 void *find_first_bad_addr(void *addr, size_t size);
224 const char *get_bug_type(struct kasan_access_info *info);
225 void metadata_fetch_row(char *buffer, void *row);
227 #if defined(CONFIG_KASAN_GENERIC) && CONFIG_KASAN_STACK
228 void print_address_stack_frame(const void *addr);
230 static inline void print_address_stack_frame(const void *addr) { }
233 bool kasan_report(unsigned long addr, size_t size,
234 bool is_write, unsigned long ip);
235 void kasan_report_invalid_free(void *object, unsigned long ip);
237 struct page *kasan_addr_to_page(const void *addr);
239 depot_stack_handle_t kasan_save_stack(gfp_t flags);
240 void kasan_set_track(struct kasan_track *track, gfp_t flags);
241 void kasan_set_free_info(struct kmem_cache *cache, void *object, u8 tag);
242 struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
243 void *object, u8 tag);
245 #if defined(CONFIG_KASAN_GENERIC) && \
246 (defined(CONFIG_SLAB) || defined(CONFIG_SLUB))
247 bool quarantine_put(struct kmem_cache *cache, void *object);
248 void quarantine_reduce(void);
249 void quarantine_remove_cache(struct kmem_cache *cache);
251 static inline bool quarantine_put(struct kmem_cache *cache, void *object) { return false; }
252 static inline void quarantine_reduce(void) { }
253 static inline void quarantine_remove_cache(struct kmem_cache *cache) { }
256 #ifndef arch_kasan_set_tag
257 static inline const void *arch_kasan_set_tag(const void *addr, u8 tag)
262 #ifndef arch_kasan_get_tag
263 #define arch_kasan_get_tag(addr) 0
266 #define set_tag(addr, tag) ((void *)arch_kasan_set_tag((addr), (tag)))
267 #define get_tag(addr) arch_kasan_get_tag(addr)
269 #ifdef CONFIG_KASAN_HW_TAGS
271 #ifndef arch_enable_tagging
272 #define arch_enable_tagging()
274 #ifndef arch_init_tags
275 #define arch_init_tags(max_tag)
277 #ifndef arch_get_random_tag
278 #define arch_get_random_tag() (0xFF)
280 #ifndef arch_get_mem_tag
281 #define arch_get_mem_tag(addr) (0xFF)
283 #ifndef arch_set_mem_tag_range
284 #define arch_set_mem_tag_range(addr, size, tag) ((void *)(addr))
287 #define hw_enable_tagging() arch_enable_tagging()
288 #define hw_init_tags(max_tag) arch_init_tags(max_tag)
289 #define hw_get_random_tag() arch_get_random_tag()
290 #define hw_get_mem_tag(addr) arch_get_mem_tag(addr)
291 #define hw_set_mem_tag_range(addr, size, tag) arch_set_mem_tag_range((addr), (size), (tag))
293 #endif /* CONFIG_KASAN_HW_TAGS */
295 #ifdef CONFIG_KASAN_SW_TAGS
297 #elif defined(CONFIG_KASAN_HW_TAGS)
298 static inline u8 random_tag(void) { return hw_get_random_tag(); }
300 static inline u8 random_tag(void) { return 0; }
303 #ifdef CONFIG_KASAN_HW_TAGS
305 static inline void poison_range(const void *address, size_t size, u8 value)
307 hw_set_mem_tag_range(kasan_reset_tag(address),
308 round_up(size, KASAN_GRANULE_SIZE), value);
311 static inline void unpoison_range(const void *address, size_t size)
313 hw_set_mem_tag_range(kasan_reset_tag(address),
314 round_up(size, KASAN_GRANULE_SIZE), get_tag(address));
317 static inline bool check_invalid_free(void *addr)
319 u8 ptr_tag = get_tag(addr);
320 u8 mem_tag = hw_get_mem_tag(addr);
322 return (mem_tag == KASAN_TAG_INVALID) ||
323 (ptr_tag != KASAN_TAG_KERNEL && ptr_tag != mem_tag);
326 #else /* CONFIG_KASAN_HW_TAGS */
328 void poison_range(const void *address, size_t size, u8 value);
329 void unpoison_range(const void *address, size_t size);
330 bool check_invalid_free(void *addr);
332 #endif /* CONFIG_KASAN_HW_TAGS */
335 * Exported functions for interfaces called from assembly or from generated
336 * code. Declarations here to avoid warning about missing declarations.
338 asmlinkage void kasan_unpoison_task_stack_below(const void *watermark);
339 void __asan_register_globals(struct kasan_global *globals, size_t size);
340 void __asan_unregister_globals(struct kasan_global *globals, size_t size);
341 void __asan_handle_no_return(void);
342 void __asan_alloca_poison(unsigned long addr, size_t size);
343 void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom);
345 void __asan_load1(unsigned long addr);
346 void __asan_store1(unsigned long addr);
347 void __asan_load2(unsigned long addr);
348 void __asan_store2(unsigned long addr);
349 void __asan_load4(unsigned long addr);
350 void __asan_store4(unsigned long addr);
351 void __asan_load8(unsigned long addr);
352 void __asan_store8(unsigned long addr);
353 void __asan_load16(unsigned long addr);
354 void __asan_store16(unsigned long addr);
355 void __asan_loadN(unsigned long addr, size_t size);
356 void __asan_storeN(unsigned long addr, size_t size);
358 void __asan_load1_noabort(unsigned long addr);
359 void __asan_store1_noabort(unsigned long addr);
360 void __asan_load2_noabort(unsigned long addr);
361 void __asan_store2_noabort(unsigned long addr);
362 void __asan_load4_noabort(unsigned long addr);
363 void __asan_store4_noabort(unsigned long addr);
364 void __asan_load8_noabort(unsigned long addr);
365 void __asan_store8_noabort(unsigned long addr);
366 void __asan_load16_noabort(unsigned long addr);
367 void __asan_store16_noabort(unsigned long addr);
368 void __asan_loadN_noabort(unsigned long addr, size_t size);
369 void __asan_storeN_noabort(unsigned long addr, size_t size);
371 void __asan_report_load1_noabort(unsigned long addr);
372 void __asan_report_store1_noabort(unsigned long addr);
373 void __asan_report_load2_noabort(unsigned long addr);
374 void __asan_report_store2_noabort(unsigned long addr);
375 void __asan_report_load4_noabort(unsigned long addr);
376 void __asan_report_store4_noabort(unsigned long addr);
377 void __asan_report_load8_noabort(unsigned long addr);
378 void __asan_report_store8_noabort(unsigned long addr);
379 void __asan_report_load16_noabort(unsigned long addr);
380 void __asan_report_store16_noabort(unsigned long addr);
381 void __asan_report_load_n_noabort(unsigned long addr, size_t size);
382 void __asan_report_store_n_noabort(unsigned long addr, size_t size);
384 void __asan_set_shadow_00(const void *addr, size_t size);
385 void __asan_set_shadow_f1(const void *addr, size_t size);
386 void __asan_set_shadow_f2(const void *addr, size_t size);
387 void __asan_set_shadow_f3(const void *addr, size_t size);
388 void __asan_set_shadow_f5(const void *addr, size_t size);
389 void __asan_set_shadow_f8(const void *addr, size_t size);
391 void __hwasan_load1_noabort(unsigned long addr);
392 void __hwasan_store1_noabort(unsigned long addr);
393 void __hwasan_load2_noabort(unsigned long addr);
394 void __hwasan_store2_noabort(unsigned long addr);
395 void __hwasan_load4_noabort(unsigned long addr);
396 void __hwasan_store4_noabort(unsigned long addr);
397 void __hwasan_load8_noabort(unsigned long addr);
398 void __hwasan_store8_noabort(unsigned long addr);
399 void __hwasan_load16_noabort(unsigned long addr);
400 void __hwasan_store16_noabort(unsigned long addr);
401 void __hwasan_loadN_noabort(unsigned long addr, size_t size);
402 void __hwasan_storeN_noabort(unsigned long addr, size_t size);
404 void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size);