Merge "BG-FGS-Launch restriction also checks for the caller's targetSdkVersion." into sc-dev
This commit is contained in:
@@ -18,7 +18,6 @@ package com.android.server.am;
|
|||||||
|
|
||||||
import static android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND;
|
import static android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND;
|
||||||
import static android.Manifest.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND;
|
import static android.Manifest.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND;
|
||||||
import static android.Manifest.permission.SYSTEM_ALERT_WINDOW;
|
|
||||||
import static android.app.ActivityManager.PROCESS_STATE_HEAVY_WEIGHT;
|
import static android.app.ActivityManager.PROCESS_STATE_HEAVY_WEIGHT;
|
||||||
import static android.app.ActivityManager.PROCESS_STATE_RECEIVER;
|
import static android.app.ActivityManager.PROCESS_STATE_RECEIVER;
|
||||||
import static android.app.ActivityManager.PROCESS_STATE_TOP;
|
import static android.app.ActivityManager.PROCESS_STATE_TOP;
|
||||||
@@ -49,6 +48,7 @@ import static android.os.PowerWhitelistManager.REASON_UID_VISIBLE;
|
|||||||
import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED;
|
import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED;
|
||||||
import static android.os.PowerWhitelistManager.getReasonCodeFromProcState;
|
import static android.os.PowerWhitelistManager.getReasonCodeFromProcState;
|
||||||
import static android.os.PowerWhitelistManager.reasonCodeToString;
|
import static android.os.PowerWhitelistManager.reasonCodeToString;
|
||||||
|
import static android.os.Process.INVALID_UID;
|
||||||
import static android.os.Process.NFC_UID;
|
import static android.os.Process.NFC_UID;
|
||||||
import static android.os.Process.ROOT_UID;
|
import static android.os.Process.ROOT_UID;
|
||||||
import static android.os.Process.SHELL_UID;
|
import static android.os.Process.SHELL_UID;
|
||||||
@@ -660,7 +660,7 @@ public final class ActiveServices {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServiceRecord r = res.record;
|
ServiceRecord r = res.record;
|
||||||
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, service, r,
|
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, service, r, userId,
|
||||||
allowBackgroundActivityStarts);
|
allowBackgroundActivityStarts);
|
||||||
|
|
||||||
if (!mAm.mUserController.exists(r.userId)) {
|
if (!mAm.mUserController.exists(r.userId)) {
|
||||||
@@ -693,19 +693,7 @@ public final class ActiveServices {
|
|||||||
+ r.shortInstanceName;
|
+ r.shortInstanceName;
|
||||||
Slog.w(TAG, msg);
|
Slog.w(TAG, msg);
|
||||||
showFgsBgRestrictedNotificationLocked(r);
|
showFgsBgRestrictedNotificationLocked(r);
|
||||||
ApplicationInfo aInfo = null;
|
if (CompatChanges.isChangeEnabled(FGS_START_EXCEPTION_CHANGE_ID, callingUid)) {
|
||||||
try {
|
|
||||||
aInfo = AppGlobals.getPackageManager().getApplicationInfo(
|
|
||||||
callingPackage, ActivityManagerService.STOCK_PM_FLAGS,
|
|
||||||
userId);
|
|
||||||
} catch (android.os.RemoteException e) {
|
|
||||||
// pm is in same process, this will never happen.
|
|
||||||
}
|
|
||||||
if (aInfo == null) {
|
|
||||||
throw new SecurityException("startServiceLocked failed, "
|
|
||||||
+ "could not resolve client package " + callingPackage);
|
|
||||||
}
|
|
||||||
if (CompatChanges.isChangeEnabled(FGS_START_EXCEPTION_CHANGE_ID, aInfo.uid)) {
|
|
||||||
throw new ForegroundServiceStartNotAllowedException(msg);
|
throw new ForegroundServiceStartNotAllowedException(msg);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -1808,7 +1796,7 @@ public final class ActiveServices {
|
|||||||
final long delayMs = SystemClock.elapsedRealtime() - r.createRealTime;
|
final long delayMs = SystemClock.elapsedRealtime() - r.createRealTime;
|
||||||
if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) {
|
if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) {
|
||||||
setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.getPid(),
|
setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.getPid(),
|
||||||
r.appInfo.uid, r.intent.getIntent(), r, false);
|
r.appInfo.uid, r.intent.getIntent(), r, r.userId,false);
|
||||||
final String temp = "startForegroundDelayMs:" + delayMs;
|
final String temp = "startForegroundDelayMs:" + delayMs;
|
||||||
if (r.mInfoAllowStartForeground != null) {
|
if (r.mInfoAllowStartForeground != null) {
|
||||||
r.mInfoAllowStartForeground += "; " + temp;
|
r.mInfoAllowStartForeground += "; " + temp;
|
||||||
@@ -1825,7 +1813,7 @@ public final class ActiveServices {
|
|||||||
r.mLastSetFgsRestrictionTime;
|
r.mLastSetFgsRestrictionTime;
|
||||||
if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) {
|
if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) {
|
||||||
setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.getPid(),
|
setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.getPid(),
|
||||||
r.appInfo.uid, r.intent.getIntent(), r, false);
|
r.appInfo.uid, r.intent.getIntent(), r, r.userId,false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logFgsBackgroundStart(r);
|
logFgsBackgroundStart(r);
|
||||||
@@ -2579,7 +2567,8 @@ public final class ActiveServices {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, service, s, false);
|
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, service, s, userId,
|
||||||
|
false);
|
||||||
|
|
||||||
if (s.app != null) {
|
if (s.app != null) {
|
||||||
ProcessServiceRecord servicePsr = s.app.mServices;
|
ProcessServiceRecord servicePsr = s.app.mServices;
|
||||||
@@ -5469,7 +5458,7 @@ public final class ActiveServices {
|
|||||||
* @return true if allow, false otherwise.
|
* @return true if allow, false otherwise.
|
||||||
*/
|
*/
|
||||||
private void setFgsRestrictionLocked(String callingPackage,
|
private void setFgsRestrictionLocked(String callingPackage,
|
||||||
int callingPid, int callingUid, Intent intent, ServiceRecord r,
|
int callingPid, int callingUid, Intent intent, ServiceRecord r, int userId,
|
||||||
boolean allowBackgroundActivityStarts) {
|
boolean allowBackgroundActivityStarts) {
|
||||||
r.mLastSetFgsRestrictionTime = SystemClock.elapsedRealtime();
|
r.mLastSetFgsRestrictionTime = SystemClock.elapsedRealtime();
|
||||||
// Check DeviceConfig flag.
|
// Check DeviceConfig flag.
|
||||||
@@ -5487,7 +5476,7 @@ public final class ActiveServices {
|
|||||||
if (r.mAllowStartForeground == REASON_DENIED) {
|
if (r.mAllowStartForeground == REASON_DENIED) {
|
||||||
r.mAllowStartForeground = shouldAllowFgsStartForegroundLocked(allowWhileInUse,
|
r.mAllowStartForeground = shouldAllowFgsStartForegroundLocked(allowWhileInUse,
|
||||||
callingPackage, callingPid, callingUid, intent, r,
|
callingPackage, callingPid, callingUid, intent, r,
|
||||||
allowBackgroundActivityStarts);
|
userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5630,13 +5619,20 @@ public final class ActiveServices {
|
|||||||
*/
|
*/
|
||||||
private @ReasonCode int shouldAllowFgsStartForegroundLocked(
|
private @ReasonCode int shouldAllowFgsStartForegroundLocked(
|
||||||
@ReasonCode int allowWhileInUse, String callingPackage, int callingPid,
|
@ReasonCode int allowWhileInUse, String callingPackage, int callingPid,
|
||||||
int callingUid, Intent intent, ServiceRecord r, boolean allowBackgroundActivityStarts) {
|
int callingUid, Intent intent, ServiceRecord r, int userId) {
|
||||||
FgsStartTempAllowList.TempFgsAllowListEntry tempAllowListReason =
|
FgsStartTempAllowList.TempFgsAllowListEntry tempAllowListReason =
|
||||||
r.mInfoTempFgsAllowListReason = mAm.isAllowlistedForFgsStartLOSP(callingUid);
|
r.mInfoTempFgsAllowListReason = mAm.isAllowlistedForFgsStartLOSP(callingUid);
|
||||||
int ret = shouldAllowFgsStartForegroundLocked(allowWhileInUse, callingPid, callingUid,
|
int ret = shouldAllowFgsStartForegroundLocked(allowWhileInUse, callingPid, callingUid,
|
||||||
callingPackage, r);
|
callingPackage, r);
|
||||||
|
|
||||||
final int uidState = mAm.getUidStateLocked(callingUid);
|
final int uidState = mAm.getUidStateLocked(callingUid);
|
||||||
|
int callerTargetSdkVersion = INVALID_UID;
|
||||||
|
try {
|
||||||
|
ApplicationInfo ai = mAm.mContext.getPackageManager().getApplicationInfoAsUser(
|
||||||
|
callingPackage, PackageManager.MATCH_KNOWN_PACKAGES, userId);
|
||||||
|
callerTargetSdkVersion = ai.targetSdkVersion;
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
}
|
||||||
final String debugInfo =
|
final String debugInfo =
|
||||||
"[callingPackage: " + callingPackage
|
"[callingPackage: " + callingPackage
|
||||||
+ "; callingUid: " + callingUid
|
+ "; callingUid: " + callingUid
|
||||||
@@ -5652,6 +5648,7 @@ public final class ActiveServices {
|
|||||||
+ ",callingUid:" + tempAllowListReason.mCallingUid))
|
+ ",callingUid:" + tempAllowListReason.mCallingUid))
|
||||||
+ ">"
|
+ ">"
|
||||||
+ "; targetSdkVersion:" + r.appInfo.targetSdkVersion
|
+ "; targetSdkVersion:" + r.appInfo.targetSdkVersion
|
||||||
|
+ "; callerTargetSdkVersion:" + callerTargetSdkVersion
|
||||||
+ "; startForegroundCount:" + r.mStartForegroundCount
|
+ "; startForegroundCount:" + r.mStartForegroundCount
|
||||||
+ "]";
|
+ "]";
|
||||||
if (!debugInfo.equals(r.mInfoAllowStartForeground)) {
|
if (!debugInfo.equals(r.mInfoAllowStartForeground)) {
|
||||||
@@ -5823,7 +5820,12 @@ public final class ActiveServices {
|
|||||||
|
|
||||||
private boolean isBgFgsRestrictionEnabled(ServiceRecord r) {
|
private boolean isBgFgsRestrictionEnabled(ServiceRecord r) {
|
||||||
return mAm.mConstants.mFlagFgsStartRestrictionEnabled
|
return mAm.mConstants.mFlagFgsStartRestrictionEnabled
|
||||||
&& CompatChanges.isChangeEnabled(FGS_BG_START_RESTRICTION_CHANGE_ID, r.appInfo.uid);
|
// Checking service's targetSdkVersion.
|
||||||
|
&& CompatChanges.isChangeEnabled(FGS_BG_START_RESTRICTION_CHANGE_ID, r.appInfo.uid)
|
||||||
|
&& (!mAm.mConstants.mFgsStartRestrictionCheckCallerTargetSdk
|
||||||
|
// Checking callingUid's targetSdkVersion.
|
||||||
|
|| CompatChanges.isChangeEnabled(
|
||||||
|
FGS_BG_START_RESTRICTION_CHANGE_ID, r.mRecentCallingUid));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logFgsBackgroundStart(ServiceRecord r) {
|
private void logFgsBackgroundStart(ServiceRecord r) {
|
||||||
|
|||||||
@@ -171,6 +171,13 @@ final class ActivityManagerConstants extends ContentObserver {
|
|||||||
private static final String KEY_DEFAULT_FGS_STARTS_RESTRICTION_ENABLED =
|
private static final String KEY_DEFAULT_FGS_STARTS_RESTRICTION_ENABLED =
|
||||||
"default_fgs_starts_restriction_enabled";
|
"default_fgs_starts_restriction_enabled";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default value for mFgsStartRestrictionCheckCallerTargetSdk if not explicitly set in
|
||||||
|
* Settings.Global.
|
||||||
|
*/
|
||||||
|
private static final String KEY_DEFAULT_FGS_STARTS_RESTRICTION_CHECK_CALLER_TARGET_SDK =
|
||||||
|
"default_fgs_starts_restriction_check_caller_target_sdk";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether FGS notification display is deferred following the transition into
|
* Whether FGS notification display is deferred following the transition into
|
||||||
* the foreground state. Default behavior is {@code true} unless overridden.
|
* the foreground state. Default behavior is {@code true} unless overridden.
|
||||||
@@ -371,6 +378,13 @@ final class ActivityManagerConstants extends ContentObserver {
|
|||||||
// at all.
|
// at all.
|
||||||
volatile boolean mFlagFgsStartRestrictionEnabled = true;
|
volatile boolean mFlagFgsStartRestrictionEnabled = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether the foreground service background start restriction is enabled for
|
||||||
|
* caller app that is targeting S+.
|
||||||
|
* This is in addition to check of {@link #mFlagFgsStartRestrictionEnabled} flag.
|
||||||
|
*/
|
||||||
|
volatile boolean mFgsStartRestrictionCheckCallerTargetSdk = true;
|
||||||
|
|
||||||
// Whether we defer FGS notifications a few seconds following their transition to
|
// Whether we defer FGS notifications a few seconds following their transition to
|
||||||
// the foreground state. Applies only to S+ apps; enabled by default.
|
// the foreground state. Applies only to S+ apps; enabled by default.
|
||||||
volatile boolean mFlagFgsNotificationDeferralEnabled = true;
|
volatile boolean mFlagFgsNotificationDeferralEnabled = true;
|
||||||
@@ -554,6 +568,9 @@ final class ActivityManagerConstants extends ContentObserver {
|
|||||||
case KEY_DEFAULT_FGS_STARTS_RESTRICTION_ENABLED:
|
case KEY_DEFAULT_FGS_STARTS_RESTRICTION_ENABLED:
|
||||||
updateFgsStartsRestriction();
|
updateFgsStartsRestriction();
|
||||||
break;
|
break;
|
||||||
|
case KEY_DEFAULT_FGS_STARTS_RESTRICTION_CHECK_CALLER_TARGET_SDK:
|
||||||
|
updateFgsStartsRestrictionCheckCallerTargetSdk();
|
||||||
|
break;
|
||||||
case KEY_DEFERRED_FGS_NOTIFICATIONS_ENABLED:
|
case KEY_DEFERRED_FGS_NOTIFICATIONS_ENABLED:
|
||||||
updateFgsNotificationDeferralEnable();
|
updateFgsNotificationDeferralEnable();
|
||||||
break;
|
break;
|
||||||
@@ -829,6 +846,13 @@ final class ActivityManagerConstants extends ContentObserver {
|
|||||||
/*defaultValue*/ true);
|
/*defaultValue*/ true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateFgsStartsRestrictionCheckCallerTargetSdk() {
|
||||||
|
mFgsStartRestrictionCheckCallerTargetSdk = DeviceConfig.getBoolean(
|
||||||
|
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
|
||||||
|
KEY_DEFAULT_FGS_STARTS_RESTRICTION_CHECK_CALLER_TARGET_SDK,
|
||||||
|
/*defaultValue*/ true);
|
||||||
|
}
|
||||||
|
|
||||||
private void updateFgsNotificationDeferralEnable() {
|
private void updateFgsNotificationDeferralEnable() {
|
||||||
mFlagFgsNotificationDeferralEnabled = DeviceConfig.getBoolean(
|
mFlagFgsNotificationDeferralEnabled = DeviceConfig.getBoolean(
|
||||||
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
|
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
|
||||||
@@ -1090,6 +1114,14 @@ final class ActivityManagerConstants extends ContentObserver {
|
|||||||
pw.println(mFgToBgFgsGraceDuration);
|
pw.println(mFgToBgFgsGraceDuration);
|
||||||
pw.print(" "); pw.print(KEY_FGS_START_FOREGROUND_TIMEOUT); pw.print("=");
|
pw.print(" "); pw.print(KEY_FGS_START_FOREGROUND_TIMEOUT); pw.print("=");
|
||||||
pw.println(mFgsStartForegroundTimeoutMs);
|
pw.println(mFgsStartForegroundTimeoutMs);
|
||||||
|
pw.print(" "); pw.print(KEY_DEFAULT_BACKGROUND_ACTIVITY_STARTS_ENABLED); pw.print("=");
|
||||||
|
pw.println(mFlagBackgroundActivityStartsEnabled);
|
||||||
|
pw.print(" "); pw.print(KEY_DEFAULT_BACKGROUND_FGS_STARTS_RESTRICTION_ENABLED);
|
||||||
|
pw.print("="); pw.println(mFlagBackgroundFgsStartRestrictionEnabled);
|
||||||
|
pw.print(" "); pw.print(KEY_DEFAULT_FGS_STARTS_RESTRICTION_ENABLED); pw.print("=");
|
||||||
|
pw.println(mFlagFgsStartRestrictionEnabled);
|
||||||
|
pw.print(" "); pw.print(KEY_DEFAULT_FGS_STARTS_RESTRICTION_CHECK_CALLER_TARGET_SDK);
|
||||||
|
pw.print("="); pw.println(mFgsStartRestrictionCheckCallerTargetSdk);
|
||||||
|
|
||||||
pw.println();
|
pw.println();
|
||||||
if (mOverrideMaxCachedProcesses >= 0) {
|
if (mOverrideMaxCachedProcesses >= 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user