From patchwork Thu Aug 10 17:26:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9894325 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 08A5860236 for ; Thu, 10 Aug 2017 17:29:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E3A581FF61 for ; Thu, 10 Aug 2017 17:29:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D7ADE27C05; Thu, 10 Aug 2017 17:29:12 +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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4DE0D285F0 for ; Thu, 10 Aug 2017 17:29:12 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfrEp-00083B-1b; Thu, 10 Aug 2017 17:27:15 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfrEn-0007zE-36 for xen-devel@lists.xenproject.org; Thu, 10 Aug 2017 17:27:13 +0000 Received: from [85.158.137.68] by server-9.bemta-3.messagelabs.com id 73/67-01995-0779C895; Thu, 10 Aug 2017 17:27:12 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrHIsWRWlGSWpSXmKPExsVyMfTASt386T2 RBtO3CVt83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBk7m2ezFCzVqth87iF7A+NdpS5GLg4hgRmM EocvnmMHcVgEXrFILDm6jhnEkRDoZ5XY++o5YxcjJ5ATJ7FtQSeQzQFkV0ps2y8LEhYSUJLYu mEpM8Skf4wS61b2MYEk2AS0JPY0zGcCSYgILBWUONl6jRHEYRZoYZJ48m8bWJWwgJfEvSPXWE FsFgFViacbjjOD2LwClhIdjRfZITabSDxpnQR2BSdQfNqymcwQqy0kpv5ayTSBUWABI8MqRo3 i1KKy1CJdIyO9pKLM9IyS3MTMHF1DA2O93NTi4sT01JzEpGK95PzcTYzA8KpnYGDcwTj1hN8h RkkOJiVR3uJP3ZFCfEn5KZUZicUZ8UWlOanFhxhlODiUJHgrpvVECgkWpaanVqRl5gADHSYtw cGjJMK7dSpQmre4IDG3ODMdInWK0ZLjypV1X5g4phzYDiRfTfj/jUmIJS8/L1VKnNcaZJ4ASE NGaR7cOFg0XmKUlRLmZWRgYBDiKUgtys0sQZV/xSjOwagkzLsTZApPZl4J3NZXQAcxAR0U4ds JclBJIkJKqoGxeEMjt/3XmITUpOgU1sAPH16eSr3LtWDClz8nD/wusqr/ms7GueTO/txdSbtt Sk8mMpn1fY4p3rFAwqm64q22yzKHaR+Uvkd8FrCbuNbgiOwm6/avr/Qk5FI48sKOGB8VO8n5q m3T1utcTGsKLtcuFm1+/HYnm8HJqCRRs7L326Yq1j7SslquxFKckWioxVxUnAgA+b1hQsECAA A= X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-9.tower-31.messagelabs.com!1502386030!54010964!1 X-Originating-IP: [209.85.192.169] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 50081 invoked from network); 10 Aug 2017 17:27:11 -0000 Received: from mail-pf0-f169.google.com (HELO mail-pf0-f169.google.com) (209.85.192.169) by server-9.tower-31.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 10 Aug 2017 17:27:11 -0000 Received: by mail-pf0-f169.google.com with SMTP id o86so5678465pfj.1 for ; Thu, 10 Aug 2017 10:27:10 -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=0hBEfcteIY5opNA06O+NdEQEPtJp6+EjG2WQFugh8rw=; b=god8+4pKAiZO/IheoX/Wkjewcyles9nJYx0p7M3wtBTmya1yCvq6u+zBOjddK+bAId kjam/rzPCk4yT2+VxIHU+sg01q+dhLJdy1tuxNzrrWf1uJUD20e9F+s5ihMVPVaL8kC7 LlXEseKIwhmuOxwf5I6tAzTct8alqYkaVQ9SnzDiUHfeqtOSyxG9FflNKzGfKCzJ8Z9H iFggTAQf3OFpQCSlMZ57gJAHJVZXE81qF73BbtMNaQ21nv1zZ5Yskqef7tPrUjiedqFN pxYrbg5iH+E9GJ+e6maMorjxQa4d/nOAkTrP+6b++34WmINXmdPu+v1vtQOcvi1apsS8 WL3A== 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=0hBEfcteIY5opNA06O+NdEQEPtJp6+EjG2WQFugh8rw=; b=pdfRR1WilbBFHzVLCScIRI+Rw2DZbRNUzz0sJypZlYZU+HxArFLCgZQblNn6Sd7V3P EjbqPk4LI4spOyY0k24Xg4iL49tCm7gQs7gvKTFlvw6MKgiYXcoIlyjvcKlqqtiRoah2 IZ4sfsKIqmY7FRE1WQGnTtr+LBHvTNlHVViSh81FVq+8Kd+vnRUNVhcW9genGe+yAFvL O1T/0LsQoZjqzuzLjmkHAxZwCLhuG2TbWAnvSxIuyA03qHS6rOM6Lin1MoZ0d7KMwxmq 24xS6S7PdzR/6XsrC1Zqm0KC2eXYPFs5u8Eyd8/ZaUqeaEWHfzGJazUmnDVIIYDQzpGt hz1g== X-Gm-Message-State: AHYfb5hPvI/IhCQ17BhTeDvg/OlFGsCsTOVa2RNpMtHfWMiZJTe1hhbK GZHTFr5FLpKu4ZPN X-Received: by 10.98.59.152 with SMTP id w24mr13303003pfj.63.1502386029051; Thu, 10 Aug 2017 10:27:09 -0700 (PDT) Received: from skynet.sea.corp.google.com ([172.31.92.33]) by smtp.gmail.com with ESMTPSA id p77sm11894925pfi.153.2017.08.10.10.27.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Aug 2017 10:27:08 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Arnd Bergmann , Thomas Garnier , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Tom Lendacky , Andy Lutomirski , Borislav Petkov , Brian Gerst , "Kirill A . Shutemov" , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Paul Gortmaker , Chris Metcalf , Andrew Morton , "Paul E . McKenney" , Nicolas Pitre , Christopher Li , "Rafael J . Wysocki" , Lukas Wunner , Mika Westerberg , Dou Liyang , Daniel Borkmann , Alexei Starovoitov , Masahiro Yamada , Markus Trippelsdorf , Steven Rostedt , Kees Cook , Rik van Riel , David Howells , Waiman Long , Kyle Huey , Peter Foley , Tim Chen , Catalin Marinas , Ard Biesheuvel , Michal Hocko , Matthew Wilcox , "H . J . Lu" , Paul Bolle , Rob Landley , Baoquan He , Daniel Micay Date: Thu, 10 Aug 2017 10:26:10 -0700 Message-Id: <20170810172615.51965-19-thgarnie@google.com> X-Mailer: git-send-email 2.14.0.434.g98096fd7a8-goog In-Reply-To: <20170810172615.51965-1-thgarnie@google.com> References: <20170810172615.51965-1-thgarnie@google.com> Cc: linux-arch@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org, linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [Xen-devel] [RFC v2 18/23] x86/relocs: Handle DYN relocations for PIE support X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Change the relocation tool to correctly handle DYN/PIE kernel where the relocation table does not reference symbols and percpu support is not needed. Also add support for R_X86_64_RELATIVE relocations that can be handled like a 64-bit relocation due to the usage of -Bsymbolic. Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit. Signed-off-by: Thomas Garnier --- arch/x86/tools/relocs.c | 74 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 73eb7fd4aec4..70f523dd68ff 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -642,6 +642,13 @@ static void add_reloc(struct relocs *r, uint32_t offset) r->offset[r->count++] = offset; } +/* Relocation found in a DYN binary, support only for 64-bit PIE */ +static int is_dyn_reloc(struct section *sec) +{ + return ELF_BITS == 64 && ehdr.e_type == ET_DYN && + sec->shdr.sh_info == SHT_NULL; +} + static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, const char *symname)) { @@ -652,6 +659,7 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, Elf_Sym *sh_symtab; struct section *sec_applies, *sec_symtab; int j; + int dyn_reloc = 0; struct section *sec = &secs[i]; if (sec->shdr.sh_type != SHT_REL_TYPE) { @@ -660,14 +668,20 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, sec_symtab = sec->link; sec_applies = &secs[sec->shdr.sh_info]; if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) { - continue; + if (!is_dyn_reloc(sec_applies)) + continue; + dyn_reloc = 1; } sh_symtab = sec_symtab->symtab; sym_strtab = sec_symtab->link->strtab; for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { Elf_Rel *rel = &sec->reltab[j]; - Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)]; - const char *symname = sym_name(sym_strtab, sym); + Elf_Sym *sym = NULL; + const char *symname = NULL; + if (!dyn_reloc) { + sym = &sh_symtab[ELF_R_SYM(rel->r_info)]; + symname = sym_name(sym_strtab, sym); + } process(sec, rel, sym, symname); } @@ -746,16 +760,21 @@ static int is_percpu_sym(ElfW(Sym) *sym, const char *symname) strncmp(symname, "init_per_cpu_", 13); } - static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, const char *symname) { unsigned r_type = ELF64_R_TYPE(rel->r_info); ElfW(Addr) offset = rel->r_offset; - int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname); + int shn_abs = 0; + int dyn_reloc = is_dyn_reloc(sec); - if (sym->st_shndx == SHN_UNDEF) - return 0; + if (!dyn_reloc) { + shn_abs = (sym->st_shndx == SHN_ABS) && + !is_reloc(S_REL, symname); + + if (sym->st_shndx == SHN_UNDEF) + return 0; + } /* * Adjust the offset if this reloc applies to the percpu section. @@ -769,6 +788,9 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, break; case R_X86_64_PC32: + if (dyn_reloc) + die("PC32 reloc in PIE DYN binary"); + /* * PC relative relocations don't need to be adjusted unless * referencing a percpu symbol. @@ -783,7 +805,7 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, /* * References to the percpu area don't need to be adjusted. */ - if (is_percpu_sym(sym, symname)) + if (!dyn_reloc && is_percpu_sym(sym, symname)) break; if (shn_abs) { @@ -814,6 +836,14 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, add_reloc(&relocs32, offset); break; + case R_X86_64_RELATIVE: + /* + * -Bsymbolic means we don't need the addend and we can reuse + * the original relocs64. + */ + add_reloc(&relocs64, offset); + break; + default: die("Unsupported relocation type: %s (%d)\n", rel_type(r_type), r_type); @@ -1044,6 +1074,21 @@ static void emit_relocs(int as_text, int use_real_mode) } } +/* Print a different header based on the type of relocation */ +static void print_reloc_header(struct section *sec) { + static int header_printed = 0; + int header_type = is_dyn_reloc(sec) ? 2 : 1; + + if (header_printed == header_type) + return; + header_printed = header_type; + + if (header_type == 2) + printf("reloc type\toffset\tvalue\n"); + else + printf("reloc section\treloc type\tsymbol\tsymbol section\n"); +} + /* * As an aid to debugging problems with different linkers * print summary information about the relocs. @@ -1053,6 +1098,18 @@ static void emit_relocs(int as_text, int use_real_mode) static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, const char *symname) { + + print_reloc_header(sec); + +#if ELF_BITS == 64 + if (is_dyn_reloc(sec)) { + printf("%s\t0x%lx\t0x%lx\n", + rel_type(ELF_R_TYPE(rel->r_info)), + rel->r_offset, + rel->r_addend); + return 0; + } +#endif printf("%s\t%s\t%s\t%s\n", sec_name(sec->shdr.sh_info), rel_type(ELF_R_TYPE(rel->r_info)), @@ -1063,7 +1120,6 @@ static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, static void print_reloc_info(void) { - printf("reloc section\treloc type\tsymbol\tsymbol section\n"); walk_relocs(do_reloc_info); }