File: dns_config_service_linux.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (530 lines) | stat: -rw-r--r-- 18,920 bytes parent folder | download | duplicates (9)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "net/dns/dns_config_service_linux.h"

#include <netdb.h>
#include <netinet/in.h>
#include <resolv.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <map>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/files/file_path.h"
#include "base/files/file_path_watcher.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/sequence_checker.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/time/time.h"
#include "net/base/ip_endpoint.h"
#include "net/dns/dns_config.h"
#include "net/dns/nsswitch_reader.h"
#include "net/dns/public/resolv_reader.h"
#include "net/dns/serial_worker.h"

namespace net {

namespace internal {

namespace {

const base::FilePath::CharType kFilePathHosts[] =
    FILE_PATH_LITERAL("/etc/hosts");

#ifndef _PATH_RESCONF  // Normally defined in <resolv.h>
#define _PATH_RESCONF FILE_PATH_LITERAL("/etc/resolv.conf")
#endif

constexpr base::FilePath::CharType kFilePathResolv[] = _PATH_RESCONF;

#ifndef _PATH_NSSWITCH_CONF  // Normally defined in <netdb.h>
#define _PATH_NSSWITCH_CONF FILE_PATH_LITERAL("/etc/nsswitch.conf")
#endif

constexpr base::FilePath::CharType kFilePathNsswitch[] = _PATH_NSSWITCH_CONF;

std::optional<DnsConfig> ConvertResStateToDnsConfig(
    const struct __res_state& res) {
  std::optional<std::vector<net::IPEndPoint>> nameservers = GetNameservers(res);
  DnsConfig dns_config;
  dns_config.unhandled_options = false;

  if (!nameservers.has_value())
    return std::nullopt;

  // Expected to be validated by GetNameservers()
  DCHECK(res.options & RES_INIT);

  dns_config.nameservers = std::move(nameservers.value());
  dns_config.search.clear();
  for (int i = 0; (i < MAXDNSRCH) && res.dnsrch[i]; ++i) {
    dns_config.search.emplace_back(res.dnsrch[i]);
  }

  dns_config.ndots = res.ndots;
  dns_config.fallback_period = base::Seconds(res.retrans);
  dns_config.attempts = res.retry;
#if defined(RES_ROTATE)
  dns_config.rotate = res.options & RES_ROTATE;
#endif
#if !defined(RES_USE_DNSSEC)
  // Some versions of libresolv don't have support for the DO bit. In this
  // case, we proceed without it.
  static const int RES_USE_DNSSEC = 0;
#endif

  // The current implementation assumes these options are set. They normally
  // cannot be overwritten by /etc/resolv.conf
  const unsigned kRequiredOptions = RES_RECURSE | RES_DEFNAMES | RES_DNSRCH;
  if ((res.options & kRequiredOptions) != kRequiredOptions) {
    dns_config.unhandled_options = true;
    return dns_config;
  }

  const unsigned kUnhandledOptions = RES_USEVC | RES_IGNTC | RES_USE_DNSSEC;
  if (res.options & kUnhandledOptions) {
    dns_config.unhandled_options = true;
    return dns_config;
  }

  if (dns_config.nameservers.empty())
    return std::nullopt;

  // If any name server is 0.0.0.0, assume the configuration is invalid.
  for (const IPEndPoint& nameserver : dns_config.nameservers) {
    if (nameserver.address().IsZero())
      return std::nullopt;
  }
  return dns_config;
}

// Helper to add the effective result of `action` to `in_out_parsed_behavior`.
// Returns false if `action` results in inconsistent behavior (setting an action
// for a status that already has a different action).
bool SetActionBehavior(const NsswitchReader::ServiceAction& action,
                       std::map<NsswitchReader::Status, NsswitchReader::Action>&
                           in_out_parsed_behavior) {
  if (action.negated) {
    for (NsswitchReader::Status status :
         {NsswitchReader::Status::kSuccess, NsswitchReader::Status::kNotFound,
          NsswitchReader::Status::kUnavailable,
          NsswitchReader::Status::kTryAgain}) {
      if (status != action.status) {
        NsswitchReader::ServiceAction effective_action = {
            /*negated=*/false, status, action.action};
        if (!SetActionBehavior(effective_action, in_out_parsed_behavior))
          return false;
      }
    }
  } else {
    if (in_out_parsed_behavior.count(action.status) >= 1 &&
        in_out_parsed_behavior[action.status] != action.action) {
      return false;
    }
    in_out_parsed_behavior[action.status] = action.action;
  }

  return true;
}

// Helper to determine if `actions` match `expected_actions`, meaning `actions`
// contains no unknown statuses or actions and for every expectation set in
// `expected_actions`, the expected action matches the effective result from
// `actions`.
bool AreActionsCompatible(
    const std::vector<NsswitchReader::ServiceAction>& actions,
    const std::map<NsswitchReader::Status, NsswitchReader::Action>
        expected_actions) {
  std::map<NsswitchReader::Status, NsswitchReader::Action> parsed_behavior;

  for (const NsswitchReader::ServiceAction& action : actions) {
    if (action.status == NsswitchReader::Status::kUnknown ||
        action.action == NsswitchReader::Action::kUnknown) {
      return false;
    }

    if (!SetActionBehavior(action, parsed_behavior))
      return false;
  }

  // Default behavior if not configured.
  if (parsed_behavior.count(NsswitchReader::Status::kSuccess) == 0)
    parsed_behavior[NsswitchReader::Status::kSuccess] =
        NsswitchReader::Action::kReturn;
  if (parsed_behavior.count(NsswitchReader::Status::kNotFound) == 0)
    parsed_behavior[NsswitchReader::Status::kNotFound] =
        NsswitchReader::Action::kContinue;
  if (parsed_behavior.count(NsswitchReader::Status::kUnavailable) == 0)
    parsed_behavior[NsswitchReader::Status::kUnavailable] =
        NsswitchReader::Action::kContinue;
  if (parsed_behavior.count(NsswitchReader::Status::kTryAgain) == 0)
    parsed_behavior[NsswitchReader::Status::kTryAgain] =
        NsswitchReader::Action::kContinue;

  for (const std::pair<const NsswitchReader::Status, NsswitchReader::Action>&
           expected : expected_actions) {
    if (parsed_behavior[expected.first] != expected.second)
      return false;
  }

  return true;
}

// These values are emitted in metrics. Entries should not be renumbered and
// numeric values should never be reused. (See NsswitchIncompatibleReason in
// tools/metrics/histograms/enums.xml.)
enum class IncompatibleNsswitchReason {
  kFilesMissing = 0,
  kMultipleFiles = 1,
  kBadFilesActions = 2,
  kDnsMissing = 3,
  kBadDnsActions = 4,
  kBadMdnsMinimalActions = 5,
  kBadOtherServiceActions = 6,
  kUnknownService = 7,
  kIncompatibleService = 8,
  kMaxValue = kIncompatibleService
};

void RecordIncompatibleNsswitchReason(
    IncompatibleNsswitchReason reason,
    std::optional<NsswitchReader::Service> service_token) {
  if (service_token) {
    base::UmaHistogramEnumeration(
        "Net.DNS.DnsConfig.Nsswitch.IncompatibleService",
        service_token.value());
  }
}

bool IsNsswitchConfigCompatible(
    const std::vector<NsswitchReader::ServiceSpecification>& nsswitch_hosts) {
  bool files_found = false;
  for (const NsswitchReader::ServiceSpecification& specification :
       nsswitch_hosts) {
    switch (specification.service) {
      case NsswitchReader::Service::kUnknown:
        RecordIncompatibleNsswitchReason(
            IncompatibleNsswitchReason::kUnknownService, specification.service);
        return false;

      case NsswitchReader::Service::kFiles:
        if (files_found) {
          RecordIncompatibleNsswitchReason(
              IncompatibleNsswitchReason::kMultipleFiles,
              specification.service);
          return false;
        }
        files_found = true;
        // Chrome will use the result on HOSTS hit and otherwise continue to
        // DNS. `kFiles` entries must match that behavior to be compatible.
        if (!AreActionsCompatible(specification.actions,
                                  {{NsswitchReader::Status::kSuccess,
                                    NsswitchReader::Action::kReturn},
                                   {NsswitchReader::Status::kNotFound,
                                    NsswitchReader::Action::kContinue},
                                   {NsswitchReader::Status::kUnavailable,
                                    NsswitchReader::Action::kContinue},
                                   {NsswitchReader::Status::kTryAgain,
                                    NsswitchReader::Action::kContinue}})) {
          RecordIncompatibleNsswitchReason(
              IncompatibleNsswitchReason::kBadFilesActions,
              specification.service);
          return false;
        }
        break;

      case NsswitchReader::Service::kDns:
        if (!files_found) {
          RecordIncompatibleNsswitchReason(
              IncompatibleNsswitchReason::kFilesMissing,
              /*service_token=*/std::nullopt);
          return false;
        }
        // Chrome will always stop if DNS finds a result or will otherwise
        // fallback to the system resolver (and get whatever behavior is
        // configured in nsswitch.conf), so the only compatibility requirement
        // is that `kDns` entries are configured to return on success.
        if (!AreActionsCompatible(specification.actions,
                                  {{NsswitchReader::Status::kSuccess,
                                    NsswitchReader::Action::kReturn}})) {
          RecordIncompatibleNsswitchReason(
              IncompatibleNsswitchReason::kBadDnsActions,
              specification.service);
          return false;
        }

        // Ignore any entries after `kDns` because Chrome will fallback to the
        // system resolver if a result was not found in DNS.
        return true;

      case NsswitchReader::Service::kMdns:
      case NsswitchReader::Service::kMdns4:
      case NsswitchReader::Service::kMdns6:
      case NsswitchReader::Service::kResolve:
      case NsswitchReader::Service::kNis:
        RecordIncompatibleNsswitchReason(
            IncompatibleNsswitchReason::kIncompatibleService,
            specification.service);
        return false;

      case NsswitchReader::Service::kMdnsMinimal:
      case NsswitchReader::Service::kMdns4Minimal:
      case NsswitchReader::Service::kMdns6Minimal:
        // Always compatible as long as `kUnavailable` is `kContinue` because
        // the service is expected to always result in `kUnavailable` for any
        // names Chrome would attempt to resolve (non-*.local names because
        // Chrome always delegates *.local names to the system resolver).
        if (!AreActionsCompatible(specification.actions,
                                  {{NsswitchReader::Status::kUnavailable,
                                    NsswitchReader::Action::kContinue}})) {
          RecordIncompatibleNsswitchReason(
              IncompatibleNsswitchReason::kBadMdnsMinimalActions,
              specification.service);
          return false;
        }
        break;

      case NsswitchReader::Service::kMyHostname:
        // Similar enough to Chrome behavior (or unlikely to matter for Chrome
        // resolutions) to be considered compatible unless the actions do
        // something very weird to skip remaining services without a result.
        if (!AreActionsCompatible(specification.actions,
                                  {{NsswitchReader::Status::kNotFound,
                                    NsswitchReader::Action::kContinue},
                                   {NsswitchReader::Status::kUnavailable,
                                    NsswitchReader::Action::kContinue},
                                   {NsswitchReader::Status::kTryAgain,
                                    NsswitchReader::Action::kContinue}})) {
          RecordIncompatibleNsswitchReason(
              IncompatibleNsswitchReason::kBadOtherServiceActions,
              specification.service);
          return false;
        }
        break;
    }
  }

  RecordIncompatibleNsswitchReason(IncompatibleNsswitchReason::kDnsMissing,
                                   /*service_token=*/std::nullopt);
  return false;
}

}  // namespace

class DnsConfigServiceLinux::Watcher : public DnsConfigService::Watcher {
 public:
  explicit Watcher(DnsConfigServiceLinux& service)
      : DnsConfigService::Watcher(service) {}
  ~Watcher() override = default;

  Watcher(const Watcher&) = delete;
  Watcher& operator=(const Watcher&) = delete;

  bool Watch() override {
    CheckOnCorrectSequence();

    bool success = true;
    if (!resolv_watcher_.Watch(
            base::FilePath(kFilePathResolv),
            base::FilePathWatcher::Type::kNonRecursive,
            base::BindRepeating(&Watcher::OnResolvFilePathWatcherChange,
                                base::Unretained(this)))) {
      LOG(ERROR) << "DNS config (resolv.conf) watch failed to start.";
      success = false;
    }

    if (!nsswitch_watcher_.Watch(
            base::FilePath(kFilePathNsswitch),
            base::FilePathWatcher::Type::kNonRecursive,
            base::BindRepeating(&Watcher::OnNsswitchFilePathWatcherChange,
                                base::Unretained(this)))) {
      LOG(ERROR) << "DNS nsswitch.conf watch failed to start.";
      success = false;
    }

    if (!hosts_watcher_.Watch(
            base::FilePath(kFilePathHosts),
            base::FilePathWatcher::Type::kNonRecursive,
            base::BindRepeating(&Watcher::OnHostsFilePathWatcherChange,
                                base::Unretained(this)))) {
      LOG(ERROR) << "DNS hosts watch failed to start.";
      success = false;
    }
    return success;
  }

 private:
  void OnResolvFilePathWatcherChange(const base::FilePath& path, bool error) {
    OnConfigChanged(!error);
  }

  void OnNsswitchFilePathWatcherChange(const base::FilePath& path, bool error) {
    OnConfigChanged(!error);
  }

  void OnHostsFilePathWatcherChange(const base::FilePath& path, bool error) {
    OnHostsChanged(!error);
  }

  base::FilePathWatcher resolv_watcher_;
  base::FilePathWatcher nsswitch_watcher_;
  base::FilePathWatcher hosts_watcher_;
};

// A SerialWorker that uses libresolv to initialize res_state and converts
// it to DnsConfig.
class DnsConfigServiceLinux::ConfigReader : public SerialWorker {
 public:
  explicit ConfigReader(DnsConfigServiceLinux& service,
                        std::unique_ptr<ResolvReader> resolv_reader,
                        std::unique_ptr<NsswitchReader> nsswitch_reader)
      : service_(&service),
        work_item_(std::make_unique<WorkItem>(std::move(resolv_reader),
                                              std::move(nsswitch_reader))) {
    // Allow execution on another thread; nothing thread-specific about
    // constructor.
    DETACH_FROM_SEQUENCE(sequence_checker_);
  }

  ~ConfigReader() override = default;

