Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit bc2fbfc

Browse files
committed
InputMethodSubtypeArray: prevent negative count injection
Fixes an issue where negative counts could be injected via the Parcel constructor. The writeToParcel method in that case would write data that a subsequent read would not consume. Fixes: 277916797 Test: atest InputMethodSubtypeArrayTest Change-Id: I7e881d82415051179c59bf5df97f8ba0a41e693e
1 parent 485a811 commit bc2fbfc

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

core/java/android/view/inputmethod/InputMethodSubtypeArray.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package android.view.inputmethod;
1818

1919
import android.compat.annotation.UnsupportedAppUsage;
20+
import android.os.BadParcelableException;
2021
import android.os.Parcel;
2122
import android.util.Slog;
2223

@@ -69,6 +70,9 @@ public InputMethodSubtypeArray(final List<InputMethodSubtype> subtypes) {
6970
*/
7071
public InputMethodSubtypeArray(final Parcel source) {
7172
mCount = source.readInt();
73+
if (mCount < 0) {
74+
throw new BadParcelableException("mCount must be non-negative.");
75+
}
7276
if (mCount > 0) {
7377
mDecompressedSize = source.readInt();
7478
mCompressedData = source.createByteArray();

core/tests/coretests/src/android/view/inputmethod/InputMethodSubtypeArrayTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616

1717
package android.view.inputmethod;
1818

19+
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
21+
1922
import static org.junit.Assert.assertEquals;
2023

24+
import android.os.BadParcelableException;
2125
import android.os.Parcel;
26+
import android.platform.test.annotations.Presubmit;
2227
import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
2328

2429
import androidx.test.filters.SmallTest;
@@ -31,6 +36,7 @@
3136

3237
@SmallTest
3338
@RunWith(AndroidJUnit4.class)
39+
@Presubmit
3440
public class InputMethodSubtypeArrayTest {
3541

3642
@Test
@@ -59,6 +65,36 @@ public void testInstantiate() throws Exception {
5965
assertEquals(clonedArray.get(2), clonedClonedArray.get(2));
6066
}
6167

68+
@Test
69+
public void testNegativeCount() throws Exception {
70+
InputMethodSubtypeArray negativeCountArray;
71+
try {
72+
// Construct a InputMethodSubtypeArray with: mCount = -1
73+
var p = Parcel.obtain();
74+
p.writeInt(-1);
75+
p.setDataPosition(0);
76+
negativeCountArray = new InputMethodSubtypeArray(p);
77+
} catch (BadParcelableException e) {
78+
// Expected with fix: Prevent negative mCount
79+
assertThat(e).hasMessageThat().contains("mCount");
80+
return;
81+
}
82+
assertWithMessage("Test set-up failed")
83+
.that(negativeCountArray.getCount()).isEqualTo(-1);
84+
85+
var p = Parcel.obtain();
86+
// Writes: int (mCount), int (mDecompressedSize), byte[] (mCompressedData)
87+
negativeCountArray.writeToParcel(p);
88+
p.setDataPosition(0);
89+
// Reads: int (mCount)
90+
// Leaves: int (mDecompressedSize), byte[] (mCompressedData)
91+
new InputMethodSubtypeArray(p);
92+
93+
assertWithMessage("Didn't read all data that was previously written")
94+
.that(p.dataPosition())
95+
.isEqualTo(p.dataSize());
96+
}
97+
6298
InputMethodSubtypeArray cloneViaParcel(final InputMethodSubtypeArray original) {
6399
Parcel parcel = null;
64100
try {

0 commit comments

Comments
 (0)