10ce20dd8SAlexander Potapenko /* SPDX-License-Identifier: GPL-2.0 */ 20ce20dd8SAlexander Potapenko /* 30ce20dd8SAlexander Potapenko * Kernel Electric-Fence (KFENCE). Public interface for allocator and fault 40ce20dd8SAlexander Potapenko * handler integration. For more info see Documentation/dev-tools/kfence.rst. 50ce20dd8SAlexander Potapenko * 60ce20dd8SAlexander Potapenko * Copyright (C) 2020, Google LLC. 70ce20dd8SAlexander Potapenko */ 80ce20dd8SAlexander Potapenko 90ce20dd8SAlexander Potapenko #ifndef _LINUX_KFENCE_H 100ce20dd8SAlexander Potapenko #define _LINUX_KFENCE_H 110ce20dd8SAlexander Potapenko 120ce20dd8SAlexander Potapenko #include <linux/mm.h> 130ce20dd8SAlexander Potapenko #include <linux/types.h> 140ce20dd8SAlexander Potapenko 150ce20dd8SAlexander Potapenko #ifdef CONFIG_KFENCE 160ce20dd8SAlexander Potapenko 170ce20dd8SAlexander Potapenko /* 180ce20dd8SAlexander Potapenko * We allocate an even number of pages, as it simplifies calculations to map 190ce20dd8SAlexander Potapenko * address to metadata indices; effectively, the very first page serves as an 200ce20dd8SAlexander Potapenko * extended guard page, but otherwise has no special purpose. 210ce20dd8SAlexander Potapenko */ 220ce20dd8SAlexander Potapenko #define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 * PAGE_SIZE) 230ce20dd8SAlexander Potapenko extern char *__kfence_pool; 240ce20dd8SAlexander Potapenko 250ce20dd8SAlexander Potapenko #ifdef CONFIG_KFENCE_STATIC_KEYS 260ce20dd8SAlexander Potapenko #include <linux/static_key.h> 270ce20dd8SAlexander Potapenko DECLARE_STATIC_KEY_FALSE(kfence_allocation_key); 280ce20dd8SAlexander Potapenko #else 290ce20dd8SAlexander Potapenko #include <linux/atomic.h> 300ce20dd8SAlexander Potapenko extern atomic_t kfence_allocation_gate; 310ce20dd8SAlexander Potapenko #endif 320ce20dd8SAlexander Potapenko 330ce20dd8SAlexander Potapenko /** 340ce20dd8SAlexander Potapenko * is_kfence_address() - check if an address belongs to KFENCE pool 350ce20dd8SAlexander Potapenko * @addr: address to check 360ce20dd8SAlexander Potapenko * 370ce20dd8SAlexander Potapenko * Return: true or false depending on whether the address is within the KFENCE 380ce20dd8SAlexander Potapenko * object range. 390ce20dd8SAlexander Potapenko * 400ce20dd8SAlexander Potapenko * KFENCE objects live in a separate page range and are not to be intermixed 410ce20dd8SAlexander Potapenko * with regular heap objects (e.g. KFENCE objects must never be added to the 420ce20dd8SAlexander Potapenko * allocator freelists). Failing to do so may and will result in heap 430ce20dd8SAlexander Potapenko * corruptions, therefore is_kfence_address() must be used to check whether 440ce20dd8SAlexander Potapenko * an object requires specific handling. 450ce20dd8SAlexander Potapenko * 460ce20dd8SAlexander Potapenko * Note: This function may be used in fast-paths, and is performance critical. 470ce20dd8SAlexander Potapenko * Future changes should take this into account; for instance, we want to avoid 480ce20dd8SAlexander Potapenko * introducing another load and therefore need to keep KFENCE_POOL_SIZE a 490ce20dd8SAlexander Potapenko * constant (until immediate patching support is added to the kernel). 500ce20dd8SAlexander Potapenko */ 510ce20dd8SAlexander Potapenko static __always_inline bool is_kfence_address(const void *addr) 520ce20dd8SAlexander Potapenko { 530ce20dd8SAlexander Potapenko /* 540ce20dd8SAlexander Potapenko * The non-NULL check is required in case the __kfence_pool pointer was 550ce20dd8SAlexander Potapenko * never initialized; keep it in the slow-path after the range-check. 560ce20dd8SAlexander Potapenko */ 570ce20dd8SAlexander Potapenko return unlikely((unsigned long)((char *)addr - __kfence_pool) < KFENCE_POOL_SIZE && addr); 580ce20dd8SAlexander Potapenko } 590ce20dd8SAlexander Potapenko 600ce20dd8SAlexander Potapenko /** 610ce20dd8SAlexander Potapenko * kfence_alloc_pool() - allocate the KFENCE pool via memblock 620ce20dd8SAlexander Potapenko */ 630ce20dd8SAlexander Potapenko void __init kfence_alloc_pool(void); 640ce20dd8SAlexander Potapenko 650ce20dd8SAlexander Potapenko /** 660ce20dd8SAlexander Potapenko * kfence_init() - perform KFENCE initialization at boot time 670ce20dd8SAlexander Potapenko * 680ce20dd8SAlexander Potapenko * Requires that kfence_alloc_pool() was called before. This sets up the 690ce20dd8SAlexander Potapenko * allocation gate timer, and requires that workqueues are available. 700ce20dd8SAlexander Potapenko */ 710ce20dd8SAlexander Potapenko void __init kfence_init(void); 720ce20dd8SAlexander Potapenko 730ce20dd8SAlexander Potapenko /** 740ce20dd8SAlexander Potapenko * kfence_shutdown_cache() - handle shutdown_cache() for KFENCE objects 750ce20dd8SAlexander Potapenko * @s: cache being shut down 760ce20dd8SAlexander Potapenko * 770ce20dd8SAlexander Potapenko * Before shutting down a cache, one must ensure there are no remaining objects 780ce20dd8SAlexander Potapenko * allocated from it. Because KFENCE objects are not referenced from the cache 790ce20dd8SAlexander Potapenko * directly, we need to check them here. 800ce20dd8SAlexander Potapenko * 810ce20dd8SAlexander Potapenko * Note that shutdown_cache() is internal to SL*B, and kmem_cache_destroy() does 820ce20dd8SAlexander Potapenko * not return if allocated objects still exist: it prints an error message and 830ce20dd8SAlexander Potapenko * simply aborts destruction of a cache, leaking memory. 840ce20dd8SAlexander Potapenko * 850ce20dd8SAlexander Potapenko * If the only such objects are KFENCE objects, we will not leak the entire 860ce20dd8SAlexander Potapenko * cache, but instead try to provide more useful debug info by making allocated 870ce20dd8SAlexander Potapenko * objects "zombie allocations". Objects may then still be used or freed (which 880ce20dd8SAlexander Potapenko * is handled gracefully), but usage will result in showing KFENCE error reports 890ce20dd8SAlexander Potapenko * which include stack traces to the user of the object, the original allocation 900ce20dd8SAlexander Potapenko * site, and caller to shutdown_cache(). 910ce20dd8SAlexander Potapenko */ 920ce20dd8SAlexander Potapenko void kfence_shutdown_cache(struct kmem_cache *s); 930ce20dd8SAlexander Potapenko 940ce20dd8SAlexander Potapenko /* 950ce20dd8SAlexander Potapenko * Allocate a KFENCE object. Allocators must not call this function directly, 960ce20dd8SAlexander Potapenko * use kfence_alloc() instead. 970ce20dd8SAlexander Potapenko */ 980ce20dd8SAlexander Potapenko void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags); 990ce20dd8SAlexander Potapenko 1000ce20dd8SAlexander Potapenko /** 1010ce20dd8SAlexander Potapenko * kfence_alloc() - allocate a KFENCE object with a low probability 1020ce20dd8SAlexander Potapenko * @s: struct kmem_cache with object requirements 1030ce20dd8SAlexander Potapenko * @size: exact size of the object to allocate (can be less than @s->size 1040ce20dd8SAlexander Potapenko * e.g. for kmalloc caches) 1050ce20dd8SAlexander Potapenko * @flags: GFP flags 1060ce20dd8SAlexander Potapenko * 1070ce20dd8SAlexander Potapenko * Return: 1080ce20dd8SAlexander Potapenko * * NULL - must proceed with allocating as usual, 1090ce20dd8SAlexander Potapenko * * non-NULL - pointer to a KFENCE object. 1100ce20dd8SAlexander Potapenko * 1110ce20dd8SAlexander Potapenko * kfence_alloc() should be inserted into the heap allocation fast path, 1120ce20dd8SAlexander Potapenko * allowing it to transparently return KFENCE-allocated objects with a low 1130ce20dd8SAlexander Potapenko * probability using a static branch (the probability is controlled by the 1140ce20dd8SAlexander Potapenko * kfence.sample_interval boot parameter). 1150ce20dd8SAlexander Potapenko */ 1160ce20dd8SAlexander Potapenko static __always_inline void *kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) 1170ce20dd8SAlexander Potapenko { 1180ce20dd8SAlexander Potapenko #ifdef CONFIG_KFENCE_STATIC_KEYS 1190ce20dd8SAlexander Potapenko if (static_branch_unlikely(&kfence_allocation_key)) 1200ce20dd8SAlexander Potapenko #else 1210ce20dd8SAlexander Potapenko if (unlikely(!atomic_read(&kfence_allocation_gate))) 1220ce20dd8SAlexander Potapenko #endif 1230ce20dd8SAlexander Potapenko return __kfence_alloc(s, size, flags); 1240ce20dd8SAlexander Potapenko return NULL; 1250ce20dd8SAlexander Potapenko } 1260ce20dd8SAlexander Potapenko 1270ce20dd8SAlexander Potapenko /** 1280ce20dd8SAlexander Potapenko * kfence_ksize() - get actual amount of memory allocated for a KFENCE object 1290ce20dd8SAlexander Potapenko * @addr: pointer to a heap object 1300ce20dd8SAlexander Potapenko * 1310ce20dd8SAlexander Potapenko * Return: 1320ce20dd8SAlexander Potapenko * * 0 - not a KFENCE object, must call __ksize() instead, 1330ce20dd8SAlexander Potapenko * * non-0 - this many bytes can be accessed without causing a memory error. 1340ce20dd8SAlexander Potapenko * 1350ce20dd8SAlexander Potapenko * kfence_ksize() returns the number of bytes requested for a KFENCE object at 1360ce20dd8SAlexander Potapenko * allocation time. This number may be less than the object size of the 1370ce20dd8SAlexander Potapenko * corresponding struct kmem_cache. 1380ce20dd8SAlexander Potapenko */ 1390ce20dd8SAlexander Potapenko size_t kfence_ksize(const void *addr); 1400ce20dd8SAlexander Potapenko 1410ce20dd8SAlexander Potapenko /** 1420ce20dd8SAlexander Potapenko * kfence_object_start() - find the beginning of a KFENCE object 1430ce20dd8SAlexander Potapenko * @addr: address within a KFENCE-allocated object 1440ce20dd8SAlexander Potapenko * 1450ce20dd8SAlexander Potapenko * Return: address of the beginning of the object. 1460ce20dd8SAlexander Potapenko * 1470ce20dd8SAlexander Potapenko * SL[AU]B-allocated objects are laid out within a page one by one, so it is 1480ce20dd8SAlexander Potapenko * easy to calculate the beginning of an object given a pointer inside it and 1490ce20dd8SAlexander Potapenko * the object size. The same is not true for KFENCE, which places a single 1500ce20dd8SAlexander Potapenko * object at either end of the page. This helper function is used to find the 1510ce20dd8SAlexander Potapenko * beginning of a KFENCE-allocated object. 1520ce20dd8SAlexander Potapenko */ 1530ce20dd8SAlexander Potapenko void *kfence_object_start(const void *addr); 1540ce20dd8SAlexander Potapenko 1550ce20dd8SAlexander Potapenko /** 1560ce20dd8SAlexander Potapenko * __kfence_free() - release a KFENCE heap object to KFENCE pool 1570ce20dd8SAlexander Potapenko * @addr: object to be freed 1580ce20dd8SAlexander Potapenko * 1590ce20dd8SAlexander Potapenko * Requires: is_kfence_address(addr) 1600ce20dd8SAlexander Potapenko * 1610ce20dd8SAlexander Potapenko * Release a KFENCE object and mark it as freed. 1620ce20dd8SAlexander Potapenko */ 1630ce20dd8SAlexander Potapenko void __kfence_free(void *addr); 1640ce20dd8SAlexander Potapenko 1650ce20dd8SAlexander Potapenko /** 1660ce20dd8SAlexander Potapenko * kfence_free() - try to release an arbitrary heap object to KFENCE pool 1670ce20dd8SAlexander Potapenko * @addr: object to be freed 1680ce20dd8SAlexander Potapenko * 1690ce20dd8SAlexander Potapenko * Return: 1700ce20dd8SAlexander Potapenko * * false - object doesn't belong to KFENCE pool and was ignored, 1710ce20dd8SAlexander Potapenko * * true - object was released to KFENCE pool. 1720ce20dd8SAlexander Potapenko * 1730ce20dd8SAlexander Potapenko * Release a KFENCE object and mark it as freed. May be called on any object, 1740ce20dd8SAlexander Potapenko * even non-KFENCE objects, to simplify integration of the hooks into the 1750ce20dd8SAlexander Potapenko * allocator's free codepath. The allocator must check the return value to 1760ce20dd8SAlexander Potapenko * determine if it was a KFENCE object or not. 1770ce20dd8SAlexander Potapenko */ 1780ce20dd8SAlexander Potapenko static __always_inline __must_check bool kfence_free(void *addr) 1790ce20dd8SAlexander Potapenko { 1800ce20dd8SAlexander Potapenko if (!is_kfence_address(addr)) 1810ce20dd8SAlexander Potapenko return false; 1820ce20dd8SAlexander Potapenko __kfence_free(addr); 1830ce20dd8SAlexander Potapenko return true; 1840ce20dd8SAlexander Potapenko } 1850ce20dd8SAlexander Potapenko 1860ce20dd8SAlexander Potapenko /** 1870ce20dd8SAlexander Potapenko * kfence_handle_page_fault() - perform page fault handling for KFENCE pages 1880ce20dd8SAlexander Potapenko * @addr: faulting address 189*bc8fbc5fSMarco Elver * @is_write: is access a write 190d438fabcSMarco Elver * @regs: current struct pt_regs (can be NULL, but shows full stack trace) 1910ce20dd8SAlexander Potapenko * 1920ce20dd8SAlexander Potapenko * Return: 1930ce20dd8SAlexander Potapenko * * false - address outside KFENCE pool, 1940ce20dd8SAlexander Potapenko * * true - page fault handled by KFENCE, no additional handling required. 1950ce20dd8SAlexander Potapenko * 1960ce20dd8SAlexander Potapenko * A page fault inside KFENCE pool indicates a memory error, such as an 1970ce20dd8SAlexander Potapenko * out-of-bounds access, a use-after-free or an invalid memory access. In these 1980ce20dd8SAlexander Potapenko * cases KFENCE prints an error message and marks the offending page as 1990ce20dd8SAlexander Potapenko * present, so that the kernel can proceed. 2000ce20dd8SAlexander Potapenko */ 201*bc8fbc5fSMarco Elver bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs *regs); 2020ce20dd8SAlexander Potapenko 2030ce20dd8SAlexander Potapenko #else /* CONFIG_KFENCE */ 2040ce20dd8SAlexander Potapenko 2050ce20dd8SAlexander Potapenko static inline bool is_kfence_address(const void *addr) { return false; } 2060ce20dd8SAlexander Potapenko static inline void kfence_alloc_pool(void) { } 2070ce20dd8SAlexander Potapenko static inline void kfence_init(void) { } 2080ce20dd8SAlexander Potapenko static inline void kfence_shutdown_cache(struct kmem_cache *s) { } 2090ce20dd8SAlexander Potapenko static inline void *kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) { return NULL; } 2100ce20dd8SAlexander Potapenko static inline size_t kfence_ksize(const void *addr) { return 0; } 2110ce20dd8SAlexander Potapenko static inline void *kfence_object_start(const void *addr) { return NULL; } 2120ce20dd8SAlexander Potapenko static inline void __kfence_free(void *addr) { } 2130ce20dd8SAlexander Potapenko static inline bool __must_check kfence_free(void *addr) { return false; } 214*bc8fbc5fSMarco Elver static inline bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write, 215*bc8fbc5fSMarco Elver struct pt_regs *regs) 216*bc8fbc5fSMarco Elver { 217*bc8fbc5fSMarco Elver return false; 218*bc8fbc5fSMarco Elver } 2190ce20dd8SAlexander Potapenko 2200ce20dd8SAlexander Potapenko #endif 2210ce20dd8SAlexander Potapenko 2220ce20dd8SAlexander Potapenko #endif /* _LINUX_KFENCE_H */ 223