From patchwork Wed Apr 26 18:34:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9701817 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 8FADE6032C for ; Wed, 26 Apr 2017 18:35:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 83FF828399 for ; Wed, 26 Apr 2017 18:35:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 78419285E7; Wed, 26 Apr 2017 18:35:17 +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 5C13B28399 for ; Wed, 26 Apr 2017 18:35:15 +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=sCDIqwy/LDNQNe6LK2GJqXeZ7NjQTvyfbbQij/T5ByA=; b=dpN hNDbUdHSjarpEaPJvxUU22cNwKieB5umnucbGvI8y2eZBUnU3+IVVinScO6OT3xMZ0xFSQUAkZXby NKGmzbx6iDLSFvcdYzk91QORUi4xD/GTWUAUgUYcTFrY2+9uPS4GmlQ4eJeqdacOcabwPXLm86ERS 5uJ8xRACE/M+Hhi8f83e+1gGQELkSx75Vigq8czzYGKGv1pPOGspL4fBWQ2D4xPsoU62G47bQb8wE QDyhvkY+cEDU//yZYOE+79Ac8bYJGH4O+zAIZept9ySt6bBzBKoDdKFrZyC3Q+s6B29IDf8jGvc/h q8RTArDoG1RHtFBMkAHmA36Ffl/Wo/Q==; 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 1d3RmV-0005Dh-KA; Wed, 26 Apr 2017 18:35:15 +0000 Received: from mail-pg0-x22d.google.com ([2607:f8b0:400e:c05::22d]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1d3RmS-0003ih-Kh for linux-arm-kernel@lists.infradead.org; Wed, 26 Apr 2017 18:35:14 +0000 Received: by mail-pg0-x22d.google.com with SMTP id 63so4118234pgh.0 for ; Wed, 26 Apr 2017 11:34:51 -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=E8uMIKTXTk0HDHqW/2D9pLGkIdNmBo7aYhEP0sOljik=; b=Tp5w6NB5d9wKNvOID8eaqkpMX+Z1xrZEsW4tgUE3GGCXOkjqiedjMaXS3DdUCRVAkP rstJrBylBol6+K0sOY+4fFhEY4CxEkCRk6aOClNVBNMl0lF08yERllX04T3MHv9oMVzw nHHx8hqEDWG23Xn+J6KrotRj/YBU1mh3yi3PW8b3KvIm8123tUwwx+qlkLroAks2fnZb pXKPbY1swWlWsabB5OnKRDVkMAQUS4BN1DyaPLuqgot0/idAj458O1zahb0FEqtOf2sG vLG1mmYWY/POLtlblwNPmhhGvbNvBZsFAocZGgNHIMpY9hVYD2voGk+EJ5ukLMpxclWn X8OQ== 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=E8uMIKTXTk0HDHqW/2D9pLGkIdNmBo7aYhEP0sOljik=; b=iN2UuuaPtg41l4E3mHrGRcioWkNBX5Oh8KdbHDUjlPw5yTbLn/kuXjiA9kIAT32Mbb 37Y41ltqAbs1DTAfFNbTZDWsdzLsAbMDnjwGG1jedz2iCgxO6Q+yTnM6I7neiR6aXPzK 3vuQaNxjF+2Y80m5FOU3NIjVv/8c7Ifi7qv2bRthneSZYKl04JDpDhrrJgz7yFOcfvgk JE+rLEcHJsA0yMWfL7zJ4ekwiPu8oIv/Jfmhe35piU5b4vuwuFIZiXfS22JzHmZMAEbG JjIYJERX1xy01yOp8WrXzXWjnDh1bL8kmEEFD4yl9aXQ6soNZJplvmMA1qNFQVN41u6W iUEA== X-Gm-Message-State: AN3rC/7ooHQr0INA489S7q0bjAn7UEOT18zElXP62774gOpySE6ozxyT gGC4qtpICrLCcBwk X-Received: by 10.84.217.205 with SMTP id d13mr1636699plj.114.1493231690823; Wed, 26 Apr 2017 11:34:50 -0700 (PDT) Received: from skynet.sea.corp.google.com ([100.100.206.164]) by smtp.gmail.com with ESMTPSA id g89sm60083pfk.25.2017.04.26.11.34.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 26 Apr 2017 11:34:50 -0700 (PDT) From: Thomas Garnier To: Martin Schwidefsky , Heiko Carstens , Dave Hansen , Arnd Bergmann , David Howells , =?UTF-8?q?Ren=C3=A9=20Nyffenegger?= , Thomas Garnier , Andrew Morton , "Paul E . McKenney" , Ingo Molnar , "Eric W . Biederman" , Thomas Gleixner , Oleg Nesterov , Pavel Tikhomirov , Ingo Molnar , "H . Peter Anvin" , Andy Lutomirski , Paolo Bonzini , Kees Cook , Rik van Riel , Josh Poimboeuf , Borislav Petkov , Brian Gerst , "Kirill A . Shutemov" , Christian Borntraeger , Russell King , Will Deacon , Catalin Marinas , Mark Rutland , James Morse Subject: [PATCH v8 1/4] syscalls: Verify address limit before returning to user-mode Date: Wed, 26 Apr 2017 11:34:22 -0700 Message-Id: <20170426183425.32158-1-thgarnie@google.com> X-Mailer: git-send-email 2.13.0.rc0.306.g87b477812d-goog X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170426_113512_734609_93BB632D X-CRM114-Status: GOOD ( 16.44 ) 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-s390@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org 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 that a syscall does not return to user-mode with a kernel address limit. If that happens, a process can corrupt kernel-mode memory and elevate privileges [1]. The CONFIG_ADDR_LIMIT_CHECK option disables the generic check so each architecture can create optimized versions. [1] https://bugs.chromium.org/p/project-zero/issues/detail?id=990 Signed-off-by: Thomas Garnier Tested-by: Kees Cook --- Based on next-20170426 --- arch/s390/Kconfig | 1 + include/linux/syscalls.h | 27 ++++++++++++++++++++++++++- init/Kconfig | 6 ++++++ kernel/sys.c | 13 +++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index d25435d94b6e..164de1d24e92 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -103,6 +103,7 @@ config S390 select ARCH_INLINE_WRITE_UNLOCK_BH select ARCH_INLINE_WRITE_UNLOCK_IRQ select ARCH_INLINE_WRITE_UNLOCK_IRQRESTORE + select ADDR_LIMIT_CHECK select ARCH_SAVE_PAGE_KEYS if HIBERNATION select ARCH_SUPPORTS_ATOMIC_RMW select ARCH_SUPPORTS_DEFERRED_STRUCT_PAGE_INIT diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 980c3c9b06f8..ebde64f1622c 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -191,6 +191,28 @@ extern struct trace_event_functions exit_syscall_print_funcs; SYSCALL_METADATA(sname, x, __VA_ARGS__) \ __SYSCALL_DEFINEx(x, sname, __VA_ARGS__) + +/* + * 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_check_syscall(void) +{ + BUG_ON(!segment_eq(get_fs(), USER_DS)); +} + +#ifndef CONFIG_ADDR_LIMIT_CHECK +#define __CHECK_USERMODE_SYSCALL() \ + bool user_caller = segment_eq(get_fs(), USER_DS) +#define __VERIFY_ADDR_LIMIT() \ + if (user_caller) addr_limit_check_syscall() +#else +#define __CHECK_USERMODE_SYSCALL() +#define __VERIFY_ADDR_LIMIT() +asmlinkage void addr_limit_check_failed(void) __noreturn; +#endif + + #define __PROTECT(...) asmlinkage_protect(__VA_ARGS__) #define __SYSCALL_DEFINEx(x, name, ...) \ asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) \ @@ -199,7 +221,10 @@ extern struct trace_event_functions exit_syscall_print_funcs; asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \ asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \ { \ - long ret = SYSC##name(__MAP(x,__SC_CAST,__VA_ARGS__)); \ + long ret; \ + __CHECK_USERMODE_SYSCALL(); \ + ret = SYSC##name(__MAP(x,__SC_CAST,__VA_ARGS__)); \ + __VERIFY_ADDR_LIMIT(); \ __MAP(x,__SC_TEST,__VA_ARGS__); \ __PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__)); \ return ret; \ diff --git a/init/Kconfig b/init/Kconfig index 42a346b0df43..599d9fe30703 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1961,6 +1961,12 @@ config PROFILING config TRACEPOINTS bool +config ADDR_LIMIT_CHECK + bool + help + Disable the generic address limit check. Allow each architecture to + optimize how and when the verification is done. + source "arch/Kconfig" endmenu # General setup diff --git a/kernel/sys.c b/kernel/sys.c index 8a94b4eabcaa..a1cbcd715d62 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2458,3 +2458,16 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info) return 0; } #endif /* CONFIG_COMPAT */ + +#ifdef CONFIG_ADDR_LIMIT_CHECK +/* + * Used when an architecture specific implementation detects an invalid address + * limit. This function does not return. + */ +asmlinkage void addr_limit_check_failed(void) +{ + /* Try to fail on the generic address limit check */ + addr_limit_check_syscall(); + panic("Invalid address limit before returning to user-mode"); +} +#endif