2019-05-26 09:47:57 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <magisk.hpp>
|
|
|
|
#include <utils.hpp>
|
|
|
|
#include <selinux.hpp>
|
|
|
|
#include <resetprop.hpp>
|
2019-05-26 09:47:57 +00:00
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include "magiskhide.hpp"
|
2019-05-26 09:47:57 +00:00
|
|
|
|
|
|
|
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",
|
2020-03-12 07:28:43 +00:00
|
|
|
"ro.secure", "ro.build.type", "ro.build.tags", "ro.build.selinux",
|
2020-05-17 21:45:08 +00:00
|
|
|
"ro.vendor.boot.warranty_bit", "ro.vendor.warranty_bit", nullptr };
|
2020-05-09 09:53:32 +00:00
|
|
|
|
2020-05-17 21:45:08 +00:00
|
|
|
static const char *prop_val[] =
|
2019-05-26 09:47:57 +00:00
|
|
|
{ "locked", "green", "1",
|
|
|
|
"enforcing", "0", "0", "0",
|
2020-03-12 07:28:43 +00:00
|
|
|
"1", "user", "release-keys", "0",
|
2020-05-17 21:45:08 +00:00
|
|
|
"0", "0", nullptr };
|
|
|
|
|
|
|
|
static const char *late_prop_key[] =
|
|
|
|
{ "vendor.boot.vbmeta.device_state", "vendor.boot.verifiedbootstate", nullptr };
|
|
|
|
|
|
|
|
static const char *late_prop_val[] =
|
|
|
|
{ "locked", "green", nullptr };
|
2019-05-26 09:47:57 +00:00
|
|
|
|
|
|
|
void hide_sensitive_props() {
|
|
|
|
LOGI("hide_policy: Hiding sensitive props\n");
|
|
|
|
|
|
|
|
for (int i = 0; prop_key[i]; ++i) {
|
|
|
|
auto value = getprop(prop_key[i]);
|
2020-05-17 21:45:08 +00:00
|
|
|
if (!value.empty() && value != prop_val[i])
|
|
|
|
setprop(prop_key[i], prop_val[i], false);
|
2019-05-26 09:47:57 +00:00
|
|
|
}
|
2020-02-17 16:44:10 +00:00
|
|
|
|
2020-03-12 07:28:43 +00:00
|
|
|
// Hide that we booted from recovery when magisk is in recovery mode
|
|
|
|
auto bootmode = getprop("ro.bootmode");
|
2020-05-17 21:45:08 +00:00
|
|
|
if (!bootmode.empty() && str_contains(bootmode, "recovery")) {
|
2020-03-12 07:28:43 +00:00
|
|
|
setprop("ro.bootmode", "unknown", false);
|
|
|
|
}
|
|
|
|
bootmode = getprop("ro.boot.mode");
|
2020-05-17 21:45:08 +00:00
|
|
|
if (!bootmode.empty() && str_contains(bootmode, "recovery")) {
|
2020-03-12 07:28:43 +00:00
|
|
|
setprop("ro.boot.mode", "unknown", false);
|
2020-03-12 11:55:44 +00:00
|
|
|
}
|
2020-02-17 16:44:10 +00:00
|
|
|
// Xiaomi cross region flash
|
|
|
|
auto hwc = getprop("ro.boot.hwc");
|
2020-05-17 21:45:08 +00:00
|
|
|
if (!hwc.empty() && str_contains(hwc, "CN")) {
|
2020-02-17 16:44:10 +00:00
|
|
|
setprop("ro.boot.hwc", "GLOBAL", false);
|
|
|
|
}
|
|
|
|
auto hwcountry = getprop("ro.boot.hwcountry");
|
2020-05-17 21:45:08 +00:00
|
|
|
if (!hwcountry.empty() && str_contains(hwcountry, "China")) {
|
2020-02-17 16:44:10 +00:00
|
|
|
setprop("ro.boot.hwcountry", "GLOBAL", false);
|
|
|
|
}
|
2019-05-26 09:47:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 21:45:08 +00:00
|
|
|
void hide_late_sensitive_props() {
|
|
|
|
LOGI("hide_policy: Hiding sensitive props (late)\n");
|
|
|
|
|
|
|
|
for (int i = 0; late_prop_key[i]; ++i) {
|
|
|
|
auto value = getprop(late_prop_key[i]);
|
|
|
|
if (!value.empty() && value != late_prop_val[i])
|
|
|
|
setprop(prop_key[i], late_prop_val[i], false);
|
|
|
|
}
|
|
|
|
auto bootmode = getprop("vendor.boot.mode");
|
|
|
|
if (!bootmode.empty() && str_contains(bootmode, "recovery")) {
|
|
|
|
setprop("vendor.boot.mode", "unknown", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 09:47:57 +00: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-26 03:55:39 +00:00
|
|
|
run_finally fin([=]() -> void {
|
2019-05-26 09:47:57 +00:00
|
|
|
// Send resume signal
|
2019-06-27 07:28:34 +00:00
|
|
|
kill(pid, SIGCONT);
|
2019-05-26 09:47:57 +00:00
|
|
|
_exit(0);
|
|
|
|
});
|
2019-06-05 05:27:19 +00:00
|
|
|
hide_unmount(pid);
|
|
|
|
}
|
2019-05-26 09:47:57 +00:00
|
|
|
|
2019-06-23 10:53:41 +00:00
|
|
|
#define TMPFS_MNT(dir) (mentry->mnt_type == "tmpfs"sv && \
|
|
|
|
strncmp(mentry->mnt_dir, "/" #dir, sizeof("/" #dir) - 1) == 0)
|
|
|
|
|
2019-06-05 05:27:19 +00:00
|
|
|
void hide_unmount(int pid) {
|
2019-05-26 09:47:57 +00:00
|
|
|
if (switch_mnt_ns(pid))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOGD("hide_policy: handling PID=[%d]\n", pid);
|
2019-05-26 10:05:23 +00: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 09:47:57 +00:00
|
|
|
vector<string> targets;
|
|
|
|
|
|
|
|
// Unmount dummy skeletons and /sbin links
|
2020-04-19 06:45:00 +00:00
|
|
|
targets.push_back(MAGISKTMP);
|
2019-06-23 10:53:41 +00:00
|
|
|
parse_mnt("/proc/self/mounts", [&](mntent *mentry) {
|
2020-04-19 06:45:00 +00:00
|
|
|
if (TMPFS_MNT(system) || TMPFS_MNT(vendor) || TMPFS_MNT(product) || TMPFS_MNT(system_ext))
|
2019-06-23 10:53:41 +00:00
|
|
|
targets.emplace_back(mentry->mnt_dir);
|
2019-05-26 09:47:57 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2020-04-19 06:45:00 +00:00
|
|
|
for (auto &s : reversed(targets))
|
2019-05-26 09:47:57 +00:00
|
|
|
lazy_unmount(s.data());
|
|
|
|
targets.clear();
|
|
|
|
|
|
|
|
// Unmount all Magisk created mounts
|
2019-06-23 10:53:41 +00:00
|
|
|
parse_mnt("/proc/self/mounts", [&](mntent *mentry) {
|
|
|
|
if (strstr(mentry->mnt_fsname, BLOCKDIR))
|
|
|
|
targets.emplace_back(mentry->mnt_dir);
|
2019-05-26 09:47:57 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2020-04-19 06:45:00 +00:00
|
|
|
for (auto &s : reversed(targets))
|
2019-05-26 09:47:57 +00:00
|
|
|
lazy_unmount(s.data());
|
|
|
|
}
|
|
|
|
|