Magisk/native/jni/magiskhide/hide_policy.cpp

100 lines
2.5 KiB
C++
Raw Normal View History

2019-05-26 02:47:57 -07:00
#include <sys/mount.h>
#include <magisk.h>
#include <utils.h>
#include <selinux.h>
#include <resetprop.h>
#include "magiskhide.h"
using namespace std;
static const char *prop_key[] =
{ "ro.boot.vbmeta.device_state", "ro.boot.verifiedbootstate", "ro.boot.flash.locked",
"ro.boot.veritymode", "ro.boot.warranty_bit", "ro.warranty_bit", "ro.debuggable",
"ro.secure", "ro.build.type", "ro.build.tags", "ro.build.selinux", nullptr };
static const char *prop_value[] =
{ "locked", "green", "1",
"enforcing", "0", "0", "0",
"1", "user", "release-keys", "0", nullptr };
void hide_sensitive_props() {
LOGI("hide_policy: Hiding sensitive props\n");
// Hide all sensitive props
for (int i = 0; prop_key[i]; ++i) {
auto value = getprop(prop_key[i]);
if (!value.empty() && value != prop_value[i])
setprop(prop_key[i], prop_value[i], false);
}
2020-02-18 00:44:10 +08:00
// Xiaomi cross region flash
auto hwc = getprop("ro.boot.hwc");
2020-02-18 01:13:26 +08:00
if (!hwc.empty() && hwc.find("CN") != string::npos) {
2020-02-18 00:44:10 +08:00
setprop("ro.boot.hwc", "GLOBAL", false);
}
auto hwcountry = getprop("ro.boot.hwcountry");
2020-02-18 01:13:26 +08:00
if (!hwcountry.empty() && hwcountry.find("China") != string::npos) {
2020-02-18 00:44:10 +08:00
setprop("ro.boot.hwcountry", "GLOBAL", false);
}
2019-05-26 02:47:57 -07:00
}
static inline void lazy_unmount(const char* mountpoint) {
if (umount2(mountpoint, MNT_DETACH) != -1)
LOGD("hide_policy: Unmounted (%s)\n", mountpoint);
}
void hide_daemon(int pid) {
2019-09-25 23:55:39 -04:00
run_finally fin([=]() -> void {
2019-05-26 02:47:57 -07:00
// Send resume signal
2019-06-27 00:28:34 -07:00
kill(pid, SIGCONT);
2019-05-26 02:47:57 -07:00
_exit(0);
});
hide_unmount(pid);
}
2019-05-26 02:47:57 -07:00
2019-06-23 03:53:41 -07:00
#define TMPFS_MNT(dir) (mentry->mnt_type == "tmpfs"sv && \
strncmp(mentry->mnt_dir, "/" #dir, sizeof("/" #dir) - 1) == 0)
void hide_unmount(int pid) {
2019-05-26 02:47:57 -07:00
if (switch_mnt_ns(pid))
return;
LOGD("hide_policy: handling PID=[%d]\n", pid);
2019-05-26 03:05:23 -07:00
char val;
int fd = xopen(SELINUX_ENFORCE, O_RDONLY);
xxread(fd, &val, sizeof(val));
close(fd);
// Permissive
if (val == '0') {
chmod(SELINUX_ENFORCE, 0640);
chmod(SELINUX_POLICY, 0440);
}
2019-05-26 02:47:57 -07:00
vector<string> targets;
// Unmount dummy skeletons and /sbin links
2019-06-23 03:53:41 -07:00
parse_mnt("/proc/self/mounts", [&](mntent *mentry) {
if (TMPFS_MNT(system) || TMPFS_MNT(vendor) || TMPFS_MNT(sbin) || TMPFS_MNT(product))
2019-06-23 03:53:41 -07:00
targets.emplace_back(mentry->mnt_dir);
2019-05-26 02:47:57 -07:00
return true;
});
for (auto &s : targets)
lazy_unmount(s.data());
targets.clear();
// Unmount all Magisk created mounts
2019-06-23 03:53:41 -07:00
parse_mnt("/proc/self/mounts", [&](mntent *mentry) {
if (strstr(mentry->mnt_fsname, BLOCKDIR))
targets.emplace_back(mentry->mnt_dir);
2019-05-26 02:47:57 -07:00
return true;
});
for (auto &s : targets)
lazy_unmount(s.data());
}