From 4384cf49fa5d934b7aefc47f8fd5606962bd937d Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Oct 2026 05:46:00 +0000 Subject: [PATCH] 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 --- .../java/com/topjohnwu/magisk/terminal/TerminalProcess.kt | 8 +++++--- .../java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt | 8 ++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt b/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt index 061d77ff2..bfbb47b9b 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt @@ -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() diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt index 7e7b38e25..94bad8e6d 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt @@ -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"