From patchwork Thu Nov 29 23:00:25 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Cross X-Patchwork-Id: 1823211 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork1.kernel.org (Postfix) with ESMTP id 4316F3FC23 for ; Thu, 29 Nov 2012 23:04:26 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1TeD6G-00005J-Sh; Thu, 29 Nov 2012 23:00:56 +0000 Received: from mail-ea0-f201.google.com ([209.85.215.201]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1TeD5s-0008Uo-UD for linux-arm-kernel@lists.infradead.org; Thu, 29 Nov 2012 23:00:36 +0000 Received: by mail-ea0-f201.google.com with SMTP id b14so1011537eaa.0 for ; Thu, 29 Nov 2012 15:00:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :x-gm-message-state; bh=yjkedFacCt6JdeXez+WLJ+hGFuLrIp8wEcmFvsjmL8c=; b=QogmXBdwDELPfvWglUOuWSo66bdbLESN5xIXmo45HRntbDh5xy4agRI8KemIJgxcd3 SD5zeimelowsMq3bXKXx1V5G3iGiKLOoFhk9B+aopc3k9sudEgSj1bCol6FUb2LUD0ls XHFBb09BvewdhWE1pMQHhbPkAjer90ILwf4Y03ohA66NLC6JLtlJZ88yDQdPSvPhGpYS eQa1bIHawvsuOoKdmgKCCDmE9VmCGXQ3wz4arBeCWbFXdrRxspXc8ILShsr4G2XrdpCV 1qGZji69rFGV/BQsY5BPDOwaKhpk9WzV8uZEDBk6GF0UEyJskjV/MGsFCGPRcz9nReQp VH2w== Received: by 10.14.180.2 with SMTP id i2mr7352673eem.1.1354230030628; Thu, 29 Nov 2012 15:00:30 -0800 (PST) Received: from hpza10.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id z47si744227eel.0.2012.11.29.15.00.30 (version=TLSv1/SSLv3 cipher=AES128-SHA); Thu, 29 Nov 2012 15:00:30 -0800 (PST) Received: from walnut.mtv.corp.google.com (walnut.mtv.corp.google.com [172.18.105.48]) by hpza10.eem.corp.google.com (Postfix) with ESMTP id 19D8C200061; Thu, 29 Nov 2012 15:00:30 -0800 (PST) Received: by walnut.mtv.corp.google.com (Postfix, from userid 99897) id 6A7FC161586; Thu, 29 Nov 2012 15:00:29 -0800 (PST) From: Colin Cross To: linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 1/3] ARM: stacktrace: harden FP stacktraces against invalid stacks Date: Thu, 29 Nov 2012 15:00:25 -0800 Message-Id: <1354230027-2204-2-git-send-email-ccross@android.com> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1354230027-2204-1-git-send-email-ccross@android.com> References: <1354230027-2204-1-git-send-email-ccross@android.com> X-Gm-Message-State: ALoCoQl+OPZMYbsBPrUEYVzy3A+HEAJwVvne2wMK23eQ5zJaMA/7xVPkJYVFZ3dbI6BFJTYqW7+k1B3ORaI3I8pS2DKVFcOjgovIn9KXYJgQBCqqT+RHExESuaeAe7GGLtBvi3h92gXv5oP16/pWGjkLnrO9pHArL7vsnTqFuIp0CsE3JVoD1r9SCoIWmhHVMz8VrLLP07SvGTlxCN6bjX0ztle1yuxJAg== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20121129_180033_431498_6CB863B0 X-CRM114-Status: GOOD ( 26.54 ) X-Spam-Score: -3.3 (---) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-3.3 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.215.201 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.7 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Dave Martin , Laura Abbott , Catalin Marinas , Will Deacon , Rabin Vincent , Colin Cross , Russell King X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Dumping stacktraces is currently disabled in ARM SMP for all tasks except the current task due to the worry that the task may be running on another CPU and that the unwinder may be unstable when presented with a stack that is being modified. Unwinding with CONFIG_FRAME_POINTER is fairly simple compared to when CONFIG_ARM_UNWIND is set. The next frame's FP and SP registers are read from the stack and can be validated against the current values to ensure that they do not leave the stack and make progress towards the upper end of the stack. This guarantees that accesses do not fault and that execution is bounded. Add additional validations to the version of unwind_frame used when CONFIG_FRAME_POINTER=y: Verify that the stack is in a mapped region of kernel memory. Fixes crashes seen in unwind_frame on real systems, although stack corruption caused by memory instability is likely the cause of the invalid sp and fp values. Fix address comparison to catch fp >= 0xfffffffc correctly. Fixes crash reported by Todd Poynor. Ensure the stack pointer moves to a higher address between each frame to make sure repeated calls to unwind_frame can't loop forever. Includes ideas from Dave Martin. Signed-off-by: Colin Cross --- v2: verify that initial sp value is in mapped lowmem verify that stack offsets are in the range [sizeof(struct thread_info), THREAD_START_SP) export stack validation functions for use by unwind arch/arm/include/asm/stacktrace.h | 4 ++ arch/arm/kernel/stacktrace.c | 105 ++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 6 deletions(-) diff --git a/arch/arm/include/asm/stacktrace.h b/arch/arm/include/asm/stacktrace.h index 4d0a164..6909056 100644 --- a/arch/arm/include/asm/stacktrace.h +++ b/arch/arm/include/asm/stacktrace.h @@ -12,4 +12,8 @@ struct stackframe { extern void walk_stackframe(struct stackframe *frame, int (*fn)(struct stackframe *, void *), void *data); +bool sp_addr_valid(unsigned long sp); +bool addr_in_stack(unsigned long orig_sp, unsigned long vsp); +bool sp_in_stack(unsigned long orig_sp, unsigned long vsp); + #endif /* __ASM_STACKTRACE_H */ diff --git a/arch/arm/kernel/stacktrace.c b/arch/arm/kernel/stacktrace.c index 00f79e5..ca7e71b 100644 --- a/arch/arm/kernel/stacktrace.c +++ b/arch/arm/kernel/stacktrace.c @@ -1,9 +1,89 @@ #include +#include #include #include #include +#define STACK_MAX(sp) (round_down(sp, THREAD_SIZE) + THREAD_START_SP) + +/** + * sp_addr_valid - verify a stack pointer + * @sp: current stack pointer + * + * Returns true if sp is a pointer inside a memory area that could be a stack. + * Does not verify that sp is inside an actual stack (i.e. does not check for + * STACK_MAGIC). + * + * If sp_addr_valid(sp) returns true, then the kernel will not fault if it + * accesses memory in the range + * [sp, round_down(sp, THREAD_SIZE) + THREAD_START_SP) + */ +bool sp_addr_valid(unsigned long sp) +{ + unsigned long high; + unsigned long offset; + unsigned int pfn; + unsigned int start_pfn; + unsigned int end_pfn; + + if (!IS_ALIGNED(sp, 4)) + return false; + + offset = sp & (THREAD_SIZE - 1); + + if (offset > THREAD_START_SP) + return false; + + if (offset < sizeof(struct thread_info)) + return false; + + high = STACK_MAX(sp); + + if (!virt_addr_valid(sp) || !virt_addr_valid(high)) + return false; + + start_pfn = page_to_pfn(virt_to_page(sp)); + end_pfn = page_to_pfn(virt_to_page(high)); + for (pfn = start_pfn; pfn <= end_pfn; pfn++) + if (!pfn_valid(pfn)) + return false; + + return true; +} + +/** + * addr_in_stack - verify a pointer is inside a specified stack + * @orig_sp: stack pointer at the bottom of the stack + * @sp: address to be verified + * + * Returns true if sp is in the stack bounded at the bottom by orig_sp, in the + * range [orig_sp, round_down(orig_sp, THREAD_SIZE) + THREAD_START_SP) + * + * If orig_sp is valid (see sp_addr_valid), then the kernel will not fault if it + * accesses a pointer where ptr_in_stack returns true. + */ +bool addr_in_stack(unsigned long orig_sp, unsigned long sp) +{ + return (sp >= orig_sp && sp < STACK_MAX(orig_sp) && IS_ALIGNED(sp, 4)); +} + +/** + * sp_in_stack - verify a stack pointer is inside a specified stack + * @orig_sp: stack pointer at the bottom of the stack + * @sp: stack pointer to be verified + * + * Returns true if sp is in the stack bounded at the bottom by orig_sp, in the + * range [orig_sp, round_down(orig_sp, THREAD_SIZE) + THREAD_START_SP] + * + * If sp_in_stack returns true, + * addr_in_stack(vsp, x) == addr_in_stack(orig_sp, x) + */ +bool sp_in_stack(unsigned long orig_sp, unsigned long sp) +{ + return (sp >= orig_sp && sp <= STACK_MAX(orig_sp) && IS_ALIGNED(sp, 4)); +} + #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND) /* * Unwind the current stack frame and store the new register values in the @@ -23,15 +103,17 @@ */ int notrace unwind_frame(struct stackframe *frame) { - unsigned long high, low; unsigned long fp = frame->fp; + unsigned long sp = frame->sp; + + if (!sp_addr_valid(sp)) + return -EINVAL; - /* only go to a higher address on the stack */ - low = frame->sp; - high = ALIGN(low, THREAD_SIZE); + /* Check current frame pointer is within the stack bounds. */ + if (!addr_in_stack(sp, fp)) + return -EINVAL; - /* check current frame pointer is within bounds */ - if (fp < (low + 12) || fp + 4 >= high) + if (fp < 12 || !addr_in_stack(sp, fp - 12)) return -EINVAL; /* restore the registers from the stack frame */ @@ -39,6 +121,17 @@ int notrace unwind_frame(struct stackframe *frame) frame->sp = *(unsigned long *)(fp - 8); frame->pc = *(unsigned long *)(fp - 4); + /* Ensure the next stack pointer is in the same stack */ + if (!sp_in_stack(sp, frame->sp)) + return -EINVAL; + + /* + * Ensure the next stack pointer is above this frame to guarantee + * bounded execution. + */ + if (frame->sp < fp) + return -EINVAL; + return 0; } #endif