Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3856178imm; Tue, 29 May 2018 15:22:15 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJNumO6DeO6KzQjVt1cs0E90vCr0D7ShLUt1m3l3s7F18RqJaUuBA32oxz22YB/OQQFK0mE X-Received: by 2002:a63:6945:: with SMTP id e66-v6mr188012pgc.306.1527632535101; Tue, 29 May 2018 15:22:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527632535; cv=none; d=google.com; s=arc-20160816; b=xghg5YF2AHFx5nRTveyyPinuE94EZ1xd2eSvQfJgTPx1PQidPtFrfMEoPoU9GELRCf ReARP7FDcyKN/ZfWXU0Y9BZEaTmUTfO1LFPPM4gMvo31SkDS9P+IvWkqdvRHp3HfUxBe pMeckJoX6hKvQivXagFQ31tzVWoK6XbLibmZQb66JsjxtpRwhSYswtrcycx0DyLnxakf y75RfhP5cSEiIyQm6/vyyyB5GmGEnDvpp2fyMkswHDE0EagwhqRwuFv6FC36L/ZU1gGb hBIg7JstVzvrisxR6takDyTrotYPwawtFrzce2XR8MwYrVY4DpyvZ9EYSR+Hn0PUXkIu zA7g== 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=WEPPQRL0lo2GVz5s9Lpab+TBTjf13cI3VR/IEJ/DMdE=; b=IXQ7cMF9f7k9OxFlLS9D3ZWo/o3x2GNgp2gXw/5ZYOCUow8ZZHQgGsoT4tlAMY8oE7 +0Kp5+s66m0txMwwB2tRE7etoTYwSxrwTwkTyx1vrp7S426lCkZ5SeUpoWyejVNrB+KN 1fh/jb3d3evv9CtddeAlk9LZ2AjpJMCwUPMlBAQ2yBg7wLm4I4rTTrFFL8hdS9+jeko4 +tC/SL7sQywVqC6rraP+AW5Oy8X5VT5eSVyU+sXTaLvYBNoplXBBZuelAylDZN9rHTc0 p0PtgOr6WlmhPNGF3Eq7mpvgeRUqB/kEEtLtpmwZm66VMSK/wLGpCdB25C+8mqynIeoi qTog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MpJBfc66; 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 y7-v6si16921535pgc.656.2018.05.29.15.22.01; Tue, 29 May 2018 15:22:15 -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=MpJBfc66; 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 S968105AbeE2WRk (ORCPT + 99 others); Tue, 29 May 2018 18:17:40 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:39984 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968068AbeE2WRa (ORCPT ); Tue, 29 May 2018 18:17:30 -0400 Received: by mail-pg0-f66.google.com with SMTP id l2-v6so7180981pgc.7 for ; Tue, 29 May 2018 15:17:30 -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=WEPPQRL0lo2GVz5s9Lpab+TBTjf13cI3VR/IEJ/DMdE=; b=MpJBfc662Ry9kOE5DY007l9K8fQjjEWCU6njLHglGF4QlVDYvLf79/mymaHOLu2Sgn OkILdnUqyPf3zbFPL796NuHbtCiUv3zBnq+O+iJb/1hnuZqx4x9Ty7f/znhQTSJKydJW O5bo4VvQfTTHK0tO3RoOKmiqA46Hy+lE3OfDtqxEaNkxcyeTxAuAoUJthsQaIyoq+4Ps zZAhTbnn/oK4Wg+uKjyDaadtIPwfL6uQ5DBLBox962uqlZ180wElo9mVVR89rJzHfwYG d4qQYaGqoO8M6Pvy4CnAwVX/tceaAG1PFQM0wG54Yye8GVvLygGMZ2XKvSxHVFku7EMu YI/w== 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=WEPPQRL0lo2GVz5s9Lpab+TBTjf13cI3VR/IEJ/DMdE=; b=Wg3imI1us3nALcEQFrQp0324eFyXqJ3bPO5/OW5/zUsGGApQF7zdSlJ+uUZWjubPAt mY2AK90ElFcnYQO8nAGONybnwfS6OuaIW9NCJiMsOqHFvko+E8hDeV+Kr1e/UxTSrpO0 Q4phFYJmD7SMAFJkFzBBTIo81qCb72utLE20CZoZUS/ws8QJIFO13xOeHu/AeB9ehkat FLQx043kQSnZw/2HC7Ynp/KwKqEpN+FjftgBAFA5JhzY1fRJCENC+Q68Kae46GmkbB3t idC4M51InI32TyvP2JIgYwWLCk0HBcbpr4w1foBTDXWT0IKaQqq6ZJdhJuIhGbA6mRU4 2XaQ== X-Gm-Message-State: ALKqPwedHbAbdcR3hFRyPCC5uOwEOYno5IEQiuLmx38f1eKt0jAUaPb/ QkMnMLEDWUvCmdIpPiOKPvb9Ow== X-Received: by 2002:a62:4086:: with SMTP id f6-v6mr212479pfd.194.1527632249687; Tue, 29 May 2018 15:17:29 -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.17.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 May 2018 15:17:29 -0700 (PDT) From: Thomas Garnier To: kernel-hardening@lists.openwall.com Cc: Thomas Garnier , Skip Mathieu Desnoyers , Skip Frederic Weisbecker , Skip Dominik Brodowski , Skip Nicholas Piggin , Skip Kees Cook , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Borislav Petkov , "Luis R. Rodriguez" , Greg Kroah-Hartman , Arnd Bergmann , Christopher Li , Steven Rostedt , Jason Baron , Dou Liyang , Lukas Wunner , "Paul E. McKenney" , Andrew Morton , Peter Zijlstra , Randy Dunlap , Sergey Senozhatsky , Petr Mladek , Masahiro Yamada , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org Subject: [PATCH v4 15/27] compiler: Option to default to hidden symbols Date: Tue, 29 May 2018 15:15:16 -0700 Message-Id: <20180529221625.33541-16-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 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. The default visibility is also enabled for external symbols that are compared as they maybe equals (start/end of sections). In this case, older versions of GCC will remove the comparison if the symbols are hidden. This issue exists at least on gcc 4.9 and before. Signed-off-by: Thomas Garnier --- arch/x86/boot/boot.h | 2 +- arch/x86/include/asm/setup.h | 2 +- arch/x86/kernel/cpu/microcode/core.c | 4 ++-- drivers/base/firmware_loader/main.c | 4 ++-- include/asm-generic/sections.h | 6 ++++++ include/linux/compiler.h | 7 +++++++ init/Kconfig | 7 +++++++ kernel/kallsyms.c | 16 ++++++++-------- kernel/trace/trace.h | 4 ++-- lib/dynamic_debug.c | 4 ++-- 10 files changed, 38 insertions(+), 18 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 ae13bc974416..083a6e99b884 100644 --- a/arch/x86/include/asm/setup.h +++ b/arch/x86/include/asm/setup.h @@ -68,7 +68,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/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c index 77e201301528..6a4f5d9d7eb6 100644 --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -149,8 +149,8 @@ static bool __init check_loader_disabled_bsp(void) return *res; } -extern struct builtin_fw __start_builtin_fw[]; -extern struct builtin_fw __end_builtin_fw[]; +extern struct builtin_fw __start_builtin_fw[] __default_visibility; +extern struct builtin_fw __end_builtin_fw[] __default_visibility; bool get_builtin_firmware(struct cpio_data *cd, const char *name) { diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c index 0943e7065e0e..2ffd019af2d4 100644 --- a/drivers/base/firmware_loader/main.c +++ b/drivers/base/firmware_loader/main.c @@ -94,8 +94,8 @@ static struct firmware_cache fw_cache; #ifdef CONFIG_FW_LOADER -extern struct builtin_fw __start_builtin_fw[]; -extern struct builtin_fw __end_builtin_fw[]; +extern struct builtin_fw __start_builtin_fw[] __default_visibility; +extern struct builtin_fw __end_builtin_fw[] __default_visibility; static void fw_copy_to_prealloc_buf(struct firmware *fw, void *buf, size_t size) diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h index 849cd8eb5ca0..0a0e23405ddd 100644 --- a/include/asm-generic/sections.h +++ b/include/asm-generic/sections.h @@ -32,6 +32,9 @@ * __softirqentry_text_start, __softirqentry_text_end * __start_opd, __end_opd */ +#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[]; @@ -49,6 +52,9 @@ extern char __start_once[], __end_once[]; /* 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 /* Start and end of .opd section - used for function descriptors. */ extern char __start_opd[], __end_opd[]; diff --git a/include/linux/compiler.h b/include/linux/compiler.h index ab4711c63601..a9ac84e37af9 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -278,6 +278,13 @@ unsigned long read_word_at_a_time(const void *addr) __u.__val; \ }) +#ifdef CONFIG_DEFAULT_HIDDEN +#pragma GCC visibility push(hidden) +#define __default_visibility __attribute__((visibility ("default"))) +#else +#define __default_visibility +#endif + #endif /* __KERNEL__ */ #endif /* __ASSEMBLY__ */ diff --git a/init/Kconfig b/init/Kconfig index e4acab9f9fd1..f16247675f84 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1694,6 +1694,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 a23e21ada81b..f4e58b7a6daf 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -29,24 +29,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; /* * Expand a compressed symbol data into the resulting uncompressed string, diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 6fb46a06c9dc..e659f452cf8c 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -1746,8 +1746,8 @@ extern int trace_event_enable_disable(struct trace_event_file *file, int enable, int soft_disable); extern int tracing_alloc_snapshot(void); -extern const char *__start___trace_bprintk_fmt[]; -extern const char *__stop___trace_bprintk_fmt[]; +extern const char *__start___trace_bprintk_fmt[] __default_visibility; +extern const char *__stop___trace_bprintk_fmt[] __default_visibility; extern const char *__start___tracepoint_str[]; extern const char *__stop___tracepoint_str[]; diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c index c7c96bc7654a..40b752b53627 100644 --- a/lib/dynamic_debug.c +++ b/lib/dynamic_debug.c @@ -37,8 +37,8 @@ #include #include -extern struct _ddebug __start___verbose[]; -extern struct _ddebug __stop___verbose[]; +extern struct _ddebug __start___verbose[] __default_visibility; +extern struct _ddebug __stop___verbose[] __default_visibility; struct ddebug_table { struct list_head link; -- 2.17.0.921.gf22659ad46-goog