blob: fb87923552ef2b90c65847fdb5c56644560ca7ec [file] [log] [blame]
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08001#ifndef __MM_KASAN_KASAN_H
2#define __MM_KASAN_KASAN_H
3
4#include <linux/kasan.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -07005#include <linux/stackdepot.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08006
7#define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
8#define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
9
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080010#define KASAN_FREE_PAGE 0xFF /* page was freed */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080011#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 Ryabininbebf56a2015-02-13 14:40:17 -080014#define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080015
Andrey Ryabininc420f162015-02-13 14:39:59 -080016/*
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 Ryabininbebf56a2015-02-13 14:40:17 -080025/* Don't break randconfig/all*config builds */
26#ifndef KASAN_ABI_VERSION
27#define KASAN_ABI_VERSION 1
28#endif
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080029
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080030struct 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 Ryabininbebf56a2015-02-13 14:40:17 -080038/* The layout of struct dictated by compiler */
39struct 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 */
46struct 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 Potapenko7ed2f9e2016-03-25 14:21:59 -070058/**
59 * Structures to keep alloc and free tracks *
60 */
61
62enum kasan_state {
63 KASAN_STATE_INIT,
64 KASAN_STATE_ALLOC,
Alexander Potapenko55834c52016-05-20 16:59:11 -070065 KASAN_STATE_QUARANTINE,
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070066 KASAN_STATE_FREE
67};
68
Alexander Potapenkocd110162016-03-25 14:22:08 -070069#define KASAN_STACK_DEPTH 64
70
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070071struct kasan_track {
Alexander Potapenkocd110162016-03-25 14:22:08 -070072 u32 pid;
73 depot_stack_handle_t stack;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070074};
75
76struct kasan_alloc_meta {
Alexander Potapenkocd110162016-03-25 14:22:08 -070077 struct kasan_track track;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070078 u32 state : 2; /* enum kasan_state */
79 u32 alloc_size : 30;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070080};
81
Alexander Potapenko55834c52016-05-20 16:59:11 -070082struct qlist_node {
83 struct qlist_node *next;
84};
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070085struct kasan_free_meta {
Alexander Potapenko55834c52016-05-20 16:59:11 -070086 /* This field is used while the object is in the quarantine.
87 * Otherwise it might be used for the allocator freelist.
88 */
89 struct qlist_node quarantine_link;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070090 struct kasan_track track;
91};
92
93struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
94 const void *object);
95struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
96 const void *object);
97
98
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080099static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
100{
101 return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
102 << KASAN_SHADOW_SCALE_SHIFT);
103}
104
Aneesh Kumar K.V0ba86632015-11-05 18:50:43 -0800105static inline bool kasan_report_enabled(void)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800106{
107 return !current->kasan_depth;
108}
109
110void kasan_report(unsigned long addr, size_t size,
111 bool is_write, unsigned long ip);
112
Alexander Potapenko55834c52016-05-20 16:59:11 -0700113#ifdef CONFIG_SLAB
114void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
115void quarantine_reduce(void);
116void quarantine_remove_cache(struct kmem_cache *cache);
117#else
118static inline void quarantine_put(struct kasan_free_meta *info,
119 struct kmem_cache *cache) { }
120static inline void quarantine_reduce(void) { }
121static inline void quarantine_remove_cache(struct kmem_cache *cache) { }
122#endif
123
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800124#endif