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