diff options
| author | 2022-11-05 22:33:17 -0700 | |
|---|---|---|
| committer | 2022-11-09 23:15:36 -0800 | |
| commit | 58bf68c2b23ac16daf8afbe37f23f3bdd9c8ff29 (patch) | |
| tree | a96b2f9248226cef6b6a826b6265c2c1da8b9362 | |
| parent | cef8c91bae32c83173c82ffb5bbe28fd79b16f95 (diff) | |
Add internal APIs to check if a given app has USB permission or not
Bug: 254662522
Bug: 246792057
Test: CtsAppFgsTestCases from follow-up CLs
Change-Id: I943a6df64ae0b58ffd25e745b68d11e18f00b16b
| -rw-r--r-- | core/java/android/hardware/usb/IUsbManager.aidl | 11 | ||||
| -rw-r--r-- | core/java/android/hardware/usb/UsbManager.java | 43 | ||||
| -rw-r--r-- | services/usb/java/com/android/server/usb/UsbService.java | 17 |
3 files changed, 71 insertions, 0 deletions
diff --git a/core/java/android/hardware/usb/IUsbManager.aidl b/core/java/android/hardware/usb/IUsbManager.aidl index 5a442445d832..51236fe3b2c0 100644 --- a/core/java/android/hardware/usb/IUsbManager.aidl +++ b/core/java/android/hardware/usb/IUsbManager.aidl @@ -79,9 +79,20 @@ interface IUsbManager /* Returns true if the caller has permission to access the device. */ boolean hasDevicePermission(in UsbDevice device, String packageName); + /* Returns true if the given package/pid/uid has permission to access the device. */ + @JavaPassthrough(annotation= + "@android.annotation.RequiresPermission(android.Manifest.permission.MANAGE_USB)") + boolean hasDevicePermissionWithIdentity(in UsbDevice device, String packageName, + int pid, int uid); + /* Returns true if the caller has permission to access the accessory. */ boolean hasAccessoryPermission(in UsbAccessory accessory); + /* Returns true if the given pid/uid has permission to access the accessory. */ + @JavaPassthrough(annotation= + "@android.annotation.RequiresPermission(android.Manifest.permission.MANAGE_USB)") + boolean hasAccessoryPermissionWithIdentity(in UsbAccessory accessory, int pid, int uid); + /* Requests permission for the given package to access the device. * Will display a system dialog to query the user if permission * had not already been given. diff --git a/core/java/android/hardware/usb/UsbManager.java b/core/java/android/hardware/usb/UsbManager.java index 2c38f7031eff..50dd0064a5cb 100644 --- a/core/java/android/hardware/usb/UsbManager.java +++ b/core/java/android/hardware/usb/UsbManager.java @@ -838,6 +838,28 @@ public class UsbManager { } /** + * Returns true if the caller has permission to access the device. It's similar to the + * {@link #hasPermission(UsbDevice)} but allows to specify a different package/uid/pid. + * + * <p>Not for third-party apps.</p> + * + * @hide + */ + @RequiresPermission(Manifest.permission.MANAGE_USB) + @RequiresFeature(PackageManager.FEATURE_USB_HOST) + public boolean hasPermission(@NonNull UsbDevice device, @NonNull String packageName, + int pid, int uid) { + if (mService == null) { + return false; + } + try { + return mService.hasDevicePermissionWithIdentity(device, packageName, pid, uid); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** * Returns true if the caller has permission to access the accessory. * Permission might have been granted temporarily via * {@link #requestPermission(UsbAccessory, PendingIntent)} or @@ -859,6 +881,27 @@ public class UsbManager { } /** + * Returns true if the caller has permission to access the accessory. It's similar to the + * {@link #hasPermission(UsbAccessory)} but allows to specify a different uid/pid. + * + * <p>Not for third-party apps.</p> + * + * @hide + */ + @RequiresPermission(Manifest.permission.MANAGE_USB) + @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY) + public boolean hasPermission(@NonNull UsbAccessory accessory, int pid, int uid) { + if (mService == null) { + return false; + } + try { + return mService.hasAccessoryPermissionWithIdentity(accessory, pid, uid); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** * Requests temporary permission for the given package to access the device. * This may result in a system dialog being displayed to the user * if permission had not already been granted. diff --git a/services/usb/java/com/android/server/usb/UsbService.java b/services/usb/java/com/android/server/usb/UsbService.java index 86f877fcd531..72f6cc3649a7 100644 --- a/services/usb/java/com/android/server/usb/UsbService.java +++ b/services/usb/java/com/android/server/usb/UsbService.java @@ -504,6 +504,15 @@ public class UsbService extends IUsbManager.Stub { } @Override + public boolean hasDevicePermissionWithIdentity(UsbDevice device, String packageName, + int pid, int uid) { + mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); + + final int userId = UserHandle.getUserId(uid); + return getPermissionsForUser(userId).hasPermission(device, packageName, pid, uid); + } + + @Override public boolean hasAccessoryPermission(UsbAccessory accessory) { final int uid = Binder.getCallingUid(); final int pid = Binder.getCallingPid(); @@ -518,6 +527,14 @@ public class UsbService extends IUsbManager.Stub { } @Override + public boolean hasAccessoryPermissionWithIdentity(UsbAccessory accessory, int pid, int uid) { + mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); + + final int userId = UserHandle.getUserId(uid); + return getPermissionsForUser(userId).hasPermission(accessory, pid, uid); + } + + @Override public void requestDevicePermission(UsbDevice device, String packageName, PendingIntent pi) { final int uid = Binder.getCallingUid(); final int pid = Binder.getCallingPid(); |