Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ken Chen | 5471dca | 2019-04-15 15:25:35 +0800 | [diff] [blame] | 17 | #define LOG_TAG "resolv" |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 18 | |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 19 | #include "PrivateDnsConfiguration.h" |
| 20 | |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 23 | #include <netdb.h> |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 24 | #include <netdutils/ThreadUtil.h> |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 25 | #include <sys/socket.h> |
| 26 | |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 27 | #include "DnsTlsTransport.h" |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 28 | #include "ResolverEventReporter.h" |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 29 | #include "netd_resolv/resolv.h" |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 30 | #include "netdutils/BackoffSequence.h" |
| 31 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 32 | namespace android { |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 33 | namespace net { |
| 34 | |
| 35 | std::string addrToString(const sockaddr_storage* addr) { |
| 36 | char out[INET6_ADDRSTRLEN] = {0}; |
| 37 | getnameinfo((const sockaddr*) addr, sizeof(sockaddr_storage), out, INET6_ADDRSTRLEN, nullptr, 0, |
| 38 | NI_NUMERICHOST); |
| 39 | return std::string(out); |
| 40 | } |
| 41 | |
| 42 | bool parseServer(const char* server, sockaddr_storage* parsed) { |
Nick Desaulniers | cd6395a | 2019-10-11 09:15:24 -0700 | [diff] [blame] | 43 | addrinfo hints = { |
| 44 | .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV, |
| 45 | .ai_family = AF_UNSPEC, |
| 46 | }; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 47 | addrinfo* res; |
| 48 | |
| 49 | int err = getaddrinfo(server, "853", &hints, &res); |
| 50 | if (err != 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 51 | LOG(WARNING) << "Failed to parse server address (" << server << "): " << gai_strerror(err); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
| 55 | memcpy(parsed, res->ai_addr, res->ai_addrlen); |
| 56 | freeaddrinfo(res); |
| 57 | return true; |
| 58 | } |
| 59 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 60 | int PrivateDnsConfiguration::set(int32_t netId, uint32_t mark, |
| 61 | const std::vector<std::string>& servers, const std::string& name, |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 62 | const std::string& caCert) { |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 63 | LOG(DEBUG) << "PrivateDnsConfiguration::set(" << netId << ", 0x" << std::hex << mark << std::dec |
| 64 | << ", " << servers.size() << ", " << name << ")"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 65 | |
| 66 | // Parse the list of servers that has been passed in |
| 67 | std::set<DnsTlsServer> tlsServers; |
| 68 | for (size_t i = 0; i < servers.size(); ++i) { |
| 69 | sockaddr_storage parsed; |
| 70 | if (!parseServer(servers[i].c_str(), &parsed)) { |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | DnsTlsServer server(parsed); |
| 74 | server.name = name; |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 75 | server.certificate = caCert; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 76 | tlsServers.insert(server); |
| 77 | } |
| 78 | |
| 79 | std::lock_guard guard(mPrivateDnsLock); |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 80 | if (!name.empty()) { |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 81 | mPrivateDnsModes[netId] = PrivateDnsMode::STRICT; |
| 82 | } else if (!tlsServers.empty()) { |
| 83 | mPrivateDnsModes[netId] = PrivateDnsMode::OPPORTUNISTIC; |
| 84 | } else { |
| 85 | mPrivateDnsModes[netId] = PrivateDnsMode::OFF; |
| 86 | mPrivateDnsTransports.erase(netId); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | // Create the tracker if it was not present |
| 91 | auto netPair = mPrivateDnsTransports.find(netId); |
| 92 | if (netPair == mPrivateDnsTransports.end()) { |
| 93 | // No TLS tracker yet for this netId. |
| 94 | bool added; |
| 95 | std::tie(netPair, added) = mPrivateDnsTransports.emplace(netId, PrivateDnsTracker()); |
| 96 | if (!added) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 97 | LOG(ERROR) << "Memory error while recording private DNS for netId " << netId; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 98 | return -ENOMEM; |
| 99 | } |
| 100 | } |
| 101 | auto& tracker = netPair->second; |
| 102 | |
| 103 | // Remove any servers from the tracker that are not in |servers| exactly. |
| 104 | for (auto it = tracker.begin(); it != tracker.end();) { |
| 105 | if (tlsServers.count(it->first) == 0) { |
| 106 | it = tracker.erase(it); |
| 107 | } else { |
| 108 | ++it; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Add any new or changed servers to the tracker, and initiate async checks for them. |
| 113 | for (const auto& server : tlsServers) { |
| 114 | if (needsValidation(tracker, server)) { |
| 115 | validatePrivateDnsProvider(server, tracker, netId, mark); |
| 116 | } |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | PrivateDnsStatus PrivateDnsConfiguration::getStatus(unsigned netId) { |
| 122 | PrivateDnsStatus status{PrivateDnsMode::OFF, {}}; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 123 | std::lock_guard guard(mPrivateDnsLock); |
| 124 | |
| 125 | const auto mode = mPrivateDnsModes.find(netId); |
| 126 | if (mode == mPrivateDnsModes.end()) return status; |
| 127 | status.mode = mode->second; |
| 128 | |
| 129 | const auto netPair = mPrivateDnsTransports.find(netId); |
| 130 | if (netPair != mPrivateDnsTransports.end()) { |
| 131 | for (const auto& serverPair : netPair->second) { |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 132 | status.serversMap.emplace(serverPair.first, serverPair.second); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | return status; |
| 137 | } |
| 138 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 139 | void PrivateDnsConfiguration::clear(unsigned netId) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 140 | LOG(DEBUG) << "PrivateDnsConfiguration::clear(" << netId << ")"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 141 | std::lock_guard guard(mPrivateDnsLock); |
| 142 | mPrivateDnsModes.erase(netId); |
| 143 | mPrivateDnsTransports.erase(netId); |
| 144 | } |
| 145 | |
| 146 | void PrivateDnsConfiguration::validatePrivateDnsProvider(const DnsTlsServer& server, |
| 147 | PrivateDnsTracker& tracker, unsigned netId, |
| 148 | uint32_t mark) REQUIRES(mPrivateDnsLock) { |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 149 | tracker[server] = Validation::in_process; |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 150 | LOG(DEBUG) << "Server " << addrToString(&server.ss) << " marked as in_process on netId " |
| 151 | << netId << ". Tracker now has size " << tracker.size(); |
| 152 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 153 | // Note that capturing |server| and |netId| in this lambda create copies. |
| 154 | std::thread validate_thread([this, server, netId, mark] { |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 155 | netdutils::setThreadName(android::base::StringPrintf("TlsVerify_%u", netId).c_str()); |
| 156 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 157 | // cat /proc/sys/net/ipv4/tcp_syn_retries yields "6". |
| 158 | // |
| 159 | // Start with a 1 minute delay and backoff to once per hour. |
| 160 | // |
| 161 | // Assumptions: |
| 162 | // [1] Each TLS validation is ~10KB of certs+handshake+payload. |
| 163 | // [2] Network typically provision clients with <=4 nameservers. |
| 164 | // [3] Average month has 30 days. |
| 165 | // |
| 166 | // Each validation pass in a given hour is ~1.2MB of data. And 24 |
| 167 | // such validation passes per day is about ~30MB per month, in the |
| 168 | // worst case. Otherwise, this will cost ~600 SYNs per month |
| 169 | // (6 SYNs per ip, 4 ips per validation pass, 24 passes per day). |
Bernie Innocenti | 23c6e2a | 2019-05-16 15:18:35 +0900 | [diff] [blame] | 170 | auto backoff = netdutils::BackoffSequence<>::Builder() |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 171 | .withInitialRetransmissionTime(std::chrono::seconds(60)) |
| 172 | .withMaximumRetransmissionTime(std::chrono::seconds(3600)) |
| 173 | .build(); |
| 174 | |
| 175 | while (true) { |
| 176 | // ::validate() is a blocking call that performs network operations. |
| 177 | // It can take milliseconds to minutes, up to the SYN retry limit. |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 178 | LOG(WARNING) << "Validating DnsTlsServer on netId " << netId; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 179 | const bool success = DnsTlsTransport::validate(server, netId, mark); |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 180 | LOG(DEBUG) << "validateDnsTlsServer returned " << success << " for " |
| 181 | << addrToString(&server.ss); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 182 | |
| 183 | const bool needs_reeval = this->recordPrivateDnsValidation(server, netId, success); |
| 184 | if (!needs_reeval) { |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | if (backoff.hasNextTimeout()) { |
| 189 | std::this_thread::sleep_for(backoff.getNextTimeout()); |
| 190 | } else { |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | }); |
| 195 | validate_thread.detach(); |
| 196 | } |
| 197 | |
| 198 | bool PrivateDnsConfiguration::recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, |
| 199 | bool success) { |
| 200 | constexpr bool NEEDS_REEVALUATION = true; |
| 201 | constexpr bool DONT_REEVALUATE = false; |
| 202 | |
| 203 | std::lock_guard guard(mPrivateDnsLock); |
| 204 | |
| 205 | auto netPair = mPrivateDnsTransports.find(netId); |
| 206 | if (netPair == mPrivateDnsTransports.end()) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 207 | LOG(WARNING) << "netId " << netId << " was erased during private DNS validation"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 208 | return DONT_REEVALUATE; |
| 209 | } |
| 210 | |
| 211 | const auto mode = mPrivateDnsModes.find(netId); |
| 212 | if (mode == mPrivateDnsModes.end()) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 213 | LOG(WARNING) << "netId " << netId << " has no private DNS validation mode"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 214 | return DONT_REEVALUATE; |
| 215 | } |
| 216 | const bool modeDoesReevaluation = (mode->second == PrivateDnsMode::STRICT); |
| 217 | |
| 218 | bool reevaluationStatus = |
| 219 | (success || !modeDoesReevaluation) ? DONT_REEVALUATE : NEEDS_REEVALUATION; |
| 220 | |
| 221 | auto& tracker = netPair->second; |
| 222 | auto serverPair = tracker.find(server); |
| 223 | if (serverPair == tracker.end()) { |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 224 | // TODO: Consider not adding this server to the tracker since this server is not expected |
| 225 | // to be one of the private DNS servers for this network now. This could prevent this |
| 226 | // server from being included when dumping status. |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 227 | LOG(WARNING) << "Server " << addrToString(&server.ss) |
| 228 | << " was removed during private DNS validation"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 229 | success = false; |
| 230 | reevaluationStatus = DONT_REEVALUATE; |
| 231 | } else if (!(serverPair->first == server)) { |
| 232 | // TODO: It doesn't seem correct to overwrite the tracker entry for |
| 233 | // |server| down below in this circumstance... Fix this. |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 234 | LOG(WARNING) << "Server " << addrToString(&server.ss) |
| 235 | << " was changed during private DNS validation"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 236 | success = false; |
| 237 | reevaluationStatus = DONT_REEVALUATE; |
| 238 | } |
| 239 | |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 240 | // Send a validation event to NetdEventListenerService. |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 241 | const auto& listeners = ResolverEventReporter::getInstance().getListeners(); |
| 242 | if (listeners.size() != 0) { |
| 243 | for (const auto& it : listeners) { |
| 244 | it->onPrivateDnsValidationEvent(netId, addrToString(&server.ss), server.name, success); |
| 245 | } |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 246 | LOG(DEBUG) << "Sent validation " << (success ? "success" : "failure") << " event on netId " |
| 247 | << netId << " for " << addrToString(&server.ss) << " with hostname {" |
| 248 | << server.name << "}"; |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 249 | } else { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 250 | LOG(ERROR) |
| 251 | << "Validation event not sent since no INetdEventListener receiver is available."; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (success) { |
| 255 | tracker[server] = Validation::success; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 256 | } else { |
| 257 | // Validation failure is expected if a user is on a captive portal. |
| 258 | // TODO: Trigger a second validation attempt after captive portal login |
| 259 | // succeeds. |
| 260 | tracker[server] = (reevaluationStatus == NEEDS_REEVALUATION) ? Validation::in_process |
| 261 | : Validation::fail; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 262 | } |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 263 | LOG(WARNING) << "Validation " << (success ? "success" : "failed"); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 264 | |
| 265 | return reevaluationStatus; |
| 266 | } |
| 267 | |
| 268 | // Start validation for newly added servers as well as any servers that have |
| 269 | // landed in Validation::fail state. Note that servers that have failed |
| 270 | // multiple validation attempts but for which there is still a validating |
| 271 | // thread running are marked as being Validation::in_process. |
| 272 | bool PrivateDnsConfiguration::needsValidation(const PrivateDnsTracker& tracker, |
| 273 | const DnsTlsServer& server) { |
| 274 | const auto& iter = tracker.find(server); |
| 275 | return (iter == tracker.end()) || (iter->second == Validation::fail); |
| 276 | } |
| 277 | |
| 278 | PrivateDnsConfiguration gPrivateDnsConfiguration; |
| 279 | |
| 280 | } // namespace net |
| 281 | } // namespace android |