Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * parse_vdso.c: Linux reference vDSO parser
0003  * Written by Andrew Lutomirski, 2011-2014.
0004  *
0005  * This code is meant to be linked in to various programs that run on Linux.
0006  * As such, it is available with as few restrictions as possible.  This file
0007  * is licensed under the Creative Commons Zero License, version 1.0,
0008  * available at http://creativecommons.org/publicdomain/zero/1.0/legalcode
0009  *
0010  * The vDSO is a regular ELF DSO that the kernel maps into user space when
0011  * it starts a program.  It works equally well in statically and dynamically
0012  * linked binaries.
0013  *
0014  * This code is tested on x86.  In principle it should work on any
0015  * architecture that has a vDSO.
0016  */
0017 
0018 #include <stdbool.h>
0019 #include <stdint.h>
0020 #include <string.h>
0021 #include <limits.h>
0022 #include <elf.h>
0023 
0024 #include "parse_vdso.h"
0025 
0026 /* And here's the code. */
0027 #ifndef ELF_BITS
0028 # if ULONG_MAX > 0xffffffffUL
0029 #  define ELF_BITS 64
0030 # else
0031 #  define ELF_BITS 32
0032 # endif
0033 #endif
0034 
0035 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
0036 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
0037 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
0038 
0039 static struct vdso_info
0040 {
0041     bool valid;
0042 
0043     /* Load information */
0044     uintptr_t load_addr;
0045     uintptr_t load_offset;  /* load_addr - recorded vaddr */
0046 
0047     /* Symbol table */
0048     ELF(Sym) *symtab;
0049     const char *symstrings;
0050     ELF(Word) *bucket, *chain;
0051     ELF(Word) nbucket, nchain;
0052 
0053     /* Version table */
0054     ELF(Versym) *versym;
0055     ELF(Verdef) *verdef;
0056 } vdso_info;
0057 
0058 /* Straight from the ELF specification. */
0059 static unsigned long elf_hash(const unsigned char *name)
0060 {
0061     unsigned long h = 0, g;
0062     while (*name)
0063     {
0064         h = (h << 4) + *name++;
0065         if (g = h & 0xf0000000)
0066             h ^= g >> 24;
0067         h &= ~g;
0068     }
0069     return h;
0070 }
0071 
0072 void vdso_init_from_sysinfo_ehdr(uintptr_t base)
0073 {
0074     size_t i;
0075     bool found_vaddr = false;
0076 
0077     vdso_info.valid = false;
0078 
0079     vdso_info.load_addr = base;
0080 
0081     ELF(Ehdr) *hdr = (ELF(Ehdr)*)base;
0082     if (hdr->e_ident[EI_CLASS] !=
0083         (ELF_BITS == 32 ? ELFCLASS32 : ELFCLASS64)) {
0084         return;  /* Wrong ELF class -- check ELF_BITS */
0085     }
0086 
0087     ELF(Phdr) *pt = (ELF(Phdr)*)(vdso_info.load_addr + hdr->e_phoff);
0088     ELF(Dyn) *dyn = 0;
0089 
0090     /*
0091      * We need two things from the segment table: the load offset
0092      * and the dynamic table.
0093      */
0094     for (i = 0; i < hdr->e_phnum; i++)
0095     {
0096         if (pt[i].p_type == PT_LOAD && !found_vaddr) {
0097             found_vaddr = true;
0098             vdso_info.load_offset = base
0099                 + (uintptr_t)pt[i].p_offset
0100                 - (uintptr_t)pt[i].p_vaddr;
0101         } else if (pt[i].p_type == PT_DYNAMIC) {
0102             dyn = (ELF(Dyn)*)(base + pt[i].p_offset);
0103         }
0104     }
0105 
0106     if (!found_vaddr || !dyn)
0107         return;  /* Failed */
0108 
0109     /*
0110      * Fish out the useful bits of the dynamic table.
0111      */
0112     ELF(Word) *hash = 0;
0113     vdso_info.symstrings = 0;
0114     vdso_info.symtab = 0;
0115     vdso_info.versym = 0;
0116     vdso_info.verdef = 0;
0117     for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
0118         switch (dyn[i].d_tag) {
0119         case DT_STRTAB:
0120             vdso_info.symstrings = (const char *)
0121                 ((uintptr_t)dyn[i].d_un.d_ptr
0122                  + vdso_info.load_offset);
0123             break;
0124         case DT_SYMTAB:
0125             vdso_info.symtab = (ELF(Sym) *)
0126                 ((uintptr_t)dyn[i].d_un.d_ptr
0127                  + vdso_info.load_offset);
0128             break;
0129         case DT_HASH:
0130             hash = (ELF(Word) *)
0131                 ((uintptr_t)dyn[i].d_un.d_ptr
0132                  + vdso_info.load_offset);
0133             break;
0134         case DT_VERSYM:
0135             vdso_info.versym = (ELF(Versym) *)
0136                 ((uintptr_t)dyn[i].d_un.d_ptr
0137                  + vdso_info.load_offset);
0138             break;
0139         case DT_VERDEF:
0140             vdso_info.verdef = (ELF(Verdef) *)
0141                 ((uintptr_t)dyn[i].d_un.d_ptr
0142                  + vdso_info.load_offset);
0143             break;
0144         }
0145     }
0146     if (!vdso_info.symstrings || !vdso_info.symtab || !hash)
0147         return;  /* Failed */
0148 
0149     if (!vdso_info.verdef)
0150         vdso_info.versym = 0;
0151 
0152     /* Parse the hash table header. */
0153     vdso_info.nbucket = hash[0];
0154     vdso_info.nchain = hash[1];
0155     vdso_info.bucket = &hash[2];
0156     vdso_info.chain = &hash[vdso_info.nbucket + 2];
0157 
0158     /* That's all we need. */
0159     vdso_info.valid = true;
0160 }
0161 
0162 static bool vdso_match_version(ELF(Versym) ver,
0163                    const char *name, ELF(Word) hash)
0164 {
0165     /*
0166      * This is a helper function to check if the version indexed by
0167      * ver matches name (which hashes to hash).
0168      *
0169      * The version definition table is a mess, and I don't know how
0170      * to do this in better than linear time without allocating memory
0171      * to build an index.  I also don't know why the table has
0172      * variable size entries in the first place.
0173      *
0174      * For added fun, I can't find a comprehensible specification of how
0175      * to parse all the weird flags in the table.
0176      *
0177      * So I just parse the whole table every time.
0178      */
0179 
0180     /* First step: find the version definition */
0181     ver &= 0x7fff;  /* Apparently bit 15 means "hidden" */
0182     ELF(Verdef) *def = vdso_info.verdef;
0183     while(true) {
0184         if ((def->vd_flags & VER_FLG_BASE) == 0
0185             && (def->vd_ndx & 0x7fff) == ver)
0186             break;
0187 
0188         if (def->vd_next == 0)
0189             return false;  /* No definition. */
0190 
0191         def = (ELF(Verdef) *)((char *)def + def->vd_next);
0192     }
0193 
0194     /* Now figure out whether it matches. */
0195     ELF(Verdaux) *aux = (ELF(Verdaux)*)((char *)def + def->vd_aux);
0196     return def->vd_hash == hash
0197         && !strcmp(name, vdso_info.symstrings + aux->vda_name);
0198 }
0199 
0200 void *vdso_sym(const char *version, const char *name)
0201 {
0202     unsigned long ver_hash;
0203     if (!vdso_info.valid)
0204         return 0;
0205 
0206     ver_hash = elf_hash(version);
0207     ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
0208 
0209     for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
0210         ELF(Sym) *sym = &vdso_info.symtab[chain];
0211 
0212         /* Check for a defined global or weak function w/ right name. */
0213         if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
0214             continue;
0215         if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
0216             ELF64_ST_BIND(sym->st_info) != STB_WEAK)
0217             continue;
0218         if (sym->st_shndx == SHN_UNDEF)
0219             continue;
0220         if (strcmp(name, vdso_info.symstrings + sym->st_name))
0221             continue;
0222 
0223         /* Check symbol version. */
0224         if (vdso_info.versym
0225             && !vdso_match_version(vdso_info.versym[chain],
0226                        version, ver_hash))
0227             continue;
0228 
0229         return (void *)(vdso_info.load_offset + sym->st_value);
0230     }
0231 
0232     return 0;
0233 }
0234 
0235 void vdso_init_from_auxv(void *auxv)
0236 {
0237     ELF(auxv_t) *elf_auxv = auxv;
0238     for (int i = 0; elf_auxv[i].a_type != AT_NULL; i++)
0239     {
0240         if (elf_auxv[i].a_type == AT_SYSINFO_EHDR) {
0241             vdso_init_from_sysinfo_ehdr(elf_auxv[i].a_un.a_val);
0242             return;
0243         }
0244     }
0245 
0246     vdso_info.valid = false;
0247 }