Commit 43b695ff authored by Paul Jensen's avatar Paul Jensen Committed by Commit Bot

Disable internal DNS resolver when Android P using private DNS

Bug: 842456
Change-Id: I8f0c17cd50c75987c051e62344ac5e907e1b1133
Reviewed-on: https://chromium-review.googlesource.com/1065463Reviewed-by: default avatarMisha Efimov <mef@chromium.org>
Commit-Queue: Paul Jensen <pauljensen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#562584}
parent 5c777ff5
...@@ -23,6 +23,7 @@ import android.security.NetworkSecurityPolicy; ...@@ -23,6 +23,7 @@ import android.security.NetworkSecurityPolicy;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.Log; import android.util.Log;
import org.chromium.base.BuildInfo;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
...@@ -281,6 +282,10 @@ class AndroidNetworkLibrary { ...@@ -281,6 +282,10 @@ class AndroidNetworkLibrary {
} }
} }
/**
* Returns list of IP addresses of DNS servers.
* If private DNS is active, then returns a 1x1 array.
*/
@TargetApi(Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M)
@CalledByNative @CalledByNative
private static byte[][] getDnsServers() { private static byte[][] getDnsServers() {
...@@ -298,6 +303,19 @@ class AndroidNetworkLibrary { ...@@ -298,6 +303,19 @@ class AndroidNetworkLibrary {
if (linkProperties == null) { if (linkProperties == null) {
return new byte[0][0]; return new byte[0][0];
} }
if (BuildInfo.isAtLeastP()) {
// TODO(pauljensen): When Android P SDK is available, remove reflection.
try {
if (((Boolean) linkProperties.getClass()
.getMethod("isPrivateDnsActive")
.invoke(linkProperties))
.booleanValue()) {
return new byte[1][1];
}
} catch (Exception e) {
Log.e(TAG, "Can not call LinkProperties.isPrivateDnsActive():", e);
}
}
List<InetAddress> dnsServersList = linkProperties.getDnsServers(); List<InetAddress> dnsServersList = linkProperties.getDnsServers();
byte[][] dnsServers = new byte[dnsServersList.size()][]; byte[][] dnsServers = new byte[dnsServersList.size()][];
for (int i = 0; i < dnsServersList.size(); i++) { for (int i = 0; i < dnsServersList.size(); i++) {
......
...@@ -123,12 +123,17 @@ std::string GetWifiSSID() { ...@@ -123,12 +123,17 @@ std::string GetWifiSSID() {
base::android::AttachCurrentThread())); base::android::AttachCurrentThread()));
} }
void GetDnsServers(std::vector<IPEndPoint>* dns_servers) { internal::ConfigParsePosixResult GetDnsServers(
std::vector<IPEndPoint>* dns_servers) {
JNIEnv* env = AttachCurrentThread(); JNIEnv* env = AttachCurrentThread();
std::vector<std::string> dns_servers_strings; std::vector<std::string> dns_servers_strings;
base::android::JavaArrayOfByteArrayToStringVector( base::android::JavaArrayOfByteArrayToStringVector(
env, Java_AndroidNetworkLibrary_getDnsServers(env).obj(), env, Java_AndroidNetworkLibrary_getDnsServers(env).obj(),
&dns_servers_strings); &dns_servers_strings);
if (dns_servers_strings.size() == 0)
return internal::CONFIG_PARSE_POSIX_NO_NAMESERVERS;
if (dns_servers_strings.size() == 1 && dns_servers_strings[0].size() == 1)
return internal::CONFIG_PARSE_POSIX_PRIVATE_DNS_ACTIVE;
for (const std::string& dns_address_string : dns_servers_strings) { for (const std::string& dns_address_string : dns_servers_strings) {
IPAddress dns_address( IPAddress dns_address(
reinterpret_cast<const uint8_t*>(dns_address_string.c_str()), reinterpret_cast<const uint8_t*>(dns_address_string.c_str()),
...@@ -136,6 +141,7 @@ void GetDnsServers(std::vector<IPEndPoint>* dns_servers) { ...@@ -136,6 +141,7 @@ void GetDnsServers(std::vector<IPEndPoint>* dns_servers) {
IPEndPoint dns_server(dns_address, dns_protocol::kDefaultPort); IPEndPoint dns_server(dns_address, dns_protocol::kDefaultPort);
dns_servers->push_back(dns_server); dns_servers->push_back(dns_server);
} }
return internal::CONFIG_PARSE_POSIX_OK;
} }
void TagSocket(SocketDescriptor socket, uid_t uid, int32_t tag) { void TagSocket(SocketDescriptor socket, uid_t uid, int32_t tag) {
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "net/base/ip_endpoint.h" #include "net/base/ip_endpoint.h"
#include "net/base/mime_util.h" #include "net/base/mime_util.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
#include "net/dns/dns_config_service_posix.h"
#include "net/socket/socket_descriptor.h" #include "net/socket/socket_descriptor.h"
namespace net { namespace net {
...@@ -86,7 +87,11 @@ NET_EXPORT_PRIVATE std::string GetWifiSSID(); ...@@ -86,7 +87,11 @@ NET_EXPORT_PRIVATE std::string GetWifiSSID();
// Gets the DNS servers and puts them in |dns_servers|. // Gets the DNS servers and puts them in |dns_servers|.
// Only callable on Marshmallow and newer releases. // Only callable on Marshmallow and newer releases.
NET_EXPORT_PRIVATE void GetDnsServers(std::vector<IPEndPoint>* dns_servers); // Returns CONFIG_PARSE_POSIX_OK upon success,
// CONFIG_PARSE_POSIX_NO_NAMESERVERS if no DNS servers found, or
// CONFIG_PARSE_POSIX_PRIVATE_DNS_ACTIVE if private DNS active.
NET_EXPORT_PRIVATE internal::ConfigParsePosixResult GetDnsServers(
std::vector<IPEndPoint>* dns_servers);
// Apply TrafficStats tag |tag| and UID |uid| to |socket|. Future network // Apply TrafficStats tag |tag| and UID |uid| to |socket|. Future network
// traffic used by |socket| will be attributed to |uid| and |tag|. // traffic used by |socket| will be attributed to |uid| and |tag|.
......
...@@ -143,9 +143,9 @@ bool IsVpnPresent() { ...@@ -143,9 +143,9 @@ bool IsVpnPresent() {
ConfigParsePosixResult ReadDnsConfig(DnsConfig* dns_config) { ConfigParsePosixResult ReadDnsConfig(DnsConfig* dns_config) {
base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK); base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
dns_config->unhandled_options = false;
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
ConfigParsePosixResult result; ConfigParsePosixResult result;
dns_config->unhandled_options = false;
// TODO(fuchsia): Use res_ninit() when it's implemented on Fuchsia. // TODO(fuchsia): Use res_ninit() when it's implemented on Fuchsia.
#if defined(OS_OPENBSD) || defined(OS_FUCHSIA) #if defined(OS_OPENBSD) || defined(OS_FUCHSIA)
// Note: res_ninit in glibc always returns 0 and sets RES_INIT. // Note: res_ninit in glibc always returns 0 and sets RES_INIT.
...@@ -189,23 +189,11 @@ ConfigParsePosixResult ReadDnsConfig(DnsConfig* dns_config) { ...@@ -189,23 +189,11 @@ ConfigParsePosixResult ReadDnsConfig(DnsConfig* dns_config) {
dns_config->timeout = base::TimeDelta::FromMilliseconds(kDnsDefaultTimeoutMs); dns_config->timeout = base::TimeDelta::FromMilliseconds(kDnsDefaultTimeoutMs);
return result; return result;
#else // defined(OS_ANDROID) #else // defined(OS_ANDROID)
// Theoretically, this is bad. __system_property_get is not a supported API
// (but it's currently visible to anyone using Bionic), and the properties
// are implementation details that may disappear in future Android releases.
// Practically, libcutils provides property_get, which is a public API, and the
// DNS code (and its clients) are already robust against failing to get the DNS
// config for whatever reason, so the properties can disappear and the world
// won't end.
// TODO(juliatuttle): Depend on libcutils, then switch this (and other uses of
// __system_property_get) to property_get.
dns_config->nameservers.clear(); dns_config->nameservers.clear();
if (base::android::BuildInfo::GetInstance()->sdk_int() >= if (base::android::BuildInfo::GetInstance()->sdk_int() >=
base::android::SDK_VERSION_MARSHMALLOW) { base::android::SDK_VERSION_MARSHMALLOW) {
net::android::GetDnsServers(&dns_config->nameservers); return net::android::GetDnsServers(&dns_config->nameservers);
if (dns_config->nameservers.empty())
return CONFIG_PARSE_POSIX_NO_NAMESERVERS;
return CONFIG_PARSE_POSIX_OK;
} }
if (IsVpnPresent()) { if (IsVpnPresent()) {
...@@ -213,6 +201,9 @@ ConfigParsePosixResult ReadDnsConfig(DnsConfig* dns_config) { ...@@ -213,6 +201,9 @@ ConfigParsePosixResult ReadDnsConfig(DnsConfig* dns_config) {
return CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS; return CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS;
} }
// NOTE(pauljensen): __system_property_get and the net.dns1/2 properties are
// not supported APIs, but they're only read on pre-Marshmallow Android which
// was released years ago and isn't changing.
char property_value[PROP_VALUE_MAX]; char property_value[PROP_VALUE_MAX];
__system_property_get("net.dns1", property_value); __system_property_get("net.dns1", property_value);
std::string dns1_string = property_value; std::string dns1_string = property_value;
......
...@@ -63,6 +63,7 @@ enum ConfigParsePosixResult { ...@@ -63,6 +63,7 @@ enum ConfigParsePosixResult {
CONFIG_PARSE_POSIX_MISSING_OPTIONS, CONFIG_PARSE_POSIX_MISSING_OPTIONS,
CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS, CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS,
CONFIG_PARSE_POSIX_NO_DNSINFO, CONFIG_PARSE_POSIX_NO_DNSINFO,
CONFIG_PARSE_POSIX_PRIVATE_DNS_ACTIVE,
CONFIG_PARSE_POSIX_MAX // Bounding values for enumeration. CONFIG_PARSE_POSIX_MAX // Bounding values for enumeration.
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment