RootShell: Be stricter about command delimiters

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland 2018-01-09 09:16:53 -06:00
parent 643b698f30
commit 3d6737e32f

View File

@ -81,30 +81,41 @@ public class RootShell {
throws IOException, NoRootException { throws IOException, NoRootException {
start(); start();
final String marker = UUID.randomUUID().toString(); final String marker = UUID.randomUUID().toString();
final String script = '(' + command + "); ret=$?; echo " + marker + " $ret; " final String script = "echo " + marker + "; echo " + marker + " >&2; (" + command +
+ "echo " + marker + " $ret >&2\n"; "); ret=$?; echo " + marker + " $ret; echo " + marker + " $ret >&2\n";
Log.v(TAG, "executing: " + script); Log.v(TAG, "executing: " + command);
stdin.write(script); stdin.write(script);
stdin.flush(); stdin.flush();
String line; String line;
int errnoStdout = Integer.MIN_VALUE; int errnoStdout = Integer.MIN_VALUE;
int errnoStderr = Integer.MAX_VALUE; int errnoStderr = Integer.MAX_VALUE;
int markersSeen = 0;
while ((line = stdout.readLine()) != null) { while ((line = stdout.readLine()) != null) {
if (line.startsWith(marker) && line.length() > marker.length() + 1) { if (line.startsWith(marker)) {
++markersSeen;
if (line.length() > marker.length() + 1) {
errnoStdout = Integer.valueOf(line.substring(marker.length() + 1)); errnoStdout = Integer.valueOf(line.substring(marker.length() + 1));
break; break;
} }
} else if (markersSeen > 0) {
if (output != null) if (output != null)
output.add(line); output.add(line);
Log.v(TAG, "stdout: " + line); Log.v(TAG, "stdout: " + line);
} }
}
while ((line = stderr.readLine()) != null) { while ((line = stderr.readLine()) != null) {
if (line.startsWith(marker) && line.length() > marker.length() + 1) { if (line.startsWith(marker)) {
++markersSeen;
if (line.length() > marker.length() + 1) {
errnoStderr = Integer.valueOf(line.substring(marker.length() + 1)); errnoStderr = Integer.valueOf(line.substring(marker.length() + 1));
break; 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) if (errnoStdout != errnoStderr)
throw new IOException("Unable to read exit status"); throw new IOException("Unable to read exit status");
Log.v(TAG, "exit: " + errnoStdout); Log.v(TAG, "exit: " + errnoStdout);