Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 1 | #ifndef __MM_KASAN_KASAN_H |
| 2 | #define __MM_KASAN_KASAN_H |
| 3 | |
| 4 | #include <linux/kasan.h> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 5 | #include <linux/stackdepot.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 6 | |
| 7 | #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT) |
| 8 | #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1) |
| 9 | |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 10 | #define KASAN_FREE_PAGE 0xFF /* page was freed */ |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 11 | #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */ |
| 12 | #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */ |
| 13 | #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */ |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 14 | #define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */ |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 15 | |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 16 | /* |
| 17 | * Stack redzone shadow values |
| 18 | * (Those are compiler's ABI, don't change them) |
| 19 | */ |
| 20 | #define KASAN_STACK_LEFT 0xF1 |
| 21 | #define KASAN_STACK_MID 0xF2 |
| 22 | #define KASAN_STACK_RIGHT 0xF3 |
| 23 | #define KASAN_STACK_PARTIAL 0xF4 |
Dmitry Vyukov | 828347f | 2016-11-30 15:54:16 -0800 | [diff] [blame] | 24 | #define KASAN_USE_AFTER_SCOPE 0xF8 |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 25 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 26 | /* Don't break randconfig/all*config builds */ |
| 27 | #ifndef KASAN_ABI_VERSION |
| 28 | #define KASAN_ABI_VERSION 1 |
| 29 | #endif |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 30 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 31 | struct kasan_access_info { |
| 32 | const void *access_addr; |
| 33 | const void *first_bad_addr; |
| 34 | size_t access_size; |
| 35 | bool is_write; |
| 36 | unsigned long ip; |
| 37 | }; |
| 38 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 39 | /* The layout of struct dictated by compiler */ |
| 40 | struct kasan_source_location { |
| 41 | const char *filename; |
| 42 | int line_no; |
| 43 | int column_no; |
| 44 | }; |
| 45 | |
| 46 | /* The layout of struct dictated by compiler */ |
| 47 | struct kasan_global { |
| 48 | const void *beg; /* Address of the beginning of the global variable. */ |
| 49 | size_t size; /* Size of the global variable. */ |
| 50 | size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */ |
| 51 | const void *name; |
| 52 | const void *module_name; /* Name of the module where the global variable is declared. */ |
| 53 | unsigned long has_dynamic_init; /* This needed for C++ */ |
| 54 | #if KASAN_ABI_VERSION >= 4 |
| 55 | struct kasan_source_location *location; |
| 56 | #endif |
Dmitry Vyukov | 045d599 | 2016-11-30 15:54:13 -0800 | [diff] [blame] | 57 | #if KASAN_ABI_VERSION >= 5 |
| 58 | char *odr_indicator; |
| 59 | #endif |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Structures to keep alloc and free tracks * |
| 64 | */ |
| 65 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 66 | #define KASAN_STACK_DEPTH 64 |
| 67 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 68 | struct kasan_track { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 69 | u32 pid; |
| 70 | depot_stack_handle_t stack; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | struct kasan_alloc_meta { |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 74 | struct kasan_track alloc_track; |
| 75 | struct kasan_track free_track; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 78 | struct qlist_node { |
| 79 | struct qlist_node *next; |
| 80 | }; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 81 | struct kasan_free_meta { |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 82 | /* This field is used while the object is in the quarantine. |
| 83 | * Otherwise it might be used for the allocator freelist. |
| 84 | */ |
| 85 | struct qlist_node quarantine_link; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache, |
| 89 | const void *object); |
| 90 | struct kasan_free_meta *get_free_info(struct kmem_cache *cache, |
| 91 | const void *object); |
| 92 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 93 | static inline const void *kasan_shadow_to_mem(const void *shadow_addr) |
| 94 | { |
| 95 | return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET) |
| 96 | << KASAN_SHADOW_SCALE_SHIFT); |
| 97 | } |
| 98 | |
Aneesh Kumar K.V | 0ba8663 | 2015-11-05 18:50:43 -0800 | [diff] [blame] | 99 | static inline bool kasan_report_enabled(void) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 100 | { |
| 101 | return !current->kasan_depth; |
| 102 | } |
| 103 | |
| 104 | void kasan_report(unsigned long addr, size_t size, |
| 105 | bool is_write, unsigned long ip); |
Andrey Ryabinin | 7e08897 | 2016-08-02 14:02:55 -0700 | [diff] [blame] | 106 | void kasan_report_double_free(struct kmem_cache *cache, void *object, |
| 107 | s8 shadow); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 108 | |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 109 | #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB) |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 110 | void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache); |
| 111 | void quarantine_reduce(void); |
| 112 | void quarantine_remove_cache(struct kmem_cache *cache); |
| 113 | #else |
| 114 | static inline void quarantine_put(struct kasan_free_meta *info, |
| 115 | struct kmem_cache *cache) { } |
| 116 | static inline void quarantine_reduce(void) { } |
| 117 | static inline void quarantine_remove_cache(struct kmem_cache *cache) { } |
| 118 | #endif |
| 119 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 120 | #endif |