Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | #include "FwmarkServer.h" |
| 18 | |
| 19 | #include "Fwmark.h" |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 20 | #include "FwmarkCommand.h" |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 21 | #include "NetdConstants.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 22 | #include "NetworkController.h" |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 23 | #include "resolv_netid.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 24 | |
Hugo Benichi | 456c906 | 2017-01-04 12:19:16 +0900 | [diff] [blame] | 25 | #include <netinet/in.h> |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 26 | #include <sys/socket.h> |
| 27 | #include <unistd.h> |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 28 | #include <utils/String16.h> |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 29 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 30 | using android::String16; |
| 31 | using android::net::metrics::INetdEventListener; |
| 32 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame^] | 33 | namespace android { |
| 34 | namespace net { |
| 35 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 36 | FwmarkServer::FwmarkServer(NetworkController* networkController, EventReporter* eventReporter) : |
| 37 | SocketListener("fwmarkd", true), mNetworkController(networkController), |
| 38 | mEventReporter(eventReporter) { |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool FwmarkServer::onDataAvailable(SocketClient* client) { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 42 | int socketFd = -1; |
| 43 | int error = processClient(client, &socketFd); |
| 44 | if (socketFd >= 0) { |
| 45 | close(socketFd); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Always send a response even if there were connection errors or read errors, so that we don't |
| 49 | // inadvertently cause the client to hang (which always waits for a response). |
| 50 | client->sendData(&error, sizeof(error)); |
| 51 | |
| 52 | // Always close the client connection (by returning false). This prevents a DoS attack where |
| 53 | // the client issues multiple commands on the same connection, never reading the responses, |
| 54 | // causing its receive buffer to fill up, and thus causing our client->sendData() to block. |
| 55 | return false; |
| 56 | } |
| 57 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 58 | int FwmarkServer::processClient(SocketClient* client, int* socketFd) { |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 59 | FwmarkCommand command; |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 60 | FwmarkConnectInfo connectInfo; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 61 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 62 | iovec iov[2] = { |
| 63 | { &command, sizeof(command) }, |
| 64 | { &connectInfo, sizeof(connectInfo) }, |
| 65 | }; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 66 | msghdr message; |
| 67 | memset(&message, 0, sizeof(message)); |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 68 | message.msg_iov = iov; |
| 69 | message.msg_iovlen = ARRAY_SIZE(iov); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 70 | |
| 71 | union { |
| 72 | cmsghdr cmh; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 73 | char cmsg[CMSG_SPACE(sizeof(*socketFd))]; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 74 | } cmsgu; |
| 75 | |
| 76 | memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg)); |
| 77 | message.msg_control = cmsgu.cmsg; |
| 78 | message.msg_controllen = sizeof(cmsgu.cmsg); |
| 79 | |
Nick Kralevich | 9384f23 | 2016-12-20 06:51:32 -0800 | [diff] [blame] | 80 | int messageLength = TEMP_FAILURE_RETRY(recvmsg(client->getSocket(), &message, MSG_CMSG_CLOEXEC)); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 81 | if (messageLength <= 0) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 82 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 85 | if (!((command.cmdId != FwmarkCommand::ON_CONNECT_COMPLETE && messageLength == sizeof(command)) |
| 86 | || (command.cmdId == FwmarkCommand::ON_CONNECT_COMPLETE |
| 87 | && messageLength == sizeof(command) + sizeof(connectInfo)))) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 88 | return -EBADMSG; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 91 | Permission permission = mNetworkController->getPermissionForUser(client->getUid()); |
| 92 | |
| 93 | if (command.cmdId == FwmarkCommand::QUERY_USER_ACCESS) { |
| 94 | if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) { |
| 95 | return -EPERM; |
| 96 | } |
| 97 | return mNetworkController->checkUserNetworkAccess(command.uid, command.netId); |
| 98 | } |
| 99 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 100 | cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message); |
| 101 | if (cmsgh && cmsgh->cmsg_level == SOL_SOCKET && cmsgh->cmsg_type == SCM_RIGHTS && |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 102 | cmsgh->cmsg_len == CMSG_LEN(sizeof(*socketFd))) { |
| 103 | memcpy(socketFd, CMSG_DATA(cmsgh), sizeof(*socketFd)); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 106 | if (*socketFd < 0) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 107 | return -EBADF; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | Fwmark fwmark; |
Sreeram Ramachandran | 5ff58d4 | 2014-05-14 09:57:31 -0700 | [diff] [blame] | 111 | socklen_t fwmarkLen = sizeof(fwmark.intValue); |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 112 | if (getsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 113 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 116 | switch (command.cmdId) { |
| 117 | case FwmarkCommand::ON_ACCEPT: { |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 118 | // Called after a socket accept(). The kernel would've marked the NetId and necessary |
Sreeram Ramachandran | a675b00 | 2014-07-05 11:00:55 -0700 | [diff] [blame] | 119 | // permissions bits, so we just add the rest of the user's permissions here. |
| 120 | permission = static_cast<Permission>(permission | fwmark.permission); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 124 | case FwmarkCommand::ON_CONNECT: { |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 125 | // Called before a socket connect() happens. Set an appropriate NetId into the fwmark so |
| 126 | // that the socket routes consistently over that network. Do this even if the socket |
| 127 | // already has a NetId, so that calling connect() multiple times still works. |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 128 | // |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 129 | // But if the explicit bit was set, the existing NetId was explicitly preferred (and not |
| 130 | // a case of connect() being called multiple times). Don't reset the NetId in that case. |
| 131 | // |
| 132 | // An "appropriate" NetId is the NetId of a bypassable VPN that applies to the user, or |
| 133 | // failing that, the default network. We'll never set the NetId of a secure VPN here. |
| 134 | // See the comments in the implementation of getNetworkForConnect() for more details. |
| 135 | // |
| 136 | // If the protect bit is set, this could be either a system proxy (e.g.: the dns proxy |
| 137 | // or the download manager) acting on behalf of another user, or a VPN provider. If it's |
| 138 | // a proxy, we shouldn't reset the NetId. If it's a VPN provider, we should set the |
| 139 | // default network's NetId. |
| 140 | // |
| 141 | // There's no easy way to tell the difference between a proxy and a VPN app. We can't |
| 142 | // use PERMISSION_SYSTEM to identify the proxy because a VPN app may also have those |
| 143 | // permissions. So we use the following heuristic: |
| 144 | // |
| 145 | // If it's a proxy, but the existing NetId is not a VPN, that means the user (that the |
| 146 | // proxy is acting on behalf of) is not subject to a VPN, so the proxy must have picked |
| 147 | // the default network's NetId. So, it's okay to replace that with the current default |
| 148 | // network's NetId (which in all likelihood is the same). |
| 149 | // |
| 150 | // Conversely, if it's a VPN provider, the existing NetId cannot be a VPN. The only time |
| 151 | // we set a VPN's NetId into a socket without setting the explicit bit is here, in |
| 152 | // ON_CONNECT, but we won't do that if the socket has the protect bit set. If the VPN |
| 153 | // provider connect()ed (and got the VPN NetId set) and then called protect(), we |
| 154 | // would've unset the NetId in PROTECT_FROM_VPN below. |
| 155 | // |
| 156 | // So, overall (when the explicit bit is not set but the protect bit is set), if the |
| 157 | // existing NetId is a VPN, don't reset it. Else, set the default network's NetId. |
| 158 | if (!fwmark.explicitlySelected) { |
| 159 | if (!fwmark.protectedFromVpn) { |
| 160 | fwmark.netId = mNetworkController->getNetworkForConnect(client->getUid()); |
| 161 | } else if (!mNetworkController->isVirtualNetwork(fwmark.netId)) { |
| 162 | fwmark.netId = mNetworkController->getDefaultNetwork(); |
| 163 | } |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 164 | } |
| 165 | break; |
| 166 | } |
| 167 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 168 | case FwmarkCommand::ON_CONNECT_COMPLETE: { |
| 169 | // Called after a socket connect() completes. |
| 170 | // This reports connect event including netId, destination IP address, destination port, |
Hugo Benichi | 7dfaa78 | 2016-10-31 15:07:23 +0900 | [diff] [blame] | 171 | // uid, connect latency, and connect errno if any. |
Hugo Benichi | 456c906 | 2017-01-04 12:19:16 +0900 | [diff] [blame] | 172 | |
| 173 | // Skip reporting if connect() happened on a UDP socket. |
| 174 | int socketProto; |
| 175 | socklen_t intSize = sizeof(socketProto); |
| 176 | const int ret = getsockopt(*socketFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &intSize); |
| 177 | if ((ret != 0) || (socketProto == IPPROTO_UDP)) { |
| 178 | break; |
| 179 | } |
| 180 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 181 | android::sp<android::net::metrics::INetdEventListener> netdEventListener = |
| 182 | mEventReporter->getNetdEventListener(); |
| 183 | |
| 184 | if (netdEventListener != nullptr) { |
| 185 | char addrstr[INET6_ADDRSTRLEN]; |
| 186 | char portstr[sizeof("65536")]; |
| 187 | const int ret = getnameinfo((sockaddr*) &connectInfo.addr, sizeof(connectInfo.addr), |
| 188 | addrstr, sizeof(addrstr), portstr, sizeof(portstr), |
| 189 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 190 | |
Hugo Benichi | 7dfaa78 | 2016-10-31 15:07:23 +0900 | [diff] [blame] | 191 | netdEventListener->onConnectEvent(fwmark.netId, connectInfo.error, |
| 192 | connectInfo.latencyMs, |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 193 | (ret == 0) ? String16(addrstr) : String16(""), |
| 194 | (ret == 0) ? strtoul(portstr, NULL, 10) : 0, client->getUid()); |
| 195 | } |
| 196 | break; |
| 197 | } |
| 198 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 199 | case FwmarkCommand::SELECT_NETWORK: { |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 200 | fwmark.netId = command.netId; |
| 201 | if (command.netId == NETID_UNSET) { |
| 202 | fwmark.explicitlySelected = false; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 203 | fwmark.protectedFromVpn = false; |
| 204 | permission = PERMISSION_NONE; |
Lorenzo Colitti | a1067c8 | 2014-10-02 22:47:41 +0900 | [diff] [blame] | 205 | } else { |
| 206 | if (int ret = mNetworkController->checkUserNetworkAccess(client->getUid(), |
| 207 | command.netId)) { |
| 208 | return ret; |
| 209 | } |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 210 | fwmark.explicitlySelected = true; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 211 | fwmark.protectedFromVpn = mNetworkController->canProtect(client->getUid()); |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 212 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 216 | case FwmarkCommand::PROTECT_FROM_VPN: { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 217 | if (!mNetworkController->canProtect(client->getUid())) { |
| 218 | return -EPERM; |
| 219 | } |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 220 | // If a bypassable VPN's provider app calls connect() and then protect(), it will end up |
| 221 | // with a socket that looks like that of a system proxy but is not (see comments for |
| 222 | // ON_CONNECT above). So, reset the NetId. |
| 223 | // |
| 224 | // In any case, it's appropriate that if the socket has an implicit VPN NetId mark, the |
| 225 | // PROTECT_FROM_VPN command should unset it. |
| 226 | if (!fwmark.explicitlySelected && mNetworkController->isVirtualNetwork(fwmark.netId)) { |
| 227 | fwmark.netId = mNetworkController->getDefaultNetwork(); |
| 228 | } |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 229 | fwmark.protectedFromVpn = true; |
| 230 | permission = static_cast<Permission>(permission | fwmark.permission); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 234 | case FwmarkCommand::SELECT_FOR_USER: { |
| 235 | if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) { |
| 236 | return -EPERM; |
| 237 | } |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 238 | fwmark.netId = mNetworkController->getNetworkForUser(command.uid); |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 239 | fwmark.protectedFromVpn = true; |
| 240 | break; |
| 241 | } |
| 242 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 243 | default: { |
| 244 | // unknown command |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 245 | return -EPROTO; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Sreeram Ramachandran | a675b00 | 2014-07-05 11:00:55 -0700 | [diff] [blame] | 249 | fwmark.permission = permission; |
| 250 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 251 | if (setsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, |
| 252 | sizeof(fwmark.intValue)) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 253 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 256 | return 0; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 257 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame^] | 258 | |
| 259 | } // namespace net |
| 260 | } // namespace android |