From: Darren Kenny <darren.kenny@oracle.com>
To: "Daniel P. Berrange" <berrange@redhat.com>
Cc: qemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH] sockets: avoid leak of listen file descriptor
Date: Fri, 20 Oct 2017 11:02:43 +0100 [thread overview]
Message-ID: <20171020100243.riam33tacf6smb7s@starbug-vm.ie.oracle.com> (raw)
In-Reply-To: <20171020092844.13880-1-berrange@redhat.com>
Hi Daniel,
On Fri, Oct 20, 2017 at 10:28:44AM +0100, Daniel P. Berrange wrote:
>If we iterate over the full port range without successfully binding+listening
>on the socket, we'll try the next address, whereupon we overwrite the slisten
>file descriptor variable without closing it.
>
>Rather than having two places where we open + close socket FDs on different
>iterations of nested for loops, re-arrange the code to always open+close
>within the same loop iteration.
>
>Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
>---
> util/qemu-sockets.c | 31 +++++++++++++++++++------------
> 1 file changed, 19 insertions(+), 12 deletions(-)
>
>diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
>index b47fb45885..a319338cca 100644
>--- a/util/qemu-sockets.c
>+++ b/util/qemu-sockets.c
>@@ -207,7 +207,7 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
> char uaddr[INET6_ADDRSTRLEN+1];
> char uport[33];
> int rc, port_min, port_max, p;
>- int slisten = 0;
>+ int slisten = -1;
> int saved_errno = 0;
> bool socket_created = false;
> Error *err = NULL;
>@@ -267,16 +267,28 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
> uaddr,INET6_ADDRSTRLEN,uport,32,
> NI_NUMERICHOST | NI_NUMERICSERV);
>
>- slisten = create_fast_reuse_socket(e);
>- if (slisten < 0) {
>- continue;
>- }
>-
> socket_created = true;
Is this setting of socket_created correct now? Maybe it should be moved to
before the try_bind() below?
> port_min = inet_getport(e);
> port_max = saddr->has_to ? saddr->to + port_offset : port_min;
> for (p = port_min; p <= port_max; p++) {
> inet_setport(e, p);
>+
>+ slisten = create_fast_reuse_socket(e);
>+ if (slisten < 0) {
>+ /* First time we expect we might fail to create the socket
>+ * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded.
>+ * Later iterations should always succeeed if first iteration
>+ * worked though, so treat that as fatal.
>+ */
>+ if (p == port_min) {
>+ continue;
>+ } else {
>+ error_setg_errno(errp, errno,
>+ "Failed to recreate failed listening socket");
>+ goto listen_failed;
>+ }
>+ }
>+
> rc = try_bind(slisten, saddr, e);
> if (rc) {
> if (errno == EADDRINUSE) {
>@@ -299,12 +311,7 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
> * 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;
>- }
>+ slisten = -1;
> }
> }
> error_setg_errno(errp, errno,
Thanks,
Darren.
prev parent reply other threads:[~2017-10-20 10:03 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-20 9:28 [Qemu-devel] [PATCH] sockets: avoid leak of listen file descriptor Daniel P. Berrange
2017-10-20 9:54 ` Daniel P. Berrange
2017-10-20 10:03 ` Darren Kenny
2017-10-20 10:02 ` Darren Kenny [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171020100243.riam33tacf6smb7s@starbug-vm.ie.oracle.com \
--to=darren.kenny@oracle.com \
--cc=berrange@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).