Skip to content

Commit 0930c8c

Browse files
committed
fix: ReZygiskd Magisk DenyList not checking against process
This commit improves the precision of ReZygiskd check for Magisk if a process is in DenyList/SuList, as previously it used "package_name" instead of the correct "process" field.
1 parent 0c0f659 commit 0930c8c

11 files changed

Lines changed: 47 additions & 34 deletions

File tree

loader/src/common/daemon.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool rezygiskd_ping() {
6464
return true;
6565
}
6666

67-
uint32_t rezygiskd_get_process_flags(uid_t uid) {
67+
uint32_t rezygiskd_get_process_flags(uid_t uid, const char *const process) {
6868
int fd = rezygiskd_connect(1);
6969
if (fd == -1) {
7070
PLOGE("connection to ReZygiskd");
@@ -74,6 +74,7 @@ uint32_t rezygiskd_get_process_flags(uid_t uid) {
7474

7575
write_uint8_t(fd, (uint8_t)GetProcessFlags);
7676
write_uint32_t(fd, (uint32_t)uid);
77+
write_string(fd, process);
7778

7879
uint32_t res = 0;
7980
read_uint32_t(fd, &res);

loader/src/common/socket_utils.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ int read_fd(int fd) {
4545
return sendfd;
4646
}
4747

48+
ssize_t write_string(int fd, const char *str) {
49+
size_t str_len = strlen(str);
50+
ssize_t write_bytes = write(fd, &str_len, sizeof(size_t));
51+
if (write_bytes != (ssize_t)sizeof(size_t)) {
52+
LOGE("Failed to write string length: Not all bytes were written (%zd != %zu).\n", write_bytes, sizeof(size_t));
53+
54+
return -1;
55+
}
56+
57+
write_bytes = write(fd, str, str_len);
58+
if (write_bytes != (ssize_t)str_len) {
59+
LOGE("Failed to write string: Promised bytes doesn't exist (%zd != %zu).\n", write_bytes, str_len);
60+
61+
return -1;
62+
}
63+
64+
return write_bytes;
65+
}
66+
4867
char *read_string(int fd) {
4968
size_t str_len = 0;
5069
ssize_t read_bytes = read(fd, &str_len, sizeof(size_t));

loader/src/include/daemon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int rezygiskd_connect(uint8_t retry);
6363

6464
bool rezygiskd_ping();
6565

66-
uint32_t rezygiskd_get_process_flags(uid_t uid);
66+
uint32_t rezygiskd_get_process_flags(uid_t uid, const char *const process);
6767

6868
void rezygiskd_get_info(struct rezygisk_info *info);
6969

loader/src/include/socket_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <stdint.h>
55

66
int read_fd(int fd);
7+
8+
ssize_t write_string(int fd, const char *str);
79

810
char *read_string(int fd);
911

loader/src/injector/hook.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ void ZygiskContext::run_modules_post() {
677677
void ZygiskContext::app_specialize_pre() {
678678
flags[APP_SPECIALIZE] = true;
679679

680-
info_flags = rezygiskd_get_process_flags(g_ctx->args.app->uid);
680+
info_flags = rezygiskd_get_process_flags(g_ctx->args.app->uid, (const char *const)process);
681681
if (info_flags & PROCESS_IS_FIRST_STARTED) {
682682
/* INFO: To ensure we are really using a clean mount namespace, we use
683683
the first process it as reference for clean mount namespace,

zygiskd/src/constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#define lp_select(a, b) a
1414
#endif
1515

16+
#define PROCESS_NAME_MAX_LEN 256 + 1
17+
1618
#define ZYGOTE_INJECTED lp_select(5, 4)
1719
#define DAEMON_SET_INFO lp_select(7, 6)
1820
#define DAEMON_SET_ERROR_INFO lp_select(9, 8)

zygiskd/src/root_impl/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool uid_granted_root(uid_t uid) {
9494
}
9595
}
9696

97-
bool uid_should_umount(uid_t uid) {
97+
bool uid_should_umount(uid_t uid, const char *const process) {
9898
switch (impl.impl) {
9999
case KernelSU: {
100100
return ksu_uid_should_umount(uid);
@@ -103,7 +103,7 @@ bool uid_should_umount(uid_t uid) {
103103
return apatch_uid_should_umount(uid);
104104
}
105105
case Magisk: {
106-
return magisk_uid_should_umount(uid);
106+
return magisk_uid_should_umount(process);
107107
}
108108
default: {
109109
return false;

zygiskd/src/root_impl/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void get_impl(struct root_impl *uimpl);
3131

3232
bool uid_granted_root(uid_t uid);
3333

34-
bool uid_should_umount(uid_t uid);
34+
bool uid_should_umount(uid_t uid, const char *const process);
3535

3636
bool uid_is_manager(uid_t uid);
3737

zygiskd/src/root_impl/magisk.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,37 +139,17 @@ bool magisk_uid_granted_root(uid_t uid) {
139139
return result[0] != '\0';
140140
}
141141

142-
bool magisk_uid_should_umount(uid_t uid) {
143-
char uid_str[16];
144-
snprintf(uid_str, sizeof(uid_str), "%d", uid);
145-
146-
char *const argv_pm[] = { "pm", "list", "packages", "--uid", uid_str, NULL };
147-
148-
char result[256];
149-
if (!exec_command(result, sizeof(result), "/system/bin/pm", argv_pm)) {
150-
LOGE("Failed to execute pm binary: %s\n", strerror(errno));
151-
errno = 0;
152-
153-
/* INFO: It's better if we do NOT umount than the opposite */
154-
return false;
155-
}
156-
157-
if (result[0] == '\0') {
158-
LOGE("Failed to get package name from UID %d\n", uid);
159-
160-
return false;
161-
}
162-
163-
char *package_name = strtok(result + strlen("package:"), " ");
164-
165-
char sqlite_cmd[256];
142+
bool magisk_uid_should_umount(const char *const process) {
143+
/* INFO: PROCESS_NAME_MAX_LEN already has a +1 for NULL */
144+
char sqlite_cmd[51 + PROCESS_NAME_MAX_LEN];
166145
if (is_using_sulist)
167-
snprintf(sqlite_cmd, sizeof(sqlite_cmd), "select 1 from sulist where package_name=\"%s\" limit 1", package_name);
146+
snprintf(sqlite_cmd, sizeof(sqlite_cmd), "SELECT 1 FROM sulist WHERE process=\"%s\" LIMIT 1", process);
168147
else
169-
snprintf(sqlite_cmd, sizeof(sqlite_cmd), "select 1 from denylist where package_name=\"%s\" limit 1", package_name);
148+
snprintf(sqlite_cmd, sizeof(sqlite_cmd), "SELECT 1 FROM denylist WHERE process=\"%s\" LIMIT 1", process);
170149

171150
char *const argv[] = { "magisk", "--sqlite", sqlite_cmd, NULL };
172151

152+
char result[sizeof("1=1")];
173153
if (!exec_command(result, sizeof(result), (const char *)path_to_magisk, argv)) {
174154
LOGE("Failed to execute magisk binary: %s\n", strerror(errno));
175155
errno = 0;

zygiskd/src/root_impl/magisk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void magisk_get_existence(struct root_impl_state *state);
1212

1313
bool magisk_uid_granted_root(uid_t uid);
1414

15-
bool magisk_uid_should_umount(uid_t uid);
15+
bool magisk_uid_should_umount(const char *const process);
1616

1717
bool magisk_uid_is_manager(uid_t uid);
1818

0 commit comments

Comments
 (0)