linux-user: Define AT_RANDOM to support target stack protection mechanism.

The dynamic linker from the GNU C library v2.10+ uses the ELF
auxiliary vector AT_RANDOM [1] as a pointer to 16 bytes with random
values to initialize the stack protection mechanism.  Technically the
emulated GNU dynamic linker crashes due to a NULL pointer
derefencement if it is built with stack protection enabled and if
AT_RANDOM is not defined by the QEMU ELF loader.

[1] This ELF auxiliary vector was introduced in Linux v2.6.29.

This patch can be tested with the code above:

    #include <elf.h>       /* Elf*_auxv_t, AT_RANDOM, */
    #include <stdio.h>     /* printf(3), */
    #include <stdlib.h>    /* exit(3), EXIT_*, */
    #include <stdint.h>    /* uint8_t, */
    #include <string.h>    /* memcpy(3), */

    #if defined(__LP64__) || defined(__ILP64__) || defined(__LLP64__)
    #    define Elf_auxv_t Elf64_auxv_t
    #else
    #    define Elf_auxv_t Elf32_auxv_t
    #endif

    main(int argc, char* argv[], char* envp[])
    {
        Elf_auxv_t *auxv;

        /* *envp = NULL marks end of envp. */
        while (*envp++ != NULL);

        /* auxv->a_type = AT_NULL marks the end of auxv. */
        for (auxv = (Elf_auxv_t *)envp; auxv->a_type != AT_NULL; auxv++) {
            if (auxv->a_type == AT_RANDOM) {
                int i;
                uint8_t rand_bytes[16];

                printf("AT_RANDOM is: 0x%x\n", auxv->a_un.a_val);
                memcpy(rand_bytes, (const uint8_t *)auxv->a_un.a_val, sizeof(rand_bytes));
                printf("it points to: ");
                for (i = 0; i < 16; i++) {
                    printf("0x%02x ", rand_bytes[i]);
                }
                printf("\n");
                exit(EXIT_SUCCESS);
            }
        }
        exit(EXIT_FAILURE);
    }

Changes introduced in v2 and v3:

    * Fix typos + thinko (AT_RANDOM is used for stack canary, not for
      ASLR)

    * AT_RANDOM points to 16 random bytes stored inside the user
      stack.

    * Add a small test program.

