diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2022-01-13 18:50:17 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-13 22:40:25 +0100 |
commit | 3e959618c3b3989228d6ba849f5c9e85a6b6a604 (patch) | |
tree | ed89c28a1837f3543f49c171696785b8cb2e7aaa /Userland/Libraries | |
parent | fb3e46e930aaf03fa1caec0cdf18ba48b12e1b0e (diff) | |
download | serenity-3e959618c3b3989228d6ba849f5c9e85a6b6a604.zip |
LibELF: Use StringBuilders instead of Strings for the interpreter path
This is required for the Kernel's usage of LibELF, since Strings do not
expose allocation failure.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibELF/DynamicLoader.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibELF/Image.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibELF/Validation.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibELF/Validation.h | 3 |
4 files changed, 17 insertions, 6 deletions
diff --git a/Userland/Libraries/LibELF/DynamicLoader.cpp b/Userland/Libraries/LibELF/DynamicLoader.cpp index d0332c2f62..d2a986f483 100644 --- a/Userland/Libraries/LibELF/DynamicLoader.cpp +++ b/Userland/Libraries/LibELF/DynamicLoader.cpp @@ -122,8 +122,11 @@ bool DynamicLoader::validate() auto* elf_header = (ElfW(Ehdr)*)m_file_data; if (!validate_elf_header(*elf_header, m_file_size)) return false; - if (!validate_program_headers(*elf_header, m_file_size, (u8*)m_file_data, m_file_size, &m_program_interpreter)) + StringBuilder interpreter_path_builder; + auto result_or_error = validate_program_headers(*elf_header, m_file_size, (u8*)m_file_data, m_file_size, &interpreter_path_builder); + if (result_or_error.is_error() || !result_or_error.value()) return false; + m_program_interpreter = interpreter_path_builder.string_view(); return true; } diff --git a/Userland/Libraries/LibELF/Image.cpp b/Userland/Libraries/LibELF/Image.cpp index d610a08291..88e66dee5d 100644 --- a/Userland/Libraries/LibELF/Image.cpp +++ b/Userland/Libraries/LibELF/Image.cpp @@ -126,7 +126,14 @@ bool Image::parse() return false; } - if (!validate_program_headers(header(), m_size, m_buffer, m_size, nullptr, m_verbose_logging)) { + auto result_or_error = validate_program_headers(header(), m_size, m_buffer, m_size, nullptr, m_verbose_logging); + if (result_or_error.is_error()) { + if (m_verbose_logging) + dbgln("ELF::Image::parse(): Failed validating ELF Program Headers"); + m_valid = false; + return false; + } + if (!result_or_error.value()) { if (m_verbose_logging) dbgln("ELF::Image::parse(): ELF Program Headers not valid"); m_valid = false; diff --git a/Userland/Libraries/LibELF/Validation.cpp b/Userland/Libraries/LibELF/Validation.cpp index 525d242286..d363dfef11 100644 --- a/Userland/Libraries/LibELF/Validation.cpp +++ b/Userland/Libraries/LibELF/Validation.cpp @@ -192,7 +192,7 @@ bool validate_elf_header(const ElfW(Ehdr) & elf_header, size_t file_size, bool v return true; } -bool validate_program_headers(const ElfW(Ehdr) & elf_header, size_t file_size, const u8* buffer, size_t buffer_size, String* interpreter_path, bool verbose) +ErrorOr<bool> validate_program_headers(const ElfW(Ehdr) & elf_header, size_t file_size, const u8* buffer, size_t buffer_size, StringBuilder* interpreter_path_builder, bool verbose) { Checked<size_t> total_size_of_program_headers = elf_header.e_phnum; total_size_of_program_headers *= elf_header.e_phentsize; @@ -268,8 +268,8 @@ bool validate_program_headers(const ElfW(Ehdr) & elf_header, size_t file_size, c dbgln("Found PT_INTERP header ({}), but p_filesz is invalid ({})", header_index, program_header.p_filesz); return false; } - if (interpreter_path) - *interpreter_path = String((const char*)&buffer[program_header.p_offset], program_header.p_filesz - 1); + if (interpreter_path_builder) + TRY(interpreter_path_builder->try_append({ &buffer[program_header.p_offset], program_header.p_filesz - 1 })); break; case PT_LOAD: case PT_DYNAMIC: diff --git a/Userland/Libraries/LibELF/Validation.h b/Userland/Libraries/LibELF/Validation.h index 7b097abf8b..41b6b4957e 100644 --- a/Userland/Libraries/LibELF/Validation.h +++ b/Userland/Libraries/LibELF/Validation.h @@ -8,10 +8,11 @@ #include <AK/String.h> #include <LibC/elf.h> +#include <limits.h> namespace ELF { bool validate_elf_header(const ElfW(Ehdr) & elf_header, size_t file_size, bool verbose = true); -bool validate_program_headers(const ElfW(Ehdr) & elf_header, size_t file_size, const u8* buffer, size_t buffer_size, String* interpreter_path, bool verbose = true); +ErrorOr<bool> validate_program_headers(const ElfW(Ehdr) & elf_header, size_t file_size, const u8* buffer, size_t buffer_size, StringBuilder* interpreter_path_builder, bool verbose = true); } // end namespace ELF |