Merge "in NetworkStatsAccess.checkAccessLevel, check SYSTEM_UID ahead" into main

This commit is contained in:
Maciej Żenczykowski 2025-01-06 15:22:01 -08:00 • committed by Gerrit Code Review
commit 8ec9d71510

View file

@ -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;
}