From 72b39594d3cf0ddec2fc132d2bf064a75b4a7ee2 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 4 Nov 2023 02:36:14 -0700 Subject: [PATCH] Always close logd_fd during fork --- native/src/core/logging.rs | 2 +- native/src/zygisk/hook.cpp | 25 +++++++------------------ 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/native/src/core/logging.rs b/native/src/core/logging.rs index c50d1f41a..63e0b5b02 100644 --- a/native/src/core/logging.rs +++ b/native/src/core/logging.rs @@ -371,7 +371,7 @@ impl MagiskD { .join(LOG_PIPE!()); unsafe { - libc::mkfifo(path.as_ptr(), 0o200); + libc::mkfifo(path.as_ptr(), 0o666); libc::chown(path.as_ptr(), 0, 0); let read = libc::open(path.as_ptr(), O_RDWR | O_CLOEXEC); let write = libc::open(path.as_ptr(), O_WRONLY | O_CLOEXEC); diff --git a/native/src/zygisk/hook.cpp b/native/src/zygisk/hook.cpp index c2b8e02bf..5d84f0471 100644 --- a/native/src/zygisk/hook.cpp +++ b/native/src/zygisk/hook.cpp @@ -22,8 +22,8 @@ using jni_hook::tree_map; using xstring = jni_hook::string; // Extreme verbose logging -//#define ZLOGV(...) ZLOGD(__VA_ARGS__) -#define ZLOGV(...) (void*)0 +#define ZLOGV(...) ZLOGD(__VA_ARGS__) +//#define ZLOGV(...) (void*)0 static void hook_unloader(); static void unhook_functions(); @@ -170,9 +170,8 @@ DCL_HOOK_FUNC(int, unshare, int flags) { // Close file descriptors to prevent crashing DCL_HOOK_FUNC(void, android_log_close) { if (g_ctx == nullptr || !g_ctx->flags[SKIP_CLOSE_LOG_PIPE]) { - // This may happen during un-managed forks like nativeForkApp and nativeForkUsap, or - // forks that does not allow exemption like nativeForkSystemServer and - // nativeForkAndSpecialize before Android O. + // This happens during forks like nativeForkApp, nativeForkUsap, + // nativeForkSystemServer, and nativeForkAndSpecialize. zygisk_close_logd(); } old_android_log_close(); @@ -438,21 +437,15 @@ void HookContext::fork_post() { } void HookContext::sanitize_fds() { + zygisk_close_logd(); + if (!is_child() || g_allowed_fds == nullptr) { - zygisk_close_logd(); return; } auto &allowed_fds = *g_allowed_fds; - if (can_exempt_fd()) { - if (int fd = zygisk_get_logd(); fd >= 0) { - exempted_fds.push_back(fd); - } - + if (can_exempt_fd() && !exempted_fds.empty()) { auto update_fd_array = [&](int old_len) -> jintArray { - if (exempted_fds.empty()) - return nullptr; - jintArray array = env->NewIntArray(static_cast(old_len + exempted_fds.size())); if (array == nullptr) return nullptr; @@ -465,7 +458,6 @@ void HookContext::sanitize_fds() { } } *args.app->fds_to_ignore = array; - flags[SKIP_CLOSE_LOG_PIPE] = true; return array; }; @@ -485,9 +477,6 @@ void HookContext::sanitize_fds() { } else { update_fd_array(0); } - } else { - zygisk_close_logd(); - android_logging(); } // Close all forbidden fds to prevent crashing