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 "FwmarkClient.h"
18
19 #include "FwmarkCommand.h"
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27
28 #include <algorithm> // std::size()
29 #include <iterator>
30
31 namespace {
32
33 // Env flag to control whether FwmarkClient sends sockets to netd for marking.
34 // This can only be disabled when the process running as root and is meant for kernel testing.
35 inline constexpr char ANDROID_NO_USE_FWMARK_CLIENT[] = "ANDROID_NO_USE_FWMARK_CLIENT";
36
37 const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"};
38
commandHasFd(int cmdId)39 bool commandHasFd(int cmdId) {
40 return (cmdId != FwmarkCommand::QUERY_USER_ACCESS);
41 }
42
43 } // namespace
44
shouldSetFwmark(int family)45 bool FwmarkClient::shouldSetFwmark(int family) {
46 // Checking whether family is supported before checking whether this can be
47 // disabled. Because there are existing processes using AF_LOCAL socket but it
48 // doesn't have permission to call geteuid(). Reference b/135422468.
49 if (!FwmarkCommand::isSupportedFamily(family)) {
50 return false;
51 }
52
53 // Permit processes running as root to disable marking. This is required, for
54 // example, to run the kernel networking tests.
55 if (getenv(ANDROID_NO_USE_FWMARK_CLIENT) && geteuid() == 0) {
56 return false;
57 }
58
59 return true;
60 }
61
FwmarkClient()62 FwmarkClient::FwmarkClient() : mChannel(-1) {
63 }
64
~FwmarkClient()65 FwmarkClient::~FwmarkClient() {
66 if (mChannel >= 0) {
67 close(mChannel);
68 }
69 }
70
send(FwmarkCommand * data,int fd,FwmarkConnectInfo * connectInfo)71 int FwmarkClient::send(FwmarkCommand* data, int fd, FwmarkConnectInfo* connectInfo) {
72 mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
73 if (mChannel == -1) {
74 return -errno;
75 }
76
77 if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH),
78 sizeof(FWMARK_SERVER_PATH))) == -1) {
79 // If we are unable to connect to the fwmark server, assume there's no error. This protects
80 // against future changes if the fwmark server goes away.
81 // TODO: This means that fd will very likely be misrouted. See if we can delete this in a
82 // separate CL.
83 return 0;
84 }
85
86 iovec iov[2] = {
87 { data, sizeof(*data) },
88 { connectInfo, (connectInfo ? sizeof(*connectInfo) : 0) },
89 };
90 msghdr message;
91 memset(&message, 0, sizeof(message));
92 message.msg_iov = iov;
93 message.msg_iovlen = std::size(iov);
94
95 union {
96 cmsghdr cmh;
97 char cmsg[CMSG_SPACE(sizeof(fd))];
98 } cmsgu;
99
100 if (commandHasFd(data->cmdId)) {
101 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
102 message.msg_control = cmsgu.cmsg;
103 message.msg_controllen = sizeof(cmsgu.cmsg);
104
105 cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
106 cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
107 cmsgh->cmsg_level = SOL_SOCKET;
108 cmsgh->cmsg_type = SCM_RIGHTS;
109 memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd));
110 }
111
112 if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
113 return -errno;
114 }
115
116 int error = 0;
117
118 if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
119 return -errno;
120 }
121
122 return error;
123 }
124