From 4a13e88b833328a0cd81f458c64194780c61c504 Mon Sep 17 00:00:00 2001 From: Alex Stetson Date: Mon, 17 Aug 2020 14:57:44 -0700 Subject: [PATCH] Add CATEGORY_CAR notification restrictions Add permission for restricting the use of CATEGORY_CAR notifications rather than restricting to system calls. Bug: 159459663 Test: manual, atest NotificationManagerServiceTest#testEnqueuedRestrictedNotifications* Change-Id: I6e8a32fec1469f5e0cbc8aa0cb765e5c04069546 --- api/system-current.txt | 1 + core/res/AndroidManifest.xml | 6 ++++++ .../car/com.google.android.car.kitchensink.xml | 1 + non-updatable-api/system-current.txt | 1 + .../NotificationManagerService.java | 5 ++++- .../NotificationManagerServiceTest.java | 18 +++++++++++------- 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/api/system-current.txt b/api/system-current.txt index 41e8593635819..9bc7231e63fbe 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -205,6 +205,7 @@ package android { field public static final String REVOKE_RUNTIME_PERMISSIONS = "android.permission.REVOKE_RUNTIME_PERMISSIONS"; field public static final String SCORE_NETWORKS = "android.permission.SCORE_NETWORKS"; field public static final String SECURE_ELEMENT_PRIVILEGED_OPERATION = "android.permission.SECURE_ELEMENT_PRIVILEGED_OPERATION"; + field public static final String SEND_CATEGORY_CAR_NOTIFICATIONS = "android.permission.SEND_CATEGORY_CAR_NOTIFICATIONS"; field public static final String SEND_DEVICE_CUSTOMIZATION_READY = "android.permission.SEND_DEVICE_CUSTOMIZATION_READY"; field public static final String SEND_SHOW_SUSPENDED_APP_DETAILS = "android.permission.SEND_SHOW_SUSPENDED_APP_DETAILS"; field public static final String SEND_SMS_NO_CONFIRMATION = "android.permission.SEND_SMS_NO_CONFIRMATION"; diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index 57c1fcf7bfb43..2902796c4edbd 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -4690,6 +4690,12 @@ + + + diff --git a/data/etc/car/com.google.android.car.kitchensink.xml b/data/etc/car/com.google.android.car.kitchensink.xml index 7292e0796bf50..59aa45e1f6e49 100644 --- a/data/etc/car/com.google.android.car.kitchensink.xml +++ b/data/etc/car/com.google.android.car.kitchensink.xml @@ -40,6 +40,7 @@ + diff --git a/non-updatable-api/system-current.txt b/non-updatable-api/system-current.txt index d1264dfdd36db..2c24e884f9ac9 100644 --- a/non-updatable-api/system-current.txt +++ b/non-updatable-api/system-current.txt @@ -205,6 +205,7 @@ package android { field public static final String REVOKE_RUNTIME_PERMISSIONS = "android.permission.REVOKE_RUNTIME_PERMISSIONS"; field public static final String SCORE_NETWORKS = "android.permission.SCORE_NETWORKS"; field public static final String SECURE_ELEMENT_PRIVILEGED_OPERATION = "android.permission.SECURE_ELEMENT_PRIVILEGED_OPERATION"; + field public static final String SEND_CATEGORY_CAR_NOTIFICATIONS = "android.permission.SEND_CATEGORY_CAR_NOTIFICATIONS"; field public static final String SEND_DEVICE_CUSTOMIZATION_READY = "android.permission.SEND_DEVICE_CUSTOMIZATION_READY"; field public static final String SEND_SHOW_SUSPENDED_APP_DETAILS = "android.permission.SEND_SHOW_SUSPENDED_APP_DETAILS"; field public static final String SEND_SMS_NO_CONFIRMATION = "android.permission.SEND_SMS_NO_CONFIRMATION"; diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 04658555f22b6..c05c2dec66864 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -8462,7 +8462,10 @@ public class NotificationManagerService extends SystemService { if (Notification.CATEGORY_CAR_EMERGENCY.equals(notification.category) || Notification.CATEGORY_CAR_WARNING.equals(notification.category) || Notification.CATEGORY_CAR_INFORMATION.equals(notification.category)) { - checkCallerIsSystem(); + getContext().enforceCallingPermission( + android.Manifest.permission.SEND_CATEGORY_CAR_NOTIFICATIONS, + String.format("Notification category %s restricted", + notification.category)); } } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index 9319bea497fb7..d6bd1f349269a 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -1086,12 +1086,18 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { } /** - * Confirm the system user on automotive devices can use car categories + * Confirm an application with the SEND_CATEGORY_CAR_NOTIFICATIONS permission on automotive + * devices can use car categories. */ @Test - public void testEnqueuedRestrictedNotifications_asSystem() throws Exception { + public void testEnqueuedRestrictedNotifications_hasPermission() throws Exception { when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) .thenReturn(true); + // SEND_CATEGORY_CAR_NOTIFICATIONS is a system-level permission that this test cannot + // obtain. Mocking out enforce permission call to ensure notifications can be created when + // permitted. + doNothing().when(mContext).enforceCallingPermission( + eq("android.permission.SEND_CATEGORY_CAR_NOTIFICATIONS"), anyString()); List categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, Notification.CATEGORY_CAR_WARNING, Notification.CATEGORY_CAR_INFORMATION); @@ -1114,7 +1120,6 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { */ @Test public void testEnqueuedRestrictedNotifications_notAutomotive() throws Exception { - mService.isSystemUid = false; when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) .thenReturn(false); List categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, @@ -1134,12 +1139,11 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { } /** - * Confirm if a non-system user tries to use the car categories on a automotive device that - * they will get a security exception + * Confirm if an application tries to use the car categories on a automotive device without the + * SEND_CATEGORY_CAR_NOTIFICATIONS permission that a security exception will be thrown. */ @Test - public void testEnqueuedRestrictedNotifications_badUser() throws Exception { - mService.isSystemUid = false; + public void testEnqueuedRestrictedNotifications_noPermission() throws Exception { when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) .thenReturn(true); List categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY,