blob: e3eb834c9a35dc2ab17b54336285c353567a1b18 [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
14#define KASAN_SHADOW_SCALE_SHIFT 3
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080015
16#include <asm/kasan.h>
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +030017#include <asm/pgtable.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080018
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +030019extern unsigned char kasan_zero_page[PAGE_SIZE];
20extern pte_t kasan_zero_pte[PTRS_PER_PTE];
21extern pmd_t kasan_zero_pmd[PTRS_PER_PMD];
22extern pud_t kasan_zero_pud[PTRS_PER_PUD];
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030023extern p4d_t kasan_zero_p4d[PTRS_PER_P4D];
Andrey Ryabinin69786cdb2015-08-13 08:37:24 +030024
25void kasan_populate_zero_shadow(const void *shadow_start,
26 const void *shadow_end);
27
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080028static inline void *kasan_mem_to_shadow(const void *addr)
29{
30 return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
31 + KASAN_SHADOW_OFFSET;
32}
33
34/* Enable reporting bugs after kasan_disable_current() */
Ingo Molnaraf8601a2017-02-03 09:57:00 +010035extern void kasan_enable_current(void);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080036
37/* Disable reporting bugs for current task */
Ingo Molnaraf8601a2017-02-03 09:57:00 +010038extern void kasan_disable_current(void);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080039
40void kasan_unpoison_shadow(const void *address, size_t size);
41
Mark Rutlande3ae1162016-03-09 14:08:15 -080042void kasan_unpoison_task_stack(struct task_struct *task);
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +020043void kasan_unpoison_stack_above_sp_to(const void *watermark);
Mark Rutlande3ae1162016-03-09 14:08:15 -080044
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080045void kasan_alloc_pages(struct page *page, unsigned int order);
46void kasan_free_pages(struct page *page, unsigned int order);
47
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070048void kasan_cache_create(struct kmem_cache *cache, size_t *size,
Alexey Dobriyand50112e2017-11-15 17:32:18 -080049 slab_flags_t *flags);
Alexander Potapenko55834c52016-05-20 16:59:11 -070050void kasan_cache_shrink(struct kmem_cache *cache);
Greg Thelenf9fa1d92017-02-24 15:00:05 -080051void kasan_cache_shutdown(struct kmem_cache *cache);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070052
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080053void kasan_poison_slab(struct page *page);
54void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
55void kasan_poison_object_data(struct kmem_cache *cache, void *object);
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -070056void kasan_init_slab_obj(struct kmem_cache *cache, const void *object);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080057
Alexander Potapenko505f5dc2016-03-25 14:22:02 -070058void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080059void kasan_kfree_large(const void *ptr);
Andrey Ryabinin9b75a862016-06-24 14:49:34 -070060void kasan_poison_kfree(void *ptr);
Alexander Potapenko505f5dc2016-03-25 14:22:02 -070061void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size,
62 gfp_t flags);
63void kasan_krealloc(const void *object, size_t new_size, gfp_t flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080064
Alexander Potapenko505f5dc2016-03-25 14:22:02 -070065void kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags);
Alexander Potapenko55834c52016-05-20 16:59:11 -070066bool kasan_slab_free(struct kmem_cache *s, void *object);
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
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080073int kasan_module_alloc(void *addr, size_t size);
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -070074void kasan_free_shadow(const struct vm_struct *vm);
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080075
Andrey Ryabinin9b75a862016-06-24 14:49:34 -070076size_t ksize(const void *);
77static inline void kasan_unpoison_slab(const void *ptr) { ksize(ptr); }
Alexander Potapenko80a92012016-07-28 15:49:07 -070078size_t kasan_metadata_size(struct kmem_cache *cache);
Andrey Ryabinin9b75a862016-06-24 14:49:34 -070079
Mark Rutlandb0845ce2017-03-31 15:12:04 -070080bool kasan_save_enable_multi_shot(void);
81void kasan_restore_multi_shot(bool enabled);
82
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080083#else /* CONFIG_KASAN */
84
85static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
86
Mark Rutlande3ae1162016-03-09 14:08:15 -080087static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +020088static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {}
Mark Rutlande3ae1162016-03-09 14:08:15 -080089
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080090static inline void kasan_enable_current(void) {}
91static inline void kasan_disable_current(void) {}
92
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080093static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
94static inline void kasan_free_pages(struct page *page, unsigned int order) {}
95
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070096static inline void kasan_cache_create(struct kmem_cache *cache,
97 size_t *size,
Alexey Dobriyand50112e2017-11-15 17:32:18 -080098 slab_flags_t *flags) {}
Alexander Potapenko55834c52016-05-20 16:59:11 -070099static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
Greg Thelenf9fa1d92017-02-24 15:00:05 -0800100static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700101
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800102static inline void kasan_poison_slab(struct page *page) {}
103static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
104 void *object) {}
105static inline void kasan_poison_object_data(struct kmem_cache *cache,
106 void *object) {}
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700107static inline void kasan_init_slab_obj(struct kmem_cache *cache,
108 const void *object) {}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800109
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700110static inline void kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags) {}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800111static inline void kasan_kfree_large(const void *ptr) {}
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700112static inline void kasan_poison_kfree(void *ptr) {}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800113static inline void kasan_kmalloc(struct kmem_cache *s, const void *object,
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700114 size_t size, gfp_t flags) {}
115static inline void kasan_krealloc(const void *object, size_t new_size,
116 gfp_t flags) {}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800117
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700118static inline void kasan_slab_alloc(struct kmem_cache *s, void *object,
119 gfp_t flags) {}
Alexander Potapenko55834c52016-05-20 16:59:11 -0700120static inline bool kasan_slab_free(struct kmem_cache *s, void *object)
121{
122 return false;
123}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800124
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800125static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700126static inline void kasan_free_shadow(const struct vm_struct *vm) {}
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800127
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700128static inline void kasan_unpoison_slab(const void *ptr) { }
Alexander Potapenko80a92012016-07-28 15:49:07 -0700129static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700130
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800131#endif /* CONFIG_KASAN */
132
133#endif /* LINUX_KASAN_H */