Signed-off-by: Cédric VINCENT <cedric.vincent@st.com>
Signed-off-by: Laurent ALFONSI <laurent.alfonsi@st.com>
Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
1 file changed
tree: 0ee597cebc6ded214c1b771458aea3dc1a3b1c4b
  1. audio/
  2. block/
  3. bsd-user/
  4. darwin-user/
  5. default-configs/
  6. docs/
  7. fpu/
  8. fsdev/
  9. gdb-xml/
  10. hw/
  11. libcacard/
  12. linux-user/
  13. net/
  14. pc-bios/
  15. QMP/
  16. roms/
  17. scripts/
  18. slirp/
  19. sysconfigs/
  20. target-alpha/
  21. target-arm/
  22. target-cris/
  23. target-i386/
  24. target-lm32/
  25. target-m68k/
  26. target-microblaze/
  27. target-mips/
  28. target-ppc/
  29. target-s390x/
  30. target-sh4/
  31. target-sparc/
  32. target-unicore32/
  33. tcg/
  34. tests/
  35. ui/
  36. .gitignore
  37. .gitmodules
  38. a.out.h
  39. acl.c
  40. acl.h
  41. aes.c
  42. aes.h
  43. aio.c
  44. alpha-dis.c
  45. alpha.ld
  46. arch_init.c
  47. arch_init.h
  48. arm-dis.c
  49. arm-semi.c
  50. arm.ld
  51. async.c
  52. balloon.c
  53. balloon.h
  54. bitmap.c
  55. bitmap.h
  56. bitops.c
  57. bitops.h
  58. block-migration.c
  59. block-migration.h
  60. block.c
  61. block.h
  62. block_int.h
  63. blockdev.c
  64. blockdev.h
  65. bswap.h
  66. bt-host.c
  67. bt-host.h
  68. bt-vhci.c
  69. buffered_file.c
  70. buffered_file.h
  71. cache-utils.c
  72. cache-utils.h
  73. Changelog
  74. check-qdict.c
  75. check-qfloat.c
  76. check-qint.c
  77. check-qjson.c
  78. check-qlist.c
  79. check-qstring.c
  80. cmd.c
  81. cmd.h
  82. CODING_STYLE
  83. compatfd.c
  84. compatfd.h
  85. config.h
  86. configure
  87. console.c
  88. console.h
  89. COPYING
  90. COPYING.LIB
  91. cpu-all.h
  92. cpu-common.h
  93. cpu-defs.h
  94. cpu-exec.c
  95. cpus.c
  96. cpus.h
  97. cris-dis.c
  98. cursor.c
  99. cursor_hidden.xpm
  100. cursor_left_ptr.xpm
  101. cutils.c
  102. def-helper.h
  103. device_tree.c
  104. device_tree.h
  105. dis-asm.h
  106. disas.c
  107. disas.h
  108. dma-helpers.c
  109. dma.h
  110. dyngen-exec.h
  111. elf.h
  112. envlist.c
  113. envlist.h
  114. error.c
  115. error.h
  116. error_int.h
  117. exec-all.h
  118. exec.c
  119. gdbstub.c
  120. gdbstub.h
  121. gen-icount.h
  122. HACKING
  123. hmp-commands.hx
  124. host-utils.c
  125. host-utils.h
  126. hpet.h
  127. hppa-dis.c
  128. hppa.ld
  129. i386-dis.c
  130. i386.ld
  131. ia64-dis.c
  132. ia64.ld
  133. input.c
  134. iohandler.c
  135. ioport-user.c
  136. ioport.c
  137. ioport.h
  138. iorange.h
  139. iov.c
  140. iov.h
  141. json-lexer.c
  142. json-lexer.h
  143. json-parser.c
  144. json-parser.h
  145. json-streamer.c
  146. json-streamer.h
  147. kvm-all.c
  148. kvm-stub.c
  149. kvm.h
  150. libfdt_env.h
  151. LICENSE
  152. linux-aio.c
  153. m68k-dis.c
  154. m68k-semi.c
  155. m68k.ld
  156. MAINTAINERS
  157. Makefile
  158. Makefile.dis
  159. Makefile.hw
  160. Makefile.objs
  161. Makefile.target
  162. Makefile.user
  163. microblaze-dis.c
  164. migration-exec.c
  165. migration-fd.c
  166. migration-tcp.c
  167. migration-unix.c
  168. migration.c
  169. migration.h
  170. mips-dis.c
  171. mips.ld
  172. module.c
  173. module.h
  174. monitor.c
  175. monitor.h
  176. nbd.c
  177. nbd.h
  178. net.c
  179. net.h
  180. notify.c
  181. notify.h
  182. os-posix.c
  183. os-win32.c
  184. osdep.c
  185. osdep.h
  186. oslib-posix.c
  187. oslib-win32.c
  188. path.c
  189. pci-ids.txt
  190. pflib.c
  191. pflib.h
  192. poison.h
  193. posix-aio-compat.c
  194. ppc-dis.c
  195. ppc.ld
  196. ppc64.ld
  197. qbool.c
  198. qbool.h
  199. qdict-test-data.txt
  200. qdict.c
  201. qdict.h
  202. qemu-aio.h
  203. qemu-barrier.h
  204. qemu-char.c
  205. qemu-char.h
  206. qemu-common.h
  207. qemu-config.c
  208. qemu-config.h
  209. qemu-doc.texi
  210. qemu-error.c
  211. qemu-error.h
  212. qemu-img-cmds.hx
  213. qemu-img.c
  214. qemu-img.texi
  215. qemu-io.c
  216. qemu-lock.h
  217. qemu-log.h
  218. qemu-malloc.c
  219. qemu-nbd.c
  220. qemu-nbd.texi
  221. qemu-objects.h
  222. qemu-option.c
  223. qemu-option.h
  224. qemu-options.h
  225. qemu-options.hx
  226. qemu-os-posix.h
  227. qemu-os-win32.h
  228. qemu-progress.c
  229. qemu-queue.h
  230. qemu-sockets.c
  231. qemu-tech.texi
  232. qemu-thread-posix.c
  233. qemu-thread-posix.h
  234. qemu-thread-win32.c
  235. qemu-thread-win32.h
  236. qemu-thread.h
  237. qemu-timer-common.c
  238. qemu-timer.c
  239. qemu-timer.h
  240. qemu-tool.c
  241. qemu-x509.h
  242. qemu.sasl
  243. qemu_socket.h
  244. qerror.c
  245. qerror.h
  246. qfloat.c
  247. qfloat.h
  248. qint.c
  249. qint.h
  250. qjson.c
  251. qjson.h
  252. qlist.c
  253. qlist.h
  254. qmp-commands.hx
  255. qobject.h
  256. qstring.c
  257. qstring.h
  258. range.h
  259. readline.c
  260. readline.h
  261. README
  262. rules.mak
  263. rwhandler.c
  264. rwhandler.h
  265. s390-dis.c
  266. s390.ld
  267. savevm.c
  268. sh4-dis.c
  269. simpletrace.c
  270. simpletrace.h
  271. softmmu-semi.h
  272. softmmu_defs.h
  273. softmmu_exec.h
  274. softmmu_header.h
  275. softmmu_template.h
  276. sparc-dis.c
  277. sparc.ld
  278. sparc64.ld
  279. spice-qemu-char.c
  280. sysemu.h
  281. targphys.h
  282. tcg-runtime.c
  283. thunk.c
  284. thunk.h
  285. TODO
  286. trace-events
  287. translate-all.c
  288. uboot_image.h
  289. usb-bsd.c
  290. usb-linux.c
  291. usb-stub.c
  292. user-exec.c
  293. VERSION
  294. version.rc
  295. vgafont.h
  296. vl.c
  297. x86_64.ld
  298. xen-all.c
  299. xen-mapcache-stub.c
  300. xen-mapcache.c
  301. xen-mapcache.h
  302. xen-stub.c