|
| 1 | +From 9096b0c775729d9e2e219fe66a9059ca3d723b68 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Pinyao Ting <pinyaoting@google.com> |
| 3 | +Date: Tue, 20 Aug 2024 21:17:13 +0000 |
| 4 | +Subject: [PATCH] Enforce hard limits on hosts per package and widgets per |
| 5 | + host. |
| 6 | + |
| 7 | +Bug: 353240784 |
| 8 | +Test: manually verified with PoC app that at most 20 hosts can exists |
| 9 | +Flag: EXEMPT CVE |
| 10 | +(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:7a7e930875aba78e99b2da922317760a2401a788) |
| 11 | +Merged-In: I60ee7faf57ed719f93cafad212fef24964dec99f |
| 12 | +Change-Id: I60ee7faf57ed719f93cafad212fef24964dec99f |
| 13 | +--- |
| 14 | + .../appwidget/AppWidgetServiceImpl.java | 50 ++++++++++++++++++- |
| 15 | + 1 file changed, 49 insertions(+), 1 deletion(-) |
| 16 | + |
| 17 | +diff --git a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java |
| 18 | +index 61a86560178a..c5812e54d435 100644 |
| 19 | +--- a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java |
| 20 | ++++ b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java |
| 21 | +@@ -170,6 +170,15 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku |
| 22 | + // used to verify which request has successfully been received by the host. |
| 23 | + private static final AtomicLong UPDATE_COUNTER = new AtomicLong(); |
| 24 | + |
| 25 | ++ // Hard limit of number of hosts an app can create, note that the app that hosts the widgets |
| 26 | ++ // can have multiple instances of {@link AppWidgetHost}, typically in respect to different |
| 27 | ++ // surfaces in the host app. |
| 28 | ++ // @see AppWidgetHost |
| 29 | ++ // @see AppWidgetHost#mHostId |
| 30 | ++ private static final int MAX_NUMBER_OF_HOSTS_PER_PACKAGE = 20; |
| 31 | ++ // Hard limit of number of widgets can be pinned by a host. |
| 32 | ++ private static final int MAX_NUMBER_OF_WIDGETS_PER_HOST = 200; |
| 33 | ++ |
| 34 | + private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 35 | + @Override |
| 36 | + public void onReceive(Context context, Intent intent) { |
| 37 | +@@ -1698,7 +1707,7 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku |
| 38 | + if (host != null) { |
| 39 | + return host; |
| 40 | + } |
| 41 | +- |
| 42 | ++ ensureHostCountBeforeAddLocked(id); |
| 43 | + host = new Host(); |
| 44 | + host.id = id; |
| 45 | + mHosts.add(host); |
| 46 | +@@ -1706,6 +1715,24 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku |
| 47 | + return host; |
| 48 | + } |
| 49 | + |
| 50 | ++ /** |
| 51 | ++ * Ensures that the number of hosts for a package is less than the maximum number of hosts per |
| 52 | ++ * package. If the number of hosts is greater than the maximum number of hosts per package, then |
| 53 | ++ * removes the oldest host. |
| 54 | ++ */ |
| 55 | ++ private void ensureHostCountBeforeAddLocked(HostId hostId) { |
| 56 | ++ final List<Host> hosts = new ArrayList<>(); |
| 57 | ++ for (Host host : mHosts) { |
| 58 | ++ if (host.id.uid == hostId.uid |
| 59 | ++ && host.id.packageName.equals(hostId.packageName)) { |
| 60 | ++ hosts.add(host); |
| 61 | ++ } |
| 62 | ++ } |
| 63 | ++ while (hosts.size() >= MAX_NUMBER_OF_HOSTS_PER_PACKAGE) { |
| 64 | ++ deleteHostLocked(hosts.remove(0)); |
| 65 | ++ } |
| 66 | ++ } |
| 67 | ++ |
| 68 | + private void deleteHostLocked(Host host) { |
| 69 | + final int N = host.widgets.size(); |
| 70 | + for (int i = N - 1; i >= 0; i--) { |
| 71 | +@@ -2843,11 +2870,32 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku |
| 72 | + * Adds the widget to mWidgets and tracks the package name in mWidgetPackages. |
| 73 | + */ |
| 74 | + void addWidgetLocked(Widget widget) { |
| 75 | ++ ensureWidgetCountBeforeAddLocked(widget); |
| 76 | + mWidgets.add(widget); |
| 77 | + |
| 78 | + onWidgetProviderAddedOrChangedLocked(widget); |
| 79 | + } |
| 80 | + |
| 81 | ++ /** |
| 82 | ++ * Ensures that the widget count for the widget's host is not greater than the maximum |
| 83 | ++ * number of widgets per host. If the count is greater than the maximum, removes oldest widgets |
| 84 | ++ * from the host until the count is less than or equal to the maximum. |
| 85 | ++ */ |
| 86 | ++ private void ensureWidgetCountBeforeAddLocked(Widget widget) { |
| 87 | ++ if (widget.host == null || widget.host.id == null) { |
| 88 | ++ return; |
| 89 | ++ } |
| 90 | ++ final List<Widget> widgetsInSameHost = new ArrayList<>(); |
| 91 | ++ for (Widget w : mWidgets) { |
| 92 | ++ if (w.host != null && widget.host.id.equals(w.host.id)) { |
| 93 | ++ widgetsInSameHost.add(w); |
| 94 | ++ } |
| 95 | ++ } |
| 96 | ++ while (widgetsInSameHost.size() >= MAX_NUMBER_OF_WIDGETS_PER_HOST) { |
| 97 | ++ removeWidgetLocked(widgetsInSameHost.remove(0)); |
| 98 | ++ } |
| 99 | ++ } |
| 100 | ++ |
| 101 | + /** |
| 102 | + * Checks if the provider is assigned and updates the mWidgetPackages to track packages |
| 103 | + * that have bound widgets. |
| 104 | +-- |
| 105 | +2.47.1.613.gc27f4b7a9f-goog |
| 106 | + |
0 commit comments