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 <[email protected]>
---
 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 f60a089..d90c2c9 100644
--- a/arch/x86/kernel/unwind_frame.c
+++ b/arch/x86/kernel/unwind_frame.c
@@ -135,8 +135,13 @@ bool unwind_next_frame(struct unwind_state *state)
                 * encoded regs pointer is on the IRQ stack but the regs
                 * themselves are on the task stack.
                 */
-               if (!update_stack_state(state, regs, sizeof(*regs)))
+               if (!update_stack_state(state, regs, sizeof(*regs))) {
+                       printk_deferred_once(KERN_WARNING "WARNING: kernel 
stack frame pointer at %p in %s:%d decodes to bad regs pointer %p\n",
+                               state->bp, state->task->comm, state->task->pid,
+                               regs);
+
                        return false;
+               }
 
                /*
                 * The regs are now safe to access and are made available to
@@ -157,8 +162,23 @@ bool unwind_next_frame(struct unwind_state *state)
         * Make sure the next frame is on a valid stack and can be accessed
         * safely.
         */
-       if (!update_stack_state(state, next_bp, FRAME_HEADER_SIZE))
+       if (!update_stack_state(state, next_bp, FRAME_HEADER_SIZE)) {
+               /*
+                * The next frame isn't on a valid stack, and we haven't
+                * reached the end, which means something went wrong: either a
+                * bad next stack pointer or a bad frame pointer.
+                */
+               if (state->regs)
+                       printk_deferred_once(KERN_WARNING "WARNING: kernel 
stack regs->bp at %p in %s:%d points to bad address %p\n",
+                               state->bp, state->task->comm,
+                               state->task->pid, regs);
+               else
+                       printk_deferred_once(KERN_WARNING "WARNING: kernel 
stack frame pointer at %p in %s:%d points to bad address %p\n",
+                               state->bp, state->task->comm,
+                               state->task->pid, next_bp);
+
                return false;
+       }
 
        /* move to the next frame */
        state->bp = next_bp;
-- 
2.7.4

Reply via email to