kfence: add kfence.fault parameter
Subsystems (4)
Marco Elver
maintainerPATCH 1/1 kfence: add kfence.fault parameter
Add kfence.fault parameter to control the behavior when a KFENCE error is detected (similar in spirit to kasan.fault=<mode>).
The supported modes for kfence.fault=<mode> are:
- report: print the error report and continue (default).
- oops: print the error report and oops.
- panic: print the error report and panic.
In particular, the 'oops' mode offers a trade-off between no mitigation on report and panicking outright (if panic_on_oops is not set).
--- .../admin-guide/kernel-parameters.txt | 6 +++ Documentation/dev-tools/kfence.rst | 7 +++ mm/kfence/core.c | 23 ++++++--- mm/kfence/kfence.h | 16 +++++- mm/kfence/report.c | 49 +++++++++++++++++-- 5 files changed, 89 insertions(+), 12 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -2958,6 +2958,12 @@ Kernel parameters Format: <bool> Default: CONFIG_KFENCE_DEFERRABLE + kfence.fault= [MM,KFENCE] Controls the behavior when a KFENCE + error is detected. + report - print the error report and continue (default). + oops - print the error report and oops. + panic - print the error report and panic. + kfence.sample_interval= [MM,KFENCE] KFENCE's sample interval in milliseconds. Format: <unsigned integer>
Documentation/dev-tools/kfence.rst +7 -0
@@ -81,6 +81,13 @@ tables being allocated. Error reports ~~~~~~~~~~~~~ +The boot parameter ``kfence.fault`` can be used to control the behavior when a +KFENCE error is detected: + +- ``kfence.fault=report``: Print the error report and continue (default). +- ``kfence.fault=oops``: Print the error report and oops. +- ``kfence.fault=panic``: Print the error report and panic. + A typical out-of-bounds access looks like this:: ==================================================================
mm/kfence/core.c +16 -7
@@ -50,7 +50,7 @@/* === Data ================================================================= */ -static bool kfence_enabled__read_mostly ; +bool kfence_enabled__read_mostly ;static bool disabled_by_warn__read_mostly ;unsigned long kfence_sample_interval__read_mostly =CONFIG_KFENCE_SAMPLE_INTERVAL ;
@@ -335,6 +335,7 @@ metadata_update_state(struct kfence_metadata *meta, enum kfence_object_state nex static check_canary_attributes bool check_canary_byte(u8 *addr) {struct kfence_metadata *meta; +enum kfence_fault fault;unsigned long flags;if (likely (*addr ==KFENCE_CANARY_PATTERN_U8 (addr)))
@@ -344,8 +345,9 @@ static check_canary_attributes bool check_canary_byte(u8 *addr) meta =addr_to_metadata ((unsigned long )addr);raw_spin_lock_irqsave (&meta->lock, flags); -kfence_report_error ((unsigned long )addr, false,NULL , meta,KFENCE_ERROR_CORRUPTION ); + fault =kfence_report_error ((unsigned long )addr, false,NULL , meta,KFENCE_ERROR_CORRUPTION );raw_spin_unlock_irqrestore (&meta->lock, flags); +kfence_handle_fault (fault);return false; }
@@ -524,11 +526,14 @@ static void kfence_guarded_free(void *addr, struct kfence_metadata *meta, bool zraw_spin_lock_irqsave (&meta->lock, flags);if (!kfence_obj_allocated (meta) || meta->addr != (unsigned long )addr) { +enum kfence_fault fault; +/* Invalid or double-free, bail out. */ atomic_long_inc (&counters[KFENCE_COUNTER_BUGS ]); -kfence_report_error ((unsigned long )addr, false,NULL , meta, -KFENCE_ERROR_INVALID_FREE ); + fault =kfence_report_error ((unsigned long )addr, false,NULL , meta, +KFENCE_ERROR_INVALID_FREE );raw_spin_unlock_irqrestore (&meta->lock, flags); +kfence_handle_fault (fault);return ; }
@@ -830,7 +835,8 @@ static void kfence_check_all_canary(void)static int kfence_check_canary_callback (struct notifier_block *nb,unsigned long reason,void *arg) { -kfence_check_all_canary (); +if (READ_ONCE (kfence_enabled)) +kfence_check_all_canary ();return NOTIFY_OK ; }
@@ -1249,6 +1255,7 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regsstruct kfence_metadata *to_report =NULL ;unsigned long unprotected_page =0 ;enum kfence_error_type error_type; +enum kfence_fault fault;unsigned long flags;if (!is_kfence_address ((void *)addr))
@@ -1307,12 +1314,14 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regsif (to_report) {raw_spin_lock_irqsave (&to_report->lock, flags); to_report->unprotected_page = unprotected_page; -kfence_report_error (addr, is_write, regs, to_report, error_type); + fault =kfence_report_error (addr, is_write, regs, 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, is_write, regs,NULL ,KFENCE_ERROR_INVALID ); + fault =kfence_report_error (addr, is_write, regs,NULL ,KFENCE_ERROR_INVALID ); } +kfence_handle_fault (fault); +return kfence_unprotect (addr);/* Unprotect and let access proceed. */ }
mm/kfence/kfence.h +14 -2
@@ -16,6 +16,8 @@#include "../slab.h" /* for struct kmem_cache */ +extern bool kfence_enabled; +/* * 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
@@ -140,8 +142,18 @@ enum kfence_error_type { KFENCE_ERROR_INVALID_FREE, /* Invalid free. */ }; -void kfence_report_error (unsigned long address,bool is_write,struct pt_regs *regs, -const struct kfence_metadata *meta,enum kfence_error_type type); +enum kfence_fault { +KFENCE_FAULT_NONE , +KFENCE_FAULT_REPORT , +KFENCE_FAULT_OOPS , +KFENCE_FAULT_PANIC , +}; + +enum kfence_fault +kfence_report_error (unsigned long address,bool is_write,struct pt_regs *regs, +const struct kfence_metadata *meta,enum kfence_error_type type); + +void kfence_handle_fault (enum kfence_fault fault); void kfence_print_object(struct seq_file *seq, const struct kfence_metadata *meta) __must_hold(&meta->lock);
mm/kfence/report.c +46 -3
82 lines not on this page · raw message
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
Quoted 8 lines · Marco Elver
mm/kfence/report.c +7
-0+static int __init early_kfence_fault (char *arg) +{ +if (!arg) +return -EINVAL ; + +if (!strcmp (arg,"report" )) + kfence_fault =KFENCE_FAULT_REPORT ;
mm/kfence/report.c line 53
Quoted 10 lines
mm/kfence/report.c +10
-0+else if (!strcmp (arg,"oops" )) + kfence_fault =KFENCE_FAULT_OOPS ; +else if (!strcmp (arg,"panic" )) + kfence_fault =KFENCE_FAULT_PANIC ; +else +return -EINVAL ; + +return 0 ; +} +early_param ("kfence.fault" , early_kfence_fault);
The other parameters in mm/kfence/ seem to be module_param, which make them tunable at run time through /sys/module/kfence/parameters/*
Why is this one different?
And, does this one show up as /sys/module/kfence/parameters/fault?
Having the ability to tweak this behavior at run time, without requiring a system reboot, could be really useful for people unexpectedly triggering kernel panics across a fleet of servers, and deciding they would rather not.
Signature · 1 line
All Rights Reversed.
Marco Elver
maintainerQuoted 25 lines · Rik van Riel
Quoted 8 lines · Marco Elver
mm/kfence/report.c +7
-0+static int __init early_kfence_fault (char *arg) +{ +if (!arg) +return -EINVAL ; + +if (!strcmp (arg,"report" )) + kfence_fault =KFENCE_FAULT_REPORT ;mm/kfence/report.c line 53
Quoted 10 lines
mm/kfence/report.c +10
-0+else if (!strcmp (arg,"oops" )) + kfence_fault =KFENCE_FAULT_OOPS ; +else if (!strcmp (arg,"panic" )) + kfence_fault =KFENCE_FAULT_PANIC ; +else +return -EINVAL ; + +return 0 ; +} +early_param ("kfence.fault" , early_kfence_fault);The other parameters in
mm/kfence/seem to bemodule_param, which make them tunable at run time through/sys/module/kfence/parameters/*
Quoted 1 line
Why is this one different?
That was my first thought too, but after much thought we should not make this changeable after init, see below ...
Quoted 6 lines
And, does this one show up as
/sys/module/kfence/parameters/fault?Having the ability to tweak this behavior at run time, without requiring a system reboot, could be really useful for people unexpectedly triggering kernel panics across a fleet of servers, and deciding they would rather not.
It's intentional - having the ability to switch it after init means we'd have to remove __ro_after_init from the kfence_fault setting. We risk having the system administrator's choice being overridden by accident in the exact situation where we do not want it to happen:
either through memory corruption overwriting that global flag, or it might give an attacker the ability to circumvent the oops/panic setting, if they manage to reset it. KFENCE is not a mitigation, but this setting is meant to give a knob to reduce the risk that someone takes advantage of KFENCE's heap layout - until now, KFENCE only reports and continues - the actual buggy access happily proceeds.
Alexander Potapenko
maintainermm/kfence/core.c static void kfence_check_all_canary(void) · line 839
Quoted 7 lines
mm/kfence/core.c +2
-1@@ -830,7 +835,8 @@ static void kfence_check_all_canary(void)static int kfence_check_canary_callback (struct notifier_block *nb,unsigned long reason,void *arg) { -kfence_check_all_canary (); +if (READ_ONCE (kfence_enabled)) +kfence_check_all_canary ();
By the way, should we also check for kfence_enabled when reporting errors?
Quoted 6 lines
mm/kfence/core.c +1
-1@@ -1307,12 +1314,14 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regsif (to_report) {raw_spin_lock_irqsave (&to_report->lock, flags); to_report->unprotected_page = unprotected_page; -kfence_report_error (addr, is_write, regs, to_report, error_type); + fault =kfence_report_error (addr, is_write, regs, to_report, error_type);
mm/kfence/core.c bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs · line 1326
Quoted 10 lines
mm/kfence/core.c +3
-1raw_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, is_write, regs,NULL ,KFENCE_ERROR_INVALID ); + fault =kfence_report_error (addr, is_write, regs,NULL ,KFENCE_ERROR_INVALID ); } +kfence_handle_fault (fault); +return kfence_unprotect (addr);/* Unprotect and let access proceed. */
If kfence_handle_fault() oopses, kfence_unprotect() will never be called, is that the desired behavior?
mm/kfence/report.c void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *r · line 233
Quoted 4 lines
mm/kfence/report.c +0
-0/* Require non-NULL meta, except if KFENCE_ERROR_INVALID. */ if (WARN_ON(type != KFENCE_ERROR_INVALID && !meta))mm/kfence/report.c +1
-1-return ; +return KFENCE_FAULT_NONE ;
We explicitly don't panic here; guess it should be fine...
Marco Elver
maintainerQuoted 10 lines · Alexander Potapenko
mm/kfence/core.c static void kfence_check_all_canary(void) · line 839
Quoted 8 lines
mm/kfence/core.c +2
-1@@ -830,7 +835,8 @@ static void kfence_check_all_canary(void)static int kfence_check_canary_callback (struct notifier_block *nb,unsigned long reason,void *arg) { -kfence_check_all_canary (); +if (READ_ONCE (kfence_enabled)) +kfence_check_all_canary ();By the way, should we also check for
kfence_enabledwhen reporting errors?
Not sure, I think it might be redundant - I don't see a way we should get to the reporting path if KFENCE is disabled. And if there currently is a way to get there, we should check kfence_enabled before (such as in this panic notifier now).
Quoted 13 lines
Quoted 13 lines
mm/kfence/core.c +2
-2@@ -1307,12 +1314,14 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regsif (to_report) {raw_spin_lock_irqsave (&to_report->lock, flags); to_report->unprotected_page = unprotected_page; -kfence_report_error (addr, is_write, regs, to_report, error_type); + fault =kfence_report_error (addr, is_write, regs, 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, is_write, regs,NULL ,KFENCE_ERROR_INVALID ); + fault =kfence_report_error (addr, is_write, regs,NULL ,KFENCE_ERROR_INVALID ); }
Quoted 6 lines
mm/kfence/core.c bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs · line 1326
Quoted 3 lines
mm/kfence/core.c +2
-0+kfence_handle_fault (fault); +return kfence_unprotect (addr);/* Unprotect and let access proceed. */ If
kfence_handle_fault()oopses,kfence_unprotect()will never be called, is that the desired behavior?
It is - consider multiple kernel threads running into the same OOB or UAF. We should oops them all, otherwise this change is almost no benefit.
Quoted 6 lines
mm/kfence/report.c void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *r · line 233
Quoted 4 lines
mm/kfence/report.c +0
-0/* Require non-NULL meta, except if KFENCE_ERROR_INVALID. */ if (WARN_ON(type != KFENCE_ERROR_INVALID && !meta))mm/kfence/report.c +1
-1-return ; +return KFENCE_FAULT_NONE ;We explicitly don't panic here; guess it should be fine...
Yes - it's a KFENCE bug if we get here, the WARN is fine.
Quoted 42 lines · Marco Elver
Quoted 10 lines · Alexander Potapenko
mm/kfence/core.c static void kfence_check_all_canary(void) · line 839
Quoted 8 lines
mm/kfence/core.c +2
-1@@ -830,7 +835,8 @@ static void kfence_check_all_canary(void)static int kfence_check_canary_callback (struct notifier_block *nb,unsigned long reason,void *arg) { -kfence_check_all_canary (); +if (READ_ONCE (kfence_enabled)) +kfence_check_all_canary ();By the way, should we also check for
kfence_enabledwhen reporting errors?Not sure, I think it might be redundant - I don't see a way we should get to the reporting path if KFENCE is disabled. And if there currently is a way to get there, we should check
kfence_enabledbefore (such as in this panic notifier now).Quoted 13 lines
Quoted 13 lines
mm/kfence/core.c +2
-2@@ -1307,12 +1314,14 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regsif (to_report) {raw_spin_lock_irqsave (&to_report->lock, flags); to_report->unprotected_page = unprotected_page; -kfence_report_error (addr, is_write, regs, to_report, error_type); + fault =kfence_report_error (addr, is_write, regs, 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, is_write, regs,NULL ,KFENCE_ERROR_INVALID ); + fault =kfence_report_error (addr, is_write, regs,NULL ,KFENCE_ERROR_INVALID ); }Quoted 6 lines
mm/kfence/core.c bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs · line 1326
Quoted 3 lines
mm/kfence/core.c +2
-0+kfence_handle_fault (fault); +return kfence_unprotect (addr);/* Unprotect and let access proceed. */ If
kfence_handle_fault()oopses,kfence_unprotect()will never be called, is that the desired behavior?It is - consider multiple kernel threads running into the same OOB or UAF. We should oops them all, otherwise this change is almost no benefit.
Quoted 8 lines
Quoted 6 lines
mm/kfence/report.c void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *r · line 233
Quoted 4 lines
mm/kfence/report.c +0
-0/* Require non-NULL meta, except if KFENCE_ERROR_INVALID. */ if (WARN_ON(type != KFENCE_ERROR_INVALID && !meta))mm/kfence/report.c +1
-1-return ; +return KFENCE_FAULT_NONE ;We explicitly don't panic here; guess it should be fine...
Yes - it's a KFENCE bug if we get here, the WARN is fine.