test_mremap_vdso.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * 32-bit test to check vDSO mremap.
  3. *
  4. * Copyright (c) 2016 Dmitry Safonov
  5. * Suggested-by: Andrew Lutomirski
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. /*
  17. * Can be built statically:
  18. * gcc -Os -Wall -static -m32 test_mremap_vdso.c
  19. */
  20. #define _GNU_SOURCE
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <sys/mman.h>
  26. #include <sys/auxv.h>
  27. #include <sys/syscall.h>
  28. #include <sys/wait.h>
  29. #define PAGE_SIZE 4096
  30. static int try_to_remap(void *vdso_addr, unsigned long size)
  31. {
  32. void *dest_addr, *new_addr;
  33. /* Searching for memory location where to remap */
  34. dest_addr = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  35. if (dest_addr == MAP_FAILED) {
  36. printf("[WARN]\tmmap failed (%d): %m\n", errno);
  37. return 0;
  38. }
  39. printf("[NOTE]\tMoving vDSO: [%p, %#lx] -> [%p, %#lx]\n",
  40. vdso_addr, (unsigned long)vdso_addr + size,
  41. dest_addr, (unsigned long)dest_addr + size);
  42. fflush(stdout);
  43. new_addr = mremap(vdso_addr, size, size,
  44. MREMAP_FIXED|MREMAP_MAYMOVE, dest_addr);
  45. if ((unsigned long)new_addr == (unsigned long)-1) {
  46. munmap(dest_addr, size);
  47. if (errno == EINVAL) {
  48. printf("[NOTE]\tvDSO partial move failed, will try with bigger size\n");
  49. return -1; /* Retry with larger */
  50. }
  51. printf("[FAIL]\tmremap failed (%d): %m\n", errno);
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. int main(int argc, char **argv, char **envp)
  57. {
  58. pid_t child;
  59. child = fork();
  60. if (child == -1) {
  61. printf("[WARN]\tfailed to fork (%d): %m\n", errno);
  62. return 1;
  63. }
  64. if (child == 0) {
  65. unsigned long vdso_size = PAGE_SIZE;
  66. unsigned long auxval;
  67. int ret = -1;
  68. auxval = getauxval(AT_SYSINFO_EHDR);
  69. printf("\tAT_SYSINFO_EHDR is %#lx\n", auxval);
  70. if (!auxval || auxval == -ENOENT) {
  71. printf("[WARN]\tgetauxval failed\n");
  72. return 0;
  73. }
  74. /* Simpler than parsing ELF header */
  75. while (ret < 0) {
  76. ret = try_to_remap((void *)auxval, vdso_size);
  77. vdso_size += PAGE_SIZE;
  78. }
  79. #ifdef __i386__
  80. /* Glibc is likely to explode now - exit with raw syscall */
  81. asm volatile ("int $0x80" : : "a" (__NR_exit), "b" (!!ret));
  82. #else /* __x86_64__ */
  83. syscall(SYS_exit, ret);
  84. #endif
  85. } else {
  86. int status;
  87. if (waitpid(child, &status, 0) != child ||
  88. !WIFEXITED(status)) {
  89. printf("[FAIL]\tmremap() of the vDSO does not work on this kernel!\n");
  90. return 1;
  91. } else if (WEXITSTATUS(status) != 0) {
  92. printf("[FAIL]\tChild failed with %d\n",
  93. WEXITSTATUS(status));
  94. return 1;
  95. }
  96. printf("[OK]\n");
  97. }
  98. return 0;
  99. }