Migrate to STL

This commit is contained in:
topjohnwu
2019-01-19 23:59:37 -05:00
parent 03c39e692a
commit 3e4c12cf56
23 changed files with 226 additions and 548 deletions

View File

@@ -15,10 +15,12 @@
#include "daemon.h"
#include "db.h"
using namespace std;
#define SAFETYNET_COMPONENT "com.google.android.gms/.droidguard.DroidGuardService"
#define SAFETYNET_PROCESS "com.google.android.gms.unstable"
Vector<CharArray> hide_list;
vector<string> hide_list;
pthread_mutex_t list_lock;
static pthread_t proc_monitor_thread;
@@ -55,7 +57,7 @@ void hide_sensitive_props() {
// Hide all sensitive props
for (int i = 0; prop_key[i]; ++i) {
CharArray value = getprop(prop_key[i]);
auto value = getprop(prop_key[i]);
if (!value.empty() && value != prop_value[i])
setprop(prop_key[i], prop_value[i], false);
}
@@ -118,7 +120,7 @@ static bool proc_name_match(int pid, const char *name) {
}
static void kill_proc_cb(int pid, void *v) {
ps_arg *args = static_cast<ps_arg *>(v);
auto args = static_cast<ps_arg *>(v);
if (proc_name_match(pid, args->name))
kill(pid, SIGTERM);
else if (args->uid > 0) {
@@ -183,7 +185,7 @@ int add_list(const char *proc) {
// Critical region
pthread_mutex_lock(&list_lock);
hide_list.push_back(proc);
hide_list.emplace_back(proc);
kill_process(proc);
pthread_mutex_unlock(&list_lock);
@@ -247,10 +249,9 @@ bool init_list() {
// Migrate old hide list into database
if (access(LEGACY_LIST, R_OK) == 0) {
Vector<CharArray> tmp;
file_to_vector(LEGACY_LIST, tmp);
auto tmp = file_to_vector(LEGACY_LIST);
for (auto &s : tmp)
add_list(s);
add_list(s.c_str());
unlink(LEGACY_LIST);
}
@@ -307,7 +308,7 @@ int launch_magiskhide(int client) {
// Start monitoring
proc_monitor();
error:
error:
hide_enabled = false;
return DAEMON_ERROR;
}

View File

@@ -2,10 +2,10 @@
#define MAGISK_HIDE_H
#include <pthread.h>
#include <vector>
#include <string>
#include "daemon.h"
#include "Vector.h"
#include "CharArray.h"
#define TERM_THREAD SIGUSR1
@@ -30,7 +30,7 @@ bool init_list();
extern bool hide_enabled;
extern pthread_mutex_t list_lock;
extern Vector<CharArray> hide_list;
extern std::vector<std::string> hide_list;
enum {
LAUNCH_MAGISKHIDE,

View File

@@ -15,19 +15,23 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <vector>
#include <string>
#include "magisk.h"
#include "daemon.h"
#include "utils.h"
#include "magiskhide.h"
using namespace std;
static int sockfd = -1;
extern char *system_block, *vendor_block, *magiskloop;
// Workaround for the lack of pthread_cancel
static void term_thread(int) {
LOGD("proc_monitor: running cleanup\n");
hide_list.clear(true);
hide_list.clear();
hide_enabled = false;
close(sockfd);
sockfd = -1;
@@ -64,7 +68,7 @@ static void hide_daemon(int pid) {
LOGD("hide_daemon: handling pid=[%d]\n", pid);
char buffer[4096];
Vector<CharArray> mounts;
vector<string> mounts;
manage_selinux();
clean_magisk_props();
@@ -75,24 +79,24 @@ static void hide_daemon(int pid) {
snprintf(buffer, sizeof(buffer), "/proc/%d", pid);
chdir(buffer);
file_to_vector("mounts", mounts);
mounts = file_to_vector("mounts");
// Unmount dummy skeletons and /sbin links
for (auto &s : mounts) {
if (s.contains("tmpfs /system/") || s.contains("tmpfs /vendor/") || s.contains("tmpfs /sbin")) {
sscanf(s, "%*s %4096s", buffer);
if (str_contains(s, "tmpfs /system/") || str_contains(s, "tmpfs /vendor/") ||
str_contains(s, "tmpfs /sbin")) {
sscanf(s.c_str(), "%*s %4096s", buffer);
lazy_unmount(buffer);
}
}
mounts.clear();
// Re-read mount infos
file_to_vector("mounts", mounts);
mounts = file_to_vector("mounts");
// Unmount everything under /system, /vendor, and loop mounts
for (auto &s : mounts) {
if ((s.contains(" /system/") || s.contains(" /vendor/")) &&
(s.contains(system_block) || s.contains(vendor_block) || s.contains(magiskloop))) {
sscanf(s, "%*s %4096s", buffer);
if ((str_contains(s, " /system/") || str_contains(s, " /vendor/")) &&
(str_contains(s, system_block) || str_contains(s, vendor_block) || str_contains(s, magiskloop))) {
sscanf(s.c_str(), "%*s %4096s", buffer);
lazy_unmount(buffer);
}
}
@@ -108,13 +112,12 @@ void proc_monitor() {
sigset_t block_set;
sigemptyset(&block_set);
sigaddset(&block_set, TERM_THREAD);
pthread_sigmask(SIG_UNBLOCK, &block_set, NULL);
pthread_sigmask(SIG_UNBLOCK, &block_set, nullptr);
// Register the cancel signal
struct sigaction act;
memset(&act, 0, sizeof(act));
struct sigaction act{};
act.sa_handler = term_thread;
sigaction(TERM_THREAD, &act, NULL);
sigaction(TERM_THREAD, &act, nullptr);
if (access("/proc/1/ns/mnt", F_OK) != 0) {
LOGE("proc_monitor: Your kernel doesn't support mount namespace :(\n");
@@ -155,7 +158,7 @@ void proc_monitor() {
bool hide = false;
pthread_mutex_lock(&list_lock);
for (auto &s : hide_list) {
if (strncmp(cpnt, s, s.size() - 1) == 0) {
if (strncmp(cpnt, s.c_str(), s.size() - 1) == 0) {
hide = true;
break;
}