From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751474AbdJCReG (ORCPT ); Tue, 3 Oct 2017 13:34:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56496 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751172AbdJCReF (ORCPT ); Tue, 3 Oct 2017 13:34:05 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3799981E19 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jpoimboe@redhat.com Date: Tue, 3 Oct 2017 12:34:02 -0500 From: Josh Poimboeuf To: Tetsuo Handa , Fengguang Wu Cc: Byungchul Park , Ingo Molnar , "Peter Zijlstra (Intel)" , linux-kernel@vger.kernel.org, LKP , Tetsuo Handa , torvalds@linux-foundation.org, bp@alien8.de, x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner Subject: Re: [lockdep] b09be676e0 BUG: unable to handle kernel NULL pointer dereference at 000001f2 Message-ID: <20171003173402.hq4aviy5hztgauyt@treble> References: <20171003140634.r2jzujgl62ox4uzh@wfg-t540p.sh.intel.com> <20171003143147.ypotpg4f3dql4gtf@treble> <20171003144136.ar7z3pavhvizfabs@treble> <20171003150538.57dq7xe6afikpzyp@treble> <20171003162815.2eatamg4h3tpsypl@treble> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20171003162815.2eatamg4h3tpsypl@treble> User-Agent: Mutt/1.6.0.1 (2016-04-01) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 03 Oct 2017 17:34:05 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 03, 2017 at 11:28:15AM -0500, Josh Poimboeuf wrote: > There are two bugs: > > 1) Somebody -- presumably lockdep -- is corrupting the stack. Need the > lockdep people to look at that. > > 2) The 32-bit FP unwinder isn't handling the corrupt stack very well, > It's blindly dereferencing untrusted data: > > /* Is the next frame pointer an encoded pointer to pt_regs? */ > regs = decode_frame_pointer(next_bp); > if (regs) { > frame = (unsigned long *)regs; > len = regs_size(regs); > state->got_irq = true; > > On 32-bit, regs_size() dereferences the regs pointer before we know it > points to a valid stack. I'll fix that, along with the other unwinder > improvements I discussed with Linus. Tetsuo and/or Fengguang, Would you mind testing with this patch? It should at least prevent the unwinder panic and should hopefully print a useful unwinder dump instead. diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c index d145a0b1f529..a0dd4df2234d 100644 --- a/arch/x86/kernel/unwind_frame.c +++ b/arch/x86/kernel/unwind_frame.c @@ -44,7 +44,8 @@ static void unwind_dump(struct unwind_state *state) state->stack_info.type, state->stack_info.next_sp, state->stack_mask, state->graph_idx); - for (sp = state->orig_sp; sp; sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) { + for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp; + sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) { if (get_stack_info(sp, state->task, &stack_info, &visit_mask)) break; @@ -77,6 +78,12 @@ static size_t regs_size(struct pt_regs *regs) return sizeof(*regs); } +#ifdef CONFIG_X86_32 +#define KERNEL_REGS_SIZE (sizeof(struct pt_regs) - 2*sizeof(long)) +#else +#define KERNEL_REGS_SIZE (sizeof(struct pt_regs)) +#endif + static bool in_entry_code(unsigned long ip) { char *addr = (char *)ip; @@ -178,7 +185,7 @@ static struct pt_regs *decode_frame_pointer(unsigned long *bp) { unsigned long regs = (unsigned long)bp; - if (!(regs & 0x1)) + if ((regs & (sizeof(long)-1)) != 1) return NULL; return (struct pt_regs *)(regs & ~0x1); @@ -202,7 +209,7 @@ static bool update_stack_state(struct unwind_state *state, regs = decode_frame_pointer(next_bp); if (regs) { frame = (unsigned long *)regs; - len = regs_size(regs); + len = KERNEL_REGS_SIZE; state->got_irq = true; } else { frame = next_bp; @@ -221,11 +228,23 @@ static bool update_stack_state(struct unwind_state *state, &state->stack_mask)) return false; + /* Make sure the frame pointer is properly aligned: */ + if ((unsigned long)frame & (sizeof(long)-1)) + return false; + /* Make sure it only unwinds up and doesn't overlap the prev frame: */ if (state->orig_sp && state->stack_info.type == prev_type && frame < prev_frame_end) return false; + /* + * On 32-bit with user mode regs, make sure the last two regs are safe + * to access: + */ + if (IS_ENABLED(CONFIG_X86_32) && regs && user_mode(regs) && + !on_stack(info, frame, len + 2*sizeof(long))) + return false; + /* Move state to the next frame: */ if (regs) { state->regs = regs;