From patchwork Thu Jun 15 01:12:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9787779 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 27CEB60231 for ; Thu, 15 Jun 2017 01:12:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1BF59284F8 for ; Thu, 15 Jun 2017 01:12:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0ED4228517; Thu, 15 Jun 2017 01:12:34 +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 EB8E5284F8 for ; Thu, 15 Jun 2017 01:12:32 +0000 (UTC) Received: (qmail 15704 invoked by uid 550); 15 Jun 2017 01:12:24 -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 15674 invoked from network); 15 Jun 2017 01:12:22 -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=gT6fkRgqcsnNcp777akjLPBUVgwFC5bxVtKaSXNYc9U=; b=bGNckVdUoMzvWts2YEA9yZ90WWxnpWUNGjPQvqwidps0g6j9H59FrZFrtLtTGHOnAy dnKh3PHeJTMHOlqzXlMZBaR7GbtcqzZkHh/9rbP7Vkkl4znPJD3y4Q/LtVXQcAXE7Dgq 3qjNZNzjuG7cpjiSaGaIsQ6LeIQjAhessSb6rd31nRB4LsGhS/MQxBbrOl+NciBEjJOc uMczu3L54IBKevSKJy/BVBIdhhMQSUW/FUJggXsR2HbJl5bfZazcxuz7QC3b0JBreXio ih6mrxSIJFrXMKD+xNjaqX2cNGsdhNEstNCu2umbBKs9sEaAO53z4yr6YOine5xtJUdI QLLA== 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=gT6fkRgqcsnNcp777akjLPBUVgwFC5bxVtKaSXNYc9U=; b=I9XQwBLTDaMLtIXwri+GDk5iVLMXkkc55Mcj8YE7hB14YV3vyZ9WCrAjg2/KvuXoOF PoRkycC2hCz6kENy99ixkpSLzM8j0ZdOhpFj48YtYLJleJstoHafobIEOk6i6/5ql4ZM IjsmEDktirrJHKxyrd5FXFnLR8snxVuQIfVhvw2IB+lacHLG6rbowCEenVdYBcWKKm7R /VdzylHutMB/CikjvMWOLcfXutq+DykFVbpuJbvQ7J6569g95V/Opviqfbt+ML5pGc+J 9fMvwwmD+ZPVTGVsJdWJ25LAGfedevFnx2OJwYgiFMABvdTX52EFsoAI7G9oN++lqaDJ gpJg== X-Gm-Message-State: AKS2vOwg8NFtm6SpYYmW5FO7qhOahoXR5kVC0CQ4abv03xo5s/zwtOZX HZy4pHsZNl86ZkHL X-Received: by 10.98.50.129 with SMTP id y123mr2630384pfy.53.1497489130450; Wed, 14 Jun 2017 18:12:10 -0700 (PDT) From: Thomas Garnier To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Andy Lutomirski , Paolo Bonzini , Rik van Riel , Oleg Nesterov , Josh Poimboeuf , Thomas Garnier , Petr Mladek , Miroslav Benes , Kees Cook , Al Viro , Arnd Bergmann , Dave Hansen , David Howells , Russell King , Andy Lutomirski , Will Drewry , Will Deacon , Catalin Marinas , Mark Rutland , Pratyush Anand , Chris Metcalf Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel-hardening@lists.openwall.com Date: Wed, 14 Jun 2017 18:12:02 -0700 Message-Id: <20170615011203.144108-2-thgarnie@google.com> X-Mailer: git-send-email 2.13.1.518.g3df882009-goog In-Reply-To: <20170615011203.144108-1-thgarnie@google.com> References: <20170615011203.144108-1-thgarnie@google.com> Subject: [kernel-hardening] [PATCH v10 2/3] arm/syscalls: Check address limit on user-mode return X-Virus-Scanned: ClamAV using ClamSMTP Ensure the address limit is a user-mode segment before returning to user-mode. Otherwise a process can corrupt kernel-mode memory and elevate privileges [1]. The set_fs function sets the TIF_SETFS flag to force a slow path on return. In the slow path, the address limit is checked to be USER_DS if needed. The TIF_SETFS flag is added to _TIF_WORK_MASK shifting _TIF_SYSCALL_WORK for arm instruction immediate support. The global work mask is too big to used on a single instruction so adapt ret_fast_syscall. [1] https://bugs.chromium.org/p/project-zero/issues/detail?id=990 Signed-off-by: Thomas Garnier --- v10 redesigns the change to use work flags on set_fs as recommended by Linus and agreed by others. Based on next-20170609 --- arch/arm/include/asm/thread_info.h | 15 +++++++++------ arch/arm/include/asm/uaccess.h | 2 ++ arch/arm/kernel/entry-common.S | 9 +++++++-- arch/arm/kernel/signal.c | 5 +++++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h index 776757d1604a..1d468b527b7b 100644 --- a/arch/arm/include/asm/thread_info.h +++ b/arch/arm/include/asm/thread_info.h @@ -139,10 +139,11 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *, #define TIF_NEED_RESCHED 1 /* rescheduling necessary */ #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */ #define TIF_UPROBE 3 /* breakpointed or singlestepping */ -#define TIF_SYSCALL_TRACE 4 /* syscall trace active */ -#define TIF_SYSCALL_AUDIT 5 /* syscall auditing active */ -#define TIF_SYSCALL_TRACEPOINT 6 /* syscall tracepoint instrumentation */ -#define TIF_SECCOMP 7 /* seccomp syscall filtering active */ +#define TIF_FSCHECK 4 /* Check FS is USER_DS on return */ +#define TIF_SYSCALL_TRACE 5 /* syscall trace active */ +#define TIF_SYSCALL_AUDIT 6 /* syscall auditing active */ +#define TIF_SYSCALL_TRACEPOINT 7 /* syscall tracepoint instrumentation */ +#define TIF_SECCOMP 8 /* seccomp syscall filtering active */ #define TIF_NOHZ 12 /* in adaptive nohz mode */ #define TIF_USING_IWMMXT 17 @@ -153,6 +154,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *, #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED) #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME) #define _TIF_UPROBE (1 << TIF_UPROBE) +#define _TIF_FSCHECK (1 << TIF_FSCHECK) #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE) #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT) #define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT) @@ -166,8 +168,9 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *, /* * Change these and you break ASM code in entry-common.S */ -#define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \ - _TIF_NOTIFY_RESUME | _TIF_UPROBE) +#define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \ + _TIF_NOTIFY_RESUME | _TIF_UPROBE | \ + _TIF_FSCHECK) #endif /* __KERNEL__ */ #endif /* __ASM_ARM_THREAD_INFO_H */ diff --git a/arch/arm/include/asm/uaccess.h b/arch/arm/include/asm/uaccess.h index 2577405d082d..6cc882223e34 100644 --- a/arch/arm/include/asm/uaccess.h +++ b/arch/arm/include/asm/uaccess.h @@ -77,6 +77,8 @@ static inline void set_fs(mm_segment_t fs) { current_thread_info()->addr_limit = fs; modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER); + /* On user-mode return, check fs is correct */ + set_thread_flag(TIF_FSCHECK); } #define segment_eq(a, b) ((a) == (b)) diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S index eb5cd77bf1d8..e33c32d56193 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -41,7 +41,9 @@ ret_fast_syscall: UNWIND(.cantunwind ) disable_irq_notrace @ disable interrupts ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK + tst r1, #_TIF_SYSCALL_WORK + bne fast_work_pending + tst r1, #_TIF_WORK_MASK bne fast_work_pending /* perform architecture specific actions before user return */ @@ -67,12 +69,15 @@ ret_fast_syscall: str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 disable_irq_notrace @ disable interrupts ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK + tst r1, #_TIF_SYSCALL_WORK + bne fast_work_pending + tst r1, #_TIF_WORK_MASK beq no_work_pending UNWIND(.fnend ) ENDPROC(ret_fast_syscall) /* Slower path - fall through to work_pending */ +fast_work_pending: #endif tst r1, #_TIF_SYSCALL_WORK diff --git a/arch/arm/kernel/signal.c b/arch/arm/kernel/signal.c index 7b8f2141427b..3a48b54c6405 100644 --- a/arch/arm/kernel/signal.c +++ b/arch/arm/kernel/signal.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -571,6 +572,10 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall) * Update the trace code with the current status. */ trace_hardirqs_off(); + + /* Check valid user FS if needed */ + addr_limit_user_check(); + do { if (likely(thread_flags & _TIF_NEED_RESCHED)) { schedule();