blob: 7c0bcd1f4c0d0894b3961110aea890d2ae5c0bcc [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 __MM_KASAN_KASAN_H
3#define __MM_KASAN_KASAN_H
4
5#include <linux/kasan.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -07006#include <linux/stackdepot.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08007
8#define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
9#define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
10
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080011#define KASAN_FREE_PAGE 0xFF /* page was freed */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080012#define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */
13#define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */
14#define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080015#define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080016
Andrey Ryabininc420f162015-02-13 14:39:59 -080017/*
18 * Stack redzone shadow values
19 * (Those are compiler's ABI, don't change them)
20 */
21#define KASAN_STACK_LEFT 0xF1
22#define KASAN_STACK_MID 0xF2
23#define KASAN_STACK_RIGHT 0xF3
24#define KASAN_STACK_PARTIAL 0xF4
Dmitry Vyukov828347f2016-11-30 15:54:16 -080025#define KASAN_USE_AFTER_SCOPE 0xF8
Andrey Ryabininc420f162015-02-13 14:39:59 -080026
Paul Lawrence342061e2018-02-06 15:36:11 -080027/*
28 * alloca redzone shadow values
29 */
30#define KASAN_ALLOCA_LEFT 0xCA
31#define KASAN_ALLOCA_RIGHT 0xCB
32
33#define KASAN_ALLOCA_REDZONE_SIZE 32
34
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080035/* Don't break randconfig/all*config builds */
36#ifndef KASAN_ABI_VERSION
37#define KASAN_ABI_VERSION 1
38#endif
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080039
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080040struct kasan_access_info {
41 const void *access_addr;
42 const void *first_bad_addr;
43 size_t access_size;
44 bool is_write;
45 unsigned long ip;
46};
47
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080048/* The layout of struct dictated by compiler */
49struct kasan_source_location {
50 const char *filename;
51 int line_no;
52 int column_no;
53};
54
55/* The layout of struct dictated by compiler */
56struct kasan_global {
57 const void *beg; /* Address of the beginning of the global variable. */
58 size_t size; /* Size of the global variable. */
59 size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */
60 const void *name;
61 const void *module_name; /* Name of the module where the global variable is declared. */
62 unsigned long has_dynamic_init; /* This needed for C++ */
63#if KASAN_ABI_VERSION >= 4
64 struct kasan_source_location *location;
65#endif
Dmitry Vyukov045d5992016-11-30 15:54:13 -080066#if KASAN_ABI_VERSION >= 5
67 char *odr_indicator;
68#endif
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080069};
70
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070071/**
72 * Structures to keep alloc and free tracks *
73 */
74
Alexander Potapenkocd110162016-03-25 14:22:08 -070075#define KASAN_STACK_DEPTH 64
76
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070077struct kasan_track {
Alexander Potapenkocd110162016-03-25 14:22:08 -070078 u32 pid;
79 depot_stack_handle_t stack;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070080};
81
82struct kasan_alloc_meta {
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -070083 struct kasan_track alloc_track;
84 struct kasan_track free_track;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070085};
86
Alexander Potapenko55834c52016-05-20 16:59:11 -070087struct qlist_node {
88 struct qlist_node *next;
89};
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070090struct kasan_free_meta {
Alexander Potapenko55834c52016-05-20 16:59:11 -070091 /* This field is used while the object is in the quarantine.
92 * Otherwise it might be used for the allocator freelist.
93 */
94 struct qlist_node quarantine_link;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070095};
96
97struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
98 const void *object);
99struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
100 const void *object);
101
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800102static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
103{
104 return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
105 << KASAN_SHADOW_SCALE_SHIFT);
106}
107
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800108void kasan_report(unsigned long addr, size_t size,
109 bool is_write, unsigned long ip);
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700110void kasan_report_double_free(struct kmem_cache *cache, void *object,
Andrey Konovalov5ab6d912017-05-03 14:56:47 -0700111 void *ip);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800112
Alexander Potapenko80a92012016-07-28 15:49:07 -0700113#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700114void 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