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