From patchwork Tue Jul 18 22:33:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9850091 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 7BBBA60392 for ; Tue, 18 Jul 2017 22:37:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67784285CB for ; Tue, 18 Jul 2017 22:37:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5BBDC285D8; Tue, 18 Jul 2017 22:37:32 +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 3AD32285CD for ; Tue, 18 Jul 2017 22:37:31 +0000 (UTC) Received: (qmail 28161 invoked by uid 550); 18 Jul 2017 22:34:51 -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 28077 invoked from network); 18 Jul 2017 22:34:48 -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=mAJv5V6I2lZ1xUFrgT12JDfH7dk1gDucWD68g8iaoDo=; b=N+cTED2dovvk2LYq3nUPFlASpyKY3zeoyYBSqKvMYAD4ptUkuPtPY1OsOMCCGFB3QI 3rFX94jJFnV4Wu0tVVi3xrCkgm79vsSyjFIQ0/92pb4VcseNI74jrfgx2zqtN42IXmDI Kn5CYrDNLfeJ2O0M46n4CNO3pf06ggQHp+SEv7k8fKNk0ofYomWoETc7g8NdxkSYqANo wQ8q69QQ9lhwrEYFaugKu7+lS4OzfzwCUVtzXoxFbZySCPNVE+pSq5hR6Jdd7UOwXwW5 hul620SI0JFBy9hui8g0kVDm3IZiBqD0SKH8OyjVVswzm1jXTnMmgbgnvn/mWsCYwOz6 PvRA== 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=mAJv5V6I2lZ1xUFrgT12JDfH7dk1gDucWD68g8iaoDo=; b=mzX9og46yIrkbEdf8gVqrrgfBe+3GogaKPufM27SrklvlJx6oA6hXpEJcqtOc44Tja X4AZ/Zr3UO7HZjBLi5XU7xGcOov6FFe6w9hNsp4rRYxhd4CYUqrupIJbON+c9z5q4uVP t1tI10/XsPFtp3S4kbK4FD/9rNNXyX0Fl/W0Hh8VTmRxSvdJKji7TEiGBrAFKeqtCUNn 1Y5i1WD7Dgr1ekhlKjNTox+/etxWQT8HI/anI4a8RIuwl6AxkhwBDQTyKnrbpVAoQXk2 PoqA7l3ClOSZjCT89twJhj7p9ONp3Q3sqvL4yUEQdzSmpFMZiwXBs0RMlbYNM3QJgcme SgXw== X-Gm-Message-State: AIVw112JDKHc6rUxIqiRmtZnSNsWtMGD+oZYlkGO1JT/PZvdEEm04YeZ LeD/W2PyLL0UxRUN X-Received: by 10.101.91.203 with SMTP id o11mr4025020pgr.206.1500417276104; Tue, 18 Jul 2017 15:34:36 -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 , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Andy Lutomirski , Borislav Petkov , "Kirill A . Shutemov" , Brian Gerst , Borislav Petkov , Christian Borntraeger , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Kees Cook , Paul Gortmaker , Chris Metcalf , "Paul E . McKenney" , Andrew Morton , Christopher Li , Dou Liyang , Masahiro Yamada , Daniel Borkmann , Markus Trippelsdorf , Peter Foley , Steven Rostedt , Tim Chen , Ard Biesheuvel , Catalin Marinas , Matthew Wilcox , Michal Hocko , Rob Landley , Jiri Kosina , "H . J . Lu" , Paul Bolle , Baoquan He , Daniel Micay Cc: x86@kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Tue, 18 Jul 2017 15:33:28 -0700 Message-Id: <20170718223333.110371-18-thgarnie@google.com> X-Mailer: git-send-email 2.13.2.932.g7449e964c-goog In-Reply-To: <20170718223333.110371-1-thgarnie@google.com> References: <20170718223333.110371-1-thgarnie@google.com> Subject: [kernel-hardening] [RFC 17/22] compiler: Option to default to hidden symbols X-Virus-Scanned: ClamAV using ClamSMTP Provide an option to default visibility to hidden except for key symbols. This option is disabled by default and will be used by x86_64 PIE support to remove errors between compilation units. Signed-off-by: Thomas Garnier --- arch/x86/boot/boot.h | 2 +- arch/x86/include/asm/setup.h | 2 +- include/asm-generic/sections.h | 6 ++++++ include/linux/compiler.h | 8 ++++++++ init/Kconfig | 7 +++++++ kernel/kallsyms.c | 16 ++++++++-------- 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h index ef5a9cc66fb8..d726c35bdd96 100644 --- a/arch/x86/boot/boot.h +++ b/arch/x86/boot/boot.h @@ -193,7 +193,7 @@ static inline bool memcmp_gs(const void *s1, addr_t s2, size_t len) } /* Heap -- available for dynamic lists. */ -extern char _end[]; +extern char _end[] __default_visibility; extern char *HEAP; extern char *heap_end; #define RESET_HEAP() ((void *)( HEAP = _end )) diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h index e4585a393965..f3ffad82bdc0 100644 --- a/arch/x86/include/asm/setup.h +++ b/arch/x86/include/asm/setup.h @@ -66,7 +66,7 @@ static inline void x86_ce4100_early_setup(void) { } * This is set up by the setup-routine at boot-time */ extern struct boot_params boot_params; -extern char _text[]; +extern char _text[] __default_visibility; static inline bool kaslr_enabled(void) { diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h index 532372c6cf15..27c12f6dd6e2 100644 --- a/include/asm-generic/sections.h +++ b/include/asm-generic/sections.h @@ -28,6 +28,9 @@ * __entry_text_start, __entry_text_end * __ctors_start, __ctors_end */ +#ifdef CONFIG_DEFAULT_HIDDEN +#pragma GCC visibility push(default) +#endif extern char _text[], _stext[], _etext[]; extern char _data[], _sdata[], _edata[]; extern char __bss_start[], __bss_stop[]; @@ -42,6 +45,9 @@ extern char __start_rodata[], __end_rodata[]; /* Start and end of .ctors section - used for constructor calls. */ extern char __ctors_start[], __ctors_end[]; +#ifdef CONFIG_DEFAULT_HIDDEN +#pragma GCC visibility pop +#endif extern __visible const void __nosave_begin, __nosave_end; diff --git a/include/linux/compiler.h b/include/linux/compiler.h index eca8ad75e28b..876b827fe4a7 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -78,6 +78,14 @@ extern void __chk_io_ptr(const volatile void __iomem *); #include #endif +/* Useful for Position Independent Code to reduce global references */ +#ifdef CONFIG_DEFAULT_HIDDEN +#pragma GCC visibility push(hidden) +#define __default_visibility __attribute__((visibility ("default"))) +#else +#define __default_visibility +#endif + /* * Generic compiler-dependent macros required for kernel * build go below this comment. Actual compiler/compiler version diff --git a/init/Kconfig b/init/Kconfig index 4fb5d6fc2c4f..a93626d40355 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1635,6 +1635,13 @@ config PROFILING config TRACEPOINTS bool +# +# Default to hidden visibility for all symbols. +# Useful for Position Independent Code to reduce global references. +# +config DEFAULT_HIDDEN + bool + source "arch/Kconfig" endmenu # General setup diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 127e7cfafa55..252019c8c3a9 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -32,24 +32,24 @@ * These will be re-linked against their real values * during the second link stage. */ -extern const unsigned long kallsyms_addresses[] __weak; -extern const int kallsyms_offsets[] __weak; -extern const u8 kallsyms_names[] __weak; +extern const unsigned long kallsyms_addresses[] __weak __default_visibility; +extern const int kallsyms_offsets[] __weak __default_visibility; +extern const u8 kallsyms_names[] __weak __default_visibility; /* * Tell the compiler that the count isn't in the small data section if the arch * has one (eg: FRV). */ extern const unsigned long kallsyms_num_syms -__attribute__((weak, section(".rodata"))); +__attribute__((weak, section(".rodata"))) __default_visibility; extern const unsigned long kallsyms_relative_base -__attribute__((weak, section(".rodata"))); +__attribute__((weak, section(".rodata"))) __default_visibility; -extern const u8 kallsyms_token_table[] __weak; -extern const u16 kallsyms_token_index[] __weak; +extern const u8 kallsyms_token_table[] __weak __default_visibility; +extern const u16 kallsyms_token_index[] __weak __default_visibility; -extern const unsigned long kallsyms_markers[] __weak; +extern const unsigned long kallsyms_markers[] __weak __default_visibility; static inline int is_kernel_inittext(unsigned long addr) {