From patchwork Thu Jun 15 01:12:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9787777 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 9A87060231 for ; Thu, 15 Jun 2017 01:12:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F968284F8 for ; Thu, 15 Jun 2017 01:12:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 61F3128517; Thu, 15 Jun 2017 01:12:27 +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 1FA9E284F8 for ; Thu, 15 Jun 2017 01:12:25 +0000 (UTC) Received: (qmail 15673 invoked by uid 550); 15 Jun 2017 01:12:22 -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 15654 invoked from network); 15 Jun 2017 01:12:21 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=PMFGL4/oVDHleQSWeKAmn1FuQ0wIw7k5BQSyt49yzmc=; b=v6ynRvspAYjXgNlcRnbGcLIDAy6a8lDXsubHFMtGwyRIuzM17AUZix+NxE81t37lhO a0P6Y5Y/CubvRBkeLbWcsTbR5EuJmAEshqjQTGmjYa9Vu0LQNDiyIOWE/xm6/sYSvZfn Jxzbaf2PcPpQiiKLlk5PovvAD9znHijYqKwpnAu0eLXxgZB5xxf0P4PIP0DYNhgLQpiq Smgo3aIrmtTVjosfyaLk9yVB87gyE7wSmOQzyVZssSg0DLcNkrmSGNnceQ2iMHjLY+FW m97DZUFWXQdeH2dfKP+Lof2NJnKTmZOIS5iLF8Qd8/xa1yEGVzi9jvR76ga+5mcKHHNo 8GrA== 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; bh=PMFGL4/oVDHleQSWeKAmn1FuQ0wIw7k5BQSyt49yzmc=; b=Opa2ZHOCKZQu7elFCOWyebhDZHibsL5aZjxS4BZ8hyaUci2s+ud68BfokuAURMDg9b 4HwQovwv0eF7td97Vx1pJcQPTicOYk1Ym8dPZRihKQX2GH+PAQ8RM20n+v3fcFYWy2/E eSB5fihkeOIij+i58JQdMnBxw+58Q+wqVHsYj3R6gDMwx40GYf7PboRCWSKaB1OluJZR ThrEB17i3Ox1mnAogQQ2ZEbu440ZWgQaz9W4yhBLlnMplpY55PDtJVSZbUrjzZr0jrSZ +ptnnHX9RJoGr3t5DGWWztYid/DWOGd9afxmlHjEWlI1AgdGXUDDnUQYZ2GDwsAr/ZTe h9Yw== X-Gm-Message-State: AKS2vOwsdUsyyarAFoHQsLKmIGU4N+oekoH2wMOlYDeX0xkM2eZ0JIw4 5HxSPbH4U745vVCS X-Received: by 10.98.87.195 with SMTP id i64mr2585820pfj.51.1497489129414; Wed, 14 Jun 2017 18:12:09 -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:01 -0700 Message-Id: <20170615011203.144108-1-thgarnie@google.com> X-Mailer: git-send-email 2.13.1.518.g3df882009-goog Subject: [kernel-hardening] [PATCH v10 1/3] x86/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 addr_limit_user_check function is added as a cross-architecture function to check the address limit. [1] https://bugs.chromium.org/p/project-zero/issues/detail?id=990 Signed-off-by: Thomas Garnier Tested-by: Kees Cook --- v10 redesigns the change to use work flags on set_fs as recommended by Linus and agreed by others. Based on next-20170609 --- arch/x86/entry/common.c | 3 +++ arch/x86/include/asm/thread_info.h | 5 ++++- arch/x86/include/asm/uaccess.h | 7 ++++++- include/linux/syscalls.h | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c index cdefcfdd9e63..03505ffbe1b6 100644 --- a/arch/x86/entry/common.c +++ b/arch/x86/entry/common.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -183,6 +184,8 @@ __visible inline void prepare_exit_to_usermode(struct pt_regs *regs) struct thread_info *ti = current_thread_info(); u32 cached_flags; + addr_limit_user_check(); + if (IS_ENABLED(CONFIG_PROVE_LOCKING) && WARN_ON(!irqs_disabled())) local_irq_disable(); diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h index e00e1bd6e7b3..5161da1a0fa0 100644 --- a/arch/x86/include/asm/thread_info.h +++ b/arch/x86/include/asm/thread_info.h @@ -98,6 +98,7 @@ struct thread_info { #define TIF_SYSCALL_TRACEPOINT 28 /* syscall tracepoint instrumentation */ #define TIF_ADDR32 29 /* 32-bit address space on 64 bits */ #define TIF_X32 30 /* 32-bit native x86-64 binary */ +#define TIF_FSCHECK 31 /* Check FS is USER_DS on return */ #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE) #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME) @@ -122,6 +123,7 @@ struct thread_info { #define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT) #define _TIF_ADDR32 (1 << TIF_ADDR32) #define _TIF_X32 (1 << TIF_X32) +#define _TIF_FSCHECK (1 << TIF_FSCHECK) /* * work to do in syscall_trace_enter(). Also includes TIF_NOHZ for @@ -137,7 +139,8 @@ struct thread_info { (_TIF_SYSCALL_TRACE | _TIF_NOTIFY_RESUME | _TIF_SIGPENDING | \ _TIF_NEED_RESCHED | _TIF_SINGLESTEP | _TIF_SYSCALL_EMU | \ _TIF_SYSCALL_AUDIT | _TIF_USER_RETURN_NOTIFY | _TIF_UPROBE | \ - _TIF_PATCH_PENDING | _TIF_NOHZ | _TIF_SYSCALL_TRACEPOINT) + _TIF_PATCH_PENDING | _TIF_NOHZ | _TIF_SYSCALL_TRACEPOINT | \ + _TIF_FSCHECK) /* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW \ diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h index a059aac9e937..11433f9018e2 100644 --- a/arch/x86/include/asm/uaccess.h +++ b/arch/x86/include/asm/uaccess.h @@ -26,7 +26,12 @@ #define get_ds() (KERNEL_DS) #define get_fs() (current->thread.addr_limit) -#define set_fs(x) (current->thread.addr_limit = (x)) +static inline void set_fs(mm_segment_t fs) +{ + current->thread.addr_limit = fs; + /* On user-mode return, check fs is correct */ + set_thread_flag(TIF_FSCHECK); +} #define segment_eq(a, b) ((a).seg == (b).seg) diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 980c3c9b06f8..ac0cf6fb25d6 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -206,6 +206,22 @@ extern struct trace_event_functions exit_syscall_print_funcs; } \ static inline long SYSC##name(__MAP(x,__SC_DECL,__VA_ARGS__)) +#ifdef TIF_FSCHECK +/* + * Called before coming back to user-mode. Returning to user-mode with an + * address limit different than USER_DS can allow to overwrite kernel memory. + */ +static inline void addr_limit_user_check(void) +{ + + if (!test_thread_flag(TIF_FSCHECK)) + return; + + BUG_ON(!segment_eq(get_fs(), USER_DS)); + clear_thread_flag(TIF_FSCHECK); +} +#endif + asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr); asmlinkage long sys_time(time_t __user *tloc);