|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package android.security.cts.CVE_2023_21291; |
| 18 | + |
| 19 | +import static android.Manifest.permission.CREATE_USERS; |
| 20 | +import static android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
| 21 | + |
| 22 | +import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 23 | + |
| 24 | +import static com.android.sts.common.SystemUtil.poll; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertFalse; |
| 27 | +import static org.junit.Assume.assumeNoException; |
| 28 | +import static org.junit.Assume.assumeTrue; |
| 29 | + |
| 30 | +import android.app.Instrumentation; |
| 31 | +import android.app.Notification; |
| 32 | +import android.app.NotificationChannel; |
| 33 | +import android.app.NotificationManager; |
| 34 | +import android.app.Person; |
| 35 | +import android.content.ContentProvider; |
| 36 | +import android.content.Context; |
| 37 | +import android.content.pm.UserInfo; |
| 38 | +import android.graphics.drawable.Icon; |
| 39 | +import android.os.UserManager; |
| 40 | +import android.provider.MediaStore; |
| 41 | +import android.service.notification.StatusBarNotification; |
| 42 | +import android.util.Log; |
| 43 | + |
| 44 | +import androidx.test.runner.AndroidJUnit4; |
| 45 | + |
| 46 | +import com.android.compatibility.common.util.SystemUtil; |
| 47 | + |
| 48 | +import org.junit.Test; |
| 49 | +import org.junit.runner.RunWith; |
| 50 | + |
| 51 | +import java.util.List; |
| 52 | + |
| 53 | +@RunWith(AndroidJUnit4.class) |
| 54 | +public class DeviceTest { |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testPocCVE_2023_21291() { |
| 58 | + try { |
| 59 | + Instrumentation instrumentation = getInstrumentation(); |
| 60 | + Context context = instrumentation.getContext(); |
| 61 | + final UserManager userManager = context.getSystemService(UserManager.class); |
| 62 | + |
| 63 | + // Check if the device supports multiple users or not |
| 64 | + assumeTrue( |
| 65 | + "This device does not support multiple users", |
| 66 | + userManager.supportsMultipleUsers()); |
| 67 | + |
| 68 | + // Get the user id of "cve_2023_21291_user" |
| 69 | + int testUserId = |
| 70 | + SystemUtil.runWithShellPermissionIdentity( |
| 71 | + () -> { |
| 72 | + List<UserInfo> list = userManager.getUsers(); |
| 73 | + for (UserInfo info : list) { |
| 74 | + if (info.toString().contains("cve_2023_21291_user")) { |
| 75 | + return info.getUserHandle().getIdentifier(); |
| 76 | + } |
| 77 | + } |
| 78 | + return -1; |
| 79 | + }, |
| 80 | + CREATE_USERS); |
| 81 | + assumeTrue("Unable to find the user cve_2023_21291_user", testUserId != -1); |
| 82 | + |
| 83 | + // Insert a placeholder content in the new user and query it to see if it has been |
| 84 | + // inserted successfully |
| 85 | + final String imagesContentUri = EXTERNAL_CONTENT_URI.toString(); |
| 86 | + assumeTrue( |
| 87 | + "Failed to insert a placeholder content in the test user", |
| 88 | + poll( |
| 89 | + () -> { |
| 90 | + try { |
| 91 | + SystemUtil.runShellCommand( |
| 92 | + instrumentation, |
| 93 | + String.format( |
| 94 | + "content insert --user %d --uri %s --bind " |
| 95 | + + "_display_name:s:cve_2023_21291.jpg", |
| 96 | + testUserId, imagesContentUri)); |
| 97 | + return SystemUtil.runShellCommand( |
| 98 | + instrumentation, |
| 99 | + String.format( |
| 100 | + "content query " + "--user %d --uri %s", |
| 101 | + testUserId, imagesContentUri)) |
| 102 | + .contains("Row"); |
| 103 | + } catch (Exception e) { |
| 104 | + Log.i("CVE-2023-21291", "Got an exception: " + e); |
| 105 | + } |
| 106 | + return false; |
| 107 | + })); |
| 108 | + |
| 109 | + // Create notificationManager |
| 110 | + NotificationManager notificationManager = |
| 111 | + context.getSystemService(NotificationManager.class); |
| 112 | + |
| 113 | + // Create notificationChannel |
| 114 | + String channelId = "cve_2023_21291_channel_id"; |
| 115 | + notificationManager.createNotificationChannel( |
| 116 | + new NotificationChannel( |
| 117 | + channelId, |
| 118 | + "cve_2023_21291_channel_name" /* notification channel name */, |
| 119 | + NotificationManager.IMPORTANCE_DEFAULT)); |
| 120 | + |
| 121 | + // Post the Notification and check if any security exception is caught |
| 122 | + try { |
| 123 | + notificationManager.notify( |
| 124 | + 0 /* notification id */, |
| 125 | + new Notification.Builder(context) |
| 126 | + .setChannelId(channelId) |
| 127 | + .setStyle( |
| 128 | + new Notification.MessagingStyle( |
| 129 | + new Person.Builder() |
| 130 | + .setName("cve_2023_21291_person") |
| 131 | + .build()) |
| 132 | + .setShortcutIcon( |
| 133 | + Icon.createWithContentUri( |
| 134 | + ContentProvider.maybeAddUserId( |
| 135 | + EXTERNAL_CONTENT_URI, |
| 136 | + testUserId)))) |
| 137 | + .setSmallIcon( |
| 138 | + Icon.createWithData( |
| 139 | + new byte[0] /* data */, |
| 140 | + 0 /* offset */, |
| 141 | + 0 /* length */)) |
| 142 | + .build()); |
| 143 | + } catch (SecurityException securityException) { |
| 144 | + if (securityException |
| 145 | + .getLocalizedMessage() |
| 146 | + .toLowerCase() |
| 147 | + .contains(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString())) { |
| 148 | + // Ignore exception thrown with fix and exit the test |
| 149 | + return; |
| 150 | + } else { |
| 151 | + throw securityException; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // Check if notification gets posted or not, fail the test if notification gets posted |
| 156 | + assertFalse( |
| 157 | + "Device is vulnerable to b/277593270 hence images belonging to another user on" |
| 158 | + + " the same device can be displayed in conversation notifications", |
| 159 | + poll( |
| 160 | + () -> { |
| 161 | + StatusBarNotification[] activeNotifications = |
| 162 | + notificationManager.getActiveNotifications(); |
| 163 | + for (StatusBarNotification notification : activeNotifications) { |
| 164 | + if (notification |
| 165 | + .getPackageName() |
| 166 | + .equals(context.getPackageName())) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + } |
| 170 | + return false; |
| 171 | + })); |
| 172 | + } catch (Exception e) { |
| 173 | + assumeNoException(e); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments