Skip to content

Commit 0a48d1b

Browse files
haggertkBayerischeMotorenWerke
authored andcommitted
Revert "Revert "Revert "BG-FGS-start while-in-use permission restriction improve..."""
Upstream change 5d30b70 ("BG-FGS-start while-in-use permission restriction improvement") properly fixes what we needed to work-around. This reverts commit b64352d. Change-Id: I00b3ab4161dceddff015e4f5365cd777acfe2faa
1 parent 41021a8 commit 0a48d1b

3 files changed

Lines changed: 21 additions & 112 deletions

File tree

services/core/java/com/android/server/am/ActiveServices.java

Lines changed: 21 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,11 @@ ComponentName startServiceLocked(IApplicationThread caller, Intent service, Stri
734734
}
735735
ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting);
736736

737-
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, r,
738-
allowBackgroundActivityStarts);
737+
if (!r.mAllowWhileInUsePermissionInFgs) {
738+
r.mAllowWhileInUsePermissionInFgs =
739+
shouldAllowWhileInUsePermissionInFgsLocked(callingPackage, callingPid,
740+
callingUid, service, r, allowBackgroundActivityStarts);
741+
}
739742

740743
return cmp;
741744
}
@@ -1408,6 +1411,14 @@ private void setServiceForegroundInnerLocked(final ServiceRecord r, int id,
14081411
+ String.format("0x%08X", manifestType)
14091412
+ " in service element of manifest file");
14101413
}
1414+
// If the foreground service is not started from TOP process, do not allow it to
1415+
// have while-in-use location/camera/microphone access.
1416+
if (!r.mAllowWhileInUsePermissionInFgs) {
1417+
Slog.w(TAG,
1418+
"Foreground service started from background can not have "
1419+
+ "location/camera/microphone access: service "
1420+
+ r.shortInstanceName);
1421+
}
14111422
}
14121423
boolean alreadyStartedOp = false;
14131424
boolean stopProcStatsOp = false;
@@ -1455,57 +1466,6 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
14551466
ignoreForeground = true;
14561467
}
14571468

1458-
if (!ignoreForeground) {
1459-
if (r.mStartForegroundCount == 0) {
1460-
/*
1461-
If the service was started with startService(), not
1462-
startForegroundService(), and if startForeground() isn't called within
1463-
mFgsStartForegroundTimeoutMs, then we check the state of the app
1464-
(who owns the service, which is the app that called startForeground())
1465-
again. If the app is in the foreground, or in any other cases where
1466-
FGS-starts are allowed, then we still allow the FGS to be started.
1467-
Otherwise, startForeground() would fail.
1468-
1469-
If the service was started with startForegroundService(), then the service
1470-
must call startForeground() within a timeout anyway, so we don't need this
1471-
check.
1472-
*/
1473-
if (!r.fgRequired) {
1474-
final long delayMs = SystemClock.elapsedRealtime() - r.createRealTime;
1475-
if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) {
1476-
resetFgsRestrictionLocked(r);
1477-
setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.pid,
1478-
r.appInfo.uid, r, false);
1479-
EventLog.writeEvent(0x534e4554, "183147114",
1480-
r.appInfo.uid,
1481-
"call setFgsRestrictionLocked again due to "
1482-
+ "startForegroundTimeout");
1483-
}
1484-
}
1485-
} else if (r.mStartForegroundCount >= 1) {
1486-
// The second or later time startForeground() is called after service is
1487-
// started. Check for app state again.
1488-
final long delayMs = SystemClock.elapsedRealtime() -
1489-
r.mLastSetFgsRestrictionTime;
1490-
if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) {
1491-
resetFgsRestrictionLocked(r);
1492-
setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.pid,
1493-
r.appInfo.uid, r, false);
1494-
EventLog.writeEvent(0x534e4554, "183147114", r.appInfo.uid,
1495-
"call setFgsRestrictionLocked for "
1496-
+ (r.mStartForegroundCount + 1) + "th startForeground");
1497-
}
1498-
}
1499-
// If the foreground service is not started from TOP process, do not allow it to
1500-
// have while-in-use location/camera/microphone access.
1501-
if (!r.mAllowWhileInUsePermissionInFgs) {
1502-
Slog.w(TAG,
1503-
"Foreground service started from background can not have "
1504-
+ "location/camera/microphone access: service "
1505-
+ r.shortInstanceName);
1506-
}
1507-
}
1508-
15091469
// Apps under strict background restrictions simply don't get to have foreground
15101470
// services, so now that we've enforced the startForegroundService() contract
15111471
// we only do the machinery of making the service foreground when the app
@@ -1541,7 +1501,6 @@ must call startForeground() within a timeout anyway, so we don't need this
15411501
active.mNumActive++;
15421502
}
15431503
r.isForeground = true;
1544-
r.mStartForegroundCount++;
15451504
if (!stopProcStatsOp) {
15461505
ServiceState stracker = r.getTracker();
15471506
if (stracker != null) {
@@ -1600,7 +1559,6 @@ must call startForeground() within a timeout anyway, so we don't need this
16001559
decActiveForegroundAppLocked(smap, r);
16011560
}
16021561
r.isForeground = false;
1603-
resetFgsRestrictionLocked(r);
16041562
ServiceState stracker = r.getTracker();
16051563
if (stracker != null) {
16061564
stracker.setForeground(false, mAm.mProcessStats.getMemFactorLocked(),
@@ -2160,7 +2118,12 @@ public void run() {
21602118
}
21612119
}
21622120

2163-
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, s, false);
2121+
if (!s.mAllowWhileInUsePermissionInFgs) {
2122+
s.mAllowWhileInUsePermissionInFgs =
2123+
shouldAllowWhileInUsePermissionInFgsLocked(callingPackage,
2124+
callingPid, callingUid,
2125+
service, s, false);
2126+
}
21642127

21652128
if (s.app != null) {
21662129
if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
@@ -3456,7 +3419,7 @@ private final void bringDownServiceLocked(ServiceRecord r) {
34563419
r.isForeground = false;
34573420
r.foregroundId = 0;
34583421
r.foregroundNoti = null;
3459-
resetFgsRestrictionLocked(r);
3422+
r.mAllowWhileInUsePermissionInFgs = false;
34603423

34613424
// Clear start entries.
34623425
r.clearDeliveredStartsLocked();
@@ -4937,7 +4900,7 @@ private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw,
49374900
* @return true if allow, false otherwise.
49384901
*/
49394902
private boolean shouldAllowWhileInUsePermissionInFgsLocked(String callingPackage,
4940-
int callingPid, int callingUid, ServiceRecord r,
4903+
int callingPid, int callingUid, Intent intent, ServiceRecord r,
49414904
boolean allowBackgroundActivityStarts) {
49424905
// Is the background FGS start restriction turned on?
49434906
if (!mAm.mConstants.mFlagBackgroundFgsStartRestrictionEnabled) {
@@ -5019,32 +4982,4 @@ private boolean shouldAllowWhileInUsePermissionInFgsLocked(String callingPackage
50194982
}
50204983
return false;
50214984
}
5022-
5023-
boolean canAllowWhileInUsePermissionInFgsLocked(int callingPid, int callingUid,
5024-
String callingPackage) {
5025-
return shouldAllowWhileInUsePermissionInFgsLocked(
5026-
callingPackage, callingPid, callingUid, null, false);
5027-
}
5028-
5029-
/**
5030-
* In R, mAllowWhileInUsePermissionInFgs is to allow while-in-use permissions in foreground
5031-
* service or not. while-in-use permissions in FGS started from background might be restricted.
5032-
* @param callingPackage caller app's package name.
5033-
* @param callingUid caller app's uid.
5034-
* @param r the service to start.
5035-
* @return true if allow, false otherwise.
5036-
*/
5037-
private void setFgsRestrictionLocked(String callingPackage,
5038-
int callingPid, int callingUid, ServiceRecord r,
5039-
boolean allowBackgroundActivityStarts) {
5040-
r.mLastSetFgsRestrictionTime = SystemClock.elapsedRealtime();
5041-
if (!r.mAllowWhileInUsePermissionInFgs) {
5042-
r.mAllowWhileInUsePermissionInFgs = shouldAllowWhileInUsePermissionInFgsLocked(
5043-
callingPackage, callingPid, callingUid, r, allowBackgroundActivityStarts);
5044-
}
5045-
}
5046-
5047-
private void resetFgsRestrictionLocked(ServiceRecord r) {
5048-
r.mAllowWhileInUsePermissionInFgs = false;
5049-
}
50504985
}

services/core/java/com/android/server/am/ActivityManagerConstants.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ final class ActivityManagerConstants extends ContentObserver {
8888
static final String KEY_PROCESS_START_ASYNC = "process_start_async";
8989
static final String KEY_MEMORY_INFO_THROTTLE_TIME = "memory_info_throttle_time";
9090
static final String KEY_TOP_TO_FGS_GRACE_DURATION = "top_to_fgs_grace_duration";
91-
static final String KEY_FGS_START_FOREGROUND_TIMEOUT = "fgs_start_foreground_timeout";
9291
static final String KEY_PENDINGINTENT_WARNING_THRESHOLD = "pendingintent_warning_threshold";
9392

9493
private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
@@ -122,7 +121,6 @@ final class ActivityManagerConstants extends ContentObserver {
122121
private static final boolean DEFAULT_PROCESS_START_ASYNC = true;
123122
private static final long DEFAULT_MEMORY_INFO_THROTTLE_TIME = 5*60*1000;
124123
private static final long DEFAULT_TOP_TO_FGS_GRACE_DURATION = 15 * 1000;
125-
private static final int DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS = 10 * 1000;
126124
private static final int DEFAULT_PENDINGINTENT_WARNING_THRESHOLD = 2000;
127125

128126
// Flag stored in the DeviceConfig API.
@@ -275,12 +273,6 @@ final class ActivityManagerConstants extends ContentObserver {
275273
// this long.
276274
public long TOP_TO_FGS_GRACE_DURATION = DEFAULT_TOP_TO_FGS_GRACE_DURATION;
277275

278-
/**
279-
* When service started from background, before the timeout it can be promoted to FGS by calling
280-
* Service.startForeground().
281-
*/
282-
volatile long mFgsStartForegroundTimeoutMs = DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS;
283-
284276
// Indicates whether the activity starts logging is enabled.
285277
// Controlled by Settings.Global.ACTIVITY_STARTS_LOGGING_ENABLED
286278
volatile boolean mFlagActivityStartsLoggingEnabled;
@@ -429,9 +421,6 @@ public void onPropertiesChanged(Properties properties) {
429421
case KEY_MIN_ASSOC_LOG_DURATION:
430422
updateMinAssocLogDuration();
431423
break;
432-
case KEY_FGS_START_FOREGROUND_TIMEOUT:
433-
updateFgsStartForegroundTimeout();
434-
break;
435424
default:
436425
break;
437426
}
@@ -708,13 +697,6 @@ private void updateMinAssocLogDuration() {
708697
/* defaultValue */ DEFAULT_MIN_ASSOC_LOG_DURATION);
709698
}
710699

711-
private void updateFgsStartForegroundTimeout() {
712-
mFgsStartForegroundTimeoutMs = DeviceConfig.getLong(
713-
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
714-
KEY_FGS_START_FOREGROUND_TIMEOUT,
715-
DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS);
716-
}
717-
718700
void dump(PrintWriter pw) {
719701
pw.println("ACTIVITY MANAGER SETTINGS (dumpsys activity settings) "
720702
+ Settings.Global.ACTIVITY_MANAGER_CONSTANTS + ":");
@@ -787,8 +769,6 @@ void dump(PrintWriter pw) {
787769
pw.println(Arrays.toString(IMPERCEPTIBLE_KILL_EXEMPT_PACKAGES.toArray()));
788770
pw.print(" "); pw.print(KEY_MIN_ASSOC_LOG_DURATION); pw.print("=");
789771
pw.println(MIN_ASSOC_LOG_DURATION);
790-
pw.print(" "); pw.print(KEY_FGS_START_FOREGROUND_TIMEOUT); pw.print("=");
791-
pw.println(mFgsStartForegroundTimeoutMs);
792772

793773
pw.println();
794774
if (mOverrideMaxCachedProcesses >= 0) {

services/core/java/com/android/server/am/ServiceRecord.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN
142142
// allow while-in-use permissions in foreground service or not.
143143
// while-in-use permissions in FGS started from background might be restricted.
144144
boolean mAllowWhileInUsePermissionInFgs;
145-
// The number of times Service.startForeground() is called;
146-
int mStartForegroundCount;
147-
// Last time mAllowWhileInUsePermissionInFgs is set.
148-
long mLastSetFgsRestrictionTime;
149145

150146
// the most recent package that start/bind this service.
151147
String mRecentCallingPackage;
@@ -410,8 +406,6 @@ void dump(PrintWriter pw, String prefix) {
410406
}
411407
pw.print(prefix); pw.print("allowWhileInUsePermissionInFgs=");
412408
pw.println(mAllowWhileInUsePermissionInFgs);
413-
pw.print(prefix); pw.print("startForegroundCount=");
414-
pw.println(mStartForegroundCount);
415409
pw.print(prefix); pw.print("recentCallingPackage=");
416410
pw.println(mRecentCallingPackage);
417411
if (delayed) {

0 commit comments

Comments
 (0)