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 |
| 24 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 25 | /* Don't break randconfig/all*config builds */ |
| 26 | #ifndef KASAN_ABI_VERSION |
| 27 | #define KASAN_ABI_VERSION 1 |
| 28 | #endif |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 29 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 30 | struct kasan_access_info { |
| 31 | const void *access_addr; |
| 32 | const void *first_bad_addr; |
| 33 | size_t access_size; |
| 34 | bool is_write; |
| 35 | unsigned long ip; |
| 36 | }; |
| 37 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 38 | /* The layout of struct dictated by compiler */ |
| 39 | struct kasan_source_location { |
| 40 | const char *filename; |
| 41 | int line_no; |
| 42 | int column_no; |
| 43 | }; |
| 44 | |
| 45 | /* The layout of struct dictated by compiler */ |
| 46 | struct kasan_global { |
| 47 | const void *beg; /* Address of the beginning of the global variable. */ |
| 48 | size_t size; /* Size of the global variable. */ |
| 49 | size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */ |
| 50 | const void *name; |
| 51 | const void *module_name; /* Name of the module where the global variable is declared. */ |
| 52 | unsigned long has_dynamic_init; /* This needed for C++ */ |
| 53 | #if KASAN_ABI_VERSION >= 4 |
| 54 | struct kasan_source_location *location; |
| 55 | #endif |
| 56 | }; |
| 57 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Structures to keep alloc and free tracks * |
| 60 | */ |
| 61 | |
| 62 | enum kasan_state { |
| 63 | KASAN_STATE_INIT, |
| 64 | KASAN_STATE_ALLOC, |
| 65 | KASAN_STATE_FREE |
| 66 | }; |
| 67 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 68 | #define KASAN_STACK_DEPTH 64 |
| 69 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 70 | struct kasan_track { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 71 | u32 pid; |
| 72 | depot_stack_handle_t stack; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct kasan_alloc_meta { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 76 | struct kasan_track track; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 77 | u32 state : 2; /* enum kasan_state */ |
| 78 | u32 alloc_size : 30; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 79 | u32 reserved; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct kasan_free_meta { |
| 83 | /* Allocator freelist pointer, unused by KASAN. */ |
| 84 | void **freelist; |
| 85 | struct kasan_track track; |
| 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 | |
| 93 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 94 | static inline const void *kasan_shadow_to_mem(const void *shadow_addr) |
| 95 | { |
| 96 | return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET) |
| 97 | << KASAN_SHADOW_SCALE_SHIFT); |
| 98 | } |
| 99 | |
Aneesh Kumar K.V | 0ba8663 | 2015-11-05 18:50:43 -0800 | [diff] [blame] | 100 | static inline bool kasan_report_enabled(void) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 101 | { |
| 102 | return !current->kasan_depth; |
| 103 | } |
| 104 | |
| 105 | void kasan_report(unsigned long addr, size_t size, |
| 106 | bool is_write, unsigned long ip); |
| 107 | |
| 108 | #endif |