Skip to content

Commit a1e1881

Browse files
narayankgitbuildkicker
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 e29c649)
1 parent 1d6c0ef commit a1e1881

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

core/java/android/os/Process.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,15 @@ private static ProcessStartResult zygoteSendArgsAndGetResult(
538538
ZygoteState zygoteState, ArrayList<String> args)
539539
throws ZygoteStartFailedEx {
540540
try {
541+
// Throw early if any of the arguments are malformed. This means we can
542+
// avoid writing a partial response to the zygote.
543+
int sz = args.size();
544+
for (int i = 0; i < sz; i++) {
545+
if (args.get(i).indexOf('\n') >= 0) {
546+
throw new ZygoteStartFailedEx("embedded newlines not allowed");
547+
}
548+
}
549+
541550
/**
542551
* See com.android.internal.os.ZygoteInit.readArgumentList()
543552
* Presently the wire format to the zygote process is:
@@ -554,13 +563,8 @@ private static ProcessStartResult zygoteSendArgsAndGetResult(
554563
writer.write(Integer.toString(args.size()));
555564
writer.newLine();
556565

557-
int sz = args.size();
558566
for (int i = 0; i < sz; i++) {
559567
String arg = args.get(i);
560-
if (arg.indexOf('\n') >= 0) {
561-
throw new ZygoteStartFailedEx(
562-
"embedded newlines not allowed");
563-
}
564568
writer.write(arg);
565569
writer.newLine();
566570
}
@@ -569,11 +573,16 @@ private static ProcessStartResult zygoteSendArgsAndGetResult(
569573

570574
// Should there be a timeout on this?
571575
ProcessStartResult result = new ProcessStartResult();
576+
577+
// Always read the entire result from the input stream to avoid leaving
578+
// bytes in the stream for future process starts to accidentally stumble
579+
// upon.
572580
result.pid = inputStream.readInt();
581+
result.usingWrapper = inputStream.readBoolean();
582+
573583
if (result.pid < 0) {
574584
throw new ZygoteStartFailedEx("fork() failed");
575585
}
576-
result.usingWrapper = inputStream.readBoolean();
577586
return result;
578587
} catch (IOException ex) {
579588
zygoteState.close();

0 commit comments

Comments
 (0)