Merge changes I0ee130db,I33d356fd am: e461b37965 am: 59561a64b7
am: 87ff463d6e
Change-Id: I72ac706e6866b6f391cc950a677a598df79fa9a0
diff --git a/adb/adb_auth_client.cpp b/adb/adb_auth_client.cpp
index 9d8dfc0..84ad6ef 100644
--- a/adb/adb_auth_client.cpp
+++ b/adb/adb_auth_client.cpp
@@ -151,9 +151,7 @@
}
static void adb_auth_listener(int fd, unsigned events, void* data) {
- sockaddr_storage addr;
- socklen_t alen = sizeof(addr);
- int s = adb_socket_accept(fd, reinterpret_cast<sockaddr*>(&addr), &alen);
+ int s = adb_socket_accept(fd, nullptr, nullptr);
if (s < 0) {
PLOG(ERROR) << "Failed to accept";
return;
diff --git a/adb/adb_listeners.cpp b/adb/adb_listeners.cpp
index f54603c..0299be3 100644
--- a/adb/adb_listeners.cpp
+++ b/adb/adb_listeners.cpp
@@ -71,10 +71,7 @@
static void ss_listener_event_func(int _fd, unsigned ev, void *_l) {
if (ev & FDE_READ) {
- sockaddr_storage ss;
- sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss);
- socklen_t alen = sizeof(ss);
- int fd = adb_socket_accept(_fd, addrp, &alen);
+ int fd = adb_socket_accept(_fd, nullptr, nullptr);
if (fd < 0) return;
int rcv_buf_size = CHUNK_SIZE;
@@ -96,13 +93,7 @@
asocket *s;
if (ev & FDE_READ) {
- sockaddr_storage ss;
- sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss);
- socklen_t alen;
- int fd;
-
- alen = sizeof(ss);
- fd = adb_socket_accept(_fd, addrp, &alen);
+ int fd = adb_socket_accept(_fd, nullptr, nullptr);
if (fd < 0) {
return;
}
diff --git a/adb/jdwp_service.cpp b/adb/jdwp_service.cpp
index 7a44801..027821a 100644
--- a/adb/jdwp_service.cpp
+++ b/adb/jdwp_service.cpp
@@ -444,10 +444,7 @@
JdwpControl* control = (JdwpControl*)_control;
if (events & FDE_READ) {
- sockaddr_storage ss;
- sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss);
- socklen_t addrlen = sizeof(ss);
- int s = adb_socket_accept(control->listen_socket, addrp, &addrlen);
+ int s = adb_socket_accept(control->listen_socket, nullptr, nullptr);
if (s < 0) {
if (errno == ECONNABORTED) {
/* oops, the JDWP process died really quick */
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index 333b861..322f992 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -285,6 +285,10 @@
#undef accept
#define accept ___xxx_accept
+int adb_getsockname(int fd, struct sockaddr* sockaddr, socklen_t* optlen);
+#undef getsockname
+#define getsockname(...) ___xxx_getsockname(__VA__ARGS__)
+
// Returns the local port number of a bound socket, or -1 on failure.
int adb_socket_get_local_port(int fd);
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index 109a548..5fda27b 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -1061,7 +1061,7 @@
return -1;
}
- int result = getsockname(fh->fh_socket, sockaddr, optlen);
+ int result = (getsockname)(fh->fh_socket, sockaddr, optlen);
if (result == SOCKET_ERROR) {
const DWORD err = WSAGetLastError();
D("adb_getsockname: setsockopt on fd %d failed: %s\n", fd,
@@ -1119,6 +1119,11 @@
int local_port = -1;
std::string error;
+ struct sockaddr_storage peer_addr = {};
+ struct sockaddr_storage client_addr = {};
+ socklen_t peer_socklen = sizeof(peer_addr);
+ socklen_t client_socklen = sizeof(client_addr);
+
server = network_loopback_server(0, SOCK_STREAM, &error);
if (server < 0) {
D("adb_socketpair: failed to create server: %s", error.c_str());
@@ -1138,12 +1143,32 @@
goto fail;
}
- accepted = adb_socket_accept(server, nullptr, nullptr);
+ // Make sure that the peer that connected to us and the client are the same.
+ accepted = adb_socket_accept(server, reinterpret_cast<sockaddr*>(&peer_addr), &peer_socklen);
if (accepted < 0) {
D("adb_socketpair: failed to accept: %s", strerror(errno));
goto fail;
}
+
+ if (adb_getsockname(client, reinterpret_cast<sockaddr*>(&client_addr), &client_socklen) != 0) {
+ D("adb_socketpair: failed to getpeername: %s", strerror(errno));
+ goto fail;
+ }
+
+ if (peer_socklen != client_socklen) {
+ D("adb_socketpair: client and peer sockaddrs have different lengths");
+ errno = EIO;
+ goto fail;
+ }
+
+ if (memcmp(&peer_addr, &client_addr, peer_socklen) != 0) {
+ D("adb_socketpair: client and peer sockaddrs don't match");
+ errno = EIO;
+ goto fail;
+ }
+
adb_close(server);
+
sv[0] = client;
sv[1] = accepted;
return 0;
diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp
index cc19785..395b893 100644
--- a/adb/transport_local.cpp
+++ b/adb/transport_local.cpp
@@ -202,9 +202,6 @@
static void server_socket_thread(void* arg) {
int serverfd, fd;
- sockaddr_storage ss;
- sockaddr *addrp = reinterpret_cast<sockaddr*>(&ss);
- socklen_t alen;
int port = (int) (uintptr_t) arg;
adb_thread_setname("server socket");
@@ -222,9 +219,8 @@
close_on_exec(serverfd);
}
- alen = sizeof(ss);
D("server: trying to get new connection from %d", port);
- fd = adb_socket_accept(serverfd, addrp, &alen);
+ fd = adb_socket_accept(serverfd, nullptr, nullptr);
if(fd >= 0) {
D("server: new connection on fd %d", fd);
close_on_exec(fd);