/* SPDX-License-Identifier: GPL-2.0 */1/*2* Kernel Electric-Fence (KFENCE). For more info please see3* Documentation/dev-tools/kfence.rst.4*5* Copyright (C) 2020, Google LLC.6*/78#ifndef MM_KFENCE_KFENCE_H9#define MM_KFENCE_KFENCE_H1011#include <linux/mm.h>12#include <linux/slab.h>13#include <linux/spinlock.h>14#include <linux/types.h>1516#include "../slab.h" /* for struct kmem_cache */1718/*19* Get the canary byte pattern for @addr. Use a pattern that varies based on the20* lower 3 bits of the address, to detect memory corruptions with higher21* probability, where similar constants are used.22*/23#define KFENCE_CANARY_PATTERN_U8(addr) ((u8)0xaa ^ (u8)((unsigned long)(addr) & 0x7))2425/*26* Define a continuous 8-byte canary starting from a multiple of 8. The canary27* of each byte is only related to the lowest three bits of its address, so the28* canary of every 8 bytes is the same. 64-bit memory can be filled and checked29* at a time instead of byte by byte to improve performance.30*/31#define KFENCE_CANARY_PATTERN_U64 ((u64)0xaaaaaaaaaaaaaaaa ^ (u64)(le64_to_cpu(0x0706050403020100)))3233/* Maximum stack depth for reports. */34#define KFENCE_STACK_DEPTH 643536/* KFENCE object states. */37enum kfence_object_state {38KFENCE_OBJECT_UNUSED, /* Object is unused. */39KFENCE_OBJECT_ALLOCATED, /* Object is currently allocated. */40KFENCE_OBJECT_RCU_FREEING, /* Object was allocated, and then being freed by rcu. */41KFENCE_OBJECT_FREED, /* Object was allocated, and then freed. */42};4344/* Alloc/free tracking information. */45struct kfence_track {46pid_t pid;47int cpu;48u64 ts_nsec;49int num_stack_entries;50unsigned long stack_entries[KFENCE_STACK_DEPTH];51};5253/* KFENCE metadata per guarded allocation. */54struct kfence_metadata {55struct list_head list; /* Freelist node; access under kfence_freelist_lock. */56struct rcu_head rcu_head; /* For delayed freeing. */5758/*59* Lock protecting below data; to ensure consistency of the below data,60* since the following may execute concurrently: __kfence_alloc(),61* __kfence_free(), kfence_handle_page_fault(). However, note that we62* cannot grab the same metadata off the freelist twice, and multiple63* __kfence_alloc() cannot run concurrently on the same metadata.64*/65raw_spinlock_t lock;6667/* The current state of the object; see above. */68enum kfence_object_state state;6970/*71* Allocated object address; cannot be calculated from size, because of72* alignment requirements.73*74* Invariant: ALIGN_DOWN(addr, PAGE_SIZE) is constant.75*/76unsigned long addr;7778/*79* The size of the original allocation.80*/81size_t size;8283/*84* The kmem_cache cache of the last allocation; NULL if never allocated85* or the cache has already been destroyed.86*/87struct kmem_cache *cache;8889/*90* In case of an invalid access, the page that was unprotected; we91* optimistically only store one address.92*/93unsigned long unprotected_page;9495/* Allocation and free stack information. */96struct kfence_track alloc_track;97struct kfence_track free_track;98/* For updating alloc_covered on frees. */99u32 alloc_stack_hash;100#ifdef CONFIG_MEMCG101struct slabobj_ext obj_exts;102#endif103};104105#define KFENCE_METADATA_SIZE PAGE_ALIGN(sizeof(struct kfence_metadata) * \106CONFIG_KFENCE_NUM_OBJECTS)107108extern struct kfence_metadata *kfence_metadata;109110static inline struct kfence_metadata *addr_to_metadata(unsigned long addr)111{112long index;113114/* The checks do not affect performance; only called from slow-paths. */115116if (!is_kfence_address((void *)addr))117return NULL;118119/*120* May be an invalid index if called with an address at the edge of121* __kfence_pool, in which case we would report an "invalid access"122* error.123*/124index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2) - 1;125if (index < 0 || index >= CONFIG_KFENCE_NUM_OBJECTS)126return NULL;127128return &kfence_metadata[index];129}130131/* KFENCE error types for report generation. */132enum kfence_error_type {133KFENCE_ERROR_OOB, /* Detected a out-of-bounds access. */134KFENCE_ERROR_UAF, /* Detected a use-after-free access. */135KFENCE_ERROR_CORRUPTION, /* Detected a memory corruption on free. */136KFENCE_ERROR_INVALID, /* Invalid access of unknown type. */137KFENCE_ERROR_INVALID_FREE, /* Invalid free. */138};139140void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *regs,141const struct kfence_metadata *meta, enum kfence_error_type type);142143void kfence_print_object(struct seq_file *seq, const struct kfence_metadata *meta);144145#endif /* MM_KFENCE_KFENCE_H */146147148