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" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 21 | #include "NetworkController.h" |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 22 | #include "resolv_netid.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 23 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <unistd.h> |
| 26 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 27 | FwmarkServer::FwmarkServer(NetworkController* networkController) : |
| 28 | SocketListener("fwmarkd", true), mNetworkController(networkController) { |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool FwmarkServer::onDataAvailable(SocketClient* client) { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 32 | int socketFd = -1; |
| 33 | int error = processClient(client, &socketFd); |
| 34 | if (socketFd >= 0) { |
| 35 | close(socketFd); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // Always send a response even if there were connection errors or read errors, so that we don't |
| 39 | // inadvertently cause the client to hang (which always waits for a response). |
| 40 | client->sendData(&error, sizeof(error)); |
| 41 | |
| 42 | // Always close the client connection (by returning false). This prevents a DoS attack where |
| 43 | // the client issues multiple commands on the same connection, never reading the responses, |
| 44 | // causing its receive buffer to fill up, and thus causing our client->sendData() to block. |
| 45 | return false; |
| 46 | } |
| 47 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 48 | int FwmarkServer::processClient(SocketClient* client, int* socketFd) { |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 49 | FwmarkCommand command; |
| 50 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 51 | iovec iov; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 52 | iov.iov_base = &command; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 53 | iov.iov_len = sizeof(command); |
| 54 | |
| 55 | msghdr message; |
| 56 | memset(&message, 0, sizeof(message)); |
| 57 | message.msg_iov = &iov; |
| 58 | message.msg_iovlen = 1; |
| 59 | |
| 60 | union { |
| 61 | cmsghdr cmh; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 62 | char cmsg[CMSG_SPACE(sizeof(*socketFd))]; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 63 | } cmsgu; |
| 64 | |
| 65 | memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg)); |
| 66 | message.msg_control = cmsgu.cmsg; |
| 67 | message.msg_controllen = sizeof(cmsgu.cmsg); |
| 68 | |
| 69 | int messageLength = TEMP_FAILURE_RETRY(recvmsg(client->getSocket(), &message, 0)); |
| 70 | if (messageLength <= 0) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 71 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 74 | if (messageLength != sizeof(command)) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 75 | return -EBADMSG; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 78 | cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message); |
| 79 | if (cmsgh && cmsgh->cmsg_level == SOL_SOCKET && cmsgh->cmsg_type == SCM_RIGHTS && |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 80 | cmsgh->cmsg_len == CMSG_LEN(sizeof(*socketFd))) { |
| 81 | memcpy(socketFd, CMSG_DATA(cmsgh), sizeof(*socketFd)); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 84 | if (*socketFd < 0) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 85 | return -EBADF; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Fwmark fwmark; |
Sreeram Ramachandran | 5ff58d4 | 2014-05-14 09:57:31 -0700 | [diff] [blame] | 89 | socklen_t fwmarkLen = sizeof(fwmark.intValue); |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 90 | if (getsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 91 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Sreeram Ramachandran | a675b00 | 2014-07-05 11:00:55 -0700 | [diff] [blame] | 94 | Permission permission = mNetworkController->getPermissionForUser(client->getUid()); |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 95 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 96 | switch (command.cmdId) { |
| 97 | case FwmarkCommand::ON_ACCEPT: { |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 98 | // 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] | 99 | // permissions bits, so we just add the rest of the user's permissions here. |
| 100 | permission = static_cast<Permission>(permission | fwmark.permission); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 104 | case FwmarkCommand::ON_CONNECT: { |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 105 | // Called before a socket connect() happens. Set the default network's NetId into the |
| 106 | // fwmark so that the socket routes consistently over that network. Do this even if the |
| 107 | // socket already has a NetId, so that calling connect() multiple times still works. |
| 108 | // |
| 109 | // But respect the existing NetId if it had been explicitly preferred, indicated by: |
| 110 | // + The explicit bit having been set. |
| 111 | // + Or, the NetId being that of a VPN, which indicates a proxy acting on behalf of a |
| 112 | // user who is subject to the VPN. The explicit bit is not set so that it works even |
| 113 | // if the VPN is a split tunnel, but it's an explicit network preference nonetheless. |
| 114 | if (!fwmark.explicitlySelected && !mNetworkController->isVirtualNetwork(fwmark.netId)) { |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 115 | fwmark.netId = mNetworkController->getDefaultNetwork(); |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | case FwmarkCommand::SELECT_NETWORK: { |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 121 | fwmark.netId = command.netId; |
| 122 | if (command.netId == NETID_UNSET) { |
| 123 | fwmark.explicitlySelected = false; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 124 | fwmark.protectedFromVpn = false; |
| 125 | permission = PERMISSION_NONE; |
| 126 | } else if (mNetworkController->canUserSelectNetwork(client->getUid(), command.netId)) { |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 127 | fwmark.explicitlySelected = true; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 128 | fwmark.protectedFromVpn = mNetworkController->canProtect(client->getUid()); |
| 129 | } else { |
| 130 | return -EPERM; |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 131 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 135 | case FwmarkCommand::PROTECT_FROM_VPN: { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 136 | if (!mNetworkController->canProtect(client->getUid())) { |
| 137 | return -EPERM; |
| 138 | } |
| 139 | fwmark.protectedFromVpn = true; |
| 140 | permission = static_cast<Permission>(permission | fwmark.permission); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 144 | case FwmarkCommand::SELECT_FOR_USER: { |
| 145 | if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) { |
| 146 | return -EPERM; |
| 147 | } |
| 148 | fwmark.netId = mNetworkController->getNetworkForUser(command.uid, NETID_UNSET, false); |
| 149 | fwmark.protectedFromVpn = true; |
| 150 | break; |
| 151 | } |
| 152 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 153 | default: { |
| 154 | // unknown command |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 155 | return -EPROTO; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Sreeram Ramachandran | a675b00 | 2014-07-05 11:00:55 -0700 | [diff] [blame] | 159 | fwmark.permission = permission; |
| 160 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 161 | if (setsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, |
| 162 | sizeof(fwmark.intValue)) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 163 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 166 | return 0; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 167 | } |