Skip to content

Commit 5985f74

Browse files
author
Android Build Coastguard Worker
committed
Merge cherrypicks of [15151698, 15151719, 15151903, 15151905, 15151907, 15151908, 15151950, 15151952, 15151953, 15151954, 15151955, 15151956, 15151958, 15151937, 15151938, 15151939, 15151860, 15151990, 15151977, 15151978, 15151979] into security-aosp-rvc-release
Change-Id: I36e1f152b8d9ca5671d29a296643478dbc7f5d45
2 parents 661a05d + 867fbc5 commit 5985f74

15 files changed

Lines changed: 313 additions & 58 deletions

File tree

core/java/android/content/pm/parsing/component/ParsedIntentInfo.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import android.annotation.Nullable;
2020
import android.content.IntentFilter;
2121
import android.os.Parcel;
22-
import android.os.Parcelable;
2322
import android.util.Pair;
2423

2524
import com.android.internal.util.DataClass;
@@ -168,19 +167,6 @@ public String toString() {
168167
+ '}';
169168
}
170169

171-
public static final Parcelable.Creator<ParsedIntentInfo> CREATOR =
172-
new Parcelable.Creator<ParsedIntentInfo>() {
173-
@Override
174-
public ParsedIntentInfo createFromParcel(Parcel source) {
175-
return new ParsedIntentInfo(source);
176-
}
177-
178-
@Override
179-
public ParsedIntentInfo[] newArray(int size) {
180-
return new ParsedIntentInfo[size];
181-
}
182-
};
183-
184170
public boolean isHasDefault() {
185171
return hasDefault;
186172
}

core/java/android/text/Layout.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,10 @@ private void ellipsize(int start, int end, int line,
23502350
final int ellipsisStringLen = ellipsisString.length();
23512351
// Use the ellipsis string only if there are that at least as many characters to replace.
23522352
final boolean useEllipsisString = ellipsisCount >= ellipsisStringLen;
2353-
for (int i = 0; i < ellipsisCount; i++) {
2353+
final int min = Math.max(0, start - ellipsisStart - lineStart);
2354+
final int max = Math.min(ellipsisCount, end - ellipsisStart - lineStart);
2355+
2356+
for (int i = min; i < max; i++) {
23542357
final char c;
23552358
if (useEllipsisString && i < ellipsisStringLen) {
23562359
c = ellipsisString.charAt(i);
@@ -2359,9 +2362,7 @@ private void ellipsize(int start, int end, int line,
23592362
}
23602363

23612364
final int a = i + ellipsisStart + lineStart;
2362-
if (start <= a && a < end) {
2363-
dest[destoff + a - start] = c;
2364-
}
2365+
dest[destoff + a - start] = c;
23652366
}
23662367
}
23672368

packages/Shell/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
<provider android:name=".HeapDumpProvider"
347347
android:authorities="com.android.shell.heapdump"
348348
android:grantUriPermissions="true"
349-
android:exported="true" />
349+
android:exported="false" />
350350

351351
<activity
352352
android:name=".BugreportWarningActivity"

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

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

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

743740
return cmp;
744741
}
@@ -1400,14 +1397,6 @@ private void setServiceForegroundInnerLocked(final ServiceRecord r, int id,
14001397
+ String.format("0x%08X", manifestType)
14011398
+ " in service element of manifest file");
14021399
}
1403-
// If the foreground service is not started from TOP process, do not allow it to
1404-
// have while-in-use location/camera/microphone access.
1405-
if (!r.mAllowWhileInUsePermissionInFgs) {
1406-
Slog.w(TAG,
1407-
"Foreground service started from background can not have "
1408-
+ "location/camera/microphone access: service "
1409-
+ r.shortInstanceName);
1410-
}
14111400
}
14121401
boolean alreadyStartedOp = false;
14131402
boolean stopProcStatsOp = false;
@@ -1455,6 +1444,57 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
14551444
ignoreForeground = true;
14561445
}
14571446

