From patchwork Wed Oct 4 21:20: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: 9985629 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 96E7760586 for ; Wed, 4 Oct 2017 21:26:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 876E728B68 for ; Wed, 4 Oct 2017 21:26:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B3FA28C2A; Wed, 4 Oct 2017 21:26:46 +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 5390E28BD9 for ; Wed, 4 Oct 2017 21:26:45 +0000 (UTC) Received: (qmail 17662 invoked by uid 550); 4 Oct 2017 21:21:55 -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 16323 invoked from network); 4 Oct 2017 21:21:44 -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=XDPD+BN3I5KbJk1S37u4pgVf5Mmlyc2fod5SXfxZZmc=; b=AhxB4Z5eY+nrnPP+DYvpoJ3uL+abcoTMyk7DCefY7Emr0LjBlQP3ZzW01phsq8hX4c FCbJDXKJI8LlXtR19ZzIW9kyE1zIsoPq1Hos8PhYrFSysE5A+870K7AphZ9yTR36vP62 heUh5zEGXtKXJboaqCd7ZhZDH4QGL5Zt6YP3s7C5pL3xzqHt3pa9jweKjZJmNJl+18+J wwzVB9pHWT5LlX/fB3xZnRxN3N4Ov7XBwQXYX2p5uVRR6tHydEn+H1iag4iIDVeJ63Ab f8YuOj06H+N2fePP2uQoG/7lQJorztUTfE8Gwn8ExytSlKkiY1YcVoZt4pAM7hTFZ+Yt Y9yQ== 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=XDPD+BN3I5KbJk1S37u4pgVf5Mmlyc2fod5SXfxZZmc=; b=MpTFn4zNkkeeKnY+1wxWxC62jN7p704svO89BbGYwVMrzaOIWEnYauuLC9aEfcUDUE 1BA/Ob65DAJ2LHwuJXBD5o6tv+8wcKCXkdwR9HsnKsW2fjxVuORccHrooo0C8RolXWtk sLPa2CK+kuYh0nmJE91V6mGuEC/4HYxU4FyRJS7rGlarlAk4UuFsvWKwNqIRbK/R+mNz CEvHwBxCyUsHpRoe0sxJszuOvylSb+Zyl/AGw0KXRTCvVkzRa+vV2zbKn6jpNGOk9v68 Slu+oSLRQyXDnTZz1r3HcccijEJtsb3uSRD/2uvp14bnsr6d+h/dYP/LqAyeYlLb73Wf D/ag== X-Gm-Message-State: AMCzsaVaxXXk8tJdXAQzCNXzBF2llNNiRhNt8oxGxLlco7wUedl3Qipo kRau19LeBwnECIg7hHtApvyB/Q== X-Google-Smtp-Source: AOwi7QDgLP3rkacqYaj9G7hUGEL4dYp+WR5yNfgGEtZ3IxGgxGSti/BvNnXM5hdQCC/uzeR3SEW/bQ== X-Received: by 10.159.216.151 with SMTP id s23mr10467640plp.176.1507152091429; Wed, 04 Oct 2017 14:21:31 -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 , Kees Cook , 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 , Alexey Dobriyan , Andrew Morton , Paul Gortmaker , Chris Metcalf , "Paul E . McKenney" , Nicolas Pitre , Borislav Petkov , "Luis R . Rodriguez" , Greg Kroah-Hartman , Christopher Li , Steven Rostedt , Jason Baron , Dou Liyang , "Rafael J . Wysocki" , Mika Westerberg , 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 , Andrey Ryabinin , Jonathan Corbet , Matthew Wilcox , Michal Hocko , Peter Foley , Paul Bolle , Jiri Kosina , Rob Landley , "H . J . Lu" , 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, 4 Oct 2017 14:20:01 -0700 Message-Id: <20171004212003.28296-26-thgarnie@google.com> X-Mailer: git-send-email 2.14.2.920.gcf0c67979c-goog In-Reply-To: <20171004212003.28296-1-thgarnie@google.com> References: <20171004212003.28296-1-thgarnie@google.com> Subject: [kernel-hardening] [RFC v3 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. 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 --- arch/x86/Kconfig | 8 ++++++++ arch/x86/Makefile | 1 + 2 files changed, 9 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 1e4b399c64e5..b92f96923712 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2141,6 +2141,14 @@ config X86_GLOBAL_STACKPROTECTOR bool depends on CC_STACKPROTECTOR +config X86_PIE + bool + depends on X86_64 + select DEFAULT_HIDDEN + select DYNAMIC_MODULE_BASE + select MODULE_REL_CRCS if MODVERSIONS + select X86_GLOBAL_STACKPROTECTOR if CC_STACKPROTECTOR + config HOTPLUG_CPU bool "Support for hot-pluggable CPUs" depends on SMP diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 42774185a58a..c49855b4b1be 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -144,6 +144,7 @@ else KBUILD_CFLAGS += -mno-red-zone ifdef CONFIG_X86_PIE + KBUILD_CFLAGS += -fPIC KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/x86/kernel/module.lds else KBUILD_CFLAGS += -mcmodel=kernel