All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH for-11.2 v2] io: fix EOF check in websocket decoding code
@ 2026-12-02 12:40 Daniel P. Berrangé
  2026-12-02 13:18 ` Marc-André Lureau
  2026-12-02 16:05 ` Eric Blake
  0 siblings, 2 replies; 3+ messages in thread
From: Daniel P. Berrangé @ 2026-12-02 12:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: Eric Blake, marcandre.lureau, Daniel P. Berrangé

qio_channel_websock_read_wire() will only mark the channel as
having hit EOF if the underlying channel reports EOF, and there
is no encoded data pending.

This is incorrect behaviour because 'encinput' may contain
cached data from a previous read off the wire, which is none the
less too short to allow a new websocket frame to be decoded
into 'rawinput'.

In this situation, if there is a watch on G_IO_IN, the code will
busy-wait repeatedly getting EOF from the underlying channel but
not reporting EOF upwards due to undecoded 'encinput'

To correctly handle this, when seeing EOF from the underlying
stream, only bubble it upwards if we failed to decode any
additional frames.

Fixes: https://gitlab.com/qemu-project/qemu/-/work_items/5030
Fixes: CVE-2026-141286
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 io/channel-websock.c | 50 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 5 deletions(-)

diff --git a/io/channel-websock.c b/io/channel-websock.c
index 646cd45be2a..5f672c5899c 100644
--- a/io/channel-websock.c
+++ b/io/channel-websock.c
@@ -965,10 +965,28 @@ static void qio_channel_websock_finalize(Object *obj)
 }
 
 
+/*
+ * Called when either:
+ *   - rawinput is empty and an read operation wants
+ *     to decode more data
+ *   - something has a watch on G_IO_IN and wants
+ *     to know if decoded data is available
+ *
+ * At start of the call encinput may contain data
+ * previously read off the wire during the handshake
+ * that can be immediately decoded without reading
+ * more data, or incomplete frames that could not
+ * be decoded by a previous call.
+ *
+ * Returns: 1 if bytes were read, 0 on eof, -1 on error,
+ *   QIO_CHANNEL_ERR_BLOCK on blocking
+ */
 static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
                                              Error **errp)
 {
+    ssize_t avail;
     ssize_t ret;
+    bool eof = false;
 
     if (ioc->encinput.offset < 4096 &&
         ioc->rawinput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
@@ -980,29 +998,51 @@ static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
                                ioc->encinput.offset,
                                want,
                                errp);
-        if (ret < 0) {
+        if (ret == -1) {
             return ret;
         }
-        if (ret == 0 && ioc->encinput.offset == 0) {
-            ioc->io_eof = TRUE;
-            return 0;
+        if (ret == 0) {
+            eof = true;
+        }
+        if (ret != QIO_CHANNEL_ERR_BLOCK) {
+            ioc->encinput.offset += ret;
         }
-        ioc->encinput.offset += ret;
     }
 
+    avail = ioc->rawinput.offset;
     while (ioc->encinput.offset != 0) {
         if (ioc->payload_remain == 0) {
             ret = qio_channel_websock_decode_header(ioc, errp);
+            if (ret == QIO_CHANNEL_ERR_BLOCK) {
+                break;
+            }
             if (ret < 0) {
                 return ret;
             }
         }
 
         ret = qio_channel_websock_decode_payload(ioc, errp);
+        if (ret == QIO_CHANNEL_ERR_BLOCK) {
+            break;
+        }
         if (ret < 0) {
             return ret;
         }
     }
+
+    /*
+     * If we didn't make progress, then we either have
+     * to report EOF, or indicate the read would block
+     */
+    if (ioc->rawinput.offset == avail) {
+        if (eof) {
+            ioc->io_eof = TRUE;
+            return 0;
+        } else {
+            return QIO_CHANNEL_ERR_BLOCK;
+        }
+    }
+
     return 1;
 }
 
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH for-11.2 v2] io: fix EOF check in websocket decoding code
  2026-12-02 12:40 [PATCH for-11.2 v2] io: fix EOF check in websocket decoding code Daniel P. Berrangé
@ 2026-12-02 13:18 ` Marc-André Lureau
  2026-12-02 16:05 ` Eric Blake
  1 sibling, 0 replies; 3+ messages in thread
From: Marc-André Lureau @ 2026-12-02 13:18 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: qemu-devel, Eric Blake

