Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3854761imm; Tue, 29 May 2018 15:19:55 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJHVenPod5c2369wKleAS+plxjKY76WiIbGhsrIIka8KVRE47dwauUOCUGlOzxhITyz/c2M X-Received: by 2002:a62:828c:: with SMTP id w134-v6mr225361pfd.138.1527632395458; Tue, 29 May 2018 15:19:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527632395; cv=none; d=google.com; s=arc-20160816; b=GaTxpQI/1AWmEUjVqPXTEi0rT8IqqXR2/JHXK6Kf2I6CRD1XUvNKQrZTQDIAPoG1nx ZAArBOW30deDYLFZJ97JoxOdqUNSScqsTsGZyLOYteL3D2tXAEntgzPKPywVxIMzkSSy jpV5I7D0YOERSaFiOxQf6BFCFeJYitCsLUWcJ2vW+mfY6YiGeD5Wf6UBiOypjMeFtVuL vf8hCUdqCZPtDHXuzgc23TWPDVVAjHss4kRqhsFwvHoe/14pWC0IgL1yyugCj/Xej8M+ 2Mmz+rOwJzrfEW3mCiVYCDLXmUEQJ0j6uuF3QVDQQtXXI5Yc0r/O2ybktaVboLDBG3rw vW7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=n7jYEivtFQl0l4Y8IkdQ2ng4077WqoTQ54sRaJdxc2E=; b=He5Y+IB33zViS7dilSDFAG4JjM+aRRiXgqMAwXIzkPxUucaUYUmLY6TQEHmTNovyMB iNZKNPBoWePWf3WsbRYboQqrvE/4rSdJzbvknw5rRsMRu/gEwjzu4nTl4ztnzlL1n1aa KfwZ1m/U85tDzQA5OAmARcz8O+3OlVcEPsN+CCWDYHdeeV3ETJsrNXHpO71vl4rqbZgs C3YM4+EDqb2sAc/OJvpQJqfa5nFlp8aalpW7jw4wqjLt2VTlKkrr54MPTRLlhXLhJqjA lSEQpzWvd5AdLbBjjphmk454OlNAfZ5enreFn2OjFaGK+H1QTwTUo/Nwbr3cujgfAjGx YRUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hfXi9lbb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k6-v6si34272853pla.78.2018.05.29.15.19.41; Tue, 29 May 2018 15:19:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hfXi9lbb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S968252AbeE2WSY (ORCPT + 99 others); Tue, 29 May 2018 18:18:24 -0400 Received: from mail-pl0-f65.google.com ([209.85.160.65]:42983 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968222AbeE2WSQ (ORCPT ); Tue, 29 May 2018 18:18:16 -0400 Received: by mail-pl0-f65.google.com with SMTP id u6-v6so9773563pls.9 for ; Tue, 29 May 2018 15:18:15 -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:in-reply-to:references; bh=n7jYEivtFQl0l4Y8IkdQ2ng4077WqoTQ54sRaJdxc2E=; b=hfXi9lbbtGaBYbjh3E0goec62rbrSucc4UYXhTcHSvnt7oZE/4TvmtX/EceOAA00DD ailPDrmyJnrl/NwwLV7f9eRW+RgwnqStyudtjVxkupMZt5cFSQdqcDyp1RyIzc+UC34L hQizfv5vei+K39HLlgD7NSGU1tg0hUdN9AeDZbjmr+7GZI40n9gamSKI49BV1URyVOqz jfxl8QwOLc11nLqc6q0wboNnrC64k+xgPdY5JQ1IjL0i31Yqof2P1gaKuHOJAV9kyPd0 2fdj0la5ZhQ6OWiyDMFqBKMOX2cUj54IMFyUxJEx+2EZEwnsOWEgB2ZoYzfVaeJxhKdE z5mw== 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=n7jYEivtFQl0l4Y8IkdQ2ng4077WqoTQ54sRaJdxc2E=; b=aise3TwlnvoO5ueTZ5DdVwRSOp0Qxstnp3itRobncmP9Y8+aMBiNxxAjsvqn8xvrwT 2dG8e67zGuAi2eJ960bl3RvwY0Fgpxt4L0DYJU7ZjYPm3EHCIuBLxBGGEXzZySCv5iAl TqODMvaIOoTb1Zu1u29V/vB7Zu3e8T9GC9w2Sz9ekt9KGMK/UUOCvEQ50bMPYrnfTL6I UzuXRywEczVAqHwpNl+5RTrD8RvIv3WAvZB6xpeCBUzDH8PnSmoVdz2e4Ny3JKi+X4+o VA3+lQbWmLwD6aViYlOcJGKKfG4gis4kJwPXK29KSGnd9ZEeaVOqLokue2E4DgFKG7VU bAQw== X-Gm-Message-State: ALKqPweXnGGpCdkoiQCJB90W01oNWe35JETuQJrKR36ZWy7jZ2e/mCQI XzH0o5+JOeaRqempF+JwefXrxg== X-Received: by 2002:a17:902:6f16:: with SMTP id w22-v6mr231627plk.216.1527632295237; Tue, 29 May 2018 15:18:15 -0700 (PDT) Received: from skynet.sea.corp.google.com ([2620:15c:17:4:29de:3bb1:1270:e679]) by smtp.gmail.com with ESMTPSA id o84-v6sm78767935pfi.27.2018.05.29.15.18.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 May 2018 15:18:14 -0700 (PDT) From: Thomas Garnier To: kernel-hardening@lists.openwall.com Cc: Thomas Garnier , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 25/27] x86/pie: Add option to build the kernel as PIE Date: Tue, 29 May 2018 15:15:26 -0700 Message-Id: <20180529221625.33541-26-thgarnie@google.com> X-Mailer: git-send-email 2.17.0.921.gf22659ad46-goog In-Reply-To: <20180529221625.33541-1-thgarnie@google.com> References: <20180529221625.33541-1-thgarnie@google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 94a00d81ec18..47cf21e452d2 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2214,6 +2214,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) -- 2.17.0.921.gf22659ad46-goog