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"