1 /*
2 * parse_vdso.c: Linux reference vDSO parser
3 * Written by Andrew Lutomirski, 2011-2014.
4 *
5 * This code is meant to be linked in to various programs that run on Linux.
6 * As such, it is available with as few restrictions as possible. This file
7 * is licensed under the Creative Commons Zero License, version 1.0,
8 * available at http://creativecommons.org/publicdomain/zero/1.0/legalcode
9 *
10 * The vDSO is a regular ELF DSO that the kernel maps into user space when
11 * it starts a program. It works equally well in statically and dynamically
12 * linked binaries.
13 *
14 * This code is tested on x86. In principle it should work on any
15 * architecture that has a vDSO.
16 */
17
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <elf.h>
23
24 /*
25 * To use this vDSO parser, first call one of the vdso_init_* functions.
26 * If you've already parsed auxv, then pass the value of AT_SYSINFO_EHDR
27 * to vdso_init_from_sysinfo_ehdr. Otherwise pass auxv to vdso_init_from_auxv.
28 * Then call vdso_sym for each symbol you want. For example, to look up
29 * gettimeofday on x86_64, use:
30 *
31 * <some pointer> = vdso_sym("LINUX_2.6", "gettimeofday");
32 * or
33 * <some pointer> = vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
34 *
35 * vdso_sym will return 0 if the symbol doesn't exist or if the init function
36 * failed or was not called. vdso_sym is a little slow, so its return value
37 * should be cached.
38 *
39 * vdso_sym is threadsafe; the init functions are not.
40 *
41 * These are the prototypes:
42 */
43 extern void vdso_init_from_auxv(void *auxv);
44 extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
45 extern void *vdso_sym(const char *version, const char *name);
46
47
48 /* And here's the code. */
49 #ifndef ELF_BITS
50 # if ULONG_MAX > 0xffffffffUL
51 # define ELF_BITS 64
52 # else
53 # define ELF_BITS 32
54 # endif
55 #endif
56
57 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
58 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
59 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
60
61 static struct vdso_info
62 {
63 bool valid;
64
65 /* Load information */
66 uintptr_t load_addr;
67 uintptr_t load_offset; /* load_addr - recorded vaddr */
68
69 /* Symbol table */
70 ELF(Sym) *symtab;
71 const char *symstrings;
72 ELF(Word) *bucket, *chain;
73 ELF(Word) nbucket, nchain;
74
75 /* Version table */
76 ELF(Versym) *versym;
77 ELF(Verdef) *verdef;
78 } vdso_info;
79
80 /* Straight from the ELF specification. */
elf_hash(const char * s_name)81 static unsigned long elf_hash(const char *s_name)
82 {
83 unsigned long h = 0, g;
84 const unsigned char *name = (const unsigned char *)s_name;
85
86 while (*name)
87 {
88 h = (h << 4) + *name++;
89 g = h & 0xf0000000;
90 if (g)
91 h ^= g >> 24;
92 h &= ~g;
93 }
94 return h;
95 }
96
vdso_init_from_sysinfo_ehdr(uintptr_t base)97 void vdso_init_from_sysinfo_ehdr(uintptr_t base)
98 {
99 size_t i;
100 bool found_vaddr = false;
101
102 vdso_info.valid = false;
103
104 vdso_info.load_addr = base;
105
106 ELF(Ehdr) *hdr = (ELF(Ehdr)*)base;
107 if (hdr->e_ident[EI_CLASS] !=
108 (ELF_BITS == 32 ? ELFCLASS32 : ELFCLASS64)) {
109 return; /* Wrong ELF class -- check ELF_BITS */
110 }
111
112 ELF(Phdr) *pt = (ELF(Phdr)*)(vdso_info.load_addr + hdr->e_phoff);
113 ELF(Dyn) *dyn = 0;
114
115 /*
116 * We need two things from the segment table: the load offset
117 * and the dynamic table.
118 */
119 for (i = 0; i < hdr->e_phnum; i++)
120 {
121 if (pt[i].p_type == PT_LOAD && !found_vaddr) {
122 found_vaddr = true;
123 vdso_info.load_offset = base
124 + (uintptr_t)pt[i].p_offset
125 - (uintptr_t)pt[i].p_vaddr;
126 } else if (pt[i].p_type == PT_DYNAMIC) {
127 dyn = (ELF(Dyn)*)(base + pt[i].p_offset);
128 }
129 }
130
131 if (!found_vaddr || !dyn)
132 return; /* Failed */
133
134 /*
135 * Fish out the useful bits of the dynamic table.
136 */
137 ELF(Word) *hash = 0;
138 vdso_info.symstrings = 0;
139 vdso_info.symtab = 0;
140 vdso_info.versym = 0;
141 vdso_info.verdef = 0;
142 for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
143 switch (dyn[i].d_tag) {
144 case DT_STRTAB:
145 vdso_info.symstrings = (const char *)
146 ((uintptr_t)dyn[i].d_un.d_ptr
147 + vdso_info.load_offset);
148 break;
149 case DT_SYMTAB:
150 vdso_info.symtab = (ELF(Sym) *)
151 ((uintptr_t)dyn[i].d_un.d_ptr
152 + vdso_info.load_offset);
153 break;
154 case DT_HASH:
155 hash = (ELF(Word) *)
156 ((uintptr_t)dyn[i].d_un.d_ptr
157 + vdso_info.load_offset);
158 break;
159 case DT_VERSYM:
160 vdso_info.versym = (ELF(Versym) *)
161 ((uintptr_t)dyn[i].d_un.d_ptr
162 + vdso_info.load_offset);
163 break;
164 case DT_VERDEF:
165 vdso_info.verdef = (ELF(Verdef) *)
166 ((uintptr_t)dyn[i].d_un.d_ptr
167 + vdso_info.load_offset);
168 break;
169 }
170 }
171 if (!vdso_info.symstrings || !vdso_info.symtab || !hash)
172 return; /* Failed */
173
174 if (!vdso_info.verdef)
175 vdso_info.versym = 0;
176
177 /* Parse the hash table header. */
178 vdso_info.nbucket = hash[0];
179 vdso_info.nchain = hash[1];
180 vdso_info.bucket = &hash[2];
181 vdso_info.chain = &hash[vdso_info.nbucket + 2];
182
183 /* That's all we need. */
184 vdso_info.valid = true;
185 }
186
vdso_match_version(ELF (Versym)ver,const char * name,ELF (Word)hash)187 static bool vdso_match_version(ELF(Versym) ver,
188 const char *name, ELF(Word) hash)
189 {
190 /*
191 * This is a helper function to check if the version indexed by
192 * ver matches name (which hashes to hash).
193 *
194 * The version definition table is a mess, and I don't know how
195 * to do this in better than linear time without allocating memory
196 * to build an index. I also don't know why the table has
197 * variable size entries in the first place.
198 *
199 * For added fun, I can't find a comprehensible specification of how
200 * to parse all the weird flags in the table.
201 *
202 * So I just parse the whole table every time.
203 */
204
205 /* First step: find the version definition */
206 ver &= 0x7fff; /* Apparently bit 15 means "hidden" */
207 ELF(Verdef) *def = vdso_info.verdef;
208 while(true) {
209 if ((def->vd_flags & VER_FLG_BASE) == 0
210 && (def->vd_ndx & 0x7fff) == ver)
211 break;
212
213 if (def->vd_next == 0)
214 return false; /* No definition. */
215
216 def = (ELF(Verdef) *)((char *)def + def->vd_next);
217 }
218
219 /* Now figure out whether it matches. */
220 ELF(Verdaux) *aux = (ELF(Verdaux)*)((char *)def + def->vd_aux);
221 return def->vd_hash == hash
222 && !strcmp(name, vdso_info.symstrings + aux->vda_name);
223 }
224
vdso_sym(const char * version,const char * name)225 void *vdso_sym(const char *version, const char *name)
226 {
227 unsigned long ver_hash;
228 if (!vdso_info.valid)
229 return 0;
230
231 ver_hash = elf_hash(version);
232 ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
233
234 for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
235 ELF(Sym) *sym = &vdso_info.symtab[chain];
236
237 /* Check for a defined global or weak function w/ right name. */
238 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
239 continue;
240 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
241 ELF64_ST_BIND(sym->st_info) != STB_WEAK)
242 continue;
243 if (sym->st_shndx == SHN_UNDEF)
244 continue;
245 if (strcmp(name, vdso_info.symstrings + sym->st_name))
246 continue;
247
248 /* Check symbol version. */
249 if (vdso_info.versym
250 && !vdso_match_version(vdso_info.versym[chain],
251 version, ver_hash))
252 continue;
253
254 return (void *)(vdso_info.load_offset + sym->st_value);
255 }
256
257 return 0;
258 }
259
vdso_init_from_auxv(void * auxv)260 void vdso_init_from_auxv(void *auxv)
261 {
262 ELF(auxv_t) *elf_auxv = auxv;
263 for (int i = 0; elf_auxv[i].a_type != AT_NULL; i++)
264 {
265 if (elf_auxv[i].a_type == AT_SYSINFO_EHDR) {
266 vdso_init_from_sysinfo_ehdr(elf_auxv[i].a_un.a_val);
267 return;
268 }
269 }
270
271 vdso_info.valid = false;
272 }
273