app: don't pass file names through the shell

The module zip's display name and path were pasted into the root shell
command that runs the installer, inside single quotes. The display name
comes from the content provider, which belongs to another app for
external install requests. A name with an apostrophe broke the install,
and a crafted name could run arbitrary commands as root.

Print the header line directly to the terminal instead of with echo,
without control characters, and quote the paths with a new
shellQuoted() helper, which also replaces the manual escaping in
runSuCommand.

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 a2f1a7a8fc
commit 4384cf49fa
2 changed files with 11 additions and 5 deletions
@@ -43,6 +43,9 @@ fun TerminalEmulator.appendLineOnMain(line: String) {
}
}
/** Quote a string as a single shell word, so it is never interpreted by the shell. */
fun String.shellQuoted() = "'" + replace("'", "'\\''") + "'"
/**
* Run a command as root inside a PTY (via busybox script).
* Reads raw bytes from the process and feeds them to the terminal emulator.
@@ -55,12 +58,11 @@ fun runSuCommand(emulator: TerminalEmulator, columns: Int, rows: Int, command: S
val exitFile = File.createTempFile("exit", null, AppContext.cacheDir)
return try {
val wrappedCmd = "export TERM=xterm-256color; stty cols $columns rows $rows 2>/dev/null; " +
"($command); echo \$? > '${exitFile.path}'"
val escapedCmd = wrappedCmd.replace("'", "'\\''")
"($command); echo \$? > ${exitFile.path.shellQuoted()}"
val process = ProcessBuilder(
"su", "-c",
"$busyboxPath script -q -c '$escapedCmd' /dev/null"
"$busyboxPath script -q -c ${wrappedCmd.shellQuoted()} /dev/null"
).redirectErrorStream(true).start()
process.outputStream.close()
@@ -21,6 +21,7 @@ import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.terminal.TerminalEmulator
import com.topjohnwu.magisk.terminal.appendLineOnMain
import com.topjohnwu.magisk.terminal.runSuCommand
import com.topjohnwu.magisk.terminal.shellQuoted
import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.CompletableDeferred
@@ -176,11 +177,14 @@ class FlashViewModel(private val savedState: SavedStateHandle) : BaseViewModel()
terminalReady.await()
val columns = emu.mColumns
val rows = emu.mRows
// The name and path come from other apps, so never let the shell interpret them, and
// drop control characters from the name so it can't send escape sequences to the terminal
emu.appendLineOnMain("- Installing ${displayName.filterNot { it.isISOControl() }}")
val success = withContext(Dispatchers.IO) {
runSuCommand(
emu, columns, rows,
"echo '- Installing $displayName'; " +
"sh $dir/update-binary dummy 1 '${zipFile.absolutePath}'; " +
"sh ${File(dir, "update-binary").path.shellQuoted()} dummy 1 " +
"${zipFile.absolutePath.shellQuoted()}; " +
"EXIT=\$?; " +
"if [ \$EXIT -ne 0 ]; then echo '! Installation failed'; fi; " +
"exit \$EXIT"