From patchwork Sat Mar 11 00:05:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9618227 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 73CEF6046A for ; Sat, 11 Mar 2017 00:05:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6330A287A9 for ; Sat, 11 Mar 2017 00:05:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 56941287B7; Sat, 11 Mar 2017 00:05:51 +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.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID 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 479A7287A9 for ; Sat, 11 Mar 2017 00:05:50 +0000 (UTC) Received: (qmail 27713 invoked by uid 550); 11 Mar 2017 00:05:35 -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 27651 invoked from network); 11 Mar 2017 00:05:33 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=vFAhjUIcDRwE3Zgnp/kPe+/wjzMjHcgib8dKfVVgM3s=; b=oxMvlL4wlX/A4nvnfmpw6oKeYjuND694zqVHH9zEShAhjKnQp7uxGpuqIWdfFgbbRC 72HqAihDePenuDu5KWhi9iuY8JfjaeqABFTltsCVC+2XDlSvAgdsDDp+yxSgIPA8oa3k DECbpNeJuAntQuau+YAiicR1ERZCg30zNE4GCWfhF3xTYi97MwgD8wzx5rBv2WshiluG V1egjGkFHIKE4sBhmMlHJJ4AfHrzQt83ulE2UuZBm4gtF2TYeyjS0oeqWTqboOXrz4rr sL5K8HosmIEQeBBMCCFB27Pl5QghDkwKTWBsVrdoahUEVlrEDmrhv3JsY3KOM5dk7b9v P+Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vFAhjUIcDRwE3Zgnp/kPe+/wjzMjHcgib8dKfVVgM3s=; b=IPNXP+u2GkVLDLQrZwQ5OooPAhD886i9SRICD57K0mqKZviweghV9uDwmCEfPf+sEn GvMBDUkhGb+tHV2r5DemzFwx5sOJsv1OUoULBdl+exVXeOQf/Q6RXIn2k5qRjbotR8hi mLmYHf9y8T8Z8j5WyCEFtjfPS9skX45uWwTXe8PIwOmuBq/d4e20Vbr3w70BIsNV8IZb lav2kH9RuQpycHCgE59oie4MgW3cwVf8w3zbKtTG+tdnvYO0wPEaht3H3ZAhiwSVnXSy WWTVHjZhyyU3ghLfPVgIiCXIdWbfbXZ030taisMeQxoGtR+DSJmxymTqzI33myL6P2OH HwCQ== X-Gm-Message-State: AMke39nVSLstfxepQ8npr7Oq8RydTPYPWs9okOSCKRFrdsXs3FWYd0ELiH229Ate0CWwVqVC X-Received: by 10.98.7.86 with SMTP id b83mr24021389pfd.66.1489190721727; Fri, 10 Mar 2017 16:05:21 -0800 (PST) From: Thomas Garnier To: Martin Schwidefsky , Heiko Carstens , David Howells , Arnd Bergmann , Al Viro , Dave Hansen , Thomas Garnier , =?UTF-8?q?Ren=C3=A9=20Nyffenegger?= , Andrew Morton , Kees Cook , "Paul E . McKenney" , Andy Lutomirski , Ard Biesheuvel , Nicolas Pitre , Petr Mladek , Sebastian Andrzej Siewior , Sergey Senozhatsky , Helge Deller , Rik van Riel , Ingo Molnar , John Stultz , Thomas Gleixner , Oleg Nesterov , Stephen Smalley , Pavel Tikhomirov , Frederic Weisbecker , Stanislav Kinsburskiy , Ingo Molnar , "H . Peter Anvin" , Paolo Bonzini , Dmitry Safonov , Borislav Petkov , Josh Poimboeuf , Brian Gerst , Jan Beulich , Christian Borntraeger , Fenghua Yu , He Chen , Russell King , Vladimir Murzin , Will Deacon , Catalin Marinas , Mark Rutland , James Morse , "David A . Long" , Pratyush Anand , Laura Abbott , Andre Przywara , Chris Metcalf Cc: linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, kernel-hardening@lists.openwall.com Date: Fri, 10 Mar 2017 16:05:00 -0800 Message-Id: <20170311000501.46607-3-thgarnie@google.com> X-Mailer: git-send-email 2.12.0.246.ga2ecc84866-goog In-Reply-To: <20170311000501.46607-1-thgarnie@google.com> References: <20170311000501.46607-1-thgarnie@google.com> Subject: [kernel-hardening] [PATCH v3 3/4] arm/syscalls: Specific usage of verify_pre_usermode_state X-Virus-Scanned: ClamAV using ClamSMTP Implement specific usage of verify_pre_usermode_state for user-mode returns for arm. --- Based on next-20170308 --- arch/arm/Kconfig | 1 + arch/arm/include/asm/domain.h | 3 +++ arch/arm/kernel/entry-common.S | 32 +++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 0d4e71b42c77..704fd8f197fa 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -12,6 +12,7 @@ config ARM select ARCH_HAVE_CUSTOM_GPIO_H select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_MIGHT_HAVE_PC_PARPORT + select ARCH_NO_SYSCALL_VERIFY_PRE_USERMODE_STATE select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWX select ARCH_OPTIONAL_KERNEL_RWX_DEFAULT if CPU_V7 select ARCH_SUPPORTS_ATOMIC_RMW diff --git a/arch/arm/include/asm/domain.h b/arch/arm/include/asm/domain.h index 99d9f630d6b6..fd1cd24ca9fe 100644 --- a/arch/arm/include/asm/domain.h +++ b/arch/arm/include/asm/domain.h @@ -58,6 +58,9 @@ #define domain_mask(dom) ((3) << (2 * (dom))) #define domain_val(dom,type) ((type) << (2 * (dom))) +#define DOMAIN_KERNEL_MASK domain_mask(DOMAIN_KERNEL) +#define DOMAIN_CLIENT_VALUE domain_val(DOMAIN_KERNEL, DOMAIN_CLIENT) + #ifdef CONFIG_CPU_SW_DOMAIN_PAN #define DACR_INIT \ (domain_val(DOMAIN_USER, DOMAIN_NOACCESS) | \ diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S index eb5cd77bf1d8..8d9bfe9ff313 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef CONFIG_AEABI #include #endif @@ -27,7 +28,6 @@ #include "entry-header.S" - .align 5 #if !(IS_ENABLED(CONFIG_TRACE_IRQFLAGS) || IS_ENABLED(CONFIG_CONTEXT_TRACKING)) /* @@ -40,9 +40,12 @@ ret_fast_syscall: UNWIND(.fnstart ) UNWIND(.cantunwind ) disable_irq_notrace @ disable interrupts + ldr r2, [tsk, #TI_ADDR_LIMIT] ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK bne fast_work_pending + cmp r2, #TASK_SIZE + blne addr_limit_fail /* perform architecture specific actions before user return */ arch_ret_to_user r1, lr @@ -66,6 +69,7 @@ ret_fast_syscall: UNWIND(.cantunwind ) str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 disable_irq_notrace @ disable interrupts + ldr r2, [tsk, #TI_ADDR_LIMIT] ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK beq no_work_pending @@ -82,6 +86,7 @@ slow_work_pending: mov r2, why @ 'syscall' bl do_work_pending cmp r0, #0 + ldreq r2, [tsk, #TI_ADDR_LIMIT] beq no_work_pending movlt scno, #(__NR_restart_syscall - __NR_SYSCALL_BASE) ldmia sp, {r0 - r6} @ have to reload r0 - r6 @@ -99,9 +104,12 @@ ret_slow_syscall: disable_irq_notrace @ disable interrupts ENTRY(ret_to_user_from_irq) ldr r1, [tsk, #TI_FLAGS] + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_WORK_MASK bne slow_work_pending no_work_pending: + cmp r2, #TASK_SIZE + blne addr_limit_fail asm_trace_hardirqs_on save = 0 /* perform architecture specific actions before user return */ @@ -125,6 +133,28 @@ ENTRY(ret_from_fork) b ret_slow_syscall ENDPROC(ret_from_fork) +addr_limit_fail: +#ifdef CONFIG_BUG_ON_DATA_CORRUPTION + stmfd sp!, {r0, lr} + bl verify_pre_usermode_state + ldmfd sp!, {r0, lr} +#else + /* + * Similar to set_fs(USER_DS) in verify_pre_usermode_state without a + * warning. + */ + mov r2, #TASK_SIZE + str r2, [tsk, #TI_ADDR_LIMIT] +#ifdef CONFIG_CPU_USE_DOMAINS + /* Switch domain like modify_domain(DOMAIN_KERNEL, DOMAIN_CLIENT) */ + mrc p15, 0, r2, c3, c0, 0 @ Get domain register + bic r2, r2, #DOMAIN_KERNEL_MASK @ Clear the domain part + orr r2, r2, #DOMAIN_CLIENT_VALUE @ Enforce DOMAIN_CLIENT + mcr p15, 0, r2, c3, c0, 0 @ Set domain register +#endif +#endif + ret lr + /*============================================================================= * SWI handler *-----------------------------------------------------------------------------