[RFPM03] Check permission by uid.

PermissionMonitor check permission by each package name but only
update the uid permission to netd. It's better to check
permission by uid directly. Thus, use
ActivityManager#checkUidPermission to check permissions for each
uid.

Bug: 132784544
Test: atest FrameworksNetTests
Change-Id: I057d34383b4dc408b2773e4c6b146774ba0c5eb2
This commit is contained in:
paulhu
2020-05-29 16:25:56 +08:00
parent c36f17d32e
commit 8adb39a688
2 changed files with 157 additions and 209 deletions

View File

@@ -21,14 +21,23 @@ import static android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS;
import static android.Manifest.permission.INTERNET; import static android.Manifest.permission.INTERNET;
import static android.Manifest.permission.NETWORK_STACK; import static android.Manifest.permission.NETWORK_STACK;
import static android.Manifest.permission.UPDATE_DEVICE_STATS; import static android.Manifest.permission.UPDATE_DEVICE_STATS;
import static android.content.pm.PackageInfo.REQUESTED_PERMISSION_GRANTED;
import static android.content.pm.PackageManager.GET_PERMISSIONS; import static android.content.pm.PackageManager.GET_PERMISSIONS;
import static android.content.pm.PackageManager.MATCH_ANY_USER; import static android.content.pm.PackageManager.MATCH_ANY_USER;
import static android.net.INetd.PERMISSION_INTERNET;
import static android.net.INetd.PERMISSION_NETWORK;
import static android.net.INetd.PERMISSION_NONE;
import static android.net.INetd.PERMISSION_SYSTEM;
import static android.net.INetd.PERMISSION_UNINSTALLED;
import static android.net.INetd.PERMISSION_UPDATE_DEVICE_STATS;
import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK; import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK;
import static android.os.Process.INVALID_UID; import static android.os.Process.INVALID_UID;
import static android.os.Process.SYSTEM_UID; import static android.os.Process.SYSTEM_UID;
import static com.android.internal.util.ArrayUtils.convertToIntArray;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
@@ -51,7 +60,6 @@ import android.util.SparseIntArray;
import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.IndentingPrintWriter;
import com.android.server.LocalServices; import com.android.server.LocalServices;
import com.android.server.SystemConfig; import com.android.server.SystemConfig;
@@ -65,7 +73,6 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
/** /**
* A utility class to inform Netd of UID permisisons. * A utility class to inform Netd of UID permisisons.
* Does a mass update at boot and then monitors for app install/remove. * Does a mass update at boot and then monitors for app install/remove.
@@ -114,6 +121,13 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
public int getDeviceFirstSdkInt() { public int getDeviceFirstSdkInt() {
return Build.VERSION.FIRST_SDK_INT; return Build.VERSION.FIRST_SDK_INT;
} }
/**
* Check whether given uid has specific permission.
*/
public int uidPermission(@NonNull final String permission, final int uid) {
return ActivityManager.checkUidPermission(permission, uid);
}
} }
public PermissionMonitor(@NonNull final Context context, @NonNull final INetd netd) { public PermissionMonitor(@NonNull final Context context, @NonNull final INetd netd) {
@@ -156,8 +170,9 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
} }
mAllApps.add(UserHandle.getAppId(uid)); mAllApps.add(UserHandle.getAppId(uid));
boolean isNetwork = hasNetworkPermission(app); final boolean isNetwork = hasPermission(CHANGE_NETWORK_STATE, uid);
boolean hasRestrictedPermission = hasRestrictedNetworkPermission(app); final boolean hasRestrictedPermission =
hasRestrictedNetworkPermission(app.applicationInfo);
if (isNetwork || hasRestrictedPermission) { if (isNetwork || hasRestrictedPermission) {
Boolean permission = mApps.get(uid); Boolean permission = mApps.get(uid);
@@ -169,8 +184,7 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
} }
//TODO: unify the management of the permissions into one codepath. //TODO: unify the management of the permissions into one codepath.
int otherNetdPerms = getNetdPermissionMask(app.requestedPermissions, final int otherNetdPerms = getNetdPermissionMask(uid);
app.requestedPermissionsFlags);
netdPermsUids.put(uid, netdPermsUids.get(uid) | otherNetdPerms); netdPermsUids.put(uid, netdPermsUids.get(uid) | otherNetdPerms);
} }
@@ -190,9 +204,8 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
// Get the uids of native services that have UPDATE_DEVICE_STATS or INTERNET permission. // Get the uids of native services that have UPDATE_DEVICE_STATS or INTERNET permission.
if (perms != null) { if (perms != null) {
netdPermission |= perms.contains(UPDATE_DEVICE_STATS) netdPermission |= perms.contains(UPDATE_DEVICE_STATS)
? INetd.PERMISSION_UPDATE_DEVICE_STATS : 0; ? PERMISSION_UPDATE_DEVICE_STATS : 0;
netdPermission |= perms.contains(INTERNET) netdPermission |= perms.contains(INTERNET) ? PERMISSION_INTERNET : 0;
? INetd.PERMISSION_INTERNET : 0;
} }
netdPermsUids.put(uid, netdPermsUids.get(uid) | netdPermission); netdPermsUids.put(uid, netdPermsUids.get(uid) | netdPermission);
} }
@@ -207,48 +220,33 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
} }
@VisibleForTesting @VisibleForTesting
boolean hasPermission(@NonNull final PackageInfo app, @NonNull final String permission) { boolean hasPermission(@NonNull final String permission, final int uid) {
if (app.requestedPermissions == null || app.requestedPermissionsFlags == null) { return mDeps.uidPermission(permission, uid) == PackageManager.PERMISSION_GRANTED;
return false;
}
final int index = ArrayUtils.indexOf(app.requestedPermissions, permission);
if (index < 0 || index >= app.requestedPermissionsFlags.length) return false;
return (app.requestedPermissionsFlags[index] & REQUESTED_PERMISSION_GRANTED) != 0;
} }
@VisibleForTesting @VisibleForTesting
boolean hasNetworkPermission(@NonNull final PackageInfo app) { boolean hasRestrictedNetworkPermission(@Nullable final ApplicationInfo appInfo) {
return hasPermission(app, CHANGE_NETWORK_STATE); if (appInfo == null) return false;
} // TODO : remove this check in the future(b/162295056). All apps should just
@VisibleForTesting
boolean hasRestrictedNetworkPermission(@NonNull final PackageInfo app) {
// TODO : remove this check in the future(b/31479477). All apps should just
// request the appropriate permission for their use case since android Q. // request the appropriate permission for their use case since android Q.
if (app.applicationInfo != null) { if ((appInfo.targetSdkVersion < VERSION_Q && isVendorApp(appInfo))
// Backward compatibility for b/114245686, on devices that launched before Q daemons // Backward compatibility for b/114245686, on devices that launched before Q daemons
// and apps running as the system UID are exempted from this check. // and apps running as the system UID are exempted from this check.
if (app.applicationInfo.uid == SYSTEM_UID && mDeps.getDeviceFirstSdkInt() < VERSION_Q) { || (appInfo.uid == SYSTEM_UID && mDeps.getDeviceFirstSdkInt() < VERSION_Q)) {
return true; return true;
}
if (app.applicationInfo.targetSdkVersion < VERSION_Q
&& isVendorApp(app.applicationInfo)) {
return true;
}
} }
return hasPermission(app, PERMISSION_MAINLINE_NETWORK_STACK) return hasPermission(PERMISSION_MAINLINE_NETWORK_STACK, appInfo.uid)
|| hasPermission(app, NETWORK_STACK) || hasPermission(NETWORK_STACK, appInfo.uid)
|| hasPermission(app, CONNECTIVITY_USE_RESTRICTED_NETWORKS); || hasPermission(CONNECTIVITY_USE_RESTRICTED_NETWORKS, appInfo.uid);
} }
/** Returns whether the given uid has using background network permission. */ /** Returns whether the given uid has using background network permission. */
public synchronized boolean hasUseBackgroundNetworksPermission(final int uid) { public synchronized boolean hasUseBackgroundNetworksPermission(final int uid) {
// Apps with any of the CHANGE_NETWORK_STATE, NETWORK_STACK, CONNECTIVITY_INTERNAL or // Apps with any of the CHANGE_NETWORK_STATE, NETWORK_STACK, CONNECTIVITY_INTERNAL or
// CONNECTIVITY_USE_RESTRICTED_NETWORKS permission has the permission to use background // CONNECTIVITY_USE_RESTRICTED_NETWORKS permission has the permission to use background
// networks. mApps contains the result of checks for both hasNetworkPermission and // networks. mApps contains the result of checks for both CHANGE_NETWORK_STATE permission
// hasRestrictedNetworkPermission. If uid is in the mApps list that means uid has one of // and hasRestrictedNetworkPermission. If uid is in the mApps list that means uid has one of
// permissions at least. // permissions at least.
return mApps.containsKey(uid); return mApps.containsKey(uid);
} }
@@ -273,11 +271,11 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
} }
try { try {
if (add) { if (add) {
mNetd.networkSetPermissionForUser(INetd.PERMISSION_NETWORK, toIntArray(network)); mNetd.networkSetPermissionForUser(PERMISSION_NETWORK, convertToIntArray(network));
mNetd.networkSetPermissionForUser(INetd.PERMISSION_SYSTEM, toIntArray(system)); mNetd.networkSetPermissionForUser(PERMISSION_SYSTEM, convertToIntArray(system));
} else { } else {
mNetd.networkClearPermissionForUser(toIntArray(network)); mNetd.networkClearPermissionForUser(convertToIntArray(network));
mNetd.networkClearPermissionForUser(toIntArray(system)); mNetd.networkClearPermissionForUser(convertToIntArray(system));
} }
} catch (RemoteException e) { } catch (RemoteException e) {
loge("Exception when updating permissions: " + e); loge("Exception when updating permissions: " + e);
@@ -323,14 +321,15 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
} }
@VisibleForTesting @VisibleForTesting
protected Boolean highestPermissionForUid(Boolean currentPermission, String name) { protected Boolean highestPermissionForUid(Boolean currentPermission, String name, int uid) {
if (currentPermission == SYSTEM) { if (currentPermission == SYSTEM) {
return currentPermission; return currentPermission;
} }
try { try {
final PackageInfo app = mPackageManager.getPackageInfo(name, GET_PERMISSIONS); final PackageInfo app = mPackageManager.getPackageInfo(name, GET_PERMISSIONS);
final boolean isNetwork = hasNetworkPermission(app); final boolean isNetwork = hasPermission(CHANGE_NETWORK_STATE, uid);
final boolean hasRestrictedPermission = hasRestrictedNetworkPermission(app); final boolean hasRestrictedPermission =
hasRestrictedNetworkPermission(app.applicationInfo);
if (isNetwork || hasRestrictedPermission) { if (isNetwork || hasRestrictedPermission) {
currentPermission = hasRestrictedPermission; currentPermission = hasRestrictedPermission;
} }
@@ -342,23 +341,14 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
} }
private int getPermissionForUid(final int uid) { private int getPermissionForUid(final int uid) {
int permission = INetd.PERMISSION_NONE;
// Check all the packages for this UID. The UID has the permission if any of the // Check all the packages for this UID. The UID has the permission if any of the
// packages in it has the permission. // packages in it has the permission.
final String[] packages = mPackageManager.getPackagesForUid(uid); final String[] packages = mPackageManager.getPackagesForUid(uid);
if (packages != null && packages.length > 0) { if (packages == null || packages.length <= 0) {
for (String name : packages) {
final PackageInfo app = getPackageInfo(name);
if (app != null && app.requestedPermissions != null) {
permission |= getNetdPermissionMask(app.requestedPermissions,
app.requestedPermissionsFlags);
}
}
} else {
// The last package of this uid is removed from device. Clean the package up. // The last package of this uid is removed from device. Clean the package up.
permission = INetd.PERMISSION_UNINSTALLED; return PERMISSION_UNINSTALLED;
} }
return permission; return getNetdPermissionMask(uid);
} }
/** /**
@@ -375,7 +365,7 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
// If multiple packages share a UID (cf: android:sharedUserId) and ask for different // If multiple packages share a UID (cf: android:sharedUserId) and ask for different
// permissions, don't downgrade (i.e., if it's already SYSTEM, leave it as is). // permissions, don't downgrade (i.e., if it's already SYSTEM, leave it as is).
final Boolean permission = highestPermissionForUid(mApps.get(uid), packageName); final Boolean permission = highestPermissionForUid(mApps.get(uid), packageName, uid);
if (permission != mApps.get(uid)) { if (permission != mApps.get(uid)) {
mApps.put(uid, permission); mApps.put(uid, permission);
@@ -431,7 +421,7 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
String[] packages = mPackageManager.getPackagesForUid(uid); String[] packages = mPackageManager.getPackagesForUid(uid);
if (packages != null && packages.length > 0) { if (packages != null && packages.length > 0) {
for (String name : packages) { for (String name : packages) {
permission = highestPermissionForUid(permission, name); permission = highestPermissionForUid(permission, name, uid);
if (permission == SYSTEM) { if (permission == SYSTEM) {
// An app with this UID still has the SYSTEM permission. // An app with this UID still has the SYSTEM permission.
// Therefore, this UID must already have the SYSTEM permission. // Therefore, this UID must already have the SYSTEM permission.
@@ -467,19 +457,13 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
sendPackagePermissionsForUid(uid, getPermissionForUid(uid)); sendPackagePermissionsForUid(uid, getPermissionForUid(uid));
} }
private static int getNetdPermissionMask(String[] requestedPermissions, private int getNetdPermissionMask(final int uid) {
int[] requestedPermissionsFlags) { int permissions = PERMISSION_NONE;
int permissions = 0; if (hasPermission(INTERNET, uid)) {
if (requestedPermissions == null || requestedPermissionsFlags == null) return permissions; permissions |= PERMISSION_INTERNET;
for (int i = 0; i < requestedPermissions.length; i++) { }
if (requestedPermissions[i].equals(INTERNET) if (hasPermission(UPDATE_DEVICE_STATS, uid)) {
&& ((requestedPermissionsFlags[i] & REQUESTED_PERMISSION_GRANTED) != 0)) { permissions |= PERMISSION_UPDATE_DEVICE_STATS;
permissions |= INetd.PERMISSION_INTERNET;
}
if (requestedPermissions[i].equals(UPDATE_DEVICE_STATS)
&& ((requestedPermissionsFlags[i] & REQUESTED_PERMISSION_GRANTED) != 0)) {
permissions |= INetd.PERMISSION_UPDATE_DEVICE_STATS;
}
} }
return permissions; return permissions;
} }
@@ -648,19 +632,19 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
for (int i = 0; i < netdPermissionsAppIds.size(); i++) { for (int i = 0; i < netdPermissionsAppIds.size(); i++) {
int permissions = netdPermissionsAppIds.valueAt(i); int permissions = netdPermissionsAppIds.valueAt(i);
switch(permissions) { switch(permissions) {
case (INetd.PERMISSION_INTERNET | INetd.PERMISSION_UPDATE_DEVICE_STATS): case (PERMISSION_INTERNET | PERMISSION_UPDATE_DEVICE_STATS):
allPermissionAppIds.add(netdPermissionsAppIds.keyAt(i)); allPermissionAppIds.add(netdPermissionsAppIds.keyAt(i));
break; break;
case INetd.PERMISSION_INTERNET: case PERMISSION_INTERNET:
internetPermissionAppIds.add(netdPermissionsAppIds.keyAt(i)); internetPermissionAppIds.add(netdPermissionsAppIds.keyAt(i));
break; break;
case INetd.PERMISSION_UPDATE_DEVICE_STATS: case PERMISSION_UPDATE_DEVICE_STATS:
updateStatsPermissionAppIds.add(netdPermissionsAppIds.keyAt(i)); updateStatsPermissionAppIds.add(netdPermissionsAppIds.keyAt(i));
break; break;
case INetd.PERMISSION_NONE: case PERMISSION_NONE:
noPermissionAppIds.add(netdPermissionsAppIds.keyAt(i)); noPermissionAppIds.add(netdPermissionsAppIds.keyAt(i));
break; break;
case INetd.PERMISSION_UNINSTALLED: case PERMISSION_UNINSTALLED:
uninstalledAppIds.add(netdPermissionsAppIds.keyAt(i)); uninstalledAppIds.add(netdPermissionsAppIds.keyAt(i));
default: default:
Log.e(TAG, "unknown permission type: " + permissions + "for uid: " Log.e(TAG, "unknown permission type: " + permissions + "for uid: "
@@ -671,24 +655,24 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
// TODO: add a lock inside netd to protect IPC trafficSetNetPermForUids() // TODO: add a lock inside netd to protect IPC trafficSetNetPermForUids()
if (allPermissionAppIds.size() != 0) { if (allPermissionAppIds.size() != 0) {
mNetd.trafficSetNetPermForUids( mNetd.trafficSetNetPermForUids(
INetd.PERMISSION_INTERNET | INetd.PERMISSION_UPDATE_DEVICE_STATS, PERMISSION_INTERNET | PERMISSION_UPDATE_DEVICE_STATS,
ArrayUtils.convertToIntArray(allPermissionAppIds)); convertToIntArray(allPermissionAppIds));
} }
if (internetPermissionAppIds.size() != 0) { if (internetPermissionAppIds.size() != 0) {
mNetd.trafficSetNetPermForUids(INetd.PERMISSION_INTERNET, mNetd.trafficSetNetPermForUids(PERMISSION_INTERNET,
ArrayUtils.convertToIntArray(internetPermissionAppIds)); convertToIntArray(internetPermissionAppIds));
} }
if (updateStatsPermissionAppIds.size() != 0) { if (updateStatsPermissionAppIds.size() != 0) {
mNetd.trafficSetNetPermForUids(INetd.PERMISSION_UPDATE_DEVICE_STATS, mNetd.trafficSetNetPermForUids(PERMISSION_UPDATE_DEVICE_STATS,
ArrayUtils.convertToIntArray(updateStatsPermissionAppIds)); convertToIntArray(updateStatsPermissionAppIds));
} }
if (noPermissionAppIds.size() != 0) { if (noPermissionAppIds.size() != 0) {
mNetd.trafficSetNetPermForUids(INetd.PERMISSION_NONE, mNetd.trafficSetNetPermForUids(PERMISSION_NONE,
ArrayUtils.convertToIntArray(noPermissionAppIds)); convertToIntArray(noPermissionAppIds));
} }
if (uninstalledAppIds.size() != 0) { if (uninstalledAppIds.size() != 0) {
mNetd.trafficSetNetPermForUids(INetd.PERMISSION_UNINSTALLED, mNetd.trafficSetNetPermForUids(PERMISSION_UNINSTALLED,
ArrayUtils.convertToIntArray(uninstalledAppIds)); convertToIntArray(uninstalledAppIds));
} }
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Pass appId list of special permission failed." + e); Log.e(TAG, "Pass appId list of special permission failed." + e);

View File

@@ -26,8 +26,6 @@ import static android.Manifest.permission.UPDATE_DEVICE_STATS;
import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_OEM; import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_OEM;
import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRODUCT; import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRODUCT;
import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_VENDOR; import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_VENDOR;
import static android.content.pm.PackageInfo.REQUESTED_PERMISSION_GRANTED;
import static android.content.pm.PackageInfo.REQUESTED_PERMISSION_REQUIRED;
import static android.content.pm.PackageManager.GET_PERMISSIONS; import static android.content.pm.PackageManager.GET_PERMISSIONS;
import static android.content.pm.PackageManager.MATCH_ANY_USER; import static android.content.pm.PackageManager.MATCH_ANY_USER;
import static android.os.Process.SYSTEM_UID; import static android.os.Process.SYSTEM_UID;
@@ -97,7 +95,6 @@ public class PermissionMonitorTest {
private static final int SYSTEM_UID1 = 1000; private static final int SYSTEM_UID1 = 1000;
private static final int SYSTEM_UID2 = 1008; private static final int SYSTEM_UID2 = 1008;
private static final int VPN_UID = 10002; private static final int VPN_UID = 10002;
private static final String REAL_SYSTEM_PACKAGE_NAME = "android";
private static final String MOCK_PACKAGE1 = "appName1"; private static final String MOCK_PACKAGE1 = "appName1";
private static final String MOCK_PACKAGE2 = "appName2"; private static final String MOCK_PACKAGE2 = "appName2";
private static final String SYSTEM_PACKAGE1 = "sysName1"; private static final String SYSTEM_PACKAGE1 = "sysName1";
@@ -128,6 +125,7 @@ public class PermissionMonitorTest {
new UserInfo(MOCK_USER1, "", 0), new UserInfo(MOCK_USER1, "", 0),
new UserInfo(MOCK_USER2, "", 0), new UserInfo(MOCK_USER2, "", 0),
})); }));
doReturn(PackageManager.PERMISSION_DENIED).when(mDeps).uidPermission(anyString(), anyInt());
mPermissionMonitor = spy(new PermissionMonitor(mContext, mNetdService, mDeps)); mPermissionMonitor = spy(new PermissionMonitor(mContext, mNetdService, mDeps));
@@ -140,35 +138,22 @@ public class PermissionMonitorTest {
verify(mMockPmi).getPackageList(mPermissionMonitor); verify(mMockPmi).getPackageList(mPermissionMonitor);
} }
/**
* Remove all permissions from the uid then build new package info and setup permissions to uid
* for checking restricted network permission.
*/
private boolean hasRestrictedNetworkPermission(String partition, int targetSdkVersion, int uid, private boolean hasRestrictedNetworkPermission(String partition, int targetSdkVersion, int uid,
String... permissions) { String... permissions) {
final PackageInfo packageInfo = final PackageInfo packageInfo = buildPackageInfo(partition, uid, MOCK_USER1);
packageInfoWithPermissions(REQUESTED_PERMISSION_GRANTED, permissions, partition);
packageInfo.applicationInfo.targetSdkVersion = targetSdkVersion; packageInfo.applicationInfo.targetSdkVersion = targetSdkVersion;
packageInfo.applicationInfo.uid = uid; removeAllPermissions(uid);
return mPermissionMonitor.hasRestrictedNetworkPermission(packageInfo); addPermissions(uid, permissions);
return mPermissionMonitor.hasRestrictedNetworkPermission(packageInfo.applicationInfo);
} }
private static PackageInfo systemPackageInfoWithPermissions(String... permissions) { private static PackageInfo packageInfoWithPartition(String partition) {
return packageInfoWithPermissions(
REQUESTED_PERMISSION_GRANTED, permissions, PARTITION_SYSTEM);
}
private static PackageInfo vendorPackageInfoWithPermissions(String... permissions) {
return packageInfoWithPermissions(
REQUESTED_PERMISSION_GRANTED, permissions, PARTITION_VENDOR);
}
private static PackageInfo packageInfoWithPermissions(int permissionsFlags,
String[] permissions, String partition) {
int[] requestedPermissionsFlags = new int[permissions.length];
for (int i = 0; i < permissions.length; i++) {
requestedPermissionsFlags[i] = permissionsFlags;
}
final PackageInfo packageInfo = new PackageInfo(); final PackageInfo packageInfo = new PackageInfo();
packageInfo.requestedPermissions = permissions;
packageInfo.applicationInfo = new ApplicationInfo(); packageInfo.applicationInfo = new ApplicationInfo();
packageInfo.requestedPermissionsFlags = requestedPermissionsFlags;
int privateFlags = 0; int privateFlags = 0;
switch (partition) { switch (partition) {
case PARTITION_OEM: case PARTITION_OEM:
@@ -185,84 +170,64 @@ public class PermissionMonitorTest {
return packageInfo; return packageInfo;
} }
private static PackageInfo buildPackageInfo(boolean hasSystemPermission, int uid, int userId) { private static PackageInfo buildPackageInfo(String partition, int uid, int userId) {
final PackageInfo pkgInfo; final PackageInfo pkgInfo = packageInfoWithPartition(partition);
if (hasSystemPermission) {
pkgInfo = systemPackageInfoWithPermissions(
CHANGE_NETWORK_STATE, NETWORK_STACK, CONNECTIVITY_USE_RESTRICTED_NETWORKS);
} else {
pkgInfo = packageInfoWithPermissions(REQUESTED_PERMISSION_GRANTED, new String[] {}, "");
}
pkgInfo.applicationInfo.uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); pkgInfo.applicationInfo.uid = UserHandle.getUid(userId, UserHandle.getAppId(uid));
return pkgInfo; return pkgInfo;
} }
/** This will REMOVE all previously set permissions from given uid. */
private void removeAllPermissions(int uid) {
doReturn(PackageManager.PERMISSION_DENIED).when(mDeps).uidPermission(anyString(), eq(uid));
}
/** Set up mocks so that given UID has the requested permissions. */
private void addPermissions(int uid, String... permissions) {
for (String permission : permissions) {
doReturn(PackageManager.PERMISSION_GRANTED)
.when(mDeps).uidPermission(eq(permission), eq(uid));
}
}
@Test @Test
public void testHasPermission() { public void testHasPermission() {
PackageInfo app = systemPackageInfoWithPermissions(); addPermissions(MOCK_UID1);
assertFalse(mPermissionMonitor.hasPermission(app, CHANGE_NETWORK_STATE)); assertFalse(mPermissionMonitor.hasPermission(CHANGE_NETWORK_STATE, MOCK_UID1));
assertFalse(mPermissionMonitor.hasPermission(app, NETWORK_STACK)); assertFalse(mPermissionMonitor.hasPermission(NETWORK_STACK, MOCK_UID1));
assertFalse(mPermissionMonitor.hasPermission(app, CONNECTIVITY_USE_RESTRICTED_NETWORKS)); assertFalse(mPermissionMonitor.hasPermission(
assertFalse(mPermissionMonitor.hasPermission(app, CONNECTIVITY_INTERNAL)); CONNECTIVITY_USE_RESTRICTED_NETWORKS, MOCK_UID1));
assertFalse(mPermissionMonitor.hasPermission(CONNECTIVITY_INTERNAL, MOCK_UID1));
app = systemPackageInfoWithPermissions(CHANGE_NETWORK_STATE, NETWORK_STACK); addPermissions(MOCK_UID1, CHANGE_NETWORK_STATE, NETWORK_STACK);
assertTrue(mPermissionMonitor.hasPermission(app, CHANGE_NETWORK_STATE)); assertTrue(mPermissionMonitor.hasPermission(CHANGE_NETWORK_STATE, MOCK_UID1));
assertTrue(mPermissionMonitor.hasPermission(app, NETWORK_STACK)); assertTrue(mPermissionMonitor.hasPermission(NETWORK_STACK, MOCK_UID1));
assertFalse(mPermissionMonitor.hasPermission(app, CONNECTIVITY_USE_RESTRICTED_NETWORKS)); assertFalse(mPermissionMonitor.hasPermission(
assertFalse(mPermissionMonitor.hasPermission(app, CONNECTIVITY_INTERNAL)); CONNECTIVITY_USE_RESTRICTED_NETWORKS, MOCK_UID1));
assertFalse(mPermissionMonitor.hasPermission(CONNECTIVITY_INTERNAL, MOCK_UID1));
assertFalse(mPermissionMonitor.hasPermission(CHANGE_NETWORK_STATE, MOCK_UID2));
assertFalse(mPermissionMonitor.hasPermission(NETWORK_STACK, MOCK_UID2));
app = systemPackageInfoWithPermissions( addPermissions(MOCK_UID2, CONNECTIVITY_USE_RESTRICTED_NETWORKS, CONNECTIVITY_INTERNAL);
CONNECTIVITY_USE_RESTRICTED_NETWORKS, CONNECTIVITY_INTERNAL); assertFalse(mPermissionMonitor.hasPermission(
assertFalse(mPermissionMonitor.hasPermission(app, CHANGE_NETWORK_STATE)); CONNECTIVITY_USE_RESTRICTED_NETWORKS, MOCK_UID1));
assertFalse(mPermissionMonitor.hasPermission(app, NETWORK_STACK)); assertFalse(mPermissionMonitor.hasPermission(CONNECTIVITY_INTERNAL, MOCK_UID1));
assertTrue(mPermissionMonitor.hasPermission(app, CONNECTIVITY_USE_RESTRICTED_NETWORKS)); assertTrue(mPermissionMonitor.hasPermission(
assertTrue(mPermissionMonitor.hasPermission(app, CONNECTIVITY_INTERNAL)); CONNECTIVITY_USE_RESTRICTED_NETWORKS, MOCK_UID2));
assertTrue(mPermissionMonitor.hasPermission(CONNECTIVITY_INTERNAL, MOCK_UID2));
app = packageInfoWithPermissions(REQUESTED_PERMISSION_REQUIRED, new String[] {
CONNECTIVITY_USE_RESTRICTED_NETWORKS, CONNECTIVITY_INTERNAL, NETWORK_STACK },
PARTITION_SYSTEM);
assertFalse(mPermissionMonitor.hasPermission(app, CHANGE_NETWORK_STATE));
assertFalse(mPermissionMonitor.hasPermission(app, NETWORK_STACK));
assertFalse(mPermissionMonitor.hasPermission(app, CONNECTIVITY_USE_RESTRICTED_NETWORKS));
assertFalse(mPermissionMonitor.hasPermission(app, CONNECTIVITY_INTERNAL));
app = systemPackageInfoWithPermissions(CHANGE_NETWORK_STATE);
app.requestedPermissions = null;
assertFalse(mPermissionMonitor.hasPermission(app, CHANGE_NETWORK_STATE));
app = systemPackageInfoWithPermissions(CHANGE_NETWORK_STATE);
app.requestedPermissionsFlags = null;
assertFalse(mPermissionMonitor.hasPermission(app, CHANGE_NETWORK_STATE));
} }
@Test @Test
public void testIsVendorApp() { public void testIsVendorApp() {
PackageInfo app = systemPackageInfoWithPermissions(); PackageInfo app = packageInfoWithPartition(PARTITION_SYSTEM);
assertFalse(mPermissionMonitor.isVendorApp(app.applicationInfo)); assertFalse(mPermissionMonitor.isVendorApp(app.applicationInfo));
app = packageInfoWithPermissions(REQUESTED_PERMISSION_GRANTED, app = packageInfoWithPartition(PARTITION_OEM);
new String[] {}, PARTITION_OEM);
assertTrue(mPermissionMonitor.isVendorApp(app.applicationInfo)); assertTrue(mPermissionMonitor.isVendorApp(app.applicationInfo));
app = packageInfoWithPermissions(REQUESTED_PERMISSION_GRANTED, app = packageInfoWithPartition(PARTITION_PRODUCT);
new String[] {}, PARTITION_PRODUCT);
assertTrue(mPermissionMonitor.isVendorApp(app.applicationInfo)); assertTrue(mPermissionMonitor.isVendorApp(app.applicationInfo));
app = vendorPackageInfoWithPermissions(); app = packageInfoWithPartition(PARTITION_VENDOR);
assertTrue(mPermissionMonitor.isVendorApp(app.applicationInfo)); assertTrue(mPermissionMonitor.isVendorApp(app.applicationInfo));
} }
@Test
public void testHasNetworkPermission() {
PackageInfo app = systemPackageInfoWithPermissions();
assertFalse(mPermissionMonitor.hasNetworkPermission(app));
app = systemPackageInfoWithPermissions(CHANGE_NETWORK_STATE);
assertTrue(mPermissionMonitor.hasNetworkPermission(app));
app = systemPackageInfoWithPermissions(NETWORK_STACK);
assertFalse(mPermissionMonitor.hasNetworkPermission(app));
app = systemPackageInfoWithPermissions(CONNECTIVITY_USE_RESTRICTED_NETWORKS);
assertFalse(mPermissionMonitor.hasNetworkPermission(app));
app = systemPackageInfoWithPermissions(CONNECTIVITY_INTERNAL);
assertFalse(mPermissionMonitor.hasNetworkPermission(app));
}
@Test @Test
public void testHasRestrictedNetworkPermission() { public void testHasRestrictedNetworkPermission() {
assertFalse(hasRestrictedNetworkPermission(PARTITION_SYSTEM, VERSION_P, MOCK_UID1)); assertFalse(hasRestrictedNetworkPermission(PARTITION_SYSTEM, VERSION_P, MOCK_UID1));
@@ -323,30 +288,27 @@ public class PermissionMonitorTest {
private void assertBackgroundPermission(boolean hasPermission, String name, int uid, private void assertBackgroundPermission(boolean hasPermission, String name, int uid,
String... permissions) throws Exception { String... permissions) throws Exception {
when(mPackageManager.getPackageInfo(eq(name), anyInt())) when(mPackageManager.getPackageInfo(eq(name), anyInt()))
.thenReturn(packageInfoWithPermissions( .thenReturn(buildPackageInfo(PARTITION_SYSTEM, uid, MOCK_USER1));
REQUESTED_PERMISSION_GRANTED, permissions, PARTITION_SYSTEM)); addPermissions(uid, permissions);
mPermissionMonitor.onPackageAdded(name, uid); mPermissionMonitor.onPackageAdded(name, uid);
assertEquals(hasPermission, mPermissionMonitor.hasUseBackgroundNetworksPermission(uid)); assertEquals(hasPermission, mPermissionMonitor.hasUseBackgroundNetworksPermission(uid));
} }
@Test @Test
public void testHasUseBackgroundNetworksPermission() throws Exception { public void testHasUseBackgroundNetworksPermission() throws Exception {
doReturn(VERSION_Q).when(mDeps).getDeviceFirstSdkInt();
assertFalse(mPermissionMonitor.hasUseBackgroundNetworksPermission(SYSTEM_UID)); assertFalse(mPermissionMonitor.hasUseBackgroundNetworksPermission(SYSTEM_UID));
assertBackgroundPermission(false, SYSTEM_PACKAGE1, SYSTEM_UID); assertBackgroundPermission(false, "system1", SYSTEM_UID);
assertBackgroundPermission(false, SYSTEM_PACKAGE1, SYSTEM_UID, CONNECTIVITY_INTERNAL); assertBackgroundPermission(false, "system2", SYSTEM_UID, CONNECTIVITY_INTERNAL);
assertBackgroundPermission(true, SYSTEM_PACKAGE1, SYSTEM_UID, CHANGE_NETWORK_STATE); assertBackgroundPermission(true, "system3", SYSTEM_UID, CHANGE_NETWORK_STATE);
assertBackgroundPermission(true, SYSTEM_PACKAGE1, SYSTEM_UID, NETWORK_STACK);
assertFalse(mPermissionMonitor.hasUseBackgroundNetworksPermission(MOCK_UID1)); assertFalse(mPermissionMonitor.hasUseBackgroundNetworksPermission(MOCK_UID1));
assertBackgroundPermission(false, MOCK_PACKAGE1, MOCK_UID1); assertBackgroundPermission(false, "mock1", MOCK_UID1);
assertBackgroundPermission(true, MOCK_PACKAGE1, MOCK_UID1, assertBackgroundPermission(true, "mock2", MOCK_UID1, CONNECTIVITY_USE_RESTRICTED_NETWORKS);
CONNECTIVITY_USE_RESTRICTED_NETWORKS);
assertFalse(mPermissionMonitor.hasUseBackgroundNetworksPermission(MOCK_UID2)); assertFalse(mPermissionMonitor.hasUseBackgroundNetworksPermission(MOCK_UID2));
assertBackgroundPermission(false, MOCK_PACKAGE2, MOCK_UID2); assertBackgroundPermission(false, "mock3", MOCK_UID2, CONNECTIVITY_INTERNAL);
assertBackgroundPermission(false, MOCK_PACKAGE2, MOCK_UID2, assertBackgroundPermission(true, "mock4", MOCK_UID2, NETWORK_STACK);
CONNECTIVITY_INTERNAL);
assertBackgroundPermission(true, MOCK_PACKAGE2, MOCK_UID2, NETWORK_STACK);
} }
private class NetdMonitor { private class NetdMonitor {
@@ -416,13 +378,14 @@ public class PermissionMonitorTest {
// MOCK_UID1: MOCK_PACKAGE1 only has network permission. // MOCK_UID1: MOCK_PACKAGE1 only has network permission.
// SYSTEM_UID: SYSTEM_PACKAGE1 has system permission. // SYSTEM_UID: SYSTEM_PACKAGE1 has system permission.
// SYSTEM_UID: SYSTEM_PACKAGE2 only has network permission. // SYSTEM_UID: SYSTEM_PACKAGE2 only has network permission.
doReturn(SYSTEM).when(mPermissionMonitor).highestPermissionForUid(eq(SYSTEM), anyString()); doReturn(SYSTEM).when(mPermissionMonitor).highestPermissionForUid(eq(SYSTEM),
anyString(), anyInt());
doReturn(SYSTEM).when(mPermissionMonitor).highestPermissionForUid(any(), doReturn(SYSTEM).when(mPermissionMonitor).highestPermissionForUid(any(),
eq(SYSTEM_PACKAGE1)); eq(SYSTEM_PACKAGE1), anyInt());
doReturn(NETWORK).when(mPermissionMonitor).highestPermissionForUid(any(), doReturn(NETWORK).when(mPermissionMonitor).highestPermissionForUid(any(),
eq(SYSTEM_PACKAGE2)); eq(SYSTEM_PACKAGE2), anyInt());
doReturn(NETWORK).when(mPermissionMonitor).highestPermissionForUid(any(), doReturn(NETWORK).when(mPermissionMonitor).highestPermissionForUid(any(),
eq(MOCK_PACKAGE1)); eq(MOCK_PACKAGE1), anyInt());
// Add SYSTEM_PACKAGE2, expect only have network permission. // Add SYSTEM_PACKAGE2, expect only have network permission.
mPermissionMonitor.onUserAdded(MOCK_USER1); mPermissionMonitor.onUserAdded(MOCK_USER1);
@@ -473,13 +436,15 @@ public class PermissionMonitorTest {
public void testUidFilteringDuringVpnConnectDisconnectAndUidUpdates() throws Exception { public void testUidFilteringDuringVpnConnectDisconnectAndUidUpdates() throws Exception {
when(mPackageManager.getInstalledPackages(eq(GET_PERMISSIONS | MATCH_ANY_USER))).thenReturn( when(mPackageManager.getInstalledPackages(eq(GET_PERMISSIONS | MATCH_ANY_USER))).thenReturn(
Arrays.asList(new PackageInfo[] { Arrays.asList(new PackageInfo[] {
buildPackageInfo(/* SYSTEM */ true, SYSTEM_UID1, MOCK_USER1), buildPackageInfo(PARTITION_SYSTEM, SYSTEM_UID1, MOCK_USER1),
buildPackageInfo(/* SYSTEM */ false, MOCK_UID1, MOCK_USER1), buildPackageInfo(PARTITION_SYSTEM, MOCK_UID1, MOCK_USER1),
buildPackageInfo(/* SYSTEM */ false, MOCK_UID2, MOCK_USER1), buildPackageInfo(PARTITION_SYSTEM, MOCK_UID2, MOCK_USER1),
buildPackageInfo(/* SYSTEM */ false, VPN_UID, MOCK_USER1) buildPackageInfo(PARTITION_SYSTEM, VPN_UID, MOCK_USER1)
})); }));
when(mPackageManager.getPackageInfo(eq(MOCK_PACKAGE1), eq(GET_PERMISSIONS))).thenReturn( when(mPackageManager.getPackageInfo(eq(MOCK_PACKAGE1), eq(GET_PERMISSIONS))).thenReturn(
buildPackageInfo(false, MOCK_UID1, MOCK_USER1)); buildPackageInfo(PARTITION_SYSTEM, MOCK_UID1, MOCK_USER1));
addPermissions(SYSTEM_UID,
CHANGE_NETWORK_STATE, NETWORK_STACK, CONNECTIVITY_USE_RESTRICTED_NETWORKS);
mPermissionMonitor.startMonitoring(); mPermissionMonitor.startMonitoring();
// Every app on user 0 except MOCK_UID2 are under VPN. // Every app on user 0 except MOCK_UID2 are under VPN.
final Set<UidRange> vpnRange1 = new HashSet<>(Arrays.asList(new UidRange[] { final Set<UidRange> vpnRange1 = new HashSet<>(Arrays.asList(new UidRange[] {
@@ -524,11 +489,11 @@ public class PermissionMonitorTest {
public void testUidFilteringDuringPackageInstallAndUninstall() throws Exception { public void testUidFilteringDuringPackageInstallAndUninstall() throws Exception {
when(mPackageManager.getInstalledPackages(eq(GET_PERMISSIONS | MATCH_ANY_USER))).thenReturn( when(mPackageManager.getInstalledPackages(eq(GET_PERMISSIONS | MATCH_ANY_USER))).thenReturn(
Arrays.asList(new PackageInfo[] { Arrays.asList(new PackageInfo[] {
buildPackageInfo(true, SYSTEM_UID1, MOCK_USER1), buildPackageInfo(PARTITION_SYSTEM, SYSTEM_UID1, MOCK_USER1),
buildPackageInfo(false, VPN_UID, MOCK_USER1) buildPackageInfo(PARTITION_SYSTEM, VPN_UID, MOCK_USER1)
})); }));
when(mPackageManager.getPackageInfo(eq(MOCK_PACKAGE1), eq(GET_PERMISSIONS))).thenReturn( when(mPackageManager.getPackageInfo(eq(MOCK_PACKAGE1), eq(GET_PERMISSIONS))).thenReturn(
buildPackageInfo(false, MOCK_UID1, MOCK_USER1)); buildPackageInfo(PARTITION_SYSTEM, MOCK_UID1, MOCK_USER1));
mPermissionMonitor.startMonitoring(); mPermissionMonitor.startMonitoring();
final Set<UidRange> vpnRange = Collections.singleton(UidRange.createForUser(MOCK_USER1)); final Set<UidRange> vpnRange = Collections.singleton(UidRange.createForUser(MOCK_USER1));
@@ -633,10 +598,10 @@ public class PermissionMonitorTest {
private PackageInfo setPackagePermissions(String packageName, int uid, String[] permissions) private PackageInfo setPackagePermissions(String packageName, int uid, String[] permissions)
throws Exception { throws Exception {
PackageInfo packageInfo = packageInfoWithPermissions( final PackageInfo packageInfo = buildPackageInfo(PARTITION_SYSTEM, uid, MOCK_USER1);
REQUESTED_PERMISSION_GRANTED, permissions, PARTITION_SYSTEM);
when(mPackageManager.getPackageInfo(eq(packageName), anyInt())).thenReturn(packageInfo); when(mPackageManager.getPackageInfo(eq(packageName), anyInt())).thenReturn(packageInfo);
when(mPackageManager.getPackagesForUid(eq(uid))).thenReturn(new String[]{packageName}); when(mPackageManager.getPackagesForUid(eq(uid))).thenReturn(new String[]{packageName});
addPermissions(uid, permissions);
return packageInfo; return packageInfo;
} }
@@ -663,14 +628,13 @@ public class PermissionMonitorTest {
public void testPackageInstallSharedUid() throws Exception { public void testPackageInstallSharedUid() throws Exception {
final NetdServiceMonitor mNetdServiceMonitor = new NetdServiceMonitor(mNetdService); final NetdServiceMonitor mNetdServiceMonitor = new NetdServiceMonitor(mNetdService);
PackageInfo packageInfo1 = addPackage(MOCK_PACKAGE1, MOCK_UID1, addPackage(MOCK_PACKAGE1, MOCK_UID1, new String[] {INTERNET, UPDATE_DEVICE_STATS});
new String[] {INTERNET, UPDATE_DEVICE_STATS});
mNetdServiceMonitor.expectPermission(INetd.PERMISSION_INTERNET mNetdServiceMonitor.expectPermission(INetd.PERMISSION_INTERNET
| INetd.PERMISSION_UPDATE_DEVICE_STATS, new int[]{MOCK_UID1}); | INetd.PERMISSION_UPDATE_DEVICE_STATS, new int[]{MOCK_UID1});
// Install another package with the same uid and no permissions should not cause the UID to // Install another package with the same uid and no permissions should not cause the UID to
// lose permissions. // lose permissions.
PackageInfo packageInfo2 = systemPackageInfoWithPermissions(); final PackageInfo packageInfo2 = buildPackageInfo(PARTITION_SYSTEM, MOCK_UID1, MOCK_USER1);
when(mPackageManager.getPackageInfo(eq(MOCK_PACKAGE2), anyInt())).thenReturn(packageInfo2); when(mPackageManager.getPackageInfo(eq(MOCK_PACKAGE2), anyInt())).thenReturn(packageInfo2);
when(mPackageManager.getPackagesForUid(MOCK_UID1)) when(mPackageManager.getPackagesForUid(MOCK_UID1))
.thenReturn(new String[]{MOCK_PACKAGE1, MOCK_PACKAGE2}); .thenReturn(new String[]{MOCK_PACKAGE1, MOCK_PACKAGE2});
@@ -701,6 +665,7 @@ public class PermissionMonitorTest {
| INetd.PERMISSION_UPDATE_DEVICE_STATS, new int[]{MOCK_UID1}); | INetd.PERMISSION_UPDATE_DEVICE_STATS, new int[]{MOCK_UID1});
when(mPackageManager.getPackagesForUid(MOCK_UID1)).thenReturn(new String[]{}); when(mPackageManager.getPackagesForUid(MOCK_UID1)).thenReturn(new String[]{});
removeAllPermissions(MOCK_UID1);
mPermissionMonitor.onPackageRemoved(MOCK_PACKAGE1, MOCK_UID1); mPermissionMonitor.onPackageRemoved(MOCK_PACKAGE1, MOCK_UID1);
mNetdServiceMonitor.expectPermission(INetd.PERMISSION_UNINSTALLED, new int[]{MOCK_UID1}); mNetdServiceMonitor.expectPermission(INetd.PERMISSION_UNINSTALLED, new int[]{MOCK_UID1});
@@ -728,10 +693,12 @@ public class PermissionMonitorTest {
| INetd.PERMISSION_UPDATE_DEVICE_STATS, new int[]{MOCK_UID1}); | INetd.PERMISSION_UPDATE_DEVICE_STATS, new int[]{MOCK_UID1});
// Mock another package with the same uid but different permissions. // Mock another package with the same uid but different permissions.
PackageInfo packageInfo2 = systemPackageInfoWithPermissions(INTERNET); final PackageInfo packageInfo2 = buildPackageInfo(PARTITION_SYSTEM, MOCK_UID1, MOCK_USER1);
when(mPackageManager.getPackageInfo(eq(MOCK_PACKAGE2), anyInt())).thenReturn(packageInfo2); when(mPackageManager.getPackageInfo(eq(MOCK_PACKAGE2), anyInt())).thenReturn(packageInfo2);
when(mPackageManager.getPackagesForUid(MOCK_UID1)).thenReturn(new String[]{ when(mPackageManager.getPackagesForUid(MOCK_UID1)).thenReturn(new String[]{
MOCK_PACKAGE2}); MOCK_PACKAGE2});
removeAllPermissions(MOCK_UID1);
addPermissions(MOCK_UID1, INTERNET);
mPermissionMonitor.onPackageRemoved(MOCK_PACKAGE1, MOCK_UID1); mPermissionMonitor.onPackageRemoved(MOCK_PACKAGE1, MOCK_UID1);
mNetdServiceMonitor.expectPermission(INetd.PERMISSION_INTERNET, new int[]{MOCK_UID1}); mNetdServiceMonitor.expectPermission(INetd.PERMISSION_INTERNET, new int[]{MOCK_UID1});
@@ -743,9 +710,6 @@ public class PermissionMonitorTest {
// necessary permission. // necessary permission.
final Context realContext = InstrumentationRegistry.getContext(); final Context realContext = InstrumentationRegistry.getContext();
final PermissionMonitor monitor = new PermissionMonitor(realContext, mNetdService); final PermissionMonitor monitor = new PermissionMonitor(realContext, mNetdService);
final PackageManager manager = realContext.getPackageManager(); assertTrue(monitor.hasPermission(CONNECTIVITY_USE_RESTRICTED_NETWORKS, SYSTEM_UID));
final PackageInfo systemInfo = manager.getPackageInfo(REAL_SYSTEM_PACKAGE_NAME,
GET_PERMISSIONS | MATCH_ANY_USER);
assertTrue(monitor.hasPermission(systemInfo, CONNECTIVITY_USE_RESTRICTED_NETWORKS));
} }
} }