Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5ab82a2 authored by George Chan's avatar George Chan
Browse files

Migrated to AIDL enumeration from integer for UsbDataSignal disable reason

Test: Manual testing, atest UsbTests
Bug: 412433451
Flag: android.security.aapm_feature_usb_data_protection
Change-Id: Ie0c02ccafd1e0ba3293a4601f5659ee0220e3573
parent 0424f662
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -17,10 +17,12 @@
package android.hardware.usb;

import android.hardware.usb.IUsbOperationInternal;
import android.hardware.usb.InternalUsbDataSignalDisableReason;

/** @hide */
interface IUsbManagerInternal {

    /* Disable/enable USB data on a port for System Service callers. */
    boolean enableUsbDataSignal(boolean enable, int disableReason);
    boolean enableUsbDataSignal(boolean enable, InternalUsbDataSignalDisableReason disableReason);
}
+25 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.usb;

/** @hide */
@Backing(type="int")
enum InternalUsbDataSignalDisableReason {
    USB_DISABLE_REASON_LOCKDOWN_MODE = 0,
    USB_DISABLE_REASON_APM = 1,
    USB_DISABLE_REASON_ENTERPRISE = 2
}
+2 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.DATA_STATUS_DISABLED_FORCE;
import static android.hardware.usb.UsbPortStatus.COMPLIANCE_WARNING_BC_1_2;
import static android.hardware.usb.UsbPortStatus.COMPLIANCE_WARNING_INPUT_POWER_LIMITED;
import static android.hardware.usb.InternalUsbDataSignalDisableReason.USB_DISABLE_REASON_APM;

import android.app.KeyguardManager;
import android.app.Notification;
@@ -94,7 +95,6 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook {
    private static final String EXTRA_SILENCE_POWER_NOTIFICATION = "silence_power_notification";

    private static final int DELAY_DISABLE_MS = 3000;
    private static final int OS_USB_DISABLE_REASON_LOCKDOWN_MODE = 1;
    private static final int USB_DATA_CHANGE_MAX_RETRY_ATTEMPTS = 3;

    private final Context mContext;
@@ -488,7 +488,7 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook {
        while (usbChangeStateReattempts < USB_DATA_CHANGE_MAX_RETRY_ATTEMPTS) {
            try {
                if (mUsbManagerInternal.enableUsbDataSignal(
                        status, OS_USB_DISABLE_REASON_LOCKDOWN_MODE)) {
                        status, USB_DISABLE_REASON_APM)) {
                    break;
                } else {
                    Slog.e(TAG, "USB Data protection toggle attempt failed");
+14 −14
Original line number Diff line number Diff line
@@ -26,6 +26,9 @@ import static android.hardware.usb.UsbPortStatus.MODE_DUAL;
import static android.hardware.usb.UsbPortStatus.MODE_UFP;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
import static android.hardware.usb.InternalUsbDataSignalDisableReason.USB_DISABLE_REASON_LOCKDOWN_MODE;
import static android.hardware.usb.InternalUsbDataSignalDisableReason.USB_DISABLE_REASON_APM;
import static android.hardware.usb.InternalUsbDataSignalDisableReason.USB_DISABLE_REASON_ENTERPRISE;

import android.hardware.usb.IUsbManagerInternal;

@@ -89,6 +92,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import java.util.concurrent.atomic.AtomicInteger;
@@ -100,15 +104,6 @@ import java.util.concurrent.atomic.AtomicInteger;
 */
public class UsbService extends IUsbManager.Stub {

    public static final int OS_USB_DISABLE_REASON_AAPM = 0;
    public static final int OS_USB_DISABLE_REASON_LOCKDOWN_MODE = 1;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(value = {OS_USB_DISABLE_REASON_AAPM,
        OS_USB_DISABLE_REASON_LOCKDOWN_MODE})
    public @interface OsUsbDisableReason {
    }

    public static class Lifecycle extends SystemService {
        private UsbService mUsbService;
        private final CompletableFuture<Void> mOnStartFinished = new CompletableFuture<>();
@@ -1532,6 +1527,8 @@ public class UsbService extends IUsbManager.Stub {
     * that controls USB data behavior.
     */
    private class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
        private static final IUsbOperationInternal sDefaultOperation =
                new IUsbOperationInternal.Default();
        private boolean mLockdownModeStatus;

        StrongAuthTracker(Context context, Looper looper) {
@@ -1551,8 +1548,8 @@ public class UsbService extends IUsbManager.Stub {
            for (UsbPort port: mPortManager.getPorts()) {
                enableUsbDataInternal(port.getId(), !lockDownTriggeredByUser,
                    STRONG_AUTH_OPERATION_ID,
                    new IUsbOperationInternal.Default(),
                    OS_USB_DISABLE_REASON_LOCKDOWN_MODE,
                    sDefaultOperation,
                    USB_DISABLE_REASON_LOCKDOWN_MODE,
                    true);
            }
        }
@@ -1560,10 +1557,13 @@ public class UsbService extends IUsbManager.Stub {

    private class UsbManagerInternalImpl extends IUsbManagerInternal.Stub {
        private static final AtomicInteger sUsbOperationCount = new AtomicInteger();

        private static final Set<Integer> sValidDisableReasons = Set.of(USB_DISABLE_REASON_APM,
            USB_DISABLE_REASON_LOCKDOWN_MODE, USB_DISABLE_REASON_ENTERPRISE);
        @Override
        public boolean enableUsbDataSignal(boolean enable,
                @OsUsbDisableReason int disableReason) {
        public boolean enableUsbDataSignal(boolean enable, int disableReason) {
                if(!sValidDisableReasons.contains(disableReason)) {
                    throw new IllegalArgumentException("Invalid disable reason: " + disableReason);
                }
                boolean result = true;
                int operationId = sUsbOperationCount.incrementAndGet() + disableReason;
                for (UsbPort port : mPortManager.getPorts()) {
+10 −2
Original line number Diff line number Diff line
@@ -85,9 +85,11 @@ public class UsbServiceTest {

    private static final int TEST_SECOND_CALLER_ID = 2000;

    private static final int TEST_INTERNAL_REQUESTER_REASON_1 = 100;
    private static final int TEST_INTERNAL_REQUESTER_REASON_1 = 0;

    private static final int TEST_INTERNAL_REQUESTER_REASON_2 = 200;
    private static final int TEST_INTERNAL_REQUESTER_REASON_2 = 1;

    private static final int TEST_INTERNAL_REQUESTER_INVALID_REASON = 999;

    private UsbService mUsbService;

@@ -300,4 +302,10 @@ public class UsbServiceTest {
            return false;
        }
    }

    @Test(expected = IllegalArgumentException.class)
    public void usbManagerInternal_enableUsbDataSignal_throwsExceptionIfInvalidDisableReason()
        throws IllegalArgumentException, RemoteException {
        mIUsbManagerInternal.enableUsbDataSignal(true, TEST_INTERNAL_REQUESTER_INVALID_REASON);
    }
}