Skip to content

Commit a549f0e

Browse files
committed
add: Magisk support; fix: (some) zygiskd code issues
This commit adds Magisk support to Zygiskd C99, and also fixes some code issues of it.
1 parent c1e45e9 commit a549f0e

14 files changed

Lines changed: 520 additions & 126 deletions

File tree

zygiskd/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ val Files = arrayOf(
5353
"root_impl/apatch.c",
5454
"root_impl/common.c",
5555
"root_impl/kernelsu.c",
56+
"root_impl/magisk.c",
5657
"companion.c",
5758
"dl.c",
5859
"main.c",

zygiskd/src/companion.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ zygisk_companion_entry_func load_module(int fd) {
3030
return (zygisk_companion_entry_func)entry;
3131
}
3232

33-
void *call_entry(void *arg) {
33+
void *call_entry(void *restrict arg) {
3434
int fd = *((int *)arg);
3535

3636
struct stat st0;
@@ -92,7 +92,7 @@ void entry(int fd) {
9292
if (entry == NULL) {
9393
LOGI("No companion entry for: %s\n", name);
9494

95-
uint8_t response[1] = { 0 };
95+
uint8_t response = 0;
9696
write(fd, &response, sizeof(response));
9797

9898
exit(0);
@@ -102,7 +102,7 @@ void entry(int fd) {
102102

103103
LOGI("Companion process created for: %s\n", name);
104104

105-
uint8_t response[1] = { 1 };
105+
uint8_t response = 1;
106106
write(fd, &response, sizeof(response));
107107

108108
while (1) {
@@ -114,21 +114,17 @@ void entry(int fd) {
114114
break;
115115
}
116116

117-
int client_fd;
118-
recv_fd(fd, &client_fd);
117+
int *client_fd = malloc(sizeof(int));
118+
recv_fd(fd, client_fd);
119119

120-
LOGI("New companion request from module \"%s\" with fd \"%d\"\n", name, client_fd);
120+
LOGI("New companion request from module \"%s\" with fd \"%d\"\n", name, *client_fd);
121121

122122
write(fd, &response, sizeof(response));
123-
124-
/* TODO: Do we really need to allocate this..? */
125-
int *client_fd_ptr = malloc(sizeof(int));
126-
*client_fd_ptr = client_fd;
127123

128124
LOGI("Creating new thread for companion request\n");
129125

130126
pthread_t thread;
131-
pthread_create(&thread, NULL, call_entry, (void *)client_fd_ptr);
127+
pthread_create(&thread, NULL, call_entry, (void *)client_fd);
132128
pthread_detach(thread);
133129
}
134130
}

zygiskd/src/dl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct AndroidDlextinfo {
2323

2424
void *android_dlopen_ext(const char *filename, int flags, const struct AndroidDlextinfo *extinfo);
2525

26-
void *android_dlopen(char *path, u_int32_t flags) {
26+
void *android_dlopen(char *restrict path, uint32_t flags) {
2727
char *dir = dirname(path);
2828
struct AndroidDlextinfo info = {
2929
.flags = 0,

zygiskd/src/dl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef DL_H
22
#define DL_H
33

4-
void *android_dlopen(char *path, u_int32_t flags);
4+
void *android_dlopen(char *restrict path, u_int32_t flags);
55

66
#endif /* DL_H */

zygiskd/src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "utils.h"
1313

14-
int __android_log_print(int prio, const char *tag, const char *fmt, ...);
14+
int __android_log_print(int prio, const char *tag, const char *fmt, ...);
1515

1616
int main(int argc, char *argv[]) {
1717
LOGI("Initializing zygiskd: %s\n", argv[0]);
@@ -61,6 +61,11 @@ int main(int argc, char *argv[]) {
6161
case APatch: {
6262
LOGI("APatch root implementation found.\n");
6363

64+
return 0;
65+
}
66+
case Magisk: {
67+
LOGI("Magisk root implementation found.\n");
68+
6469
return 0;
6570
}
6671
}

zygiskd/src/root_impl/apatch.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,25 @@
1212
enum RootImplState apatch_get_existence(void) {
1313
struct stat s;
1414
if (stat("/data/adb/apd", &s) != 0) {
15-
LOGE("APATCH | Failed to stat /data/adb/apd: %s\n", strerror(errno));
15+
if (errno != ENOENT) LOGE("Failed to stat APatch apd binary: %s\n", strerror(errno));
16+
errno = 0;
1617

1718
return Inexistent;
1819
}
1920

2021
char apatch_version[32];
2122
char *const argv[] = { "apd", "-V", NULL };
2223

23-
LOGI("APATCH | Checking for apd existence\n");
2424
if (!exec_command(apatch_version, sizeof(apatch_version), "/data/adb/apd", argv)) {
25-
LOGE("APATCH | Failed to execute apd binary: %s\n", strerror(errno));
25+
LOGE("Failed to execute apd binary: %s\n", strerror(errno));
2626
errno = 0;
2727

2828
return Inexistent;
2929
}
3030

3131
int version = atoi(apatch_version + strlen("apd "));
32-
LOGI("APATCH | apd version: %d\n", version);
33-
if (version == 0) return Abnormal;
3432

33+
if (version == 0) return Abnormal;
3534
if (version >= MIN_APATCH_VERSION && version <= 999999) return Supported;
3635
if (version >= 1 && version <= MIN_APATCH_VERSION - 1) return TooOld;
3736

@@ -49,38 +48,54 @@ struct packages_config {
4948
size_t size;
5049
};
5150

52-
bool _apatch_get_package_config(struct packages_config *config) {
51+
/* WARNING: Dynamic memory based */
52+
bool _apatch_get_package_config(struct packages_config *restrict config) {
5353
FILE *fp = fopen("/data/adb/ap/package_config", "r");
5454
if (fp == NULL) {
55-
LOGE("APATCH | Failed to open package_config: %s\n", strerror(errno));
55+
LOGE("Failed to open APatch's package_config: %s\n", strerror(errno));
5656

5757
return false;
5858
}
5959

60-
char line[256];
60+
char line[1024];
6161
/* INFO: Skip the CSV header */
6262
fgets(line, sizeof(line), fp);
6363

64+
LOGI("meow meow: %s\n", line);
65+
66+
config->size = 0;
6467
while (fgets(line, sizeof(line), fp) != NULL) {
65-
config->configs = realloc(config, (config->size + 1) * sizeof(struct package_config));
68+
LOGI("meow meow\n");
69+
70+
config->configs = realloc(config->configs, (config->size + 1) * sizeof(struct package_config));
6671
if (config->configs == NULL) {
67-
LOGE("APATCH | Failed to realloc package config: %s\n", strerror(errno));
72+
LOGE("Failed to realloc APatch config struct: %s\n", strerror(errno));
6873

6974
fclose(fp);
7075

7176
return false;
7277
}
7378

79+
LOGI("meow meow (1): %s\n", line);
80+
7481
strtok(line, ",");
7582

83+
LOGI("meow meow (2)\n");
84+
7685
char *exclude_str = strtok(NULL, ",");
7786
if (exclude_str == NULL) continue;
7887

88+
LOGI("meow meow: %s\n", exclude_str);
89+
7990
char *allow_str = strtok(NULL, ",");
8091
if (allow_str == NULL) continue;
8192

93+
LOGI("meow meow: %s\n", allow_str);
94+
8295
char *uid_str = strtok(NULL, ",");
83-
if (uid_str == NULL) continue;
96+
if (uid_str == NULL) continue;
97+
98+
LOGI("meow meow: %s\n", uid_str);
8499

85100
config->configs[config->size].uid = atoi(uid_str);
86101
config->configs[config->size].root_granted = strcmp(allow_str, "1") == 0;
@@ -138,7 +153,12 @@ bool apatch_uid_should_umount(uid_t uid) {
138153

139154
bool apatch_uid_is_manager(uid_t uid) {
140155
struct stat s;
141-
if (stat("/data/user_de/0/me.bmax.apatch", &s) == -1) return false;
156+
if (stat("/data/user_de/0/me.bmax.apatch", &s) == -1) {
157+
if (errno != ENOENT) LOGE("Failed to stat APatch manager data directory: %s\n", strerror(errno));
158+
errno = 0;
159+
160+
return false;
161+
}
142162

143163
return s.st_uid == uid;
144164
}

zygiskd/src/root_impl/common.c

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
#include <stdio.h>
2+
13
#include <sys/types.h>
24

5+
#include "../utils.h"
36
#include "kernelsu.h"
47
#include "apatch.h"
8+
#include "magisk.h"
59

610
#include "common.h"
711

@@ -10,51 +14,89 @@ static enum RootImpl ROOT_IMPL = None;
1014
void root_impls_setup(void) {
1115
if (ksu_get_existence() == Supported) ROOT_IMPL = KernelSU;
1216
else if (apatch_get_existence() == Supported) ROOT_IMPL = APatch;
17+
else if (magisk_get_existence() == Supported) ROOT_IMPL = Magisk;
1318
else ROOT_IMPL = None;
19+
20+
switch (ROOT_IMPL) {
21+
case None: {
22+
LOGI("No root implementation found.\n");
23+
24+
break;
25+
}
26+
case Multiple: {
27+
LOGI("Multiple root implementations found.\n");
28+
29+
break;
30+
}
31+
case KernelSU: {
32+
LOGI("KernelSU root implementation found.\n");
33+
34+
break;
35+
}
36+
case APatch: {
37+
LOGI("APatch root implementation found.\n");
38+
39+
break;
40+
}
41+
case Magisk: {
42+
LOGI("Magisk root implementation found.\n");
43+
44+
break;
45+
}
46+
}
1447
}
1548

1649
enum RootImpl get_impl(void) {
1750
return ROOT_IMPL;
1851
}
1952

2053
bool uid_granted_root(uid_t uid) {
21-
// switch (get_impl()) {
22-
// case KernelSU: {
54+
switch (get_impl()) {
55+
case KernelSU: {
2356
return ksu_uid_granted_root(uid);
24-
// }
25-
// case APatch: {
26-
// return apatch_uid_granted_root(uid);
27-
// }
28-
// default: {
29-
// return false;
30-
// }
31-
// }
57+
}
58+
case APatch: {
59+
return apatch_uid_granted_root(uid);
60+
}
61+
case Magisk: {
62+
return magisk_uid_granted_root(uid);
63+
}
64+
default: {
65+
return false;
66+
}
67+
}
3268
}
3369

3470
bool uid_should_umount(uid_t uid) {
35-
// switch (get_impl()) {
36-
// case KernelSU: {
71+
switch (get_impl()) {
72+
case KernelSU: {
3773
return ksu_uid_should_umount(uid);
38-
// }
39-
// case APatch: {
40-
// return apatch_uid_should_umount(uid);
41-
// }
42-
// default: {
43-
// return false;
44-
// }
45-
// }
74+
}
75+
case APatch: {
76+
return apatch_uid_should_umount(uid);
77+
}
78+
case Magisk: {
79+
return magisk_uid_should_umount(uid);
80+
}
81+
default: {
82+
return false;
83+
}
84+
}
4685
}
4786

4887
bool uid_is_manager(uid_t uid) {
49-
// switch (get_impl()) {
50-
// case KernelSU: {
88+
switch (get_impl()) {
89+
case KernelSU: {
5190
return ksu_uid_is_manager(uid);
52-
// }
53-
// case APatch: {
54-
// return apatch_uid_is_manager(uid);
55-
// }
56-
// default: {
57-
// return false;
58-
// }
59-
// }
91+
}
92+
case APatch: {
93+
return apatch_uid_is_manager(uid);
94+
}
95+
case Magisk: {
96+
return magisk_uid_is_manager(uid);
97+
}
98+
default: {
99+
return false;
100+
}
101+
}
60102
}

zygiskd/src/root_impl/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ enum RootImpl {
77
None,
88
Multiple,
99
KernelSU,
10-
APatch
10+
APatch,
11+
Magisk
1112
};
1213

1314
void root_impls_setup(void);

zygiskd/src/root_impl/kernelsu.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ bool ksu_uid_should_umount(uid_t uid) {
4848

4949
bool ksu_uid_is_manager(uid_t uid) {
5050
struct stat s;
51-
if (stat("/data/user_de/0/me.weishu.kernelsu", &s) == -1) return false;
51+
if (stat("/data/user_de/0/me.weishu.kernelsu", &s) == -1) {
52+
if (errno != ENOENT) LOGE("Failed to stat KSU manager data directory: %s\n", strerror(errno));
53+
errno = 0;
54+
55+
return false;
56+
}
5257

5358
return s.st_uid == uid;
5459
}

0 commit comments

Comments
 (0)