Received: by 10.213.65.68 with SMTP id h4csp2236882imn; Sun, 8 Apr 2018 23:14:37 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+2RVelFPNNB81xOdWVMO/QR3Zf8Cashugg2DWgI1HsXbdTQ0J0D5pq5G3l6zKikudtOUuj X-Received: by 10.99.97.11 with SMTP id v11mr708813pgb.172.1523254476967; Sun, 08 Apr 2018 23:14:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523254476; cv=none; d=google.com; s=arc-20160816; b=rQch2w5YANqUP/CBPag1FAN0Wj3qSgsZdTgP/uEPDVg4fBmgxK0r2aprSzGDIblLdu BHwm8SuZxBRzkBpLJJaPrKN/6b//2udsRB8MWN2eN+jO9enBWFI5FDvt2NcSbHliqaZ7 7qRrlNZk1VHlevor9r3UN4yx3DXHTpuv55zVav5eTx4SseCKD3MYwMAQn1n3KGWB5hdX 9KtwTI7rxC5wTmV8qIGM5ZHdMHwBdZagi+VDdVlCf7C4i0znPTbnd7ZrMJiPQoP6JK2y 7t6CRTx4hZH/kqKh8lHQFVgOCH73g7eS7klh9AgqVAHXUdgwPA424hj/hUxCi7OKjGax yCTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=KOgJDer8FTpMEV8XvRCOfH3WuNJipW87GXybq+T51VQ=; b=zh8ye4su9+uPxyFIAvyvQHEymZ08sOGABMPZmjXlHYD0yg+8gqIo39uz2XmUcHMZuM rCi0RhgBQhUeCvzGrnF2oLGz9UP/6gTyMVE1x5x2dBUd7jNl6LcqvExq4A4NiJQWoKrU jP/voFZ8PBsqAuINEPkskNY5dZSuxqpuWsZCjsTYnL9mlZ0x7pIaOMXAxMuTOObI+xd+ r+3RdCQaLUKq8uY6w8O0SyIL2fqJdpRJGS2uin2LR4b/gHO3niXBLpgCFz55iaKZAsIM 68LyiySK0F6RNz0LWfSzTwXv6K9XzOIzZG9GFJsyLuZ7x32nZhdcGIb5dSEGVLXtvrN1 fkxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=VGmZpa4s; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q6si10558437pgt.130.2018.04.08.23.14.00; Sun, 08 Apr 2018 23:14:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=VGmZpa4s; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752070AbeDIGLK (ORCPT + 99 others); Mon, 9 Apr 2018 02:11:10 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:40784 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751833AbeDIGLI (ORCPT ); Mon, 9 Apr 2018 02:11:08 -0400 Received: by mail-wr0-f195.google.com with SMTP id n2so7881735wrj.7 for ; Sun, 08 Apr 2018 23:11:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KOgJDer8FTpMEV8XvRCOfH3WuNJipW87GXybq+T51VQ=; b=VGmZpa4sk59ISHTaMj0jpl5YMY8eaJ4DxJjSZqbikfXNiJjrh+TtlZ9KCeHjV+W2W3 NCJb80L99IOR8F/ZqaHwh213RR3VOmbCYXt1JoubGY/zwmyA8DRDu5I/OjXBvqtrvvKa qhdwdl9R+TY5cFoslKrVzJJHf+OTFa5d3AjHoKGIJou2WrePbXX2M1GeVPe7ZC+vNOjG aeUtXTDJTEUYiWtN0wog9YsJTd7S5PEQg73d3UjOE5FJB7YZJF0dV8lQVhz7jzAe06Zg NfSaL95OT+ovKdk4bxSY4FqFZQSustnTekuAWrNYvwq1gOSzGqAlL6L6wyJ4wlIwVflD 8EsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KOgJDer8FTpMEV8XvRCOfH3WuNJipW87GXybq+T51VQ=; b=BGKjzzuUOJQcZ6Aqi84IP4xstz8lfNcgPl6JlyTCFpvBqMjjVEc+rCoKsURwTT6RJd 0D/yzh/QoTNIDACULqURKIdNsIM2ynJ+YLuBDxQ3qJeKdxHZgamB6jWKJKK2+eI31pZ/ /WVHrDQgMiaopf+tyQ11YJUb9/bOHCL/vO2vhVNofQeg3+YObXrKYtQjNB+w2/RGzXjd 3eiQJh0QWJr0/QnAW7qJkpK0YK8nBK8tJs1YsGZhOKd8BmvklVbZw3JGS5+foQpEwsqT dgQj1dxofzSbb+xLrvbL4MY6xTGpo/0Fhun2g5zkZKtbFQXIjlD8fQVjowYQZKzZwixb PCfw== X-Gm-Message-State: AElRT7EbFWkt/YfXeem20p/QWQDUqI7/ByorX9ke59qLYoZY5vyZpeZb c+Wde5IuUsbigFZBNJwRFuZkGg== X-Received: by 10.223.176.253 with SMTP id j58mr24825378wra.269.1523254267095; Sun, 08 Apr 2018 23:11:07 -0700 (PDT) Received: from sahara-ad.darkmatter.uae (bba421079.alshamil.net.ae. [83.110.21.201]) by smtp.gmail.com with ESMTPSA id 185sm23240237wmj.46.2018.04.08.23.11.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 08 Apr 2018 23:11:06 -0700 (PDT) From: kpark3469@gmail.com To: kernel-hardening@lists.openwall.com Cc: catalin.marinas@arm.com, keescook@chromium.org, will.deacon@arm.com, mark.rutland@arm.com, james.morse@arm.com, panand@redhat.com, keun-o.park@darkmatter.ae, psodagud@codeaurora.org, jpoimboe@redhat.com, mingo@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] x86: usercopy: reimplement arch_within_stack_frames with unwinder Date: Mon, 9 Apr 2018 10:06:09 +0400 Message-Id: <1523253969-31699-4-git-send-email-kpark3469@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523253969-31699-3-git-send-email-kpark3469@gmail.com> References: <1523253969-31699-1-git-send-email-kpark3469@gmail.com> <1523253969-31699-2-git-send-email-kpark3469@gmail.com> <1523253969-31699-3-git-send-email-kpark3469@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sahara The old arch_within_stack_frames which used the frame pointer is now reimplemented to use frame pointer unwinder apis. So the main functionality is same as before. Signed-off-by: Sahara --- arch/x86/include/asm/unwind.h | 5 ++++ arch/x86/kernel/stacktrace.c | 64 +++++++++++++++++++++++++++++++++--------- arch/x86/kernel/unwind_frame.c | 4 +-- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h index 1f86e1b..6f04906f 100644 --- a/arch/x86/include/asm/unwind.h +++ b/arch/x86/include/asm/unwind.h @@ -87,6 +87,11 @@ void unwind_init(void); void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size, void *orc, size_t orc_size); #else +#ifdef CONFIG_UNWINDER_FRAME_POINTER +#define FRAME_HEADER_SIZE (sizeof(long) * 2) +size_t regs_size(struct pt_regs *regs); +#endif + static inline void unwind_init(void) {} static inline void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size, diff --git a/arch/x86/kernel/stacktrace.c b/arch/x86/kernel/stacktrace.c index f433a33..11ec101 100644 --- a/arch/x86/kernel/stacktrace.c +++ b/arch/x86/kernel/stacktrace.c @@ -12,6 +12,33 @@ #include +static inline void *get_cur_frame(struct unwind_state *state) +{ + void *frame = NULL; + +#if defined(CONFIG_UNWINDER_FRAME_POINTER) + if (state->regs) + frame = (void *)state->regs; + else + frame = (void *)state->bp; +#endif + return frame; +} + +static inline void *get_frame_end(struct unwind_state *state) +{ + void *frame_end = NULL; + +#if defined(CONFIG_UNWINDER_FRAME_POINTER) + if (state->regs) { + frame_end = (void *)state->regs + regs_size(state->regs); + } else { + frame_end = (void *)state->bp + FRAME_HEADER_SIZE; + } +#endif + return frame_end; +} + /* * Walks up the stack frames to make sure that the specified object is * entirely contained by a single stack frame. @@ -25,31 +52,42 @@ int arch_within_stack_frames(const void * const stack, const void * const stackend, const void *obj, unsigned long len) { -#if defined(CONFIG_FRAME_POINTER) - const void *frame = NULL; - const void *oldframe; - - oldframe = __builtin_frame_address(2); - if (oldframe) - frame = __builtin_frame_address(3); +#if defined(CONFIG_UNWINDER_FRAME_POINTER) + struct unwind_state state; + void *prev_frame_end = NULL; /* * low ----------------------------------------------> high * [saved bp][saved ip][args][local vars][saved bp][saved ip] * ^----------------^ * allow copies only within here + * + * Skip 3 non-inlined frames: arch_within_stack_frames(), + * check_stack_object() and __check_object_size(). + * */ - while (stack <= frame && frame < stackend) { + unsigned int discard_frames = 3; + + for (unwind_start(&state, current, NULL, NULL); !unwind_done(&state); + unwind_next_frame(&state)) { /* * If obj + len extends past the last frame, this * check won't pass and the next frame will be 0, * causing us to bail out and correctly report * the copy as invalid. */ - if (obj + len <= frame) - return obj >= oldframe + 2 * sizeof(void *) ? - GOOD_FRAME : BAD_STACK; - oldframe = frame; - frame = *(const void * const *)frame; + if (discard_frames) { + discard_frames--; + } else { + void *frame = get_cur_frame(&state); + + if (!frame || !prev_frame_end) + return NOT_STACK; + if (obj + len <= frame) + return obj >= prev_frame_end ? + GOOD_FRAME : BAD_STACK; + } + /* save current frame end before move to next frame */ + prev_frame_end = get_frame_end(&state); } return BAD_STACK; #else diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c index 3dc26f9..c8bfa5c 100644 --- a/arch/x86/kernel/unwind_frame.c +++ b/arch/x86/kernel/unwind_frame.c @@ -8,8 +8,6 @@ #include #include -#define FRAME_HEADER_SIZE (sizeof(long) * 2) - unsigned long unwind_get_return_address(struct unwind_state *state) { if (unwind_done(state)) @@ -69,7 +67,7 @@ static void unwind_dump(struct unwind_state *state) } } -static size_t regs_size(struct pt_regs *regs) +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)) -- 2.7.4