e638255ce906021b62cba43f73f9db294c9b2552
[platform/kernel/linux-rpi.git] / include / linux / kasan.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KASAN_H
3 #define _LINUX_KASAN_H
4
5 #include <linux/types.h>
6
7 struct kmem_cache;
8 struct page;
9 struct vm_struct;
10 struct task_struct;
11
12 #ifdef CONFIG_KASAN
13
14 #include <linux/linkage.h>
15 #include <asm/kasan.h>
16
17 /* kasan_data struct is used in KUnit tests for KASAN expected failures */
18 struct kunit_kasan_expectation {
19         bool report_expected;
20         bool report_found;
21 };
22
23 #endif
24
25 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
26
27 #include <linux/pgtable.h>
28
29 /* Software KASAN implementations use shadow memory. */
30
31 #ifdef CONFIG_KASAN_SW_TAGS
32 #define KASAN_SHADOW_INIT 0xFF
33 #else
34 #define KASAN_SHADOW_INIT 0
35 #endif
36
37 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
38 extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
39 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
40 extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
41 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
42
43 int kasan_populate_early_shadow(const void *shadow_start,
44                                 const void *shadow_end);
45
46 static inline void *kasan_mem_to_shadow(const void *addr)
47 {
48         return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
49                 + KASAN_SHADOW_OFFSET;
50 }
51
52 int kasan_add_zero_shadow(void *start, unsigned long size);
53 void kasan_remove_zero_shadow(void *start, unsigned long size);
54
55 /* Enable reporting bugs after kasan_disable_current() */
56 extern void kasan_enable_current(void);
57
58 /* Disable reporting bugs for current task */
59 extern void kasan_disable_current(void);
60
61 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
62
63 static inline int kasan_add_zero_shadow(void *start, unsigned long size)
64 {
65         return 0;
66 }
67 static inline void kasan_remove_zero_shadow(void *start,
68                                         unsigned long size)
69 {}
70
71 static inline void kasan_enable_current(void) {}
72 static inline void kasan_disable_current(void) {}
73
74 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
75
76 #ifdef CONFIG_KASAN
77
78 void kasan_unpoison_range(const void *address, size_t size);
79
80 void kasan_alloc_pages(struct page *page, unsigned int order);
81 void kasan_free_pages(struct page *page, unsigned int order);
82
83 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
84                         slab_flags_t *flags);
85
86 void kasan_poison_slab(struct page *page);
87 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
88 void kasan_poison_object_data(struct kmem_cache *cache, void *object);
89 void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
90                                         const void *object);
91
92 void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
93                                                 gfp_t flags);
94 void kasan_kfree_large(void *ptr, unsigned long ip);
95 void kasan_poison_kfree(void *ptr, unsigned long ip);
96 void * __must_check kasan_kmalloc(struct kmem_cache *s, const void *object,
97                                         size_t size, gfp_t flags);
98 void * __must_check kasan_krealloc(const void *object, size_t new_size,
99                                         gfp_t flags);
100
101 void * __must_check kasan_slab_alloc(struct kmem_cache *s, void *object,
102                                         gfp_t flags);
103 bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
104
105 struct kasan_cache {
106         int alloc_meta_offset;
107         int free_meta_offset;
108 };
109
110 size_t __ksize(const void *);
111 static inline void kasan_unpoison_slab(const void *ptr)
112 {
113         kasan_unpoison_range(ptr, __ksize(ptr));
114 }
115 size_t kasan_metadata_size(struct kmem_cache *cache);
116
117 bool kasan_save_enable_multi_shot(void);
118 void kasan_restore_multi_shot(bool enabled);
119
120 #else /* CONFIG_KASAN */
121
122 static inline void kasan_unpoison_range(const void *address, size_t size) {}
123
124 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
125 static inline void kasan_free_pages(struct page *page, unsigned int order) {}
126
127 static inline void kasan_cache_create(struct kmem_cache *cache,
128                                       unsigned int *size,
129                                       slab_flags_t *flags) {}
130
131 static inline void kasan_poison_slab(struct page *page) {}
132 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
133                                         void *object) {}
134 static inline void kasan_poison_object_data(struct kmem_cache *cache,
135                                         void *object) {}
136 static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
137                                 const void *object)
138 {
139         return (void *)object;
140 }
141
142 static inline void *kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags)
143 {
144         return ptr;
145 }
146 static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
147 static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {}
148 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
149                                 size_t size, gfp_t flags)
150 {
151         return (void *)object;
152 }
153 static inline void *kasan_krealloc(const void *object, size_t new_size,
154                                  gfp_t flags)
155 {
156         return (void *)object;
157 }
158
159 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
160                                    gfp_t flags)
161 {
162         return object;
163 }
164 static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
165                                    unsigned long ip)
166 {
167         return false;
168 }
169
170 static inline void kasan_unpoison_slab(const void *ptr) { }
171 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
172
173 #endif /* CONFIG_KASAN */
174
175 #if defined(CONFIG_KASAN) && CONFIG_KASAN_STACK
176 void kasan_unpoison_task_stack(struct task_struct *task);
177 #else
178 static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
179 #endif
180
181 #ifdef CONFIG_KASAN_GENERIC
182
183 void kasan_cache_shrink(struct kmem_cache *cache);
184 void kasan_cache_shutdown(struct kmem_cache *cache);
185 void kasan_record_aux_stack(void *ptr);
186
187 #else /* CONFIG_KASAN_GENERIC */
188
189 static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
190 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
191 static inline void kasan_record_aux_stack(void *ptr) {}
192
193 #endif /* CONFIG_KASAN_GENERIC */
194
195 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
196
197 void *kasan_reset_tag(const void *addr);
198
199 bool kasan_report(unsigned long addr, size_t size,
200                 bool is_write, unsigned long ip);
201
202 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
203
204 static inline void *kasan_reset_tag(const void *addr)
205 {
206         return (void *)addr;
207 }
208
209 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/
210
211 #ifdef CONFIG_KASAN_SW_TAGS
212 void __init kasan_init_sw_tags(void);
213 #else
214 static inline void kasan_init_sw_tags(void) { }
215 #endif
216
217 #ifdef CONFIG_KASAN_HW_TAGS
218 void kasan_init_hw_tags_cpu(void);
219 void __init kasan_init_hw_tags(void);
220 #else
221 static inline void kasan_init_hw_tags_cpu(void) { }
222 static inline void kasan_init_hw_tags(void) { }
223 #endif
224
225 #ifdef CONFIG_KASAN_VMALLOC
226
227 int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
228 void kasan_poison_vmalloc(const void *start, unsigned long size);
229 void kasan_unpoison_vmalloc(const void *start, unsigned long size);
230 void kasan_release_vmalloc(unsigned long start, unsigned long end,
231                            unsigned long free_region_start,
232                            unsigned long free_region_end);
233
234 #else /* CONFIG_KASAN_VMALLOC */
235
236 static inline int kasan_populate_vmalloc(unsigned long start,
237                                         unsigned long size)
238 {
239         return 0;
240 }
241
242 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
243 { }
244 static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
245 { }
246 static inline void kasan_release_vmalloc(unsigned long start,
247                                          unsigned long end,
248                                          unsigned long free_region_start,
249                                          unsigned long free_region_end) {}
250
251 #endif /* CONFIG_KASAN_VMALLOC */
252
253 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
254                 !defined(CONFIG_KASAN_VMALLOC)
255
256 /*
257  * These functions provide a special case to support backing module
258  * allocations with real shadow memory. With KASAN vmalloc, the special
259  * case is unnecessary, as the work is handled in the generic case.
260  */
261 int kasan_module_alloc(void *addr, size_t size);
262 void kasan_free_shadow(const struct vm_struct *vm);
263
264 #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
265
266 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
267 static inline void kasan_free_shadow(const struct vm_struct *vm) {}
268
269 #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
270
271 #ifdef CONFIG_KASAN_INLINE
272 void kasan_non_canonical_hook(unsigned long addr);
273 #else /* CONFIG_KASAN_INLINE */
274 static inline void kasan_non_canonical_hook(unsigned long addr) { }
275 #endif /* CONFIG_KASAN_INLINE */
276
277 #endif /* LINUX_KASAN_H */