Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e0bf7b86 authored by Andy Lutomirski's avatar Andy Lutomirski Committed by H. Peter Anvin
Browse files

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: default avatarAndy Lutomirski <luto@amacapital.net>
Link: http://lkml.kernel.org/r/d10618176c4bd39b457a5e85c497295c90cab1bc.1402620737.git.luto@amacapital.net


Signed-off-by: default avatarH. Peter Anvin <hpa@zytor.com>
parent b4b31f61
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -15,12 +15,8 @@ vdso-install-$(VDSO32-y) += $(vdso32-images)


# files to link into the vdso
vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o

vobjs-$(VDSOX32-y) += $(vobjx32s-compat)

# Filter out x32 objects.
vobj64s := $(filter-out $(vobjx32s-compat),$(vobjs-y))
vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdso-fakesections.o
vobjs-nox32 := vdso-fakesections.o

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

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

vobjs := $(foreach F,$(vobj64s),$(obj)/$F)
vobjs := $(foreach F,$(vobjs-y),$(obj)/$F)

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

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

vobjx32s-y := $(vobj64s:.o=-x32.o)
# 64-bit objects to re-brand as x32
vobjs64-for-x32 := $(filter-out $(vobjs-nox32),$(vobjs-y))

# x32-rebranded versions
vobjx32s-y := $(vobjs64-for-x32:.o=-x32.o)

# same thing, but in the output directory
vobjx32s := $(foreach F,$(vobjx32s-y),$(obj)/$F)

# Convert 64bit object file to x32 for x32 vDSO.
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright 2014 Andy Lutomirski
 * Subject to the GNU Public License, v.2
 *
 * Hack to keep broken Go programs working.
 *
 * The Go runtime had a couple of bugs: it would read the section table to try
 * to figure out how many dynamic symbols there were (it shouldn't have looked
 * at the section table at all) and, if there were no SHT_SYNDYM section table
 * entry, it would use an uninitialized value for the number of symbols.  As a
 * workaround, we supply a minimal section table.  vdso2c will adjust the
 * in-memory image so that "vdso_fake_sections" becomes the section table.
 *
 * The bug was introduced by:
 * https://code.google.com/p/go/source/detail?r=56ea40aac72b (2012-08-31)
 * and is being addressed in the Go runtime in this issue:
 * https://code.google.com/p/go/issues/detail?id=8197
 */

#ifndef __x86_64__
#error This hack is specific to the 64-bit vDSO
#endif

#include <linux/elf.h>

extern const __visible struct elf64_shdr vdso_fake_sections[];
const __visible struct elf64_shdr vdso_fake_sections[] = {
	{
		.sh_type = SHT_DYNSYM,
		.sh_entsize = sizeof(Elf64_Sym),
	}
};
+18 −5
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
	const char *secstrings;
	uint64_t syms[NSYMS] = {};

	uint64_t fake_sections_value = 0, fake_sections_size = 0;

	Elf_Phdr *pt = (Elf_Phdr *)(addr + GET_LE(&hdr->e_phoff));

	/* Walk the segment table. */
@@ -84,6 +86,7 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
			GET_LE(&symtab_hdr->sh_entsize) * i;
		const char *name = addr + GET_LE(&strtab_hdr->sh_offset) +
			GET_LE(&sym->st_name);

		for (k = 0; k < NSYMS; k++) {
			if (!strcmp(name, required_syms[k])) {
				if (syms[k]) {
@@ -93,6 +96,13 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
				syms[k] = GET_LE(&sym->st_value);
			}
		}

		if (!strcmp(name, "vdso_fake_sections")) {
			if (fake_sections_value)
				fail("duplicate vdso_fake_sections\n");
			fake_sections_value = GET_LE(&sym->st_value);
			fake_sections_size = GET_LE(&sym->st_size);
		}
	}

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

	/* Remove sections. */
	hdr->e_shoff = 0;
	hdr->e_shentsize = 0;
	hdr->e_shnum = 0;
	hdr->e_shstrndx = SHN_UNDEF; /* SHN_UNDEF == 0 */
	/* Remove sections or use fakes */
	if (fake_sections_size % sizeof(Elf_Shdr))
		fail("vdso_fake_sections size is not a multiple of %ld\n",
		     (long)sizeof(Elf_Shdr));
	PUT_LE(&hdr->e_shoff, fake_sections_value);
	PUT_LE(&hdr->e_shentsize, fake_sections_value ? sizeof(Elf_Shdr) : 0);
	PUT_LE(&hdr->e_shnum, fake_sections_size / sizeof(Elf_Shdr));
	PUT_LE(&hdr->e_shstrndx, SHN_UNDEF);

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