From patchwork Sun Feb 5 12:14:09 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: 9556061 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 44F9B602B1 for ; Sun, 5 Feb 2017 12:14:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 367E2269E2 for ; Sun, 5 Feb 2017 12:14:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B1D227F85; Sun, 5 Feb 2017 12:14:45 +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 559CF269E2 for ; Sun, 5 Feb 2017 12:14:44 +0000 (UTC) Received: (qmail 30423 invoked by uid 550); 5 Feb 2017 12:14:41 -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 30341 invoked from network); 5 Feb 2017 12:14:41 -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=vl46Z2tZLhuPnT7jxwGbd9C9R8WenQtZGYFPXzE2VmQ=; b=cIzJJjz4W+C7sdaaTlLlCBAsJuW45IN0eblkF0kKorg0GAAWJwvKR9vGJ0VzUa1ylw xgS3Rm2ZrqN0Xvneiz5u6HUNScISxY+d9KD4cOnc/z618PpXiUbXDzoo2HZJnqJUw+8y wXDngx32nqjBntcC0C400yCV3AJwtrW/iUTL6O0x41vs88ebvUakt7W2WiiaWXBuCo9T qYdYQTi+JHNUBdh8VGP/X5udUT/+9vBx8+hERjmbrAWngjaYPBcFlu7wAgO2z942QRnw xWfk5L3YDZGBxHfaKzN2QkNjWTm0ewLpyGALZ+RTTLQoIrYkPPfDMRjWrVcRn3UlBgbv qxZA== 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=vl46Z2tZLhuPnT7jxwGbd9C9R8WenQtZGYFPXzE2VmQ=; b=eL8DXYXJUE9j4d1mkBw1OOsSBC4zwjVl6j2yAK7/BqwQdxqE8a8pMcJYeyJ3E83LNc x6cMGuBdIAG3TXFh6jDakOh0YSRRmKqaaobbji26iuDPU1DhVE+B7NG9brWZNYAnBx1S F3Vf0M7g69cipy9FGDJ0EJm6M8Zx7m7aZDI9u1pZBTqpx0rhAA1YTGwQjOPHpThSf0iG uNLmynoCpgkU/fiiRt8rlVEJ/bcsgHY+BBkSqQhTKTzpBTVE/lxpBUZG64t+0Ga2J5z4 0AQ8kkZQs+3rf9d7/SGdVk1viPEYjEGGDNUN1WtiIdvDynSG7iMKgNX0rAVdb4kz/n6q P3dA== X-Gm-Message-State: AIkVDXJhslcruGQkEnkQ+aZa3JEyUXzhD1RzjgCEiFitF703GUFJHqgZPArDkESvBm9R3g== X-Received: by 10.25.25.8 with SMTP id 8mr2069911lfz.164.1486296869586; Sun, 05 Feb 2017 04:14:29 -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: Sun, 5 Feb 2017 16:14:09 +0400 Message-Id: <1486296850-16045-2-git-send-email-kpark3469@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1486296850-16045-1-git-send-email-kpark3469@gmail.com> References: <1486296850-16045-1-git-send-email-kpark3469@gmail.com> Subject: [kernel-hardening] [PATCH v3 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 | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 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..70baad3 100644 --- a/arch/arm64/include/asm/thread_info.h +++ b/arch/arm64/include/asm/thread_info.h @@ -68,7 +68,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: