From patchwork Fri Feb 17 18:09:08 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: 9580487 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 15B446042F for ; Fri, 17 Feb 2017 18:09:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 05CBE28742 for ; Fri, 17 Feb 2017 18:09:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB88A2875B; Fri, 17 Feb 2017 18:09:36 +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=-1.9 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5B8F628742 for ; Fri, 17 Feb 2017 18:09:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From: References:In-Reply-To:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Bvoo3lV7lDlh9yHQd3nlj4A5oS2Vhf0ZB960M+lPh+4=; b=RjL+kkY+WzQQo2 DSy+CCfZRIUlGhpBbQvbIg7yklLIqIe23eBAIUrM8FcW2ikAiONMbJovsqAU0C8xc7f2Ool70p2p9 iFCjxV21DAcz3E+yBByLTHCBE+RbUr+8s+VbJC94kQ40ZlZjEzpqByYQire+S9Eh37t7SP9kBw4fS I0wE14OyBaTNvtmtJOtvPifMxGxWXwmj5dra4S44W+KX/lmKWntdgJRd0XfDghuzlufAo64sY+AGw uLPdnqPUwbEWpzHzfv40IU5eTpXKeUhkSNCKOYKOI0UgjUkmMy9apCUQABHFs/UQ3MxFxuMnyfhHY EnkDePEtS78xNx5PUxEw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cemyN-0003k1-F6; Fri, 17 Feb 2017 18:09:35 +0000 Received: from mail-yw0-x244.google.com ([2607:f8b0:4002:c05::244]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cemyI-0003hb-S0 for linux-arm-kernel@lists.infradead.org; Fri, 17 Feb 2017 18:09:32 +0000 Received: by mail-yw0-x244.google.com with SMTP id l16so3796489ywb.2 for ; Fri, 17 Feb 2017 10:09:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=YKSRqErxi+pDfzdHLN9DSvs0Mc21XBmZTLhYJ1NUieM=; b=EiMRgNJtKYv5+XjAXFpnvRamn7ZZLCooGTTUinlD9VdDufkybzmbbgO/orELPyTRxY th+2d1jO23/4Q5rRHOlahZhAOHdQBii0AxbZ5SjqG3Eskw1Mja9MvMl96W/mLGPpapB1 pdQwdDFx0XzXGoL6f1Qiqw5YT3BvpfiLdhr7AFtXdlzPwAsvkjZfSxPAJDGEY5nbdu+i 0J74k0wxuqM5kJMTUQu3Q6EegxcWHxbA+D8YvYpqk9Ud72tx7U35yvpu0M88IHWzgMWL D/leMsN9AsH4ZcuJIWUD5zenC1voeOwdDjgBDSPSMaDL9P/fyHtgOX8TanzDZUj296MK 2xhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=YKSRqErxi+pDfzdHLN9DSvs0Mc21XBmZTLhYJ1NUieM=; b=ABktVDSBL+JDX8HGNo0ik4nb77pFbgxe9rzhQhi9jOB7uEhwaj/P9QWsi4BbSFBrYF sqxNmNz7OWvymbpjanF5+s/nkrCpp2ltJZPMhEXyfXshnwr5Kn/iPrGar9Kx7irZpEAq AppLgdUT/1N1plLizyKfwUmOevLiX2t3jgdc7Uhxa8HHEK3EuIoLdpXQaVoCNwiX3gu0 5bG8qPueqFfw6cmr3dxamVNV2mesjkYWTtYKbqR0pdxTwSNIAenyZ4KVA+2Ugyn6vYgN bz7DegQEMWtd8TTOKyScpjgaWVChitzs6xf2k5jGPur1oBb8W1E3lDcJg16ZwXubdsYt rcJA== X-Gm-Message-State: AMke39lLAdH+kTU2laJktjqWL/OwZHsyxMoYsfTSBvbnuwHQUEHDmUMZy/hvzEU/GmLX43Flx9FWFxerrzXrvw== X-Received: by 10.129.84.68 with SMTP id i65mr6620570ywb.38.1487354949240; Fri, 17 Feb 2017 10:09:09 -0800 (PST) MIME-Version: 1.0 Received: by 10.37.13.206 with HTTP; Fri, 17 Feb 2017 10:09:08 -0800 (PST) In-Reply-To: <20170216182917.19637-4-james.morse@arm.com> References: <20170216182917.19637-1-james.morse@arm.com> <20170216182917.19637-4-james.morse@arm.com> From: Keun-O Park Date: Fri, 17 Feb 2017 22:09:08 +0400 Message-ID: Subject: Re: [kernel-hardening] [PATCH v4 3/3] arm64/uaccess: Add hardened usercopy check for bad stack accesses To: James Morse X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170217_100931_039997_9C4A7136 X-CRM114-Status: GOOD ( 17.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Pratyush Anand , Kees Cook , kernel-hardening@lists.openwall.com, Catalin Marinas , Will Deacon , keun-o.park@darkmatter.ae, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Hello James, > diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h > index 46da3ea638bb..d3494840a61c 100644 > --- a/arch/arm64/include/asm/uaccess.h > +++ b/arch/arm64/include/asm/uaccess.h > @@ -356,6 +356,22 @@ do { \ > -EFAULT; \ > }) > > +static inline void check_obj_in_unused_stack(const void *obj, unsigned long len) > +{ > + unsigned long stack = (unsigned long)task_stack_page(current); > + > + if (__builtin_constant_p(len) || !IS_ENABLED(CONFIG_HARDENED_USERCOPY) || !len) > + return; > + > + /* > + * If current_stack_pointer is on the task stack, obj must not lie > + * between current_stack_pointer and the last stack address. > + */ > + if ((current_stack_pointer & ~(THREAD_SIZE-1)) == stack) > + BUG_ON(stack <= (unsigned long)obj && > + (unsigned long)obj < current_stack_pointer); > +} > + It looks to me that this function is just doing the similar check that check_stack_object() may do. Probably I guess you had a problem in checking the correct fp of caller's function while you tried to use walk_stackframe(). Anyway, your patch works fine for me. But, I can not find any reason to create check_obj_in_unused_stack(). Instead of creating check_obj_in_unused_stack(), how about handing over current_stack_pointer to check_stack_object(); Even though Akashi's case happens, since we hand over correct stack pointer, it can check if the obj is within the correct stack boundary. /* Check if object is safely within a valid frame. */ @@ -227,7 +229,8 @@ static inline const char *check_heap_object(const void *ptr, unsigned long n, * - known-safe heap or stack object * - not in kernel text */ -void __check_object_size(const void *ptr, unsigned long n, bool to_user) +void __check_object_size(const void *ptr, unsigned long n, + unsigned long sp, bool to_user) { const char *err; @@ -246,7 +249,7 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user) goto report; /* Check for bad stack object. */ - switch (check_stack_object(ptr, n)) { + switch (check_stack_object(ptr, n, sp)) { case NOT_STACK: /* Object is not touching the current process stack. */ break; Thanks so much for your patches. BR Sahara diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index d349484..0c21a0d 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -356,22 +356,6 @@ do { \ -EFAULT; \ }) -static inline void check_obj_in_unused_stack(const void *obj, unsigned long len) -{ - unsigned long stack = (unsigned long)task_stack_page(current); - - if (__builtin_constant_p(len) || !IS_ENABLED(CONFIG_HARDENED_USERCOPY) || !len) - return; - - /* - * If current_stack_pointer is on the task stack, obj must not lie - * between current_stack_pointer and the last stack address. - */ - if ((current_stack_pointer & ~(THREAD_SIZE-1)) == stack) - BUG_ON(stack <= (unsigned long)obj && - (unsigned long)obj < current_stack_pointer); -} - extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n); extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n); extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n); @@ -380,16 +364,14 @@ extern unsigned long __must_check __clear_user(void __user *addr, unsigned long static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n) { kasan_check_write(to, n); - check_obj_in_unused_stack(to, n); - check_object_size(to, n, false); + check_object_size(to, n, current_stack_pointer, false); return __arch_copy_from_user(to, from, n); } static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n) { kasan_check_read(from, n); - check_obj_in_unused_stack(from, n); - check_object_size(from, n, true); + check_object_size(from, n, current_stack_pointer, true); return __arch_copy_to_user(to, from, n); } @@ -399,8 +381,7 @@ static inline unsigned long __must_check copy_from_user(void *to, const void __u kasan_check_write(to, n); if (access_ok(VERIFY_READ, from, n)) { - check_obj_in_unused_stack(to, n); - check_object_size(to, n, false); + check_object_size(to, n, current_stack_pointer, false); res = __arch_copy_from_user(to, from, n); } if (unlikely(res)) @@ -413,8 +394,7 @@ static inline unsigned long __must_check copy_to_user(void __user *to, const voi kasan_check_read(from, n); if (access_ok(VERIFY_WRITE, to, n)) { - check_obj_in_unused_stack(from, n); - check_object_size(from, n, true); + check_object_size(from, n, current_stack_pointer, true); n = __arch_copy_to_user(to, from, n); } return n; diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h index a38b3be..5c676bc 100644 --- a/include/linux/thread_info.h +++ b/include/linux/thread_info.h @@ -94,17 +94,17 @@ static inline enum stack_type arch_within_stack_frames(const void * const stack, #ifdef CONFIG_HARDENED_USERCOPY extern void __check_object_size(const void *ptr, unsigned long n, - bool to_user); + unsigned long sp, bool to_user); static __always_inline void check_object_size(const void *ptr, unsigned long n, - bool to_user) + unsigned long sp, bool to_user) { if (!__builtin_constant_p(n)) - __check_object_size(ptr, n, to_user); + __check_object_size(ptr, n, sp, to_user); } #else static inline void check_object_size(const void *ptr, unsigned long n, - bool to_user) + unsigned long sp, bool to_user) { } #endif /* CONFIG_HARDENED_USERCOPY */ diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c index 7e35fc4..4118da4 100644 --- a/lib/strncpy_from_user.c +++ b/lib/strncpy_from_user.c @@ -112,7 +112,7 @@ long strncpy_from_user(char *dst, const char __user *src, long count) long retval; kasan_check_write(dst, count); - check_object_size(dst, count, false); + check_object_size(dst, count, current_stack_pointer, false); user_access_begin(); retval = do_strncpy_from_user(dst, src, count, max); user_access_end(); diff --git a/mm/usercopy.c b/mm/usercopy.c index 3531ae7..3739022 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -29,7 +29,8 @@ * GOOD_STACK: fully on the stack (when can't do frame-checking) * BAD_STACK: error condition (invalid stack position or bad stack frame) */ -static noinline int check_stack_object(const void *obj, unsigned long len) +static noinline int check_stack_object(const void *obj, unsigned long len, + unsigned long usercopy_sp) { const void * const stack = task_stack_page(current); const void * const stackend = stack + THREAD_SIZE; @@ -44,7 +45,7 @@ static noinline int check_stack_object(const void *obj, unsigned long len) * the check above means at least one end is within the stack, * so if this check fails, the other end is outside the stack). */ - if (obj < stack || stackend < obj + len) + if (obj < usercopy_sp || stackend < obj + len) return BAD_STACK;