2023-02-27 13:32:38 +01:00
|
|
|
#include "HookSystem.hpp"
|
2023-11-26 19:33:53 +01:00
|
|
|
#include "../debug/Log.hpp"
|
2023-12-27 11:43:04 +01:00
|
|
|
#include "../helpers/VarList.hpp"
|
2024-04-28 19:28:19 +02:00
|
|
|
#include "../managers/TokenManager.hpp"
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
#define register
|
|
|
|
#include <udis86.h>
|
|
|
|
#undef register
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstring>
|
2024-04-04 19:50:37 +02:00
|
|
|
#include <fstream>
|
2024-04-28 19:58:31 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
CFunctionHook::CFunctionHook(HANDLE owner, void* source, void* destination) {
|
|
|
|
m_pSource = source;
|
|
|
|
m_pDestination = destination;
|
|
|
|
m_pOwner = owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFunctionHook::~CFunctionHook() {
|
2023-06-16 18:45:11 +02:00
|
|
|
if (m_bActive)
|
2023-02-27 13:32:38 +01:00
|
|
|
unhook();
|
|
|
|
}
|
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
CFunctionHook::SInstructionProbe CFunctionHook::getInstructionLenAt(void* start) {
|
2023-02-27 13:32:38 +01:00
|
|
|
ud_t udis;
|
|
|
|
|
|
|
|
ud_init(&udis);
|
|
|
|
ud_set_mode(&udis, 64);
|
2023-12-27 11:43:04 +01:00
|
|
|
ud_set_syntax(&udis, UD_SYN_ATT);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
size_t curOffset = 1;
|
|
|
|
size_t insSize = 0;
|
|
|
|
while (true) {
|
|
|
|
ud_set_input_buffer(&udis, (uint8_t*)start, curOffset);
|
|
|
|
insSize = ud_disassemble(&udis);
|
|
|
|
if (insSize != curOffset)
|
|
|
|
break;
|
|
|
|
curOffset++;
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:34:44 +01:00
|
|
|
// check for RIP refs
|
|
|
|
std::string ins;
|
|
|
|
if (const auto CINS = ud_insn_asm(&udis); CINS)
|
|
|
|
ins = std::string(CINS);
|
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
return {insSize, ins};
|
2023-02-27 13:32:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
CFunctionHook::SInstructionProbe CFunctionHook::probeMinimumJumpSize(void* start, size_t min) {
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
std::string instrs = "";
|
|
|
|
std::vector<size_t> sizes;
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
while (size <= min) {
|
|
|
|
// find info about this instruction
|
2023-12-27 11:43:04 +01:00
|
|
|
auto probe = getInstructionLenAt((uint8_t*)start + size);
|
|
|
|
sizes.push_back(probe.len);
|
|
|
|
size += probe.len;
|
|
|
|
instrs += probe.assembly + "\n";
|
2023-02-27 13:32:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
return {size, instrs, sizes};
|
|
|
|
}
|
|
|
|
|
|
|
|
CFunctionHook::SAssembly CFunctionHook::fixInstructionProbeRIPCalls(const SInstructionProbe& probe) {
|
2024-04-04 19:50:37 +02:00
|
|
|
SAssembly returns;
|
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
// analyze the code and fix what we know how to.
|
|
|
|
uint64_t currentAddress = (uint64_t)m_pSource;
|
|
|
|
// actually newline + 1
|
2024-04-04 19:50:37 +02:00
|
|
|
size_t lastAsmNewline = 0;
|
|
|
|
// needle for destination binary
|
|
|
|
size_t currentDestinationOffset = 0;
|
2023-12-27 11:43:04 +01:00
|
|
|
std::string assemblyBuilder;
|
|
|
|
for (auto& len : probe.insSizes) {
|
|
|
|
|
|
|
|
std::string code = probe.assembly.substr(lastAsmNewline, probe.assembly.find("\n", lastAsmNewline) - lastAsmNewline);
|
|
|
|
if (code.contains("%rip")) {
|
|
|
|
CVarList tokens{code, 0, 's'};
|
2024-01-24 14:53:06 +01:00
|
|
|
size_t plusPresent = tokens[1][0] == '+' ? 1 : 0;
|
|
|
|
size_t minusPresent = tokens[1][0] == '-' ? 1 : 0;
|
|
|
|
std::string addr = tokens[1].substr((plusPresent || minusPresent), tokens[1].find("(%rip)") - (plusPresent || minusPresent));
|
|
|
|
const uint64_t OFFSET = (minusPresent ? -1 : 1) * configStringToInt(addr);
|
2023-12-27 11:43:04 +01:00
|
|
|
if (OFFSET == 0)
|
|
|
|
return {};
|
|
|
|
const uint64_t DESTINATION = currentAddress + OFFSET + len;
|
|
|
|
|
2024-04-04 19:50:37 +02:00
|
|
|
if (code.starts_with("call")) {
|
2023-12-27 11:43:04 +01:00
|
|
|
// call +0xdeadbeef(%rip)
|
|
|
|
assemblyBuilder += std::format("pushq %rax\nmovabs $0x{:x}, %rax\ncallq *%rax\npopq %rax\n", DESTINATION);
|
2024-04-04 19:50:37 +02:00
|
|
|
currentDestinationOffset += 14;
|
2023-12-27 11:43:04 +01:00
|
|
|
} else if (code.starts_with("lea")) {
|
|
|
|
// lea 0xdeadbeef(%rip), %rax
|
|
|
|
assemblyBuilder += std::format("movabs $0x{:x}, {}\n", DESTINATION, tokens[2]);
|
2024-04-04 19:50:37 +02:00
|
|
|
currentDestinationOffset += 10;
|
2023-12-27 11:43:04 +01:00
|
|
|
} else {
|
2024-04-04 19:50:37 +02:00
|
|
|
auto ADDREND = code.find("(%rip)");
|
|
|
|
auto ADDRSTART = (code.substr(0, ADDREND).find_last_of(' '));
|
|
|
|
|
|
|
|
if (ADDREND == std::string::npos || ADDRSTART == std::string::npos)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const uint64_t PREDICTEDRIP = (uint64_t)m_pTrampolineAddr + currentDestinationOffset + len;
|
|
|
|
const bool POSITIVE = DESTINATION > PREDICTEDRIP;
|
|
|
|
const uint64_t NEWRIPOFFSET = POSITIVE ? DESTINATION - PREDICTEDRIP : PREDICTEDRIP - DESTINATION;
|
|
|
|
|
|
|
|
assemblyBuilder += std::format("{} {}0x{:x}{}\n", code.substr(0, ADDRSTART), POSITIVE ? '+' : '-', NEWRIPOFFSET, code.substr(ADDREND));
|
|
|
|
currentDestinationOffset += len;
|
2023-12-27 11:43:04 +01:00
|
|
|
}
|
2023-12-28 13:36:09 +01:00
|
|
|
} else if (code.contains("invalid")) {
|
|
|
|
std::vector<uint8_t> bytes;
|
|
|
|
bytes.resize(len);
|
|
|
|
memcpy(bytes.data(), (std::byte*)currentAddress, len);
|
|
|
|
if (len == 4 && bytes[0] == 0xF3 && bytes[1] == 0x0F && bytes[2] == 0x1E && bytes[3] == 0xFA) {
|
|
|
|
// F3 0F 1E FA = endbr64, udis doesn't understand that one
|
|
|
|
assemblyBuilder += "endbr64\n";
|
2024-04-04 19:50:37 +02:00
|
|
|
currentDestinationOffset += 4;
|
2023-12-28 13:36:09 +01:00
|
|
|
} else {
|
|
|
|
// raise error, unknown op
|
|
|
|
std::string strBytes;
|
|
|
|
for (auto& b : bytes) {
|
|
|
|
strBytes += std::format("{:x} ", b);
|
|
|
|
}
|
|
|
|
Debug::log(ERR, "[functionhook] unknown bytes: {}", strBytes);
|
|
|
|
return {};
|
|
|
|
}
|
2023-12-27 11:43:04 +01:00
|
|
|
} else {
|
|
|
|
assemblyBuilder += code + "\n";
|
2024-04-04 19:50:37 +02:00
|
|
|
currentDestinationOffset += len;
|
2023-12-27 11:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lastAsmNewline = probe.assembly.find("\n", lastAsmNewline) + 1;
|
|
|
|
currentAddress += len;
|
|
|
|
}
|
|
|
|
|
2024-04-28 19:28:19 +02:00
|
|
|
const auto RANDOMDIR = "/tmp/hypr/" + g_pTokenManager->getRandomUUID();
|
|
|
|
|
2024-04-28 22:20:10 +02:00
|
|
|
if (std::filesystem::exists(RANDOMDIR)) {
|
|
|
|
Debug::log(ERR, "[hooksystem] random out dir exists??");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir(RANDOMDIR.c_str(), S_IRWXU) < 0)
|
|
|
|
return {};
|
2024-04-28 19:28:19 +02:00
|
|
|
|
2024-04-28 19:58:31 +02:00
|
|
|
if (!std::filesystem::exists(RANDOMDIR))
|
|
|
|
return {};
|
2024-04-28 19:28:19 +02:00
|
|
|
|
|
|
|
std::ofstream ofs(RANDOMDIR + "/.hookcode.asm", std::ios::trunc);
|
2023-12-27 11:43:04 +01:00
|
|
|
ofs << assemblyBuilder;
|
|
|
|
ofs.close();
|
2024-04-28 19:28:19 +02:00
|
|
|
std::string ret = execAndGet(std::string{"cc -x assembler -c " + RANDOMDIR + "/.hookcode.asm -o " + RANDOMDIR + "/.hookbinary.o 2>&1 && objcopy -O binary -j .text " +
|
|
|
|
RANDOMDIR + "/.hookbinary.o " + RANDOMDIR + "/.hookbinary2.o 2>&1"}
|
|
|
|
.c_str());
|
2023-12-27 19:23:06 +01:00
|
|
|
Debug::log(LOG, "[functionhook] assembler returned:\n{}", ret);
|
2024-04-28 19:28:19 +02:00
|
|
|
if (!std::filesystem::exists(RANDOMDIR + "/.hookbinary2.o")) {
|
|
|
|
std::filesystem::remove(RANDOMDIR + "/.hookcode.asm");
|
|
|
|
std::filesystem::remove(RANDOMDIR + "/.hookbinary.asm");
|
2023-12-27 11:43:04 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-04-28 19:28:19 +02:00
|
|
|
std::ifstream ifs(RANDOMDIR + "/.hookbinary2.o", std::ios::binary);
|
2024-04-04 19:50:37 +02:00
|
|
|
returns = {std::vector<char>(std::istreambuf_iterator<char>(ifs), {})};
|
2023-12-27 11:43:04 +01:00
|
|
|
ifs.close();
|
2024-04-28 19:28:19 +02:00
|
|
|
std::filesystem::remove_all(RANDOMDIR);
|
2023-12-27 11:43:04 +01:00
|
|
|
|
|
|
|
return returns;
|
2023-02-27 13:32:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CFunctionHook::hook() {
|
|
|
|
|
|
|
|
// check for unsupported platforms
|
|
|
|
#if !defined(__x86_64__)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// movabs $0,%rax | jmpq *%rax
|
2023-02-27 19:34:44 +01:00
|
|
|
// offset for addr: 2
|
|
|
|
static constexpr uint8_t ABSOLUTE_JMP_ADDRESS[] = {0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE0};
|
|
|
|
static constexpr size_t ABSOLUTE_JMP_ADDRESS_OFFSET = 2;
|
2023-02-27 13:32:38 +01:00
|
|
|
// pushq %rax
|
|
|
|
static constexpr uint8_t PUSH_RAX[] = {0x50};
|
|
|
|
// popq %rax
|
|
|
|
static constexpr uint8_t POP_RAX[] = {0x58};
|
|
|
|
// nop
|
|
|
|
static constexpr uint8_t NOP = 0x90;
|
|
|
|
|
2024-04-04 19:50:37 +02:00
|
|
|
// alloc trampoline
|
|
|
|
const auto MAX_TRAMPOLINE_SIZE = HOOK_TRAMPOLINE_MAX_SIZE; // we will never need more.
|
|
|
|
m_pTrampolineAddr = (void*)g_pFunctionHookSystem->getAddressForTrampo();
|
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
// probe instructions to be trampolin'd
|
|
|
|
SInstructionProbe probe;
|
2023-11-26 18:53:51 +01:00
|
|
|
try {
|
2023-12-27 11:43:04 +01:00
|
|
|
probe = probeMinimumJumpSize(m_pSource, sizeof(ABSOLUTE_JMP_ADDRESS) + sizeof(PUSH_RAX) + sizeof(POP_RAX));
|
2023-11-26 18:53:51 +01:00
|
|
|
} catch (std::exception& e) { return false; }
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
const auto PROBEFIXEDASM = fixInstructionProbeRIPCalls(probe);
|
|
|
|
|
|
|
|
if (PROBEFIXEDASM.bytes.size() == 0) {
|
2023-12-27 19:27:15 +01:00
|
|
|
Debug::log(ERR, "[functionhook] failed, unsupported asm / failed assembling:\n{}", probe.assembly);
|
2023-12-27 11:43:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t HOOKSIZE = PROBEFIXEDASM.bytes.size();
|
|
|
|
const size_t ORIGSIZE = probe.len;
|
|
|
|
|
2024-04-04 19:50:37 +02:00
|
|
|
const auto TRAMPOLINE_SIZE = sizeof(ABSOLUTE_JMP_ADDRESS) + HOOKSIZE + sizeof(PUSH_RAX);
|
|
|
|
|
|
|
|
if (TRAMPOLINE_SIZE > MAX_TRAMPOLINE_SIZE) {
|
|
|
|
Debug::log(ERR, "[functionhook] failed, not enough space in trampo to alloc:\n{}", probe.assembly);
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
m_pOriginalBytes = malloc(ORIGSIZE);
|
|
|
|
memcpy(m_pOriginalBytes, m_pSource, ORIGSIZE);
|
2023-02-27 20:17:58 +01:00
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
// populate trampoline
|
2023-12-27 11:43:04 +01:00
|
|
|
memcpy(m_pTrampolineAddr, PROBEFIXEDASM.bytes.data(), HOOKSIZE); // first, original but fixed func bytes
|
2023-03-04 15:02:40 +01:00
|
|
|
memcpy((uint8_t*)m_pTrampolineAddr + HOOKSIZE, PUSH_RAX, sizeof(PUSH_RAX)); // then, pushq %rax
|
|
|
|
memcpy((uint8_t*)m_pTrampolineAddr + HOOKSIZE + sizeof(PUSH_RAX), ABSOLUTE_JMP_ADDRESS, sizeof(ABSOLUTE_JMP_ADDRESS)); // then, jump to source
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// fixup trampoline addr
|
2023-06-16 18:45:11 +02:00
|
|
|
*(uint64_t*)((uint8_t*)m_pTrampolineAddr + TRAMPOLINE_SIZE - sizeof(ABSOLUTE_JMP_ADDRESS) + ABSOLUTE_JMP_ADDRESS_OFFSET) =
|
|
|
|
(uint64_t)((uint8_t*)m_pSource + sizeof(ABSOLUTE_JMP_ADDRESS));
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// make jump to hk
|
2024-01-01 23:05:26 +01:00
|
|
|
const auto PAGESIZE_VAR = sysconf(_SC_PAGE_SIZE);
|
|
|
|
const uint8_t* PROTSTART = (uint8_t*)m_pSource - ((uint64_t)m_pSource % PAGESIZE_VAR);
|
|
|
|
const size_t PROTLEN = std::ceil((float)(ORIGSIZE + ((uint64_t)m_pSource - (uint64_t)PROTSTART)) / (float)PAGESIZE_VAR) * PAGESIZE_VAR;
|
2023-12-09 17:16:46 +01:00
|
|
|
mprotect((uint8_t*)PROTSTART, PROTLEN, PROT_READ | PROT_WRITE | PROT_EXEC);
|
|
|
|
memcpy((uint8_t*)m_pSource, ABSOLUTE_JMP_ADDRESS, sizeof(ABSOLUTE_JMP_ADDRESS));
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// make popq %rax and NOP all remaining
|
2023-03-04 15:02:40 +01:00
|
|
|
memcpy((uint8_t*)m_pSource + sizeof(ABSOLUTE_JMP_ADDRESS), POP_RAX, sizeof(POP_RAX));
|
2023-02-27 13:32:38 +01:00
|
|
|
size_t currentOp = sizeof(ABSOLUTE_JMP_ADDRESS) + sizeof(POP_RAX);
|
2023-12-27 11:43:04 +01:00
|
|
|
memset((uint8_t*)m_pSource + currentOp, NOP, ORIGSIZE - currentOp);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// fixup jump addr
|
2023-03-04 15:02:40 +01:00
|
|
|
*(uint64_t*)((uint8_t*)m_pSource + ABSOLUTE_JMP_ADDRESS_OFFSET) = (uint64_t)(m_pDestination);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// revert mprot
|
2023-12-09 17:16:46 +01:00
|
|
|
mprotect((uint8_t*)PROTSTART, PROTLEN, PROT_READ | PROT_EXEC);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// set original addr to trampo addr
|
|
|
|
m_pOriginal = m_pTrampolineAddr;
|
|
|
|
|
|
|
|
m_bActive = true;
|
2023-12-27 11:43:04 +01:00
|
|
|
m_iHookLen = ORIGSIZE;
|
2023-02-27 19:34:44 +01:00
|
|
|
m_iTrampoLen = TRAMPOLINE_SIZE;
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFunctionHook::unhook() {
|
|
|
|
// check for unsupported platforms
|
|
|
|
#if !defined(__x86_64__)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!m_bActive)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// allow write to src
|
2023-03-04 15:02:40 +01:00
|
|
|
mprotect((uint8_t*)m_pSource - ((uint64_t)m_pSource) % sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE | PROT_EXEC);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// write back original bytes
|
2023-02-27 20:17:58 +01:00
|
|
|
memcpy(m_pSource, m_pOriginalBytes, m_iHookLen);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// revert mprot
|
2023-03-04 15:02:40 +01:00
|
|
|
mprotect((uint8_t*)m_pSource - ((uint64_t)m_pSource) % sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_EXEC);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// reset vars
|
|
|
|
m_bActive = false;
|
|
|
|
m_iHookLen = 0;
|
|
|
|
m_iTrampoLen = 0;
|
2024-04-04 19:50:37 +02:00
|
|
|
m_pTrampolineAddr = nullptr; // no unmapping, it's managed by the HookSystem
|
2023-02-27 20:17:58 +01:00
|
|
|
m_pOriginalBytes = nullptr;
|
|
|
|
|
|
|
|
free(m_pOriginalBytes);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFunctionHook* CHookSystem::initHook(HANDLE owner, void* source, void* destination) {
|
|
|
|
return m_vHooks.emplace_back(std::make_unique<CFunctionHook>(owner, source, destination)).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CHookSystem::removeHook(CFunctionHook* hook) {
|
|
|
|
std::erase_if(m_vHooks, [&](const auto& other) { return other.get() == hook; });
|
|
|
|
return true; // todo: make false if not found
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHookSystem::removeAllHooksFrom(HANDLE handle) {
|
|
|
|
std::erase_if(m_vHooks, [&](const auto& other) { return other->m_pOwner == handle; });
|
2023-03-04 15:02:40 +01:00
|
|
|
}
|
2024-04-04 19:50:37 +02:00
|
|
|
|
|
|
|
static uintptr_t seekNewPageAddr() {
|
|
|
|
const uint64_t PAGESIZE_VAR = sysconf(_SC_PAGE_SIZE);
|
|
|
|
auto MAPS = std::ifstream("/proc/self/maps");
|
|
|
|
|
|
|
|
uint64_t lastStart = 0, lastEnd = 0;
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(MAPS, line)) {
|
|
|
|
CVarList props{line, 0, 's', true};
|
|
|
|
|
|
|
|
uint64_t start = 0, end = 0;
|
|
|
|
if (props[0].empty()) {
|
|
|
|
Debug::log(WARN, "seekNewPageAddr: unexpected line in self maps");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CVarList startEnd{props[0], 0, '-', true};
|
|
|
|
|
|
|
|
try {
|
|
|
|
start = std::stoull(startEnd[0], nullptr, 16);
|
|
|
|
end = std::stoull(startEnd[1], nullptr, 16);
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
Debug::log(WARN, "seekNewPageAddr: unexpected line in self maps: {}", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug::log(LOG, "seekNewPageAddr: page 0x{:x} - 0x{:x}", start, end);
|
|
|
|
|
|
|
|
if (lastStart == 0) {
|
|
|
|
lastStart = start;
|
|
|
|
lastEnd = end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start - lastEnd > PAGESIZE_VAR * 2) {
|
|
|
|
Debug::log(LOG, "seekNewPageAddr: found gap: 0x{:x}-0x{:x} ({} bytes)", lastEnd, start, start - lastEnd);
|
|
|
|
MAPS.close();
|
|
|
|
return lastEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastStart = start;
|
|
|
|
lastEnd = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
MAPS.close();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t CHookSystem::getAddressForTrampo() {
|
|
|
|
// yes, technically this creates a memory leak of 64B every hook creation. But I don't care.
|
|
|
|
// tracking all the users of the memory would be painful.
|
2024-04-05 13:56:53 +02:00
|
|
|
// Nobody will hook 100k times, and even if, that's only 6.4 MB. Nothing.
|
2024-04-04 19:50:37 +02:00
|
|
|
|
|
|
|
SAllocatedPage* page = nullptr;
|
|
|
|
for (auto& p : pages) {
|
|
|
|
if (p.used + HOOK_TRAMPOLINE_MAX_SIZE > p.len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
page = &p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!page)
|
|
|
|
page = &pages.emplace_back();
|
|
|
|
|
|
|
|
if (!page->addr) {
|
|
|
|
// allocate it
|
|
|
|
Debug::log(LOG, "getAddressForTrampo: Allocating new page for hooks");
|
|
|
|
const uint64_t PAGESIZE_VAR = sysconf(_SC_PAGE_SIZE);
|
|
|
|
const auto BASEPAGEADDR = seekNewPageAddr();
|
|
|
|
for (int attempt = 0; attempt < 2; ++attempt) {
|
2024-04-05 18:16:09 +02:00
|
|
|
for (int i = 0; i <= 2; ++i) {
|
2024-04-04 19:50:37 +02:00
|
|
|
const auto PAGEADDR = BASEPAGEADDR + i * PAGESIZE_VAR;
|
|
|
|
|
|
|
|
page->addr = (uint64_t)mmap((void*)PAGEADDR, PAGESIZE_VAR, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
page->len = PAGESIZE_VAR;
|
|
|
|
page->used = 0;
|
|
|
|
|
|
|
|
Debug::log(LOG, "Attempted to allocate 0x{:x}, got 0x{:x}", PAGEADDR, page->addr);
|
|
|
|
|
|
|
|
if (page->addr == (uint64_t)MAP_FAILED)
|
|
|
|
continue;
|
|
|
|
if (page->addr != PAGEADDR && attempt == 0) {
|
|
|
|
munmap((void*)page->addr, PAGESIZE_VAR);
|
|
|
|
page->addr = 0;
|
|
|
|
page->len = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (page->addr)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto ADDRFORCONSUMER = page->addr + page->used;
|
|
|
|
|
|
|
|
page->used += HOOK_TRAMPOLINE_MAX_SIZE;
|
|
|
|
|
|
|
|
Debug::log(LOG, "getAddressForTrampo: Returning addr 0x{:x} for page at 0x{:x}", ADDRFORCONSUMER, page->addr);
|
|
|
|
|
|
|
|
return ADDRFORCONSUMER;
|
|
|
|
}
|