From patchwork Thu Oct 26 09:09:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 10027869 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 C416D6022E for ; Thu, 26 Oct 2017 09:10:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B98FD28CDD for ; Thu, 26 Oct 2017 09:10:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE55828D7F; Thu, 26 Oct 2017 09:10:24 +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 CF03A28D79 for ; Thu, 26 Oct 2017 09:10:23 +0000 (UTC) Received: (qmail 29730 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 28559 invoked from network); 26 Oct 2017 09:10:07 -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:42 +0100 Message-Id: <20171026090942.7041-3-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 2/2] arm64: allow paranoid __{get, put}user X-Virus-Scanned: ClamAV using ClamSMTP Now that the compiler can identify redundant access_ok() checks, we can make __get-user() and __put_user() BUG()-out if there wasn't a preceding access_ok() check. So long as that's in the same compilation unit, the compiler should be able to get rid of the redundant second check and BUG entry. This will allow us to catch __{get,put}_user() calls which did not have a preceding access_ok() check, but may adversely affect a small number of callsites where GCC fails to spot that it can fold two access_ok() checks together. As these checks may impact performance and code size, they are only enabled when CONFIG_ARM64_PARANOID_UACCESS is selected. In testing with v4.14-rc5 with the Linaro 17.05 GCC 6.3.1 toolchain, this makes the kernel Image ~4KiB bigger, and the vmlinux ~93k bigger. I have no performance numbers so far. Signed-off-by: Mark Rutland Cc: Catalin Marinas Cc: Kees Cook Cc: Laura Abbott Cc: Will Deacon --- arch/arm64/Kconfig | 9 +++++++++ arch/arm64/include/asm/uaccess.h | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 0df64a6a56d4..34df81acda8e 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -1028,6 +1028,15 @@ config RANDOMIZE_MODULE_REGION_FULL a limited range that contains the [_stext, _etext] interval of the core kernel, so branch relocations are always in range. +config ARM64_PARANOID_UACCESS + bool "Use paranoid uaccess primitives" + help + Forces access_ok() checks in __get_user(), __put_user(), and other + low-level uaccess primitives which usually do not have checks. This + can limit the effect of missing access_ok() checks in higher-level + primitives, with a runtime performance overhead in some cases and a + small code size overhead. + endmenu menu "Boot options" diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 36f84ec92b9d..dbe8dfd46ceb 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -195,6 +195,12 @@ static inline void uaccess_enable_not_uao(void) __uaccess_enable(ARM64_ALT_PAN_NOT_UAO); } +#define verify_uaccess(dir, ptr) \ +({ \ + if (IS_ENABLED(CONFIG_ARM64_PARANOID_UACCESS)) \ + BUG_ON(!access_ok(dir, (ptr), sizeof(*(ptr)))); \ +}) + /* * The "__xxx" versions of the user access functions do not verify the address * space - it must have been done previously with a separate "access_ok()" @@ -222,6 +228,7 @@ static inline void uaccess_enable_not_uao(void) do { \ unsigned long __gu_val; \ __chk_user_ptr(ptr); \ + verify_uaccess(VERIFY_READ, ptr); \ uaccess_enable_not_uao(); \ switch (sizeof(*(ptr))) { \ case 1: \ @@ -287,6 +294,7 @@ do { \ do { \ __typeof__(*(ptr)) __pu_val = (x); \ __chk_user_ptr(ptr); \ + verify_uaccess(VERIFY_WRITE, ptr); \ uaccess_enable_not_uao(); \ switch (sizeof(*(ptr))) { \ case 1: \