app: run module actions in BusyBox ash

Every script running in the context of Magisk is supposed to run in
BusyBox ash with standalone mode. Since module actions and installs run
in a PTY, they ran in /system/bin/sh instead: busybox script starts
$SHELL, which su sets to /system/bin/sh, and ASH_STANDALONE was never
set. Module installers recover by re-executing themselves in BusyBox,
but action.sh has no such fallback.

Run the PTY command in BusyBox ash with ASH_STANDALONE=1, which has to
be set before ash starts. Also run the action from the module's
directory name, quoted, rather than from the id in module.prop, which
can differ from it.

Assisted-by: Claude Opus 5.5
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 7dca458185
commit d582d8145d
6 changed files with 16 additions and 9 deletions
@@ -57,8 +57,13 @@ fun runSuCommand(emulator: TerminalEmulator, columns: Int, rows: Int, command: S
// busybox script always exits with 0, so have the command write its exit code to a file
val exitFile = File.createTempFile("exit", null, AppContext.cacheDir)
return try {
val wrappedCmd = "export TERM=xterm-256color; stty cols $columns rows $rows 2>/dev/null; " +
// Run the command in busybox ash with standalone mode, like every other script in the
// context of Magisk. busybox script itself starts $SHELL, which su sets to /system/bin/sh.
// ASH_STANDALONE has to be set before ash starts: it is only read on startup.
val shellCmd = "export TERM=xterm-256color; " +
"stty cols $columns rows $rows 2>/dev/null; " +
"($command); echo \$? > ${exitFile.path.shellQuoted()}"
val wrappedCmd = "ASH_STANDALONE=1 $busyboxPath sh -c ${shellCmd.shellQuoted()}"
val process = ProcessBuilder(
"su", "-c",
@@ -210,8 +210,8 @@ class MainActivity : ComponentActivity(), SplashScreenHost {
return@entry
}
LaunchedEffect(key) {
if (vm.actionId.isEmpty()) {
vm.actionId = key.id
if (vm.actionDir.isEmpty()) {
vm.actionDir = key.dir
vm.actionName = key.name
vm.startRunAction()
}
@@ -9,6 +9,7 @@ import com.topjohnwu.magisk.core.utils.MediaStoreUtils
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.terminal.TerminalEmulator
import com.topjohnwu.magisk.terminal.runSuCommand
import com.topjohnwu.magisk.terminal.shellQuoted
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
@@ -32,7 +33,7 @@ class ActionViewModel(private val savedState: SavedStateHandle) : BaseViewModel(
private val _actionState = MutableStateFlow(State.RUNNING)
val actionState: StateFlow<State> = _actionState.asStateFlow()
var actionId: String = ""
var actionDir: String = ""
var actionName: String = ""
val emulator = TerminalEmulator(
@@ -60,7 +61,7 @@ class ActionViewModel(private val savedState: SavedStateHandle) : BaseViewModel(
val success = withContext(Dispatchers.IO) {
runSuCommand(
emulator, columns, rows,
"cd /data/adb/modules/$actionId && sh ./action.sh"
"cd ${"/data/adb/modules/$actionDir".shellQuoted()} && sh ./action.sh"
)
}
@@ -379,7 +379,7 @@ private fun ModuleCard(
// Drop taps while a screen transition runs, so a quick
// second tap can't open another screen
onClick = dropUnlessResumed {
viewModel.runAction(item.module.id, item.module.name)
viewModel.runAction(item.module.base.name, item.module.name)
},
) {
Row(
@@ -117,8 +117,8 @@ class ModuleViewModel : AsyncLoadViewModel() {
navigateTo(Route.Flash(Const.Value.FLASH_ZIP, uri.toString()))
}
fun runAction(id: String, name: String) {
navigateTo(Route.Action(id, name))
fun runAction(dir: String, name: String) {
navigateTo(Route.Action(dir, name))
}
fun toggleEnabled(item: ModuleItem) {
@@ -26,7 +26,8 @@ sealed interface Route : NavKey, Parcelable {
@Parcelize
data class Action(
val id: String,
// The module's directory name: unlike the id in module.prop, it is unique
val dir: String,
val name: String,
) : Route
}