parse_vdso.c 7.4 KB

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