Migrate unsafer parcel APIs [2]

Manually migrate the few cases of:
* readArray()
* readParcelableArray()

To the new parcel APIs that take the expected type as the last
parameter. This enhances security because it prevents unexpected types
*before* running unparcelling code. More details at go/safer-parcel.

Owners, please check that the type of the objects expected is always a
subtype of the type provided as the 3rd parameter. This is usually easy
to verify due to casts that happen shortly after.

These changes often allowed further transformations but I decided to
avoid them to keep this change small and targeted.

This was manual since it's tricky to get lint to infer the type in
those cases and it was only a few.

Bug: 195622897
Test: TH passes
Change-Id: I262ed7cd6d3bc15b32e9296e88a8a67fdb59e880
This commit is contained in:
Bernardo Rufino
2021-12-21 17:13:03 +00:00
parent 2e72a82872
commit 74ee11c3f4
11 changed files with 16 additions and 11 deletions

View File

@@ -68,7 +68,7 @@ public final class Session2CommandGroup implements Parcelable {
/**
* Used by parcelable creator.
*/
@SuppressWarnings("WeakerAccess") /* synthetic access */
@SuppressWarnings({"WeakerAccess", "UnsafeParcelApi"}) /* synthetic access */
Session2CommandGroup(Parcel in) {
Parcelable[] commands = in.readParcelableArray(Session2Command.class.getClassLoader());
if (commands != null) {

View File

@@ -525,7 +525,7 @@ public final class GestureDescription {
public GestureStep(Parcel parcel) {
timeSinceGestureStart = parcel.readLong();
Parcelable[] parcelables =
parcel.readParcelableArray(TouchPoint.class.getClassLoader());
parcel.readParcelableArray(TouchPoint.class.getClassLoader(), TouchPoint.class);
numTouchPoints = (parcelables == null) ? 0 : parcelables.length;
touchPoints = new TouchPoint[numTouchPoints];
for (int i = 0; i < numTouchPoints; i++) {

View File

@@ -505,7 +505,7 @@ public class RestrictionEntry implements Parcelable {
mChoiceValues = in.readStringArray();
mCurrentValue = in.readString();
mCurrentValues = in.readStringArray();
Parcelable[] parcelables = in.readParcelableArray(null);
Parcelable[] parcelables = in.readParcelableArray(null, RestrictionEntry.class);
if (parcelables != null) {
mRestrictions = new RestrictionEntry[parcelables.length];
for (int i = 0; i < parcelables.length; i++) {

View File

@@ -599,7 +599,8 @@ public final class CaptureRequest extends CameraMetadata<CaptureRequest.Key<?>>
synchronized (mSurfacesLock) {
mSurfaceSet.clear();
Parcelable[] parcelableArray = in.readParcelableArray(Surface.class.getClassLoader());
Parcelable[] parcelableArray = in.readParcelableArray(Surface.class.getClassLoader(),
Surface.class);
if (parcelableArray != null) {
for (Parcelable p : parcelableArray) {
Surface s = (Surface) p;

View File

@@ -436,7 +436,8 @@ public class RadioManager {
mNumAudioSources = in.readInt();
mIsInitializationRequired = in.readInt() == 1;
mIsCaptureSupported = in.readInt() == 1;
Parcelable[] tmp = in.readParcelableArray(BandDescriptor.class.getClassLoader());
Parcelable[] tmp = in.readParcelableArray(BandDescriptor.class.getClassLoader(),
BandDescriptor.class);
mBands = new BandDescriptor[tmp.length];
for (int i = 0; i < tmp.length; i++) {
mBands[i] = (BandDescriptor) tmp[i];

View File

@@ -240,7 +240,7 @@ public final class PrintJobInfo implements Parcelable {
mTag = parcel.readString();
mCreationTime = parcel.readLong();
mCopies = parcel.readInt();
Parcelable[] parcelables = parcel.readParcelableArray(null);
Parcelable[] parcelables = parcel.readParcelableArray(null, PageRange.class);
if (parcelables != null) {
mPageRanges = new PageRange[parcelables.length];
for (int i = 0; i < parcelables.length; i++) {

View File

@@ -190,7 +190,7 @@ public class PresResInstanceInfo implements Parcelable{
mResInstanceState = source.readInt();
mPresentityUri = source.readString();
Parcelable[] tempParcelableArray = source.readParcelableArray(
PresTupleInfo.class.getClassLoader());
PresTupleInfo.class.getClassLoader(), PresTupleInfo.class);
mTupleInfoArray = new PresTupleInfo[] {};
if(tempParcelableArray != null) {
mTupleInfoArray = Arrays.copyOf(tempParcelableArray, tempParcelableArray.length,

View File

@@ -239,7 +239,8 @@ public class DisplayResolveInfo implements TargetInfo, Parcelable {
mExtendedInfo = in.readCharSequence();
mResolvedIntent = in.readParcelable(null /* ClassLoader */, android.content.Intent.class);
mSourceIntents.addAll(
Arrays.asList((Intent[]) in.readParcelableArray(null /* ClassLoader */)));
Arrays.asList((Intent[]) in.readParcelableArray(null /* ClassLoader */,
Intent.class)));
mIsSuspended = in.readBoolean();
mPinned = in.readBoolean();
mResolveInfo = in.readParcelable(null /* ClassLoader */, android.content.pm.ResolveInfo.class);

View File

@@ -24,7 +24,6 @@ import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.EventLog;
import android.util.Log;
import com.android.internal.annotations.GuardedBy;
@@ -585,6 +584,7 @@ public class AndroidFuture<T> extends CompletableFuture<T> implements Parcelable
/**
* @see #writeThrowable
*/
@SuppressWarnings("UnsafeParcelApi")
private static @Nullable Throwable readThrowable(@NonNull Parcel parcel) {
final boolean hasThrowable = parcel.readBoolean();
if (!hasThrowable) {

View File

@@ -221,7 +221,8 @@ public final class NetworkScanRequest implements Parcelable {
private NetworkScanRequest(Parcel in) {
mScanType = in.readInt();
Parcelable[] tempSpecifiers = in.readParcelableArray(Object.class.getClassLoader());
Parcelable[] tempSpecifiers = in.readParcelableArray(Object.class.getClassLoader(),
RadioAccessSpecifier.class);
if (tempSpecifiers != null) {
mSpecifiers = new RadioAccessSpecifier[tempSpecifiers.length];
for (int i = 0; i < tempSpecifiers.length; i++) {

View File

@@ -847,7 +847,8 @@ public final class ImsCallProfile implements Parcelable {
mHasKnownUserIntentEmergency = in.readBoolean();
mRestrictCause = in.readInt();
mCallerNumberVerificationStatus = in.readInt();
Object[] accepted = in.readArray(RtpHeaderExtensionType.class.getClassLoader());
Object[] accepted = in.readArray(RtpHeaderExtensionType.class.getClassLoader(),
RtpHeaderExtensionType.class);
mAcceptedRtpHeaderExtensionTypes = Arrays.stream(accepted)
.map(o -> (RtpHeaderExtensionType) o).collect(Collectors.toSet());
}