blob: 82522e996c76067eece294b38623fa32f81d6c92 [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 _LINUX_KASAN_H
3#define _LINUX_KASAN_H
4
5#include <linux/types.h>
6
7struct kmem_cache;
8struct page;
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -07009struct vm_struct;
Masami Hiramatsu5be9b732017-03-16 16:40:21 -070010struct task_struct;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080011
12#ifdef CONFIG_KASAN
13
Mike Rapoportca5999f2020-06-08 21:32:38 -070014#include <linux/pgtable.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070015#include <asm/kasan.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080016
Andrey Konovalov9577dd72018-12-28 00:30:01 -080017extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
18extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
19extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
20extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
21extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +030022
Andrey Konovalov9577dd72018-12-28 00:30:01 -080023int kasan_populate_early_shadow(const void *shadow_start,
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +030024 const void *shadow_end);
25
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080026static inline void *kasan_mem_to_shadow(const void *addr)
27{
28 return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
29 + KASAN_SHADOW_OFFSET;
30}
31
32/* Enable reporting bugs after kasan_disable_current() */
Ingo Molnaraf8601a2017-02-03 09:57:00 +010033extern void kasan_enable_current(void);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080034
35/* Disable reporting bugs for current task */
Ingo Molnaraf8601a2017-02-03 09:57:00 +010036extern void kasan_disable_current(void);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080037
38void kasan_unpoison_shadow(const void *address, size_t size);
39
Mark Rutlande3ae1162016-03-09 14:08:15 -080040void kasan_unpoison_task_stack(struct task_struct *task);
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +020041void kasan_unpoison_stack_above_sp_to(const void *watermark);
Mark Rutlande3ae1162016-03-09 14:08:15 -080042
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080043void kasan_alloc_pages(struct page *page, unsigned int order);
44void kasan_free_pages(struct page *page, unsigned int order);
45
Alexey Dobriyanbe4a7982018-04-05 16:21:28 -070046void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
Alexey Dobriyand50112e2017-11-15 17:32:18 -080047 slab_flags_t *flags);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070048
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080049void kasan_poison_slab(struct page *page);
50void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
51void kasan_poison_object_data(struct kmem_cache *cache, void *object);
Andrey Konovalov66afc7f2018-12-28 00:31:01 -080052void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
53 const void *object);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080054
Andrey Konovalov66afc7f2018-12-28 00:31:01 -080055void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
56 gfp_t flags);
Dmitry Vyukovee3ce772018-02-06 15:36:27 -080057void kasan_kfree_large(void *ptr, unsigned long ip);
Dmitry Vyukov6860f632018-02-06 15:36:30 -080058void kasan_poison_kfree(void *ptr, unsigned long ip);
Andrey Konovalov66afc7f2018-12-28 00:31:01 -080059void * __must_check kasan_kmalloc(struct kmem_cache *s, const void *object,
60 size_t size, gfp_t flags);
61void * __must_check kasan_krealloc(const void *object, size_t new_size,
62 gfp_t flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080063
Andrey Konovalov66afc7f2018-12-28 00:31:01 -080064void * __must_check kasan_slab_alloc(struct kmem_cache *s, void *object,
65 gfp_t flags);
Dmitry Vyukovee3ce772018-02-06 15:36:27 -080066bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080067
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070068struct kasan_cache {
69 int alloc_meta_offset;
70 int free_meta_offset;
71};
72
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -080073/*
74 * These functions provide a special case to support backing module
75 * allocations with real shadow memory. With KASAN vmalloc, the special
76 * case is unnecessary, as the work is handled in the generic case.
77 */
78#ifndef CONFIG_KASAN_VMALLOC
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080079int kasan_module_alloc(void *addr, size_t size);
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -070080void kasan_free_shadow(const struct vm_struct *vm);
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -080081#else
82static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
83static inline void kasan_free_shadow(const struct vm_struct *vm) {}
84#endif
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080085
Andrey Ryabinin0207df42018-08-17 15:47:04 -070086int kasan_add_zero_shadow(void *start, unsigned long size);
87void kasan_remove_zero_shadow(void *start, unsigned long size);
88
Marco Elver0d4ca4c2019-07-11 20:54:18 -070089size_t __ksize(const void *);
90static inline void kasan_unpoison_slab(const void *ptr)
91{
92 kasan_unpoison_shadow(ptr, __ksize(ptr));
93}
Alexander Potapenko80a92012016-07-28 15:49:07 -070094size_t kasan_metadata_size(struct kmem_cache *cache);
Andrey Ryabinin9b75a862016-06-24 14:49:34 -070095
Mark Rutlandb0845ce2017-03-31 15:12:04 -070096bool kasan_save_enable_multi_shot(void);
97void kasan_restore_multi_shot(bool enabled);
98
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080099#else /* CONFIG_KASAN */
100
101static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
102
Mark Rutlande3ae1162016-03-09 14:08:15 -0800103static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +0200104static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {}
Mark Rutlande3ae1162016-03-09 14:08:15 -0800105
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800106static inline void kasan_enable_current(void) {}
107static inline void kasan_disable_current(void) {}
108
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800109static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
110static inline void kasan_free_pages(struct page *page, unsigned int order) {}
111
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700112static inline void kasan_cache_create(struct kmem_cache *cache,
Alexey Dobriyanbe4a7982018-04-05 16:21:28 -0700113 unsigned int *size,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800114 slab_flags_t *flags) {}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700115
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800116static inline void kasan_poison_slab(struct page *page) {}
117static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
118 void *object) {}
119static inline void kasan_poison_object_data(struct kmem_cache *cache,
120 void *object) {}
Andrey Konovalov01165232018-12-28 00:29:37 -0800121static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
122 const void *object)
123{
124 return (void *)object;
125}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800126
Andrey Konovalov01165232018-12-28 00:29:37 -0800127static inline void *kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags)
128{
129 return ptr;
130}
Dmitry Vyukovee3ce772018-02-06 15:36:27 -0800131static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
Dmitry Vyukov6860f632018-02-06 15:36:30 -0800132static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {}
Andrey Konovalov01165232018-12-28 00:29:37 -0800133static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
134 size_t size, gfp_t flags)
135{
136 return (void *)object;
137}
138static inline void *kasan_krealloc(const void *object, size_t new_size,
139 gfp_t flags)
140{
141 return (void *)object;
142}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800143
Andrey Konovalov01165232018-12-28 00:29:37 -0800144static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
145 gfp_t flags)
146{
147 return object;
148}
Dmitry Vyukovee3ce772018-02-06 15:36:27 -0800149static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
150 unsigned long ip)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700151{
152 return false;
153}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800154
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800155static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700156static inline void kasan_free_shadow(const struct vm_struct *vm) {}
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800157
Andrey Ryabinin0207df42018-08-17 15:47:04 -0700158static inline int kasan_add_zero_shadow(void *start, unsigned long size)
159{
160 return 0;
161}
162static inline void kasan_remove_zero_shadow(void *start,
163 unsigned long size)
164{}
165
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700166static inline void kasan_unpoison_slab(const void *ptr) { }
Alexander Potapenko80a92012016-07-28 15:49:07 -0700167static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700168
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800169#endif /* CONFIG_KASAN */
170
Andrey Konovalov2bd926b2018-12-28 00:29:53 -0800171#ifdef CONFIG_KASAN_GENERIC
172
Andrey Konovalov080eb832018-12-28 00:30:09 -0800173#define KASAN_SHADOW_INIT 0
174
Andrey Konovalov2bd926b2018-12-28 00:29:53 -0800175void kasan_cache_shrink(struct kmem_cache *cache);
176void kasan_cache_shutdown(struct kmem_cache *cache);
177
178#else /* CONFIG_KASAN_GENERIC */
179
180static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
181static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
182
183#endif /* CONFIG_KASAN_GENERIC */
184
Andrey Konovalov080eb832018-12-28 00:30:09 -0800185#ifdef CONFIG_KASAN_SW_TAGS
186
187#define KASAN_SHADOW_INIT 0xFF
188
Andrey Konovalov3c9e3aa2018-12-28 00:30:16 -0800189void kasan_init_tags(void);
190
191void *kasan_reset_tag(const void *addr);
192
Walter Wu8cceeff2020-04-01 21:09:37 -0700193bool kasan_report(unsigned long addr, size_t size,
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800194 bool is_write, unsigned long ip);
195
Andrey Konovalov3c9e3aa2018-12-28 00:30:16 -0800196#else /* CONFIG_KASAN_SW_TAGS */
197
198static inline void kasan_init_tags(void) { }
199
200static inline void *kasan_reset_tag(const void *addr)
201{
202 return (void *)addr;
203}
204
Andrey Konovalov080eb832018-12-28 00:30:09 -0800205#endif /* CONFIG_KASAN_SW_TAGS */
206
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -0800207#ifdef CONFIG_KASAN_VMALLOC
Andrey Ryabinind98c9e82019-12-17 20:51:38 -0800208int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
209void kasan_poison_vmalloc(const void *start, unsigned long size);
210void kasan_unpoison_vmalloc(const void *start, unsigned long size);
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -0800211void kasan_release_vmalloc(unsigned long start, unsigned long end,
212 unsigned long free_region_start,
213 unsigned long free_region_end);
214#else
Andrey Ryabinind98c9e82019-12-17 20:51:38 -0800215static inline int kasan_populate_vmalloc(unsigned long start,
216 unsigned long size)
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -0800217{
218 return 0;
219}
220
Andrey Ryabinind98c9e82019-12-17 20:51:38 -0800221static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
222{ }
223static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
224{ }
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -0800225static inline void kasan_release_vmalloc(unsigned long start,
226 unsigned long end,
227 unsigned long free_region_start,
228 unsigned long free_region_end) {}
229#endif
230
Jann Horn2f004ee2019-12-19 00:11:50 +0100231#ifdef CONFIG_KASAN_INLINE
232void kasan_non_canonical_hook(unsigned long addr);
233#else /* CONFIG_KASAN_INLINE */
234static inline void kasan_non_canonical_hook(unsigned long addr) { }
235#endif /* CONFIG_KASAN_INLINE */
236
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800237#endif /* LINUX_KASAN_H */