diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 875a463f4..d93145995 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -308,5 +308,10 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/GuestIntelTrampolinesTests.cpp ) add_test(NAME guest_intel_trampolines_execution COMMAND guest_intel_trampolines_tests $) + + add_test_executable(dynamic_table_bounds_tests + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/DynamicTableBoundsTests.cpp + ) + add_test(NAME dynamic_table_bounds COMMAND dynamic_table_bounds_tests $) endif() endif() diff --git a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp index 63d311a13..2d0134bc5 100644 --- a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp +++ b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp @@ -216,7 +216,7 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf } const FileByteOffset symOff = dynSymTabOffset + static_cast(symIdx) * symEntSize; - if (symOff + 4 > raw.size()) + if (symOff < dynSymTabOffset || symOff > raw.size() || raw.size() - symOff < 4) throw RelinkerException("Symbol table entry out of bounds", symOff); std::uint32_t nameOff = 0; diff --git a/core/relinker/relinker/tests/DynamicTableBoundsTests.cpp b/core/relinker/relinker/tests/DynamicTableBoundsTests.cpp new file mode 100644 index 000000000..c91f3640e --- /dev/null +++ b/core/relinker/relinker/tests/DynamicTableBoundsTests.cpp @@ -0,0 +1,63 @@ +#include "ElfFixture.hpp" +#include "RelinkerProcess.hpp" +#include +#include +#include +#include + +namespace { + +using namespace RelinkerTests; + +void require(const bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +const Bytes kCode = {0xC3}; + +constexpr std::size_t kTagStride = 16; +constexpr std::size_t kTagBase = kCodeOffset + kDynamicVaddr; +constexpr std::size_t kSymTabTag = 2; +constexpr std::size_t kRelaSzTag = 5; +constexpr std::int64_t kDtOsSymtab = 0x61000039; +constexpr std::int64_t kDtRelaSz = 8; +constexpr std::uint64_t kBogusOffset = 0xFFFFFFFFFFFFFFFEull; +constexpr std::size_t kRelaEntry = kCodeOffset + 0x700; + +void WriteTag(Bytes& image, const std::size_t index, const std::int64_t tag, const std::uint64_t value) { + Write(image, kTagBase + index * kTagStride, tag); + Write(image, kTagBase + index * kTagStride + 8, value); +} + +void WriteRelaEntry(Bytes& image, const std::uint64_t info) { + Write(image, kRelaEntry, 0); + Write(image, kRelaEntry + 8, info); + Write(image, kRelaEntry + 16, 0); +} + +} + +int main(const int argc, char** argv) { + try { + require(argc == 2, "usage: dynamic_table_bounds_tests "); + const std::string binary = argv[1]; + const TempDirectory directory; + const auto input = directory.Path() / "input.elf"; + const auto output = directory.Path() / "output.elf"; + + Bytes image = MakeExecutable(kCode); + WriteRelaEntry(image, (1ull << 32) | 6ull); + WriteTag(image, kRelaSzTag, kDtRelaSz, 24); + WriteTag(image, kSymTabTag, kDtOsSymtab, kBogusOffset); + WriteFile(input, image); + + const auto run = RunRelinker(binary, {"--skip-sce-module", "--to-intel", input.string(), output.string()}, directory.Path() / "relinker.log"); + require(run.ExitCode != 0, "Relinker read a symbol table outside the file and exited 0:\n" + run.Output); + require(run.Output.find("Symbol table entry out of bounds") != std::string::npos, + "Relinker did not report the symbol table entry as out of bounds:\n" + run.Output); + return 0; + } catch (const std::exception& error) { + std::cerr << "dynamic_table_bounds_tests: " << error.what() << '\n'; + return 1; + } +}