1447+
if (!ignoreForeground) {
1448+
if (r.mStartForegroundCount == 0) {
1449+
/*
1450+
If the service was started with startService(), not
1451+
startForegroundService(), and if startForeground() isn't called within
1452+
mFgsStartForegroundTimeoutMs, then we check the state of the app
1453+
(who owns the service, which is the app that called startForeground())
1454+
again. If the app is in the foreground, or in any other cases where
1455+
FGS-starts are allowed, then we still allow the FGS to be started.
1456+
Otherwise, startForeground() would fail.
1457+
1458+
If the service was started with startForegroundService(), then the service
1459+
must call startForeground() within a timeout anyway, so we don't need this
1460+
check.
1461+
*/
1462+
if (!r.fgRequired) {
1463+
final long delayMs = SystemClock.elapsedRealtime() - r.createRealTime;
1464+
if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) {
1465+
resetFgsRestrictionLocked(r);
1466+
setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.pid,
1467+
r.appInfo.uid, r, false);
1468+
EventLog.writeEvent(0x534e4554, "183147114",
1469+
r.appInfo.uid,
1470+
"call setFgsRestrictionLocked again due to "
1471+
+ "startForegroundTimeout");
1472+
}
1473+
}
1474+
} else if (r.mStartForegroundCount >= 1) {
1475+
// The second or later time startForeground() is called after service is
1476+
// started. Check for app state again.
1477+
final long delayMs = SystemClock.elapsedRealtime() -
1478+
r.mLastSetFgsRestrictionTime;
1479+
if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) {
1480+
resetFgsRestrictionLocked(r);
1481+
setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.pid,
1482+
r.appInfo.uid, r, false);
1483+
EventLog.writeEvent(0x534e4554, "183147114", r.appInfo.uid,
1484+
"call setFgsRestrictionLocked for "
1485+
+ (r.mStartForegroundCount + 1) + "th startForeground");
1486+
}
1487+
}
1488+
// If the foreground service is not started from TOP process, do not allow it to
1489+
// have while-in-use location/camera/microphone access.
1490+
if (!r.mAllowWhileInUsePermissionInFgs) {
1491+
Slog.w(TAG,
1492+
"Foreground service started from background can not have "
1493+
+ "location/camera/microphone access: service "
1494+
+ r.shortInstanceName);
1495+
}
1496+
}
1497+
14581498
// Apps under strict background restrictions simply don't get to have foreground
14591499
// services, so now that we've enforced the startForegroundService() contract
14601500
// we only do the machinery of making the service foreground when the app
@@ -1490,6 +1530,7 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
14901530
active.mNumActive++;
14911531
}
14921532
r.isForeground = true;
1533+
r.mStartForegroundCount++;
14931534
if (!stopProcStatsOp) {
14941535
ServiceState stracker = r.getTracker();
14951536
if (stracker != null) {
@@ -1548,6 +1589,7 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
15481589
decActiveForegroundAppLocked(smap, r);
15491590
}
15501591
r.isForeground = false;
1592+
resetFgsRestrictionLocked(r);
15511593
ServiceState stracker = r.getTracker();
15521594
if (stracker != null) {
15531595
stracker.setForeground(false, mAm.mProcessStats.getMemFactorLocked(),
@@ -2107,12 +2149,7 @@ public void run() {
21072149
}
21082150
}
21092151

2110-
if (!s.mAllowWhileInUsePermissionInFgs) {
2111-
s.mAllowWhileInUsePermissionInFgs =
2112-
shouldAllowWhileInUsePermissionInFgsLocked(callingPackage,
2113-
callingPid, callingUid,
2114-
service, s, false);
2115-
}
2152+
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, s, false);
21162153

21172154
if (s.app != null) {
21182155
if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
@@ -3408,7 +3445,7 @@ private final void bringDownServiceLocked(ServiceRecord r) {
34083445
r.isForeground = false;
34093446
r.foregroundId = 0;
34103447
r.foregroundNoti = null;
3411-
r.mAllowWhileInUsePermissionInFgs = false;
3448+
resetFgsRestrictionLocked(r);
34123449

34133450
// Clear start entries.
34143451
r.clearDeliveredStartsLocked();
@@ -4889,7 +4926,7 @@ private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw,
48894926
* @return true if allow, false otherwise.
48904927
*/
48914928
private boolean shouldAllowWhileInUsePermissionInFgsLocked(String callingPackage,
4892-
int callingPid, int callingUid, Intent intent, ServiceRecord r,
4929+
int callingPid, int callingUid, ServiceRecord r,
48934930
boolean allowBackgroundActivityStarts) {
48944931
// Is the background FGS start restriction turned on?
48954932
if (!mAm.mConstants.mFlagBackgroundFgsStartRestrictionEnabled) {
@@ -4960,4 +4997,32 @@ private boolean shouldAllowWhileInUsePermissionInFgsLocked(String callingPackage
49604997
}
49614998
return false;
49624999
}
5000+
5001+
boolean canAllowWhileInUsePermissionInFgsLocked(int callingPid, int callingUid,
5002+
String callingPackage) {
5003+
return shouldAllowWhileInUsePermissionInFgsLocked(
5004+
callingPackage, callingPid, callingUid, null, false);
5005+
}
5006+
5007+
/**
5008+
* In R, mAllowWhileInUsePermissionInFgs is to allow while-in-use permissions in foreground
5009+
* service or not. while-in-use permissions in FGS started from background might be restricted.
5010+
* @param callingPackage caller app's package name.
5011+
* @param callingUid caller app's uid.
5012+
* @param r the service to start.
5013+
* @return true if allow, false otherwise.
5014+
*/
5015+
private void setFgsRestrictionLocked(String callingPackage,
5016+
int callingPid, int callingUid, ServiceRecord r,
5017+
boolean allowBackgroundActivityStarts) {
5018+
r.mLastSetFgsRestrictionTime = SystemClock.elapsedRealtime();
5019+
if (!r.mAllowWhileInUsePermissionInFgs) {
5020+
r.mAllowWhileInUsePermissionInFgs = shouldAllowWhileInUsePermissionInFgsLocked(
5021+
callingPackage, callingPid, callingUid, r, allowBackgroundActivityStarts);
5022+
}
5023+
}
5024+
5025+
private void resetFgsRestrictionLocked(ServiceRecord r) {
5026+
r.mAllowWhileInUsePermissionInFgs = false;
5027+
}
49635028
}

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

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

9293
private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
@@ -120,6 +121,7 @@ final class ActivityManagerConstants extends ContentObserver {
120121
private static final boolean DEFAULT_PROCESS_START_ASYNC = true;
121122
private static final long DEFAULT_MEMORY_INFO_THROTTLE_TIME = 5*60*1000;
122123
private static final long DEFAULT_TOP_TO_FGS_GRACE_DURATION = 15 * 1000;
124+
private static final int DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS = 10 * 1000;
123125
private static final int DEFAULT_PENDINGINTENT_WARNING_THRESHOLD = 2000;
124126

125127
// Flag stored in the DeviceConfig API.
@@ -272,6 +274,12 @@ final class ActivityManagerConstants extends ContentObserver {
272274
// this long.
273275
public long TOP_TO_FGS_GRACE_DURATION = DEFAULT_TOP_TO_FGS_GRACE_DURATION;
274276

277+
/**
278+
* When service started from background, before the timeout it can be promoted to FGS by calling
279+
* Service.startForeground().
280+
*/
281+
volatile long mFgsStartForegroundTimeoutMs = DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS;
282+
275283
// Indicates whether the activity starts logging is enabled.
276284
// Controlled by Settings.Global.ACTIVITY_STARTS_LOGGING_ENABLED
277285
volatile boolean mFlagActivityStartsLoggingEnabled;
@@ -415,6 +423,9 @@ public void onPropertiesChanged(Properties properties) {
415423
case KEY_MIN_ASSOC_LOG_DURATION:
416424
updateMinAssocLogDuration();
417425
break;
426+
case KEY_FGS_START_FOREGROUND_TIMEOUT:
427+
updateFgsStartForegroundTimeout();
428+
break;
418429
default:
419430
break;
420431
}
@@ -687,6 +698,13 @@ private void updateMinAssocLogDuration() {
687698
/* defaultValue */ DEFAULT_MIN_ASSOC_LOG_DURATION);
688699
}
689700

701+
private void updateFgsStartForegroundTimeout() {
702+
mFgsStartForegroundTimeoutMs = DeviceConfig.getLong(
703+
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
704+
KEY_FGS_START_FOREGROUND_TIMEOUT,
705+
DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS);
706+
}
707+
690708
void dump(PrintWriter pw) {
691709
pw.println("ACTIVITY MANAGER SETTINGS (dumpsys activity settings) "
692710
+ Settings.Global.ACTIVITY_MANAGER_CONSTANTS + ":");
@@ -759,6 +777,8 @@ void dump(PrintWriter pw) {
759777
pw.println(Arrays.toString(IMPERCEPTIBLE_KILL_EXEMPT_PACKAGES.toArray()));
760778
pw.print(" "); pw.print(KEY_MIN_ASSOC_LOG_DURATION); pw.print("=");
761779
pw.println(MIN_ASSOC_LOG_DURATION);
780+
pw.print(" "); pw.print(KEY_FGS_START_FOREGROUND_TIMEOUT); pw.print("=");
781+
pw.println(mFgsStartForegroundTimeoutMs);
762782

763783
pw.println();
764784
if (mOverrideMaxCachedProcesses >= 0) {

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
import com.android.server.compat.PlatformCompat;
9595

9696
import java.io.BufferedReader;
97-
import java.io.File;
9897
import java.io.IOException;
9998
import java.io.InputStream;
10099
import java.io.InputStreamReader;
@@ -787,8 +786,7 @@ int runTraceIpcStop(PrintWriter pw) throws RemoteException {
787786
return -1;
788787
}
789788

790-
File file = new File(filename);
791-
file.delete();
789+
// Writes an error message to stderr on failure
792790
ParcelFileDescriptor fd = openFileForSystem(filename, "w");
793791
if (fd == null) {
794792
return -1;
@@ -942,16 +940,16 @@ int runDumpHeap(PrintWriter pw) throws RemoteException {
942940
String logNameTimeString = LOG_NAME_TIME_FORMATTER.format(localDateTime);
943941
heapFile = "/data/local/tmp/heapdump-" + logNameTimeString + ".prof";
944942
}
945-
pw.println("File: " + heapFile);
946-
pw.flush();
947943

948-
File file = new File(heapFile);
949-
file.delete();
944+
// Writes an error message to stderr on failure
950945
ParcelFileDescriptor fd = openFileForSystem(heapFile, "w");
951946
if (fd == null) {
952947
return -1;
953948
}
954949

950+
pw.println("File: " + heapFile);
951+
pw.flush();
952+
955953
final CountDownLatch latch = new CountDownLatch(1);
956954

957955
final RemoteCallback finishCallback = new RemoteCallback(new OnResultListener() {

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

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

142146
// the most recent package that start/bind this service.
143147
String mRecentCallingPackage;
@@ -400,6 +404,8 @@ void dump(PrintWriter pw, String prefix) {
400404
}
401405
pw.print(prefix); pw.print("allowWhileInUsePermissionInFgs=");
402406
pw.println(mAllowWhileInUsePermissionInFgs);
407+
pw.print(prefix); pw.print("startForegroundCount=");
408+
pw.println(mStartForegroundCount);
403409
pw.print(prefix); pw.print("recentCallingPackage=");
404410
pw.println(mRecentCallingPackage);
405411
if (delayed) {

0 commit comments

Comments
 (0)