Skip to content

Commit 53afaf4

Browse files
narayankandi34
authored andcommitted
Process: Fix communication with zygote.
Don't write partial requests, and don't return (or throw) early after partially reading a response. bug: 30143607 (cherry-picked from commit 448be0a) Change-Id: I5881fdd5e81023cd21fb4d23a471a5031987a1f1 (cherry picked from commit 8e69dd2)
1 parent 3f4deb3 commit 53afaf4

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

core/java/android/os/Process.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,15 @@ private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> a
495495
openZygoteSocketIfNeeded();
496496

497497
try {
498+
// Throw early if any of the arguments are malformed. This means we can
499+
// avoid writing a partial response to the zygote.
500+
int sz = args.size();
501+
for (int i = 0; i < sz; i++) {
502+
if (args.get(i).indexOf('\n') >= 0) {
503+
throw new ZygoteStartFailedEx("embedded newlines not allowed");
504+
}
505+
}
506+
498507
/**
499508
* See com.android.internal.os.ZygoteInit.readArgumentList()
500509
* Presently the wire format to the zygote process is:
@@ -509,13 +518,8 @@ private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> a
509518
sZygoteWriter.write(Integer.toString(args.size()));
510519
sZygoteWriter.newLine();
511520

512-
int sz = args.size();
513521
for (int i = 0; i < sz; i++) {
514522
String arg = args.get(i);
515-
if (arg.indexOf('\n') >= 0) {
516-
throw new ZygoteStartFailedEx(
517-
"embedded newlines not allowed");
518-
}
519523
sZygoteWriter.write(arg);
520524
sZygoteWriter.newLine();
521525
}
@@ -524,11 +528,15 @@ private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> a
524528

525529
// Should there be a timeout on this?
526530
ProcessStartResult result = new ProcessStartResult();
531+
// Always read the entire result from the input stream to avoid leaving
532+
// bytes in the stream for future process starts to accidentally stumble
533+
// upon.
527534
result.pid = sZygoteInputStream.readInt();
535+
result.usingWrapper = sZygoteInputStream.readBoolean();
536+
528537
if (result.pid < 0) {
529538
throw new ZygoteStartFailedEx("fork() failed");
530539
}
531-
result.usingWrapper = sZygoteInputStream.readBoolean();
532540
return result;
533541
} catch (IOException ex) {
534542
try {

0 commit comments

Comments
 (0)