2018-11-01 18:23:12 +01:00
|
|
|
/* proc_monitor.cpp - Monitor am_proc_start events and unmount
|
2017-08-01 09:34:16 +02:00
|
|
|
*
|
2019-02-12 15:12:03 +01:00
|
|
|
* We monitor the listed APK files from /data/app until they get opened
|
|
|
|
* via inotify to detect a new app launch.
|
|
|
|
*
|
|
|
|
* If it's a target we pause it ASAP, and fork a new process to join
|
|
|
|
* its mount namespace and do all the unmounting/mocking.
|
2017-04-06 00:12:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-07-13 16:14:32 +02:00
|
|
|
#include <fcntl.h>
|
2017-04-06 00:12:29 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
2019-03-05 09:22:20 +01:00
|
|
|
#include <sys/ptrace.h>
|
2019-02-12 15:12:03 +01:00
|
|
|
#include <sys/inotify.h>
|
2017-04-06 00:12:29 +02:00
|
|
|
#include <sys/types.h>
|
2017-04-09 01:25:10 +02:00
|
|
|
#include <sys/wait.h>
|
2017-07-10 17:39:33 +02:00
|
|
|
#include <sys/mount.h>
|
2019-03-10 04:28:43 +01:00
|
|
|
#include <vector>
|
2017-04-06 00:12:29 +02:00
|
|
|
|
2019-02-10 07:05:19 +01:00
|
|
|
#include <magisk.h>
|
|
|
|
#include <utils.h>
|
|
|
|
|
2016-12-30 19:44:24 +01:00
|
|
|
#include "magiskhide.h"
|
|
|
|
|
2019-01-20 05:59:37 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2019-02-12 08:14:57 +01:00
|
|
|
extern char *system_block, *vendor_block, *data_block;
|
2017-04-07 01:50:02 +02:00
|
|
|
|
2019-02-13 12:16:26 +01:00
|
|
|
static int inotify_fd = -1;
|
2019-03-05 09:22:20 +01:00
|
|
|
|
|
|
|
static void term_thread(int sig = SIGTERMTHRD);
|
|
|
|
static void new_zygote(int pid);
|
|
|
|
|
2019-03-10 04:28:43 +01:00
|
|
|
/**********************
|
|
|
|
* All data structures
|
|
|
|
**********************/
|
2019-03-05 09:22:20 +01:00
|
|
|
|
2019-03-06 11:40:52 +01:00
|
|
|
set<pair<string, string>> hide_set; /* set of <pkg, process> pair */
|
2019-03-05 09:22:20 +01:00
|
|
|
static map<int, struct stat> zygote_map; /* zygote pid -> mnt ns */
|
|
|
|
static map<int, vector<string_view>> uid_proc_map; /* uid -> list of process */
|
|
|
|
|
|
|
|
pthread_mutex_t monitor_lock;
|
|
|
|
|
2019-03-10 04:28:43 +01:00
|
|
|
#define PID_MAX 32768
|
2019-03-12 21:48:01 +01:00
|
|
|
static vector<bool> attaches(PID_MAX); /* true if pid is monitored */
|
2019-03-10 04:28:43 +01:00
|
|
|
static vector<bool> detaches(PID_MAX); /* true if tid should be detached */
|
2019-03-05 09:22:20 +01:00
|
|
|
|
|
|
|
/********
|
|
|
|
* Utils
|
|
|
|
********/
|
2017-01-01 11:54:13 +01:00
|
|
|
|
2019-02-12 15:12:03 +01:00
|
|
|
static inline int read_ns(const int pid, struct stat *st) {
|
2017-07-02 19:02:11 +02:00
|
|
|
char path[32];
|
2018-06-19 19:20:49 +02:00
|
|
|
sprintf(path, "/proc/%d/ns/mnt", pid);
|
2018-07-11 17:41:38 +02:00
|
|
|
return stat(path, st);
|
2017-07-02 19:02:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-23 20:32:33 +01:00
|
|
|
static inline void lazy_unmount(const char* mountpoint) {
|
2018-01-11 17:23:38 +01:00
|
|
|
if (umount2(mountpoint, MNT_DETACH) != -1)
|
2017-07-10 17:39:33 +02:00
|
|
|
LOGD("hide_daemon: Unmounted (%s)\n", mountpoint);
|
|
|
|
}
|
|
|
|
|
2019-03-09 05:53:53 +01:00
|
|
|
static int parse_ppid(int pid) {
|
|
|
|
char path[32];
|
|
|
|
int ppid;
|
|
|
|
|
|
|
|
sprintf(path, "/proc/%d/stat", pid);
|
|
|
|
FILE *stat = fopen(path, "re");
|
|
|
|
if (stat == nullptr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* PID COMM STATE PPID ..... */
|
|
|
|
fscanf(stat, "%*d %*s %*c %d", &ppid);
|
|
|
|
fclose(stat);
|
|
|
|
|
|
|
|
return ppid;
|
|
|
|
}
|
|
|
|
|
2019-03-05 09:22:20 +01:00
|
|
|
static long xptrace(bool log, int request, pid_t pid, void *addr, void *data) {
|
|
|
|
long ret = ptrace(request, pid, addr, data);
|
|
|
|
if (log && ret == -1)
|
|
|
|
PLOGE("ptrace %d", pid);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long xptrace(int request, pid_t pid, void *addr, void *data) {
|
|
|
|
return xptrace(true, request, pid, addr, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static long xptrace(int request, pid_t pid, void *addr = nullptr, intptr_t data = 0) {
|
|
|
|
return xptrace(true, request, pid, addr, reinterpret_cast<void *>(data));
|
|
|
|
}
|
2019-02-12 15:12:03 +01:00
|
|
|
|
2019-03-06 02:27:09 +01:00
|
|
|
static bool parse_packages_xml(string_view s) {
|
2019-03-05 09:22:20 +01:00
|
|
|
if (!str_starts(s, "<package "))
|
|
|
|
return true;
|
|
|
|
/* <package key1="value1" key2="value2"....> */
|
|
|
|
char *start = (char *) s.data();
|
2019-03-09 10:27:04 +01:00
|
|
|
start[s.length() - 1] = '\0'; /* Remove trailing '>' */
|
2019-03-05 09:22:20 +01:00
|
|
|
start += 9; /* Skip '<package ' */
|
2019-02-12 15:12:03 +01:00
|
|
|
|
2019-03-09 10:27:04 +01:00
|
|
|
string_view pkg;
|
|
|
|
for (char *tok = start; *tok;) {
|
|
|
|
char *eql = strchr(tok, '=');
|
|
|
|
*eql = '\0'; /* Terminate '=' */
|
|
|
|
string_view key(tok, eql - tok);
|
|
|
|
eql += 2; /* Skip '="' */
|
|
|
|
tok = strchr(eql, '\"'); /* Find closing '"' */
|
|
|
|
*tok = '\0';
|
|
|
|
string_view value(eql, tok - eql);
|
|
|
|
tok += 2;
|
|
|
|
if (key == "name") {
|
2019-03-06 11:40:52 +01:00
|
|
|
for (auto &hide : hide_set) {
|
2019-03-09 10:27:04 +01:00
|
|
|
if (hide.first == value) {
|
|
|
|
pkg = hide.first;
|
2019-03-05 09:22:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 10:27:04 +01:00
|
|
|
if (pkg.empty())
|
2019-03-05 09:22:20 +01:00
|
|
|
return true;
|
2019-03-09 10:27:04 +01:00
|
|
|
} else if (key == "userId" || key == "sharedUserId") {
|
2019-03-05 09:22:20 +01:00
|
|
|
int uid = parse_int(value);
|
2019-03-06 11:40:52 +01:00
|
|
|
for (auto &hide : hide_set) {
|
|
|
|
if (hide.first == pkg)
|
|
|
|
uid_proc_map[uid].emplace_back(hide.second);
|
2019-03-05 09:22:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-08 09:35:17 +01:00
|
|
|
static void check_zygote() {
|
2019-03-09 05:53:53 +01:00
|
|
|
int min_zyg = 1;
|
|
|
|
if (access("/system/bin/app_process64", R_OK) == 0)
|
|
|
|
min_zyg = 2;
|
|
|
|
for (bool first = true; zygote_map.size() < min_zyg; first = false) {
|
|
|
|
if (!first)
|
|
|
|
usleep(10000);
|
|
|
|
crawl_procfs([](int pid) -> bool {
|
|
|
|
char buf[512];
|
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
|
|
|
|
FILE *f = fopen(buf, "re");
|
|
|
|
if (f) {
|
|
|
|
fgets(buf, sizeof(buf), f);
|
|
|
|
if (strncmp(buf, "zygote", 6) == 0 && parse_ppid(pid) == 1)
|
|
|
|
new_zygote(pid);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2019-03-08 09:35:17 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 09:22:20 +01:00
|
|
|
void *update_uid_map(void*) {
|
|
|
|
MutexGuard lock(monitor_lock);
|
|
|
|
uid_proc_map.clear();
|
|
|
|
file_readline("/data/system/packages.xml", parse_packages_xml, true);
|
|
|
|
return nullptr;
|
2018-06-19 19:20:49 +02:00
|
|
|
}
|
2019-03-05 09:22:20 +01:00
|
|
|
|
|
|
|
/*************************
|
|
|
|
* The actual hide daemon
|
2019-03-09 05:53:53 +01:00
|
|
|
*************************/
|
2019-02-12 15:12:03 +01:00
|
|
|
|
2018-04-29 09:10:35 +02:00
|
|
|
static void hide_daemon(int pid) {
|
2019-02-19 07:50:41 +01:00
|
|
|
RunFinally fin([=]() -> void {
|
|
|
|
// Send resume signal
|
2019-03-12 21:48:01 +01:00
|
|
|
tgkill(pid, pid, SIGCONT);
|
2019-02-19 07:50:41 +01:00
|
|
|
_exit(0);
|
|
|
|
});
|
|
|
|
|
2017-07-10 17:39:33 +02:00
|
|
|
if (switch_mnt_ns(pid))
|
2019-02-19 07:50:41 +01:00
|
|
|
return;
|
2017-07-10 17:39:33 +02:00
|
|
|
|
2019-02-18 04:30:23 +01:00
|
|
|
LOGD("hide_daemon: handling PID=[%d]\n", pid);
|
|
|
|
manage_selinux();
|
|
|
|
clean_magisk_props();
|
2019-02-19 07:50:41 +01:00
|
|
|
|
|
|
|
vector<string> targets;
|
2017-07-10 17:39:33 +02:00
|
|
|
|
2018-06-16 23:16:52 +02:00
|
|
|
// Unmount dummy skeletons and /sbin links
|
2019-03-06 02:27:09 +01:00
|
|
|
file_readline("/proc/self/mounts", [&](string_view s) -> bool {
|
2019-01-20 05:59:37 +01:00
|
|
|
if (str_contains(s, "tmpfs /system/") || str_contains(s, "tmpfs /vendor/") ||
|
|
|
|
str_contains(s, "tmpfs /sbin")) {
|
2019-02-19 07:50:41 +01:00
|
|
|
char *path = (char *) s.data();
|
|
|
|
// Skip first token
|
|
|
|
strtok_r(nullptr, " ", &path);
|
|
|
|
targets.emplace_back(strtok_r(nullptr, " ", &path));
|
2017-07-10 17:39:33 +02:00
|
|
|
}
|
2019-02-18 04:30:23 +01:00
|
|
|
return true;
|
|
|
|
});
|
2017-07-10 17:39:33 +02:00
|
|
|
|
2019-02-19 07:50:41 +01:00
|
|
|
for (auto &s : targets)
|
|
|
|
lazy_unmount(s.data());
|
|
|
|
targets.clear();
|
|
|
|
|
2019-02-12 08:14:57 +01:00
|
|
|
// Unmount everything under /system, /vendor, and data mounts
|
2019-03-06 02:27:09 +01:00
|
|
|
file_readline("/proc/self/mounts", [&](string_view s) -> bool {
|
2019-01-20 05:59:37 +01:00
|
|
|
if ((str_contains(s, " /system/") || str_contains(s, " /vendor/")) &&
|
2019-02-18 04:30:23 +01:00
|
|
|
(str_contains(s, system_block) || str_contains(s, vendor_block) ||
|
|
|
|
str_contains(s, data_block))) {
|
2019-02-19 07:50:41 +01:00
|
|
|
char *path = (char *) s.data();
|
|
|
|
// Skip first token
|
|
|
|
strtok_r(nullptr, " ", &path);
|
|
|
|
targets.emplace_back(strtok_r(nullptr, " ", &path));
|
2017-07-10 17:39:33 +02:00
|
|
|
}
|
2019-02-18 04:30:23 +01:00
|
|
|
return true;
|
|
|
|
});
|
2017-07-10 17:39:33 +02:00
|
|
|
|
2019-02-19 07:50:41 +01:00
|
|
|
for (auto &s : targets)
|
|
|
|
lazy_unmount(s.data());
|
2017-07-10 17:39:33 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 09:22:20 +01:00
|
|
|
/************************
|
|
|
|
* Async signal handlers
|
|
|
|
************************/
|
|
|
|
|
|
|
|
static void inotify_event(int) {
|
|
|
|
/* Make sure we can actually read stuffs
|
|
|
|
* or else the whole thread will be blocked.*/
|
|
|
|
struct pollfd pfd = {
|
2019-03-06 11:40:52 +01:00
|
|
|
.fd = inotify_fd,
|
|
|
|
.events = POLLIN,
|
|
|
|
.revents = 0
|
2019-03-05 09:22:20 +01:00
|
|
|
};
|
|
|
|
if (poll(&pfd, 1, 0) <= 0)
|
|
|
|
return; // Nothing to read
|
|
|
|
char buf[512];
|
|
|
|
auto event = reinterpret_cast<struct inotify_event *>(buf);
|
|
|
|
read(inotify_fd, buf, sizeof(buf));
|
|
|
|
if ((event->mask & IN_CLOSE_WRITE) && strcmp(event->name, "packages.xml") == 0) {
|
|
|
|
LOGD("proc_monitor: /data/system/packages.xml updated\n");
|
2019-03-09 05:53:53 +01:00
|
|
|
new_daemon_thread(update_uid_map);
|
2019-02-14 06:52:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 09:22:20 +01:00
|
|
|
// Workaround for the lack of pthread_cancel
|
|
|
|
static void term_thread(int) {
|
|
|
|
LOGD("proc_monitor: cleaning up\n");
|
|
|
|
uid_proc_map.clear();
|
|
|
|
zygote_map.clear();
|
2019-03-06 11:40:52 +01:00
|
|
|
hide_set.clear();
|
2019-03-10 04:28:43 +01:00
|
|
|
std::fill(attaches.begin(), attaches.end(), false);
|
|
|
|
std::fill(detaches.begin(), detaches.end(), false);
|
2019-03-05 09:22:20 +01:00
|
|
|
// Misc
|
|
|
|
hide_enabled = false;
|
|
|
|
pthread_mutex_destroy(&monitor_lock);
|
|
|
|
close(inotify_fd);
|
|
|
|
inotify_fd = -1;
|
|
|
|
LOGD("proc_monitor: terminate\n");
|
|
|
|
pthread_exit(nullptr);
|
|
|
|
}
|
2019-03-02 09:44:24 +01:00
|
|
|
|
2019-03-05 09:22:20 +01:00
|
|
|
/******************
|
|
|
|
* Ptrace Madness
|
|
|
|
******************/
|
|
|
|
|
|
|
|
/* Ptrace is super tricky, preserve all excessive debug in code
|
|
|
|
* but disable when actually building for usage (you won't want
|
|
|
|
* your logcat spammed with new thread events from all apps) */
|
|
|
|
|
|
|
|
//#define PTRACE_LOG(fmt, args...) LOGD("PID=[%d] " fmt, pid, ##args)
|
|
|
|
#define PTRACE_LOG(...)
|
|
|
|
|
2019-03-12 21:48:01 +01:00
|
|
|
static void detach_pid(int pid, int signal = 0) {
|
|
|
|
char path[128];
|
|
|
|
xptrace(PTRACE_DETACH, pid, nullptr, signal);
|
|
|
|
|
|
|
|
// Detach all child threads too
|
|
|
|
sprintf(path, "/proc/%d/task", pid);
|
|
|
|
DIR *dir = opendir(path);
|
|
|
|
crawl_procfs(dir, [&](int tid) -> bool {
|
|
|
|
if (tid != pid) {
|
|
|
|
// Check if we should force a SIGSTOP
|
|
|
|
if (waitpid(tid, nullptr, __WALL | __WNOTHREAD | WNOHANG) == tid) {
|
|
|
|
PTRACE_LOG("detach thread [%d]\n", tid);
|
|
|
|
xptrace(PTRACE_DETACH, tid);
|
|
|
|
} else {
|
|
|
|
detaches[tid] = true;
|
|
|
|
tgkill(pid, tid, SIGSTOP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2019-03-05 09:22:20 +01:00
|
|
|
static bool check_pid(int pid) {
|
|
|
|
char path[128];
|
|
|
|
char cmdline[1024];
|
|
|
|
sprintf(path, "/proc/%d/cmdline", pid);
|
|
|
|
FILE *f = fopen(path, "re");
|
|
|
|
// Process killed unexpectedly, ignore
|
|
|
|
if (!f) return true;
|
|
|
|
fgets(cmdline, sizeof(cmdline), f);
|
|
|
|
fclose(f);
|
|
|
|
if (strncmp(cmdline, "zygote", 6) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* This process is fully initialized, we will stop
|
|
|
|
* tracing it no matter if it is a target or not. */
|
2019-03-10 04:28:43 +01:00
|
|
|
attaches[pid] = false;
|
2019-03-05 09:22:20 +01:00
|
|
|
|
|
|
|
sprintf(path, "/proc/%d", pid);
|
|
|
|
struct stat st;
|
|
|
|
lstat(path, &st);
|
|
|
|
int uid = st.st_uid % 100000;
|
|
|
|
auto it = uid_proc_map.find(uid);
|
|
|
|
if (it != uid_proc_map.end()) {
|
|
|
|
for (auto &s : it->second) {
|
|
|
|
if (s == cmdline) {
|
|
|
|
// Double check whether ns is separated
|
|
|
|
read_ns(pid, &st);
|
|
|
|
bool mnt_ns = true;
|
|
|
|
for (auto &zit : zygote_map) {
|
|
|
|
if (zit.second.st_ino == st.st_ino &&
|
|
|
|
zit.second.st_dev == st.st_dev) {
|
|
|
|
mnt_ns = false;
|
2019-02-18 09:05:13 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-02-12 15:12:03 +01:00
|
|
|
}
|
2019-03-05 09:22:20 +01:00
|
|
|
// For some reason ns is not separated, abort
|
|
|
|
if (!mnt_ns)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Finally this is our target!
|
|
|
|
* Detach from ptrace but should still remain stopped.
|
|
|
|
* The hide daemon will resume the process. */
|
|
|
|
PTRACE_LOG("target found\n");
|
2019-03-12 21:48:01 +01:00
|
|
|
LOGI("proc_monitor: [%s] PID=[%d] UID=[%d]\n", cmdline, pid, uid);
|
|
|
|
detach_pid(pid, SIGSTOP);
|
2019-03-05 09:22:20 +01:00
|
|
|
if (fork_dont_care() == 0)
|
|
|
|
hide_daemon(pid);
|
|
|
|
return true;
|
2019-03-01 23:08:08 +01:00
|
|
|
}
|
2019-02-12 15:12:03 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 09:22:20 +01:00
|
|
|
PTRACE_LOG("not our target\n");
|
2019-03-12 21:48:01 +01:00
|
|
|
detach_pid(pid);
|
2019-02-18 09:05:13 +01:00
|
|
|
return true;
|
2019-02-12 15:12:03 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 09:22:20 +01:00
|
|
|
static void new_zygote(int pid) {
|
|
|
|
if (zygote_map.count(pid))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOGD("proc_monitor: ptrace zygote PID=[%d]\n", pid);
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (read_ns(pid, &st))
|
|
|
|
return;
|
|
|
|
zygote_map[pid] = st;
|
|
|
|
|
|
|
|
xptrace(PTRACE_ATTACH, pid);
|
|
|
|
|
|
|
|
waitpid(pid, nullptr, __WALL | __WNOTHREAD);
|
|
|
|
xptrace(PTRACE_SETOPTIONS, pid, nullptr,
|
|
|
|
PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXIT);
|
|
|
|
xptrace(PTRACE_CONT, pid);
|
2019-03-01 23:08:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-10 07:14:41 +01:00
|
|
|
#define DETACH_AND_CONT { detach = true; continue; }
|
2017-04-21 18:54:08 +02:00
|
|
|
void proc_monitor() {
|
2019-03-05 09:22:20 +01:00
|
|
|
inotify_fd = xinotify_init1(IN_CLOEXEC);
|
|
|
|
if (inotify_fd < 0)
|
|
|
|
term_thread();
|
|
|
|
|
|
|
|
// Unblock some signals
|
2017-11-14 22:15:58 +01:00
|
|
|
sigset_t block_set;
|
|
|
|
sigemptyset(&block_set);
|
2019-03-05 09:22:20 +01:00
|
|
|
sigaddset(&block_set, SIGTERMTHRD);
|
|
|
|
sigaddset(&block_set, SIGIO);
|
2019-01-20 05:59:37 +01:00
|
|
|
pthread_sigmask(SIG_UNBLOCK, &block_set, nullptr);
|
2017-11-14 22:15:58 +01:00
|
|
|
|
2019-01-20 05:59:37 +01:00
|
|
|
struct sigaction act{};
|
2017-11-08 20:05:01 +01:00
|
|
|
act.sa_handler = term_thread;
|
2019-03-05 09:22:20 +01:00
|
|
|
sigaction(SIGTERMTHRD, &act, nullptr);
|
|
|
|
act.sa_handler = inotify_event;
|
|
|
|
sigaction(SIGIO, &act, nullptr);
|
|
|
|
|
|
|
|
// Setup inotify asynchronous I/O
|
|
|
|
fcntl(inotify_fd, F_SETFL, O_ASYNC);
|
|
|
|
struct f_owner_ex ex = {
|
|
|
|
.type = F_OWNER_TID,
|
|
|
|
.pid = gettid()
|
|
|
|
};
|
|
|
|
fcntl(inotify_fd, F_SETOWN_EX, &ex);
|
|
|
|
|
|
|
|
// Start monitoring packages.xml
|
|
|
|
inotify_add_watch(inotify_fd, "/data/system", IN_CLOSE_WRITE);
|
|
|
|
|
|
|
|
// First find existing zygotes
|
2019-03-08 09:35:17 +01:00
|
|
|
check_zygote();
|
2017-05-07 21:11:14 +02:00
|
|
|
|
2019-03-05 09:22:20 +01:00
|
|
|
int status;
|
|
|
|
|
|
|
|
for (;;) {
|
2019-03-10 07:14:41 +01:00
|
|
|
const int pid = waitpid(-1, &status, __WALL | __WNOTHREAD);
|
2019-03-05 09:22:20 +01:00
|
|
|
if (pid < 0)
|
2019-02-16 08:49:36 +01:00
|
|
|
continue;
|
2019-03-10 07:14:41 +01:00
|
|
|
bool detach = false;
|
|
|
|
RunFinally detach_task([&]() -> void {
|
|
|
|
if (detach) {
|
|
|
|
// Non of our business now
|
|
|
|
attaches[pid] = false;
|
|
|
|
detaches[pid] = false;
|
2019-03-10 07:46:32 +01:00
|
|
|
ptrace(PTRACE_DETACH, pid, 0, 0);
|
2019-03-10 07:14:41 +01:00
|
|
|
}
|
|
|
|
});
|
2019-03-12 21:48:01 +01:00
|
|
|
if (!WIFSTOPPED(status) || detaches[pid]) {
|
|
|
|
PTRACE_LOG("detached\n");
|
2019-03-11 17:44:50 +01:00
|
|
|
DETACH_AND_CONT;
|
|
|
|
}
|
|
|
|
if (WSTOPSIG(status) == SIGTRAP && WEVENT(status)) {
|
|
|
|
unsigned long msg;
|
|
|
|
xptrace(PTRACE_GETEVENTMSG, pid, nullptr, &msg);
|
|
|
|
if (zygote_map.count(pid)) {
|
|
|
|
// Zygote event
|
|
|
|
switch (WEVENT(status)) {
|
|
|
|
case PTRACE_EVENT_FORK:
|
|
|
|
case PTRACE_EVENT_VFORK:
|
|
|
|
PTRACE_LOG("zygote forked: [%d]\n", msg);
|
|
|
|
attaches[msg] = true;
|
|
|
|
break;
|
|
|
|
case PTRACE_EVENT_EXIT:
|
|
|
|
PTRACE_LOG("zygote exited with status: [%d]\n", msg);
|
|
|
|
zygote_map.erase(pid);
|
|
|
|
DETACH_AND_CONT;
|
|
|
|
default:
|
|
|
|
PTRACE_LOG("unknown event: %d\n", WEVENT(status));
|
|
|
|
break;
|
2019-03-05 09:22:20 +01:00
|
|
|
}
|
2019-03-11 17:44:50 +01:00
|
|
|
xptrace(PTRACE_CONT, pid);
|
|
|
|
} else {
|
|
|
|
switch (WEVENT(status)) {
|
|
|
|
case PTRACE_EVENT_CLONE:
|
|
|
|
PTRACE_LOG("create new threads: [%d]\n", msg);
|
|
|
|
if (attaches[pid] && check_pid(pid))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case PTRACE_EVENT_EXEC:
|
|
|
|
case PTRACE_EVENT_EXIT:
|
|
|
|
PTRACE_LOG("exited or execve\n");
|
|
|
|
DETACH_AND_CONT;
|
|
|
|
default:
|
|
|
|
PTRACE_LOG("unknown event: %d\n", WEVENT(status));
|
|
|
|
break;
|
2019-03-05 09:22:20 +01:00
|
|
|
}
|
|
|
|
xptrace(PTRACE_CONT, pid);
|
2019-03-11 17:44:50 +01:00
|
|
|
}
|
|
|
|
} else if (WSTOPSIG(status) == SIGSTOP) {
|
2019-03-12 21:48:01 +01:00
|
|
|
PTRACE_LOG("SIGSTOP from child\n");
|
|
|
|
xptrace(PTRACE_SETOPTIONS, pid, nullptr,
|
|
|
|
PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC | PTRACE_O_TRACEEXIT);
|
2019-03-11 17:44:50 +01:00
|
|
|
xptrace(PTRACE_CONT, pid);
|
2019-03-05 09:22:20 +01:00
|
|
|
} else {
|
2019-03-11 17:44:50 +01:00
|
|
|
// Not caused by us, resend signal
|
|
|
|
xptrace(PTRACE_CONT, pid, nullptr, WSTOPSIG(status));
|
|
|
|
PTRACE_LOG("signal [%d]\n", WSTOPSIG(status));
|
2019-02-12 15:12:03 +01:00
|
|
|
}
|
2017-06-02 22:31:01 +02:00
|
|
|
}
|
2017-03-24 20:45:12 +01:00
|
|
|
}
|