mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-22 23:26:42 -08:00
Introduce new root overlay system
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <vector>
|
||||
|
||||
#include <magisk.h>
|
||||
#include <magiskpolicy.h>
|
||||
@@ -31,6 +32,8 @@ static void patch_socket_name(const char *path) {
|
||||
munmap(buf, size);
|
||||
}
|
||||
|
||||
static vector<raw_data> rc_list;
|
||||
|
||||
static void patch_init_rc(FILE *rc) {
|
||||
file_readline("/init.rc", [=](string_view line) -> bool {
|
||||
// Do not start vaultkeeper
|
||||
@@ -48,6 +51,15 @@ static void patch_init_rc(FILE *rc) {
|
||||
fprintf(rc, "%s", line.data());
|
||||
return true;
|
||||
});
|
||||
|
||||
fprintf(rc, "\n");
|
||||
|
||||
// Inject custom rc scripts
|
||||
for (auto &d : rc_list)
|
||||
fprintf(rc, "\n%s\n", d.buf);
|
||||
rc_list.clear();
|
||||
|
||||
// Inject Magisk rc scripts
|
||||
char pfd_svc[16], ls_svc[16], bc_svc[16];
|
||||
gen_rand_str(pfd_svc, sizeof(pfd_svc));
|
||||
gen_rand_str(ls_svc, sizeof(ls_svc));
|
||||
@@ -56,6 +68,24 @@ static void patch_init_rc(FILE *rc) {
|
||||
fprintf(rc, magiskrc, pfd_svc, pfd_svc, ls_svc, bc_svc, bc_svc);
|
||||
}
|
||||
|
||||
static void load_overlay_rc(int dirfd) {
|
||||
// Do not allow overwrite init.rc
|
||||
unlinkat(dirfd, "init.rc", 0);
|
||||
DIR *dir = fdopendir(dirfd);
|
||||
for (dirent *entry; (entry = readdir(dir));) {
|
||||
if (strend(entry->d_name, ".rc") == 0) {
|
||||
LOGD("Found rc script [%s]\n", entry->d_name);
|
||||
int rc = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
|
||||
raw_data data;
|
||||
fd_full_read(rc, data.buf, data.sz);
|
||||
close(rc);
|
||||
rc_list.push_back(std::move(data));
|
||||
unlinkat(dirfd, entry->d_name, 0);
|
||||
}
|
||||
}
|
||||
rewinddir(dir);
|
||||
}
|
||||
|
||||
void RootFSInit::setup_rootfs() {
|
||||
if (patch_sepolicy()) {
|
||||
char *addr;
|
||||
@@ -72,7 +102,7 @@ void RootFSInit::setup_rootfs() {
|
||||
munmap(addr, size);
|
||||
}
|
||||
|
||||
// Handle ramdisk overlays
|
||||
// Handle legacy overlays
|
||||
int fd = open("/overlay", O_RDONLY | O_CLOEXEC);
|
||||
if (fd >= 0) {
|
||||
LOGD("Merge overlay folder\n");
|
||||
@@ -81,6 +111,16 @@ void RootFSInit::setup_rootfs() {
|
||||
rmdir("/overlay");
|
||||
}
|
||||
|
||||
// Handle overlays
|
||||
fd = open("/overlay.d", O_RDONLY | O_CLOEXEC);
|
||||
if (fd >= 0) {
|
||||
LOGD("Merge overlay.d\n");
|
||||
load_overlay_rc(fd);
|
||||
mv_dir(fd, root);
|
||||
close(fd);
|
||||
rmdir("/overlay.d");
|
||||
}
|
||||
|
||||
// Patch init.rc
|
||||
FILE *rc = xfopen("/init.p.rc", "we");
|
||||
patch_init_rc(rc);
|
||||
@@ -185,18 +225,40 @@ static void sbin_overlay(const raw_data &self, const raw_data &config) {
|
||||
xsymlink("./magiskinit", "/sbin/supolicy");
|
||||
}
|
||||
|
||||
#define ROOTOVL MAGISKTMP "/rootdir"
|
||||
#define ROOTMIR MIRRDIR "/system_root"
|
||||
#define ROOTBLK BLOCKDIR "/system_root"
|
||||
#define MONOPOLICY "/sepolicy"
|
||||
#define PATCHPOLICY "/sbin/.se"
|
||||
#define LIBSELINUX "/system/" LIBNAME "/libselinux.so"
|
||||
|
||||
static void magic_mount(int dirfd, const string &path) {
|
||||
DIR *dir = xfdopendir(dirfd);
|
||||
for (dirent *entry; (entry = readdir(dir));) {
|
||||
if (entry->d_name == "."sv || entry->d_name == ".."sv)
|
||||
continue;
|
||||
string dest = path + "/" + entry->d_name;
|
||||
if (access(dest.data(), F_OK) == 0) {
|
||||
if (entry->d_type == DT_DIR) {
|
||||
// Recursive
|
||||
int fd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
|
||||
magic_mount(fd, dest);
|
||||
close(fd);
|
||||
} else {
|
||||
string src = ROOTOVL + dest;
|
||||
LOGD("Mount [%s] -> [%s]\n", src.data(), dest.data());
|
||||
xmount(src.data(), dest.data(), nullptr, MS_BIND, nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SARCommon::patch_rootdir() {
|
||||
sbin_overlay(self, config);
|
||||
|
||||
// Mount system_root mirror
|
||||
xmkdir(MIRRDIR, 0);
|
||||
xmkdir(ROOTMIR, 0755);
|
||||
xmkdir(ROOTMIR, 0);
|
||||
xmkdir(BLOCKDIR, 0);
|
||||
mknod(ROOTBLK, S_IFBLK | 0600, system_dev);
|
||||
if (xmount(ROOTBLK, ROOTMIR, "ext4", MS_RDONLY, nullptr))
|
||||
@@ -231,20 +293,16 @@ void SARCommon::patch_rootdir() {
|
||||
close(src);
|
||||
close(dest);
|
||||
|
||||
/* ******************
|
||||
* Customize rootdir
|
||||
* ******************/
|
||||
|
||||
char *addr;
|
||||
size_t size;
|
||||
// Patch init
|
||||
raw_data init;
|
||||
file_attr attr;
|
||||
bool redirect = false;
|
||||
xmkdir(ROOTOVERLAY, 0);
|
||||
src = xopen("/init", O_RDONLY | O_CLOEXEC);
|
||||
fd_full_read(src, init.buf, init.sz);
|
||||
fgetattr(src, &attr);
|
||||
fd_full_read(src, (void**)&addr, &size);
|
||||
close(src);
|
||||
for (char *p = addr; p < addr + size; ++p) {
|
||||
uint8_t *eof = init.buf + init.sz;
|
||||
for (uint8_t *p = init.buf; p < eof; ++p) {
|
||||
if (memcmp(p, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL)) == 0) {
|
||||
// Force init to load monolithic policy
|
||||
LOGD("Remove from init: " SPLIT_PLAT_CIL "\n");
|
||||
@@ -258,18 +316,20 @@ void SARCommon::patch_rootdir() {
|
||||
p += sizeof(MONOPOLICY) - 1;
|
||||
}
|
||||
}
|
||||
dest = xopen(ROOTOVERLAY "/init", O_CREAT | O_WRONLY | O_CLOEXEC);
|
||||
xwrite(dest, addr, size);
|
||||
free(addr);
|
||||
xmkdir(ROOTOVL, 0);
|
||||
dest = xopen(ROOTOVL "/init", O_CREAT | O_WRONLY | O_CLOEXEC);
|
||||
xwrite(dest, init.buf, init.sz);
|
||||
fsetattr(dest, &attr);
|
||||
close(dest);
|
||||
xmount(ROOTOVERLAY "/init", "/init", nullptr, MS_BIND, nullptr);
|
||||
|
||||
// Patch libselinux
|
||||
if (!redirect) {
|
||||
raw_data lib;
|
||||
// init is dynamically linked, need to patch libselinux
|
||||
full_read(LIBSELINUX, (void**)&addr, &size);
|
||||
full_read(LIBSELINUX, lib.buf, lib.sz);
|
||||
getattr(LIBSELINUX, &attr);
|
||||
for (char *p = addr; p < addr + size; ++p) {
|
||||
eof = lib.buf + lib.sz;
|
||||
for (uint8_t *p = lib.buf; p < eof; ++p) {
|
||||
if (memcmp(p, MONOPOLICY, sizeof(MONOPOLICY)) == 0) {
|
||||
// Redirect /sepolicy to tmpfs
|
||||
LOGD("Patch libselinux.so [" MONOPOLICY "] -> [" PATCHPOLICY "]\n");
|
||||
@@ -277,26 +337,50 @@ void SARCommon::patch_rootdir() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
dest = xopen(ROOTOVERLAY "/libselinux.so", O_CREAT | O_WRONLY | O_CLOEXEC);
|
||||
xwrite(dest, addr, size);
|
||||
free(addr);
|
||||
xmkdir(ROOTOVL "/system", 0755);
|
||||
xmkdir(ROOTOVL "/system/" LIBNAME, 0755);
|
||||
dest = xopen(ROOTOVL LIBSELINUX, O_CREAT | O_WRONLY | O_CLOEXEC);
|
||||
xwrite(dest, lib.buf, lib.sz);
|
||||
fsetattr(dest, &attr);
|
||||
close(dest);
|
||||
xmount(ROOTOVERLAY "/libselinux.so", LIBSELINUX, nullptr, MS_BIND, nullptr);
|
||||
}
|
||||
|
||||
// sepolicy
|
||||
patch_sepolicy(PATCHPOLICY);
|
||||
|
||||
FILE *rc = xfopen(ROOTOVERLAY "/init.rc", "we");
|
||||
// Handle overlay
|
||||
if ((src = xopen("/dev/overlay.d", O_RDONLY | O_CLOEXEC)) >= 0) {
|
||||
load_overlay_rc(src);
|
||||
if (int fd = xopen("/dev/overlay.d/sbin", O_RDONLY | O_CLOEXEC); fd >= 0) {
|
||||
dest = xopen("/sbin", O_RDONLY | O_CLOEXEC);
|
||||
clone_dir(fd, dest);
|
||||
close(fd);
|
||||
close(dest);
|
||||
xmkdir(ROOTOVL "/sbin", 0); // Prevent copying
|
||||
}
|
||||
dest = xopen(ROOTOVL, O_RDONLY | O_CLOEXEC);
|
||||
clone_dir(src, dest, false);
|
||||
rmdir(ROOTOVL "/sbin");
|
||||
close(src);
|
||||
close(dest);
|
||||
rm_rf("/dev/overlay.d");
|
||||
}
|
||||
|
||||
// Patch init.rc
|
||||
FILE *rc = xfopen(ROOTOVL "/init.rc", "we");
|
||||
patch_init_rc(rc);
|
||||
fclose(rc);
|
||||
clone_attr("/init.rc", ROOTOVERLAY "/init.rc");
|
||||
xmount(ROOTOVERLAY "/init.rc", "/init.rc", nullptr, MS_BIND, nullptr);
|
||||
clone_attr("/init.rc", ROOTOVL "/init.rc");
|
||||
|
||||
// Mount rootdir
|
||||
src = xopen(ROOTOVL, O_RDONLY | O_CLOEXEC);
|
||||
magic_mount(src, "");
|
||||
close(src);
|
||||
}
|
||||
|
||||
#define FSR "/first_stage_ramdisk"
|
||||
|
||||
void FirstStageInit::patch_fstab() {
|
||||
void FirstStageInit::prepare() {
|
||||
// Find fstab
|
||||
DIR *dir = xopendir(FSR);
|
||||
if (!dir)
|
||||
@@ -352,8 +436,10 @@ void FirstStageInit::patch_fstab() {
|
||||
xmkdir(FSR "/system", 0755);
|
||||
xmkdir(FSR "/system/bin", 0755);
|
||||
rename("/init", FSR "/system/bin/init");
|
||||
symlink("/system/bin/init", FSR "/init");
|
||||
xmkdir(FSR "/.backup", 0);
|
||||
rename("/.backup/.magisk", FSR "/.backup/.magisk");
|
||||
rename("/overlay.d", FSR "/overlay.d");
|
||||
}
|
||||
|
||||
#ifdef MAGISK_DEBUG
|
||||
@@ -379,8 +465,8 @@ int magisk_proxy_main(int argc, char *argv[]) {
|
||||
raw_data config;
|
||||
raw_data self;
|
||||
|
||||
full_read("/sbin/magisk", &self.buf, &self.sz);
|
||||
full_read("/.backup/.magisk", &config.buf, &config.sz);
|
||||
full_read("/sbin/magisk", self.buf, self.sz);
|
||||
full_read("/.backup/.magisk", config.buf, config.sz);
|
||||
|
||||
xmount(nullptr, "/", nullptr, MS_REMOUNT, nullptr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user