xref: /openbmc/linux/mm/kasan/kasan.h (revision 25b892b5)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __MM_KASAN_KASAN_H
3 #define __MM_KASAN_KASAN_H
4 
5 #include <linux/kasan.h>
6 #include <linux/kasan-tags.h>
7 #include <linux/kfence.h>
8 #include <linux/stackdepot.h>
9 
10 #ifdef CONFIG_KASAN_HW_TAGS
11 
12 #include <linux/static_key.h>
13 #include "../slab.h"
14 
15 DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
16 extern bool kasan_flag_async __ro_after_init;
17 
18 static inline bool kasan_stack_collection_enabled(void)
19 {
20 	return static_branch_unlikely(&kasan_flag_stacktrace);
21 }
22 
23 static inline bool kasan_async_mode_enabled(void)
24 {
25 	return kasan_flag_async;
26 }
27 #else
28 
29 static inline bool kasan_stack_collection_enabled(void)
30 {
31 	return true;
32 }
33 
34 static inline bool kasan_async_mode_enabled(void)
35 {
36 	return false;
37 }
38 
39 #endif
40 
41 extern bool kasan_flag_async __ro_after_init;
42 
43 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
44 #define KASAN_GRANULE_SIZE	(1UL << KASAN_SHADOW_SCALE_SHIFT)
45 #else
46 #include <asm/mte-kasan.h>
47 #define KASAN_GRANULE_SIZE	MTE_GRANULE_SIZE
48 #endif
49 
50 #define KASAN_GRANULE_MASK	(KASAN_GRANULE_SIZE - 1)
51 
52 #define KASAN_MEMORY_PER_SHADOW_PAGE	(KASAN_GRANULE_SIZE << PAGE_SHIFT)
53 
54 #ifdef CONFIG_KASAN_GENERIC
55 #define KASAN_FREE_PAGE         0xFF  /* page was freed */
56 #define KASAN_PAGE_REDZONE      0xFE  /* redzone for kmalloc_large allocations */
57 #define KASAN_KMALLOC_REDZONE   0xFC  /* redzone inside slub object */
58 #define KASAN_KMALLOC_FREE      0xFB  /* object was freed (kmem_cache_free/kfree) */
59 #define KASAN_KMALLOC_FREETRACK 0xFA  /* object was freed and has free track set */
60 #else
61 #define KASAN_FREE_PAGE         KASAN_TAG_INVALID
62 #define KASAN_PAGE_REDZONE      KASAN_TAG_INVALID
63 #define KASAN_KMALLOC_REDZONE   KASAN_TAG_INVALID
64 #define KASAN_KMALLOC_FREE      KASAN_TAG_INVALID
65 #define KASAN_KMALLOC_FREETRACK KASAN_TAG_INVALID
66 #endif
67 
68 #define KASAN_GLOBAL_REDZONE    0xF9  /* redzone for global variable */
69 #define KASAN_VMALLOC_INVALID   0xF8  /* unallocated space in vmapped page */
70 
71 /*
72  * Stack redzone shadow values
73  * (Those are compiler's ABI, don't change them)
74  */
75 #define KASAN_STACK_LEFT        0xF1
76 #define KASAN_STACK_MID         0xF2
77 #define KASAN_STACK_RIGHT       0xF3
78 #define KASAN_STACK_PARTIAL     0xF4
79 
80 /*
81  * alloca redzone shadow values
82  */
83 #define KASAN_ALLOCA_LEFT	0xCA
84 #define KASAN_ALLOCA_RIGHT	0xCB
85 
86 #define KASAN_ALLOCA_REDZONE_SIZE	32
87 
88 /*
89  * Stack frame marker (compiler ABI).
90  */
91 #define KASAN_CURRENT_STACK_FRAME_MAGIC 0x41B58AB3
92 
93 /* Don't break randconfig/all*config builds */
94 #ifndef KASAN_ABI_VERSION
95 #define KASAN_ABI_VERSION 1
96 #endif
97 
98 /* Metadata layout customization. */
99 #define META_BYTES_PER_BLOCK 1
100 #define META_BLOCKS_PER_ROW 16
101 #define META_BYTES_PER_ROW (META_BLOCKS_PER_ROW * META_BYTES_PER_BLOCK)
102 #define META_MEM_BYTES_PER_ROW (META_BYTES_PER_ROW * KASAN_GRANULE_SIZE)
103 #define META_ROWS_AROUND_ADDR 2
104 
105 struct kasan_access_info {
106 	const void *access_addr;
107 	const void *first_bad_addr;
108 	size_t access_size;
109 	bool is_write;
110 	unsigned long ip;
111 };
112 
113 /* The layout of struct dictated by compiler */
114 struct kasan_source_location {
115 	const char *filename;
116 	int line_no;
117 	int column_no;
118 };
119 
120 /* The layout of struct dictated by compiler */
121 struct kasan_global {
122 	const void *beg;		/* Address of the beginning of the global variable. */
123 	size_t size;			/* Size of the global variable. */
124 	size_t size_with_redzone;	/* Size of the variable + size of the red zone. 32 bytes aligned */
125 	const void *name;
126 	const void *module_name;	/* Name of the module where the global variable is declared. */
127 	unsigned long has_dynamic_init;	/* This needed for C++ */
128 #if KASAN_ABI_VERSION >= 4
129 	struct kasan_source_location *location;
130 #endif
131 #if KASAN_ABI_VERSION >= 5
132 	char *odr_indicator;
133 #endif
134 };
135 
136 /**
137  * Structures to keep alloc and free tracks *
138  */
139 
140 #define KASAN_STACK_DEPTH 64
141 
142 struct kasan_track {
143 	u32 pid;
144 	depot_stack_handle_t stack;
145 };
146 
147 #if defined(CONFIG_KASAN_TAGS_IDENTIFY) && defined(CONFIG_KASAN_SW_TAGS)
148 #define KASAN_NR_FREE_STACKS 5
149 #else
150 #define KASAN_NR_FREE_STACKS 1
151 #endif
152 
153 struct kasan_alloc_meta {
154 	struct kasan_track alloc_track;
155 #ifdef CONFIG_KASAN_GENERIC
156 	/*
157 	 * The auxiliary stack is stored into struct kasan_alloc_meta.
158 	 * The free stack is stored into struct kasan_free_meta.
159 	 */
160 	depot_stack_handle_t aux_stack[2];
161 #else
162 	struct kasan_track free_track[KASAN_NR_FREE_STACKS];
163 #endif
164 #ifdef CONFIG_KASAN_TAGS_IDENTIFY
165 	u8 free_pointer_tag[KASAN_NR_FREE_STACKS];
166 	u8 free_track_idx;
167 #endif
168 };
169 
170 struct qlist_node {
171 	struct qlist_node *next;
172 };
173 
174 /*
175  * Generic mode either stores free meta in the object itself or in the redzone
176  * after the object. In the former case free meta offset is 0, in the latter
177  * case it has some sane value smaller than INT_MAX. Use INT_MAX as free meta
178  * offset when free meta isn't present.
179  */
180 #define KASAN_NO_FREE_META INT_MAX
181 
182 struct kasan_free_meta {
183 #ifdef CONFIG_KASAN_GENERIC
184 	/* This field is used while the object is in the quarantine.
185 	 * Otherwise it might be used for the allocator freelist.
186 	 */
187 	struct qlist_node quarantine_link;
188 	struct kasan_track free_track;
189 #endif
190 };
191 
192 struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache,
193 						const void *object);
194 #ifdef CONFIG_KASAN_GENERIC
195 struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache,
196 						const void *object);
197 #endif
198 
199 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
200 
201 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
202 {
203 	return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
204 		<< KASAN_SHADOW_SCALE_SHIFT);
205 }
206 
207 static inline bool addr_has_metadata(const void *addr)
208 {
209 	return (addr >= kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
210 }
211 
212 /**
213  * kasan_check_range - Check memory region, and report if invalid access.
214  * @addr: the accessed address
215  * @size: the accessed size
216  * @write: true if access is a write access
217  * @ret_ip: return address
218  * @return: true if access was valid, false if invalid
219  */
220 bool kasan_check_range(unsigned long addr, size_t size, bool write,
221 				unsigned long ret_ip);
222 
223 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
224 
225 static inline bool addr_has_metadata(const void *addr)
226 {
227 	return (is_vmalloc_addr(addr) || virt_addr_valid(addr));
228 }
229 
230 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
231 
232 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
233 void kasan_print_tags(u8 addr_tag, const void *addr);
234 #else
235 static inline void kasan_print_tags(u8 addr_tag, const void *addr) { }
236 #endif
237 
238 void *kasan_find_first_bad_addr(void *addr, size_t size);
239 const char *kasan_get_bug_type(struct kasan_access_info *info);
240 void kasan_metadata_fetch_row(char *buffer, void *row);
241 
242 #if defined(CONFIG_KASAN_GENERIC) && defined(CONFIG_KASAN_STACK)
243 void kasan_print_address_stack_frame(const void *addr);
244 #else
245 static inline void kasan_print_address_stack_frame(const void *addr) { }
246 #endif
247 
248 bool kasan_report(unsigned long addr, size_t size,
249 		bool is_write, unsigned long ip);
250 void kasan_report_invalid_free(void *object, unsigned long ip);
251 
252 struct page *kasan_addr_to_page(const void *addr);
253 
254 depot_stack_handle_t kasan_save_stack(gfp_t flags);
255 void kasan_set_track(struct kasan_track *track, gfp_t flags);
256 void kasan_set_free_info(struct kmem_cache *cache, void *object, u8 tag);
257 struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
258 				void *object, u8 tag);
259 
260 #if defined(CONFIG_KASAN_GENERIC) && \
261 	(defined(CONFIG_SLAB) || defined(CONFIG_SLUB))
262 bool kasan_quarantine_put(struct kmem_cache *cache, void *object);
263 void kasan_quarantine_reduce(void);
264 void kasan_quarantine_remove_cache(struct kmem_cache *cache);
265 #else
266 static inline bool kasan_quarantine_put(struct kmem_cache *cache, void *object) { return false; }
267 static inline void kasan_quarantine_reduce(void) { }
268 static inline void kasan_quarantine_remove_cache(struct kmem_cache *cache) { }
269 #endif
270 
271 #ifndef arch_kasan_set_tag
272 static inline const void *arch_kasan_set_tag(const void *addr, u8 tag)
273 {
274 	return addr;
275 }
276 #endif
277 #ifndef arch_kasan_get_tag
278 #define arch_kasan_get_tag(addr)	0
279 #endif
280 
281 #define set_tag(addr, tag)	((void *)arch_kasan_set_tag((addr), (tag)))
282 #define get_tag(addr)		arch_kasan_get_tag(addr)
283 
284 #ifdef CONFIG_KASAN_HW_TAGS
285 
286 #ifndef arch_enable_tagging_sync
287 #define arch_enable_tagging_sync()
288 #endif
289 #ifndef arch_enable_tagging_async
290 #define arch_enable_tagging_async()
291 #endif
292 #ifndef arch_force_async_tag_fault
293 #define arch_force_async_tag_fault()
294 #endif
295 #ifndef arch_get_random_tag
296 #define arch_get_random_tag()	(0xFF)
297 #endif
298 #ifndef arch_get_mem_tag
299 #define arch_get_mem_tag(addr)	(0xFF)
300 #endif
301 #ifndef arch_set_mem_tag_range
302 #define arch_set_mem_tag_range(addr, size, tag, init) ((void *)(addr))
303 #endif
304 
305 #define hw_enable_tagging_sync()		arch_enable_tagging_sync()
306 #define hw_enable_tagging_async()		arch_enable_tagging_async()
307 #define hw_force_async_tag_fault()		arch_force_async_tag_fault()
308 #define hw_get_random_tag()			arch_get_random_tag()
309 #define hw_get_mem_tag(addr)			arch_get_mem_tag(addr)
310 #define hw_set_mem_tag_range(addr, size, tag, init) \
311 			arch_set_mem_tag_range((addr), (size), (tag), (init))
312 
313 #else /* CONFIG_KASAN_HW_TAGS */
314 
315 #define hw_enable_tagging_sync()
316 #define hw_enable_tagging_async()
317 
318 #endif /* CONFIG_KASAN_HW_TAGS */
319 
320 #if defined(CONFIG_KASAN_HW_TAGS) && IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
321 
322 void kasan_enable_tagging_sync(void);
323 void kasan_force_async_fault(void);
324 
325 #else /* CONFIG_KASAN_HW_TAGS || CONFIG_KASAN_KUNIT_TEST */
326 
327 static inline void kasan_enable_tagging_sync(void) { }
328 static inline void kasan_force_async_fault(void) { }
329 
330 #endif /* CONFIG_KASAN_HW_TAGS || CONFIG_KASAN_KUNIT_TEST */
331 
332 #ifdef CONFIG_KASAN_SW_TAGS
333 u8 kasan_random_tag(void);
334 #elif defined(CONFIG_KASAN_HW_TAGS)
335 static inline u8 kasan_random_tag(void) { return hw_get_random_tag(); }
336 #else
337 static inline u8 kasan_random_tag(void) { return 0; }
338 #endif
339 
340 #ifdef CONFIG_KASAN_HW_TAGS
341 
342 static inline void kasan_poison(const void *addr, size_t size, u8 value, bool init)
343 {
344 	addr = kasan_reset_tag(addr);
345 
346 	/* Skip KFENCE memory if called explicitly outside of sl*b. */
347 	if (is_kfence_address(addr))
348 		return;
349 
350 	if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
351 		return;
352 	if (WARN_ON(size & KASAN_GRANULE_MASK))
353 		return;
354 
355 	hw_set_mem_tag_range((void *)addr, size, value, init);
356 }
357 
358 static inline void kasan_unpoison(const void *addr, size_t size, bool init)
359 {
360 	u8 tag = get_tag(addr);
361 
362 	addr = kasan_reset_tag(addr);
363 
364 	/* Skip KFENCE memory if called explicitly outside of sl*b. */
365 	if (is_kfence_address(addr))
366 		return;
367 
368 	if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
369 		return;
370 	/*
371 	 * Explicitly initialize the memory with the precise object size to
372 	 * avoid overwriting the SLAB redzone. This disables initialization in
373 	 * the arch code and may thus lead to performance penalty. The penalty
374 	 * is accepted since SLAB redzones aren't enabled in production builds.
375 	 */
376 	if (__slub_debug_enabled() &&
377 	    init && ((unsigned long)size & KASAN_GRANULE_MASK)) {
378 		init = false;
379 		memzero_explicit((void *)addr, size);
380 	}
381 	size = round_up(size, KASAN_GRANULE_SIZE);
382 
383 	hw_set_mem_tag_range((void *)addr, size, tag, init);
384 }
385 
386 static inline bool kasan_byte_accessible(const void *addr)
387 {
388 	u8 ptr_tag = get_tag(addr);
389 	u8 mem_tag = hw_get_mem_tag((void *)addr);
390 
391 	return ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag;
392 }
393 
394 #else /* CONFIG_KASAN_HW_TAGS */
395 
396 /**
397  * kasan_poison - mark the memory range as inaccessible
398  * @addr - range start address, must be aligned to KASAN_GRANULE_SIZE
399  * @size - range size, must be aligned to KASAN_GRANULE_SIZE
400  * @value - value that's written to metadata for the range
401  * @init - whether to initialize the memory range (only for hardware tag-based)
402  *
403  * The size gets aligned to KASAN_GRANULE_SIZE before marking the range.
404  */
405 void kasan_poison(const void *addr, size_t size, u8 value, bool init);
406 
407 /**
408  * kasan_unpoison - mark the memory range as accessible
409  * @addr - range start address, must be aligned to KASAN_GRANULE_SIZE
410  * @size - range size, can be unaligned
411  * @init - whether to initialize the memory range (only for hardware tag-based)
412  *
413  * For the tag-based modes, the @size gets aligned to KASAN_GRANULE_SIZE before
414  * marking the range.
415  * For the generic mode, the last granule of the memory range gets partially
416  * unpoisoned based on the @size.
417  */
418 void kasan_unpoison(const void *addr, size_t size, bool init);
419 
420 bool kasan_byte_accessible(const void *addr);
421 
422 #endif /* CONFIG_KASAN_HW_TAGS */
423 
424 #ifdef CONFIG_KASAN_GENERIC
425 
426 /**
427  * kasan_poison_last_granule - mark the last granule of the memory range as
428  * inaccessible
429  * @addr - range start address, must be aligned to KASAN_GRANULE_SIZE
430  * @size - range size
431  *
432  * This function is only available for the generic mode, as it's the only mode
433  * that has partially poisoned memory granules.
434  */
435 void kasan_poison_last_granule(const void *address, size_t size);
436 
437 #else /* CONFIG_KASAN_GENERIC */
438 
439 static inline void kasan_poison_last_granule(const void *address, size_t size) { }
440 
441 #endif /* CONFIG_KASAN_GENERIC */
442 
443 #ifndef kasan_arch_is_ready
444 static inline bool kasan_arch_is_ready(void)	{ return true; }
445 #elif !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE)
446 #error kasan_arch_is_ready only works in KASAN generic outline mode!
447 #endif
448 
449 /*
450  * Exported functions for interfaces called from assembly or from generated
451  * code. Declarations here to avoid warning about missing declarations.
452  */
453 asmlinkage void kasan_unpoison_task_stack_below(const void *watermark);
454 void __asan_register_globals(struct kasan_global *globals, size_t size);
455 void __asan_unregister_globals(struct kasan_global *globals, size_t size);
456 void __asan_handle_no_return(void);
457 void __asan_alloca_poison(unsigned long addr, size_t size);
458 void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom);
459 
460 void __asan_load1(unsigned long addr);
461 void __asan_store1(unsigned long addr);
462 void __asan_load2(unsigned long addr);
463 void __asan_store2(unsigned long addr);
464 void __asan_load4(unsigned long addr);
465 void __asan_store4(unsigned long addr);
466 void __asan_load8(unsigned long addr);
467 void __asan_store8(unsigned long addr);
468 void __asan_load16(unsigned long addr);
469 void __asan_store16(unsigned long addr);
470 void __asan_loadN(unsigned long addr, size_t size);
471 void __asan_storeN(unsigned long addr, size_t size);
472 
473 void __asan_load1_noabort(unsigned long addr);
474 void __asan_store1_noabort(unsigned long addr);
475 void __asan_load2_noabort(unsigned long addr);
476 void __asan_store2_noabort(unsigned long addr);
477 void __asan_load4_noabort(unsigned long addr);
478 void __asan_store4_noabort(unsigned long addr);
479 void __asan_load8_noabort(unsigned long addr);
480 void __asan_store8_noabort(unsigned long addr);
481 void __asan_load16_noabort(unsigned long addr);
482 void __asan_store16_noabort(unsigned long addr);
483 void __asan_loadN_noabort(unsigned long addr, size_t size);
484 void __asan_storeN_noabort(unsigned long addr, size_t size);
485 
486 void __asan_report_load1_noabort(unsigned long addr);
487 void __asan_report_store1_noabort(unsigned long addr);
488 void __asan_report_load2_noabort(unsigned long addr);
489 void __asan_report_store2_noabort(unsigned long addr);
490 void __asan_report_load4_noabort(unsigned long addr);
491 void __asan_report_store4_noabort(unsigned long addr);
492 void __asan_report_load8_noabort(unsigned long addr);
493 void __asan_report_store8_noabort(unsigned long addr);
494 void __asan_report_load16_noabort(unsigned long addr);
495 void __asan_report_store16_noabort(unsigned long addr);
496 void __asan_report_load_n_noabort(unsigned long addr, size_t size);
497 void __asan_report_store_n_noabort(unsigned long addr, size_t size);
498 
499 void __asan_set_shadow_00(const void *addr, size_t size);
500 void __asan_set_shadow_f1(const void *addr, size_t size);
501 void __asan_set_shadow_f2(const void *addr, size_t size);
502 void __asan_set_shadow_f3(const void *addr, size_t size);
503 void __asan_set_shadow_f5(const void *addr, size_t size);
504 void __asan_set_shadow_f8(const void *addr, size_t size);
505 
506 void __hwasan_load1_noabort(unsigned long addr);
507 void __hwasan_store1_noabort(unsigned long addr);
508 void __hwasan_load2_noabort(unsigned long addr);
509 void __hwasan_store2_noabort(unsigned long addr);
510 void __hwasan_load4_noabort(unsigned long addr);
511 void __hwasan_store4_noabort(unsigned long addr);
512 void __hwasan_load8_noabort(unsigned long addr);
513 void __hwasan_store8_noabort(unsigned long addr);
514 void __hwasan_load16_noabort(unsigned long addr);
515 void __hwasan_store16_noabort(unsigned long addr);
516 void __hwasan_loadN_noabort(unsigned long addr, size_t size);
517 void __hwasan_storeN_noabort(unsigned long addr, size_t size);
518 
519 void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size);
520 
521 #endif
522