From patchwork Thu Jan 31 19:24:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10791355 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4EAD213B5 for ; Thu, 31 Jan 2019 19:45:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 463303102E for ; Thu, 31 Jan 2019 19:45:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 39F8C31793; Thu, 31 Jan 2019 19:45:07 +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=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED 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 66F1B3102E for ; Thu, 31 Jan 2019 19:45:06 +0000 (UTC) Received: (qmail 11537 invoked by uid 550); 31 Jan 2019 19:43:40 -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 Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 20138 invoked from network); 31 Jan 2019 19:29:07 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=s01BexUIwWPAOZc8ULyS2OIx258H75BSpCIC9WlFXeA=; b=Co3V1O2wLJokDkFdLVsjREI0nao2Jg7jlHpj2lNY+2lrpHZRzNneoqScxkhcIpLze5 8S7D+RePNeJ29sqQDnQbf7k4atEUy6m58s/U+MttCo8N5HwI4Wir5FMsTfYD8Q4YcON4 JPocWAj6c1SizKDGsVfD4L5MMLc91rT/EnQkQ= 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:mime-version:content-transfer-encoding; bh=s01BexUIwWPAOZc8ULyS2OIx258H75BSpCIC9WlFXeA=; b=nSwpbvTimmMQDaZN2ovA6oIG1YeZPpKqvP62WRjC96PzsMa9cDECRzefqthI8z4mxB 64noN/Qy1vOCTvcE1foXmUV22EZy+f0IiN0kJ6UYEiCyLjtyGpdEmF9RrF+O/O/BNVv0 LVazeqcZ/trxXB3DPPTiel3O5G6Mi2BLA0qAOcQRKW0rjBuXcYBqWe4pFeukP69xENGb RPq7fEKuYxyhL1F1rhvmO/HZz7kI0cZPd45pqmwpU3fGfYerqP8gKxbZKBXLX9Nun0Mr y8Zl18Viu9HOhGugHNzgY/z//sx175q62k2QoYK9ogSQYE05RnX3kwYynzP/C4rTGPGG O+pQ== X-Gm-Message-State: AJcUukctxfl3H8QJY3efS1MtDOksejIPpSBD8jTB1YxIlaaObgIxwj5u SRgC5FnshlAtIBasCYzvtCzG5hOicJs= X-Google-Smtp-Source: ALg8bN6cmIR/qbhV/Uq/U9H2S0j+DXRELuumBvMvyw891Bchw+ZY/JtGEi73minvNQOLad44JyJxUA== X-Received: by 2002:a62:7042:: with SMTP id l63mr37555534pfc.89.1548962935607; Thu, 31 Jan 2019 11:28:55 -0800 (PST) From: Thomas Garnier To: kernel-hardening@lists.openwall.com Cc: kristen@linux.intel.com, Thomas Garnier , Masahiro Yamada , "Peter Zijlstra (Intel)" , Ingo Molnar , Andrew Morton , Kees Cook , Mathieu Desnoyers , Thomas Garnier , Nicholas Piggin , Sam Ravnborg , Palmer Dabbelt , Michael Forney , Cao jin , linux-kernel@vger.kernel.org Subject: [PATCH v6 16/27] compiler: Option to add PROVIDE_HIDDEN replacement for weak symbols Date: Thu, 31 Jan 2019 11:24:23 -0800 Message-Id: <20190131192533.34130-17-thgarnie@chromium.org> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog In-Reply-To: <20190131192533.34130-1-thgarnie@chromium.org> References: <20190131192533.34130-1-thgarnie@chromium.org> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Provide an option to have a PROVIDE_HIDDEN (linker script) entry for each weak symbol. This option solves an error in x86_64 where the linker optimizes PIE generated code to be non-PIE because --emit-relocs was used instead of -pie (to reduce dynamic relocations). Signed-off-by: Thomas Garnier --- init/Kconfig | 7 +++++++ scripts/link-vmlinux.sh | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/init/Kconfig b/init/Kconfig index 116e0de4817f..2261c9891631 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -2104,6 +2104,13 @@ config ASN1 inform it as to what tags are to be expected in a stream and what functions to call on what tags. +config WEAK_PROVIDE_HIDDEN + bool + help + Generate linker script PROVIDE_HIDDEN entries for all weak symbols. It + allows to prevent non-PIE code being replaced by the linker if the + emit-relocs option is used instead of PIE (useful for x86_64 PIE). + source "kernel/Kconfig.locks" config ARCH_HAS_SYNC_CORE_BEFORE_USERMODE diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index bc7f1fc1f55b..abf44a804c79 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -119,6 +119,17 @@ kallsyms() ${CC} ${aflags} -c -o ${2} ${afile} } +gen_weak_provide_hidden() +{ + if [ -n "${CONFIG_WEAK_PROVIDE_HIDDEN}" ]; then + local pattern="s/^\s\+ w \(\w\+\)$/PROVIDE_HIDDEN(\1 = .);/gp" + echo -e "SECTIONS {\n. = _end;" > .tmp_vmlinux_hiddenld + ${NM} ${1} | sed -n "${pattern}" >> .tmp_vmlinux_hiddenld + echo "}" >> .tmp_vmlinux_hiddenld + LDFLAGS_vmlinux="${LDFLAGS_vmlinux} -T .tmp_vmlinux_hiddenld" + fi +} + # Create map file with all symbols from ${1} # See mksymap for additional details mksysmap() @@ -200,6 +211,9 @@ modpost_link vmlinux.o # modpost vmlinux.o to check for section mismatches ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o +# Generate weak linker script +gen_weak_provide_hidden vmlinux.o + kallsymso="" kallsyms_vmlinux="" if [ -n "${CONFIG_KALLSYMS}" ]; then