• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

frameworks/base


Commit MetaInfo

修订版c6fd63a7a80f06a89b34aa1894694922c3af9f20 (tree)
时间2020-03-13 05:34:25
作者Christopher Tate <ctate@goog...>
CommiterAnis Assi

Log Message

DO NOT MERGE - Kill apps outright for API contract violations

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

Backport of security fix.

Bug: 128649910
Bug: 140108616
Test: manual
Test: atest OsHostTests#testForegroundServiceBadNotification
Change-Id: I94d9de50bb03c33666471e3dbd9c721e9278f7cb
Merged-In: I94d9de50bb03c33666471e3dbd9c721e9278f7cb
(cherry picked from commit 874c974f73839da761177a4e0a53b7f4a7d29288)

更改概述

差异

--- a/core/java/android/app/IActivityManager.aidl
+++ b/core/java/android/app/IActivityManager.aidl
@@ -266,7 +266,8 @@ interface IActivityManager {
266266 boolean isImmersive(in IBinder token);
267267 void setImmersive(in IBinder token, boolean immersive);
268268 boolean isTopActivityImmersive();
269- void crashApplication(int uid, int initialPid, in String packageName, int userId, in String message);
269+ void crashApplication(int uid, int initialPid, in String packageName, int userId,
270+ in String message, boolean force);
270271 String getProviderMimeType(in Uri uri, int userId);
271272 IBinder newUriPermissionOwner(in String name);
272273 void grantUriPermissionFromOwner(in IBinder owner, int fromUid, in String targetPkg,
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -653,6 +653,15 @@ public final class ActiveServices {
653653 }
654654 }
655655
656+ void killMisbehavingService(ServiceRecord r,
657+ int appUid, int appPid, String localPackageName) {
658+ synchronized (mAm) {
659+ stopServiceLocked(r);
660+ mAm.crashApplication(appUid, appPid, localPackageName, -1,
661+ "Bad notification for startForeground", true /*force*/);
662+ }
663+ }
664+
656665 IBinder peekServiceLocked(Intent service, String resolvedType, String callingPackage) {
657666 ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, callingPackage,
658667 Binder.getCallingPid(), Binder.getCallingUid(),
@@ -3391,7 +3400,8 @@ public final class ActiveServices {
33913400
33923401 void serviceForegroundCrash(ProcessRecord app) {
33933402 mAm.crashApplication(app.uid, app.pid, app.info.packageName, app.userId,
3394- "Context.startForegroundService() did not then call Service.startForeground()");
3403+ "Context.startForegroundService() did not then call Service.startForeground()",
3404+ false /*force*/);
33953405 }
33963406
33973407 void scheduleServiceTimeoutLocked(ProcessRecord proc) {
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -5141,7 +5141,7 @@ public class ActivityManagerService extends IActivityManager.Stub
51415141
51425142 @Override
51435143 public void crashApplication(int uid, int initialPid, String packageName, int userId,
5144- String message) {
5144+ String message, boolean force) {
51455145 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
51465146 != PackageManager.PERMISSION_GRANTED) {
51475147 String msg = "Permission Denial: crashApplication() from pid="
@@ -5153,7 +5153,8 @@ public class ActivityManagerService extends IActivityManager.Stub
51535153 }
51545154
51555155 synchronized(this) {
5156- mAppErrors.scheduleAppCrashLocked(uid, initialPid, packageName, userId, message);
5156+ mAppErrors.scheduleAppCrashLocked(uid, initialPid, packageName, userId,
5157+ message, force);
51575158 }
51585159 }
51595160
--- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
+++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
@@ -921,7 +921,7 @@ final class ActivityManagerShellCommand extends ShellCommand {
921921 } catch (NumberFormatException e) {
922922 packageName = arg;
923923 }
924- mInterface.crashApplication(-1, pid, packageName, userId, "shell-induced crash");
924+ mInterface.crashApplication(-1, pid, packageName, userId, "shell-induced crash", false);
925925 return 0;
926926 }
927927
--- a/services/core/java/com/android/server/am/AppErrors.java
+++ b/services/core/java/com/android/server/am/AppErrors.java
@@ -243,20 +243,24 @@ class AppErrors {
243243 }
244244
245245 void killAppAtUserRequestLocked(ProcessRecord app, Dialog fromDialog) {
246- app.crashing = false;
247- app.crashingReport = null;
248- app.notResponding = false;
249- app.notRespondingReport = null;
250246 if (app.anrDialog == fromDialog) {
251247 app.anrDialog = null;
252248 }
253249 if (app.waitDialog == fromDialog) {
254250 app.waitDialog = null;
255251 }
252+ killAppImmediateLocked(app, "user-terminated", "user request after error");
253+ }
254+
255+ private void killAppImmediateLocked(ProcessRecord app, String reason, String killReason) {
256+ app.crashing = false;
257+ app.crashingReport = null;
258+ app.notResponding = false;
259+ app.notRespondingReport = null;
256260 if (app.pid > 0 && app.pid != MY_PID) {
257- handleAppCrashLocked(app, "user-terminated" /*reason*/,
261+ handleAppCrashLocked(app, reason,
258262 null /*shortMsg*/, null /*longMsg*/, null /*stackTrace*/, null /*data*/);
259- app.kill("user request after error", true);
263+ app.kill(killReason, true);
260264 }
261265 }
262266
@@ -270,7 +274,7 @@ class AppErrors {
270274 * @param message
271275 */
272276 void scheduleAppCrashLocked(int uid, int initialPid, String packageName, int userId,
273- String message) {
277+ String message, boolean force) {
274278 ProcessRecord proc = null;
275279
276280 // Figure out which process to kill. We don't trust that initialPid
@@ -303,6 +307,14 @@ class AppErrors {
303307 }
304308
305309 proc.scheduleCrash(message);
310+ if (force) {
311+ // If the app is responsive, the scheduled crash will happen as expected
312+ // and then the delayed summary kill will be a no-op.
313+ final ProcessRecord p = proc;
314+ mService.mHandler.postDelayed(
315+ () -> killAppImmediateLocked(p, "forced", "killed for invalid state"),
316+ 5000L);
317+ }
306318 }
307319
308320 /**
--- a/services/core/java/com/android/server/am/ServiceRecord.java
+++ b/services/core/java/com/android/server/am/ServiceRecord.java
@@ -453,6 +453,7 @@ final class ServiceRecord extends Binder {
453453 final String localPackageName = packageName;
454454 final int localForegroundId = foregroundId;
455455 final Notification _foregroundNoti = foregroundNoti;
456+ final ServiceRecord record = this;
456457 ams.mHandler.post(new Runnable() {
457458 public void run() {
458459 NotificationManagerInternal nm = LocalServices.getService(
@@ -551,10 +552,8 @@ final class ServiceRecord extends Binder {
551552 Slog.w(TAG, "Error showing notification for service", e);
552553 // If it gave us a garbage notification, it doesn't
553554 // get to be foreground.
554- ams.setServiceForeground(name, ServiceRecord.this,
555- 0, null, 0);
556- ams.crashApplication(appUid, appPid, localPackageName, -1,
557- "Bad notification for startForeground: " + e);
555+ ams.mServices.killMisbehavingService(record,
556+ appUid, appPid, localPackageName);
558557 }
559558 }
560559 });
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -714,18 +714,23 @@ public class NotificationManagerService extends SystemService {
714714 @Override
715715 public void onNotificationError(int callingUid, int callingPid, String pkg, String tag, int id,
716716 int uid, int initialPid, String message, int userId) {
717- Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
718- + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
717+ final boolean fgService;
718+ synchronized (mNotificationLock) {
719+ NotificationRecord r = findNotificationLocked(pkg, tag, id, userId);
720+ fgService = r != null
721+ && (r.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE) != 0;
722+ }
719723 cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId,
720724 REASON_ERROR, null);
721- long ident = Binder.clearCallingIdentity();
722- try {
723- ActivityManager.getService().crashApplication(uid, initialPid, pkg, -1,
724- "Bad notification posted from package " + pkg
725- + ": " + message);
726- } catch (RemoteException e) {
725+ if (fgService) {
726+ // Still crash for foreground services, preventing the not-crash behaviour abused
727+ // by apps to give us a garbage notification and silently start a fg service.
728+ Binder.withCleanCallingIdentity(
729+ () -> mAm.crashApplication(uid, initialPid, pkg, -1,
730+ "Bad notification(tag=" + tag + ", id=" + id + ") posted from package "
731+ + pkg + ", crashing app(uid=" + uid + ", pid=" + initialPid + "): "
732+ + message, true /* force */));
727733 }
728- Binder.restoreCallingIdentity(ident);
729734 }
730735
731736 @Override