summaryrefslogtreecommitdiffstats
path: root/net/dns/dns_config_service.cc
blob: a520d261b9dbe6ac2f53a5e786ff766eeb55634f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/dns/dns_config_service.h"

#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/values.h"
#include "net/base/ip_endpoint.h"

namespace net {

namespace {

// Check if particular nameserver address is rogue. See:
// http://www.fbi.gov/news/stories/2011/november/malware_110911/DNS-changer-malware.pdf
bool CheckRogueDnsAddress(const IPAddressNumber& address) {
  const struct Bounds {
    const unsigned char lower[4];  // inclusive
    const unsigned char upper[4];  // exclusive
  } cases[] = {
    { { '\x55', '\xFF', '\x70', '\x00' },    // 85.255.112.0
      { '\x55', '\xFF', '\x80', '\x00' } },  // 85.255.128.0
    { { '\x43', '\xD2', '\x00', '\x00' },    // 67.210.0.0
      { '\x43', '\xD2', '\x10', '\x00' } },  // 67.210.16.0
    { { '\x5D', '\xBC', '\xA0', '\x00' },    // 93.188.160.0
      { '\x5D', '\xBC', '\xA8', '\x00' } },  // 93.188.168.0
    { { '\x4D', '\x43', '\x53', '\x00' },    // 77.67.83.0
      { '\x4D', '\x43', '\x54', '\x00' } },  // 77.67.84.0
    { { '\x40', '\x1C', '\xB2', '\x00' },    // 64.28.178.0
      { '\x40', '\x1C', '\xC0', '\x00' } },  // 64.28.192.0
  };
  for (unsigned i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
    const Bounds& bounds = cases[i];
    IPAddressNumber lower(bounds.lower, bounds.lower + 4);
    IPAddressNumber upper(bounds.upper, bounds.upper + 4);
    if (address.size() == kIPv6AddressSize) {
      lower = ConvertIPv4NumberToIPv6Number(lower);
      upper = ConvertIPv4NumberToIPv6Number(upper);
    }
    if ((lower <= address) && (address < upper))
      return true;
  }
  return false;
}

void CheckRogueDnsConfig(const DnsConfig& config) {
  for (size_t i = 0; i < config.nameservers.size(); ++i) {
    if (CheckRogueDnsAddress(config.nameservers[i].address())) {
      UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DNSChangerDetected", true);
      return;
    }
  }
  UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DNSChangerDetected", false);
}

}  // namespace

// Default values are taken from glibc resolv.h.
DnsConfig::DnsConfig()
    : append_to_multi_label_name(true),
      ndots(1),
      timeout(base::TimeDelta::FromSeconds(5)),
      attempts(2),
      rotate(false),
      edns0(false) {}

DnsConfig::~DnsConfig() {}

bool DnsConfig::Equals(const DnsConfig& d) const {
  return EqualsIgnoreHosts(d) && (hosts == d.hosts);
}

bool DnsConfig::EqualsIgnoreHosts(const DnsConfig& d) const {
  return (nameservers == d.nameservers) &&
         (search == d.search) &&
         (append_to_multi_label_name == d.append_to_multi_label_name) &&
         (ndots == d.ndots) &&
         (timeout == d.timeout) &&
         (attempts == d.attempts) &&
         (rotate == d.rotate) &&
         (edns0 == d.edns0);
}

void DnsConfig::CopyIgnoreHosts(const DnsConfig& d) {
  nameservers = d.nameservers;
  search = d.search;
  append_to_multi_label_name = d.append_to_multi_label_name;
  ndots = d.ndots;
  timeout = d.timeout;
  attempts = d.attempts;
  rotate = d.rotate;
  edns0 = d.edns0;
}

base::Value* DnsConfig::ToValue() const {
  DictionaryValue* dict = new DictionaryValue();

  ListValue* list = new ListValue();
  for (size_t i = 0; i < nameservers.size(); ++i)
    list->Append(Value::CreateStringValue(nameservers[i].ToString()));
  dict->Set("nameservers", list);

  list = new ListValue();
  for (size_t i = 0; i < search.size(); ++i)
    list->Append(Value::CreateStringValue(search[i]));
  dict->Set("search", list);

  dict->SetBoolean("append_to_multi_label_name", append_to_multi_label_name);
  dict->SetInteger("ndots", ndots);
  dict->SetDouble("timeout", timeout.InSecondsF());
  dict->SetInteger("attempts", attempts);
  dict->SetBoolean("rotate", rotate);
  dict->SetBoolean("edns0", edns0);
  dict->SetInteger("num_hosts", hosts.size());

  return dict;
}


DnsConfigService::DnsConfigService()
    : checked_rogue_dns_(false),
      have_config_(false),
      have_hosts_(false),
      need_update_(false),
      last_sent_empty_(true) {}

DnsConfigService::~DnsConfigService() {
  // Must always clean up.
  NetworkChangeNotifier::RemoveDNSObserver(this);
}

void DnsConfigService::Read(const CallbackType& callback) {
  DCHECK(CalledOnValidThread());
  DCHECK(!callback.is_null());
  DCHECK(callback_.is_null());
  callback_ = callback;
  OnDNSChanged(NetworkChangeNotifier::CHANGE_DNS_WATCH_STARTED);
}

void DnsConfigService::Watch(const CallbackType& callback) {
  DCHECK(CalledOnValidThread());
  DCHECK(!callback.is_null());
  DCHECK(callback_.is_null());
  NetworkChangeNotifier::AddDNSObserver(this);
  callback_ = callback;
  if (NetworkChangeNotifier::IsWatchingDNS())
    OnDNSChanged(NetworkChangeNotifier::CHANGE_DNS_WATCH_STARTED);
  // else: Wait until signal before reading.
}

void DnsConfigService::InvalidateConfig() {
  DCHECK(CalledOnValidThread());
  base::TimeTicks now = base::TimeTicks::Now();
  if (!last_invalidate_config_time_.is_null()) {
    UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.ConfigNotifyInterval",
                             now - last_invalidate_config_time_);
  }
  last_invalidate_config_time_ = now;
  if (!have_config_)
    return;
  have_config_ = false;
  StartTimer();
}

void DnsConfigService::InvalidateHosts() {
  DCHECK(CalledOnValidThread());
  base::TimeTicks now = base::TimeTicks::Now();
  if (!last_invalidate_hosts_time_.is_null()) {
    UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.HostsNotifyInterval",
                             now - last_invalidate_hosts_time_);
  }
  last_invalidate_hosts_time_ = now;
  if (!have_hosts_)
    return;
  have_hosts_ = false;
  StartTimer();
}

void DnsConfigService::OnConfigRead(const DnsConfig& config) {
  DCHECK(CalledOnValidThread());
  DCHECK(config.IsValid());

  bool changed = false;
  if (!config.EqualsIgnoreHosts(dns_config_)) {
    dns_config_.CopyIgnoreHosts(config);
    need_update_ = true;
    changed = true;
  }
  if (!changed && !last_sent_empty_time_.is_null()) {
    UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedConfigInterval",
                             base::TimeTicks::Now() - last_sent_empty_time_);
  }
  UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigChange", changed);

  have_config_ = true;
  if (have_hosts_)
    OnCompleteConfig();
}

void DnsConfigService::OnHostsRead(const DnsHosts& hosts) {
  DCHECK(CalledOnValidThread());

  bool changed = false;
  if (hosts != dns_config_.hosts) {
    dns_config_.hosts = hosts;
    need_update_ = true;
    changed = true;
  }
  if (!changed && !last_sent_empty_time_.is_null()) {
    UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedHostsInterval",
                             base::TimeTicks::Now() - last_sent_empty_time_);
  }
  UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostsChange", changed);

  have_hosts_ = true;
  if (have_config_)
    OnCompleteConfig();
}

void DnsConfigService::StartTimer() {
  DCHECK(CalledOnValidThread());
  if (last_sent_empty_) {
    DCHECK(!timer_.IsRunning());
    return;  // No need to withdraw again.
  }
  timer_.Stop();

  // Give it a short timeout to come up with a valid config. Otherwise withdraw
  // the config from the receiver. The goal is to avoid perceivable network
  // outage (when using the wrong config) but at the same time avoid
  // unnecessary Job aborts in HostResolverImpl. The signals come from multiple
  // sources so it might receive multiple events during a config change.

  // DHCP and user-induced changes are on the order of seconds, so 100ms should
  // not add perceivable delay. On the other hand, config readers should finish
  // within 100ms with the rare exception of I/O block or extra large HOSTS.
  const base::TimeDelta kTimeout = base::TimeDelta::FromMilliseconds(100);

  timer_.Start(FROM_HERE,
               kTimeout,
               this,
               &DnsConfigService::OnTimeout);
}

void DnsConfigService::OnTimeout() {
  DCHECK(CalledOnValidThread());
  DCHECK(!last_sent_empty_);
  // Indicate that even if there is no change in On*Read, we will need to
  // update the receiver when the config becomes complete.
  need_update_ = true;
  // Empty config is considered invalid.
  last_sent_empty_ = true;
  last_sent_empty_time_ = base::TimeTicks::Now();
  callback_.Run(DnsConfig());
}

void DnsConfigService::OnCompleteConfig() {
  timer_.Stop();
  if (!need_update_)
    return;
  if (!checked_rogue_dns_ && dns_config_.IsValid()) {
    CheckRogueDnsConfig(dns_config_);
    checked_rogue_dns_ = true;
  }
  need_update_ = false;
  last_sent_empty_ = false;
  callback_.Run(dns_config_);
}

}  // namespace net