From ce7fc8b07d2380fe4e92a6f149ff626b0d59f9a1 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Wed, 28 Aug 2024 20:26:22 +0200 Subject: [PATCH] Prepare module mapping --- src/emulator/memory_manager.hpp | 2 +- src/windows_emulator/syscalls.cpp | 51 +++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/emulator/memory_manager.hpp b/src/emulator/memory_manager.hpp index 700533de..d4ec5021 100644 --- a/src/emulator/memory_manager.hpp +++ b/src/emulator/memory_manager.hpp @@ -247,7 +247,7 @@ public: return start_address; } - start_address = region.first + region.second.length; + start_address = page_align_up(region.first + region.second.length); } if (start_address + size <= 0x00007ffffffeffff) diff --git a/src/windows_emulator/syscalls.cpp b/src/windows_emulator/syscalls.cpp index 3ee4f11a..6ef7a704 100644 --- a/src/windows_emulator/syscalls.cpp +++ b/src/windows_emulator/syscalls.cpp @@ -1,5 +1,6 @@ #include "std_include.hpp" #include "syscalls.hpp" +#include "module_mapper.hpp" struct syscall_context { @@ -286,6 +287,17 @@ namespace } } + if (handle & SECTION_BIT) + { + const auto event_index = static_cast(handle & ~SECTION_BIT); + const auto entry = c.proc.sections.find(event_index); + if (entry != c.proc.sections.end()) + { + c.proc.sections.erase(entry); + return STATUS_SUCCESS; + } + } + return STATUS_INVALID_HANDLE; } @@ -344,13 +356,19 @@ namespace section_handle.write(index | SECTION_BIT); + auto status = STATUS_SUCCESS; object_attributes.access([&](const OBJECT_ATTRIBUTES& attributes) { - auto section = read_unicode_string(c.emu, attributes.ObjectName); + if (reinterpret_cast(attributes.RootDirectory) != KNOWN_DLLS_DIRECTORY) + { + status = STATUS_NOT_SUPPORTED; + return; + } + auto section = L"C:\\WINDOWS\\System32\\" + read_unicode_string(c.emu, attributes.ObjectName); c.proc.sections.try_emplace(index, std::move(section)); }); - return STATUS_SUCCESS; + return status; } NTSTATUS handle_NtMapViewOfSection(const syscall_context& c, uint64_t section_handle, uint64_t process_handle, @@ -359,10 +377,37 @@ namespace const emulator_object view_size, SECTION_INHERIT inherit_disposition, ULONG allocation_type, ULONG win32_protect) { - const auto desired_base = base_address.read(); + if (process_handle != ~0ULL) + { + return STATUS_INVALID_HANDLE; + } + + if (!(section_handle & SECTION_BIT)) + { + return STATUS_INVALID_HANDLE; + } + + const auto section_index = static_cast(section_handle & ~SECTION_BIT); + const auto section_entry = c.proc.sections.find(section_index); + if (section_entry == c.proc.sections.end()) + { + return STATUS_INVALID_HANDLE; + } + + const auto& section_name = section_entry->second; + const auto binary = map_file(c.emu, section_name); + + if (view_size.value()) + { + view_size.write(binary.size_of_image); + } + + base_address.write(binary.image_base); + return STATUS_SUCCESS; } + NTSTATUS handle_NtCreateIoCompletion(const syscall_context& c, const emulator_object event_handle, const ACCESS_MASK desired_access, const uint64_t object_attributes, uint32_t /*number_of_concurrent_threads*/)