This source file includes following definitions.
- kasan_init_tags
- random_tag
- kasan_reset_tag
- check_memory_region
- __hwasan_loadN_noabort
- __hwasan_storeN_noabort
- __hwasan_tag_memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/export.h>
17 #include <linux/interrupt.h>
18 #include <linux/init.h>
19 #include <linux/kasan.h>
20 #include <linux/kernel.h>
21 #include <linux/kmemleak.h>
22 #include <linux/linkage.h>
23 #include <linux/memblock.h>
24 #include <linux/memory.h>
25 #include <linux/mm.h>
26 #include <linux/module.h>
27 #include <linux/printk.h>
28 #include <linux/random.h>
29 #include <linux/sched.h>
30 #include <linux/sched/task_stack.h>
31 #include <linux/slab.h>
32 #include <linux/stacktrace.h>
33 #include <linux/string.h>
34 #include <linux/types.h>
35 #include <linux/vmalloc.h>
36 #include <linux/bug.h>
37
38 #include "kasan.h"
39 #include "../slab.h"
40
41 static DEFINE_PER_CPU(u32, prng_state);
42
43 void kasan_init_tags(void)
44 {
45 int cpu;
46
47 for_each_possible_cpu(cpu)
48 per_cpu(prng_state, cpu) = (u32)get_cycles();
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62
63 u8 random_tag(void)
64 {
65 u32 state = this_cpu_read(prng_state);
66
67 state = 1664525 * state + 1013904223;
68 this_cpu_write(prng_state, state);
69
70 return (u8)(state % (KASAN_TAG_MAX + 1));
71 }
72
73 void *kasan_reset_tag(const void *addr)
74 {
75 return reset_tag(addr);
76 }
77
78 bool check_memory_region(unsigned long addr, size_t size, bool write,
79 unsigned long ret_ip)
80 {
81 u8 tag;
82 u8 *shadow_first, *shadow_last, *shadow;
83 void *untagged_addr;
84
85 if (unlikely(size == 0))
86 return true;
87
88 tag = get_tag((const void *)addr);
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 if (tag == KASAN_TAG_KERNEL)
108 return true;
109
110 untagged_addr = reset_tag((const void *)addr);
111 if (unlikely(untagged_addr <
112 kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
113 kasan_report(addr, size, write, ret_ip);
114 return false;
115 }
116 shadow_first = kasan_mem_to_shadow(untagged_addr);
117 shadow_last = kasan_mem_to_shadow(untagged_addr + size - 1);
118 for (shadow = shadow_first; shadow <= shadow_last; shadow++) {
119 if (*shadow != tag) {
120 kasan_report(addr, size, write, ret_ip);
121 return false;
122 }
123 }
124
125 return true;
126 }
127
128 #define DEFINE_HWASAN_LOAD_STORE(size) \
129 void __hwasan_load##size##_noabort(unsigned long addr) \
130 { \
131 check_memory_region(addr, size, false, _RET_IP_); \
132 } \
133 EXPORT_SYMBOL(__hwasan_load##size##_noabort); \
134 void __hwasan_store##size##_noabort(unsigned long addr) \
135 { \
136 check_memory_region(addr, size, true, _RET_IP_); \
137 } \
138 EXPORT_SYMBOL(__hwasan_store##size##_noabort)
139
140 DEFINE_HWASAN_LOAD_STORE(1);
141 DEFINE_HWASAN_LOAD_STORE(2);
142 DEFINE_HWASAN_LOAD_STORE(4);
143 DEFINE_HWASAN_LOAD_STORE(8);
144 DEFINE_HWASAN_LOAD_STORE(16);
145
146 void __hwasan_loadN_noabort(unsigned long addr, unsigned long size)
147 {
148 check_memory_region(addr, size, false, _RET_IP_);
149 }
150 EXPORT_SYMBOL(__hwasan_loadN_noabort);
151
152 void __hwasan_storeN_noabort(unsigned long addr, unsigned long size)
153 {
154 check_memory_region(addr, size, true, _RET_IP_);
155 }
156 EXPORT_SYMBOL(__hwasan_storeN_noabort);
157
158 void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size)
159 {
160 kasan_poison_shadow((void *)addr, size, tag);
161 }
162 EXPORT_SYMBOL(__hwasan_tag_memory);