Magisk/native/jni/magiskhide/hide_policy.cpp

62 lines
1.6 KiB
C++
Raw Normal View History

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;
2020-05-17 22:01:20 +00:00
static void lazy_unmount(const char* mountpoint) {
if (umount2(mountpoint, MNT_DETACH) != -1)
2021-01-11 03:27:54 +00:00
LOGD("hide: Unmounted (%s)\n", mountpoint);
2019-05-26 09:47:57 +00:00
}
2021-08-19 11:55:17 +00:00
void hide_daemon(int pid, int client) {
if (fork_dont_care() == 0) {
hide_unmount(pid);
2021-08-19 11:55:17 +00:00
write_int(client, 0);
_exit(0);
}
}
2019-05-26 09:47:57 +00:00
#define TMPFS_MNT(dir) (mentry->mnt_type == "tmpfs"sv && str_starts(mentry->mnt_dir, "/" #dir))
2019-06-23 10:53:41 +00:00
void hide_unmount(int pid) {
2021-08-26 10:09:56 +00:00
if (pid > 0) {
if (switch_mnt_ns(pid))
return;
LOGD("hide: handling PID=[%d]\n", pid);
}
vector<string> targets;
2021-08-26 10:09:56 +00:00
// Unmount dummy skeletons and MAGISKTMP
targets.push_back(MAGISKTMP);
auto magiskpts = MAGISKTMP + "/" SHELLPTS;
if (access(magiskpts.data(), F_OK) == 0) {
targets.push_back(magiskpts);
}
parse_mnt("/proc/self/mounts", [&](mntent *mentry) {
if (TMPFS_MNT(system) || TMPFS_MNT(vendor) || TMPFS_MNT(product) || TMPFS_MNT(system_ext))
targets.emplace_back(mentry->mnt_dir);
return true;
});
for (auto &s : reversed(targets))
lazy_unmount(s.data());
targets.clear();
// Unmount all Magisk created mounts
parse_mnt("/proc/self/mounts", [&](mntent *mentry) {
if (str_contains(mentry->mnt_fsname, BLOCKDIR))
targets.emplace_back(mentry->mnt_dir);
return true;
});
for (auto &s : reversed(targets))
lazy_unmount(s.data());
2019-05-26 09:47:57 +00:00
}