335584f
From: Eric Blake <eblake@redhat.com>
335584f
Date: Thu, 8 Jun 2017 17:26:17 -0500
335584f
Subject: [PATCH] nbd: Fix regression on resiliency to port scan
335584f
335584f
Back in qemu 2.5, qemu-nbd was immune to port probes (a transient
335584f
server would not quit, regardless of how many probe connections
335584f
came and went, until a connection actually negotiated).  But we
335584f
broke that in commit ee7d7aa when removing the return value to
335584f
nbd_client_new(), although that patch also introduced a bug causing
335584f
an assertion failure on a client that fails negotiation.  We then
335584f
made it worse during refactoring in commit 1a6245a (a segfault
335584f
before we could even assert); the (masked) assertion was cleaned
335584f
up in d3780c2 (still in 2.6), and just recently we finally fixed
335584f
the segfault ("nbd: Fully intialize client in case of failed
335584f
negotiation").  But that still means that ever since we added
335584f
TLS support to qemu-nbd, we have been vulnerable to an ill-timed
335584f
port-scan being able to cause a denial of service by taking down
335584f
qemu-nbd before a real client has a chance to connect.
335584f
335584f
Since negotiation is now handled asynchronously via coroutines,
335584f
we no longer have a synchronous point of return by re-adding a
335584f
return value to nbd_client_new().  So this patch instead wires
335584f
things up to pass the negotiation status through the close_fn
335584f
callback function.
335584f
335584f
Simple test across two terminals:
335584f
$ qemu-nbd -f raw -p 30001 file
335584f
$ nmap 127.0.0.1 -p 30001 && \
335584f
  qemu-io -c 'r 0 512' -f raw nbd://localhost:30001
335584f
335584f
Note that this patch does not change what constitutes successful
335584f
negotiation (thus, a client must enter transmission phase before
335584f
that client can be considered as a reason to terminate the server
335584f
when the connection ends).  Perhaps we may want to tweak things
335584f
in a later patch to also treat a client that uses NBD_OPT_ABORT
335584f
as being a 'successful' negotiation (the client correctly talked
335584f
the NBD protocol, and informed us it was not going to use our
335584f
export after all), but that's a discussion for another day.
335584f
335584f
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1451614
335584f
335584f
Signed-off-by: Eric Blake <eblake@redhat.com>
335584f
Message-Id: <20170608222617.20376-1-eblake@redhat.com>
335584f
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
335584f
(cherry picked from commit 0c9390d978cbf61e8f16c9f580fa96b305c43568)
335584f
---
335584f
 blockdev-nbd.c      |  6 +++++-
335584f
 include/block/nbd.h |  2 +-
335584f
 nbd/server.c        | 24 +++++++++++++++---------
335584f
 qemu-nbd.c          |  4 ++--
335584f
 4 files changed, 23 insertions(+), 13 deletions(-)
335584f
335584f
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
335584f
index 8a11807df3..8d7284ac56 100644
335584f
--- a/blockdev-nbd.c
335584f
+++ b/blockdev-nbd.c
335584f
@@ -27,6 +27,10 @@ typedef struct NBDServerData {
335584f
 
335584f
 static NBDServerData *nbd_server;
335584f
 
335584f
+static void nbd_blockdev_client_closed(NBDClient *client, bool ignored)
335584f
+{
335584f
+    nbd_client_put(client);
335584f
+}
335584f
 
335584f
 static gboolean nbd_accept(QIOChannel *ioc, GIOCondition condition,
335584f
                            gpointer opaque)
335584f
@@ -46,7 +50,7 @@ static gboolean nbd_accept(QIOChannel *ioc, GIOCondition condition,
335584f
     qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server");
335584f
     nbd_client_new(NULL, cioc,
335584f
                    nbd_server->tlscreds, NULL,
335584f
-                   nbd_client_put);
335584f
+                   nbd_blockdev_client_closed);
335584f
     object_unref(OBJECT(cioc));
335584f
     return TRUE;
335584f
 }
335584f
diff --git a/include/block/nbd.h b/include/block/nbd.h
335584f
index 3e373f0498..b69c30d063 100644
335584f
--- a/include/block/nbd.h
335584f
+++ b/include/block/nbd.h
335584f
@@ -160,7 +160,7 @@ void nbd_client_new(NBDExport *exp,
335584f
                     QIOChannelSocket *sioc,
335584f
                     QCryptoTLSCreds *tlscreds,
335584f
                     const char *tlsaclname,
335584f
-                    void (*close)(NBDClient *));
335584f
+                    void (*close_fn)(NBDClient *, bool));
335584f
 void nbd_client_get(NBDClient *client);