On Wed, Dec 2, 2026 at 4:40 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
>
> qio_channel_websock_read_wire() will only mark the channel as
> having hit EOF if the underlying channel reports EOF, and there
> is no encoded data pending.
>
> This is incorrect behaviour because 'encinput' may contain
> cached data from a previous read off the wire, which is none the
> less too short to allow a new websocket frame to be decoded
> into 'rawinput'.
>
> In this situation, if there is a watch on G_IO_IN, the code will
> busy-wait repeatedly getting EOF from the underlying channel but
> not reporting EOF upwards due to undecoded 'encinput'
>
> To correctly handle this, when seeing EOF from the underlying
> stream, only bubble it upwards if we failed to decode any
> additional frames.
>
> Fixes: https://gitlab.com/qemu-project/qemu/-/work_items/5030
> Fixes: CVE-2026-141286
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
>  io/channel-websock.c | 50 +++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 45 insertions(+), 5 deletions(-)
>
> diff --git a/io/channel-websock.c b/io/channel-websock.c
> index 646cd45be2a..5f672c5899c 100644
> --- a/io/channel-websock.c
> +++ b/io/channel-websock.c
> @@ -965,10 +965,28 @@ static void qio_channel_websock_finalize(Object *obj)
>  }
>
>
> +/*
> + * Called when either:
> + *   - rawinput is empty and an read operation wants
> + *     to decode more data
> + *   - something has a watch on G_IO_IN and wants
> + *     to know if decoded data is available
> + *
> + * At start of the call encinput may contain data
> + * previously read off the wire during the handshake
> + * that can be immediately decoded without reading
> + * more data, or incomplete frames that could not
> + * be decoded by a previous call.
> + *
> + * Returns: 1 if bytes were read, 0 on eof, -1 on error,
> + *   QIO_CHANNEL_ERR_BLOCK on blocking
> + */
>  static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
>                                               Error **errp)
>  {
> +    ssize_t avail;
>      ssize_t ret;
> +    bool eof = false;
>
>      if (ioc->encinput.offset < 4096 &&
>          ioc->rawinput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
> @@ -980,29 +998,51 @@ static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
>                                 ioc->encinput.offset,
>                                 want,
>                                 errp);
> -        if (ret < 0) {
> +        if (ret == -1) {
>              return ret;
>          }
> -        if (ret == 0 && ioc->encinput.offset == 0) {
> -            ioc->io_eof = TRUE;
> -            return 0;
> +        if (ret == 0) {
> +            eof = true;
> +        }
> +        if (ret != QIO_CHANNEL_ERR_BLOCK) {
> +            ioc->encinput.offset += ret;
>          }
> -        ioc->encinput.offset += ret;
>      }
>
> +    avail = ioc->rawinput.offset;
>      while (ioc->encinput.offset != 0) {
>          if (ioc->payload_remain == 0) {
>              ret = qio_channel_websock_decode_header(ioc, errp);
> +            if (ret == QIO_CHANNEL_ERR_BLOCK) {
> +                break;
> +            }
>              if (ret < 0) {
>                  return ret;
>              }
>          }
>
>          ret = qio_channel_websock_decode_payload(ioc, errp);
> +        if (ret == QIO_CHANNEL_ERR_BLOCK) {
> +            break;
> +        }
>          if (ret < 0) {
>              return ret;
>          }
>      }
> +
> +    /*
> +     * If we didn't make progress, then we either have
> +     * to report EOF, or indicate the read would block
> +     */
> +    if (ioc->rawinput.offset == avail) {
> +        if (eof) {
> +            ioc->io_eof = TRUE;
> +            return 0;
> +        } else {
> +            return QIO_CHANNEL_ERR_BLOCK;
> +        }
> +    }
> +
>      return 1;
>  }
>
> --
> 2.56.1
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH for-11.2 v2] io: fix EOF check in websocket decoding code
  2026-12-02 12:40 [PATCH for-11.2 v2] io: fix EOF check in websocket decoding code Daniel P. Berrangé
  2026-12-02 13:18 ` Marc-André Lureau
@ 2026-12-02 16:05 ` Eric Blake
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Blake @ 2026-12-02 16:05 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: qemu-devel, marcandre.lureau

On Wed, Dec 02, 2026 at 12:40:29PM +0000, Daniel P. Berrangé wrote:
> qio_channel_websock_read_wire() will only mark the channel as
> having hit EOF if the underlying channel reports EOF, and there
> is no encoded data pending.
> 
> This is incorrect behaviour because 'encinput' may contain
> cached data from a previous read off the wire, which is none the
> less too short to allow a new websocket frame to be decoded
> into 'rawinput'.
> 
> In this situation, if there is a watch on G_IO_IN, the code will
> busy-wait repeatedly getting EOF from the underlying channel but
> not reporting EOF upwards due to undecoded 'encinput'
> 
> To correctly handle this, when seeing EOF from the underlying
> stream, only bubble it upwards if we failed to decode any
> additional frames.
> 
> Fixes: https://gitlab.com/qemu-project/qemu/-/work_items/5030
> Fixes: CVE-2026-141286
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  io/channel-websock.c | 50 +++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 45 insertions(+), 5 deletions(-)
> 
> diff --git a/io/channel-websock.c b/io/channel-websock.c
> index 646cd45be2a..5f672c5899c 100644
> --- a/io/channel-websock.c
> +++ b/io/channel-websock.c
> @@ -965,10 +965,28 @@ static void qio_channel_websock_finalize(Object *obj)
>  }
>  
>  
> +/*
> + * Called when either:
> + *   - rawinput is empty and an read operation wants
> + *     to decode more data
> + *   - something has a watch on G_IO_IN and wants
> + *     to know if decoded data is available
> + *
> + * At start of the call encinput may contain data
> + * previously read off the wire during the handshake
> + * that can be immediately decoded without reading
> + * more data, or incomplete frames that could not
> + * be decoded by a previous call.
> + *
> + * Returns: 1 if bytes were read, 0 on eof, -1 on error,
> + *   QIO_CHANNEL_ERR_BLOCK on blocking
> + */
>  static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
>                                               Error **errp)

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 16:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 12:40 [PATCH for-11.2 v2] io: fix EOF check in websocket decoding code Daniel P. Berrangé
2026-12-02 13:18 ` Marc-André Lureau
2026-12-02 16:05 ` Eric Blake

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.