1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <poll.h> /* poll */
#include <sys/socket.h>
#include <thread>
#include <android-base/parseint.h>
#include <android-base/unique_fd.h>
#include <gtest/gtest.h>
#include "NetdClient.h"
#include "netdclient_priv.h"
namespace {
// Keep in sync with FrameworkListener.cpp (500, "Command not recognized")
constexpr char NOT_SUPPORT_MSG[] = "500 Command not recognized";
int openDnsProxyFuncStub() {
return -1;
};
typedef int (*DnsOpenProxyType)();
typedef int (*SocketFunctionType)(int, int, int);
DnsOpenProxyType openDnsProxyFuncPtr = openDnsProxyFuncStub;
SocketFunctionType socketFuncPtr = socket;
void serverLoop(int dnsProxyFd) {
while (true) {
pollfd fds[1] = {{.fd = dnsProxyFd, .events = POLLIN}};
enum { SERVERFD = 0 };
int poll_result = TEMP_FAILURE_RETRY(poll(fds, std::size(fds), -1));
ASSERT_GT(poll_result, 0);
if (fds[SERVERFD].revents & POLLERR) return;
if (fds[SERVERFD].revents & POLLIN) {
char buf[4096];
TEMP_FAILURE_RETRY(read(fds[SERVERFD].fd, &buf, sizeof(buf)));
// TODO: verify command
TEMP_FAILURE_RETRY(write(fds[SERVERFD].fd, NOT_SUPPORT_MSG, sizeof(NOT_SUPPORT_MSG)));
}
}
}
void expectAllowNetworkingForProcess() {
// netdClientSocket
android::base::unique_fd ipv4(socketFuncPtr(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)),
ipv6(socketFuncPtr(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0));
EXPECT_LE(3, ipv4);
EXPECT_LE(3, ipv6);
// dns_open_proxy
android::base::unique_fd dnsproxydSocket(openDnsProxyFuncPtr());
EXPECT_LE(3, dnsproxydSocket);
}
void expectNotAllowNetworkingForProcess() {
// netdClientSocket
android::base::unique_fd unixSocket(socketFuncPtr(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0));
EXPECT_LE(3, unixSocket);
android::base::unique_fd ipv4(socketFuncPtr(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0));
EXPECT_EQ(-1, ipv4);
EXPECT_EQ(errno, EPERM);
android::base::unique_fd ipv6(socketFuncPtr(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0));
EXPECT_EQ(-1, ipv6);
EXPECT_EQ(errno, EPERM);
// dns_open_proxy
android::base::unique_fd dnsproxydSocket(openDnsProxyFuncPtr());
EXPECT_EQ(-1, dnsproxydSocket);
EXPECT_EQ(errno, EPERM);
}
} // namespace
TEST(NetdClientTest, getNetworkForDnsInternal) {
// Test invalid fd
unsigned dnsNetId = 0;
const int invalidFd = -1;
EXPECT_EQ(-EBADF, getNetworkForDnsInternal(invalidFd, &dnsNetId));
// Test what the client does if the resolver does not support the "getdnsnetid" command.
android::base::unique_fd clientFd, serverFd;
ASSERT_TRUE(android::base::Socketpair(AF_UNIX, &clientFd, &serverFd));
std::thread serverThread = std::thread(serverLoop, serverFd.get());
EXPECT_EQ(-EOPNOTSUPP, getNetworkForDnsInternal(clientFd.get(), &dnsNetId));
clientFd.reset(); // Causes serverLoop() to exit
serverThread.join();
}
TEST(NetdClientTest, getNetworkForDns) {
// Test null input
unsigned* testNull = nullptr;
EXPECT_EQ(-EFAULT, getNetworkForDns(testNull));
}
TEST(NetdClientTest, protectFromVpnBadFd) {
EXPECT_EQ(-EBADF, protectFromVpn(-1));
}
TEST(NetdClientTest, protectFromVpnUnixStream) {
int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
ASSERT_GE(s, 3);
EXPECT_EQ(-EAFNOSUPPORT, protectFromVpn(s));
close(s);
}
TEST(NetdClientTest, protectFromVpnTcp6) {
int s = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
ASSERT_GE(s, 3);
EXPECT_EQ(0, protectFromVpn(s));
close(s);
}
TEST(NetdClientTest, setAllowNetworkingForProcess) {
netdClientInitDnsOpenProxy(&openDnsProxyFuncPtr);
netdClientInitSocket(&socketFuncPtr);
// At the beginning, we should be able to use socket since the default setting is allowing.
expectAllowNetworkingForProcess();
// Disable
setAllowNetworkingForProcess(false);
expectNotAllowNetworkingForProcess();
// Reset
setAllowNetworkingForProcess(true);
expectAllowNetworkingForProcess();
}
|