blob: ac499456740f1f8b9805a991e8e71faa54256e8e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08002#ifndef __MM_KASAN_KASAN_H
3#define __MM_KASAN_KASAN_H
4
5#include <linux/kasan.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -07006#include <linux/stackdepot.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08007
8#define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
9#define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
10
Andrey Konovalov3c9e3aa2018-12-28 00:30:16 -080011#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 */
14
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -080015#ifdef CONFIG_KASAN_GENERIC
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080016#define KASAN_FREE_PAGE 0xFF /* page was freed */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080017#define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */
18#define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */
19#define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */
Walter Wue4b78182020-08-06 23:24:39 -070020#define KASAN_KMALLOC_FREETRACK 0xFA /* object was freed and has free track set */
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -080021#else
22#define KASAN_FREE_PAGE KASAN_TAG_INVALID
23#define KASAN_PAGE_REDZONE KASAN_TAG_INVALID
24#define KASAN_KMALLOC_REDZONE KASAN_TAG_INVALID
25#define KASAN_KMALLOC_FREE KASAN_TAG_INVALID
Walter Wue4b78182020-08-06 23:24:39 -070026#define KASAN_KMALLOC_FREETRACK KASAN_TAG_INVALID
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -080027#endif
28
Walter Wue4b78182020-08-06 23:24:39 -070029#define KASAN_GLOBAL_REDZONE 0xF9 /* redzone for global variable */
30#define KASAN_VMALLOC_INVALID 0xF8 /* unallocated space in vmapped page */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080031
Andrey Ryabininc420f162015-02-13 14:39:59 -080032/*
33 * Stack redzone shadow values
34 * (Those are compiler's ABI, don't change them)
35 */
36#define KASAN_STACK_LEFT 0xF1
37#define KASAN_STACK_MID 0xF2
38#define KASAN_STACK_RIGHT 0xF3
39#define KASAN_STACK_PARTIAL 0xF4
40
Paul Lawrence342061e2018-02-06 15:36:11 -080041/*
42 * alloca redzone shadow values
43 */
44#define KASAN_ALLOCA_LEFT 0xCA
45#define KASAN_ALLOCA_RIGHT 0xCB
46
47#define KASAN_ALLOCA_REDZONE_SIZE 32
48
Marco Elvere8969212019-07-11 20:53:49 -070049/*
50 * Stack frame marker (compiler ABI).
51 */
52#define KASAN_CURRENT_STACK_FRAME_MAGIC 0x41B58AB3
53
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080054/* Don't break randconfig/all*config builds */
55#ifndef KASAN_ABI_VERSION
56#define KASAN_ABI_VERSION 1
57#endif
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080058
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080059struct kasan_access_info {
60 const void *access_addr;
61 const void *first_bad_addr;
62 size_t access_size;
63 bool is_write;
64 unsigned long ip;
65};
66
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080067/* The layout of struct dictated by compiler */
68struct kasan_source_location {
69 const char *filename;
70 int line_no;
71 int column_no;
72};
73
74/* The layout of struct dictated by compiler */
75struct kasan_global {
76 const void *beg; /* Address of the beginning of the global variable. */
77 size_t size; /* Size of the global variable. */
78 size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */
79 const void *name;
80 const void *module_name; /* Name of the module where the global variable is declared. */
81 unsigned long has_dynamic_init; /* This needed for C++ */
82#if KASAN_ABI_VERSION >= 4
83 struct kasan_source_location *location;
84#endif
Dmitry Vyukov045d5992016-11-30 15:54:13 -080085#if KASAN_ABI_VERSION >= 5
86 char *odr_indicator;
87#endif
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080088};
89
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070090/**
91 * Structures to keep alloc and free tracks *
92 */
93
Alexander Potapenkocd110162016-03-25 14:22:08 -070094#define KASAN_STACK_DEPTH 64
95
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070096struct kasan_track {
Alexander Potapenkocd110162016-03-25 14:22:08 -070097 u32 pid;
98 depot_stack_handle_t stack;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070099};
100
Walter Wuae8f06b2019-09-23 15:34:13 -0700101#ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
102#define KASAN_NR_FREE_STACKS 5
103#else
104#define KASAN_NR_FREE_STACKS 1
105#endif
106
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700107struct kasan_alloc_meta {
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700108 struct kasan_track alloc_track;
Walter Wu26e760c2020-08-06 23:24:35 -0700109#ifdef CONFIG_KASAN_GENERIC
110 /*
111 * call_rcu() call stack is stored into struct kasan_alloc_meta.
112 * The free stack is stored into struct kasan_free_meta.
113 */
114 depot_stack_handle_t aux_stack[2];
Walter Wue4b78182020-08-06 23:24:39 -0700115#else
Walter Wuae8f06b2019-09-23 15:34:13 -0700116 struct kasan_track free_track[KASAN_NR_FREE_STACKS];
Walter Wue4b78182020-08-06 23:24:39 -0700117#endif
Walter Wuae8f06b2019-09-23 15:34:13 -0700118#ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
119 u8 free_pointer_tag[KASAN_NR_FREE_STACKS];
120 u8 free_track_idx;
121#endif
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700122};
123
Alexander Potapenko55834c52016-05-20 16:59:11 -0700124struct qlist_node {
125 struct qlist_node *next;
126};
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700127struct kasan_free_meta {
Alexander Potapenko55834c52016-05-20 16:59:11 -0700128 /* This field is used while the object is in the quarantine.
129 * Otherwise it might be used for the allocator freelist.
130 */
131 struct qlist_node quarantine_link;
Walter Wue4b78182020-08-06 23:24:39 -0700132#ifdef CONFIG_KASAN_GENERIC
133 struct kasan_track free_track;
134#endif
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700135};
136
137struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
138 const void *object);
139struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
140 const void *object);
141
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800142static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
143{
144 return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
145 << KASAN_SHADOW_SCALE_SHIFT);
146}
147
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -0800148static inline bool addr_has_shadow(const void *addr)
149{
150 return (addr >= kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
151}
152
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800153void kasan_poison_shadow(const void *address, size_t size, u8 value);
154
Marco Elverb5f6e0f2019-07-11 20:54:07 -0700155/**
156 * check_memory_region - Check memory region, and report if invalid access.
157 * @addr: the accessed address
158 * @size: the accessed size
159 * @write: true if access is a write access
160 * @ret_ip: return address
161 * @return: true if access was valid, false if invalid
162 */
163bool check_memory_region(unsigned long addr, size_t size, bool write,
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800164 unsigned long ret_ip);
165
Andrey Konovalov121e8f82018-12-28 00:30:42 -0800166void *find_first_bad_addr(void *addr, size_t size);
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -0800167const char *get_bug_type(struct kasan_access_info *info);
168
Walter Wu8cceeff2020-04-01 21:09:37 -0700169bool kasan_report(unsigned long addr, size_t size,
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800170 bool is_write, unsigned long ip);
Dmitry Vyukovee3ce772018-02-06 15:36:27 -0800171void kasan_report_invalid_free(void *object, unsigned long ip);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800172
Walter Wuae8f06b2019-09-23 15:34:13 -0700173struct page *kasan_addr_to_page(const void *addr);
174
Walter Wu26e760c2020-08-06 23:24:35 -0700175depot_stack_handle_t kasan_save_stack(gfp_t flags);
Walter Wue4b78182020-08-06 23:24:39 -0700176void kasan_set_track(struct kasan_track *track, gfp_t flags);
177void kasan_set_free_info(struct kmem_cache *cache, void *object, u8 tag);
178struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
179 void *object, u8 tag);
Walter Wu26e760c2020-08-06 23:24:35 -0700180
Andrey Konovalov2bd926b2018-12-28 00:29:53 -0800181#if defined(CONFIG_KASAN_GENERIC) && \
182 (defined(CONFIG_SLAB) || defined(CONFIG_SLUB))
Alexander Potapenko55834c52016-05-20 16:59:11 -0700183void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
184void quarantine_reduce(void);
185void quarantine_remove_cache(struct kmem_cache *cache);
186#else
187static inline void quarantine_put(struct kasan_free_meta *info,
188 struct kmem_cache *cache) { }
189static inline void quarantine_reduce(void) { }
190static inline void quarantine_remove_cache(struct kmem_cache *cache) { }
191#endif
192
Andrey Konovalov3c9e3aa2018-12-28 00:30:16 -0800193#ifdef CONFIG_KASAN_SW_TAGS
194
Andrey Konovalov121e8f82018-12-28 00:30:42 -0800195void print_tags(u8 addr_tag, const void *addr);
196
Andrey Konovalov3c9e3aa2018-12-28 00:30:16 -0800197u8 random_tag(void);
198
199#else
200
Andrey Konovalov121e8f82018-12-28 00:30:42 -0800201static inline void print_tags(u8 addr_tag, const void *addr) { }
202
Andrey Konovalov3c9e3aa2018-12-28 00:30:16 -0800203static inline u8 random_tag(void)
204{
205 return 0;
206}
207
208#endif
209
210#ifndef arch_kasan_set_tag
Qian Caic412a762019-03-28 20:43:15 -0700211static inline const void *arch_kasan_set_tag(const void *addr, u8 tag)
212{
213 return addr;
214}
Andrey Konovalov3c9e3aa2018-12-28 00:30:16 -0800215#endif
216#ifndef arch_kasan_reset_tag
217#define arch_kasan_reset_tag(addr) ((void *)(addr))
218#endif
219#ifndef arch_kasan_get_tag
220#define arch_kasan_get_tag(addr) 0
221#endif
222
223#define set_tag(addr, tag) ((void *)arch_kasan_set_tag((addr), (tag)))
224#define reset_tag(addr) ((void *)arch_kasan_reset_tag(addr))
225#define get_tag(addr) arch_kasan_get_tag(addr)
226
Alexander Potapenkod3215992018-02-06 15:36:20 -0800227/*
228 * Exported functions for interfaces called from assembly or from generated
229 * code. Declarations here to avoid warning about missing declarations.
230 */
231asmlinkage void kasan_unpoison_task_stack_below(const void *watermark);
232void __asan_register_globals(struct kasan_global *globals, size_t size);
233void __asan_unregister_globals(struct kasan_global *globals, size_t size);
Alexander Potapenkod3215992018-02-06 15:36:20 -0800234void __asan_handle_no_return(void);
Alexander Potapenkod3215992018-02-06 15:36:20 -0800235void __asan_alloca_poison(unsigned long addr, size_t size);
236void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom);
237
238void __asan_load1(unsigned long addr);
239void __asan_store1(unsigned long addr);
240void __asan_load2(unsigned long addr);
241void __asan_store2(unsigned long addr);
242void __asan_load4(unsigned long addr);
243void __asan_store4(unsigned long addr);
244void __asan_load8(unsigned long addr);
245void __asan_store8(unsigned long addr);
246void __asan_load16(unsigned long addr);
247void __asan_store16(unsigned long addr);
Andrey Konovalov13cf0482020-05-13 17:50:54 -0700248void __asan_loadN(unsigned long addr, size_t size);
249void __asan_storeN(unsigned long addr, size_t size);
Alexander Potapenkod3215992018-02-06 15:36:20 -0800250
251void __asan_load1_noabort(unsigned long addr);
252void __asan_store1_noabort(unsigned long addr);
253void __asan_load2_noabort(unsigned long addr);
254void __asan_store2_noabort(unsigned long addr);
255void __asan_load4_noabort(unsigned long addr);
256void __asan_store4_noabort(unsigned long addr);
257void __asan_load8_noabort(unsigned long addr);
258void __asan_store8_noabort(unsigned long addr);
259void __asan_load16_noabort(unsigned long addr);
260void __asan_store16_noabort(unsigned long addr);
Andrey Konovalov13cf0482020-05-13 17:50:54 -0700261void __asan_loadN_noabort(unsigned long addr, size_t size);
262void __asan_storeN_noabort(unsigned long addr, size_t size);
263
264void __asan_report_load1_noabort(unsigned long addr);
265void __asan_report_store1_noabort(unsigned long addr);
266void __asan_report_load2_noabort(unsigned long addr);
267void __asan_report_store2_noabort(unsigned long addr);
268void __asan_report_load4_noabort(unsigned long addr);
269void __asan_report_store4_noabort(unsigned long addr);
270void __asan_report_load8_noabort(unsigned long addr);
271void __asan_report_store8_noabort(unsigned long addr);
272void __asan_report_load16_noabort(unsigned long addr);
273void __asan_report_store16_noabort(unsigned long addr);
274void __asan_report_load_n_noabort(unsigned long addr, size_t size);
275void __asan_report_store_n_noabort(unsigned long addr, size_t size);
Alexander Potapenkod3215992018-02-06 15:36:20 -0800276
277void __asan_set_shadow_00(const void *addr, size_t size);
278void __asan_set_shadow_f1(const void *addr, size_t size);
279void __asan_set_shadow_f2(const void *addr, size_t size);
280void __asan_set_shadow_f3(const void *addr, size_t size);
281void __asan_set_shadow_f5(const void *addr, size_t size);
282void __asan_set_shadow_f8(const void *addr, size_t size);
283
Andrey Konovalov13cf0482020-05-13 17:50:54 -0700284void __hwasan_load1_noabort(unsigned long addr);
285void __hwasan_store1_noabort(unsigned long addr);
286void __hwasan_load2_noabort(unsigned long addr);
287void __hwasan_store2_noabort(unsigned long addr);
288void __hwasan_load4_noabort(unsigned long addr);
289void __hwasan_store4_noabort(unsigned long addr);
290void __hwasan_load8_noabort(unsigned long addr);
291void __hwasan_store8_noabort(unsigned long addr);
292void __hwasan_load16_noabort(unsigned long addr);
293void __hwasan_store16_noabort(unsigned long addr);
294void __hwasan_loadN_noabort(unsigned long addr, size_t size);
295void __hwasan_storeN_noabort(unsigned long addr, size_t size);
296
297void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size);
298
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800299#endif