Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 781ce614 authored by Hongwei Wang's avatar Hongwei Wang Committed by Android Build Coastguard Worker
Browse files

Rate limiting PiP aspect ratio change request

Using CountQuotaTrack to limit how frequent an app can request aspect
ratio change via PictureInPictureParams, which could result flood of PiP
resizing requests and freeze the PiP window.

Note that CountQuotaTrack is initialized out of the WM lock to avoid
dead lock with the AM one.

Bug: 283103220
Test: Manually, using the POC app
Test: Manually, switching YT PiP video functions at a regular rate
Test: atest WindowOrganizerTests ActivityThreadTest
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:c3f4dd070cfc7f20581779abd83620fc8769fbd4)
Merged-In: Icb7dd17bbf7df573a9bb28f3dc56e90e78384f4f
Change-Id: Icb7dd17bbf7df573a9bb28f3dc56e90e78384f4f
parent 338e0ac6
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ import android.view.RemoteAnimationDefinition;
import android.window.SizeConfigurationBuckets;
import android.window.TransitionInfo;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.AssistUtils;
import com.android.internal.policy.IKeyguardDismissCallback;
import com.android.internal.protolog.common.ProtoLog;
@@ -85,6 +86,9 @@ import com.android.server.LocalServices;
import com.android.server.Watchdog;
import com.android.server.pm.parsing.pkg.AndroidPackage;
import com.android.server.uri.NeededUriGrants;
import com.android.server.utils.quota.Categorizer;
import com.android.server.utils.quota.Category;
import com.android.server.utils.quota.CountQuotaTracker;
import com.android.server.vr.VrManagerInternal;

/**
@@ -100,6 +104,13 @@ class ActivityClientController extends IActivityClientController.Stub {
    private final ActivityTaskSupervisor mTaskSupervisor;
    private final Context mContext;

    // Prevent malicious app abusing the Activity#setPictureInPictureParams API
    @VisibleForTesting CountQuotaTracker mSetPipAspectRatioQuotaTracker;
    // Limit to 60 times / minute
    private static final int SET_PIP_ASPECT_RATIO_LIMIT = 60;
    // The timeWindowMs here can not be smaller than QuotaTracker#MIN_WINDOW_SIZE_MS
    private static final long SET_PIP_ASPECT_RATIO_TIME_WINDOW_MS = 60_000;

    /** Wrapper around VoiceInteractionServiceManager. */
    private AssistUtils mAssistUtils;

@@ -719,6 +730,7 @@ class ActivityClientController extends IActivityClientController.Stub {
    public boolean enterPictureInPictureMode(IBinder token, final PictureInPictureParams params) {
        final long origId = Binder.clearCallingIdentity();
        try {
            ensureSetPipAspectRatioQuotaTracker();
            synchronized (mGlobalLock) {
                final ActivityRecord r = ensureValidPictureInPictureActivityParams(
                        "enterPictureInPictureMode", token, params);
@@ -733,6 +745,7 @@ class ActivityClientController extends IActivityClientController.Stub {
    public void setPictureInPictureParams(IBinder token, final PictureInPictureParams params) {
        final long origId = Binder.clearCallingIdentity();
        try {
            ensureSetPipAspectRatioQuotaTracker();
            synchronized (mGlobalLock) {
                final ActivityRecord r = ensureValidPictureInPictureActivityParams(
                        "setPictureInPictureParams", token, params);
@@ -766,6 +779,19 @@ class ActivityClientController extends IActivityClientController.Stub {
        Binder.restoreCallingIdentity(origId);
    }

    /**
     * Initialize the {@link #mSetPipAspectRatioQuotaTracker} if applicable, which should happen
     * out of {@link #mGlobalLock} to avoid deadlock (AM lock is used in QuotaTrack ctor).
     */
    private void ensureSetPipAspectRatioQuotaTracker() {
        if (mSetPipAspectRatioQuotaTracker == null) {
            mSetPipAspectRatioQuotaTracker = new CountQuotaTracker(mContext,
                    Categorizer.SINGLE_CATEGORIZER);
            mSetPipAspectRatioQuotaTracker.setCountLimit(Category.SINGLE_CATEGORY,
                    SET_PIP_ASPECT_RATIO_LIMIT, SET_PIP_ASPECT_RATIO_TIME_WINDOW_MS);
        }
    }

    /**
     * Checks the state of the system and the activity associated with the given {@param token} to
     * verify that picture-in-picture is supported for that activity.
@@ -790,6 +816,19 @@ class ActivityClientController extends IActivityClientController.Stub {
                    + ": Current activity does not support picture-in-picture.");
        }

        // Rate limit how frequent an app can request aspect ratio change via
        // Activity#setPictureInPictureParams
        final int userId = UserHandle.getCallingUserId();
        if (r.pictureInPictureArgs.hasSetAspectRatio()
                && params.hasSetAspectRatio()
                && !r.pictureInPictureArgs.getAspectRatioRational().equals(
                params.getAspectRatioRational())
                && !mSetPipAspectRatioQuotaTracker.noteEvent(
                userId, r.packageName, "setPipAspectRatio")) {
            throw new IllegalStateException(caller
                    + ": Too many PiP aspect ratio change requests from " + r.packageName);
        }

        if (params.hasSetAspectRatio()
                && !mService.mWindowManager.isValidPictureInPictureAspectRatio(
                r.mDisplayContent, params.getAspectRatio())) {
+6 −0
Original line number Diff line number Diff line
@@ -881,6 +881,12 @@ public class WindowOrganizerTests extends WindowTestsBase {
        assertNotNull(o.mInfo);
        assertNotNull(o.mInfo.pictureInPictureParams);

        // Bypass the quota check, which causes NPE in current test setup.
        if (mWm.mAtmService.mActivityClientController.mSetPipAspectRatioQuotaTracker != null) {
            mWm.mAtmService.mActivityClientController.mSetPipAspectRatioQuotaTracker
                    .setEnabled(false);
        }

        final PictureInPictureParams p2 = new PictureInPictureParams.Builder()
                .setAspectRatio(new Rational(3, 4)).build();
        mWm.mAtmService.mActivityClientController.setPictureInPictureParams(record.token, p2);