From 0c1a37b7ad9ebe9554afaf5e39cd29e83c3a4276 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Thu, 18 Mar 2021 15:54:31 +0900 Subject: [PATCH] Expose isUidNetworkingBlocked and isUidRestrictedOnMeteredNetworks These methods are used by ConnectivityService for synchronous calls such as getActiveNetworkInfo, isActiveNetworkMetered, etc. These calls must call into NPMS and acquire the NPMS lock because they are synchronous. They cannot use the stale copy of the policy rules maintained by ConnectivityService, because if they did, races like the following could occur: 1. App gets broadcast/callback/.... 2. App calls isActiveNetworkMetered or other synchronous method. 3. ConnectivityService's copy of the rules is out of date, so the call returns stale information that the UID is still blocked. 4. The app thinks it has no networking, and does not call the synchronous method again until some other event occurs, potentially much later. Bug: 176289731 Test: passes existing tests in ConnectivityServiceTest Change-Id: I4ad0ca60431fe3702be85332530b6e93728d55e7 --- core/api/module-lib-current.txt | 2 ++ core/api/test-current.txt | 2 ++ core/java/android/net/NetworkPolicyManager.java | 6 ++---- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt index 42892a5f3ad99..a8f4ddc713f5e 100644 --- a/core/api/module-lib-current.txt +++ b/core/api/module-lib-current.txt @@ -196,6 +196,8 @@ package android.net { method @RequiresPermission(android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) public int getMultipathPreference(@NonNull android.net.Network); method @RequiresPermission(android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) public int getRestrictBackgroundStatus(int); method public static boolean isUidBlocked(int, boolean); + method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public boolean isUidNetworkingBlocked(int, boolean); + method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public boolean isUidRestrictedOnMeteredNetworks(int); method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public void registerNetworkPolicyCallback(@Nullable java.util.concurrent.Executor, @NonNull android.net.NetworkPolicyManager.NetworkPolicyCallback); method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public void unregisterNetworkPolicyCallback(@NonNull android.net.NetworkPolicyManager.NetworkPolicyCallback); field public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 262144; // 0x40000 diff --git a/core/api/test-current.txt b/core/api/test-current.txt index e486fa2c2e29f..efb413e029b32 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -1480,6 +1480,8 @@ package android.net { public class NetworkPolicyManager { method public boolean getRestrictBackground(); + method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public boolean isUidNetworkingBlocked(int, boolean); + method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public boolean isUidRestrictedOnMeteredNetworks(int); method @NonNull public static String resolveNetworkId(@NonNull android.net.wifi.WifiConfiguration); method public void setRestrictBackground(boolean); } diff --git a/core/java/android/net/NetworkPolicyManager.java b/core/java/android/net/NetworkPolicyManager.java index 78ad781158a27..3da1227f1bac3 100644 --- a/core/java/android/net/NetworkPolicyManager.java +++ b/core/java/android/net/NetworkPolicyManager.java @@ -630,9 +630,8 @@ public class NetworkPolicyManager { * @param meteredNetwork True if the network is metered. * @return true if networking is blocked for the given uid according to current networking * policies. - * - * @hide */ + @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public boolean isUidNetworkingBlocked(int uid, boolean meteredNetwork) { try { return mService.isUidNetworkingBlocked(uid, meteredNetwork); @@ -671,9 +670,8 @@ public class NetworkPolicyManager { * * @param uid The target uid. * @return true if the given uid is restricted from doing networking on metered networks. - * - * @hide */ + @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public boolean isUidRestrictedOnMeteredNetworks(int uid) { try { return mService.isUidRestrictedOnMeteredNetworks(uid);