From caf8aa98495735c92d339bba79ae4853cbfe67a2 Mon Sep 17 00:00:00 2001 From: Ilhan Raja Date: Mon, 16 Dec 2024 23:13:17 -0800 Subject: [PATCH] namespace refactor fixing --- arm64/breakpoint_arm64.s | 48 +++++++++++++++++++++++++++++++++++++- darwinkit/basic_block.h | 2 ++ darwinkit/disassembler.cc | 2 +- darwinkit/hook.cc | 5 +++- darwinkit/hook.h | 3 +++ darwinkit/patcher.cc | 7 ++++-- kernel/device_tree.cc | 8 ++++--- kernel/kernel.cc | 2 +- kernel/kernel_patcher.cc | 7 ++++-- kernel/kext.cc | 4 +++- kernel/plugin.cc | 12 +++++++--- user/loader.cc | 4 +++- user/macho_userspace.cc | 4 +++- user/task.cc | 7 ++++-- user/user_patcher.cc | 12 ++++++---- x86_64/breakpoint_x86_64.s | 46 +++++++++++++++++++++++++++++++++++- 16 files changed, 149 insertions(+), 24 deletions(-) diff --git a/arm64/breakpoint_arm64.s b/arm64/breakpoint_arm64.s index 8c568507..afbfffe1 100644 --- a/arm64/breakpoint_arm64.s +++ b/arm64/breakpoint_arm64.s @@ -68,4 +68,50 @@ _pop_registers_arm64: ldp x0, x1, [sp, 0x100] add sp, sp, 0x110 _pop_registers_arm64_end: - nop \ No newline at end of file + nop +_start_coverage: +.rept 10000 + sub sp, sp, 0x110 + stp x0, x1, [sp, 0x100] + stp x2, x3, [sp, 0xF0] + stp x4, x5, [sp, 0xE0] + stp x6, x7, [sp, 0xD0] + stp x8, x9, [sp, 0xC0] + stp x10, x11, [sp, 0xB0] + stp x12, x13, [sp, 0xA0] + stp x14, x15, [sp, 0x90] + stp x16, x17, [sp, 0x80] + stp x18, x19, [sp, 0x70] + stp x20, x21, [sp, 0x60] + stp x22, x23, [sp, 0x50] + stp x24, x25, [sp, 0x40] + stp x26, x27, [sp, 0x30] + stp x28, x29, [sp, 0x20] + stp x30, x31, [sp, 0x10] + nop + nop + nop + cmp x0, 1 + b.ne 0x4 + nop + brk #0 + nop + ldp x30, x31, [sp, 0x10] + ldp x28, x29, [sp, 0x20] + ldp x26, x27, [sp, 0x30] + ldp x24, x25, [sp, 0x40] + ldp x22, x23, [sp, 0x50] + ldp x20, x21, [sp, 0x60] + ldp x18, x19, [sp, 0x70] + ldp x16, x17, [sp, 0x80] + ldp x14, x15, [sp, 0x90] + ldp x12, x13, [sp, 0xA0] + ldp x10, x11, [sp, 0xB0] + ldp x8, x9, [sp, 0xC0] + ldp x6, x7, [sp, 0xD0] + ldp x4, x5, [sp, 0xE0] + ldp x2, x3, [sp, 0xF0] + ldp x0, x1, [sp, 0x100] + add sp, sp, 0x110 + nop +.endr \ No newline at end of file diff --git a/darwinkit/basic_block.h b/darwinkit/basic_block.h index f9ac72d2..5e19384b 100644 --- a/darwinkit/basic_block.h +++ b/darwinkit/basic_block.h @@ -32,6 +32,8 @@ class BasicBlock { explicit BasicBlock() {} + ~BasicBlock() = default; + Bin* GetBinary() { return bin; } InstructionList& GetInstructions() { return instructions; } diff --git a/darwinkit/disassembler.cc b/darwinkit/disassembler.cc index 0e6e5114..b9bab566 100644 --- a/darwinkit/disassembler.cc +++ b/darwinkit/disassembler.cc @@ -193,4 +193,4 @@ xnu::mach::VmAddress Disassembler::DisassembleSignature(xnu::mach::VmAddress add } return 0; -} \ No newline at end of file +} diff --git a/darwinkit/hook.cc b/darwinkit/hook.cc index 79d2abfb..d9c7a1f8 100644 --- a/darwinkit/hook.cc +++ b/darwinkit/hook.cc @@ -26,9 +26,10 @@ #include "arch.h" -using namespace darwin; using namespace xnu; +namespace darwin { + static constexpr UInt64 kBaseKernelAddress = 0xfffffe0000000000; Hook::Hook(Patcher* patcher, enum HookType hooktype) @@ -413,3 +414,5 @@ void Hook::AddBreakpoint(xnu::mach::VmAddress breakpoint_hook, enum HookType hoo void Hook::RemoveBreakpoint() { } + +} \ No newline at end of file diff --git a/darwinkit/hook.h b/darwinkit/hook.h index 19d5c5d2..c77d5543 100644 --- a/darwinkit/hook.h +++ b/darwinkit/hook.h @@ -81,6 +81,9 @@ template using HookArray = std::vector; namespace darwin { + +void InstrumentTrampoline(); + class Hook { public: explicit Hook(darwin::Patcher* patcher, enum HookType hooktype); diff --git a/darwinkit/patcher.cc b/darwinkit/patcher.cc index 7238ae5b..8a929185 100644 --- a/darwinkit/patcher.cc +++ b/darwinkit/patcher.cc @@ -18,12 +18,13 @@ #include "hook.h" -using namespace darwin; +namespace darwin { void Patcher::FindAndReplace(void* data, Size data_size, const void* find, Size find_size, const void* replace, Size replace_size) {} -void Patcher::OnKextLoad(void* kext, kmod_info_t* kmod) {} +void Patcher::OnKextLoad(void* kext, kmod_info_t* kmod) { +} void Patcher::RouteFunction(Hook* hook) { hooks.push_back(hook); @@ -111,4 +112,6 @@ void Patcher::RemoveHook(Hook* hook) { hooks.erase(std::remove(hooks.begin(), hooks.end(), hook), hooks.end()); delete hook; +} + } \ No newline at end of file diff --git a/kernel/device_tree.cc b/kernel/device_tree.cc index 5cce1d3c..6eae7693 100644 --- a/kernel/device_tree.cc +++ b/kernel/device_tree.cc @@ -24,8 +24,6 @@ #include "log.h" -using namespace xnu; - Bool is_ascii(char* c, Size len) { UInt32 zeros = 0; @@ -39,6 +37,8 @@ Bool is_ascii(char* c, Size len) { return zeros < 3 ? true : false; } +namespace xnu { + Bool DeviceTree::IterateNodeProperties(void** data, void* data_end, UInt32* depth, DeviceTreeNode* node, dt_property_callback_t prop_cb, Bool* success) { @@ -375,7 +375,7 @@ T DeviceTree::Dump() { return reinterpret_cast(device_tree); } -PE_state_t* xnu::PlatformExpertState(xnu::Kernel* kernel) { +PE_state_t* PlatformExpertState(xnu::Kernel* kernel) { uintptr_t device_tree; UInt64 deviceTreeHead; @@ -411,4 +411,6 @@ PE_state_t* xnu::PlatformExpertState(xnu::Kernel* kernel) { return reinterpret_cast(PE_state); } +} + #endif \ No newline at end of file diff --git a/kernel/kernel.cc b/kernel/kernel.cc index 456ca471..86664a8c 100644 --- a/kernel/kernel.cc +++ b/kernel/kernel.cc @@ -1482,4 +1482,4 @@ xnu::mach::VmAddress Kernel::GetSymbolAddressByName(char* symbolname) { return symbolAddress; } -} \ No newline at end of file +} // namespace xnu \ No newline at end of file diff --git a/kernel/kernel_patcher.cc b/kernel/kernel_patcher.cc index c3d5bfc2..5126628c 100644 --- a/kernel/kernel_patcher.cc +++ b/kernel/kernel_patcher.cc @@ -45,7 +45,8 @@ using namespace arch::x86_64::patchfinder; #endif using namespace arch; -using namespace darwin; + +namespace darwin { static KernelPatcher* that = nullptr; @@ -884,4 +885,6 @@ void KernelPatcher::RemoveKextPatch(struct KextPatch* patch) { } kextPatches.push_back(patch); -} \ No newline at end of file +} + +} // namespace darwin \ No newline at end of file diff --git a/kernel/kext.cc b/kernel/kext.cc index 2702bc88..b12bd5a3 100644 --- a/kernel/kext.cc +++ b/kernel/kext.cc @@ -24,7 +24,7 @@ #include "macho.h" -using namespace xnu; +namespace xnu { Kext::Kext(Kernel* kernel, xnu::mach::VmAddress base, char* identifier) : kernel(kernel), address(base), identifier(identifier) { @@ -84,3 +84,5 @@ Kext* Kext::FindKextWithId(Kernel* kernel, UInt32 kext_id) { void Kext::OnKextLoad(void* kext, xnu::KmodInfo* kmod_info) { return; } + +} // namespace xnu diff --git a/kernel/plugin.cc b/kernel/plugin.cc index 6faa66db..cafa026f 100644 --- a/kernel/plugin.cc +++ b/kernel/plugin.cc @@ -16,18 +16,24 @@ #include "plugin.h" -using namespace darwin; +namespace darwin { Plugin::Plugin(IOService* service, char* product, Size version, UInt32 runmode, const char** disableArg, Size disableArgNum, const char** debugArg, Size debugArgNum, const char** betaArg, Size betaArgNum) : service(service), product(product), version(version), runmode(runmode), disableArg(disableArg), disableArgNum(disableArgNum), debugArg(debugArg), - debugArgNum(debugArgNum), betaArg(betaArg), betaArgNum(betaArgNum) {} + debugArgNum(debugArgNum), betaArg(betaArg), betaArgNum(betaArgNum) { + +} Plugin::Plugin(char* product, Size version, UInt32 runmode, const char** disableArg, Size disableArgNum, const char** debugArg, Size debugArgNum, const char** betaArg, Size betaArgNum) : service(nullptr), product(product), version(version), runmode(runmode), disableArg(disableArg), disableArgNum(disableArgNum), debugArg(debugArg), debugArgNum(debugArgNum), betaArg(betaArg), - betaArgNum(betaArgNum) {} \ No newline at end of file + betaArgNum(betaArgNum) { + +} + +} // namespace darwin \ No newline at end of file diff --git a/user/loader.cc b/user/loader.cc index 197f8572..7b054dba 100644 --- a/user/loader.cc +++ b/user/loader.cc @@ -24,7 +24,9 @@ namespace fuzzer { -void Module::Load() {} +void Module::Load() { + +} /* template diff --git a/user/macho_userspace.cc b/user/macho_userspace.cc index 6e86c50d..0bd1c7f5 100644 --- a/user/macho_userspace.cc +++ b/user/macho_userspace.cc @@ -23,7 +23,7 @@ #include "dyld.h" #include "task.h" -using namespace darwin; +namespace darwin { MachOUserspace::MachOUserspace(const char* path) : objc(nullptr), file_path(strdup(path)) { WithFilePath(path); @@ -843,3 +843,5 @@ void MachOUserspace::ParseHeader() { void MachOUserspace::ParseMachO() { ParseHeader(); } + +} // namespace darwin diff --git a/user/task.cc b/user/task.cc index 59b2c508..9ade3a7b 100644 --- a/user/task.cc +++ b/user/task.cc @@ -25,7 +25,7 @@ #include "dyld.h" #include "task.h" -using namespace xnu; +namespace xnu { static int EndsWith(const char* str, const char* suffix) { if (!str || !suffix) @@ -552,4 +552,7 @@ xnu::mach::VmAddress Task::GetImageLoadedAt(char* image_name, char** image_path) return image; } -void Task::PrintLoadedImages() {} +void Task::PrintLoadedImages() { +} + +} // namespace xnu diff --git a/user/user_patcher.cc b/user/user_patcher.cc index e5766819..d0b7ad0a 100644 --- a/user/user_patcher.cc +++ b/user/user_patcher.cc @@ -20,14 +20,18 @@ #include "payload.h" void UserPatcher::FindAndReplace(void* data, Size dataSize, const void* find, Size findSize, - const void* replace, Size replaceSize) {} + const void* replace, Size replaceSize) { +} -void UserPatcher::RouteFunction(Hook* hook) {} +void UserPatcher::RouteFunction(Hook* hook) { +} -void UserPatcher::OnKextLoad(void* kext, kmod_info_t* kmod) {} +void UserPatcher::OnKextLoad(void* kext, kmod_info_t* kmod) { +} void UserPatcher::OnExec(char* name, int pid, xnu::mach::Port port, xnu::mach::VmAddress task, - xnu::mach::VmAddress proc) {} + xnu::mach::VmAddress proc) { +} xnu::mach::VmAddress UserPatcher::InjectPayload(xnu::mach::VmAddress address, Payload* payload) { return 0; diff --git a/x86_64/breakpoint_x86_64.s b/x86_64/breakpoint_x86_64.s index 440c9f92..785a2b16 100644 --- a/x86_64/breakpoint_x86_64.s +++ b/x86_64/breakpoint_x86_64.s @@ -67,4 +67,48 @@ _pop_registers_x86_64: pop rbp pop rsp _pop_registers_x86_64_end: - nop \ No newline at end of file + nop +_start_coverage: +.rept 10000 + push rsp + push rbp + push rax + push rbx + push rcx + push rdx + push rdi + push rsi + push r8 + push r9 + push r10 + push r11 + push r12 + push r13 + push r14 + push r15 + nop + lea rdi, [rsp + 0x80] + nop + cmp rax, 1 + jne short $+4h + nop + int3 + nop + pop r15 + pop r14 + pop r13 + pop r12 + pop r11 + pop r10 + pop r9 + pop r8 + pop rsi + pop rdi + pop rdx + pop rcx + pop rbx + pop rax + pop rbp + pop rsp + nop +.endr \ No newline at end of file