From 1fbd053a420b82ad629c4bed5d8f1a736d85618e Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 1 Jul 2024 18:20:21 -0700 Subject: [PATCH] Prevent polluting global shell env --- .../java/com/topjohnwu/magisk/core/Info.kt | 54 +++++++++++++++---- .../magisk/core/tasks/MagiskInstaller.kt | 29 +++++----- .../topjohnwu/magisk/core/utils/ShellInit.kt | 24 ++------- app/src/main/res/raw/manager.sh | 20 ++++++- 4 files changed, 79 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Info.kt b/app/src/main/java/com/topjohnwu/magisk/core/Info.kt index c292e8451..fba43bf6d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Info.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Info.kt @@ -7,8 +7,11 @@ import com.topjohnwu.magisk.core.di.AppContext import com.topjohnwu.magisk.core.ktx.getProperty import com.topjohnwu.magisk.core.model.UpdateInfo import com.topjohnwu.magisk.core.repository.NetworkService +import com.topjohnwu.superuser.CallbackList import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.ShellUtils.fastCmd +import com.topjohnwu.superuser.ShellUtils.fastCmdResult +import kotlinx.coroutines.Runnable val isRunningAsStub get() = Info.stub != null @@ -24,19 +27,25 @@ object Info { } else remote } - // Device state + var isRooted = false + var noDataExec = false + var patchBootVbmeta = false + @JvmStatic var env = Env() private set - @JvmField var isSAR = false + @JvmStatic var isSAR = false + private set var legacySAR = false + private set var isAB = false + private set + var slot = "" + private set @JvmField val isZygiskEnabled = System.getenv("ZYGISK_ENABLED") == "1" @JvmStatic val isFDE get() = crypto == "block" - @JvmField var ramdisk = false - var patchBootVbmeta = false - var crypto = "" - var noDataExec = false - var isRooted = false + @JvmStatic var ramdisk = false + private set + private var crypto = "" var hasGMS = true val isEmulator = @@ -69,12 +78,39 @@ object Info { fun init(shell: Shell) { if (shell.isRoot) { - isRooted = true - val v = fastCmd(shell, "magisk -v").split(":".toRegex()) + val v = fastCmd(shell, "magisk -v").split(":") env = Env( v[0], v.size >= 3 && v[2] == "D", runCatching { fastCmd("magisk -V").toInt() }.getOrDefault(-1) ) + Config.denyList = fastCmdResult(shell, "magisk --denylist status") } + + val map = mutableMapOf() + val list = object : CallbackList(Runnable::run) { + override fun onAddElement(e: String) { + val split = e.split("=") + if (split.size >= 2) { + map[split[0]] = split[1] + } + } + } + shell.newJob().add("(app_init)").to(list).exec() + + fun getVar(name: String) = map[name] ?: "" + fun getBool(name: String) = map[name].toBoolean() + + isSAR = getBool("SYSTEM_AS_ROOT") + ramdisk = getBool("RAMDISKEXIST") + isAB = getBool("ISAB") + patchBootVbmeta = getBool("PATCHVBMETAFLAG") + crypto = getVar("CRYPTOTYPE") + slot = getVar("SLOT") + legacySAR = getBool("LEGACYSAR") + + // Default presets + Config.recovery = getBool("RECOVERYMODE") + Config.keepVerity = getBool("KEEPVERITY") + Config.keepEnc = getBool("KEEPFORCEENCRYPT") } } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index accb72f3a..42620a449 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -82,8 +82,11 @@ abstract class MagiskInstallImpl protected constructor( } } - private fun findImage(): Boolean { - val bootPath = "RECOVERYMODE=${Config.recovery} find_boot_image; echo \"\$BOOTIMAGE\"".fsh() + private fun findImage(slot: String): Boolean { + val bootPath = ( + "(RECOVERYMODE=${Config.recovery} " + + "SLOT=$slot find_boot_image; " + + "echo \$BOOTIMAGE)").fsh() if (bootPath.isEmpty()) { console.add("! Unable to detect target image") return false @@ -93,22 +96,14 @@ abstract class MagiskInstallImpl protected constructor( return true } + private fun findImage(): Boolean { + return findImage(Info.slot) + } + private fun findSecondary(): Boolean { - val slot = "echo \$SLOT".fsh() - val target = if (slot == "_a") "_b" else "_a" - console.add("- Target slot: $target") - val bootPath = arrayOf( - "SLOT=$target", - "find_boot_image", - "SLOT=$slot", - "echo \"\$BOOTIMAGE\"").fsh() - if (bootPath.isEmpty()) { - console.add("! Unable to detect target image") - return false - } - srcBoot = rootFS.getFile(bootPath) - console.add("- Target image: $bootPath") - return true + val slot = if (Info.slot == "_a") "_b" else "_a" + console.add("- Target slot: $slot") + return findImage(slot) } private suspend fun extractFiles(): Boolean { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/utils/ShellInit.kt b/app/src/main/java/com/topjohnwu/magisk/core/utils/ShellInit.kt index 0537ba3b9..3182c87f3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/utils/ShellInit.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/utils/ShellInit.kt @@ -20,8 +20,8 @@ import java.util.jar.JarFile class ShellInit : Shell.Initializer() { override fun onInit(context: Context, shell: Shell): Boolean { - Info.init(shell) if (shell.isRoot) { + Info.isRooted = true RootUtils.bindTask?.let { shell.execTask(it) } RootUtils.bindTask = null } @@ -47,7 +47,8 @@ class ShellInit : Shell.Initializer() { if (shell.isRoot) { add("export MAGISKTMP=\$(magisk --path)") // Test if we can properly execute stuff in /data - Info.noDataExec = !shell.newJob().add("$localBB sh -c \"$localBB true\"").exec().isSuccess + Info.noDataExec = !shell.newJob() + .add("$localBB sh -c '$localBB true'").exec().isSuccess } if (Info.noDataExec) { @@ -70,26 +71,9 @@ class ShellInit : Shell.Initializer() { if (shell.isRoot) { add(context.assets.open("util_functions.sh")) } - add("app_init") }.exec() - fun fastCmd(cmd: String) = ShellUtils.fastCmd(shell, cmd) - fun getVar(name: String) = fastCmd("echo \$$name") - fun getBool(name: String) = getVar(name).toBoolean() - - Info.isSAR = getBool("SYSTEM_AS_ROOT") - Info.ramdisk = getBool("RAMDISKEXIST") - Info.isAB = getBool("ISAB") - Info.crypto = getVar("CRYPTOTYPE") - Info.patchBootVbmeta = getBool("PATCHVBMETAFLAG") - Info.legacySAR = getBool("LEGACYSAR") - - // Default presets - Config.recovery = getBool("RECOVERYMODE") - Config.keepVerity = getBool("KEEPVERITY") - Config.keepEnc = getBool("KEEPFORCEENCRYPT") - Config.denyList = shell.newJob().add("magisk --denylist status").exec().isSuccess - + Info.init(shell) return true } } diff --git a/app/src/main/res/raw/manager.sh b/app/src/main/res/raw/manager.sh index 1d2342909..aa82eb277 100644 --- a/app/src/main/res/raw/manager.sh +++ b/app/src/main/res/raw/manager.sh @@ -186,6 +186,10 @@ check_encryption() { fi } +printvar() { + eval echo $1=\$$1 +} + ########################## # Non-root util_functions ########################## @@ -224,13 +228,25 @@ grep_prop() { return; } ############# app_init() { - mount_partitions + mount_partitions >/dev/null RAMDISKEXIST=false check_boot_ramdisk && RAMDISKEXIST=true - get_flags + get_flags >/dev/null run_migrations SHA1=$(grep_prop SHA1 $MAGISKTMP/.magisk/config) check_encryption + + # Dump variables + printvar SLOT + printvar SYSTEM_AS_ROOT + printvar RAMDISKEXIST + printvar ISAB + printvar CRYPTOTYPE + printvar PATCHVBMETAFLAG + printvar LEGACYSAR + printvar RECOVERYMODE + printvar KEEPVERITY + printvar KEEPFORCEENCRYPT } export BOOTMODE=true