update: some loader/ code to C

This commit changes some code from "loader" folder to use C keywords and not C++ only keywords.
This commit is contained in:
ThePedroo
2024-06-16 21:13:44 -03:00
parent c50d89e28e
commit d0da6efd79
8 changed files with 693 additions and 534 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
.gradle .gradle
.idea .idea
.cxx .cxx
.vscode
build build
local.properties local.properties
Cargo.lock Cargo.lock

View File

@@ -1,55 +1,53 @@
#include <cstdio> #include "main.h"
#include <cstdlib>
#include <string_view>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/system_properties.h>
#include "main.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "daemon.h" #include "daemon.h"
#include <sys/mount.h>
using namespace std::string_view_literals;
int main(int argc, char **argv) { int main(int argc, char **argv) {
zygiskd::Init(getenv("TMP_PATH")); zygiskd::Init(getenv("TMP_PATH"));
if (argc >= 2 && argv[1] == "monitor"sv) { if (argc >= 2 && strcmp(argv[1], "monitor") == 0) {
init_monitor(); init_monitor();
return 0;
} else if (argc >= 3 && argv[1] == "trace"sv) { return 0;
if (argc >= 4 && argv[3] == "--restart"sv) { } else if (argc >= 3 && strcmp(argv[1], "trace") == 0) {
zygiskd::ZygoteRestart(); if (argc >= 4 && strcmp(argv[3], "--restart") == 0) zygiskd::ZygoteRestart();
}
auto pid = strtol(argv[2], 0, 0); long pid = strtol(argv[2], 0, 0);
if (!trace_zygote(pid)) { if (!trace_zygote(pid)) {
kill(pid, SIGKILL); kill(pid, SIGKILL);
return 1;
}
return 0;
} else if (argc >= 2 && argv[1] == "ctl"sv) {
if (argc == 3) {
if (argv[2] == "start"sv) {
send_control_command(START);
return 0;
} else if (argv[2] == "stop"sv) {
send_control_command(STOP);
return 0;
} else if (argv[2] == "exit"sv) {
send_control_command(EXIT);
return 0;
}
}
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
printf("Usage: %s ctl start|stop|exit\n", argv[0]);
return 1; return 1;
} else if (argc >= 2 && argv[1] == "version"sv) { }
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
return 0;
} else if (argc >= 2 && strcmp(argv[1], "ctl") == 0) {
if (argc == 3) {
if (strcmp(argv[2], "start") == 0) {
send_control_command(START);
return 0; return 0;
} else { } else if (strcmp(argv[2], "stop") == 0) {
printf("ReZygisk Tracer %s\n", ZKSU_VERSION); send_control_command(STOP);
printf("usage: %s monitor | trace <pid> | ctl <start|stop|exit> | version\n", argv[0]);
return 1; return 0;
} else if (strcmp(argv[2], "exit") == 0) {
send_control_command(EXIT);
return 0;
}
} }
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
printf("Usage: %s ctl start|stop|exit\n", argv[0]);
return 1;
} else if (argc >= 2 && strcmp(argv[1], "version") == 0) {
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
return 0;
} else {
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
printf("usage: %s monitor | trace <pid> | ctl <start|stop|exit> | version\n", argv[0]);
return 1;
}
} }

26
loader/src/ptracer/main.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef MAIN_HPP
#define MAIN_HPP
#include <stdbool.h>
void init_monitor();
bool trace_zygote(int pid);
enum Command {
START = 1,
STOP = 2,
EXIT = 3,
/* sent from daemon */
ZYGOTE64_INJECTED = 4,
ZYGOTE32_INJECTED = 5,
DAEMON64_SET_INFO = 6,
DAEMON32_SET_INFO = 7,
DAEMON64_SET_ERROR_INFO = 8,
DAEMON32_SET_ERROR_INFO = 9,
SYSTEM_SERVER_STARTED = 10
};
void send_control_command(enum Command cmd);
#endif /* MAIN_HPP */

View File

@@ -1,21 +0,0 @@
#pragma once
void init_monitor();
bool trace_zygote(int pid);
enum Command {
START = 1,
STOP = 2,
EXIT = 3,
// sent from daemon
ZYGOTE64_INJECTED = 4,
ZYGOTE32_INJECTED = 5,
DAEMON64_SET_INFO = 6,
DAEMON32_SET_INFO = 7,
DAEMON64_SET_ERROR_INFO = 8,
DAEMON32_SET_ERROR_INFO = 9,
SYSTEM_SERVER_STARTED = 10
};
void send_control_command(Command cmd);

File diff suppressed because it is too large Load Diff

View File

