2020-12-06 03:07:47 -08:00
|
|
|
#include <sys/mount.h>
|
2020-04-19 02:35:28 -07:00
|
|
|
#include <libgen.h>
|
2023-03-08 14:42:54 +08:00
|
|
|
#include <sys/sysmacros.h>
|
2019-05-27 00:29:43 -07:00
|
|
|
|
2023-10-16 17:38:44 -07:00
|
|
|
#include <sepolicy.hpp>
|
2023-11-08 01:46:02 -08:00
|
|
|
#include <consts.hpp>
|
2022-05-12 02:03:42 -07:00
|
|
|
#include <base.hpp>
|
2023-04-25 23:34:45 -07:00
|
|
|
#include <flags.h>
|
2019-05-27 00:29:43 -07:00
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "init.hpp"
|
2019-05-27 00:29:43 -07:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2020-04-25 23:19:36 -07:00
|
|
|
static vector<string> rc_list;
|
2019-07-16 01:08:28 -07:00
|
|
|
|
2023-11-06 15:39:48 -08:00
|
|
|
static void patch_rc_scripts(const char *src_path, const char *tmp_path, bool writable) {
|
|
|
|
auto src_dir = xopen_dir(src_path);
|
|
|
|
if (!src_dir) return;
|
|
|
|
int src_fd = dirfd(src_dir.get());
|
|
|
|
|
|
|
|
// If writable, directly modify the file in src_path, or else add to rootfs overlay
|
|
|
|
auto dest_dir = writable ? [&] {
|
|
|
|
return xopen_dir(src_path);
|
|
|
|
}() : [&] {
|
|
|
|
char buf[PATH_MAX] = {};
|
|
|
|
ssprintf(buf, sizeof(buf), ROOTOVL "%s", src_path);
|
|
|
|
xmkdirs(buf, 0755);
|
|
|
|
return xopen_dir(buf);
|
|
|
|
}();
|
|
|
|
if (!dest_dir) return;
|
|
|
|
int dest_fd = dirfd(dest_dir.get());
|
|
|
|
|
|
|
|
// First patch init.rc
|
|
|
|
{
|
|
|
|
auto src = xopen_file(xopenat(src_fd, "init.rc", O_RDONLY | O_CLOEXEC, 0), "re");
|
|
|
|
if (!src) return;
|
|
|
|
if (writable) unlinkat(src_fd, "init.rc", 0);
|
|
|
|
auto dest = xopen_file(
|
|
|
|
xopenat(dest_fd, "init.rc", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0), "we");
|
|
|
|
if (!dest) return;
|
|
|
|
LOGD("Patching init.rc in %s\n", src_path);
|
|
|
|
file_readline(false, src.get(), [&dest](string_view line) -> bool {
|
|
|
|
// Do not start vaultkeeper
|
|
|
|
if (str_contains(line, "start vaultkeeper")) {
|
|
|
|
LOGD("Remove vaultkeeper\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Do not run flash_recovery
|
|
|
|
if (line.starts_with("service flash_recovery")) {
|
|
|
|
LOGD("Remove flash_recovery\n");
|
|
|
|
fprintf(dest.get(), "service flash_recovery /system/bin/true\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Samsung's persist.sys.zygote.early will cause Zygote to start before post-fs-data
|
|
|
|
if (line.starts_with("on property:persist.sys.zygote.early=")) {
|
|
|
|
LOGD("Invalidate persist.sys.zygote.early\n");
|
|
|
|
fprintf(dest.get(), "on property:persist.sys.zygote.early.xxxxx=true\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Else just write the line
|
|
|
|
fprintf(dest.get(), "%s", line.data());
|
2022-02-03 16:46:52 +08:00
|
|
|
return true;
|
2023-11-06 15:39:48 -08:00
|
|
|
});
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2023-11-06 15:39:48 -08:00
|
|
|
fprintf(dest.get(), "\n");
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2023-11-06 15:39:48 -08:00
|
|
|
// Inject custom rc scripts
|
|
|
|
for (auto &script : rc_list) {
|
|
|
|
// Replace template arguments of rc scripts with dynamic paths
|
|
|
|
replace_all(script, "${MAGISKTMP}", tmp_path);
|
|
|
|
fprintf(dest.get(), "\n%s\n", script.data());
|
|
|
|
}
|
|
|
|
rc_list.clear();
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2023-11-06 15:39:48 -08:00
|
|
|
// Inject Magisk rc scripts
|
|
|
|
LOGD("Inject magisk rc\n");
|
|
|
|
fprintf(dest.get(), R"EOF(
|
2023-03-16 10:26:27 +08:00
|
|
|
on post-fs-data
|
|
|
|
start logd
|
|
|
|
exec %2$s 0 0 -- %1$s/magisk --post-fs-data
|
|
|
|
|
|
|
|
on property:vold.decrypt=trigger_restart_framework
|
|
|
|
exec %2$s 0 0 -- %1$s/magisk --service
|
|
|
|
|
|
|
|
on nonencrypted
|
|
|
|
exec %2$s 0 0 -- %1$s/magisk --service
|
|
|
|
|
|
|
|
on property:sys.boot_completed=1
|
|
|
|
exec %2$s 0 0 -- %1$s/magisk --boot-complete
|
|
|
|
|
|
|
|
on property:init.svc.zygote=stopped
|
|
|
|
exec %2$s 0 0 -- %1$s/magisk --zygote-restart
|
2023-11-06 15:39:48 -08:00
|
|
|
)EOF", tmp_path, MAGISK_PROC_CON);
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2023-11-06 15:39:48 -08:00
|
|
|
fclone_attr(fileno(src.get()), fileno(dest.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then patch init.zygote*.rc
|
|
|
|
for (dirent *entry; (entry = readdir(src_dir.get()));) {
|
|
|
|
auto name = std::string_view(entry->d_name);
|
|
|
|
if (!name.starts_with("init.zygote") || !name.ends_with(".rc")) continue;
|
|
|
|
auto src = xopen_file(xopenat(src_fd, name.data(), O_RDONLY | O_CLOEXEC, 0), "re");
|
|
|
|
if (!src) continue;
|
|
|
|
if (writable) unlinkat(src_fd, name.data(), 0);
|
|
|
|
auto dest = xopen_file(
|
|
|
|
xopenat(dest_fd, name.data(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0), "we");
|
|
|
|
if (!dest) continue;
|
|
|
|
LOGD("Patching %s in %s\n", name.data(), src_path);
|
|
|
|
file_readline(false, src.get(), [&dest, &tmp_path](string_view line) -> bool {
|
|
|
|
if (line.starts_with("service zygote ")) {
|
|
|
|
LOGD("Inject zygote restart\n");
|
|
|
|
fprintf(dest.get(), "%s", line.data());
|
|
|
|
fprintf(dest.get(), " onrestart exec %2$s 0 0 -- %1$s/magisk --zygote-restart\n",
|
|
|
|
tmp_path, MAGISK_PROC_CON);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
fprintf(dest.get(), "%s", line.data());
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
fclone_attr(fileno(src.get()), fileno(dest.get()));
|
|
|
|
}
|
2019-06-25 23:31:59 -07:00
|
|
|
}
|
2019-05-27 00:29:43 -07:00
|
|
|
|
2020-04-01 04:39:28 -07:00
|
|
|
static void load_overlay_rc(const char *overlay) {
|
2020-12-30 22:11:24 -08:00
|
|
|
auto dir = open_dir(overlay);
|
|
|
|
if (!dir) return;
|
|
|
|
|
|
|
|
int dfd = dirfd(dir.get());
|
|
|
|
// Do not allow overwrite init.rc
|
|
|
|
unlinkat(dfd, "init.rc", 0);
|
2022-12-26 19:28:10 +08:00
|
|
|
|
|
|
|
// '/' + name + '\0'
|
|
|
|
char buf[NAME_MAX + 2];
|
|
|
|
buf[0] = '/';
|
2020-12-30 22:11:24 -08:00
|
|
|
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
2022-12-26 19:28:10 +08:00
|
|
|
if (!str_ends(entry->d_name, ".rc")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strscpy(buf + 1, entry->d_name, sizeof(buf) - 1);
|
|
|
|
if (access(buf, F_OK) == 0) {
|
|
|
|
LOGD("Replace rc script [%s]\n", entry->d_name);
|
|
|
|
} else {
|
2020-12-30 22:11:24 -08:00
|
|
|
LOGD("Found rc script [%s]\n", entry->d_name);
|
|
|
|
int rc = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC);
|
2022-06-17 02:36:04 -07:00
|
|
|
rc_list.push_back(full_read(rc));
|
2020-12-30 22:11:24 -08:00
|
|
|
close(rc);
|
|
|
|
unlinkat(dfd, entry->d_name, 0);
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 01:08:28 -07:00
|
|
|
}
|
|
|
|
|
2020-01-09 23:42:27 +08:00
|
|
|
static void recreate_sbin(const char *mirror, bool use_bind_mount) {
|
2020-12-30 22:11:24 -08:00
|
|
|
auto dp = xopen_dir(mirror);
|
|
|
|
int src = dirfd(dp.get());
|
|
|
|
char buf[4096];
|
|
|
|
for (dirent *entry; (entry = xreaddir(dp.get()));) {
|
|
|
|
string sbin_path = "/sbin/"s + entry->d_name;
|
|
|
|
struct stat st;
|
|
|
|
fstatat(src, entry->d_name, &st, AT_SYMLINK_NOFOLLOW);
|
|
|
|
if (S_ISLNK(st.st_mode)) {
|
|
|
|
xreadlinkat(src, entry->d_name, buf, sizeof(buf));
|
|
|
|
xsymlink(buf, sbin_path.data());
|
|
|
|
} else {
|
|
|
|
sprintf(buf, "%s/%s", mirror, entry->d_name);
|
|
|
|
if (use_bind_mount) {
|
|
|
|
auto mode = st.st_mode & 0777;
|
|
|
|
// Create dummy
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
xmkdir(sbin_path.data(), mode);
|
|
|
|
else
|
|
|
|
close(xopen(sbin_path.data(), O_CREAT | O_WRONLY | O_CLOEXEC, mode));
|
|
|
|
|
|
|
|
xmount(buf, sbin_path.data(), nullptr, MS_BIND, nullptr);
|
|
|
|
} else {
|
|
|
|
xsymlink(buf, sbin_path.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 15:31:49 -05:00
|
|
|
}
|
|
|
|
|
2019-11-19 00:16:20 -05:00
|
|
|
static string magic_mount_list;
|
2019-07-16 23:54:52 -07:00
|
|
|
|
2020-02-21 00:49:58 -08:00
|
|
|
static void magic_mount(const string &sdir, const string &ddir = "") {
|
2020-12-30 22:11:24 -08:00
|
|
|
auto dir = xopen_dir(sdir.data());
|
2021-08-14 13:29:12 +08:00
|
|
|
if (!dir) return;
|
2020-12-30 22:11:24 -08:00
|
|
|
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
|
|
|
string src = sdir + "/" + entry->d_name;
|
|
|
|
string dest = ddir + "/" + entry->d_name;
|
|
|
|
if (access(dest.data(), F_OK) == 0) {
|
|
|
|
if (entry->d_type == DT_DIR) {
|
|
|
|
// Recursive
|
|
|
|
magic_mount(src, dest);
|
|
|
|
} else {
|
|
|
|
LOGD("Mount [%s] -> [%s]\n", src.data(), dest.data());
|
|
|
|
xmount(src.data(), dest.data(), nullptr, MS_BIND, nullptr);
|
|
|
|
magic_mount_list += dest;
|
|
|
|
magic_mount_list += '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 01:08:28 -07:00
|
|
|
}
|
|
|
|
|
2022-04-08 00:20:21 -07:00
|
|
|
static void extract_files(bool sbin) {
|
|
|
|
const char *m32 = sbin ? "/sbin/magisk32.xz" : "magisk32.xz";
|
|
|
|
const char *m64 = sbin ? "/sbin/magisk64.xz" : "magisk64.xz";
|
2022-10-31 22:31:15 +08:00
|
|
|
const char *stub_xz = sbin ? "/sbin/stub.xz" : "stub.xz";
|
2022-04-08 00:20:21 -07:00
|
|
|
|
2022-07-23 04:01:21 +08:00
|
|
|
if (access(m32, F_OK) == 0) {
|
2023-06-03 03:16:03 -07:00
|
|
|
mmap_data magisk(m32);
|
2022-07-23 04:01:21 +08:00
|
|
|
unlink(m32);
|
|
|
|
int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755);
|
2023-09-21 05:47:21 -07:00
|
|
|
fd_channel ch(fd);
|
|
|
|
unxz(ch, magisk);
|
2022-07-23 04:01:21 +08:00
|
|
|
close(fd);
|
|
|
|
}
|
2022-04-08 00:20:21 -07:00
|
|
|
if (access(m64, F_OK) == 0) {
|
2023-06-03 03:16:03 -07:00
|
|
|
mmap_data magisk(m64);
|
2022-04-08 00:20:21 -07:00
|
|
|
unlink(m64);
|
2022-07-23 04:01:21 +08:00
|
|
|
int fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755);
|
2023-09-21 05:47:21 -07:00
|
|
|
fd_channel ch(fd);
|
|
|
|
unxz(ch, magisk);
|
2022-04-08 00:20:21 -07:00
|
|
|
close(fd);
|
|
|
|
xsymlink("./magisk64", "magisk");
|
|
|
|
} else {
|
|
|
|
xsymlink("./magisk32", "magisk");
|
|
|
|
}
|
2022-12-26 16:06:28 -08:00
|
|
|
if (access(stub_xz, F_OK) == 0) {
|
2023-06-03 03:16:03 -07:00
|
|
|
mmap_data stub(stub_xz);
|
2022-10-31 22:31:15 +08:00
|
|
|
unlink(stub_xz);
|
|
|
|
int fd = xopen("stub.apk", O_WRONLY | O_CREAT, 0);
|
2023-09-21 05:47:21 -07:00
|
|
|
fd_channel ch(fd);
|
|
|
|
unxz(ch, stub);
|
2022-10-31 22:31:15 +08:00
|
|
|
close(fd);
|
|
|
|
}
|
2022-04-08 00:20:21 -07:00
|
|
|
}
|
|
|
|
|
2023-03-08 14:42:54 +08:00
|
|
|
void MagiskInit::parse_config_file() {
|
2023-03-08 06:52:09 +08:00
|
|
|
parse_prop_file("/data/.backup/.magisk", [&](auto key, auto value) -> bool {
|
2023-03-16 04:07:00 -07:00
|
|
|
if (key == "PREINITDEVICE") {
|
2023-03-16 21:37:29 +08:00
|
|
|
preinit_dev = value;
|
2023-06-14 17:05:49 -07:00
|
|
|
return false;
|
2023-03-08 14:42:54 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-06 15:39:48 -08:00
|
|
|
#define ROOTMIR MIRRDIR "/system_root"
|
|
|
|
#define NEW_INITRC_DIR "/system/etc/init/hw"
|
2020-04-12 05:34:56 -07:00
|
|
|
|
2022-12-14 21:14:12 +08:00
|
|
|
void MagiskInit::patch_ro_root() {
|
|
|
|
mount_list.emplace_back("/data");
|
2023-03-08 14:42:54 +08:00
|
|
|
parse_config_file();
|
2022-12-14 21:14:12 +08:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
string tmp_dir;
|
|
|
|
|
|
|
|
if (access("/sbin", F_OK) == 0) {
|
|
|
|
tmp_dir = "/sbin";
|
|
|
|
} else {
|
2023-04-03 02:02:22 +08:00
|
|
|
tmp_dir = "/debug_ramdisk";
|
|
|
|
xmkdir("/data/debug_ramdisk", 0);
|
|
|
|
xmount("/debug_ramdisk", "/data/debug_ramdisk", nullptr, MS_MOVE, nullptr);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
setup_tmp(tmp_dir.data());
|
2023-04-05 18:08:38 +08:00
|
|
|
chdir(tmp_dir.data());
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2023-04-04 10:41:20 +08:00
|
|
|
if (tmp_dir == "/sbin") {
|
2023-04-07 01:54:40 +08:00
|
|
|
// Recreate original sbin structure
|
2023-04-04 10:41:20 +08:00
|
|
|
xmkdir(ROOTMIR, 0755);
|
|
|
|
xmount("/", ROOTMIR, nullptr, MS_BIND, nullptr);
|
2020-12-30 22:11:24 -08:00
|
|
|
recreate_sbin(ROOTMIR "/sbin", true);
|
2023-04-04 10:41:20 +08:00
|
|
|
xumount2(ROOTMIR, MNT_DETACH);
|
2023-04-03 02:02:22 +08:00
|
|
|
} else {
|
|
|
|
// Restore debug_ramdisk
|
|
|
|
xmount("/data/debug_ramdisk", "/debug_ramdisk", nullptr, MS_MOVE, nullptr);
|
|
|
|
rmdir("/data/debug_ramdisk");
|
2023-04-04 10:41:20 +08:00
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2022-12-14 21:14:12 +08:00
|
|
|
xrename("overlay.d", ROOTOVL);
|
Introduce new sepolicy injection mechanism
In the current implementation, Magisk will either have to recreate
all early mount implementation (for legacy SAR and rootfs devices) or
delegate early mount to first stage init (for 2SI devices) to access
required partitions for loading sepolicy. It then has to recreate the
split sepolicy loading implementation in-house, apply patches, then
dump the compiled + patched policies into monolithic format somewhere.
Finally, it patches the original init to force it to load the sepolicy
file we just created.
With the increasing complexity involved in early mount and split
sepolicy (there is even APEX module involved in the future!),
it is about time to rethink Magisk's sepolicy strategy as rebuilding
init's functionality is not scalable and easy to maintain.
In this commit, instead of building sepolicy ourselves, we mock
selinuxfs with FIFO files connected to a pre-init daemon, waiting
for the actual init process to directly write the sepolicy file into
MagiskInit. We then patch the file and load it into the kernel. Some
FIFO tricks has to be used to hijack the original init process's
control flow and prevent race conditions, details are directly in the
comments in code.
At the moment, only system-as-root (read-only root) support is added.
Support for legacy rootfs devices will come with a follow up commit.
2022-03-16 00:31:53 -07:00
|
|
|
|
2023-04-25 23:34:45 -07:00
|
|
|
extern bool avd_hack;
|
Introduce new sepolicy injection mechanism
In the current implementation, Magisk will either have to recreate
all early mount implementation (for legacy SAR and rootfs devices) or
delegate early mount to first stage init (for 2SI devices) to access
required partitions for loading sepolicy. It then has to recreate the
split sepolicy loading implementation in-house, apply patches, then
dump the compiled + patched policies into monolithic format somewhere.
Finally, it patches the original init to force it to load the sepolicy
file we just created.
With the increasing complexity involved in early mount and split
sepolicy (there is even APEX module involved in the future!),
it is about time to rethink Magisk's sepolicy strategy as rebuilding
init's functionality is not scalable and easy to maintain.
In this commit, instead of building sepolicy ourselves, we mock
selinuxfs with FIFO files connected to a pre-init daemon, waiting
for the actual init process to directly write the sepolicy file into
MagiskInit. We then patch the file and load it into the kernel. Some
FIFO tricks has to be used to hijack the original init process's
control flow and prevent race conditions, details are directly in the
comments in code.
At the moment, only system-as-root (read-only root) support is added.
Support for legacy rootfs devices will come with a follow up commit.
2022-03-16 00:31:53 -07:00
|
|
|
// Handle avd hack
|
|
|
|
if (avd_hack) {
|
2020-12-30 22:11:24 -08:00
|
|
|
int src = xopen("/init", O_RDONLY | O_CLOEXEC);
|
2023-06-03 03:16:03 -07:00
|
|
|
mmap_data init("/init");
|
Introduce new sepolicy injection mechanism
In the current implementation, Magisk will either have to recreate
all early mount implementation (for legacy SAR and rootfs devices) or
delegate early mount to first stage init (for 2SI devices) to access
required partitions for loading sepolicy. It then has to recreate the
split sepolicy loading implementation in-house, apply patches, then
dump the compiled + patched policies into monolithic format somewhere.
Finally, it patches the original init to force it to load the sepolicy
file we just created.
With the increasing complexity involved in early mount and split
sepolicy (there is even APEX module involved in the future!),
it is about time to rethink Magisk's sepolicy strategy as rebuilding
init's functionality is not scalable and easy to maintain.
In this commit, instead of building sepolicy ourselves, we mock
selinuxfs with FIFO files connected to a pre-init daemon, waiting
for the actual init process to directly write the sepolicy file into
MagiskInit. We then patch the file and load it into the kernel. Some
FIFO tricks has to be used to hijack the original init process's
control flow and prevent race conditions, details are directly in the
comments in code.
At the moment, only system-as-root (read-only root) support is added.
Support for legacy rootfs devices will come with a follow up commit.
2022-03-16 00:31:53 -07:00
|
|
|
// Force disable early mount on original init
|
2023-06-06 17:11:42 -07:00
|
|
|
for (size_t off : init.patch("android,fstab", "xxx")) {
|
|
|
|
LOGD("Patch @ %08zX [android,fstab] -> [xxx]\n", off);
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
int dest = xopen(ROOTOVL "/init", O_CREAT | O_WRONLY | O_CLOEXEC, 0);
|
2023-06-03 03:16:03 -07:00
|
|
|
xwrite(dest, init.buf(), init.sz());
|
2020-12-30 22:11:24 -08:00
|
|
|
fclone_attr(src, dest);
|
|
|
|
close(src);
|
|
|
|
close(dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
load_overlay_rc(ROOTOVL);
|
|
|
|
if (access(ROOTOVL "/sbin", F_OK) == 0) {
|
|
|
|
// Move files in overlay.d/sbin into tmp_dir
|
|
|
|
mv_path(ROOTOVL "/sbin", ".");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch init.rc
|
2023-11-06 15:39:48 -08:00
|
|
|
if (access(NEW_INITRC_DIR, F_OK) == 0) {
|
2020-12-30 22:11:24 -08:00
|
|
|
// Android 11's new init.rc
|
2023-11-06 15:39:48 -08:00
|
|
|
patch_rc_scripts(NEW_INITRC_DIR, tmp_dir.data(), false);
|
2021-10-29 18:21:20 +08:00
|
|
|
} else {
|
2023-11-06 15:39:48 -08:00
|
|
|
patch_rc_scripts("/", tmp_dir.data(), false);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
2021-01-18 04:25:26 -08:00
|
|
|
// Extract magisk
|
2022-04-08 00:20:21 -07:00
|
|
|
extract_files(false);
|
2021-01-18 04:25:26 -08:00
|
|
|
|
2022-04-06 17:37:04 +08:00
|
|
|
// Oculus Go will use a special sepolicy if unlocked
|
|
|
|
if (access("/sepolicy.unlocked", F_OK) == 0) {
|
|
|
|
patch_sepolicy("/sepolicy.unlocked", ROOTOVL "/sepolicy.unlocked");
|
2023-04-07 01:54:40 +08:00
|
|
|
} else if ((access(SPLIT_PLAT_CIL, F_OK) != 0 && access("/sepolicy", F_OK) == 0) ||
|
|
|
|
!hijack_sepolicy()) {
|
2022-04-06 17:37:04 +08:00
|
|
|
patch_sepolicy("/sepolicy", ROOTOVL "/sepolicy");
|
2022-03-17 22:32:49 -07:00
|
|
|
}
|
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// Mount rootdir
|
|
|
|
magic_mount(ROOTOVL);
|
2021-01-18 04:25:26 -08:00
|
|
|
int dest = xopen(ROOTMNT, O_WRONLY | O_CREAT, 0);
|
2020-12-30 22:11:24 -08:00
|
|
|
write(dest, magic_mount_list.data(), magic_mount_list.length());
|
|
|
|
close(dest);
|
|
|
|
|
|
|
|
chdir("/");
|
2019-06-24 01:21:33 -07:00
|
|
|
}
|
|
|
|
|
2022-03-16 21:41:20 -07:00
|
|
|
void RootFSInit::prepare() {
|
2022-12-14 21:14:12 +08:00
|
|
|
prepare_data();
|
2022-03-16 21:41:20 -07:00
|
|
|
LOGD("Restoring /init\n");
|
|
|
|
rename(backup_init(), "/init");
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:36:38 +08:00
|
|
|
#define PRE_TMPSRC "/magisk"
|
|
|
|
#define PRE_TMPDIR PRE_TMPSRC "/tmp"
|
2020-11-02 23:20:38 -08:00
|
|
|
|
2022-03-14 04:22:09 -07:00
|
|
|
void MagiskInit::patch_rw_root() {
|
2022-12-14 21:14:12 +08:00
|
|
|
mount_list.emplace_back("/data");
|
2023-03-08 14:42:54 +08:00
|
|
|
parse_config_file();
|
|
|
|
|
2021-01-25 00:19:10 -08:00
|
|
|
// Create hardlink mirror of /sbin to /root
|
|
|
|
mkdir("/root", 0777);
|
|
|
|
clone_attr("/sbin", "/root");
|
|
|
|
link_path("/sbin", "/root");
|
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// Handle overlays
|
2023-01-25 17:54:13 +08:00
|
|
|
load_overlay_rc("/overlay.d");
|
|
|
|
mv_path("/overlay.d", "/");
|
|
|
|
rm_rf("/data/overlay.d");
|
2022-03-16 20:01:26 -07:00
|
|
|
rm_rf("/.backup");
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2022-03-16 20:01:26 -07:00
|
|
|
// Patch init.rc
|
2023-11-06 15:39:48 -08:00
|
|
|
patch_rc_scripts("/", "/sbin", true);
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2022-03-19 17:07:46 -07:00
|
|
|
bool treble;
|
|
|
|
{
|
|
|
|
auto init = mmap_data("/init");
|
|
|
|
treble = init.contains(SPLIT_PLAT_CIL);
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:36:38 +08:00
|
|
|
xmkdir(PRE_TMPSRC, 0);
|
|
|
|
xmount("tmpfs", PRE_TMPSRC, "tmpfs", 0, "mode=755");
|
2022-03-16 20:01:26 -07:00
|
|
|
xmkdir(PRE_TMPDIR, 0);
|
|
|
|
setup_tmp(PRE_TMPDIR);
|
|
|
|
chdir(PRE_TMPDIR);
|
|
|
|
|
2022-04-08 00:20:21 -07:00
|
|
|
// Extract magisk
|
|
|
|
extract_files(true);
|
2022-03-16 20:01:26 -07:00
|
|
|
|
2022-04-01 13:16:23 +08:00
|
|
|
if ((!treble && access("/sepolicy", F_OK) == 0) || !hijack_sepolicy()) {
|
2022-04-06 17:37:04 +08:00
|
|
|
patch_sepolicy("/sepolicy", "/sepolicy");
|
2022-03-16 20:01:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
chdir("/");
|
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// Dump magiskinit as magisk
|
2022-12-14 21:14:12 +08:00
|
|
|
cp_afc(REDIR_PATH, "/sbin/magisk");
|
2020-11-02 23:20:38 -08:00
|
|
|
}
|
|
|
|
|
2022-03-16 20:01:26 -07:00
|
|
|
int magisk_proxy_main(int argc, char *argv[]) {
|
2023-05-02 16:49:43 -07:00
|
|
|
rust::setup_klog();
|
2022-03-16 20:01:26 -07:00
|
|
|
LOGD("%s\n", __FUNCTION__);
|
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// Mount rootfs as rw to do post-init rootfs patches
|
|
|
|
xmount(nullptr, "/", nullptr, MS_REMOUNT, nullptr);
|
2020-11-02 23:20:38 -08:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
unlink("/sbin/magisk");
|
2019-06-22 03:14:33 -07:00
|
|
|
|
2022-03-16 20:01:26 -07:00
|
|
|
// Move tmpfs to /sbin
|
2023-02-12 16:36:38 +08:00
|
|
|
// make parent private before MS_MOVE
|
|
|
|
xmount(nullptr, PRE_TMPSRC, nullptr, MS_PRIVATE, nullptr);
|
|
|
|
xmount(PRE_TMPDIR, "/sbin", nullptr, MS_MOVE, nullptr);
|
|
|
|
xumount2(PRE_TMPSRC, MNT_DETACH);
|
2022-03-16 20:01:26 -07:00
|
|
|
rmdir(PRE_TMPDIR);
|
2023-02-12 16:36:38 +08:00
|
|
|
rmdir(PRE_TMPSRC);
|
2021-01-18 04:25:26 -08:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// Create symlinks pointing back to /root
|
|
|
|
recreate_sbin("/root", false);
|
2019-06-22 03:14:33 -07:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// Tell magiskd to remount rootfs
|
|
|
|
setenv("REMOUNT_ROOT", "1", 1);
|
|
|
|
execv("/sbin/magisk", argv);
|
2022-03-16 20:01:26 -07:00
|
|
|
return 1;
|
2019-06-22 03:14:33 -07:00
|
|
|
}
|