yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-nbd-Fix-regression-on-resiliency-to-port-scan.patch

619821
From 5bd3c61792fe793b1d42e675b53e47396f4219a3 Mon Sep 17 00:00:00 2001
95f93f
From: Eric Blake <eblake@redhat.com>
619821
Date: Fri, 9 Jun 2017 22:04:13 +0200
619821
Subject: [PATCH 6/6] nbd: Fix regression on resiliency to port scan
95f93f
95f93f
RH-Author: Eric Blake <eblake@redhat.com>
619821
Message-id: <20170609220413.28793-3-eblake@redhat.com>
619821
Patchwork-id: 75575
619821
O-Subject: [RHEL-7.4 qemu-kvm PATCH 2/2] nbd: Fix regression on resiliency to port scan
619821
Bugzilla: 1451614
619821
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
95f93f
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
95f93f
RH-Acked-by: Thomas Huth <thuth@redhat.com>
95f93f
95f93f
Back in qemu 2.5, qemu-nbd was immune to port probes (a transient
95f93f
server would not quit, regardless of how many probe connections
95f93f
came and went, until a connection actually negotiated).  But we
95f93f
broke that in commit ee7d7aa when removing the return value to
95f93f
nbd_client_new(), although that patch also introduced a bug causing
95f93f
an assertion failure on a client that fails negotiation.  We then
95f93f
made it worse during refactoring in commit 1a6245a (a segfault
95f93f
before we could even assert); the (masked) assertion was cleaned
95f93f
up in d3780c2 (still in 2.6), and just recently we finally fixed
95f93f
the segfault ("nbd: Fully intialize client in case of failed
95f93f
negotiation").  But that still means that ever since we added
95f93f
TLS support to qemu-nbd, we have been vulnerable to an ill-timed
95f93f
port-scan being able to cause a denial of service by taking down
95f93f
qemu-nbd before a real client has a chance to connect.
95f93f
95f93f
Since negotiation is now handled asynchronously via coroutines,
95f93f
we no longer have a synchronous point of return by re-adding a
95f93f
return value to nbd_client_new().  So this patch instead wires
95f93f
things up to pass the negotiation status through the close_fn
95f93f
callback function.
95f93f
95f93f
Simple test across two terminals:
95f93f
$ qemu-nbd -f raw -p 30001 file
95f93f
$ nmap 127.0.0.1 -p 30001 && \
95f93f
  qemu-io -c 'r 0 512' -f raw nbd://localhost:30001
95f93f
95f93f
Note that this patch does not change what constitutes successful
95f93f
negotiation (thus, a client must enter transmission phase before
95f93f
that client can be considered as a reason to terminate the server
95f93f
when the connection ends).  Perhaps we may want to tweak things
95f93f
in a later patch to also treat a client that uses NBD_OPT_ABORT
95f93f
as being a 'successful' negotiation (the client correctly talked
95f93f
the NBD protocol, and informed us it was not going to use our
95f93f
export after all), but that's a discussion for another day.
95f93f
95f93f
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1451614
95f93f
95f93f
Signed-off-by: Eric Blake <eblake@redhat.com>
95f93f
Message-Id: <20170608222617.20376-1-eblake@redhat.com>
95f93f
(cherry picked from commit ???)
619821
https://bugzilla.redhat.com/show_bug.cgi?id=1451614
95f93f
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
95f93f
95f93f
Conflicts:
95f93f
	blockdev-nbd.c - context
95f93f
	include/block/nbd.h - context with nbd_client_new parameters,
95f93f
	   nbd_client_close not static
95f93f
	nbd/server.c - in nbd.c instead, context, comment to nbd_client_new
95f93f
	   matching different parameters
95f93f
	qemu-nbd.c - context
95f93f
---
95f93f
 blockdev-nbd.c      |  7 ++++++-
95f93f
 include/block/nbd.h |  5 +++--
95f93f
 nbd.c               | 25 ++++++++++++++++---------
95f93f
 qemu-nbd.c          |  4 ++--
95f93f
 4 files changed, 27 insertions(+), 14 deletions(-)
95f93f
95f93f
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
95f93f
index 922cf56..dc7cffa 100644
95f93f
--- a/blockdev-nbd.c
95f93f
+++ b/blockdev-nbd.c
95f93f
@@ -21,6 +21,11 @@
95f93f
 
95f93f
 static int server_fd = -1;
95f93f
 
95f93f
+static void nbd_blockdev_client_closed(NBDClient *client, bool ignored)
95f93f
+{
95f93f
+    nbd_client_put(client);
95f93f
+}
95f93f
+
95f93f
 static void nbd_accept(void *opaque)
95f93f
 {
95f93f
     struct sockaddr_in addr;
95f93f
@@ -28,7 +33,7 @@ static void nbd_accept(void *opaque)
95f93f
 
95f93f
     int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
95f93f
     if (fd >= 0) {
95f93f
-        nbd_client_new(NULL, fd, nbd_client_put);
95f93f
+        nbd_client_new(NULL, fd, nbd_blockdev_client_closed);
95f93f
     }
95f93f
 }
95f93f
 
95f93f
diff --git a/include/block/nbd.h b/include/block/nbd.h
95f93f
index 92e360e..963c7ce 100644
95f93f
--- a/include/block/nbd.h
95f93f
+++ b/include/block/nbd.h
95f93f
@@ -92,8 +92,9 @@ NBDExport *nbd_export_find(const char *name);
95f93f
 void nbd_export_set_name(NBDExport *exp, const char *name);
95f93f
 void nbd_export_close_all(void);
95f93f
 
95f93f
-void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *));
95f93f
-void nbd_client_close(NBDClient *client);
95f93f
+void nbd_client_new(NBDExport *exp, int csock,
95f93f
+                    void (*close_fn)(NBDClient *, bool));
95f93f
+void nbd_client_close(NBDClient *client, bool negotiated);
95f93f
 void nbd_client_get(NBDClient *client);
