Skip to contentSkip to conversation

kfence: Use pt_regs to generate stack trace on faults

PATCH Applied Marco Elver 2 participants 4 messages Nov 5, 2020

Subsystems (6)
Memory management x86 arm64 x86 MM +2 more subsystems
lore
Share & tools
lore

Marco Elver

author maintainer
#0 lore raw

PATCH 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
index 5ac0f599cc9a..6c0afeeab635 100644
@@ -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
index 2d60204b4ed2..183d1e6dd9e0 100644
@@ -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
index beeac105dae7..2f3f877a7a5c 100644
@@ -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
index e42db2836438..53d732161b4f 100644
@@ -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
index ed2d48acdafe..98a97f9d43cd 100644
@@ -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
index 9d597013cd5d..9358f42a9a9e 100644
@@ -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 z
 	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);
+		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
index f115aabc2052..fa3579d03089 100644
@@ -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
reviewer
#1 lore raw
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_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.

Quoted 5 lines

If the kernel is a DEBUG_KERNEL, also show registers for more information.

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
index ed2d48acdafe..98a97f9d43cd 100644
@@ -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_entries
 		case 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 -2
 		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
+			 * 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.

author maintainer
#2 lore raw
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_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.

Quoted 5 lines

If the kernel is a DEBUG_KERNEL, also show registers for more information.

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
index ed2d48acdafe..98a97f9d43cd 100644
@@ -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_entries
              case 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

reviewer Acked-by
#3 lore raw
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_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.

Quoted 5 lines

If the kernel is a DEBUG_KERNEL, also show registers for more information.

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
index ed2d48acdafe..98a97f9d43cd 100644
@@ -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_entries
              case 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.