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: 9580485 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 AFBA46042F for ; Fri, 17 Feb 2017 18:09:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A145428742 for ; Fri, 17 Feb 2017 18:09:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 95B902875B; Fri, 17 Feb 2017 18:09: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=-4.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, 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 6CD1928742 for ; Fri, 17 Feb 2017 18:09:23 +0000 (UTC) Received: (qmail 31855 invoked by uid 550); 17 Feb 2017 18:09:22 -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 31828 invoked from network); 17 Feb 2017 18:09:21 -0000 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=QwQ+HqZlBtTyc19k8sqkeb/pRJKGowVtMENlJd9Y96+uj5VWnlpi/oZH6Z/XXwZ5Sr Eix52Nlzvm9HSnsgivooE30ycX8APtAMplrQiY8PJBO17K5NF39xcdrhS3pmnpkDoS5l VmXfJg1GdnoHb4Al6CsZ/3xIXAXfU56b4WZvr+2W8cv7Es7SQt/ChCktqkFTxdv+zE9N 8Tr87j7nxEx9WY92+mSXqV3cLHtjSCmApTHVDMC23A4Y14kEStY3CzC+miAKBHqXS3Cq 8UMhWie1XI3IfjY9BP1K9IrzAzUXWnDgLEvH9RQ8N67GDrAhAw/T7x0btpnW+GXRUjSt QLyQ== X-Gm-Message-State: AMke39lHlHhiC/1LDLAEgOfPLB6ttUJb8V0phyCqlCqljZRu6QWRyMyfGnMG5TFK4BVtb37e/Eh1wFaPJgjrNA== 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 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: To: James Morse Cc: kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, Will Deacon , Catalin Marinas , Kees Cook , Mark Rutland , Pratyush Anand , keun-o.park@darkmatter.ae Subject: Re: [kernel-hardening] [PATCH v4 3/3] arm64/uaccess: Add hardened usercopy check for bad stack accesses 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;