Forbid granting access to NLSes with too-long component names

This makes the limitation, which was previously only checked on the Settings UI, enforced everywhere.

Fixes: 260570119
Fixes: 286043036
Test: atest + manually
Change-Id: I4c25d80978cb37a8fa1531f5045259d25ac64692
Merged-In: I4c25d80978cb37a8fa1531f5045259d25ac64692
This commit is contained in:
Matías Hernández
2023-06-15 18:31:34 +02:00
parent 964e3b6230
commit 8a40b0b3a1
4 changed files with 41 additions and 1 deletions

View File

@@ -561,6 +561,12 @@ public class NotificationManager {
*/ */
public static final int BUBBLE_PREFERENCE_SELECTED = 2; public static final int BUBBLE_PREFERENCE_SELECTED = 2;
/**
* Maximum length of the component name of a registered NotificationListenerService.
* @hide
*/
public static int MAX_SERVICE_COMPONENT_NAME_LENGTH = 500;
@UnsupportedAppUsage @UnsupportedAppUsage
private static INotificationManager sService; private static INotificationManager sService;

View File

@@ -5381,6 +5381,11 @@ public class NotificationManagerService extends SystemService {
boolean granted, boolean userSet) { boolean granted, boolean userSet) {
Objects.requireNonNull(listener); Objects.requireNonNull(listener);
checkNotificationListenerAccess(); checkNotificationListenerAccess();
if (granted && listener.flattenToString().length()
> NotificationManager.MAX_SERVICE_COMPONENT_NAME_LENGTH) {
throw new IllegalArgumentException(
"Component name too long: " + listener.flattenToString());
}
if (!userSet && isNotificationListenerAccessUserSet(listener)) { if (!userSet && isNotificationListenerAccessUserSet(listener)) {
// Don't override user's choice // Don't override user's choice
return; return;

View File

@@ -1049,7 +1049,11 @@ public class VrManagerService extends SystemService
for (ComponentName c : possibleServices) { for (ComponentName c : possibleServices) {
if (Objects.equals(c.getPackageName(), pkg)) { if (Objects.equals(c.getPackageName(), pkg)) {
nm.setNotificationListenerAccessGrantedForUser(c, userId, true); try {
nm.setNotificationListenerAccessGrantedForUser(c, userId, true);
} catch (Exception e) {
Slog.w(TAG, "Could not grant NLS access to package " + pkg, e);
}
} }
} }
} }

View File

@@ -76,6 +76,7 @@ import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail; import static junit.framework.Assert.fail;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyLong; import static org.mockito.Matchers.anyLong;
@@ -3147,6 +3148,30 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
any(), anyInt(), anyBoolean(), anyBoolean(), anyBoolean()); any(), anyInt(), anyBoolean(), anyBoolean(), anyBoolean());
} }
@Test
public void testSetListenerAccessForUser_grantWithNameTooLong_throws() {
UserHandle user = UserHandle.of(mContext.getUserId() + 10);
ComponentName c = new ComponentName("com.example.package",
com.google.common.base.Strings.repeat("Blah", 150));
assertThrows(IllegalArgumentException.class,
() -> mBinderService.setNotificationListenerAccessGrantedForUser(
c, user.getIdentifier(), /* enabled= */ true, true));
}
@Test
public void testSetListenerAccessForUser_revokeWithNameTooLong_okay() throws Exception {
UserHandle user = UserHandle.of(mContext.getUserId() + 10);
ComponentName c = new ComponentName("com.example.package",
com.google.common.base.Strings.repeat("Blah", 150));
mBinderService.setNotificationListenerAccessGrantedForUser(
c, user.getIdentifier(), /* enabled= */ false, true);
verify(mListeners).setPackageOrComponentEnabled(
c.flattenToString(), user.getIdentifier(), true, /* enabled= */ false, true);
}
@Test @Test
public void testSetAssistantAccessForUser() throws Exception { public void testSetAssistantAccessForUser() throws Exception {
UserInfo ui = new UserInfo(); UserInfo ui = new UserInfo();