blob: 60ee852de3cbb9839c8938266bfb6615dd6ad3f0 [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"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070021#include "NetworkController.h"
Sreeram Ramachandranec008842014-05-21 14:01:16 -070022#include "resolv_netid.h"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070023
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070024#include <sys/socket.h>
25#include <unistd.h>
26
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070027FwmarkServer::FwmarkServer(NetworkController* networkController) :
28 SocketListener("fwmarkd", true), mNetworkController(networkController) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070029}
30
31bool FwmarkServer::onDataAvailable(SocketClient* client) {
32 int fd = -1;
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070033 int error = processClient(client, &fd);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070034 if (fd >= 0) {
35 close(fd);
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 Ramachandran3a069e62014-06-22 11:02:57 -070048int FwmarkServer::processClient(SocketClient* client, int* fd) {
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070049 FwmarkCommand command;
50
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070051 iovec iov;
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070052 iov.iov_base = &command;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070053 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;
62 char cmsg[CMSG_SPACE(sizeof(*fd))];
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 Ramachandran3a069e62014-06-22 11:02:57 -070071 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070072 }
73
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070074 if (messageLength != sizeof(command)) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070075 return -EBADMSG;
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070076 }
77
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070078 cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
79 if (cmsgh && cmsgh->cmsg_level == SOL_SOCKET && cmsgh->cmsg_type == SCM_RIGHTS &&
80 cmsgh->cmsg_len == CMSG_LEN(sizeof(*fd))) {
81 memcpy(fd, CMSG_DATA(cmsgh), sizeof(*fd));
82 }
83
84 if (*fd < 0) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070085 return -EBADF;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070086 }
87
88 Fwmark fwmark;
Sreeram Ramachandran5ff58d42014-05-14 09:57:31 -070089 socklen_t fwmarkLen = sizeof(fwmark.intValue);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070090 if (getsockopt(*fd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070091 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070092 }
93
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070094 fwmark.permission = mNetworkController->getPermissionForUser(client->getUid());
Sreeram Ramachandranec008842014-05-21 14:01:16 -070095
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070096 switch (command.cmdId) {
97 case FwmarkCommand::ON_ACCEPT: {
Sreeram Ramachandran4f536622014-05-13 13:25:34 -070098 // Called after a socket accept(). The kernel would've marked the netId into the socket
99 // already, so we just need to check permissions here.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700100 if (!mNetworkController->isUserPermittedOnNetwork(client->getUid(), fwmark.netId)) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700101 return -EPERM;
Sreeram Ramachandran4f536622014-05-13 13:25:34 -0700102 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700103 break;
104 }
105
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700106 case FwmarkCommand::ON_CONNECT: {
107 // Set the netId (of the default network) into the fwmark, if it has not already been
108 // set explicitly. Called before a socket connect() happens.
109 if (!fwmark.explicitlySelected) {
110 fwmark.netId = mNetworkController->getDefaultNetwork();
111 }
112 break;
113 }
114
115 case FwmarkCommand::SELECT_NETWORK: {
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700116 fwmark.netId = command.netId;
117 if (command.netId == NETID_UNSET) {
118 fwmark.explicitlySelected = false;
119 } else {
120 fwmark.explicitlySelected = true;
121 // If the socket already has the protectedFromVpn bit set, don't reset it, because
122 // non-CONNECTIVITY_INTERNAL apps (e.g.: VpnService) may also protect sockets.
123 if (fwmark.permission & PERMISSION_CONNECTIVITY_INTERNAL) {
124 fwmark.protectedFromVpn = true;
125 }
126 if (!mNetworkController->isValidNetwork(command.netId)) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700127 return -ENONET;
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700128 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700129 if (!mNetworkController->isUserPermittedOnNetwork(client->getUid(),
130 command.netId)) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700131 return -EPERM;
Sreeram Ramachandranec008842014-05-21 14:01:16 -0700132 }
133 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700134 break;
135 }
136
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -0700137 case FwmarkCommand::PROTECT_FROM_VPN: {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700138 // set vpn protect
139 // TODO
140 break;
141 }
142
143 default: {
144 // unknown command
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700145 return -EPROTO;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700146 }
147 }
148
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700149 if (setsockopt(*fd, SOL_SOCKET, SO_MARK, &fwmark.intValue, sizeof(fwmark.intValue)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700150 return -errno;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700151 }
152
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700153 return 0;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700154}