forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns_udp_tracker.cc
164 lines (132 loc) · 4.92 KB
/
dns_udp_tracker.cc
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
// Copyright 2020 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_udp_tracker.h"
#include <algorithm>
#include <utility>
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/time/tick_clock.h"
#include "net/base/net_errors.h"
namespace net {
namespace {
// Used in UMA (DNS.UdpLowEntropyReason). Do not renumber or remove values.
enum class LowEntropyReason {
kPortReuse = 0,
kRecognizedIdMismatch = 1,
kUnrecognizedIdMismatch = 2,
kSocketLimitExhaustion = 3,
kMaxValue = kSocketLimitExhaustion,
};
void RecordLowEntropyUma(LowEntropyReason reason) {
UMA_HISTOGRAM_ENUMERATION("Net.DNS.DnsTransaction.UDP.LowEntropyReason",
reason);
}
} // namespace
// static
constexpr base::TimeDelta DnsUdpTracker::kMaxAge;
// static
constexpr size_t DnsUdpTracker::kMaxRecordedQueries;
// static
constexpr base::TimeDelta DnsUdpTracker::kMaxRecognizedIdAge;
// static
constexpr size_t DnsUdpTracker::kUnrecognizedIdMismatchThreshold;
// static
constexpr size_t DnsUdpTracker::kRecognizedIdMismatchThreshold;
// static
constexpr int DnsUdpTracker::kPortReuseThreshold;
struct DnsUdpTracker::QueryData {
uint16_t port;
uint16_t query_id;
base::TimeTicks time;
};
DnsUdpTracker::DnsUdpTracker() = default;
DnsUdpTracker::~DnsUdpTracker() = default;
DnsUdpTracker::DnsUdpTracker(DnsUdpTracker&&) = default;
DnsUdpTracker& DnsUdpTracker::operator=(DnsUdpTracker&&) = default;
void DnsUdpTracker::RecordQuery(uint16_t port, uint16_t query_id) {
PurgeOldRecords();
int reused_port_count = base::checked_cast<int>(std::count_if(
recent_queries_.cbegin(), recent_queries_.cend(),
[port](const auto& recent_query) { return port == recent_query.port; }));
if (reused_port_count >= kPortReuseThreshold && !low_entropy_) {
low_entropy_ = true;
RecordLowEntropyUma(LowEntropyReason::kPortReuse);
}
SaveQuery({port, query_id, tick_clock_->NowTicks()});
}
void DnsUdpTracker::RecordResponseId(uint16_t query_id, uint16_t response_id) {
PurgeOldRecords();
if (query_id != response_id) {
SaveIdMismatch(response_id);
}
}
void DnsUdpTracker::RecordConnectionError(int connection_error) {
if (!low_entropy_ && connection_error == ERR_INSUFFICIENT_RESOURCES) {
// On UDP connection, this error signifies that the process is using an
// unreasonably large number of UDP sockets, potentially a deliberate
// attack to reduce DNS port entropy.
low_entropy_ = true;
RecordLowEntropyUma(LowEntropyReason::kSocketLimitExhaustion);
}
}
void DnsUdpTracker::PurgeOldRecords() {
base::TimeTicks now = tick_clock_->NowTicks();
while (!recent_queries_.empty() &&
(now - recent_queries_.front().time) > kMaxAge) {
recent_queries_.pop_front();
}
while (!recent_unrecognized_id_hits_.empty() &&
now - recent_unrecognized_id_hits_.front() > kMaxAge) {
recent_unrecognized_id_hits_.pop_front();
}
while (!recent_recognized_id_hits_.empty() &&
now - recent_recognized_id_hits_.front() > kMaxAge) {
recent_recognized_id_hits_.pop_front();
}
}
void DnsUdpTracker::SaveQuery(QueryData query) {
if (recent_queries_.size() == kMaxRecordedQueries)
recent_queries_.pop_front();
DCHECK_LT(recent_queries_.size(), kMaxRecordedQueries);
DCHECK(recent_queries_.empty() || query.time >= recent_queries_.back().time);
recent_queries_.push_back(std::move(query));
}
void DnsUdpTracker::SaveIdMismatch(uint16_t id) {
// No need to track mismatches if already flagged for low entropy.
if (low_entropy_)
return;
base::TimeTicks now = tick_clock_->NowTicks();
base::TimeTicks time_cutoff = now - kMaxRecognizedIdAge;
bool is_recognized = std::any_of(
recent_queries_.cbegin(), recent_queries_.cend(),
[&](const auto& recent_query) {
return recent_query.query_id == id && recent_query.time >= time_cutoff;
});
if (is_recognized) {
DCHECK_LT(recent_recognized_id_hits_.size(),
kRecognizedIdMismatchThreshold);
if (recent_recognized_id_hits_.size() ==
kRecognizedIdMismatchThreshold - 1) {
low_entropy_ = true;
RecordLowEntropyUma(LowEntropyReason::kRecognizedIdMismatch);
return;
}
DCHECK(recent_recognized_id_hits_.empty() ||
now >= recent_recognized_id_hits_.back());
recent_recognized_id_hits_.push_back(now);
} else {
DCHECK_LT(recent_unrecognized_id_hits_.size(),
kUnrecognizedIdMismatchThreshold);
if (recent_unrecognized_id_hits_.size() ==
kUnrecognizedIdMismatchThreshold - 1) {
low_entropy_ = true;
RecordLowEntropyUma(LowEntropyReason::kUnrecognizedIdMismatch);
return;
}
DCHECK(recent_unrecognized_id_hits_.empty() ||
now >= recent_unrecognized_id_hits_.back());
recent_unrecognized_id_hits_.push_back(now);
}
}
} // namespace net