KFENCE: A low-overhead sampling-based memory safety error detector
Subsystems (13)
Marco Elver
maintainercover KFENCE: A low-overhead sampling-based memory safety error detector
This adds the Kernel Electric-Fence (KFENCE) infrastructure. KFENCE is a low-overhead sampling-based memory safety error detector of heap use-after-free, invalid-free, and out-of-bounds access errors. This series enables KFENCE for the x86 and arm64 architectures, and adds KFENCE hooks to the SLAB and SLUB allocators.
KFENCE is designed to be enabled in production kernels, and has near zero performance overhead. Compared to KASAN, KFENCE trades performance for precision. The main motivation behind KFENCE's design, is that with enough total uptime KFENCE will detect bugs in code paths not typically exercised by non-production test workloads. One way to quickly achieve a large enough total uptime is when the tool is deployed across a large fleet of machines.
KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error.
Guarded allocations are set up based on a sample interval (can be set via kfence.sample_interval). After expiration of the sample interval, a guarded allocation from the KFENCE object pool is returned to the main allocator (SLAB or SLUB). At this point, the timer is reset, and the next allocation is set up after the expiration of the interval.
To enable/disable a KFENCE allocation through the main allocator's fast-path without overhead, KFENCE relies on static branches via the static keys infrastructure. The static branch is toggled to redirect the allocation to KFENCE.
We have verified by running synthetic benchmarks (sysbench I/O, hackbench) that a kernel with KFENCE is performance-neutral compared to a non-KFENCE baseline kernel.
KFENCE is inspired by GWP-ASan [1], a userspace tool with similar properties. The name "KFENCE" is a homage to the Electric Fence Malloc Debugger [2].
For more details, see Documentation/dev-tools/kfence.rst added in the series -- also viewable here:
https://raw.githubusercontent.com/google/kasan/kfence/Documentation/dev-tools/kfence.rst
[1] http://llvm.org/docs/GwpAsan.html
[2] https://linux.die.net/man/3/efence
Alexander Potapenko (6):
mm: add Kernel Electric-Fence infrastructure x86, kfence: enable KFENCE for x86
mm, kfence: insert KFENCE hooks for SLAB
mm, kfence: insert KFENCE hooks for SLUB kfence, kasan: make KFENCE compatible with KASAN kfence, kmemleak: make KFENCE compatible with KMEMLEAK
Marco Elver (4):
arm64, kfence: enable KFENCE for ARM64 kfence, lockdep: make KFENCE compatible with lockdep kfence, Documentation: add KFENCE documentation
kfence: add test suite
Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++ MAINTAINERS | 11 + arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kfence.h | 39 ++ arch/arm64/mm/fault.c | 4 + arch/x86/Kconfig | 2 + arch/x86/include/asm/kfence.h | 60 +++ arch/x86/mm/fault.c | 4 + include/linux/kfence.h | 174 +++++++ init/main.c | 2 + kernel/locking/lockdep.c | 8 + lib/Kconfig.debug | 1 + lib/Kconfig.kfence | 70 +++ mm/Makefile | 1 + mm/kasan/common.c | 7 + mm/kfence/Makefile | 6 + mm/kfence/core.c | 730 +++++++++++++++++++++++++++ mm/kfence/kfence-test.c | 777 +++++++++++++++++++++++++++++ mm/kfence/kfence.h | 104 ++++ mm/kfence/report.c | 201 ++++++++ mm/kmemleak.c | 11 + mm/slab.c | 46 +- mm/slab_common.c | 6 +- mm/slub.c | 72 ++- 25 files changed, 2591 insertions(+), 32 deletions(-) create mode 100644 Documentation/dev-tools/kfence.rst create mode 100644 arch/arm64/include/asm/kfence.h create mode 100644 arch/x86/include/asm/kfence.h create mode 100644 include/linux/kfence.h create mode 100644 lib/Kconfig.kfence create mode 100644 mm/kfence/Makefile create mode 100644 mm/kfence/core.c create mode 100644 mm/kfence/kfence-test.c create mode 100644 mm/kfence/kfence.h create mode 100644 mm/kfence/report.c
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Marco Elver
maintainerRFC PATCH 1/10 mm: add Kernel Electric-Fence infrastructure
From: Alexander Potapenko <[email protected]>
This adds the Kernel Electric-Fence (KFENCE) infrastructure. KFENCE is a low-overhead sampling-based memory safety error detector of heap use-after-free, invalid-free, and out-of-bounds access errors.
KFENCE is designed to be enabled in production kernels, and has near zero performance overhead. Compared to KASAN, KFENCE trades performance for precision. The main motivation behind KFENCE's design, is that with enough total uptime KFENCE will detect bugs in code paths not typically exercised by non-production test workloads. One way to quickly achieve a large enough total uptime is when the tool is deployed across a large fleet of machines.
KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error.
Guarded allocations are set up based on a sample interval (can be set via kfence.sample_interval). After expiration of the sample interval, a guarded allocation from the KFENCE object pool is returned to the main allocator (SLAB or SLUB). At this point, the timer is reset, and the next allocation is set up after the expiration of the interval.
To enable/disable a KFENCE allocation through the main allocator's fast-path without overhead, KFENCE relies on static branches via the static keys infrastructure. The static branch is toggled to redirect the allocation to KFENCE. To date, we have verified by running synthetic benchmarks (sysbench I/O workloads) that a kernel compiled with KFENCE is performance-neutral compared to the non-KFENCE baseline.
For more details, see Documentation/dev-tools/kfence.rst (added later in the series).
--- MAINTAINERS | 11 + include/linux/kfence.h | 174 ++++++++++ init/main.c | 2 + lib/Kconfig.debug | 1 + lib/Kconfig.kfence | 58 ++++ mm/Makefile | 1 + mm/kfence/Makefile | 3 + mm/kfence/core.c | 730 +++++++++++++++++++++++++++++++++++++++++ mm/kfence/kfence.h | 104 ++++++ mm/kfence/report.c | 201 ++++++++++++ 10 files changed, 1285 insertions(+) create mode 100644 include/linux/kfence.h create mode 100644 lib/Kconfig.kfence create mode 100644 mm/kfence/Makefile create mode 100644 mm/kfence/core.c create mode 100644 mm/kfence/kfence.h create mode 100644 mm/kfence/report.c
MAINTAINERS +11 -0
@@ -9673,6 +9673,17 @@ F: include/linux/keyctl.h F: include/uapi/linux/keyctl.h F: security/keys/ +KFENCE +M: Alexander Potapenko <[email protected]> +M: Marco Elver <[email protected]> +R: Dmitry Vyukov <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/dev-tools/kfence.rst +F: include/linux/kfence.h +F: lib/Kconfig.kfence +F: mm/kfence/ + KFIFO M: Stefani Seibold <[email protected]> S: Maintained
include/linux/kfence.h new file +174 -0
174 lines not on this page · raw message
init/main.c +2 -0
@@ -39,6 +39,7 @@#include <linux/security.h> #include <linux/smp.h> #include <linux/profile.h> +#include <linux/kfence.h> #include <linux/rcupdate.h> #include <linux/moduleparam.h> #include <linux/kallsyms.h>
@@ -942,6 +943,7 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)hrtimers_init ();softirq_init ();timekeeping_init (); +kfence_init ();/* * For best initial stack canary entropy, prepare it after:
lib/Kconfig.debug +1 -0
@@ -880,6 +880,7 @@ config DEBUG_STACKOVERFLOW If in doubt, say "N". source "lib/Kconfig.kasan" +source "lib/Kconfig.kfence" endmenu # "Memory Debugging"
lib/Kconfig.kfence new file +58 -0
58 lines not on this page · raw message
mm/Makefile +1 -0
@@ -81,6 +81,7 @@ obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_SLUB) += slub.o obj-$(CONFIG_KASAN) += kasan/ +obj-$(CONFIG_KFENCE) += kfence/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-$(CONFIG_MEMTEST) += memtest.o
mm/kfence/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_KFENCE) := core.o report.o
mm/kfence/core.c new file +730 -0
730 lines not on this page · raw message
mm/kfence/kfence.h new file +104 -0
104 lines not on this page · raw message
mm/kfence/report.c new file +201 -0
201 lines not on this page · raw message
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Re: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
Quoted 34 lines · On Mon, 7 Sep 2020 15:40:46 +0200 Marco Elver <[email protected]> wrote:
From: Alexander Potapenko <[email protected]>
This adds the Kernel Electric-Fence (KFENCE) infrastructure. KFENCE is a low-overhead sampling-based memory safety error detector of heap use-after-free, invalid-free, and out-of-bounds access errors.
KFENCE is designed to be enabled in production kernels, and has near zero performance overhead. Compared to KASAN, KFENCE trades performance for precision. The main motivation behind KFENCE's design, is that with enough total uptime KFENCE will detect bugs in code paths not typically exercised by non-production test workloads. One way to quickly achieve a large enough total uptime is when the tool is deployed across a large fleet of machines.
KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error.
Guarded allocations are set up based on a sample interval (can be set via kfence.sample_interval). After expiration of the sample interval, a guarded allocation from the KFENCE object pool is returned to the main allocator (SLAB or SLUB). At this point, the timer is reset, and the next allocation is set up after the expiration of the interval.
To enable/disable a KFENCE allocation through the main allocator's fast-path without overhead, KFENCE relies on static branches via the static keys infrastructure. The static branch is toggled to redirect the allocation to KFENCE. To date, we have verified by running synthetic benchmarks (sysbench I/O workloads) that a kernel compiled with KFENCE is performance-neutral compared to the non-KFENCE baseline.
Quoted 6 lines
For more details, see
Documentation/dev-tools/kfence.rst(added later in the series).Co-developed-by Marco Elver <[email protected]>Signed-off-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>
Interesting bit of work. A few trivial things inline I spotted whilst having a first read through.
Thanks,
Jonathan
Quoted 5 lines
mm/kfence/core.c new file +5
-0+ +static void *kfence_guarded_alloc (struct kmem_cache *cache,size_t size,gfp_t gfp) +{ +/* +* Note: for allocations made before RNG initialization, will always
mm/kfence/core.c line 235
Quoted 10 lines
mm/kfence/core.c new file +10
-0+* return zero. We still benefit from enabling KFENCE as early as +* possible, even when the RNG is not yet available, as this will allow +* KFENCE to detect bugs due to earlier allocations. The only downside +* is that the out-of-bounds accesses detected are deterministic for +* such allocations. +*/ +const bool right =prandom_u32_max (2 ); +unsigned long flags; +struct kfence_metadata *meta =NULL ; +void *addr =NULL ;
I think this is set in all paths, so no need to initialize here.
Quoted 70 lines
mm/kfence/core.c new file +70
-0+ +/* Try to obtain a free object. */ +raw_spin_lock_irqsave (&kfence_freelist_lock, flags); +if (!list_empty (&kfence_freelist)) { + meta = list_entry(kfence_freelist.next, struct kfence_metadata, list); +list_del_init (&meta->list); + } +raw_spin_unlock_irqrestore (&kfence_freelist_lock, flags); +if (!meta) +return NULL ; + +if (unlikely (!raw_spin_trylock_irqsave (&meta->lock, flags))) { +/* +* This is extremely unlikely -- we are reporting on a +* use-after-free, which locked meta->lock, and the reporting +* code via printk calls kmalloc() which ends up in +* kfence_alloc() and tries to grab the same object that we're +* reporting on. While it has never been observed, lockdep does +* report that there is a possibility of deadlock. Fix it by +* using trylock and bailing out gracefully. +*/ +raw_spin_lock_irqsave (&kfence_freelist_lock, flags); +/* Put the object back on the freelist. */ +list_add_tail (&meta->list, &kfence_freelist); +raw_spin_unlock_irqrestore (&kfence_freelist_lock, flags); + +return NULL ; + } + + meta->addr =metadata_to_pageaddr (meta); +/* Unprotect if we're reusing this page. */ +if (meta->state ==KFENCE_OBJECT_FREED ) +kfence_unprotect (meta->addr); + +/* Calculate address for this allocation. */ +if (right) + meta->addr +=PAGE_SIZE - size; + meta->addr =ALIGN_DOWN (meta->addr, cache->align); + +/* Update remaining metadata. */ +metadata_update_state (meta,KFENCE_OBJECT_ALLOCATED ); +/* Pairs with READ_ONCE() in kfence_shutdown_cache(). */ +WRITE_ONCE (meta->cache, cache); + meta->size = right ? -size : size; +for_each_canary (meta, set_canary_byte); +virt_to_page (meta->addr)->slab_cache = cache; + +raw_spin_unlock_irqrestore (&meta->lock, flags); + +/* Memory initialization. */ + +/* +* We check slab_want_init_on_alloc() ourselves, rather than letting +* SL*B do the initialization, as otherwise we might overwrite KFENCE's +* redzone. +*/ + addr = (void *)meta->addr; +if (unlikely (slab_want_init_on_alloc (gfp, cache))) +memzero_explicit (addr, size); +if (cache->ctor) + cache->ctor (addr); + +if (CONFIG_KFENCE_FAULT_INJECTION && !prandom_u32_max (CONFIG_KFENCE_FAULT_INJECTION )) +kfence_protect (meta->addr);/* Random "faults" by protecting the object. */ + +atomic_long_inc (&counters[KFENCE_COUNTER_ALLOCATED ]); +atomic_long_inc (&counters[KFENCE_COUNTER_ALLOCS ]); + +return addr; +}
...
Quoted 9 lines
mm/kfence/core.c new file +9
-0+ +size_t kfence_ksize (const void *addr) +{ +const struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +/* +* Read locklessly -- if there is a race with __kfence_alloc(), this +* most certainly is either a use-after-free, or invalid access. +*/
mm/kfence/core.c line 644
Quoted 10 lines
mm/kfence/core.c new file +10
-0+return meta ?abs (meta->size) :0 ; +} + +void *kfence_object_start (const void *addr) +{ +const struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +/* +* Read locklessly -- if there is a race with __kfence_alloc(), this +* most certainly is either a use-after-free, or invalid access.
To my reading using "most certainly" makes this statement less clear
Read locklessly -- if there is a race with __kfence_alloc() this is either a use-after-free or invalid access.
Same for other cases of that particular "most certainly".
Quoted 22 lines
mm/kfence/core.c new file +22
-0+*/ +return meta ? (void *)meta->addr :NULL ; +} + +void __kfence_free (void *addr) +{ +struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +if (unlikely (meta->cache->flags &SLAB_TYPESAFE_BY_RCU )) +call_rcu (&meta->rcu_head, rcu_guarded_free); +else +kfence_guarded_free (addr, meta); +} + +bool kfence_handle_page_fault (unsigned long addr) +{ +const int page_index = (addr - (unsigned long )__kfence_pool) /PAGE_SIZE ; +struct kfence_metadata *to_report =NULL ; +enum kfence_error_type error_type; +unsigned long flags; + +if (!is_kfence_address ((void *)addr))
mm/kfence/core.c line 676
Quoted 10 lines
mm/kfence/core.c new file +10
-0+return false; + +if (!READ_ONCE (kfence_enabled))/* If disabled at runtime ... */ +return kfence_unprotect (addr);/* ... unprotect and proceed. */ + +atomic_long_inc (&counters[KFENCE_COUNTER_BUGS ]); + +if (page_index %2 ) { +/* This is a redzone, report a buffer overflow. */ +struct kfence_metadata *meta =NULL ;
Not need to set to NULL here as assigned 3 lines down.
Quoted 55 lines
mm/kfence/core.c new file +54
-0+int distance =0 ; + + meta = addr_to_metadata(addr - PAGE_SIZE) +if (meta &&READ_ONCE (meta->state) ==KFENCE_OBJECT_ALLOCATED ) { + to_report = meta; +/* Data race ok; distance calculation approximate. */ + distance = addr -data_race (meta->addr +abs (meta->size)); + } + + meta =addr_to_metadata (addr +PAGE_SIZE ); +if (meta &&READ_ONCE (meta->state) ==KFENCE_OBJECT_ALLOCATED ) { +/* Data race ok; distance calculation approximate. */ +if (!to_report || distance >data_race (meta->addr) - addr) + to_report = meta; + } + +if (!to_report) + goto out; + +raw_spin_lock_irqsave (&to_report->lock, flags); + to_report->unprotected_page = addr; + error_type =KFENCE_ERROR_OOB ; + +/* +* If the object was freed before we took the look we can still +* report this as an OOB -- the report will simply show the +* stacktrace of the free as well. +*/ + }else { + to_report =addr_to_metadata (addr); +if (!to_report) + goto out; + +raw_spin_lock_irqsave (&to_report->lock, flags); + error_type =KFENCE_ERROR_UAF ; +/* +* We may race with __kfence_alloc(), and it is possible that a +* freed object may be reallocated. We simply report this as a +* use-after-free, with the stack trace showing the place where +* the object was re-allocated. +*/ + } + +out: +if (to_report) { +kfence_report_error (addr, to_report, error_type); +raw_spin_unlock_irqrestore (&to_report->lock, flags); + }else { +/* This may be a UAF or OOB access, but we can't be sure. */ +kfence_report_error (addr,NULL ,KFENCE_ERROR_INVALID ); + } + +return kfence_unprotect (addr);/* Unprotect and let access proceed. */ +}
...
Marco Elver
maintainerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
On Mon, 7 Sep 2020 at 17:43, Jonathan Cameron
<[email protected]> wrote:
...
Quoted 6 lines
Interesting bit of work. A few trivial things inline I spotted whilst having a first read through.
Thanks,
Jonathan
Thank you for having a look! We'll address these for v2.
Thanks,
-- Marco
Dmitry Vyukov
reviewerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
In reply to Marco Elverlib/Kconfig.kfence line 46
Quoted 8 lines · Marco Elver
lib/Kconfig.kfence new file +8
-0+config KFENCE_NUM_OBJECTS + int "Number of guarded objects available" + default 255 + range 1 65535 + help + The number of guarded objects available. For each KFENCE object, 2 + pages are required; with one containing the object and two adjacent + ones used as guard pages.
Hi Marco,
Wonder if you tested build/boot with KFENCE_NUM_OBJECTS=65535? Can a compiler create such a large object?
lib/Kconfig.kfence line 56
Quoted 9 lines
lib/Kconfig.kfence new file +9
-0+config KFENCE_FAULT_INJECTION + int "Fault injection for stress testing" + default 0 + depends on EXPERT + help + The inverse probability with which to randomly protect KFENCE object + pages, resulting in spurious use-after-frees. The main purpose of + this option is to stress-test KFENCE with concurrent error reports + and allocations/frees. A value of 0 disables fault injection.
I would name this differently. "FAULT_INJECTION" is already taken for a different thing, so it's a bit confusing. KFENCE_DEBUG_SOMETHING may be a better name.
It would also be good to make it very clear in the short description that this is for testing of KFENCE itself. When I configure syzbot I routinely can't figure out if various DEBUG configs detect user errors, or enable additional unit tests, or something else. Maybe it should depend on DEBUG_KERNEL as well?
mm/kfence/kfence.h line 25
Quoted 6 lines
mm/kfence/kfence.h new file +6
-0+/* +* Get the canary byte pattern for @addr. Use a pattern that varies based on the +* lower 3 bits of the address, to detect memory corruptions with higher +* probability, where similar constants are used. +*/ +#define KFENCE_CANARY_PATTERN (addr) ((u8)0xaa ^ (u8)((unsigned long)addr & 0x7))
(addr) in macro body
mm/kfence/report.c line 96
Quoted 2 lines
mm/kfence/report.c new file +2
-0+seq_con_printf (seq, + "kfence-#%zd [0x" PTR_FMT "-0x" PTR_FMT
PTR_FMT is only used in this file, should it be declared in report.c?
Please post example reports somewhere. It's hard to figure out all details of the reporting/formatting.
Marco Elver
maintainerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
Quoted 11 lines · Dmitry Vyukov
lib/Kconfig.kfence line 46
Quoted 8 lines · Marco Elver
lib/Kconfig.kfence new file +8
-0+config KFENCE_NUM_OBJECTS + int "Number of guarded objects available" + default 255 + range 1 65535 + help + The number of guarded objects available. For each KFENCE object, 2 + pages are required; with one containing the object and two adjacent + ones used as guard pages.
Quoted 4 lines
Hi Marco,
Wonder if you tested build/boot with KFENCE_NUM_OBJECTS=65535? Can a compiler create such a large object?
Indeed, I get a "ld: kernel image bigger than KERNEL_IMAGE_SIZE". Let's lower it to something more reasonable.
The main reason to have the limit is to constrain random configs and avoid the inevitable error reports.
Quoted 10 lines
lib/Kconfig.kfence line 56
Quoted 9 lines
lib/Kconfig.kfence new file +9
-0+config KFENCE_FAULT_INJECTION + int "Fault injection for stress testing" + default 0 + depends on EXPERT + help + The inverse probability with which to randomly protect KFENCE object + pages, resulting in spurious use-after-frees. The main purpose of + this option is to stress-test KFENCE with concurrent error reports + and allocations/frees. A value of 0 disables fault injection.
Quoted 7 lines
I would name this differently. "
FAULT_INJECTION" is already taken for a different thing, so it's a bit confusing.KFENCE_DEBUG_SOMETHINGmay be a better name.
It would also be good to make it very clear in the short description that this is for testing of KFENCE itself. When I configure syzbot I routinely can't figure out if various DEBUG configs detect user errors, or enable additional unit tests, or something else.
Makes sense, we'll change the name.
Quoted 1 line
Maybe it should depend on
DEBUG_KERNELas well?
EXPERT selects DEBUG_KERNEL, so depending on DEBUG_KERNEL doesn't make sense.
Quoted 8 lines
mm/kfence/kfence.h line 25
Quoted 6 lines
mm/kfence/kfence.h new file +6
-0+/* +* Get the canary byte pattern for @addr. Use a pattern that varies based on the +* lower 3 bits of the address, to detect memory corruptions with higher +* probability, where similar constants are used. +*/ +#define KFENCE_CANARY_PATTERN (addr) ((u8)0xaa ^ (u8)((unsigned long)addr & 0x7))(addr) in macro body
Done for v2.
Quoted 4 lines
mm/kfence/report.c line 96
Quoted 2 lines
mm/kfence/report.c new file +2
-0+seq_con_printf (seq, + "kfence-#%zd [0x" PTR_FMT "-0x" PTR_FMT
PTR_FMTis only used in this file, should it be declared inreport.c?
It's also used by the test.
Quoted 2 lines
Please post example reports somewhere. It's hard to figure out all details of the reporting/formatting.
They can be seen in Documentation added later in the series (also
viewable here: https://github.com/google/kasan/blob/kfence/Documentation/dev-tools/kfence.rst)
Thank you!
-- Marco
Dmitry Vyukov
reviewerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
Quoted 61 lines · Marco Elver
Quoted 10 lines · Marco Elver
lib/Kconfig.kfence line 46
Quoted 8 lines
lib/Kconfig.kfence new file +8
-0+config KFENCE_NUM_OBJECTS + int "Number of guarded objects available" + default 255 + range 1 65535 + help + The number of guarded objects available. For each KFENCE object, 2 + pages are required; with one containing the object and two adjacent + ones used as guard pages.Quoted 4 lines
Hi Marco,
Wonder if you tested build/boot with KFENCE_NUM_OBJECTS=65535? Can a compiler create such a large object?
Indeed, I get a "ld: kernel image bigger than
KERNEL_IMAGE_SIZE". Let's lower it to something more reasonable.The main reason to have the limit is to constrain random configs and avoid the inevitable error reports.
Quoted 10 lines
lib/Kconfig.kfence line 56
Quoted 9 lines
lib/Kconfig.kfence new file +9
-0+config KFENCE_FAULT_INJECTION + int "Fault injection for stress testing" + default 0 + depends on EXPERT + help + The inverse probability with which to randomly protect KFENCE object + pages, resulting in spurious use-after-frees. The main purpose of + this option is to stress-test KFENCE with concurrent error reports + and allocations/frees. A value of 0 disables fault injection.Quoted 7 lines
I would name this differently. "
FAULT_INJECTION" is already taken for a different thing, so it's a bit confusing.KFENCE_DEBUG_SOMETHINGmay be a better name.
It would also be good to make it very clear in the short description that this is for testing of KFENCE itself. When I configure syzbot I routinely can't figure out if various DEBUG configs detect user errors, or enable additional unit tests, or something else.Makes sense, we'll change the name.
Quoted 1 line
Maybe it should depend on
DEBUG_KERNELas well?EXPERT selects
DEBUG_KERNEL, so depending onDEBUG_KERNELdoesn't make sense.Quoted 8 lines
mm/kfence/kfence.h line 25
Quoted 6 lines
mm/kfence/kfence.h new file +6
-0+/* +* Get the canary byte pattern for @addr. Use a pattern that varies based on the +* lower 3 bits of the address, to detect memory corruptions with higher +* probability, where similar constants are used. +*/ +#define KFENCE_CANARY_PATTERN (addr) ((u8)0xaa ^ (u8)((unsigned long)addr & 0x7))(addr) in macro body
Done for v2.
Quoted 4 lines
mm/kfence/report.c line 96
Quoted 2 lines
mm/kfence/report.c new file +2
-0+seq_con_printf (seq, + "kfence-#%zd [0x" PTR_FMT "-0x" PTR_FMT
PTR_FMTis only used in this file, should it be declared inreport.c?
Quoted 7 lines
It's also used by the test.
Quoted 2 lines
Please post example reports somewhere. It's hard to figure out all details of the reporting/formatting.
They can be seen in Documentation added later in the series (also
viewable here: https://github.com/google/kasan/blob/kfence/Documentation/dev-tools/kfence.rst)
Looking at the first report. I got impression we are trying to skip __kfence frames, but this includes it:
kfence-#17 [0xffffffffb672f000-0xffffffffb672f01f, size=32, cache=kmalloc-32] allocated in:
__kfence_alloc+0x42d/0x4c0 __kmalloc+0x133/0x200
Is it working as intended?
Marco Elver
maintainerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
Quoted 75 lines · Dmitry Vyukov
Quoted 61 lines · Marco Elver
Quoted 10 lines · Marco Elver
lib/Kconfig.kfence line 46
Quoted 8 lines
lib/Kconfig.kfence new file +8
-0+config KFENCE_NUM_OBJECTS + int "Number of guarded objects available" + default 255 + range 1 65535 + help + The number of guarded objects available. For each KFENCE object, 2 + pages are required; with one containing the object and two adjacent + ones used as guard pages.Quoted 4 lines
Hi Marco,
Wonder if you tested build/boot with KFENCE_NUM_OBJECTS=65535? Can a compiler create such a large object?
Indeed, I get a "ld: kernel image bigger than
KERNEL_IMAGE_SIZE". Let's lower it to something more reasonable.The main reason to have the limit is to constrain random configs and avoid the inevitable error reports.
Quoted 10 lines
lib/Kconfig.kfence line 56
Quoted 9 lines
lib/Kconfig.kfence new file +9
-0+config KFENCE_FAULT_INJECTION + int "Fault injection for stress testing" + default 0 + depends on EXPERT + help + The inverse probability with which to randomly protect KFENCE object + pages, resulting in spurious use-after-frees. The main purpose of + this option is to stress-test KFENCE with concurrent error reports + and allocations/frees. A value of 0 disables fault injection.Quoted 7 lines
I would name this differently. "
FAULT_INJECTION" is already taken for a different thing, so it's a bit confusing.KFENCE_DEBUG_SOMETHINGmay be a better name.
It would also be good to make it very clear in the short description that this is for testing of KFENCE itself. When I configure syzbot I routinely can't figure out if various DEBUG configs detect user errors, or enable additional unit tests, or something else.Makes sense, we'll change the name.
Quoted 1 line
Maybe it should depend on
DEBUG_KERNELas well?EXPERT selects
DEBUG_KERNEL, so depending onDEBUG_KERNELdoesn't make sense.Quoted 8 lines
mm/kfence/kfence.h line 25
Quoted 6 lines
mm/kfence/kfence.h new file +6
-0+/* +* Get the canary byte pattern for @addr. Use a pattern that varies based on the +* lower 3 bits of the address, to detect memory corruptions with higher +* probability, where similar constants are used. +*/ +#define KFENCE_CANARY_PATTERN (addr) ((u8)0xaa ^ (u8)((unsigned long)addr & 0x7))(addr) in macro body
Done for v2.
Quoted 4 lines
mm/kfence/report.c line 96
Quoted 2 lines
mm/kfence/report.c new file +2
-0+seq_con_printf (seq, + "kfence-#%zd [0x" PTR_FMT "-0x" PTR_FMT
PTR_FMTis only used in this file, should it be declared inreport.c?Quoted 7 lines
It's also used by the test.
Quoted 2 lines
Please post example reports somewhere. It's hard to figure out all details of the reporting/formatting.
They can be seen in Documentation added later in the series (also
viewable here: https://github.com/google/kasan/blob/kfence/Documentation/dev-tools/kfence.rst)Looking at the first report. I got impression we are trying to skip
__kfenceframes, but this includes it:
Quoted 6 lines
kfence-#17 [0xffffffffb672f000-0xffffffffb672f01f, size=32, cache=kmalloc-32] allocated in:
__kfence_alloc+0x42d/0x4c0 __kmalloc+0x133/0x200Is it working as intended?
We're not skipping them for the allocation/free stacks. We can skip the kfence+kmalloc frame as well.
Dmitry Vyukov
reviewerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
In reply to Marco Elvermm/kfence/core.c line 273
Quoted 9 lines · Marco Elver
mm/kfence/core.c new file +9
-0+ meta->addr =metadata_to_pageaddr (meta); +/* Unprotect if we're reusing this page. */ +if (meta->state ==KFENCE_OBJECT_FREED ) +kfence_unprotect (meta->addr); + +/* Calculate address for this allocation. */ +if (right) + meta->addr +=PAGE_SIZE - size; + meta->addr =ALIGN_DOWN (meta->addr, cache->align);
I would move this ALIGN_DOWN under the (right) if.
Do I understand it correctly that it will work, but we expect it to do nothing for !right? If cache align is >PAGE_SIZE, nothing good will happen anyway, right?
The previous 2 lines look like part of the same calculation -- "figure out the addr for the right case".
Quoted 266 lines
mm/kfence/core.c new file +266
-0+/* Update remaining metadata. */ +metadata_update_state (meta,KFENCE_OBJECT_ALLOCATED ); +/* Pairs with READ_ONCE() in kfence_shutdown_cache(). */ +WRITE_ONCE (meta->cache, cache); + meta->size = right ? -size : size; +for_each_canary (meta, set_canary_byte); +virt_to_page (meta->addr)->slab_cache = cache; + +raw_spin_unlock_irqrestore (&meta->lock, flags); + +/* Memory initialization. */ + +/* +* We check slab_want_init_on_alloc() ourselves, rather than letting +* SL*B do the initialization, as otherwise we might overwrite KFENCE's +* redzone. +*/ + addr = (void *)meta->addr; +if (unlikely (slab_want_init_on_alloc (gfp, cache))) +memzero_explicit (addr, size); +if (cache->ctor) + cache->ctor (addr); + +if (CONFIG_KFENCE_FAULT_INJECTION && !prandom_u32_max (CONFIG_KFENCE_FAULT_INJECTION )) +kfence_protect (meta->addr);/* Random "faults" by protecting the object. */ + +atomic_long_inc (&counters[KFENCE_COUNTER_ALLOCATED ]); +atomic_long_inc (&counters-F [KFENCE_COUNTER_ALLOCS ]); +return addr; +} + +static void kfence_guarded_free (void *addr,struct kfence_metadata *meta) +{ +struct kcsan_scoped_access assert_page_exclusive; +unsigned long flags; + +raw_spin_lock_irqsave (&meta->lock, flags); + +if (meta->state !=KFENCE_OBJECT_ALLOCATED || meta->addr != (unsigned long )addr) { +/* Invalid or double-free, bail out. */ +atomic_long_inc (&counters[KFENCE_COUNTER_BUGS ]); +kfence_report_error ((unsigned long )addr, meta,KFENCE_ERROR_INVALID_FREE ); +raw_spin_unlock_irqrestore (&meta->lock, flags); +return ; + } + +/* Detect racy use-after-free, or incorrect reallocation of this page by KFENCE. */ +kcsan_begin_scoped_access ((void *)ALIGN_DOWN ((unsigned long )addr,PAGE_SIZE ),PAGE_SIZE , +KCSAN_ACCESS_SCOPED |KCSAN_ACCESS_WRITE |KCSAN_ACCESS_ASSERT , + &assert_page_exclusive); + +if (CONFIG_KFENCE_FAULT_INJECTION ) +kfence_unprotect ((unsigned long )addr);/* To check canary bytes. */ + +/* Restore page protection if there was an OOB access. */ +if (meta->unprotected_page) { +kfence_protect (meta->unprotected_page); + meta->unprotected_page =0 ; + } + +/* Check canary bytes for memory corruption. */ +for_each_canary (meta, check_canary_byte); + +/* +* Clear memory if init-on-free is set. While we protect the page, the +* data is still there, and after a use-after-free is detected, we +* unprotect the page, so the data is still accessible. +*/ +if (unlikely (slab_want_init_on_free (meta->cache))) +memzero_explicit (addr,abs (meta->size)); + +/* Mark the object as freed. */ +metadata_update_state (meta,KFENCE_OBJECT_FREED ); + +raw_spin_unlock_irqrestore (&meta->lock, flags); + +/* Protect to detect use-after-frees. */ +kfence_protect ((unsigned long )addr); + +/* Add it to the tail of the freelist for reuse. */ +raw_spin_lock_irqsave (&kfence_freelist_lock, flags); +KFENCE_WARN_ON (!list_empty (&meta->list)); +list_add_tail (&meta->list, &kfence_freelist); +kcsan_end_scoped_access (&assert_page_exclusive); +raw_spin_unlock_irqrestore (&kfence_freelist_lock, flags); + +atomic_long_dec (&counters[KFENCE_COUNTER_ALLOCATED ]); +atomic_long_inc (&counters[KFENCE_COUNTER_FREES ]); +} + +static void rcu_guarded_free (struct rcu_head *h) +{ + struct kfence_metadata *meta = container_of(h, struct kfence_metadata, rcu_head); + +kfence_guarded_free ((void *)meta->addr, meta); +} + +static bool __init kfence_initialize_pool (void ) +{ +unsigned long addr; +struct page *pages; +int i; + +if (!arch_kfence_initialize_pool ()) +return false; + + addr = (unsigned long )__kfence_pool; + pages =virt_to_page (addr); + +/* +* Set up non-redzone pages: they must have PG_slab set, to avoid +* freeing these as real pages. +* +* We also want to avoid inserting kfence_free() in the kfree() +* fast-path in SLUB, and therefore need to ensure kfree() correctly +* enters __slab_free() slow-path. +*/ +for (i =0 ; i <KFENCE_POOL_SIZE /PAGE_SIZE ; i++) { +if (!i || (i %2 )) +continue ; + +__SetPageSlab (&pages[i]); + } + +/* +* Protect the first 2 pages. The first page is mostly unnecessary, and +* merely serves as an extended guard page. However, adding one +* additional page in the beginning gives us an even number of pages, +* which simplifies the mapping of address to metadata index. +*/ +for (i =0 ; i <2 ; i++) { +if (unlikely (!kfence_protect (addr))) +return false; + + addr +=PAGE_SIZE ; + } + +for (i =0 ; i <CONFIG_KFENCE_NUM_OBJECTS ; i++) { +struct kfence_metadata *meta = &kfence_metadata[i]; + +/* Initialize metadata. */ +INIT_LIST_HEAD (&meta->list); +raw_spin_lock_init (&meta->lock); + meta->state =KFENCE_OBJECT_UNUSED ; + meta->addr = addr;/* Initialize for validation in metadata_to_pageaddr(). */ +list_add_tail (&meta->list, &kfence_freelist); + +/* Protect the right redzone. */ +if (unlikely (!kfence_protect (addr +PAGE_SIZE ))) +return false; + + addr +=2 *PAGE_SIZE ; + } + +return true; +} + +/* === DebugFS Interface ==================================================== */ + +static int stats_show (struct seq_file *seq,void *v) +{ +int i; + +seq_printf (seq,"enabled: %i\n" ,READ_ONCE (kfence_enabled)); +for (i =0 ; i <KFENCE_COUNTER_COUNT ; i++) +seq_printf (seq,"%s: %ld\n" , counter_names[i],atomic_long_read (&counters[i])); + +return 0 ; +} +DEFINE_SHOW_ATTRIBUTE (stats); + +/* +* debugfs seq_file operations for /sys/kernel/debug/kfence/objects. +* start_object() and next_object() return the object index + 1, because NULL is used +* to stop iteration. +*/ +static void *start_object (struct seq_file *seq,loff_t *pos) +{ +if (*pos <CONFIG_KFENCE_NUM_OBJECTS ) +return (void *)((long )*pos +1 ); +return NULL ; +} + +static void stop_object (struct seq_file *seq,void *v) +{ +} + +static void *next_object (struct seq_file *seq,void *v,loff_t *pos) +{ + ++*pos; +if (*pos <CONFIG_KFENCE_NUM_OBJECTS ) +return (void *)((long )*pos +1 ); +return NULL ; +} + +static int show_object (struct seq_file *seq,void *v) +{ +struct kfence_metadata *meta = &kfence_metadata[(long )v -1 ]; +unsigned long flags; + +raw_spin_lock_irqsave (&meta->lock, flags); +kfence_print_object (seq, meta); +raw_spin_unlock_irqrestore (&meta->lock, flags); +seq_puts (seq,"---------------------------------\n" ); + +return 0 ; +} + +static const struct seq_operations object_seqops = { + .start = start_object, + .next = next_object, + .stop = stop_object, + .show = show_object, +}; + +static int open_objects (struct inode *inode,struct file *file) +{ +return seq_open (file, &object_seqops); +} + +static const struct file_operations objects_fops = { + .open = open_objects, + .read = seq_read, + .llseek = seq_lseek, +}; + +static int __init kfence_debugfs_init (void ) +{ +struct dentry *kfence_dir =debugfs_create_dir ("kfence" ,NULL ); + +debugfs_create_file ("stats" ,0400 , kfence_dir,NULL , &stats_fops); +debugfs_create_file ("objects" ,0400 , kfence_dir,NULL , &objects_fops); +return 0 ; +} + +late_initcall (kfence_debugfs_init); + +/* === Allocation Gate Timer ================================================ */ + +/* +* Set up delayed work, which will enable and disable the static key. We need to +* use a work queue (rather than a simple timer), since enabling and disabling a +* static key cannot be done from an interrupt. +*/ +static struct delayed_work kfence_timer; +static void toggle_allocation_gate (struct work_struct *work) +{ +if (!READ_ONCE (kfence_enabled)) +return ; + +/* Enable static key, and await allocation to happen. */ +atomic_set (&allocation_gate,0 ); +static_branch_enable (&kfence_allocation_key); +wait_event (allocation_wait,atomic_read (&allocation_gate) !=0 ); + +/* Disable static key and reset timer. */ +static_branch_disable (&kfence_allocation_key); +schedule_delayed_work (&kfence_timer,msecs_to_jiffies (kfence_sample_interval)); +} +static DECLARE_DELAYED_WORK(kfence_timer, toggle_allocation_gate); + +/* === Public interface ===================================================== */ + +void __init kfence_init (void ) +{ +/* Setting kfence_sample_interval to 0 on boot disables KFENCE. */
mm/kfence/core.c line 551
Quoted 10 lines
mm/kfence/core.c new file +10
-0+if (!kfence_sample_interval) +return ; + +if (!kfence_initialize_pool ()) { +pr_err ("%s failed\n" , __func__); +return ; + } + +schedule_delayed_work (&kfence_timer,0 ); +WRITE_ONCE (kfence_enabled, true);
Can toggle_allocation_gate run before we set kfence_enabled? If yes, it can break. If not, it's still somewhat confusing.
Quoted 92 lines
mm/kfence/core.c new file +92
-0+pr_info ("initialized - using %zu bytes for %d objects" ,KFENCE_POOL_SIZE , +CONFIG_KFENCE_NUM_OBJECTS ); +if (IS_ENABLED (CONFIG_DEBUG_KERNEL )) +pr_cont (" at 0x%px-0x%px\n" , (void *)__kfence_pool, + (void *)(__kfence_pool +KFENCE_POOL_SIZE )); +else +pr_cont ("\n" ); +} + +bool kfence_shutdown_cache (struct kmem_cache *s) +{ +unsigned long flags; +struct kfence_metadata *meta; +int i; + +for (i =0 ; i <CONFIG_KFENCE_NUM_OBJECTS ; i++) { +bool in_use; + + meta = &kfence_metadata[i]; + +/* +* If we observe some inconsistent cache and state pair where we +* should have returned false here, cache destruction is racing +* with either kmem_cache_alloc() or kmem_cache_free(). Taking +* the lock will not help, as different critical section +* serialization will have the same outcome. +*/ +if (READ_ONCE (meta->cache) != s || +READ_ONCE (meta->state) !=KFENCE_OBJECT_ALLOCATED ) +continue ; + +raw_spin_lock_irqsave (&meta->lock, flags); + in_use = meta->cache == s && meta->state ==KFENCE_OBJECT_ALLOCATED ; +raw_spin_unlock_irqrestore (&meta->lock, flags); + +if (in_use) +return false; + } + +for (i =0 ; i <CONFIG_KFENCE_NUM_OBJECTS ; i++) { + meta = &kfence_metadata[i]; + +/* See above. */ +if (READ_ONCE (meta->cache) != s ||READ_ONCE (meta->state) !=KFENCE_OBJECT_FREED ) +continue ; + +raw_spin_lock_irqsave (&meta->lock, flags); +if (meta->cache == s && meta->state ==KFENCE_OBJECT_FREED ) + meta->cache =NULL ; +raw_spin_unlock_irqrestore (&meta->lock, flags); + } + +return true; +} + +void *__kfence_alloc (struct kmem_cache *s,size_t size,gfp_t flags) +{ +/* +* allocation_gate only needs to become non-zero, so it doesn't make +* sense to continue writing to it and pay the associated contention +* cost, in case we have a large number of concurrent allocations. +*/ +if (atomic_read (&allocation_gate) ||atomic_inc_return (&allocation_gate) >1 ) +return NULL ; +wake_up (&allocation_wait); + +if (!READ_ONCE (kfence_enabled)) +return NULL ; + +if (size >PAGE_SIZE ) +return NULL ; + +return kfence_guarded_alloc (s, size, flags); +} + +size_t kfence_ksize (const void *addr) +{ +const struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +/* +* Read locklessly -- if there is a race with __kfence_alloc(), this +* most certainly is either a use-after-free, or invalid access. +*/ +return meta ?abs (meta->size) :0 ; +} + +void *kfence_object_start (const void *addr) +{ +const struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +/* +* Read locklessly -- if there is a race with __kfence_alloc(), this
mm/kfence/core.c line 653
Quoted 10 lines
mm/kfence/core.c new file +10
-0+* most certainly is either a use-after-free, or invalid access. +*/ +return meta ? (void *)meta->addr :NULL ; +} + +void __kfence_free (void *addr) +{ +struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +if (unlikely (meta->cache->flags &SLAB_TYPESAFE_BY_RCU ))
This may deserve a comment as to why we apply rcu on object level whereas SLAB_TYPESAFE_BY_RCU means slab level only.
Quoted 267 lines
mm/kfence/core.c new file +77
-0+call_rcu (&meta->rcu_head, rcu_guarded_free); + else +kfence_guarded_free (addr ,meta ); +} + +bool kfence_handle_page_fault (unsigned long addr) +{ +const int page_index = (addr - (unsigned long )__kfence_pool) /PAGE_SIZE ; +struct kfence_metadata *to_report =NULL ; +enum kfence_error_type error_type; +unsigned long flags; + +if (!is_kfence_address ((void *)addr)) +return false; + +if (!READ_ONCE (kfence_enabled))/* If disabled at runtime ... */ +return kfence_unprotect (addr);/* ... unprotect and proceed. */ + +atomic_long_inc (&counters[KFENCE_COUNTER_BUGS ]); + +if (page_index %2 ) { +/* This is a redzone, report a buffer overflow. */ +struct kfence_metadata *meta =NULL ; +int distance =0 ; + + meta =addr_to_metadata (addr -PAGE_SIZE ); +if (meta &&READ_ONCE (meta->state) ==KFENCE_OBJECT_ALLOCATED ) { + to_report = meta; +/* Data race ok; distance calculation approximate. */ + distance = addr -data_race (meta->addr +abs (meta->size)); + } + + meta =addr_to_metadata (addr +PAGE_SIZE ); +if (meta &&READ_ONCE (meta->state) ==KFENCE_OBJECT_ALLOCATED ) { +/* Data race ok; distance calculation approximate. */ +if (!to_report || distance >data_race (meta->addr) - addr) + to_report = meta; + } + +if (!to_report) + goto out; + +raw_spin_lock_irqsave (&to_report->lock, flags); + to_report->unprotected_page = addr; + error_type =KFENCE_ERROR_OOB ; + +/* +* If the object was freed before we took the look we can still +* report this as an OOB -- the report will simply show the +* stacktrace of the free as well. +*/ + }else { + to_report =addr_to_metadata (addr); +if (!to_report) + goto out; + +raw_spin_lock_irqsave (&to_report->lock, flags); + error_type =KFENCE_ERROR_UAF ; +/* +* We may race with __kfence_alloc(), and it is possible that a +* freed object may be reallocated. We simply report this as a +* use-after-free, with the stack trace showing the place where +* the object was re-allocated. +*/ + } + +out: +if (to_report) { +kfence_report_error (addr, to_report, error_type); +raw_spin_unlock_irqrestore (&to_report->lock, flags); + }else { +/* This may be a UAF or OOB access, but we can't be sure. */ +kfence_report_error (addr,NULL ,KFENCE_ERROR_INVALID ); + } + +return kfence_unprotect (addr);/* Unprotect and let access proceed. */ +}mm/kfence/kfence.h new file +104
-0@@ -0,0 +1,104 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef MM_KFENCE_KFENCE_H +#define MM_KFENCE_KFENCE_H + +#include <linux/mm.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/types.h> + +#include "../slab.h" /* for struct kmem_cache */ + +/* For non-debug builds, avoid leaking kernel pointers into dmesg. */ +#ifdef CONFIG_DEBUG_KERNEL +#define PTR_FMT "%px" +#else +#define PTR_FMT "%p" +#endif + +/* +* Get the canary byte pattern for @addr. Use a pattern that varies based on the +* lower 3 bits of the address, to detect memory corruptions with higher +* probability, where similar constants are used. +*/ +#define KFENCE_CANARY_PATTERN (addr) ((u8)0xaa ^ (u8)((unsigned long)addr & 0x7)) + +/* Maximum stack depth for reports. */ +#define KFENCE_STACK_DEPTH 64 + +/* KFENCE object states. */ +enum kfence_object_state { +KFENCE_OBJECT_UNUSED ,/* Object is unused. */ +KFENCE_OBJECT_ALLOCATED ,/* Object is currently allocated. */ +KFENCE_OBJECT_FREED ,/* Object was allocated, and then freed. */ +}; + +/* KFENCE metadata per guarded allocation. */ +struct kfence_metadata { +struct list_head list;/* Freelist node; access under kfence_freelist_lock. */ +struct rcu_head rcu_head;/* For delayed freeing. */ + +/* +* Lock protecting below data; to ensure consistency of the below data, +* since the following may execute concurrently: __kfence_alloc(), +* __kfence_free(), kfence_handle_page_fault(). However, note that we +* cannot grab the same metadata off the freelist twice, and multiple +* __kfence_alloc() cannot run concurrently on the same metadata. +*/ +raw_spinlock_t lock; + +/* The current state of the object; see above. */ +enum kfence_object_state state; + +/* +* Allocated object address; cannot be calculated from size, because of +* alignment requirements. +* +* Invariant: ALIGN_DOWN(addr, PAGE_SIZE) is constant. +*/ +unsigned long addr; + +/* +* The size of the original allocation: +* size > 0: left page alignment +* size < 0: right page alignment +*/ +int size; + +/* +* The kmem_cache cache of the last allocation; NULL if never allocated +* or the cache has already been destroyed. +*/ +struct kmem_cache *cache; + +/* +* In case of an invalid access, the page that was unprotected; we +* optimistically only store address. +*/ +unsigned long unprotected_page; + +/* Allocation and free stack information. */ +int num_alloc_stack; +int num_free_stack; +unsigned long alloc_stack[KFENCE_STACK_DEPTH ]; +unsigned long free_stack[KFENCE_STACK_DEPTH ]; +}; + +extern struct kfence_metadata kfence_metadata[CONFIG_KFENCE_NUM_OBJECTS ]; + +/* KFENCE error types for report generation. */ +enum kfence_error_type { +KFENCE_ERROR_OOB ,/* Detected a out-of-bounds access. */ +KFENCE_ERROR_UAF ,/* Detected a use-after-free access. */ +KFENCE_ERROR_CORRUPTION ,/* Detected a memory corruption on free. */ +KFENCE_ERROR_INVALID ,/* Invalid access of unknown type. */ +KFENCE_ERROR_INVALID_FREE ,/* Invalid free. */ +}; + +void kfence_report_error (unsigned long address,const struct kfence_metadata *meta, +enum kfence_error_type type); + +void kfence_print_object (struct seq_file *seq,const struct kfence_metadata *meta); + +#endif /* MM_KFENCE_KFENCE_H */ mm/kfence/report.c new file +74
-0@@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <stdarg.h> + +#include <linux/kernel.h> +#include <linux/lockdep.h> +#include <linux/printk.h> +#include <linux/seq_file.h> +#include <linux/stacktrace.h> +#include <linux/string.h> + +#include <asm/kfence.h> + +#include "kfence.h" + +/* Helper function to either print to a seq_file or to console. */ +static void seq_con_printf (struct seq_file *seq,const char *fmt, ...) +{ +va_list args; + +va_start (args, fmt); +if (seq) +seq_vprintf (seq, fmt, args); +else +vprintk (fmt, args); +va_end (args); +} + +/* Get the number of stack entries to skip get out of MM internals. */ +static int get_stack_skipnr (const unsigned long stack_entries[],int num_entries, +enum kfence_error_type type) +{ +char buf[64 ]; +int skipnr, fallback =0 ; + +for (skipnr =0 ; skipnr < num_entries; skipnr++) { +int len =scnprintf (buf,sizeof (buf),"%ps" , (void *)stack_entries[skipnr]); + +/* Depending on error type, find different stack entries. */ +switch (type) { +case KFENCE_ERROR_UAF : +case KFENCE_ERROR_OOB : +case KFENCE_ERROR_INVALID : +if (!strncmp (buf,KFENCE_SKIP_ARCH_FAULT_HANDLER , len)) + goto found; +break ; +case KFENCE_ERROR_CORRUPTION : +case KFENCE_ERROR_INVALID_FREE : +if (str_has_prefix (buf,"kfence_" ) ||str_has_prefix (buf,"__kfence_" )) + fallback = skipnr +1 ;/* In case kfree tail calls into kfence. */ + +/* Also the *_bulk() variants by only checking prefixes. */ +if (str_has_prefix (buf,"kfree" ) ||str_has_prefix (buf,"kmem_cache_free" )) + goto found; +break ; + } + } +if (fallback < num_entries) +return fallback; +found: + skipnr++; +return skipnr < num_entries ? skipnr :0 ; +} + +static void kfence_print_stack (struct seq_file *seq,const struct kfence_metadata *meta, +bool show_alloc) +{ +const unsigned long *entries = show_alloc ? meta->alloc_stack : meta->free_stack; +const int nentries = show_alloc ? meta->num_alloc_stack : meta->num_free_stack; + +if (nentries) { +int i; + +/* stack_trace_seq_print() does not exist; open code our own. */
mm/kfence/report.c line 84
Quoted 10 lines
mm/kfence/report.c new file +10
-0+for (i =0 ; i < nentries; i++) +seq_con_printf (seq," %pS\n" , entries[i]); + }else { +seq_con_printf (seq," no %s stack\n" , show_alloc ?"allocation" :"deallocation" ); + } +} + +void kfence_print_object (struct seq_file *seq,const struct kfence_metadata *meta) +{ +const int size =abs (meta->size);
This negative encoding is somewhat confusing. We do lots of abs, but do we even look at the sign anywhere? I can't find any use that is not abs.
Quoted 120 lines
mm/kfence/report.c new file +117
-1+const unsigned long start = meta->addr; +const struct kmem_cache *const cache = meta->cache; + +lockdep_assert_held (&meta->lock); + +if (meta->state ==KFENCE_OBJECT_UNUSED ) { +seq_con_printf (seq,"kfence-#%zd unused\n" , meta - kfence_metadata); +return ; + } + +seq_con_printf (seq, +"kfence-#%zd [0x" PTR_FMT "-0x" PTR_FMT +", size=%d, cache=%s] allocated in:\n" , + meta - kfence_metadata, (void *)start, (void *)(start + size -1 ), size, + (cache && cache->name) ? cache->name :"<destroyed>" ); +kfence_print_stack (seq, meta, true); + +if (meta->state ==KFENCE_OBJECT_FREED ) { +seq_con_printf (seq,"freed in:\n" ); +kfence_print_stack (seq, meta, false); + } +} + +/* +* Show bytes at @addr that are different from the expected canary values, up to +* @max_bytes. +*/ +static void print_diff_canary (const u8 *addr,size_t max_bytes) +{ +const u8 *max_addr =min ((const u8 *)PAGE_ALIGN ((unsigned long )addr), addr + max_bytes); + +pr_cont ("[" ); +for (; addr < max_addr; addr++) { +if (*addr ==KFENCE_CANARY_PATTERN (addr)) +pr_cont (" ." ); +else if (IS_ENABLED (CONFIG_DEBUG_KERNEL )) +pr_cont (" 0x%02x" , *addr); +else /* Do not leak kernel memory in non-debug builds. */ +pr_cont (" !" ); + } +pr_cont (" ]" ); +} + +void kfence_report_error (unsigned long address,const struct kfence_metadata *meta, +enum kfence_error_type type) +{ +unsigned long stack_entries[KFENCE_STACK_DEPTH ] = {0 }; +int num_stack_entries =stack_trace_save (stack_entries,KFENCE_STACK_DEPTH ,1 ); +int skipnr =get_stack_skipnr (stack_entries, num_stack_entries, type); + +/* KFENCE_ERROR_OOB requires non-NULL meta; for the rest it's optional. */ +if (WARN_ON (type ==KFENCE_ERROR_OOB && !meta)) +return ; + +if (meta) +lockdep_assert_held (&meta->lock); +/* +* Because we may generate reports in printk-unfriendly parts of the +* kernel, such as scheduler code, the use of printk() could deadlock. +* Until such time that all printing code here is safe in all parts of +* the kernel, accept the risk, and just get our message out (given the +* system might already behave unpredictably due to the memory error). +* As such, also disable lockdep to hide warnings, and avoid disabling +* lockdep for the rest of the kernel. +*/ +lockdep_off (); + +pr_err ("==================================================================\n" ); +/* Print report header. */ +switch (type) { +case KFENCE_ERROR_OOB : +pr_err ("BUG: KFENCE: out-of-bounds in %pS\n\n" , (void *)stack_entries[skipnr]); +pr_err ("Out-of-bounds access at 0x" PTR_FMT " (%s of kfence-#%zd):\n" , + (void *)address, address < meta->addr ?"left" :"right" , + meta - kfence_metadata); +break ; +case KFENCE_ERROR_UAF : +pr_err ("BUG: KFENCE: use-after-free in %pS\n\n" , (void *)stack_entries[skipnr]); +pr_err ("Use-after-free access at 0x" PTR_FMT ":\n" , (void *)address); +break ; +case KFENCE_ERROR_CORRUPTION : +pr_err ("BUG: KFENCE: memory corruption in %pS\n\n" , (void *)stack_entries[skipnr]); +pr_err ("Detected corrupted memory at 0x" PTR_FMT " " , (void *)address); +print_diff_canary ((u8 *)address,16 ); +pr_cont (":\n" ); +break ; +case KFENCE_ERROR_INVALID : +pr_err ("BUG: KFENCE: invalid access in %pS\n\n" , (void *)stack_entries[skipnr]); +pr_err ("Invalid access at 0x" PTR_FMT ":\n" , (void *)address); +break ; +case KFENCE_ERROR_INVALID_FREE : +pr_err ("BUG: KFENCE: invalid free in %pS\n\n" , (void *)stack_entries[skipnr]); +pr_err ("Invalid free of 0x" PTR_FMT ":\n" , (void *)address); +break ; + } + +/* Print stack trace and object info. */ +stack_trace_print (stack_entries + skipnr, num_stack_entries - skipnr,0 ); + +if (meta) { +pr_err ("\n" ); +kfence_print_object (NULL , meta); + } + +/* Print report footer. */ +pr_err ("\n" ); +dump_stack_print_info (KERN_DEFAULT ); +pr_err ("==================================================================\n" ); + +lockdep_on (); + +if (panic_on_warn) +panic ("panic_on_warn set ...\n" ); + +/* We encountered a memory unsafety error, taint the kernel! */ +add_taint (TAINT_WARN ,LOCKDEP_STILL_OK ); +} --2.28.0.526.ge36021eeef-goog
Alexander Potapenko
maintainerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
Quoted 11 lines · Dmitry Vyukov
mm/kfence/core.c line 273
Quoted 4 lines
mm/kfence/core.c new file +4
-0+/* Calculate address for this allocation. */ +if (right) + meta->addr +=PAGE_SIZE - size; + meta->addr =ALIGN_DOWN (meta->addr, cache->align);I would move this
ALIGN_DOWNunder the (right) if.
Do I understand it correctly that it will work, but we expect it to do nothing for !right? If cache align is >PAGE_SIZE, nothing good will happen anyway, right?
The previous 2 lines look like part of the same calculation -- "figure out the addr for the right case".
Yes, makes sense.
Quoted 6 lines
mm/kfence/core.c line 551
Quoted 3 lines
mm/kfence/core.c new file +3
-0+ +schedule_delayed_work (&kfence_timer,0 ); +WRITE_ONCE (kfence_enabled, true);Can
toggle_allocation_gaterun before we setkfence_enabled? If yes, it can break. If not, it's still somewhat confusing.
Correct, it should go after we enable KFENCE. We'll fix that in v2.
Quoted 8 lines
mm/kfence/core.c line 653
Quoted 5 lines
mm/kfence/core.c new file +5
-0+void __kfence_free (void *addr) +{ +struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +if (unlikely (meta->cache->flags &SLAB_TYPESAFE_BY_RCU ))This may deserve a comment as to why we apply rcu on object level whereas
SLAB_TYPESAFE_BY_RCUmeans slab level only.
Sorry, what do you mean by "slab level"? SLAB_TYPESAFE_BY_RCU means we have to wait for possible RCU accesses in flight before freeing objects from that slab - that's basically what we are doing here below:
Quoted 6 lines
Quoted 4 lines
mm/kfence/core.c new file +4
-0+call_rcu (&meta->rcu_head, rcu_guarded_free); + else +kfence_guarded_free (addr ,meta ); +}
Quoted 7 lines
mm/kfence/report.c line 84
Quoted 3 lines
mm/kfence/report.c new file +3
-0+void kfence_print_object (struct seq_file *seq,const struct kfence_metadata *meta) +{ +const int size =abs (meta->size);This negative encoding is somewhat confusing. We do lots of abs, but do we even look at the sign anywhere? I can't find any use that is not abs.
I think initially there was a reason for this, but now we don't seem to use it anywhere. Nice catch!
Alex
Dmitry Vyukov
reviewerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
Quoted 36 lines · Alexander Potapenko
Quoted 11 lines · Dmitry Vyukov
mm/kfence/core.c line 273
Quoted 4 lines
mm/kfence/core.c new file +4
-0+/* Calculate address for this allocation. */ +if (right) + meta->addr +=PAGE_SIZE - size; + meta->addr =ALIGN_DOWN (meta->addr, cache->align);I would move this
ALIGN_DOWNunder the (right) if.
Do I understand it correctly that it will work, but we expect it to do nothing for !right? If cache align is >PAGE_SIZE, nothing good will happen anyway, right?
The previous 2 lines look like part of the same calculation -- "figure out the addr for the right case".Yes, makes sense.
Quoted 6 lines
mm/kfence/core.c line 551
Quoted 3 lines
mm/kfence/core.c new file +3
-0+ +schedule_delayed_work (&kfence_timer,0 ); +WRITE_ONCE (kfence_enabled, true);Can
toggle_allocation_gaterun before we setkfence_enabled? If yes, it can break. If not, it's still somewhat confusing.Correct, it should go after we enable KFENCE. We'll fix that in v2.
Quoted 8 lines
mm/kfence/core.c line 653
Quoted 5 lines
mm/kfence/core.c new file +5
-0+void __kfence_free (void *addr) +{ +struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +if (unlikely (meta->cache->flags &SLAB_TYPESAFE_BY_RCU ))This may deserve a comment as to why we apply rcu on object level whereas
SLAB_TYPESAFE_BY_RCUmeans slab level only.
Quoted 4 lines
Sorry, what do you mean by "slab level"?
SLAB_TYPESAFE_BY_RCUmeans we have to wait for possible RCU accesses in flight before freeing objects from that slab - that's basically what we are doing here below:
Exactly! You see it is confusing :)SLAB_TYPESAFE_BY_RCU does not mean that. rcu-freeing only applies to whole pages, that's what I mean by "slab level" (whole slabs are freed by rcu).
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
mm/kfence/core.c new file +4
-0+call_rcu (&meta->rcu_head, rcu_guarded_free); + else +kfence_guarded_free (addr ,meta ); +}
Marco Elver
maintainerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
Quoted 43 lines · Dmitry Vyukov
Quoted 36 lines · Alexander Potapenko
Quoted 11 lines · Dmitry Vyukov
mm/kfence/core.c line 273
Quoted 4 lines
mm/kfence/core.c new file +4
-0+/* Calculate address for this allocation. */ +if (right) + meta->addr +=PAGE_SIZE - size; + meta->addr =ALIGN_DOWN (meta->addr, cache->align);I would move this
ALIGN_DOWNunder the (right) if.
Do I understand it correctly that it will work, but we expect it to do nothing for !right? If cache align is >PAGE_SIZE, nothing good will happen anyway, right?
The previous 2 lines look like part of the same calculation -- "figure out the addr for the right case".Yes, makes sense.
Quoted 6 lines
mm/kfence/core.c line 551
Quoted 3 lines
mm/kfence/core.c new file +3
-0+ +schedule_delayed_work (&kfence_timer,0 ); +WRITE_ONCE (kfence_enabled, true);Can
toggle_allocation_gaterun before we setkfence_enabled? If yes, it can break. If not, it's still somewhat confusing.Correct, it should go after we enable KFENCE. We'll fix that in v2.
Quoted 8 lines
mm/kfence/core.c line 653
Quoted 5 lines
mm/kfence/core.c new file +5
-0+void __kfence_free (void *addr) +{ +struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +if (unlikely (meta->cache->flags &SLAB_TYPESAFE_BY_RCU ))This may deserve a comment as to why we apply rcu on object level whereas
SLAB_TYPESAFE_BY_RCUmeans slab level only.Quoted 4 lines
Sorry, what do you mean by "slab level"?
SLAB_TYPESAFE_BY_RCUmeans we have to wait for possible RCU accesses in flight before freeing objects from that slab - that's basically what we are doing here below:
Quoted 4 lines
Exactly! You see it is confusing :)
SLAB_TYPESAFE_BY_RCUdoes not mean that. rcu-freeing only applies to whole pages, that's what I mean by "slab level" (whole slabs are freed by rcu).
In the case here, we have to defer freeing the object, because unlike real SLAB_TYPESAFE_BY_RCU slabs, our page here may get recycled for other-typed objects. We can update the comment to be clearer.
Re: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
In reply to Dmitry VyukovQuoted 42 lines · Dmitry Vyukov
Quoted 36 lines · Alexander Potapenko
Quoted 11 lines · Dmitry Vyukov
mm/kfence/core.c line 273
Quoted 4 lines
mm/kfence/core.c new file +4
-0+/* Calculate address for this allocation. */ +if (right) + meta->addr +=PAGE_SIZE - size; + meta->addr =ALIGN_DOWN (meta->addr, cache->align);I would move this
ALIGN_DOWNunder the (right) if.
Do I understand it correctly that it will work, but we expect it to do nothing for !right? If cache align is >PAGE_SIZE, nothing good will happen anyway, right?
The previous 2 lines look like part of the same calculation -- "figure out the addr for the right case".Yes, makes sense.
Quoted 6 lines
mm/kfence/core.c line 551
Quoted 3 lines
mm/kfence/core.c new file +3
-0+ +schedule_delayed_work (&kfence_timer,0 ); +WRITE_ONCE (kfence_enabled, true);Can
toggle_allocation_gaterun before we setkfence_enabled? If yes, it can break. If not, it's still somewhat confusing.Correct, it should go after we enable KFENCE. We'll fix that in v2.
Quoted 8 lines
mm/kfence/core.c line 653
Quoted 5 lines
mm/kfence/core.c new file +5
-0+void __kfence_free (void *addr) +{ +struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); + +if (unlikely (meta->cache->flags &SLAB_TYPESAFE_BY_RCU ))This may deserve a comment as to why we apply rcu on object level whereas
SLAB_TYPESAFE_BY_RCUmeans slab level only.Quoted 4 lines
Sorry, what do you mean by "slab level"?
SLAB_TYPESAFE_BY_RCUmeans we have to wait for possible RCU accesses in flight before freeing objects from that slab - that's basically what we are doing here below:
Quoted 4 lines
Exactly! You see it is confusing :)
SLAB_TYPESAFE_BY_RCUdoes not mean that. rcu-freeing only applies to whole pages, that's what I mean by "slab level" (whole slabs are freed by rcu).
Just confirming Dmitry's description of SLAB_TYPESAFE_BY_RCU semantics.
Thanx, Paul
Re: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
In reply to Marco ElverQuoted 34 lines · On Mon, 7 Sep 2020 15:40:46 +0200 Marco Elver <[email protected]> wrote:
From: Alexander Potapenko <[email protected]>
This adds the Kernel Electric-Fence (KFENCE) infrastructure. KFENCE is a low-overhead sampling-based memory safety error detector of heap use-after-free, invalid-free, and out-of-bounds access errors.
KFENCE is designed to be enabled in production kernels, and has near zero performance overhead. Compared to KASAN, KFENCE trades performance for precision. The main motivation behind KFENCE's design, is that with enough total uptime KFENCE will detect bugs in code paths not typically exercised by non-production test workloads. One way to quickly achieve a large enough total uptime is when the tool is deployed across a large fleet of machines.
KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error.
Guarded allocations are set up based on a sample interval (can be set via kfence.sample_interval). After expiration of the sample interval, a guarded allocation from the KFENCE object pool is returned to the main allocator (SLAB or SLUB). At this point, the timer is reset, and the next allocation is set up after the expiration of the interval.
To enable/disable a KFENCE allocation through the main allocator's fast-path without overhead, KFENCE relies on static branches via the static keys infrastructure. The static branch is toggled to redirect the allocation to KFENCE. To date, we have verified by running synthetic benchmarks (sysbench I/O workloads) that a kernel compiled with KFENCE is performance-neutral compared to the non-KFENCE baseline.
Quoted 2 lines
For more details, see
Documentation/dev-tools/kfence.rst(added later in the series).
So interesting feature! I left some tirvial comments below.
Quoted 22 lines
Co-developed-by Marco Elver <[email protected]>Signed-off-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>--- MAINTAINERS | 11 + include/linux/kfence.h | 174 ++++++++++ init/main.c | 2 + lib/Kconfig.debug | 1 + lib/Kconfig.kfence | 58 ++++ mm/Makefile | 1 + mm/kfence/Makefile | 3 + mm/kfence/core.c | 730 +++++++++++++++++++++++++++++++++++++++++ mm/kfence/kfence.h | 104 ++++++ mm/kfence/report.c | 201 ++++++++++++ 10 files changed, 1285 insertions(+) create mode 100644 include/linux/kfence.h create mode 100644 lib/Kconfig.kfence create mode 100644 mm/kfence/Makefile create mode 100644 mm/kfence/core.c create mode 100644 mm/kfence/kfence.h create mode 100644 mm/kfence/report.c
[...]
Quoted 18 lines
lib/Kconfig.kfence new file +12
-0@@ -0,0 +1,58 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config HAVE_ARCH_KFENCE + bool + +config HAVE_ARCH_KFENCE_STATIC_POOL + bool + help + If the architecture supports using the static pool. + +menuconfig KFENCE + bool "KFENCE: low-overhead sampling-based memory safety error detector"
lib/Kconfig.kfence line 22
Quoted 10 lines
lib/Kconfig.kfence new file +10
-0+ depends on HAVE_ARCH_KFENCE && !KASAN && (SLAB || SLUB) + depends on JUMP_LABEL # To ensure performance, require jump labels + select STACKTRACE + help + KFENCE is low-overhead sampling-based detector for heap out-of-bounds + access, use-after-free, and invalid-free errors. KFENCE is designed + to have negligible cost to permit enabling it in production + environments. + + See <file:Documentation/dev-tools/kfence.rst> for more details.
This patch doesn't provide the file yet. Why don't you add the reference with the patch introducing the file?
Quoted 5 lines
lib/Kconfig.kfence new file +5
-0+ + Note that, KFENCE is not a substitute for explicit testing with tools + such as KASAN. KFENCE can detect a subset of bugs that KASAN can + detect (therefore enabling KFENCE together with KASAN does not make + sense), albeit at very different performance profiles.
[...]
Quoted 9 lines
mm/kfence/core.c new file +3
-0@@ -0,0 +1,730 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define pr_fmt (fmt) "kfence: " fmt
[...]
Quoted 6 lines
mm/kfence/core.c new file +6
-0+ +static inline struct kfence_metadata *addr_to_metadata (unsigned long addr) +{ +long index; + +/* The checks do not affect performance; only called from slow-paths. */
mm/kfence/core.c line 127
Quoted 10 lines
mm/kfence/core.c new file +10
-0+ +if (!is_kfence_address ((void *)addr)) +return NULL ; + +/* +* May be an invalid index if called with an address at the edge of +* __kfence_pool, in which case we would report an "invalid access" +* error. +*/ + index = ((addr - (unsigned long )__kfence_pool) / (PAGE_SIZE *2 )) -1 ;
Seems the outermost parentheses unnecessary.
mm/kfence/core.c line 136
Quoted 9 lines
mm/kfence/core.c new file +9
-0+if (index <0 || index >=CONFIG_KFENCE_NUM_OBJECTS ) +return NULL ; + +return &kfence_metadata[index]; +} + +static inline unsigned long metadata_to_pageaddr (const struct kfence_metadata *meta) +{ +unsigned long offset = ((meta - kfence_metadata) +1 ) *PAGE_SIZE *2 ;
Seems the innermost parentheses unnecessary.
mm/kfence/core.c line 143
Quoted 7 lines
mm/kfence/core.c new file +7
-0+unsigned long pageaddr = (unsigned long )&__kfence_pool[offset]; + +/* The checks do not affect performance; only called from slow-paths. */ + +/* Only call with a pointer into kfence_metadata. */ +if (KFENCE_WARN_ON (meta < kfence_metadata || + meta >= kfence_metadata +ARRAY_SIZE (kfence_metadata)))
Is there a reason to use ARRAY_SIZE(kfence_metadata) instead of CONFIG_KFENCE_NUM_OBJECTS?
Quoted 11 lines
mm/kfence/core.c new file +11
-0+return 0 ; + +/* +* This metadata object only ever maps to 1 page; verify the calculation +* happens and that the stored address was not corrupted. +*/ +if (KFENCE_WARN_ON (ALIGN_DOWN (meta->addr,PAGE_SIZE ) != pageaddr)) +return 0 ; + +return pageaddr; +}
[...]
Quoted 8 lines
mm/kfence/core.c new file +8
-0+void __init kfence_init (void ) +{ +/* Setting kfence_sample_interval to 0 on boot disables KFENCE. */ +if (!kfence_sample_interval) +return ; + +if (!kfence_initialize_pool ()) { +pr_err ("%s failed\n" , __func__);
mm/kfence/core.c line 556
Quoted 10 lines
mm/kfence/core.c new file +10
-0+return ; + } + +schedule_delayed_work (&kfence_timer,0 ); +WRITE_ONCE (kfence_enabled, true); +pr_info ("initialized - using %zu bytes for %d objects" ,KFENCE_POOL_SIZE , +CONFIG_KFENCE_NUM_OBJECTS ); +if (IS_ENABLED (CONFIG_DEBUG_KERNEL )) +pr_cont (" at 0x%px-0x%px\n" , (void *)__kfence_pool, + (void *)(__kfence_pool +KFENCE_POOL_SIZE ));
Why don't you use PTR_FMT that defined in 'kfence.h'?
Quoted 3 lines
mm/kfence/core.c new file +3
-0+else +pr_cont ("\n" ); +}
[...]
Quoted 30 lines
mm/kfence/kfence.h new file +24
-0@@ -0,0 +1,104 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef MM_KFENCE_KFENCE_H +#define MM_KFENCE_KFENCE_H + +#include <linux/mm.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/types.h> + +#include "../slab.h" /* for struct kmem_cache */ + +/* For non-debug builds, avoid leaking kernel pointers into dmesg. */ +#ifdef CONFIG_DEBUG_KERNEL +#define PTR_FMT "%px" +#else +#define PTR_FMT "%p" +#endif + +/* +* Get the canary byte pattern for @addr. Use a pattern that varies based on the +* lower 3 bits of the address, to detect memory corruptions with higher +* probability, where similar constants are used. +*/
mm/kfence/kfence.h line 34
Quoted 10 lines
mm/kfence/kfence.h new file +10
-0+#define KFENCE_CANARY_PATTERN (addr) ((u8)0xaa ^ (u8)((unsigned long)addr & 0x7)) + +/* Maximum stack depth for reports. */ +#define KFENCE_STACK_DEPTH 64 + +/* KFENCE object states. */ +enum kfence_object_state { +KFENCE_OBJECT_UNUSED ,/* Object is unused. */ +KFENCE_OBJECT_ALLOCATED ,/* Object is currently allocated. */ +KFENCE_OBJECT_FREED ,/* Object was allocated, and then freed. */
Aligning the comments would look better (same to below comments).
Quoted 1 line
mm/kfence/kfence.h new file +1
-0+};
[...]
Quoted 7 lines
mm/kfence/report.c new file +1
-0@@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0
[...]
Quoted 6 lines
mm/kfence/report.c new file +6
-0+/* Get the number of stack entries to skip get out of MM internals. */ +static int get_stack_skipnr (const unsigned long stack_entries[],int num_entries, +enum kfence_error_type type) +{ +char buf[64 ]; +int skipnr, fallback =0 ;
mm/kfence/report.c line 44
Quoted 10 lines
mm/kfence/report.c new file +10
-0+ +for (skipnr =0 ; skipnr < num_entries; skipnr++) { +int len =scnprintf (buf,sizeof (buf),"%ps" , (void *)stack_entries[skipnr]); + +/* Depending on error type, find different stack entries. */ +switch (type) { +case KFENCE_ERROR_UAF : +case KFENCE_ERROR_OOB : +case KFENCE_ERROR_INVALID : +if (!strncmp (buf,KFENCE_SKIP_ARCH_FAULT_HANDLER , len))
Seems KFENCE_SKIP_ARCH_FAULT_HANDLER not defined yet?
mm/kfence/report.c
Quoted 2 lines
mm/kfence/report.c new file +2
-0+ goto found; +break ;
[...]
Thanks,
SeongJae Park
Marco Elver
maintainerRe: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
On Tue, Sep 15, 2020 at 03:57PM +0200, SeongJae Park wrote:
[...]
Quoted 2 lines
So interesting feature! I left some tirvial comments below.
Thank you!
Quoted 24 lines
[...]
Quoted 23 lines
lib/Kconfig.kfence new file +17
-0@@ -0,0 +1,58 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config HAVE_ARCH_KFENCE + bool + +config HAVE_ARCH_KFENCE_STATIC_POOL + bool + help + If the architecture supports using the static pool. + +menuconfig KFENCE + bool "KFENCE: low-overhead sampling-based memory safety error detector" + depends on HAVE_ARCH_KFENCE && !KASAN && (SLAB || SLUB) + depends on JUMP_LABEL # To ensure performance, require jump labels + select STACKTRACE + help + KFENCE is low-overhead sampling-based detector for heap out-of-bounds
Quoted 8 lines
lib/Kconfig.kfence line 22
Quoted 5 lines
lib/Kconfig.kfence new file +5
-0+ access, use-after-free, and invalid-free errors. KFENCE is designed + to have negligible cost to permit enabling it in production + environments. + + See <file:Documentation/dev-tools/kfence.rst> for more details.This patch doesn't provide the file yet. Why don't you add the reference with the patch introducing the file?
Sure, will fix for v3.
Quoted 26 lines
Quoted 5 lines
lib/Kconfig.kfence new file +5
-0+ + Note that, KFENCE is not a substitute for explicit testing with tools + such as KASAN. KFENCE can detect a subset of bugs that KASAN can + detect (therefore enabling KFENCE together with KASAN does not make + sense), albeit at very different performance profiles.[...]
Quoted 9 lines
mm/kfence/core.c new file +3
-0@@ -0,0 +1,730 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define pr_fmt (fmt) "kfence: " fmt[...]
Quoted 10 lines
mm/kfence/core.c new file +10
-0+ +static inline struct kfence_metadata *addr_to_metadata (unsigned long addr) +{ +long index; + +/* The checks do not affect performance; only called from slow-paths. */ + +if (!is_kfence_address ((void *)addr)) +return NULL ; +
Quoted 8 lines
mm/kfence/core.c line 127
Quoted 6 lines
mm/kfence/core.c new file +6
-0+/* +* May be an invalid index if called with an address at the edge of +* __kfence_pool, in which case we would report an "invalid access" +* error. +*/ + index = ((addr - (unsigned long )__kfence_pool) / (PAGE_SIZE *2 )) -1 ;Seems the outermost parentheses unnecessary.
Will fix.
Quoted 11 lines
mm/kfence/core.c line 136
Quoted 9 lines
mm/kfence/core.c new file +9
-0+if (index <0 || index >=CONFIG_KFENCE_NUM_OBJECTS ) +return NULL ; + +return &kfence_metadata[index]; +} + +static inline unsigned long metadata_to_pageaddr (const struct kfence_metadata *meta) +{ +unsigned long offset = ((meta - kfence_metadata) +1 ) *PAGE_SIZE *2 ;Seems the innermost parentheses unnecessary.
Will fix.
Quoted 10 lines
mm/kfence/core.c line 143
Quoted 7 lines
mm/kfence/core.c new file +7
-0+unsigned long pageaddr = (unsigned long )&__kfence_pool[offset]; + +/* The checks do not affect performance; only called from slow-paths. */ + +/* Only call with a pointer into kfence_metadata. */ +if (KFENCE_WARN_ON (meta < kfence_metadata || + meta >= kfence_metadata +ARRAY_SIZE (kfence_metadata)))Is there a reason to use
ARRAY_SIZE(kfence_metadata)instead ofCONFIG_KFENCE_NUM_OBJECTS?
They're equivalent. We can switch it. (Although I don't see one being superior to the other.. maybe we save on compile-time?)
Quoted 24 lines
Quoted 11 lines
mm/kfence/core.c new file +11
-0+return 0 ; + +/* +* This metadata object only ever maps to 1 page; verify the calculation +* happens and that the stored address was not corrupted. +*/ +if (KFENCE_WARN_ON (ALIGN_DOWN (meta->addr,PAGE_SIZE ) != pageaddr)) +return 0 ; + +return pageaddr; +}[...]
Quoted 12 lines
mm/kfence/core.c new file +12
-0+void __init kfence_init (void ) +{ +/* Setting kfence_sample_interval to 0 on boot disables KFENCE. */ +if (!kfence_sample_interval) +return ; + +if (!kfence_initialize_pool ()) { +pr_err ("%s failed\n" , __func__); +return ; + } + +schedule_delayed_work (&kfence_timer,0 );
Quoted 8 lines
mm/kfence/core.c line 556
Quoted 6 lines
mm/kfence/core.c new file +6
-0+WRITE_ONCE (kfence_enabled, true); +pr_info ("initialized - using %zu bytes for %d objects" ,KFENCE_POOL_SIZE , +CONFIG_KFENCE_NUM_OBJECTS ); +if (IS_ENABLED (CONFIG_DEBUG_KERNEL )) +pr_cont (" at 0x%px-0x%px\n" , (void *)__kfence_pool, + (void *)(__kfence_pool +KFENCE_POOL_SIZE ));Why don't you use
PTR_FMTthat defined in 'kfence.h'?
It's unnecessary, since all this is conditional on IS_ENABLED(CONFIG_DEBUG_KERNEL)) and we can just avoid the indirection through PTR_FMT.
Quoted 38 lines
Quoted 3 lines
mm/kfence/core.c new file +3
-0+else +pr_cont ("\n" ); +}[...]
Quoted 34 lines
mm/kfence/kfence.h new file +28
-0@@ -0,0 +1,104 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef MM_KFENCE_KFENCE_H +#define MM_KFENCE_KFENCE_H + +#include <linux/mm.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/types.h> + +#include "../slab.h" /* for struct kmem_cache */ + +/* For non-debug builds, avoid leaking kernel pointers into dmesg. */ +#ifdef CONFIG_DEBUG_KERNEL +#define PTR_FMT "%px" +#else +#define PTR_FMT "%p" +#endif + +/* +* Get the canary byte pattern for @addr. Use a pattern that varies based on the +* lower 3 bits of the address, to detect memory corruptions with higher +* probability, where similar constants are used. +*/ +#define KFENCE_CANARY_PATTERN (addr) ((u8)0xaa ^ (u8)((unsigned long)addr & 0x7)) + +/* Maximum stack depth for reports. */ +#define KFENCE_STACK_DEPTH 64
Quoted 8 lines
mm/kfence/kfence.h line 34
Quoted 6 lines
mm/kfence/kfence.h new file +6
-0+ +/* KFENCE object states. */ +enum kfence_object_state { +KFENCE_OBJECT_UNUSED ,/* Object is unused. */ +KFENCE_OBJECT_ALLOCATED ,/* Object is currently allocated. */ +KFENCE_OBJECT_FREED ,/* Object was allocated, and then freed. */ Aligning the comments would look better (same to below comments).
Will fix.
Quoted 20 lines
Quoted 1 line
mm/kfence/kfence.h new file +1
-0+};[...]
Quoted 7 lines
mm/kfence/report.c new file +1
-0@@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 [...]
Quoted 10 lines
mm/kfence/report.c new file +10
-0+/* Get the number of stack entries to skip get out of MM internals. */ +static int get_stack_skipnr (const unsigned long stack_entries[],int num_entries, +enum kfence_error_type type) +{ +char buf[64 ]; +int skipnr, fallback =0 ; + +for (skipnr =0 ; skipnr < num_entries; skipnr++) { +int len =scnprintf (buf,sizeof (buf),"%ps" , (void *)stack_entries[skipnr]); +
Quoted 8 lines
mm/kfence/report.c line 44
Quoted 6 lines
mm/kfence/report.c new file +6
-0+/* Depending on error type, find different stack entries. */ +switch (type) { +case KFENCE_ERROR_UAF : +case KFENCE_ERROR_OOB : +case KFENCE_ERROR_INVALID : +if (!strncmp (buf,KFENCE_SKIP_ARCH_FAULT_HANDLER , len))Seems
KFENCE_SKIP_ARCH_FAULT_HANDLERnot defined yet?
Correct, it'll be defined in <asm/kfence.h> in the x86 and arm64 patches. Leaving this is fine, since no architecture has selected HAVE_ARCH_KFENCE in this patch yet; as a result, we also can't break the build even if this is undefined.
Thanks,
-- Marco
Re: [PATCH RFC 01/10] mm: add Kernel Electric-Fence infrastructure
Quoted 6 lines · On Tue, 15 Sep 2020 16:14:49 +0200 Marco Elver <[email protected]> wrote:
On Tue, Sep 15, 2020 at 03:57PM +0200, SeongJae Park wrote:
[...]Quoted 2 lines
So interesting feature! I left some tirvial comments below.
Thank you!
[...]
Quoted 10 lines
Quoted 7 lines
mm/kfence/core.c line 143
Quoted 4 lines
mm/kfence/core.c new file +4
-0+ +/* Only call with a pointer into kfence_metadata. */ +if (KFENCE_WARN_ON (meta < kfence_metadata || + meta >= kfence_metadata +ARRAY_SIZE (kfence_metadata)))Is there a reason to use
ARRAY_SIZE(kfence_metadata)instead ofCONFIG_KFENCE_NUM_OBJECTS?They're equivalent. We can switch it. (Although I don't see one being superior to the other.. maybe we save on compile-time?)
I prefer CONFIG_KFENCE_NUM_OBJECTS here just because it's more widely used in the code. Also, I personally think it's more easy to read.
[...]
Quoted 11 lines
Quoted 7 lines
mm/kfence/core.c line 556
Quoted 5 lines
mm/kfence/core.c new file +5
-0+pr_info ("initialized - using %zu bytes for %d objects" ,KFENCE_POOL_SIZE , +CONFIG_KFENCE_NUM_OBJECTS ); +if (IS_ENABLED (CONFIG_DEBUG_KERNEL )) +pr_cont (" at 0x%px-0x%px\n" , (void *)__kfence_pool, + (void *)(__kfence_pool +KFENCE_POOL_SIZE ));Why don't you use
PTR_FMTthat defined in 'kfence.h'?It's unnecessary, since all this is conditional on
IS_ENABLED(CONFIG_DEBUG_KERNEL)) and we can just avoid the indirection throughPTR_FMT.
Ok, agreed.
[...]
Quoted 12 lines
Quoted 11 lines
mm/kfence/report.c line 44
Quoted 9 lines
mm/kfence/report.c new file +9
-0+for (skipnr =0 ; skipnr < num_entries; skipnr++) { +int len =scnprintf (buf,sizeof (buf),"%ps" , (void *)stack_entries[skipnr]); + +/* Depending on error type, find different stack entries. */ +switch (type) { +case KFENCE_ERROR_UAF : +case KFENCE_ERROR_OOB : +case KFENCE_ERROR_INVALID : +if (!strncmp (buf,KFENCE_SKIP_ARCH_FAULT_HANDLER , len))Seems
KFENCE_SKIP_ARCH_FAULT_HANDLERnot defined yet?
Quoted 4 lines
Correct, it'll be defined in <
asm/kfence.h> in the x86 and arm64 patches. Leaving this is fine, since no architecture has selectedHAVE_ARCH_KFENCEin this patch yet; as a result, we also can't break the build even if this is undefined.
Ah, got it. Thank you for the kind explanation.
Thanks,
SeongJae Park
Quoted 3 lines
Thanks,
-- Marco
Marco Elver
maintainerRFC PATCH 2/10 x86, kfence: enable KFENCE for x86
In reply to Marco ElverFrom: Alexander Potapenko <[email protected]>
Add architecture specific implementation details for KFENCE and enable KFENCE for the x86 architecture. In particular, this implements the required interface in <asm/kfence.h> for setting up the pool and providing helper functions for protecting and unprotecting pages.
For x86, we need to ensure that the pool uses 4K pages, which is done using the set_memory_4k() helper function.
--- arch/x86/Kconfig | 2 ++ arch/x86/include/asm/kfence.h | 60 +++++++++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 4 +++ 3 files changed, 66 insertions(+) create mode 100644 arch/x86/include/asm/kfence.h
arch/x86/Kconfig +2 -0
@@ -144,6 +144,8 @@ config X86 select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if X86_64 select HAVE_ARCH_KASAN_VMALLOC if X86_64 + select HAVE_ARCH_KFENCE + select HAVE_ARCH_KFENCE_STATIC_POOL select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT
arch/x86/include/asm/kfence.h new file +60 -0
60 lines not on this page · raw message
arch/x86/mm/fault.c +4 -0
@@ -9,6 +9,7 @@#include <linux/kdebug.h> /* oops_begin/end, ... */ #include <linux/extable.h> /* search_exception_tables */ #include <linux/memblock.h> /* max_low_pfn */ +#include <linux/kfence.h> /* kfence_handle_page_fault */ #include <linux/kprobes.h> /* NOKPROBE_SYMBOL, ... */ #include <linux/mmiotrace.h> /* kmmio_handler, ... */ #include <linux/perf_event.h> /* perf_sw_event */
@@ -701,6 +702,9 @@ no_context(struct pt_regs *regs, unsigned long error_code, }#endif +if (kfence_handle_page_fault (address)) +return ; +/* * 32-bit: *
Signature · 1 line
2.28.0.526.ge36021eeef-goog
kernel test robot
build botRe: [PATCH RFC 02/10] x86, kfence: enable KFENCE for x86
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Marco,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on hnaz-linux-mm/master]
[also build test WARNING on arm64/for-next/core tip/x86/core linus/master v5.9-rc4 next-20200903] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Marco-Elver/KFENCE-A-low-overhead-sampling-based-memory-safety-error-detector/20200907-214229 base: https://github.com/hnaz/linux-mm master config: i386-allyesconfig (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # save the attached .config to linux build tree make W=1 ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
In file included from include/linux/printk.h:7,
from include/linux/kernel.h:15,
from include/asm-generic/bug.h:20,
from arch/x86/include/asm/bug.h:93,
from include/linux/bug.h:5,
from mm/kfence/core.c:6:
mm/kfence/core.c: In function 'kfence_init':
>> include/linux/kern_levels.h:5:18: warning: format '%zu' expects argument of type 'size_t', but argument 2 has type 'long unsigned int' [-Wformat=]
5 | #define KERN_SOH "\001" /* ASCII Start Of Header */
| ^~~~~~
include/linux/kern_levels.h:14:19: note: in expansion of macro 'KERN_SOH'
14 | #define KERN_INFO KERN_SOH "6" /* informational */
| ^~~~~~~~
include/linux/printk.h:369:9: note: in expansion of macro 'KERN_INFO'
369 | printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
| ^~~~~~~~~
mm/kfence/core.c:552:2: note: in expansion of macro 'pr_info'
552 | pr_info("initialized - using %zu bytes for %d objects", KFENCE_POOL_SIZE,
| ^~~~~~~
mm/kfence/core.c:552:33: note: format string is defined here
552 | pr_info("initialized - using %zu bytes for %d objects", KFENCE_POOL_SIZE,
| ~~^
| |
| unsigned int
| %lu# https://github.com/0day-ci/linux/commit/347eefe4fa3022b7f8ddd093ce109529b511240b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Marco-Elver/KFENCE-A-low-overhead-sampling-based-memory-safety-error-detector/20200907-214229 git checkout 347eefe4fa3022b7f8ddd093ce109529b511240b
vim +5 include/linux/kern_levels.h
314ba3520e513a Joe Perches 2012-07-30 404d2c8c83d0e3a Joe Perches 2012-07-30 @5 #define KERN_SOH "\001" /* ASCII Start Of Header */04d2c8c83d0e3a Joe Perches 2012-07-30 6 #define KERN_SOH_ASCII '\001'04d2c8c83d0e3a Joe Perches 2012-07-30 7
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Marco Elver
maintainerRFC PATCH 3/10 arm64, kfence: enable KFENCE for ARM64
In reply to Marco ElverAdd architecture specific implementation details for KFENCE and enable KFENCE for the arm64 architecture. In particular, this implements the required interface in <asm/kfence.h>. Currently, the arm64 version does not yet use a statically allocated memory pool, at the cost of a pointer load for each is_kfence_address().
---
For ARM64, we would like to solicit feedback on what the best option is to obtain a constant address for __kfence_pool. One option is to declare a memory range in the memory layout to be dedicated to KFENCE (like is done for KASAN), however, it is unclear if this is the best available option. We would like to avoid touching the memory layout.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kfence.h | 39 +++++++++++++++++++++++++++++++++ arch/arm64/mm/fault.c | 4 ++++ 3 files changed, 44 insertions(+) create mode 100644 arch/arm64/include/asm/kfence.h
arch/arm64/Kconfig +1 -0
@@ -132,6 +132,7 @@ config ARM64 select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if !(ARM64_16K_PAGES && ARM64_VA_BITS_48) select HAVE_ARCH_KASAN_SW_TAGS if HAVE_ARCH_KASAN + select HAVE_ARCH_KFENCE if (!ARM64_16K_PAGES && !ARM64_64K_PAGES) select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT
arch/arm64/include/asm/kfence.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_KFENCE_H +#define __ASM_KFENCE_H + +#include <linux/kfence.h> +#include <linux/log2.h> +#include <linux/mm.h> + +#include <asm/cacheflush.h> + +#define KFENCE_SKIP_ARCH_FAULT_HANDLER "el1_sync" + +/* +* FIXME: Support HAVE_ARCH_KFENCE_STATIC_POOL: Use the statically allocated +* __kfence_pool, to avoid the extra pointer load for is_kfence_address(). By +* default, however, we do not have struct pages for static allocations. +*/ + +static inline bool arch_kfence_initialize_pool (void ) +{ +const unsigned int num_pages =ilog2 (roundup_pow_of_two (KFENCE_POOL_SIZE /PAGE_SIZE )); +struct page *pages =alloc_pages (GFP_KERNEL , num_pages); + +if (!pages) +return false; + + __kfence_pool =page_address (pages); +return true; +} + +static inline bool kfence_protect_page (unsigned long addr,bool protect) +{ +set_memory_valid (addr,1 , !protect); + +return true; +} + +#endif /* __ASM_KFENCE_H */
arch/arm64/mm/fault.c +4 -0
@@ -10,6 +10,7 @@#include <linux/acpi.h> #include <linux/bitfield.h> #include <linux/extable.h> +#include <linux/kfence.h> #include <linux/signal.h> #include <linux/mm.h> #include <linux/hardirq.h>
@@ -310,6 +311,9 @@ static void __do_kernel_fault(unsigned long addr, unsigned int esr, "Ignoring spurious kernel translation fault at virtual address %016lx\n", addr)) return; +if (kfence_handle_page_fault (addr)) +return ; +if (is_el1_permission_fault (addr, esr, regs)) {if (esr & ESR_ELx_WNR) msg ="write to read-only memory" ;
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Marco Elver
maintainerRe: [PATCH RFC 03/10] arm64, kfence: enable KFENCE for ARM64
Hello arm64 maintainers,
Quoted 11 lines · Marco Elver
Add architecture specific implementation details for KFENCE and enable KFENCE for the arm64 architecture. In particular, this implements the required interface in <
asm/kfence.h>. Currently, the arm64 version does not yet use a statically allocated memory pool, at the cost of a pointer load for eachis_kfence_address().For ARM64, we would like to solicit feedback on what the best option is to obtain a constant address for
__kfence_pool. One option is to declare a memory range in the memory layout to be dedicated to KFENCE (like is done for KASAN), however, it is unclear if this is the best available option. We would like to avoid touching the memory layout.
We can't yet tell what the best option for this might be. So, any suggestions on how to go about switching to a static pool would be much appreciated.
Many thanks,
-- Marco
Marco Elver
maintainerRFC PATCH 4/10 mm, kfence: insert KFENCE hooks for SLAB
In reply to Marco ElverFrom: Alexander Potapenko <[email protected]>
Inserts KFENCE hooks into the SLAB allocator.
We note the addition of the 'orig_size' argument to slab_alloc*() functions, to be able to pass the originally requested size to KFENCE. When KFENCE is disabled, there is no additional overhead, since these functions are __always_inline.
--- mm/slab.c | 46 ++++++++++++++++++++++++++++++++++------------ mm/slab_common.c | 6 +++++- 2 files changed, 39 insertions(+), 13 deletions(-)
mm/slab.c +34 -12
@@ -100,6 +100,7 @@#include <linux/seq_file.h> #include <linux/notifier.h> #include <linux/kallsyms.h> +#include <linux/kfence.h> #include <linux/cpu.h> #include <linux/sysctl.h> #include <linux/module.h>
@@ -3206,7 +3207,7 @@ static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, } static __always_inline void * -slab_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid, +slab_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid,size_t orig_size,unsigned long caller) {unsigned long save_flags;
@@ -3219,6 +3220,10 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (unlikely (!cachep))return NULL ; + ptr =kfence_alloc (cachep, orig_size, flags); +if (unlikely (ptr)) + goto out_hooks; +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags);
@@ -3251,6 +3256,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (unlikely (slab_want_init_on_alloc (flags, cachep)) && ptr)memset (ptr,0 , cachep->object_size); +out_hooks:slab_post_alloc_hook (cachep, objcg, flags,1 , &ptr);return ptr; }
@@ -3288,7 +3294,7 @@ __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)#endif /* CONFIG_NUMA */ static __always_inline void * -slab_alloc (struct kmem_cache *cachep,gfp_t flags,unsigned long caller) +slab_alloc (struct kmem_cache *cachep,gfp_t flags,size_t orig_size,unsigned long caller) {unsigned long save_flags;void *objp;
@@ -3299,6 +3305,10 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)if (unlikely (!cachep))return NULL ; + objp =kfence_alloc (cachep, orig_size, flags); +if (unlikely (objp)) + goto leave; +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags); objp =__do_cache_alloc (cachep, flags);
@@ -3309,6 +3319,7 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)if (unlikely (slab_want_init_on_alloc (flags, cachep)) && objp)memset (objp,0 , cachep->object_size); +leave:slab_post_alloc_hook (cachep, objcg, flags,1 , &objp);return objp; }
@@ -3414,6 +3425,11 @@ static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac) static __always_inline void __cache_free(struct kmem_cache *cachep, void *objp,unsigned long caller) { +if (kfence_free (objp)) { +kmemleak_free_recursive (objp, cachep->flags); +return ; + } +/* Put the object into the quarantine, don't touch it for now. */ if (kasan_slab_free (cachep, objp, _RET_IP_))return ;
@@ -3479,7 +3495,7 @@ void ___cache_free(struct kmem_cache *cachep, void *objp,*/ void *kmem_cache_alloc (struct kmem_cache *cachep,gfp_t flags) { -void *ret =slab_alloc (cachep, flags, _RET_IP_); +void *ret =slab_alloc (cachep, flags, cachep->object_size, _RET_IP_);trace_kmem_cache_alloc (_RET_IP_, ret, cachep->object_size, cachep->size, flags);
@@ -3512,7 +3528,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,local_irq_disable ();for (i =0 ; i < size; i++) { -void *objp =__do_cache_alloc (s, flags); +void *objp =kfence_alloc (s, s->object_size, flags) ?:__do_cache_alloc (s, flags);if (unlikely (!objp)) goto error;
@@ -3545,7 +3561,7 @@ kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size) {void *ret; - ret =slab_alloc (cachep, flags, _RET_IP_); + ret =slab_alloc (cachep, flags, size, _RET_IP_); ret =kasan_kmalloc (cachep, ret, size, flags); trace_kmalloc(_RET_IP_, ret,
@@ -3571,7 +3587,7 @@ EXPORT_SYMBOL(kmem_cache_alloc_trace);*/ void *kmem_cache_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid) { -void *ret =slab_alloc_node (cachep, flags, nodeid, _RET_IP_); +void *ret =slab_alloc_node (cachep, flags, nodeid, cachep->object_size, _RET_IP_); trace_kmem_cache_alloc_node(_RET_IP_, ret, cachep->object_size, cachep->size,
@@ -3589,7 +3605,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep, {void *ret; - ret =slab_alloc_node (cachep, flags, nodeid, _RET_IP_); + ret =slab_alloc_node (cachep, flags, nodeid, size, _RET_IP_); ret =kasan_kmalloc (cachep, ret, size, flags); trace_kmalloc_node(_RET_IP_, ret,
@@ -3650,7 +3666,7 @@ static __always_inline void *__do_kmalloc(size_t size, gfp_t flags, cachep =kmalloc_slab (size, flags);if (unlikely (ZERO_OR_NULL_PTR (cachep)))return cachep; - ret =slab_alloc (cachep, flags, caller); + ret =slab_alloc (cachep, flags, size, caller); ret =kasan_kmalloc (cachep, ret, size, flags); trace_kmalloc(caller, ret,
@@ -4138,18 +4154,24 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page, bool to_user) {struct kmem_cache *cachep; -unsigned int objnr; +unsigned int objnr =0 ;unsigned long offset; +bool is_kfence =is_kfence_address (ptr); ptr =kasan_reset_tag (ptr);/* Find and validate object. */ cachep = page->slab_cache; - objnr =obj_to_index (cachep, page, (void *)ptr); -BUG_ON (objnr >= cachep->num); +if (!is_kfence) { + objnr =obj_to_index (cachep, page, (void *)ptr); +BUG_ON (objnr >= cachep->num); + }/* Find offset within object. */ - offset = ptr -index_to_obj (cachep, page, objnr) -obj_offset (cachep); +if (is_kfence_address (ptr)) + offset = ptr -kfence_object_start (ptr); +else + offset = ptr -index_to_obj (cachep, page, objnr) -obj_offset (cachep);/* Allow address range falling entirely within usercopy region. */ if (offset >= cachep->useroffset &&
mm/slab_common.c +5 -1
@@ -12,6 +12,7 @@#include <linux/memory.h> #include <linux/cache.h> #include <linux/compiler.h> +#include <linux/kfence.h> #include <linux/module.h> #include <linux/cpu.h> #include <linux/uaccess.h>
@@ -448,6 +449,9 @@ static int shutdown_cache(struct kmem_cache *s)/* free asan quarantined objects */ kasan_cache_shutdown (s); +if (!kfence_shutdown_cache (s)) +return -EBUSY ; +if (__kmem_cache_shutdown (s) !=0 )return -EBUSY ;
@@ -1171,7 +1175,7 @@ size_t ksize(const void *objp)if (unlikely (ZERO_OR_NULL_PTR (objp)) || !__kasan_check_read (objp,1 ))return 0 ; - size =__ksize (objp); + size =kfence_ksize (objp) ?:__ksize (objp);/* * We assume that ksize callers could use whole allocated area, * so we need to unpoison this area.
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Dmitry Vyukov
reviewerRe: [PATCH RFC 04/10] mm, kfence: insert KFENCE hooks for SLAB
Quoted 99 lines · Marco Elver
From: Alexander Potapenko <[email protected]>
Inserts KFENCE hooks into the SLAB allocator.
We note the addition of the '
orig_size' argument to slab_alloc*() functions, to be able to pass the originally requested size to KFENCE. When KFENCE is disabled, there is no additional overhead, since these functions are__always_inline.Co-developed-by Marco Elver <[email protected]>Signed-off-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>--- mm/slab.c | 46 ++++++++++++++++++++++++++++++++++------------ mm/slab_common.c | 6 +++++- 2 files changed, 39 insertions(+), 13 deletions(-)mm/slab.c +18
-2@@ -100,6 +100,7 @@#include <linux/seq_file.h> #include <linux/notifier.h> #include <linux/kallsyms.h> +#include <linux/kfence.h> #include <linux/cpu.h> #include <linux/sysctl.h> #include <linux/module.h> @@ -3206,7 +3207,7 @@ static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, } static __always_inline void * -slab_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid, +slab_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid,size_t orig_size,unsigned long caller) {unsigned long save_flags;@@ -3219,6 +3220,10 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (unlikely (!cachep))return NULL ; + ptr =kfence_alloc (cachep, orig_size, flags); +if (unlikely (ptr)) + goto out_hooks; +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags);@@ -3251,6 +3256,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (unlikely (slab_want_init_on_alloc (flags, cachep)) && ptr)memset (ptr,0 , cachep->object_size); +out_hooks:slab_post_alloc_hook (cachep, objcg, flags,1 , &ptr);return ptr; }@@ -3288,7 +3294,7 @@ __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)#endif /* CONFIG_NUMA */ static __always_inline void * -slab_alloc (struct kmem_cache *cachep,gfp_t flags,unsigned long caller) +slab_alloc (struct kmem_cache *cachep,gfp_t flags,size_t orig_size,unsigned long caller) {unsigned long save_flags;void *objp;@@ -3299,6 +3305,10 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)if (unlikely (!cachep))return NULL ; + objp =kfence_alloc (cachep, orig_size, flags); +if (unlikely (objp)) + goto leave; +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags); objp =__do_cache_alloc (cachep, flags);@@ -3309,6 +3319,7 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)if (unlikely (slab_want_init_on_alloc (flags, cachep)) && objp)memset (objp,0 , cachep->object_size); +leave:slab_post_alloc_hook (cachep, objcg, flags,1 , &objp);return objp; }@@ -3414,6 +3425,11 @@ static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac) static __always_inline void __cache_free(struct kmem_cache *cachep, void *objp,unsigned long caller) { +if (kfence_free (objp)) { +kmemleak_free_recursive (objp, cachep->flags); +return ; + } +/* Put the object into the quarantine, don't touch it for now. */ if (kasan_slab_free (cachep, objp, _RET_IP_))return ;
mm/slab.c void ___cache_free(struct kmem_cache *cachep, void *objp, · line 3498
Quoted 5 lines
mm/slab.c +1
-1@@ -3479,7 +3495,7 @@ void ___cache_free(struct kmem_cache *cachep, void *objp,*/ void *kmem_cache_alloc (struct kmem_cache *cachep,gfp_t flags) { -void *ret =slab_alloc (cachep, flags, _RET_IP_); +void *ret =slab_alloc (cachep, flags, cachep->object_size, _RET_IP_);
It's kinda minor, but since we are talking about malloc fast path:
will passing 0 instead of cachep->object_size (here and everywhere else) and then using cachep->object_size on the slow path if 0 is passed as size improve codegen?
Quoted 110 lines
mm/slab.c +15
-9trace_kmem_cache_alloc (_RET_IP_, ret, cachep->object_size, cachep->size, flags);@@ -3512,7 +3528,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,local_irq_disable ();for (i =0 ; i < size; i++) { -void *objp =__do_cache_alloc (s, flags); +void *objp =kfence_alloc (s, s->object_size, flags) ?:__do_cache_alloc (s, flags);if (unlikely (!objp)) goto error;@@ -3545,7 +3561,7 @@ kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size) {void *ret; - ret =slab_alloc (cachep, flags, _RET_IP_); + ret =slab_alloc (cachep, flags, size, _RET_IP_); ret =kasan_kmalloc (cachep, ret, size, flags); trace_kmalloc(_RET_IP_, ret,@@ -3571,7 +3587,7 @@ EXPORT_SYMBOL(kmem_cache_alloc_trace);*/ void *kmem_cache_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid) { -void *ret =slab_alloc_node (cachep, flags, nodeid, _RET_IP_); +void *ret =slab_alloc_node (cachep, flags, nodeid, cachep->object_size, _RET_IP_); trace_kmem_cache_alloc_node(_RET_IP_, ret, cachep->object_size, cachep->size,@@ -3589,7 +3605,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep, {void *ret; - ret =slab_alloc_node (cachep, flags, nodeid, _RET_IP_); + ret =slab_alloc_node (cachep, flags, nodeid, size, _RET_IP_); ret =kasan_kmalloc (cachep, ret, size, flags); trace_kmalloc_node(_RET_IP_, ret,@@ -3650,7 +3666,7 @@ static __always_inline void *__do_kmalloc(size_t size, gfp_t flags, cachep =kmalloc_slab (size, flags);if (unlikely (ZERO_OR_NULL_PTR (cachep)))return cachep; - ret =slab_alloc (cachep, flags, caller); + ret =slab_alloc (cachep, flags, size, caller); ret =kasan_kmalloc (cachep, ret, size, flags); trace_kmalloc(caller, ret,@@ -4138,18 +4154,24 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page, bool to_user) {struct kmem_cache *cachep; -unsigned int objnr; +unsigned int objnr =0 ;unsigned long offset; +bool is_kfence =is_kfence_address (ptr); ptr =kasan_reset_tag (ptr);/* Find and validate object. */ cachep = page->slab_cache; - objnr =obj_to_index (cachep, page, (void *)ptr); -BUG_ON (objnr >= cachep->num); +if (!is_kfence) { + objnr =obj_to_index (cachep, page, (void *)ptr); +BUG_ON (objnr >= cachep->num); + }/* Find offset within object. */ - offset = ptr -index_to_obj (cachep, page, objnr) -obj_offset (cachep); +if (is_kfence_address (ptr)) + offset = ptr -kfence_object_start (ptr); +else + offset = ptr -index_to_obj (cachep, page, objnr) -obj_offset (cachep);/* Allow address range falling entirely within usercopy region. */ if (offset >= cachep->useroffset &&mm/slab_common.c +5
-2@@ -12,6 +12,7 @@#include <linux/memory.h> #include <linux/cache.h> #include <linux/compiler.h> +#include <linux/kfence.h> #include <linux/module.h> #include <linux/cpu.h> #include <linux/uaccess.h> @@ -448,6 +449,9 @@ static int shutdown_cache(struct kmem_cache *s)/* free asan quarantined objects */ kasan_cache_shutdown (s); +if (!kfence_shutdown_cache (s)) +return -EBUSY ; +if (__kmem_cache_shutdown (s) !=0 )return -EBUSY ;@@ -1171,7 +1175,7 @@ size_t ksize(const void *objp)if (unlikely (ZERO_OR_NULL_PTR (objp)) || !__kasan_check_read (objp,1 ))return 0 ; - size =__ksize (objp); + size =kfence_ksize (objp) ?:__ksize (objp);/* * We assume that ksize callers could use whole allocated area, * so we need to unpoison this area. -- 2.28.0.526.ge36021eeef-goog
Marco Elver
maintainerRe: [PATCH RFC 04/10] mm, kfence: insert KFENCE hooks for SLAB
Quoted 104 lines · Dmitry Vyukov
Quoted 102 lines · Marco Elver
From: Alexander Potapenko <[email protected]>
Inserts KFENCE hooks into the SLAB allocator.
We note the addition of the '
orig_size' argument to slab_alloc*() functions, to be able to pass the originally requested size to KFENCE. When KFENCE is disabled, there is no additional overhead, since these functions are__always_inline.Co-developed-by Marco Elver <[email protected]>Signed-off-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>--- mm/slab.c | 46 ++++++++++++++++++++++++++++++++++------------ mm/slab_common.c | 6 +++++- 2 files changed, 39 insertions(+), 13 deletions(-)mm/slab.c +18
-2@@ -100,6 +100,7 @@#include <linux/seq_file.h> #include <linux/notifier.h> #include <linux/kallsyms.h> +#include <linux/kfence.h> #include <linux/cpu.h> #include <linux/sysctl.h> #include <linux/module.h> @@ -3206,7 +3207,7 @@ static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, } static __always_inline void * -slab_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid, +slab_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid,size_t orig_size,unsigned long caller) {unsigned long save_flags;@@ -3219,6 +3220,10 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (unlikely (!cachep))return NULL ; + ptr =kfence_alloc (cachep, orig_size, flags); +if (unlikely (ptr)) + goto out_hooks; +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags);@@ -3251,6 +3256,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (unlikely (slab_want_init_on_alloc (flags, cachep)) && ptr)memset (ptr,0 , cachep->object_size); +out_hooks:slab_post_alloc_hook (cachep, objcg, flags,1 , &ptr);return ptr; }@@ -3288,7 +3294,7 @@ __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)#endif /* CONFIG_NUMA */ static __always_inline void * -slab_alloc (struct kmem_cache *cachep,gfp_t flags,unsigned long caller) +slab_alloc (struct kmem_cache *cachep,gfp_t flags,size_t orig_size,unsigned long caller) {unsigned long save_flags;void *objp;@@ -3299,6 +3305,10 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)if (unlikely (!cachep))return NULL ; + objp =kfence_alloc (cachep, orig_size, flags); +if (unlikely (objp)) + goto leave; +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags); objp =__do_cache_alloc (cachep, flags);@@ -3309,6 +3319,7 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)if (unlikely (slab_want_init_on_alloc (flags, cachep)) && objp)memset (objp,0 , cachep->object_size); +leave:slab_post_alloc_hook (cachep, objcg, flags,1 , &objp);return objp; }@@ -3414,6 +3425,11 @@ static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac) static __always_inline void __cache_free(struct kmem_cache *cachep, void *objp,unsigned long caller) { +if (kfence_free (objp)) { +kmemleak_free_recursive (objp, cachep->flags); +return ; + } +/* Put the object into the quarantine, don't touch it for now. */ if (kasan_slab_free (cachep, objp, _RET_IP_))return ;@@ -3479,7 +3495,7 @@ void ___cache_free(struct kmem_cache *cachep, void *objp,*/ void *kmem_cache_alloc (struct kmem_cache *cachep,gfp_t flags) {
Quoted 8 lines
mm/slab.c void ___cache_free(struct kmem_cache *cachep, void *objp, · line 3498
Quoted 2 lines
mm/slab.c +1
-1-void *ret =slab_alloc (cachep, flags, _RET_IP_); +void *ret =slab_alloc (cachep, flags, cachep->object_size, _RET_IP_);It's kinda minor, but since we are talking about malloc fast path:
will passing 0 instead ofcachep->object_size(here and everywhere else) and then usingcachep->object_sizeon the slow path if 0 is passed as size improve codegen?
It doesn't save us much, maybe 1 instruction based on what I'm looking at right now. The main worry I have is that the 'orig_size' argument is now part of slab_alloc, and changing its semantics may cause problems in future if it's no longer just passed to kfence_alloc(). Today, we can do the 'size = size ?: cache->object_size' trick inside kfence_alloc(), but at the cost breaking the intuitive semantics of slab_alloc's orig_size argument for future users. Is it worth it?
Thanks,
-- Marco
Dmitry Vyukov
reviewerRe: [PATCH RFC 04/10] mm, kfence: insert KFENCE hooks for SLAB
Quoted 110 lines · Marco Elver
Quoted 101 lines
Quoted 101 lines
From: Alexander Potapenko <[email protected]>
Inserts KFENCE hooks into the SLAB allocator.
We note the addition of the '
orig_size' argument to slab_alloc*() functions, to be able to pass the originally requested size to KFENCE. When KFENCE is disabled, there is no additional overhead, since these functions are__always_inline.Co-developed-by Marco Elver <[email protected]>Signed-off-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>--- mm/slab.c | 46 ++++++++++++++++++++++++++++++++++------------ mm/slab_common.c | 6 +++++- 2 files changed, 39 insertions(+), 13 deletions(-)mm/slab.c +18
-2@@ -100,6 +100,7 @@#include <linux/seq_file.h> #include <linux/notifier.h> #include <linux/kallsyms.h> +#include <linux/kfence.h> #include <linux/cpu.h> #include <linux/sysctl.h> #include <linux/module.h> @@ -3206,7 +3207,7 @@ static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, } static __always_inline void * -slab_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid, +slab_alloc_node (struct kmem_cache *cachep,gfp_t flags,int nodeid,size_t orig_size,unsigned long caller) {unsigned long save_flags;@@ -3219,6 +3220,10 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (unlikely (!cachep))return NULL ; + ptr =kfence_alloc (cachep, orig_size, flags); +if (unlikely (ptr)) + goto out_hooks; +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags);@@ -3251,6 +3256,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,if (unlikely (slab_want_init_on_alloc (flags, cachep)) && ptr)memset (ptr,0 , cachep->object_size); +out_hooks:slab_post_alloc_hook (cachep, objcg, flags,1 , &ptr);return ptr; }@@ -3288,7 +3294,7 @@ __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)#endif /* CONFIG_NUMA */ static __always_inline void * -slab_alloc (struct kmem_cache *cachep,gfp_t flags,unsigned long caller) +slab_alloc (struct kmem_cache *cachep,gfp_t flags,size_t orig_size,unsigned long caller) {unsigned long save_flags;void *objp;@@ -3299,6 +3305,10 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)if (unlikely (!cachep))return NULL ; + objp =kfence_alloc (cachep, orig_size, flags); +if (unlikely (objp)) + goto leave; +cache_alloc_debugcheck_before (cachep, flags);local_irq_save (save_flags); objp =__do_cache_alloc (cachep, flags);@@ -3309,6 +3319,7 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)if (unlikely (slab_want_init_on_alloc (flags, cachep)) && objp)memset (objp,0 , cachep->object_size); +leave:slab_post_alloc_hook (cachep, objcg, flags,1 , &objp);return objp; }@@ -3414,6 +3425,11 @@ static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac) static __always_inline void __cache_free(struct kmem_cache *cachep, void *objp,unsigned long caller) { +if (kfence_free (objp)) { +kmemleak_free_recursive (objp, cachep->flags); +return ; + } +/* Put the object into the quarantine, don't touch it for now. */ if (kasan_slab_free (cachep, objp, _RET_IP_))return ;@@ -3479,7 +3495,7 @@ void ___cache_free(struct kmem_cache *cachep, void *objp,*/ void *kmem_cache_alloc (struct kmem_cache *cachep,gfp_t flags) {Quoted 8 lines
mm/slab.c void ___cache_free(struct kmem_cache *cachep, void *objp, · line 3498
Quoted 2 lines
mm/slab.c +1
-1-void *ret =slab_alloc (cachep, flags, _RET_IP_); +void *ret =slab_alloc (cachep, flags, cachep->object_size, _RET_IP_);It's kinda minor, but since we are talking about malloc fast path:
will passing 0 instead ofcachep->object_size(here and everywhere else) and then usingcachep->object_sizeon the slow path if 0 is passed as size improve codegen?
Quoted 7 lines
It doesn't save us much, maybe 1 instruction based on what I'm looking at right now. The main worry I have is that the '
orig_size' argument is now part ofslab_alloc, and changing its semantics may cause problems in future if it's no longer just passed tokfence_alloc(). Today, we can do the 'size = size ?:cache->object_size' trick insidekfence_alloc(), but at the cost breaking the intuitive semantics of slab_alloc'sorig_sizeargument for future users. Is it worth it?
I don't have an answer to this question. I will leave this to others. If nobody has strong support for changing semantics, let's leave it as is. Maybe keep in mind as potential ballast.
FWIW most likely misuse of 0 size for other future purposes should manifest itself in a quite straightforward way.
Marco Elver
maintainerRFC PATCH 5/10 mm, kfence: insert KFENCE hooks for SLUB
In reply to Marco ElverFrom: Alexander Potapenko <[email protected]>
Inserts KFENCE hooks into the SLUB allocator.
We note the addition of the 'orig_size' argument to slab_alloc*() functions, to be able to pass the originally requested size to KFENCE. When KFENCE is disabled, there is no additional overhead, since these functions are __always_inline.
--- mm/slub.c | 72 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 19 deletions(-)
mm/slub.c +53 -19
@@ -27,6 +27,7 @@#include <linux/ctype.h> #include <linux/debugobjects.h> #include <linux/kallsyms.h> +#include <linux/kfence.h> #include <linux/memory.h> #include <linux/math64.h> #include <linux/fault-inject.h>
@@ -1557,6 +1558,11 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s,void *old_tail = *tail ? *tail : *head;int rsize; +if (is_kfence_address (next)) { +slab_free_hook (s, next); +return true; + } +/* Head and tail of the reconstructed freelist */ *head =NULL ; *tail =NULL ;
@@ -2660,7 +2666,8 @@ static inline void *get_freelist(struct kmem_cache *s, struct page *page)* already disabled (which is the case for bulk allocation). */ static void *___slab_alloc (struct kmem_cache *s,gfp_t gfpflags,int node, -unsigned long addr,struct kmem_cache_cpu *c) +unsigned long addr,struct kmem_cache_cpu *c, +size_t orig_size) {void *freelist;struct page *page;
@@ -2763,7 +2770,8 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,* cpu changes by refetching the per cpu area pointer. */ static void *__slab_alloc (struct kmem_cache *s,gfp_t gfpflags,int node, -unsigned long addr,struct kmem_cache_cpu *c) +unsigned long addr,struct kmem_cache_cpu *c, +size_t orig_size) {void *p;unsigned long flags;
@@ -2778,7 +2786,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, c =this_cpu_ptr (s->cpu_slab);#endif - p =___slab_alloc (s, gfpflags, node, addr, c); + p =___slab_alloc (s, gfpflags, node, addr, c, orig_size);local_irq_restore (flags);return p; }
@@ -2805,7 +2813,7 @@ static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s,* Otherwise we can simply pick the next object from the lockless free list. */ static __always_inline void *slab_alloc_node(struct kmem_cache *s, -gfp_t gfpflags,int node,unsigned long addr) +gfp_t gfpflags,int node,unsigned long addr,size_t orig_size) {void *object;struct kmem_cache_cpu *c;
@@ -2816,6 +2824,11 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, s =slab_pre_alloc_hook (s, &objcg,1 , gfpflags);if (!s)return NULL ; + + object =kfence_alloc (s, orig_size, gfpflags); +if (unlikely (object)) + goto out; + redo:/* * Must read kmem_cache cpu data via this cpu ptr. Preemption is
@@ -2853,7 +2866,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, object = c->freelist; page = c->page;if (unlikely (!object || !node_match (page, node))) { - object =__slab_alloc (s, gfpflags, node, addr, c); + object =__slab_alloc (s, gfpflags, node, addr, c, orig_size);stat (s,ALLOC_SLOWPATH ); }else {void *next_object =get_freepointer_safe (s, object);
@@ -2889,20 +2902,21 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,if (unlikely (slab_want_init_on_alloc (gfpflags, s)) && object)memset (object,0 , s->object_size); +out:slab_post_alloc_hook (s, objcg, gfpflags,1 , &object);return object; } static __always_inline void *slab_alloc(struct kmem_cache *s, -gfp_t gfpflags,unsigned long addr) +gfp_t gfpflags,unsigned long addr,size_t orig_size) { -return slab_alloc_node (s, gfpflags,NUMA_NO_NODE , addr); +return slab_alloc_node (s, gfpflags,NUMA_NO_NODE , addr, orig_size); }void *kmem_cache_alloc (struct kmem_cache *s,gfp_t gfpflags) { -void *ret =slab_alloc (s, gfpflags, _RET_IP_); +void *ret =slab_alloc (s, gfpflags, _RET_IP_, s->object_size);trace_kmem_cache_alloc (_RET_IP_, ret, s->object_size, s->size, gfpflags);
@@ -2914,7 +2928,7 @@ EXPORT_SYMBOL(kmem_cache_alloc);#ifdef CONFIG_TRACING void *kmem_cache_alloc_trace (struct kmem_cache *s,gfp_t gfpflags,size_t size) { -void *ret =slab_alloc (s, gfpflags, _RET_IP_); +void *ret =slab_alloc (s, gfpflags, _RET_IP_, size);trace_kmalloc (_RET_IP_, ret, size, s->size, gfpflags); ret =kasan_kmalloc (s, ret, size, gfpflags);return ret;
@@ -2925,7 +2939,7 @@ EXPORT_SYMBOL(kmem_cache_alloc_trace);#ifdef CONFIG_NUMA void *kmem_cache_alloc_node (struct kmem_cache *s,gfp_t gfpflags,int node) { -void *ret =slab_alloc_node (s, gfpflags, node, _RET_IP_); +void *ret =slab_alloc_node (s, gfpflags, node, _RET_IP_, s->object_size);trace_kmem_cache_alloc_node (_RET_IP_, ret, s->object_size, s->size, gfpflags, node);
@@ -2939,7 +2953,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *s,gfp_t gfpflags, int node, size_t size) { - void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_); + void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_, size);trace_kmalloc_node (_RET_IP_, ret, size, s->size, gfpflags, node);
@@ -2973,6 +2987,9 @@ static void __slab_free(struct kmem_cache *s, struct page *page,stat (s,FREE_SLOWPATH ); +if (kfence_free (head)) +return ; +if (kmem_cache_debug (s) && !free_debug_processing (s, page, head, tail, cnt, addr))return ;
@@ -3216,6 +3233,13 @@ int build_detached_freelist(struct kmem_cache *s, size_t size, df->s =cache_from_obj (s, object);/* Support for memcg */ } +if (is_kfence_address (object)) { +slab_free_hook (df->s, object); +WARN_ON (!kfence_free (object)); + p[size] =NULL ;/* mark object processed */ +return size; + } +/* Start new detached freelist */ df->page = page;set_freepointer (df->s, object,NULL );
@@ -3290,8 +3314,14 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, c =this_cpu_ptr (s->cpu_slab);for (i =0 ; i < size; i++) { -void *object = c->freelist; +void *object =kfence_alloc (s, s->object_size, flags); +if (unlikely (object)) { + p[i] = object; +continue ; + } + + object = c->freelist;if (unlikely (!object)) {/* * We may have removed an object from c->freelist using
@@ -3307,7 +3337,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,* of re-populating per CPU c->freelist */ p[i] =___slab_alloc (s, flags,NUMA_NO_NODE , - _RET_IP_, c); + _RET_IP_, c, size);if (unlikely (!p[i])) goto error;
@@ -3962,7 +3992,7 @@ void *__kmalloc(size_t size, gfp_t flags)if (unlikely (ZERO_OR_NULL_PTR (s)))return s; - ret =slab_alloc (s, flags, _RET_IP_); + ret =slab_alloc (s, flags, _RET_IP_, size);trace_kmalloc (_RET_IP_, ret, size, s->size, flags);
@@ -4010,7 +4040,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)if (unlikely (ZERO_OR_NULL_PTR (s)))return s; - ret =slab_alloc_node (s, flags, node, _RET_IP_); + ret =slab_alloc_node (s, flags, node, _RET_IP_, size);trace_kmalloc_node (_RET_IP_, ret, size, s->size, flags, node);
@@ -4036,6 +4066,7 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page,struct kmem_cache *s;unsigned int offset;size_t object_size; +bool is_kfence =is_kfence_address (ptr); ptr =kasan_reset_tag (ptr);
@@ -4048,10 +4079,13 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page, to_user, 0, n);/* Find offset within object. */ - offset = (ptr -page_address (page)) % s->size; +if (is_kfence) + offset = ptr -kfence_object_start (ptr); +else + offset = (ptr -page_address (page)) % s->size;/* Adjust for redzone and reject if within the redzone. */ -if (kmem_cache_debug_flags (s,SLAB_RED_ZONE )) { +if (!is_kfence &&kmem_cache_debug_flags (s,SLAB_RED_ZONE )) {if (offset < s->red_left_pad)usercopy_abort ("SLUB object in left red zone" , s->name, to_user, offset, n);
@@ -4460,7 +4494,7 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)if (unlikely (ZERO_OR_NULL_PTR (s)))return s; - ret =slab_alloc (s, gfpflags, caller); + ret =slab_alloc (s, gfpflags, caller, size);/* Honor the call site pointer we received. */ trace_kmalloc (caller, ret, size, s->size, gfpflags);
@@ -4491,7 +4525,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,if (unlikely (ZERO_OR_NULL_PTR (s)))return s; - ret =slab_alloc_node (s, gfpflags, node, caller); + ret =slab_alloc_node (s, gfpflags, node, caller, size);/* Honor the call site pointer we received. */ trace_kmalloc_node (caller, ret, size, s->size, gfpflags, node);
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Marco Elver
maintainerRFC PATCH 6/10 kfence, kasan: make KFENCE compatible with KASAN
In reply to Marco ElverFrom: Alexander Potapenko <[email protected]>
We make KFENCE compatible with KASAN for testing KFENCE itself. In particular, KASAN helps to catch any potential corruptions to KFENCE state, or other corruptions that may be a result of freepointer corruptions in the main allocators.
To indicate that the combination of the two is generally discouraged, CONFIG_EXPERT=y should be set. It also gives us the nice property that KFENCE will be build-tested by allyesconfig builds.
--- lib/Kconfig.kfence | 2 +- mm/kasan/common.c | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-)
lib/Kconfig.kfence +1 -1
@@ -10,7 +10,7 @@ config HAVE_ARCH_KFENCE_STATIC_POOL menuconfig KFENCE bool "KFENCE: low-overhead sampling-based memory safety error detector" - depends on HAVE_ARCH_KFENCE && !KASAN && (SLAB || SLUB) + depends on HAVE_ARCH_KFENCE && (!KASAN || EXPERT) && (SLAB || SLUB) depends on JUMP_LABEL # To ensure performance, require jump labels select STACKTRACE help
mm/kasan/common.c +7 -0
@@ -18,6 +18,7 @@#include <linux/init.h> #include <linux/kasan.h> #include <linux/kernel.h> +#include <linux/kfence.h> #include <linux/kmemleak.h> #include <linux/linkage.h> #include <linux/memblock.h>
@@ -396,6 +397,9 @@ static bool __kasan_slab_free(struct kmem_cache *cache, void *object, tagged_object = object; object =reset_tag (object); +if (is_kfence_address (object)) +return false; +if (unlikely (nearest_obj (cache,virt_to_head_page (object), object) != object)) {kasan_report_invalid_free (tagged_object, ip);
@@ -444,6 +448,9 @@ static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object,if (unlikely (object ==NULL ))return NULL ; +if (is_kfence_address (object)) +return (void *)object; + redzone_start =round_up ((unsigned long )(object + size),KASAN_SHADOW_SCALE_SIZE ); redzone_end = round_up((unsigned long)object + cache->object_size,
Signature · 1 line
2.28.0.526.ge36021eeef-goog
kernel test robot
build botRe: [PATCH RFC 06/10] kfence, kasan: make KFENCE compatible with KASAN
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Marco,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on hnaz-linux-mm/master]
[also build test WARNING on arm64/for-next/core linus/master v5.9-rc4] [cannot apply to tip/x86/core next-20200903]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Marco-Elver/KFENCE-A-low-overhead-sampling-based-memory-safety-error-detector/20200907-214229 base: https://github.com/hnaz/linux-mm master config: x86_64-allyesconfig (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # save the attached .config to linux build tree make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
mm/kfence/report.c: In function 'seq_con_printf':
>> mm/kfence/report.c:23:3: warning: function 'seq_con_printf' might be a candidate for 'gnu_printf' format attribute [-Wsuggest-attribute=format]
23 | seq_vprintf(seq, fmt, args);
| ^~~~~~~~~~~
mm/kfence/report.c:25:3: warning: function 'seq_con_printf' might be a candidate for 'gnu_printf' format attribute [-Wsuggest-attribute=format]
25 | vprintk(fmt, args);
| ^~~~~~~# https://github.com/0day-ci/linux/commit/0137895ca2628347cf17bc232a4613e6454fdece
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Marco-Elver/KFENCE-A-low-overhead-sampling-based-memory-safety-error-detector/20200907-214229 git checkout 0137895ca2628347cf17bc232a4613e6454fdece
vim +23 mm/kfence/report.c
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 15
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 16 /* Helper function to either print to a seq_file or to console. */
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 17 static void seq_con_printf(struct seq_file *seq, const char *fmt, ...)
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 18 {
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 19 va_list args;
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 20
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 21 va_start(args, fmt);
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 22 if (seq)
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 @23 seq_vprintf(seq, fmt, args);
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 24 else
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 25 vprintk(fmt, args);
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 26 va_end(args);
3ac0cf48be1cfa Alexander Potapenko 2020-09-07 27 }3ac0cf48be1cfa Alexander Potapenko 2020-09-07 28
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Dmitry Vyukov
reviewerRe: [PATCH RFC 06/10] kfence, kasan: make KFENCE compatible with KASAN
In reply to Marco ElverQuoted 46 lines · Marco Elver
From: Alexander Potapenko <[email protected]>
We make KFENCE compatible with KASAN for testing KFENCE itself. In particular, KASAN helps to catch any potential corruptions to KFENCE state, or other corruptions that may be a result of freepointer corruptions in the main allocators.
To indicate that the combination of the two is generally discouraged,
CONFIG_EXPERT=yshould be set. It also gives us the nice property that KFENCE will be build-tested by allyesconfig builds.Co-developed-by Marco Elver <[email protected]>Signed-off-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>--- lib/Kconfig.kfence | 2 +- mm/kasan/common.c | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-)lib/Kconfig.kfence +1
-1@@ -10,7 +10,7 @@ config HAVE_ARCH_KFENCE_STATIC_POOL menuconfig KFENCE bool "KFENCE: low-overhead sampling-based memory safety error detector" - depends on HAVE_ARCH_KFENCE && !KASAN && (SLAB || SLUB) + depends on HAVE_ARCH_KFENCE && (!KASAN || EXPERT) && (SLAB || SLUB) depends on JUMP_LABEL # To ensure performance, require jump labels select STACKTRACE helpmm/kasan/common.c +1
-0@@ -18,6 +18,7 @@#include <linux/init.h> #include <linux/kasan.h> #include <linux/kernel.h> +#include <linux/kfence.h> #include <linux/kmemleak.h> #include <linux/linkage.h> #include <linux/memblock.h>
mm/kasan/common.c static bool __kasan_slab_free(struct kmem_cache *cache, void *object, · line 401
Quoted 5 lines
mm/kasan/common.c +2
-0@@ -396,6 +397,9 @@ static bool __kasan_slab_free(struct kmem_cache *cache, void *object, tagged_object = object; object =reset_tag (object); +if (is_kfence_address (object)) +return false;
Is this needed?
At least in the slab patch I see that we do :
if (kfence_free(objp)) {
kmemleak_free_recursive(objp, cachep->flags);
return;
}
before:
/* Put the object into the quarantine, don't touch it for now. */ /* Put the object into the quarantine, don't touch it for now. */ if (kasan_slab_free(cachep, objp, _RET_IP_)) if (kasan_slab_free(cachep, objp, _RET_IP_))
return; return;
If it's not supposed to be triggered, it can make sense to replace with BUG/WARN.
Quoted 16 lines
mm/kasan/common.c +3
-1if (unlikely (nearest_obj (cache,virt_to_head_page (object), object) != object)) {kasan_report_invalid_free (tagged_object, ip);@@ -444,6 +448,9 @@ static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object,if (unlikely (object ==NULL ))return NULL ; +if (is_kfence_address (object)) +return (void *)object; + redzone_start =round_up ((unsigned long )(object + size),KASAN_SHADOW_SCALE_SIZE ); redzone_end = round_up((unsigned long)object + cache->object_size, --2.28.0.526.ge36021eeef-goog
Marco Elver
maintainerRe: [PATCH RFC 06/10] kfence, kasan: make KFENCE compatible with KASAN
Quoted 71 lines · Dmitry Vyukov
Quoted 46 lines · Marco Elver
From: Alexander Potapenko <[email protected]>
We make KFENCE compatible with KASAN for testing KFENCE itself. In particular, KASAN helps to catch any potential corruptions to KFENCE state, or other corruptions that may be a result of freepointer corruptions in the main allocators.
To indicate that the combination of the two is generally discouraged,
CONFIG_EXPERT=yshould be set. It also gives us the nice property that KFENCE will be build-tested by allyesconfig builds.Co-developed-by Marco Elver <[email protected]>Signed-off-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>--- lib/Kconfig.kfence | 2 +- mm/kasan/common.c | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-)lib/Kconfig.kfence +1
-1@@ -10,7 +10,7 @@ config HAVE_ARCH_KFENCE_STATIC_POOL menuconfig KFENCE bool "KFENCE: low-overhead sampling-based memory safety error detector" - depends on HAVE_ARCH_KFENCE && !KASAN && (SLAB || SLUB) + depends on HAVE_ARCH_KFENCE && (!KASAN || EXPERT) && (SLAB || SLUB) depends on JUMP_LABEL # To ensure performance, require jump labels select STACKTRACE helpmm/kasan/common.c +1
-0@@ -18,6 +18,7 @@#include <linux/init.h> #include <linux/kasan.h> #include <linux/kernel.h> +#include <linux/kfence.h> #include <linux/kmemleak.h> #include <linux/linkage.h> #include <linux/memblock.h> mm/kasan/common.c static bool __kasan_slab_free(struct kmem_cache *cache, void *object, · line 401
Quoted 5 lines
mm/kasan/common.c +2
-0@@ -396,6 +397,9 @@ static bool __kasan_slab_free(struct kmem_cache *cache, void *object, tagged_object = object; object =reset_tag (object); +if (is_kfence_address (object)) +return false;Is this needed?
At least in the slab patch I see that we do :if (
kfence_free(objp)) {
kmemleak_free_recursive(objp, cachep->flags);
return;
}before:
/* Put the object into the quarantine, don't touch it for now. */ /* Put the object into the quarantine, don't touch it for now. */ if (
kasan_slab_free(cachep, objp, _RET_IP_)) if (kasan_slab_free(cachep, objp, _RET_IP_))
return; return;
Quoted 2 lines
If it's not supposed to be triggered, it can make sense to replace with BUG/WARN.
It is required for SLUB. For SLAB, it seems it might not be necessary. Making the check in kasan/common.c conditional on the allocator seems ugly, so I propose we keep it there.
Thanks,
-- Marco
Marco Elver
maintainerRFC PATCH 7/10 kfence, kmemleak: make KFENCE compatible with KMEMLEAK
In reply to Marco ElverFrom: Alexander Potapenko <[email protected]>
Add compatibility with KMEMLEAK, by making KMEMLEAK aware of the KFENCE memory pool. This allows building debug kernels with both enabled, which also helped in debugging KFENCE.
--- mm/kmemleak.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
mm/kmemleak.c +11 -0
@@ -97,6 +97,7 @@#include <linux/atomic.h> #include <linux/kasan.h> +#include <linux/kfence.h> #include <linux/kmemleak.h> #include <linux/memory_hotplug.h>
@@ -1946,8 +1947,18 @@ void __init kmemleak_init(void)/* register the data/bss sections */ create_object ((unsigned long )_sdata, _edata - _sdata,KMEMLEAK_GREY ,GFP_ATOMIC ); +#if defined(CONFIG_KFENCE ) && defined(CONFIG_HAVE_ARCH_KFENCE_STATIC_POOL ) +/* KFENCE objects are located in .bss, which may confuse kmemleak. Skip them. */ +create_object ((unsigned long )__bss_start, __kfence_pool - __bss_start, +KMEMLEAK_GREY ,GFP_ATOMIC ); +create_object ((unsigned long )__kfence_pool +KFENCE_POOL_SIZE , + __bss_stop - (__kfence_pool +KFENCE_POOL_SIZE ), +KMEMLEAK_GREY ,GFP_ATOMIC ); +#else create_object ((unsigned long )__bss_start, __bss_stop - __bss_start,KMEMLEAK_GREY ,GFP_ATOMIC ); +#endif +/* only register .data..ro_after_init if not within .data */ if (&__start_ro_after_init < &_sdata || &__end_ro_after_init > &_edata) create_object((unsigned long)__start_ro_after_init,
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Catalin Marinas
maintainerRe: [PATCH RFC 07/10] kfence, kmemleak: make KFENCE compatible with KMEMLEAK
Quoted 30 lines · Marco Elver
From: Alexander Potapenko <[email protected]>
Add compatibility with KMEMLEAK, by making KMEMLEAK aware of the KFENCE memory pool. This allows building debug kernels with both enabled, which also helped in debugging KFENCE.
Co-developed-by Marco Elver <[email protected]>Signed-off-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>--- mm/kmemleak.c | 11 +++++++++++ 1 file changed, 11 insertions(+)mm/kmemleak.c +2
-0@@ -97,6 +97,7 @@#include <linux/atomic.h> #include <linux/kasan.h> +#include <linux/kfence.h> #include <linux/kmemleak.h> #include <linux/memory_hotplug.h> @@ -1946,8 +1947,18 @@ void __init kmemleak_init(void)/* register the data/bss sections */ create_object ((unsigned long )_sdata, _edata - _sdata,KMEMLEAK_GREY ,GFP_ATOMIC ); +#if defined(CONFIG_KFENCE ) && defined(CONFIG_HAVE_ARCH_KFENCE_STATIC_POOL )
mm/kmemleak.c void __init kmemleak_init(void) · line 1960
Quoted 10 lines
mm/kmemleak.c +8
-0+/* KFENCE objects are located in .bss, which may confuse kmemleak. Skip them. */ +create_object ((unsigned long )__bss_start, __kfence_pool - __bss_start, +KMEMLEAK_GREY ,GFP_ATOMIC ); +create_object ((unsigned long )__kfence_pool +KFENCE_POOL_SIZE , + __bss_stop - (__kfence_pool +KFENCE_POOL_SIZE ), +KMEMLEAK_GREY ,GFP_ATOMIC ); +#else create_object ((unsigned long )__bss_start, __bss_stop - __bss_start,KMEMLEAK_GREY ,GFP_ATOMIC ); +#endif
Could you instead do:
#if defined(CONFIG_KFENCE) && defined(CONFIG_HAVE_ARCH_KFENCE_STATIC_POOL) delete_object_part((unsigned long)__kfence_pool, KFENCE_POOL_SIZE);
#endif
Signature · 1 line
Catalin
Alexander Potapenko
maintainerRe: [PATCH RFC 07/10] kfence, kmemleak: make KFENCE compatible with KMEMLEAK
Quoted 5 lines
Could you instead do:
#if defined(CONFIG_KFENCE) && defined(CONFIG_HAVE_ARCH_KFENCE_STATIC_POOL) delete_object_part((unsigned long)__kfence_pool, KFENCE_POOL_SIZE);#endif
Thanks, we'll apply this to v2!
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Marco Elver
maintainerRFC PATCH 8/10 kfence, lockdep: make KFENCE compatible with lockdep
In reply to Marco ElverLockdep checks that dynamic key registration is only performed on keys that are not static objects. With KFENCE, it is possible that such a dynamically allocated key is a KFENCE object which may, however, be allocated from a static memory pool (if HAVE_ARCH_KFENCE_STATIC_POOL).
Therefore, ignore KFENCE-allocated objects in static_obj().
--- kernel/locking/lockdep.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/locking/lockdep.c +8 -0
@@ -38,6 +38,7 @@#include <linux/seq_file.h> #include <linux/spinlock.h> #include <linux/kallsyms.h> +#include <linux/kfence.h> #include <linux/interrupt.h> #include <linux/stacktrace.h> #include <linux/debug_locks.h>
@@ -755,6 +756,13 @@ static int static_obj(const void *obj)if (arch_is_kernel_initmem_freed (addr))return 0 ; +/* +* KFENCE objects may be allocated from a static memory pool, but are +* not actually static objects. +*/ +if (is_kfence_address (obj)) +return 0 ; +/* * static variable? */
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Marco Elver
maintainerRFC PATCH 9/10 kfence, Documentation: add KFENCE documentation
In reply to Marco ElverAdd KFENCE documentation in dev-tools/kfence.rst, and add to index.
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100644 Documentation/dev-tools/kfence.rst
Documentation/dev-tools/index.rst +1 -0
@@ -22,6 +22,7 @@ whole; patches welcome! ubsan kmemleak kcsan + kfence gdb-kernel-debugging kgdb kselftest
Documentation/dev-tools/kfence.rst new file +285 -0
285 lines not on this page · raw message
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Re: [PATCH RFC 09/10] kfence, Documentation: add KFENCE documentation
Quoted 85 lines · Marco Elver
Add KFENCE documentation in
dev-tools/kfence.rst, and add to index.Co-developed-by Alexander Potapenko <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>Signed-off-by Marco Elver <[email protected]>--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100644 Documentation/dev-tools/kfence.rstDocumentation/dev-tools/index.rst +1
-0@@ -22,6 +22,7 @@ whole; patches welcome! ubsan kmemleak kcsan + kfence gdb-kernel-debugging kgdb kselftestDocumentation/dev-tools/kfence.rst new file +55
-0@@ -0,0 +1,285 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Kernel Electric-Fence (KFENCE) +============================== + +Kernel Electric-Fence (KFENCE) is a low-overhead sampling-based memory safety +error detector. KFENCE detects heap out-of-bounds access, use-after-free, and +invalid-free errors. + +KFENCE is designed to be enabled in production kernels, and has near zero +performance overhead. Compared to KASAN, KFENCE trades performance for +precision. The main motivation behind KFENCE's design, is that with enough +total uptime KFENCE will detect bugs in code paths not typically exercised by +non-production test workloads. One way to quickly achieve a large enough total +uptime is when the tool is deployed across a large fleet of machines. + +Usage +----- + +To enable KFENCE, configure the kernel with:: + + CONFIG_KFENCE=y + +KFENCE provides several other configuration options to customize behaviour (see +the respective help text in ``lib/Kconfig.kfence`` for more info). + +Tuning performance +~~~~~~~~~~~~~~~~~~ + +The most important parameter is KFENCE's sample interval, which can be set via +the kernel boot parameter ``kfence.sample_interval`` in milliseconds. The +sample interval determines the frequency with which heap allocations will be +guarded by KFENCE. The default is configurable via the Kconfig option +``CONFIG_KFENCE_SAMPLE_INTERVAL``. Setting ``kfence.sample_interval=0`` +disables KFENCE. + +With the Kconfig option ``CONFIG_KFENCE_NUM_OBJECTS`` (default 255), the number +of available guarded objects can be controlled. Each object requires 2 pages, +one for the object itself and the other one used as a guard page; object pages +are interleaved with guard pages, and every object page is therefore surrounded +by two guard pages. + +The total memory dedicated to the KFENCE memory pool can be computed as:: + + ( #objects + 1 ) * 2 * PAGE_SIZE + +Using the default config, and assuming a page size of 4 KiB, results in +dedicating 2 MiB to the KFENCE memory pool. + +Error reports +~~~~~~~~~~~~~ + +A typical out-of-bounds access looks like this:: + + ==================================================================
Documentation/dev-tools/kfence.rst line 65
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ BUG: KFENCE: out-of-bounds in test_out_of_bounds_read+0xa3/0x22b + + Out-of-bounds access at 0xffffffffb672efff (left of kfence-#17): + test_out_of_bounds_read+0xa3/0x22b + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#17 [0xffffffffb672f000-0xffffffffb672f01f, size=32, cache=kmalloc-32] allocated in:
Does the user need to know that this is object #17? This doesn't seem like something that can be useful for anything.
Quoted 20 lines
Documentation/dev-tools/kfence.rst new file +20
-0+ __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_out_of_bounds_read+0x98/0x22b + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 107 Comm: kunit_try_catch Not tainted 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +The header of the report provides a short summary of the function involved in +the access. It is followed by more detailed information about the access and +its origin. + +Use-after-free accesses are reported as:: + + ==================================================================
Documentation/dev-tools/kfence.rst line 95
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ BUG: KFENCE: use-after-free in test_use_after_free_read+0xb3/0x143 + + Use-after-free access at 0xffffffffb673dfe0: + test_use_after_free_read+0xb3/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#24 [0xffffffffb673dfe0-0xffffffffb673dfff, size=32, cache=kmalloc-32] allocated in:
Same here.
Also, this says object #24, but the stack trace above doesn't mention which object it is. Is it the same one?
Quoted 53 lines
Documentation/dev-tools/kfence.rst new file +53
-0+ __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_use_after_free_read+0x76/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_use_after_free_read+0xa8/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 109 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also reports on invalid frees, such as double-frees:: + + ================================================================== + BUG: KFENCE: invalid free in test_double_free+0xdc/0x171 + + Invalid free of 0xffffffffb6741000: + test_double_free+0xdc/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#26 [0xffffffffb6741000-0xffffffffb674101f, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_double_free+0x76/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_double_free+0xa8/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 111 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014
Documentation/dev-tools/kfence.rst line 158
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ ================================================================== + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object. +These are reported on frees:: + + ================================================================== + BUG: KFENCE: memory corruption in test_kmalloc_aligned_oob_write+0xef/0x184 + + Detected corrupted memory at 0xffffffffb6797ff9 [ 0xac . . . . . . ]:
It's not really clear what is 0xac here. Value of the corrupted byte? What does '.' stand for?
Also, if this is to be used in production, printing kernel memory bytes might lead to info-leaks.
Quoted 47 lines
Documentation/dev-tools/kfence.rst new file +47
-0+ test_kmalloc_aligned_oob_write+0xef/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#69 [0xffffffffb6797fb0-0xffffffffb6797ff8, size=73, cache=kmalloc-96] allocated in: + __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_kmalloc_aligned_oob_write+0x57/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 120 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +For such errors, the address where the corruption as well as the corrupt bytes +are shown. + +And finally, KFENCE may also report on invalid accesses to any protected page +where it was not possible to determine an associated object, e.g. if adjacent +object pages had not yet been allocated:: + + ================================================================== + BUG: KFENCE: invalid access in test_invalid_access+0x26/0xe0 + + Invalid access at 0xffffffffb670b00a: + test_invalid_access+0x26/0xe0 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 124 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +DebugFS interface +~~~~~~~~~~~~~~~~~ + +Some debugging information is exposed via debugfs: + +* The file ``/sys/kernel/debug/kfence/stats`` provides runtime statistics.
Documentation/dev-tools/kfence.rst
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ +* The file ``/sys/kernel/debug/kfence/objects`` provides a list of objects + allocated via KFENCE, including those already freed but protected. + +Implementation Details +---------------------- + +Guarded allocations are set up based on the sample interval. After expiration +of the sample interval, a guarded allocation from the KFENCE object pool is +returned to the main allocator (SLAB or SLUB).
Only for freed allocations, right?
Documentation/dev-tools/kfence.rst
Quoted 12 lines
At this point, the timer is
Documentation/dev-tools/kfence.rst new file +11
-0+reset, and the next allocation is set up after the expiration of the interval. +To "gate" a KFENCE allocation through the main allocator's fast-path without +overhead, KFENCE relies on static branches via the static keys infrastructure. +The static branch is toggled to redirect the allocation to KFENCE. + +KFENCE objects each reside on a dedicated page, at either the left or right +page boundaries selected at random. The pages to the left and right of the +object page are "guard pages", whose attributes are changed to a protected +state, and cause page faults on any attempted access. Such page faults are then +intercepted by KFENCE, which handles the fault gracefully by reporting an +out-of-bounds access.
I'd start a new paragraph here:
Quoted 1 line
The side opposite of an object's guard page is used as a
Not a native speaker, but "The side opposite _to_" sounds better. Or "The opposite side of".
Quoted 1 line
+pattern-based redzone, to detect out-of-bounds writes on the unprotected sed of
"sed"?
Documentation/dev-tools/kfence.rst line 233
Quoted 6 lines
Documentation/dev-tools/kfence.rst new file +6
-0+the object on frees (for special alignment and size combinations, both sides of +the object are redzoned). + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object; +these are reported on frees.
Not really clear, what is "other side" and how it's different from the "opposite side" mentioned above. The figure doesn't really help.
Quoted 8 lines
Documentation/dev-tools/kfence.rst new file +8
-0+ +The following figure illustrates the page layout:: + + ---+-----------+-----------+-----------+-----------+-----------+--- + | xxxxxxxxx | O : | xxxxxxxxx | : O | xxxxxxxxx | + | xxxxxxxxx | B : | xxxxxxxxx | : B | xxxxxxxxx | + | x GUARD x | J : RED- | x GUARD x | RED- : J | x GUARD x | + | xxxxxxxxx | E : ZONE | xxxxxxxxx | ZONE : E | xxxxxxxxx |
Documentation/dev-tools/kfence.rst line 251
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ | xxxxxxxxx | C : | xxxxxxxxx | : C | xxxxxxxxx | + | xxxxxxxxx | T : | xxxxxxxxx | : T | xxxxxxxxx | + ---+-----------+-----------+-----------+-----------+-----------+--- + +Upon deallocation of a KFENCE object, the object's page is again protected and +the object is marked as freed. Any further access to the object causes a fault +and KFENCE reports a use-after-free access. Freed objects are inserted at the +tail of KFENCE's freelist, so that the least recently freed objects are reused +first, and the chances of detecting use-after-frees of recently freed objects +is increased.
Seems really similar to KASAN's quarantine? Is the implementation much different?
Quoted 37 lines
Documentation/dev-tools/kfence.rst new file +34
-1+ +Interface +--------- + +The following describes the functions which are used by allocators as well page +handling code to set up and deal with KFENCE allocations. + +.. kernel-doc:: include/linux/kfence.h + :functions: is_kfence_address + kfence_shutdown_cache + kfence_alloc kfence_free + kfence_ksize kfence_object_start + kfence_handle_page_fault + +Related Tools +------------- + +In userspace, a similar approach is taken by `GWP-ASan +<http://llvm.org/docs/GwpAsan.html>`_. GWP-ASan also relies on guard pages and +a sampling strategy to detect memory unsafety bugs at scale. KFENCE's design is +directly influenced by GWP-ASan, and can be seen as its kernel sibling. Another +similar but non-sampling approach, that also inspired the name "KFENCE", can be +found in the userspace `Electric Fence Malloc Debugger +<https://linux.die.net/man/3/efence>`_. + +In the kernel, several tools exist to debug memory access errors, and in +particular KASAN can detect all bug classes that KFENCE can detect. While KASAN +is more precise, relying on compiler instrumentation, this comes at a +performance cost. We want to highlight that KASAN and KFENCE are complementary, +with different target environments. For instance, KASAN is the better +debugging-aid, where a simple reproducer exists: due to the lower chance to +detect the error, it would require more effort using KFENCE to debug. +Deployments at scale, however, would benefit from using KFENCE to discover bugs +due to code paths not exercised by test cases or fuzzers. --2.28.0.526.ge36021eeef-goog
Marco Elver
maintainerRe: [PATCH RFC 09/10] kfence, Documentation: add KFENCE documentation
Quoted 92 lines · Andrey Konovalov
Quoted 90 lines · Marco Elver
Add KFENCE documentation in
dev-tools/kfence.rst, and add to index.Co-developed-by Alexander Potapenko <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>Signed-off-by Marco Elver <[email protected]>--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100644 Documentation/dev-tools/kfence.rstDocumentation/dev-tools/index.rst +1
-0@@ -22,6 +22,7 @@ whole; patches welcome! ubsan kmemleak kcsan + kfence gdb-kernel-debugging kgdb kselftestDocumentation/dev-tools/kfence.rst new file +60
-0@@ -0,0 +1,285 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Kernel Electric-Fence (KFENCE) +============================== + +Kernel Electric-Fence (KFENCE) is a low-overhead sampling-based memory safety +error detector. KFENCE detects heap out-of-bounds access, use-after-free, and +invalid-free errors. + +KFENCE is designed to be enabled in production kernels, and has near zero +performance overhead. Compared to KASAN, KFENCE trades performance for +precision. The main motivation behind KFENCE's design, is that with enough +total uptime KFENCE will detect bugs in code paths not typically exercised by +non-production test workloads. One way to quickly achieve a large enough total +uptime is when the tool is deployed across a large fleet of machines. + +Usage +----- + +To enable KFENCE, configure the kernel with:: + + CONFIG_KFENCE=y + +KFENCE provides several other configuration options to customize behaviour (see +the respective help text in ``lib/Kconfig.kfence`` for more info). + +Tuning performance +~~~~~~~~~~~~~~~~~~ + +The most important parameter is KFENCE's sample interval, which can be set via +the kernel boot parameter ``kfence.sample_interval`` in milliseconds. The +sample interval determines the frequency with which heap allocations will be +guarded by KFENCE. The default is configurable via the Kconfig option +``CONFIG_KFENCE_SAMPLE_INTERVAL``. Setting ``kfence.sample_interval=0`` +disables KFENCE. + +With the Kconfig option ``CONFIG_KFENCE_NUM_OBJECTS`` (default 255), the number +of available guarded objects can be controlled. Each object requires 2 pages, +one for the object itself and the other one used as a guard page; object pages +are interleaved with guard pages, and every object page is therefore surrounded +by two guard pages. + +The total memory dedicated to the KFENCE memory pool can be computed as:: + + ( #objects + 1 ) * 2 * PAGE_SIZE + +Using the default config, and assuming a page size of 4 KiB, results in +dedicating 2 MiB to the KFENCE memory pool. + +Error reports +~~~~~~~~~~~~~ + +A typical out-of-bounds access looks like this:: + + ================================================================== + BUG: KFENCE: out-of-bounds in test_out_of_bounds_read+0xa3/0x22b + + Out-of-bounds access at 0xffffffffb672efff (left of kfence-#17): + test_out_of_bounds_read+0xa3/0x22b + kunit_try_run_case+0x51/0x85
Quoted 8 lines
Documentation/dev-tools/kfence.rst line 65
Quoted 5 lines
Documentation/dev-tools/kfence.rst new file +5
-0+ kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#17 [0xffffffffb672f000-0xffffffffb672f01f, size=32, cache=kmalloc-32] allocated in:Does the user need to know that this is object #17? This doesn't seem like something that can be useful for anything.
Some arguments for keeping it:
- We need to write something like "left of <object>". And then we need to say where <object> is allocated. Giving objects names makes it easier to understand the link between "left of <object>" and the stacktrace shown after "<object> allocated in". We could make <object> just "object", but reading "left/right of object" and then "object allocated in:" can be a little confusing.
- We can look up the object via its number in the debugfs objects list (/sys/kernel/debug/kfence/objects). For example, if we see an OOB access, we can then check the objects file and see if the object is still allocated or not, or if it has been recycled.
I don't believe it's distracting anyone, and if there is a chance that keeping this information can help debug a problem, we ought to keep it.
Quoted 33 lines
Quoted 20 lines
Documentation/dev-tools/kfence.rst new file +20
-0+ __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_out_of_bounds_read+0x98/0x22b + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 107 Comm: kunit_try_catch Not tainted 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +The header of the report provides a short summary of the function involved in +the access. It is followed by more detailed information about the access and +its origin. + +Use-after-free accesses are reported as:: + + ==================================================================Documentation/dev-tools/kfence.rst line 95
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ BUG: KFENCE: use-after-free in test_use_after_free_read+0xb3/0x143 + + Use-after-free access at 0xffffffffb673dfe0: + test_use_after_free_read+0xb3/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#24 [0xffffffffb673dfe0-0xffffffffb673dfff, size=32, cache=kmalloc-32] allocated in:Same here.
Quoted 2 lines
Also, this says object #24, but the stack trace above doesn't mention which object it is. Is it the same one?
Right, the above stacktrace should then say "kfence-#24". (But the address also hints at this.)
Quoted 58 lines
Quoted 58 lines
Documentation/dev-tools/kfence.rst new file +58
-0+ __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_use_after_free_read+0x76/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_use_after_free_read+0xa8/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 109 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also reports on invalid frees, such as double-frees:: + + ================================================================== + BUG: KFENCE: invalid free in test_double_free+0xdc/0x171 + + Invalid free of 0xffffffffb6741000: + test_double_free+0xdc/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#26 [0xffffffffb6741000-0xffffffffb674101f, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_double_free+0x76/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_double_free+0xa8/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 111 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object. +These are reported on frees::
Quoted 8 lines
Documentation/dev-tools/kfence.rst line 158
Quoted 5 lines
Documentation/dev-tools/kfence.rst new file +5
-0+ + ================================================================== + BUG: KFENCE: memory corruption in test_kmalloc_aligned_oob_write+0xef/0x184 + + Detected corrupted memory at 0xffffffffb6797ff9 [ 0xac . . . . . . ]:It's not really clear what is
0xachere. Value of the corrupted byte? What does '.' stand for?
We can probably explain that better below. The values are the corrupt bytes, the '.' are untouched bytes.
Quoted 2 lines
Also, if this is to be used in production, printing kernel memory bytes might lead to info-leaks.
We do not print them if !CONFIG_DEBUG_KERNEL, and instead show '!' for changed bytes. Maybe we can add this somewhere here as well.
Quoted 51 lines
Quoted 51 lines
Documentation/dev-tools/kfence.rst new file +51
-0+ test_kmalloc_aligned_oob_write+0xef/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#69 [0xffffffffb6797fb0-0xffffffffb6797ff8, size=73, cache=kmalloc-96] allocated in: + __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_kmalloc_aligned_oob_write+0x57/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 120 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +For such errors, the address where the corruption as well as the corrupt bytes +are shown. + +And finally, KFENCE may also report on invalid accesses to any protected page +where it was not possible to determine an associated object, e.g. if adjacent +object pages had not yet been allocated:: + + ================================================================== + BUG: KFENCE: invalid access in test_invalid_access+0x26/0xe0 + + Invalid access at 0xffffffffb670b00a: + test_invalid_access+0x26/0xe0 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 124 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +DebugFS interface +~~~~~~~~~~~~~~~~~ + +Some debugging information is exposed via debugfs: + +* The file ``/sys/kernel/debug/kfence/stats`` provides runtime statistics. + +* The file ``/sys/kernel/debug/kfence/objects`` provides a list of objects + allocated via KFENCE, including those already freed but protected. +
Quoted 8 lines
Documentation/dev-tools/kfence.rst
Quoted 6 lines
Documentation/dev-tools/kfence.rst new file +6
-0+Implementation Details +---------------------- + +Guarded allocations are set up based on the sample interval. After expiration +of the sample interval, a guarded allocation from the KFENCE object pool is +returned to the main allocator (SLAB or SLUB).Only for freed allocations, right?
Which "freed allocation"? What this paragraph says is that after the sample interval elapsed, we'll return a KFENCE allocation on kmalloc. It doesn't yet talk about freeing.
Quoted 15 lines
Documentation/dev-tools/kfence.rst
Quoted 12 lines
At this point, the timer is
Documentation/dev-tools/kfence.rst new file +11
-0+reset, and the next allocation is set up after the expiration of the interval. +To "gate" a KFENCE allocation through the main allocator's fast-path without +overhead, KFENCE relies on static branches via the static keys infrastructure. +The static branch is toggled to redirect the allocation to KFENCE. + +KFENCE objects each reside on a dedicated page, at either the left or right +page boundaries selected at random. The pages to the left and right of the +object page are "guard pages", whose attributes are changed to a protected +state, and cause page faults on any attempted access. Such page faults are then +intercepted by KFENCE, which handles the fault gracefully by reporting an +out-of-bounds access.I'd start a new paragraph here:
Quoted 4 lines
Quoted 1 line
The side opposite of an object's guard page is used as a
Not a native speaker, but "The side opposite _to_" sounds better. Or "The opposite side of".
All are fine. Using "to" indicates direction, which in this case is valid too.
Quoted 3 lines
Quoted 1 line
+pattern-based redzone, to detect out-of-bounds writes on the unprotected sed of
"sed"?
side
Quoted 9 lines
Documentation/dev-tools/kfence.rst line 233
Quoted 6 lines
Documentation/dev-tools/kfence.rst new file +6
-0+the object on frees (for special alignment and size combinations, both sides of +the object are redzoned). + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object; +these are reported on frees.Not really clear, what is "other side" and how it's different from the "opposite side" mentioned above. The figure doesn't really help.
Redzone and guard page sandwich the object. Not sure how I can make it clearer yet, but I'll try.
Quoted 13 lines
Quoted 13 lines
Documentation/dev-tools/kfence.rst new file +13
-0+ +The following figure illustrates the page layout:: + + ---+-----------+-----------+-----------+-----------+-----------+--- + | xxxxxxxxx | O : | xxxxxxxxx | : O | xxxxxxxxx | + | xxxxxxxxx | B : | xxxxxxxxx | : B | xxxxxxxxx | + | x GUARD x | J : RED- | x GUARD x | RED- : J | x GUARD x | + | xxxxxxxxx | E : ZONE | xxxxxxxxx | ZONE : E | xxxxxxxxx | + | xxxxxxxxx | C : | xxxxxxxxx | : C | xxxxxxxxx | + | xxxxxxxxx | T : | xxxxxxxxx | : T | xxxxxxxxx | + ---+-----------+-----------+-----------+-----------+-----------+--- + +Upon deallocation of a KFENCE object, the object's page is again protected and
Quoted 8 lines
Documentation/dev-tools/kfence.rst line 251
Quoted 5 lines
Documentation/dev-tools/kfence.rst new file +5
-0+the object is marked as freed. Any further access to the object causes a fault +and KFENCE reports a use-after-free access. Freed objects are inserted at the +tail of KFENCE's freelist, so that the least recently freed objects are reused +first, and the chances of detecting use-after-frees of recently freed objects +is increased.Seems really similar to KASAN's quarantine? Is the implementation much different?
It's a list, and we just insert at the tail. Why does it matter?
Thanks for the comments. I'll try to fix in v2.
Thanks,
-- Marco
Re: [PATCH RFC 09/10] kfence, Documentation: add KFENCE documentation
Quoted 296 lines · Marco Elver
Quoted 92 lines · Andrey Konovalov
Quoted 90 lines · Marco Elver
Add KFENCE documentation in
dev-tools/kfence.rst, and add to index.Co-developed-by Alexander Potapenko <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>Signed-off-by Marco Elver <[email protected]>--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100644 Documentation/dev-tools/kfence.rstDocumentation/dev-tools/index.rst +1
-0@@ -22,6 +22,7 @@ whole; patches welcome! ubsan kmemleak kcsan + kfence gdb-kernel-debugging kgdb kselftestDocumentation/dev-tools/kfence.rst new file +60
-0@@ -0,0 +1,285 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Kernel Electric-Fence (KFENCE) +============================== + +Kernel Electric-Fence (KFENCE) is a low-overhead sampling-based memory safety +error detector. KFENCE detects heap out-of-bounds access, use-after-free, and +invalid-free errors. + +KFENCE is designed to be enabled in production kernels, and has near zero +performance overhead. Compared to KASAN, KFENCE trades performance for +precision. The main motivation behind KFENCE's design, is that with enough +total uptime KFENCE will detect bugs in code paths not typically exercised by +non-production test workloads. One way to quickly achieve a large enough total +uptime is when the tool is deployed across a large fleet of machines. + +Usage +----- + +To enable KFENCE, configure the kernel with:: + + CONFIG_KFENCE=y + +KFENCE provides several other configuration options to customize behaviour (see +the respective help text in ``lib/Kconfig.kfence`` for more info). + +Tuning performance +~~~~~~~~~~~~~~~~~~ + +The most important parameter is KFENCE's sample interval, which can be set via +the kernel boot parameter ``kfence.sample_interval`` in milliseconds. The +sample interval determines the frequency with which heap allocations will be +guarded by KFENCE. The default is configurable via the Kconfig option +``CONFIG_KFENCE_SAMPLE_INTERVAL``. Setting ``kfence.sample_interval=0`` +disables KFENCE. + +With the Kconfig option ``CONFIG_KFENCE_NUM_OBJECTS`` (default 255), the number +of available guarded objects can be controlled. Each object requires 2 pages, +one for the object itself and the other one used as a guard page; object pages +are interleaved with guard pages, and every object page is therefore surrounded +by two guard pages. + +The total memory dedicated to the KFENCE memory pool can be computed as:: + + ( #objects + 1 ) * 2 * PAGE_SIZE + +Using the default config, and assuming a page size of 4 KiB, results in +dedicating 2 MiB to the KFENCE memory pool. + +Error reports +~~~~~~~~~~~~~ + +A typical out-of-bounds access looks like this:: + + ================================================================== + BUG: KFENCE: out-of-bounds in test_out_of_bounds_read+0xa3/0x22b + + Out-of-bounds access at 0xffffffffb672efff (left of kfence-#17): + test_out_of_bounds_read+0xa3/0x22b + kunit_try_run_case+0x51/0x85Quoted 8 lines
Documentation/dev-tools/kfence.rst line 65
Quoted 5 lines
Documentation/dev-tools/kfence.rst new file +5
-0+ kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#17 [0xffffffffb672f000-0xffffffffb672f01f, size=32, cache=kmalloc-32] allocated in:Does the user need to know that this is object #17? This doesn't seem like something that can be useful for anything.
Some arguments for keeping it:
- We need to write something like "left of <object>". And then we need to say where <object> is allocated. Giving objects names makes it easier to understand the link between "left of <object>" and the stacktrace shown after "<object> allocated in". We could make <object> just "object", but reading "left/right of object" and then "object allocated in:" can be a little confusing.
- We can look up the object via its number in the debugfs objects list (
/sys/kernel/debug/kfence/objects). For example, if we see an OOB access, we can then check the objects file and see if the object is still allocated or not, or if it has been recycled.I don't believe it's distracting anyone, and if there is a chance that keeping this information can help debug a problem, we ought to keep it.
Quoted 33 lines
Quoted 20 lines
Documentation/dev-tools/kfence.rst new file +20
-0+ __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_out_of_bounds_read+0x98/0x22b + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 107 Comm: kunit_try_catch Not tainted 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +The header of the report provides a short summary of the function involved in +the access. It is followed by more detailed information about the access and +its origin. + +Use-after-free accesses are reported as:: + + ==================================================================Documentation/dev-tools/kfence.rst line 95
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ BUG: KFENCE: use-after-free in test_use_after_free_read+0xb3/0x143 + + Use-after-free access at 0xffffffffb673dfe0: + test_use_after_free_read+0xb3/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#24 [0xffffffffb673dfe0-0xffffffffb673dfff, size=32, cache=kmalloc-32] allocated in:Same here.
Quoted 2 lines
Also, this says object #24, but the stack trace above doesn't mention which object it is. Is it the same one?
Right, the above stacktrace should then say "kfence-#24". (But the address also hints at this.)
Quoted 58 lines
Quoted 58 lines
Documentation/dev-tools/kfence.rst new file +58
-0+ __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_use_after_free_read+0x76/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_use_after_free_read+0xa8/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 109 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also reports on invalid frees, such as double-frees:: + + ================================================================== + BUG: KFENCE: invalid free in test_double_free+0xdc/0x171 + + Invalid free of 0xffffffffb6741000: + test_double_free+0xdc/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#26 [0xffffffffb6741000-0xffffffffb674101f, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_double_free+0x76/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_double_free+0xa8/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 111 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object. +These are reported on frees::Quoted 8 lines
Documentation/dev-tools/kfence.rst line 158
Quoted 5 lines
Documentation/dev-tools/kfence.rst new file +5
-0+ + ================================================================== + BUG: KFENCE: memory corruption in test_kmalloc_aligned_oob_write+0xef/0x184 + + Detected corrupted memory at 0xffffffffb6797ff9 [ 0xac . . . . . . ]:It's not really clear what is
0xachere. Value of the corrupted byte? What does '.' stand for?We can probably explain that better below. The values are the corrupt bytes, the '.' are untouched bytes.
Quoted 2 lines
Also, if this is to be used in production, printing kernel memory bytes might lead to info-leaks.
We do not print them if !CONFIG_DEBUG_KERNEL, and instead show '!' for changed bytes. Maybe we can add this somewhere here as well.
Quoted 51 lines
Quoted 51 lines
Documentation/dev-tools/kfence.rst new file +51
-0+ test_kmalloc_aligned_oob_write+0xef/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#69 [0xffffffffb6797fb0-0xffffffffb6797ff8, size=73, cache=kmalloc-96] allocated in: + __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_kmalloc_aligned_oob_write+0x57/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 120 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +For such errors, the address where the corruption as well as the corrupt bytes +are shown. + +And finally, KFENCE may also report on invalid accesses to any protected page +where it was not possible to determine an associated object, e.g. if adjacent +object pages had not yet been allocated:: + + ================================================================== + BUG: KFENCE: invalid access in test_invalid_access+0x26/0xe0 + + Invalid access at 0xffffffffb670b00a: + test_invalid_access+0x26/0xe0 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 124 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +DebugFS interface +~~~~~~~~~~~~~~~~~ + +Some debugging information is exposed via debugfs: + +* The file ``/sys/kernel/debug/kfence/stats`` provides runtime statistics. + +* The file ``/sys/kernel/debug/kfence/objects`` provides a list of objects + allocated via KFENCE, including those already freed but protected. +Quoted 8 lines
Documentation/dev-tools/kfence.rst
Quoted 6 lines
Documentation/dev-tools/kfence.rst new file +6
-0+Implementation Details +---------------------- + +Guarded allocations are set up based on the sample interval. After expiration +of the sample interval, a guarded allocation from the KFENCE object pool is +returned to the main allocator (SLAB or SLUB).Only for freed allocations, right?
Quoted 3 lines
Which "freed allocation"? What this paragraph says is that after the sample interval elapsed, we'll return a KFENCE allocation on kmalloc. It doesn't yet talk about freeing.
It says that an allocation is returned to the main allocator, and this is what is usually described with the word "freed". Do you mean something else here?
Quoted 63 lines
Quoted 15 lines
Documentation/dev-tools/kfence.rst
Quoted 12 lines
At this point, the timer is
Documentation/dev-tools/kfence.rst new file +11
-0+reset, and the next allocation is set up after the expiration of the interval. +To "gate" a KFENCE allocation through the main allocator's fast-path without +overhead, KFENCE relies on static branches via the static keys infrastructure. +The static branch is toggled to redirect the allocation to KFENCE. + +KFENCE objects each reside on a dedicated page, at either the left or right +page boundaries selected at random. The pages to the left and right of the +object page are "guard pages", whose attributes are changed to a protected +state, and cause page faults on any attempted access. Such page faults are then +intercepted by KFENCE, which handles the fault gracefully by reporting an +out-of-bounds access.I'd start a new paragraph here:
Quoted 4 lines
Quoted 1 line
The side opposite of an object's guard page is used as a
Not a native speaker, but "The side opposite _to_" sounds better. Or "The opposite side of".
All are fine. Using "to" indicates direction, which in this case is valid too.
Quoted 3 lines
Quoted 1 line
+pattern-based redzone, to detect out-of-bounds writes on the unprotected sed of
"sed"?
side
Quoted 9 lines
Documentation/dev-tools/kfence.rst line 233
Quoted 6 lines
Documentation/dev-tools/kfence.rst new file +6
-0+the object on frees (for special alignment and size combinations, both sides of +the object are redzoned). + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object; +these are reported on frees.Not really clear, what is "other side" and how it's different from the "opposite side" mentioned above. The figure doesn't really help.
Redzone and guard page sandwich the object. Not sure how I can make it clearer yet, but I'll try.
Quoted 13 lines
Quoted 13 lines
Documentation/dev-tools/kfence.rst new file +13
-0+ +The following figure illustrates the page layout:: + + ---+-----------+-----------+-----------+-----------+-----------+--- + | xxxxxxxxx | O : | xxxxxxxxx | : O | xxxxxxxxx | + | xxxxxxxxx | B : | xxxxxxxxx | : B | xxxxxxxxx | + | x GUARD x | J : RED- | x GUARD x | RED- : J | x GUARD x | + | xxxxxxxxx | E : ZONE | xxxxxxxxx | ZONE : E | xxxxxxxxx | + | xxxxxxxxx | C : | xxxxxxxxx | : C | xxxxxxxxx | + | xxxxxxxxx | T : | xxxxxxxxx | : T | xxxxxxxxx | + ---+-----------+-----------+-----------+-----------+-----------+--- + +Upon deallocation of a KFENCE object, the object's page is again protected andQuoted 8 lines
Documentation/dev-tools/kfence.rst line 251
Quoted 5 lines
Documentation/dev-tools/kfence.rst new file +5
-0+the object is marked as freed. Any further access to the object causes a fault +and KFENCE reports a use-after-free access. Freed objects are inserted at the +tail of KFENCE's freelist, so that the least recently freed objects are reused +first, and the chances of detecting use-after-frees of recently freed objects +is increased.Seems really similar to KASAN's quarantine? Is the implementation much different?
Quoted 1 line
It's a list, and we just insert at the tail. Why does it matter?
If the implementation is similar, we can then reuse quarantine. But I guess it's not.
Marco Elver
maintainerRe: [PATCH RFC 09/10] kfence, Documentation: add KFENCE documentation
Quoted 1 line · Andrey Konovalov
On Mon, Sep 7, 2020 at 6:33 PM Marco Elver <[email protected]> wrote:
[...]
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Documentation/dev-tools/kfence.rst
Quoted 3 lines
Documentation/dev-tools/kfence.rst new file +3
-0+Guarded allocations are set up based on the sample interval. After expiration +of the sample interval, a guarded allocation from the KFENCE object pool is +returned to the main allocator (SLAB or SLUB).Only for freed allocations, right?
Quoted 7 lines
Quoted 3 lines
Which "freed allocation"? What this paragraph says is that after the sample interval elapsed, we'll return a KFENCE allocation on kmalloc. It doesn't yet talk about freeing.
It says that an allocation is returned to the main allocator, and this is what is usually described with the word "freed". Do you mean something else here?
Ah, I see what's goin on. So the "returned to the main allocator" is ambiguous here. I meant to say "returned" as in kfence gives sl[au]b a kfence object to return for the next kmalloc. I'll reword this as it seems the phrase is overloaded in this context already.
[...]
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Documentation/dev-tools/kfence.rst line 251
Quoted 6 lines
Documentation/dev-tools/kfence.rst new file +6
-0+Upon deallocation of a KFENCE object, the object's page is again protected and +the object is marked as freed. Any further access to the object causes a fault +and KFENCE reports a use-after-free access. Freed objects are inserted at the +tail of KFENCE's freelist, so that the least recently freed objects are reused +first, and the chances of detecting use-after-frees of recently freed objects +is increased.Seems really similar to KASAN's quarantine? Is the implementation much different?
Quoted 4 lines
Quoted 1 line
It's a list, and we just insert at the tail. Why does it matter?
If the implementation is similar, we can then reuse quarantine. But I guess it's not.
The concept is similar, but the implementations are very different. Both use a list (although KASAN quarantine seems to reimplement its own singly-linked list). We just rely on a standard doubly-linked list, without any of the delayed freeing logic of the KASAN quarantine as KFENCE objects just change state to "freed" until they're reused (freed kfence objects are just inserted at the tail, and the next object to be used for an allocation is at the head).
Thanks,
-- Marco
Re: [PATCH RFC 09/10] kfence, Documentation: add KFENCE documentation
In reply to Marco ElverDocumentation/dev-tools/kfence.rst line 41
Quoted 12 lines · Marco Elver
Documentation/dev-tools/kfence.rst new file +12
-0+The most important parameter is KFENCE's sample interval, which can be set via +the kernel boot parameter ``kfence.sample_interval`` in milliseconds. The +sample interval determines the frequency with which heap allocations will be +guarded by KFENCE. The default is configurable via the Kconfig option +``CONFIG_KFENCE_SAMPLE_INTERVAL``. Setting ``kfence.sample_interval=0`` +disables KFENCE. + +With the Kconfig option ``CONFIG_KFENCE_NUM_OBJECTS`` (default 255), the number +of available guarded objects can be controlled. Each object requires 2 pages, +one for the object itself and the other one used as a guard page; object pages +are interleaved with guard pages, and every object page is therefore surrounded +by two guard pages.
Is it hard to make these both tunable at runtime?
It would be nice if I hit a KFENCE error on a system to bump up the number of objects and turn up the frequency of guarded objects to try to hit it again. That would be a really nice feature for development environments.
It would also be nice to have a counter somewhere (/proc/vmstat?) to explicitly say how many pages are currently being used.
I didn't mention it elsewhere, but this work looks really nice. It has very little impact on the core kernel and looks like a very nice tool to have in the toolbox. I don't see any major reasons we wouldn't want to merge after our typical bikeshedding. :)
Marco Elver
maintainerRe: [PATCH RFC 09/10] kfence, Documentation: add KFENCE documentation
Quoted 15 lines · Dave Hansen
Documentation/dev-tools/kfence.rst line 41
Quoted 12 lines · Marco Elver
Documentation/dev-tools/kfence.rst new file +12
-0+The most important parameter is KFENCE's sample interval, which can be set via +the kernel boot parameter ``kfence.sample_interval`` in milliseconds. The +sample interval determines the frequency with which heap allocations will be +guarded by KFENCE. The default is configurable via the Kconfig option +``CONFIG_KFENCE_SAMPLE_INTERVAL``. Setting ``kfence.sample_interval=0`` +disables KFENCE. + +With the Kconfig option ``CONFIG_KFENCE_NUM_OBJECTS`` (default 255), the number +of available guarded objects can be controlled. Each object requires 2 pages, +one for the object itself and the other one used as a guard page; object pages +are interleaved with guard pages, and every object page is therefore surrounded +by two guard pages.Is it hard to make these both tunable at runtime?
The number of objects is quite hard, because it really complicates bookkeeping and might also have an impact on performance, which is why we prefer the statically allocated pool (like on x86, and we're trying to get it for arm64 as well).
The sample interval is already tunable, just write to /sys/module/kfence/parameters/sample_interval. Although we have this (see core.c):
module_param_named(sample_interval, kfence_sample_interval, ulong, IS_ENABLED(CONFIG_DEBUG_KERNEL) ? 0600 : 0400);
I was wondering if it should also be tweakable on non-debug kernels, but I fear it might be abused. Sure, you need to be root to change it, but maybe I'm being overly conservative here? If you don't see huge problems with it we could just make it 0600 for all builds.
Quoted 4 lines
It would be nice if I hit a KFENCE error on a system to bump up the number of objects and turn up the frequency of guarded objects to try to hit it again. That would be a really nice feature for development environments.
Indeed, which is why we also found it might be useful to tweak sample_interval at runtime for debug-kernels. Although I don't know how much luck you'll have hitting it again.
My strategy at that point would be to take the stack traces, try to construct test-cases for those code paths, and run them through KASAN (if it isn't immediately obvious what the problem is).
Quoted 2 lines
It would also be nice to have a counter somewhere (
/proc/vmstat?) to explicitly say how many pages are currently being used.
You can check /sys/kernel/debug/kfence/stats. On a system I just booted:
[root@syzkaller][~]# cat /sys/kernel/debug/kfence/stats enabled: 1 currently allocated: 18 total allocations: 105 total frees: 87 total bugs: 0
The "currently allocated" count is the currently used KFENCE objects (of 255 for the default config).
Quoted 4 lines
I didn't mention it elsewhere, but this work looks really nice. It has very little impact on the core kernel and looks like a very nice tool to have in the toolbox. I don't see any major reasons we wouldn't want to merge after our typical bikeshedding. :)
Thank you!
-- Marco
Dmitry Vyukov
reviewerRe: [PATCH RFC 09/10] kfence, Documentation: add KFENCE documentation
In reply to Marco ElverQuoted 123 lines · Marco Elver
Add KFENCE documentation in
dev-tools/kfence.rst, and add to index.Co-developed-by Alexander Potapenko <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>Signed-off-by Marco Elver <[email protected]>--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100644 Documentation/dev-tools/kfence.rstDocumentation/dev-tools/index.rst +1
-0@@ -22,6 +22,7 @@ whole; patches welcome! ubsan kmemleak kcsan + kfence gdb-kernel-debugging kgdb kselftestDocumentation/dev-tools/kfence.rst new file +93
-0@@ -0,0 +1,285 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Kernel Electric-Fence (KFENCE) +============================== + +Kernel Electric-Fence (KFENCE) is a low-overhead sampling-based memory safety +error detector. KFENCE detects heap out-of-bounds access, use-after-free, and +invalid-free errors. + +KFENCE is designed to be enabled in production kernels, and has near zero +performance overhead. Compared to KASAN, KFENCE trades performance for +precision. The main motivation behind KFENCE's design, is that with enough +total uptime KFENCE will detect bugs in code paths not typically exercised by +non-production test workloads. One way to quickly achieve a large enough total +uptime is when the tool is deployed across a large fleet of machines. + +Usage +----- + +To enable KFENCE, configure the kernel with:: + + CONFIG_KFENCE=y + +KFENCE provides several other configuration options to customize behaviour (see +the respective help text in ``lib/Kconfig.kfence`` for more info). + +Tuning performance +~~~~~~~~~~~~~~~~~~ + +The most important parameter is KFENCE's sample interval, which can be set via +the kernel boot parameter ``kfence.sample_interval`` in milliseconds. The +sample interval determines the frequency with which heap allocations will be +guarded by KFENCE. The default is configurable via the Kconfig option +``CONFIG_KFENCE_SAMPLE_INTERVAL``. Setting ``kfence.sample_interval=0`` +disables KFENCE. + +With the Kconfig option ``CONFIG_KFENCE_NUM_OBJECTS`` (default 255), the number +of available guarded objects can be controlled. Each object requires 2 pages, +one for the object itself and the other one used as a guard page; object pages +are interleaved with guard pages, and every object page is therefore surrounded +by two guard pages. + +The total memory dedicated to the KFENCE memory pool can be computed as:: + + ( #objects + 1 ) * 2 * PAGE_SIZE + +Using the default config, and assuming a page size of 4 KiB, results in +dedicating 2 MiB to the KFENCE memory pool. + +Error reports +~~~~~~~~~~~~~ + +A typical out-of-bounds access looks like this:: + + ================================================================== + BUG: KFENCE: out-of-bounds in test_out_of_bounds_read+0xa3/0x22b + + Out-of-bounds access at 0xffffffffb672efff (left of kfence-#17): + test_out_of_bounds_read+0xa3/0x22b + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#17 [0xffffffffb672f000-0xffffffffb672f01f, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_out_of_bounds_read+0x98/0x22b + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 107 Comm: kunit_try_catch Not tainted 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +The header of the report provides a short summary of the function involved in +the access. It is followed by more detailed information about the access and +its origin. + +Use-after-free accesses are reported as:: + + ================================================================== + BUG: KFENCE: use-after-free in test_use_after_free_read+0xb3/0x143 + + Use-after-free access at 0xffffffffb673dfe0: + test_use_after_free_read+0xb3/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30
Documentation/dev-tools/kfence.rst line 103
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ + kfence-#24 [0xffffffffb673dfe0-0xffffffffb673dfff, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_use_after_free_read+0x76/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30
Empty line between stacks for consistency and readability.
Quoted 108 lines
Documentation/dev-tools/kfence.rst new file +108
-0+ freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_use_after_free_read+0xa8/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 109 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also reports on invalid frees, such as double-frees:: + + ================================================================== + BUG: KFENCE: invalid free in test_double_free+0xdc/0x171 + + Invalid free of 0xffffffffb6741000: + test_double_free+0xdc/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#26 [0xffffffffb6741000-0xffffffffb674101f, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_double_free+0x76/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_double_free+0xa8/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 111 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object. +These are reported on frees:: + + ================================================================== + BUG: KFENCE: memory corruption in test_kmalloc_aligned_oob_write+0xef/0x184 + + Detected corrupted memory at 0xffffffffb6797ff9 [ 0xac . . . . . . ]: + test_kmalloc_aligned_oob_write+0xef/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#69 [0xffffffffb6797fb0-0xffffffffb6797ff8, size=73, cache=kmalloc-96] allocated in: + __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_kmalloc_aligned_oob_write+0x57/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 120 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +For such errors, the address where the corruption as well as the corrupt bytes +are shown. + +And finally, KFENCE may also report on invalid accesses to any protected page +where it was not possible to determine an associated object, e.g. if adjacent +object pages had not yet been allocated:: + + ================================================================== + BUG: KFENCE: invalid access in test_invalid_access+0x26/0xe0 + + Invalid access at 0xffffffffb670b00a: + test_invalid_access+0x26/0xe0 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 124 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +DebugFS interface +~~~~~~~~~~~~~~~~~ + +Some debugging information is exposed via debugfs: + +* The file ``/sys/kernel/debug/kfence/stats`` provides runtime statistics. + +* The file ``/sys/kernel/debug/kfence/objects`` provides a list of objects + allocated via KFENCE, including those already freed but protected. + +Implementation Details +----------------------
Documentation/dev-tools/kfence.rst line 221
Quoted 10 lines
Documentation/dev-tools/kfence.rst new file +10
-0+ +Guarded allocations are set up based on the sample interval. After expiration +of the sample interval, a guarded allocation from the KFENCE object pool is +returned to the main allocator (SLAB or SLUB). At this point, the timer is +reset, and the next allocation is set up after the expiration of the interval. +To "gate" a KFENCE allocation through the main allocator's fast-path without +overhead, KFENCE relies on static branches via the static keys infrastructure. +The static branch is toggled to redirect the allocation to KFENCE. + +KFENCE objects each reside on a dedicated page, at either the left or right
Do we mention anywhere explicitly that KFENCE currently only supports allocations <=page_size?
May be worth mentioning. It kinda follows from implementation but quite implicitly. One may also be confused assuming KFENCE handles larger allocations, but then not being able to figure out.
Quoted 67 lines
Documentation/dev-tools/kfence.rst new file +64
-1+page boundaries selected at random. The pages to the left and right of the +object page are "guard pages", whose attributes are changed to a protected +state, and cause page faults on any attempted access. Such page faults are then +intercepted by KFENCE, which handles the fault gracefully by reporting an +out-of-bounds access. The side opposite of an object's guard page is used as a +pattern-based redzone, to detect out-of-bounds writes on the unprotected sed of +the object on frees (for special alignment and size combinations, both sides of +the object are redzoned). + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object; +these are reported on frees. + +The following figure illustrates the page layout:: + + ---+-----------+-----------+-----------+-----------+-----------+--- + | xxxxxxxxx | O : | xxxxxxxxx | : O | xxxxxxxxx | + | xxxxxxxxx | B : | xxxxxxxxx | : B | xxxxxxxxx | + | x GUARD x | J : RED- | x GUARD x | RED- : J | x GUARD x | + | xxxxxxxxx | E : ZONE | xxxxxxxxx | ZONE : E | xxxxxxxxx | + | xxxxxxxxx | C : | xxxxxxxxx | : C | xxxxxxxxx | + | xxxxxxxxx | T : | xxxxxxxxx | : T | xxxxxxxxx | + ---+-----------+-----------+-----------+-----------+-----------+--- + +Upon deallocation of a KFENCE object, the object's page is again protected and +the object is marked as freed. Any further access to the object causes a fault +and KFENCE reports a use-after-free access. Freed objects are inserted at the +tail of KFENCE's freelist, so that the least recently freed objects are reused +first, and the chances of detecting use-after-frees of recently freed objects +is increased. + +Interface +--------- + +The following describes the functions which are used by allocators as well page +handling code to set up and deal with KFENCE allocations. + +.. kernel-doc:: include/linux/kfence.h + :functions: is_kfence_address + kfence_shutdown_cache + kfence_alloc kfence_free + kfence_ksize kfence_object_start + kfence_handle_page_fault + +Related Tools +------------- + +In userspace, a similar approach is taken by `GWP-ASan +<http://llvm.org/docs/GwpAsan.html>`_. GWP-ASan also relies on guard pages and +a sampling strategy to detect memory unsafety bugs at scale. KFENCE's design is +directly influenced by GWP-ASan, and can be seen as its kernel sibling. Another +similar but non-sampling approach, that also inspired the name "KFENCE", can be +found in the userspace `Electric Fence Malloc Debugger +<https://linux.die.net/man/3/efence>`_. + +In the kernel, several tools exist to debug memory access errors, and in +particular KASAN can detect all bug classes that KFENCE can detect. While KASAN +is more precise, relying on compiler instrumentation, this comes at a +performance cost. We want to highlight that KASAN and KFENCE are complementary, +with different target environments. For instance, KASAN is the better +debugging-aid, where a simple reproducer exists: due to the lower chance to +detect the error, it would require more effort using KFENCE to debug. +Deployments at scale, however, would benefit from using KFENCE to discover bugs +due to code paths not exercised by test cases or fuzzers. --2.28.0.526.ge36021eeef-goog
Marco Elver
maintainerRe: [PATCH RFC 09/10] kfence, Documentation: add KFENCE documentation
Quoted 129 lines · Dmitry Vyukov
Quoted 127 lines · Marco Elver
Add KFENCE documentation in
dev-tools/kfence.rst, and add to index.Co-developed-by Alexander Potapenko <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>Signed-off-by Marco Elver <[email protected]>--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100644 Documentation/dev-tools/kfence.rstDocumentation/dev-tools/index.rst +1
-0@@ -22,6 +22,7 @@ whole; patches welcome! ubsan kmemleak kcsan + kfence gdb-kernel-debugging kgdb kselftestDocumentation/dev-tools/kfence.rst new file +97
-0@@ -0,0 +1,285 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Kernel Electric-Fence (KFENCE) +============================== + +Kernel Electric-Fence (KFENCE) is a low-overhead sampling-based memory safety +error detector. KFENCE detects heap out-of-bounds access, use-after-free, and +invalid-free errors. + +KFENCE is designed to be enabled in production kernels, and has near zero +performance overhead. Compared to KASAN, KFENCE trades performance for +precision. The main motivation behind KFENCE's design, is that with enough +total uptime KFENCE will detect bugs in code paths not typically exercised by +non-production test workloads. One way to quickly achieve a large enough total +uptime is when the tool is deployed across a large fleet of machines. + +Usage +----- + +To enable KFENCE, configure the kernel with:: + + CONFIG_KFENCE=y + +KFENCE provides several other configuration options to customize behaviour (see +the respective help text in ``lib/Kconfig.kfence`` for more info). + +Tuning performance +~~~~~~~~~~~~~~~~~~ + +The most important parameter is KFENCE's sample interval, which can be set via +the kernel boot parameter ``kfence.sample_interval`` in milliseconds. The +sample interval determines the frequency with which heap allocations will be +guarded by KFENCE. The default is configurable via the Kconfig option +``CONFIG_KFENCE_SAMPLE_INTERVAL``. Setting ``kfence.sample_interval=0`` +disables KFENCE. + +With the Kconfig option ``CONFIG_KFENCE_NUM_OBJECTS`` (default 255), the number +of available guarded objects can be controlled. Each object requires 2 pages, +one for the object itself and the other one used as a guard page; object pages +are interleaved with guard pages, and every object page is therefore surrounded +by two guard pages. + +The total memory dedicated to the KFENCE memory pool can be computed as:: + + ( #objects + 1 ) * 2 * PAGE_SIZE + +Using the default config, and assuming a page size of 4 KiB, results in +dedicating 2 MiB to the KFENCE memory pool. + +Error reports +~~~~~~~~~~~~~ + +A typical out-of-bounds access looks like this:: + + ================================================================== + BUG: KFENCE: out-of-bounds in test_out_of_bounds_read+0xa3/0x22b + + Out-of-bounds access at 0xffffffffb672efff (left of kfence-#17): + test_out_of_bounds_read+0xa3/0x22b + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#17 [0xffffffffb672f000-0xffffffffb672f01f, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_out_of_bounds_read+0x98/0x22b + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 107 Comm: kunit_try_catch Not tainted 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +The header of the report provides a short summary of the function involved in +the access. It is followed by more detailed information about the access and +its origin. + +Use-after-free accesses are reported as:: + + ================================================================== + BUG: KFENCE: use-after-free in test_use_after_free_read+0xb3/0x143 + + Use-after-free access at 0xffffffffb673dfe0: + test_use_after_free_read+0xb3/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#24 [0xffffffffb673dfe0-0xffffffffb673dfff, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200
Quoted 8 lines
Documentation/dev-tools/kfence.rst line 103
Quoted 6 lines
Documentation/dev-tools/kfence.rst new file +6
-0+ test_alloc+0xf3/0x25b + test_use_after_free_read+0x76/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30Empty line between stacks for consistency and readability.
Done for v2.
Quoted 116 lines
Quoted 116 lines
Documentation/dev-tools/kfence.rst new file +116
-0+ freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_use_after_free_read+0xa8/0x143 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 109 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also reports on invalid frees, such as double-frees:: + + ================================================================== + BUG: KFENCE: invalid free in test_double_free+0xdc/0x171 + + Invalid free of 0xffffffffb6741000: + test_double_free+0xdc/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#26 [0xffffffffb6741000-0xffffffffb674101f, size=32, cache=kmalloc-32] allocated in: + __kfence_alloc+0x42d/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_double_free+0x76/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + freed in: + kfence_guarded_free+0x158/0x380 + __kfence_free+0x38/0xc0 + test_double_free+0xa8/0x171 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 111 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +KFENCE also uses pattern-based redzones on the other side of an object's guard +page, to detect out-of-bounds writes on the unprotected side of the object. +These are reported on frees:: + + ================================================================== + BUG: KFENCE: memory corruption in test_kmalloc_aligned_oob_write+0xef/0x184 + + Detected corrupted memory at 0xffffffffb6797ff9 [ 0xac . . . . . . ]: + test_kmalloc_aligned_oob_write+0xef/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + kfence-#69 [0xffffffffb6797fb0-0xffffffffb6797ff8, size=73, cache=kmalloc-96] allocated in: + __kfence_alloc+0x277/0x4c0 + __kmalloc+0x133/0x200 + test_alloc+0xf3/0x25b + test_kmalloc_aligned_oob_write+0x57/0x184 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 120 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +For such errors, the address where the corruption as well as the corrupt bytes +are shown. + +And finally, KFENCE may also report on invalid accesses to any protected page +where it was not possible to determine an associated object, e.g. if adjacent +object pages had not yet been allocated:: + + ================================================================== + BUG: KFENCE: invalid access in test_invalid_access+0x26/0xe0 + + Invalid access at 0xffffffffb670b00a: + test_invalid_access+0x26/0xe0 + kunit_try_run_case+0x51/0x85 + kunit_generic_run_threadfn_adapter+0x16/0x30 + kthread+0x137/0x160 + ret_from_fork+0x22/0x30 + + CPU: 4 PID: 124 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 + ================================================================== + +DebugFS interface +~~~~~~~~~~~~~~~~~ + +Some debugging information is exposed via debugfs: + +* The file ``/sys/kernel/debug/kfence/stats`` provides runtime statistics. + +* The file ``/sys/kernel/debug/kfence/objects`` provides a list of objects + allocated via KFENCE, including those already freed but protected. + +Implementation Details +---------------------- + +Guarded allocations are set up based on the sample interval. After expiration +of the sample interval, a guarded allocation from the KFENCE object pool is +returned to the main allocator (SLAB or SLUB). At this point, the timer is +reset, and the next allocation is set up after the expiration of the interval. +To "gate" a KFENCE allocation through the main allocator's fast-path without +overhead, KFENCE relies on static branches via the static keys infrastructure. +The static branch is toggled to redirect the allocation to KFENCE.
Quoted 8 lines
Documentation/dev-tools/kfence.rst line 221
Quoted 2 lines
Documentation/dev-tools/kfence.rst new file +2
-0+ +KFENCE objects each reside on a dedicated page, at either the left or rightDo we mention anywhere explicitly that KFENCE currently only supports allocations <=page_size?
May be worth mentioning. It kinda follows from implementation but quite implicitly. One may also be confused assuming KFENCE handles larger allocations, but then not being able to figure out.
We can add a note that "allocation sizes up to PAGE_SIZE are supported".
Thanks,
-- Marco
Marco Elver
maintainerRFC PATCH 10/10 kfence: add test suite
In reply to Marco ElverAdd KFENCE test suite, testing various error detection scenarios. Makes use of KUnit for test organization. Since KFENCE's interface to obtain error reports is via the console, the test verifies that KFENCE outputs expected reports to the console.
--- lib/Kconfig.kfence | 12 + mm/kfence/Makefile | 3 + mm/kfence/kfence-test.c | 777 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 792 insertions(+) create mode 100644 mm/kfence/kfence-test.c
lib/Kconfig.kfence +12 -0
@@ -55,4 +55,16 @@ config KFENCE_FAULT_INJECTION this option is to stress-test KFENCE with concurrent error reports and allocations/frees. A value of 0 disables fault injection. +config KFENCE_TEST + tristate "KFENCE test suite" + depends on TRACEPOINTS && KUNIT + help + Test suite for KFENCE, testing various error detection scenarios with + various allocation types, and checking that reports are correctly + output to console. + + Say Y here if you want the test to be built into the kernel and run + during boot; say M if you want the test to build as a module; say N + if you are unsure. + endif # KFENCE
mm/kfence/Makefile +3 -0
@@ -1,3 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_KFENCE) := core.o report.o + +CFLAGS_kfence-test.o := -g -fno-omit-frame-pointer -fno-optimize-sibling-calls +obj-$(CONFIG_KFENCE_TEST) += kfence-test.o
mm/kfence/kfence-test.c new file +777 -0
777 lines not on this page · raw message
Signature · 1 line
2.28.0.526.ge36021eeef-goog
kernel test robot
build botRe: [PATCH RFC 10/10] kfence: add test suite
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Marco,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on hnaz-linux-mm/master]
[also build test WARNING on arm64/for-next/core linus/master v5.9-rc4] [cannot apply to tip/x86/core next-20200903]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Marco-Elver/KFENCE-A-low-overhead-sampling-based-memory-safety-error-detector/20200907-214229 base: https://github.com/hnaz/linux-mm master config: i386-allyesconfig (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # save the attached .config to linux build tree make W=1 ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
mm/kfence/kfence-test.c: In function 'report_matches':
>> mm/kfence/kfence-test.c:150:1: warning: the frame size of 1032 bytes is larger than 1024 bytes [-Wframe-larger-than=]
150 | }
| ^# https://github.com/0day-ci/linux/commit/d81cf236ae33a44a679b99121af4c4bf088858f2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Marco-Elver/KFENCE-A-low-overhead-sampling-based-memory-safety-error-detector/20200907-214229 git checkout d81cf236ae33a44a679b99121af4c4bf088858f2
vim +150 mm/kfence/kfence-test.c
74
75 /* Check observed report matches information in @r. */
76 static bool report_matches(const struct expect_report *r)
77 {
78 bool ret = false;
79 unsigned long flags;
80 typeof(observed.lines) expect;
81 const char *end;
82 char *cur;
83
84 /* Doubled-checked locking. */
85 if (!report_available())
86 return false;
87
88 /* Generate expected report contents. */
89
90 /* Title */
91 cur = expect[0];
92 end = &expect[0][sizeof(expect[0]) - 1];
93 switch (r->type) {
94 case KFENCE_ERROR_OOB:
95 cur += scnprintf(cur, end - cur, "BUG: KFENCE: out-of-bounds");
96 break;
97 case KFENCE_ERROR_UAF:
98 cur += scnprintf(cur, end - cur, "BUG: KFENCE: use-after-free");
99 break;
100 case KFENCE_ERROR_CORRUPTION:
101 cur += scnprintf(cur, end - cur, "BUG: KFENCE: memory corruption");
102 break;
103 case KFENCE_ERROR_INVALID:
104 cur += scnprintf(cur, end - cur, "BUG: KFENCE: invalid access");
105 break;
106 case KFENCE_ERROR_INVALID_FREE:
107 cur += scnprintf(cur, end - cur, "BUG: KFENCE: invalid free");
108 break;
109 }
110
111 scnprintf(cur, end - cur, " in %pS", r->fn);
112 /* The exact offset won't match, remove it; also strip module name. */
113 cur = strchr(expect[0], '+');
114 if (cur)
115 *cur = '\0';
116
117 /* Access information */
118 cur = expect[1];
119 end = &expect[1][sizeof(expect[1]) - 1];
120
121 switch (r->type) {
122 case KFENCE_ERROR_OOB:
123 cur += scnprintf(cur, end - cur, "Out-of-bounds access at");
124 break;
125 case KFENCE_ERROR_UAF:
126 cur += scnprintf(cur, end - cur, "Use-after-free access at");
127 break;
128 case KFENCE_ERROR_CORRUPTION:
129 cur += scnprintf(cur, end - cur, "Detected corrupted memory at");
130 break;
131 case KFENCE_ERROR_INVALID:
132 cur += scnprintf(cur, end - cur, "Invalid access at");
133 break;
134 case KFENCE_ERROR_INVALID_FREE:
135 cur += scnprintf(cur, end - cur, "Invalid free of");
136 break;
137 }
138
139 cur += scnprintf(cur, end - cur, " 0x" PTR_FMT, (void *)r->addr);
140
141 spin_lock_irqsave(&observed.lock, flags);
142 if (!report_available())
143 goto out; /* A new report is being captured. */
144
145 /* Finally match expected output to what we actually observed. */
146 ret = strstr(observed.lines[0], expect[0]) && strstr(observed.lines[1], expect[1]);
147 out:
148 spin_unlock_irqrestore(&observed.lock, flags);
149 return ret;
> 150 }151
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 6 lines · Marco Elver
This adds the Kernel Electric-Fence (KFENCE) infrastructure. KFENCE is a low-overhead sampling-based memory safety error detector of heap use-after-free, invalid-free, and out-of-bounds access errors. This series enables KFENCE for the x86 and arm64 architectures, and adds KFENCE hooks to the SLAB and SLUB allocators.
Quoted 7 lines
KFENCE is designed to be enabled in production kernels, and has near zero performance overhead. Compared to KASAN, KFENCE trades performance for precision. The main motivation behind KFENCE's design, is that with enough total uptime KFENCE will detect bugs in code paths not typically exercised by non-production test workloads. One way to quickly achieve a large enough total uptime is when the tool is deployed across a large fleet of machines.
Looks nice!
Quoted 13 lines
KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error.
Guarded allocations are set up based on a sample interval (can be set via kfence.sample_interval). After expiration of the sample interval, a guarded allocation from the KFENCE object pool is returned to the main allocator (SLAB or SLUB). At this point, the timer is reset, and the next allocation is set up after the expiration of the interval.
Quoted 4 lines
To enable/disable a KFENCE allocation through the main allocator's fast-path without overhead, KFENCE relies on static branches via the static keys infrastructure. The static branch is toggled to redirect the allocation to KFENCE.
Toggling a static branch is AFAIK quite disruptive (PeterZ will probably tell you better), and with the default 100ms sample interval, I'd think it's not good to toggle it so often? Did you measure what performance would you get, if the static key was only for long-term toggling the whole feature on and off (boot time or even runtime), but the decisions "am I in a sample interval right now?" would be normal tests behind this static key? Thanks.
Quoted 67 lines
We have verified by running synthetic benchmarks (sysbench I/O, hackbench) that a kernel with KFENCE is performance-neutral compared to a non-KFENCE baseline kernel.
KFENCE is inspired by GWP-ASan [1], a userspace tool with similar properties. The name "KFENCE" is a homage to the Electric Fence Malloc Debugger [2].
For more details, see
Documentation/dev-tools/kfence.rstadded in the series -- also viewable here:https://raw.githubusercontent.com/google/kasan/kfence/Documentation/dev-tools/kfence.rst
[1] http://llvm.org/docs/GwpAsan.html
[2] https://linux.die.net/man/3/efenceAlexander Potapenko (6):
mm: add Kernel Electric-Fence infrastructure x86, kfence: enable KFENCE for x86
mm, kfence: insert KFENCE hooks for SLAB
mm, kfence: insert KFENCE hooks for SLUB kfence, kasan: make KFENCE compatible with KASAN kfence, kmemleak: make KFENCE compatible with KMEMLEAKMarco Elver (4):
arm64, kfence: enable KFENCE for ARM64 kfence, lockdep: make KFENCE compatible with lockdep kfence, Documentation: add KFENCE documentation
kfence: add test suiteDocumentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++ MAINTAINERS | 11 + arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kfence.h | 39 ++ arch/arm64/mm/fault.c | 4 + arch/x86/Kconfig | 2 + arch/x86/include/asm/kfence.h | 60 +++ arch/x86/mm/fault.c | 4 + include/linux/kfence.h | 174 +++++++ init/main.c | 2 + kernel/locking/lockdep.c | 8 + lib/Kconfig.debug | 1 + lib/Kconfig.kfence | 70 +++ mm/Makefile | 1 + mm/kasan/common.c | 7 + mm/kfence/Makefile | 6 + mm/kfence/core.c | 730 +++++++++++++++++++++++++++ mm/kfence/kfence-test.c | 777 +++++++++++++++++++++++++++++ mm/kfence/kfence.h | 104 ++++ mm/kfence/report.c | 201 ++++++++ mm/kmemleak.c | 11 + mm/slab.c | 46 +- mm/slab_common.c | 6 +- mm/slub.c | 72 ++- 25 files changed, 2591 insertions(+), 32 deletions(-) create mode 100644 Documentation/dev-tools/kfence.rst create mode 100644 arch/arm64/include/asm/kfence.h create mode 100644 arch/x86/include/asm/kfence.h create mode 100644 include/linux/kfence.h create mode 100644 lib/Kconfig.kfence create mode 100644 mm/kfence/Makefile create mode 100644 mm/kfence/core.c create mode 100644 mm/kfence/kfence-test.c create mode 100644 mm/kfence/kfence.h create mode 100644 mm/kfence/report.c
Alexander Potapenko
maintainerQuoted 6 lines
Toggling a static branch is AFAIK quite disruptive (PeterZ will probably tell you better), and with the default 100ms sample interval, I'd think it's not good to toggle it so often? Did you measure what performance would you get, if the static key was only for long-term toggling the whole feature on and off (boot time or even runtime), but the decisions "am I in a sample interval right now?" would be normal tests behind this static key? Thanks.
100ms is the default that we use for testing, but for production it should be fine to pick a longer interval (e.g. 1 second or more). We haven't noticed any performance impact with neither 100ms nor bigger values.
Regarding using normal branches, they are quite expensive. E.g. at some point we used to have a branch in slab_free() to check whether the freed object belonged to KFENCE pool.
When the pool address was taken from memory, this resulted in some non-zero performance penalty.
As for enabling the whole feature at runtime, our intention is to let the users have it enabled by default, otherwise someone will need to tell every machine in the fleet when the feature is to be enabled.
Quoted 69 lines
Quoted 68 lines
We have verified by running synthetic benchmarks (sysbench I/O, hackbench) that a kernel with KFENCE is performance-neutral compared to a non-KFENCE baseline kernel.
KFENCE is inspired by GWP-ASan [1], a userspace tool with similar properties. The name "KFENCE" is a homage to the Electric Fence Malloc Debugger [2].
For more details, see
Documentation/dev-tools/kfence.rstadded in the series -- also viewable here:https://raw.githubusercontent.com/google/kasan/kfence/Documentation/dev-tools/kfence.rst
[1] http://llvm.org/docs/GwpAsan.html
[2] https://linux.die.net/man/3/efenceAlexander Potapenko (6):
mm: add Kernel Electric-Fence infrastructure x86, kfence: enable KFENCE for x86
mm, kfence: insert KFENCE hooks for SLAB
mm, kfence: insert KFENCE hooks for SLUB kfence, kasan: make KFENCE compatible with KASAN kfence, kmemleak: make KFENCE compatible with KMEMLEAKMarco Elver (4):
arm64, kfence: enable KFENCE for ARM64 kfence, lockdep: make KFENCE compatible with lockdep kfence, Documentation: add KFENCE documentation
kfence: add test suiteDocumentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++ MAINTAINERS | 11 + arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kfence.h | 39 ++ arch/arm64/mm/fault.c | 4 + arch/x86/Kconfig | 2 + arch/x86/include/asm/kfence.h | 60 +++ arch/x86/mm/fault.c | 4 + include/linux/kfence.h | 174 +++++++ init/main.c | 2 + kernel/locking/lockdep.c | 8 + lib/Kconfig.debug | 1 + lib/Kconfig.kfence | 70 +++ mm/Makefile | 1 + mm/kasan/common.c | 7 + mm/kfence/Makefile | 6 + mm/kfence/core.c | 730 +++++++++++++++++++++++++++ mm/kfence/kfence-test.c | 777 +++++++++++++++++++++++++++++ mm/kfence/kfence.h | 104 ++++ mm/kfence/report.c | 201 ++++++++ mm/kmemleak.c | 11 + mm/slab.c | 46 +- mm/slab_common.c | 6 +- mm/slub.c | 72 ++- 25 files changed, 2591 insertions(+), 32 deletions(-) create mode 100644 Documentation/dev-tools/kfence.rst create mode 100644 arch/arm64/include/asm/kfence.h create mode 100644 arch/x86/include/asm/kfence.h create mode 100644 include/linux/kfence.h create mode 100644 lib/Kconfig.kfence create mode 100644 mm/kfence/Makefile create mode 100644 mm/kfence/core.c create mode 100644 mm/kfence/kfence-test.c create mode 100644 mm/kfence/kfence.h create mode 100644 mm/kfence/report.c
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Quoted 10 lines · Alexander Potapenko
Quoted 6 lines
Toggling a static branch is AFAIK quite disruptive (PeterZ will probably tell you better), and with the default 100ms sample interval, I'd think it's not good to toggle it so often? Did you measure what performance would you get, if the static key was only for long-term toggling the whole feature on and off (boot time or even runtime), but the decisions "am I in a sample interval right now?" would be normal tests behind this static key? Thanks.
100ms is the default that we use for testing, but for production it should be fine to pick a longer interval (e.g. 1 second or more). We haven't noticed any performance impact with neither 100ms nor bigger values.
Hmm, I see.
Quoted 5 lines
Regarding using normal branches, they are quite expensive. E.g. at some point we used to have a branch in
slab_free()to check whether the freed object belonged to KFENCE pool.
When the pool address was taken from memory, this resulted in some non-zero performance penalty.
Well yeah, if the checks involve extra cache misses, that adds up. But AFAICS you can't avoid that kind of checks with static key anyway (am I looking right at is_kfence_address()?) because some kfence-allocated objects will exist even after the sampling period ended, right?
So AFAICS kfence_alloc() is the only user of the static key and I wonder if it really makes such difference there.
Quoted 3 lines
As for enabling the whole feature at runtime, our intention is to let the users have it enabled by default, otherwise someone will need to tell every machine in the fleet when the feature is to be enabled.
Sure, but I guess there are tools that make it no difference in effort between 1 machine and fleet.
I'll try to explain my general purpose distro-kernel POV. What I like e.g. about debug_pagealloc and page_owner (and contributed to that state of these features) is that a distro kernel can be shipped with them compiled in, but they are static-key disabled thus have no overhead, until a user enables them on boot, without a need to replace the kernel with a debug one first. Users can enable them for their own debugging, or when asked by somebody from the distro assisting with the debugging.
I think KFENCE has similar potential and could work the same way - compiled in always, but a static key would eliminate everything, even the is_kfence_address() checks, until it became enabled (but then it would probably be a one-way street for the rest of the kernel's uptime). Some distro users would decide to enable it always, some not, but could be advised to when needed. So the existing static key could be repurposed for this, or if it's really worth having the current one to control just the sampling period, then there would be two?
Thanks.
Quoted 70 lines
Quoted 68 lines
Quoted 67 lines
We have verified by running synthetic benchmarks (sysbench I/O, hackbench) that a kernel with KFENCE is performance-neutral compared to a non-KFENCE baseline kernel.
KFENCE is inspired by GWP-ASan [1], a userspace tool with similar properties. The name "KFENCE" is a homage to the Electric Fence Malloc Debugger [2].
For more details, see
Documentation/dev-tools/kfence.rstadded in the series -- also viewable here:https://raw.githubusercontent.com/google/kasan/kfence/Documentation/dev-tools/kfence.rst
[1] http://llvm.org/docs/GwpAsan.html
[2] https://linux.die.net/man/3/efenceAlexander Potapenko (6):
mm: add Kernel Electric-Fence infrastructure x86, kfence: enable KFENCE for x86
mm, kfence: insert KFENCE hooks for SLAB
mm, kfence: insert KFENCE hooks for SLUB kfence, kasan: make KFENCE compatible with KASAN kfence, kmemleak: make KFENCE compatible with KMEMLEAKMarco Elver (4):
arm64, kfence: enable KFENCE for ARM64 kfence, lockdep: make KFENCE compatible with lockdep kfence, Documentation: add KFENCE documentation
kfence: add test suiteDocumentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++ MAINTAINERS | 11 + arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kfence.h | 39 ++ arch/arm64/mm/fault.c | 4 + arch/x86/Kconfig | 2 + arch/x86/include/asm/kfence.h | 60 +++ arch/x86/mm/fault.c | 4 + include/linux/kfence.h | 174 +++++++ init/main.c | 2 + kernel/locking/lockdep.c | 8 + lib/Kconfig.debug | 1 + lib/Kconfig.kfence | 70 +++ mm/Makefile | 1 + mm/kasan/common.c | 7 + mm/kfence/Makefile | 6 + mm/kfence/core.c | 730 +++++++++++++++++++++++++++ mm/kfence/kfence-test.c | 777 +++++++++++++++++++++++++++++ mm/kfence/kfence.h | 104 ++++ mm/kfence/report.c | 201 ++++++++ mm/kmemleak.c | 11 + mm/slab.c | 46 +- mm/slab_common.c | 6 +- mm/slub.c | 72 ++- 25 files changed, 2591 insertions(+), 32 deletions(-) create mode 100644 Documentation/dev-tools/kfence.rst create mode 100644 arch/arm64/include/asm/kfence.h create mode 100644 arch/x86/include/asm/kfence.h create mode 100644 include/linux/kfence.h create mode 100644 lib/Kconfig.kfence create mode 100644 mm/kfence/Makefile create mode 100644 mm/kfence/core.c create mode 100644 mm/kfence/kfence-test.c create mode 100644 mm/kfence/kfence.h create mode 100644 mm/kfence/report.c
Marco Elver
maintainerQuoted 8 lines · Vlastimil Babka
Quoted 7 lines · Alexander Potapenko
Quoted 6 lines
Toggling a static branch is AFAIK quite disruptive (PeterZ will probably tell you better), and with the default 100ms sample interval, I'd think it's not good to toggle it so often? Did you measure what performance would you get, if the static key was only for long-term toggling the whole feature on and off (boot time or even runtime), but the decisions "am I in a sample interval right now?" would be normal tests behind this static key? Thanks.
Quoted 5 lines
Quoted 3 lines
100ms is the default that we use for testing, but for production it should be fine to pick a longer interval (e.g. 1 second or more). We haven't noticed any performance impact with neither 100ms nor bigger values.
Hmm, I see.
To add to this, we initially also weren't sure what the results would be toggling the static branches at varying intervals. In the end we were pleasantly surprised, and our benchmarking results always proved there is no noticeable slowdown above 100ms (somewhat noticeable in the range of 1-10ms but it's tolerable if you wanted to go there).
I think we were initially, just like you might be, deceived about the time scales here. 100ms is a really long time for a computer.
Quoted 6 lines
Quoted 5 lines
Regarding using normal branches, they are quite expensive. E.g. at some point we used to have a branch in
slab_free()to check whether the freed object belonged to KFENCE pool.
When the pool address was taken from memory, this resulted in some non-zero performance penalty.
Quoted 6 lines
Well yeah, if the checks involve extra cache misses, that adds up. But AFAICS you can't avoid that kind of checks with static key anyway (am I looking right at
is_kfence_address()?) because some kfence-allocated objects will exist even after the sampling period ended, right?
So AFAICSkfence_alloc()is the only user of the static key and I wonder if it really makes such difference there.
The really important bit here is to differentiate between fast-paths and slow-paths!
We insert kfence_alloc() into the allocator fast-paths, which is where the majority of cost would be. On the other hand, the major user of is_kfence_address(), kfence_free(), is only inserted into the slow-path.
As a result, is_kfence_address() usage has negligible cost (esp. if the statically allocated pool is used) -- we benchmarked this quite extensively.
Quoted 15 lines
Quoted 3 lines
As for enabling the whole feature at runtime, our intention is to let the users have it enabled by default, otherwise someone will need to tell every machine in the fleet when the feature is to be enabled.
Sure, but I guess there are tools that make it no difference in effort between 1 machine and fleet.
I'll try to explain my general purpose distro-kernel POV. What I like e.g. about
debug_pageallocandpage_owner(and contributed to that state of these features) is that a distro kernel can be shipped with them compiled in, but they are static-key disabled thus have no overhead, until a user enables them on boot, without a need to replace the kernel with a debug one first. Users can enable them for their own debugging, or when asked by somebody from the distro assisting with the debugging.
Quoted 3 lines
I think KFENCE has similar potential and could work the same way - compiled in always, but a static key would eliminate everything, even the
is_kfence_address()checks,
[ See my answer for the cost of is_kfence_address() above. In short,
until we add is_kfence_address() to fast-paths, introducing yet another static branch would be premature optimization. ]
Quoted 5 lines
until it became enabled (but then it would probably
be a one-way street for the rest of the kernel's uptime). Some distro users would decide to enable it always, some not, but could be advised to when needed. So the existing static key could be repurposed for this, or if it's really worth having the current one to control just the sampling period, then there would be two?
You can already do this. Just set CONFIG_KFENCE_SAMPLE_INTERVAL=0. When you decide to enable it, set kfence.sample_interval=<somenumber> as a boot parameter.
I'll add something to that effect into Documentation/dev-tools/kfence.rst.
Thanks,
-- Marco
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 3 lines · Marco Elver
KFENCE is designed to be enabled in production kernels, and has near zero performance overhead. Compared to KASAN, KFENCE trades performance for precision.
Could you talk a little bit about where you expect folks to continue to use KASAN? How would a developer or a tester choose which one to use?
Quoted 6 lines
KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error.
How much memory overhead does this end up having? I know it depends on the object size and so forth. But, could you give some real-world examples of memory consumption? Also, what's the worst case? Say I have a ton of worst-case-sized (32b) slab objects. Will I notice?
Marco Elver
maintainerQuoted 7 lines · Dave Hansen
Quoted 3 lines · Marco Elver
KFENCE is designed to be enabled in production kernels, and has near zero performance overhead. Compared to KASAN, KFENCE trades performance for precision.
Could you talk a little bit about where you expect folks to continue to use KASAN? How would a developer or a tester choose which one to use?
We mention some of this in Documentation/dev-tools/kfence.rst:
In the kernel, several tools exist to debug memory access errors, and in particular KASAN can detect all bug classes that KFENCE can detect. While KASAN is more precise, relying on compiler instrumentation, this comes at a performance cost. We want to highlight that KASAN and KFENCE are complementary, with different target environments. For instance, KASAN is the better debugging-aid, where a simple reproducer exists: due to the lower chance to detect the error, it would require more effort using KFENCE to debug. Deployments at scale, however, would benefit from using KFENCE to discover bugs due to code paths not exercised by test cases or fuzzers.
If you can afford to use KASAN, continue using KASAN. Usually this only applies to test environments. If you have kernels for production use, and cannot enable KASAN for the obvious cost reasons, you could consider KFENCE.
I'll try to make this clearer, maybe summarizing what I said here in Documentation as well.
Quoted 11 lines
Quoted 6 lines
KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error.
How much memory overhead does this end up having? I know it depends on the object size and so forth. But, could you give some real-world examples of memory consumption? Also, what's the worst case? Say I have a ton of worst-case-sized (32b) slab objects. Will I notice?
KFENCE objects are limited (default 255). If we exhaust KFENCE's memory pool, no more KFENCE allocations will occur. Documentation/dev-tools/kfence.rst gives a formula to calculate the KFENCE pool size:
The total memory dedicated to the KFENCE memory pool can be computed as::
( #objects + 1 ) * 2 * PAGE_SIZE
Using the default config, and assuming a page size of 4 KiB, results in dedicating 2 MiB to the KFENCE memory pool.
Does that clarify this point? Or anything else that could help clarify this?
Thanks,
-- Marco
Quoted 13 lines · Marco Elver
Quoted 5 lines
How much memory overhead does this end up having? I know it depends on the object size and so forth. But, could you give some real-world examples of memory consumption? Also, what's the worst case? Say I have a ton of worst-case-sized (32b) slab objects. Will I notice?
KFENCE objects are limited (default 255). If we exhaust KFENCE's memory pool, no more KFENCE allocations will occur.
Documentation/dev-tools/kfence.rstgives a formula to calculate the KFENCE pool size:The total memory dedicated to the KFENCE memory pool can be computed as::
Quoted 7 lines
( #objects + 1 ) * 2 *
PAGE_SIZEUsing the default config, and assuming a page size of 4 KiB, results in dedicating 2 MiB to the KFENCE memory pool.
Does that clarify this point? Or anything else that could help clarify this?
Hmm did you observe that with this limit, a long-running system would eventually converge to KFENCE memory pool being filled with long-aged objects, so there would be no space to sample new ones?
Quoted 3 lines
Thanks,
-- Marco
Marco Elver
maintainerQuoted 19 lines · Vlastimil Babka
Quoted 18 lines · Marco Elver
Quoted 5 lines
How much memory overhead does this end up having? I know it depends on the object size and so forth. But, could you give some real-world examples of memory consumption? Also, what's the worst case? Say I have a ton of worst-case-sized (32b) slab objects. Will I notice?
KFENCE objects are limited (default 255). If we exhaust KFENCE's memory pool, no more KFENCE allocations will occur.
Documentation/dev-tools/kfence.rstgives a formula to calculate the KFENCE pool size:The total memory dedicated to the KFENCE memory pool can be computed as::
( #objects + 1 ) * 2 *
PAGE_SIZEUsing the default config, and assuming a page size of 4 KiB, results in dedicating 2 MiB to the KFENCE memory pool.
Quoted 6 lines
Quoted 2 lines
Does that clarify this point? Or anything else that could help clarify this?
Hmm did you observe that with this limit, a long-running system would eventually converge to KFENCE memory pool being filled with long-aged objects, so there would be no space to sample new ones?
Sure, that's a possibility. But remember that we're not trying to deterministically detect bugs on 1 system (if you wanted that, you should use KASAN), but a fleet of machines! The non-determinism of which allocations will end up in KFENCE, will ensure we won't end up with a fleet of machines of identical allocations. That's exactly what we're after. Even if we eventually exhaust the pool, you'll still detect bugs if there are any.
If you are overly worried, either the sample interval or number of available objects needs to be tweaked to be larger. The default of 255 is quite conservative, and even using something larger on a modern system is hardly noticeable. Choosing a sample interval & number of objects should also factor in how many machines you plan to deploy this on. Monitoring /sys/kernel/debug/kfence/stats can help you here.
Thanks,
-- Marco
Dmitry Vyukov
reviewerQuoted 36 lines · Marco Elver
Quoted 19 lines · Vlastimil Babka
Quoted 18 lines · Marco Elver
Quoted 5 lines
How much memory overhead does this end up having? I know it depends on the object size and so forth. But, could you give some real-world examples of memory consumption? Also, what's the worst case? Say I have a ton of worst-case-sized (32b) slab objects. Will I notice?
KFENCE objects are limited (default 255). If we exhaust KFENCE's memory pool, no more KFENCE allocations will occur.
Documentation/dev-tools/kfence.rstgives a formula to calculate the KFENCE pool size:The total memory dedicated to the KFENCE memory pool can be computed as::
( #objects + 1 ) * 2 *
PAGE_SIZEUsing the default config, and assuming a page size of 4 KiB, results in dedicating 2 MiB to the KFENCE memory pool.
Quoted 6 lines
Quoted 2 lines
Does that clarify this point? Or anything else that could help clarify this?
Hmm did you observe that with this limit, a long-running system would eventually converge to KFENCE memory pool being filled with long-aged objects, so there would be no space to sample new ones?
Sure, that's a possibility. But remember that we're not trying to deterministically detect bugs on 1 system (if you wanted that, you should use KASAN), but a fleet of machines! The non-determinism of which allocations will end up in KFENCE, will ensure we won't end up with a fleet of machines of identical allocations. That's exactly what we're after. Even if we eventually exhaust the pool, you'll still detect bugs if there are any.
Quoted 6 lines
If you are overly worried, either the sample interval or number of available objects needs to be tweaked to be larger. The default of 255 is quite conservative, and even using something larger on a modern system is hardly noticeable. Choosing a sample interval & number of objects should also factor in how many machines you plan to deploy this on. Monitoring
/sys/kernel/debug/kfence/statscan help you here.
Hi Marco,
I reviewed patches and they look good to me (minus some local comments that I've left).
The main question/concern I have is what Vlastimil mentioned re long-aged objects.
Is the default sample interval values reasonable for typical workloads? Do we have any guidelines on choosing the sample interval? Should it depend on workload/use pattern?
By "reasonable" I mean if the pool will last long enough to still sample something after hours/days? Have you tried any experiments with some workload (both short-lived processes and long-lived processes/namespaces) capturing state of the pool? It can make sense to do to better understand dynamics. I suspect that the rate may need to be orders of magnitude lower.
Also I am wondering about the boot process (both kernel and init). It's both inherently almost the same for the whole population of machines and inherently produces persistent objects. Should we lower the rate for the first minute of uptime? Or maybe make it proportional to uptime?
I feel it's quite an important aspect. We can have this awesome idea and implementation, but radically lower its utility by using bad sampling value (which will have silent "failure mode" -- no bugs detected).
But to make it clear: all of this does not conflict with the merge of the first version. Just having tunable sampling interval is good enough. We will get the ultimate understanding only when we start using it widely anyway.
Marco Elver
maintainerQuoted 2 lines · Dmitry Vyukov
Quoted 1 line · Marco Elver
On Tue, Sep 08, 2020 at 05:36PM +0200, Vlastimil Babka wrote:
[...]
Quoted 19 lines
Quoted 12 lines
Quoted 3 lines
Hmm did you observe that with this limit, a long-running system would eventually converge to KFENCE memory pool being filled with long-aged objects, so there would be no space to sample new ones?
Sure, that's a possibility. But remember that we're not trying to deterministically detect bugs on 1 system (if you wanted that, you should use KASAN), but a fleet of machines! The non-determinism of which allocations will end up in KFENCE, will ensure we won't end up with a fleet of machines of identical allocations. That's exactly what we're after. Even if we eventually exhaust the pool, you'll still detect bugs if there are any.
Quoted 6 lines
If you are overly worried, either the sample interval or number of available objects needs to be tweaked to be larger. The default of 255 is quite conservative, and even using something larger on a modern system is hardly noticeable. Choosing a sample interval & number of objects should also factor in how many machines you plan to deploy this on. Monitoring
/sys/kernel/debug/kfence/statscan help you here.
Quoted 4 lines
Hi Marco,
I reviewed patches and they look good to me (minus some local comments that I've left).
Thank you.
Quoted 5 lines
The main question/concern I have is what Vlastimil mentioned re long-aged objects.
Is the default sample interval values reasonable for typical workloads? Do we have any guidelines on choosing the sample interval? Should it depend on workload/use pattern?
As I hinted at before, the sample interval & number of objects needs to depend on:
- number of machines,
- workload,
- acceptable overhead (performance, memory).
However, workload can vary greatly, and something more dynamic may be needed. We do have the option to monitor /sys/kernel/debug/kfence/stats and even change the sample interval at runtime, e.g. from a user space tool that checks the currently used objects, and as the pool is closer to exhausted, starts increasing /sys/module/kfence/parameters/sample_interval.
Of course, if we figure out the best dynamic policy, we can add this policy into the kernel. But I don't think it makes sense to hard-code such a policy right now.
Quoted 6 lines
By "reasonable" I mean if the pool will last long enough to still sample something after hours/days? Have you tried any experiments with some workload (both short-lived processes and long-lived processes/namespaces) capturing state of the pool? It can make sense to do to better understand dynamics. I suspect that the rate may need to be orders of magnitude lower.
Yes, the current default sample interval is a lower bound, and is also a reasonable default for testing. I expect real deployments to use much higher sample intervals (lower rate).
So here's some data (with CONFIG_KFENCE_NUM_OBJECTS=1000, so that allocated KFENCE objects isn't artificially capped):
-- With a mostly vanilla config + KFENCE (sample interval 100 ms), after ~40 min uptime (only boot, then idle) I see ~60 KFENCE objects (total allocations >600). Those aren't always the same objects, with roughly ~2 allocations/frees per second.
-- Then running sysbench I/O benchmark, KFENCE objects allocated peak at 82. During the benchmark, allocations/frees per second are closer to 10-15. After the benchmark, the KFENCE objects allocated remain at 82, and allocations/frees per second fall back to ~2.
-- For the same system, changing the sample interval to 1 ms (echo 1 > /sys/module/kfence/parameters/sample_interval), and re-running the benchmark gives me: KFENCE objects allocated peak at exactly 500, with ~500 allocations/frees per second. After that, allocated KFENCE objects dropped a little to 496, and allocations/frees per second fell back to ~2.
-- The long-lived objects are due to caches, and just running 'echo 1
Quoted 1 line
/proc/sys/vm/drop_caches' reduced allocated KFENCE objects back to
45.
Quoted 5 lines
Also I am wondering about the boot process (both kernel and init). It's both inherently almost the same for the whole population of machines and inherently produces persistent objects. Should we lower the rate for the first minute of uptime? Or maybe make it proportional to uptime?
It should depend on current usage, which is dependent on the workload. I don't think uptime helps much, as seen above. If we imagine a user space tool that tweaks this for us, we can initialize KFENCE with a very large sample interval, and once booted, this user space tool/script adjusts /sys/module/kfence/parameters/sample_interval.
At the very least, I think I'll just make /sys/module/kfence/parameters/sample_interval root-writable unconditionally, so that we can experiment with such a tool.
Lowering the rate for the first minute of uptime might also be an option, although if we do that, we can also just move kfence_init() to the end of start_kernel(). IMHO, I think it still makes sense to sample normally during boot, because who knows how those allocations are used with different workloads once the kernel is live. With a sample interval of 1000 ms (which is closer to what we probably want in production), I see no more than 20 KFENCE objects allocated after boot. I think we can live with that.
Quoted 4 lines
I feel it's quite an important aspect. We can have this awesome idea and implementation, but radically lower its utility by using bad sampling value (which will have silent "failure mode" -- no bugs detected).
As a first step, I think monitoring the entire fleet here is key here (collect /sys/kernel/debug/kfence/stats). Essentially, as long as allocations/frees per second remains >0, we're probably fine, even if we always run at max. KFENCE objects allocated.
An improvement over allocations/frees per second >0 would be dynamically tweaking sample_interval based on how close we get to max KFENCE objects allocated.
Yet another option is to skip KFENCE allocations based on the memcache name, e.g. for those caches dedicated to long-lived allocations.
Quoted 4 lines
But to make it clear: all of this does not conflict with the merge of the first version. Just having tunable sampling interval is good enough. We will get the ultimate understanding only when we start using it widely anyway.
Thanks,
-- Marco
Dmitry Vyukov
reviewerQuoted 85 lines · Marco Elver
Quoted 2 lines · Dmitry Vyukov
Quoted 1 line · Marco Elver
On Tue, Sep 08, 2020 at 05:36PM +0200, Vlastimil Babka wrote:
[...]
Quoted 19 lines
Quoted 12 lines
Quoted 3 lines
Hmm did you observe that with this limit, a long-running system would eventually converge to KFENCE memory pool being filled with long-aged objects, so there would be no space to sample new ones?
Sure, that's a possibility. But remember that we're not trying to deterministically detect bugs on 1 system (if you wanted that, you should use KASAN), but a fleet of machines! The non-determinism of which allocations will end up in KFENCE, will ensure we won't end up with a fleet of machines of identical allocations. That's exactly what we're after. Even if we eventually exhaust the pool, you'll still detect bugs if there are any.
Quoted 6 lines
If you are overly worried, either the sample interval or number of available objects needs to be tweaked to be larger. The default of 255 is quite conservative, and even using something larger on a modern system is hardly noticeable. Choosing a sample interval & number of objects should also factor in how many machines you plan to deploy this on. Monitoring
/sys/kernel/debug/kfence/statscan help you here.Quoted 4 lines
Hi Marco,
I reviewed patches and they look good to me (minus some local comments that I've left).
Thank you.
Quoted 5 lines
The main question/concern I have is what Vlastimil mentioned re long-aged objects.
Is the default sample interval values reasonable for typical workloads? Do we have any guidelines on choosing the sample interval? Should it depend on workload/use pattern?As I hinted at before, the sample interval & number of objects needs to depend on:
- number of machines,
- workload,
- acceptable overhead (performance, memory).However, workload can vary greatly, and something more dynamic may be needed. We do have the option to monitor
/sys/kernel/debug/kfence/statsand even change the sample interval at runtime, e.g. from a user space tool that checks the currently used objects, and as the pool is closer to exhausted, starts increasing/sys/module/kfence/parameters/sample_interval.Of course, if we figure out the best dynamic policy, we can add this policy into the kernel. But I don't think it makes sense to hard-code such a policy right now.
Quoted 6 lines
By "reasonable" I mean if the pool will last long enough to still sample something after hours/days? Have you tried any experiments with some workload (both short-lived processes and long-lived processes/namespaces) capturing state of the pool? It can make sense to do to better understand dynamics. I suspect that the rate may need to be orders of magnitude lower.
Yes, the current default sample interval is a lower bound, and is also a reasonable default for testing. I expect real deployments to use much higher sample intervals (lower rate).
So here's some data (with
CONFIG_KFENCE_NUM_OBJECTS=1000, so that allocated KFENCE objects isn't artificially capped):-- With a mostly vanilla config + KFENCE (sample interval 100 ms), after ~40 min uptime (only boot, then idle) I see ~60 KFENCE objects (total allocations >600). Those aren't always the same objects, with roughly ~2 allocations/frees per second.
-- Then running sysbench I/O benchmark, KFENCE objects allocated peak at 82. During the benchmark, allocations/frees per second are closer to 10-15. After the benchmark, the KFENCE objects allocated remain at 82, and allocations/frees per second fall back to ~2.
-- For the same system, changing the sample interval to 1 ms (echo 1 >
/sys/module/kfence/parameters/sample_interval), and re-running the benchmark gives me: KFENCE objects allocated peak at exactly 500, with ~500 allocations/frees per second. After that, allocated KFENCE objects dropped a little to 496, and allocations/frees per second fell back to ~2.
Quoted 3 lines
-- The long-lived objects are due to caches, and just running 'echo 1
Quoted 1 line
/proc/sys/vm/drop_caches' reduced allocated KFENCE objects back to45.
Interesting. What type of caches is this? If there is some type of cache that caches particularly lots of sampled objects, we could potentially change the cache to release sampled objects eagerly.
Quoted 49 lines
Quoted 5 lines
Also I am wondering about the boot process (both kernel and init). It's both inherently almost the same for the whole population of machines and inherently produces persistent objects. Should we lower the rate for the first minute of uptime? Or maybe make it proportional to uptime?
It should depend on current usage, which is dependent on the workload. I don't think uptime helps much, as seen above. If we imagine a user space tool that tweaks this for us, we can initialize KFENCE with a very large sample interval, and once booted, this user space tool/script adjusts
/sys/module/kfence/parameters/sample_interval.At the very least, I think I'll just make
/sys/module/kfence/parameters/sample_intervalroot-writable unconditionally, so that we can experiment with such a tool.Lowering the rate for the first minute of uptime might also be an option, although if we do that, we can also just move
kfence_init()to the end ofstart_kernel(). IMHO, I think it still makes sense to sample normally during boot, because who knows how those allocations are used with different workloads once the kernel is live. With a sample interval of 1000 ms (which is closer to what we probably want in production), I see no more than 20 KFENCE objects allocated after boot. I think we can live with that.Quoted 4 lines
I feel it's quite an important aspect. We can have this awesome idea and implementation, but radically lower its utility by using bad sampling value (which will have silent "failure mode" -- no bugs detected).
As a first step, I think monitoring the entire fleet here is key here (collect
/sys/kernel/debug/kfence/stats). Essentially, as long as allocations/frees per second remains >0, we're probably fine, even if we always run at max. KFENCE objects allocated.An improvement over allocations/frees per second >0 would be dynamically tweaking
sample_intervalbased on how close we get to max KFENCE objects allocated.Yet another option is to skip KFENCE allocations based on the memcache name, e.g. for those caches dedicated to long-lived allocations.
Quoted 4 lines
But to make it clear: all of this does not conflict with the merge of the first version. Just having tunable sampling interval is good enough. We will get the ultimate understanding only when we start using it widely anyway.
Thanks,
-- Marco
Marco Elver
maintainerQuoted 2 lines · Dmitry Vyukov
Quoted 1 line · Marco Elver
On Fri, 11 Sep 2020 at 09:36, Dmitry Vyukov <[email protected]> wrote:
[...]
Quoted 31 lines
Quoted 31 lines
Quoted 6 lines
By "reasonable" I mean if the pool will last long enough to still sample something after hours/days? Have you tried any experiments with some workload (both short-lived processes and long-lived processes/namespaces) capturing state of the pool? It can make sense to do to better understand dynamics. I suspect that the rate may need to be orders of magnitude lower.
Yes, the current default sample interval is a lower bound, and is also a reasonable default for testing. I expect real deployments to use much higher sample intervals (lower rate).
So here's some data (with
CONFIG_KFENCE_NUM_OBJECTS=1000, so that allocated KFENCE objects isn't artificially capped):-- With a mostly vanilla config + KFENCE (sample interval 100 ms), after ~40 min uptime (only boot, then idle) I see ~60 KFENCE objects (total allocations >600). Those aren't always the same objects, with roughly ~2 allocations/frees per second.
-- Then running sysbench I/O benchmark, KFENCE objects allocated peak at 82. During the benchmark, allocations/frees per second are closer to 10-15. After the benchmark, the KFENCE objects allocated remain at 82, and allocations/frees per second fall back to ~2.
-- For the same system, changing the sample interval to 1 ms (echo 1 >
/sys/module/kfence/parameters/sample_interval), and re-running the benchmark gives me: KFENCE objects allocated peak at exactly 500, with ~500 allocations/frees per second. After that, allocated KFENCE objects dropped a little to 496, and allocations/frees per second fell back to ~2.
Quoted 7 lines
Quoted 3 lines
-- The long-lived objects are due to caches, and just running 'echo 1
Quoted 1 line
/proc/sys/vm/drop_caches' reduced allocated KFENCE objects back to45.
Interesting. What type of caches is this? If there is some type of cache that caches particularly lots of sampled objects, we could potentially change the cache to release sampled objects eagerly.
The 2 major users of KFENCE objects for that workload are 'buffer_head' and 'bio-0'.
If we want to deal with those, I guess there are 2 options:
1. More complex, but more precise: make the users of them check is_kfence_address() and release their buffers earlier.
2. Simpler, generic solution: make KFENCE stop return allocations for non-kmalloc_caches memcaches after more than ~90% of the pool is exhausted. This assumes that creators of long-lived objects usually set up their own memcaches.
I'm currently inclined to go for (2).
Thanks,
-- Marco
Marco Elver
maintainerQuoted 51 lines · Marco Elver
Quoted 2 lines · Dmitry Vyukov
Quoted 1 line · Marco Elver
On Fri, 11 Sep 2020 at 09:36, Dmitry Vyukov <[email protected]> wrote:
[...]
Quoted 31 lines
Quoted 31 lines
Quoted 6 lines
By "reasonable" I mean if the pool will last long enough to still sample something after hours/days? Have you tried any experiments with some workload (both short-lived processes and long-lived processes/namespaces) capturing state of the pool? It can make sense to do to better understand dynamics. I suspect that the rate may need to be orders of magnitude lower.
Yes, the current default sample interval is a lower bound, and is also a reasonable default for testing. I expect real deployments to use much higher sample intervals (lower rate).
So here's some data (with
CONFIG_KFENCE_NUM_OBJECTS=1000, so that allocated KFENCE objects isn't artificially capped):-- With a mostly vanilla config + KFENCE (sample interval 100 ms), after ~40 min uptime (only boot, then idle) I see ~60 KFENCE objects (total allocations >600). Those aren't always the same objects, with roughly ~2 allocations/frees per second.
-- Then running sysbench I/O benchmark, KFENCE objects allocated peak at 82. During the benchmark, allocations/frees per second are closer to 10-15. After the benchmark, the KFENCE objects allocated remain at 82, and allocations/frees per second fall back to ~2.
-- For the same system, changing the sample interval to 1 ms (echo 1 >
/sys/module/kfence/parameters/sample_interval), and re-running the benchmark gives me: KFENCE objects allocated peak at exactly 500, with ~500 allocations/frees per second. After that, allocated KFENCE objects dropped a little to 496, and allocations/frees per second fell back to ~2.Quoted 7 lines
Quoted 3 lines
-- The long-lived objects are due to caches, and just running 'echo 1
Quoted 1 line
/proc/sys/vm/drop_caches' reduced allocated KFENCE objects back to45.
Interesting. What type of caches is this? If there is some type of cache that caches particularly lots of sampled objects, we could potentially change the cache to release sampled objects eagerly.
The 2 major users of KFENCE objects for that workload are '
buffer_head' and 'bio-0'.If we want to deal with those, I guess there are 2 options:
1. More complex, but more precise: make the users of them check
is_kfence_address()and release their buffers earlier.
Quoted 6 lines
2. Simpler, generic solution: make KFENCE stop return allocations for non-kmalloc_caches memcaches after more than ~90% of the pool is exhausted. This assumes that creators of long-lived objects usually set up their own memcaches.
I'm currently inclined to go for (2).
Ok, after some offline chat, we determined that (2) would be premature and we can't really say if kmalloc should have precedence if we reach some usage threshold. So for now, let's just leave as-is and start with the recommendation to monitor and adjust based on usage, fleet size, etc.
Thanks,
-- Marco
On 9/8/20 8:31 AM, Marco Elver wrote:
...
Quoted 4 lines
If you can afford to use KASAN, continue using KASAN. Usually this only applies to test environments. If you have kernels for production use, and cannot enable KASAN for the obvious cost reasons, you could consider KFENCE.
That's a really nice, succinct way to put it. You might even want to consider putting this in the Kconfig help text.
Quoted 19 lines
Quoted 11 lines
Quoted 6 lines
KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error.
How much memory overhead does this end up having? I know it depends on the object size and so forth. But, could you give some real-world examples of memory consumption? Also, what's the worst case? Say I have a ton of worst-case-sized (32b) slab objects. Will I notice?
KFENCE objects are limited (default 255). If we exhaust KFENCE's memory pool, no more KFENCE allocations will occur.
Documentation/dev-tools/kfence.rstgives a formula to calculate the KFENCE pool size:The total memory dedicated to the KFENCE memory pool can be computed as::
Quoted 7 lines
( #objects + 1 ) * 2 *
PAGE_SIZEUsing the default config, and assuming a page size of 4 KiB, results in dedicating 2 MiB to the KFENCE memory pool.
Does that clarify this point? Or anything else that could help clarify this?
That clears it up, thanks!
I would suggest adding a tiny nugget about this in the cover letter, just saying that the worst-case memory consumption on x86 is ~2M.