xref: /openbmc/linux/include/linux/kfence.h (revision cabdf74e)
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 
1707e8481dSMarco Elver #include <linux/atomic.h>
1807e8481dSMarco Elver #include <linux/static_key.h>
1907e8481dSMarco Elver 
208913c610SPeng Liu extern unsigned long kfence_sample_interval;
218913c610SPeng Liu 
220ce20dd8SAlexander Potapenko /*
230ce20dd8SAlexander Potapenko  * We allocate an even number of pages, as it simplifies calculations to map
240ce20dd8SAlexander Potapenko  * address to metadata indices; effectively, the very first page serves as an
250ce20dd8SAlexander Potapenko  * extended guard page, but otherwise has no special purpose.
260ce20dd8SAlexander Potapenko  */
270ce20dd8SAlexander Potapenko #define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 * PAGE_SIZE)
280ce20dd8SAlexander Potapenko extern char *__kfence_pool;
290ce20dd8SAlexander Potapenko 
300ce20dd8SAlexander Potapenko DECLARE_STATIC_KEY_FALSE(kfence_allocation_key);
310ce20dd8SAlexander Potapenko extern atomic_t kfence_allocation_gate;
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  */
is_kfence_address(const void * addr)510ce20dd8SAlexander Potapenko static __always_inline bool is_kfence_address(const void *addr)
520ce20dd8SAlexander Potapenko {
530ce20dd8SAlexander Potapenko 	/*
54a7cb5d23SMarco Elver 	 * The __kfence_pool != NULL check is required to deal with the case
55a7cb5d23SMarco Elver 	 * where __kfence_pool == NULL && addr < KFENCE_POOL_SIZE. Keep it in
56a7cb5d23SMarco Elver 	 * the slow-path after the range-check!
570ce20dd8SAlexander Potapenko 	 */
58a7cb5d23SMarco Elver 	return unlikely((unsigned long)((char *)addr - __kfence_pool) < KFENCE_POOL_SIZE && __kfence_pool);
590ce20dd8SAlexander Potapenko }
600ce20dd8SAlexander Potapenko 
610ce20dd8SAlexander Potapenko /**
62*cabdf74eSPeng Zhang  * kfence_alloc_pool_and_metadata() - allocate the KFENCE pool and KFENCE
63*cabdf74eSPeng Zhang  * metadata via memblock
640ce20dd8SAlexander Potapenko  */
65*cabdf74eSPeng Zhang void __init kfence_alloc_pool_and_metadata(void);
660ce20dd8SAlexander Potapenko 
670ce20dd8SAlexander Potapenko /**
680ce20dd8SAlexander Potapenko  * kfence_init() - perform KFENCE initialization at boot time
690ce20dd8SAlexander Potapenko  *
70*cabdf74eSPeng Zhang  * Requires that kfence_alloc_pool_and_metadata() was called before. This sets
71*cabdf74eSPeng Zhang  * up the allocation gate timer, and requires that workqueues are available.
720ce20dd8SAlexander Potapenko  */
730ce20dd8SAlexander Potapenko void __init kfence_init(void);
740ce20dd8SAlexander Potapenko 
750ce20dd8SAlexander Potapenko /**
760ce20dd8SAlexander Potapenko  * kfence_shutdown_cache() - handle shutdown_cache() for KFENCE objects
770ce20dd8SAlexander Potapenko  * @s: cache being shut down
780ce20dd8SAlexander Potapenko  *
790ce20dd8SAlexander Potapenko  * Before shutting down a cache, one must ensure there are no remaining objects
800ce20dd8SAlexander Potapenko  * allocated from it. Because KFENCE objects are not referenced from the cache
810ce20dd8SAlexander Potapenko  * directly, we need to check them here.
820ce20dd8SAlexander Potapenko  *
830ce20dd8SAlexander Potapenko  * Note that shutdown_cache() is internal to SL*B, and kmem_cache_destroy() does
840ce20dd8SAlexander Potapenko  * not return if allocated objects still exist: it prints an error message and
850ce20dd8SAlexander Potapenko  * simply aborts destruction of a cache, leaking memory.
860ce20dd8SAlexander Potapenko  *
870ce20dd8SAlexander Potapenko  * If the only such objects are KFENCE objects, we will not leak the entire
880ce20dd8SAlexander Potapenko  * cache, but instead try to provide more useful debug info by making allocated
890ce20dd8SAlexander Potapenko  * objects "zombie allocations". Objects may then still be used or freed (which
900ce20dd8SAlexander Potapenko  * is handled gracefully), but usage will result in showing KFENCE error reports
910ce20dd8SAlexander Potapenko  * which include stack traces to the user of the object, the original allocation
920ce20dd8SAlexander Potapenko  * site, and caller to shutdown_cache().
930ce20dd8SAlexander Potapenko  */
940ce20dd8SAlexander Potapenko void kfence_shutdown_cache(struct kmem_cache *s);
950ce20dd8SAlexander Potapenko 
960ce20dd8SAlexander Potapenko /*
970ce20dd8SAlexander Potapenko  * Allocate a KFENCE object. Allocators must not call this function directly,
980ce20dd8SAlexander Potapenko  * use kfence_alloc() instead.
990ce20dd8SAlexander Potapenko  */
1000ce20dd8SAlexander Potapenko void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags);
1010ce20dd8SAlexander Potapenko 
1020ce20dd8SAlexander Potapenko /**
1030ce20dd8SAlexander Potapenko  * kfence_alloc() - allocate a KFENCE object with a low probability
1040ce20dd8SAlexander Potapenko  * @s:     struct kmem_cache with object requirements
1050ce20dd8SAlexander Potapenko  * @size:  exact size of the object to allocate (can be less than @s->size
1060ce20dd8SAlexander Potapenko  *         e.g. for kmalloc caches)
1070ce20dd8SAlexander Potapenko  * @flags: GFP flags
1080ce20dd8SAlexander Potapenko  *
1090ce20dd8SAlexander Potapenko  * Return:
1100ce20dd8SAlexander Potapenko  * * NULL     - must proceed with allocating as usual,
1110ce20dd8SAlexander Potapenko  * * non-NULL - pointer to a KFENCE object.
1120ce20dd8SAlexander Potapenko  *
1130ce20dd8SAlexander Potapenko  * kfence_alloc() should be inserted into the heap allocation fast path,
1140ce20dd8SAlexander Potapenko  * allowing it to transparently return KFENCE-allocated objects with a low
1150ce20dd8SAlexander Potapenko  * probability using a static branch (the probability is controlled by the
1160ce20dd8SAlexander Potapenko  * kfence.sample_interval boot parameter).
1170ce20dd8SAlexander Potapenko  */
kfence_alloc(struct kmem_cache * s,size_t size,gfp_t flags)1180ce20dd8SAlexander Potapenko static __always_inline void *kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
1190ce20dd8SAlexander Potapenko {
12007e8481dSMarco Elver #if defined(CONFIG_KFENCE_STATIC_KEYS) || CONFIG_KFENCE_SAMPLE_INTERVAL == 0
12107e8481dSMarco Elver 	if (!static_branch_unlikely(&kfence_allocation_key))
1220ce20dd8SAlexander Potapenko 		return NULL;
12307e8481dSMarco Elver #else
12407e8481dSMarco Elver 	if (!static_branch_likely(&kfence_allocation_key))
12507e8481dSMarco Elver 		return NULL;
12607e8481dSMarco Elver #endif
12707e8481dSMarco Elver 	if (likely(atomic_read(&kfence_allocation_gate)))
12807e8481dSMarco Elver 		return NULL;
12907e8481dSMarco Elver 	return __kfence_alloc(s, size, flags);
1300ce20dd8SAlexander Potapenko }
1310ce20dd8SAlexander Potapenko 
1320ce20dd8SAlexander Potapenko /**
1330ce20dd8SAlexander Potapenko  * kfence_ksize() - get actual amount of memory allocated for a KFENCE object
1340ce20dd8SAlexander Potapenko  * @addr: pointer to a heap object
1350ce20dd8SAlexander Potapenko  *
1360ce20dd8SAlexander Potapenko  * Return:
1370ce20dd8SAlexander Potapenko  * * 0     - not a KFENCE object, must call __ksize() instead,
1380ce20dd8SAlexander Potapenko  * * non-0 - this many bytes can be accessed without causing a memory error.
1390ce20dd8SAlexander Potapenko  *
1400ce20dd8SAlexander Potapenko  * kfence_ksize() returns the number of bytes requested for a KFENCE object at
1410ce20dd8SAlexander Potapenko  * allocation time. This number may be less than the object size of the
1420ce20dd8SAlexander Potapenko  * corresponding struct kmem_cache.
1430ce20dd8SAlexander Potapenko  */
1440ce20dd8SAlexander Potapenko size_t kfence_ksize(const void *addr);
1450ce20dd8SAlexander Potapenko 
1460ce20dd8SAlexander Potapenko /**
1470ce20dd8SAlexander Potapenko  * kfence_object_start() - find the beginning of a KFENCE object
1480ce20dd8SAlexander Potapenko  * @addr: address within a KFENCE-allocated object
1490ce20dd8SAlexander Potapenko  *
1500ce20dd8SAlexander Potapenko  * Return: address of the beginning of the object.
1510ce20dd8SAlexander Potapenko  *
1520ce20dd8SAlexander Potapenko  * SL[AU]B-allocated objects are laid out within a page one by one, so it is
1530ce20dd8SAlexander Potapenko  * easy to calculate the beginning of an object given a pointer inside it and
1540ce20dd8SAlexander Potapenko  * the object size. The same is not true for KFENCE, which places a single
1550ce20dd8SAlexander Potapenko  * object at either end of the page. This helper function is used to find the
1560ce20dd8SAlexander Potapenko  * beginning of a KFENCE-allocated object.
1570ce20dd8SAlexander Potapenko  */
1580ce20dd8SAlexander Potapenko void *kfence_object_start(const void *addr);
1590ce20dd8SAlexander Potapenko 
1600ce20dd8SAlexander Potapenko /**
1610ce20dd8SAlexander Potapenko  * __kfence_free() - release a KFENCE heap object to KFENCE pool
1620ce20dd8SAlexander Potapenko  * @addr: object to be freed
1630ce20dd8SAlexander Potapenko  *
1640ce20dd8SAlexander Potapenko  * Requires: is_kfence_address(addr)
1650ce20dd8SAlexander Potapenko  *
1660ce20dd8SAlexander Potapenko  * Release a KFENCE object and mark it as freed.
1670ce20dd8SAlexander Potapenko  */
1680ce20dd8SAlexander Potapenko void __kfence_free(void *addr);
1690ce20dd8SAlexander Potapenko 
1700ce20dd8SAlexander Potapenko /**
1710ce20dd8SAlexander Potapenko  * kfence_free() - try to release an arbitrary heap object to KFENCE pool
1720ce20dd8SAlexander Potapenko  * @addr: object to be freed
1730ce20dd8SAlexander Potapenko  *
1740ce20dd8SAlexander Potapenko  * Return:
1750ce20dd8SAlexander Potapenko  * * false - object doesn't belong to KFENCE pool and was ignored,
1760ce20dd8SAlexander Potapenko  * * true  - object was released to KFENCE pool.
1770ce20dd8SAlexander Potapenko  *
1780ce20dd8SAlexander Potapenko  * Release a KFENCE object and mark it as freed. May be called on any object,
1790ce20dd8SAlexander Potapenko  * even non-KFENCE objects, to simplify integration of the hooks into the
1800ce20dd8SAlexander Potapenko  * allocator's free codepath. The allocator must check the return value to
1810ce20dd8SAlexander Potapenko  * determine if it was a KFENCE object or not.
1820ce20dd8SAlexander Potapenko  */
kfence_free(void * addr)1830ce20dd8SAlexander Potapenko static __always_inline __must_check bool kfence_free(void *addr)
1840ce20dd8SAlexander Potapenko {
1850ce20dd8SAlexander Potapenko 	if (!is_kfence_address(addr))
1860ce20dd8SAlexander Potapenko 		return false;
1870ce20dd8SAlexander Potapenko 	__kfence_free(addr);
1880ce20dd8SAlexander Potapenko 	return true;
1890ce20dd8SAlexander Potapenko }
1900ce20dd8SAlexander Potapenko 
1910ce20dd8SAlexander Potapenko /**
1920ce20dd8SAlexander Potapenko  * kfence_handle_page_fault() - perform page fault handling for KFENCE pages
1930ce20dd8SAlexander Potapenko  * @addr: faulting address
194bc8fbc5fSMarco Elver  * @is_write: is access a write
195d438fabcSMarco Elver  * @regs: current struct pt_regs (can be NULL, but shows full stack trace)
1960ce20dd8SAlexander Potapenko  *
1970ce20dd8SAlexander Potapenko  * Return:
1980ce20dd8SAlexander Potapenko  * * false - address outside KFENCE pool,
1990ce20dd8SAlexander Potapenko  * * true  - page fault handled by KFENCE, no additional handling required.
2000ce20dd8SAlexander Potapenko  *
2010ce20dd8SAlexander Potapenko  * A page fault inside KFENCE pool indicates a memory error, such as an
2020ce20dd8SAlexander Potapenko  * out-of-bounds access, a use-after-free or an invalid memory access. In these
2030ce20dd8SAlexander Potapenko  * cases KFENCE prints an error message and marks the offending page as
2040ce20dd8SAlexander Potapenko  * present, so that the kernel can proceed.
2050ce20dd8SAlexander Potapenko  */
206bc8fbc5fSMarco Elver bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs *regs);
2070ce20dd8SAlexander Potapenko 
2082dfe63e6SMarco Elver #ifdef CONFIG_PRINTK
2092dfe63e6SMarco Elver struct kmem_obj_info;
2102dfe63e6SMarco Elver /**
2112dfe63e6SMarco Elver  * __kfence_obj_info() - fill kmem_obj_info struct
2122dfe63e6SMarco Elver  * @kpp: kmem_obj_info to be filled
2132dfe63e6SMarco Elver  * @object: the object
2142dfe63e6SMarco Elver  *
2152dfe63e6SMarco Elver  * Return:
2162dfe63e6SMarco Elver  * * false - not a KFENCE object
2172dfe63e6SMarco Elver  * * true - a KFENCE object, filled @kpp
2182dfe63e6SMarco Elver  *
2192dfe63e6SMarco Elver  * Copies information to @kpp for KFENCE objects.
2202dfe63e6SMarco Elver  */
2212dfe63e6SMarco Elver bool __kfence_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
2222dfe63e6SMarco Elver #endif
2232dfe63e6SMarco Elver 
2240ce20dd8SAlexander Potapenko #else /* CONFIG_KFENCE */
2250ce20dd8SAlexander Potapenko 
is_kfence_address(const void * addr)2260ce20dd8SAlexander Potapenko static inline bool is_kfence_address(const void *addr) { return false; }
kfence_alloc_pool_and_metadata(void)227*cabdf74eSPeng Zhang static inline void kfence_alloc_pool_and_metadata(void) { }
kfence_init(void)2280ce20dd8SAlexander Potapenko static inline void kfence_init(void) { }
kfence_shutdown_cache(struct kmem_cache * s)2290ce20dd8SAlexander Potapenko static inline void kfence_shutdown_cache(struct kmem_cache *s) { }
kfence_alloc(struct kmem_cache * s,size_t size,gfp_t flags)2300ce20dd8SAlexander Potapenko static inline void *kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) { return NULL; }
kfence_ksize(const void * addr)2310ce20dd8SAlexander Potapenko static inline size_t kfence_ksize(const void *addr) { return 0; }
kfence_object_start(const void * addr)2320ce20dd8SAlexander Potapenko static inline void *kfence_object_start(const void *addr) { return NULL; }
__kfence_free(void * addr)2330ce20dd8SAlexander Potapenko static inline void __kfence_free(void *addr) { }
kfence_free(void * addr)2340ce20dd8SAlexander Potapenko static inline bool __must_check kfence_free(void *addr) { return false; }
kfence_handle_page_fault(unsigned long addr,bool is_write,struct pt_regs * regs)235bc8fbc5fSMarco Elver static inline bool __must_check kfence_handle_page_fault(unsigned long addr, bool is_write,
236bc8fbc5fSMarco Elver 							 struct pt_regs *regs)
237bc8fbc5fSMarco Elver {
238bc8fbc5fSMarco Elver 	return false;
239bc8fbc5fSMarco Elver }
2400ce20dd8SAlexander Potapenko 
2412dfe63e6SMarco Elver #ifdef CONFIG_PRINTK
2422dfe63e6SMarco Elver struct kmem_obj_info;
__kfence_obj_info(struct kmem_obj_info * kpp,void * object,struct slab * slab)2432dfe63e6SMarco Elver static inline bool __kfence_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
2442dfe63e6SMarco Elver {
2452dfe63e6SMarco Elver 	return false;
2462dfe63e6SMarco Elver }
2472dfe63e6SMarco Elver #endif
2482dfe63e6SMarco Elver 
2490ce20dd8SAlexander Potapenko #endif
2500ce20dd8SAlexander Potapenko 
2510ce20dd8SAlexander Potapenko #endif /* _LINUX_KFENCE_H */
252