Skip to contentSkip to conversation

chardev: close QIOChannel before unref'ing

PATCH In review Daniel P. Berrangé 2 participants 2 messages Jan 10, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 chardev: close QIOChannel before unref'ing

The chardev socket backend will unref the QIOChannel object while it is still potentially open. When using TLS there could be a pending TLS handshake taking place. If the channel is left open then when the TLS handshake callback runs, it can end up accessing free'd memory in the tcp_chr_tls_handshake method.

Closing the QIOChannel will unregister any pending handshake source.

---
 chardev/char-socket.c | 4 ++++
 1 file changed, 4 insertions(+)
chardev/char-socket.c +4 -0
index 73947da188..7105753815 100644
@@ -378,6 +378,10 @@ static void tcp_chr_free_connection(Chardev *chr)
                                  char_socket_yank_iochannel,
                                  QIO_CHANNEL(s->sioc));
     }
+
+    if (s->ioc) {
+        qio_channel_close(s->ioc, NULL);
+    }
     object_unref(OBJECT(s->sioc));
     s->sioc = NULL;
     object_unref(OBJECT(s->ioc));
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi

Quoted 9 lines · Daniel P. Berrangé

The chardev socket backend will unref the QIOChannel object while it is still potentially open. When using TLS there could be a pending TLS handshake taking place. If the channel is left open then when the TLS handshake callback runs, it can end up accessing free'd memory in the tcp_chr_tls_handshake method.

Closing the QIOChannel will unregister any pending handshake source.

ooh oh, one of those little things..

(weak refs could be a solution? but it's also sometime tricky, and we don't have those..)

Quoted 25 lines
---
 chardev/char-socket.c | 4 ++++
 1 file changed, 4 insertions(+)
chardev/char-socket.c +4 -1
index 73947da188..7105753815 100644
@@ -378,6 +378,10 @@ static void tcp_chr_free_connection(Chardev *chr)
                                  char_socket_yank_iochannel,
                                  QIO_CHANNEL(s->sioc));
     }
+
+    if (s->ioc) {
+        qio_channel_close(s->ioc, NULL);
+    }
     object_unref(OBJECT(s->sioc));
     s->sioc = NULL;
     object_unref(OBJECT(s->ioc));
--

2.43.0