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