peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0103-nbd-Fix-regression-on-resiliency-to-port-scan.patch

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