From patchwork Wed Aug 2 13:46:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 109219 Delivered-To: patch@linaro.org Received: by 10.140.101.6 with SMTP id t6csp2802839qge; Wed, 2 Aug 2017 06:46:51 -0700 (PDT) X-Received: by 10.98.72.216 with SMTP id q85mr22395845pfi.23.1501681610953; Wed, 02 Aug 2017 06:46:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501681610; cv=none; d=google.com; s=arc-20160816; b=q+yp048CiyBFrNbnKaUh2aNo8sq94+PJWxN8CUBW3kPqSMKFN/zKLZQsx9QzyNu76j bCkylLGlr4tfcdJ41KgDTEbDM/tmo0MExYeB+q0XhvwJ6mtzxtAQLb57LPanGE9pwv6r waUS40iVCSY9lFmYmMQOrMBQ24/QKqYtgpiVGo5GBkCgJdwVVqqVyYQcdT+Rr6yT4zVy yhiizlK2E6ElUKd/Gb+XEvdsfHe4ITSYrbaIupJqGKs50/pqNGdqfiPze/JQLtMHAVg+ eJGu5cl4B0SwTzQ+1cvkjrjPbPraq6jPKmKQIlEEj1Um5Ksh+OkjvF9hWw77dFHiusf3 45ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=qK/cay7PuA8K5dfmqzk5l/EiGqOt6s1U6T2OmzfirS4=; b=SH+EYVsORGuDLq7vfPceNPtLSjLW9bKhL7t7GdLqEfBCNTFM0A4IAQqwbTjWTwkL6P jg7ZkDgSBy5A0P0HUWMvlmIyWYxP6vVoLB0oNeSZHDx5/zYp0uBb+k42NZiijo03UDx7 BcL6KlDLEb86tezvPma9F5FUx+yc4glPCzyWJOFjlv35Vw7mlAtYFpDts87RKRgDyexy /SgwYw5rpvp77vrLfTF2QSU7vo70oGQ/gmWBNvoPnd53w4R2gySZb1CfkeVfmEZ9Y1av R/Wi/HbJdukzO2ldX5iyxhlCsOvhA4f+YX4Aj41pQBTEQJSJsINZazEIXHz6ZSe2Yr7z PMDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.b=jJ9dHKLJ; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f23si3888796pfd.231.2017.08.02.06.46.50; Wed, 02 Aug 2017 06:46:50 -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=@linaro.org header.b=jJ9dHKLJ; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752111AbdHBNqr (ORCPT + 25 others); Wed, 2 Aug 2017 09:46:47 -0400 Received: from mail-wm0-f42.google.com ([74.125.82.42]:36630 "EHLO mail-wm0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751974AbdHBNqo (ORCPT ); Wed, 2 Aug 2017 09:46:44 -0400 Received: by mail-wm0-f42.google.com with SMTP id t201so42015874wmt.1 for ; Wed, 02 Aug 2017 06:46:44 -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; bh=qK/cay7PuA8K5dfmqzk5l/EiGqOt6s1U6T2OmzfirS4=; b=jJ9dHKLJPx1ptYSdX+txBmv+Ymu5XzUFViGUVxu/4AG/dsNqJ39HP+znCaFZq1GCKD bQWec1zTRurk8qJfSoyIC2XddLbAKHyRxFyf0WMqlVnSO9MapAzsEqAk6W/015Bhwc/A fTqu7FcLwwZ6FYuAX7+BFuoPsKG2tB8BDcx0I= 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; bh=qK/cay7PuA8K5dfmqzk5l/EiGqOt6s1U6T2OmzfirS4=; b=WkC69/IWElwuydVAjK5LpKCyaJMTGBBYQbLkmXiFJoYajj5dDwlnsTUON7CcWRbK3H iOpDwmEVDSouoQpyWc4i2UdbBsaJfWKHvrrOlfUmyjljfrFOGjupmATNC5iTszrGkprg wKu1FvkDV85DDxG7kAVEzmKchoKA61W1rz5DtEVi6E89q/4TAo3LFD5Lh7qur7teeVtS P26fmrxO75Sn78a5uHZMLxtDnjFZnbhEaqzgVMdpuj8yu0PowRJyzzcQ73bStFtxh1h8 nPGE2c8nbainIqo6WuXapAC7HrGGcrT6xXg1d2knx5BOxET27gPBVbUlDOTYeWMb+MOi jFSw== X-Gm-Message-State: AIVw111vfQFpLanuasUybjNHuAp/IxX0BAQtyZwpuOR6MCWxU/hBHJBq kgM6V9RWuP8bMxOX X-Received: by 10.28.52.142 with SMTP id b136mr4056501wma.48.1501681603224; Wed, 02 Aug 2017 06:46:43 -0700 (PDT) Received: from localhost.localdomain ([105.144.196.245]) by smtp.gmail.com with ESMTPSA id g17sm5565041wrc.28.2017.08.02.06.46.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Aug 2017 06:46:39 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Jessica Yu , Arnd Bergmann , Russell King , Andrew Morton , Ingo Molnar , Kees Cook , Thomas Garnier , Nicolas Pitre Subject: [RFC PATCH] module: use relative references for __ksymtab entries Date: Wed, 2 Aug 2017 14:46:32 +0100 Message-Id: <20170802134632.19040-1-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org An ordinary arm64 defconfig build has ~64 KB worth of __ksymtab entries, each consisting of two 64-bit fields containing absolute references, to the symbol itself and to a char array containing its name, respectively. When we build the same configuration with KASLR enabled, we end up with an additional ~192 KB of relocations in the .init section, i.e., one 24 byte entry for each absolute reference, which all need to be processed at boot time. Given how the struct kernel_symbol that describes each entry is completely local to module.c (except for the references emitted by EXPORT_SYMBOL() itself), we can easily modify it to contain two 32-bit relative references instead. This reduces the size of the __ksymtab section by 50% for all 64-bit architectures, and gets rid of the runtime relocations entirely for architectures implementing KASLR, either via standard PIE linking (arm64) or using custom host tools (x86). Since EXPORT_SYMBOL() no longer refers to struct kernel_symbol, move the definition into module.c, which is its only user. Note that the binary search involving __ksymtab contents relies on each section being sorted by symbol name. This is implemented based on the input section names, not the names in the ksymtab entries, so this patch does not interfere with that. Cc: Jessica Yu Cc: Arnd Bergmann Cc: Russell King Cc: Andrew Morton Cc: Ingo Molnar Cc: Kees Cook Cc: Thomas Garnier Cc: Nicolas Pitre Signed-off-by: Ard Biesheuvel --- Note that ARM requires a little tweak to ensure that the unwind info related to the discarded code is discarded as well. I'm sure there will be an arch or two where something similar may be required. arch/arm/kernel/vmlinux.lds.S | 1 + include/asm-generic/export.h | 10 +------ include/linux/export.h | 27 +++++++++-------- kernel/module.c | 31 ++++++++++++++++---- 4 files changed, 42 insertions(+), 27 deletions(-) -- 2.11.0 diff --git a/arch/arm/kernel/vmlinux.lds.S b/arch/arm/kernel/vmlinux.lds.S index c83a7ba737d6..4bdba75e510c 100644 --- a/arch/arm/kernel/vmlinux.lds.S +++ b/arch/arm/kernel/vmlinux.lds.S @@ -89,6 +89,7 @@ SECTIONS #endif *(.discard) *(.discard.*) + *(.ARM.exidx.discard) } . = PAGE_OFFSET + TEXT_OFFSET; diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h index 719db1968d81..fac5b2e6df37 100644 --- a/include/asm-generic/export.h +++ b/include/asm-generic/export.h @@ -4,17 +4,9 @@ #ifndef KSYM_FUNC #define KSYM_FUNC(x) x #endif -#ifdef CONFIG_64BIT -#define __put .quad #ifndef KSYM_ALIGN #define KSYM_ALIGN 8 #endif -#else -#define __put .long -#ifndef KSYM_ALIGN -#define KSYM_ALIGN 4 -#endif -#endif #ifndef KCRC_ALIGN #define KCRC_ALIGN 4 #endif @@ -35,7 +27,7 @@ .section ___ksymtab\sec+\name,"a" .balign KSYM_ALIGN KSYM(__ksymtab_\name): - __put \val, KSYM(__kstrtab_\name) + .long \val - ., KSYM(__kstrtab_\name) - . .previous .section __ksymtab_strings,"a" KSYM(__kstrtab_\name): diff --git a/include/linux/export.h b/include/linux/export.h index 1a1dfdb2a5c6..06d673799ac8 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -24,12 +24,6 @@ #define VMLINUX_SYMBOL_STR(x) __VMLINUX_SYMBOL_STR(x) #ifndef __ASSEMBLY__ -struct kernel_symbol -{ - unsigned long value; - const char *name; -}; - #ifdef MODULE extern struct module __this_module; #define THIS_MODULE (&__this_module) @@ -60,17 +54,26 @@ extern struct module __this_module; #define __CRC_SYMBOL(sym, sec) #endif -/* For every exported symbol, place a struct in the __ksymtab section */ +/* + * For every exported symbol, place a struct in the __ksymtab section. + * Note that we have to visibly take the address of sym, so the compiler + * is forced to emit it, rather than inlining it or removing it + * altogether. Do so in a way that avoids taking the address statically, + * and emit that code into a section that is discarded by the linker. + */ #define ___EXPORT_SYMBOL(sym, sec) \ extern typeof(sym) sym; \ __CRC_SYMBOL(sym, sec) \ static const char __kstrtab_##sym[] \ - __attribute__((section("__ksymtab_strings"), aligned(1))) \ + __attribute__((section("__ksymtab_strings"), used, aligned(1))) \ = VMLINUX_SYMBOL_STR(sym); \ - static const struct kernel_symbol __ksymtab_##sym \ - __used \ - __attribute__((section("___ksymtab" sec "+" #sym), used)) \ - = { (unsigned long)&sym, __kstrtab_##sym } + static void * __attribute__((section(".discard"), used)) \ + __discard_##sym(void) { return (void *)&sym; } \ + asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \ + " .balign 8 \n" \ + " .long " VMLINUX_SYMBOL_STR(sym) "- . \n" \ + " .long " VMLINUX_SYMBOL_STR(__kstrtab_##sym) "- .\n" \ + " .previous \n") #if defined(__KSYM_DEPS__) diff --git a/kernel/module.c b/kernel/module.c index 40f983cbea81..f09529f695f9 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -88,6 +88,12 @@ /* If this is set, the section belongs in the init part of the module */ #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) +struct kernel_symbol +{ + signed int value_offset; + signed int name_offset; +}; + /* * Mutex protects: * 1) List of modules (also safely readable with preempt_disable), @@ -539,12 +545,23 @@ static bool check_symbol(const struct symsearch *syms, return true; } +static unsigned long kernel_symbol_value(const struct kernel_symbol *sym) +{ + return (unsigned long)&sym->value_offset + sym->value_offset; +} + +static const char *kernel_symbol_name(const struct kernel_symbol *sym) +{ + return (const char *)((unsigned long)&sym->name_offset + + sym->name_offset); +} + static int cmp_name(const void *va, const void *vb) { const char *a; const struct kernel_symbol *b; a = va; b = vb; - return strcmp(a, b->name); + return strcmp(a, kernel_symbol_name(b)); } static bool find_symbol_in_section(const struct symsearch *syms, @@ -2190,7 +2207,7 @@ void *__symbol_get(const char *symbol) sym = NULL; preempt_enable(); - return sym ? (void *)sym->value : NULL; + return sym ? (void *)kernel_symbol_value(sym) : NULL; } EXPORT_SYMBOL_GPL(__symbol_get); @@ -2220,10 +2237,12 @@ static int verify_export_symbols(struct module *mod) for (i = 0; i < ARRAY_SIZE(arr); i++) { for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { - if (find_symbol(s->name, &owner, NULL, true, false)) { + if (find_symbol(kernel_symbol_name(s), &owner, NULL, + true, false)) { pr_err("%s: exports duplicate symbol %s" " (owned by %s)\n", - mod->name, s->name, module_name(owner)); + mod->name, kernel_symbol_name(s), + module_name(owner)); return -ENOEXEC; } } @@ -2272,7 +2291,7 @@ static int simplify_symbols(struct module *mod, const struct load_info *info) ksym = resolve_symbol_wait(mod, info, name); /* Ok if resolved. */ if (ksym && !IS_ERR(ksym)) { - sym[i].st_value = ksym->value; + sym[i].st_value = kernel_symbol_value(ksym); break; } @@ -2532,7 +2551,7 @@ static int is_exported(const char *name, unsigned long value, ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); else ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); - return ks != NULL && ks->value == value; + return ks != NULL && kernel_symbol_value(ks) == value; } /* As per nm */