xref: /openbmc/linux/include/linux/kfence.h (revision a7cb5d23)
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 	/*
54*a7cb5d23SMarco Elver 	 * The __kfence_pool != NULL check is required to deal with the case
55*a7cb5d23SMarco Elver 	 * where __kfence_pool == NULL && addr < KFENCE_POOL_SIZE. Keep it in
56*a7cb5d23SMarco Elver 	 * the slow-path after the range-check!
570ce20dd8SAlexander Potapenko 	 */
58*a7cb5d23SMarco Elver 	return unlikely((unsigned long)((char *)addr - __kfence_pool) < KFENCE_POOL_SIZE && __kfence_pool);
590ce20dd8SAlexander Potapenko }
600ce20dd8SAlexander Potapenko 
610ce20dd8SAlexander Potapenko /**
620ce20dd8SAlexander Potapenko  * kfence_alloc_pool() - allocate the KFENCE pool via memblock
630ce20dd8SAlexander Potapenko  */
640ce20dd8SAlexander Potapenko void __init kfence_alloc_pool(void);
650ce20dd8SAlexander Potapenko 
660ce20dd8SAlexander Potapenko /**
670ce20dd8SAlexander Potapenko  * kfence_init() - perform KFENCE initialization at boot time
680ce20dd8SAlexander Potapenko  *
690ce20dd8SAlexander Potapenko  * Requires that kfence_alloc_pool() was called before. This sets up the
700ce20dd8SAlexander Potapenko  * allocation gate timer, and requires that workqueues are available.
710ce20dd8SAlexander Potapenko  */
720ce20dd8SAlexander Potapenko void __init kfence_init(void);
730ce20dd8SAlexander Potapenko 
740ce20dd8SAlexander Potapenko /**
750ce20dd8SAlexander Potapenko  * kfence_shutdown_cache() - handle shutdown_cache() for KFENCE objects
760ce20dd8SAlexander Potapenko  * @s: cache being shut down
770ce20dd8SAlexander Potapenko  *
780ce20dd8SAlexander Potapenko  * Before shutting down a cache, one must ensure there are no remaining objects
790ce20dd8SAlexander Potapenko  * allocated from it. Because KFENCE objects are not referenced from the cache
800ce20dd8SAlexander Potapenko  * directly, we need to check them here.
810ce20dd8SAlexander Potapenko  *
820ce20dd8SAlexander Potapenko  * Note that shutdown_cache() is internal to SL*B, and kmem_cache_destroy() does
830ce20dd8SAlexander Potapenko  * not return if allocated objects still exist: it prints an error message and
840ce20dd8SAlexander Potapenko  * simply aborts destruction of a cache, leaking memory.
850ce20dd8SAlexander Potapenko  *
860ce20dd8SAlexander Potapenko  * If the only such objects are KFENCE objects, we will not leak the entire
870ce20dd8SAlexander Potapenko  * cache, but instead try to provide more useful debug info by making allocated
880ce20dd8SAlexander Potapenko  * objects "zombie allocations". Objects may then still be used or freed (which
890ce20dd8SAlexander Potapenko  * is handled gracefully), but usage will result in showing KFENCE error reports
900ce20dd8SAlexander Potapenko  * which include stack traces to the user of the object, the original allocation
910ce20dd8SAlexander Potapenko  * site, and caller to shutdown_cache().
920ce20dd8SAlexander Potapenko  */
930ce20dd8SAlexander Potapenko void kfence_shutdown_cache(struct kmem_cache *s);
940ce20dd8SAlexander Potapenko 
950ce20dd8SAlexander Potapenko /*
960ce20dd8SAlexander Potapenko  * Allocate a KFENCE object. Allocators must not call this function directly,
970ce20dd8SAlexander Potapenko  * use kfence_alloc() instead.
980ce20dd8SAlexander Potapenko  */
990ce20dd8SAlexander Potapenko void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags);
1000ce20dd8SAlexander Potapenko 
1010ce20dd8SAlexander Potapenko /**
1020ce20dd8SAlexander Potapenko  * kfence_alloc() - allocate a KFENCE object with a low probability
1030ce20dd8SAlexander Potapenko  * @s:     struct kmem_cache with object requirements
1040ce20dd8SAlexander Potapenko  * @size:  exact size of the object to allocate (can be less than @s->size
1050ce20dd8SAlexander Potapenko  *         e.g. for kmalloc caches)
1060ce20dd8SAlexander Potapenko  * @flags: GFP flags
1070ce20dd8SAlexander Potapenko  *
1080ce20dd8SAlexander Potapenko  * Return:
1090ce20dd8SAlexander Potapenko  * * NULL     - must proceed with allocating as usual,
1100ce20dd8SAlexander Potapenko  * * non-NULL - pointer to a KFENCE object.
1110ce20dd8SAlexander Potapenko  *
1120ce20dd8SAlexander Potapenko  * kfence_alloc() should be inserted into the heap allocation fast path,
1130ce20dd8SAlexander Potapenko  * allowing it to transparently return KFENCE-allocated objects with a low
1140ce20dd8SAlexander Potapenko  * probability using a static branch (the probability is controlled by the
1150ce20dd8SAlexander Potapenko  * kfence.sample_interval boot parameter).
1160ce20dd8SAlexander Potapenko  */
1170ce20dd8SAlexander Potapenko static __always_inline void *kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
1180ce20dd8SAlexander Potapenko {
1190ce20dd8SAlexander Potapenko #ifdef CONFIG_KFENCE_STATIC_KEYS
1200ce20dd8SAlexander Potapenko 	if (static_branch_unlikely(&kfence_allocation_key))
1210ce20dd8SAlexander Potapenko #else
1220ce20dd8SAlexander Potapenko 	if (unlikely(!atomic_read(&kfence_allocation_gate)))
1230ce20dd8SAlexander Potapenko #endif
1240ce20dd8SAlexander Potapenko 		return __kfence_alloc(s, size, flags);
1250ce20dd8SAlexander Potapenko 	return NULL;
1260ce20dd8SAlexander Potapenko }
1270ce20dd8SAlexander Potapenko 
1280ce20dd8SAlexander Potapenko /**
1290ce20dd8SAlexander Potapenko  * kfence_ksize() - get actual amount of memory allocated for a KFENCE object
1300ce20dd8SAlexander Potapenko  * @addr: pointer to a heap object
1310ce20dd8SAlexander Potapenko  *
1320ce20dd8SAlexander Potapenko  * Return:
1330ce20dd8SAlexander Potapenko  * * 0     - not a KFENCE object, must call __ksize() instead,
1340ce20dd8SAlexander Potapenko  * * non-0 - this many bytes can be accessed without causing a memory error.
1350ce20dd8SAlexander Potapenko  *
1360ce20dd8SAlexander Potapenko  * kfence_ksize() returns the number of bytes requested for a KFENCE object at
1370ce20dd8SAlexander Potapenko  * allocation time. This number may be less than the object size of the
1380ce20dd8SAlexander Potapenko  * corresponding struct kmem_cache.
1390ce20dd8SAlexander Potapenko  */
1400ce20dd8SAlexander Potapenko size_t kfence_ksize(const void *addr);
1410ce20dd8SAlexander Potapenko 
1420ce20dd8SAlexander Potapenko /**
1430ce20dd8SAlexander Potapenko  * kfence_object_start() - find the beginning of a KFENCE object
1440ce20dd8SAlexander Potapenko  * @addr: address within a KFENCE-allocated object
1450ce20dd8SAlexander Potapenko  *
1460ce20dd8SAlexander Potapenko  * Return: address of the beginning of the object.
1470ce20dd8SAlexander Potapenko  *
1480ce20dd8SAlexander Potapenko  * SL[AU]B-allocated objects are laid out within a page one by one, so it is
1490ce20dd8SAlexander Potapenko  * easy to calculate the beginning of an object given a pointer inside it and
1500ce20dd8SAlexander Potapenko  * the object size. The same is not true for KFENCE, which places a single
1510ce20dd8SAlexander Potapenko  * object at either end of the page. This helper function is used to find the
1520ce20dd8SAlexander Potapenko  * beginning of a KFENCE-allocated object.
1530ce20dd8SAlexander Potapenko  */
1540ce20dd8SAlexander Potapenko void *kfence_object_start(const void *addr);
1550ce20dd8SAlexander Potapenko 
1560ce20dd8SAlexander Potapenko /**
1570ce20dd8SAlexander Potapenko  * __kfence_free() - release a KFENCE heap object to KFENCE pool
1580ce20dd8SAlexander Potapenko  * @addr: object to be freed
1590ce20dd8SAlexander Potapenko  *
1600ce20dd8SAlexander Potapenko  * Requires: is_kfence_address(addr)
1610ce20dd8SAlexander Potapenko  *
1620ce20dd8SAlexander Potapenko  * Release a KFENCE object and mark it as freed.
1630ce20dd8SAlexander Potapenko  */
1640ce20dd8SAlexander Potapenko void __kfence_free(void *addr);
1650ce20dd8SAlexander Potapenko 
1660ce20dd8SAlexander Potapenko /**
1670ce20dd8SAlexander Potapenko  * kfence_free() - try to release an arbitrary heap object to KFENCE pool
1680ce20dd8SAlexander Potapenko  * @addr: object to be freed
1690ce20dd8SAlexander Potapenko  *
1700ce20dd8SAlexander Potapenko  * Return:
1710ce20dd8SAlexander Potapenko  * * false - object doesn't belong to KFENCE pool and was ignored,
1720ce20dd8SAlexander Potapenko  * * true  - object was released to KFENCE pool.
1730ce20dd8SAlexander Potapenko  *
1740ce20dd8SAlexander Potapenko  * Release a KFENCE object and mark it as freed. May be called on any object,
1750ce20dd8SAlexander Potapenko  * even non-KFENCE objects, to simplify integration of the hooks into the
1760ce20dd8SAlexander Potapenko  * allocator's free codepath. The allocator must check the return value to
1770ce20dd8SAlexander Potapenko  * determine if it was a KFENCE object or not.
1780ce20dd8SAlexander Potapenko  */
1790ce20dd8SAlexander Potapenko static __always_inline __must_check bool kfence_free(void *addr)
1800ce20dd8SAlexander Potapenko {
1810ce20dd8SAlexander Potapenko 	if (!is_kfence_address(addr))
1820ce20dd8SAlexander Potapenko 		return false;
1830ce20dd8SAlexander Potapenko 	__kfence_free(addr);
1840ce20dd8SAlexander Potapenko 	return true;
1850ce20dd8SAlexander Potapenko }
1860ce20dd8SAlexander Potapenko 
1870ce20dd8SAlexander Potapenko /**
1880ce20dd8SAlexander Potapenko  * kfence_handle_page_fault() - perform page fault handling for KFENCE pages
1890ce20dd8SAlexander Potapenko  * @addr: faulting address
190bc8fbc5fSMarco Elver  * @is_write: is access a write
191d438fabcSMarco Elver  * @regs: current struct pt_regs (can be NULL, but shows full stack trace)
1920ce20dd8SAlexander Potapenko  *
1930ce20dd8SAlexander Potapenko  * Return:
1940ce20dd8SAlexander Potapenko  * * false - address outside KFENCE pool,
1950ce20dd8SAlexander Potapenko  * * true  - page fault handled by KFENCE, no additional handling required.
1960ce20dd8SAlexander Potapenko  *
1970ce20dd8SAlexander Potapenko  * A page fault inside KFENCE pool indicates a memory error, such as an
1980ce20dd8SAlexander Potapenko  * out-of-bounds access, a use-after-free or an invalid memory access. In these
1990ce20dd8SAlexander Potapenko  * cases KFENCE prints an error message and marks the offending page as
2000ce20dd8SAlexander Potapenko  * present, so that the kernel can proceed.
2010ce20dd8SAlexander Potapenko  */
202bc8fbc5fSMarco Elver bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs *regs);
2030ce20dd8SAlexander Potapenko 
2040ce20dd8SAlexander Potapenko #else /* CONFIG_KFENCE */
2050ce20dd8SAlexander Potapenko 
2060ce20dd8SAlexander Potapenko static inline bool is_kfence_address(const void *addr) { return false; }
2070ce20dd8SAlexander Potapenko static inline void kfence_alloc_pool(void) { }
2080ce20dd8SAlexander Potapenko static inline void kfence_init(void) { }
2090ce20dd8SAlexander Potapenko static inline void kfence_shutdown_cache(struct kmem_cache *s) { }
2100ce20dd8SAlexander Potapenko static inline void *kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) { return NULL; }
2110ce20dd8SAlexander Potapenko static inline size_t kfence_ksize(const void *addr) { return 0; }
2120ce20dd8SAlexander Potapenko static inline void *kfence_object_start(const void *addr) { return NULL; }
2130ce20dd8SAlexander Potapenko static inline void __kfence_free(void *addr) { }
2140ce20dd8SAlexander Potapenko static inline bool __must_check kfence_free(void *addr) { return false; }
215bc8fbc5fSMarco Elver static inline bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write,
216bc8fbc5fSMarco Elver 							 struct pt_regs *regs)
217bc8fbc5fSMarco Elver {
218bc8fbc5fSMarco Elver 	return false;
219bc8fbc5fSMarco Elver }
2200ce20dd8SAlexander Potapenko 
2210ce20dd8SAlexander Potapenko #endif
2220ce20dd8SAlexander Potapenko 
2230ce20dd8SAlexander Potapenko #endif /* _LINUX_KFENCE_H */
224