From a58914b5485d2c55cb8e4ec48b08a6f61d263e8e Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Thu, 4 May 2023 11:01:33 +0800 Subject: [PATCH] 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 --- .../src/com/android/server/net/NetworkStatsService.java | 5 ++++- .../java/com/android/server/net/NetworkStatsServiceTest.java | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java index f977a2724c..e7ef510160 100644 --- a/service-t/src/com/android/server/net/NetworkStatsService.java +++ b/service-t/src/com/android/server/net/NetworkStatsService.java @@ -46,6 +46,7 @@ import static android.net.NetworkStats.TAG_NONE; import static android.net.NetworkStats.UID_ALL; import static android.net.NetworkStatsHistory.FIELD_ALL; 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.TrafficStats.KB_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 // client to track the user locations via querying data usage. Thus, enforce // 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 .checkCallersLocationPermission(callingPackage, null /* featureId */, diff --git a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java index 99f6d638ca..b8b0289b06 100644 --- a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java +++ b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java @@ -1926,12 +1926,17 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // Templates w/o wifi network keys can query stats as usual. assertNetworkTotal(sTemplateCarrierWifi1, 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) .checkCallersLocationPermission(any(), any(), anyInt(), anyBoolean(), any()); assertNetworkTotal(sTemplateCarrierWifi1, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateImsi1, 0L, 0L, 0L, 0L, 0); + assertNetworkTotal(templateTestIface1, 0L, 0L, 0L, 0L, 0); } /**