RESTRICT AUTOMERGE: Fix integer underflow vulnerability in PduParser Problem: A negative value-length in PduParser.parseContentType bypasses the bounds check, leading to an integer underflow and a massive array allocation (new byte[2GiB]) which causes an OutOfMemoryError in com.android.phone. Fix details: Added a bounds check `if (length < 0 || length > startPos)` in `PduParser.parseContentType` to reject negative lengths. Bug: 517378147 Flag: EXEMPT BUGFIX Test: atest ApiDemosTests (on main) TAG: agy CONV: 1f19cac3-1d08-4bfc-aa80-f5c6f3aecbab Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:cca4fb311881b2da3f767380e1a9be6ade7b58c6 Merged-In: Ib9b6d2e399d759037b3196b7f2fdff1b3afcfffe Change-Id: Ib9b6d2e399d759037b3196b7f2fdff1b3afcfffe
diff --git a/samples/ApiDemos/src/com/example/android/mmslib/pdu/PduParser.java b/samples/ApiDemos/src/com/example/android/mmslib/pdu/PduParser.java index 52dad29..cca4631 100755 --- a/samples/ApiDemos/src/com/example/android/mmslib/pdu/PduParser.java +++ b/samples/ApiDemos/src/com/example/android/mmslib/pdu/PduParser.java
@@ -1567,6 +1567,11 @@ if (cur < TEXT_MIN) { int length = parseValueLength(pduDataStream); int startPos = pduDataStream.available(); + if (length < 0 || length > startPos) { + Log.e(LOG_TAG, "parseContentType: Invalid length " + length + + " when available bytes are " + startPos); + return (PduContentTypes.contentTypes[0]).getBytes(); //"*/*" + } pduDataStream.mark(1); temp = pduDataStream.read(); assert(-1 != temp);
diff --git a/samples/ApiDemos/tests/AndroidManifest.xml b/samples/ApiDemos/tests/AndroidManifest.xml index 084c88d..0712419 100644 --- a/samples/ApiDemos/tests/AndroidManifest.xml +++ b/samples/ApiDemos/tests/AndroidManifest.xml
@@ -28,9 +28,9 @@ <!-- This declares that this app uses the instrumentation test runner targeting the package of com.example.android.apis. To run the tests use the command: - "adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner" + "adb shell am instrument -w com.example.android.apis.tests/androidx.test.runner.AndroidJUnitRunner" --> - <instrumentation android:name="android.test.InstrumentationTestRunner" + <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner" android:targetPackage="com.example.android.apis" android:label="Tests for Api Demos."/>
diff --git a/samples/ApiDemos/tests/src/com/example/android/mmslib/pdu/PduParserTest.java b/samples/ApiDemos/tests/src/com/example/android/mmslib/pdu/PduParserTest.java new file mode 100644 index 0000000..a6d79d2 --- /dev/null +++ b/samples/ApiDemos/tests/src/com/example/android/mmslib/pdu/PduParserTest.java
@@ -0,0 +1,44 @@ +/* + * Copyright (C) 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.mmslib.pdu; + +import android.test.AndroidTestCase; + +public class PduParserTest extends AndroidTestCase { + + public void testPduParserUnderflowOOM() { + // CONTENT_TYPE (0x84), LENGTH_QUOTE (0x1F), uintvar for Integer.MIN_VALUE, well-known type (0x83) + byte[] pdu = new byte[] { + (byte) 0x84, + (byte) 0x1F, + (byte) 0x88, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x00, + (byte) 0x83 + }; + + try { + PduParser parser = new PduParser(pdu, false); + // This should not crash with OOM. + parser.parse(); + } catch (OutOfMemoryError e) { + fail("Triggered OutOfMemoryError due to integer underflow!"); + } + } +}