2021-08-11 07:00:21 +00:00
|
|
|
#include <cinttypes>
|
2022-05-12 09:03:42 +00:00
|
|
|
#include <base.hpp>
|
2021-01-07 06:21:17 +00:00
|
|
|
|
2021-10-05 10:53:11 +00:00
|
|
|
#include "zygisk.hpp"
|
2021-01-07 06:21:17 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct map_info {
|
|
|
|
uintptr_t start;
|
|
|
|
uintptr_t end;
|
2021-08-11 07:00:21 +00:00
|
|
|
uintptr_t off;
|
2021-01-07 06:21:17 +00:00
|
|
|
int perms;
|
2022-02-06 08:27:31 +00:00
|
|
|
unsigned long inode;
|
2021-01-07 06:21:17 +00:00
|
|
|
|
2022-02-06 08:27:31 +00:00
|
|
|
map_info() : start(0), end(0), off(0), perms(0), inode(0) {}
|
2021-01-07 06:21:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // 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();
|
2021-01-07 06:21:17 +00:00
|
|
|
map_info info;
|
2021-08-11 07:00:21 +00:00
|
|
|
char perm[5];
|
|
|
|
int path_off;
|
2021-01-07 06:21:17 +00:00
|
|
|
|
2022-02-06 08:27:31 +00:00
|
|
|
if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %lu %n%*s",
|
|
|
|
&info.start, &info.end, perm, &info.off, &info.inode, &path_off) != 5)
|
2021-08-11 07:00:21 +00:00
|
|
|
return true;
|
2021-01-07 06:21:17 +00:00
|
|
|
|
|
|
|
// Parse permissions
|
2021-08-11 07:00:21 +00:00
|
|
|
if (perm[0] != '-')
|
2021-09-23 02:14:05 +00:00
|
|
|
info.perms |= PROT_READ;
|
2021-08-11 07:00:21 +00:00
|
|
|
if (perm[1] != '-')
|
2021-09-23 02:14:05 +00:00
|
|
|
info.perms |= PROT_WRITE;
|
2021-08-11 07:00:21 +00:00
|
|
|
if (perm[2] != '-')
|
2021-09-23 02:14:05 +00:00
|
|
|
info.perms |= PROT_EXEC;
|
2021-01-07 06:21:17 +00:00
|
|
|
|
2022-02-06 08:27:31 +00:00
|
|
|
return fn(info, line + path_off);
|
2021-01-07 06:21:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-23 02:14:05 +00:00
|
|
|
static vector<map_info> find_maps(const char *name) {
|
|
|
|
vector<map_info> maps;
|
2022-02-06 08:27:31 +00:00
|
|
|
parse_maps(getpid(), [=, &maps](const map_info &info, const char *path) -> bool {
|
|
|
|
if (strcmp(path, name) == 0)
|
2021-09-23 02:14:05 +00:00
|
|
|
maps.emplace_back(info);
|
2021-01-07 06:21:17 +00:00
|
|
|
return true;
|
|
|
|
});
|
2021-09-23 02:14:05 +00:00
|
|
|
return maps;
|
|
|
|
}
|
|
|
|
|
2022-02-06 08:27:31 +00:00
|
|
|
std::pair<void *, size_t> find_map_range(const char *name, unsigned long inode) {
|
|
|
|
vector<map_info> maps = find_maps(name);
|
|
|
|
uintptr_t start = 0u;
|
|
|
|
uintptr_t end = 0u;
|
|
|
|
for (const auto &map : maps) {
|
|
|
|
if (map.inode == inode) {
|
|
|
|
if (start == 0) {
|
|
|
|
start = map.start;
|
|
|
|
end = map.end;
|
|
|
|
} else if (map.start == end) {
|
|
|
|
end = map.end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOGD("found map %s with start = %zx, end = %zx\n", name, start, end);
|
|
|
|
return make_pair(reinterpret_cast<void *>(start), end - start);
|
|
|
|
}
|
|
|
|
|
2021-09-23 02:14:05 +00:00
|
|
|
void unmap_all(const char *name) {
|
|
|
|
vector<map_info> maps = find_maps(name);
|
|
|
|
for (map_info &info : maps) {
|
2021-01-07 06:21:17 +00:00
|
|
|
void *addr = reinterpret_cast<void *>(info.start);
|
|
|
|
size_t size = info.end - info.start;
|
2021-09-23 02:14:05 +00:00
|
|
|
if (info.perms & PROT_READ) {
|
2021-01-07 06:21:17 +00:00
|
|
|
// Make sure readable pages are still readable
|
2021-09-23 02:14:05 +00:00
|
|
|
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);
|
2021-01-07 06:21:17 +00:00
|
|
|
}
|
2021-09-23 02:14:05 +00:00
|
|
|
memcpy(copy, addr, size);
|
|
|
|
mremap(copy, size, size, MREMAP_MAYMOVE | MREMAP_FIXED, addr);
|
|
|
|
mprotect(addr, size, info.perms);
|
2021-01-07 06:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 07:00:21 +00:00
|
|
|
uintptr_t get_function_off(int pid, uintptr_t addr, char *lib) {
|
|
|
|
uintptr_t off = 0;
|
2022-02-06 08:27:31 +00:00
|
|
|
parse_maps(pid, [=, &off](const map_info &info, const char *path) -> bool {
|
2021-01-07 06:21:17 +00:00
|
|
|
if (addr >= info.start && addr < info.end) {
|
|
|
|
if (lib)
|
2022-02-06 08:27:31 +00:00
|
|
|
strcpy(lib, path);
|
2021-08-11 07:00:21 +00:00
|
|
|
off = addr - info.start + info.off;
|
2021-01-07 06:21:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2021-08-11 07:00:21 +00:00
|
|
|
return off;
|
2021-01-07 06:21:17 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2022-02-06 08:27:31 +00:00
|
|
|
parse_maps(pid, [=, &addr](const map_info &info, const char *path) -> bool {
|
|
|
|
if (strcmp(path, lib) == 0 && (info.perms & PROT_EXEC)) {
|
2021-08-11 07:00:21 +00:00
|
|
|
addr = info.start - info.off + off;
|
2021-01-07 06:21:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2021-08-11 07:00:21 +00:00
|
|
|
return addr;
|
2021-01-07 06:21:17 +00:00
|
|
|
}
|