blob: dcbdb5d3307346ad002a682f43777774543f5b1b [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>
Lorenzo Colitti09175be2017-11-17 14:01:21 +090027#include <selinux/selinux.h>
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070028#include <sys/socket.h>
29#include <unistd.h>
Michal Karpinski7d374532016-10-06 19:33:55 +010030#include <utils/String16.h>
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070031
Chenbo Feng9944ba82017-10-10 17:33:20 -070032#include <binder/IServiceManager.h>
33
Michal Karpinski7d374532016-10-06 19:33:55 +010034using android::String16;
35using android::net::metrics::INetdEventListener;
36
Lorenzo Colitti7035f222017-02-13 18:29:00 +090037namespace android {
38namespace net {
39
Lorenzo Colitti09175be2017-11-17 14:01:21 +090040constexpr const char *UPDATE_DEVICE_STATS = "android.permission.UPDATE_DEVICE_STATS";
41constexpr const char *SYSTEM_SERVER_CONTEXT = "u:r:system_server:s0";
42
43bool isSystemServer(SocketClient* client) {
44 if (client->getUid() != AID_SYSTEM) {
45 return false;
46 }
47
48 char *context;
49 if (getpeercon(client->getSocket(), &context)) {
50 return false;
51 }
52
53 // We can't use context_new and context_type_get as they're private to libselinux. So just do
54 // a string match instead.
55 bool ret = !strcmp(context, SYSTEM_SERVER_CONTEXT);
56 freecon(context);
57
58 return ret;
59}
Chenbo Feng9944ba82017-10-10 17:33:20 -070060
61bool hasUpdateDeviceStatsPermission(SocketClient* client) {
Lorenzo Colitti09175be2017-11-17 14:01:21 +090062 // If the caller is the system server, allow without any further checks.
63 // Otherwise, if the system server's binder thread pool is full, and all the threads are
64 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
65 return isSystemServer(client) ||
66 checkPermission(String16(UPDATE_DEVICE_STATS), client->getPid(), client->getUid());
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070067}
68
Chenbo Feng9944ba82017-10-10 17:33:20 -070069FwmarkServer::FwmarkServer(NetworkController* networkController, EventReporter* eventReporter,
70 TrafficController* trafficCtrl)
71 : SocketListener(SOCKET_NAME, true),
72 mNetworkController(networkController),
73 mEventReporter(eventReporter),
74 mTrafficCtrl(trafficCtrl) {}
75
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070076bool FwmarkServer::onDataAvailable(SocketClient* client) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070077 int socketFd = -1;
78 int error = processClient(client, &socketFd);
79 if (socketFd >= 0) {
80 close(socketFd);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070081 }
82
83 // Always send a response even if there were connection errors or read errors, so that we don't
84 // inadvertently cause the client to hang (which always waits for a response).
85 client->sendData(&error, sizeof(error));
86
87 // Always close the client connection (by returning false). This prevents a DoS attack where
88 // the client issues multiple commands on the same connection, never reading the responses,
89 // causing its receive buffer to fill up, and thus causing our client->sendData() to block.
90 return false;
91}
92
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070093int FwmarkServer::processClient(SocketClient* client, int* socketFd) {
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070094 FwmarkCommand command;
Michal Karpinski7d374532016-10-06 19:33:55 +010095 FwmarkConnectInfo connectInfo;
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070096
Michal Karpinski7d374532016-10-06 19:33:55 +010097 iovec iov[2] = {
98 { &command, sizeof(command) },
99 { &connectInfo, sizeof(connectInfo) },
100 };
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700101 msghdr message;
102 memset(&message, 0, sizeof(message));
Michal Karpinski7d374532016-10-06 19:33:55 +0100103 message.msg_iov = iov;
104 message.msg_iovlen = ARRAY_SIZE(iov);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700105
106 union {
107 cmsghdr cmh;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700108 char cmsg[CMSG_SPACE(sizeof(*socketFd))];
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700109 } cmsgu;
110
111 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
112 message.msg_control = cmsgu.cmsg;
113 message.msg_controllen = sizeof(cmsgu.cmsg);
114
Nick Kralevich9384f232016-12-20 06:51:32 -0800115 int messageLength = TEMP_FAILURE_RETRY(recvmsg(client->getSocket(), &message, MSG_CMSG_CLOEXEC));
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700116 if (messageLength <= 0) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700117 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700118 }
119
Michal Karpinski7d374532016-10-06 19:33:55 +0100120 if (!((command.cmdId != FwmarkCommand::ON_CONNECT_COMPLETE && messageLength == sizeof(command))
121 || (command.cmdId == FwmarkCommand::ON_CONNECT_COMPLETE
122 && messageLength == sizeof(command) + sizeof(connectInfo)))) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700123 return -EBADMSG;
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700124 }
125
Paul Jensend1df5972015-05-06 07:29:56 -0400126 Permission permission = mNetworkController->getPermissionForUser(client->getUid());
127
128 if (command.cmdId == FwmarkCommand::QUERY_USER_ACCESS) {
129 if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) {
130 return -EPERM;
131 }
132 return mNetworkController->checkUserNetworkAccess(command.uid, command.netId);
133 }
134
Chenbo Feng9944ba82017-10-10 17:33:20 -0700135 if (command.cmdId == FwmarkCommand::SET_COUNTERSET) {
136 if (!hasUpdateDeviceStatsPermission(client)) {
137 return -EPERM;
138 }
139 return mTrafficCtrl->setCounterSet(command.trafficCtrlInfo, command.uid);
140 }
141
142 if (command.cmdId == FwmarkCommand::DELETE_TAGDATA) {
143 if (!hasUpdateDeviceStatsPermission(client)) {
144 return -EPERM;
145 }
146 return mTrafficCtrl->deleteTagData(command.trafficCtrlInfo, command.uid);
147 }
148
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700149 cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
150 if (cmsgh && cmsgh->cmsg_level == SOL_SOCKET && cmsgh->cmsg_type == SCM_RIGHTS &&
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700151 cmsgh->cmsg_len == CMSG_LEN(sizeof(*socketFd))) {
152 memcpy(socketFd, CMSG_DATA(cmsgh), sizeof(*socketFd));
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700153 }
154
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700155 if (*socketFd < 0) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700156 return -EBADF;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700157 }
158
Remi NGUYEN VANeabc5da2018-04-24 18:54:58 +0900159 int family;
160 socklen_t familyLen = sizeof(family);
161 if (getsockopt(*socketFd, SOL_SOCKET, SO_DOMAIN, &family, &familyLen) == -1) {
162 return -errno;
163 }
164 if (!FwmarkCommand::isSupportedFamily(family)) {
165 return -EAFNOSUPPORT;
166 }
167
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700168 Fwmark fwmark;
Sreeram Ramachandran5ff58d42014-05-14 09:57:31 -0700169 socklen_t fwmarkLen = sizeof(fwmark.intValue);
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700170 if (getsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700171 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700172 }
173
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700174 switch (command.cmdId) {
175 case FwmarkCommand::ON_ACCEPT: {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700176 // Called after a socket accept(). The kernel would've marked the NetId and necessary
Sreeram Ramachandrana675b002014-07-05 11:00:55 -0700177 // permissions bits, so we just add the rest of the user's permissions here.
178 permission = static_cast<Permission>(permission | fwmark.permission);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700179 break;
180 }
181
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700182 case FwmarkCommand::ON_CONNECT: {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700183 // Called before a socket connect() happens. Set an appropriate NetId into the fwmark so
184 // that the socket routes consistently over that network. Do this even if the socket
185 // already has a NetId, so that calling connect() multiple times still works.
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700186 //
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700187 // But if the explicit bit was set, the existing NetId was explicitly preferred (and not
188 // a case of connect() being called multiple times). Don't reset the NetId in that case.
189 //
190 // An "appropriate" NetId is the NetId of a bypassable VPN that applies to the user, or
191 // failing that, the default network. We'll never set the NetId of a secure VPN here.
192 // See the comments in the implementation of getNetworkForConnect() for more details.
193 //
194 // If the protect bit is set, this could be either a system proxy (e.g.: the dns proxy
195 // or the download manager) acting on behalf of another user, or a VPN provider. If it's
196 // a proxy, we shouldn't reset the NetId. If it's a VPN provider, we should set the
197 // default network's NetId.
198 //
199 // There's no easy way to tell the difference between a proxy and a VPN app. We can't
200 // use PERMISSION_SYSTEM to identify the proxy because a VPN app may also have those
201 // permissions. So we use the following heuristic:
202 //
203 // If it's a proxy, but the existing NetId is not a VPN, that means the user (that the
204 // proxy is acting on behalf of) is not subject to a VPN, so the proxy must have picked
205 // the default network's NetId. So, it's okay to replace that with the current default
206 // network's NetId (which in all likelihood is the same).
207 //
208 // Conversely, if it's a VPN provider, the existing NetId cannot be a VPN. The only time
209 // we set a VPN's NetId into a socket without setting the explicit bit is here, in
210 // ON_CONNECT, but we won't do that if the socket has the protect bit set. If the VPN
211 // provider connect()ed (and got the VPN NetId set) and then called protect(), we
212 // would've unset the NetId in PROTECT_FROM_VPN below.
213 //
214 // So, overall (when the explicit bit is not set but the protect bit is set), if the
215 // existing NetId is a VPN, don't reset it. Else, set the default network's NetId.
216 if (!fwmark.explicitlySelected) {
217 if (!fwmark.protectedFromVpn) {
218 fwmark.netId = mNetworkController->getNetworkForConnect(client->getUid());
219 } else if (!mNetworkController->isVirtualNetwork(fwmark.netId)) {
220 fwmark.netId = mNetworkController->getDefaultNetwork();
221 }
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700222 }
223 break;
224 }
225
Michal Karpinski7d374532016-10-06 19:33:55 +0100226 case FwmarkCommand::ON_CONNECT_COMPLETE: {
227 // Called after a socket connect() completes.
228 // This reports connect event including netId, destination IP address, destination port,
Hugo Benichi7dfaa782016-10-31 15:07:23 +0900229 // uid, connect latency, and connect errno if any.
Hugo Benichi456c9062017-01-04 12:19:16 +0900230
231 // Skip reporting if connect() happened on a UDP socket.
232 int socketProto;
233 socklen_t intSize = sizeof(socketProto);
234 const int ret = getsockopt(*socketFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &intSize);
235 if ((ret != 0) || (socketProto == IPPROTO_UDP)) {
236 break;
237 }
238
Michal Karpinski7d374532016-10-06 19:33:55 +0100239 android::sp<android::net::metrics::INetdEventListener> netdEventListener =
240 mEventReporter->getNetdEventListener();
241
242 if (netdEventListener != nullptr) {
243 char addrstr[INET6_ADDRSTRLEN];
244 char portstr[sizeof("65536")];
245 const int ret = getnameinfo((sockaddr*) &connectInfo.addr, sizeof(connectInfo.addr),
246 addrstr, sizeof(addrstr), portstr, sizeof(portstr),
247 NI_NUMERICHOST | NI_NUMERICSERV);
248
Hugo Benichi7dfaa782016-10-31 15:07:23 +0900249 netdEventListener->onConnectEvent(fwmark.netId, connectInfo.error,
250 connectInfo.latencyMs,
Michal Karpinski7d374532016-10-06 19:33:55 +0100251 (ret == 0) ? String16(addrstr) : String16(""),
Yi Kongbdfd57e2018-07-25 13:26:10 -0700252 (ret == 0) ? strtoul(portstr, nullptr, 10) : 0, client->getUid());
Michal Karpinski7d374532016-10-06 19:33:55 +0100253 }
254 break;
255 }
256
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700257 case FwmarkCommand::SELECT_NETWORK: {
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700258 fwmark.netId = command.netId;
259 if (command.netId == NETID_UNSET) {
260 fwmark.explicitlySelected = false;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700261 fwmark.protectedFromVpn = false;
262 permission = PERMISSION_NONE;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900263 } else {
264 if (int ret = mNetworkController->checkUserNetworkAccess(client->getUid(),
265 command.netId)) {
266 return ret;
267 }
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700268 fwmark.explicitlySelected = true;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700269 fwmark.protectedFromVpn = mNetworkController->canProtect(client->getUid());
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700270 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700271 break;
272 }
273
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700274 case FwmarkCommand::PROTECT_FROM_VPN: {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700275 if (!mNetworkController->canProtect(client->getUid())) {
276 return -EPERM;
277 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700278 // If a bypassable VPN's provider app calls connect() and then protect(), it will end up
279 // with a socket that looks like that of a system proxy but is not (see comments for
280 // ON_CONNECT above). So, reset the NetId.
281 //
282 // In any case, it's appropriate that if the socket has an implicit VPN NetId mark, the
283 // PROTECT_FROM_VPN command should unset it.
284 if (!fwmark.explicitlySelected && mNetworkController->isVirtualNetwork(fwmark.netId)) {
285 fwmark.netId = mNetworkController->getDefaultNetwork();
286 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700287 fwmark.protectedFromVpn = true;
288 permission = static_cast<Permission>(permission | fwmark.permission);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700289 break;
290 }
291
Sreeram Ramachandrana69d9472014-07-11 16:27:02 -0700292 case FwmarkCommand::SELECT_FOR_USER: {
293 if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) {
294 return -EPERM;
295 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700296 fwmark.netId = mNetworkController->getNetworkForUser(command.uid);
Sreeram Ramachandrana69d9472014-07-11 16:27:02 -0700297 fwmark.protectedFromVpn = true;
298 break;
299 }
300
Chenbo Feng9944ba82017-10-10 17:33:20 -0700301 case FwmarkCommand::TAG_SOCKET: {
302 // If the UID is -1, tag as the caller's UID:
303 // - TrafficStats and NetworkManagementSocketTagger use -1 to indicate "use the
304 // caller's UID".
305 // - xt_qtaguid will see -1 on the command line, fail to parse it as a uint32_t, and
306 // fall back to current_fsuid().
307 if (static_cast<int>(command.uid) == -1) {
308 command.uid = client->getUid();
309 }
310 if (command.uid != client->getUid() && !hasUpdateDeviceStatsPermission(client)) {
311 return -EPERM;
312 }
313 return mTrafficCtrl->tagSocket(*socketFd, command.trafficCtrlInfo, command.uid);
314 }
315
316 case FwmarkCommand::UNTAG_SOCKET: {
317 // Any process can untag a socket it has an fd for.
318 return mTrafficCtrl->untagSocket(*socketFd);
319 }
320
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700321 default: {
322 // unknown command
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700323 return -EPROTO;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700324 }
325 }
326
Sreeram Ramachandrana675b002014-07-05 11:00:55 -0700327 fwmark.permission = permission;
328
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700329 if (setsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue,
330 sizeof(fwmark.intValue)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700331 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700332 }
333
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700334 return 0;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700335}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900336
337} // namespace net
338} // namespace android