From patchwork Tue Jul 18 22:33:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9850099 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 27F3A60392 for ; Tue, 18 Jul 2017 22:37:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 11507285CB for ; Tue, 18 Jul 2017 22:37:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 05A28285D8; Tue, 18 Jul 2017 22:37:54 +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 ED2D8285CB for ; Tue, 18 Jul 2017 22:37:52 +0000 (UTC) Received: (qmail 28259 invoked by uid 550); 18 Jul 2017 22:34:54 -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 28163 invoked from network); 18 Jul 2017 22:34:51 -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=bpzECORCtR+yUbsXEjnx6CANNXCH8OxCDIAIyxybdpg=; b=i8htuRbndGaBqOZDryocE1ttQf0qy9B3kLve7bjNXb31FxVVqqTC890tgGWfrfbQDF qmLK8fDtoXU5ScxJP45QgXhj6HfBsUhFAnhmB8v0yReOZao5rg6Gup4CpDHayM3troh5 FoFOXJPGpZ3u2FHP9YIi1xgo7yg2XvTcCdNTP+bqf5ZrLPTSVOICvhpQYu/J8yIE9rKO JfDHIA4x5n+ScgSZ6sYj2ulnckquXiH9XJdFfoPB/KVFmXO40w+E2JghgYIA3x0A8bBO QdF4acX4TzOmNiW6EnbL0wDVCO2/n0SLlu5LYEvfAx1tVgwGCka3JWtOULJ2YDhpX4NE e8fA== 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=bpzECORCtR+yUbsXEjnx6CANNXCH8OxCDIAIyxybdpg=; b=IXhPljFouyfIb+aWv/rxTTsH8oZzRVQ3M3VN9ulUE52pnvv0Aasa0Ahp79eJwN2lsr 7INsMNjTvZlDpgeZvDtwFPHXU+lO7dOHgFdSJHK237QXx+cXJjy3fMyNpdPTh81mhWPD TA/UH2ULhh/pstDp+R28oILpmY1Uvg3bG+kvgqnAMT9KiZXJeseHWNKFMoVsk1Ph09l9 A+AwsbVABAgR/0/+kJG3aPVi1VCE7Zd4f8+5ysTjKHOIpm24HDTvf38Ga8Wd5xwRW/ik YKnnX+Jrh8pzqzK8mKRb/DN3nogP6M4KFsAjUTUs8duzEGALMGkWbvg1N4j3CmKUkTQ6 YMUQ== X-Gm-Message-State: AIVw110ukfVwHcl7gIc+SPSTL2oKRp6MjPmF6qqCRADngk1l0IoKa8kO mB6QGviT/ODOYnu1 X-Received: by 10.84.231.131 with SMTP id g3mr4224469plk.176.1500417279407; Tue, 18 Jul 2017 15:34:39 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Thomas Garnier , Arnd Bergmann , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Andy Lutomirski , Borislav Petkov , "Kirill A . Shutemov" , Brian Gerst , Borislav Petkov , Christian Borntraeger , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Kees Cook , Paul Gortmaker , Chris Metcalf , "Paul E . McKenney" , Andrew Morton , Christopher Li , Dou Liyang , Masahiro Yamada , Daniel Borkmann , Markus Trippelsdorf , Peter Foley , Steven Rostedt , Tim Chen , Ard Biesheuvel , Catalin Marinas , Matthew Wilcox , Michal Hocko , Rob Landley , Jiri Kosina , "H . J . Lu" , Paul Bolle , Baoquan He , Daniel Micay Cc: x86@kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Tue, 18 Jul 2017 15:33:30 -0700 Message-Id: <20170718223333.110371-20-thgarnie@google.com> X-Mailer: git-send-email 2.13.2.932.g7449e964c-goog In-Reply-To: <20170718223333.110371-1-thgarnie@google.com> References: <20170718223333.110371-1-thgarnie@google.com> Subject: [kernel-hardening] [RFC 19/22] x86/pie: Add option to build the kernel as PIE for x86_64 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 -2G limit increasing the KASLR range from 1GB to 3GB. The modules do not support PIE due to how they are linked. Disable PIE for them and default to mcmodel=kernel for now. The PIE configuration is not yet compatible with XEN_PVH. Xen PVH generates 32-bit assembly and uses a long jump to transition to 64-bit. A long jump require an absolute reference that is not compatible with PIE. Performance/Size impact: Hackbench (50% and 1600% loads): - PIE disabled: no significant change (-0.50% / +0.50%) - PIE enabled: 7% to 8% on half load, 10% on heavy load. These results are aligned with the different research on user-mode PIE impact on cpu intensive benchmarks (around 10% on x86_64). slab_test (average of 10 runs): - PIE disabled: no significant change (-1% / +1%) - PIE enabled: 3% to 4% Kernbench (average of 10 Half and Optimal runs): Elapsed Time: - PIE disabled: no significant change (-0.22% / +0.06%) - PIE enabled: around 0.50% System Time: - PIE disabled: no significant change (-0.99% / -1.28%) - PIE enabled: 5% to 6% Size of vmlinux (Ubuntu configuration): File size: - PIE disabled: 472928672 bytes (-0.000169% from baseline) - PIE enabled: 216878461 bytes (-54.14% from baseline) .text sections: - PIE disabled: 9373572 bytes (+0.04% from baseline) - PIE enabled: 9499138 bytes (+1.38% from baseline) The big decrease in vmlinux file size is due to the lower number of relocations appended to the file. Signed-off-by: Thomas Garnier --- arch/x86/Kconfig | 6 ++++++ arch/x86/Makefile | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 781521b7cf9e..b26ee6751021 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2080,6 +2080,12 @@ config RANDOMIZE_MEMORY_PHYSICAL_PADDING If unsure, leave at the default value. +config X86_PIE + bool + depends on X86_64 && !XEN_PVH + select DEFAULT_HIDDEN + 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 1e902f926be3..452a9621af8f 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -45,8 +45,12 @@ export REALMODE_CFLAGS export BITS ifdef CONFIG_X86_NEED_RELOCS +ifdef CONFIG_X86_PIE + LDFLAGS_vmlinux := -pie -shared -Bsymbolic +else LDFLAGS_vmlinux := --emit-relocs endif +endif # # Prevent GCC from generating any FP code by mistake. @@ -132,7 +136,12 @@ else KBUILD_CFLAGS += $(cflags-y) KBUILD_CFLAGS += -mno-red-zone +ifdef CONFIG_X86_PIE + KBUILD_CFLAGS += -fPIC + KBUILD_CFLAGS_MODULE += -fno-PIC -mcmodel=kernel +else KBUILD_CFLAGS += -mcmodel=kernel +endif # -funit-at-a-time shrinks the kernel .text considerably # unfortunately it makes reading oopses harder.