sockets: Handle race condition between binds to the same port

If an offset of ports is specified to the inet_listen_saddr function(),
and two or more processes tries to bind from these ports at the same time,
occasionally more than one process may be able to bind to the same
port. The condition is detected by listen() but too late to avoid a failure.

This function is called by socket_listen() and used
by all socket listening code in QEMU, so all cases where any form of dynamic
port selection is used should be subject to this issue.

Add code to close and re-establish the socket when this
condition is observed, hiding the race condition from the user.

Also clean up some issues with error handling to allow more
accurate reporting of the cause of an error.

This has been developed and tested by means of the
test-listen unit test in the previous commit.
Enable the test for make check now that it passes.

Reviewed-by: Bhavesh Davda <bhavesh.davda@oracle.com>
Reviewed-by: Yuval Shaia <yuval.shaia@oracle.com>
Reviewed-by: Girish Moodalbail <girish.moodalbail@oracle.com>
Signed-off-by: Knut Omang <knut.omang@oracle.com>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Knut Omang 2017-08-07 12:58:42 +02:00 committed by Daniel P. Berrange
parent 39f80521df
commit 9cf961bba7

View File

@ -206,7 +206,10 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
char port[33]; char port[33];
char uaddr[INET6_ADDRSTRLEN+1]; char uaddr[INET6_ADDRSTRLEN+1];
char uport[33]; char uport[33];
int slisten, rc, port_min, port_max, p; int rc, port_min, port_max, p;
int slisten = 0;
int saved_errno = 0;
bool socket_created = false;
Error *err = NULL; Error *err = NULL;
memset(&ai,0, sizeof(ai)); memset(&ai,0, sizeof(ai));
@ -258,7 +261,7 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
return -1; return -1;
} }
/* create socket + bind */ /* create socket + bind/listen */
for (e = res; e != NULL; e = e->ai_next) { for (e = res; e != NULL; e = e->ai_next) {
getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
uaddr,INET6_ADDRSTRLEN,uport,32, uaddr,INET6_ADDRSTRLEN,uport,32,
@ -266,37 +269,58 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
slisten = create_fast_reuse_socket(e); slisten = create_fast_reuse_socket(e);
if (slisten < 0) { if (slisten < 0) {
if (!e->ai_next) {
error_setg_errno(errp, errno, "Failed to create socket");
}
continue; continue;
} }
socket_created = true;
port_min = inet_getport(e); port_min = inet_getport(e);
port_max = saddr->has_to ? saddr->to + port_offset : port_min; port_max = saddr->has_to ? saddr->to + port_offset : port_min;
for (p = port_min; p <= port_max; p++) { for (p = port_min; p <= port_max; p++) {
inet_setport(e, p); inet_setport(e, p);
if (try_bind(slisten, saddr, e) >= 0) { rc = try_bind(slisten, saddr, e);
goto listen; if (rc) {
} if (errno == EADDRINUSE) {
if (p == port_max) { continue;
if (!e->ai_next) { } else {
error_setg_errno(errp, errno, "Failed to bind socket"); error_setg_errno(errp, errno, "Failed to bind socket");
goto listen_failed;
} }
} }
if (!listen(slisten, 1)) {
goto listen_ok;
}
if (errno != EADDRINUSE) {
error_setg_errno(errp, errno, "Failed to listen on socket");
goto listen_failed;
}
/* Someone else managed to bind to the same port and beat us
* to listen on it! Socket semantics does not allow us to
* recover from this situation, so we need to recreate the
* socket to allow bind attempts for subsequent ports:
*/
closesocket(slisten);
slisten = create_fast_reuse_socket(e);
if (slisten < 0) {
error_setg_errno(errp, errno,
"Failed to recreate failed listening socket");
goto listen_failed;
}
} }
}
error_setg_errno(errp, errno,
socket_created ?
"Failed to find an available port" :
"Failed to create a socket");
listen_failed:
saved_errno = errno;
if (slisten >= 0) {
closesocket(slisten); closesocket(slisten);
} }
freeaddrinfo(res); freeaddrinfo(res);
errno = saved_errno;
return -1; return -1;
listen: listen_ok:
if (listen(slisten,1) != 0) {
error_setg_errno(errp, errno, "Failed to listen on socket");
closesocket(slisten);
freeaddrinfo(res);
return -1;
}
if (update_addr) { if (update_addr) {
g_free(saddr->host); g_free(saddr->host);
saddr->host = g_strdup(uaddr); saddr->host = g_strdup(uaddr);