Prevent Zygisk from closing new fds created by Zygote itself

This commit is contained in:
残页 2023-11-08 16:34:38 +08:00 committed by GitHub
parent a80cadf587
commit ecb31eed40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -48,7 +48,6 @@ bool should_unmap_zygisk = false;
// Current context // Current context
HookContext *g_ctx; HookContext *g_ctx;
bitset<MAX_FD_SIZE> *g_allowed_fds = nullptr;
const JNINativeInterface *old_functions = nullptr; const JNINativeInterface *old_functions = nullptr;
JNINativeInterface *new_functions = nullptr; JNINativeInterface *new_functions = nullptr;
const NativeBridgeRuntimeCallbacks *runtime_callbacks = nullptr; const NativeBridgeRuntimeCallbacks *runtime_callbacks = nullptr;
@ -71,6 +70,7 @@ struct HookContext {
int pid; int pid;
bitset<FLAG_MAX> flags; bitset<FLAG_MAX> flags;
uint32_t info_flags; uint32_t info_flags;
bitset<MAX_FD_SIZE> allowed_fds;
vector<int> exempted_fds; vector<int> exempted_fds;
struct RegisterInfo { struct RegisterInfo {
@ -411,10 +411,14 @@ int sigmask(int how, int signum) {
} }
void HookContext::fork_pre() { void HookContext::fork_pre() {
if (g_allowed_fds == nullptr) { // Do our own fork before loading any 3rd party code
default_new(g_allowed_fds); // First block SIGCHLD, unblock after original fork is done
sigmask(SIG_BLOCK, SIGCHLD);
pid = old_fork();
if (!is_child())
return;
auto &allowed_fds = *g_allowed_fds;
// Record all open fds // Record all open fds
auto dir = xopen_dir("/proc/self/fd"); auto dir = xopen_dir("/proc/self/fd");
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
@ -431,12 +435,6 @@ void HookContext::fork_pre() {
if (int fd = zygisk_get_logd(); fd >= 0) { if (int fd = zygisk_get_logd(); fd >= 0) {
allowed_fds[fd] = false; allowed_fds[fd] = false;
} }
}
// Do our own fork before loading any 3rd party code
// First block SIGCHLD, unblock after original fork is done
sigmask(SIG_BLOCK, SIGCHLD);
pid = old_fork();
} }
void HookContext::fork_post() { void HookContext::fork_post() {
@ -447,11 +445,10 @@ void HookContext::fork_post() {
void HookContext::sanitize_fds() { void HookContext::sanitize_fds() {
zygisk_close_logd(); zygisk_close_logd();
if (!is_child() || g_allowed_fds == nullptr) { if (!is_child()) {
return; return;
} }
auto &allowed_fds = *g_allowed_fds;
if (can_exempt_fd() && !exempted_fds.empty()) { if (can_exempt_fd() && !exempted_fds.empty()) {
auto update_fd_array = [&](int old_len) -> jintArray { auto update_fd_array = [&](int old_len) -> jintArray {
jintArray array = env->NewIntArray(static_cast<int>(old_len + exempted_fds.size())); jintArray array = env->NewIntArray(static_cast<int>(old_len + exempted_fds.size()));