File Index
Files and directories
Sign in
FILE

sw_tags.c

File preview with syntax highlighting.

linux-6.19.14/mm/kasan/sw_tags.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * This file contains core software tag-based KASAN code.
  4 *
  5 * Copyright (c) 2018 Google, Inc.
  6 * Author: Andrey Konovalov <andreyknvl@google.com>
  7 */
  8
  9#define pr_fmt(fmt) "kasan: " fmt
 10
 11#include <linux/export.h>
 12#include <linux/interrupt.h>
 13#include <linux/init.h>
 14#include <linux/kasan.h>
 15#include <linux/kernel.h>
 16#include <linux/kmemleak.h>
 17#include <linux/linkage.h>
 18#include <linux/memblock.h>
 19#include <linux/memory.h>
 20#include <linux/mm.h>
 21#include <linux/module.h>
 22#include <linux/printk.h>
 23#include <linux/random.h>
 24#include <linux/sched.h>
 25#include <linux/sched/task_stack.h>
 26#include <linux/slab.h>
 27#include <linux/stacktrace.h>
 28#include <linux/string.h>
 29#include <linux/string_choices.h>
 30#include <linux/types.h>
 31#include <linux/vmalloc.h>
 32#include <linux/bug.h>
 33
 34#include "kasan.h"
 35#include "../slab.h"
 36
 37static DEFINE_PER_CPU(u32, prng_state);
 38
 39void __init kasan_init_sw_tags(void)
 40{
 41	int cpu;
 42
 43	for_each_possible_cpu(cpu)
 44		per_cpu(prng_state, cpu) = (u32)get_cycles();
 45
 46	kasan_init_tags();
 47	kasan_enable();
 48
 49	pr_info("KernelAddressSanitizer initialized (sw-tags, stacktrace=%s)\n",
 50		str_on_off(kasan_stack_collection_enabled()));
 51}
 52
 53/*
 54 * If a preemption happens between this_cpu_read and this_cpu_write, the only
 55 * side effect is that we'll give a few allocated in different contexts objects
 56 * the same tag. Since tag-based KASAN is meant to be used a probabilistic
 57 * bug-detection debug feature, this doesn't have significant negative impact.
 58 *
 59 * Ideally the tags use strong randomness to prevent any attempts to predict
 60 * them during explicit exploit attempts. But strong randomness is expensive,
 61 * and we did an intentional trade-off to use a PRNG. This non-atomic RMW
 62 * sequence has in fact positive effect, since interrupts that randomly skew
 63 * PRNG at unpredictable points do only good.
 64 */
 65u8 kasan_random_tag(void)
 66{
 67	u32 state = this_cpu_read(prng_state);
 68
 69	state = 1664525 * state + 1013904223;
 70	this_cpu_write(prng_state, state);
 71
 72	return (u8)(state % (KASAN_TAG_MAX + 1));
 73}
 74
 75bool kasan_check_range(const void *addr, size_t size, bool write,
 76			unsigned long ret_ip)
 77{
 78	u8 tag;
 79	u8 *shadow_first, *shadow_last, *shadow;
 80	void *untagged_addr;
 81
 82	if (unlikely(size == 0))
 83		return true;
 84
 85	if (unlikely(addr + size < addr))
 86		return !kasan_report(addr, size, write, ret_ip);
 87
 88	tag = get_tag((const void *)addr);
 89
 90	/*
 91	 * Ignore accesses for pointers tagged with 0xff (native kernel
 92	 * pointer tag) to suppress false positives caused by kmap.
 93	 *
 94	 * Some kernel code was written to account for archs that don't keep
 95	 * high memory mapped all the time, but rather map and unmap particular
 96	 * pages when needed. Instead of storing a pointer to the kernel memory,
 97	 * this code saves the address of the page structure and offset within
 98	 * that page for later use. Those pages are then mapped and unmapped
 99	 * with kmap/kunmap when necessary and virt_to_page is used to get the
100	 * virtual address of the page. For arm64 (that keeps the high memory
101	 * mapped all the time), kmap is turned into a page_address call.
102
103	 * The issue is that with use of the page_address + virt_to_page
104	 * sequence the top byte value of the original pointer gets lost (gets
105	 * set to KASAN_TAG_KERNEL (0xFF)).
106	 */
107	if (tag == KASAN_TAG_KERNEL)
108		return true;
109
110	untagged_addr = kasan_reset_tag((const void *)addr);
111	if (unlikely(!addr_has_metadata(untagged_addr)))
112		return !kasan_report(addr, size, write, ret_ip);
113	shadow_first = kasan_mem_to_shadow(untagged_addr);
114	shadow_last = kasan_mem_to_shadow(untagged_addr + size - 1);
115	for (shadow = shadow_first; shadow <= shadow_last; shadow++) {
116		if (*shadow != tag) {
117			return !kasan_report(addr, size, write, ret_ip);
118		}
119	}
120
121	return true;
122}
123
124bool kasan_byte_accessible(const void *addr)
125{
126	u8 tag = get_tag(addr);
127	void *untagged_addr = kasan_reset_tag(addr);
128	u8 shadow_byte;
129
130	if (!addr_has_metadata(untagged_addr))
131		return false;
132
133	shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(untagged_addr));
134	return tag == KASAN_TAG_KERNEL || tag == shadow_byte;
135}
136
137#define DEFINE_HWASAN_LOAD_STORE(size)					\
138	void __hwasan_load##size##_noabort(void *addr)			\
139	{								\
140		kasan_check_range(addr, size, false, _RET_IP_);		\
141	}								\
142	EXPORT_SYMBOL(__hwasan_load##size##_noabort);			\
143	void __hwasan_store##size##_noabort(void *addr)			\
144	{								\
145		kasan_check_range(addr, size, true, _RET_IP_);		\
146	}								\
147	EXPORT_SYMBOL(__hwasan_store##size##_noabort)
148
149DEFINE_HWASAN_LOAD_STORE(1);
150DEFINE_HWASAN_LOAD_STORE(2);
151DEFINE_HWASAN_LOAD_STORE(4);
152DEFINE_HWASAN_LOAD_STORE(8);
153DEFINE_HWASAN_LOAD_STORE(16);
154
155void __hwasan_loadN_noabort(void *addr, ssize_t size)
156{
157	kasan_check_range(addr, size, false, _RET_IP_);
158}
159EXPORT_SYMBOL(__hwasan_loadN_noabort);
160
161void __hwasan_storeN_noabort(void *addr, ssize_t size)
162{
163	kasan_check_range(addr, size, true, _RET_IP_);
164}
165EXPORT_SYMBOL(__hwasan_storeN_noabort);
166
167void __hwasan_tag_memory(void *addr, u8 tag, ssize_t size)
168{
169	kasan_poison(addr, size, tag, false);
170}
171EXPORT_SYMBOL(__hwasan_tag_memory);
172
173void kasan_tag_mismatch(void *addr, unsigned long access_info,
174			unsigned long ret_ip)
175{
176	kasan_report(addr, 1 << (access_info & 0xf), access_info & 0x10,
177		     ret_ip);
178}
~/ Read-only file preview.