From patchwork Thu Feb 2 12:50:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: kpark3469@gmail.com X-Patchwork-Id: 9551751 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 E3B8060236 for ; Thu, 2 Feb 2017 12:51:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CDDD728329 for ; Thu, 2 Feb 2017 12:51:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C26352839A; Thu, 2 Feb 2017 12:51:00 +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.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID 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 EA68E28329 for ; Thu, 2 Feb 2017 12:50:59 +0000 (UTC) Received: (qmail 13925 invoked by uid 550); 2 Feb 2017 12:50:57 -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 13898 invoked from network); 2 Feb 2017 12:50:57 -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=f80E9EUUohCO7e/04oWHgsWOOF6cy2rvKCTDdbMncDo=; b=Ai1rR89LQKv5/ZdMZFUWQ9lLj669MGft1IwKyDfMSaxQcrIR5Vp6Qeou3b//BcI+CP KmDqfZGoPzHeWIXTtQcTEzZwKsdYrfUqfKkqOgwj3mDU7LjW0uVfsFYABVHD075TYEDh g5ShuOLv2OrJExqOHlRt865/2EkTsKde9s5q9z8MTdYunMDDtJr/zr4AdXgNrTwxkKP0 k0sHPCM3WsRUD6xMhvyPq7YOpPB4WV1nQKbCsa8s1XxpO2cIRWnWS121RrsoFuhRJIJc ijC/bzkFaDxkYei/j0VzMgxWcWTcntoujknSIV5QddUtQnXxEnEUYvsSJ19mZIId7DAl Ui5g== 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=f80E9EUUohCO7e/04oWHgsWOOF6cy2rvKCTDdbMncDo=; b=Imo+tWE0K/wnAyHqRkXB83ria++fRKVPvgzvfrMlRZkoEUZNuUXwz0D4bA3x+pp0Yy ZCWNXdpeXPV7lJ4dfWdfNknfr/n0sblhxU+IQu6N80gzTyIr/ek8QzrmWS1eIW0nOfLS LJBoUMlMd3lbSOn0esZk42W/sPDw2+dBFDjMA5FPOPUJhMPY8yBHWepEHZsRSFcj2SCf fdodeVHe+ZC3f4Y6Jo95fPhz9TXvaMTWUnB6Ifhulmf+TNurelCc4o4VKbVenqZo6vjA H1hi381mTuNyKdyNi85/sk7z3sw0lu5jrVGujNZlE29pYoZCFejOm0YbIP7uwuCmVBiQ MPSQ== X-Gm-Message-State: AIkVDXIm4pknvQdLDoZndLjdvqcdxzBX1DInsPKzz0Y2WC18iIsera5JepZhHahc2DGRXA== X-Received: by 10.28.127.13 with SMTP id a13mr7261028wmd.96.1486039845540; Thu, 02 Feb 2017 04:50:45 -0800 (PST) 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 Date: Thu, 2 Feb 2017 16:50:23 +0400 Message-Id: <1486039824-8470-2-git-send-email-kpark3469@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1486039824-8470-1-git-send-email-kpark3469@gmail.com> References: <1486039824-8470-1-git-send-email-kpark3469@gmail.com> Subject: [kernel-hardening] [PATCH v2 2/3] arm64: usercopy: Implement stack frame object validation X-Virus-Scanned: ClamAV using ClamSMTP From: Sahara 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: Sahara Reviewed-by: James Morse --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/thread_info.h | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 1117421..8bf70b4 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -97,6 +97,7 @@ config ARM64 select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES if HAVE_KPROBES + select HAVE_ARCH_WITHIN_STACK_FRAMES select IOMMU_DMA if IOMMU_SUPPORT select IRQ_DOMAIN select IRQ_FORCED_THREADING diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h index 46c3b93..4ca7a0b 100644 --- a/arch/arm64/include/asm/thread_info.h +++ b/arch/arm64/include/asm/thread_info.h @@ -36,6 +36,7 @@ struct task_struct; +#include #include #include @@ -68,7 +69,71 @@ struct thread_info { #define thread_saved_fp(tsk) \ ((unsigned long)(tsk->thread.cpu_context.fp)) +#define get_stack_start(fp) (fp + 2 * sizeof(void *)) + +/* + * 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 (no frame pointers, etc) + */ + +static inline enum stack_type arch_within_stack_frames(const void * const stack, + const void * const stackend, + const void *obj, unsigned long len) +{ +#ifdef CONFIG_FRAME_POINTER + const void *callee_fp = NULL; + const void *caller_fp = NULL; + + callee_fp = __builtin_frame_address(1); + if (callee_fp) + caller_fp = *(const void * const *)callee_fp; + /* + * Case #1: + * low ----------------------------------------------> high + * [callee_fp][lr][args][local vars][caller_fp'][lr'] + * ^----------------^ + * allow copies only within here + * + * Case #2: + * low ----------------------------------------------> high + * [check_object_size_fp][lr][args][local vars][callee_fp][lr] + * ^----------------^ + * dynamically allocated stack variable of + * callee frame copies are allowed within here + * + * < example code snippet for Case#2 > + * array_size = get_random_int() & 0x0f; + * if (to_user) { + * unsigned char array[array_size]; + * if (copy_to_user((void __user *)user_addr, array, + * unconst + sizeof(array))) { + */ + while (stack <= callee_fp && callee_fp < stackend && + !((unsigned long)caller_fp & 0xf)) { + /* + * If obj + len extends past the caller 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 (!caller_fp || (obj + len <= caller_fp)) + return (obj >= get_stack_start(callee_fp)) ? + GOOD_FRAME : BAD_STACK; + callee_fp = caller_fp; + caller_fp = *(const void * const *)caller_fp; + } + return BAD_STACK; +#else + return NOT_STACK; #endif +} + +#endif /* !__ASSEMBLY__ */ /* * thread information flags: