619821
From 0e97bcfd7ab3c7b3d489de3cf4c7c4977b73cd23 Mon Sep 17 00:00:00 2001
95f93f
From: Eric Blake <eblake@redhat.com>
619821
Date: Fri, 9 Jun 2017 22:04:12 +0200
619821
Subject: [PATCH 5/6] nbd: Fully initialize client in case of failed
95f93f
 negotiation
95f93f
95f93f
RH-Author: Eric Blake <eblake@redhat.com>
619821
Message-id: <20170609220413.28793-2-eblake@redhat.com>
619821
Patchwork-id: 75576
619821
O-Subject: [RHEL-7.4 qemu-kvm PATCH 1/2] nbd: Fully initialize client in case of failed negotiation
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
If a non-NBD client connects to qemu-nbd, we would end up with
95f93f
a SIGSEGV in nbd_client_put() because we were trying to
95f93f
unregister the client's association to the export, even though
95f93f
we skipped inserting the client into that list.  Easy trigger
95f93f
in two terminals:
95f93f
95f93f
$ qemu-nbd -p 30001 --format=raw file
95f93f
$ nmap 127.0.0.1 -p 30001
95f93f
95f93f
nmap claims that it thinks it connected to a pago-services1
95f93f
server (which probably means nmap could be updated to learn the
95f93f
NBD protocol and give a more accurate diagnosis of the open
95f93f
port - but that's not our problem), then terminates immediately,
95f93f
so our call to nbd_negotiate() fails.  The fix is to reorder
95f93f
nbd_co_client_start() to ensure that all initialization occurs
95f93f
before we ever try talking to a client in nbd_negotiate(), so
95f93f
that the teardown sequence on negotiation failure doesn't fault
95f93f
while dereferencing a half-initialized object.
95f93f
95f93f
While debugging this, I also noticed that nbd_update_server_watch()
95f93f
called by nbd_client_closed() was still adding a channel to accept
95f93f
the next client, even when the state was no longer RUNNING.  That
95f93f
is fixed by making nbd_can_accept() pay attention to the current
95f93f
state.
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: <20170527030421.28366-1-eblake@redhat.com>
95f93f
Signed-off-by: Paolo Bonzini <pbonzini@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
	nbd/server.c - code lives in nbd.c instead, with older handlers
95f93f
---
95f93f
 nbd.c      | 6 ++----
95f93f
 qemu-nbd.c | 2 +-
95f93f
 2 files changed, 3 insertions(+), 5 deletions(-)
95f93f
95f93f
diff --git a/nbd.c b/nbd.c
95f93f
index 8a32e18..b5cdc1b 100644
95f93f
--- a/nbd.c
95f93f
+++ b/nbd.c
95f93f
@@ -1280,18 +1280,16 @@ static coroutine_fn void nbd_co_client_start(void *opaque)
95f93f
 
95f93f
     if (exp) {
95f93f
         nbd_export_get(exp);
95f93f
+        QTAILQ_INSERT_TAIL(&exp->clients, client, next);
95f93f
     }
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
         goto out;
95f93f
     }
95f93f
-    qemu_co_mutex_init(&client->send_lock);
95f93f
     qemu_set_fd_handler2(client->sock, nbd_can_read, nbd_read, NULL, client);
95f93f
 
95f93f
-    if (exp) {
95f93f
-        QTAILQ_INSERT_TAIL(&exp->clients, client, next);
95f93f
-    }
95f93f
 out:
95f93f
     g_free(data);
95f93f
 }
95f93f
diff --git a/qemu-nbd.c b/qemu-nbd.c
95f93f
index 047dd49..cde7431 100644
95f93f
--- a/qemu-nbd.c
95f93f
+++ b/qemu-nbd.c
95f93f
@@ -267,7 +267,7 @@ out:
95f93f
 
95f93f
 static int nbd_can_accept(void *opaque)
95f93f
 {
95f93f
-    return nb_fds < shared;
95f93f
+    return state == RUNNING && nb_fds < shared;
95f93f
 }
95f93f
 
95f93f
 static void nbd_export_closed(NBDExport *exp)
95f93f
-- 
95f93f
1.8.3.1
95f93f