From 8fe5189801a138d3bdb1354df3606099b94b3c85 Mon Sep 17 00:00:00 2001 From: WeiZhang Date: Sun, 15 Dec 2024 13:59:11 -0600 Subject: [PATCH] in NetworkStatsAccess.checkAccessLevel, check SYSTEM_UID ahead to avoid possible deadlock from following DevicePolicyManager.isDeviceOwnerApp move SYSTEM_UID checking ahead. Change-Id: I182cadb175ca5028b6fe8e7f6316dcd349e32573 --- .../src/android/net/NetworkStatsAccess.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/framework-t/src/android/net/NetworkStatsAccess.java b/framework-t/src/android/net/NetworkStatsAccess.java index 7c9b3ecbf5..449588af00 100644 --- a/framework-t/src/android/net/NetworkStatsAccess.java +++ b/framework-t/src/android/net/NetworkStatsAccess.java @@ -111,6 +111,12 @@ public final class NetworkStatsAccess { /** Returns the {@link NetworkStatsAccess.Level} for the given caller. */ public static @NetworkStatsAccess.Level int checkAccessLevel( Context context, int callingPid, int callingUid, @Nullable String callingPackage) { + final int appId = UserHandle.getAppId(callingUid); + if (appId == Process.SYSTEM_UID) { + // the system can access data usage for all apps on the device. + // check system uid first, to avoid possible dead lock from other APIs + return NetworkStatsAccess.Level.DEVICE; + } final DevicePolicyManager mDpm = context.getSystemService(DevicePolicyManager.class); final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); @@ -126,16 +132,13 @@ public final class NetworkStatsAccess { Binder.restoreCallingIdentity(token); } - final int appId = UserHandle.getAppId(callingUid); - final boolean isNetworkStack = PermissionUtils.hasAnyPermissionOf( context, callingPid, callingUid, android.Manifest.permission.NETWORK_STACK, NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); - if (hasCarrierPrivileges || isDeviceOwner - || appId == Process.SYSTEM_UID || isNetworkStack) { - // Carrier-privileged apps and device owners, and the system (including the - // network stack) can access data usage for all apps on the device. + if (hasCarrierPrivileges || isDeviceOwner || isNetworkStack) { + // Carrier-privileged apps and device owners, and the network stack + // can access data usage for all apps on the device. return NetworkStatsAccess.Level.DEVICE; }