From patchwork Wed Oct 11 20:30:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10000627 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 6DCCA6037F for ; Wed, 11 Oct 2017 20:37:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6192928B37 for ; Wed, 11 Oct 2017 20:37:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5561E28B69; Wed, 11 Oct 2017 20:37:05 +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 1BC3828B37 for ; Wed, 11 Oct 2017 20:37:03 +0000 (UTC) Received: (qmail 16117 invoked by uid 550); 11 Oct 2017 20:32:05 -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 15889 invoked from network); 11 Oct 2017 20:31:57 -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=hx8j/aAyhLIqLNFLGSNLcQhQo3LNYGiCKQDS6WAZduI=; b=c/aGnDiBaGHoF3TFma6riHarNoKEJKQ+UpcodG7G+fPVlls8kh9FfuRrEhVn+VIiSw E0NCgrpTkbWyyAxWYckjCmChHZB+/W+1nD6ljsBIH/EH4inqkNZsfXWZcAUPtAhaARk6 Ij55f2/i50yWEo5af0ihVwn1vTxQB2DI7w0m/pkH1bYUJku7ywvkrrE5S/ABHe0jgkeU +qjNRtM3WWSF8K5XOUmWvOinpYRuc2RMg+iBr1GLGePDQhgOCbfGHJXiYi1TUPjCTJlX tDnDgiVbx6q4XNK0VHCJ/qKEFo8H0L4x41gGCixUw5mGMxsC+PgHBImwBjkdZME3KARt 9UNA== 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=hx8j/aAyhLIqLNFLGSNLcQhQo3LNYGiCKQDS6WAZduI=; b=D6ZO4ttRJCsE2T/hPAq9k3nlfs6eR5oVbQXQ2ktcW/plc9BMNZBC6bG6wZqllDnpVb mdLGe8UEnW5tABqE/yet4DZo11eWll7q6dnwVbRXccLGTgIMfM5dUyCS6VhJQ4jmbz6P NEK7EJaMNULWyT+2QBXHNr1OThfDLGebX3eNkoN8qRy9u0cO9FdTEGcLUac3yhi4Gvga RSwoA/Y/sFUA311k+dJYrF3BB8hbdRfWEqf3MyAafRYI58dxe9/gTWV+PAZa2YslqMCn kA+ufxLEFdTX/3/r4uZ2kh0hRGn0Ekhb8MNUhNdCvlCJ4PPQXObC7nMJ6fdY7AYwUWGz 0EzA== X-Gm-Message-State: AMCzsaXsK1AvqFY0HXIApl/IS5NwJtO55xnosRrOiPPzxU2B8YG4ZIVZ 8U3QjeRPYAno9fTP78Zm6hAswA== X-Google-Smtp-Source: AOwi7QDd7LA8JkbIzjFwDRJ4dCCDlW3DjkT2lutQtGOVMqRbM4Jkr/PlF1dIGXlKF0vBKI5OBzdWPw== X-Received: by 10.84.140.35 with SMTP id 32mr181558pls.378.1507753905346; Wed, 11 Oct 2017 13:31:45 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Arnd Bergmann , Thomas Garnier , Kees Cook , Andrey Ryabinin , Matthias Kaehlcke , Tom Lendacky , Andy Lutomirski , "Kirill A . Shutemov" , Borislav Petkov , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Juergen Gross , Chris Wright , Alok Kataria , Rusty Russell , Tejun Heo , Christoph Lameter , Boris Ostrovsky , Paul Gortmaker , Andrew Morton , Alexey Dobriyan , "Paul E . McKenney" , Nicolas Pitre , Borislav Petkov , "Luis R . Rodriguez" , Greg Kroah-Hartman , Christopher Li , Steven Rostedt , Jason Baron , Mika Westerberg , Dou Liyang , "Rafael J . Wysocki" , Lukas Wunner , Masahiro Yamada , Alexei Starovoitov , Daniel Borkmann , Markus Trippelsdorf , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Rik van Riel , David Howells , Ard Biesheuvel , Waiman Long , Kyle Huey , Jonathan Corbet , Michal Hocko , Peter Foley , Paul Bolle , Jiri Kosina , "H . J . Lu" , Rob Landley , Baoquan He , =?UTF-8?q?Jan=20H=20=2E=20Sch=C3=B6nherr?= , Daniel Micay Cc: x86@kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, virtualization@lists.linux-foundation.org, xen-devel@lists.xenproject.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Wed, 11 Oct 2017 13:30:25 -0700 Message-Id: <20171011203027.11248-26-thgarnie@google.com> X-Mailer: git-send-email 2.15.0.rc0.271.g36b669edcc-goog In-Reply-To: <20171011203027.11248-1-thgarnie@google.com> References: <20171011203027.11248-1-thgarnie@google.com> Subject: [kernel-hardening] [PATCH v1 25/27] x86/pie: Add option to build the kernel as PIE X-Virus-Scanned: ClamAV using ClamSMTP Add the CONFIG_X86_PIE option which builds the kernel as a Position Independent Executable (PIE). The kernel is currently build with the mcmodel=kernel option which forces it to stay on the top 2G of the virtual address space. With PIE, the kernel will be able to move below the current limit. The --emit-relocs linker option was kept instead of using -pie to limit the impact on mapped sections. Any incompatible relocation will be catch by the arch/x86/tools/relocs binary at compile time. If segment based stack cookies are enabled, try to use the compiler option to select the segment register. If not available, automatically enabled global stack cookie in auto mode. Otherwise, recommend compiler update or global stack cookie option. Performance/Size impact: Size of vmlinux (Default configuration): File size: - PIE disabled: +0.000031% - PIE enabled: -3.210% (less relocations) .text section: - PIE disabled: +0.000644% - PIE enabled: +0.837% Size of vmlinux (Ubuntu configuration): File size: - PIE disabled: -0.201% - PIE enabled: -0.082% .text section: - PIE disabled: same - PIE enabled: +1.319% Size of vmlinux (Default configuration + ORC): File size: - PIE enabled: -3.167% .text section: - PIE enabled: +0.814% Size of vmlinux (Ubuntu configuration + ORC): File size: - PIE enabled: -3.167% .text section: - PIE enabled: +1.26% The size increase is mainly due to not having access to the 32-bit signed relocation that can be used with mcmodel=kernel. A small part is due to reduced optimization for PIE code. This bug [1] was opened with gcc to provide a better code generation for kernel PIE. Hackbench (50% and 1600% on thread/process for pipe/sockets): - PIE disabled: no significant change (avg +0.1% on latest test). - PIE enabled: between -0.50% to +0.86% in average (default and Ubuntu config). slab_test (average of 10 runs): - PIE disabled: no significant change (-2% on latest run, likely noise). - PIE enabled: between -1% and +0.8% on latest runs. Kernbench (average of 10 Half and Optimal runs): Elapsed Time: - PIE disabled: no significant change (avg -0.239%) - PIE enabled: average +0.07% System Time: - PIE disabled: no significant change (avg -0.277%) - PIE enabled: average +0.7% [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82303 Signed-off-by: Thomas Garnier merge PIE --- arch/x86/Kconfig | 7 +++++++ arch/x86/Makefile | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 1f2b731c9ec3..bbd28a46ab55 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2148,6 +2148,13 @@ config X86_GLOBAL_STACKPROTECTOR If unsure, say N +config X86_PIE + bool + depends on X86_64 + select DEFAULT_HIDDEN + select DYNAMIC_MODULE_BASE + select MODULE_REL_CRCS if MODVERSIONS + config HOTPLUG_CPU bool "Support for hot-pluggable CPUs" depends on SMP diff --git a/arch/x86/Makefile b/arch/x86/Makefile index b592d57c531b..beae9504c3f4 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -135,7 +135,34 @@ else KBUILD_CFLAGS += -mno-red-zone ifdef CONFIG_X86_PIE + KBUILD_CFLAGS += -fPIC KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/x86/kernel/module.lds + +ifndef CONFIG_CC_STACKPROTECTOR_NONE +ifndef CONFIG_X86_GLOBAL_STACKPROTECTOR + stackseg-flag := -mstack-protector-guard-reg=%gs + ifeq ($(call cc-option-yn,$(stackseg-flag)),n) + ifdef CONFIG_CC_STACKPROTECTOR_AUTO + $(warning Cannot use CONFIG_CC_STACKPROTECTOR_* while \ + building a position independent kernel. \ + Default to global stack protector \ + (CONFIG_X86_GLOBAL_STACKPROTECTOR).) + CONFIG_X86_GLOBAL_STACKPROTECTOR := y + KBUILD_CFLAGS += -DCONFIG_X86_GLOBAL_STACKPROTECTOR + KBUILD_AFLAGS += -DCONFIG_X86_GLOBAL_STACKPROTECTOR + else + $(error echo Cannot use \ + CONFIG_CC_STACKPROTECTOR_(REGULAR|STRONG) \ + while building a position independent binary \ + Update your compiler or use \ + CONFIG_X86_GLOBAL_STACKPROTECTOR) + endif + else + KBUILD_CFLAGS += $(stackseg-flag) + endif +endif +endif + else KBUILD_CFLAGS += -mcmodel=kernel endif