Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1510264

[PATCH v2] x86/unwind: ensure stack grows down

From Josh Poimboeuf <jpoimboe@redhat.com>
Newsgroups linux.kernel
Subject [PATCH v2] x86/unwind: ensure stack grows down
Date 2016-10-27 16:30 +0200
Message-ID <swTQR-355-25@gated-at.bofh.it> (permalink)
References <swyCJ-5Mj-5@gated-at.bofh.it> <swyCJ-5Mj-11@gated-at.bofh.it> <swMw3-6AV-53@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Add a sanity check to ensure the stack only grows down, and print a
warning if the check fails.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
 arch/x86/kernel/unwind_frame.c | 34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
index 5639db6..fe4d387 100644
--- a/arch/x86/kernel/unwind_frame.c
+++ b/arch/x86/kernel/unwind_frame.c
@@ -32,6 +32,15 @@ unsigned long unwind_get_return_address(struct unwind_state *state)
 }
 EXPORT_SYMBOL_GPL(unwind_get_return_address);
 
+static size_t regs_size(struct pt_regs *regs)
+{
+	/* x86_32 regs from kernel mode are two words shorter */
+	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
+		return sizeof(*regs) - 2*sizeof(long);
+
+	return sizeof(*regs);
+}
+
 static bool is_last_task_frame(struct unwind_state *state)
 {
 	unsigned long bp = (unsigned long)state->bp;
@@ -79,6 +88,7 @@ bool unwind_next_frame(struct unwind_state *state)
 	struct pt_regs *regs;
 	unsigned long *next_bp, *next_frame;
 	size_t next_len;
+	enum stack_type prev_type = state->stack_info.type;
 
 	if (unwind_done(state))
 		return false;
@@ -142,6 +152,15 @@ bool unwind_next_frame(struct unwind_state *state)
 		goto bad_address;
 	}
 
+	/* Make sure it only unwinds up and doesn't overlap the last frame: */
+	if (state->stack_info.type == prev_type) {
+		if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
+			goto bad_address;
+
+		if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
+			goto bad_address;
+	}
+
 	/* move to the next frame */
 	if (regs) {
 		state->regs = regs;
@@ -154,10 +173,17 @@ bool unwind_next_frame(struct unwind_state *state)
 	return true;
 
 bad_address:
-	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);
+	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_frame);
+	} 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_frame);
+	}
 the_end:
 	state->stack_info.type = STACK_TYPE_UNKNOWN;
 	return false;
-- 
2.7.4

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 0/4] x86/unwind: add some unwinder warnings Josh Poimboeuf <jpoimboe@redhat.com> - 2016-10-26 17:50 +0200
  [PATCH 4/4] x86/unwind: detect bad stack return address Josh Poimboeuf <jpoimboe@redhat.com> - 2016-10-26 17:50 +0200
    [tip:x86/asm] x86/unwind: Detect bad stack return address tip-bot for Josh Poimboeuf <tipbot@zytor.com> - 2016-10-27 09:40 +0200
  [PATCH 2/4] x86/unwind: ensure stack grows down Josh Poimboeuf <jpoimboe@redhat.com> - 2016-10-26 17:50 +0200
    Re: [PATCH 2/4] x86/unwind: ensure stack grows down Ingo Molnar <mingo@kernel.org> - 2016-10-27 08:40 +0200
      [PATCH v2] x86/unwind: ensure stack grows down Josh Poimboeuf <jpoimboe@redhat.com> - 2016-10-27 16:30 +0200
        [tip:x86/asm] x86/unwind: Ensure stack grows down tip-bot for Josh Poimboeuf <tipbot@zytor.com> - 2016-10-28 09:00 +0200
  [PATCH 3/4] x86/dumpstack: warn on stack recursion Josh Poimboeuf <jpoimboe@redhat.com> - 2016-10-26 17:50 +0200
    [tip:x86/asm] x86/dumpstack: Warn on stack recursion tip-bot for Josh Poimboeuf <tipbot@zytor.com> - 2016-10-27 09:50 +0200
  [PATCH 1/4] x86/unwind: warn on bad frame pointer Josh Poimboeuf <jpoimboe@redhat.com> - 2016-10-26 17:50 +0200
    [tip:x86/asm] x86/unwind: Warn on bad frame pointer tip-bot for Josh Poimboeuf <tipbot@zytor.com> - 2016-10-27 09:50 +0200

csiph-web