335584f
 void nbd_client_put(NBDClient *client);
335584f
 
335584f
diff --git a/nbd/server.c b/nbd/server.c
335584f
index edfda84d43..a98bb21a0a 100644
335584f
--- a/nbd/server.c
335584f
+++ b/nbd/server.c
335584f
@@ -81,7 +81,7 @@ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
335584f
 
335584f
 struct NBDClient {
335584f
     int refcount;
335584f
-    void (*close)(NBDClient *client);
335584f
+    void (*close_fn)(NBDClient *client, bool negotiated);
335584f
 
335584f
     bool no_zeroes;
335584f
     NBDExport *exp;
335584f
@@ -796,7 +796,7 @@ void nbd_client_put(NBDClient *client)
335584f
     }
335584f
 }
335584f
 
335584f
-static void client_close(NBDClient *client)
335584f
+static void client_close(NBDClient *client, bool negotiated)
335584f
 {
335584f
     if (client->closing) {
335584f
         return;
335584f
@@ -811,8 +811,8 @@ static void client_close(NBDClient *client)
335584f
                          NULL);
335584f
 
335584f
     /* Also tell the client, so that they release their reference.  */
335584f
-    if (client->close) {
335584f
-        client->close(client);
335584f
+    if (client->close_fn) {
335584f
+        client->close_fn(client, negotiated);
335584f
     }
335584f
 }
335584f
 
335584f
@@ -993,7 +993,7 @@ void nbd_export_close(NBDExport *exp)
335584f
 
335584f
     nbd_export_get(exp);
335584f
     QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
335584f
-        client_close(client);
335584f
+        client_close(client, true);
335584f
     }
335584f
     nbd_export_set_name(exp, NULL);
335584f
     nbd_export_set_description(exp, NULL);
335584f
@@ -1355,7 +1355,7 @@ done:
335584f
 
335584f
 out:
335584f
     nbd_request_put(req);
335584f
-    client_close(client);
335584f
+    client_close(client, true);
335584f
     nbd_client_put(client);
335584f
 }
335584f
 
335584f
@@ -1381,7 +1381,7 @@ static coroutine_fn void nbd_co_client_start(void *opaque)
335584f
     qemu_co_mutex_init(&client->send_lock);
335584f
 
335584f
     if (nbd_negotiate(data)) {
335584f
-        client_close(client);
335584f
+        client_close(client, false);
335584f
         goto out;
335584f
     }
335584f
 
335584f
@@ -1391,11 +1391,17 @@ out:
335584f
     g_free(data);
335584f
 }
335584f
 
335584f
+/*
335584f
+ * Create a new client listener on the given export @exp, using the
335584f
+ * given channel @sioc.  Begin servicing it in a coroutine.  When the
335584f
+ * connection closes, call @close_fn with an indication of whether the
335584f
+ * client completed negotiation.
335584f
+ */
335584f
 void nbd_client_new(NBDExport *exp,
335584f
                     QIOChannelSocket *sioc,
335584f
                     QCryptoTLSCreds *tlscreds,
335584f
                     const char *tlsaclname,
335584f
-                    void (*close_fn)(NBDClient *))
335584f
+                    void (*close_fn)(NBDClient *, bool))
335584f
 {
335584f
     NBDClient *client;
335584f
     NBDClientNewData *data = g_new(NBDClientNewData, 1);
335584f
@@ -1412,7 +1418,7 @@ void nbd_client_new(NBDExport *exp,
335584f
     object_ref(OBJECT(client->sioc));
335584f
     client->ioc = QIO_CHANNEL(sioc);
335584f
     object_ref(OBJECT(client->ioc));
335584f
-    client->close = close_fn;
335584f
+    client->close_fn = close_fn;
335584f
 
335584f
     data->client = client;
335584f
     data->co = qemu_coroutine_create(nbd_co_client_start, data);
335584f
diff --git a/qemu-nbd.c b/qemu-nbd.c
335584f
index b44764eb87..483dd77a77 100644
335584f
--- a/qemu-nbd.c
335584f
+++ b/qemu-nbd.c
335584f
@@ -335,10 +335,10 @@ static void nbd_export_closed(NBDExport *exp)
335584f
 
335584f
 static void nbd_update_server_watch(void);
335584f
 
335584f
-static void nbd_client_closed(NBDClient *client)
335584f
+static void nbd_client_closed(NBDClient *client, bool negotiated)
335584f
 {
335584f
     nb_fds--;
335584f
-    if (nb_fds == 0 && !persistent && state == RUNNING) {
335584f
+    if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
335584f
         state = TERMINATE;
335584f
     }
335584f
     nbd_update_server_watch();