Fix template for test network needs fine location permission

For a template with MATCH_TEST, since the wifi network key is
just a placeholder to identify a specific test network, it is
not related to track user location. Hence the check is not
really needed.

Test: 1. atest NetworkStatsIntegrationTest with use location turned off
      2. atest FrameworksNetTests:android.net.connectivity.com.android.server.net.NetworkStatsServiceTest
Fix: 280686048
Change-Id: Ic128cfbbb2d03c427c9030c99bf205eb65a33e58
This commit is contained in:
Junyu Lai
2023-05-04 11:01:33 +08:00
parent 69b6ca4ff1
commit a58914b548
2 changed files with 9 additions and 1 deletions

View File

@@ -46,6 +46,7 @@ import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL; import static android.net.NetworkStats.UID_ALL;
import static android.net.NetworkStatsHistory.FIELD_ALL; import static android.net.NetworkStatsHistory.FIELD_ALL;
import static android.net.NetworkTemplate.MATCH_MOBILE; import static android.net.NetworkTemplate.MATCH_MOBILE;
import static android.net.NetworkTemplate.MATCH_TEST;
import static android.net.NetworkTemplate.MATCH_WIFI; import static android.net.NetworkTemplate.MATCH_WIFI;
import static android.net.TrafficStats.KB_IN_BYTES; import static android.net.TrafficStats.KB_IN_BYTES;
import static android.net.TrafficStats.MB_IN_BYTES; import static android.net.TrafficStats.MB_IN_BYTES;
@@ -1582,7 +1583,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// For a template with wifi network keys, it is possible for a malicious // For a template with wifi network keys, it is possible for a malicious
// client to track the user locations via querying data usage. Thus, enforce // client to track the user locations via querying data usage. Thus, enforce
// fine location permission check. // fine location permission check.
if (!template.getWifiNetworkKeys().isEmpty()) { // For a template with MATCH_TEST, since the wifi network key is just a placeholder
// to identify a specific test network, it is not related to track user location.
if (!template.getWifiNetworkKeys().isEmpty() && template.getMatchRule() != MATCH_TEST) {
final boolean canAccessFineLocation = mLocationPermissionChecker final boolean canAccessFineLocation = mLocationPermissionChecker
.checkCallersLocationPermission(callingPackage, .checkCallersLocationPermission(callingPackage,
null /* featureId */, null /* featureId */,

View File

@@ -1926,12 +1926,17 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
// Templates w/o wifi network keys can query stats as usual. // Templates w/o wifi network keys can query stats as usual.
assertNetworkTotal(sTemplateCarrierWifi1, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateCarrierWifi1, 0L, 0L, 0L, 0L, 0);
assertNetworkTotal(sTemplateImsi1, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateImsi1, 0L, 0L, 0L, 0L, 0);
// Templates for test network does not need to enforce location permission.
final NetworkTemplate templateTestIface1 = new NetworkTemplate.Builder(MATCH_TEST)
.setWifiNetworkKeys(Set.of(TEST_IFACE)).build();
assertNetworkTotal(templateTestIface1, 0L, 0L, 0L, 0L, 0);
doReturn(true).when(mLocationPermissionChecker) doReturn(true).when(mLocationPermissionChecker)
.checkCallersLocationPermission(any(), any(), anyInt(), anyBoolean(), any()); .checkCallersLocationPermission(any(), any(), anyInt(), anyBoolean(), any());
assertNetworkTotal(sTemplateCarrierWifi1, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateCarrierWifi1, 0L, 0L, 0L, 0L, 0);
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
assertNetworkTotal(sTemplateImsi1, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateImsi1, 0L, 0L, 0L, 0L, 0);
assertNetworkTotal(templateTestIface1, 0L, 0L, 0L, 0L, 0);
} }
/** /**