From patchwork Tue Aug 8 12:23:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Redfearn X-Patchwork-Id: 9888021 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3BFA560384 for ; Tue, 8 Aug 2017 12:25:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2E98628881 for ; Tue, 8 Aug 2017 12:25:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2395D28885; Tue, 8 Aug 2017 12:25:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 30DEE28881 for ; Tue, 8 Aug 2017 12:25:33 +0000 (UTC) Received: (qmail 18005 invoked by uid 550); 8 Aug 2017 12:25:31 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 16261 invoked from network); 8 Aug 2017 12:24:00 -0000 From: Matt Redfearn To: Ralf Baechle CC: , , Matt Redfearn , , James Hogan , Paul Burton Date: Tue, 8 Aug 2017 13:23:42 +0100 Message-ID: <1502195022-18161-1-git-send-email-matt.redfearn@imgtec.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-Originating-IP: [10.150.130.83] Subject: [kernel-hardening] [PATCH] MIPS: usercopy: Implement stack frame object validation X-Virus-Scanned: ClamAV using ClamSMTP This implements arch_within_stack_frames() for MIPS that validates if an object is wholly contained by a kernel stack frame. With CONFIG_HARDENED_USERCOPY enabled, MIPS now passes the LKDTM tests USERCOPY_STACK_FRAME_TO, USERCOPY_STACK_FRAME_FROM and USERCOPY_STACK_BEYOND on a Creator Ci40. Since the MIPS kernel does not use frame pointers, we re-use the MIPS kernels stack frame unwinder which uses instruction inspection to deduce the stack frame size. As such it introduces a larger performance penalty than on arches which use the frame pointer. On qemu, before this patch, hackbench gives: Running with 10*40 (== 400) tasks. Time: 5.484 Running with 10*40 (== 400) tasks. Time: 4.039 Running with 10*40 (== 400) tasks. Time: 3.908 Running with 10*40 (== 400) tasks. Time: 3.955 Running with 10*40 (== 400) tasks. Time: 4.185 Running with 10*40 (== 400) tasks. Time: 4.497 Running with 10*40 (== 400) tasks. Time: 3.980 Running with 10*40 (== 400) tasks. Time: 4.078 Running with 10*40 (== 400) tasks. Time: 4.219 Running with 10*40 (== 400) tasks. Time: 4.026 Giving an average of 4.2371 With this patch, hackbench gives: Running with 10*40 (== 400) tasks. Time: 5.671 Running with 10*40 (== 400) tasks. Time: 4.282 Running with 10*40 (== 400) tasks. Time: 4.101 Running with 10*40 (== 400) tasks. Time: 4.040 Running with 10*40 (== 400) tasks. Time: 4.683 Running with 10*40 (== 400) tasks. Time: 4.387 Running with 10*40 (== 400) tasks. Time: 4.289 Running with 10*40 (== 400) tasks. Time: 4.027 Running with 10*40 (== 400) tasks. Time: 4.048 Running with 10*40 (== 400) tasks. Time: 4.079 Giving an average of 4.3607 This indicates an additional 3% overhead for inspecting the kernel stack when CONFIG_HARDENED_USERCOPY is enabled. This patch is based on Linux v4.13-rc4, and for correct operation on microMIPS depends on my series "MIPS: Further microMIPS stack unwinding fixes" Signed-off-by: Matt Redfearn Reviewed-by: James Hogan --- arch/mips/Kconfig | 1 + arch/mips/include/asm/thread_info.h | 74 +++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig index 8dd20358464f..6cbf2d525c8d 100644 --- a/arch/mips/Kconfig +++ b/arch/mips/Kconfig @@ -35,6 +35,7 @@ config MIPS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE if CPU_SUPPORTS_HUGEPAGES && 64BIT + select HAVE_ARCH_WITHIN_STACK_FRAMES if KALLSYMS select HAVE_CBPF_JIT if (!64BIT && !CPU_MICROMIPS) select HAVE_EBPF_JIT if (64BIT && !CPU_MICROMIPS) select HAVE_CC_STACKPROTECTOR diff --git a/arch/mips/include/asm/thread_info.h b/arch/mips/include/asm/thread_info.h index b439e512792b..931652460393 100644 --- a/arch/mips/include/asm/thread_info.h +++ b/arch/mips/include/asm/thread_info.h @@ -14,6 +14,80 @@ #include +#ifdef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES + +/* + * Walks up the stack frames to make sure that the specified object is + * entirely contained by a single stack frame. + * + * Returns: + * GOOD_FRAME if within a frame + * BAD_STACK if placed across a frame boundary (or outside stack) + * NOT_STACK unable to determine + */ +static inline int arch_within_stack_frames(const void *const stack, + const void *const stackend, + const void *obj, unsigned long len) +{ + /* Avoid header recursion by just declaring this here */ + extern unsigned long unwind_stack_by_address( + unsigned long stack_page, + unsigned long *sp, + unsigned long pc, + unsigned long *ra); + unsigned long sp, lastsp, ra, pc; + int skip_frames; + + /* Get this frame's details */ + sp = (unsigned long)__builtin_frame_address(0); + pc = (unsigned long)current_text_addr(); + + /* + * Skip initial frames to get back the function requesting the copy. + * Unwind the frames of: + * arch_within_stack_frames (inlined into check_stack_object) + * __check_object_size + * This leaves sp & pc in the frame associated with + * copy_{to,from}_user() (inlined into do_usercopy_stack) + */ + for (skip_frames = 0; skip_frames < 2; skip_frames++) { + pc = unwind_stack_by_address((unsigned long)stack, &sp, pc, &ra); + if (!pc) + return BAD_STACK; + } + + if ((unsigned long)obj < sp) { + /* obj is not in the frame of the requestor or it's callers */ + return BAD_STACK; + } + + /* + * low ---------------------------------------> high + * [local vars][saved regs][ra][local vars'] + * ^ ^ + * lastsp sp + * ^----------------------^ + * allow copies only within here + */ + do { + lastsp = sp; + pc = unwind_stack_by_address((unsigned long)stack, &sp, pc, &ra); + if ((((unsigned long)obj) >= lastsp) && + (((unsigned long)obj + len) <= (sp - sizeof(void *)))) { + /* obj is entirely within this stack frame */ + return GOOD_FRAME; + } + } while (pc); + + /* + * We can't unwind any further. If we haven't found the object entirely + * within one of our callers frames, it must be a bad object. + */ + return BAD_STACK; +} + +#endif /* CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES */ + /* * low level task data that entry.S needs immediate access to * - this struct should fit entirely inside of one cache line