Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86, vdso: Reimplement vdso.so preparation in build-time C

Currently, vdso.so files are prepared and analyzed by a combination
of objcopy, nm, some linker script tricks, and some simple ELF
parsers in the kernel. Replace all of that with plain C code that
runs at build time.

All five vdso images now generate .c files that are compiled and
linked in to the kernel image.

This should cause only one userspace-visible change: the loaded vDSO
images are stripped more heavily than they used to be. Everything
outside the loadable segment is dropped. In particular, this causes
the section table and section name strings to be missing. This
should be fine: real dynamic loaders don't load or inspect these
tables anyway. The result is roughly equivalent to eu-strip's
--strip-sections option.

The purpose of this change is to enable the vvar and hpet mappings
to be moved to the page following the vDSO load segment. Currently,
it is possible for the section table to extend into the page after
the load segment, so, if we map it, it risks overlapping the vvar or
hpet page. This happens whenever the load segment is just under a
multiple of PAGE_SIZE.

The only real subtlety here is that the old code had a C file with
inline assembler that did 'call VDSO32_vsyscall' and a linker script
that defined 'VDSO32_vsyscall = __kernel_vsyscall'. This most
likely worked by accident: the linker script entry defines a symbol
associated with an address as opposed to an alias for the real
dynamic symbol __kernel_vsyscall. That caused ld to relocate the
reference at link time instead of leaving an interposable dynamic
relocation. Since the VDSO32_vsyscall hack is no longer needed, I
now use 'call __kernel_vsyscall', and I added -Bsymbolic to make it
work. vdso2c will generate an error and abort the build if the
resulting image contains any dynamic relocations, so we won't
silently generate bad vdso images.

(Dynamic relocations are a problem because nothing will even attempt
to relocate the vdso.)

Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Link: http://lkml.kernel.org/r/2c4fcf45524162a34d87fdda1eb046b2a5cecee7.1399317206.git.luto@amacapital.net
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>

authored by

Andy Lutomirski and committed by
H. Peter Anvin
6f121e54 cfda7bb9