@@ -161,7 +161,10 @@ bool inject_on_main(int pid, const char *lib_path) {
return true; return true;
} else { } else {
LOGE("stopped by other reason: %s", parse_status(status).c_str()); char status_str[64];
parse_status(status, status_str, sizeof(status_str));
LOGE("stopped by other reason: %s", status_str);
} }
return false; return false;
} }
@@ -205,12 +208,18 @@ bool trace_zygote(int pid) {
ptrace(PTRACE_DETACH, pid, 0, SIGCONT); ptrace(PTRACE_DETACH, pid, 0, SIGCONT);
} }
} else { } else {
LOGE("unknown state %s, not SIGTRAP + EVENT_STOP", parse_status(status).c_str()); char status_str[64];
parse_status(status, status_str, sizeof(status_str));
LOGE("unknown state %s, not SIGTRAP + EVENT_STOP", status_str);
ptrace(PTRACE_DETACH, pid, 0, 0); ptrace(PTRACE_DETACH, pid, 0, 0);
return false; return false;
} }
} else { } else {
LOGE("unknown state %s, not SIGSTOP + EVENT_STOP", parse_status(status).c_str()); char status_str[64];
parse_status(status, status_str, sizeof(status_str));
LOGE("unknown state %s, not SIGSTOP + EVENT_STOP", status_str);
ptrace(PTRACE_DETACH, pid, 0, 0); ptrace(PTRACE_DETACH, pid, 0, 0);
return false; return false;
} }

View File

@@ -359,7 +359,10 @@ uintptr_t remote_call(int pid, struct user_regs_struct &regs, uintptr_t func_add
} }
return regs.REG_RET; return regs.REG_RET;
} else { } else {
LOGE("stopped by other reason %s at addr %p", parse_status(status).c_str(), (void*) regs.REG_IP); char status_str[64];
parse_status(status, status_str, sizeof(status_str));
LOGE("stopped by other reason %s at addr %p", status_str, (void*) regs.REG_IP);
} }
return 0; return 0;
} }
@@ -394,42 +397,45 @@ void wait_for_trace(int pid, int* status, int flags) {
} }
} }
if (!WIFSTOPPED(*status)) { if (!WIFSTOPPED(*status)) {
LOGE("process %d not stopped for trace: %s, exit", pid, parse_status(*status).c_str()); char status_str[64];
parse_status(*status, status_str, sizeof(status_str));
LOGE("process %d not stopped for trace: %s, exit", pid, status_str);
exit(1); exit(1);
} }
return; return;
} }
} }
std::string parse_status(int status) { void parse_status(int status, char *buf, size_t len) {
std::ostringstream os; snprintf(buf, len, "0x%x ", status);
os << "0x" << std::hex << status << std::dec << " "; if (WIFEXITED(status)) {
if (WIFEXITED(status)) { snprintf(buf + strlen(buf), len - strlen(buf), "exited with %d", WEXITSTATUS(status));
os << "exited with " << WEXITSTATUS(status); } else if (WIFSIGNALED(status)) {
} else if (WIFSIGNALED(status)) { snprintf(buf + strlen(buf), len - strlen(buf), "signaled with %s(%d)", sigabbrev_np(WTERMSIG(status)), WTERMSIG(status));
os << "signaled with " << sigabbrev_np(WTERMSIG(status)) << "(" << WTERMSIG(status) << ")"; } else if (WIFSTOPPED(status)) {
} else if (WIFSTOPPED(status)) { snprintf(buf + strlen(buf), len - strlen(buf), "stopped by ");
os << "stopped by "; auto stop_sig = WSTOPSIG(status);
auto stop_sig = WSTOPSIG(status); snprintf(buf + strlen(buf), len - strlen(buf), "signal=%s(%d),", sigabbrev_np(stop_sig), stop_sig);
os << "signal=" << sigabbrev_np(stop_sig) << "(" << stop_sig << "),"; snprintf(buf + strlen(buf), len - strlen(buf), "event=%s", parse_ptrace_event(status));
os << "event=" << parse_ptrace_event(status); } else {
} else { snprintf(buf + strlen(buf), len - strlen(buf), "unknown");
os << "unknown"; }
}
return os.str();
} }
std::string get_program(int pid) { int get_program(int pid, char *buf, size_t size) {
std::string path = "/proc/"; char path[64];
path += std::to_string(pid); snprintf(path, sizeof(path), "/proc/%d/exe", pid);
path += "/exe";
constexpr const auto SIZE = 256; ssize_t sz = readlink(path, buf, size);
char buf[SIZE + 1];
auto sz = readlink(path.c_str(), buf, SIZE); if (sz == -1) {
if (sz == -1) { PLOGE("readlink /proc/%d/exe", pid);
PLOGE("readlink /proc/%d/exe", pid);
return ""; return -1;
} }
buf[sz] = 0;
return buf; buf[sz] = 0;
return 0;
} }

View File

@@ -89,7 +89,7 @@ int fork_dont_care();
void wait_for_trace(int pid, int* status, int flags); void wait_for_trace(int pid, int* status, int flags);
std::string parse_status(int status); void parse_status(int status, char *buf, size_t len);
#define WPTEVENT(x) (x >> 16) #define WPTEVENT(x) (x >> 16)
@@ -116,7 +116,7 @@ inline const char* sigabbrev_np(int sig) {
return "(unknown)"; return "(unknown)";
} }
std::string get_program(int pid); int get_program(int pid, char *buf, size_t size);
void *find_module_return_addr(std::vector<MapInfo> &info, std::string_view suffix); void *find_module_return_addr(std::vector<MapInfo> &info, std::string_view suffix);
// pid = 0, fd != nullptr -> set to fd // pid = 0, fd != nullptr -> set to fd