Skip to contentSkip to conversation

io/channel-websock: fix an unauthenticated crash in the handshake

PATCH In review Denis V. Lunev 5 participants 18 messages Aug 31 – Sep 2 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover io/channel-websock: fix an unauthenticated crash in the handshake

A client which can reach a VNC websocket port crashes QEMU before it has authenticated, by sending an HTTP greeting whose request line holds no space:

printf 'stats\r\nx\r\n\r\n' | nc $host $port

Three defects line up to produce it. The greeting is rejected without queueing a response, so the handshake goes on to flush an empty buffer. A zero length sendmsg() succeeds and returns 0, which qio_channel_socket_writev() mistakes for failure and reports as QIO_CHANNEL_ERR_BLOCK with errp left unset. The handshake treats every negative return as fatal and hands that NULL Error to error_get_pretty(). Patches 1 to 3 close the three links.

Patch 5 is the same NULL Error on the read side of the handshake, where ERR_BLOCK is folded into -1. It is reachable for a wss:// client, whose master channel is then a TLS channel: a wakeup carrying only part of a record makes gnutls report EAGAIN.

The tests drive the handshake through a channel which reports ERR_BLOCK on demand, covering both directions. No test reproduces the original crash itself, which turns on a stale errno and is not reliably reproducible in a unit test. What they pin is that a 400 is emitted and that ERR_BLOCK no longer reaches error_get_pretty().

CC Daniel P. Berrangé <[email protected]>
CC Marc-André Lureau <[email protected]>

Denis V. Lunev (6):
io/channel-socket: do not treat a zero length write as an error io/channel-websock: send an HTTP 400 when the greeting has no space io/channel-websock: handle a blocked write during the handshake tests/unit: add websock handshake test io/channel-websock: do not lose QIO_CHANNEL_ERR_BLOCK while reading tests/unit: cover blocked IO during the websock handshake

 io/channel-socket.c                  |   2 +-
 io/channel-websock.c                 |  10 +-
 tests/unit/meson.build               |   1 +
 tests/unit/test-io-channel-websock.c | 249 +++++++++++++++++++++++++++
 4 files changed, 260 insertions(+), 2 deletions(-)
 create mode 100644 tests/unit/test-io-channel-websock.c
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/6 io/channel-socket: do not treat a zero length write as an error

From: Denis V. Lunev <[email protected]>

qio_channel_socket_writev() checks "ret <= 0" after sendmsg(). A zero length iovec is written successfully and returns 0, so the success falls into the errno switch, which acts on whatever the last failing syscall left in errno. A stale EAGAIN turns it into QIO_CHANNEL_ERR_BLOCK with errp untouched, and a caller which treats every negative return as fatal then passes a NULL Error to error_get_pretty(). The websocket handshake does exactly that, so an unauthenticated client crashes QEMU during the greeting.

Returning 0 is safe for callers which loop until everything is written. qio_channel_writev_full_all() has no zero progress guard, but iov_copy() yields no entries for a zero length write, so that loop is never entered. A connected stream socket returns 0 only when there is nothing to send.

The WIN32 implementation in the same file uses "ret < 0".

Fixes 559607ea173a ("io: add QIOChannelSocket class")
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 io/channel-socket.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
io/channel-socket.c +1 -1
index 12773b832c..7920cee639 100644
@@ -667,7 +667,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
 
  retry:
     ret = sendmsg(sioc->fd, &msg, sflags);
