1 #ifndef __MM_KASAN_KASAN_H
2 #define __MM_KASAN_KASAN_H
4 #include <linux/kasan.h>
5 #include <linux/stackdepot.h>
7 #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
8 #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
10 #define KASAN_FREE_PAGE 0xFF /* page was freed */
11 #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */
12 #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */
13 #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */
14 #define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */
17 * Stack redzone shadow values
18 * (Those are compiler's ABI, don't change them)
20 #define KASAN_STACK_LEFT 0xF1
21 #define KASAN_STACK_MID 0xF2
22 #define KASAN_STACK_RIGHT 0xF3
23 #define KASAN_STACK_PARTIAL 0xF4
25 /* Don't break randconfig/all*config builds */
26 #ifndef KASAN_ABI_VERSION
27 #define KASAN_ABI_VERSION 1
30 struct kasan_access_info
{
31 const void *access_addr
;
32 const void *first_bad_addr
;
38 /* The layout of struct dictated by compiler */
39 struct kasan_source_location
{
45 /* The layout of struct dictated by compiler */
47 const void *beg
; /* Address of the beginning of the global variable. */
48 size_t size
; /* Size of the global variable. */
49 size_t size_with_redzone
; /* Size of the variable + size of the red zone. 32 bytes aligned */
51 const void *module_name
; /* Name of the module where the global variable is declared. */
52 unsigned long has_dynamic_init
; /* This needed for C++ */
53 #if KASAN_ABI_VERSION >= 4
54 struct kasan_source_location
*location
;
59 * Structures to keep alloc and free tracks *
65 KASAN_STATE_QUARANTINE
,
69 #define KASAN_STACK_DEPTH 64
73 depot_stack_handle_t stack
;
76 struct kasan_alloc_meta
{
77 struct kasan_track track
;
78 u32 state
: 2; /* enum kasan_state */
83 struct qlist_node
*next
;
85 struct kasan_free_meta
{
86 /* This field is used while the object is in the quarantine.
87 * Otherwise it might be used for the allocator freelist.
89 struct qlist_node quarantine_link
;
90 struct kasan_track track
;
93 struct kasan_alloc_meta
*get_alloc_info(struct kmem_cache
*cache
,
95 struct kasan_free_meta
*get_free_info(struct kmem_cache
*cache
,
99 static inline const void *kasan_shadow_to_mem(const void *shadow_addr
)
101 return (void *)(((unsigned long)shadow_addr
- KASAN_SHADOW_OFFSET
)
102 << KASAN_SHADOW_SCALE_SHIFT
);
105 static inline bool kasan_report_enabled(void)
107 return !current
->kasan_depth
;
110 void kasan_report(unsigned long addr
, size_t size
,
111 bool is_write
, unsigned long ip
);
114 void quarantine_put(struct kasan_free_meta
*info
, struct kmem_cache
*cache
);
115 void quarantine_reduce(void);
116 void quarantine_remove_cache(struct kmem_cache
*cache
);
118 static inline void quarantine_put(struct kasan_free_meta
*info
,
119 struct kmem_cache
*cache
) { }
120 static inline void quarantine_reduce(void) { }
121 static inline void quarantine_remove_cache(struct kmem_cache
*cache
) { }