Use a better function to read through files
This commit is contained in:
parent
fb55fe184c
commit
14aa6041ec
@ -378,35 +378,35 @@ static bool magisk_env() {
|
|||||||
xmkdir(SECURE_DIR "/service.d", 0755);
|
xmkdir(SECURE_DIR "/service.d", 0755);
|
||||||
|
|
||||||
LOGI("* Mounting mirrors");
|
LOGI("* Mounting mirrors");
|
||||||
auto mounts = file_to_vector("/proc/mounts");
|
|
||||||
bool system_as_root = false;
|
bool system_as_root = false;
|
||||||
for (auto &line : mounts) {
|
file_readline("/proc/mounts", [&](string_view &line) -> bool {
|
||||||
if (str_contains(line, " /system_root ")) {
|
if (str_contains(line, " /system_root ")) {
|
||||||
bind_mount("/system_root/system", MIRRDIR "/system");
|
bind_mount("/system_root/system", MIRRDIR "/system");
|
||||||
sscanf(line.c_str(), "%s", buf);
|
sscanf(line.data(), "%s", buf);
|
||||||
system_block = strdup(buf);
|
system_block = strdup(buf);
|
||||||
system_as_root = true;
|
system_as_root = true;
|
||||||
} else if (!system_as_root && str_contains(line, " /system ")) {
|
} else if (!system_as_root && str_contains(line, " /system ")) {
|
||||||
sscanf(line.c_str(), "%s %*s %s", buf, buf2);
|
sscanf(line.data(), "%s %*s %s", buf, buf2);
|
||||||
system_block = strdup(buf);
|
system_block = strdup(buf);
|
||||||
xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr);
|
xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr);
|
||||||
VLOGI("mount", system_block, MIRRDIR "/system");
|
VLOGI("mount", system_block, MIRRDIR "/system");
|
||||||
} else if (str_contains(line, " /vendor ")) {
|
} else if (str_contains(line, " /vendor ")) {
|
||||||
seperate_vendor = true;
|
seperate_vendor = true;
|
||||||
sscanf(line.c_str(), "%s %*s %s", buf, buf2);
|
sscanf(line.data(), "%s %*s %s", buf, buf2);
|
||||||
vendor_block = strdup(buf);
|
vendor_block = strdup(buf);
|
||||||
xmkdir(MIRRDIR "/vendor", 0755);
|
xmkdir(MIRRDIR "/vendor", 0755);
|
||||||
xmount(vendor_block, MIRRDIR "/vendor", buf2, MS_RDONLY, nullptr);
|
xmount(vendor_block, MIRRDIR "/vendor", buf2, MS_RDONLY, nullptr);
|
||||||
VLOGI("mount", vendor_block, MIRRDIR "/vendor");
|
VLOGI("mount", vendor_block, MIRRDIR "/vendor");
|
||||||
} else if (str_contains(line, " /data ")) {
|
} else if (str_contains(line, " /data ")) {
|
||||||
sscanf(line.c_str(), "%s", buf);
|
sscanf(line.data(), "%s", buf);
|
||||||
data_block = strdup(buf);
|
data_block = strdup(buf);
|
||||||
} else if (SDK_INT >= 24 &&
|
} else if (SDK_INT >= 24 &&
|
||||||
str_contains(line, " /proc ") && !str_contains(line, "hidepid=2")) {
|
str_contains(line, " /proc ") && !str_contains(line, "hidepid=2")) {
|
||||||
// Enforce hidepid
|
// Enforce hidepid
|
||||||
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
|
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
|
||||||
}
|
}
|
||||||
}
|
return true;
|
||||||
|
});
|
||||||
if (!seperate_vendor) {
|
if (!seperate_vendor) {
|
||||||
xsymlink(MIRRDIR "/system/vendor", MIRRDIR "/vendor");
|
xsymlink(MIRRDIR "/system/vendor", MIRRDIR "/vendor");
|
||||||
VLOGI("link", MIRRDIR "/system/vendor", MIRRDIR "/vendor");
|
VLOGI("link", MIRRDIR "/system/vendor", MIRRDIR "/vendor");
|
||||||
@ -492,12 +492,11 @@ static void collect_modules() {
|
|||||||
static bool check_data() {
|
static bool check_data() {
|
||||||
bool mnt = false;
|
bool mnt = false;
|
||||||
bool data = false;
|
bool data = false;
|
||||||
auto mounts = file_to_vector("/proc/mounts");
|
file_readline("/proc/mounts", [&](string_view &s) -> bool {
|
||||||
for (auto &line : mounts) {
|
if (str_contains(s, " /data ") && !str_contains(s, "tmpfs"))
|
||||||
if (line.find(" /data ") != string::npos &&
|
|
||||||
line.find("tmpfs") == string::npos)
|
|
||||||
mnt = true;
|
mnt = true;
|
||||||
}
|
return true;
|
||||||
|
});
|
||||||
if (mnt) {
|
if (mnt) {
|
||||||
auto crypto = getprop("ro.crypto.state");
|
auto crypto = getprop("ro.crypto.state");
|
||||||
if (!crypto.empty()) {
|
if (!crypto.empty()) {
|
||||||
|
@ -263,9 +263,10 @@ bool init_list() {
|
|||||||
|
|
||||||
// Migrate old hide list into database
|
// Migrate old hide list into database
|
||||||
if (access(LEGACY_LIST, R_OK) == 0) {
|
if (access(LEGACY_LIST, R_OK) == 0) {
|
||||||
auto tmp = file_to_vector(LEGACY_LIST);
|
file_readline(LEGACY_LIST, [](string_view &s) -> bool {
|
||||||
for (auto &s : tmp)
|
add_list(s.data());
|
||||||
add_list(s.c_str());
|
return true;
|
||||||
|
});
|
||||||
unlink(LEGACY_LIST);
|
unlink(LEGACY_LIST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,41 +97,36 @@ static bool is_pid_safetynet_process(const int pid) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void hide_daemon(int pid) {
|
static void hide_daemon(int pid) {
|
||||||
LOGD("hide_daemon: handling PID=[%d]\n", pid);
|
|
||||||
|
|
||||||
char buffer[4096];
|
char buffer[4096];
|
||||||
vector<string> mounts;
|
|
||||||
|
|
||||||
manage_selinux();
|
|
||||||
clean_magisk_props();
|
|
||||||
|
|
||||||
if (switch_mnt_ns(pid))
|
if (switch_mnt_ns(pid))
|
||||||
goto exit;
|
goto exit;
|
||||||
|
|
||||||
|
LOGD("hide_daemon: handling PID=[%d]\n", pid);
|
||||||
|
manage_selinux();
|
||||||
|
clean_magisk_props();
|
||||||
snprintf(buffer, sizeof(buffer), "/proc/%d", pid);
|
snprintf(buffer, sizeof(buffer), "/proc/%d", pid);
|
||||||
chdir(buffer);
|
chdir(buffer);
|
||||||
|
|
||||||
mounts = file_to_vector("mounts");
|
|
||||||
// Unmount dummy skeletons and /sbin links
|
// Unmount dummy skeletons and /sbin links
|
||||||
for (auto &s : mounts) {
|
file_readline("mounts", [&](string_view &s) -> bool {
|
||||||
if (str_contains(s, "tmpfs /system/") || str_contains(s, "tmpfs /vendor/") ||
|
if (str_contains(s, "tmpfs /system/") || str_contains(s, "tmpfs /vendor/") ||
|
||||||
str_contains(s, "tmpfs /sbin")) {
|
str_contains(s, "tmpfs /sbin")) {
|
||||||
sscanf(s.c_str(), "%*s %4096s", buffer);
|
sscanf(s.data(), "%*s %4096s", buffer);
|
||||||
lazy_unmount(buffer);
|
lazy_unmount(buffer);
|
||||||
}
|
}
|
||||||
}
|
return true;
|
||||||
|
});
|
||||||
// Re-read mount infos
|
|
||||||
mounts = file_to_vector("mounts");
|
|
||||||
|
|
||||||
// Unmount everything under /system, /vendor, and data mounts
|
// Unmount everything under /system, /vendor, and data mounts
|
||||||
for (auto &s : mounts) {
|
file_readline("mounts", [&](string_view &s) -> bool {
|
||||||
if ((str_contains(s, " /system/") || str_contains(s, " /vendor/")) &&
|
if ((str_contains(s, " /system/") || str_contains(s, " /vendor/")) &&
|
||||||
(str_contains(s, system_block) || str_contains(s, vendor_block) || str_contains(s, data_block))) {
|
(str_contains(s, system_block) || str_contains(s, vendor_block) ||
|
||||||
sscanf(s.c_str(), "%*s %4096s", buffer);
|
str_contains(s, data_block))) {
|
||||||
|
sscanf(s.data(), "%*s %4096s", buffer);
|
||||||
lazy_unmount(buffer);
|
lazy_unmount(buffer);
|
||||||
}
|
}
|
||||||
}
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
// Send resume signal
|
// Send resume signal
|
||||||
|
@ -384,25 +384,26 @@ void write_zero(int fd, size_t size) {
|
|||||||
lseek(fd, pos + size, SEEK_SET);
|
lseek(fd, pos + size, SEEK_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<string> file_to_vector(const char *filename) {
|
void file_readline(const char *filename, const function<bool (string_view&)> &fn, bool trim) {
|
||||||
vector<string> arr;
|
|
||||||
if (access(filename, R_OK) != 0)
|
|
||||||
return arr;
|
|
||||||
char *line = nullptr;
|
|
||||||
size_t len = 0;
|
|
||||||
ssize_t read;
|
|
||||||
|
|
||||||
FILE *fp = xfopen(filename, "re");
|
FILE *fp = xfopen(filename, "re");
|
||||||
if (fp == nullptr)
|
if (fp == nullptr)
|
||||||
return arr;
|
return;
|
||||||
|
size_t len = 1024;
|
||||||
while ((read = getline(&line, &len, fp)) != -1) {
|
char *buf = (char *) malloc(len);
|
||||||
// Remove end newline
|
char *start;
|
||||||
if (line[read - 1] == '\n')
|
ssize_t read;
|
||||||
line[read - 1] = '\0';
|
while ((read = getline(&buf, &len, fp)) >= 0) {
|
||||||
arr.emplace_back(line);
|
start = buf;
|
||||||
|
if (trim) {
|
||||||
|
while (buf[read - 1] == '\n' || buf[read - 1] == ' ')
|
||||||
|
buf[read-- - 1] = '\0';
|
||||||
|
while (*start == ' ')
|
||||||
|
++start;
|
||||||
|
}
|
||||||
|
string_view s(start);
|
||||||
|
if (!fn(s))
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
free(line);
|
free(buf);
|
||||||
return arr;
|
|
||||||
}
|
}
|
||||||
|
@ -140,13 +140,15 @@ void write_zero(int fd, size_t size);
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
|
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
|
||||||
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
|
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
|
||||||
|
|
||||||
// file.cpp
|
// file.cpp
|
||||||
|
|
||||||
std::vector<std::string> file_to_vector(const char *filename);
|
void file_readline(const char *filename, const std::function<bool (std::string_view&)> &fn, bool trim = false);
|
||||||
|
|
||||||
// misc.cpp
|
// misc.cpp
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user