LSPlt/lsplt/src/main/jni/lsplt.cc

251 lines
8.8 KiB
C++
Raw Normal View History

2022-11-26 22:45:45 +08:00
#include "include/lsplt.hpp"
#include <regex.h>
#include <sys/mman.h>
2022-11-27 00:59:56 +08:00
#include <sys/sysmacros.h>
2022-11-26 22:45:45 +08:00
#include <array>
#include <cinttypes>
#include <list>
#include <map>
#include <mutex>
#include <vector>
#include "elf_util.hpp"
namespace {
2022-11-27 00:29:32 +08:00
struct RegisterInfo {
2022-11-26 22:45:45 +08:00
ino_t inode;
std::string symbol;
void *callback;
void **backup;
};
2022-11-27 00:59:56 +08:00
struct HookInfo : public lsplt::MapInfo {
2022-11-26 22:45:45 +08:00
std::map<uintptr_t, uintptr_t> hooks;
uintptr_t backup;
std::unique_ptr<Elf> elf;
2022-11-27 00:29:32 +08:00
[[nodiscard]] bool Match(const RegisterInfo &info) const { return info.inode == inode; }
2022-11-26 22:45:45 +08:00
};
2022-11-27 00:59:56 +08:00
class HookInfos : public std::map<uintptr_t, HookInfo, std::greater<>> {
2022-11-26 22:45:45 +08:00
public:
2022-11-27 00:59:56 +08:00
static auto ScanHookInfo() {
HookInfos info;
for (auto &map : lsplt::MapInfo::Scan()) {
// we basically only care about r--p entry
// and for offset == 0 it's an ELF header
// and for offset != 0 it's what we hook
// if (perm[0] != 'r') continue;
if (!map.is_private) continue;
if (map.perm & PROT_EXEC) continue;
// if (off != 0) continue;
if (map.path.empty()) continue;
if (map.path[0] == '[') continue;
auto start = map.start;
info.emplace(start, HookInfo{{std::move(map)}, {}, 0, nullptr});
2022-11-26 22:45:45 +08:00
}
return info;
}
// fiter out ignored
2022-11-27 00:29:32 +08:00
void Filter(const std::list<RegisterInfo> &register_info) {
2022-11-26 22:45:45 +08:00
for (auto iter = begin(); iter != end();) {
const auto &info = iter->second;
bool matched = false;
for (const auto &reg : register_info) {
2022-11-27 00:29:32 +08:00
if (info.Match(reg)) {
2022-11-26 22:45:45 +08:00
matched = true;
break;
}
}
if (matched) {
++iter;
} else {
iter = erase(iter);
}
}
}
2022-11-27 00:59:56 +08:00
void Merge(HookInfos &old) {
2022-11-26 22:45:45 +08:00
// merge with old map info
for (auto &info : old) {
if (info.second.backup) {
erase(info.second.backup);
}
if (auto iter = find(info.first); iter != end()) {
iter->second = std::move(info.second);
} else {
emplace(info.first, std::move(info.second));
}
}
}
bool DoHook(uintptr_t addr, uintptr_t callback, uintptr_t *backup) {
auto iter = lower_bound(addr);
if (iter == end()) return false;
// iter.first < addr
auto &info = iter->second;
if (info.end <= addr) return false;
if (!iter->second.backup) {
auto len = info.end - info.start;
// let os find a suitable address
auto *backup_addr = mmap(nullptr, len, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (backup_addr == MAP_FAILED) return false;
if (auto *new_addr = mremap(reinterpret_cast<void *>(info.start), len, len,
MREMAP_FIXED | MREMAP_MAYMOVE, backup_addr);
new_addr == MAP_FAILED || new_addr != backup_addr) {
return false;
}
if (auto *new_addr =
mmap(reinterpret_cast<void *>(info.start), len, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0);
new_addr == MAP_FAILED) {
return false;
}
memcpy(reinterpret_cast<void *>(info.start), backup_addr, len);
info.backup = reinterpret_cast<uintptr_t>(backup_addr);
}
auto *the_addr = reinterpret_cast<uintptr_t *>(addr);
*backup = *the_addr;
*the_addr = callback;
if (auto hook_iter = info.hooks.find(addr); hook_iter != info.hooks.end()) {
if (hook_iter->second == callback) info.hooks.erase(hook_iter);
} else {
info.hooks.emplace(addr, *backup);
}
if (info.hooks.empty()) {
auto len = info.end - info.start;
if (auto *new_addr =
mremap(reinterpret_cast<void *>(info.backup), len, len,
MREMAP_FIXED | MREMAP_MAYMOVE, reinterpret_cast<void *>(info.start));
new_addr == MAP_FAILED || reinterpret_cast<uintptr_t>(new_addr) != info.start) {
return false;
}
info.backup = 0;
}
return true;
}
2022-11-27 00:29:32 +08:00
bool DoHook(std::list<RegisterInfo> &register_info) {
2022-11-26 22:45:45 +08:00
bool res = true;
for (auto &[_, info] : *this) {
for (auto iter = register_info.begin(); iter != register_info.end();) {
const auto &reg = *iter;
2022-11-27 00:29:32 +08:00
if (info.start != 0 || !info.Match(reg)) continue;
if (!info.elf) info.elf = std::make_unique<Elf>(info.start);
2022-11-26 22:45:45 +08:00
if (info.elf && info.elf->Valid()) {
2022-11-27 00:29:32 +08:00
for (auto addr : info.elf->FindPltAddr(reg.symbol)) {
2022-11-26 22:45:45 +08:00
res = DoHook(addr, reinterpret_cast<uintptr_t>(reg.callback),
2022-11-27 00:29:32 +08:00
reinterpret_cast<uintptr_t *>(reg.backup)) &&
res;
2022-11-26 22:45:45 +08:00
}
}
2022-11-27 00:29:32 +08:00
iter = register_info.erase(iter);
2022-11-26 22:45:45 +08:00
}
}
return res;
}
bool InvalidateBackup() {
bool res = true;
for (auto &[_, info] : *this) {
2022-11-27 00:29:32 +08:00
if (!info.backup) continue;
for (auto &[addr, backup] : info.hooks) {
// store new address to backup since we don't need backup
backup = *reinterpret_cast<uintptr_t *>(addr);
}
auto len = info.end - info.start;
if (auto *new_addr =
mremap(reinterpret_cast<void *>(info.backup), len, len,
MREMAP_FIXED | MREMAP_MAYMOVE, reinterpret_cast<void *>(info.start));
new_addr == MAP_FAILED || reinterpret_cast<uintptr_t>(new_addr) != info.start) {
res = false;
2022-11-26 22:45:45 +08:00
info.hooks.clear();
2022-11-27 00:29:32 +08:00
continue;
}
2022-11-27 01:03:58 +08:00
mprotect(reinterpret_cast<void *>(info.start), len, PROT_WRITE);
2022-11-27 00:29:32 +08:00
for (auto &[addr, backup] : info.hooks) {
*reinterpret_cast<uintptr_t *>(addr) = backup;
2022-11-26 22:45:45 +08:00
}
2022-11-27 01:03:58 +08:00
mprotect(reinterpret_cast<void *>(info.start), len, info.perm);
2022-11-27 00:29:32 +08:00
info.hooks.clear();
info.backup = 0;
2022-11-26 22:45:45 +08:00
}
return res;
}
};
2022-11-27 00:29:32 +08:00
std::mutex hook_mutex;
2022-11-26 22:45:45 +08:00
std::list<RegisterInfo> register_info;
2022-11-27 00:59:56 +08:00
HookInfos hook_info;
2022-11-26 22:45:45 +08:00
} // namespace
namespace lsplt {
2022-11-27 00:59:56 +08:00
[[maybe_unused]] std::vector<MapInfo> MapInfo::Scan() {
2022-11-27 00:43:46 +08:00
constexpr static auto kPermLength = 5;
2022-11-27 00:59:56 +08:00
constexpr static auto kMapEntry = 7;
2022-11-27 00:43:46 +08:00
std::vector<MapInfo> info;
auto maps = std::unique_ptr<FILE, decltype(&fclose)>{fopen("/proc/self/maps", "r"), &fclose};
if (maps) {
char *line = nullptr;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, maps.get())) != -1) {
uintptr_t start = 0;
uintptr_t end = 0;
uintptr_t off = 0;
ino_t inode = 0;
2022-11-27 00:59:56 +08:00
unsigned int dev_major = 0;
unsigned int dev_minor = 0;
2022-11-27 00:43:46 +08:00
std::array<char, kPermLength> perm{'\0'};
int path_off;
2022-11-27 00:59:56 +08:00
if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %x:%x %lu %n%*s", &start,
&end, perm.data(), &off, &dev_major, &dev_minor, &inode,
&path_off) != kMapEntry) {
2022-11-27 00:43:46 +08:00
continue;
}
while (path_off < read && isspace(line[path_off])) path_off++;
2022-11-27 00:59:56 +08:00
auto &ref = info.emplace_back(MapInfo{start, end, 0, perm[3] == 'p',
static_cast<dev_t>(makedev(dev_major, dev_minor)),
inode, line + path_off});
if (perm[0] == 'r') ref.perm |= PROT_READ;
if (perm[1] == 'w') ref.perm |= PROT_WRITE;
if (perm[2] == 'x') ref.perm |= PROT_EXEC;
2022-11-27 00:43:46 +08:00
}
free(line);
}
return info;
}
2022-11-26 22:45:45 +08:00
[[maybe_unused]] bool RegisterHook(ino_t ino, std::string_view symbol, void *callback,
void **backup) {
if (symbol.empty() || !callback) return false;
2022-11-27 00:29:32 +08:00
std::unique_lock lock(hook_mutex);
register_info.emplace_back(RegisterInfo{ino, std::string{symbol}, callback, backup});
2022-11-26 22:45:45 +08:00
return true;
}
[[maybe_unused]] bool CommitHook() {
2022-11-27 00:29:32 +08:00
std::unique_lock lock(hook_mutex);
2022-11-26 22:45:45 +08:00
if (register_info.empty()) return true;
2022-11-27 00:59:56 +08:00
auto new_hook_info = HookInfos::ScanHookInfo();
if (new_hook_info.empty()) return false;
2022-11-26 22:45:45 +08:00
2022-11-27 00:59:56 +08:00
new_hook_info.Filter(register_info);
2022-11-26 22:45:45 +08:00
2022-11-27 00:59:56 +08:00
new_hook_info.Merge(hook_info);
2022-11-26 22:45:45 +08:00
// update to new map info
2022-11-27 00:59:56 +08:00
hook_info = std::move(new_hook_info);
2022-11-26 22:45:45 +08:00
2022-11-27 00:59:56 +08:00
return hook_info.DoHook(register_info);
2022-11-26 22:45:45 +08:00
}
[[gnu::destructor]] [[maybe_unused]] bool InvalidateBackup() {
2022-11-27 00:29:32 +08:00
std::unique_lock lock(hook_mutex);
2022-11-27 00:59:56 +08:00
return hook_info.InvalidateBackup();
2022-11-26 22:45:45 +08:00
}
} // namespace lsplt