From patchwork Mon Aug 14 12:53:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9898885 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 33C1A602BA for ; Mon, 14 Aug 2017 13:03:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2763428606 for ; Mon, 14 Aug 2017 13:03:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1C0652860F; Mon, 14 Aug 2017 13:03:25 +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=-2.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A7C9F28606 for ; Mon, 14 Aug 2017 13:03:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=4HCXUR7NfHWtE1TeXqkFTkpb/uuGr3UEYVus0knUV+M=; b=hefdbuKO/Yjir9qADHXWXTVUv1 dtgdFKknDi/e0C0Tn9/alWAkfoVyeq0JPqMXWYLbITHZ9fVmHcfu5svmldA8hv4sTdy+10ekL4KLx kd//5n3yF73XsTNroIJ6XSFneE10pe8LkOMJKXZjjGGccXkd+qoTsdcTFmXHpiaupa3+avce9fsTU CVG9uZZU09IjGPGcd1giaoVLqh9tQkJPJRtktshEGFHEzvrb/WPKpUZ8TDFxknOZc3ynIqv3zwhbM wDyC0oe7qpVhAUIBj3yDSliPeZ35/L3Xg7xCEL6Jy+1eO586C9UCmgS8y1auuvZRNQ9YqdlMjVGw4 td3Llngw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dhF1e-0006Rv-A1; Mon, 14 Aug 2017 13:03:22 +0000 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dhEuI-0007y0-Ss for linux-arm-kernel@lists.infradead.org; Mon, 14 Aug 2017 12:56:28 +0000 Received: by mail-wm0-x230.google.com with SMTP id f15so43013419wmg.1 for ; Mon, 14 Aug 2017 05:55:26 -0700 (PDT) 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=sOJNY1eYh5bDxGiJvi6hs9jLiu0I/1qCSyfPxJxe6SE=; b=Isb5uCpqdbjlAMTmd5ZLIj8OgNjk3sOURmY2473eaqnGItq/2YP+p5reKIxNxFzWy5 GNlauikOpeIkS9WORCGVLsPNRccH1OoLSCZ7ik13x2BK3bGHwnvUTjoICiTumsgpQB82 ddeYjWeoxRIXStNptJd8y0vZlHhQnALYvAb3c= 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=sOJNY1eYh5bDxGiJvi6hs9jLiu0I/1qCSyfPxJxe6SE=; b=rtT6n1Pih0AZthhczcoyeQ4eJN+WC8QivuFJX7ui+Ng+zuwi9D5hptZzqaskwNv/qG BrqRVIB4LZCDgNG9n+Zp8DzJgAKbKjeytiU0BWlz5L9AtpuXgq9zHd9wKfMA037jaGIh ZC58Z1n8JPao1BTsVR+2wdjXtikwptyguPIWAMq6tq7xcYgd4v+b7PvWMB8JsmFnvIzF vY9RMfhP+cmHG5s7zqhxBmWAaeZWkj4LRKGDxs3FTVIM1eLh7V8eX6vMkXdCfo3WIlEC psMV5kSZUPwemFSJpH954ZY5mrxn9yRL3QzIxrqPNMulKyDY0/mP9uHg5GWb+66WgnTB wH1g== X-Gm-Message-State: AHYfb5i0K1BA9ywcPkAyReVkyCrUgVXnbiIYPAwL0PW3qZwIJT4XupiJ E3xi3LoQwLQxRowo X-Received: by 10.28.225.139 with SMTP id y133mr3741806wmg.41.1502715325455; Mon, 14 Aug 2017 05:55:25 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id 91sm7981360wrg.83.2017.08.14.05.55.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Aug 2017 05:55:24 -0700 (PDT) From: Ard Biesheuvel To: kernel-hardening@lists.openwall.com Subject: [PATCH 18/30] ARM: kernel: make vmlinux buildable as a PIE executable Date: Mon, 14 Aug 2017 13:53:59 +0100 Message-Id: <20170814125411.22604-19-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170814125411.22604-1-ard.biesheuvel@linaro.org> References: <20170814125411.22604-1-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170814_055547_444457_4EE011E3 X-CRM114-Status: GOOD ( 17.11 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Kees Cook , Arnd Bergmann , Nicolas Pitre , Marc Zyngier , Ard Biesheuvel , Russell King , Dave Martin , Tony Lindgren , Matt Fleming , Thomas Garnier , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Update the build flags and linker script to allow vmlinux to be build 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 --- arch/arm/Kconfig | 4 ++++ arch/arm/Makefile | 5 +++++ arch/arm/include/asm/assembler.h | 7 ++++++- arch/arm/kernel/vmlinux.lds.S | 9 +++++++++ include/linux/hidden.h | 21 ++++++++++++++++++++ 5 files changed, 45 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..88d9c33e24f5 100644 --- a/arch/arm/Makefile +++ b/arch/arm/Makefile @@ -52,6 +52,11 @@ AS += -EL LD += -EL endif +ifeq ($(CONFIG_RELOCATABLE),y) +KBUILD_CFLAGS += -include $(srctree)/include/linux/hidden.h -fpic +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 09b6b28f2595..b2254c1dc2de 100644 --- a/arch/arm/include/asm/assembler.h +++ b/arch/arm/include/asm/assembler.h @@ -550,7 +550,12 @@ 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, cond - .if __LINUX_ARM_ARCH__ < 7 + .if CONFIG_RELOCATABLE == 1 + ldr_l \dst, 333f, \cond + .section ".data.rel.ro.local", "aw", %progbits +333: .long \imm + .previous + .elseif __LINUX_ARM_ARCH__ < 7 ldr\cond \dst, =\imm .else W(movw\cond\()) \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..49b7823eed6e --- /dev/null +++ b/include/linux/hidden.h @@ -0,0 +1,21 @@ + +/* + * 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)