Do not allow FGS starts from sync

When requestSync() is called by a TOP app, we put grant the sync adapter
app temp-allowlist of 10 minutes.

We need to keep this behavior, but we shouldn't allow FGS to be started
in this window.

Fix: 178233289
Test: atest CtsSyncManagerTestsCases
Test: atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java

BYPASS_INCLUSIVE_LANGUAGE_REASON=Existing public API.

Change-Id: I5d32206bf97cdbc3678fbb4c710642ee0620fd07
This commit is contained in:
Makoto Onuki
2021-03-05 16:22:52 -08:00
parent 397e520f79
commit af6196d5d4
3 changed files with 37 additions and 15 deletions

View File

@@ -17,6 +17,7 @@
package com.android.server;
import android.annotation.Nullable;
import android.os.PowerWhitelistManager;
import android.os.PowerWhitelistManager.ReasonCode;
import android.os.PowerWhitelistManager.TempAllowListType;
@@ -32,10 +33,21 @@ public interface DeviceIdleInternal {
void exitIdle(String reason);
/**
* Same as {@link #addPowerSaveTempWhitelistApp(int, String, long, int, boolean, int, String)}
* with {@link PowerWhitelistManager#TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED}.
*/
void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
long durationMs, int userId, boolean sync, @ReasonCode int reasonCode,
@Nullable String reason);
/**
* Put a package in the temp-allowlist.
*/
void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
long durationMs, @TempAllowListType int tempAllowListType, int userId, boolean sync,
@ReasonCode int reasonCode, @Nullable String reason);
/**
* Called by ActivityManagerService to directly add UID to DeviceIdleController's temp
* allowlist.

View File

@@ -1942,22 +1942,29 @@ public class DeviceIdleController extends SystemService
exitIdleInternal(reason);
}
// duration in milliseconds
@Override
public void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
long durationMs, int userId, boolean sync, @ReasonCode int reasonCode,
@Nullable String reason) {
addPowerSaveTempAllowlistAppInternal(callingUid, packageName, durationMs,
TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED,
userId, sync, reasonCode, reason);
}
// duration in milliseconds
@Override
public void addPowerSaveTempWhitelistAppDirect(int uid, long duration,
@TempAllowListType int type, boolean sync, @ReasonCode int reasonCode,
public void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
long durationMs, @TempAllowListType int tempAllowListType, int userId, boolean sync,
@ReasonCode int reasonCode, @Nullable String reason) {
addPowerSaveTempAllowlistAppInternal(callingUid, packageName, durationMs,
tempAllowListType, userId, sync, reasonCode, reason);
}
@Override
public void addPowerSaveTempWhitelistAppDirect(int uid, long durationMs,
@TempAllowListType int tempAllowListType, boolean sync, @ReasonCode int reasonCode,
@Nullable String reason, int callingUid) {
addPowerSaveTempWhitelistAppDirectInternal(callingUid, uid, duration, type, sync,
reasonCode, reason);
addPowerSaveTempWhitelistAppDirectInternal(callingUid, uid, durationMs,
tempAllowListType, sync, reasonCode, reason);
}
// duration in milliseconds
@@ -2699,7 +2706,8 @@ public class DeviceIdleController extends SystemService
final long token = Binder.clearCallingIdentity();
try {
addPowerSaveTempAllowlistAppInternal(callingUid,
packageName, duration, userId, true, reasonCode, reason);
packageName, duration, TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED,
userId, true, reasonCode, reason);
} finally {
Binder.restoreCallingIdentity(token);
}
@@ -2731,8 +2739,8 @@ public class DeviceIdleController extends SystemService
* app an exemption to access network and acquire wakelocks.
*/
void addPowerSaveTempAllowlistAppInternal(int callingUid, String packageName,
long duration, int userId, boolean sync, @ReasonCode int reasonCode,
@Nullable String reason) {
long durationMs, @TempAllowListType int tempAllowListType, int userId, boolean sync,
@ReasonCode int reasonCode, @Nullable String reason) {
synchronized (this) {
int callingAppId = UserHandle.getAppId(callingUid);
if (callingAppId >= Process.FIRST_APPLICATION_UID) {
@@ -2745,8 +2753,8 @@ public class DeviceIdleController extends SystemService
}
try {
int uid = getContext().getPackageManager().getPackageUidAsUser(packageName, userId);
addPowerSaveTempWhitelistAppDirectInternal(callingUid, uid, duration,
TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, sync, reasonCode, reason);
addPowerSaveTempWhitelistAppDirectInternal(callingUid, uid, durationMs,
tempAllowListType, sync, reasonCode, reason);
} catch (NameNotFoundException e) {
}
}
@@ -2756,8 +2764,8 @@ public class DeviceIdleController extends SystemService
* app an exemption to access network and acquire wakelocks.
*/
void addPowerSaveTempWhitelistAppDirectInternal(int callingUid, int uid,
long duration, @TempAllowListType int type, boolean sync, @ReasonCode int reasonCode,
@Nullable String reason) {
long duration, @TempAllowListType int tempAllowListType, boolean sync,
@ReasonCode int reasonCode, @Nullable String reason) {
final long timeNow = SystemClock.elapsedRealtime();
boolean informWhitelistChanged = false;
int appId = UserHandle.getAppId(uid);
@@ -2782,8 +2790,8 @@ public class DeviceIdleController extends SystemService
} catch (RemoteException e) {
}
postTempActiveTimeoutMessage(uid, duration);
updateTempWhitelistAppIdsLocked(uid, true, duration, type, reasonCode,
reason, callingUid);
updateTempWhitelistAppIdsLocked(uid, true, duration, tempAllowListType,
reasonCode, reason, callingUid);
if (sync) {
informWhitelistChanged = true;
} else {

View File

@@ -17,6 +17,7 @@
package com.android.server.content;
import static android.os.PowerWhitelistManager.REASON_SYNC_MANAGER;
import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED;
import static com.android.server.content.SyncLogger.logSafe;
@@ -1672,6 +1673,7 @@ public class SyncManager {
dic.addPowerSaveTempWhitelistApp(Process.SYSTEM_UID,
syncOperation.owningPackage,
mConstants.getKeyExemptionTempWhitelistDurationInSeconds() * 1000,
TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED,
UserHandle.getUserId(syncOperation.owningUid),
/* sync=*/ false, REASON_SYNC_MANAGER, "sync by top app");
}