fix injector cannot get tmp path

This commit is contained in:
5ec1cff
2024-01-05 10:37:48 +08:00
parent 043cfd93d6
commit 9a95377d7b
6 changed files with 26 additions and 8 deletions

View File

@@ -7,13 +7,23 @@
#include "socket_utils.h"
namespace zygiskd {
static std::string TMP_PATH;
// TODO: use /sbin or /debug_ramdisk directly
void Init(const char *path) {
TMP_PATH = path;
}
std::string GetTmpPath() {
return TMP_PATH;
}
int Connect(uint8_t retry) {
int fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
struct sockaddr_un addr{
.sun_family = AF_UNIX,
.sun_path={0},
};
auto socket_path = std::string(TMP_PATH) + kCPSocketName;
auto socket_path = TMP_PATH + kCPSocketName;
strcpy(addr.sun_path, socket_path.c_str());
socklen_t socklen = sizeof(addr);

View File

@@ -12,7 +12,6 @@
#endif
constexpr auto kCPSocketName = "/" LP_SELECT("cp32", "cp64") ".sock";
inline static const char* TMP_PATH = getenv("TMP_PATH");
class UniqueFd {
using Fd = int;
@@ -63,6 +62,10 @@ namespace zygiskd {
SystemServerStarted,
};
void Init(const char *path);
std::string GetTmpPath();
bool PingHeartbeat();
int RequestLogcatFd();

View File

@@ -8,9 +8,10 @@ using namespace std;
void *self_handle = nullptr;
extern "C" [[gnu::visibility("default")]]
void entry(void* handle) {
void entry(void* handle, const char* path) {
LOGI("Zygisk library injected, version %s", ZKSU_VERSION);
self_handle = handle;
zygiskd::Init(path);
if (!zygiskd::PingHeartbeat()) {
LOGE("Zygisk daemon is not running");

View File

@@ -14,6 +14,7 @@
using namespace std::string_view_literals;
int main(int argc, char **argv) {
zygiskd::Init(getenv("TMP_PATH"));
if (argc >= 2 && argv[1] == "monitor"sv) {
init_monitor();
return 0;

View File

@@ -136,7 +136,7 @@ struct SocketHandler : public EventHandler {
.sun_family = AF_UNIX,
.sun_path={0},
};
sprintf(addr.sun_path, "%s/%s", TMP_PATH, SOCKET_NAME);
sprintf(addr.sun_path, "%s/%s", zygiskd::GetTmpPath().c_str(), SOCKET_NAME);
socklen_t socklen = sizeof(sa_family_t) + strlen(addr.sun_path);
if (bind(sock_fd_, (struct sockaddr *) &addr, socklen) == -1) {
PLOGE("bind socket");
@@ -544,7 +544,7 @@ static void updateStatus() {
}
static bool prepare_environment() {
prop_path = std::string(TMP_PATH) + "/module.prop";
prop_path = zygiskd::GetTmpPath() + "/module.prop";
close(open(prop_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644));
auto orig_prop = xopen_file("./module.prop", "r");
if (orig_prop == nullptr) {
@@ -595,7 +595,8 @@ void send_control_command(Command cmd) {
.sun_family = AF_UNIX,
.sun_path={0},
};
sprintf(addr.sun_path, "%s/%s", TMP_PATH, SOCKET_NAME);
zygiskd::Init(getenv("TMP_PATH"));
sprintf(addr.sun_path, "%s/%s", zygiskd::GetTmpPath().c_str(), SOCKET_NAME);
socklen_t socklen = sizeof(sa_family_t) + strlen(addr.sun_path);
auto nsend = sendto(sockfd, (void *) &cmd, sizeof(cmd), 0, (sockaddr *) &addr, socklen);
if (nsend == -1) {

View File

@@ -142,9 +142,11 @@ bool inject_on_main(int pid, const char *lib_path) {
return false;
}
// call injector entry(handle, magic)
// call injector entry(handle, path)
args.clear();
args.push_back(remote_handle);
str = push_string(pid, regs, zygiskd::GetTmpPath().c_str());
args.push_back((long) str);
remote_call(pid, regs, injector_entry, (uintptr_t) libc_return_addr, args);
// reset pc to entry
@@ -178,7 +180,7 @@ bool trace_zygote(int pid) {
}
WAIT_OR_DIE
if (STOPPED_WITH(SIGSTOP, PTRACE_EVENT_STOP)) {
std::string lib_path = TMP_PATH;
std::string lib_path = zygiskd::GetTmpPath();
lib_path += "/lib" LP_SELECT("", "64") "/libzygisk.so";
if (!inject_on_main(pid, lib_path.c_str())) {
LOGE("failed to inject");