Always close logd_fd during fork

This commit is contained in:
topjohnwu 2023-11-04 02:36:14 -07:00
parent 16ae4aedf1
commit 72b39594d3
2 changed files with 8 additions and 19 deletions

View File

@ -371,7 +371,7 @@ impl MagiskD {
.join(LOG_PIPE!()); .join(LOG_PIPE!());
unsafe { unsafe {
libc::mkfifo(path.as_ptr(), 0o200); libc::mkfifo(path.as_ptr(), 0o666);
libc::chown(path.as_ptr(), 0, 0); libc::chown(path.as_ptr(), 0, 0);
let read = libc::open(path.as_ptr(), O_RDWR | O_CLOEXEC); let read = libc::open(path.as_ptr(), O_RDWR | O_CLOEXEC);
let write = libc::open(path.as_ptr(), O_WRONLY | O_CLOEXEC); let write = libc::open(path.as_ptr(), O_WRONLY | O_CLOEXEC);

View File

@ -22,8 +22,8 @@ using jni_hook::tree_map;
using xstring = jni_hook::string; using xstring = jni_hook::string;
// Extreme verbose logging // Extreme verbose logging
//#define ZLOGV(...) ZLOGD(__VA_ARGS__) #define ZLOGV(...) ZLOGD(__VA_ARGS__)
#define ZLOGV(...) (void*)0 //#define ZLOGV(...) (void*)0
static void hook_unloader(); static void hook_unloader();
static void unhook_functions(); static void unhook_functions();
@ -170,9 +170,8 @@ DCL_HOOK_FUNC(int, unshare, int flags) {
// Close file descriptors to prevent crashing // Close file descriptors to prevent crashing
DCL_HOOK_FUNC(void, android_log_close) { DCL_HOOK_FUNC(void, android_log_close) {
if (g_ctx == nullptr || !g_ctx->flags[SKIP_CLOSE_LOG_PIPE]) { if (g_ctx == nullptr || !g_ctx->flags[SKIP_CLOSE_LOG_PIPE]) {
// This may happen during un-managed forks like nativeForkApp and nativeForkUsap, or // This happens during forks like nativeForkApp, nativeForkUsap,
// forks that does not allow exemption like nativeForkSystemServer and // nativeForkSystemServer, and nativeForkAndSpecialize.
// nativeForkAndSpecialize before Android O.
zygisk_close_logd(); zygisk_close_logd();
} }
old_android_log_close(); old_android_log_close();
@ -438,21 +437,15 @@ void HookContext::fork_post() {
} }
void HookContext::sanitize_fds() { void HookContext::sanitize_fds() {
zygisk_close_logd();
if (!is_child() || g_allowed_fds == nullptr) { if (!is_child() || g_allowed_fds == nullptr) {
zygisk_close_logd();
return; return;
} }
auto &allowed_fds = *g_allowed_fds; auto &allowed_fds = *g_allowed_fds;
if (can_exempt_fd()) { if (can_exempt_fd() && !exempted_fds.empty()) {
if (int fd = zygisk_get_logd(); fd >= 0) {
exempted_fds.push_back(fd);
}
auto update_fd_array = [&](int old_len) -> jintArray { auto update_fd_array = [&](int old_len) -> jintArray {
if (exempted_fds.empty())
return nullptr;
jintArray array = env->NewIntArray(static_cast<int>(old_len + exempted_fds.size())); jintArray array = env->NewIntArray(static_cast<int>(old_len + exempted_fds.size()));
if (array == nullptr) if (array == nullptr)
return nullptr; return nullptr;
@ -465,7 +458,6 @@ void HookContext::sanitize_fds() {
} }
} }
*args.app->fds_to_ignore = array; *args.app->fds_to_ignore = array;
flags[SKIP_CLOSE_LOG_PIPE] = true;
return array; return array;
}; };
@ -485,9 +477,6 @@ void HookContext::sanitize_fds() {
} else { } else {
update_fd_array(0); update_fd_array(0);
} }
} else {
zygisk_close_logd();
android_logging();
} }
// Close all forbidden fds to prevent crashing // Close all forbidden fds to prevent crashing