95f93f
 void nbd_client_put(NBDClient *client);
95f93f
 
95f93f
diff --git a/nbd.c b/nbd.c
95f93f
index b5cdc1b..c3d554d 100644
95f93f
--- a/nbd.c
95f93f
+++ b/nbd.c
95f93f
@@ -105,7 +105,7 @@ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
95f93f
 
95f93f
 struct NBDClient {
95f93f
     int refcount;
95f93f
-    void (*close)(NBDClient *client);
95f93f
+    void (*close_fn)(NBDClient *client, bool negotiated);
95f93f
 
95f93f
     NBDExport *exp;
95f93f
     int sock;
95f93f
@@ -862,7 +862,7 @@ void nbd_client_put(NBDClient *client)
95f93f
     }
95f93f
 }
95f93f
 
95f93f
-void nbd_client_close(NBDClient *client)
95f93f
+void nbd_client_close(NBDClient *client, bool negotiated)
95f93f
 {
95f93f
     if (client->closing) {
95f93f
         return;
95f93f
@@ -876,8 +876,8 @@ void nbd_client_close(NBDClient *client)
95f93f
     shutdown(client->sock, 2);
95f93f
 
95f93f
     /* Also tell the client, so that they release their reference.  */
95f93f
-    if (client->close) {
95f93f
-        client->close(client);
95f93f
+    if (client->close_fn) {
95f93f
+        client->close_fn(client, negotiated);
95f93f
     }
95f93f
 }
95f93f
 
95f93f
@@ -964,7 +964,7 @@ void nbd_export_close(NBDExport *exp)
95f93f
 
95f93f
     nbd_export_get(exp);
95f93f
     QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
95f93f
-        nbd_client_close(client);
95f93f
+        nbd_client_close(client, true);
95f93f
     }
95f93f
     nbd_export_set_name(exp, NULL);
95f93f
     nbd_export_put(exp);
95f93f
@@ -1244,7 +1244,7 @@ done:
95f93f
 
95f93f
 out:
95f93f
     nbd_request_put(req);
95f93f
-    nbd_client_close(client);
95f93f
+    nbd_client_close(client, true);
95f93f
 }
95f93f
 
95f93f
 static int nbd_can_read(void *opaque)
95f93f
@@ -1285,7 +1285,7 @@ static coroutine_fn void nbd_co_client_start(void *opaque)
95f93f
     qemu_set_nonblock(client->sock);
95f93f
     qemu_co_mutex_init(&client->send_lock);
95f93f
     if (nbd_negotiate(data)) {
95f93f
-        nbd_client_close(client);
95f93f
+        nbd_client_close(client, false);
95f93f
         goto out;
95f93f
     }
95f93f
     qemu_set_fd_handler2(client->sock, nbd_can_read, nbd_read, NULL, client);
95f93f
@@ -1294,7 +1294,14 @@ out:
95f93f
     g_free(data);
95f93f
 }
95f93f
 
95f93f
-void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *))
95f93f
+/*
95f93f
+ * Create a new client listener on the given export @exp, using the
95f93f
+ * given socket @csock.  Begin servicing it in a coroutine.  When the
95f93f
+ * connection closes, call @close_fn with an indication of whether the
95f93f
+ * client completed negotiation.
95f93f
+ */
95f93f
+void nbd_client_new(NBDExport *exp, int csock,
95f93f
+                    void (*close_fn)(NBDClient *, bool))
95f93f
 {
95f93f
     NBDClient *client;
95f93f
     NBDClientNewData *data = g_new(NBDClientNewData, 1);
95f93f
@@ -1303,7 +1310,7 @@ void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *))
95f93f
     client->refcount = 1;
95f93f
     client->exp = exp;
95f93f
     client->sock = csock;
95f93f
-    client->close = close_fn;
95f93f
+    client->close_fn = close_fn;
95f93f
 
95f93f
     data->client = client;
95f93f
     data->co = qemu_coroutine_create(nbd_co_client_start);
95f93f
diff --git a/qemu-nbd.c b/qemu-nbd.c
95f93f
index cde7431..e0f4517 100644
95f93f
--- a/qemu-nbd.c
95f93f
+++ b/qemu-nbd.c
95f93f
@@ -276,10 +276,10 @@ static void nbd_export_closed(NBDExport *exp)
95f93f
     state = TERMINATED;
95f93f
 }
95f93f
 
95f93f
-static void nbd_client_closed(NBDClient *client)
95f93f
+static void nbd_client_closed(NBDClient *client, bool negotiated)
95f93f
 {
95f93f
     nb_fds--;
95f93f
-    if (nb_fds == 0 && !persistent && state == RUNNING) {
95f93f
+    if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
95f93f
         state = TERMINATE;
95f93f
     }
95f93f
     qemu_notify_event();
95f93f
-- 
95f93f
1.8.3.1
95f93f