From f24d52436b533cce7a5e44813fd0765d0df7ff3d Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 8 Apr 2022 00:20:21 -0700 Subject: [PATCH] Deduplicate logic --- buildSrc/build.gradle.kts | 2 +- native/jni/init/rootdir.cpp | 87 +++++++++++++++---------------------- 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 1c0a1318a..dabf74745 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -25,7 +25,7 @@ tasks.withType { dependencies { implementation(kotlin("gradle-plugin", "1.6.10")) - implementation("com.android.tools.build:gradle:7.1.2") + implementation("com.android.tools.build:gradle:7.1.3") implementation("androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0-alpha01") implementation("io.michaelrocks:paranoid-gradle-plugin:0.3.7") implementation("org.eclipse.jgit:org.eclipse.jgit:5.12.0.202106070339-r") diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index d9e358cbd..e592b61d2 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -131,14 +131,6 @@ static void magic_mount(const string &sdir, const string &ddir = "") { } } -static void patch_socket_name(const char *path) { - static char rstr[16] = { 0 }; - if (rstr[0] == '\0') - gen_rand_str(rstr, sizeof(rstr)); - auto bin = mmap_data(path, true); - bin.patch({ make_pair(MAIN_SOCKET, rstr) }); -} - void SARBase::backup_files() { if (access("/overlay.d", F_OK) == 0) backup_folder("/overlay.d", overlays); @@ -152,6 +144,39 @@ void SARBase::backup_files() { magisk_cfg = mmap_data("/data/.backup/.magisk"); } +static void patch_socket_name(const char *path) { + static char rstr[16] = { 0 }; + if (rstr[0] == '\0') + gen_rand_str(rstr, sizeof(rstr)); + auto bin = mmap_data(path, true); + bin.patch({ make_pair(MAIN_SOCKET, rstr) }); +} + +static void extract_files(bool sbin) { + const char *m32 = sbin ? "/sbin/magisk32.xz" : "magisk32.xz"; + const char *m64 = sbin ? "/sbin/magisk64.xz" : "magisk64.xz"; + + auto magisk = mmap_data(m32); + unlink(m32); + int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755); + unxz(fd, magisk.buf, magisk.sz); + close(fd); + patch_socket_name("magisk32"); + if (access(m64, F_OK) == 0) { + magisk = mmap_data(m64); + unlink(m64); + fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755); + unxz(fd, magisk.buf, magisk.sz); + close(fd); + patch_socket_name("magisk64"); + xsymlink("./magisk64", "magisk"); + } else { + xsymlink("./magisk32", "magisk"); + } + + dump_manager("stub.apk", 0); +} + #define ROOTMIR MIRRDIR "/system_root" #define NEW_INITRC "/system/etc/init/hw/init.rc" @@ -215,27 +240,7 @@ void SARBase::patch_ro_root() { } // Extract magisk - { - auto magisk = mmap_data("magisk32.xz"); - unlink("magisk32.xz"); - int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755); - unxz(fd, magisk.buf, magisk.sz); - close(fd); - patch_socket_name("magisk32"); - if (access("magisk64.xz", F_OK) == 0) { - magisk = mmap_data("magisk64.xz"); - unlink("magisk64.xz"); - fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755); - unxz(fd, magisk.buf, magisk.sz); - close(fd); - patch_socket_name("magisk64"); - xsymlink("./magisk64", "magisk"); - } else { - xsymlink("./magisk32", "magisk"); - } - - dump_manager("stub.apk", 0644); - } + extract_files(false); if ((access(SPLIT_PLAT_CIL, F_OK) != 0 && access("/sepolicy", F_OK) == 0) || !hijack_sepolicy()) { patch_sepolicy(ROOTOVL "/sepolicy"); @@ -288,28 +293,8 @@ void MagiskInit::patch_rw_root() { setup_tmp(PRE_TMPDIR); chdir(PRE_TMPDIR); - { - // Extract magisk - auto magisk = mmap_data("/sbin/magisk32.xz"); - unlink("/sbin/magisk32.xz"); - int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755); - unxz(fd, magisk.buf, magisk.sz); - close(fd); - patch_socket_name("magisk32"); - if (access("/sbin/magisk64.xz", F_OK) == 0) { - magisk = mmap_data("/sbin/magisk64.xz"); - unlink("/sbin/magisk64.xz"); - fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755); - unxz(fd, magisk.buf, magisk.sz); - close(fd); - patch_socket_name("magisk64"); - xsymlink("./magisk64", "magisk"); - } else { - xsymlink("./magisk32", "magisk"); - } - - dump_manager("stub.apk", 0644); - } + // Extract magisk + extract_files(true); if ((!treble && access("/sepolicy", F_OK) == 0) || !hijack_sepolicy()) { patch_sepolicy("/sepolicy");