blob: f519626a54d8642e303285452f5283f41eb62ae7 [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
Ken Chend81356c2023-08-24 18:39:26 +080019#include <net/if.h>
Hugo Benichi456c9062017-01-04 12:19:16 +090020#include <netinet/in.h>
Lorenzo Colitti09175be2017-11-17 14:01:21 +090021#include <selinux/selinux.h>
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070022#include <sys/socket.h>
23#include <unistd.h>
Michal Karpinski7d374532016-10-06 19:33:55 +010024#include <utils/String16.h>
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070025
Josh Gaoba76bd62020-01-06 17:22:41 -080026#include <android-base/cmsg.h>
27#include <android-base/logging.h>
Ken Chen89863932020-02-21 16:30:47 +080028#include <android-base/properties.h>
Chenbo Feng9944ba82017-10-10 17:33:20 -070029#include <binder/IServiceManager.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090030#include <netd_resolv/resolv.h> // NETID_UNSET
31
32#include "Fwmark.h"
33#include "FwmarkCommand.h"
34#include "NetdConstants.h"
35#include "NetworkController.h"
Chenbo Feng9944ba82017-10-10 17:33:20 -070036
Ken Chenebdeba82021-10-28 09:54:46 +080037#include "NetdUpdatablePublic.h"
38
Josh Gaoba76bd62020-01-06 17:22:41 -080039using android::base::ReceiveFileDescriptorVector;
40using android::base::unique_fd;
Michal Karpinski7d374532016-10-06 19:33:55 +010041using android::net::metrics::INetdEventListener;
42
Lorenzo Colitti7035f222017-02-13 18:29:00 +090043namespace android {
44namespace net {
45
Patrick Rohrb371bc32022-02-01 22:43:23 +010046FwmarkServer::FwmarkServer(NetworkController* networkController, EventReporter* eventReporter)
Chenbo Feng9944ba82017-10-10 17:33:20 -070047 : SocketListener(SOCKET_NAME, true),
48 mNetworkController(networkController),
Maciej Żenczykowski1a1e43f2023-09-06 09:48:26 +000049 mEventReporter(eventReporter) {}
Chenbo Feng9944ba82017-10-10 17:33:20 -070050
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070051bool FwmarkServer::onDataAvailable(SocketClient* client) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070052 int socketFd = -1;
53 int error = processClient(client, &socketFd);
54 if (socketFd >= 0) {
55 close(socketFd);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070056 }
57
58 // Always send a response even if there were connection errors or read errors, so that we don't
59 // inadvertently cause the client to hang (which always waits for a response).
60 client->sendData(&error, sizeof(error));
61
62 // Always close the client connection (by returning false). This prevents a DoS attack where
63 // the client issues multiple commands on the same connection, never reading the responses,
64 // causing its receive buffer to fill up, and thus causing our client->sendData() to block.
65 return false;
66}
67
Maciej Żenczykowski1a1e43f2023-09-06 09:48:26 +000068static bool hasDestinationAddress(FwmarkCommand::CmdId cmdId) {
Maciej Żenczykowski5e330152023-08-29 13:56:41 +000069 switch (cmdId) {
Maciej Żenczykowski1a1e43f2023-09-06 09:48:26 +000070 case FwmarkCommand::ON_CONNECT:
Maciej Żenczykowski5e330152023-08-29 13:56:41 +000071 case FwmarkCommand::ON_CONNECT_COMPLETE:
Maciej Żenczykowski1a1e43f2023-09-06 09:48:26 +000072 case FwmarkCommand::ON_SENDMSG:
73 case FwmarkCommand::ON_SENDMMSG:
74 case FwmarkCommand::ON_SENDTO:
Maciej Żenczykowski5e330152023-08-29 13:56:41 +000075 return true;
76 default:
77 return false;
Ken Chen22e5fb82020-02-25 18:05:52 +000078 }
Ken Chen22e5fb82020-02-25 18:05:52 +000079}
80
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070081int FwmarkServer::processClient(SocketClient* client, int* socketFd) {
Maciej Żenczykowskied656a12023-10-15 17:22:33 +000082 struct {
83 FwmarkCommand command;
84 FwmarkConnectInfo connectInfo;
85 } buf;
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070086
Maciej Żenczykowskied656a12023-10-15 17:22:33 +000087 // make sure there is no spurious padding
88 static_assert(sizeof(buf) == sizeof(buf.command) + sizeof(buf.connectInfo));
89
Josh Gaoba76bd62020-01-06 17:22:41 -080090 std::vector<unique_fd> received_fds;
91 ssize_t messageLength =
Maciej Żenczykowskied656a12023-10-15 17:22:33 +000092 ReceiveFileDescriptorVector(client->getSocket(), &buf, sizeof(buf), 1, &received_fds);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070093
Josh Gaoba76bd62020-01-06 17:22:41 -080094 if (messageLength < 0) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070095 return -errno;
Josh Gaoba76bd62020-01-06 17:22:41 -080096 } else if (messageLength == 0) {
97 return -ESHUTDOWN;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070098 }
99
Maciej Żenczykowskied656a12023-10-15 17:22:33 +0000100 const FwmarkCommand &command = buf.command;
101 const FwmarkConnectInfo &connectInfo = buf.connectInfo;
Josh Gaoba76bd62020-01-06 17:22:41 -0800102
Ken Chen89863932020-02-21 16:30:47 +0800103 size_t expectedLen = sizeof(command);
Maciej Żenczykowski1a1e43f2023-09-06 09:48:26 +0000104 if (hasDestinationAddress(command.cmdId)) {
Ken Chen89863932020-02-21 16:30:47 +0800105 expectedLen += sizeof(connectInfo);
Ken Chen22e5fb82020-02-25 18:05:52 +0000106 }
107
Ken Chen89863932020-02-21 16:30:47 +0800108 if (messageLength != static_cast<ssize_t>(expectedLen)) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700109 return -EBADMSG;
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700110 }
111
Paul Jensend1df5972015-05-06 07:29:56 -0400112 Permission permission = mNetworkController->getPermissionForUser(client->getUid());
113
114 if (command.cmdId == FwmarkCommand::QUERY_USER_ACCESS) {
115 if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) {
116 return -EPERM;
117 }
118 return mNetworkController->checkUserNetworkAccess(command.uid, command.netId);
119 }
120
Josh Gaoba76bd62020-01-06 17:22:41 -0800121 if (received_fds.size() != 1) {
122 LOG(ERROR) << "FwmarkServer received " << received_fds.size() << " fds from client?";
123 return -EBADF;
124 } else if (received_fds[0] < 0) {
125 LOG(ERROR) << "FwmarkServer received fd -1 from ReceiveFileDescriptorVector?";
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700126 return -EBADF;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700127 }
128
Josh Gaoba76bd62020-01-06 17:22:41 -0800129 *socketFd = received_fds[0].release();
130
Remi NGUYEN VANeabc5da2018-04-24 18:54:58 +0900131 int family;
132 socklen_t familyLen = sizeof(family);
133 if (getsockopt(*socketFd, SOL_SOCKET, SO_DOMAIN, &family, &familyLen) == -1) {
134 return -errno;
135 }
136 if (!FwmarkCommand::isSupportedFamily(family)) {
137 return -EAFNOSUPPORT;
138 }
139
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700140 Fwmark fwmark;
Sreeram Ramachandran5ff58d42014-05-14 09:57:31 -0700141 socklen_t fwmarkLen = sizeof(fwmark.intValue);
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700142 if (getsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700143 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700144 }
145
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700146 switch (command.cmdId) {
147 case FwmarkCommand::ON_ACCEPT: {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700148 // Called after a socket accept(). The kernel would've marked the NetId and necessary
Sreeram Ramachandrana675b002014-07-05 11:00:55 -0700149 // permissions bits, so we just add the rest of the user's permissions here.
150 permission = static_cast<Permission>(permission | fwmark.permission);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700151 break;
152 }
153
Maciej Żenczykowski1a1e43f2023-09-06 09:48:26 +0000154 case FwmarkCommand::ON_CONNECT: {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700155 // Called before a socket connect() happens. Set an appropriate NetId into the fwmark so
156 // that the socket routes consistently over that network. Do this even if the socket
157 // already has a NetId, so that calling connect() multiple times still works.
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700158 //
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700159 // But if the explicit bit was set, the existing NetId was explicitly preferred (and not
160 // a case of connect() being called multiple times). Don't reset the NetId in that case.
161 //
162 // An "appropriate" NetId is the NetId of a bypassable VPN that applies to the user, or
163 // failing that, the default network. We'll never set the NetId of a secure VPN here.
164 // See the comments in the implementation of getNetworkForConnect() for more details.
165 //
166 // If the protect bit is set, this could be either a system proxy (e.g.: the dns proxy
167 // or the download manager) acting on behalf of another user, or a VPN provider. If it's
168 // a proxy, we shouldn't reset the NetId. If it's a VPN provider, we should set the
169 // default network's NetId.
170 //
171 // There's no easy way to tell the difference between a proxy and a VPN app. We can't
172 // use PERMISSION_SYSTEM to identify the proxy because a VPN app may also have those
173 // permissions. So we use the following heuristic:
174 //
175 // If it's a proxy, but the existing NetId is not a VPN, that means the user (that the
176 // proxy is acting on behalf of) is not subject to a VPN, so the proxy must have picked
177 // the default network's NetId. So, it's okay to replace that with the current default
178 // network's NetId (which in all likelihood is the same).
179 //
180 // Conversely, if it's a VPN provider, the existing NetId cannot be a VPN. The only time
181 // we set a VPN's NetId into a socket without setting the explicit bit is here, in
Maciej Żenczykowski1a1e43f2023-09-06 09:48:26 +0000182 // ON_CONNECT, but we won't do that if the socket has the protect bit set.
Maciej Żenczykowski492cc712023-08-29 14:05:04 +0000183 // If the VPN provider connect()ed (and got the VPN NetId set) and then called
184 // protect(), we would've unset the NetId in PROTECT_FROM_VPN below.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700185 //
186 // So, overall (when the explicit bit is not set but the protect bit is set), if the
187 // existing NetId is a VPN, don't reset it. Else, set the default network's NetId.
188 if (!fwmark.explicitlySelected) {
Ken Chend81356c2023-08-24 18:39:26 +0800189 if (family == AF_INET6 && connectInfo.addr.sin6.sin6_scope_id &&
190 IN6_IS_ADDR_LINKLOCAL(&connectInfo.addr.sin6.sin6_addr)) {
191 fwmark.netId = mNetworkController->getNetworkForInterface(
192 connectInfo.addr.sin6.sin6_scope_id);
193 } else if (!fwmark.protectedFromVpn) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700194 fwmark.netId = mNetworkController->getNetworkForConnect(client->getUid());
195 } else if (!mNetworkController->isVirtualNetwork(fwmark.netId)) {
196 fwmark.netId = mNetworkController->getDefaultNetwork();
197 }
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700198 }
199 break;
200 }
201
Michal Karpinski7d374532016-10-06 19:33:55 +0100202 case FwmarkCommand::ON_CONNECT_COMPLETE: {
203 // Called after a socket connect() completes.
204 // This reports connect event including netId, destination IP address, destination port,
Hugo Benichi7dfaa782016-10-31 15:07:23 +0900205 // uid, connect latency, and connect errno if any.
Hugo Benichi456c9062017-01-04 12:19:16 +0900206
207 // Skip reporting if connect() happened on a UDP socket.
208 int socketProto;
209 socklen_t intSize = sizeof(socketProto);
210 const int ret = getsockopt(*socketFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &intSize);
211 if ((ret != 0) || (socketProto == IPPROTO_UDP)) {
212 break;
213 }
214
Michal Karpinski7d374532016-10-06 19:33:55 +0100215 android::sp<android::net::metrics::INetdEventListener> netdEventListener =
216 mEventReporter->getNetdEventListener();
217
218 if (netdEventListener != nullptr) {
Maciej Żenczykowskid8a146b2023-10-15 17:50:36 +0000219 char addrstr[INET6_ADDRSTRLEN + IFNAMSIZ]; // ipv6 address + optional %scope
220 char portstr[sizeof("65535")];
221 static_assert(sizeof(addrstr) >= 62);
222 static_assert(sizeof(portstr) >= 6);
Maciej Żenczykowskifdd22822023-10-15 17:11:44 +0000223 const int ret = getnameinfo(&connectInfo.addr.s, sizeof(connectInfo.addr.s),
Michal Karpinski7d374532016-10-06 19:33:55 +0100224 addrstr, sizeof(addrstr), portstr, sizeof(portstr),
225 NI_NUMERICHOST | NI_NUMERICSERV);
226
Hugo Benichi7dfaa782016-10-31 15:07:23 +0900227 netdEventListener->onConnectEvent(fwmark.netId, connectInfo.error,
228 connectInfo.latencyMs,
Michal Karpinski7d374532016-10-06 19:33:55 +0100229 (ret == 0) ? String16(addrstr) : String16(""),
Yi Kongbdfd57e2018-07-25 13:26:10 -0700230 (ret == 0) ? strtoul(portstr, nullptr, 10) : 0, client->getUid());
Michal Karpinski7d374532016-10-06 19:33:55 +0100231 }
232 break;
233 }
234
Maciej Żenczykowskida12b9d2020-04-21 18:14:52 -0700235 case FwmarkCommand::ON_SENDMMSG:
236 case FwmarkCommand::ON_SENDMSG:
Ken Chen22e5fb82020-02-25 18:05:52 +0000237 case FwmarkCommand::ON_SENDTO: {
238 return 0;
239 }
240
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700241 case FwmarkCommand::SELECT_NETWORK: {
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700242 fwmark.netId = command.netId;
243 if (command.netId == NETID_UNSET) {
244 fwmark.explicitlySelected = false;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700245 fwmark.protectedFromVpn = false;
246 permission = PERMISSION_NONE;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900247 } else {
248 if (int ret = mNetworkController->checkUserNetworkAccess(client->getUid(),
249 command.netId)) {
250 return ret;
251 }
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700252 fwmark.explicitlySelected = true;
Patrick Rohr4fafa6f2024-04-23 11:36:21 -0700253 fwmark.protectedFromVpn =
254 mNetworkController->canProtect(client->getUid(), command.netId);
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700255 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700256 break;
257 }
258
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700259 case FwmarkCommand::PROTECT_FROM_VPN: {
Patrick Rohr4fafa6f2024-04-23 11:36:21 -0700260 // TODO: Add support to specify netId in protectFromVpn(). Currently, NetdClient always
261 // passes NETID_UNSET
262 if (!mNetworkController->canProtect(client->getUid(), fwmark.netId)) {
Ken Chen6acc2bf2023-06-10 18:23:33 +0800263 LOG(ERROR) << "uid " << client->getUid() << " protect from VPN failed.";
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700264 return -EPERM;
265 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700266 // If a bypassable VPN's provider app calls connect() and then protect(), it will end up
267 // with a socket that looks like that of a system proxy but is not (see comments for
Maciej Żenczykowski1a1e43f2023-09-06 09:48:26 +0000268 // ON_CONNECT above). So, reset the NetId.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700269 //
270 // In any case, it's appropriate that if the socket has an implicit VPN NetId mark, the
271 // PROTECT_FROM_VPN command should unset it.
272 if (!fwmark.explicitlySelected && mNetworkController->isVirtualNetwork(fwmark.netId)) {
273 fwmark.netId = mNetworkController->getDefaultNetwork();
274 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700275 fwmark.protectedFromVpn = true;
276 permission = static_cast<Permission>(permission | fwmark.permission);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700277 break;
278 }
279
Sreeram Ramachandrana69d9472014-07-11 16:27:02 -0700280 case FwmarkCommand::SELECT_FOR_USER: {
281 if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) {
282 return -EPERM;
283 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700284 fwmark.netId = mNetworkController->getNetworkForUser(command.uid);
Sreeram Ramachandrana69d9472014-07-11 16:27:02 -0700285 fwmark.protectedFromVpn = true;
286 break;
287 }
288
Chenbo Feng9944ba82017-10-10 17:33:20 -0700289 case FwmarkCommand::TAG_SOCKET: {
290 // If the UID is -1, tag as the caller's UID:
291 // - TrafficStats and NetworkManagementSocketTagger use -1 to indicate "use the
292 // caller's UID".
Maciej Żenczykowskied656a12023-10-15 17:22:33 +0000293 // - xt_qtaguid will see -1 on the command line, fail to parse it as a uint32_t,
294 // and fall back to current_fsuid().
295 uid_t tagUid = command.uid;
296 if (static_cast<int>(tagUid) == -1) tagUid = client->getUid();
297 return libnetd_updatable_tagSocket(*socketFd, command.trafficCtrlInfo, tagUid,
Ken Chenebdeba82021-10-28 09:54:46 +0800298 client->getUid());
Chenbo Feng9944ba82017-10-10 17:33:20 -0700299 }
300
301 case FwmarkCommand::UNTAG_SOCKET: {
302 // Any process can untag a socket it has an fd for.
Ken Chenebdeba82021-10-28 09:54:46 +0800303 return libnetd_updatable_untagSocket(*socketFd);
Chenbo Feng9944ba82017-10-10 17:33:20 -0700304 }
305
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700306 default: {
307 // unknown command
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700308 return -EPROTO;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700309 }
310 }
311
Sreeram Ramachandrana675b002014-07-05 11:00:55 -0700312 fwmark.permission = permission;
313
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700314 if (setsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue,
315 sizeof(fwmark.intValue)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700316 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700317 }
318
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700319 return 0;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700320}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900321
322} // namespace net
323} // namespace android