1 /*
2 * Copyright (C) 2016 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 "socket_spec.h"
18
19 #include <limits>
20 #include <string>
21 #include <string_view>
22 #include <unordered_map>
23 #include <vector>
24
25 #include <android-base/parseint.h>
26 #include <android-base/parsenetaddress.h>
27 #include <android-base/stringprintf.h>
28 #include <android-base/strings.h>
29 #include <cutils/sockets.h>
30
31 #include "adb.h"
32 #include "adb_utils.h"
33 #include "sysdeps.h"
34
35 using namespace std::string_literals;
36
37 using android::base::ConsumePrefix;
38 using android::base::StringPrintf;
39
40 #if defined(__linux__)
41 #define ADB_LINUX 1
42 #else
43 #define ADB_LINUX 0
44 #endif
45
46 #if defined(_WIN32)
47 #define ADB_WINDOWS 1
48 #else
49 #define ADB_WINDOWS 0
50 #endif
51
52 #if ADB_LINUX
53 #include <sys/socket.h>
54 #include "sysdeps/vm_sockets.h"
55 #endif
56
57 // Not static because it is used in commandline.c.
58 int gListenAll = 0;
59
60 struct LocalSocketType {
61 int socket_namespace;
62 bool available;
63 };
64
65 static auto& kLocalSocketTypes = *new std::unordered_map<std::string, LocalSocketType>({
66 #if ADB_HOST
67 { "local", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
68 #else
69 { "local", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_WINDOWS } },
70 #endif
71
72 { "localreserved", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_HOST } },
73 { "localabstract", { ANDROID_SOCKET_NAMESPACE_ABSTRACT, ADB_LINUX } },
74 { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
75 });
76
parse_tcp_socket_spec(std::string_view spec,std::string * hostname,int * port,std::string * serial,std::string * error)77 bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
78 std::string* serial, std::string* error) {
79 if (!spec.starts_with("tcp:")) {
80 *error = "specification is not tcp: ";
81 *error += spec;
82 return false;
83 }
84
85 std::string hostname_value;
86 int port_value;
87
88 // If the spec is tcp:<port>, parse it ourselves.
89 // Otherwise, delegate to android::base::ParseNetAddress.
90 if (android::base::ParseInt(&spec[4], &port_value)) {
91 // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234'
92 // identically.
93 if (port_value < 0 || port_value > 65535) {
94 *error = StringPrintf("bad port number '%d'", port_value);
95 return false;
96 }
97 } else {
98 std::string addr(spec.substr(4));
99 port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
100
101 // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
102 // on an address that isn't 'localhost' is unsupported.
103 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
104 return false;
105 }
106
107 if (port_value == -1) {
108 *error = "missing port in specification: ";
109 *error += spec;
110 return false;
111 }
112 }
113
114 if (hostname) {
115 *hostname = std::move(hostname_value);
116 }
117
118 if (port) {
119 *port = port_value;
120 }
121
122 return true;
123 }
124
get_host_socket_spec_port(std::string_view spec,std::string * error)125 int get_host_socket_spec_port(std::string_view spec, std::string* error) {
126 int port;
127 if (spec.starts_with("tcp:")) {
128 if (!parse_tcp_socket_spec(spec, nullptr, &port, nullptr, error)) {
129 return -1;
130 }
131 } else if (spec.starts_with("vsock:")) {
132 #if ADB_LINUX
133 std::string spec_str(spec);
134 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
135 if (fragments.size() != 2) {
136 *error = "given vsock server socket string was invalid";
137 return -1;
138 }
139 if (!android::base::ParseInt(fragments[1], &port)) {
140 *error = "could not parse vsock port";
141 errno = EINVAL;
142 return -1;
143 }
144 if (port < 0) {
145 *error = "vsock port was negative.";
146 errno = EINVAL;
147 return -1;
148 }
149 #else // ADB_LINUX
150 *error = "vsock is only supported on linux";
151 return -1;
152 #endif // ADB_LINUX
153 } else {
154 *error = "given socket spec string was invalid";
155 return -1;
156 }
157 return port;
158 }
159
tcp_host_is_local(std::string_view hostname)160 static bool tcp_host_is_local(std::string_view hostname) {
161 // FIXME
162 return hostname.empty() || hostname == "localhost";
163 }
164
is_socket_spec(std::string_view spec)165 bool is_socket_spec(std::string_view spec) {
166 for (const auto& it : kLocalSocketTypes) {
167 std::string prefix = it.first + ":";
168 if (spec.starts_with(prefix)) {
169 return true;
170 }
171 }
172 return spec.starts_with("tcp:") || spec.starts_with("acceptfd:");
173 }
174
is_local_socket_spec(std::string_view spec)175 bool is_local_socket_spec(std::string_view spec) {
176 for (const auto& it : kLocalSocketTypes) {
177 std::string prefix = it.first + ":";
178 if (spec.starts_with(prefix)) {
179 return true;
180 }
181 }
182
183 std::string error;
184 std::string hostname;
185 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
186 return false;
187 }
188 return tcp_host_is_local(hostname);
189 }
190
socket_spec_connect(unique_fd * fd,std::string_view address,int * port,std::string * serial,std::string * error)191 bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial,
192 std::string* error) {
193 if (address.starts_with("tcp:")) {
194 std::string hostname;
195 int port_value = port ? *port : 0;
196 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
197 return false;
198 }
199
200 if (tcp_host_is_local(hostname)) {
201 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
202 } else {
203 #if ADB_HOST
204 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
205 #else
206 // Disallow arbitrary connections in adbd.
207 *error = "adbd does not support arbitrary tcp connections";
208 return false;
209 #endif
210 }
211
212 if (fd->get() > 0) {
213 disable_tcp_nagle(fd->get());
214 if (port) {
215 *port = port_value;
216 }
217 return true;
218 }
219 return false;
220 } else if (address.starts_with("vsock:")) {
221 #if ADB_LINUX
222 std::string spec_str(address);
223 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
224 unsigned int port_value = port ? *port : 0;
225 if (fragments.size() != 2 && fragments.size() != 3) {
226 *error = android::base::StringPrintf("expected vsock:cid or vsock:port:cid in '%s'",
227 spec_str.c_str());
228 errno = EINVAL;
229 return false;
230 }
231 unsigned int cid = 0;
232 if (!android::base::ParseUint(fragments[1], &cid)) {
233 *error = android::base::StringPrintf("could not parse vsock cid in '%s'",
234 spec_str.c_str());
235 errno = EINVAL;
236 return false;
237 }
238 if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) {
239 *error = android::base::StringPrintf("could not parse vsock port in '%s'",
240 spec_str.c_str());
241 errno = EINVAL;
242 return false;
243 }
244 if (port_value == 0) {
245 *error = android::base::StringPrintf("vsock port was not provided.");
246 errno = EINVAL;
247 return false;
248 }
249 fd->reset(socket(AF_VSOCK, SOCK_STREAM, 0));
250 if (fd->get() == -1) {
251 *error = "could not open vsock socket";
252 return false;
253 }
254 sockaddr_vm addr{};
255 addr.svm_family = AF_VSOCK;
256 addr.svm_port = port_value;
257 addr.svm_cid = cid;
258 if (serial) {
259 *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value);
260 }
261 if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
262 int error_num = errno;
263 *error = android::base::StringPrintf("could not connect to vsock address '%s'",
264 spec_str.c_str());
265 errno = error_num;
266 return false;
267 }
268 if (port) {
269 *port = port_value;
270 }
271 return true;
272 #else // ADB_LINUX
273 *error = "vsock is only supported on linux";
274 return false;
275 #endif // ADB_LINUX
276 } else if (address.starts_with("acceptfd:")) {
277 *error = "cannot connect to acceptfd";
278 return false;
279 }
280
281 for (const auto& it : kLocalSocketTypes) {
282 std::string prefix = it.first + ":";
283 if (address.starts_with(prefix)) {
284 if (!it.second.available) {
285 *error = StringPrintf("socket type %s is unavailable on this platform",
286 it.first.c_str());
287 return false;
288 }
289
290 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
291 SOCK_STREAM, error));
292
293 if (fd->get() < 0) {
294 *error =
295 android::base::StringPrintf("could not connect to %s address '%s'",
296 it.first.c_str(), std::string(address).c_str());
297 return false;
298 }
299
300 if (serial) {
301 *serial = address;
302 }
303 return true;
304 }
305 }
306
307 *error = "unknown socket specification: ";
308 *error += address;
309 return false;
310 }
311
socket_spec_listen(std::string_view spec,std::string * error,int * resolved_port)312 int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) {
313 if (spec.starts_with("tcp:")) {
314 std::string hostname;
315 int port;
316 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
317 return -1;
318 }
319
320 int result;
321 #if ADB_HOST
322 if (hostname.empty() && gListenAll) {
323 #else
324 if (hostname.empty()) {
325 #endif
326 result = network_inaddr_any_server(port, SOCK_STREAM, error);
327 } else if (tcp_host_is_local(hostname)) {
328 result = network_loopback_server(port, SOCK_STREAM, error, true);
329 } else if (hostname == "::1") {
330 result = network_loopback_server(port, SOCK_STREAM, error, false);
331 } else {
332 // TODO: Implement me.
333 *error = "listening on specified hostname currently unsupported";
334 return -1;
335 }
336
337 if (result >= 0 && resolved_port) {
338 *resolved_port = adb_socket_get_local_port(result);
339 }
340 return result;
341 } else if (spec.starts_with("vsock:")) {
342 #if ADB_LINUX
343 std::string spec_str(spec);
344 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
345 if (fragments.size() != 2) {
346 *error = "given vsock server socket string was invalid";
347 return -1;
348 }
349 int port;
350 if (!android::base::ParseInt(fragments[1], &port)) {
351 *error = "could not parse vsock port";
352 errno = EINVAL;
353 return -1;
354 } else if (port < 0) {
355 *error = "vsock port was negative.";
356 errno = EINVAL;
357 return -1;
358 }
359 unique_fd serverfd(socket(AF_VSOCK, SOCK_STREAM, 0));
360 if (serverfd == -1) {
361 int error_num = errno;
362 *error = android::base::StringPrintf("could not create vsock server: '%s'",
363 strerror(error_num));
364 errno = error_num;
365 return -1;
366 }
367 sockaddr_vm addr{};
368 addr.svm_family = AF_VSOCK;
369 addr.svm_port = port == 0 ? VMADDR_PORT_ANY : port;
370 addr.svm_cid = VMADDR_CID_ANY;
371 socklen_t addr_len = sizeof(addr);
372 if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) {
373 return -1;
374 }
375 if (listen(serverfd.get(), 4)) {
376 return -1;
377 }
378 if (serverfd >= 0 && resolved_port) {
379 if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) {
380 *resolved_port = addr.svm_port;
381 } else {
382 return -1;
383 }
384 }
385 return serverfd.release();
386 #else // ADB_LINUX
387 *error = "vsock is only supported on linux";
388 return -1;
389 #endif // ADB_LINUX
390 } else if (ConsumePrefix(&spec, "acceptfd:")) {
391 #if ADB_WINDOWS
392 *error = "socket activation not supported under Windows";
393 return -1;
394 #else
395 // We inherited the socket from some kind of launcher. It's already bound and
396 // listening. Return a copy of the FD instead of the FD itself so we implement the
397 // normal "listen" contract and can succeed more than once.
398 unsigned int fd_u;
399 if (!ParseUint(&fd_u, spec) || fd_u > std::numeric_limits<int>::max()) {
400 *error = "invalid fd";
401 return -1;
402 }
403 int fd = static_cast<int>(fd_u);
404 int flags = get_fd_flags(fd);
405 if (flags < 0) {
406 *error = android::base::StringPrintf("could not get flags of inherited fd %d: '%s'", fd,
407 strerror(errno));
408 return -1;
409 }
410 if (flags & FD_CLOEXEC) {
411 *error = android::base::StringPrintf("fd %d was not inherited from parent", fd);
412 return -1;
413 }
414
415 int dummy_sock_type;
416 socklen_t dummy_sock_type_size = sizeof(dummy_sock_type);
417 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &dummy_sock_type, &dummy_sock_type_size)) {
418 *error = android::base::StringPrintf("fd %d does not refer to a socket", fd);
419 return -1;
420 }
421
422 int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
423 if (new_fd < 0) {
424 *error = android::base::StringPrintf("could not dup inherited fd %d: '%s'", fd,
425 strerror(errno));
426 return -1;
427 }
428 return new_fd;
429 #endif
430 }
431
432 for (const auto& it : kLocalSocketTypes) {
433 std::string prefix = it.first + ":";
434 if (spec.starts_with(prefix)) {
435 if (!it.second.available) {
436 *error = "attempted to listen on unavailable socket type: ";
437 *error += spec;
438 return -1;
439 }
440
441 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
442 SOCK_STREAM, error);
443 }
444 }
445
446 *error = "unknown socket specification:";
447 *error += spec;
448 return -1;
449 }
450