Magisk/native/jni/zygisk/utils.cpp

117 lines
3.2 KiB
C++
Raw Normal View History

2021-08-11 07:00:21 +00:00
#include <cinttypes>
#include <utils.hpp>
2021-10-05 10:53:11 +00:00
#include "zygisk.hpp"
using namespace std;
namespace {
struct map_info {
uintptr_t start;
uintptr_t end;
2021-08-11 07:00:21 +00:00
uintptr_t off;
int perms;
const char *path;
2021-08-11 07:00:21 +00:00
map_info() : start(0), end(0), off(0), perms(0), path(nullptr) {}
};
} // namespace
template<typename Func>
static void parse_maps(int pid, Func fn) {
char file[32];
// format: start-end perms offset dev inode path
sprintf(file, "/proc/%d/maps", pid);
file_readline(true, file, [=](string_view l) -> bool {
2021-08-11 07:00:21 +00:00
char *line = (char *) l.data();
map_info info;
2021-08-11 07:00:21 +00:00
char perm[5];
int path_off;
2021-08-11 07:00:21 +00:00
if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n%*s",
&info.start, &info.end, perm, &info.off, &path_off) != 4)
return true;
// Parse permissions
2021-08-11 07:00:21 +00:00
if (perm[0] != '-')
info.perms |= PROT_READ;
2021-08-11 07:00:21 +00:00
if (perm[1] != '-')
info.perms |= PROT_WRITE;
2021-08-11 07:00:21 +00:00
if (perm[2] != '-')
info.perms |= PROT_EXEC;
2021-08-11 07:00:21 +00:00
info.path = line + path_off;
return fn(info);
});
}
static vector<map_info> find_maps(const char *name) {
vector<map_info> maps;
parse_maps(getpid(), [=, &maps](map_info &info) -> bool {
if (strcmp(info.path, name) == 0)
maps.emplace_back(info);
return true;
});
return maps;
}
void unmap_all(const char *name) {
vector<map_info> maps = find_maps(name);
for (map_info &info : maps) {
void *addr = reinterpret_cast<void *>(info.start);
size_t size = info.end - info.start;
if (info.perms & PROT_READ) {
// Make sure readable pages are still readable
void *dummy = xmmap(nullptr, size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
mremap(dummy, size, size, MREMAP_MAYMOVE | MREMAP_FIXED, addr);
} else {
munmap(addr, size);
}
}
}
void remap_all(const char *name) {
vector<map_info> maps = find_maps(name);
for (map_info &info : maps) {
void *addr = reinterpret_cast<void *>(info.start);
size_t size = info.end - info.start;
void *copy = xmmap(nullptr, size, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if ((info.perms & PROT_READ) == 0) {
mprotect(addr, size, PROT_READ);
}
memcpy(copy, addr, size);
mremap(copy, size, size, MREMAP_MAYMOVE | MREMAP_FIXED, addr);
mprotect(addr, size, info.perms);
}
}
2021-08-11 07:00:21 +00:00
uintptr_t get_function_off(int pid, uintptr_t addr, char *lib) {
uintptr_t off = 0;
parse_maps(pid, [=, &off](map_info &info) -> bool {
if (addr >= info.start && addr < info.end) {
if (lib)
strcpy(lib, info.path);
2021-08-11 07:00:21 +00:00
off = addr - info.start + info.off;
return false;
}
return true;
});
2021-08-11 07:00:21 +00:00
return off;
}
2021-08-11 07:00:21 +00:00
uintptr_t get_function_addr(int pid, const char *lib, uintptr_t off) {
uintptr_t addr = 0;
parse_maps(pid, [=, &addr](map_info &info) -> bool {
if (strcmp(info.path, lib) == 0 && (info.perms & PROT_EXEC)) {
2021-08-11 07:00:21 +00:00
addr = info.start - info.off + off;
return false;
}
return true;
});
2021-08-11 07:00:21 +00:00
return addr;
}