Skip to content

Commit e0bf7b8

Browse files
amlutoH. Peter Anvin
authored andcommitted
x86/vdso: Hack to keep 64-bit Go programs working
The Go runtime has a buggy vDSO parser that currently segfaults. This writes an empty SHT_DYNSYM entry that causes Go's runtime to malfunction by thinking that the vDSO is empty rather than malfunctioning by running off the end and segfaulting. This affects x86-64 only as far as we know, so we do not need this for the i386 and x32 vdsos. Signed-off-by: Andy Lutomirski <luto@amacapital.net> Link: http://lkml.kernel.org/r/d10618176c4bd39b457a5e85c497295c90cab1bc.1402620737.git.luto@amacapital.net Signed-off-by: H. Peter Anvin <hpa@zytor.com>
1 parent b4b31f6 commit e0bf7b8

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

arch/x86/vdso/Makefile

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ vdso-install-$(VDSO32-y) += $(vdso32-images)
1515

1616

1717
# files to link into the vdso
18-
vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o
19-
20-
vobjs-$(VDSOX32-y) += $(vobjx32s-compat)
21-
22-
# Filter out x32 objects.
23-
vobj64s := $(filter-out $(vobjx32s-compat),$(vobjs-y))
18+
vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdso-fakesections.o
19+
vobjs-nox32 := vdso-fakesections.o
2420

2521
# files to link into kernel
2622
obj-y += vma.o
@@ -34,7 +30,7 @@ vdso_img-$(VDSO32-y) += 32-sysenter
3430

3531
obj-$(VDSO32-y) += vdso32-setup.o
3632

37-
vobjs := $(foreach F,$(vobj64s),$(obj)/$F)
33+
vobjs := $(foreach F,$(vobjs-y),$(obj)/$F)
3834

3935
$(obj)/vdso.o: $(obj)/vdso.so
4036

@@ -104,7 +100,13 @@ VDSO_LDFLAGS_vdsox32.lds = -Wl,-m,elf32_x86_64 \
104100
-Wl,-z,max-page-size=4096 \
105101
-Wl,-z,common-page-size=4096
106102

107-
vobjx32s-y := $(vobj64s:.o=-x32.o)
103+
# 64-bit objects to re-brand as x32
104+
vobjs64-for-x32 := $(filter-out $(vobjs-nox32),$(vobjs-y))
105+
106+
# x32-rebranded versions
107+
vobjx32s-y := $(vobjs64-for-x32:.o=-x32.o)
108+
109+
# same thing, but in the output directory
108110
vobjx32s := $(foreach F,$(vobjx32s-y),$(obj)/$F)
109111

110112
# Convert 64bit object file to x32 for x32 vDSO.

arch/x86/vdso/vdso-fakesections.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2014 Andy Lutomirski
3+
* Subject to the GNU Public License, v.2
4+
*
5+
* Hack to keep broken Go programs working.
6+
*
7+
* The Go runtime had a couple of bugs: it would read the section table to try
8+
* to figure out how many dynamic symbols there were (it shouldn't have looked
9+
* at the section table at all) and, if there were no SHT_SYNDYM section table
10+
* entry, it would use an uninitialized value for the number of symbols. As a
11+
* workaround, we supply a minimal section table. vdso2c will adjust the
12+
* in-memory image so that "vdso_fake_sections" becomes the section table.
13+
*
14+
* The bug was introduced by:
15+
* https://code.google.com/p/go/source/detail?r=56ea40aac72b (2012-08-31)
16+
* and is being addressed in the Go runtime in this issue:
17+
* https://code.google.com/p/go/issues/detail?id=8197
18+
*/
19+
20+
#ifndef __x86_64__
21+
#error This hack is specific to the 64-bit vDSO
22+
#endif
23+
24+
#include <linux/elf.h>
25+
26+
extern const __visible struct elf64_shdr vdso_fake_sections[];
27+
const __visible struct elf64_shdr vdso_fake_sections[] = {
28+
{
29+
.sh_type = SHT_DYNSYM,
30+
.sh_entsize = sizeof(Elf64_Sym),
31+
}
32+
};

arch/x86/vdso/vdso2c.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
1818
const char *secstrings;
1919
uint64_t syms[NSYMS] = {};
2020

21+
uint64_t fake_sections_value = 0, fake_sections_size = 0;
22+
2123
Elf_Phdr *pt = (Elf_Phdr *)(addr + GET_LE(&hdr->e_phoff));
2224

2325
/* Walk the segment table. */
@@ -84,6 +86,7 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
8486
GET_LE(&symtab_hdr->sh_entsize) * i;
8587
const char *name = addr + GET_LE(&strtab_hdr->sh_offset) +
8688
GET_LE(&sym->st_name);
89+
8790
for (k = 0; k < NSYMS; k++) {
8891
if (!strcmp(name, required_syms[k])) {
8992
if (syms[k]) {
@@ -93,6 +96,13 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
9396
syms[k] = GET_LE(&sym->st_value);
9497
}
9598
}
99+
100+
if (!strcmp(name, "vdso_fake_sections")) {
101+
if (fake_sections_value)
102+
fail("duplicate vdso_fake_sections\n");
103+
fake_sections_value = GET_LE(&sym->st_value);
104+
fake_sections_size = GET_LE(&sym->st_size);
105+
}
96106
}
97107

98108
/* Validate mapping addresses. */
@@ -112,11 +122,14 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
112122
if (syms[sym_end_mapping] % 4096)
113123
fail("end_mapping must be a multiple of 4096\n");
114124

115-
/* Remove sections. */
116-
hdr->e_shoff = 0;
117-
hdr->e_shentsize = 0;
118-
hdr->e_shnum = 0;
119-
hdr->e_shstrndx = SHN_UNDEF; /* SHN_UNDEF == 0 */
125+
/* Remove sections or use fakes */
126+
if (fake_sections_size % sizeof(Elf_Shdr))
127+
fail("vdso_fake_sections size is not a multiple of %ld\n",
128+
(long)sizeof(Elf_Shdr));
129+
PUT_LE(&hdr->e_shoff, fake_sections_value);
130+
PUT_LE(&hdr->e_shentsize, fake_sections_value ? sizeof(Elf_Shdr) : 0);
131+
PUT_LE(&hdr->e_shnum, fake_sections_size / sizeof(Elf_Shdr));
132+
PUT_LE(&hdr->e_shstrndx, SHN_UNDEF);
120133

121134
if (!name) {
122135
fwrite(addr, load_size, 1, outfile);

0 commit comments

Comments
 (0)