From patchwork Thu Feb 16 18:29:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Morse X-Patchwork-Id: 9578041 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 C0DE260589 for ; Thu, 16 Feb 2017 18:30:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B42592865D for ; Thu, 16 Feb 2017 18:30:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A8E0128665; Thu, 16 Feb 2017 18:30:12 +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.2 required=2.0 tests=BAYES_00, 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 AD0122865D for ; Thu, 16 Feb 2017 18:30:07 +0000 (UTC) Received: (qmail 15896 invoked by uid 550); 16 Feb 2017 18:29:58 -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 15817 invoked from network); 16 Feb 2017 18:29:57 -0000 From: James Morse To: kernel-hardening@lists.openwall.com Cc: linux-arm-kernel@lists.infradead.org, Will Deacon , Catalin Marinas , keescook@chromium.org, Mark Rutland , panand@redhat.com, keun-o.park@darkmatter.ae Date: Thu, 16 Feb 2017 18:29:17 +0000 Message-Id: <20170216182917.19637-4-james.morse@arm.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20170216182917.19637-1-james.morse@arm.com> References: <20170216182917.19637-1-james.morse@arm.com> Subject: [kernel-hardening] [PATCH v4 3/3] arm64/uaccess: Add hardened usercopy check for bad stack accesses X-Virus-Scanned: ClamAV using ClamSMTP lkdtm tests copy_{to,from}_user() by trying to copy an address range on the stack that isn't yet part of a stack frame. By the time the stack walker is invoked to check that the object being copied doesn't overlap stack frame, the invalid range is part of a valid stack frame. Discarding a constant number of frames is fragile as different compiler versions may make different inline choices. Instead, add a check that the object isn't between the current stack pointer and the end of the stack. Add this early enough that it should be inlined into the caller. CC: Sahara Signed-off-by: James Morse --- arch/arm64/include/asm/uaccess.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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); +} + 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); @@ -364,6 +380,7 @@ 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); return __arch_copy_from_user(to, from, n); } @@ -371,6 +388,7 @@ static inline unsigned long __must_check __copy_from_user(void *to, const void _ 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); return __arch_copy_to_user(to, from, n); } @@ -381,6 +399,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); res = __arch_copy_from_user(to, from, n); } @@ -394,6 +413,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); n = __arch_copy_to_user(to, from, n); }