]> Git Repo - linux.git/blob - include/linux/kasan.h
kasan: rename (un)poison_shadow to (un)poison_range
[linux.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 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
56
57 static inline int kasan_add_zero_shadow(void *start, unsigned long size)
58 {
59         return 0;
60 }
61 static inline void kasan_remove_zero_shadow(void *start,
62                                         unsigned long size)
63 {}
64
65 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
66
67 #ifdef CONFIG_KASAN
68
69 /* Enable reporting bugs after kasan_disable_current() */
70 extern void kasan_enable_current(void);
71
72 /* Disable reporting bugs for current task */
73 extern void kasan_disable_current(void);
74
75 void kasan_unpoison_range(const void *address, size_t size);
76
77 void kasan_unpoison_task_stack(struct task_struct *task);
78
79 void kasan_alloc_pages(struct page *page, unsigned int order);
80 void kasan_free_pages(struct page *page, unsigned int order);
81
82 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
83                         slab_flags_t *flags);
84
85 void kasan_poison_slab(struct page *page);
86 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
87 void kasan_poison_object_data(struct kmem_cache *cache, void *object);
88 void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
89                                         const void *object);
90
91 void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
92                                                 gfp_t flags);
93 void kasan_kfree_large(void *ptr, unsigned long ip);
94 void kasan_poison_kfree(void *ptr, unsigned long ip);
95 void * __must_check kasan_kmalloc(struct kmem_cache *s, const void *object,
96                                         size_t size, gfp_t flags);
97 void * __must_check kasan_krealloc(const void *object, size_t new_size,
98                                         gfp_t flags);
99
100 void * __must_check kasan_slab_alloc(struct kmem_cache *s, void *object,
101                                         gfp_t flags);
102 bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
103
104 struct kasan_cache {
105         int alloc_meta_offset;
106         int free_meta_offset;
107 };
108
109 size_t __ksize(const void *);
110 static inline void kasan_unpoison_slab(const void *ptr)
111 {
112         kasan_unpoison_range(ptr, __ksize(ptr));
113 }
114 size_t kasan_metadata_size(struct kmem_cache *cache);
115
116 bool kasan_save_enable_multi_shot(void);
117 void kasan_restore_multi_shot(bool enabled);
118
119 #else /* CONFIG_KASAN */
120
121 static inline void kasan_unpoison_range(const void *address, size_t size) {}
122
123 static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
124
125 static inline void kasan_enable_current(void) {}
126 static inline void kasan_disable_current(void) {}
127
128 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
129 static inline void kasan_free_pages(struct page *page, unsigned int order) {}
130
131 static inline void kasan_cache_create(struct kmem_cache *cache,
132                                       unsigned int *size,
133                                       slab_flags_t *flags) {}
134
135 static inline void kasan_poison_slab(struct page *page) {}
136 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
137                                         void *object) {}
138 static inline void kasan_poison_object_data(struct kmem_cache *cache,
139                                         void *object) {}
140 static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
141                                 const void *object)
142 {
143         return (void *)object;
144 }
145
146 static inline void *kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags)
147 {
148         return ptr;
149 }
150 static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
151 static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {}
152 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
153                                 size_t size, gfp_t flags)
154 {
155         return (void *)object;
156 }
157 static inline void *kasan_krealloc(const void *object, size_t new_size,
158                                  gfp_t flags)
159 {
160         return (void *)object;
161 }
162
163 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
164                                    gfp_t flags)
165 {
166         return object;
167 }
168 static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
169                                    unsigned long ip)
170 {
171         return false;
172 }
173
174 static inline void kasan_unpoison_slab(const void *ptr) { }
175 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
176
177 #endif /* CONFIG_KASAN */
178
179 #ifdef CONFIG_KASAN_GENERIC
180
181 void kasan_cache_shrink(struct kmem_cache *cache);
182 void kasan_cache_shutdown(struct kmem_cache *cache);
183 void kasan_record_aux_stack(void *ptr);
184
185 #else /* CONFIG_KASAN_GENERIC */
186
187 static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
188 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
189 static inline void kasan_record_aux_stack(void *ptr) {}
190
191 #endif /* CONFIG_KASAN_GENERIC */
192
193 #ifdef CONFIG_KASAN_SW_TAGS
194
195 void kasan_init_tags(void);
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 */
203
204 static inline void kasan_init_tags(void) { }
205
206 static inline void *kasan_reset_tag(const void *addr)
207 {
208         return (void *)addr;
209 }
210
211 #endif /* CONFIG_KASAN_SW_TAGS */
212
213 #ifdef CONFIG_KASAN_VMALLOC
214
215 int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
216 void kasan_poison_vmalloc(const void *start, unsigned long size);
217 void kasan_unpoison_vmalloc(const void *start, unsigned long size);
218 void kasan_release_vmalloc(unsigned long start, unsigned long end,
219                            unsigned long free_region_start,
220                            unsigned long free_region_end);
221
222 #else /* CONFIG_KASAN_VMALLOC */
223
224 static inline int kasan_populate_vmalloc(unsigned long start,
225                                         unsigned long size)
226 {
227         return 0;
228 }
229
230 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
231 { }
232 static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
233 { }
234 static inline void kasan_release_vmalloc(unsigned long start,
235                                          unsigned long end,
236                                          unsigned long free_region_start,
237                                          unsigned long free_region_end) {}
238
239 #endif /* CONFIG_KASAN_VMALLOC */
240
241 #if defined(CONFIG_KASAN) && !defined(CONFIG_KASAN_VMALLOC)
242
243 /*
244  * These functions provide a special case to support backing module
245  * allocations with real shadow memory. With KASAN vmalloc, the special
246  * case is unnecessary, as the work is handled in the generic case.
247  */
248 int kasan_module_alloc(void *addr, size_t size);
249 void kasan_free_shadow(const struct vm_struct *vm);
250
251 #else /* CONFIG_KASAN && !CONFIG_KASAN_VMALLOC */
252
253 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
254 static inline void kasan_free_shadow(const struct vm_struct *vm) {}
255
256 #endif /* CONFIG_KASAN && !CONFIG_KASAN_VMALLOC */
257
258 #ifdef CONFIG_KASAN_INLINE
259 void kasan_non_canonical_hook(unsigned long addr);
260 #else /* CONFIG_KASAN_INLINE */
261 static inline void kasan_non_canonical_hook(unsigned long addr) { }
262 #endif /* CONFIG_KASAN_INLINE */
263
264 #endif /* LINUX_KASAN_H */
This page took 0.04757 seconds and 4 git commands to generate.