From patchwork Thu Oct 26 09:09:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 10027863 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 689D66022E for ; Thu, 26 Oct 2017 09:10:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5942528D82 for ; Thu, 26 Oct 2017 09:10:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4D63628D37; Thu, 26 Oct 2017 09:10:18 +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 559AC28D7A for ; Thu, 26 Oct 2017 09:10:17 +0000 (UTC) Received: (qmail 28587 invoked by uid 550); 26 Oct 2017 09:10:08 -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 28450 invoked from network); 26 Oct 2017 09:10:04 -0000 From: Mark Rutland To: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, Mark Rutland , Catalin Marinas , Kees Cook , Laura Abbott , Will Deacon Date: Thu, 26 Oct 2017 10:09:41 +0100 Message-Id: <20171026090942.7041-2-mark.rutland@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171026090942.7041-1-mark.rutland@arm.com> References: <20171026090942.7041-1-mark.rutland@arm.com> Subject: [kernel-hardening] [RFC PATCH 1/2] arm64: write __range_ok() in C X-Virus-Scanned: ClamAV using ClamSMTP Currently arm64's __range_ok() is written in assembly for efficiency. This hides the logic from the compiler, preventing the compiler from making some optimizations, such as re-ordering instructions or folding multiple calls to __range_ok(). This patch uses GCC's __builtin_uaddl_overflow() to provide an equivalent, efficient check, while giving the compiler the visibility it needs to optimize the check. In testing with v4.14-rc5 using the Linaro 17.05 GCC 6.3.1 toolchain, this has no impact on the kernel Image size, (but results in a smaller vmlinux). Signed-off-by: Mark Rutland Cc: Catalin Marinas Cc: Kees Cook Cc: Laura Abbott Cc: Will Deacon --- arch/arm64/include/asm/uaccess.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index fc0f9eb66039..36f84ec92b9d 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -70,17 +70,20 @@ static inline void set_fs(mm_segment_t fs) * * This needs 65-bit arithmetic. */ +static bool __range_ok_c(unsigned long addr, unsigned long size) +{ + unsigned long result; + + if (__builtin_uaddl_overflow(addr, size, &result)) + return false; + + return result < current_thread_info()->addr_limit; +} + #define __range_ok(addr, size) \ ({ \ - unsigned long __addr = (unsigned long)(addr); \ - unsigned long flag, roksum; \ __chk_user_ptr(addr); \ - asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \ - : "=&r" (flag), "=&r" (roksum) \ - : "1" (__addr), "Ir" (size), \ - "r" (current_thread_info()->addr_limit) \ - : "cc"); \ - flag; \ + __range_ok_c((unsigned long)(addr), (unsigned long)(size)); \ }) /*