From 1dfc88428cf35f59e3bdc73ee7cddbd9947f3634 Mon Sep 17 00:00:00 2001 From: Hani Kazmi Date: Fri, 3 Feb 2023 14:12:18 +0000 Subject: [PATCH] Update BAL controller to return codes in priority order ASM uses BAL codes to restrict which activity launches are allowed. BAL_ALLOW_BAL_PERMISSION is currrently allowed, whereas BAL_ALLOW_GRACE_PERIOD is not. CTS Tests instrument with BAL permission to launch activities from the background. However, due to the ordering of checks, BAL_ALLOW_GRACE_PERIOD would be returned if the CTS app had recently entered the background, rather than BAL_ALLOW_BAL_PERMISSION. Bug: 267453720 Test: atest AccessibilityViewTreeReportingTest Change-Id: I012fcf91301b022f57f078c80772e12bbce72581 --- .../wm/BackgroundLaunchProcessController.java | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/services/core/java/com/android/server/wm/BackgroundLaunchProcessController.java b/services/core/java/com/android/server/wm/BackgroundLaunchProcessController.java index 63dc7d28dd1f1..f94fd2bb772f2 100644 --- a/services/core/java/com/android/server/wm/BackgroundLaunchProcessController.java +++ b/services/core/java/com/android/server/wm/BackgroundLaunchProcessController.java @@ -102,6 +102,41 @@ class BackgroundLaunchProcessController { boolean hasActivityInVisibleTask, boolean hasBackgroundActivityStartPrivileges, long lastStopAppSwitchesTime, long lastActivityLaunchTime, long lastActivityFinishTime) { + // Allow if the proc is instrumenting with background activity starts privs. + if (hasBackgroundActivityStartPrivileges) { + if (DEBUG_ACTIVITY_STARTS) { + Slog.d(TAG, "[Process(" + pid + + ")] Activity start allowed: process instrumenting with background " + + "activity starts privileges"); + } + return BAL_ALLOW_BAL_PERMISSION; + } + // Allow if the flag was explicitly set. + if (isBackgroundStartAllowedByToken(uid, packageName, isCheckingForFgsStart)) { + if (DEBUG_ACTIVITY_STARTS) { + Slog.d(TAG, "[Process(" + pid + + ")] Activity start allowed: process allowed by token"); + } + return BAL_ALLOW_BAL_PERMISSION; + } + // Allow if the caller is bound by a UID that's currently foreground. + if (isBoundByForegroundUid()) { + if (DEBUG_ACTIVITY_STARTS) { + Slog.d(TAG, "[Process(" + pid + + ")] Activity start allowed: process bound by foreground uid"); + } + return BAL_ALLOW_VISIBLE_WINDOW; + } + // Allow if the caller has an activity in any foreground task. + if (hasActivityInVisibleTask + && (appSwitchState == APP_SWITCH_ALLOW || appSwitchState == APP_SWITCH_FG_ONLY)) { + if (DEBUG_ACTIVITY_STARTS) { + Slog.d(TAG, "[Process(" + pid + + ")] Activity start allowed: process has activity in foreground task"); + } + return BAL_ALLOW_FOREGROUND; + } + // If app switching is not allowed, we ignore all the start activity grace period // exception so apps cannot start itself in onPause() after pressing home button. if (appSwitchState == APP_SWITCH_ALLOW) { @@ -129,40 +164,6 @@ class BackgroundLaunchProcessController { } } - // Allow if the proc is instrumenting with background activity starts privs. - if (hasBackgroundActivityStartPrivileges) { - if (DEBUG_ACTIVITY_STARTS) { - Slog.d(TAG, "[Process(" + pid - + ")] Activity start allowed: process instrumenting with background " - + "activity starts privileges"); - } - return BAL_ALLOW_BAL_PERMISSION; - } - // Allow if the caller has an activity in any foreground task. - if (hasActivityInVisibleTask - && (appSwitchState == APP_SWITCH_ALLOW || appSwitchState == APP_SWITCH_FG_ONLY)) { - if (DEBUG_ACTIVITY_STARTS) { - Slog.d(TAG, "[Process(" + pid - + ")] Activity start allowed: process has activity in foreground task"); - } - return BAL_ALLOW_FOREGROUND; - } - // Allow if the caller is bound by a UID that's currently foreground. - if (isBoundByForegroundUid()) { - if (DEBUG_ACTIVITY_STARTS) { - Slog.d(TAG, "[Process(" + pid - + ")] Activity start allowed: process bound by foreground uid"); - } - return BAL_ALLOW_VISIBLE_WINDOW; - } - // Allow if the flag was explicitly set. - if (isBackgroundStartAllowedByToken(uid, packageName, isCheckingForFgsStart)) { - if (DEBUG_ACTIVITY_STARTS) { - Slog.d(TAG, "[Process(" + pid - + ")] Activity start allowed: process allowed by token"); - } - return BAL_ALLOW_BAL_PERMISSION; - } return BAL_BLOCK; }