ELFImage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include <AK/StringBuilder.h>
  2. #include <AK/kstdio.h>
  3. #include <LibELF/ELFImage.h>
  4. ELFImage::ELFImage(const u8* buffer, size_t size)
  5. : m_buffer(buffer)
  6. , m_size(size)
  7. {
  8. m_valid = parse();
  9. }
  10. ELFImage::~ELFImage()
  11. {
  12. }
  13. static const char* object_file_type_to_string(Elf32_Half type)
  14. {
  15. switch (type) {
  16. case ET_NONE:
  17. return "None";
  18. case ET_REL:
  19. return "Relocatable";
  20. case ET_EXEC:
  21. return "Executable";
  22. case ET_DYN:
  23. return "Shared object";
  24. case ET_CORE:
  25. return "Core";
  26. default:
  27. return "(?)";
  28. }
  29. }
  30. StringView ELFImage::section_index_to_string(unsigned index) const
  31. {
  32. if (index == SHN_UNDEF)
  33. return "Undefined";
  34. if (index >= SHN_LORESERVE)
  35. return "Reserved";
  36. return section(index).name();
  37. }
  38. unsigned ELFImage::symbol_count() const
  39. {
  40. return section(m_symbol_table_section_index).entry_count();
  41. }
  42. void ELFImage::dump() const
  43. {
  44. dbgprintf("ELFImage{%p} {\n", this);
  45. dbgprintf(" is_valid: %u\n", is_valid());
  46. if (!is_valid()) {
  47. dbgprintf("}\n");
  48. return;
  49. }
  50. dbgprintf(" type: %s\n", object_file_type_to_string(header().e_type));
  51. dbgprintf(" machine: %u\n", header().e_machine);
  52. dbgprintf(" entry: %x\n", header().e_entry);
  53. dbgprintf(" shoff: %u\n", header().e_shoff);
  54. dbgprintf(" shnum: %u\n", header().e_shnum);
  55. dbgprintf(" phoff: %u\n", header().e_phoff);
  56. dbgprintf(" phnum: %u\n", header().e_phnum);
  57. dbgprintf(" shstrndx: %u\n", header().e_shstrndx);
  58. for_each_program_header([&](const ProgramHeader& program_header) {
  59. dbgprintf(" Program Header %d: {\n", program_header.index());
  60. dbgprintf(" type: %x\n", program_header.type());
  61. dbgprintf(" offset: %x\n", program_header.offset());
  62. dbgprintf(" flags: %x\n", program_header.flags());
  63. dbgprintf(" \n");
  64. dbgprintf(" }\n");
  65. });
  66. for (unsigned i = 0; i < header().e_shnum; ++i) {
  67. auto& section = this->section(i);
  68. dbgprintf(" Section %u: {\n", i);
  69. dbgprintf(" name: %s\n", section.name());
  70. dbgprintf(" type: %x\n", section.type());
  71. dbgprintf(" offset: %x\n", section.offset());
  72. dbgprintf(" size: %u\n", section.size());
  73. dbgprintf(" \n");
  74. dbgprintf(" }\n");
  75. }
  76. dbgprintf("Symbol count: %u (table is %u)\n", symbol_count(), m_symbol_table_section_index);
  77. for (unsigned i = 1; i < symbol_count(); ++i) {
  78. auto& sym = symbol(i);
  79. dbgprintf("Symbol @%u:\n", i);
  80. dbgprintf(" Name: %s\n", sym.name());
  81. dbgprintf(" In section: %s\n", section_index_to_string(sym.section_index()));
  82. dbgprintf(" Value: %x\n", sym.value());
  83. dbgprintf(" Size: %u\n", sym.size());
  84. }
  85. dbgprintf("}\n");
  86. }
  87. unsigned ELFImage::section_count() const
  88. {
  89. return header().e_shnum;
  90. }
  91. unsigned ELFImage::program_header_count() const
  92. {
  93. return header().e_phnum;
  94. }
  95. bool ELFImage::parse()
  96. {
  97. if (!validate_elf_header(header(), m_size)) {
  98. dbgputstr("ELFImage::parse(): ELF Header not valid\n");
  99. return false;
  100. }
  101. // First locate the string tables.
  102. for (unsigned i = 0; i < section_count(); ++i) {
  103. auto& sh = section_header(i);
  104. if (sh.sh_type == SHT_SYMTAB) {
  105. ASSERT(!m_symbol_table_section_index || m_symbol_table_section_index == i);
  106. m_symbol_table_section_index = i;
  107. }
  108. if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
  109. if (StringView(".strtab") == section_header_table_string(sh.sh_name))
  110. m_string_table_section_index = i;
  111. }
  112. }
  113. // Then create a name-to-index map.
  114. for (unsigned i = 0; i < section_count(); ++i) {
  115. auto& section = this->section(i);
  116. m_sections.set(section.name(), move(i));
  117. }
  118. return true;
  119. }
  120. StringView ELFImage::table_string(unsigned table_index, unsigned offset) const
  121. {
  122. auto& sh = section_header(table_index);
  123. if (sh.sh_type != SHT_STRTAB)
  124. return nullptr;
  125. size_t computed_offset = sh.sh_offset + offset;
  126. if (computed_offset >= m_size) {
  127. dbgprintf("SHENANIGANS! ELFImage::table_string() computed offset outside image.\n");
  128. return {};
  129. }
  130. size_t max_length = m_size - computed_offset;
  131. size_t length = strnlen(raw_data(sh.sh_offset + offset), max_length);
  132. return { raw_data(sh.sh_offset + offset), length };
  133. }
  134. StringView ELFImage::section_header_table_string(unsigned offset) const
  135. {
  136. return table_string(header().e_shstrndx, offset);
  137. }
  138. StringView ELFImage::table_string(unsigned offset) const
  139. {
  140. return table_string(m_string_table_section_index, offset);
  141. }
  142. const char* ELFImage::raw_data(unsigned offset) const
  143. {
  144. return reinterpret_cast<const char*>(m_buffer) + offset;
  145. }
  146. const Elf32_Ehdr& ELFImage::header() const
  147. {
  148. return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
  149. }
  150. const Elf32_Phdr& ELFImage::program_header_internal(unsigned index) const
  151. {
  152. ASSERT(index < header().e_phnum);
  153. return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
  154. }
  155. const Elf32_Shdr& ELFImage::section_header(unsigned index) const
  156. {
  157. ASSERT(index < header().e_shnum);
  158. return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
  159. }
  160. const ELFImage::Symbol ELFImage::symbol(unsigned index) const
  161. {
  162. ASSERT(index < symbol_count());
  163. auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
  164. return Symbol(*this, index, raw_syms[index]);
  165. }
  166. const ELFImage::Section ELFImage::section(unsigned index) const
  167. {
  168. ASSERT(index < section_count());
  169. return Section(*this, index);
  170. }
  171. const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
  172. {
  173. ASSERT(index < program_header_count());
  174. return ProgramHeader(*this, index);
  175. }
  176. const ELFImage::Relocation ELFImage::RelocationSection::relocation(unsigned index) const
  177. {
  178. ASSERT(index < relocation_count());
  179. auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
  180. return Relocation(m_image, rels[index]);
  181. }
  182. const ELFImage::RelocationSection ELFImage::Section::relocations() const
  183. {
  184. StringBuilder builder;
  185. builder.append(".rel");
  186. builder.append(name());
  187. auto relocation_section = m_image.lookup_section(builder.to_string());
  188. if (relocation_section.type() != SHT_REL)
  189. return static_cast<const RelocationSection>(m_image.section(0));
  190. #ifdef ELFIMAGE_DEBUG
  191. dbgprintf("Found relocations for %s in %s\n", name(), relocation_section.name());
  192. #endif
  193. return static_cast<const RelocationSection>(relocation_section);
  194. }
  195. const ELFImage::Section ELFImage::lookup_section(const String& name) const
  196. {
  197. if (auto it = m_sections.find(name); it != m_sections.end())
  198. return section((*it).value);
  199. return section(0);
  200. }
  201. bool ELFImage::validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size)
  202. {
  203. if (!IS_ELF(elf_header)) {
  204. dbgputstr("File is not an ELF file.\n");
  205. return false;
  206. }
  207. if (ELFCLASS32 != elf_header.e_ident[EI_CLASS]) {
  208. dbgputstr("File is not a 32 bit ELF file.\n");
  209. return false;
  210. }
  211. if (ELFDATA2LSB != elf_header.e_ident[EI_DATA]) {
  212. dbgputstr("File is not a little endian ELF file.\n");
  213. return false;
  214. }
  215. if (EV_CURRENT != elf_header.e_ident[EI_VERSION]) {
  216. dbgprintf("File has unrecognized ELF version (%d), expected (%d)!\n", elf_header.e_ident[EI_VERSION], EV_CURRENT);
  217. return false;
  218. }
  219. if (ELFOSABI_SYSV != elf_header.e_ident[EI_OSABI]) {
  220. dbgprintf("File has unknown OS ABI (%d), expected SYSV(0)!\n", elf_header.e_ident[EI_OSABI]);
  221. return false;
  222. }
  223. if (0 != elf_header.e_ident[EI_ABIVERSION]) {
  224. dbgprintf("File has unknown SYSV ABI version (%d)!\n", elf_header.e_ident[EI_ABIVERSION]);
  225. return false;
  226. }
  227. if (EM_386 != elf_header.e_machine) {
  228. dbgprintf("File has unknown machine (%d), expected i386 (3)!\n", elf_header.e_machine);
  229. return false;
  230. }
  231. if (ET_EXEC != elf_header.e_type && ET_DYN != elf_header.e_type && ET_REL != elf_header.e_type) {
  232. dbgprintf("File has unloadable ELF type (%d), expected REL (1), EXEC (2) or DYN (3)!\n", elf_header.e_type);
  233. return false;
  234. }
  235. if (EV_CURRENT != elf_header.e_version) {
  236. dbgprintf("File has unrecognized ELF version (%d), expected (%d)!\n", elf_header.e_version, EV_CURRENT);
  237. return false;
  238. }
  239. if (sizeof(Elf32_Ehdr) != elf_header.e_ehsize) {
  240. dbgprintf("File has incorrect ELF header size..? (%d), expected (%d)!\n", elf_header.e_ehsize, sizeof(Elf32_Ehdr));
  241. return false;
  242. }
  243. if (elf_header.e_phoff > file_size || elf_header.e_shoff > file_size) {
  244. dbgprintf("SHENANIGANS! program header offset (%d) or section header offset (%d) are past the end of the file!\n",
  245. elf_header.e_phoff, elf_header.e_shoff);
  246. return false;
  247. }
  248. if (elf_header.e_phnum != 0 && elf_header.e_phoff != elf_header.e_ehsize) {
  249. dbgprintf("File does not have program headers directly after the ELF header? program header offset (%d), expected (%d).\n",
  250. elf_header.e_phoff, elf_header.e_ehsize);
  251. return false;
  252. }
  253. if (0 != elf_header.e_flags) {
  254. dbgprintf("File has incorrect ELF header flags...? (%d), expected (%d).\n", elf_header.e_flags, 0);
  255. return false;
  256. }
  257. if (0 != elf_header.e_phnum && sizeof(Elf32_Phdr) != elf_header.e_phentsize) {
  258. dbgprintf("File has incorrect program header size..? (%d), expected (%d).\n", elf_header.e_phentsize, sizeof(Elf32_Phdr));
  259. return false;
  260. }
  261. if (sizeof(Elf32_Shdr) != elf_header.e_shentsize) {
  262. dbgprintf("File has incorrect section header size..? (%d), expected (%d).\n", elf_header.e_shentsize, sizeof(Elf32_Shdr));
  263. return false;
  264. }
  265. size_t end_of_last_program_header = elf_header.e_phoff + (elf_header.e_phnum * elf_header.e_phentsize);
  266. if (end_of_last_program_header > file_size) {
  267. dbgprintf("SHENANIGANS! End of last program header (%d) is past the end of the file!\n", end_of_last_program_header);
  268. return false;
  269. }
  270. size_t end_of_last_section_header = elf_header.e_shoff + (elf_header.e_shnum * elf_header.e_shentsize);
  271. if (end_of_last_section_header > file_size) {
  272. dbgprintf("SHENANIGANS! End of last section header (%d) is past the end of the file!\n", end_of_last_section_header);
  273. return false;
  274. }
  275. if (elf_header.e_shstrndx >= elf_header.e_shnum) {
  276. dbgprintf("SHENANIGANS! Section header string table index (%d) is not a valid index given we have %d section headers!\n", elf_header.e_shstrndx, elf_header.e_shnum);
  277. return false;
  278. }
  279. return true;
  280. }
  281. bool ELFImage::validate_program_headers(const Elf32_Ehdr& elf_header, size_t file_size, u8* buffer, size_t buffer_size, String& interpreter_path)
  282. {
  283. // Can we actually parse all the program headers in the given buffer?
  284. size_t end_of_last_program_header = elf_header.e_phoff + (elf_header.e_phnum * elf_header.e_phentsize);
  285. if (end_of_last_program_header > buffer_size) {
  286. dbgprintf("Unable to parse program headers from buffer, buffer too small! Buffer size: %zu, End of program headers %zu\n",
  287. buffer_size, end_of_last_program_header);
  288. return false;
  289. }
  290. if (file_size < buffer_size) {
  291. dbgputstr("We somehow read more from a file than was in the file in the first place!\n");
  292. ASSERT_NOT_REACHED();
  293. }
  294. size_t num_program_headers = elf_header.e_phnum;
  295. auto program_header_begin = (const Elf32_Phdr*)&(buffer[elf_header.e_phoff]);
  296. for (size_t header_index = 0; header_index < num_program_headers; ++header_index) {
  297. auto& program_header = program_header_begin[header_index];
  298. switch (program_header.p_type) {
  299. case PT_INTERP:
  300. if (ET_DYN != elf_header.e_type) {
  301. dbgprintf("Found PT_INTERP header (%d) in non-DYN ELF object! What? We can't handle this!\n", header_index);
  302. return false;
  303. }
  304. // We checked above that file_size was >= buffer size. We only care about buffer size anyway, we're trying to read this!
  305. if (program_header.p_offset + program_header.p_filesz > buffer_size) {
  306. dbgprintf("Found PT_INTERP header (%d), but the .interp section was not within our buffer :( Your program will not be loaded today.\n", header_index);
  307. return false;
  308. }
  309. interpreter_path = String((const char*)&buffer[program_header.p_offset], program_header.p_filesz - 1);
  310. break;
  311. case PT_LOAD:
  312. case PT_DYNAMIC:
  313. case PT_NOTE:
  314. case PT_PHDR:
  315. case PT_TLS:
  316. if (program_header.p_offset + program_header.p_filesz > file_size) {
  317. dbgprintf("SHENANIGANS! Program header %d segment leaks beyond end of file!\n", header_index);
  318. return false;
  319. }
  320. if ((program_header.p_flags & PF_X) && (program_header.p_flags & PF_W)) {
  321. dbgprintf("SHENANIGANS! Program header %d segment is marked write and execute\n", header_index);
  322. return false;
  323. }
  324. break;
  325. default:
  326. // Not handling other program header types in other code so... let's not surprise them
  327. dbgprintf("Found program header (%d) of unrecognized type %d!\n", header_index, program_header.p_type);
  328. ASSERT_NOT_REACHED();
  329. break;
  330. }
  331. }
  332. return true;
  333. }