From 8473a2c1035c24ba9037cce779e419d07fd4c124 Mon Sep 17 00:00:00 2001 From: Alan Stokes Date: Mon, 29 Apr 2019 13:51:18 +0100 Subject: [PATCH] Fix for client tracking on bind to service. We were not correctly updating the bound client UIDs when binding to a service with an already-running process. This could result in a background activity start by the app hosting the service being incorrectly blocked. (Also includes a drive-by fix of a potential NPE.) Bug: 131468397 Test: Manually verified YouTube VR problem is fixed. Test: atest BackgroundActivityLaunchTest Test: atest RootWindowContainerTests Test: atest WmTests:ActivityStarterTests Test: atest CtsWindowManagerDeviceTestCases:ActivityStarterTests Test: atest CtsAppTestCases:.ServiceTest Change-Id: I153cf3678f10439075af39a8bd88ff8d492e1ffe --- .../com/android/server/am/ActiveServices.java | 13 ++++--------- .../com/android/server/am/ProcessRecord.java | 4 ++-- .../com/android/server/am/ServiceRecord.java | 19 ++++++++++--------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index 76136dfc81a0d..83281ac2b45b8 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -1768,12 +1768,7 @@ public final class ActiveServices { callerApp.uid, callerApp.processName, callingPackage); IBinder binder = connection.asBinder(); - ArrayList clist = s.getConnections().get(binder); - if (clist == null) { - clist = new ArrayList(); - s.putConnection(binder, clist); - } - clist.add(c); + s.addConnection(binder, c); b.connections.add(c); if (activity != null) { activity.addConnection(c); @@ -1792,9 +1787,9 @@ public final class ActiveServices { if (s.app != null) { updateServiceClientActivitiesLocked(s.app, c, true); } - clist = mServiceConnections.get(binder); + ArrayList clist = mServiceConnections.get(binder); if (clist == null) { - clist = new ArrayList(); + clist = new ArrayList<>(); mServiceConnections.put(binder, clist); } clist.add(c); @@ -3828,8 +3823,8 @@ public final class ActiveServices { public PendingIntent getRunningServiceControlPanelLocked(ComponentName name) { int userId = UserHandle.getUserId(Binder.getCallingUid()); ServiceRecord r = getServiceByNameLocked(name, userId); - ArrayMap> connections = r.getConnections(); if (r != null) { + ArrayMap> connections = r.getConnections(); for (int conni = connections.size() - 1; conni >= 0; conni--) { ArrayList conn = connections.valueAt(conni); for (int i=0; i clientUids) { - mBoundClientUids.addAll(clientUids); + void addBoundClientUid(int clientUid) { + mBoundClientUids.add(clientUid); mWindowProcessController.setBoundClientUids(mBoundClientUids); } diff --git a/services/core/java/com/android/server/am/ServiceRecord.java b/services/core/java/com/android/server/am/ServiceRecord.java index 27c62d03f960e..beaea41399098 100644 --- a/services/core/java/com/android/server/am/ServiceRecord.java +++ b/services/core/java/com/android/server/am/ServiceRecord.java @@ -38,7 +38,6 @@ import android.os.SystemClock; import android.os.UserHandle; import android.provider.Settings; import android.util.ArrayMap; -import android.util.ArraySet; import android.util.Slog; import android.util.TimeUtils; import android.util.proto.ProtoOutputStream; @@ -580,15 +579,17 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN return connections; } - void putConnection(IBinder binder, ArrayList clist) { - connections.put(binder, clist); - // if we have a process attached, add bound client uids of this connection to it + void addConnection(IBinder binder, ConnectionRecord c) { + ArrayList clist = connections.get(binder); + if (clist == null) { + clist = new ArrayList<>(); + connections.put(binder, clist); + } + clist.add(c); + + // if we have a process attached, add bound client uid of this connection to it if (app != null) { - ArraySet boundClientUids = new ArraySet<>(); - for (int i = 0; i < clist.size(); i++) { - boundClientUids.add(clist.get(i).clientUid); - } - app.addBoundClientUids(boundClientUids); + app.addBoundClientUid(c.clientUid); } }