From dfbd1305b310531d55da46d9542eeab788c68aef Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 19 Apr 2020 02:35:28 -0700 Subject: [PATCH] =?UTF-8?q?Android=2011=20support=20=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- native/jni/core/db.cpp | 4 +-- native/jni/init/rootdir.cpp | 52 +++++++++++++++++------------------- native/jni/init/twostage.cpp | 7 +++-- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/native/jni/core/db.cpp b/native/jni/core/db.cpp index 12baa732d..d5a7c5ead 100644 --- a/native/jni/core/db.cpp +++ b/native/jni/core/db.cpp @@ -57,9 +57,9 @@ static void (*android_update_LD_LIBRARY_PATH)(const char *ld_library_path); } #ifdef __LP64__ -constexpr char apex_path[] = "/apex/com.android.runtime/lib64:"; +constexpr char apex_path[] = "/apex/com.android.runtime/lib64:/apex/com.android.art/lib64:"; #else -constexpr char apex_path[] = "/apex/com.android.runtime/lib:"; +constexpr char apex_path[] = "/apex/com.android.runtime/lib:/apex/com.android.art/lib:"; #endif static int dl_init = 0; diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index dd51c66c7..40860f7e9 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -21,8 +22,9 @@ using namespace std; static vector rc_list; -static void patch_init_rc(FILE *rc, const char *tmp_dir) { - file_readline("/init.rc", [=](string_view line) -> bool { +static void patch_init_rc(const char *src, const char *dest, const char *tmp_dir) { + FILE *rc = xfopen(dest, "we"); + file_readline(src, [=](string_view line) -> bool { // Do not start vaultkeeper if (str_contains(line, "start vaultkeeper")) { LOGD("Remove vaultkeeper\n"); @@ -53,6 +55,9 @@ static void patch_init_rc(FILE *rc, const char *tmp_dir) { gen_rand_str(bc_svc, sizeof(bc_svc)); LOGD("Inject magisk services: [%s] [%s] [%s]\n", pfd_svc, ls_svc, bc_svc); fprintf(rc, magiskrc, tmp_dir, pfd_svc, ls_svc, bc_svc); + + fclose(rc); + clone_attr(src, dest); } static void load_overlay_rc(const char *overlay) { @@ -98,11 +103,7 @@ void RootFSInit::setup_rootfs() { mv_path("/overlay.d", "/"); } - // Patch init.rc - FILE *rc = xfopen("/init.p.rc", "we"); - patch_init_rc(rc, "/sbin"); - fclose(rc); - clone_attr("/init.rc", "/init.p.rc"); + patch_init_rc("/init.rc", "/init.p.rc", "/sbin"); rename("/init.p.rc", "/init.rc"); // Create hardlink mirror of /sbin to /root @@ -196,9 +197,7 @@ static string magic_mount_list; static void magic_mount(const string &sdir, const string &ddir = "") { auto dir = xopen_dir(sdir.data()); - for (dirent *entry; (entry = readdir(dir.get()));) { - if (entry->d_name == "."sv || entry->d_name == ".."sv) - continue; + 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) { @@ -215,10 +214,11 @@ static void magic_mount(const string &sdir, const string &ddir = "") { } } -#define ROOTMIR MIRRDIR "/system_root" -#define ROOTBLK BLOCKDIR "/system_root" +#define ROOTMIR MIRRDIR "/system_root" +#define ROOTBLK BLOCKDIR "/system_root" #define MONOPOLICY "/sepolicy" #define LIBSELINUX "/system/" LIBNAME "/libselinux.so" +#define NEW_INITRC "/system/etc/init/hw/init.rc" void SARBase::patch_rootdir() { char tmp_dir[16]; @@ -229,9 +229,8 @@ void SARBase::patch_rootdir() { p = tmp_dir + sprintf(tmp_dir, "%s", "/sbin"); sepol = "/sbin/.se"; } else { - strcpy(tmp_dir, "/dev/"); - p = tmp_dir + 5; - p += gen_rand_str(p, 10); + p = tmp_dir + sprintf(tmp_dir, "%s", "/dev/"); + p += gen_rand_str(p, 8); xmkdir(tmp_dir, 0); sepol = "/dev/.se"; } @@ -257,12 +256,9 @@ void SARBase::patch_rootdir() { // Patch init raw_data init; - file_attr attr; bool redirect = false; int src = xopen("/init", O_RDONLY | O_CLOEXEC); fd_full_read(src, init.buf, init.sz); - fgetattr(src, &attr); - close(src); uint8_t *eof = init.buf + init.sz; for (uint8_t *p = init.buf; p < eof;) { if (memcmp(p, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL)) == 0) { @@ -281,14 +277,14 @@ void SARBase::patch_rootdir() { xmkdir(ROOTOVL, 0); int dest = xopen(ROOTOVL "/init", O_CREAT | O_WRONLY | O_CLOEXEC); xwrite(dest, init.buf, init.sz); - fsetattr(dest, &attr); + fclone_attr(src, dest); + close(src); close(dest); if (!redirect) { // init is dynamically linked, need to patch libselinux raw_data lib; full_read(LIBSELINUX, lib.buf, lib.sz); - getattr(LIBSELINUX, &attr); eof = lib.buf + lib.sz; for (uint8_t *p = lib.buf; p < eof; ++p) { if (memcmp(p, MONOPOLICY, sizeof(MONOPOLICY)) == 0) { @@ -297,12 +293,11 @@ void SARBase::patch_rootdir() { break; } } - xmkdir(ROOTOVL "/system", 0755); - xmkdir(ROOTOVL "/system/" LIBNAME, 0755); + xmkdirs(dirname(ROOTOVL LIBSELINUX), 0755); dest = xopen(ROOTOVL LIBSELINUX, O_CREAT | O_WRONLY | O_CLOEXEC); xwrite(dest, lib.buf, lib.sz); - fsetattr(dest, &attr); close(dest); + clone_attr(LIBSELINUX, ROOTOVL LIBSELINUX); } // sepolicy @@ -333,10 +328,13 @@ void SARBase::patch_rootdir() { } // Patch init.rc - FILE *rc = xfopen(ROOTOVL "/init.rc", "we"); - patch_init_rc(rc, tmp_dir); - fclose(rc); - clone_attr("/init.rc", ROOTOVL "/init.rc"); + if (access("/init.rc", F_OK) == 0) { + patch_init_rc("/init.rc", ROOTOVL "/init.rc", tmp_dir); + } else { + // Android 11's new init.rc + xmkdirs(dirname(ROOTOVL NEW_INITRC), 0755); + patch_init_rc(NEW_INITRC, ROOTOVL NEW_INITRC, tmp_dir); + } // Mount rootdir magic_mount(ROOTOVL); diff --git a/native/jni/init/twostage.cpp b/native/jni/init/twostage.cpp index 4c8c6f633..7bfd6e70f 100644 --- a/native/jni/init/twostage.cpp +++ b/native/jni/init/twostage.cpp @@ -71,10 +71,9 @@ void ForcedFirstStageInit::prepare() { // Move stuffs for next stage xmkdir(FSR "/system", 0755); xmkdir(FSR "/system/bin", 0755); - rename("/init", FSR "/system/bin/init"); + rename("/init" /* magiskinit */, FSR "/system/bin/init"); symlink("/system/bin/init", FSR "/init"); - xmkdir(FSR "/.backup", 0); - rename("/.backup/.magisk", FSR "/.backup/.magisk"); + rename("/.backup", FSR "/.backup"); rename("/overlay.d", FSR "/overlay.d"); } @@ -90,7 +89,7 @@ void FirstStageInit::prepare() { // Move stuffs for next stage xmkdir("/system", 0755); xmkdir("/system/bin", 0755); - rename("/init", "/system/bin/init"); + rename("/init" /* magiskinit */ , "/system/bin/init"); rename("/.backup/init", "/init"); }