+401 -261
+4 -4
arch/x86/ia32/ia32_signal.c
··· 383 383 } else { 384 384 /* Return stub is in 32bit vsyscall page */ 385 385 if (current->mm->context.vdso) 386 - restorer = VDSO32_SYMBOL(current->mm->context.vdso, 387 - sigreturn); 386 + restorer = current->mm->context.vdso + 387 + selected_vdso32->sym___kernel_sigreturn; 388 388 else 389 389 restorer = &frame->retcode; 390 390 } ··· 462 462 if (ksig->ka.sa.sa_flags & SA_RESTORER) 463 463 restorer = ksig->ka.sa.sa_restorer; 464 464 else 465 - restorer = VDSO32_SYMBOL(current->mm->context.vdso, 466 - rt_sigreturn); 465 + restorer = current->mm->context.vdso + 466 + selected_vdso32->sym___kernel_rt_sigreturn; 467 467 put_user_ex(ptr_to_compat(restorer), &frame->pretcode); 468 468 469 469 /*
+4 -3
arch/x86/include/asm/elf.h
··· 299 299 do { \ 300 300 if (vdso64_enabled) \ 301 301 NEW_AUX_ENT(AT_SYSINFO_EHDR, \ 302 - (unsigned long)current->mm->context.vdso); \ 302 + (unsigned long __force)current->mm->context.vdso); \ 303 303 } while (0) 304 304 305 305 /* As a historical oddity, the x32 and x86_64 vDSOs are controlled together. */ ··· 307 307 do { \ 308 308 if (vdso64_enabled) \ 309 309 NEW_AUX_ENT(AT_SYSINFO_EHDR, \ 310 - (unsigned long)current->mm->context.vdso); \ 310 + (unsigned long __force)current->mm->context.vdso); \ 311 311 } while (0) 312 312 313 313 #define AT_SYSINFO 32 ··· 325 325 #define VDSO_CURRENT_BASE ((unsigned long)current->mm->context.vdso) 326 326 327 327 #define VDSO_ENTRY \ 328 - ((unsigned long)VDSO32_SYMBOL(VDSO_CURRENT_BASE, vsyscall)) 328 + ((unsigned long)current->mm->context.vdso + \ 329 + selected_vdso32->sym___kernel_vsyscall) 329 330 330 331 struct linux_binprm; 331 332
+1 -1
arch/x86/include/asm/mmu.h
··· 18 18 #endif 19 19 20 20 struct mutex lock; 21 - void *vdso; 21 + void __user *vdso; 22 22 } mm_context_t; 23 23 24 24 #ifdef CONFIG_SMP
+26 -46
arch/x86/include/asm/vdso.h
··· 3 3 4 4 #include <asm/page_types.h> 5 5 #include <linux/linkage.h> 6 + #include <linux/init.h> 6 7 7 - #ifdef __ASSEMBLER__ 8 + #ifndef __ASSEMBLER__ 8 9 9 - #define DEFINE_VDSO_IMAGE(symname, filename) \ 10 - __PAGE_ALIGNED_DATA ; \ 11 - .globl symname##_start, symname##_end ; \ 12 - .align PAGE_SIZE ; \ 13 - symname##_start: ; \ 14 - .incbin filename ; \ 15 - symname##_end: ; \ 16 - .align PAGE_SIZE /* extra data here leaks to userspace. */ ; \ 17 - \ 18 - .previous ; \ 19 - \ 20 - .globl symname##_pages ; \ 21 - .bss ; \ 22 - .align 8 ; \ 23 - .type symname##_pages, @object ; \ 24 - symname##_pages: ; \ 25 - .zero (symname##_end - symname##_start + PAGE_SIZE - 1) / PAGE_SIZE * (BITS_PER_LONG / 8) ; \ 26 - .size symname##_pages, .-symname##_pages 10 + struct vdso_image { 11 + void *data; 12 + unsigned long size; /* Always a multiple of PAGE_SIZE */ 13 + struct page **pages; /* Big enough for data/size page pointers */ 27 14 28 - #else 15 + unsigned long alt, alt_len; 29 16 30 - #define DECLARE_VDSO_IMAGE(symname) \ 31 - extern char symname##_start[], symname##_end[]; \ 32 - extern struct page *symname##_pages[] 17 + unsigned long sym_VDSO32_NOTE_MASK; 18 + unsigned long sym___kernel_sigreturn; 19 + unsigned long sym___kernel_rt_sigreturn; 20 + unsigned long sym___kernel_vsyscall; 21 + unsigned long sym_VDSO32_SYSENTER_RETURN; 22 + }; 23 + 24 + #ifdef CONFIG_X86_64 25 + extern const struct vdso_image vdso_image_64; 26 + #endif 27 + 28 + #ifdef CONFIG_X86_X32 29 + extern const struct vdso_image vdso_image_x32; 30 + #endif 33 31 34 32 #if defined CONFIG_X86_32 || defined CONFIG_COMPAT 35 - 36 - #include <asm/vdso32.h> 37 - 38 - DECLARE_VDSO_IMAGE(vdso32_int80); 33 + extern const struct vdso_image vdso_image_32_int80; 39 34 #ifdef CONFIG_COMPAT 40 - DECLARE_VDSO_IMAGE(vdso32_syscall); 35 + extern const struct vdso_image vdso_image_32_syscall; 41 36 #endif 42 - DECLARE_VDSO_IMAGE(vdso32_sysenter); 37 + extern const struct vdso_image vdso_image_32_sysenter; 43 38 44 - /* 45 - * Given a pointer to the vDSO image, find the pointer to VDSO32_name 46 - * as that symbol is defined in the vDSO sources or linker script. 47 - */ 48 - #define VDSO32_SYMBOL(base, name) \ 49 - ({ \ 50 - extern const char VDSO32_##name[]; \ 51 - (void __user *)(VDSO32_##name + (unsigned long)(base)); \ 52 - }) 39 + extern const struct vdso_image *selected_vdso32; 53 40 #endif 54 41 55 - /* 56 - * These symbols are defined with the addresses in the vsyscall page. 57 - * See vsyscall-sigreturn.S. 58 - */ 59 - extern void __user __kernel_sigreturn; 60 - extern void __user __kernel_rt_sigreturn; 61 - 62 - void __init patch_vdso32(void *vdso, size_t len); 42 + extern void __init init_vdso_image(const struct vdso_image *image); 63 43 64 44 #endif /* __ASSEMBLER__ */ 65 45
+4 -2
arch/x86/kernel/signal.c
··· 298 298 } 299 299 300 300 if (current->mm->context.vdso) 301 - restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn); 301 + restorer = current->mm->context.vdso + 302 + selected_vdso32->sym___kernel_sigreturn; 302 303 else 303 304 restorer = &frame->retcode; 304 305 if (ksig->ka.sa.sa_flags & SA_RESTORER) ··· 362 361 save_altstack_ex(&frame->uc.uc_stack, regs->sp); 363 362 364 363 /* Set up to return from userspace. */ 365 - restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn); 364 + restorer = current->mm->context.vdso + 365 + selected_vdso32->sym___kernel_sigreturn; 366 366 if (ksig->ka.sa.sa_flags & SA_RESTORER) 367 367 restorer = ksig->ka.sa.sa_restorer; 368 368 put_user_ex(restorer, &frame->pretcode);
+2 -1
arch/x86/mm/init_64.c
··· 1223 1223 1224 1224 const char *arch_vma_name(struct vm_area_struct *vma) 1225 1225 { 1226 - if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) 1226 + if (vma->vm_mm && vma->vm_start == 1227 + (long __force)vma->vm_mm->context.vdso) 1227 1228 return "[vdso]"; 1228 1229 if (vma == &gate_vma) 1229 1230 return "[vsyscall]";
+2 -3
arch/x86/vdso/.gitignore
··· 1 1 vdso.lds 2 - vdso-syms.lds 3 2 vdsox32.lds 4 - vdsox32-syms.lds 5 - vdso32-syms.lds 6 3 vdso32-syscall-syms.lds 7 4 vdso32-sysenter-syms.lds 8 5 vdso32-int80-syms.lds 6 + vdso-image-*.c 7 + vdso2c
+32 -58
arch/x86/vdso/Makefile
··· 24 24 25 25 # files to link into kernel 26 26 obj-y += vma.o 27 - obj-$(VDSO64-y) += vdso.o 28 - obj-$(VDSOX32-y) += vdsox32.o 29 - obj-$(VDSO32-y) += vdso32.o vdso32-setup.o 27 + 28 + # vDSO images to build 29 + vdso_img-$(VDSO64-y) += 64 30 + vdso_img-$(VDSOX32-y) += x32 31 + vdso_img-$(VDSO32-y) += 32-int80 32 + vdso_img-$(CONFIG_COMPAT) += 32-syscall 33 + vdso_img-$(VDSO32-y) += 32-sysenter 34 + 35 + obj-$(VDSO32-y) += vdso32-setup.o 30 36 31 37 vobjs := $(foreach F,$(vobj64s),$(obj)/$F) 32 38 33 39 $(obj)/vdso.o: $(obj)/vdso.so 34 40 35 - targets += vdso.so vdso.so.dbg vdso.lds $(vobjs-y) 41 + targets += vdso.lds $(vobjs-y) 42 + 43 + # Build the vDSO image C files and link them in. 44 + vdso_img_objs := $(vdso_img-y:%=vdso-image-%.o) 45 + vdso_img_cfiles := $(vdso_img-y:%=vdso-image-%.c) 46 + vdso_img_sodbg := $(vdso_img-y:%=vdso%.so.dbg) 47 + obj-y += $(vdso_img_objs) 48 + targets += $(vdso_img_cfiles) 49 + targets += $(vdso_img_sodbg) 50 + .SECONDARY: $(vdso_img-y:%=$(obj)/vdso-image-%.c) 36 51 37 52 export CPPFLAGS_vdso.lds += -P -C 38 53 ··· 56 41 -Wl,-z,max-page-size=4096 -Wl,-z,common-page-size=4096 \ 57 42 $(DISABLE_LTO) 58 43 59 - $(obj)/vdso.o: $(src)/vdso.S $(obj)/vdso.so 60 - 61 - $(obj)/vdso.so.dbg: $(src)/vdso.lds $(vobjs) FORCE 44 + $(obj)/vdso64.so.dbg: $(src)/vdso.lds $(vobjs) FORCE 62 45 $(call if_changed,vdso) 63 46 64 - $(obj)/%.so: OBJCOPYFLAGS := -S 65 - $(obj)/%.so: $(obj)/%.so.dbg FORCE 66 - $(call if_changed,objcopy) 47 + hostprogs-y += vdso2c 48 + 49 + quiet_cmd_vdso2c = VDSO2C $@ 50 + define cmd_vdso2c 51 + $(obj)/vdso2c $< $@ 52 + endef 53 + 54 + $(obj)/vdso-image-%.c: $(obj)/vdso%.so.dbg $(obj)/vdso2c FORCE 55 + $(call if_changed,vdso2c) 67 56 68 57 # 69 58 # Don't omit frame pointers for ease of userspace debugging, but do ··· 87 68 CFLAGS_REMOVE_vgetcpu.o = -pg 88 69 CFLAGS_REMOVE_vvar.o = -pg 89 70 90 - targets += vdso-syms.lds 91 - obj-$(VDSO64-y) += vdso-syms.lds 92 - 93 - # 94 - # Match symbols in the DSO that look like VDSO*; produce a file of constants. 95 - # 96 - sed-vdsosym := -e 's/^00*/0/' \ 97 - -e 's/^\([0-9a-fA-F]*\) . \(VDSO[a-zA-Z0-9_]*\)$$/\2 = 0x\1;/p' 98 - quiet_cmd_vdsosym = VDSOSYM $@ 99 - define cmd_vdsosym 100 - $(NM) $< | LC_ALL=C sed -n $(sed-vdsosym) | LC_ALL=C sort > $@ 101 - endef 102 - 103 - $(obj)/%-syms.lds: $(obj)/%.so.dbg FORCE 104 - $(call if_changed,vdsosym) 105 - 106 71 # 107 72 # X32 processes use x32 vDSO to access 64bit kernel data. 108 73 # ··· 96 93 # 3. Build x32 VDSO image with x32 objects, which contains 64bit codes 97 94 # so that it can reach 64bit address space with 64bit pointers. 98 95 # 99 - 100 - targets += vdsox32-syms.lds 101 - obj-$(VDSOX32-y) += vdsox32-syms.lds 102 96 103 97 CPPFLAGS_vdsox32.lds = $(CPPFLAGS_vdso.lds) 104 98 VDSO_LDFLAGS_vdsox32.lds = -Wl,-m,elf32_x86_64 \ ··· 113 113 $(obj)/%-x32.o: $(obj)/%.o FORCE 114 114 $(call if_changed,x32) 115 115 116 - targets += vdsox32.so vdsox32.so.dbg vdsox32.lds $(vobjx32s-y) 117 - 118 - $(obj)/vdsox32.o: $(src)/vdsox32.S $(obj)/vdsox32.so 116 + targets += vdsox32.lds $(vobjx32s-y) 119 117 120 118 $(obj)/vdsox32.so.dbg: $(src)/vdsox32.lds $(vobjx32s) FORCE 121 119 $(call if_changed,vdso) ··· 121 123 # 122 124 # Build multiple 32-bit vDSO images to choose from at boot time. 123 125 # 124 - obj-$(VDSO32-y) += vdso32-syms.lds 125 126 vdso32.so-$(VDSO32-y) += int80 126 127 vdso32.so-$(CONFIG_COMPAT) += syscall 127 128 vdso32.so-$(VDSO32-y) += sysenter ··· 135 138 override obj-dirs = $(dir $(obj)) $(obj)/vdso32/ 136 139 137 140 targets += vdso32/vdso32.lds 138 - targets += $(vdso32-images) $(vdso32-images:=.dbg) 139 141 targets += vdso32/note.o vdso32/vclock_gettime.o $(vdso32.so-y:%=vdso32/%.o) 140 - 141 - extra-y += $(vdso32-images) 142 + targets += vdso32/vclock_gettime.o 142 143 143 144 $(obj)/vdso32.o: $(vdso32-images:%=$(obj)/%) 144 145 ··· 161 166 $(obj)/vdso32/%.o 162 167 $(call if_changed,vdso) 163 168 164 - # Make vdso32-*-syms.lds from each image, and then make sure they match. 165 - # The only difference should be that some do not define VDSO32_SYSENTER_RETURN. 166 - 167 - targets += vdso32-syms.lds $(vdso32.so-y:%=vdso32-%-syms.lds) 168 - 169 - quiet_cmd_vdso32sym = VDSOSYM $@ 170 - define cmd_vdso32sym 171 - if LC_ALL=C sort -u $(filter-out FORCE,$^) > $(@D)/.tmp_$(@F) && \ 172 - $(foreach H,$(filter-out FORCE,$^),\ 173 - if grep -q VDSO32_SYSENTER_RETURN $H; \ 174 - then diff -u $(@D)/.tmp_$(@F) $H; \ 175 - else sed /VDSO32_SYSENTER_RETURN/d $(@D)/.tmp_$(@F) | \ 176 - diff -u - $H; fi &&) : ;\ 177 - then mv -f $(@D)/.tmp_$(@F) $@; \ 178 - else rm -f $(@D)/.tmp_$(@F); exit 1; \ 179 - fi 180 - endef 181 - 182 - $(obj)/vdso32-syms.lds: $(vdso32.so-y:%=$(obj)/vdso32-%-syms.lds) FORCE 183 - $(call if_changed,vdso32sym) 184 - 185 169 # 186 170 # The DSO images are built using a special linker script. 187 171 # ··· 171 197 sh $(srctree)/$(src)/checkundef.sh '$(NM)' '$@' 172 198 173 199 VDSO_LDFLAGS = -fPIC -shared $(call cc-ldoption, -Wl$(comma)--hash-style=sysv) \ 174 - $(LTO_CFLAGS) 200 + -Wl,-Bsymbolic $(LTO_CFLAGS) 175 201 GCOV_PROFILE := n 176 202 177 203 #
+2 -2
arch/x86/vdso/vclock_gettime.c
··· 154 154 asm( 155 155 "mov %%ebx, %%edx \n" 156 156 "mov %2, %%ebx \n" 157 - "call VDSO32_vsyscall \n" 157 + "call __kernel_vsyscall \n" 158 158 "mov %%edx, %%ebx \n" 159 159 : "=a" (ret) 160 160 : "0" (__NR_clock_gettime), "g" (clock), "c" (ts) ··· 169 169 asm( 170 170 "mov %%ebx, %%edx \n" 171 171 "mov %2, %%ebx \n" 172 - "call VDSO32_vsyscall \n" 172 + "call __kernel_vsyscall \n" 173 173 "mov %%edx, %%ebx \n" 174 174 : "=a" (ret) 175 175 : "0" (__NR_gettimeofday), "g" (tv), "c" (tz)
-3
arch/x86/vdso/vdso.S
··· 1 - #include <asm/vdso.h> 2 - 3 - DEFINE_VDSO_IMAGE(vdso, "arch/x86/vdso/vdso.so")
+142
arch/x86/vdso/vdso2c.c
··· 1 + #include <inttypes.h> 2 + #include <stdint.h> 3 + #include <unistd.h> 4 + #include <stdarg.h> 5 + #include <stdlib.h> 6 + #include <stdio.h> 7 + #include <string.h> 8 + #include <fcntl.h> 9 + #include <err.h> 10 + 11 + #include <sys/mman.h> 12 + #include <sys/types.h> 13 + 14 + #include <linux/elf.h> 15 + #include <linux/types.h> 16 + 17 + /* Symbols that we need in vdso2c. */ 18 + char const * const required_syms[] = { 19 + "VDSO32_NOTE_MASK", 20 + "VDSO32_SYSENTER_RETURN", 21 + "__kernel_vsyscall", 22 + "__kernel_sigreturn", 23 + "__kernel_rt_sigreturn", 24 + }; 25 + 26 + __attribute__((format(printf, 1, 2))) __attribute__((noreturn)) 27 + static void fail(const char *format, ...) 28 + { 29 + va_list ap; 30 + va_start(ap, format); 31 + fprintf(stderr, "Error: "); 32 + vfprintf(stderr, format, ap); 33 + exit(1); 34 + va_end(ap); 35 + } 36 + 37 + #define NSYMS (sizeof(required_syms) / sizeof(required_syms[0])) 38 + 39 + #define BITS 64 40 + #define GOFUNC go64 41 + #define Elf_Ehdr Elf64_Ehdr 42 + #define Elf_Shdr Elf64_Shdr 43 + #define Elf_Phdr Elf64_Phdr 44 + #define Elf_Sym Elf64_Sym 45 + #define Elf_Dyn Elf64_Dyn 46 + #include "vdso2c.h" 47 + #undef BITS 48 + #undef GOFUNC 49 + #undef Elf_Ehdr 50 + #undef Elf_Shdr 51 + #undef Elf_Phdr 52 + #undef Elf_Sym 53 + #undef Elf_Dyn 54 + 55 + #define BITS 32 56 + #define GOFUNC go32 57 + #define Elf_Ehdr Elf32_Ehdr 58 + #define Elf_Shdr Elf32_Shdr 59 + #define Elf_Phdr Elf32_Phdr 60 + #define Elf_Sym Elf32_Sym 61 + #define Elf_Dyn Elf32_Dyn 62 + #include "vdso2c.h" 63 + #undef BITS 64 + #undef GOFUNC 65 + #undef Elf_Ehdr 66 + #undef Elf_Shdr 67 + #undef Elf_Phdr 68 + #undef Elf_Sym 69 + #undef Elf_Dyn 70 + 71 + static int go(void *addr, size_t len, FILE *outfile, const char *name) 72 + { 73 + Elf64_Ehdr *hdr = (Elf64_Ehdr *)addr; 74 + 75 + if (hdr->e_ident[EI_CLASS] == ELFCLASS64) { 76 + return go64(addr, len, outfile, name); 77 + } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) { 78 + return go32(addr, len, outfile, name); 79 + } else { 80 + fprintf(stderr, "Error: unknown ELF class\n"); 81 + return 1; 82 + } 83 + } 84 + 85 + int main(int argc, char **argv) 86 + { 87 + int fd; 88 + off_t len; 89 + void *addr; 90 + FILE *outfile; 91 + int ret; 92 + char *name, *tmp; 93 + int namelen; 94 + 95 + if (argc != 3) { 96 + printf("Usage: vdso2c INPUT OUTPUT\n"); 97 + return 1; 98 + } 99 + 100 + /* 101 + * Figure out the struct name. If we're writing to a .so file, 102 + * generate raw output insted. 103 + */ 104 + name = strdup(argv[2]); 105 + namelen = strlen(name); 106 + if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) { 107 + name = NULL; 108 + } else { 109 + tmp = strrchr(name, '/'); 110 + if (tmp) 111 + name = tmp + 1; 112 + tmp = strchr(name, '.'); 113 + if (tmp) 114 + *tmp = '\0'; 115 + for (tmp = name; *tmp; tmp++) 116 + if (*tmp == '-') 117 + *tmp = '_'; 118 + } 119 + 120 + fd = open(argv[1], O_RDONLY); 121 + if (fd == -1) 122 + err(1, "%s", argv[1]); 123 + 124 + len = lseek(fd, 0, SEEK_END); 125 + if (len == (off_t)-1) 126 + err(1, "lseek"); 127 + 128 + addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 129 + if (addr == MAP_FAILED) 130 + err(1, "mmap"); 131 + 132 + outfile = fopen(argv[2], "w"); 133 + if (!outfile) 134 + err(1, "%s", argv[2]); 135 + 136 + ret = go(addr, (size_t)len, outfile, name); 137 + 138 + munmap(addr, len); 139 + fclose(outfile); 140 + 141 + return ret; 142 + }
+137
arch/x86/vdso/vdso2c.h
··· 1 + /* 2 + * This file is included twice from vdso2c.c. It generates code for 32-bit 3 + * and 64-bit vDSOs. We need both for 64-bit builds, since 32-bit vDSOs 4 + * are built for 32-bit userspace. 5 + */ 6 + 7 + static int GOFUNC(void *addr, size_t len, FILE *outfile, const char *name) 8 + { 9 + int found_load = 0; 10 + unsigned long load_size = -1; /* Work around bogus warning */ 11 + unsigned long data_size; 12 + Elf_Ehdr *hdr = (Elf_Ehdr *)addr; 13 + int i; 14 + unsigned long j; 15 + Elf_Shdr *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr, 16 + *alt_sec = NULL; 17 + Elf_Dyn *dyn = 0, *dyn_end = 0; 18 + const char *secstrings; 19 + uint64_t syms[NSYMS] = {}; 20 + 21 + Elf_Phdr *pt = (Elf_Phdr *)(addr + hdr->e_phoff); 22 + 23 + /* Walk the segment table. */ 24 + for (i = 0; i < hdr->e_phnum; i++) { 25 + if (pt[i].p_type == PT_LOAD) { 26 + if (found_load) 27 + fail("multiple PT_LOAD segs\n"); 28 + 29 + if (pt[i].p_offset != 0 || pt[i].p_vaddr != 0) 30 + fail("PT_LOAD in wrong place\n"); 31 + 32 + if (pt[i].p_memsz != pt[i].p_filesz) 33 + fail("cannot handle memsz != filesz\n"); 34 + 35 + load_size = pt[i].p_memsz; 36 + found_load = 1; 37 + } else if (pt[i].p_type == PT_DYNAMIC) { 38 + dyn = addr + pt[i].p_offset; 39 + dyn_end = addr + pt[i].p_offset + pt[i].p_memsz; 40 + } 41 + } 42 + if (!found_load) 43 + fail("no PT_LOAD seg\n"); 44 + data_size = (load_size + 4095) / 4096 * 4096; 45 + 46 + /* Walk the dynamic table */ 47 + for (i = 0; dyn + i < dyn_end && dyn[i].d_tag != DT_NULL; i++) { 48 + if (dyn[i].d_tag == DT_REL || dyn[i].d_tag == DT_RELSZ || 49 + dyn[i].d_tag == DT_RELENT || dyn[i].d_tag == DT_TEXTREL) 50 + fail("vdso image contains dynamic relocations\n"); 51 + } 52 + 53 + /* Walk the section table */ 54 + secstrings_hdr = addr + hdr->e_shoff + hdr->e_shentsize*hdr->e_shstrndx; 55 + secstrings = addr + secstrings_hdr->sh_offset; 56 + for (i = 0; i < hdr->e_shnum; i++) { 57 + Elf_Shdr *sh = addr + hdr->e_shoff + hdr->e_shentsize * i; 58 + if (sh->sh_type == SHT_SYMTAB) 59 + symtab_hdr = sh; 60 + 61 + if (!strcmp(secstrings + sh->sh_name, ".altinstructions")) 62 + alt_sec = sh; 63 + } 64 + 65 + if (!symtab_hdr) { 66 + fail("no symbol table\n"); 67 + return 1; 68 + } 69 + 70 + strtab_hdr = addr + hdr->e_shoff + 71 + hdr->e_shentsize * symtab_hdr->sh_link; 72 + 73 + /* Walk the symbol table */ 74 + for (i = 0; i < symtab_hdr->sh_size / symtab_hdr->sh_entsize; i++) { 75 + int k; 76 + Elf_Sym *sym = addr + symtab_hdr->sh_offset + 77 + symtab_hdr->sh_entsize * i; 78 + const char *name = addr + strtab_hdr->sh_offset + sym->st_name; 79 + for (k = 0; k < NSYMS; k++) { 80 + if (!strcmp(name, required_syms[k])) { 81 + if (syms[k]) { 82 + fail("duplicate symbol %s\n", 83 + required_syms[k]); 84 + } 85 + syms[k] = sym->st_value; 86 + } 87 + } 88 + } 89 + 90 + /* Remove sections. */ 91 + hdr->e_shoff = 0; 92 + hdr->e_shentsize = 0; 93 + hdr->e_shnum = 0; 94 + hdr->e_shstrndx = SHN_UNDEF; 95 + 96 + if (!name) { 97 + fwrite(addr, load_size, 1, outfile); 98 + return 0; 99 + } 100 + 101 + fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n"); 102 + fprintf(outfile, "#include <linux/linkage.h>\n"); 103 + fprintf(outfile, "#include <asm/page_types.h>\n"); 104 + fprintf(outfile, "#include <asm/vdso.h>\n"); 105 + fprintf(outfile, "\n"); 106 + fprintf(outfile, 107 + "static unsigned char raw_data[%lu] __page_aligned_data = {", 108 + data_size); 109 + for (j = 0; j < load_size; j++) { 110 + if (j % 10 == 0) 111 + fprintf(outfile, "\n\t"); 112 + fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]); 113 + } 114 + fprintf(outfile, "\n};\n\n"); 115 + 116 + fprintf(outfile, "static struct page *pages[%lu];\n\n", 117 + data_size / 4096); 118 + 119 + fprintf(outfile, "const struct vdso_image %s = {\n", name); 120 + fprintf(outfile, "\t.data = raw_data,\n"); 121 + fprintf(outfile, "\t.size = %lu,\n", data_size); 122 + fprintf(outfile, "\t.pages = pages,\n"); 123 + if (alt_sec) { 124 + fprintf(outfile, "\t.alt = %lu,\n", 125 + (unsigned long)alt_sec->sh_offset); 126 + fprintf(outfile, "\t.alt_len = %lu,\n", 127 + (unsigned long)alt_sec->sh_size); 128 + } 129 + for (i = 0; i < NSYMS; i++) { 130 + if (syms[i]) 131 + fprintf(outfile, "\t.sym_%s = 0x%" PRIx64 ",\n", 132 + required_syms[i], syms[i]); 133 + } 134 + fprintf(outfile, "};\n"); 135 + 136 + return 0; 137 + }
+20 -30
arch/x86/vdso/vdso32-setup.c
··· 29 29 #include <asm/fixmap.h> 30 30 #include <asm/hpet.h> 31 31 #include <asm/vvar.h> 32 + #include <asm/vdso32.h> 32 33 33 34 #ifdef CONFIG_COMPAT_VDSO 34 35 #define VDSO_DEFAULT 0 ··· 68 67 __setup_param("vdso=", vdso_setup, vdso32_setup, 0); 69 68 #endif 70 69 71 - static struct page **vdso32_pages; 72 - static unsigned vdso32_size; 73 - 74 70 #ifdef CONFIG_X86_64 75 71 76 72 #define vdso32_sysenter() (boot_cpu_has(X86_FEATURE_SYSENTER32)) ··· 80 82 81 83 #endif /* CONFIG_X86_64 */ 82 84 85 + #if defined(CONFIG_X86_32) || defined(CONFIG_COMPAT) 86 + const struct vdso_image *selected_vdso32; 87 + #endif 88 + 83 89 int __init sysenter_setup(void) 84 90 { 85 - char *vdso32_start, *vdso32_end; 86 - int npages, i; 87 - 88 91 #ifdef CONFIG_COMPAT 89 - if (vdso32_syscall()) { 90 - vdso32_start = vdso32_syscall_start; 91 - vdso32_end = vdso32_syscall_end; 92 - vdso32_pages = vdso32_syscall_pages; 93 - } else 92 + if (vdso32_syscall()) 93 + selected_vdso32 = &vdso_image_32_syscall; 94 + else 94 95 #endif 95 - if (vdso32_sysenter()) { 96 - vdso32_start = vdso32_sysenter_start; 97 - vdso32_end = vdso32_sysenter_end; 98 - vdso32_pages = vdso32_sysenter_pages; 99 - } else { 100 - vdso32_start = vdso32_int80_start; 101 - vdso32_end = vdso32_int80_end; 102 - vdso32_pages = vdso32_int80_pages; 103 - } 96 + if (vdso32_sysenter()) 97 + selected_vdso32 = &vdso_image_32_sysenter; 98 + else 99 + selected_vdso32 = &vdso_image_32_int80; 104 100 105 - npages = ((vdso32_end - vdso32_start) + PAGE_SIZE - 1) / PAGE_SIZE; 106 - vdso32_size = npages << PAGE_SHIFT; 107 - for (i = 0; i < npages; i++) 108 - vdso32_pages[i] = virt_to_page(vdso32_start + i*PAGE_SIZE); 109 - 110 - patch_vdso32(vdso32_start, vdso32_size); 101 + init_vdso_image(selected_vdso32); 111 102 112 103 return 0; 113 104 } ··· 108 121 unsigned long addr; 109 122 int ret = 0; 110 123 struct vm_area_struct *vma; 124 + unsigned long vdso32_size = selected_vdso32->size; 111 125 112 126 #ifdef CONFIG_X86_X32_ABI 113 127 if (test_thread_flag(TIF_X32)) ··· 128 140 129 141 addr += VDSO_OFFSET(VDSO_PREV_PAGES); 130 142 131 - current->mm->context.vdso = (void *)addr; 143 + current->mm->context.vdso = (void __user *)addr; 132 144 133 145 /* 134 146 * MAYWRITE to allow gdb to COW and set breakpoints ··· 138 150 vdso32_size, 139 151 VM_READ|VM_EXEC| 140 152 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 141 - vdso32_pages); 153 + selected_vdso32->pages); 142 154 143 155 if (ret) 144 156 goto up_fail; ··· 176 188 } 177 189 #endif 178 190 179 - current_thread_info()->sysenter_return = 180 - VDSO32_SYMBOL(addr, SYSENTER_RETURN); 191 + if (selected_vdso32->sym_VDSO32_SYSENTER_RETURN) 192 + current_thread_info()->sysenter_return = 193 + current->mm->context.vdso + 194 + selected_vdso32->sym_VDSO32_SYSENTER_RETURN; 181 195 182 196 up_fail: 183 197 if (ret)
-9
arch/x86/vdso/vdso32.S
··· 1 - #include <asm/vdso.h> 2 - 3 - DEFINE_VDSO_IMAGE(vdso32_int80, "arch/x86/vdso/vdso32-int80.so") 4 - 5 - #ifdef CONFIG_COMPAT 6 - DEFINE_VDSO_IMAGE(vdso32_syscall, "arch/x86/vdso/vdso32-syscall.so") 7 - #endif 8 - 9 - DEFINE_VDSO_IMAGE(vdso32_sysenter, "arch/x86/vdso/vdso32-sysenter.so")
-10
arch/x86/vdso/vdso32/vdso32.lds.S
··· 38 38 local: *; 39 39 }; 40 40 } 41 - 42 - /* 43 - * Symbols we define here called VDSO* get their values into vdso32-syms.h. 44 - */ 45 - VDSO32_vsyscall = __kernel_vsyscall; 46 - VDSO32_sigreturn = __kernel_sigreturn; 47 - VDSO32_rt_sigreturn = __kernel_rt_sigreturn; 48 - VDSO32_clock_gettime = clock_gettime; 49 - VDSO32_gettimeofday = gettimeofday; 50 - VDSO32_time = time;
-3
arch/x86/vdso/vdsox32.S
··· 1 - #include <asm/vdso.h> 2 - 3 - DEFINE_VDSO_IMAGE(vdsox32, "arch/x86/vdso/vdsox32.so")
+16 -84
arch/x86/vdso/vma.c
··· 19 19 #if defined(CONFIG_X86_64) 20 20 unsigned int __read_mostly vdso64_enabled = 1; 21 21 22 - DECLARE_VDSO_IMAGE(vdso); 23 22 extern unsigned short vdso_sync_cpuid; 24 - static unsigned vdso_size; 25 - 26 - #ifdef CONFIG_X86_X32_ABI 27 - DECLARE_VDSO_IMAGE(vdsox32); 28 - static unsigned vdsox32_size; 29 - #endif 30 23 #endif 31 24 32 - #if defined(CONFIG_X86_32) || defined(CONFIG_X86_X32_ABI) || \ 33 - defined(CONFIG_COMPAT) 34 - void __init patch_vdso32(void *vdso, size_t len) 25 + void __init init_vdso_image(const struct vdso_image *image) 35 26 { 36 - Elf32_Ehdr *hdr = vdso; 37 - Elf32_Shdr *sechdrs, *alt_sec = 0; 38 - char *secstrings; 39 - void *alt_data; 40 27 int i; 28 + int npages = (image->size) / PAGE_SIZE; 41 29 42 - BUG_ON(len < sizeof(Elf32_Ehdr)); 43 - BUG_ON(memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0); 30 + BUG_ON(image->size % PAGE_SIZE != 0); 31 + for (i = 0; i < npages; i++) 32 + image->pages[i] = virt_to_page(image->data + i*PAGE_SIZE); 44 33 45 - sechdrs = (void *)hdr + hdr->e_shoff; 46 - secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 47 - 48 - for (i = 1; i < hdr->e_shnum; i++) { 49 - Elf32_Shdr *shdr = &sechdrs[i]; 50 - if (!strcmp(secstrings + shdr->sh_name, ".altinstructions")) { 51 - alt_sec = shdr; 52 - goto found; 53 - } 54 - } 55 - 56 - /* If we get here, it's probably a bug. */ 57 - pr_warning("patch_vdso32: .altinstructions not found\n"); 58 - return; /* nothing to patch */ 59 - 60 - found: 61 - alt_data = (void *)hdr + alt_sec->sh_offset; 62 - apply_alternatives(alt_data, alt_data + alt_sec->sh_size); 34 + apply_alternatives((struct alt_instr *)(image->data + image->alt), 35 + (struct alt_instr *)(image->data + image->alt + 36 + image->alt_len)); 63 37 } 64 - #endif 38 + 65 39 66 40 #if defined(CONFIG_X86_64) 67 - static void __init patch_vdso64(void *vdso, size_t len) 68 - { 69 - Elf64_Ehdr *hdr = vdso; 70 - Elf64_Shdr *sechdrs, *alt_sec = 0; 71 - char *secstrings; 72 - void *alt_data; 73 - int i; 74 - 75 - BUG_ON(len < sizeof(Elf64_Ehdr)); 76 - BUG_ON(memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0); 77 - 78 - sechdrs = (void *)hdr + hdr->e_shoff; 79 - secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 80 - 81 - for (i = 1; i < hdr->e_shnum; i++) { 82 - Elf64_Shdr *shdr = &sechdrs[i]; 83 - if (!strcmp(secstrings + shdr->sh_name, ".altinstructions")) { 84 - alt_sec = shdr; 85 - goto found; 86 - } 87 - } 88 - 89 - /* If we get here, it's probably a bug. */ 90 - pr_warning("patch_vdso64: .altinstructions not found\n"); 91 - return; /* nothing to patch */ 92 - 93 - found: 94 - alt_data = (void *)hdr + alt_sec->sh_offset; 95 - apply_alternatives(alt_data, alt_data + alt_sec->sh_size); 96 - } 97 - 98 41 static int __init init_vdso(void) 99 42 { 100 - int npages = (vdso_end - vdso_start + PAGE_SIZE - 1) / PAGE_SIZE; 101 - int i; 102 - 103 - patch_vdso64(vdso_start, vdso_end - vdso_start); 104 - 105 - vdso_size = npages << PAGE_SHIFT; 106 - for (i = 0; i < npages; i++) 107 - vdso_pages[i] = virt_to_page(vdso_start + i*PAGE_SIZE); 43 + init_vdso_image(&vdso_image_64); 108 44 109 45 #ifdef CONFIG_X86_X32_ABI 110 - patch_vdso32(vdsox32_start, vdsox32_end - vdsox32_start); 111 - npages = (vdsox32_end - vdsox32_start + PAGE_SIZE - 1) / PAGE_SIZE; 112 - vdsox32_size = npages << PAGE_SHIFT; 113 - for (i = 0; i < npages; i++) 114 - vdsox32_pages[i] = virt_to_page(vdsox32_start + i*PAGE_SIZE); 46 + init_vdso_image(&vdso_image_x32); 115 47 #endif 116 48 117 49 return 0; ··· 103 171 goto up_fail; 104 172 } 105 173 106 - current->mm->context.vdso = (void *)addr; 174 + current->mm->context.vdso = (void __user *)addr; 107 175 108 176 ret = install_special_mapping(mm, addr, size, 109 177 VM_READ|VM_EXEC| ··· 121 189 122 190 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 123 191 { 124 - return setup_additional_pages(bprm, uses_interp, vdso_pages, 125 - vdso_size); 192 + return setup_additional_pages(bprm, uses_interp, vdso_image_64.pages, 193 + vdso_image_64.size); 126 194 } 127 195 128 196 #ifdef CONFIG_X86_X32_ABI 129 197 int x32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 130 198 { 131 - return setup_additional_pages(bprm, uses_interp, vdsox32_pages, 132 - vdsox32_size); 199 + return setup_additional_pages(bprm, uses_interp, vdso_image_x32.pages, 200 + vdso_image_x32.size); 133 201 } 134 202 #endif 135 203
+9 -2
arch/x86/xen/setup.c
··· 516 516 static void __init fiddle_vdso(void) 517 517 { 518 518 #ifdef CONFIG_X86_32 519 + /* 520 + * This could be called before selected_vdso32 is initialized, so 521 + * just fiddle with both possible images. vdso_image_32_syscall 522 + * can't be selected, since it only exists on 64-bit systems. 523 + */ 519 524 u32 *mask; 520 - mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK); 525 + mask = vdso_image_32_int80.data + 526 + vdso_image_32_int80.sym_VDSO32_NOTE_MASK; 521 527 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; 522 - mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK); 528 + mask = vdso_image_32_sysenter.data + 529 + vdso_image_32_sysenter.sym_VDSO32_NOTE_MASK; 523 530 *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; 524 531 #endif 525 532 }