blob: 5fe4cbec77df9e7b26786ea92e6ec3cd18bce9e1 [file] [log] [blame]
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -07001/*
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 Ramachandranf4cfad32014-05-21 08:54:07 -070020#include "FwmarkCommand.h"
Michal Karpinski7d374532016-10-06 19:33:55 +010021#include "NetdConstants.h"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070022#include "NetworkController.h"
Chenbo Feng9944ba82017-10-10 17:33:20 -070023#include "TrafficController.h"
Sreeram Ramachandranec008842014-05-21 14:01:16 -070024#include "resolv_netid.h"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070025
Hugo Benichi456c9062017-01-04 12:19:16 +090026#include <netinet/in.h>
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070027#include <sys/socket.h>
28#include <unistd.h>
Michal Karpinski7d374532016-10-06 19:33:55 +010029#include <utils/String16.h>
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070030
Chenbo Feng9944ba82017-10-10 17:33:20 -070031#include <binder/IServiceManager.h>
32
Michal Karpinski7d374532016-10-06 19:33:55 +010033using android::String16;
34using android::net::metrics::INetdEventListener;
35
Lorenzo Colitti7035f222017-02-13 18:29:00 +090036namespace android {
37namespace net {
38
Chenbo Feng9944ba82017-10-10 17:33:20 -070039const char UPDATE_DEVICE_STATS[] = "android.permission.UPDATE_DEVICE_STATS";
40
41bool hasUpdateDeviceStatsPermission(SocketClient* client) {
42 return checkPermission(String16(UPDATE_DEVICE_STATS), client->getPid(), client->getUid());
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070043}
44
Chenbo Feng9944ba82017-10-10 17:33:20 -070045FwmarkServer::FwmarkServer(NetworkController* networkController, EventReporter* eventReporter,
46 TrafficController* trafficCtrl)
47 : SocketListener(SOCKET_NAME, true),
48 mNetworkController(networkController),
49 mEventReporter(eventReporter),
50 mTrafficCtrl(trafficCtrl) {}
51
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070052bool FwmarkServer::onDataAvailable(SocketClient* client) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070053 int socketFd = -1;
54 int error = processClient(client, &socketFd);
55 if (socketFd >= 0) {
56 close(socketFd);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070057 }
58
59 // Always send a response even if there were connection errors or read errors, so that we don't
60 // inadvertently cause the client to hang (which always waits for a response).
61 client->sendData(&error, sizeof(error));
62
63 // Always close the client connection (by returning false). This prevents a DoS attack where
64 // the client issues multiple commands on the same connection, never reading the responses,
65 // causing its receive buffer to fill up, and thus causing our client->sendData() to block.
66 return false;
67}
68
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070069int FwmarkServer::processClient(SocketClient* client, int* socketFd) {
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070070 FwmarkCommand command;
Michal Karpinski7d374532016-10-06 19:33:55 +010071 FwmarkConnectInfo connectInfo;
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070072
Michal Karpinski7d374532016-10-06 19:33:55 +010073 iovec iov[2] = {
74 { &command, sizeof(command) },
75 { &connectInfo, sizeof(connectInfo) },
76 };
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070077 msghdr message;
78 memset(&message, 0, sizeof(message));
Michal Karpinski7d374532016-10-06 19:33:55 +010079 message.msg_iov = iov;
80 message.msg_iovlen = ARRAY_SIZE(iov);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070081
82 union {
83 cmsghdr cmh;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070084 char cmsg[CMSG_SPACE(sizeof(*socketFd))];
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070085 } cmsgu;
86
87 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
88 message.msg_control = cmsgu.cmsg;
89 message.msg_controllen = sizeof(cmsgu.cmsg);
90
Nick Kralevich9384f232016-12-20 06:51:32 -080091 int messageLength = TEMP_FAILURE_RETRY(recvmsg(client->getSocket(), &message, MSG_CMSG_CLOEXEC));
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070092 if (messageLength <= 0) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070093 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070094 }
95
Michal Karpinski7d374532016-10-06 19:33:55 +010096 if (!((command.cmdId != FwmarkCommand::ON_CONNECT_COMPLETE && messageLength == sizeof(command))
97 || (command.cmdId == FwmarkCommand::ON_CONNECT_COMPLETE
98 && messageLength == sizeof(command) + sizeof(connectInfo)))) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070099 return -EBADMSG;
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700100 }
101
Paul Jensend1df5972015-05-06 07:29:56 -0400102 Permission permission = mNetworkController->getPermissionForUser(client->getUid());
103
104 if (command.cmdId == FwmarkCommand::QUERY_USER_ACCESS) {
105 if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) {
106 return -EPERM;
107 }
108 return mNetworkController->checkUserNetworkAccess(command.uid, command.netId);
109 }
110
Chenbo Feng9944ba82017-10-10 17:33:20 -0700111 if (command.cmdId == FwmarkCommand::SET_COUNTERSET) {
112 if (!hasUpdateDeviceStatsPermission(client)) {
113 return -EPERM;
114 }
115 return mTrafficCtrl->setCounterSet(command.trafficCtrlInfo, command.uid);
116 }
117
118 if (command.cmdId == FwmarkCommand::DELETE_TAGDATA) {
119 if (!hasUpdateDeviceStatsPermission(client)) {
120 return -EPERM;
121 }
122 return mTrafficCtrl->deleteTagData(command.trafficCtrlInfo, command.uid);
123 }
124
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700125 cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
126 if (cmsgh && cmsgh->cmsg_level == SOL_SOCKET && cmsgh->cmsg_type == SCM_RIGHTS &&
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700127 cmsgh->cmsg_len == CMSG_LEN(sizeof(*socketFd))) {
128 memcpy(socketFd, CMSG_DATA(cmsgh), sizeof(*socketFd));
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700129 }
130
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700131 if (*socketFd < 0) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700132 return -EBADF;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700133 }
134
135 Fwmark fwmark;
Sreeram Ramachandran5ff58d42014-05-14 09:57:31 -0700136 socklen_t fwmarkLen = sizeof(fwmark.intValue);
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700137 if (getsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700138 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700139 }
140
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700141 switch (command.cmdId) {
142 case FwmarkCommand::ON_ACCEPT: {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700143 // Called after a socket accept(). The kernel would've marked the NetId and necessary
Sreeram Ramachandrana675b002014-07-05 11:00:55 -0700144 // permissions bits, so we just add the rest of the user's permissions here.
145 permission = static_cast<Permission>(permission | fwmark.permission);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700146 break;
147 }
148
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700149 case FwmarkCommand::ON_CONNECT: {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700150 // Called before a socket connect() happens. Set an appropriate NetId into the fwmark so
151 // that the socket routes consistently over that network. Do this even if the socket
152 // already has a NetId, so that calling connect() multiple times still works.
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700153 //
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700154 // But if the explicit bit was set, the existing NetId was explicitly preferred (and not
155 // a case of connect() being called multiple times). Don't reset the NetId in that case.
156 //
157 // An "appropriate" NetId is the NetId of a bypassable VPN that applies to the user, or
158 // failing that, the default network. We'll never set the NetId of a secure VPN here.
159 // See the comments in the implementation of getNetworkForConnect() for more details.
160 //
161 // If the protect bit is set, this could be either a system proxy (e.g.: the dns proxy
162 // or the download manager) acting on behalf of another user, or a VPN provider. If it's
163 // a proxy, we shouldn't reset the NetId. If it's a VPN provider, we should set the
164 // default network's NetId.
165 //
166 // There's no easy way to tell the difference between a proxy and a VPN app. We can't
167 // use PERMISSION_SYSTEM to identify the proxy because a VPN app may also have those
168 // permissions. So we use the following heuristic:
169 //
170 // If it's a proxy, but the existing NetId is not a VPN, that means the user (that the
171 // proxy is acting on behalf of) is not subject to a VPN, so the proxy must have picked
172 // the default network's NetId. So, it's okay to replace that with the current default
173 // network's NetId (which in all likelihood is the same).
174 //
175 // Conversely, if it's a VPN provider, the existing NetId cannot be a VPN. The only time
176 // we set a VPN's NetId into a socket without setting the explicit bit is here, in
177 // ON_CONNECT, but we won't do that if the socket has the protect bit set. If the VPN
178 // provider connect()ed (and got the VPN NetId set) and then called protect(), we
179 // would've unset the NetId in PROTECT_FROM_VPN below.
180 //
181 // So, overall (when the explicit bit is not set but the protect bit is set), if the
182 // existing NetId is a VPN, don't reset it. Else, set the default network's NetId.
183 if (!fwmark.explicitlySelected) {
184 if (!fwmark.protectedFromVpn) {
185 fwmark.netId = mNetworkController->getNetworkForConnect(client->getUid());
186 } else if (!mNetworkController->isVirtualNetwork(fwmark.netId)) {
187 fwmark.netId = mNetworkController->getDefaultNetwork();
188 }
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700189 }
190 break;
191 }
192
Michal Karpinski7d374532016-10-06 19:33:55 +0100193 case FwmarkCommand::ON_CONNECT_COMPLETE: {
194 // Called after a socket connect() completes.
195 // This reports connect event including netId, destination IP address, destination port,
Hugo Benichi7dfaa782016-10-31 15:07:23 +0900196 // uid, connect latency, and connect errno if any.
Hugo Benichi456c9062017-01-04 12:19:16 +0900197
198 // Skip reporting if connect() happened on a UDP socket.
199 int socketProto;
200 socklen_t intSize = sizeof(socketProto);
201 const int ret = getsockopt(*socketFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &intSize);
202 if ((ret != 0) || (socketProto == IPPROTO_UDP)) {
203 break;
204 }
205
Michal Karpinski7d374532016-10-06 19:33:55 +0100206 android::sp<android::net::metrics::INetdEventListener> netdEventListener =
207 mEventReporter->getNetdEventListener();
208
209 if (netdEventListener != nullptr) {
210 char addrstr[INET6_ADDRSTRLEN];
211 char portstr[sizeof("65536")];
212 const int ret = getnameinfo((sockaddr*) &connectInfo.addr, sizeof(connectInfo.addr),
213 addrstr, sizeof(addrstr), portstr, sizeof(portstr),
214 NI_NUMERICHOST | NI_NUMERICSERV);
215
Hugo Benichi7dfaa782016-10-31 15:07:23 +0900216 netdEventListener->onConnectEvent(fwmark.netId, connectInfo.error,
217 connectInfo.latencyMs,
Michal Karpinski7d374532016-10-06 19:33:55 +0100218 (ret == 0) ? String16(addrstr) : String16(""),
219 (ret == 0) ? strtoul(portstr, NULL, 10) : 0, client->getUid());
220 }
221 break;
222 }
223
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700224 case FwmarkCommand::SELECT_NETWORK: {
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700225 fwmark.netId = command.netId;
226 if (command.netId == NETID_UNSET) {
227 fwmark.explicitlySelected = false;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700228 fwmark.protectedFromVpn = false;
229 permission = PERMISSION_NONE;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900230 } else {
231 if (int ret = mNetworkController->checkUserNetworkAccess(client->getUid(),
232 command.netId)) {
233 return ret;
234 }
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700235 fwmark.explicitlySelected = true;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700236 fwmark.protectedFromVpn = mNetworkController->canProtect(client->getUid());
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700237 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700238 break;
239 }
240
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700241 case FwmarkCommand::PROTECT_FROM_VPN: {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700242 if (!mNetworkController->canProtect(client->getUid())) {
243 return -EPERM;
244 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700245 // If a bypassable VPN's provider app calls connect() and then protect(), it will end up
246 // with a socket that looks like that of a system proxy but is not (see comments for
247 // ON_CONNECT above). So, reset the NetId.
248 //
249 // In any case, it's appropriate that if the socket has an implicit VPN NetId mark, the
250 // PROTECT_FROM_VPN command should unset it.
251 if (!fwmark.explicitlySelected && mNetworkController->isVirtualNetwork(fwmark.netId)) {
252 fwmark.netId = mNetworkController->getDefaultNetwork();
253 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700254 fwmark.protectedFromVpn = true;
255 permission = static_cast<Permission>(permission | fwmark.permission);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700256 break;
257 }
258
Sreeram Ramachandrana69d9472014-07-11 16:27:02 -0700259 case FwmarkCommand::SELECT_FOR_USER: {
260 if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) {
261 return -EPERM;
262 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700263 fwmark.netId = mNetworkController->getNetworkForUser(command.uid);
Sreeram Ramachandrana69d9472014-07-11 16:27:02 -0700264 fwmark.protectedFromVpn = true;
265 break;
266 }
267
Chenbo Feng9944ba82017-10-10 17:33:20 -0700268 case FwmarkCommand::TAG_SOCKET: {
269 // If the UID is -1, tag as the caller's UID:
270 // - TrafficStats and NetworkManagementSocketTagger use -1 to indicate "use the
271 // caller's UID".
272 // - xt_qtaguid will see -1 on the command line, fail to parse it as a uint32_t, and
273 // fall back to current_fsuid().
274 if (static_cast<int>(command.uid) == -1) {
275 command.uid = client->getUid();
276 }
277 if (command.uid != client->getUid() && !hasUpdateDeviceStatsPermission(client)) {
278 return -EPERM;
279 }
280 return mTrafficCtrl->tagSocket(*socketFd, command.trafficCtrlInfo, command.uid);
281 }
282
283 case FwmarkCommand::UNTAG_SOCKET: {
284 // Any process can untag a socket it has an fd for.
285 return mTrafficCtrl->untagSocket(*socketFd);
286 }
287
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700288 default: {
289 // unknown command
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700290 return -EPROTO;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700291 }
292 }
293
Sreeram Ramachandrana675b002014-07-05 11:00:55 -0700294 fwmark.permission = permission;
295
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700296 if (setsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue,
297 sizeof(fwmark.intValue)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700298 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700299 }
300
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700301 return 0;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700302}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900303
304} // namespace net
305} // namespace android