yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
Blob Blame History Raw
From 5bd3c61792fe793b1d42e675b53e47396f4219a3 Mon Sep 17 00:00:00 2001
From: Eric Blake <eblake@redhat.com>
Date: Fri, 9 Jun 2017 22:04:13 +0200
Subject: [PATCH 6/6] nbd: Fix regression on resiliency to port scan

RH-Author: Eric Blake <eblake@redhat.com>
Message-id: <20170609220413.28793-3-eblake@redhat.com>
Patchwork-id: 75575
O-Subject: [RHEL-7.4 qemu-kvm PATCH 2/2] nbd: Fix regression on resiliency to port scan
Bugzilla: 1451614
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Thomas Huth <thuth@redhat.com>

Back in qemu 2.5, qemu-nbd was immune to port probes (a transient
server would not quit, regardless of how many probe connections
came and went, until a connection actually negotiated).  But we
broke that in commit ee7d7aa when removing the return value to
nbd_client_new(), although that patch also introduced a bug causing
an assertion failure on a client that fails negotiation.  We then
made it worse during refactoring in commit 1a6245a (a segfault
before we could even assert); the (masked) assertion was cleaned
up in d3780c2 (still in 2.6), and just recently we finally fixed
the segfault ("nbd: Fully intialize client in case of failed
negotiation").  But that still means that ever since we added
TLS support to qemu-nbd, we have been vulnerable to an ill-timed
port-scan being able to cause a denial of service by taking down
qemu-nbd before a real client has a chance to connect.

Since negotiation is now handled asynchronously via coroutines,
we no longer have a synchronous point of return by re-adding a
return value to nbd_client_new().  So this patch instead wires
things up to pass the negotiation status through the close_fn
callback function.

Simple test across two terminals:
$ qemu-nbd -f raw -p 30001 file
$ nmap 127.0.0.1 -p 30001 && \
  qemu-io -c 'r 0 512' -f raw nbd://localhost:30001

Note that this patch does not change what constitutes successful
negotiation (thus, a client must enter transmission phase before
that client can be considered as a reason to terminate the server
when the connection ends).  Perhaps we may want to tweak things
in a later patch to also treat a client that uses NBD_OPT_ABORT
as being a 'successful' negotiation (the client correctly talked
the NBD protocol, and informed us it was not going to use our
export after all), but that's a discussion for another day.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1451614

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20170608222617.20376-1-eblake@redhat.com>
(cherry picked from commit ???)
https://bugzilla.redhat.com/show_bug.cgi?id=1451614
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>

Conflicts:
	blockdev-nbd.c - context
	include/block/nbd.h - context with nbd_client_new parameters,
	   nbd_client_close not static
	nbd/server.c - in nbd.c instead, context, comment to nbd_client_new
	   matching different parameters
	qemu-nbd.c - context
---
 blockdev-nbd.c      |  7 ++++++-
 include/block/nbd.h |  5 +++--
 nbd.c               | 25 ++++++++++++++++---------
 qemu-nbd.c          |  4 ++--
 4 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/blockdev-nbd.c b/blockdev-nbd.c
index 922cf56..dc7cffa 100644
--- a/blockdev-nbd.c
+++ b/blockdev-nbd.c
@@ -21,6 +21,11 @@
 
 static int server_fd = -1;
 
+static void nbd_blockdev_client_closed(NBDClient *client, bool ignored)
+{
+    nbd_client_put(client);
+}
+
 static void nbd_accept(void *opaque)
 {
     struct sockaddr_in addr;
@@ -28,7 +33,7 @@ static void nbd_accept(void *opaque)
 
     int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
     if (fd >= 0) {
-        nbd_client_new(NULL, fd, nbd_client_put);
+        nbd_client_new(NULL, fd, nbd_blockdev_client_closed);
     }
 }
 
diff --git a/include/block/nbd.h b/include/block/nbd.h
index 92e360e..963c7ce 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -92,8 +92,9 @@ NBDExport *nbd_export_find(const char *name);
 void nbd_export_set_name(NBDExport *exp, const char *name);
 void nbd_export_close_all(void);
 
-void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *));
-void nbd_client_close(NBDClient *client);
+void nbd_client_new(NBDExport *exp, int csock,
+                    void (*close_fn)(NBDClient *, bool));
+void nbd_client_close(NBDClient *client, bool negotiated);
 void nbd_client_get(NBDClient *client);
 void nbd_client_put(NBDClient *client);
 
