From patchwork Wed May 23 19:54:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10422303 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 8351F60224 for ; Wed, 23 May 2018 20:01:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7283928CDA for ; Wed, 23 May 2018 20:01:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6421A291DB; Wed, 23 May 2018 20:01: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=-12.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, USER_IN_DEF_DKIM_WL 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 0CEB12917F for ; Wed, 23 May 2018 20:01:15 +0000 (UTC) Received: (qmail 25672 invoked by uid 550); 23 May 2018 19:56:27 -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 24488 invoked from network); 23 May 2018 19:56:19 -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=Y2NPjgM8kmULVZ0/56ir06emjwD6YnmhH1dZIb/Jr0U=; b=bqWhbGCVolJgpC+8KdvoJPQIHtJXgbwUehpD6zb+k69qatCXWvn1ilJi/RNyqn4P4C 92jiN52+xIb2qOXirqI+IS2O0I7KqFsJbhitA6epM/4LYBBwPLD2y+xtGJDHXJf9TIE8 pBRgO6N3BrOFs8NE9oqOCAu4BXNtwpDspd2jA+T1lxo+XqULawOu7Iwaq0gM+kwupKRb qqha6gFULokJ6DzdjOIro8miV1iseZG+xS87R3mOzQde0TpidBTbeVDVBk+y315e3Upk xVjI7k3Ff6Yi9Uap7aXaHioAgMy7qtphCyR7KwT6OYf3ynupgOdHe9BHEc27D7kLxiPV NeZA== 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=Y2NPjgM8kmULVZ0/56ir06emjwD6YnmhH1dZIb/Jr0U=; b=RhjohNtJc6a4QjNkoFYcuTosbEopfYqOW4QZIFYjfW0m1zjnlMarp6/USOFcDqwi7H Blw0ySGxYiEboghiM24AfRYCBGHv5OeDJZIZpEYxfS1rbUQt9G6Wd2pLELhD50XOS3ZM 3CJeGV4VtSFm1Y7HYwVyoczzCGDWh0G1L3uQ3gV8t5FhK3HOW1/i8EQqoDYNOdcyn1OW 3gHf2rHk3KnG2AeONCtmNXwUDIvph+iaZUJNAq/5vw/egA5oRQyMAP8aYuXLE4a4dJr0 N4tFhYPy2rKQ4+Br5x7ikGBx/7pHE/1ByByNDwO3M/PD8NkyqotqVOt6nKMbJzoYHeQu eZgg== X-Gm-Message-State: ALKqPwflfgZxtYx8LWif71By6W4AVEF2qmcnvqasCJ/XsE8Orko4NmIV GnyDSCcwSulJi7EDiGrFEQWMHA== X-Google-Smtp-Source: AB8JxZoTe8sWVW9Lb+CPiExphU405INGqMmD/jG7vuIdxvB5xpXF3Slao9knfa94yg7QviswLZSYWg== X-Received: by 2002:a17:902:52ed:: with SMTP id a100-v6mr4256358pli.131.1527105366750; Wed, 23 May 2018 12:56:06 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Greg Kroah-Hartman , Thomas Garnier , Philippe Ombredanne , Kate Stewart , Arnaldo Carvalho de Melo , Yonghong Song , Andrey Ryabinin , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Andy Lutomirski , Dominik Brodowski , Borislav Petkov , Borislav Petkov , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Juergen Gross , Alok Kataria , Steven Rostedt , Jan Kiszka , Tejun Heo , Christoph Lameter , Dennis Zhou , Boris Ostrovsky , Alexey Dobriyan , Masami Hiramatsu , Cao jin , Francis Deslauriers , "Paul E . McKenney" , Nicolas Pitre , Andrew Morton , Randy Dunlap , "Luis R . Rodriguez" , Arnd Bergmann , Christopher Li , Jason Baron , Mika Westerberg , Lukas Wunner , Dou Liyang , Sergey Senozhatsky , Petr Mladek , Masahiro Yamada , Ingo Molnar , Nicholas Piggin , "H . J . Lu" , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , David Woodhouse , Dave Hansen , Rik van Riel , Jia Zhang , Ricardo Neri , Jonathan Corbet , Jan Beulich , Matthias Kaehlcke , 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 Subject: [PATCH v3 25/27] x86/pie: Add option to build the kernel as PIE Date: Wed, 23 May 2018 12:54:19 -0700 Message-Id: <20180523195421.180248-26-thgarnie@google.com> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog In-Reply-To: <20180523195421.180248-1-thgarnie@google.com> References: <20180523195421.180248-1-thgarnie@google.com> 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.18% - PIE enabled: -1.977% (less relocations) .text section: - PIE disabled: same - PIE enabled: same Size of vmlinux (Ubuntu configuration): File size: - PIE disabled: +0.21% - PIE enabled: +10% .text section: - PIE disabled: same - PIE enabled: +0.001% 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.5% on latest test). - PIE enabled: between -1% to +1% in average (default and Ubuntu config). Kernbench (average of 10 Half and Optimal runs): Elapsed Time: - PIE disabled: no significant change (avg -0.5%) - PIE enabled: average -0.5% to +0.5% System Time: - PIE disabled: no significant change (avg -0.1%) - PIE enabled: average -0.4% to +0.4%. [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82303 Signed-off-by: Thomas Garnier merge pie --- arch/x86/Kconfig | 8 ++++++++ arch/x86/Makefile | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 28eb2b3757bf..26d5d4942777 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2215,6 +2215,14 @@ config X86_GLOBAL_STACKPROTECTOR If unsure, say N +config X86_PIE + bool + depends on X86_64 + select DEFAULT_HIDDEN + select WEAK_PROVIDE_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 20bb6cbd8938..c92bcca4400c 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -60,6 +60,8 @@ endif KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow KBUILD_CFLAGS += $(call cc-option,-mno-avx,) +stackglobal := $(call cc-option-yn, -mstack-protector-guard=global) + ifeq ($(CONFIG_X86_32),y) BITS := 32 UTS_MACHINE := i386 @@ -135,7 +137,48 @@ else KBUILD_CFLAGS += -mno-red-zone ifdef CONFIG_X86_PIE + KBUILD_CFLAGS += -fPIE KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/x86/kernel/module.lds + + # Relax relocation in both CFLAGS and LDFLAGS to support older compilers + KBUILD_CFLAGS += $(call cc-option,-Wa$(comma)-mrelax-relocations=no) + LDFLAGS_vmlinux += $(call ld-option,--no-relax) + KBUILD_LDFLAGS_MODULE += $(call ld-option,--no-relax) + + # Stack validation is not yet support due to self-referenced switches +ifdef CONFIG_STACK_VALIDATION + $(warning CONFIG_STACK_VALIDATION is not yet supported for x86_64 pie \ + build.) + SKIP_STACK_VALIDATION := 1 + export SKIP_STACK_VALIDATION +endif + +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) + # Try to enable global stack cookie if possible + ifeq ($(stackglobal), y) + $(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|AUTO) \ + 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 @@ -146,7 +189,7 @@ endif endif ifdef CONFIG_X86_GLOBAL_STACKPROTECTOR - ifeq ($(call cc-option, -mstack-protector-guard=global),) + ifeq ($(stackglobal), n) $(error Cannot use CONFIG_X86_GLOBAL_STACKPROTECTOR: \ -mstack-protector-guard=global not supported \ by compiler)