Mike Yu | bab3daa | 2018-10-19 22:11:43 +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 | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 18 | |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 19 | #include "DnsTlsSocket.h" |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 20 | |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 21 | #include <arpa/inet.h> |
| 22 | #include <arpa/nameser.h> |
| 23 | #include <errno.h> |
| 24 | #include <linux/tcp.h> |
| 25 | #include <openssl/err.h> |
| 26 | #include <openssl/sha.h> |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 27 | #include <sys/eventfd.h> |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 28 | #include <sys/poll.h> |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 29 | #include <unistd.h> |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 30 | #include <algorithm> |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 31 | |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 32 | #include "DnsTlsSessionCache.h" |
| 33 | #include "IDnsTlsSocketObserver.h" |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 34 | |
Chiachang Wang | 3237217 | 2019-07-06 13:54:18 +0800 | [diff] [blame] | 35 | #include <Fwmark.h> |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 36 | #include <android-base/logging.h> |
Chiachang Wang | 3237217 | 2019-07-06 13:54:18 +0800 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 38 | #include <netdutils/SocketOption.h> |
| 39 | #include <netdutils/ThreadUtil.h> |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 40 | |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 41 | #include "private/android_filesystem_config.h" // AID_DNS |
Sehee Park | d975bf3 | 2019-08-07 13:21:16 +0900 | [diff] [blame] | 42 | #include "resolv_private.h" |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 43 | |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 44 | // NOTE: Inject CA certificate for internal testing -- do NOT enable in production builds |
| 45 | #ifndef RESOLV_INJECT_CA_CERTIFICATE |
| 46 | #define RESOLV_INJECT_CA_CERTIFICATE 0 |
| 47 | #endif |
| 48 | |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
| 51 | using netdutils::enableSockopt; |
| 52 | using netdutils::enableTcpKeepAlives; |
| 53 | using netdutils::isOk; |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 54 | using netdutils::Slice; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 55 | using netdutils::Status; |
| 56 | |
| 57 | namespace net { |
| 58 | namespace { |
| 59 | |
| 60 | constexpr const char kCaCertDir[] = "/system/etc/security/cacerts"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 61 | |
| 62 | int waitForReading(int fd) { |
| 63 | struct pollfd fds = { .fd = fd, .events = POLLIN }; |
| 64 | const int ret = TEMP_FAILURE_RETRY(poll(&fds, 1, -1)); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | int waitForWriting(int fd) { |
| 69 | struct pollfd fds = { .fd = fd, .events = POLLOUT }; |
| 70 | const int ret = TEMP_FAILURE_RETRY(poll(&fds, 1, -1)); |
| 71 | return ret; |
| 72 | } |
| 73 | |
Chiachang Wang | 3237217 | 2019-07-06 13:54:18 +0800 | [diff] [blame] | 74 | std::string markToFwmarkString(unsigned mMark) { |
| 75 | Fwmark mark; |
| 76 | mark.intValue = mMark; |
| 77 | return android::base::StringPrintf("%d, %d, %d, %d, %d", mark.netId, mark.explicitlySelected, |
| 78 | mark.protectedFromVpn, mark.permission, mark.uidBillingDone); |
| 79 | } |
| 80 | |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 81 | } // namespace |
| 82 | |
| 83 | Status DnsTlsSocket::tcpConnect() { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 84 | LOG(DEBUG) << mMark << " connecting TCP socket"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 85 | int type = SOCK_NONBLOCK | SOCK_CLOEXEC; |
| 86 | switch (mServer.protocol) { |
| 87 | case IPPROTO_TCP: |
| 88 | type |= SOCK_STREAM; |
| 89 | break; |
| 90 | default: |
| 91 | return Status(EPROTONOSUPPORT); |
| 92 | } |
| 93 | |
| 94 | mSslFd.reset(socket(mServer.ss.ss_family, type, mServer.protocol)); |
| 95 | if (mSslFd.get() == -1) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 96 | LOG(ERROR) << "Failed to create socket"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 97 | return Status(errno); |
| 98 | } |
| 99 | |
Sehee Park | d975bf3 | 2019-08-07 13:21:16 +0900 | [diff] [blame] | 100 | resolv_tag_socket(mSslFd.get(), AID_DNS); |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 101 | |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 102 | const socklen_t len = sizeof(mMark); |
| 103 | if (setsockopt(mSslFd.get(), SOL_SOCKET, SO_MARK, &mMark, len) == -1) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 104 | LOG(ERROR) << "Failed to set socket mark"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 105 | mSslFd.reset(); |
| 106 | return Status(errno); |
| 107 | } |
| 108 | |
| 109 | const Status tfo = enableSockopt(mSslFd.get(), SOL_TCP, TCP_FASTOPEN_CONNECT); |
| 110 | if (!isOk(tfo) && tfo.code() != ENOPROTOOPT) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 111 | LOG(WARNING) << "Failed to enable TFO: " << tfo.msg(); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Send 5 keepalives, 3 seconds apart, after 15 seconds of inactivity. |
| 115 | enableTcpKeepAlives(mSslFd.get(), 15U, 5U, 3U).ignoreError(); |
| 116 | |
| 117 | if (connect(mSslFd.get(), reinterpret_cast<const struct sockaddr *>(&mServer.ss), |
| 118 | sizeof(mServer.ss)) != 0 && |
| 119 | errno != EINPROGRESS) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 120 | LOG(DEBUG) << "Socket failed to connect"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 121 | mSslFd.reset(); |
| 122 | return Status(errno); |
| 123 | } |
| 124 | |
| 125 | return netdutils::status::ok; |
| 126 | } |
| 127 | |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 128 | bool DnsTlsSocket::setTestCaCertificate() { |
| 129 | bssl::UniquePtr<BIO> bio( |
| 130 | BIO_new_mem_buf(mServer.certificate.data(), mServer.certificate.size())); |
| 131 | bssl::UniquePtr<X509> cert(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 132 | if (!cert) { |
| 133 | LOG(ERROR) << "Failed to read cert"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 134 | return false; |
| 135 | } |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 136 | |
| 137 | X509_STORE* cert_store = SSL_CTX_get_cert_store(mSslCtx.get()); |
| 138 | if (!X509_STORE_add_cert(cert_store, cert.get())) { |
| 139 | LOG(ERROR) << "Failed to add cert"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 145 | // TODO: Try to use static sSslCtx instead of mSslCtx |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 146 | bool DnsTlsSocket::initialize() { |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 147 | // This method is called every time when a new SSL connection is created. |
| 148 | // This lock only serves to help catch bugs in code that calls this method. |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 149 | std::lock_guard guard(mLock); |
| 150 | if (mSslCtx) { |
| 151 | // This is a bug in the caller. |
| 152 | return false; |
| 153 | } |
| 154 | mSslCtx.reset(SSL_CTX_new(TLS_method())); |
| 155 | if (!mSslCtx) { |
| 156 | return false; |
| 157 | } |
| 158 | |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 159 | // Load system CA certs from CAPath for hostname verification. |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 160 | // |
| 161 | // For discussion of alternative, sustainable approaches see b/71909242. |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 162 | if (RESOLV_INJECT_CA_CERTIFICATE && !mServer.certificate.empty()) { |
| 163 | // Inject test CA certs from ResolverParamsParcel.caCertificate for internal testing. |
| 164 | LOG(WARNING) << "test CA certificate is valid"; |
| 165 | if (!setTestCaCertificate()) { |
| 166 | LOG(ERROR) << "Failed to set test CA certificate"; |
| 167 | return false; |
| 168 | } |
| 169 | } else { |
| 170 | if (SSL_CTX_load_verify_locations(mSslCtx.get(), nullptr, kCaCertDir) != 1) { |
| 171 | LOG(ERROR) << "Failed to load CA cert dir: " << kCaCertDir; |
| 172 | return false; |
| 173 | } |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Enable TLS false start |
| 177 | SSL_CTX_set_false_start_allowed_without_alpn(mSslCtx.get(), 1); |
| 178 | SSL_CTX_set_mode(mSslCtx.get(), SSL_MODE_ENABLE_FALSE_START); |
| 179 | |
| 180 | // Enable session cache |
| 181 | mCache->prepareSslContext(mSslCtx.get()); |
| 182 | |
| 183 | // Connect |
| 184 | Status status = tcpConnect(); |
| 185 | if (!status.ok()) { |
| 186 | return false; |
| 187 | } |
| 188 | mSsl = sslConnect(mSslFd.get()); |
| 189 | if (!mSsl) { |
| 190 | return false; |
| 191 | } |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 192 | |
| 193 | mEventFd.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 194 | |
| 195 | // Start the I/O loop. |
| 196 | mLoopThread.reset(new std::thread(&DnsTlsSocket::loop, this)); |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bssl::UniquePtr<SSL> DnsTlsSocket::sslConnect(int fd) { |
| 202 | if (!mSslCtx) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 203 | LOG(ERROR) << "Internal error: context is null in sslConnect"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 204 | return nullptr; |
| 205 | } |
| 206 | if (!SSL_CTX_set_min_proto_version(mSslCtx.get(), TLS1_2_VERSION)) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 207 | LOG(ERROR) << "Failed to set minimum TLS version"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 208 | return nullptr; |
| 209 | } |
| 210 | |
| 211 | bssl::UniquePtr<SSL> ssl(SSL_new(mSslCtx.get())); |
| 212 | // This file descriptor is owned by mSslFd, so don't let libssl close it. |
| 213 | bssl::UniquePtr<BIO> bio(BIO_new_socket(fd, BIO_NOCLOSE)); |
| 214 | SSL_set_bio(ssl.get(), bio.get(), bio.get()); |
| 215 | bio.release(); |
| 216 | |
| 217 | if (!mCache->prepareSsl(ssl.get())) { |
| 218 | return nullptr; |
| 219 | } |
| 220 | |
| 221 | if (!mServer.name.empty()) { |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 222 | LOG(VERBOSE) << "Checking DNS over TLS hostname = " << mServer.name.c_str(); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 223 | if (SSL_set_tlsext_host_name(ssl.get(), mServer.name.c_str()) != 1) { |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 224 | LOG(ERROR) << "Failed to set SNI to " << mServer.name; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 225 | return nullptr; |
| 226 | } |
| 227 | X509_VERIFY_PARAM* param = SSL_get0_param(ssl.get()); |
| 228 | if (X509_VERIFY_PARAM_set1_host(param, mServer.name.data(), mServer.name.size()) != 1) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 229 | LOG(ERROR) << "Failed to set verify host param to " << mServer.name; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 230 | return nullptr; |
| 231 | } |
| 232 | // This will cause the handshake to fail if certificate verification fails. |
| 233 | SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, nullptr); |
| 234 | } |
| 235 | |
| 236 | bssl::UniquePtr<SSL_SESSION> session = mCache->getSession(); |
| 237 | if (session) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 238 | LOG(DEBUG) << "Setting session"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 239 | SSL_set_session(ssl.get(), session.get()); |
| 240 | } else { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 241 | LOG(DEBUG) << "No session available"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | for (;;) { |
Chiachang Wang | 3237217 | 2019-07-06 13:54:18 +0800 | [diff] [blame] | 245 | LOG(DEBUG) << " Calling SSL_connect with " << markToFwmarkString(mMark); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 246 | int ret = SSL_connect(ssl.get()); |
Chiachang Wang | 3237217 | 2019-07-06 13:54:18 +0800 | [diff] [blame] | 247 | LOG(DEBUG) << " SSL_connect returned " << ret << " with " << markToFwmarkString(mMark); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 248 | if (ret == 1) break; // SSL handshake complete; |
| 249 | |
| 250 | const int ssl_err = SSL_get_error(ssl.get(), ret); |
| 251 | switch (ssl_err) { |
| 252 | case SSL_ERROR_WANT_READ: |
| 253 | if (waitForReading(fd) != 1) { |
Chiachang Wang | 3237217 | 2019-07-06 13:54:18 +0800 | [diff] [blame] | 254 | PLOG(WARNING) << "SSL_connect read error, " << markToFwmarkString(mMark); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 255 | return nullptr; |
| 256 | } |
| 257 | break; |
| 258 | case SSL_ERROR_WANT_WRITE: |
| 259 | if (waitForWriting(fd) != 1) { |
Chiachang Wang | 3237217 | 2019-07-06 13:54:18 +0800 | [diff] [blame] | 260 | PLOG(WARNING) << "SSL_connect write error, " << markToFwmarkString(mMark); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 261 | return nullptr; |
| 262 | } |
| 263 | break; |
| 264 | default: |
Chiachang Wang | 3237217 | 2019-07-06 13:54:18 +0800 | [diff] [blame] | 265 | PLOG(WARNING) << "SSL_connect ssl error =" << ssl_err << ", " |
| 266 | << markToFwmarkString(mMark); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 267 | return nullptr; |
| 268 | } |
| 269 | } |
| 270 | |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 271 | LOG(DEBUG) << mMark << " handshake complete"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 272 | |
| 273 | return ssl; |
| 274 | } |
| 275 | |
| 276 | void DnsTlsSocket::sslDisconnect() { |
| 277 | if (mSsl) { |
| 278 | SSL_shutdown(mSsl.get()); |
| 279 | mSsl.reset(); |
| 280 | } |
| 281 | mSslFd.reset(); |
| 282 | } |
| 283 | |
| 284 | bool DnsTlsSocket::sslWrite(const Slice buffer) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 285 | LOG(DEBUG) << mMark << " Writing " << buffer.size() << " bytes"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 286 | for (;;) { |
| 287 | int ret = SSL_write(mSsl.get(), buffer.base(), buffer.size()); |
| 288 | if (ret == int(buffer.size())) break; // SSL write complete; |
| 289 | |
| 290 | if (ret < 1) { |
| 291 | const int ssl_err = SSL_get_error(mSsl.get(), ret); |
| 292 | switch (ssl_err) { |
| 293 | case SSL_ERROR_WANT_WRITE: |
| 294 | if (waitForWriting(mSslFd.get()) != 1) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 295 | LOG(DEBUG) << "SSL_write error"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | continue; |
| 299 | case 0: |
| 300 | break; // SSL write complete; |
| 301 | default: |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 302 | LOG(DEBUG) << "SSL_write error " << ssl_err; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | } |
| 306 | } |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 307 | LOG(DEBUG) << mMark << " Wrote " << buffer.size() << " bytes"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
| 311 | void DnsTlsSocket::loop() { |
| 312 | std::lock_guard guard(mLock); |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 313 | std::deque<std::vector<uint8_t>> q; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 314 | const int timeout_msecs = DnsTlsSocket::kIdleTimeout.count() * 1000; |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 315 | |
| 316 | Fwmark mark; |
| 317 | mark.intValue = mMark; |
| 318 | netdutils::setThreadName(android::base::StringPrintf("TlsListen_%u", mark.netId).c_str()); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 319 | while (true) { |
| 320 | // poll() ignores negative fds |
| 321 | struct pollfd fds[2] = { { .fd = -1 }, { .fd = -1 } }; |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 322 | enum { SSLFD = 0, EVENTFD = 1 }; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 323 | |
| 324 | // Always listen for a response from server. |
| 325 | fds[SSLFD].fd = mSslFd.get(); |
| 326 | fds[SSLFD].events = POLLIN; |
| 327 | |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 328 | // If we have pending queries, wait for space to write one. |
| 329 | // Otherwise, listen for new queries. |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 330 | // Note: This blocks the destructor until q is empty, i.e. until all pending |
| 331 | // queries are sent or have failed to send. |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 332 | if (!q.empty()) { |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 333 | fds[SSLFD].events |= POLLOUT; |
| 334 | } else { |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 335 | fds[EVENTFD].fd = mEventFd.get(); |
| 336 | fds[EVENTFD].events = POLLIN; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | const int s = TEMP_FAILURE_RETRY(poll(fds, std::size(fds), timeout_msecs)); |
| 340 | if (s == 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 341 | LOG(DEBUG) << "Idle timeout"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 342 | break; |
| 343 | } |
| 344 | if (s < 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 345 | LOG(DEBUG) << "Poll failed: " << errno; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 346 | break; |
| 347 | } |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 348 | if (fds[SSLFD].revents & (POLLIN | POLLERR | POLLHUP)) { |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 349 | if (!readResponse()) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 350 | LOG(DEBUG) << "SSL remote close or read error."; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | } |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 354 | if (fds[EVENTFD].revents & (POLLIN | POLLERR)) { |
| 355 | int64_t num_queries; |
| 356 | ssize_t res = read(mEventFd.get(), &num_queries, sizeof(num_queries)); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 357 | if (res < 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 358 | LOG(WARNING) << "Error during eventfd read"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 359 | break; |
| 360 | } else if (res == 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 361 | LOG(WARNING) << "eventfd closed; disconnecting"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 362 | break; |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 363 | } else if (res != sizeof(num_queries)) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 364 | LOG(ERROR) << "Int size mismatch: " << res << " != " << sizeof(num_queries); |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 365 | break; |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 366 | } else if (num_queries < 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 367 | LOG(DEBUG) << "Negative eventfd read indicates destructor-initiated shutdown"; |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | // Take ownership of all pending queries. (q is always empty here.) |
| 371 | mQueue.swap(q); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 372 | } else if (fds[SSLFD].revents & POLLOUT) { |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 373 | // q cannot be empty here. |
| 374 | // Sending the entire queue here would risk a TCP flow control deadlock, so |
| 375 | // we only send a single query on each cycle of this loop. |
| 376 | // TODO: Coalesce multiple pending queries if there is enough space in the |
| 377 | // write buffer. |
| 378 | if (!sendQuery(q.front())) { |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 379 | break; |
| 380 | } |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 381 | q.pop_front(); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 382 | } |
| 383 | } |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 384 | LOG(DEBUG) << "Disconnecting"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 385 | sslDisconnect(); |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 386 | LOG(DEBUG) << "Calling onClosed"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 387 | mObserver->onClosed(); |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 388 | LOG(DEBUG) << "Ending loop"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | DnsTlsSocket::~DnsTlsSocket() { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 392 | LOG(DEBUG) << "Destructor"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 393 | // This will trigger an orderly shutdown in loop(). |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 394 | requestLoopShutdown(); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 395 | { |
| 396 | // Wait for the orderly shutdown to complete. |
| 397 | std::lock_guard guard(mLock); |
| 398 | if (mLoopThread && std::this_thread::get_id() == mLoopThread->get_id()) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 399 | LOG(ERROR) << "Violation of re-entrance precondition"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | } |
| 403 | if (mLoopThread) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 404 | LOG(DEBUG) << "Waiting for loop thread to terminate"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 405 | mLoopThread->join(); |
| 406 | mLoopThread.reset(); |
| 407 | } |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 408 | LOG(DEBUG) << "Destructor completed"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | bool DnsTlsSocket::query(uint16_t id, const Slice query) { |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 412 | // Compose the entire message in a single buffer, so that it can be |
| 413 | // sent as a single TLS record. |
| 414 | std::vector<uint8_t> buf(query.size() + 4); |
| 415 | // Write 2-byte length |
| 416 | uint16_t len = query.size() + 2; // + 2 for the ID. |
| 417 | buf[0] = len >> 8; |
| 418 | buf[1] = len; |
| 419 | // Write 2-byte ID |
| 420 | buf[2] = id >> 8; |
| 421 | buf[3] = id; |
| 422 | // Copy body |
| 423 | std::memcpy(buf.data() + 4, query.base(), query.size()); |
| 424 | |
| 425 | mQueue.push(std::move(buf)); |
| 426 | // Increment the mEventFd counter by 1. |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 427 | return incrementEventFd(1); |
| 428 | } |
| 429 | |
| 430 | void DnsTlsSocket::requestLoopShutdown() { |
Bernie Innocenti | 97ee109 | 2019-03-28 15:52:59 +0900 | [diff] [blame] | 431 | if (mEventFd != -1) { |
| 432 | // Write a negative number to the eventfd. This triggers an immediate shutdown. |
| 433 | incrementEventFd(INT64_MIN); |
| 434 | } |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | bool DnsTlsSocket::incrementEventFd(const int64_t count) { |
Bernie Innocenti | 97ee109 | 2019-03-28 15:52:59 +0900 | [diff] [blame] | 438 | if (mEventFd == -1) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 439 | LOG(ERROR) << "eventfd is not initialized"; |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 440 | return false; |
| 441 | } |
Bernie Innocenti | 97ee109 | 2019-03-28 15:52:59 +0900 | [diff] [blame] | 442 | ssize_t written = write(mEventFd.get(), &count, sizeof(count)); |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 443 | if (written != sizeof(count)) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 444 | LOG(ERROR) << "Failed to increment eventfd by " << count; |
Ben Schwartz | 62176fd | 2019-01-22 17:32:17 -0500 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | return true; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Read exactly len bytes into buffer or fail with an SSL error code |
| 451 | int DnsTlsSocket::sslRead(const Slice buffer, bool wait) { |
| 452 | size_t remaining = buffer.size(); |
| 453 | while (remaining > 0) { |
| 454 | int ret = SSL_read(mSsl.get(), buffer.limit() - remaining, remaining); |
| 455 | if (ret == 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 456 | if (remaining < buffer.size()) |
| 457 | LOG(WARNING) << "SSL closed with " << remaining << " of " << buffer.size() |
| 458 | << " bytes remaining"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 459 | return SSL_ERROR_ZERO_RETURN; |
| 460 | } |
| 461 | |
| 462 | if (ret < 0) { |
| 463 | const int ssl_err = SSL_get_error(mSsl.get(), ret); |
| 464 | if (wait && ssl_err == SSL_ERROR_WANT_READ) { |
| 465 | if (waitForReading(mSslFd.get()) != 1) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 466 | LOG(DEBUG) << "Poll failed in sslRead: " << errno; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 467 | return SSL_ERROR_SYSCALL; |
| 468 | } |
| 469 | continue; |
| 470 | } else { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 471 | LOG(DEBUG) << "SSL_read error " << ssl_err; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 472 | return ssl_err; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | remaining -= ret; |
| 477 | wait = true; // Once a read is started, try to finish. |
| 478 | } |
| 479 | return SSL_ERROR_NONE; |
| 480 | } |
| 481 | |
Ben Schwartz | 2187abe | 2019-01-10 14:30:46 -0500 | [diff] [blame] | 482 | bool DnsTlsSocket::sendQuery(const std::vector<uint8_t>& buf) { |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 483 | if (!sslWrite(netdutils::makeSlice(buf))) { |
| 484 | return false; |
| 485 | } |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 486 | LOG(DEBUG) << mMark << " SSL_write complete"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 487 | return true; |
| 488 | } |
| 489 | |
| 490 | bool DnsTlsSocket::readResponse() { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 491 | LOG(DEBUG) << "reading response"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 492 | uint8_t responseHeader[2]; |
| 493 | int err = sslRead(Slice(responseHeader, 2), false); |
| 494 | if (err == SSL_ERROR_WANT_READ) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 495 | LOG(DEBUG) << "Ignoring spurious wakeup from server"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 496 | return true; |
| 497 | } |
| 498 | if (err != SSL_ERROR_NONE) { |
| 499 | return false; |
| 500 | } |
| 501 | // Truncate responses larger than MAX_SIZE. This is safe because a DNS packet is |
| 502 | // always invalid when truncated, so the response will be treated as an error. |
| 503 | constexpr uint16_t MAX_SIZE = 8192; |
| 504 | const uint16_t responseSize = (responseHeader[0] << 8) | responseHeader[1]; |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 505 | LOG(DEBUG) << mMark << " Expecting response of size " << responseSize; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 506 | std::vector<uint8_t> response(std::min(responseSize, MAX_SIZE)); |
| 507 | if (sslRead(netdutils::makeSlice(response), true) != SSL_ERROR_NONE) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 508 | LOG(DEBUG) << mMark << " Failed to read " << response.size() << " bytes"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | uint16_t remainingBytes = responseSize - response.size(); |
| 512 | while (remainingBytes > 0) { |
| 513 | constexpr uint16_t CHUNK_SIZE = 2048; |
| 514 | std::vector<uint8_t> discard(std::min(remainingBytes, CHUNK_SIZE)); |
| 515 | if (sslRead(netdutils::makeSlice(discard), true) != SSL_ERROR_NONE) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 516 | LOG(DEBUG) << mMark << " Failed to discard " << discard.size() << " bytes"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | remainingBytes -= discard.size(); |
| 520 | } |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 521 | LOG(DEBUG) << mMark << " SSL_read complete"; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 522 | |
| 523 | mObserver->onResponse(std::move(response)); |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | } // end of namespace net |
| 528 | } // end of namespace android |