diff --git a/nbd.c b/nbd.c
index b5cdc1b..c3d554d 100644
--- a/nbd.c
+++ b/nbd.c
@@ -105,7 +105,7 @@ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
 
 struct NBDClient {
     int refcount;
-    void (*close)(NBDClient *client);
+    void (*close_fn)(NBDClient *client, bool negotiated);
 
     NBDExport *exp;
     int sock;
@@ -862,7 +862,7 @@ void nbd_client_put(NBDClient *client)
     }
 }
 
-void nbd_client_close(NBDClient *client)
+void nbd_client_close(NBDClient *client, bool negotiated)
 {
     if (client->closing) {
         return;
@@ -876,8 +876,8 @@ void nbd_client_close(NBDClient *client)
     shutdown(client->sock, 2);
 
     /* Also tell the client, so that they release their reference.  */
-    if (client->close) {
-        client->close(client);
+    if (client->close_fn) {
+        client->close_fn(client, negotiated);
     }
 }
 
@@ -964,7 +964,7 @@ void nbd_export_close(NBDExport *exp)
 
     nbd_export_get(exp);
     QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
-        nbd_client_close(client);
+        nbd_client_close(client, true);
     }
     nbd_export_set_name(exp, NULL);
     nbd_export_put(exp);
@@ -1244,7 +1244,7 @@ done:
 
 out:
     nbd_request_put(req);
-    nbd_client_close(client);
+    nbd_client_close(client, true);
 }
 
 static int nbd_can_read(void *opaque)
@@ -1285,7 +1285,7 @@ static coroutine_fn void nbd_co_client_start(void *opaque)
     qemu_set_nonblock(client->sock);
     qemu_co_mutex_init(&client->send_lock);
     if (nbd_negotiate(data)) {
-        nbd_client_close(client);
+        nbd_client_close(client, false);
         goto out;
     }
     qemu_set_fd_handler2(client->sock, nbd_can_read, nbd_read, NULL, client);
@@ -1294,7 +1294,14 @@ out:
     g_free(data);
 }
 
-void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *))
+/*
+ * Create a new client listener on the given export @exp, using the
+ * given socket @csock.  Begin servicing it in a coroutine.  When the
+ * connection closes, call @close_fn with an indication of whether the
+ * client completed negotiation.
+ */
+void nbd_client_new(NBDExport *exp, int csock,
+                    void (*close_fn)(NBDClient *, bool))
 {
     NBDClient *client;
     NBDClientNewData *data = g_new(NBDClientNewData, 1);
@@ -1303,7 +1310,7 @@ void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *))
     client->refcount = 1;
     client->exp = exp;
     client->sock = csock;
-    client->close = close_fn;
+    client->close_fn = close_fn;
 
     data->client = client;
     data->co = qemu_coroutine_create(nbd_co_client_start);
diff --git a/qemu-nbd.c b/qemu-nbd.c
index cde7431..e0f4517 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -276,10 +276,10 @@ static void nbd_export_closed(NBDExport *exp)
     state = TERMINATED;
 }
 
-static void nbd_client_closed(NBDClient *client)
+static void nbd_client_closed(NBDClient *client, bool negotiated)
 {
     nb_fds--;
-    if (nb_fds == 0 && !persistent && state == RUNNING) {
+    if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
         state = TERMINATE;
     }
     qemu_notify_event();
-- 
1.8.3.1