From ef40c1212ebae4cbc43cdcb0cd5203e0ef4af6f4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 13 Nov 2018 02:11:02 -0500 Subject: [PATCH] Prevent infinite loop if process is killed Close #761 --- native/jni/magiskhide/proc_monitor.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 10179b3d2..d54780251 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -53,6 +53,8 @@ static int parse_ppid(int pid) { int fd, ppid; sprintf(path, "/proc/%d/stat", pid); fd = xopen(path, O_RDONLY); + if (fd < 0) + return -1; xread(fd, stat, sizeof(stat)); close(fd); /* PID COMM STATE PPID ..... */ @@ -164,13 +166,13 @@ void proc_monitor() { } } pthread_mutex_unlock(&list_lock); - if (!hide) + + if (!hide || (ppid = parse_ppid(pid)) < 0 || read_ns(ppid, &pns) == -1) continue; - ppid = parse_ppid(pid); - read_ns(ppid, &pns); do { - read_ns(pid, &ns); + if (read_ns(pid, &ns) == -1) + break; if (ns.st_dev == pns.st_dev && ns.st_ino == pns.st_ino) usleep(50); else