2022-03-16 21:41:20 -07:00
|
|
|
#include <sys/mount.h>
|
|
|
|
|
|
|
|
#include <magisk.hpp>
|
2022-03-29 22:26:38 -07:00
|
|
|
#include <sepolicy.hpp>
|
2022-05-12 02:03:42 -07:00
|
|
|
#include <base.hpp>
|
2022-03-16 21:41:20 -07:00
|
|
|
|
|
|
|
#include "init.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2022-04-06 17:37:04 +08:00
|
|
|
void MagiskInit::patch_sepolicy(const char *in, const char *out) {
|
2022-03-16 21:41:20 -07:00
|
|
|
LOGD("Patching monolithic policy\n");
|
2022-04-06 17:37:04 +08:00
|
|
|
auto sepol = unique_ptr<sepolicy>(sepolicy::from_file(in));
|
2022-03-16 21:41:20 -07:00
|
|
|
|
|
|
|
sepol->magisk_rules();
|
|
|
|
|
|
|
|
// Custom rules
|
|
|
|
if (!custom_rules_dir.empty()) {
|
|
|
|
if (auto dir = xopen_dir(custom_rules_dir.data())) {
|
|
|
|
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
|
|
|
auto rule = custom_rules_dir + "/" + entry->d_name + "/sepolicy.rule";
|
|
|
|
if (xaccess(rule.data(), R_OK) == 0) {
|
|
|
|
LOGD("Loading custom sepolicy patch: [%s]\n", rule.data());
|
|
|
|
sepol->load_rule_file(rule.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:37:04 +08:00
|
|
|
LOGD("Dumping sepolicy to: [%s]\n", out);
|
|
|
|
sepol->to_file(out);
|
2022-03-16 21:41:20 -07:00
|
|
|
|
|
|
|
// Remove OnePlus stupid debug sepolicy and use our own
|
|
|
|
if (access("/sepolicy_debug", F_OK) == 0) {
|
|
|
|
unlink("/sepolicy_debug");
|
|
|
|
link("/sepolicy", "/sepolicy_debug");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MOCK_COMPAT SELINUXMOCK "/compatible"
|
2022-03-18 00:46:34 -07:00
|
|
|
#define MOCK_LOAD SELINUXMOCK "/load"
|
2022-04-08 02:13:31 -07:00
|
|
|
#define MOCK_ENFORCE SELINUXMOCK "/enforce"
|
2022-03-16 21:41:20 -07:00
|
|
|
|
2022-04-01 13:16:23 +08:00
|
|
|
bool MagiskInit::hijack_sepolicy() {
|
2022-03-18 00:46:34 -07:00
|
|
|
xmkdir(SELINUXMOCK, 0);
|
|
|
|
|
2022-04-08 02:13:31 -07:00
|
|
|
if (access("/system/bin/init", F_OK) == 0) {
|
|
|
|
// On 2SI devices, the 2nd stage init file is always a dynamic executable.
|
|
|
|
// This meant that instead of going through convoluted methods trying to alter
|
|
|
|
// and block init's control flow, we can just LD_PRELOAD and replace the
|
|
|
|
// security_load_policy function with our own implementation.
|
|
|
|
dump_preload("/dev/preload.so", 0644);
|
|
|
|
setenv("LD_PRELOAD", "/dev/preload.so", 1);
|
2022-03-16 21:41:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack the "load" and "enforce" node in selinuxfs to manipulate
|
|
|
|
// the actual sepolicy being loaded into the kernel
|
2022-03-18 00:46:34 -07:00
|
|
|
auto hijack = [&] {
|
|
|
|
LOGD("Hijack [" SELINUX_LOAD "]\n");
|
2022-04-16 07:27:34 -07:00
|
|
|
close(xopen(MOCK_LOAD, O_CREAT | O_RDONLY, 0600));
|
2022-03-16 21:41:20 -07:00
|
|
|
xmount(MOCK_LOAD, SELINUX_LOAD, nullptr, MS_BIND, nullptr);
|
2022-04-08 02:13:31 -07:00
|
|
|
LOGD("Hijack [" SELINUX_ENFORCE "]\n");
|
|
|
|
mkfifo(MOCK_ENFORCE, 0644);
|
|
|
|
xmount(MOCK_ENFORCE, SELINUX_ENFORCE, nullptr, MS_BIND, nullptr);
|
2022-03-16 21:41:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
string dt_compat;
|
|
|
|
if (access(SELINUX_ENFORCE, F_OK) != 0) {
|
|
|
|
// selinuxfs not mounted yet. Hijack the dt fstab nodes first
|
2022-04-08 02:13:31 -07:00
|
|
|
// and let the original init mount selinuxfs for us.
|
2022-03-16 21:41:20 -07:00
|
|
|
// This only happens on Android 8.0 - 9.0
|
|
|
|
|
|
|
|
char buf[4096];
|
|
|
|
snprintf(buf, sizeof(buf), "%s/fstab/compatible", config->dt_dir);
|
|
|
|
dt_compat = full_read(buf);
|
2022-04-01 13:16:23 +08:00
|
|
|
if (dt_compat.empty()) {
|
2022-04-08 02:13:31 -07:00
|
|
|
// Device does not do early mount and uses monolithic policy
|
2022-04-01 13:16:23 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-08 02:13:31 -07:00
|
|
|
// Remount procfs with proper options
|
|
|
|
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
|
|
|
|
|
2022-03-16 21:41:20 -07:00
|
|
|
LOGD("Hijack [%s]\n", buf);
|
2022-04-01 13:16:23 +08:00
|
|
|
|
|
|
|
// Preserve sysfs and procfs for hijacking
|
|
|
|
mount_list.erase(std::remove_if(
|
|
|
|
mount_list.begin(), mount_list.end(),
|
|
|
|
[](const string &s) { return s == "/proc" || s == "/sys"; }), mount_list.end());
|
|
|
|
|
2022-03-16 21:41:20 -07:00
|
|
|
mkfifo(MOCK_COMPAT, 0444);
|
|
|
|
xmount(MOCK_COMPAT, buf, nullptr, MS_BIND, nullptr);
|
|
|
|
} else {
|
|
|
|
hijack();
|
|
|
|
}
|
|
|
|
|
2022-03-18 00:46:34 -07:00
|
|
|
// Read all custom rules into memory
|
|
|
|
string rules;
|
|
|
|
if (!custom_rules_dir.empty()) {
|
|
|
|
if (auto dir = xopen_dir(custom_rules_dir.data())) {
|
|
|
|
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
|
|
|
auto rule_file = custom_rules_dir + "/" + entry->d_name + "/sepolicy.rule";
|
|
|
|
if (xaccess(rule_file.data(), R_OK) == 0) {
|
|
|
|
LOGD("Load custom sepolicy patch: [%s]\n", rule_file.data());
|
|
|
|
full_read(rule_file.data(), rules);
|
|
|
|
rules += '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 21:41:20 -07:00
|
|
|
// Create a new process waiting for init operations
|
|
|
|
if (xfork()) {
|
|
|
|
// In parent, return and continue boot process
|
2022-04-01 13:16:23 +08:00
|
|
|
return true;
|
2022-03-16 21:41:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dt_compat.empty()) {
|
|
|
|
// This open will block until init calls DoFirstStageMount
|
|
|
|
// The only purpose here is actually to wait for init to mount selinuxfs for us
|
|
|
|
int fd = xopen(MOCK_COMPAT, O_WRONLY);
|
|
|
|
|
|
|
|
char buf[4096];
|
|
|
|
snprintf(buf, sizeof(buf), "%s/fstab/compatible", config->dt_dir);
|
|
|
|
xumount2(buf, MNT_DETACH);
|
|
|
|
|
|
|
|
hijack();
|
2022-03-18 00:46:34 -07:00
|
|
|
|
2022-03-16 21:41:20 -07:00
|
|
|
xwrite(fd, dt_compat.data(), dt_compat.size());
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2022-04-08 02:13:31 -07:00
|
|
|
// This open will block until init calls security_getenforce
|
2022-04-16 07:27:34 -07:00
|
|
|
int fd = xopen(MOCK_ENFORCE, O_WRONLY);
|
2022-03-16 21:41:20 -07:00
|
|
|
|
|
|
|
// Cleanup the hijacks
|
|
|
|
umount2("/init", MNT_DETACH);
|
|
|
|
xumount2(SELINUX_LOAD, MNT_DETACH);
|
2022-04-08 02:13:31 -07:00
|
|
|
xumount2(SELINUX_ENFORCE, MNT_DETACH);
|
2022-03-16 21:41:20 -07:00
|
|
|
|
2022-04-16 07:27:34 -07:00
|
|
|
// Load and patch policy
|
|
|
|
auto sepol = unique_ptr<sepolicy>(sepolicy::from_file(MOCK_LOAD));
|
|
|
|
sepol->magisk_rules();
|
|
|
|
sepol->load_rules(rules);
|
|
|
|
|
|
|
|
// Load patched policy into kernel
|
|
|
|
sepol->to_file(SELINUX_LOAD);
|
2022-03-16 21:41:20 -07:00
|
|
|
|
2022-04-08 02:13:31 -07:00
|
|
|
// Write to the enforce node ONLY after sepolicy is loaded. We need to make sure
|
2022-03-16 21:41:20 -07:00
|
|
|
// the actual init process is blocked until sepolicy is loaded, or else
|
|
|
|
// restorecon will fail and re-exec won't change context, causing boot failure.
|
2022-04-08 02:13:31 -07:00
|
|
|
// We (ab)use the fact that init reads the enforce node, and because
|
|
|
|
// it has been replaced with our FIFO file, init will block until we
|
2022-03-16 21:41:20 -07:00
|
|
|
// write something into the pipe, effectively hijacking its control flow.
|
|
|
|
|
2022-04-16 07:27:34 -07:00
|
|
|
string enforce = full_read(SELINUX_ENFORCE);
|
2022-04-08 02:13:31 -07:00
|
|
|
xwrite(fd, enforce.data(), enforce.length());
|
2022-03-16 21:41:20 -07:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
// At this point, the init process will be unblocked
|
|
|
|
// and continue on with restorecon + re-exec.
|
|
|
|
|
|
|
|
// Terminate process
|
|
|
|
exit(0);
|
|
|
|
}
|