From 20f204810e8243ef5dc24ac8c848221db064ca8d Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 10 Mar 2019 01:14:41 -0500 Subject: [PATCH] Use RAII to detach PIDs --- native/jni/magiskhide/proc_monitor.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 51f90d04d..754885ed1 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -349,6 +349,7 @@ static void new_zygote(int pid) { xptrace(PTRACE_CONT, pid); } +#define DETACH_AND_CONT { detach = true; continue; } void proc_monitor() { inotify_fd = xinotify_init1(IN_CLOEXEC); if (inotify_fd < 0) @@ -384,15 +385,23 @@ void proc_monitor() { int status; for (;;) { - int pid = waitpid(-1, &status, __WALL | __WNOTHREAD); + const int pid = waitpid(-1, &status, __WALL | __WNOTHREAD); if (pid < 0) continue; + bool detach = false; + RunFinally detach_task([&]() -> void { + if (detach) { + // Non of our business now + attaches[pid] = false; + detaches[pid] = false; + unknowns[pid] = false; + xptrace(PTRACE_DETACH, pid); + } + }); if (WIFSTOPPED(status)) { if (detaches[pid]) { PTRACE_LOG("detach\n"); - detaches[pid] = false; - xptrace(PTRACE_DETACH, pid); - continue; + DETACH_AND_CONT; } if (WSTOPSIG(status) == SIGTRAP && WEVENT(status)) { unsigned long msg; @@ -427,10 +436,7 @@ void proc_monitor() { case PTRACE_EVENT_EXEC: case PTRACE_EVENT_EXIT: PTRACE_LOG("exited or execve\n", msg); - attaches[pid] = false; - unknowns[pid] = false; - xptrace(PTRACE_DETACH, pid); - continue; + DETACH_AND_CONT; default: PTRACE_LOG("unknown event: %d\n", WEVENT(status)); break; @@ -454,8 +460,8 @@ void proc_monitor() { } } else { // Nothing to do with us - ptrace(PTRACE_DETACH, pid); PTRACE_LOG("terminate\n"); + DETACH_AND_CONT; } } }