Loading services/core/java/com/android/server/security/advancedprotection/features/UsbDataAdvancedProtectionHook.java +43 −23 Original line number Diff line number Diff line Loading @@ -65,13 +65,13 @@ import android.content.pm.PackageManager; import com.android.server.LocalServices; import java.lang.Runnable; import android.security.advancedprotection.AdvancedProtectionFeature; import android.security.advancedprotection.AdvancedProtectionProtoEnums; import com.android.internal.R; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; import com.android.internal.util.FrameworkStatsLog; import com.android.internal.annotations.VisibleForTesting; import com.android.server.security.advancedprotection.AdvancedProtectionService; import java.net.URISyntaxException; Loading Loading @@ -152,15 +152,15 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { NOTIFICATION_DATA, R.string.usb_apm_usb_plugged_in_when_locked_replug_notification_text); private final Context mContext; // We use handlers for tasks that may need to be updated by broadcasts events. private final Handler mDelayedDisableHandler = new Handler(Looper.getMainLooper()); private final Handler mDelayedNotificationHandler = new Handler(Looper.getMainLooper()); private Handler mDelayedDisableHandler = new Handler(Looper.getMainLooper()); private Handler mDelayedNotificationHandler = new Handler(Looper.getMainLooper()); private AdvancedProtectionFeature mFeature = new AdvancedProtectionFeature(FEATURE_ID_DISALLOW_USB); private final Context mContext; private UsbManager mUsbManager; private IUsbManagerInternal mUsbManagerInternal; private BroadcastReceiver mUsbProtectionBroadcastReceiver; Loading Loading @@ -203,6 +203,31 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { onAdvancedProtectionChanged(enabled); } @VisibleForTesting public UsbDataAdvancedProtectionHook( Context context, AdvancedProtectionService advancedProtectionService, UsbManager usbManager, IUsbManagerInternal usbManagerInternal, KeyguardManager keyguardManager, NotificationManager notificationManager, Handler delayDisableHandler, Handler delayedNotificationHandler, boolean canSetUsbDataSignal, boolean afterFirstUnlock) { super(context, false); mContext = context; mAdvancedProtectionService = advancedProtectionService; mUsbManager = usbManager; mUsbManagerInternal = usbManagerInternal; mKeyguardManager = keyguardManager; mNotificationManager = notificationManager; mDelayedNotificationHandler = delayedNotificationHandler; mDelayedDisableHandler = delayDisableHandler; mCanSetUsbDataSignal = canSetUsbDataSignal; mIsAfterFirstUnlock = afterFirstUnlock; } @Override public AdvancedProtectionFeature getFeature() { return mFeature; Loading @@ -216,7 +241,8 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { Slog.d(TAG, "USB data protection is disabled through system property"); } return Flags.aapmFeatureUsbDataProtection() && (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST) || mContext.getPackageManager() && (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST) || mContext.getPackageManager() .hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)) && mAdvancedProtectionService.isUsbDataProtectionEnabled() && mCanSetUsbDataSignal Loading @@ -237,7 +263,9 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { if (!mBroadcastReceiverIsRegistered) { registerReceiver(); } if (mKeyguardManager.isKeyguardLocked()) { setUsbDataSignalIfPossible(false); } } else { if (mBroadcastReceiverIsRegistered) { unregisterReceiver(); Loading Loading @@ -274,7 +302,6 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { mDelayedDisableHandler.removeCallbacksAndMessages(null); cleanUpNotificationHandlerTasks(); setUsbDataSignalIfPossible(true); } else if (ACTION_SCREEN_OFF.equals(intent.getAction()) && mKeyguardManager.isKeyguardLocked()) { setUsbDataSignalIfPossible(false); Loading Loading @@ -359,7 +386,7 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { private void updateDelayedDisableTask(UsbPortStatus portStatus) { // For recovered intermittent/unreliable USB connections if (usbPortIsConnectedAndDataEnabled(portStatus)) { if (usbPortIsConnectedWithPotentialDataUse(portStatus)) { mDelayedDisableHandler.removeCallbacksAndMessages(null); } else if (!mDelayedDisableHandler.hasMessagesOrCallbacks()) { boolean taskPosted = Loading Loading @@ -407,13 +434,6 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { mDelayedNotificationHandler.removeCallbacksAndMessages(null); } private boolean usbPortIsConnectedAndDataEnabled(UsbPortStatus portStatus) { return portStatus != null && portStatus.isConnected() && portStatus.getUsbDataStatus() != UsbPortStatus.DATA_STATUS_DISABLED_FORCE; } // TODO:(b/401540215) Remove this as part of pre-release cleanup private void dumpUsbDevices(UsbPortStatus portStatus) { Map<String, UsbDevice> portStatusMap = mUsbManager.getDeviceList(); Loading Loading @@ -550,9 +570,10 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { } private boolean usbPortIsConnectedWithDataDisabled(UsbPortStatus portStatus) { return portStatus != null && portStatus.isConnected() && portStatus.getUsbDataStatus() == DATA_STATUS_DISABLED_FORCE; if (portStatus != null && portStatus.isConnected()) { return (portStatus.getUsbDataStatus() & UsbPortStatus.DATA_STATUS_DISABLED_FORCE) != 0; } return false; } private void setUsbDataSignalIfPossible(boolean status) { Loading Loading @@ -600,15 +621,14 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { ? "null" : usbPort.getStatus().toString()); } if (usbPortIsConnectedWithDataEnabled(usbPort)) { if (usbPortIsConnectedWithPotentialDataUse(usbPort.getStatus())) { return true; } } return false; } private boolean usbPortIsConnectedWithDataEnabled(UsbPort usbPort) { UsbPortStatus usbPortStatus = usbPort.getStatus(); private boolean usbPortIsConnectedWithPotentialDataUse(UsbPortStatus usbPortStatus) { return usbPortStatus != null && usbPortStatus.isConnected() && usbPortStatus.getCurrentDataRole() != DATA_ROLE_NONE; Loading services/tests/mockingservicestests/Android.bp +7 −0 Original line number Diff line number Diff line Loading @@ -411,3 +411,10 @@ test_module_config { test_suites: ["device-tests"], include_filters: ["com.android.server.BatteryServiceTest"], } test_module_config { name: "FrameworksMockingServicesTests_advanced_protection_mode", base: "FrameworksMockingServicesTests", test_suites: ["device-tests"], include_filters: ["com.android.server.security.advancedprotection.features.UsbDataAdvancedProtectionHookTest"], } services/tests/mockingservicestests/src/com/android/server/security/advancedprotection/features/OWNERS 0 → 100644 +1 −0 Original line number Diff line number Diff line per-file UsbDataAdvancedProtectionHookTest.java = georgechan@google.com, maunik@google.com No newline at end of file services/tests/mockingservicestests/src/com/android/server/security/advancedprotection/features/UsbDataAdvancedProtectionHookTest.java 0 → 100644 +781 −0 File added.Preview size limit exceeded, changes collapsed. Show changes Loading
services/core/java/com/android/server/security/advancedprotection/features/UsbDataAdvancedProtectionHook.java +43 −23 Original line number Diff line number Diff line Loading @@ -65,13 +65,13 @@ import android.content.pm.PackageManager; import com.android.server.LocalServices; import java.lang.Runnable; import android.security.advancedprotection.AdvancedProtectionFeature; import android.security.advancedprotection.AdvancedProtectionProtoEnums; import com.android.internal.R; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; import com.android.internal.util.FrameworkStatsLog; import com.android.internal.annotations.VisibleForTesting; import com.android.server.security.advancedprotection.AdvancedProtectionService; import java.net.URISyntaxException; Loading Loading @@ -152,15 +152,15 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { NOTIFICATION_DATA, R.string.usb_apm_usb_plugged_in_when_locked_replug_notification_text); private final Context mContext; // We use handlers for tasks that may need to be updated by broadcasts events. private final Handler mDelayedDisableHandler = new Handler(Looper.getMainLooper()); private final Handler mDelayedNotificationHandler = new Handler(Looper.getMainLooper()); private Handler mDelayedDisableHandler = new Handler(Looper.getMainLooper()); private Handler mDelayedNotificationHandler = new Handler(Looper.getMainLooper()); private AdvancedProtectionFeature mFeature = new AdvancedProtectionFeature(FEATURE_ID_DISALLOW_USB); private final Context mContext; private UsbManager mUsbManager; private IUsbManagerInternal mUsbManagerInternal; private BroadcastReceiver mUsbProtectionBroadcastReceiver; Loading Loading @@ -203,6 +203,31 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { onAdvancedProtectionChanged(enabled); } @VisibleForTesting public UsbDataAdvancedProtectionHook( Context context, AdvancedProtectionService advancedProtectionService, UsbManager usbManager, IUsbManagerInternal usbManagerInternal, KeyguardManager keyguardManager, NotificationManager notificationManager, Handler delayDisableHandler, Handler delayedNotificationHandler, boolean canSetUsbDataSignal, boolean afterFirstUnlock) { super(context, false); mContext = context; mAdvancedProtectionService = advancedProtectionService; mUsbManager = usbManager; mUsbManagerInternal = usbManagerInternal; mKeyguardManager = keyguardManager; mNotificationManager = notificationManager; mDelayedNotificationHandler = delayedNotificationHandler; mDelayedDisableHandler = delayDisableHandler; mCanSetUsbDataSignal = canSetUsbDataSignal; mIsAfterFirstUnlock = afterFirstUnlock; } @Override public AdvancedProtectionFeature getFeature() { return mFeature; Loading @@ -216,7 +241,8 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { Slog.d(TAG, "USB data protection is disabled through system property"); } return Flags.aapmFeatureUsbDataProtection() && (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST) || mContext.getPackageManager() && (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST) || mContext.getPackageManager() .hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)) && mAdvancedProtectionService.isUsbDataProtectionEnabled() && mCanSetUsbDataSignal Loading @@ -237,7 +263,9 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { if (!mBroadcastReceiverIsRegistered) { registerReceiver(); } if (mKeyguardManager.isKeyguardLocked()) { setUsbDataSignalIfPossible(false); } } else { if (mBroadcastReceiverIsRegistered) { unregisterReceiver(); Loading Loading @@ -274,7 +302,6 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { mDelayedDisableHandler.removeCallbacksAndMessages(null); cleanUpNotificationHandlerTasks(); setUsbDataSignalIfPossible(true); } else if (ACTION_SCREEN_OFF.equals(intent.getAction()) && mKeyguardManager.isKeyguardLocked()) { setUsbDataSignalIfPossible(false); Loading Loading @@ -359,7 +386,7 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { private void updateDelayedDisableTask(UsbPortStatus portStatus) { // For recovered intermittent/unreliable USB connections if (usbPortIsConnectedAndDataEnabled(portStatus)) { if (usbPortIsConnectedWithPotentialDataUse(portStatus)) { mDelayedDisableHandler.removeCallbacksAndMessages(null); } else if (!mDelayedDisableHandler.hasMessagesOrCallbacks()) { boolean taskPosted = Loading Loading @@ -407,13 +434,6 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { mDelayedNotificationHandler.removeCallbacksAndMessages(null); } private boolean usbPortIsConnectedAndDataEnabled(UsbPortStatus portStatus) { return portStatus != null && portStatus.isConnected() && portStatus.getUsbDataStatus() != UsbPortStatus.DATA_STATUS_DISABLED_FORCE; } // TODO:(b/401540215) Remove this as part of pre-release cleanup private void dumpUsbDevices(UsbPortStatus portStatus) { Map<String, UsbDevice> portStatusMap = mUsbManager.getDeviceList(); Loading Loading @@ -550,9 +570,10 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { } private boolean usbPortIsConnectedWithDataDisabled(UsbPortStatus portStatus) { return portStatus != null && portStatus.isConnected() && portStatus.getUsbDataStatus() == DATA_STATUS_DISABLED_FORCE; if (portStatus != null && portStatus.isConnected()) { return (portStatus.getUsbDataStatus() & UsbPortStatus.DATA_STATUS_DISABLED_FORCE) != 0; } return false; } private void setUsbDataSignalIfPossible(boolean status) { Loading Loading @@ -600,15 +621,14 @@ public class UsbDataAdvancedProtectionHook extends AdvancedProtectionHook { ? "null" : usbPort.getStatus().toString()); } if (usbPortIsConnectedWithDataEnabled(usbPort)) { if (usbPortIsConnectedWithPotentialDataUse(usbPort.getStatus())) { return true; } } return false; } private boolean usbPortIsConnectedWithDataEnabled(UsbPort usbPort) { UsbPortStatus usbPortStatus = usbPort.getStatus(); private boolean usbPortIsConnectedWithPotentialDataUse(UsbPortStatus usbPortStatus) { return usbPortStatus != null && usbPortStatus.isConnected() && usbPortStatus.getCurrentDataRole() != DATA_ROLE_NONE; Loading
services/tests/mockingservicestests/Android.bp +7 −0 Original line number Diff line number Diff line Loading @@ -411,3 +411,10 @@ test_module_config { test_suites: ["device-tests"], include_filters: ["com.android.server.BatteryServiceTest"], } test_module_config { name: "FrameworksMockingServicesTests_advanced_protection_mode", base: "FrameworksMockingServicesTests", test_suites: ["device-tests"], include_filters: ["com.android.server.security.advancedprotection.features.UsbDataAdvancedProtectionHookTest"], }
services/tests/mockingservicestests/src/com/android/server/security/advancedprotection/features/OWNERS 0 → 100644 +1 −0 Original line number Diff line number Diff line per-file UsbDataAdvancedProtectionHookTest.java = georgechan@google.com, maunik@google.com No newline at end of file
services/tests/mockingservicestests/src/com/android/server/security/advancedprotection/features/UsbDataAdvancedProtectionHookTest.java 0 → 100644 +781 −0 File added.Preview size limit exceeded, changes collapsed. Show changes