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: 9787789 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 6DBC060231 for ; Thu, 15 Jun 2017 01:12:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 60D33284F8 for ; Thu, 15 Jun 2017 01:12:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 52DB328517; Thu, 15 Jun 2017 01:12:59 +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=-1.9 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D1F58284F8 for ; Thu, 15 Jun 2017 01:12:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=Fud5YEvUVUsu3/7VGLngteFOhSbp7fAVD+apjL/Z2yI=; b=tcS JNAAVdOORhSwaedgpU0cqJZyp1UmiG1RLyd+tduczVYG0D4EulfO2jkF4GktJlsYkSzcIRUTxdeYG AdS1yaSQJQLo0J2ndCoIJtRr3SrcrCCs1EYa1W+RJQKt6wIk34tBZZzKpqXGIZkEqZ67X57Qync7+ YMWAEMOSqateSjXN+7y7LwwhYidO10cCa9/LTk6S7eK5pHSesiGdRxphEZhem2SUh+w7q4e4alYTf pJJnlQPvJKIqt/GBuJ2UEm2wDz+O6B4IIllRYZPsWSYB6MxfG6zg7wphfR9OPcUXW0z408Clz+vqv mkUh6V3U2dwAkyEMMduSYwIfzKcP8cg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dLJLB-0002i4-Pw; Thu, 15 Jun 2017 01:12:53 +0000 Received: from mail-pf0-x234.google.com ([2607:f8b0:400e:c00::234]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dLJKo-0002eL-UO for linux-arm-kernel@lists.infradead.org; Thu, 15 Jun 2017 01:12:32 +0000 Received: by mail-pf0-x234.google.com with SMTP id x63so125859pff.3 for ; Wed, 14 Jun 2017 18:12:10 -0700 (PDT) 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=FkNtFZxTcjT4/AVlyd4HewbQN+PxlfWVDh3K4GIYAX7u8EJa7oIYGKI86OM6jUGYSV nvMat5Qfoncp9jbYXy402Qa1rWkAVarPGHxyoI/J54qZUocaWCH32ulpvrpLnKoFuDnM oEJ0KUDGZwhuS12ECBp9L/tzmbYsl6OQMYaowM26uQh3q7bf5M9dBXmej+Akz21G7S5D Vdbq1Z6SOdZ08opUjdnHBfY9HfzyvIwK4EH4y8nckb0JIs9kfUJIiO2f0Jadik+lId5n QPtOeVNqCWs5JdKcvZglosyoQAsSj+pZZ+KM4qj6lmep3nSlAD2z1XXERBAipQXX/CbR 7Vbw== X-Gm-Message-State: AKS2vOwZ2WJG8Yd4d7f+Od0G5ZCIL0Z+oBK+hD7t9kASFUuAkrIYpMq/ RLF9uObg7X6mMT98 X-Received: by 10.98.87.195 with SMTP id i64mr2585820pfj.51.1497489129414; Wed, 14 Jun 2017 18:12:09 -0700 (PDT) Received: from skynet.sea.corp.google.com ([100.100.206.164]) by smtp.gmail.com with ESMTPSA id z66sm2337017pfl.13.2017.06.14.18.12.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 14 Jun 2017 18:12:08 -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 Subject: [PATCH v10 1/3] x86/syscalls: Check address limit on user-mode return 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170614_181231_002309_C24E832B X-CRM114-Status: GOOD ( 16.54 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-api@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel-hardening@lists.openwall.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org 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);