diff options
author | Samuel Holland <samuel@sholland.org> | 2018-01-09 09:16:53 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-01-09 09:16:53 -0600 |
commit | 3d6737e32f86f6220a940b2a8b80fa21e02e06b1 (patch) | |
tree | 863e6e67a954e33931d5b6f7e98b62da0b69ec16 /app/src/main | |
parent | 643b698f3096ab8e48ef124e76752f1e30be3804 (diff) |
RootShell: Be stricter about command delimiters
Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/com/wireguard/android/util/RootShell.java | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/app/src/main/java/com/wireguard/android/util/RootShell.java b/app/src/main/java/com/wireguard/android/util/RootShell.java index a565523b..1e00020f 100644 --- a/app/src/main/java/com/wireguard/android/util/RootShell.java +++ b/app/src/main/java/com/wireguard/android/util/RootShell.java @@ -81,30 +81,41 @@ public class RootShell { throws IOException, NoRootException { start(); final String marker = UUID.randomUUID().toString(); - final String script = '(' + command + "); ret=$?; echo " + marker + " $ret; " - + "echo " + marker + " $ret >&2\n"; - Log.v(TAG, "executing: " + script); + final String script = "echo " + marker + "; echo " + marker + " >&2; (" + command + + "); ret=$?; echo " + marker + " $ret; echo " + marker + " $ret >&2\n"; + Log.v(TAG, "executing: " + command); stdin.write(script); stdin.flush(); String line; int errnoStdout = Integer.MIN_VALUE; int errnoStderr = Integer.MAX_VALUE; + int markersSeen = 0; while ((line = stdout.readLine()) != null) { - if (line.startsWith(marker) && line.length() > marker.length() + 1) { - errnoStdout = Integer.valueOf(line.substring(marker.length() + 1)); - break; + if (line.startsWith(marker)) { + ++markersSeen; + if (line.length() > marker.length() + 1) { + errnoStdout = Integer.valueOf(line.substring(marker.length() + 1)); + break; + } + } else if (markersSeen > 0) { + if (output != null) + output.add(line); + Log.v(TAG, "stdout: " + line); } - if (output != null) - output.add(line); - Log.v(TAG, "stdout: " + line); } while ((line = stderr.readLine()) != null) { - if (line.startsWith(marker) && line.length() > marker.length() + 1) { - errnoStderr = Integer.valueOf(line.substring(marker.length() + 1)); - break; + if (line.startsWith(marker)) { + ++markersSeen; + if (line.length() > marker.length() + 1) { + errnoStderr = Integer.valueOf(line.substring(marker.length() + 1)); + break; + } + } else if (markersSeen > 2) { + Log.v(TAG, "stderr: " + line); } - Log.v(TAG, "stderr: " + line); } + if (markersSeen != 4) + throw new IOException("Expected 4 markers, received " + markersSeen); if (errnoStdout != errnoStderr) throw new IOException("Unable to read exit status"); Log.v(TAG, "exit: " + errnoStdout); |