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 #include "parse_vdso.h"
25 
26 /* And here's the code. */
27 #ifndef ELF_BITS
28 # if ULONG_MAX > 0xffffffffUL
29 #  define ELF_BITS 64
30 # else
31 #  define ELF_BITS 32
32 # endif
33 #endif
34 
35 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
36 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
37 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
38 
39 static struct vdso_info
40 {
41 	bool valid;
42 
43 	/* Load information */
44 	uintptr_t load_addr;
45 	uintptr_t load_offset;  /* load_addr - recorded vaddr */
46 
47 	/* Symbol table */
48 	ELF(Sym) *symtab;
49 	const char *symstrings;
50 	ELF(Word) *bucket, *chain;
51 	ELF(Word) nbucket, nchain;
52 
53 	/* Version table */
54 	ELF(Versym) *versym;
55 	ELF(Verdef) *verdef;
56 } vdso_info;
57 
58 /*
59  * Straight from the ELF specification...and then tweaked slightly, in order to
60  * avoid a few clang warnings.
61  */
elf_hash(const char * name)62 static unsigned long elf_hash(const char *name)
63 {
64 	unsigned long h = 0, g;
65 	const unsigned char *uch_name = (const unsigned char *)name;
66 
67 	while (*uch_name)
68 	{
69 		h = (h << 4) + *uch_name++;
70 		g = h & 0xf0000000;
71 		if (g)
72 			h ^= g >> 24;
73 		h &= ~g;
74 	}
75 	return h;
76 }
77 
vdso_init_from_sysinfo_ehdr(uintptr_t base)78 void vdso_init_from_sysinfo_ehdr(uintptr_t base)
79 {
80 	size_t i;
81 	bool found_vaddr = false;
82 
83 	vdso_info.valid = false;
84 
85 	vdso_info.load_addr = base;
86 
87 	ELF(Ehdr) *hdr = (ELF(Ehdr)*)base;
88 	if (hdr->e_ident[EI_CLASS] !=
89 	    (ELF_BITS == 32 ? ELFCLASS32 : ELFCLASS64)) {
90 		return;  /* Wrong ELF class -- check ELF_BITS */
91 	}
92 
93 	ELF(Phdr) *pt = (ELF(Phdr)*)(vdso_info.load_addr + hdr->e_phoff);
94 	ELF(Dyn) *dyn = 0;
95 
96 	/*
97 	 * We need two things from the segment table: the load offset
98 	 * and the dynamic table.
99 	 */
100 	for (i = 0; i < hdr->e_phnum; i++)
101 	{
102 		if (pt[i].p_type == PT_LOAD && !found_vaddr) {
103 			found_vaddr = true;
104 			vdso_info.load_offset =	base
105 				+ (uintptr_t)pt[i].p_offset
106 				- (uintptr_t)pt[i].p_vaddr;
107 		} else if (pt[i].p_type == PT_DYNAMIC) {
108 			dyn = (ELF(Dyn)*)(base + pt[i].p_offset);
109 		}
110 	}
111 
112 	if (!found_vaddr || !dyn)
113 		return;  /* Failed */
114 
115 	/*
116 	 * Fish out the useful bits of the dynamic table.
117 	 */
118 	ELF(Word) *hash = 0;
119 	vdso_info.symstrings = 0;
120 	vdso_info.symtab = 0;
121 	vdso_info.versym = 0;
122 	vdso_info.verdef = 0;
123 	for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
124 		switch (dyn[i].d_tag) {
125 		case DT_STRTAB:
126 			vdso_info.symstrings = (const char *)
127 				((uintptr_t)dyn[i].d_un.d_ptr
128 				 + vdso_info.load_offset);
129 			break;
130 		case DT_SYMTAB:
131 			vdso_info.symtab = (ELF(Sym) *)
132 				((uintptr_t)dyn[i].d_un.d_ptr
133 				 + vdso_info.load_offset);
134 			break;
135 		case DT_HASH:
136 			hash = (ELF(Word) *)
137 				((uintptr_t)dyn[i].d_un.d_ptr
138 				 + vdso_info.load_offset);
139 			break;
140 		case DT_VERSYM:
141 			vdso_info.versym = (ELF(Versym) *)
142 				((uintptr_t)dyn[i].d_un.d_ptr
143 				 + vdso_info.load_offset);
144 			break;
145 		case DT_VERDEF:
146 			vdso_info.verdef = (ELF(Verdef) *)
147 				((uintptr_t)dyn[i].d_un.d_ptr
148 				 + vdso_info.load_offset);
149 			break;
150 		}
151 	}
152 	if (!vdso_info.symstrings || !vdso_info.symtab || !hash)
153 		return;  /* Failed */
154 
155 	if (!vdso_info.verdef)
156 		vdso_info.versym = 0;
157 
158 	/* Parse the hash table header. */
159 	vdso_info.nbucket = hash[0];
160 	vdso_info.nchain = hash[1];
161 	vdso_info.bucket = &hash[2];
162 	vdso_info.chain = &hash[vdso_info.nbucket + 2];
163 
164 	/* That's all we need. */
165 	vdso_info.valid = true;
166 }
167 
vdso_match_version(ELF (Versym)ver,const char * name,ELF (Word)hash)168 static bool vdso_match_version(ELF(Versym) ver,
169 			       const char *name, ELF(Word) hash)
170 {
171 	/*
172 	 * This is a helper function to check if the version indexed by
173 	 * ver matches name (which hashes to hash).
174 	 *
175 	 * The version definition table is a mess, and I don't know how
176 	 * to do this in better than linear time without allocating memory
177 	 * to build an index.  I also don't know why the table has
178 	 * variable size entries in the first place.
179 	 *
180 	 * For added fun, I can't find a comprehensible specification of how
181 	 * to parse all the weird flags in the table.
182 	 *
183 	 * So I just parse the whole table every time.
184 	 */
185 
186 	/* First step: find the version definition */
187 	ver &= 0x7fff;  /* Apparently bit 15 means "hidden" */
188 	ELF(Verdef) *def = vdso_info.verdef;
189 	while(true) {
190 		if ((def->vd_flags & VER_FLG_BASE) == 0
191 		    && (def->vd_ndx & 0x7fff) == ver)
192 			break;
193 
194 		if (def->vd_next == 0)
195 			return false;  /* No definition. */
196 
197 		def = (ELF(Verdef) *)((char *)def + def->vd_next);
198 	}
199 
200 	/* Now figure out whether it matches. */
201 	ELF(Verdaux) *aux = (ELF(Verdaux)*)((char *)def + def->vd_aux);
202 	return def->vd_hash == hash
203 		&& !strcmp(name, vdso_info.symstrings + aux->vda_name);
204 }
205 
vdso_sym(const char * version,const char * name)206 void *vdso_sym(const char *version, const char *name)
207 {
208 	unsigned long ver_hash;
209 	if (!vdso_info.valid)
210 		return 0;
211 
212 	ver_hash = elf_hash(version);
213 	ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
214 
215 	for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
216 		ELF(Sym) *sym = &vdso_info.symtab[chain];
217 
218 		/* Check for a defined global or weak function w/ right name. */
219 		if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
220 			continue;
221 		if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
222 		    ELF64_ST_BIND(sym->st_info) != STB_WEAK)
223 			continue;
224 		if (sym->st_shndx == SHN_UNDEF)
225 			continue;
226 		if (strcmp(name, vdso_info.symstrings + sym->st_name))
227 			continue;
228 
229 		/* Check symbol version. */
230 		if (vdso_info.versym
231 		    && !vdso_match_version(vdso_info.versym[chain],
232 					   version, ver_hash))
233 			continue;
234 
235 		return (void *)(vdso_info.load_offset + sym->st_value);
236 	}
237 
238 	return 0;
239 }
240 
vdso_init_from_auxv(void * auxv)241 void vdso_init_from_auxv(void *auxv)
242 {
243 	ELF(auxv_t) *elf_auxv = auxv;
244 	for (int i = 0; elf_auxv[i].a_type != AT_NULL; i++)
245 	{
246 		if (elf_auxv[i].a_type == AT_SYSINFO_EHDR) {
247 			vdso_init_from_sysinfo_ehdr(elf_auxv[i].a_un.a_val);
248 			return;
249 		}
250 	}
251 
252 	vdso_info.valid = false;
253 }
254