fix: Zygiskd remote read types

This commit fixes the mismatch between types sent by lizygisk.so and read by Zygiskd, causing issues.
This commit is contained in:
ThePedroo
2024-12-21 16:46:36 -03:00
parent 95073d9f4a
commit 6b0b71a690
8 changed files with 68 additions and 47 deletions

View File

@@ -30,7 +30,7 @@ struct Module {
struct Context {
struct Module *modules;
int len;
size_t len;
};
enum Architecture {
@@ -85,14 +85,14 @@ int create_library_fd(const char *restrict so_path) {
/* INFO: This is required as older implementations of glibc may not
have the memfd_create function call, causing a crash. */
int memfd = syscall(SYS_memfd_create, "jit-cache-zygisk", MFD_ALLOW_SEALING);
int memfd = (int)syscall(SYS_memfd_create, "jit-cache-zygisk", MFD_ALLOW_SEALING);
if (memfd == -1) {
LOGE("Failed creating memfd: %s\n", strerror(errno));
return -1;
}
if (sendfile(memfd, so_fd, NULL, so_size) == -1) {
if (sendfile(memfd, so_fd, NULL, (size_t)so_size) == -1) {
LOGE("Failed copying so file to memfd: %s\n", strerror(errno));
close(so_fd);
@@ -174,7 +174,7 @@ static void load_modules(enum Architecture arch, struct Context *restrict contex
}
context->modules = realloc(context->modules, ((context->len + 1) * sizeof(struct Module)));
context->modules = realloc(context->modules, (size_t)((context->len + 1) * sizeof(struct Module)));
if (context->modules == NULL) {
LOGE("Failed reallocating memory for modules.\n");
@@ -189,7 +189,7 @@ static void load_modules(enum Architecture arch, struct Context *restrict contex
}
static void free_modules(struct Context *restrict context) {
for (int i = 0; i < context->len; i++) {
for (size_t i = 0; i < context->len; i++) {
free(context->modules[i].name);
if (context->modules[i].companion != -1) close(context->modules[i].companion);
}
@@ -344,7 +344,7 @@ void zygiskd_start(char *restrict argv[]) {
msg->length = sprintf(msg->data, "Unsupported environment: Multiple root implementations found");
}
unix_datagram_sendto(CONTROLLER_SOCKET, (void *)msg, sizeof(struct MsgHead) + msg->length);
unix_datagram_sendto(CONTROLLER_SOCKET, (void *)msg, (size_t)((int)sizeof(struct MsgHead) + msg->length));
free(msg);
} else {
@@ -357,7 +357,7 @@ void zygiskd_start(char *restrict argv[]) {
module_list = strdup("None");
module_list_len = strlen("None");
} else {
for (int i = 0; i < context.len; i++) {
for (size_t i = 0; i < context.len; i++) {
if (i != context.len - 1) {
module_list = realloc(module_list, module_list_len + strlen(context.modules[i].name) + strlen(", ") + 1);
if (module_list == NULL) {
@@ -397,7 +397,7 @@ void zygiskd_start(char *restrict argv[]) {
msg->length = snprintf(msg->data, msg_length, "Root: %s, Modules: %s", impl_name, module_list) + 1;
msg->cmd = DAEMON_SET_INFO;
unix_datagram_sendto(CONTROLLER_SOCKET, (void *)msg, sizeof(struct MsgHead) + msg->length);
unix_datagram_sendto(CONTROLLER_SOCKET, (void *)msg, (size_t)((int)sizeof(struct MsgHead) + msg->length));
free(msg);
free(module_list);
@@ -440,7 +440,7 @@ void zygiskd_start(char *restrict argv[]) {
break;
}
case ZygoteRestart: {
for (int i = 0; i < context.len; i++) {
for (size_t i = 0; i < context.len; i++) {
if (context.modules[i].companion != -1) {
close(context.modules[i].companion);
context.modules[i].companion = -1;
@@ -534,7 +534,7 @@ void zygiskd_start(char *restrict argv[]) {
}
}
ret = write_int(client_fd, flags);
ret = write_uint32_t(client_fd, flags);
ASSURE_SIZE_WRITE_BREAK("GetProcessFlags", "flags", ret, sizeof(flags));
break;
@@ -562,10 +562,11 @@ void zygiskd_start(char *restrict argv[]) {
}
}
ssize_t ret = write_size_t(client_fd, flags);
ssize_t ret = write_uint32_t(client_fd, flags);
ASSURE_SIZE_WRITE_BREAK("GetInfo", "flags", ret, sizeof(flags));
uint32_t pid = getpid();
/* TODO: Use pid_t */
uint32_t pid = (uint32_t)getpid();
ret = write_uint32_t(client_fd, pid);
ASSURE_SIZE_WRITE_BREAK("GetInfo", "pid", ret, sizeof(pid));
@@ -612,10 +613,8 @@ void zygiskd_start(char *restrict argv[]) {
struct Module *module = &context.modules[index];
if (module->companion != -1) {
LOGI(" - Polling companion for module \"%s\"\n", module->name);
if (!check_unix_socket(module->companion, false)) {
LOGE(" - Poll companion for module \"%s\" crashed\n", module->name);
LOGE(" - Companion for module \"%s\" crashed\n", module->name);
close(module->companion);
module->companion = -1;
@@ -626,7 +625,7 @@ void zygiskd_start(char *restrict argv[]) {
module->companion = spawn_companion(argv, module->name, module->lib_fd);
if (module->companion > 0) {
LOGI(" - Spawned companion for \"%s\"\n", module->name);
LOGI(" - Spawned companion for \"%s\": %d\n", module->name, module->companion);
} else {
if (module->companion == -2) {
LOGE(" - No companion spawned for \"%s\" because it has no entry.\n", module->name);
@@ -654,14 +653,16 @@ void zygiskd_start(char *restrict argv[]) {
close(module->companion);
module->companion = -1;
/* INFO: RequestCompanionSocket by defailt doesn't close the client_fd */
/* INFO: RequestCompanionSocket by default doesn't close the client_fd */
close(client_fd);
}
} else {
LOGE(" - Failed to spawn companion for module \"%s\"\n", module->name);
ret = write_uint8_t(client_fd, 0);
ASSURE_SIZE_WRITE_BREAK("RequestCompanionSocket", "response", ret, sizeof(int));
/* INFO: RequestCompanionSocket by defailt doesn't close the client_fd */
/* INFO: RequestCompanionSocket by default doesn't close the client_fd */
close(client_fd);
}