blob: d9cf9e2cc78222c9bba01b6da1c2e1d1b420f281 [file] [log] [blame]
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08001#ifndef __MM_KASAN_KASAN_H
2#define __MM_KASAN_KASAN_H
3
4#include <linux/kasan.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -07005#include <linux/stackdepot.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08006
7#define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
8#define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
9
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080010#define KASAN_FREE_PAGE 0xFF /* page was freed */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080011#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) */
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080014#define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080015
Andrey Ryabininc420f162015-02-13 14:39:59 -080016/*
17 * Stack redzone shadow values
18 * (Those are compiler's ABI, don't change them)
19 */
20#define KASAN_STACK_LEFT 0xF1
21#define KASAN_STACK_MID 0xF2
22#define KASAN_STACK_RIGHT 0xF3
23#define KASAN_STACK_PARTIAL 0xF4
Dmitry Vyukov828347f2016-11-30 15:54:16 -080024#define KASAN_USE_AFTER_SCOPE 0xF8
Andrey Ryabininc420f162015-02-13 14:39:59 -080025
Paul Lawrence9351dcd2018-02-06 15:36:11 -080026/*
27 * alloca redzone shadow values
28 */
29#define KASAN_ALLOCA_LEFT 0xCA
30#define KASAN_ALLOCA_RIGHT 0xCB
31
32#define KASAN_ALLOCA_REDZONE_SIZE 32
33
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080034/* Don't break randconfig/all*config builds */
35#ifndef KASAN_ABI_VERSION
36#define KASAN_ABI_VERSION 1
37#endif
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080038
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080039struct kasan_access_info {
40 const void *access_addr;
41 const void *first_bad_addr;
42 size_t access_size;
43 bool is_write;
44 unsigned long ip;
45};
46
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080047/* The layout of struct dictated by compiler */
48struct kasan_source_location {
49 const char *filename;
50 int line_no;
51 int column_no;
52};
53
54/* The layout of struct dictated by compiler */
55struct kasan_global {
56 const void *beg; /* Address of the beginning of the global variable. */
57 size_t size; /* Size of the global variable. */
58 size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */
59 const void *name;
60 const void *module_name; /* Name of the module where the global variable is declared. */
61 unsigned long has_dynamic_init; /* This needed for C++ */
62#if KASAN_ABI_VERSION >= 4
63 struct kasan_source_location *location;
64#endif
Dmitry Vyukov045d5992016-11-30 15:54:13 -080065#if KASAN_ABI_VERSION >= 5
66 char *odr_indicator;
67#endif
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080068};
69
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070070/**
71 * Structures to keep alloc and free tracks *
72 */
73
Alexander Potapenkocd110162016-03-25 14:22:08 -070074#define KASAN_STACK_DEPTH 64
75
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070076struct kasan_track {
Alexander Potapenkocd110162016-03-25 14:22:08 -070077 u32 pid;
78 depot_stack_handle_t stack;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070079};
80
81struct kasan_alloc_meta {
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -070082 struct kasan_track alloc_track;
83 struct kasan_track free_track;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070084};
85
Alexander Potapenko55834c52016-05-20 16:59:11 -070086struct qlist_node {
87 struct qlist_node *next;
88};
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070089struct kasan_free_meta {
Alexander Potapenko55834c52016-05-20 16:59:11 -070090 /* This field is used while the object is in the quarantine.
91 * Otherwise it might be used for the allocator freelist.
92 */
93 struct qlist_node quarantine_link;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070094};
95
96struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
97 const void *object);
98struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
99 const void *object);
100
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800101static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
102{
103 return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
104 << KASAN_SHADOW_SCALE_SHIFT);
105}
106
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800107void kasan_report(unsigned long addr, size_t size,
108 bool is_write, unsigned long ip);
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700109void kasan_report_double_free(struct kmem_cache *cache, void *object,
Andrey Konovalovbef4eb32017-05-03 14:56:47 -0700110 void *ip);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800111
Alexander Potapenko80a92012016-07-28 15:49:07 -0700112#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700113void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
114void quarantine_reduce(void);
115void quarantine_remove_cache(struct kmem_cache *cache);
116#else
117static inline void quarantine_put(struct kasan_free_meta *info,
118 struct kmem_cache *cache) { }
119static inline void quarantine_reduce(void) { }
120static inline void quarantine_remove_cache(struct kmem_cache *cache) { }
121#endif
122
Alexander Potapenkod6f27742018-02-06 15:36:20 -0800123/*
124 * Exported functions for interfaces called from assembly or from generated
125 * code. Declarations here to avoid warning about missing declarations.
126 */
127asmlinkage void kasan_unpoison_task_stack_below(const void *watermark);
128void __asan_register_globals(struct kasan_global *globals, size_t size);
129void __asan_unregister_globals(struct kasan_global *globals, size_t size);
130void __asan_loadN(unsigned long addr, size_t size);
131void __asan_storeN(unsigned long addr, size_t size);
132void __asan_handle_no_return(void);
133void __asan_poison_stack_memory(const void *addr, size_t size);
134void __asan_unpoison_stack_memory(const void *addr, size_t size);
135void __asan_alloca_poison(unsigned long addr, size_t size);
136void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom);
137
138void __asan_load1(unsigned long addr);
139void __asan_store1(unsigned long addr);
140void __asan_load2(unsigned long addr);
141void __asan_store2(unsigned long addr);
142void __asan_load4(unsigned long addr);
143void __asan_store4(unsigned long addr);
144void __asan_load8(unsigned long addr);
145void __asan_store8(unsigned long addr);
146void __asan_load16(unsigned long addr);
147void __asan_store16(unsigned long addr);
148
149void __asan_load1_noabort(unsigned long addr);
150void __asan_store1_noabort(unsigned long addr);
151void __asan_load2_noabort(unsigned long addr);
152void __asan_store2_noabort(unsigned long addr);
153void __asan_load4_noabort(unsigned long addr);
154void __asan_store4_noabort(unsigned long addr);
155void __asan_load8_noabort(unsigned long addr);
156void __asan_store8_noabort(unsigned long addr);
157void __asan_load16_noabort(unsigned long addr);
158void __asan_store16_noabort(unsigned long addr);
159
160void __asan_set_shadow_00(const void *addr, size_t size);
161void __asan_set_shadow_f1(const void *addr, size_t size);
162void __asan_set_shadow_f2(const void *addr, size_t size);
163void __asan_set_shadow_f3(const void *addr, size_t size);
164void __asan_set_shadow_f5(const void *addr, size_t size);
165void __asan_set_shadow_f8(const void *addr, size_t size);
166
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800167#endif