thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

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

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