  ConfigReader(const ConfigReader&) = delete;
  ConfigReader& operator=(const ConfigReader&) = delete;

  std::unique_ptr<SerialWorker::WorkItem> CreateWorkItem() override {
    // Reuse same `WorkItem` to allow reuse of contained reader objects.
    DCHECK(work_item_);
    return std::move(work_item_);
  }

  bool OnWorkFinished(std::unique_ptr<SerialWorker::WorkItem>
                          serial_worker_work_item) override {
    DCHECK(serial_worker_work_item);
    DCHECK(!work_item_);
    DCHECK(!IsCancelled());

    work_item_.reset(static_cast<WorkItem*>(serial_worker_work_item.release()));
    if (work_item_->dns_config_.has_value()) {
      service_->OnConfigRead(std::move(work_item_->dns_config_).value());
      return true;
    } else {
      LOG(WARNING) << "Failed to read DnsConfig.";
      return false;
    }
  }

 private:
  class WorkItem : public SerialWorker::WorkItem {
   public:
    WorkItem(std::unique_ptr<ResolvReader> resolv_reader,
             std::unique_ptr<NsswitchReader> nsswitch_reader)
        : resolv_reader_(std::move(resolv_reader)),
          nsswitch_reader_(std::move(nsswitch_reader)) {
      DCHECK(resolv_reader_);
      DCHECK(nsswitch_reader_);
    }

    void DoWork() override {
      base::ScopedBlockingCall scoped_blocking_call(
          FROM_HERE, base::BlockingType::MAY_BLOCK);

      {
        std::unique_ptr<ScopedResState> res = resolv_reader_->GetResState();
        if (res) {
          dns_config_ = ConvertResStateToDnsConfig(res->state());
        }
      }

      if (!dns_config_.has_value())
        return;
      base::UmaHistogramBoolean("Net.DNS.DnsConfig.Resolv.Compatible",
                                !dns_config_->unhandled_options);

      // Override `fallback_period` value to match default setting on
      // Windows.
      dns_config_->fallback_period = kDnsDefaultFallbackPeriod;

      if (dns_config_ && !dns_config_->unhandled_options) {
        std::vector<NsswitchReader::ServiceSpecification> nsswitch_hosts =
            nsswitch_reader_->ReadAndParseHosts();
        dns_config_->unhandled_options =
            !IsNsswitchConfigCompatible(nsswitch_hosts);
        base::UmaHistogramBoolean("Net.DNS.DnsConfig.Nsswitch.Compatible",
                                  !dns_config_->unhandled_options);
      }
    }

   private:
    friend class ConfigReader;
    std::optional<DnsConfig> dns_config_;
    std::unique_ptr<ResolvReader> resolv_reader_;
    std::unique_ptr<NsswitchReader> nsswitch_reader_;
  };

  // Raw pointer to owning DnsConfigService.
  const raw_ptr<DnsConfigServiceLinux> service_;

  // Null while the `WorkItem` is running on the `ThreadPool`.
  std::unique_ptr<WorkItem> work_item_;
};

DnsConfigServiceLinux::DnsConfigServiceLinux()
    : DnsConfigService(kFilePathHosts) {
  // Allow constructing on one thread and living on another.
  DETACH_FROM_SEQUENCE(sequence_checker_);
}

DnsConfigServiceLinux::~DnsConfigServiceLinux() {
  if (config_reader_)
    config_reader_->Cancel();
}

void DnsConfigServiceLinux::ReadConfigNow() {
  if (!config_reader_)
    CreateReader();
  config_reader_->WorkNow();
}

bool DnsConfigServiceLinux::StartWatching() {
  CreateReader();
  watcher_ = std::make_unique<Watcher>(*this);
  return watcher_->Watch();
}

void DnsConfigServiceLinux::CreateReader() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!config_reader_);
  DCHECK(resolv_reader_);
  DCHECK(nsswitch_reader_);
  config_reader_ = std::make_unique<ConfigReader>(
      *this, std::move(resolv_reader_), std::move(nsswitch_reader_));
}

}  // namespace internal

// static
std::unique_ptr<DnsConfigService> DnsConfigService::CreateSystemService() {
  return std::make_unique<internal::DnsConfigServiceLinux>();
}

}  // namespace net