[PATCH v4 49/57] x86/unwind: warn on kernel stack corruption

From: Josh Poimboeuf
Date: Thu Aug 18 2016 - 09:12:13 EST


Detect situations in the unwinder where the frame pointer refers to a
bad address, and print an appropriate warning.

Use printk_deferred_once() because the unwinder can be called with the
console lock by lockdep via save_stack_trace().

Signed-off-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
---
arch/x86/kernel/unwind_frame.c | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
index 2e729bf..65c5e416 100644
--- a/arch/x86/kernel/unwind_frame.c
+++ b/arch/x86/kernel/unwind_frame.c
@@ -130,8 +130,17 @@ bool unwind_next_frame(struct unwind_state *state)
}

/* make sure the next frame's data is accessible */
- if (!update_stack_state(state, next_frame, next_len))
- return false;
+ if (!update_stack_state(state, next_frame, next_len)) {
+ /*
+ * Don't warn on bad regs->bp. An interrupt in entry code
+ * might cause a false positive warning.
+ */
+ if (state->regs)
+ goto the_end;
+
+ goto bad_address;
+ }
+
/* move to the next frame */
if (regs) {
state->regs = regs;
@@ -143,6 +152,17 @@ bool unwind_next_frame(struct unwind_state *state)

return true;

+bad_address:
+ if (state->regs)
+ printk_deferred_once(KERN_WARNING
+ "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
+ state->regs, state->task->comm,
+ state->task->pid, next_bp);
+ else
+ printk_deferred_once(KERN_WARNING
+ "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
+ state->bp, state->task->comm,
+ state->task->pid, next_bp);
the_end:
state->stack_info.type = STACK_TYPE_UNKNOWN;
return false;
--
2.7.4