From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934021AbcHDWYH (ORCPT ); Thu, 4 Aug 2016 18:24:07 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33764 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933621AbcHDWYD (ORCPT ); Thu, 4 Aug 2016 18:24:03 -0400 From: Josh Poimboeuf To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Andy Lutomirski , Linus Torvalds , Steven Rostedt , Brian Gerst , Kees Cook , Peter Zijlstra , Frederic Weisbecker , Byungchul Park Subject: [PATCH v2 42/44] x86/unwind: warn on kernel stack corruption Date: Thu, 4 Aug 2016 17:22:38 -0500 Message-Id: <8a5ccf7097c36d2926bfb29309d9e8d18b22add6.1470345772.git.jpoimboe@redhat.com> In-Reply-To: References: X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 04 Aug 2016 22:23:58 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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