From patchwork Thu Jun 15 01:12:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9787781 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 DF47160231 for ; Thu, 15 Jun 2017 01:12:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D42D7284F8 for ; Thu, 15 Jun 2017 01:12:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C8CFA28517; Thu, 15 Jun 2017 01:12:39 +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 C257D284F8 for ; Thu, 15 Jun 2017 01:12:38 +0000 (UTC) Received: (qmail 15865 invoked by uid 550); 15 Jun 2017 01:12:25 -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 15684 invoked from network); 15 Jun 2017 01:12:23 -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=F6WvNtoy46MmUEJrO1uCoEKqR+X+TMhzCE5tIlkMPUs=; b=qo+24M6Izp5dctcqGibyNJyGAlgFtfTWiZXuKXe8yZgRX/o+lisTa4B7ZLXqqKKE1f gebDDT6BIfJrle+81UI2fguPzu/h7Mb1HN7ensklHShVk9+TDcnzgHonAd3SF4tB/kK6 09E8dyKB6mGwF3C/O0vsnFk1TDjxSfGeQyGfgPBPzr5RTRD3hzAIK4U2xdK8ke3eNUul hjLZVy5DrY6nKK634li9HxPcrhK9PmnhUXPEewDe6g4bM5i4+Q8wGu0a5FGZyTq2QAyx Kd8CoxyAR8xSY4inFldor4wzMtvoIOTPIyklSJj8uf9czBQhXdejiX8tNucY/dudr0bf Elmw== 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=F6WvNtoy46MmUEJrO1uCoEKqR+X+TMhzCE5tIlkMPUs=; b=RJ3knnuH9mz4jJLBCEikbrqsyR2IgC/zCSK1NNNR9Q65lkudhOhJMZpGFm2f3y/XzC qW3ReTGGFbVreILtetX74qX/7aAAQq34IVMjuPk3JueXWzfN0Hh4WxuaJJP9+ghVdo6l HB9Uzb7k1hh/yJBgir9iRxWov6roHBv9mLp8RKj9W7iIjg1ovla9qpnch2IsyiNg0D/R k7W+L+77l6UcUdQNiCqsvgFMs7MjVnmcbc/pZ0dFLNyimYc4FvFRZ2DDeIyz5GDG7QSV o6aeNEbMWXTOJwwKZNRXw3qCvfiAt3+ochYJX3X6b33GmM3hJhCa3PPcW8/Izy6n2wBW ILKg== X-Gm-Message-State: AKS2vOxwyIn+g1Ga2MNdPJDfXykm7yd1Bxnvcl+oYAGUsvVNASilwHfb S4pdQPNf9p66FI42 X-Received: by 10.99.137.195 with SMTP id v186mr2685349pgd.204.1497489131725; Wed, 14 Jun 2017 18:12:11 -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:03 -0700 Message-Id: <20170615011203.144108-3-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 3/3] arm64/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. [1] https://bugs.chromium.org/p/project-zero/issues/detail?id=990 Signed-off-by: Thomas Garnier Reviewed-by: Catalin Marinas --- v10 redesigns the change to use work flags on set_fs as recommended by Linus and agreed by others. Based on next-20170609 --- arch/arm64/include/asm/thread_info.h | 4 +++- arch/arm64/include/asm/uaccess.h | 3 +++ arch/arm64/kernel/signal.c | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h index 46c3b93cf865..c5ba565544ee 100644 --- a/arch/arm64/include/asm/thread_info.h +++ b/arch/arm64/include/asm/thread_info.h @@ -86,6 +86,7 @@ struct thread_info { #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */ #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */ #define TIF_UPROBE 4 /* uprobe breakpoint or singlestep */ +#define TIF_FSCHECK 5 /* Check FS is USER_DS on return */ #define TIF_NOHZ 7 #define TIF_SYSCALL_TRACE 8 #define TIF_SYSCALL_AUDIT 9 @@ -107,11 +108,12 @@ struct thread_info { #define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT) #define _TIF_SECCOMP (1 << TIF_SECCOMP) #define _TIF_UPROBE (1 << TIF_UPROBE) +#define _TIF_FSCHECK (1 << TIF_FSCHECK) #define _TIF_32BIT (1 << TIF_32BIT) #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \ _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \ - _TIF_UPROBE) + _TIF_UPROBE | _TIF_FSCHECK) #define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \ _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \ diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 7b8a04789cef..ced7a7c2dd41 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -45,6 +45,9 @@ static inline void set_fs(mm_segment_t fs) { current_thread_info()->addr_limit = fs; + /* On user-mode return, check fs is correct */ + set_thread_flag(TIF_FSCHECK); + /* * Enable/disable UAO so that copy_to_user() etc can access * kernel memory with the unprivileged instructions. diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c index c7b6de62f9d3..0f0279148bdc 100644 --- a/arch/arm64/kernel/signal.c +++ b/arch/arm64/kernel/signal.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -408,6 +409,10 @@ asmlinkage void do_notify_resume(struct pt_regs *regs, * Update the trace code with the current status. */ trace_hardirqs_off(); + + /* Check valid user FS if needed */ + addr_limit_user_check(); + do { if (thread_flags & _TIF_NEED_RESCHED) { schedule();