aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mm/kasan/report_hw_tags.c
blob: 065e1b2fc484cae07354008b2d3c398b9f562e06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: GPL-2.0
/*
 * This file contains hardware tag-based KASAN specific error reporting code.
 *
 * Copyright (c) 2020 Google, Inc.
 * Author: Andrey Konovalov <andreyknvl@google.com>
 */

#include <linux/kasan.h>
#include <linux/kernel.h>
#include <linux/memory.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/types.h>

#include "kasan.h"

const void *kasan_find_first_bad_addr(const void *addr, size_t size)
{
	/*
	 * Hardware Tag-Based KASAN only calls this function for normal memory
	 * accesses, and thus addr points precisely to the first bad address
	 * with an invalid (and present) memory tag. Therefore:
	 * 1. Return the address as is without walking memory tags.
	 * 2. Skip the addr_has_metadata check.
	 */
	return kasan_reset_tag(addr);
}

size_t kasan_get_alloc_size(void *object, struct kmem_cache *cache)
{
	size_t size = 0;
	int i = 0;
	u8 memory_tag;

	/*
	 * Skip the addr_has_metadata check, as this function only operates on
	 * slab memory, which must have metadata.
	 */

	/*
	 * The loop below returns 0 for freed objects, for which KASAN cannot
	 * calculate the allocation size based on the metadata.
	 */
	while (size < cache->object_size) {
		memory_tag = hw_get_mem_tag(object + i * KASAN_GRANULE_SIZE);
		if (memory_tag != KASAN_TAG_INVALID)
			size += KASAN_GRANULE_SIZE;
		else
			return size;
		i++;
	}

	return cache->object_size;
}

void kasan_metadata_fetch_row(char *buffer, void *row)
{
	int i;

	for (i = 0; i < META_BYTES_PER_ROW; i++)
		buffer[i] = hw_get_mem_tag(row + i * KASAN_GRANULE_SIZE);
}

void kasan_print_tags(u8 addr_tag, const void *addr)
{
	u8 memory_tag = hw_get_mem_tag((void *)addr);

	pr_err("Pointer tag: [%02x], memory tag: [%02x]\n",
		addr_tag, memory_tag);
}