Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3856827imm; Tue, 29 May 2018 15:23:18 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ3ZQtd5VEqsg5CBIAGWpbc7wvXyogBDVBNVres+trDj2csP7gzeNgzuyhYsTUXPRaUJkff X-Received: by 2002:a63:ad4a:: with SMTP id y10-v6mr187161pgo.330.1527632598929; Tue, 29 May 2018 15:23:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527632598; cv=none; d=google.com; s=arc-20160816; b=E1bgmERUmis0ALET30zNl9Lv4A8avudnhVV/gypQV93hMqo4+vt6abJs98SuMZ6ta6 xBU8MYWML6n99fnRZ3pokFZr9sA3/Jlztu/y7UxjJUR5w/BcCgbTXmvjB7Dlb5R63kqJ 9sgE02NgtzZ8FUdrWkvI0fGnh34z1kMKuXIvuaxjwz4L95gukC4ynoupQQiQKo0fz1qo z8c7NaSyvL90VDB3LlKoVD3vDkH/VSfCTu4nv7APn+zcrYvjM1xM8dvSYyGO456Ha1Ps iakeg0RkEmi/Oem1r79gllNsLAFJc03XxuSSGacvCGjPGiy8NiYtsejTWvzUzvuOiY/S PFhg== 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=P5KjS1vgVS34pLzcmc7il5aUG62D0LdZZkGAf+CycAE=; b=A0G0cTHFKnD6o1Jx2iua8YEYpV5reWvZmN5EPauSAwQ9I1s4r0ETt/Q7nOGDXmcdf6 BJeFIBnPPqYvUAVHMRm2UuGNhw8lWoIi2D3kf2h+YlsGV/0aUg7EwLINl8y9vWSPTIgs 0rxK3s9ci9OVhUy8PHv6RjZ8nET7bwaKPOYkfRY6gtiKXL4RKme1C3nk318KikaZZ3EU 9N2PByEQe1WXvENGYXr9TjADrNkWhLAj6TdxhZn/vD/Lo7GVjSLr9qLSlooiijKwyXZT 1PZpfXQam5de3bcd+y2tnNxNJ4vG/Bv+3kvQvvv766cJUkQueT47pNJbO7mD/cCvdK3b ggvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=egS94V0T; 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 t7-v6si34293626pfa.170.2018.05.29.15.23.04; Tue, 29 May 2018 15:23:18 -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=egS94V0T; 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 S968207AbeE2WVt (ORCPT + 99 others); Tue, 29 May 2018 18:21:49 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:35034 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968075AbeE2WRg (ORCPT ); Tue, 29 May 2018 18:17:36 -0400 Received: by mail-pf0-f196.google.com with SMTP id x9-v6so7949491pfm.2 for ; Tue, 29 May 2018 15:17:36 -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=P5KjS1vgVS34pLzcmc7il5aUG62D0LdZZkGAf+CycAE=; b=egS94V0TQFTWzzB07h3TBPwmvzvktPHKxTVLe1jZMSsCFJrykIqXMX7BRawyMnhf8T nuChUrISmruPzDHQEzPTpK2DIx3UytkWeJbrea2M6MNeE6SsJ9j35mPcYk8q1Y0fvXmd Iipja3EmAAiPVvY2G+OImjNLqXbOFg82z3dyInlxIgO5dc9yYbu7Yh6gucJFDqkKnZw8 bskJFHLg4drvFChtm531q+U1azuvRHJo0xgIxtrNgxU1WraSUQrO0cBYRMTSubrZHInc 3M03c/OssrPwZ0YO/ldo9rrMIot5XjLAvGGxCC3W62iaHI3P1MqhfbVelnRu0Xv+46Sj xOlA== 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=P5KjS1vgVS34pLzcmc7il5aUG62D0LdZZkGAf+CycAE=; b=TuFcTdpNWhwMgcHbi7aZO419vouJfb/neIut5dgHtsQNguV/dmrsbjtQURhe9TthkK T+M13XvAyCNzXoXeSR6iWt7d7ivIu4MByDs5GuJ8oEKQvQJi2xKcxcc1YUVXvTSzDNtv fRFc6w7Zq3hDxJXOTJJTYXS3Lrsdo1FPFTwBkh9oPDivTJ136yntyjQwwtDTYzFkrRJ+ HV7p547muQ/WUdgZNvviFjwDj1ImpIwfsUEntkK9OnlxNrFBZp8QOvnV/654oFiHMezl MCnYPOCEuxwnufHINZgmyBW9Njp3B2bGtj18HHpowgNerVj3yjIj+PSjRLHgQnA1C4CQ SSUw== X-Gm-Message-State: ALKqPwc0n7hazCgbZ28atLtV3ALaSuazFlIJx00VOw0TjDrReEcYgyGq AjuR0BHEr1l4vLcSCJdvPCpnmA== X-Received: by 2002:a62:89db:: with SMTP id n88-v6mr206034pfk.11.1527632255915; Tue, 29 May 2018 15:17:35 -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.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 May 2018 15:17:35 -0700 (PDT) From: Thomas Garnier To: kernel-hardening@lists.openwall.com Cc: Thomas Garnier , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Kate Stewart , Philippe Ombredanne , "H.J. Lu" , Greg Kroah-Hartman , linux-kernel@vger.kernel.org Subject: [PATCH v4 17/27] x86/relocs: Handle PIE relocations Date: Tue, 29 May 2018 15:15:18 -0700 Message-Id: <20180529221625.33541-18-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 Change the relocation tool to correctly handle relocations generated by -fPIE option: - Add relocation for each entry of the .got section given the linker does not generate R_X86_64_GLOB_DAT on a simple link. - Ignore R_X86_64_GOTPCREL. Signed-off-by: Thomas Garnier --- arch/x86/tools/relocs.c | 93 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 220e97841e49..a35cc337f883 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -32,6 +32,7 @@ struct section { Elf_Sym *symtab; Elf_Rel *reltab; char *strtab; + Elf_Addr *got; }; static struct section *secs; @@ -293,6 +294,35 @@ static Elf_Sym *sym_lookup(const char *symname) return 0; } +static Elf_Sym *sym_lookup_addr(Elf_Addr addr, const char **name) +{ + int i; + for (i = 0; i < ehdr.e_shnum; i++) { + struct section *sec = &secs[i]; + long nsyms; + Elf_Sym *symtab; + Elf_Sym *sym; + + if (sec->shdr.sh_type != SHT_SYMTAB) + continue; + + nsyms = sec->shdr.sh_size/sizeof(Elf_Sym); + symtab = sec->symtab; + + for (sym = symtab; --nsyms >= 0; sym++) { + if (sym->st_value == addr) { + if (name) { + *name = sym_name(sec->link->strtab, + sym); + } + return sym; + } + } + } + return 0; +} + + #if BYTE_ORDER == LITTLE_ENDIAN #define le16_to_cpu(val) (val) #define le32_to_cpu(val) (val) @@ -513,6 +543,33 @@ static void read_relocs(FILE *fp) } } +static void read_got(FILE *fp) +{ + int i; + for (i = 0; i < ehdr.e_shnum; i++) { + struct section *sec = &secs[i]; + sec->got = NULL; + if (sec->shdr.sh_type != SHT_PROGBITS || + strcmp(sec_name(i), ".got")) { + continue; + } + sec->got = malloc(sec->shdr.sh_size); + if (!sec->got) { + die("malloc of %d bytes for got failed\n", + sec->shdr.sh_size); + } + if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { + die("Seek to %d failed: %s\n", + sec->shdr.sh_offset, strerror(errno)); + } + if (fread(sec->got, 1, sec->shdr.sh_size, fp) + != sec->shdr.sh_size) { + die("Cannot read got: %s\n", + strerror(errno)); + } + } +} + static void print_absolute_symbols(void) { @@ -643,6 +700,32 @@ static void add_reloc(struct relocs *r, uint32_t offset) r->offset[r->count++] = offset; } +/* + * The linker does not generate relocations for the GOT for the kernel. + * If a GOT is found, simulate the relocations that should have been included. + */ +static void walk_got_table(int (*process)(struct section *sec, Elf_Rel *rel, + Elf_Sym *sym, const char *symname), + struct section *sec) +{ + int i; + Elf_Addr entry; + Elf_Sym *sym; + const char *symname; + Elf_Rel rel; + + for (i = 0; i < sec->shdr.sh_size/sizeof(Elf_Addr); i++) { + entry = sec->got[i]; + sym = sym_lookup_addr(entry, &symname); + if (!sym) + die("Could not found got symbol for entry %d\n", i); + rel.r_offset = sec->shdr.sh_addr + i * sizeof(Elf_Addr); + rel.r_info = ELF_BITS == 64 ? R_X86_64_GLOB_DAT + : R_386_GLOB_DAT; + process(sec, &rel, sym, symname); + } +} + static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, const char *symname)) { @@ -656,6 +739,8 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, struct section *sec = &secs[i]; if (sec->shdr.sh_type != SHT_REL_TYPE) { + if (sec->got) + walk_got_table(process, sec); continue; } sec_symtab = sec->link; @@ -765,6 +850,7 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, offset += per_cpu_load_addr; switch (r_type) { + case R_X86_64_GOTPCREL: case R_X86_64_NONE: /* NONE can be ignored. */ break; @@ -809,7 +895,7 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, * the relocations are processed. * Make sure that the offset will fit. */ - if ((int32_t)offset != (int64_t)offset) + if (r_type != R_X86_64_64 && (int32_t)offset != (int64_t)offset) die("Relocation offset doesn't fit in 32 bits\n"); if (r_type == R_X86_64_64) @@ -818,6 +904,10 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, add_reloc(&relocs32, offset); break; + case R_X86_64_GLOB_DAT: + add_reloc(&relocs64, offset); + break; + default: die("Unsupported relocation type: %s (%d)\n", rel_type(r_type), r_type); @@ -1087,6 +1177,7 @@ void process(FILE *fp, int use_real_mode, int as_text, read_strtabs(fp); read_symtabs(fp); read_relocs(fp); + read_got(fp); if (ELF_BITS == 64) percpu_init(); if (show_absolute_syms) { -- 2.17.0.921.gf22659ad46-goog