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 | |
Hugo Benichi | 456c906 | 2017-01-04 12:19:16 +0900 | [diff] [blame] | 19 | #include <netinet/in.h> |
Lorenzo Colitti | 09175be | 2017-11-17 14:01:21 +0900 | [diff] [blame] | 20 | #include <selinux/selinux.h> |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 21 | #include <sys/socket.h> |
| 22 | #include <unistd.h> |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 23 | #include <utils/String16.h> |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 24 | |
Josh Gao | ba76bd6 | 2020-01-06 17:22:41 -0800 | [diff] [blame] | 25 | #include <android-base/cmsg.h> |
| 26 | #include <android-base/logging.h> |
Ken Chen | 8986393 | 2020-02-21 16:30:47 +0800 | [diff] [blame] | 27 | #include <android-base/properties.h> |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 29 | #include <netd_resolv/resolv.h> // NETID_UNSET |
| 30 | |
| 31 | #include "Fwmark.h" |
| 32 | #include "FwmarkCommand.h" |
| 33 | #include "NetdConstants.h" |
| 34 | #include "NetworkController.h" |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 35 | |
Ken Chen | ebdeba8 | 2021-10-28 09:54:46 +0800 | [diff] [blame] | 36 | #include "NetdUpdatablePublic.h" |
| 37 | |
Josh Gao | ba76bd6 | 2020-01-06 17:22:41 -0800 | [diff] [blame] | 38 | using android::base::ReceiveFileDescriptorVector; |
| 39 | using android::base::unique_fd; |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 40 | using android::net::metrics::INetdEventListener; |
| 41 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 42 | namespace android { |
| 43 | namespace net { |
| 44 | |
Lorenzo Colitti | 09175be | 2017-11-17 14:01:21 +0900 | [diff] [blame] | 45 | constexpr const char *SYSTEM_SERVER_CONTEXT = "u:r:system_server:s0"; |
| 46 | |
| 47 | bool isSystemServer(SocketClient* client) { |
| 48 | if (client->getUid() != AID_SYSTEM) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | char *context; |
| 53 | if (getpeercon(client->getSocket(), &context)) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | // We can't use context_new and context_type_get as they're private to libselinux. So just do |
| 58 | // a string match instead. |
| 59 | bool ret = !strcmp(context, SYSTEM_SERVER_CONTEXT); |
| 60 | freecon(context); |
| 61 | |
| 62 | return ret; |
| 63 | } |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 64 | |
Patrick Rohr | b371bc3 | 2022-02-01 22:43:23 +0100 | [diff] [blame] | 65 | FwmarkServer::FwmarkServer(NetworkController* networkController, EventReporter* eventReporter) |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 66 | : SocketListener(SOCKET_NAME, true), |
| 67 | mNetworkController(networkController), |
| 68 | mEventReporter(eventReporter), |
Ken Chen | 8986393 | 2020-02-21 16:30:47 +0800 | [diff] [blame] | 69 | mRedirectSocketCalls( |
| 70 | android::base::GetBoolProperty("ro.vendor.redirect_socket_calls", false)) {} |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 71 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 72 | bool FwmarkServer::onDataAvailable(SocketClient* client) { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 73 | int socketFd = -1; |
| 74 | int error = processClient(client, &socketFd); |
| 75 | if (socketFd >= 0) { |
| 76 | close(socketFd); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Always send a response even if there were connection errors or read errors, so that we don't |
| 80 | // inadvertently cause the client to hang (which always waits for a response). |
| 81 | client->sendData(&error, sizeof(error)); |
| 82 | |
| 83 | // Always close the client connection (by returning false). This prevents a DoS attack where |
| 84 | // the client issues multiple commands on the same connection, never reading the responses, |
| 85 | // causing its receive buffer to fill up, and thus causing our client->sendData() to block. |
| 86 | return false; |
| 87 | } |
| 88 | |
Ken Chen | 8986393 | 2020-02-21 16:30:47 +0800 | [diff] [blame] | 89 | static bool hasDestinationAddress(FwmarkCommand::CmdId cmdId, bool redirectSocketCalls) { |
| 90 | if (redirectSocketCalls) { |
| 91 | return (cmdId == FwmarkCommand::ON_SENDTO || cmdId == FwmarkCommand::ON_CONNECT || |
| 92 | cmdId == FwmarkCommand::ON_SENDMSG || cmdId == FwmarkCommand::ON_SENDMMSG || |
| 93 | cmdId == FwmarkCommand::ON_CONNECT_COMPLETE); |
| 94 | } else { |
| 95 | return (cmdId == FwmarkCommand::ON_CONNECT_COMPLETE); |
Ken Chen | 22e5fb8 | 2020-02-25 18:05:52 +0000 | [diff] [blame] | 96 | } |
Ken Chen | 22e5fb8 | 2020-02-25 18:05:52 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 99 | int FwmarkServer::processClient(SocketClient* client, int* socketFd) { |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 100 | FwmarkCommand command; |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 101 | FwmarkConnectInfo connectInfo; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 102 | |
Josh Gao | ba76bd6 | 2020-01-06 17:22:41 -0800 | [diff] [blame] | 103 | char buf[sizeof(command) + sizeof(connectInfo)]; |
| 104 | std::vector<unique_fd> received_fds; |
| 105 | ssize_t messageLength = |
| 106 | ReceiveFileDescriptorVector(client->getSocket(), buf, sizeof(buf), 1, &received_fds); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 107 | |
Josh Gao | ba76bd6 | 2020-01-06 17:22:41 -0800 | [diff] [blame] | 108 | if (messageLength < 0) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 109 | return -errno; |
Josh Gao | ba76bd6 | 2020-01-06 17:22:41 -0800 | [diff] [blame] | 110 | } else if (messageLength == 0) { |
| 111 | return -ESHUTDOWN; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Josh Gao | ba76bd6 | 2020-01-06 17:22:41 -0800 | [diff] [blame] | 114 | memcpy(&command, buf, sizeof(command)); |
| 115 | memcpy(&connectInfo, buf + sizeof(command), sizeof(connectInfo)); |
| 116 | |
Ken Chen | 8986393 | 2020-02-21 16:30:47 +0800 | [diff] [blame] | 117 | size_t expectedLen = sizeof(command); |
| 118 | if (hasDestinationAddress(command.cmdId, mRedirectSocketCalls)) { |
| 119 | expectedLen += sizeof(connectInfo); |
Ken Chen | 22e5fb8 | 2020-02-25 18:05:52 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Ken Chen | 8986393 | 2020-02-21 16:30:47 +0800 | [diff] [blame] | 122 | if (messageLength != static_cast<ssize_t>(expectedLen)) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 123 | return -EBADMSG; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 126 | 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 | |
Josh Gao | ba76bd6 | 2020-01-06 17:22:41 -0800 | [diff] [blame] | 135 | if (received_fds.size() != 1) { |
| 136 | LOG(ERROR) << "FwmarkServer received " << received_fds.size() << " fds from client?"; |
| 137 | return -EBADF; |
| 138 | } else if (received_fds[0] < 0) { |
| 139 | LOG(ERROR) << "FwmarkServer received fd -1 from ReceiveFileDescriptorVector?"; |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 140 | return -EBADF; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Josh Gao | ba76bd6 | 2020-01-06 17:22:41 -0800 | [diff] [blame] | 143 | *socketFd = received_fds[0].release(); |
| 144 | |
Remi NGUYEN VAN | eabc5da | 2018-04-24 18:54:58 +0900 | [diff] [blame] | 145 | int family; |
| 146 | socklen_t familyLen = sizeof(family); |
| 147 | if (getsockopt(*socketFd, SOL_SOCKET, SO_DOMAIN, &family, &familyLen) == -1) { |
| 148 | return -errno; |
| 149 | } |
| 150 | if (!FwmarkCommand::isSupportedFamily(family)) { |
| 151 | return -EAFNOSUPPORT; |
| 152 | } |
| 153 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 154 | Fwmark fwmark; |
Sreeram Ramachandran | 5ff58d4 | 2014-05-14 09:57:31 -0700 | [diff] [blame] | 155 | socklen_t fwmarkLen = sizeof(fwmark.intValue); |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 156 | if (getsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 157 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 160 | switch (command.cmdId) { |
| 161 | case FwmarkCommand::ON_ACCEPT: { |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 162 | // 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] | 163 | // permissions bits, so we just add the rest of the user's permissions here. |
| 164 | permission = static_cast<Permission>(permission | fwmark.permission); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 168 | case FwmarkCommand::ON_CONNECT: { |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 169 | // Called before a socket connect() happens. Set an appropriate NetId into the fwmark so |
| 170 | // that the socket routes consistently over that network. Do this even if the socket |
| 171 | // already has a NetId, so that calling connect() multiple times still works. |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 172 | // |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 173 | // But if the explicit bit was set, the existing NetId was explicitly preferred (and not |
| 174 | // a case of connect() being called multiple times). Don't reset the NetId in that case. |
| 175 | // |
| 176 | // An "appropriate" NetId is the NetId of a bypassable VPN that applies to the user, or |
| 177 | // failing that, the default network. We'll never set the NetId of a secure VPN here. |
| 178 | // See the comments in the implementation of getNetworkForConnect() for more details. |
| 179 | // |
| 180 | // If the protect bit is set, this could be either a system proxy (e.g.: the dns proxy |
| 181 | // or the download manager) acting on behalf of another user, or a VPN provider. If it's |
| 182 | // a proxy, we shouldn't reset the NetId. If it's a VPN provider, we should set the |
| 183 | // default network's NetId. |
| 184 | // |
| 185 | // There's no easy way to tell the difference between a proxy and a VPN app. We can't |
| 186 | // use PERMISSION_SYSTEM to identify the proxy because a VPN app may also have those |
| 187 | // permissions. So we use the following heuristic: |
| 188 | // |
| 189 | // If it's a proxy, but the existing NetId is not a VPN, that means the user (that the |
| 190 | // proxy is acting on behalf of) is not subject to a VPN, so the proxy must have picked |
| 191 | // the default network's NetId. So, it's okay to replace that with the current default |
| 192 | // network's NetId (which in all likelihood is the same). |
| 193 | // |
| 194 | // Conversely, if it's a VPN provider, the existing NetId cannot be a VPN. The only time |
| 195 | // we set a VPN's NetId into a socket without setting the explicit bit is here, in |
| 196 | // ON_CONNECT, but we won't do that if the socket has the protect bit set. If the VPN |
| 197 | // provider connect()ed (and got the VPN NetId set) and then called protect(), we |
| 198 | // would've unset the NetId in PROTECT_FROM_VPN below. |
| 199 | // |
| 200 | // So, overall (when the explicit bit is not set but the protect bit is set), if the |
| 201 | // existing NetId is a VPN, don't reset it. Else, set the default network's NetId. |
| 202 | if (!fwmark.explicitlySelected) { |
| 203 | if (!fwmark.protectedFromVpn) { |
| 204 | fwmark.netId = mNetworkController->getNetworkForConnect(client->getUid()); |
| 205 | } else if (!mNetworkController->isVirtualNetwork(fwmark.netId)) { |
| 206 | fwmark.netId = mNetworkController->getDefaultNetwork(); |
| 207 | } |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 208 | } |
| 209 | break; |
| 210 | } |
| 211 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 212 | case FwmarkCommand::ON_CONNECT_COMPLETE: { |
| 213 | // Called after a socket connect() completes. |
| 214 | // This reports connect event including netId, destination IP address, destination port, |
Hugo Benichi | 7dfaa78 | 2016-10-31 15:07:23 +0900 | [diff] [blame] | 215 | // uid, connect latency, and connect errno if any. |
Hugo Benichi | 456c906 | 2017-01-04 12:19:16 +0900 | [diff] [blame] | 216 | |
| 217 | // Skip reporting if connect() happened on a UDP socket. |
| 218 | int socketProto; |
| 219 | socklen_t intSize = sizeof(socketProto); |
| 220 | const int ret = getsockopt(*socketFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &intSize); |
| 221 | if ((ret != 0) || (socketProto == IPPROTO_UDP)) { |
| 222 | break; |
| 223 | } |
| 224 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 225 | android::sp<android::net::metrics::INetdEventListener> netdEventListener = |
| 226 | mEventReporter->getNetdEventListener(); |
| 227 | |
| 228 | if (netdEventListener != nullptr) { |
| 229 | char addrstr[INET6_ADDRSTRLEN]; |
| 230 | char portstr[sizeof("65536")]; |
| 231 | const int ret = getnameinfo((sockaddr*) &connectInfo.addr, sizeof(connectInfo.addr), |
| 232 | addrstr, sizeof(addrstr), portstr, sizeof(portstr), |
| 233 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 234 | |
Hugo Benichi | 7dfaa78 | 2016-10-31 15:07:23 +0900 | [diff] [blame] | 235 | netdEventListener->onConnectEvent(fwmark.netId, connectInfo.error, |
| 236 | connectInfo.latencyMs, |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 237 | (ret == 0) ? String16(addrstr) : String16(""), |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 238 | (ret == 0) ? strtoul(portstr, nullptr, 10) : 0, client->getUid()); |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 239 | } |
| 240 | break; |
| 241 | } |
| 242 | |
Maciej Żenczykowski | da12b9d | 2020-04-21 18:14:52 -0700 | [diff] [blame] | 243 | case FwmarkCommand::ON_SENDMMSG: |
| 244 | case FwmarkCommand::ON_SENDMSG: |
Ken Chen | 22e5fb8 | 2020-02-25 18:05:52 +0000 | [diff] [blame] | 245 | case FwmarkCommand::ON_SENDTO: { |
| 246 | return 0; |
| 247 | } |
| 248 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 249 | case FwmarkCommand::SELECT_NETWORK: { |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 250 | fwmark.netId = command.netId; |
| 251 | if (command.netId == NETID_UNSET) { |
| 252 | fwmark.explicitlySelected = false; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 253 | fwmark.protectedFromVpn = false; |
| 254 | permission = PERMISSION_NONE; |
Lorenzo Colitti | a1067c8 | 2014-10-02 22:47:41 +0900 | [diff] [blame] | 255 | } else { |
| 256 | if (int ret = mNetworkController->checkUserNetworkAccess(client->getUid(), |
| 257 | command.netId)) { |
| 258 | return ret; |
| 259 | } |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 260 | fwmark.explicitlySelected = true; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 261 | fwmark.protectedFromVpn = mNetworkController->canProtect(client->getUid()); |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 262 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 266 | case FwmarkCommand::PROTECT_FROM_VPN: { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 267 | if (!mNetworkController->canProtect(client->getUid())) { |
| 268 | return -EPERM; |
| 269 | } |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 270 | // If a bypassable VPN's provider app calls connect() and then protect(), it will end up |
| 271 | // with a socket that looks like that of a system proxy but is not (see comments for |
| 272 | // ON_CONNECT above). So, reset the NetId. |
| 273 | // |
| 274 | // In any case, it's appropriate that if the socket has an implicit VPN NetId mark, the |
| 275 | // PROTECT_FROM_VPN command should unset it. |
| 276 | if (!fwmark.explicitlySelected && mNetworkController->isVirtualNetwork(fwmark.netId)) { |
| 277 | fwmark.netId = mNetworkController->getDefaultNetwork(); |
| 278 | } |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 279 | fwmark.protectedFromVpn = true; |
| 280 | permission = static_cast<Permission>(permission | fwmark.permission); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 284 | case FwmarkCommand::SELECT_FOR_USER: { |
| 285 | if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) { |
| 286 | return -EPERM; |
| 287 | } |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 288 | fwmark.netId = mNetworkController->getNetworkForUser(command.uid); |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 289 | fwmark.protectedFromVpn = true; |
| 290 | break; |
| 291 | } |
| 292 | |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 293 | case FwmarkCommand::TAG_SOCKET: { |
| 294 | // If the UID is -1, tag as the caller's UID: |
| 295 | // - TrafficStats and NetworkManagementSocketTagger use -1 to indicate "use the |
| 296 | // caller's UID". |
| 297 | // - xt_qtaguid will see -1 on the command line, fail to parse it as a uint32_t, and |
| 298 | // fall back to current_fsuid(). |
| 299 | if (static_cast<int>(command.uid) == -1) { |
| 300 | command.uid = client->getUid(); |
| 301 | } |
Ken Chen | ebdeba8 | 2021-10-28 09:54:46 +0800 | [diff] [blame] | 302 | return libnetd_updatable_tagSocket(*socketFd, command.trafficCtrlInfo, command.uid, |
| 303 | client->getUid()); |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | case FwmarkCommand::UNTAG_SOCKET: { |
| 307 | // Any process can untag a socket it has an fd for. |
Ken Chen | ebdeba8 | 2021-10-28 09:54:46 +0800 | [diff] [blame] | 308 | return libnetd_updatable_untagSocket(*socketFd); |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 311 | default: { |
| 312 | // unknown command |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 313 | return -EPROTO; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Sreeram Ramachandran | a675b00 | 2014-07-05 11:00:55 -0700 | [diff] [blame] | 317 | fwmark.permission = permission; |
| 318 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 319 | if (setsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, |
| 320 | sizeof(fwmark.intValue)) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 321 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 324 | return 0; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 325 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 326 | |
| 327 | } // namespace net |
| 328 | } // namespace android |