-    if (ret <= 0) {
+    if (ret < 0) {
         switch (errno) {
         case EAGAIN:
             return QIO_CHANNEL_ERR_BLOCK;
Signature · 1 line
2.53.0
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] io/channel-socket: do not treat a zero length write as an error

Quoted 19 lines · Denis V. Lunev

From: Denis V. Lunev <[email protected]>

qio_channel_socket_writev() checks "ret <= 0" after sendmsg(). A zero length iovec is written successfully and returns 0, so the success falls into the errno switch, which acts on whatever the last failing syscall left in errno. A stale EAGAIN turns it into QIO_CHANNEL_ERR_BLOCK with errp untouched, and a caller which treats every negative return as fatal then passes a NULL Error to error_get_pretty(). The websocket handshake does exactly that, so an unauthenticated client crashes QEMU during the greeting.

Returning 0 is safe for callers which loop until everything is written. qio_channel_writev_full_all() has no zero progress guard, but iov_copy() yields no entries for a zero length write, so that loop is never entered. A connected stream socket returns 0 only when there is nothing to send.

The WIN32 implementation in the same file uses "ret < 0".

Quoted 8 lines
Fixes 559607ea173a ("io: add QIOChannelSocket class")
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 io/channel-socket.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Though IMHO it is also a bug in the caller to request writing of zero bytes.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#3 lore raw

PATCH 2/6 io/channel-websock: send an HTTP 400 when the greeting has no space

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

qio_channel_websock_extract_headers() returns 0 without queueing a response when the request line contains no space, unlike every sibling check which jumps to bad_request. encoutput stays empty, yet qio_channel_websock_handshake_read() still reports success and the caller arms a G_IO_OUT watch to flush nothing.

Flushing that empty buffer is where QEMU crashes. Any client can trigger it before authentication on a VNC websocket port:

printf 'stats\r\nx\r\n\r\n' | nc $host $port

Fixes 07e95cd529af ("io: fully parse & validate HTTP headers for websocket protocol handshake")
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 io/channel-websock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
io/channel-websock.c +1 -1
index 1929abf56a..66c91ed2a2 100644
@@ -230,7 +230,7 @@ qio_channel_websock_extract_headers(QIOChannelWebsock *ioc,
     tmp = strchr(buffer, ' ');
     if (!tmp) {
         error_setg(errp, "Missing HTTP path delimiter");
-        return 0;
+        goto bad_request;
     }
     *tmp = '\0';
 
Signature · 1 line
2.53.0
Reviewed-by on v1 2/6
#4 lore raw

Re: [PATCH 2/6] io/channel-websock: send an HTTP 400 when the greeting has no space

Quoted 8 lines · Denis V. Lunev

From: Denis V. Lunev <[email protected]>

qio_channel_websock_extract_headers() returns 0 without queueing a response when the request line contains no space, unlike every sibling check which jumps to bad_request. encoutput stays empty, yet qio_channel_websock_handshake_read() still reports success and the caller arms a G_IO_OUT watch to flush nothing.

Quoted 6 lines

Flushing that empty buffer is where QEMU crashes. Any client can trigger it before authentication on a VNC websocket port:

printf 'stats\r\nx\r\n\r\n' | nc $host $port

Fixes 07e95cd529af ("io: fully parse & validate HTTP headers for websocket protocol handshake")

"bad_request" was introduced in f69a8bde29354493ff8aea64cc9cb3b531d16337 which intended to fix 07e95cd529af, but missed that one return statement. So I'd suggest "Fixes: f69a8bde29", or even both commits.

Quoted 7 lines
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 io/channel-websock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
io/channel-websock.c +1 -1
index 1929abf56a..66c91ed2a2 100644
@@ -230,7 +230,7 @@ qio_channel_websock_extract_headers(QIOChannelWebsock *ioc,
     tmp = strchr(buffer, ' ');
     if (!tmp) {
         error_setg(errp, "Missing HTTP path delimiter");
-        return 0;
+        goto bad_request;
     }
     *tmp = '\0';
 
Signature · 1 line
2.53.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#5 lore raw

PATCH 3/6 io/channel-websock: handle a blocked write during the handshake

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

qio_channel_websock_handshake_send() treats every negative return from qio_channel_write() as fatal and passes err to error_get_pretty(). QIO_CHANNEL_ERR_BLOCK is negative but leaves err NULL, so a socket which cannot take the response immediately crashes QEMU before the client has authenticated.

Keep the G_IO_OUT watch armed and retry instead.

Fixes 2d1d0e70cf3e ("io: add QIOChannelWebsock class")
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 io/channel-websock.c | 5 +++++
 1 file changed, 5 insertions(+)
io/channel-websock.c +5 -0
index 66c91ed2a2..8f27b1f12b 100644
@@ -562,6 +562,11 @@ static gboolean qio_channel_websock_handshake_send(QIOChannel *ioc,
                             wioc->encoutput.offset,
                             &err);
 
+    if (ret == QIO_CHANNEL_ERR_BLOCK) {
+        /* Socket buffer is full, the G_IO_OUT watch stays armed */
+        return TRUE;
+    }
+
     if (ret < 0) {
         trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
         qio_task_set_error(task, err);
Signature · 1 line
2.53.0
Reviewed-by on v1 3/6
#6 lore raw

Re: [PATCH 3/6] io/channel-websock: handle a blocked write during the handshake

Quoted 10 lines · Denis V. Lunev

From: Denis V. Lunev <[email protected]>

qio_channel_websock_handshake_send() treats every negative return from qio_channel_write() as fatal and passes err to error_get_pretty(). QIO_CHANNEL_ERR_BLOCK is negative but leaves err NULL, so a socket which cannot take the response immediately crashes QEMU before the client has authenticated.

Keep the G_IO_OUT watch armed and retry instead.

Quoted 8 lines
Fixes 2d1d0e70cf3e ("io: add QIOChannelWebsock class")
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 io/channel-websock.c | 5 +++++
 1 file changed, 5 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#7 lore raw

PATCH 4/6 tests/unit: add websock handshake test

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

Check that malformed HTTP greetings are answered with an HTTP 400 rather than an empty response. The no-space case is the one which used to leave the response buffer empty.

Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 tests/unit/meson.build               |   1 +
 tests/unit/test-io-channel-websock.c | 105 +++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)
 create mode 100644 tests/unit/test-io-channel-websock.c
tests/unit/meson.build +1 -0
index 3a9866c1f2..6a11f07112 100644
@@ -93,6 +93,7 @@ if have_block
     'test-io-channel-command': ['io-channel-helpers.c', io],
     'test-io-channel-buffer': ['io-channel-helpers.c', io],
     'test-io-channel-null': [io],
+    'test-io-channel-websock': [io],
     'test-crypto-ivgen': [io],
     'test-crypto-afsplit': [io],
     'test-crypto-block': [io],
tests/unit/test-io-channel-websock.c new file +105 -0

105 lines not on this page · raw message

Signature · 1 line
2.53.0
Reviewed-by on v1 4/6
#8 lore raw

Re: [PATCH 4/6] tests/unit: add websock handshake test

Quoted 6 lines · Denis V. Lunev

From: Denis V. Lunev <[email protected]>

Check that malformed HTTP greetings are answered with an HTTP 400 rather than an empty response. The no-space case is the one which used to leave the response buffer empty.

Quoted 8 lines
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 tests/unit/meson.build               |   1 +
 tests/unit/test-io-channel-websock.c | 105 +++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)
 create mode 100644 tests/unit/test-io-channel-websock.c

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#9 lore raw

PATCH 5/6 io/channel-websock: do not lose QIO_CHANNEL_ERR_BLOCK while reading

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

qio_channel_websock_handshake_read() folds every negative return from qio_channel_read() into -1. QIO_CHANNEL_ERR_BLOCK leaves errp unset, so qio_channel_websock_handshake_io() then hands a NULL Error to error_get_pretty() and QEMU dies.

The master channel is non-blocking and, for a wss:// client, is a TLS channel. A G_IO_IN wakeup carrying only part of a TLS record makes gnutls report EAGAIN, which is all it takes to reach this before the client has authenticated.

ERR_BLOCK here means the headers are not complete yet, which is what a 0 return already tells the caller. Report it that way and keep waiting. The watch is level triggered, so an incomplete record sitting in the socket spins the main loop until the rest of it arrives. That is bounded by the round trip and is what every reader layered over TLS already does.

Fixes 2d1d0e70cf3e ("io: add QIOChannelWebsock class")
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 io/channel-websock.c | 3 +++
 1 file changed, 3 insertions(+)
io/channel-websock.c +3 -0
index 8f27b1f12b..461abcae48 100644
@@ -492,6 +492,9 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
     buffer_reserve(&ioc->encinput, want);
     ret = qio_channel_read(ioc->master,
                            (char *)buffer_end(&ioc->encinput), want, errp);
+    if (ret == QIO_CHANNEL_ERR_BLOCK) {
+        return 0;
+    }
     if (ret < 0) {
         return -1;
     }
Signature · 1 line
2.53.0
Reviewed-by on v1 5/6
#10 lore raw

Re: [PATCH 5/6] io/channel-websock: do not lose QIO_CHANNEL_ERR_BLOCK while reading

Quoted 19 lines · Denis V. Lunev

From: Denis V. Lunev <[email protected]>

qio_channel_websock_handshake_read() folds every negative return from qio_channel_read() into -1. QIO_CHANNEL_ERR_BLOCK leaves errp unset, so qio_channel_websock_handshake_io() then hands a NULL Error to error_get_pretty() and QEMU dies.

The master channel is non-blocking and, for a wss:// client, is a TLS channel. A G_IO_IN wakeup carrying only part of a TLS record makes gnutls report EAGAIN, which is all it takes to reach this before the client has authenticated.

ERR_BLOCK here means the headers are not complete yet, which is what a 0 return already tells the caller. Report it that way and keep waiting. The watch is level triggered, so an incomplete record sitting in the socket spins the main loop until the rest of it arrives. That is bounded by the round trip and is what every reader layered over TLS already does.

Quoted 8 lines
Fixes 2d1d0e70cf3e ("io: add QIOChannelWebsock class")
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 io/channel-websock.c | 3 +++
 1 file changed, 3 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#11 lore raw

PATCH 6/6 tests/unit: cover blocked IO during the websock handshake

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

Add a channel which reports QIO_CHANNEL_ERR_BLOCK on demand, the way a TLS channel does when a record arrives split across segments or when the socket cannot take the whole reply at once, and drive the server handshake through it in both directions. Without the fixes each direction dereferences a NULL Error and the test dies on SIGSEGV.

Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 tests/unit/test-io-channel-websock.c | 150 ++++++++++++++++++++++++++-
 1 file changed, 147 insertions(+), 3 deletions(-)
tests/unit/test-io-channel-websock.c +147 -3
index 2a55a4bcdf..88da24f993 100644
@@ -12,6 +12,123 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 #include "qemu/sockets.h"
+#include "qom/object.h"
+
+#define TYPE_QIO_CHANNEL_STALL "qio-channel-stall"
+OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelStall, QIO_CHANNEL_STALL)
+
+/*
+ * Reports QIO_CHANNEL_ERR_BLOCK for the first @rstalls reads and @wstalls
+ * writes, the way a TLS channel does when a record arrives split across TCP
+ * segments or the socket cannot take the whole reply at once.
+ */
+struct QIOChannelStall {
+    QIOChannel parent;
+    QIOChannel *master;
+    unsigned rstalls;
+    unsigned wstalls;
+};
+
+static ssize_t qio_channel_stall_readv(QIOChannel *ioc,
+                                       const struct iovec *iov,
+                                       size_t niov,
+                                       int **fds,
+                                       size_t *nfds,
+                                       int flags,
+                                       Error **errp)
+{
+    QIOChannelStall *sioc = QIO_CHANNEL_STALL(ioc);
+
+    if (sioc->rstalls) {
+        sioc->rstalls--;
+        return QIO_CHANNEL_ERR_BLOCK;
+    }
+    return qio_channel_readv_full(sioc->master, iov, niov, fds, nfds,
+                                  flags, errp);
+}
+
+static ssize_t qio_channel_stall_writev(QIOChannel *ioc,
+                                        const struct iovec *iov,
+                                        size_t niov,
+                                        int *fds,
+                                        size_t nfds,
+                                        int flags,
+                                        Error **errp)
+{
+    QIOChannelStall *sioc = QIO_CHANNEL_STALL(ioc);
+
+    if (sioc->wstalls) {
+        sioc->wstalls--;
+        return QIO_CHANNEL_ERR_BLOCK;
+    }
+    return qio_channel_writev_full(sioc->master, iov, niov, fds, nfds,
+                                   flags, errp);
+}
+
+static int qio_channel_stall_set_blocking(QIOChannel *ioc, bool enabled,
+                                          Error **errp)
+{
+    QIOChannelStall *sioc = QIO_CHANNEL_STALL(ioc);
+
+    return qio_channel_set_blocking(sioc->master, enabled, errp) ? 0 : -1;
+}
+
+static int qio_channel_stall_close(QIOChannel *ioc, Error **errp)
+{
+    QIOChannelStall *sioc = QIO_CHANNEL_STALL(ioc);
+
+    return qio_channel_close(sioc->master, errp);
+}
+
+static GSource *qio_channel_stall_create_watch(QIOChannel *ioc,
+                                               GIOCondition condition)
+{
+    QIOChannelStall *sioc = QIO_CHANNEL_STALL(ioc);
+
+    return qio_channel_create_watch(sioc->master, condition);
+}
+
+static void qio_channel_stall_finalize(Object *obj)
+{
+    QIOChannelStall *sioc = QIO_CHANNEL_STALL(obj);
+
+    object_unref(OBJECT(sioc->master));
+}
+
+static void qio_channel_stall_class_init(ObjectClass *klass,
+                                         const void *class_data G_GNUC_UNUSED)
+{
+    QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
+
+    ioc_klass->io_writev = qio_channel_stall_writev;
+    ioc_klass->io_readv = qio_channel_stall_readv;
+    ioc_klass->io_set_blocking = qio_channel_stall_set_blocking;
+    ioc_klass->io_close = qio_channel_stall_close;
+    ioc_klass->io_create_watch = qio_channel_stall_create_watch;
+}
+
+static const TypeInfo qio_channel_stall_info = {
+    .parent = TYPE_QIO_CHANNEL,
+    .name = TYPE_QIO_CHANNEL_STALL,
+    .instance_size = sizeof(QIOChannelStall),
+    .instance_finalize = qio_channel_stall_finalize,
+    .class_init = qio_channel_stall_class_init,
+};
+
+static QIOChannelStall *qio_channel_stall_new(QIOChannel *master,
+                                              unsigned rstalls,
+                                              unsigned wstalls)
+{
+    QIOChannelStall *sioc = QIO_CHANNEL_STALL(
+        object_new(TYPE_QIO_CHANNEL_STALL));
+
+    object_ref(OBJECT(master));
+    sioc->master = master;
+    sioc->rstalls = rstalls;
+    sioc->wstalls = wstalls;
+
+    return sioc;
+}
 
 typedef struct {
     bool finished;
@@ -31,10 +148,12 @@ static void test_websock_handshake_done(QIOTask *task, gpointer opaque)
  * the server wrote back, NUL terminated. The handshake is expected to
  * fail; the point of the test is the HTTP response that goes with it.
  */
-static char *test_websock_handshake_reply(const char *request)
+static char *test_websock_handshake_reply(const char *request,
+                                          unsigned rstalls, unsigned wstalls)
 {
     QIOChannelWebsockHandshake res = { false, false };
     QIOChannelSocket *cli, *srv;
+    QIOChannelStall *stall;
     QIOChannelWebsock *wioc;
     GMainContext *mainloop;
     int channel[2];
@@ -48,7 +167,8 @@ static char *test_websock_handshake_reply(const char *request)
     qio_channel_set_blocking(QIO_CHANNEL(srv), false, &error_abort);
     qio_channel_set_blocking(QIO_CHANNEL(cli), false, &error_abort);
 
-    wioc = qio_channel_websock_new_server(QIO_CHANNEL(srv));
+    stall = qio_channel_stall_new(QIO_CHANNEL(srv), rstalls, wstalls);
+    wioc = qio_channel_websock_new_server(QIO_CHANNEL(stall));
     qio_channel_websock_handshake(wioc, test_websock_handshake_done,
                                   &res, NULL);
 
@@ -68,6 +188,7 @@ static char *test_websock_handshake_reply(const char *request)
     }
 
     object_unref(OBJECT(wioc));
+    object_unref(OBJECT(stall));
     object_unref(OBJECT(srv));
     object_unref(OBJECT(cli));
 
@@ -77,7 +198,23 @@ static char *test_websock_handshake_reply(const char *request)
 static void test_websock_bad_request(const void *opaque)
 {
     const char *request = opaque;
-    g_autofree char *reply = test_websock_handshake_reply(request);
+    g_autofree char *reply = test_websock_handshake_reply(request, 0, 0);
+
+    g_assert_true(g_str_has_prefix(reply, "HTTP/1.1 400 Bad Request\r\n"));
+}
+
+static void test_websock_stalled_read(const void *opaque)
+{
+    const char *request = opaque;
+    g_autofree char *reply = test_websock_handshake_reply(request, 1, 0);
+
+    g_assert_true(g_str_has_prefix(reply, "HTTP/1.1 400 Bad Request\r\n"));
+}
+
+static void test_websock_stalled_write(const void *opaque)
+{
+    const char *request = opaque;
+    g_autofree char *reply = test_websock_handshake_reply(request, 0, 1);
 
     g_assert_true(g_str_has_prefix(reply, "HTTP/1.1 400 Bad Request\r\n"));
 }
@@ -85,6 +222,7 @@ static void test_websock_bad_request(const void *opaque)
 int main(int argc, char **argv)
 {
     module_call_init(MODULE_INIT_QOM);
+    type_register_static(&qio_channel_stall_info);
     g_test_init(&argc, &argv, NULL);
 
 #define TEST_BAD_REQUEST(name, request)                         \
@@ -101,5 +239,11 @@ int main(int argc, char **argv)
     TEST_BAD_REQUEST("bad-method", "POST / HTTP/1.1\r\nx: y\r\n\r\n");
     TEST_BAD_REQUEST("bad-version", "GET / HTTP/1.0\r\nx: y\r\n\r\n");
 
+    /* A read which blocks before any header arrives is not a fatal error. */
+    g_test_add_data_func("/io/channel/websock/stalled-read",
+                         "stats\r\nx\r\n\r\n", test_websock_stalled_read);
+    g_test_add_data_func("/io/channel/websock/stalled-write",
+                         "stats\r\nx\r\n\r\n", test_websock_stalled_write);
+
     return g_test_run();
 }
Signature · 1 line
2.53.0
Reviewed-by on v1 6/6
#12 lore raw

Re: [PATCH 6/6] tests/unit: cover blocked IO during the websock handshake

Quoted 8 lines · Denis V. Lunev

From: Denis V. Lunev <[email protected]>

Add a channel which reports QIO_CHANNEL_ERR_BLOCK on demand, the way a TLS channel does when a record arrives split across segments or when the socket cannot take the whole reply at once, and drive the server handshake through it in both directions. Without the fixes each direction dereferences a NULL Error and the test dies on SIGSEGV.

Quoted 6 lines
Cc Daniel P. Berrangé <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 tests/unit/test-io-channel-websock.c | 150 ++++++++++++++++++++++++++-
 1 file changed, 147 insertions(+), 3 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on the v1 series
#13 lore raw
In reply to Denis V. Lunev
Quoted 37 lines

A client which can reach a VNC websocket port crashes QEMU before it has authenticated, by sending an HTTP greeting whose request line holds no space:

printf 'stats\r\nx\r\n\r\n' | nc $host $port

Three defects line up to produce it. The greeting is rejected without queueing a response, so the handshake goes on to flush an empty buffer. A zero length sendmsg() succeeds and returns 0, which qio_channel_socket_writev() mistakes for failure and reports as QIO_CHANNEL_ERR_BLOCK with errp left unset. The handshake treats every negative return as fatal and hands that NULL Error to error_get_pretty(). Patches 1 to 3 close the three links.

Patch 5 is the same NULL Error on the read side of the handshake, where ERR_BLOCK is folded into -1. It is reachable for a wss:// client, whose master channel is then a TLS channel: a wakeup carrying only part of a record makes gnutls report EAGAIN.

The tests drive the handshake through a channel which reports ERR_BLOCK on demand, covering both directions. No test reproduces the original crash itself, which turns on a stale errno and is not reliably reproducible in a unit test. What they pin is that a 400 is emitted and that ERR_BLOCK no longer reaches error_get_pretty().

CC Daniel P. Berrangé <[email protected]>
CC Marc-André Lureau <[email protected]>

Denis V. Lunev (6):
io/channel-socket: do not treat a zero length write as an error io/channel-websock: send an HTTP 400 when the greeting has no space io/channel-websock: handle a blocked write during the handshake tests/unit: add websock handshake test io/channel-websock: do not lose QIO_CHANNEL_ERR_BLOCK while reading tests/unit: cover blocked IO during the websock handshake

Quoted 7 lines
 io/channel-socket.c                  |   2 +-
 io/channel-websock.c                 |  10 +-
 tests/unit/meson.build               |   1 +
 tests/unit/test-io-channel-websock.c | 249 +++++++++++++++++++++++++++
 4 files changed, 260 insertions(+), 2 deletions(-)
 create mode 100644 tests/unit/test-io-channel-websock.c

Patch series lgtm. You cc stable, why didn't you file a CVE?

Signature · 1 line
Marc-André Lureau <[email protected]>
#14 lore raw
Quoted 44 lines · On 8/31/26 13:29, [email protected] wrote:
Quoted 37 lines

A client which can reach a VNC websocket port crashes QEMU before it has authenticated, by sending an HTTP greeting whose request line holds no space:

printf 'stats\r\nx\r\n\r\n' | nc $host $port

Three defects line up to produce it. The greeting is rejected without queueing a response, so the handshake goes on to flush an empty buffer. A zero length sendmsg() succeeds and returns 0, which qio_channel_socket_writev() mistakes for failure and reports as QIO_CHANNEL_ERR_BLOCK with errp left unset. The handshake treats every negative return as fatal and hands that NULL Error to error_get_pretty(). Patches 1 to 3 close the three links.

Patch 5 is the same NULL Error on the read side of the handshake, where ERR_BLOCK is folded into -1. It is reachable for a wss:// client, whose master channel is then a TLS channel: a wakeup carrying only part of a record makes gnutls report EAGAIN.

The tests drive the handshake through a channel which reports ERR_BLOCK on demand, covering both directions. No test reproduces the original crash itself, which turns on a stale errno and is not reliably reproducible in a unit test. What they pin is that a 400 is emitted and that ERR_BLOCK no longer reaches error_get_pretty().

CC Daniel P. Berrangé <[email protected]>
CC Marc-André Lureau <[email protected]>

Denis V. Lunev (6):
io/channel-socket: do not treat a zero length write as an error io/channel-websock: send an HTTP 400 when the greeting has no space io/channel-websock: handle a blocked write during the handshake tests/unit: add websock handshake test io/channel-websock: do not lose QIO_CHANNEL_ERR_BLOCK while reading tests/unit: cover blocked IO during the websock handshake

Quoted 7 lines
 io/channel-socket.c                  |   2 +-
 io/channel-websock.c                 |  10 +-
 tests/unit/meson.build               |   1 +
 tests/unit/test-io-channel-websock.c | 249 +++++++++++++++++++++++++++
 4 files changed, 260 insertions(+), 2 deletions(-)
 create mode 100644 tests/unit/test-io-channel-websock.c
Quoted 4 lines

Patch series lgtm. You cc stable, why didn't you file a CVE?

I have calculated the scope and get around 6.2 which is not looking too impressive to spend a time.

There are toooooo many CVEs circulating around :-)

If this is wrong approach - let me know. I'll change the approach.

Thanks for review,
    Den

#15 lore raw
Quoted 45 lines · Denis V. Lunev
Quoted 44 lines · On 8/31/26 13:29, [email protected] wrote:
Quoted 44 lines

A client which can reach a VNC websocket port crashes QEMU before it has authenticated, by sending an HTTP greeting whose request line holds no space:

printf 'stats\r\nx\r\n\r\n' | nc $host $port

Three defects line up to produce it. The greeting is rejected without queueing a response, so the handshake goes on to flush an empty buffer. A zero length sendmsg() succeeds and returns 0, which qio_channel_socket_writev() mistakes for failure and reports as QIO_CHANNEL_ERR_BLOCK with errp left unset. The handshake treats every negative return as fatal and hands that NULL Error to error_get_pretty(). Patches 1 to 3 close the three links.

Patch 5 is the same NULL Error on the read side of the handshake, where ERR_BLOCK is folded into -1. It is reachable for a wss:// client, whose master channel is then a TLS channel: a wakeup carrying only part of a record makes gnutls report EAGAIN.

The tests drive the handshake through a channel which reports ERR_BLOCK on demand, covering both directions. No test reproduces the original crash itself, which turns on a stale errno and is not reliably reproducible in a unit test. What they pin is that a 400 is emitted and that ERR_BLOCK no longer reaches error_get_pretty().

CC Daniel P. Berrangé <[email protected]>
CC Marc-André Lureau <[email protected]>

Denis V. Lunev (6):
io/channel-socket: do not treat a zero length write as an error io/channel-websock: send an HTTP 400 when the greeting has no space io/channel-websock: handle a blocked write during the handshake tests/unit: add websock handshake test io/channel-websock: do not lose QIO_CHANNEL_ERR_BLOCK while reading tests/unit: cover blocked IO during the websock handshake

 io/channel-socket.c                  |   2 +-
 io/channel-websock.c                 |  10 +-
 tests/unit/meson.build               |   1 +
 tests/unit/test-io-channel-websock.c | 249 +++++++++++++++++++++++++++
 4 files changed, 260 insertions(+), 2 deletions(-)
 create mode 100644 tests/unit/test-io-channel-websock.c
Quoted 6 lines
Quoted 4 lines

Patch series lgtm. You cc stable, why didn't you file a CVE?

I have calculated the scope and get around 6.2 which is not looking too impressive to spend a time.

In the context of a VNC server enabling websocket. If we assume the VNC server has authentication enabled, including the TLS extension VeNCrypt, then the deployment can be said to be protecting against a malicious client.

The flaws mean the malicious client can inflict a denial of service attack on the VM before getting to the VNC authentication step. IMHO that is enough to justify a CVE assignment.

CC'ing Mauro to double check my view & assign a CVE if appropriate

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#16 lore raw
Quoted 59 lines · Daniel P. Berrangé
Quoted 45 lines · Denis V. Lunev
Quoted 44 lines · On 8/31/26 13:29, [email protected] wrote:
Quoted 44 lines

A client which can reach a VNC websocket port crashes QEMU before it has authenticated, by sending an HTTP greeting whose request line holds no space:

printf 'stats\r\nx\r\n\r\n' | nc $host $port

Three defects line up to produce it. The greeting is rejected without queueing a response, so the handshake goes on to flush an empty buffer. A zero length sendmsg() succeeds and returns 0, which qio_channel_socket_writev() mistakes for failure and reports as QIO_CHANNEL_ERR_BLOCK with errp left unset. The handshake treats every negative return as fatal and hands that NULL Error to error_get_pretty(). Patches 1 to 3 close the three links.

Patch 5 is the same NULL Error on the read side of the handshake, where ERR_BLOCK is folded into -1. It is reachable for a wss:// client, whose master channel is then a TLS channel: a wakeup carrying only part of a record makes gnutls report EAGAIN.

The tests drive the handshake through a channel which reports ERR_BLOCK on demand, covering both directions. No test reproduces the original crash itself, which turns on a stale errno and is not reliably reproducible in a unit test. What they pin is that a 400 is emitted and that ERR_BLOCK no longer reaches error_get_pretty().

CC Daniel P. Berrangé <[email protected]>
CC Marc-André Lureau <[email protected]>

Denis V. Lunev (6):
io/channel-socket: do not treat a zero length write as an error io/channel-websock: send an HTTP 400 when the greeting has no space io/channel-websock: handle a blocked write during the handshake tests/unit: add websock handshake test io/channel-websock: do not lose QIO_CHANNEL_ERR_BLOCK while reading tests/unit: cover blocked IO during the websock handshake

 io/channel-socket.c                  |   2 +-
 io/channel-websock.c                 |  10 +-
 tests/unit/meson.build               |   1 +
 tests/unit/test-io-channel-websock.c | 249 +++++++++++++++++++++++++++
 4 files changed, 260 insertions(+), 2 deletions(-)
 create mode 100644 tests/unit/test-io-channel-websock.c
Quoted 6 lines
Quoted 4 lines

Patch series lgtm. You cc stable, why didn't you file a CVE?

I have calculated the scope and get around 6.2 which is not looking too impressive to spend a time.

In the context of a VNC server enabling websocket. If we assume the VNC server has authentication enabled, including the TLS extension VeNCrypt, then the deployment can be said to be protecting against a malicious client.

Quoted 5 lines

The flaws mean the malicious client can inflict a denial of service attack on the VM before getting to the VNC authentication step. IMHO that is enough to justify a CVE assignment.

CC'ing Mauro to double check my view & assign a CVE if appropriate

Agreed; a pre-auth VNC crash warrants CVE assignment. This is similar to https://access.redhat.com/security/cve/cve-2025-11234

Please use CVE-2026-84788 for this one.

Thanks,

Quoted 7 lines

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Signature · 2 lines
Mauro Matteo Cascella
Red Hat Product Security
#17 lore raw
In reply to Denis V. Lunev
Quoted 19 lines · Denis V. Lunev

A client which can reach a VNC websocket port crashes QEMU before it has authenticated, by sending an HTTP greeting whose request line holds no space:

printf 'stats\r\nx\r\n\r\n' | nc $host $port

Three defects line up to produce it. The greeting is rejected without queueing a response, so the handshake goes on to flush an empty buffer. A zero length sendmsg() succeeds and returns 0, which qio_channel_socket_writev() mistakes for failure and reports as QIO_CHANNEL_ERR_BLOCK with errp left unset. The handshake treats every negative return as fatal and hands that NULL Error to error_get_pretty(). Patches 1 to 3 close the three links.

Patch 5 is the same NULL Error on the read side of the handshake, where ERR_BLOCK is folded into -1. It is reachable for a wss:// client, whose master channel is then a TLS channel: a wakeup carrying only part of a record makes gnutls report EAGAIN.

Quoted 5 lines

The tests drive the handshake through a channel which reports ERR_BLOCK on demand, covering both directions. No test reproduces the original crash itself, which turns on a stale errno and is not reliably reproducible in a unit test. What they pin is that a 400 is emitted and that ERR_BLOCK no longer reaches error_get_pretty().

Thanks for the various fixes.

While we're on this topic though, way back when Websocket support was first introduced to QEMU, I was pretty sceptical that it was a good idea for QEMU to be implementing the HTTP protocol directly. Some of these bugs (possibly even all) were likely a direct result of me porting the websockets code into QIOChannel, so not neccessarily the original impl.

None the less, I still feel pretty uncomfortable about the idea of QEMU implementing websockets/HTTP support directly. The lack of TLS support is a big flag that makes the whole thing questionable. Although you could put TLS in at the VNC level with VeNCrypt, IMHO doing it at the HTTP level is the right approach so the VNC protocol handshake is fully covered.

Having it inside QEMU also means it missed the biggest benefit of using websockets, which is that you can have a single TCP port hosting all VMs and select them dynamically from the HTTP request, instead of one TCP port per VM.

So I've thought about proposing its deprecation & deletion several times, on the basis that it is better to put an external websockets proxy in front of QEMU's VNC server instead of inside QEMU.

None the less, I'll queue all these patches.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|