From patchwork Sun Sep 3 12:07:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9936153 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 C2F466037D for ; Sun, 3 Sep 2017 12:12:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B4AE3286A9 for ; Sun, 3 Sep 2017 12:12:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A970E286B3; Sun, 3 Sep 2017 12:12: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_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 4B69F286B0 for ; Sun, 3 Sep 2017 12:12:04 +0000 (UTC) Received: (qmail 16348 invoked by uid 550); 3 Sep 2017 12:09:31 -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 16246 invoked from network); 3 Sep 2017 12:09:29 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zDDlmLPIKHGtMS8kChJ6jxvJSrxvj64hS1SRaZy7/LQ=; b=gerPZ0lPz0rjWaOOoQNzG4Sa9u8DPdDdMJDOVFMRvICTAxE5ECgun9rHL5A0Nq0+V4 C0hGVhVwR2wIFGMWX+pcI44g6g0PT8qiK526MCRK0eQTRnBvQVJ//CrqKCvz/SkUYQrm wWOEm60mBr1+4hbL5KtBuCV5OknUY63oPBUIo= 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=zDDlmLPIKHGtMS8kChJ6jxvJSrxvj64hS1SRaZy7/LQ=; b=NifXVqQ3JK2Vhm0BIxUy/yOmCfhBB0kXxl1yIsspxiM2+wXq8KlJZMoSSqgJoaC88o kk8Q+rzRjnqvEditLUVv8ig9GOUaqX95N+/hSh9JYUnfiT34GmT0e/AJnDHbEXJIvvOS sZ1ggzY3s0/wNkvNCuuFDqSA64o3m3gVrWoasC0WLMRP8MxJdTS01CMqSl7YWOUJek3U Fw4mYNTdlSqiGMOr3WmR3x9ZbNT++q4F7E+kwfnuptHC02I4hJ5KiwRZSfRVbO2+X3rx bUh4wKegg5w8310LJohf2h1WyXl/jbhLD8kjGmQiobvfwAzVhCDUMebFFgRWyON9g3s9 WO+A== X-Gm-Message-State: AHPjjUhJ17Ioft1yF70OyfZMaigVbz73n3avgbLb4WjM881fI60gDmxT qqSB0olhX4hL04+j X-Google-Smtp-Source: ADKCNb6r/EX2hRyyXnLLHiKexkUlOLNpwaZs5kQJqp0Dmcfc1+0czAy9fs0zTGox3mE+B8rjePaqhA== X-Received: by 10.28.236.67 with SMTP id k64mr2082790wmh.146.1504440558143; Sun, 03 Sep 2017 05:09:18 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, kernel-hardening@lists.openwall.com Cc: Ard Biesheuvel , Arnd Bergmann , Nicolas Pitre , Russell King , Kees Cook , Thomas Garnier , Marc Zyngier , Mark Rutland , Tony Lindgren , Matt Fleming , Dave Martin Date: Sun, 3 Sep 2017 13:07:47 +0100 Message-Id: <20170903120757.14968-20-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170903120757.14968-1-ard.biesheuvel@linaro.org> References: <20170903120757.14968-1-ard.biesheuvel@linaro.org> Subject: [kernel-hardening] [PATCH v2 19/29] ARM: kernel: make vmlinux buildable as a PIE executable X-Virus-Scanned: ClamAV using ClamSMTP Update the build flags and linker script to allow vmlinux to be built as a PIE binary, which retains relocation information about absolute symbol references so that they can be fixed up at runtime. This will be used for implementing KASLR, Cc: Russell King Signed-off-by: Ard Biesheuvel Acked-by: Nicolas Pitre --- arch/arm/Kconfig | 4 ++++ arch/arm/Makefile | 5 +++++ arch/arm/include/asm/assembler.h | 2 +- arch/arm/kernel/vmlinux.lds.S | 9 +++++++++ include/linux/hidden.h | 20 ++++++++++++++++++++ scripts/module-common.lds | 1 + 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 61a0cb15067e..300add3b8023 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -2085,6 +2085,10 @@ config DMI firmware need to be enabled. This would require the DMI subsystem to be enabled much earlier than we do on ARM, which is non-trivial. +config RELOCATABLE + bool + select HAVE_ARCH_PREL32_RELOCATIONS + endmenu menu "CPU Power Management" diff --git a/arch/arm/Makefile b/arch/arm/Makefile index 47d3a1ab08d2..8544c8148b9f 100644 --- a/arch/arm/Makefile +++ b/arch/arm/Makefile @@ -52,6 +52,11 @@ AS += -EL LD += -EL endif +ifeq ($(CONFIG_RELOCATABLE),y) +KBUILD_CFLAGS += -fpic -include $(srctree)/include/linux/hidden.h +LDFLAGS_vmlinux += -pie -shared -Bsymbolic +endif + # # The Scalar Replacement of Aggregates (SRA) optimization pass in GCC 4.9 and # later may result in code being generated that handles signed short and signed diff --git a/arch/arm/include/asm/assembler.h b/arch/arm/include/asm/assembler.h index ae43859a9000..0586e1c77a80 100644 --- a/arch/arm/include/asm/assembler.h +++ b/arch/arm/include/asm/assembler.h @@ -555,7 +555,7 @@ THUMB( orr \reg , \reg , #PSR_T_BIT ) * mov_l - move a constant value or [relocated] address into a register */ .macro mov_l, dst:req, imm:req - .if __LINUX_ARM_ARCH__ < 7 + .if CONFIG_RELOCATABLE == 1 || __LINUX_ARM_ARCH__ < 7 ldr \dst, =\imm .else movw \dst, #:lower16:\imm diff --git a/arch/arm/kernel/vmlinux.lds.S b/arch/arm/kernel/vmlinux.lds.S index c83a7ba737d6..5853d4be2067 100644 --- a/arch/arm/kernel/vmlinux.lds.S +++ b/arch/arm/kernel/vmlinux.lds.S @@ -89,6 +89,9 @@ SECTIONS #endif *(.discard) *(.discard.*) + *(.ARM.exidx.discard.text) + *(.interp .dynamic) + *(.dynsym .dynstr .hash) } . = PAGE_OFFSET + TEXT_OFFSET; @@ -209,6 +212,12 @@ SECTIONS __smpalt_end = .; } #endif + .rel.dyn : ALIGN(8) { + __rel_begin = .; + *(.rel .rel.* .rel.dyn) + } + __rel_end = ADDR(.rel.dyn) + SIZEOF(.rel.dyn); + .init.pv_table : { __pv_table_begin = .; *(.pv_table) diff --git a/include/linux/hidden.h b/include/linux/hidden.h new file mode 100644 index 000000000000..781fb6f0c742 --- /dev/null +++ b/include/linux/hidden.h @@ -0,0 +1,20 @@ +/* + * GCC assumes that we are building shared libraries or hosted binaries + * when the -fpic or -fpie switches are used. This results in all references + * to symbols with external linkage to be redirected via entries in the global + * offset table (GOT), which keeps .text pages clean and reduces the footprint + * of CoWed dirty pages to the GOT itself. It also allows symbol preemption, + * which is mandatory under ELF rules for shared libraries. + * + * For the kernel, we use PIC so that we can relocate the executable image at + * runtime. This does not involve CoW or symbol preemption, and we'd rather + * have relative references instead of absolute ones whenever possible. + * So set the default visibility to hidden: this informs the compiler that + * none of our symbols will ever be exported from a shared library, allowing + * it to use relative references where possible. + * + * Note that simply passing -fvisibility=hidden is not sufficient to achieve + * this: In that case, definitions will be marked hidden, but declarations + * will not, and we still end up with GOT entries unnecessarily. + */ +#pragma GCC visibility push(hidden) diff --git a/scripts/module-common.lds b/scripts/module-common.lds index d61b9e8678e8..55bb41f32c8a 100644 --- a/scripts/module-common.lds +++ b/scripts/module-common.lds @@ -7,6 +7,7 @@ SECTIONS { /DISCARD/ : { *(.discard) *(.discard.*) + *(*.discard.*) } __ksymtab 0 : { *(SORT(___ksymtab+*)) }