Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Memory.h>
28#include <AK/StringBuilder.h>
29#include <AK/StringView.h>
30#include <LibELF/ELFImage.h>
31
32ELFImage::ELFImage(const u8* buffer, size_t size)
33 : m_buffer(buffer)
34 , m_size(size)
35{
36 m_valid = parse();
37}
38
39ELFImage::~ELFImage()
40{
41}
42
43static const char* object_file_type_to_string(Elf32_Half type)
44{
45 switch (type) {
46 case ET_NONE:
47 return "None";
48 case ET_REL:
49 return "Relocatable";
50 case ET_EXEC:
51 return "Executable";
52 case ET_DYN:
53 return "Shared object";
54 case ET_CORE:
55 return "Core";
56 default:
57 return "(?)";
58 }
59}
60
61StringView ELFImage::section_index_to_string(unsigned index) const
62{
63 if (index == SHN_UNDEF)
64 return "Undefined";
65 if (index >= SHN_LORESERVE)
66 return "Reserved";
67 return section(index).name();
68}
69
70unsigned ELFImage::symbol_count() const
71{
72 return section(m_symbol_table_section_index).entry_count();
73}
74
75void ELFImage::dump() const
76{
77 dbgprintf("ELFImage{%p} {\n", this);
78 dbgprintf(" is_valid: %u\n", is_valid());
79
80 if (!is_valid()) {
81 dbgprintf("}\n");
82 return;
83 }
84
85 dbgprintf(" type: %s\n", object_file_type_to_string(header().e_type));
86 dbgprintf(" machine: %u\n", header().e_machine);
87 dbgprintf(" entry: %x\n", header().e_entry);
88 dbgprintf(" shoff: %u\n", header().e_shoff);
89 dbgprintf(" shnum: %u\n", header().e_shnum);
90 dbgprintf(" phoff: %u\n", header().e_phoff);
91 dbgprintf(" phnum: %u\n", header().e_phnum);
92 dbgprintf(" shstrndx: %u\n", header().e_shstrndx);
93
94 for_each_program_header([&](const ProgramHeader& program_header) {
95 dbgprintf(" Program Header %d: {\n", program_header.index());
96 dbgprintf(" type: %x\n", program_header.type());
97 dbgprintf(" offset: %x\n", program_header.offset());
98 dbgprintf(" flags: %x\n", program_header.flags());
99 dbgprintf(" \n");
100 dbgprintf(" }\n");
101 });
102
103 for (unsigned i = 0; i < header().e_shnum; ++i) {
104 auto& section = this->section(i);
105 dbgprintf(" Section %u: {\n", i);
106 dbgprintf(" name: %s\n", section.name());
107 dbgprintf(" type: %x\n", section.type());
108 dbgprintf(" offset: %x\n", section.offset());
109 dbgprintf(" size: %u\n", section.size());
110 dbgprintf(" \n");
111 dbgprintf(" }\n");
112 }
113
114 dbgprintf("Symbol count: %u (table is %u)\n", symbol_count(), m_symbol_table_section_index);
115 for (unsigned i = 1; i < symbol_count(); ++i) {
116 auto& sym = symbol(i);
117 dbgprintf("Symbol @%u:\n", i);
118 dbgprintf(" Name: %s\n", sym.name());
119 dbgprintf(" In section: %s\n", section_index_to_string(sym.section_index()));
120 dbgprintf(" Value: %x\n", sym.value());
121 dbgprintf(" Size: %u\n", sym.size());
122 }
123
124 dbgprintf("}\n");
125}
126
127unsigned ELFImage::section_count() const
128{
129 return header().e_shnum;
130}
131
132unsigned ELFImage::program_header_count() const
133{
134 return header().e_phnum;
135}
136
137bool ELFImage::parse()
138{
139 if (!validate_elf_header(header(), m_size)) {
140 dbgputstr("ELFImage::parse(): ELF Header not valid\n");
141 return false;
142 }
143
144 // First locate the string tables.
145 for (unsigned i = 0; i < section_count(); ++i) {
146 auto& sh = section_header(i);
147 if (sh.sh_type == SHT_SYMTAB) {
148 ASSERT(!m_symbol_table_section_index || m_symbol_table_section_index == i);
149 m_symbol_table_section_index = i;
150 }
151 if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
152 if (section_header_table_string(sh.sh_name) == ELF_STRTAB)
153 m_string_table_section_index = i;
154 }
155 }
156
157 // Then create a name-to-index map.
158 for (unsigned i = 0; i < section_count(); ++i) {
159 auto& section = this->section(i);
160 m_sections.set(section.name(), move(i));
161 }
162
163 return true;
164}
165
166StringView ELFImage::table_string(unsigned table_index, unsigned offset) const
167{
168 auto& sh = section_header(table_index);
169 if (sh.sh_type != SHT_STRTAB)
170 return nullptr;
171 size_t computed_offset = sh.sh_offset + offset;
172 if (computed_offset >= m_size) {
173 dbgprintf("SHENANIGANS! ELFImage::table_string() computed offset outside image.\n");
174 return {};
175 }
176 size_t max_length = m_size - computed_offset;
177 size_t length = strnlen(raw_data(sh.sh_offset + offset), max_length);
178 return { raw_data(sh.sh_offset + offset), length };
179}
180
181StringView ELFImage::section_header_table_string(unsigned offset) const
182{
183 return table_string(header().e_shstrndx, offset);
184}
185
186StringView ELFImage::table_string(unsigned offset) const
187{
188 return table_string(m_string_table_section_index, offset);
189}
190
191const char* ELFImage::raw_data(unsigned offset) const
192{
193 return reinterpret_cast<const char*>(m_buffer) + offset;
194}
195
196const Elf32_Ehdr& ELFImage::header() const
197{
198 return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
199}
200
201const Elf32_Phdr& ELFImage::program_header_internal(unsigned index) const
202{
203 ASSERT(index < header().e_phnum);
204 return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
205}
206
207const Elf32_Shdr& ELFImage::section_header(unsigned index) const
208{
209 ASSERT(index < header().e_shnum);
210 return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
211}
212
213const ELFImage::Symbol ELFImage::symbol(unsigned index) const
214{
215 ASSERT(index < symbol_count());
216 auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
217 return Symbol(*this, index, raw_syms[index]);
218}
219
220const ELFImage::Section ELFImage::section(unsigned index) const
221{
222 ASSERT(index < section_count());
223 return Section(*this, index);
224}
225
226const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
227{
228 ASSERT(index < program_header_count());
229 return ProgramHeader(*this, index);
230}
231
232const ELFImage::Relocation ELFImage::RelocationSection::relocation(unsigned index) const
233{
234 ASSERT(index < relocation_count());
235 auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
236 return Relocation(m_image, rels[index]);
237}
238
239const ELFImage::RelocationSection ELFImage::Section::relocations() const
240{
241 StringBuilder builder;
242 builder.append(".rel");
243 builder.append(name());
244
245 auto relocation_section = m_image.lookup_section(builder.to_string());
246 if (relocation_section.type() != SHT_REL)
247 return static_cast<const RelocationSection>(m_image.section(0));
248
249#ifdef ELFIMAGE_DEBUG
250 dbgprintf("Found relocations for %s in %s\n", name(), relocation_section.name());
251#endif
252 return static_cast<const RelocationSection>(relocation_section);
253}
254
255const ELFImage::Section ELFImage::lookup_section(const String& name) const
256{
257 if (auto it = m_sections.find(name); it != m_sections.end())
258 return section((*it).value);
259 return section(0);
260}
261
262bool ELFImage::validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size)
263{
264 if (!IS_ELF(elf_header)) {
265 dbgputstr("File is not an ELF file.\n");
266 return false;
267 }
268
269 if (ELFCLASS32 != elf_header.e_ident[EI_CLASS]) {
270 dbgputstr("File is not a 32 bit ELF file.\n");
271 return false;
272 }
273
274 if (ELFDATA2LSB != elf_header.e_ident[EI_DATA]) {
275 dbgputstr("File is not a little endian ELF file.\n");
276 return false;
277 }
278
279 if (EV_CURRENT != elf_header.e_ident[EI_VERSION]) {
280 dbgprintf("File has unrecognized ELF version (%d), expected (%d)!\n", elf_header.e_ident[EI_VERSION], EV_CURRENT);
281 return false;
282 }
283
284 if (ELFOSABI_SYSV != elf_header.e_ident[EI_OSABI]) {
285 dbgprintf("File has unknown OS ABI (%d), expected SYSV(0)!\n", elf_header.e_ident[EI_OSABI]);
286 return false;
287 }
288
289 if (0 != elf_header.e_ident[EI_ABIVERSION]) {
290 dbgprintf("File has unknown SYSV ABI version (%d)!\n", elf_header.e_ident[EI_ABIVERSION]);
291 return false;
292 }
293
294 if (EM_386 != elf_header.e_machine) {
295 dbgprintf("File has unknown machine (%d), expected i386 (3)!\n", elf_header.e_machine);
296 return false;
297 }
298
299 if (ET_EXEC != elf_header.e_type && ET_DYN != elf_header.e_type && ET_REL != elf_header.e_type) {
300 dbgprintf("File has unloadable ELF type (%d), expected REL (1), EXEC (2) or DYN (3)!\n", elf_header.e_type);
301 return false;
302 }
303
304 if (EV_CURRENT != elf_header.e_version) {
305 dbgprintf("File has unrecognized ELF version (%d), expected (%d)!\n", elf_header.e_version, EV_CURRENT);
306 return false;
307 }
308
309 if (sizeof(Elf32_Ehdr) != elf_header.e_ehsize) {
310 dbgprintf("File has incorrect ELF header size..? (%d), expected (%d)!\n", elf_header.e_ehsize, sizeof(Elf32_Ehdr));
311 return false;
312 }
313
314 if (elf_header.e_phoff > file_size || elf_header.e_shoff > file_size) {
315 dbgprintf("SHENANIGANS! program header offset (%d) or section header offset (%d) are past the end of the file!\n",
316 elf_header.e_phoff, elf_header.e_shoff);
317 return false;
318 }
319
320 if (elf_header.e_phnum != 0 && elf_header.e_phoff != elf_header.e_ehsize) {
321 dbgprintf("File does not have program headers directly after the ELF header? program header offset (%d), expected (%d).\n",
322 elf_header.e_phoff, elf_header.e_ehsize);
323 return false;
324 }
325
326 if (0 != elf_header.e_flags) {
327 dbgprintf("File has incorrect ELF header flags...? (%d), expected (%d).\n", elf_header.e_flags, 0);
328 return false;
329 }
330
331 if (0 != elf_header.e_phnum && sizeof(Elf32_Phdr) != elf_header.e_phentsize) {
332 dbgprintf("File has incorrect program header size..? (%d), expected (%d).\n", elf_header.e_phentsize, sizeof(Elf32_Phdr));
333 return false;
334 }
335
336 if (sizeof(Elf32_Shdr) != elf_header.e_shentsize) {
337 dbgprintf("File has incorrect section header size..? (%d), expected (%d).\n", elf_header.e_shentsize, sizeof(Elf32_Shdr));
338 return false;
339 }
340
341 size_t end_of_last_program_header = elf_header.e_phoff + (elf_header.e_phnum * elf_header.e_phentsize);
342 if (end_of_last_program_header > file_size) {
343 dbgprintf("SHENANIGANS! End of last program header (%d) is past the end of the file!\n", end_of_last_program_header);
344 return false;
345 }
346
347 size_t end_of_last_section_header = elf_header.e_shoff + (elf_header.e_shnum * elf_header.e_shentsize);
348 if (end_of_last_section_header > file_size) {
349 dbgprintf("SHENANIGANS! End of last section header (%d) is past the end of the file!\n", end_of_last_section_header);
350 return false;
351 }
352
353 if (elf_header.e_shstrndx >= elf_header.e_shnum) {
354 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);
355 return false;
356 }
357
358 return true;
359}
360
361bool ELFImage::validate_program_headers(const Elf32_Ehdr& elf_header, size_t file_size, u8* buffer, size_t buffer_size, String& interpreter_path)
362{
363 // Can we actually parse all the program headers in the given buffer?
364 size_t end_of_last_program_header = elf_header.e_phoff + (elf_header.e_phnum * elf_header.e_phentsize);
365 if (end_of_last_program_header > buffer_size) {
366 dbgprintf("Unable to parse program headers from buffer, buffer too small! Buffer size: %zu, End of program headers %zu\n",
367 buffer_size, end_of_last_program_header);
368 return false;
369 }
370
371 if (file_size < buffer_size) {
372 dbgputstr("We somehow read more from a file than was in the file in the first place!\n");
373 ASSERT_NOT_REACHED();
374 }
375
376 size_t num_program_headers = elf_header.e_phnum;
377 auto program_header_begin = (const Elf32_Phdr*)&(buffer[elf_header.e_phoff]);
378
379 for (size_t header_index = 0; header_index < num_program_headers; ++header_index) {
380 auto& program_header = program_header_begin[header_index];
381 switch (program_header.p_type) {
382 case PT_INTERP:
383 if (ET_DYN != elf_header.e_type) {
384 dbgprintf("Found PT_INTERP header (%d) in non-DYN ELF object! What? We can't handle this!\n", header_index);
385 return false;
386 }
387 // We checked above that file_size was >= buffer size. We only care about buffer size anyway, we're trying to read this!
388 if (program_header.p_offset + program_header.p_filesz > buffer_size) {
389 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);
390 return false;
391 }
392 interpreter_path = String((const char*)&buffer[program_header.p_offset], program_header.p_filesz - 1);
393 break;
394 case PT_LOAD:
395 case PT_DYNAMIC:
396 case PT_NOTE:
397 case PT_PHDR:
398 case PT_TLS:
399 if (program_header.p_offset + program_header.p_filesz > file_size) {
400 dbgprintf("SHENANIGANS! Program header %d segment leaks beyond end of file!\n", header_index);
401 return false;
402 }
403 if ((program_header.p_flags & PF_X) && (program_header.p_flags & PF_W)) {
404 dbgprintf("SHENANIGANS! Program header %d segment is marked write and execute\n", header_index);
405 return false;
406 }
407 break;
408 default:
409 // Not handling other program header types in other code so... let's not surprise them
410 dbgprintf("Found program header (%d) of unrecognized type %d!\n", header_index, program_header.p_type);
411 ASSERT_NOT_REACHED();
412 break;
413 }
414 }
415 return true;
416}