Advertisement
arter97

net.dns patch 1/2

May 19th, 2022
615
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java
  2. index 1ff6fc1ac..b8260346f 100644
  3. --- a/service/src/com/android/server/ConnectivityService.java
  4. +++ b/service/src/com/android/server/ConnectivityService.java
  5. @@ -1470,7 +1470,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
  6.                  mContext, mHandler, () -> updateAvoidBadWifi());
  7.          mMultinetworkPolicyTracker.start();
  8.  
  9. -        mDnsManager = new DnsManager(mContext, mDnsResolver);
  10. +        mDnsManager = new DnsManager(mContext, mDnsResolver, mSystemProperties);
  11.          registerPrivateDnsSettingsCallbacks();
  12.  
  13.          // This NAI is a sentinel used to offer no service to apps that are on a multi-layer
  14. @@ -7187,12 +7187,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
  15.              return;  // no updating necessary
  16.          }
  17.  
  18. +        final NetworkAgentInfo defaultNai = getDefaultNetwork();
  19. +        final boolean isDefaultNetwork = (defaultNai != null && defaultNai.network.netId == netId);
  20. +
  21.          if (DBG) {
  22.              final Collection<InetAddress> dnses = newLp.getDnsServers();
  23.              log("Setting DNS servers for network " + netId + " to " + dnses);
  24.          }
  25.          try {
  26.              mDnsManager.noteDnsServersForNetwork(netId, newLp);
  27. +            // TODO: netd should listen on [::1]:53 and proxy queries to the current
  28. +            // default network, and we should just set net.dns1 to ::1, not least
  29. +            // because applications attempting to use net.dns resolvers will bypass
  30. +            // the privacy protections of things like DNS-over-TLS.
  31. +            if (isDefaultNetwork) mDnsManager.setDefaultDnsSystemProperties(newLp.getDnsServers());
  32.              mDnsManager.flushVmDnsCache();
  33.          } catch (Exception e) {
  34.              loge("Exception in setDnsConfigurationForNetwork: " + e);
  35. diff --git a/service/src/com/android/server/connectivity/DnsManager.java b/service/src/com/android/server/connectivity/DnsManager.java
  36. index 1493cae79..2aea01199 100644
  37. --- a/service/src/com/android/server/connectivity/DnsManager.java
  38. +++ b/service/src/com/android/server/connectivity/DnsManager.java
  39. @@ -247,21 +247,24 @@ public class DnsManager {
  40.      private final Context mContext;
  41.      private final ContentResolver mContentResolver;
  42.      private final IDnsResolver mDnsResolver;
  43. +    private final MockableSystemProperties mSystemProperties;
  44.      private final ConcurrentHashMap<Integer, PrivateDnsConfig> mPrivateDnsMap;
  45.      // TODO: Replace the Map with SparseArrays.
  46.      private final Map<Integer, PrivateDnsValidationStatuses> mPrivateDnsValidationMap;
  47.      private final Map<Integer, LinkProperties> mLinkPropertiesMap;
  48.      private final Map<Integer, int[]> mTransportsMap;
  49.  
  50. +    private int mNumDnsEntries;
  51.      private int mSampleValidity;
  52.      private int mSuccessThreshold;
  53.      private int mMinSamples;
  54.      private int mMaxSamples;
  55.  
  56. -    public DnsManager(Context ctx, IDnsResolver dnsResolver) {
  57. +    public DnsManager(Context ctx, IDnsResolver dnsResolver, MockableSystemProperties sp) {
  58.          mContext = ctx;
  59.          mContentResolver = mContext.getContentResolver();
  60.          mDnsResolver = dnsResolver;
  61. +        mSystemProperties = sp;
  62.          mPrivateDnsMap = new ConcurrentHashMap<>();
  63.          mPrivateDnsValidationMap = new HashMap<>();
  64.          mLinkPropertiesMap = new HashMap<>();
  65. @@ -412,6 +415,18 @@ public class DnsManager {
  66.          }
  67.      }
  68.  
  69. +    public void setDefaultDnsSystemProperties(Collection<InetAddress> dnses) {
  70. +        int last = 0;
  71. +        for (InetAddress dns : dnses) {
  72. +            ++last;
  73. +            setNetDnsProperty(last, dns.getHostAddress());
  74. +        }
  75. +        for (int i = last + 1; i <= mNumDnsEntries; ++i) {
  76. +            setNetDnsProperty(i, "");
  77. +        }
  78. +        mNumDnsEntries = last;
  79. +    }
  80. +
  81.      /**
  82.       * Flush DNS caches and events work before boot has completed.
  83.       */
  84. @@ -467,6 +482,16 @@ public class DnsManager {
  85.          return Settings.Global.getInt(mContentResolver, which, dflt);
  86.      }
  87.  
  88. +    private void setNetDnsProperty(int which, String value) {
  89. +        final String key = "net.dns" + which;
  90. +        // Log and forget errors setting unsupported properties.
  91. +        try {
  92. +            mSystemProperties.set(key, value);
  93. +        } catch (Exception e) {
  94. +            Log.e(TAG, "Error setting unsupported net.dns property: ", e);
  95. +        }
  96. +    }
  97. +
  98.      /**
  99.       * Create a string array of host addresses from a collection of InetAddresses
  100.       *
  101. diff --git a/service/src/com/android/server/connectivity/MockableSystemProperties.java b/service/src/com/android/server/connectivity/MockableSystemProperties.java
  102. index a25b89ac0..77b86d8e4 100644
  103. --- a/service/src/com/android/server/connectivity/MockableSystemProperties.java
  104. +++ b/service/src/com/android/server/connectivity/MockableSystemProperties.java
  105. @@ -31,4 +31,8 @@ public class MockableSystemProperties {
  106.      public boolean getBoolean(String key, boolean def) {
  107.          return SystemProperties.getBoolean(key, def);
  108.      }
  109. +
  110. +    public void set(String key, String value) {
  111. +        SystemProperties.set(key, value);
  112. +    }
  113.  }
  114. diff --git a/tests/unit/java/com/android/server/connectivity/DnsManagerTest.java b/tests/unit/java/com/android/server/connectivity/DnsManagerTest.java
  115. index 24aecdb90..6ddef92b9 100644
  116. --- a/tests/unit/java/com/android/server/connectivity/DnsManagerTest.java
  117. +++ b/tests/unit/java/com/android/server/connectivity/DnsManagerTest.java
  118. @@ -105,6 +105,7 @@ public class DnsManagerTest {
  119.  
  120.      @Mock Context mCtx;
  121.      @Mock IDnsResolver mMockDnsResolver;
  122. +    @Mock MockableSystemProperties mSystemProperties;
  123.  
  124.      private void assertResolverOptionsEquals(
  125.              @Nullable ResolverOptionsParcel actual,
  126. @@ -149,7 +150,7 @@ public class DnsManagerTest {
  127.          mContentResolver.addProvider(Settings.AUTHORITY,
  128.                  new FakeSettingsProvider());
  129.          when(mCtx.getContentResolver()).thenReturn(mContentResolver);
  130. -        mDnsManager = new DnsManager(mCtx, mMockDnsResolver);
  131. +        mDnsManager = new DnsManager(mCtx, mMockDnsResolver, mSystemProperties);
  132.  
  133.          // Clear the private DNS settings
  134.          Settings.Global.putString(mContentResolver, PRIVATE_DNS_DEFAULT_MODE, "");
  135. @@ -171,6 +172,7 @@ public class DnsManagerTest {
  136.          // Send a validation event that is tracked on the alternate netId
  137.          mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES);
  138.          mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp);
  139. +        mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers());
  140.          mDnsManager.flushVmDnsCache();
  141.          mDnsManager.updateTransportsForNetwork(TEST_NETID_ALTERNATE, TEST_TRANSPORT_TYPES);
  142.          mDnsManager.noteDnsServersForNetwork(TEST_NETID_ALTERNATE, lp);
  143. @@ -207,6 +209,7 @@ public class DnsManagerTest {
  144.                      }));
  145.          mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES);
  146.          mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp);
  147. +        mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers());
  148.          mDnsManager.flushVmDnsCache();
  149.          fixedLp = new LinkProperties(lp);
  150.          mDnsManager.updatePrivateDnsStatus(TEST_NETID, fixedLp);
  151. @@ -244,6 +247,7 @@ public class DnsManagerTest {
  152.          lp.addDnsServer(InetAddress.getByName("3.3.3.3"));
  153.          mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES);
  154.          mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp);
  155. +        mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers());
  156.          mDnsManager.flushVmDnsCache();
  157.          mDnsManager.updatePrivateDnsValidation(
  158.                  new DnsManager.PrivateDnsValidationUpdate(TEST_NETID,
  159. @@ -258,6 +262,7 @@ public class DnsManagerTest {
  160.                  mDnsManager.getPrivateDnsConfig());
  161.          mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES);
  162.          mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp);
  163. +        mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers());
  164.          mDnsManager.flushVmDnsCache();
  165.          mDnsManager.updatePrivateDnsValidation(
  166.                  new DnsManager.PrivateDnsValidationUpdate(TEST_NETID_UNTRACKED,
  167. @@ -309,6 +314,7 @@ public class DnsManagerTest {
  168.                  mDnsManager.getPrivateDnsConfig());
  169.          mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES);
  170.          mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp);
  171. +        mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers());
  172.          mDnsManager.flushVmDnsCache();
  173.          mDnsManager.updatePrivateDnsValidation(
  174.                  new DnsManager.PrivateDnsValidationUpdate(TEST_NETID,
  175. @@ -354,6 +360,7 @@ public class DnsManagerTest {
  176.          lp.addDnsServer(InetAddress.getByName("4.4.4.4"));
  177.          mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES);
  178.          mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp);
  179. +        mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers());
  180.          mDnsManager.flushVmDnsCache();
  181.  
  182.          final ArgumentCaptor<ResolverParamsParcel> resolverParamsParcelCaptor =
  183.  
Advertisement
RAW Paste Data Copied
Advertisement