Skip to content

Commit ef525e8

Browse files
JensenPaulThe Android Automerger
authored andcommitted
Avoid crashing when downloading MitM'd PAC that is too big am: 7d2198b am: 9c1cb7a am: 6634e90
am: 66ee229 Change-Id: Ib0023b44e521b936ab2f9450ad367b1feda64492
1 parent f653d36 commit ef525e8

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

services/core/java/com/android/server/connectivity/PacManager.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.net.ProxyInfo;
2828
import android.net.Uri;
2929
import android.os.Handler;
30+
import android.os.HandlerThread;
3031
import android.os.IBinder;
3132
import android.os.RemoteException;
3233
import android.os.ServiceManager;
@@ -39,10 +40,10 @@
3940
import com.android.net.IProxyCallback;
4041
import com.android.net.IProxyPortListener;
4142
import com.android.net.IProxyService;
42-
import com.android.server.IoThread;
4343

4444
import libcore.io.Streams;
4545

46+
import java.io.ByteArrayOutputStream;
4647
import java.io.IOException;
4748
import java.net.URL;
4849
import java.net.URLConnection;
@@ -66,6 +67,7 @@ public class PacManager {
6667
private static final int DELAY_1 = 0;
6768
private static final int DELAY_4 = 3;
6869
private static final int DELAY_LONG = 4;
70+
private static final long MAX_PAC_SIZE = 20 * 1000 * 1000;
6971

7072
/** Keep these values up-to-date with ProxyService.java */
7173
public static final String KEY_PROXY = "keyProxy";
@@ -123,15 +125,21 @@ public void run() {
123125
}
124126
};
125127

128+
private final HandlerThread mNetThread = new HandlerThread("android.pacmanager",
129+
android.os.Process.THREAD_PRIORITY_DEFAULT);
130+
private final Handler mNetThreadHandler;
131+
126132
class PacRefreshIntentReceiver extends BroadcastReceiver {
127133
public void onReceive(Context context, Intent intent) {
128-
IoThread.getHandler().post(mPacDownloader);
134+
mNetThreadHandler.post(mPacDownloader);
129135
}
130136
}
131137

132138
public PacManager(Context context, Handler handler, int proxyMessage) {
133139
mContext = context;
134140
mLastPort = -1;
141+
mNetThread.start();
142+
mNetThreadHandler = new Handler(mNetThread.getLooper());
135143

136144
mPacRefreshIntent = PendingIntent.getBroadcast(
137145
context, 0, new Intent(ACTION_PAC_REFRESH), 0);
@@ -199,7 +207,25 @@ public synchronized boolean setCurrentProxyScriptUrl(ProxyInfo proxy) {
199207
private static String get(Uri pacUri) throws IOException {
200208
URL url = new URL(pacUri.toString());
201209
URLConnection urlConnection = url.openConnection(java.net.Proxy.NO_PROXY);
202-
return new String(Streams.readFully(urlConnection.getInputStream()));
210+
long contentLength = -1;
211+
try {
212+
contentLength = Long.parseLong(urlConnection.getHeaderField("Content-Length"));
213+
} catch (NumberFormatException e) {
214+
// Ignore
215+
}
216+
if (contentLength > MAX_PAC_SIZE) {
217+
throw new IOException("PAC too big: " + contentLength + " bytes");
218+
}
219+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
220+
byte[] buffer = new byte[1024];
221+
int count;
222+
while ((count = urlConnection.getInputStream().read(buffer)) != -1) {
223+
bytes.write(buffer, 0, count);
224+
if (bytes.size() > MAX_PAC_SIZE) {
225+
throw new IOException("PAC too big");
226+
}
227+
}
228+
return bytes.toString();
203229
}
204230

205231
private int getNextDelay(int currentDelay) {
@@ -297,7 +323,7 @@ public void onServiceConnected(ComponentName component, IBinder binder) {
297323
} catch (RemoteException e) {
298324
Log.e(TAG, "Unable to reach ProxyService - PAC will not be started", e);
299325
}
300-
IoThread.getHandler().post(mPacDownloader);
326+
mNetThreadHandler.post(mPacDownloader);
301327
}
302328
}
303329
}

0 commit comments

Comments
 (0)