/* 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 643536extern raw_spinlock_t kfence_freelist_lock;3738/* KFENCE object states. */39enum kfence_object_state {40KFENCE_OBJECT_UNUSED, /* Object is unused. */41KFENCE_OBJECT_ALLOCATED, /* Object is currently allocated. */42KFENCE_OBJECT_RCU_FREEING, /* Object was allocated, and then being freed by rcu. */43KFENCE_OBJECT_FREED, /* Object was allocated, and then freed. */44};4546/* Alloc/free tracking information. */47struct kfence_track {48pid_t pid;49int cpu;50u64 ts_nsec;51int num_stack_entries;52unsigned long stack_entries[KFENCE_STACK_DEPTH];53};5455/* KFENCE metadata per guarded allocation. */56struct kfence_metadata {57struct list_head list __guarded_by(&kfence_freelist_lock); /* Freelist node. */58struct rcu_head rcu_head; /* For delayed freeing. */5960/*61* Lock protecting below data; to ensure consistency of the below data,62* since the following may execute concurrently: __kfence_alloc(),63* __kfence_free(), kfence_handle_page_fault(). However, note that we64* cannot grab the same metadata off the freelist twice, and multiple65* __kfence_alloc() cannot run concurrently on the same metadata.66*/67raw_spinlock_t lock;6869/* The current state of the object; see above. */70enum kfence_object_state state;7172/*73* Allocated object address; cannot be calculated from size, because of74* alignment requirements.75*76* Invariant: ALIGN_DOWN(addr, PAGE_SIZE) is constant.77*/78unsigned long addr;7980/*81* The size of the original allocation.82*/83size_t size;8485/*86* The kmem_cache cache of the last allocation; NULL if never allocated87* or the cache has already been destroyed.88*/89struct kmem_cache *cache;9091/*92* In case of an invalid access, the page that was unprotected; we93* optimistically only store one address.94*/95unsigned long unprotected_page __guarded_by(&lock);9697/* Allocation and free stack information. */98struct kfence_track alloc_track __guarded_by(&lock);99struct kfence_track free_track __guarded_by(&lock);100/* For updating alloc_covered on frees. */101u32 alloc_stack_hash __guarded_by(&lock);102#ifdef CONFIG_MEMCG103struct slabobj_ext obj_exts;104#endif105};106107#define KFENCE_METADATA_SIZE PAGE_ALIGN(sizeof(struct kfence_metadata) * \108CONFIG_KFENCE_NUM_OBJECTS)109110extern struct kfence_metadata *kfence_metadata;111112static inline struct kfence_metadata *addr_to_metadata(unsigned long addr)113{114long index;115116/* The checks do not affect performance; only called from slow-paths. */117118if (!is_kfence_address((void *)addr))119return NULL;120121/*122* May be an invalid index if called with an address at the edge of123* __kfence_pool, in which case we would report an "invalid access"124* error.125*/126index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2) - 1;127if (index < 0 || index >= CONFIG_KFENCE_NUM_OBJECTS)128return NULL;129130return &kfence_metadata[index];131}132133/* KFENCE error types for report generation. */134enum kfence_error_type {135KFENCE_ERROR_OOB, /* Detected a out-of-bounds access. */136KFENCE_ERROR_UAF, /* Detected a use-after-free access. */137KFENCE_ERROR_CORRUPTION, /* Detected a memory corruption on free. */138KFENCE_ERROR_INVALID, /* Invalid access of unknown type. */139KFENCE_ERROR_INVALID_FREE, /* Invalid free. */140};141142void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *regs,143const struct kfence_metadata *meta, enum kfence_error_type type);144145void kfence_print_object(struct seq_file *seq, const struct kfence_metadata *meta) __must_hold(&meta->lock);146147#endif /* MM_KFENCE_KFENCE_H */148149150