kfence: Use pt_regs to generate stack trace on faults
Subsystems (6)
Marco Elver
maintainerPATCH 1/1 kfence: Use pt_regs to generate stack trace on faults
Instead of removing the fault handling portion of the stack trace based on the fault handler's name, just use struct pt_regs directly.
Change kfence_handle_page_fault() to take a struct pt_regs, and plumb it through to kfence_report_error() for out-of-bounds, use-after-free, or invalid access errors, where pt_regs is used to generate the stack trace.
If the kernel is a DEBUG_KERNEL, also show registers for more information.
--- arch/arm64/include/asm/kfence.h | 2 -- arch/arm64/mm/fault.c | 2 +- arch/x86/include/asm/kfence.h | 6 ---- arch/x86/mm/fault.c | 2 +- include/linux/kfence.h | 5 +-- mm/kfence/core.c | 10 +++--- mm/kfence/kfence.h | 4 +-- mm/kfence/report.c | 63 +++++++++++++++++++-------------- 8 files changed, 48 insertions(+), 46 deletions(-)
arch/arm64/include/asm/kfence.h +0 -2
@@ -5,8 +5,6 @@#include <asm/cacheflush.h> -#define KFENCE_SKIP_ARCH_FAULT_HANDLER "el1_sync" -static inline bool arch_kfence_init_pool (void ) {return true; }static inline bool kfence_protect_page (unsigned long addr,bool protect)
arch/arm64/mm/fault.c +1 -1
@@ -323,7 +323,7 @@ static void __do_kernel_fault(unsigned long addr, unsigned int esr, }else if (addr <PAGE_SIZE ) { msg ="NULL pointer dereference" ; }else { -if (kfence_handle_page_fault (addr)) +if (kfence_handle_page_fault (addr, regs))return ; msg ="paging request" ;
arch/x86/include/asm/kfence.h +0 -6
@@ -11,12 +11,6 @@#include <asm/set_memory.h> #include <asm/tlbflush.h> -/* -* The page fault handler entry function, up to which the stack trace is -* truncated in reports. -*/ -#define KFENCE_SKIP_ARCH_FAULT_HANDLER "asm_exc_page_fault" -/* Force 4K pages for __kfence_pool. */ static inline bool arch_kfence_init_pool (void ) {
arch/x86/mm/fault.c +1 -1
@@ -727,7 +727,7 @@ no_context(struct pt_regs *regs, unsigned long error_code,efi_recover_from_page_fault (address);/* Only not-present faults should be handled by KFENCE. */ -if (!(error_code &X86_PF_PROT ) &&kfence_handle_page_fault (address)) +if (!(error_code &X86_PF_PROT ) &&kfence_handle_page_fault (address, regs))return ; oops:
include/linux/kfence.h +3 -2
@@ -171,6 +171,7 @@ static __always_inline __must_check bool kfence_free(void *addr)/** * kfence_handle_page_fault() - perform page fault handling for KFENCE pages * @addr: faulting address +* @regs: current struct pt_regs (can be NULL, but shows full stack trace) * * Return: * * false - address outside KFENCE pool,
@@ -181,7 +182,7 @@ static __always_inline __must_check bool kfence_free(void *addr)* cases KFENCE prints an error message and marks the offending page as * present, so that the kernel can proceed. */ -bool __must_check kfence_handle_page_fault (unsigned long addr); +bool __must_check kfence_handle_page_fault (unsigned long addr,struct pt_regs *regs);#else /* CONFIG_KFENCE */
@@ -194,7 +195,7 @@ static inline size_t kfence_ksize(const void *addr) { return 0; }static inline void *kfence_object_start (const void *addr) {return NULL ; }static inline void __kfence_free (void *addr) { }static inline bool __must_check kfence_free (void *addr) {return false; } -static inline bool __must_check kfence_handle_page_fault (unsigned long addr) {return false; } +static inline bool __must_check kfence_handle_page_fault (unsigned long addr,struct pt_regs *regs) {return false; }#endif
mm/kfence/core.c +5 -5
@@ -212,7 +212,7 @@ static inline bool check_canary_byte(u8 *addr)return true;atomic_long_inc (&counters[KFENCE_COUNTER_BUGS ]); -kfence_report_error ((unsigned long )addr,addr_to_metadata ((unsigned long )addr), +kfence_report_error ((unsigned long )addr,NULL ,addr_to_metadata ((unsigned long )addr),KFENCE_ERROR_CORRUPTION );return false; }
@@ -351,7 +351,7 @@ static void kfence_guarded_free(void *addr, struct kfence_metadata *meta, bool zif (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 ); +kfence_report_error ((unsigned long )addr,NULL , meta,KFENCE_ERROR_INVALID_FREE );raw_spin_unlock_irqrestore (&meta->lock, flags);return ; }
@@ -752,7 +752,7 @@ void __kfence_free(void *addr)kfence_guarded_free (addr, meta, false); } -bool kfence_handle_page_fault (unsigned long addr) +bool kfence_handle_page_fault (unsigned long addr,struct pt_regs *regs) {const int page_index = (addr - (unsigned long )__kfence_pool) /PAGE_SIZE ;struct kfence_metadata *to_report =NULL ;
@@ -815,11 +815,11 @@ bool kfence_handle_page_fault(unsigned long addr) out:if (to_report) { -kfence_report_error (addr, to_report, error_type); +kfence_report_error (addr, 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,NULL ,KFENCE_ERROR_INVALID ); +kfence_report_error (addr, regs,NULL ,KFENCE_ERROR_INVALID ); }return kfence_unprotect (addr);/* Unprotect and let access proceed. */
mm/kfence/kfence.h +2 -2
@@ -99,8 +99,8 @@ enum kfence_error_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_report_error (unsigned long address,struct pt_regs *regs, +const struct kfence_metadata *meta,enum kfence_error_type type);void kfence_print_object (struct seq_file *seq,const struct kfence_metadata *meta);
mm/kfence/report.c +36 -27
103 lines not on this page · raw message
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
Mark Rutland
reviewerQuoted 8 lines · Marco Elver
Instead of removing the fault handling portion of the stack trace based on the fault handler's name, just use struct
pt_regsdirectly.Change
kfence_handle_page_fault()to take a structpt_regs, and plumb it through tokfence_report_error()for out-of-bounds, use-after-free, or invalid access errors, wherept_regsis used to generate the stack trace.
Quoted 5 lines
If the kernel is a
DEBUG_KERNEL, also show registers for more information.Suggested-by Mark Rutland <[email protected]>Signed-off-by Marco Elver <[email protected]>
Wow; I wasn't expecting this to be put together so quickly, thanks for doing this!
From a scan, this looks good to me -- just one question below.
Quoted 15 lines
include/linux/kfence.h +1
-0@@ -171,6 +171,7 @@ static __always_inline __must_check bool kfence_free(void *addr)/** * kfence_handle_page_fault() - perform page fault handling for KFENCE pages * @addr: faulting address +* @regs: current struct pt_regs (can be NULL, but shows full stack trace) * * Return: * * false - address outside KFENCE pool, @@ -44,8 +44,12 @@ static int get_stack_skipnr(const unsigned long stack_entries[], int num_entriescase KFENCE_ERROR_UAF :
include/linux/kfence.h static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries · line 52
Quoted 10 lines
include/linux/kfence.h +6
-2case KFENCE_ERROR_OOB :case KFENCE_ERROR_INVALID : - is_access_fault = true; -break ; +/* +* kfence_handle_page_fault() may be called with pt_regs +* set to NULL; in that case we'll simply show the full +* stack trace. +*/ +return 0 ;
For both the above comments, when/where is kfence_handle_page_fault() called with regs set to NULL? I couldn't spot that in this patch, so unless I mised it I'm guessing that's somewhere outside of the patch context?
If this is a case we don't expect to happen, maybe add a WARN_ON_ONCE()?
Thanks,
Mark.
Marco Elver
maintainerQuoted 42 lines · Mark Rutland
Quoted 8 lines · Marco Elver
Instead of removing the fault handling portion of the stack trace based on the fault handler's name, just use struct
pt_regsdirectly.Change
kfence_handle_page_fault()to take a structpt_regs, and plumb it through tokfence_report_error()for out-of-bounds, use-after-free, or invalid access errors, wherept_regsis used to generate the stack trace.Quoted 5 lines
If the kernel is a
DEBUG_KERNEL, also show registers for more information.Suggested-by Mark Rutland <[email protected]>Signed-off-by Marco Elver <[email protected]>Wow; I wasn't expecting this to be put together so quickly, thanks for doing this!
From a scan, this looks good to me -- just one question below.
Quoted 22 lines
include/linux/kfence.h +4
-2@@ -171,6 +171,7 @@ static __always_inline __must_check bool kfence_free(void *addr)/** * kfence_handle_page_fault() - perform page fault handling for KFENCE pages * @addr: faulting address +* @regs: current struct pt_regs (can be NULL, but shows full stack trace) * * Return: * * false - address outside KFENCE pool, @@ -44,8 +44,12 @@ static int get_stack_skipnr(const unsigned long stack_entries[], int num_entriescase KFENCE_ERROR_UAF :case KFENCE_ERROR_OOB :case KFENCE_ERROR_INVALID : - is_access_fault = true; -break ; +/* +* kfence_handle_page_fault() may be called with pt_regs +* set to NULL; in that case we'll simply show the full
Quoted 8 lines
include/linux/kfence.h static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries · line 52
Quoted 3 lines
include/linux/kfence.h +3
-0+* stack trace. +*/ +return 0 ;For both the above comments, when/where is
kfence_handle_page_fault()called with regs set to NULL? I couldn't spot that in this patch, so unless I mised it I'm guessing that's somewhere outside of the patch context?
Right, currently it's not expected to happen, but I'd like to permit this function being called not from fault handlers, for use-cases like this:
https://lkml.kernel.org/r/CANpmjNNxAvembOetv15FfZ=04mpj0Qwx+1tnn22tABaHHRRv=Q@mail.gmail.com
The revised recommendation when trying to get KFENCE to give us more information about allocation/free stacks after refcount underflow (like what Paul was trying to do) would be to call kfence_handle_page_fault(addr, NULL).
Quoted 1 line
If this is a case we don't expect to happen, maybe add a
WARN_ON_ONCE()?
While it's currently not expected, I don't see why we should make this WARN and limit the potential uses of the API if it works just fine if we pass regs set to NULL. Although arguably the name kfence_handle_page_fault() might be confusing for such uses, for now, until more widespread use is evident (if at all) I'd say let's keep as-is, but simply not prevent such use-cases.
Thanks,
-- Marco
Quoted 63 lines · Marco Elver
Quoted 42 lines · Mark Rutland
Quoted 8 lines · Marco Elver
Instead of removing the fault handling portion of the stack trace based on the fault handler's name, just use struct
pt_regsdirectly.Change
kfence_handle_page_fault()to take a structpt_regs, and plumb it through tokfence_report_error()for out-of-bounds, use-after-free, or invalid access errors, wherept_regsis used to generate the stack trace.Quoted 5 lines
If the kernel is a
DEBUG_KERNEL, also show registers for more information.Suggested-by Mark Rutland <[email protected]>Signed-off-by Marco Elver <[email protected]>Wow; I wasn't expecting this to be put together so quickly, thanks for doing this!
From a scan, this looks good to me -- just one question below.
Quoted 22 lines
include/linux/kfence.h +4
-2@@ -171,6 +171,7 @@ static __always_inline __must_check bool kfence_free(void *addr)/** * kfence_handle_page_fault() - perform page fault handling for KFENCE pages * @addr: faulting address +* @regs: current struct pt_regs (can be NULL, but shows full stack trace) * * Return: * * false - address outside KFENCE pool, @@ -44,8 +44,12 @@ static int get_stack_skipnr(const unsigned long stack_entries[], int num_entriescase KFENCE_ERROR_UAF :case KFENCE_ERROR_OOB :case KFENCE_ERROR_INVALID : - is_access_fault = true; -break ; +/* +* kfence_handle_page_fault() may be called with pt_regs +* set to NULL; in that case we'll simply show the full Quoted 8 lines
include/linux/kfence.h static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries · line 52
Quoted 3 lines
include/linux/kfence.h +3
-0+* stack trace. +*/ +return 0 ;For both the above comments, when/where is
kfence_handle_page_fault()called with regs set to NULL? I couldn't spot that in this patch, so unless I mised it I'm guessing that's somewhere outside of the patch context?Right, currently it's not expected to happen, but I'd like to permit this function being called not from fault handlers, for use-cases like this:
https://lkml.kernel.org/r/CANpmjNNxAvembOetv15FfZ=04mpj0Qwx+1tnn22tABaHHRRv=Q@mail.gmail.com
The revised recommendation when trying to get KFENCE to give us more information about allocation/free stacks after refcount underflow (like what Paul was trying to do) would be to call
kfence_handle_page_fault(addr, NULL).
Quoted 8 lines
Quoted 1 line
If this is a case we don't expect to happen, maybe add a
WARN_ON_ONCE()?While it's currently not expected, I don't see why we should make this WARN and limit the potential uses of the API if it works just fine if we pass regs set to NULL. Although arguably the name
kfence_handle_page_fault()might be confusing for such uses, for now, until more widespread use is evident (if at all) I'd say let's keep as-is, but simply not prevent such use-cases.
Fair enough! I guess in future we could always revise that anyhow.
FWIW, for this as-is:
Mark.