Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/relinker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ if(BUILD_TESTING)
add_test(NAME guest_deferred_dependencies COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_deferred_dependencies.py
$<TARGET_FILE:relinker> $<TARGET_FILE:guest_module_args_host>)
add_test(NAME guest_preinit COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_preinit.py
add_test(NAME guest_preinit_once COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_preinit_once.py
$<TARGET_FILE:relinker> $<TARGET_FILE:guest_module_args_host>)
add_test(NAME guest_absolute_symbols COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_absolute_symbols.py $<TARGET_FILE:relinker>)
Expand Down
1 change: 0 additions & 1 deletion core/relinker/domain/include/domain/GuestRuntime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ struct GuestRuntime {
std::vector<GuestImport> Imports;
std::vector<std::uint32_t> InitArrayRvas;
std::vector<std::uint32_t> FiniArrayRvas;
std::vector<std::uint32_t> PreInitArrayRvas;
std::uint32_t InitRva = 0;
std::uint32_t FiniRva = 0;
bool UsePlatformTlsResolver = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ struct MacOsImageInput {
std::uint32_t FiniRva = 0;
std::vector<std::uint32_t> InitArrayRvas;
std::vector<std::uint32_t> FiniArrayRvas;
std::vector<std::uint32_t> PreInitArrayRvas;
std::vector<std::uint32_t> TlsModuleSlots;
std::vector<MacOsTlsImport> TlsImports;
std::vector<MacOsTlsExport> TlsExports;
Expand Down
27 changes: 1 addition & 26 deletions core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,7 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteLinux(const Relinker::GuestIma
Io::AppendU32(bytes, static_cast<std::uint32_t>(distance));
return start;
};
const auto preinit = [&](std::uint64_t target) {
if (image.PreInitArray.empty()) return std::uint64_t{};
if (target == 0) throw Domain::RelinkerException("Guest preinit array has no initializer");
const auto start = address();
std::vector<std::uint8_t> stub{0x50};
for (const auto slot : image.PreInitArray) {
const auto load = static_cast<std::int64_t>(stub.size());
const auto distance = static_cast<std::int64_t>(slot) - static_cast<std::int64_t>(start + static_cast<std::uint64_t>(load) + 7);
if (distance < std::numeric_limits<std::int32_t>::min() || distance > std::numeric_limits<std::int32_t>::max())
throw Domain::RelinkerException("Guest preinit exceeds relative branch range");
stub.insert(stub.end(), {0x48, 0x8b, 0x05});
Io::AppendU32(stub, static_cast<std::uint32_t>(static_cast<std::int32_t>(distance)));
stub.insert(stub.end(), {0x48, 0x85, 0xc0, 0x74, 0x08, 0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0xff, 0xd0});
}
stub.push_back(0x58);
const auto distance = static_cast<std::int64_t>(target) - static_cast<std::int64_t>(start + stub.size() + 5);
if (distance < std::numeric_limits<std::int32_t>::min() || distance > std::numeric_limits<std::int32_t>::max())
throw Domain::RelinkerException("Guest preinit exceeds relative branch range");
stub.push_back(0xe9);
Io::AppendU32(stub, static_cast<std::uint32_t>(static_cast<std::int32_t>(distance)));
bytes.insert(bytes.end(), stub.begin(), stub.end());
return start;
};
const auto init = lifecycle(image.Init);
const auto preinitInit = preinit(init);
const auto fini = callThrough(image.Fini);
Io::AlignBuffer(bytes, 8);
const auto dynamicOffset = bytes.size();
Expand All @@ -201,8 +177,7 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteLinux(const Relinker::GuestIma
}
if (!image.InitArray.empty()) { tag(25, image.InitArray.front()); tag(27, image.InitArray.size() * 8); }
if (!image.FiniArray.empty()) { tag(26, image.FiniArray.front()); tag(28, image.FiniArray.size() * 8); }
const auto entry = preinitInit != 0 ? preinitInit : init;
if (entry != 0) tag(12, entry);
if (init != 0) tag(12, init);
if (fini != 0) tag(13, fini);
tag(30, DF_BIND_NOW | (image.Symbolic ? DF_SYMBOLIC : 0));
tag(0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteMacOs(const Relinker::GuestIma
input.RunPaths = MachORunPaths(runPath, "@loader_path");
for (const auto slot : guest.InitArray) input.InitArrayRvas.push_back(image.GetRva(slot, 8));
for (const auto slot : guest.FiniArray) input.FiniArrayRvas.push_back(image.GetRva(slot, 8));
for (const auto slot : guest.PreInitArray) input.PreInitArrayRvas.push_back(image.GetRva(slot, 8));
input.InitRva = guest.Init == 0 ? 0 : image.GetRva(guest.Init);
input.FiniRva = guest.Fini == 0 ? 0 : image.GetRva(guest.Fini);
input.Trampolines = &guest.Trampolines;
Expand Down
3 changes: 1 addition & 2 deletions core/relinker/elfpatcher/src/macos/MacOsImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ std::vector<std::uint8_t> WriteMacOsImage(MacOsImageInput& input) {
input.Binds.push_back({"___cxa_atexit", metaRva + static_cast<std::uint32_t>(MetaCxaAtexitSlot)});
}

const bool hasInit = hasTls || !input.TlsImports.empty() || input.InitRva != 0 || !input.InitArrayRvas.empty() || !input.PreInitArrayRvas.empty() || hasFini;
const bool hasInit = hasTls || !input.TlsImports.empty() || input.InitRva != 0 || !input.InitArrayRvas.empty() || hasFini;
std::vector<Section> dataSections{{"__meta", 0, metaSize, 0, 3}};
if (hasInit) {
const auto initStub = code.GetRva();
Expand All @@ -580,7 +580,6 @@ std::vector<std::uint8_t> WriteMacOsImage(MacOsImageInput& input) {
}
code.Rip({0x48, 0x89, 0x05}, import.Rva);
}
for (const auto slot : input.PreInitArrayRvas) callSlot(slot);
if (input.InitRva != 0) {
code.Rip({0xff, 0x15}, metaRva + static_cast<std::uint32_t>(moduleArgsSlot));
code.Emit({0x48, 0x8b, 0x38, 0x48, 0x8b, 0x70, 0x08, 0x31, 0xd2});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <elfpatcher/general/GuestModuleWriter.hpp>
#include <elfpatcher/windows/WindowsImportBuilder.hpp>
#include <elfpatcher/windows/WindowsLoadImage.hpp>
#include <elfpatcher/windows/WindowsStubEmitter.hpp>
#include <elfpatcher/windows/WindowsTlsBuilder.hpp>
#include <elfpatcher/windows/WindowsPeWriter.hpp>
#include <elfpatcher/windows/WindowsRelocationBuilder.hpp>
Expand Down Expand Up @@ -154,37 +153,7 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteWindows(const Relinker::GuestI
for (const auto slot : guest.InitArray) Io::AppendU32(lifecycle.Data, image.GetRva(slot, 8));
if (!exports.emplace(Relinker::GuestInitializeExport, nextRva).second) throw Domain::RelinkerException("Duplicate guest export: " + std::string(Relinker::GuestInitializeExport));
nextRva = AlignRva(nextRva + lifecycle.Data.size());
if (guest.PreInitArray.empty()) sections.push_back(std::move(lifecycle));
else {
if (guest.Init == 0) throw Domain::RelinkerException("Guest preinit array has no initializer");
const auto thunkRva = nextRva;
const auto codeSize = static_cast<std::uint32_t>(40 + 23 * guest.PreInitArray.size());
const auto slotsRva = static_cast<std::uint32_t>(thunkRva + codeSize + (8 - codeSize % 8) % 8);
WindowsStubEmitter code(thunkRva);
code.Emit({0x41, 0x54, 0x41, 0x55, 0x41, 0x56});
code.Emit({0x49, 0x89, 0xfc, 0x49, 0x89, 0xf5, 0x49, 0x89, 0xd6});
for (std::size_t index = 0; index < guest.PreInitArray.size(); ++index) {
code.Rip({0x48, 0x8b, 0x05}, slotsRva + static_cast<std::uint32_t>(index) * 8);
code.Emit({0x48, 0x8b, 0x00, 0x48, 0x85, 0xc0, 0x74, 0x08});
code.Emit({0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0xff, 0xd0});
}
code.Emit({0x4c, 0x89, 0xe7, 0x4c, 0x89, 0xee, 0x4c, 0x89, 0xf2});
code.Rip({0x48, 0x8b, 0x05}, slotsRva + static_cast<std::uint32_t>(guest.PreInitArray.size()) * 8);
code.Emit({0xff, 0xd0});
code.Emit({0x41, 0x5e, 0x41, 0x5d, 0x41, 0x5c, 0xc3});
auto thunk = code.TakeBytes();
while (thunk.size() % 8 != 0) thunk.push_back(0x90);
for (const auto slot : guest.PreInitArray) {
relocations.push_back(CheckedRva(thunkRva + thunk.size()));
Io::AppendU64(thunk, ImageBase + image.GetRva(slot, 8));
}
relocations.push_back(CheckedRva(thunkRva + thunk.size()));
Io::AppendU64(thunk, ImageBase + image.GetRva(guest.Init, 1));
Io::WriteU32(lifecycle.Data, 0, thunkRva);
nextRva = AlignRva(nextRva + thunk.size());
sections.push_back(std::move(lifecycle));
sections.push_back({".ginitc", thunkRva, SectionRead | SectionExecute | 0x60u, std::move(thunk)});
}
sections.push_back(std::move(lifecycle));
}
if (exports.size() > 65535) throw Domain::RelinkerException("Too many guest PE exports");
PeSection exportSection{".edata", nextRva, SectionRead | 0x40u, std::vector<std::uint8_t>(40)};
Expand Down Expand Up @@ -231,7 +200,6 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteWindows(const Relinker::GuestI
const auto entry = nextRva;
sections.push_back({".dllmain", entry, SectionRead | SectionExecute | 0x20u, {0xb8, 1, 0, 0, 0, 0xc3}});
for (const auto slot : guest.InitArray) runtime.InitArrayRvas.push_back(image.GetRva(slot, 8));
for (const auto slot : guest.PreInitArray) runtime.PreInitArrayRvas.push_back(image.GetRva(slot, 8));
for (const auto slot : guest.FiniArray) runtime.FiniArrayRvas.push_back(image.GetRva(slot, 8));
runtime.InitRva = guest.Init == 0 ? 0 : image.GetRva(guest.Init);
runtime.UnmatchedImportModules.assign(guest.Dynamic.UnmatchedImportModules.begin(), guest.Dynamic.UnmatchedImportModules.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ void WindowsGuestStartup::Initialize(WindowsStubEmitter& code, const std::vector
for (std::size_t index = 0; index < modules.size(); ++index) {
if (modules[index].DeferInitialization) continue;
const auto handle = CheckedRva(handles + index * 8);
for (const auto slot : modules[index].PreInitArrayRvas) callLifecycle(code, handle, slot, true);
callLifecycle(code, handle, modules[index].InitRva);
for (const auto slot : modules[index].InitArrayRvas) callLifecycle(code, handle, slot, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct GuestImage {
std::vector<GuestSymbol> Symbols;
std::vector<std::string> Dependencies;
Domain::SysVDynamicSection Dynamic;
std::vector<std::uint64_t> PreInitArray;
std::vector<std::uint64_t> InitArray;
std::vector<std::uint64_t> FiniArray;
std::uint64_t Init = 0;
Expand Down
1 change: 0 additions & 1 deletion core/relinker/relinker/src/guest/GuestImageReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ GuestImage GuestImageReader::Read(const std::filesystem::path& path, std::vector
entries.push_back(address + offset);
}
};
readLifecycleArray(32, 33, image.PreInitArray);
readLifecycleArray(25, 27, image.InitArray);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep validating the preinit array before discarding it

When a guest has a non-empty DT_PREINIT_ARRAY with a malformed size/address or a relocation targeting mapped but non-executable data, the generic relocation checks can accept it; this removed call was the only path through readLifecycleArray that enforced the array layout, mapping, and executable function targets. Because initialization now relies on the guest's DT_INIT routine to invoke these entries, conversion succeeds but can fault during initialization instead of rejecting the invalid module. Keep this validation even if the collected slot RVAs are no longer stored.

Useful? React with 👍 / 👎.

readLifecycleArray(26, 28, image.FiniArray);
if (tags.contains(33) && tags.at(33) != 0 && image.Init == 0) fail("Guest PREINIT_ARRAY has no module initializer");
Expand Down
Loading