Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0c1a37b7 authored by Lorenzo Colitti's avatar Lorenzo Colitti Committed by Remi NGUYEN VAN
Browse files

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
parent 87eb49b6
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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
+2 −0
Original line number Diff line number Diff line
@@ -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);
  }
+2 −4
Original line number Diff line number Diff line
@@ -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);