Skip to contentSkip to conversation

io: remove io watch if TLS channel is closed during handshake

PATCH Superseded Daniel P. Berrangé 3 participants 4 messages Jul 5, 2023 – Jul 12, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io: remove io watch if TLS channel is closed during handshake

The TLS handshake make take some time to complete, during which time an I/O watch might be registered with the main loop. If the owner of the I/O channel invokes qio_channel_close() while the handshake is waiting to continue the I/O watch must be removed. Failing to remove it will later trigger the completion callback which the owner is not expecting to receive. In the case of the VNC server, this results in a SEGV as vnc_disconnect_start() tries to shutdown a client connection that is already gone / NULL.

CVE-2023-3354

---
 include/io/channel-tls.h |  1 +
 io/channel-tls.c         | 18 ++++++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
include/io/channel-tls.h +1 -0
index 5672479e9e..26c67f17e2 100644
@@ -48,6 +48,7 @@ struct QIOChannelTLS {
     QIOChannel *master;
     QCryptoTLSSession *session;
     QIOChannelShutdown shutdown;
+    guint hs_ioc_tag;
 };
 
 /**
io/channel-tls.c +12 -6
index 9805dd0a3f..e327e6a5c2 100644
@@ -198,12 +198,13 @@ static void qio_channel_tls_handshake_task(QIOChannelTLS *ioc,
         }
 
         trace_qio_channel_tls_handshake_pending(ioc, status);
-        qio_channel_add_watch_full(ioc->master,
-                                   condition,
-                                   qio_channel_tls_handshake_io,
-                                   data,
-                                   NULL,
-                                   context);
+        ioc->hs_ioc_tag =
+            qio_channel_add_watch_full(ioc->master,
+                                       condition,
+                                       qio_channel_tls_handshake_io,
+                                       data,
+                                       NULL,
+                                       context);
     }
 }
 
@@ -218,6 +219,7 @@ static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(
         qio_task_get_source(task));
 
+    tioc->hs_ioc_tag = 0;
     g_free(data);
     qio_channel_tls_handshake_task(tioc, task, context);
 
@@ -378,6 +380,10 @@ static int qio_channel_tls_close(QIOChannel *ioc,
 {
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
 
+    if (tioc->hs_ioc_tag) {
+        g_source_remove(tioc->hs_ioc_tag);
+    }
+
     return qio_channel_close(tioc->master, errp);
 }
 
Signature · 1 line
2.41.0
#1 lore raw
Quoted 68 lines · Daniel P. Berrangé

The TLS handshake make take some time to complete, during which time an I/O watch might be registered with the main loop. If the owner of the I/O channel invokes qio_channel_close() while the handshake is waiting to continue the I/O watch must be removed. Failing to remove it will later trigger the completion callback which the owner is not expecting to receive. In the case of the VNC server, this results in a SEGV as vnc_disconnect_start() tries to shutdown a client connection that is already gone / NULL.

CVE-2023-3354

---
 include/io/channel-tls.h |  1 +
 io/channel-tls.c         | 18 ++++++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
include/io/channel-tls.h +1 -0
index 5672479e9e..26c67f17e2 100644
@@ -48,6 +48,7 @@ struct QIOChannelTLS {
     QIOChannel *master;
     QCryptoTLSSession *session;
     QIOChannelShutdown shutdown;
+    guint hs_ioc_tag;
 };
 
 /**
io/channel-tls.c +8 -6
index 9805dd0a3f..e327e6a5c2 100644
@@ -198,12 +198,13 @@ static void
 qio_channel_tls_handshake_task(QIOChannelTLS *ioc,
         }
 
         trace_qio_channel_tls_handshake_pending(ioc, status);
-        qio_channel_add_watch_full(ioc->master,
-                                   condition,
-                                   qio_channel_tls_handshake_io,
-                                   data,
-                                   NULL,
-                                   context);
+        ioc->hs_ioc_tag =
+            qio_channel_add_watch_full(ioc->master,
+                                       condition,
+                                       qio_channel_tls_handshake_io,
+                                       data,
+                                       NULL,
+                                       context);
     }
 }
@@ -218,6 +219,7 @@ static gboolean
 qio_channel_tls_handshake_io(QIOChannel *ioc,
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(
         qio_task_get_source(task));
 
+    tioc->hs_ioc_tag = 0;
     g_free(data);
     qio_channel_tls_handshake_task(tioc, task, context);

io/channel-tls.c static int qio_channel_tls_close(QIOChannel *ioc, · line 385

Quoted 6 lines
io/channel-tls.c +2 -0
@@ -378,6 +380,10 @@ static int qio_channel_tls_close(QIOChannel *ioc,
 {
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
 
+    if (tioc->hs_ioc_tag) {
+        g_source_remove(tioc->hs_ioc_tag);
 

set it to 0 ?
or
g_clear_handle_id(&tios->hs_ioc_tag, g_source_remove);

Quoted 10 lines
io/channel-tls.c +2 -1
+    }
+
     return qio_channel_close(tioc->master, errp);
 }
 
--

2.41.0

Signature · 1 line
Marc-André Lureau
#2 lore raw
Quoted 74 lines · Marc-André Lureau
Quoted 71 lines · Daniel P. Berrangé

The TLS handshake make take some time to complete, during which time an I/O watch might be registered with the main loop. If the owner of the I/O channel invokes qio_channel_close() while the handshake is waiting to continue the I/O watch must be removed. Failing to remove it will later trigger the completion callback which the owner is not expecting to receive. In the case of the VNC server, this results in a SEGV as vnc_disconnect_start() tries to shutdown a client connection that is already gone / NULL.

CVE-2023-3354

---
 include/io/channel-tls.h |  1 +
 io/channel-tls.c         | 18 ++++++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
include/io/channel-tls.h +1 -0
index 5672479e9e..26c67f17e2 100644
@@ -48,6 +48,7 @@ struct QIOChannelTLS {
     QIOChannel *master;
     QCryptoTLSSession *session;
     QIOChannelShutdown shutdown;
+    guint hs_ioc_tag;
 };
 
 /**
io/channel-tls.c +8 -6
index 9805dd0a3f..e327e6a5c2 100644
@@ -198,12 +198,13 @@ static void
 qio_channel_tls_handshake_task(QIOChannelTLS *ioc,
         }
 
         trace_qio_channel_tls_handshake_pending(ioc, status);
-        qio_channel_add_watch_full(ioc->master,
-                                   condition,
-                                   qio_channel_tls_handshake_io,
-                                   data,
-                                   NULL,
-                                   context);
+        ioc->hs_ioc_tag =
+            qio_channel_add_watch_full(ioc->master,
+                                       condition,
+                                       qio_channel_tls_handshake_io,
+                                       data,
+                                       NULL,
+                                       context);
     }
 }
@@ -218,6 +219,7 @@ static gboolean
 qio_channel_tls_handshake_io(QIOChannel *ioc,
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(
         qio_task_get_source(task));
 
+    tioc->hs_ioc_tag = 0;
     g_free(data);
     qio_channel_tls_handshake_task(tioc, task, context);
@@ -378,6 +380,10 @@ static int qio_channel_tls_close(QIOChannel *ioc,
 {
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
 
Quoted 7 lines

io/channel-tls.c static int qio_channel_tls_close(QIOChannel *ioc, · line 389

Quoted 3 lines
io/channel-tls.c +2 -0
+    if (tioc->hs_ioc_tag) {
+        g_source_remove(tioc->hs_ioc_tag);
 

set it to 0 ?
or
g_clear_handle_id(&tios->hs_ioc_tag, g_source_remove);

Yes, close can be called mutliple times, so we must set to zero.

Quoted 5 lines
Quoted 5 lines
io/channel-tls.c +2 -0
+    }
+
     return qio_channel_close(tioc->master, errp);
 }
Quoted 8 lines
Quoted 5 lines

--
2.41.0

Signature · 1 line
Marc-André Lureau

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 1 line · Daniel P. Berrangé

On Fri, Jul 07, 2023 at 01:30:16PM +0400, Marc-André Lureau wrote:

...>>> @@ -378,6 +380,10 @@ static int qio_channel_tls_close(QIOChannel *ioc,

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
  {
      QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
io/channel-tls.c +2 -0
+    if (tioc->hs_ioc_tag) {
+        g_source_remove(tioc->hs_ioc_tag);
Quoted 5 lines
Quoted 3 lines

set it to 0 ?
or
g_clear_handle_id(&tios->hs_ioc_tag, g_source_remove);

Yes, close can be called mutliple times, so we must set to zero.

Ping, for qemu 8.1 and -stable ?

Thanks,

/mjt