From patchwork Mon Apr 9 06:06:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: kpark3469@gmail.com X-Patchwork-Id: 10330625 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 9D9786020F for ; Mon, 9 Apr 2018 06:11:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 91FAD288C3 for ; Mon, 9 Apr 2018 06:11:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8676D28A67; Mon, 9 Apr 2018 06:11:25 +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=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, 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 7CC60288C3 for ; Mon, 9 Apr 2018 06:11:24 +0000 (UTC) Received: (qmail 14177 invoked by uid 550); 9 Apr 2018 06:11:12 -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 Received: (qmail 14094 invoked from network); 9 Apr 2018 06:11:11 -0000 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=RkjLCt+BKHZke82Ie8QRFB1J3mQGdDs1aTZg8jUpixQ=; b=F6epiUHlZo1coh/ABtoTdPVTeH/ivD2zK4qOWda7ORe72TCntN73WKLTlDWbBfJ/3K r8VRCCYmrFQVzfJzuBLmbl7Ln5d67SpR+OiY7xx+0HQp3pDr6N1WctmX5sRWkWijFNBI jCXJQCnraGsZdVC1AW4yMxvkz6LWHFaUqazMml/GQasVuF2EjdWjOtcqCtgChV+kEIwH F925YJtDQ+N68SXgJAl38Bqp+SU1HN1XHgAwQV1KsuZXaUzBzxujxNyYACps5l7bfOKH Fu7Ko6Z2Dn7T0LLYAbmziBj4NuaPvyox1lON6D95sNt5pgN9rh94lZM96oV5drVN8A3E dmyw== 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=RkjLCt+BKHZke82Ie8QRFB1J3mQGdDs1aTZg8jUpixQ=; b=OunJ5JJcKuYR9mKmDloUH7y6Kr35XHGCwBUIzoX81e/gCHzDquXQRCrEKRLE7Jp3yK Bs1+PuQony1anLwi6FYOIktwajXQvP6YAJQKBzwBjz8wZxe2kRTGV+gvsOtJPsz9i7Wq vk3waR1CvtKbVCw5Bs3lWROACL22GiuaBpU4s1oLfHcSDZlAktd5SVHtseSptscgnyCb vBXJWwqhJ7LDT7FPgr7OoRMPvQp/1cOE8mq9hoLM9RV4r0xUxshOdJE0nr6w2FtdN7gw 53xFhwCkeZ0TGOrvIXb0z9U46bMDm/WDSLB8j+QMLvtQtG2jwKP0haGq7E4XYe8nkWHA aQ/g== X-Gm-Message-State: ALQs6tDph0A3qaX7+FGrh305OTlFGnguilZYwjhfPDIcLiEaB0S2V3hn dfexXmec2WvllBm1xtxTBKQhGQ== X-Google-Smtp-Source: AIpwx48zt5vzCIbs0TNF1ChvqixNZ8WanW8EghI+LCVZHFDsxlky2LxcDvgCB/DDL55ETumPcF5QfA== X-Received: by 10.28.148.129 with SMTP id w123mr16226529wmd.116.1523254260502; Sun, 08 Apr 2018 23:11:00 -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 2/3] arm64: usercopy: implement arch_within_stack_frames Date: Mon, 9 Apr 2018 10:06:08 +0400 Message-Id: <1523253969-31699-3-git-send-email-kpark3469@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523253969-31699-2-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> X-Virus-Scanned: ClamAV using ClamSMTP From: James Morse This implements arch_within_stack_frames() for arm64 that should validate if a given object is contained by a kernel stack frame. Signed-off-by: James Morse Reviewed-by: Sahara Reviewed-by: Kees Cook --- arch/arm64/Kconfig | 1 + arch/arm64/kernel/stacktrace.c | 76 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 177be0d..72d0747 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -128,6 +128,7 @@ config ARM64 select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES + select HAVE_ARCH_WITHIN_STACK_FRAMES select IOMMU_DMA if IOMMU_SUPPORT select IRQ_DOMAIN select IRQ_FORCED_THREADING diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 33c4028..9918698 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -26,6 +26,11 @@ #include #include +#define FAKE_FRAME(frame, my_func) do { \ + frame.fp = (unsigned long)__builtin_frame_address(0); \ + frame.pc = (unsigned long)my_func; \ +} while (0) + /* * AArch64 PCS assigns the frame pointer to x29. * @@ -99,6 +104,77 @@ void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame, } } +struct check_frame_arg { + unsigned long obj_start; + unsigned long obj_end; + unsigned long frame_start; + int discard_frames; + int err; +}; + +static int check_frame(struct stackframe *frame, void *d) +{ + struct check_frame_arg *arg = d; + unsigned long frame_end = frame->fp; + + /* object overlaps multiple frames */ + if (arg->obj_start < frame->fp && frame->fp < arg->obj_end) { + arg->err = BAD_STACK; + return 1; + } + + /* + * Discard frames and check object is in a frame written early + * enough. + */ + if (arg->discard_frames) + arg->discard_frames--; + else if ((arg->frame_start <= arg->obj_start && + arg->obj_start < frame_end) && + (arg->frame_start < arg->obj_end && arg->obj_end <= frame_end)) + return 1; + + /* object exists in a previous frame */ + if (arg->obj_end < arg->frame_start) { + arg->err = BAD_STACK; + return 1; + } + + arg->frame_start = frame_end + 0x10; + + return 0; +} + +/* Check obj doesn't overlap a stack frame record */ +int arch_within_stack_frames(const void *stack, + const void *stack_end, + const void *obj, unsigned long obj_len) +{ + struct stackframe frame; + struct check_frame_arg arg; + + if (!IS_ENABLED(CONFIG_FRAME_POINTER)) + return NOT_STACK; + + arg.err = GOOD_FRAME; + arg.obj_start = (unsigned long)obj; + arg.obj_end = arg.obj_start + obj_len; + + FAKE_FRAME(frame, arch_within_stack_frames); + arg.frame_start = frame.fp; + + /* + * Skip 4 non-inlined frames: , + * arch_within_stack_frames(), check_stack_object() and + * __check_object_size(). + */ + arg.discard_frames = 4; + + walk_stackframe(current, &frame, check_frame, &arg); + + return arg.err; +} + #ifdef CONFIG_STACKTRACE struct stack_trace_data { struct stack_trace *trace;