DO NOT MERGE - Kill apps outright for API contract violations

...rather than relying on in-app code to perform the shutdown.

Bug: 128649910
Bug: 140108616
Test: manual
Test: atest OsHostTests#testForegroundServiceBadNotification
Change-Id: I94d9de50bb03c33666471e3dbd9c721e9278f7cb
Merged-In: I94d9de50bb03c33666471e3dbd9c721e9278f7cb
(cherry picked from commit 45a53e6cb8)
(cherry picked from commit e737c1bc6295286a1a44979d2281c3eb637fe8b1)
This commit is contained in:
Christopher Tate
2019-08-19 16:16:20 -07:00
committed by Chris Tate
parent 0487c3a14c
commit 72dbe56d76
7 changed files with 39 additions and 17 deletions

View File

@@ -299,7 +299,8 @@ interface IActivityManager {
void handleApplicationStrictModeViolation(in IBinder app, int penaltyMask,
in StrictMode.ViolationInfo crashInfo);
boolean isTopActivityImmersive();
void crashApplication(int uid, int initialPid, in String packageName, int userId, in String message);
void crashApplication(int uid, int initialPid, in String packageName, int userId,
in String message, boolean force);
@UnsupportedAppUsage
String getProviderMimeType(in Uri uri, int userId);
// Cause the specified process to dump the specified heap.

View File

@@ -842,6 +842,15 @@ public final class ActiveServices {
}
}
void killMisbehavingService(ServiceRecord r,
int appUid, int appPid, String localPackageName) {
synchronized (mAm) {
stopServiceLocked(r);
mAm.crashApplication(appUid, appPid, localPackageName, -1,
"Bad notification for startForeground", true /*force*/);
}
}
IBinder peekServiceLocked(Intent service, String resolvedType, String callingPackage) {
ServiceLookupResult r = retrieveServiceLocked(service, null, resolvedType, callingPackage,
Binder.getCallingPid(), Binder.getCallingUid(),
@@ -3949,7 +3958,7 @@ public final class ActiveServices {
void serviceForegroundCrash(ProcessRecord app, CharSequence serviceRecord) {
mAm.crashApplication(app.uid, app.pid, app.info.packageName, app.userId,
"Context.startForegroundService() did not then call Service.startForeground(): "
+ serviceRecord);
+ serviceRecord, false /*force*/);
}
void scheduleServiceTimeoutLocked(ProcessRecord proc) {

View File

@@ -3596,7 +3596,7 @@ public class ActivityManagerService extends IActivityManager.Stub
@Override
public void crashApplication(int uid, int initialPid, String packageName, int userId,
String message) {
String message, boolean force) {
if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
!= PackageManager.PERMISSION_GRANTED) {
String msg = "Permission Denial: crashApplication() from pid="
@@ -3608,7 +3608,8 @@ public class ActivityManagerService extends IActivityManager.Stub
}
synchronized(this) {
mAppErrors.scheduleAppCrashLocked(uid, initialPid, packageName, userId, message);
mAppErrors.scheduleAppCrashLocked(uid, initialPid, packageName, userId,
message, force);
}
}

View File

@@ -1058,7 +1058,7 @@ final class ActivityManagerShellCommand extends ShellCommand {
} catch (NumberFormatException e) {
packageName = arg;
}
mInterface.crashApplication(-1, pid, packageName, userId, "shell-induced crash");
mInterface.crashApplication(-1, pid, packageName, userId, "shell-induced crash", false);
return 0;
}

View File

@@ -314,20 +314,24 @@ class AppErrors {
}
void killAppAtUserRequestLocked(ProcessRecord app, Dialog fromDialog) {
app.setCrashing(false);
app.crashingReport = null;
app.setNotResponding(false);
app.notRespondingReport = null;
if (app.anrDialog == fromDialog) {
app.anrDialog = null;
}
if (app.waitDialog == fromDialog) {
app.waitDialog = null;
}
killAppImmediateLocked(app, "user-terminated", "user request after error");
}
private void killAppImmediateLocked(ProcessRecord app, String reason, String killReason) {
app.setCrashing(false);
app.crashingReport = null;
app.setNotResponding(false);
app.notRespondingReport = null;
if (app.pid > 0 && app.pid != MY_PID) {
handleAppCrashLocked(app, "user-terminated" /*reason*/,
handleAppCrashLocked(app, reason,
null /*shortMsg*/, null /*longMsg*/, null /*stackTrace*/, null /*data*/);
app.kill("user request after error", true);
app.kill(killReason, true);
}
}
@@ -341,7 +345,7 @@ class AppErrors {
* @param message
*/
void scheduleAppCrashLocked(int uid, int initialPid, String packageName, int userId,
String message) {
String message, boolean force) {
ProcessRecord proc = null;
// Figure out which process to kill. We don't trust that initialPid
@@ -374,6 +378,14 @@ class AppErrors {
}
proc.scheduleCrash(message);
if (force) {
// If the app is responsive, the scheduled crash will happen as expected
// and then the delayed summary kill will be a no-op.
final ProcessRecord p = proc;
mService.mHandler.postDelayed(
() -> killAppImmediateLocked(p, "forced", "killed for invalid state"),
5000L);
}
}
/**

View File

@@ -798,6 +798,7 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN
final String localPackageName = packageName;
final int localForegroundId = foregroundId;
final Notification _foregroundNoti = foregroundNoti;
final ServiceRecord record = this;
ams.mHandler.post(new Runnable() {
public void run() {
NotificationManagerInternal nm = LocalServices.getService(
@@ -896,10 +897,8 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN
Slog.w(TAG, "Error showing notification for service", e);
// If it gave us a garbage notification, it doesn't
// get to be foreground.
ams.setServiceForeground(instanceName, ServiceRecord.this,
0, null, 0, 0);
ams.crashApplication(appUid, appPid, localPackageName, -1,
"Bad notification for startForeground: " + e);
ams.mServices.killMisbehavingService(record,
appUid, appPid, localPackageName);
}
}
});

View File

@@ -920,7 +920,7 @@ public class NotificationManagerService extends SystemService {
() -> mAm.crashApplication(uid, initialPid, pkg, -1,
"Bad notification(tag=" + tag + ", id=" + id + ") posted from package "
+ pkg + ", crashing app(uid=" + uid + ", pid=" + initialPid + "): "
+ message));
+ message, true /* force */));
}
}