1 /*
2  * Copyright (C) 2024 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;
18 
19 import static com.google.common.truth.TruthJUnit.assume;
20 
21 import static org.junit.Assume.assumeNoException;
22 
23 import android.platform.test.annotations.AsbSecurityTest;
24 
25 import com.android.sts.common.tradefed.testtype.NonRootSecurityTestCase;
26 import com.android.tradefed.device.ITestDevice;
27 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
28 import com.android.tradefed.testtype.junit4.DeviceTestRunOptions;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @RunWith(DeviceJUnit4ClassRunner.class)
34 public class CVE_2024_31332 extends NonRootSecurityTestCase {
35 
36     @Test
37     @AsbSecurityTest(cveBugId = 299931076)
testPocCVE_2024_31332()38     public void testPocCVE_2024_31332() {
39         try {
40             // Install test-app
41             installPackage("CVE-2024-31332.apk", "-t");
42 
43             // Set the 'PocDeviceAdminReceiver' as device-owner using device policy manager
44             final String testPkg = "android.security.cts.CVE_2024_31332";
45             try (AutoCloseable withPocDeviceAdminReceiverAsDeviceOwner =
46                     withPocDeviceAdminReceiverAsDeviceOwner(testPkg)) {
47                 // Run DeviceTest
48                 runDeviceTests(new DeviceTestRunOptions(testPkg).setDisableHiddenApiCheck(true));
49             }
50         } catch (Exception e) {
51             assumeNoException(e);
52         }
53     }
54 
withPocDeviceAdminReceiverAsDeviceOwner(String testPackage)55     private AutoCloseable withPocDeviceAdminReceiverAsDeviceOwner(String testPackage)
56             throws Exception {
57         // Set the 'PocDeviceAdminReceiver' as device-owner using device policy manager
58         final ITestDevice device = getDevice();
59         final int userId = device.getCurrentUser();
60         final String componentName = testPackage + "/.PocDeviceAdminReceiver";
61         assume().withMessage("Unable to set device owner")
62                 .that(device.setDeviceOwner(componentName, userId))
63                 .isTrue();
64 
65         // Return 'AutoCloseable' to remove the 'PocDeviceAdminReceiver' as device-owner
66         return () -> device.removeAdmin(componentName, userId);
67     }
68 }
69