From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936544AbcJ1Gux (ORCPT ); Fri, 28 Oct 2016 02:50:53 -0400 Received: from terminus.zytor.com ([198.137.202.10]:49438 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751014AbcJ1Guv (ORCPT ); Fri, 28 Oct 2016 02:50:51 -0400 Date: Thu, 27 Oct 2016 23:49:55 -0700 From: tip-bot for Josh Poimboeuf Message-ID: Cc: torvalds@linux-foundation.org, luto@kernel.org, dvlasenk@redhat.com, peterz@infradead.org, bp@alien8.de, linux-kernel@vger.kernel.org, jpoimboe@redhat.com, brgerst@gmail.com, mingo@kernel.org, hpa@zytor.com, tglx@linutronix.de Reply-To: dvlasenk@redhat.com, peterz@infradead.org, bp@alien8.de, jpoimboe@redhat.com, linux-kernel@vger.kernel.org, mingo@kernel.org, hpa@zytor.com, brgerst@gmail.com, tglx@linutronix.de, torvalds@linux-foundation.org, luto@kernel.org In-Reply-To: <20161027131058.tpdffwlqipv7pcd6@treble> References: <20161027131058.tpdffwlqipv7pcd6@treble> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/asm] x86/unwind: Ensure stack grows down Git-Commit-ID: 24d86f59093b0bcb3756cdf47f2db10ff4e90dbb X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 24d86f59093b0bcb3756cdf47f2db10ff4e90dbb Gitweb: http://git.kernel.org/tip/24d86f59093b0bcb3756cdf47f2db10ff4e90dbb Author: Josh Poimboeuf AuthorDate: Thu, 27 Oct 2016 08:10:58 -0500 Committer: Ingo Molnar CommitDate: Fri, 28 Oct 2016 08:16:45 +0200 x86/unwind: Ensure stack grows down Add a sanity check to ensure the stack only grows down, and print a warning if the check fails. Signed-off-by: Josh Poimboeuf Cc: Andy Lutomirski Cc: Borislav Petkov Cc: Brian Gerst Cc: Denys Vlasenko Cc: H. Peter Anvin Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Link: http://lkml.kernel.org/r/20161027131058.tpdffwlqipv7pcd6@treble Signed-off-by: Ingo Molnar --- 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..ea7b7f9 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;