Kill all processes using the same UID of the target

To workaround OOS embryo optimization
This commit is contained in:
topjohnwu 2018-08-08 05:47:58 +08:00
parent 3e3f38500d
commit 7b5d79d313
3 changed files with 42 additions and 32 deletions

View File

@ -95,7 +95,7 @@ ssize_t fdgets(char *buf, size_t size, int fd);
ssize_t my_getline(char **lineptr, size_t *n, FILE *stream); ssize_t my_getline(char **lineptr, size_t *n, FILE *stream);
ssize_t my_getdelim(char **lineptr, size_t *n, int delim, FILE *stream); ssize_t my_getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
void ps(void (*func)(int)); void ps(void (*func)(int));
void ps_filter_proc_name(const char *filter, void (*func)(int)); int check_proc_name(int pid, const char *filter);
void unlock_blocks(); void unlock_blocks();
void setup_sighandlers(void (*handler)(int)); void setup_sighandlers(void (*handler)(int));
int exec_array(int err, int *fd, void (*setenv)(struct vector *), char *const *argv); int exec_array(int err, int *fd, void (*setenv)(struct vector *), char *const *argv);

View File

@ -27,6 +27,9 @@ static char *prop_value[] =
"0", "0", "0", "1", "0", "0", "0", "1",
"user", "release-keys", "0", NULL }; "user", "release-keys", "0", NULL };
static const char *proc_name;
static gid_t proc_gid;
void manage_selinux() { void manage_selinux() {
char val; char val;
int fd = xopen(SELINUX_ENFORCE, O_RDONLY); int fd = xopen(SELINUX_ENFORCE, O_RDONLY);
@ -60,8 +63,30 @@ static void rm_magisk_prop(const char *name, const char *value, void *v) {
} }
} }
static void kill_proc(int pid) { static void kill_proc_cb(int pid) {
if (check_proc_name(pid, proc_name))
kill(pid, SIGTERM); kill(pid, SIGTERM);
else if (proc_gid > 0) {
char buf[128];
struct stat st;
sprintf(buf, "/proc/%d", pid);
stat(buf, &st);
if (proc_gid == st.st_gid)
kill(pid, SIGTERM);
}
}
static void kill_process(const char *name) {
proc_name = name;
char buf[128];
struct stat st;
sprintf(buf, "/data/data/%s", name);
if (stat(buf, &st) == 0)
proc_gid = st.st_gid;
else
proc_gid = 0;
ps(kill_proc_cb);
} }
void clean_magisk_props() { void clean_magisk_props() {
@ -94,7 +119,7 @@ int add_list(char *proc) {
vec_push_back(new_list, proc); vec_push_back(new_list, proc);
LOGI("hide_list add: [%s]\n", proc); LOGI("hide_list add: [%s]\n", proc);
ps_filter_proc_name(proc, kill_proc); kill_process(proc);
// Critical region // Critical region
pthread_mutex_lock(&hide_lock); pthread_mutex_lock(&hide_lock);
@ -135,7 +160,7 @@ int rm_list(char *proc) {
if (do_rm) { if (do_rm) {
LOGI("hide_list rm: [%s]\n", proc); LOGI("hide_list rm: [%s]\n", proc);
ps_filter_proc_name(proc, kill_proc); kill_process(proc);
// Critical region // Critical region
pthread_mutex_lock(&hide_lock); pthread_mutex_lock(&hide_lock);
vec_destroy(hide_list); vec_destroy(hide_list);
@ -170,7 +195,7 @@ int init_list() {
char *line; char *line;
vec_for_each(hide_list, line) { vec_for_each(hide_list, line) {
LOGI("hide_list: [%s]\n", line); LOGI("hide_list: [%s]\n", line);
ps_filter_proc_name(line, kill_proc); kill_process(line);
} }
return 0; return 0;
} }
@ -178,7 +203,7 @@ int init_list() {
int destroy_list() { int destroy_list() {
char *line; char *line;
vec_for_each(hide_list, line) { vec_for_each(hide_list, line) {
ps_filter_proc_name(line, kill_proc); kill_process(line);
} }
vec_deep_destroy(hide_list); vec_deep_destroy(hide_list);
free(hide_list); free(hide_list);

View File

@ -240,49 +240,34 @@ void ps(void (*func)(int)) {
closedir(dir); closedir(dir);
} }
// Internal usage int check_proc_name(int pid, const char *name) {
static void (*ps_filter_cb)(int);
static const char *ps_filter_pattern;
static void proc_name_filter(int pid) {
char buf[128]; char buf[128];
FILE *f; FILE *f;
sprintf(buf, "/proc/%d/comm", pid); sprintf(buf, "/proc/%d/comm", pid);
if ((f = fopen(buf, "r"))) { if ((f = fopen(buf, "r"))) {
fgets(buf, sizeof(buf), f); fgets(buf, sizeof(buf), f);
if (strcmp(buf, ps_filter_pattern) == 0) if (strcmp(buf, name) == 0)
goto run_cb; return 1;
} else { } else {
// The PID is already killed // The PID is already killed
return; return 0;
} }
fclose(f); fclose(f);
sprintf(buf, "/proc/%d/cmdline", pid); sprintf(buf, "/proc/%d/cmdline", pid);
f = fopen(buf, "r"); f = fopen(buf, "r");
fgets(buf, sizeof(buf), f); fgets(buf, sizeof(buf), f);
if (strcmp(basename(buf), ps_filter_pattern) == 0) if (strcmp(basename(buf), name) == 0)
goto run_cb; return 1;
fclose(f); fclose(f);
sprintf(buf, "/proc/%d/exe", pid); sprintf(buf, "/proc/%d/exe", pid);
if (access(buf, F_OK) != 0) if (access(buf, F_OK) != 0)
return; return 0;
xreadlink(buf, buf, sizeof(buf)); xreadlink(buf, buf, sizeof(buf));
if (strcmp(basename(buf), ps_filter_pattern) == 0) if (strcmp(basename(buf), name) == 0)
goto run_cb; return 1;
return 0;
return;
run_cb:
ps_filter_cb(pid);
fclose(f);
return;
}
/* Call func with process name filtered with pattern */
void ps_filter_proc_name(const char *pattern, void (*func)(int)) {
ps_filter_cb = func;
ps_filter_pattern = ((pattern == NULL) ? "" : pattern);
ps(proc_name_filter);
} }
void unlock_blocks() { void unlock_blocks() {