Skip to contentSkip to conversation

crypto: propagate Error object on premature termination

PATCH In review Daniel P. Berrangé 3 participants 4 messages Oct 6, 2025 – Oct 21, 2025 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: propagate Error object on premature termination

The way that premature termination was handled in TLS connections was changed to handle an ordering problem during graceful shutdown in the migration code.

Unfortunately one of the codepaths returned -1 to indicate an error condition, but failed to set the 'errp' parameter.

This broke error handling in the qio_channel_tls_handshake function, as the QTask callback would no longer see that an error was raised. As a result, the client will go on to try to use the already closed TLS connection, resulting in misleading errors.

This was evidenced in the I/O test 233 which showed changes such as

-qemu-nbd: Certificate does not match the hostname localhost
+qemu-nbd: Failed to read initial magic: Unable to read from socket: Connection reset by peer

Fixes 7e0c22d585581b8083ffdeb332ea497218665daf
---
 crypto/tlssession.c |  8 +++++---
 io/channel-tls.c    | 13 +++++++------
 2 files changed, 12 insertions(+), 9 deletions(-)
crypto/tlssession.c +5 -3
index ac38c2121d..8c0bf457ad 100644
@@ -569,8 +569,6 @@ qcrypto_tls_session_read(QCryptoTLSSession *session,
     if (ret < 0) {
         if (ret == GNUTLS_E_AGAIN) {
             return QCRYPTO_TLS_SESSION_ERR_BLOCK;
-        } else if (ret == GNUTLS_E_PREMATURE_TERMINATION) {
-            return QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION;
         } else {
             if (session->rerr) {
                 error_propagate(errp, session->rerr);
@@ -580,7 +578,11 @@ qcrypto_tls_session_read(QCryptoTLSSession *session,
                            "Cannot read from TLS channel: %s",
                            gnutls_strerror(ret));
             }
-            return -1;
+            if (ret == GNUTLS_E_PREMATURE_TERMINATION) {
+                return QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION;
+            } else {
+                return -1;
+            }
         }
     }
 
io/channel-tls.c +7 -6
index 1fbed4be0c..70fad38d18 100644
@@ -368,6 +368,7 @@ static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
                                      int flags,
                                      Error **errp)
 {
+    ERRP_GUARD();
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
     size_t i;
     ssize_t got = 0;
@@ -384,13 +385,13 @@ static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
             } else {
                 return QIO_CHANNEL_ERR_BLOCK;
             }
-        } else if (ret == QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION) {
-            if (qio_channel_tls_allow_premature_termination(tioc, flags)) {
-                ret = 0;
-            } else {
-                return -1;
-            }
         } else if (ret < 0) {
+            if (ret == QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION &&
+                qio_channel_tls_allow_premature_termination(tioc, flags)) {
+                error_free(*errp);
+                *errp = NULL;
+                return got;
+            }
             return -1;
         }
         got += ret;
Signature · 1 line
2.50.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Daniel P. Berrangé

The way that premature termination was handled in TLS connections was changed to handle an ordering problem during graceful shutdown in the migration code.

Unfortunately one of the codepaths returned -1 to indicate an error condition, but failed to set the 'errp' parameter.

Quoted 6 lines

This broke error handling in the qio_channel_tls_handshake function, as the QTask callback would no longer see that an error was raised. As a result, the client will go on to try to use the already closed TLS connection, resulting in misleading errors.

This was evidenced in the I/O test 233 which showed changes such as

Oops..

Quoted 50 lines
(quoted diff) +1 -1
-qemu-nbd: Certificate does not match the hostname localhost
+qemu-nbd: Failed to read initial magic: Unable to read from socket: Connection reset by peer
Fixes 7e0c22d585581b8083ffdeb332ea497218665daf
---
 crypto/tlssession.c |  8 +++++---
 io/channel-tls.c    | 13 +++++++------
 2 files changed, 12 insertions(+), 9 deletions(-)
crypto/tlssession.c +5 -3
index ac38c2121d..8c0bf457ad 100644
@@ -569,8 +569,6 @@ qcrypto_tls_session_read(QCryptoTLSSession *session,
     if (ret < 0) {
         if (ret == GNUTLS_E_AGAIN) {
             return QCRYPTO_TLS_SESSION_ERR_BLOCK;
-        } else if (ret == GNUTLS_E_PREMATURE_TERMINATION) {
-            return QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION;
         } else {
             if (session->rerr) {
                 error_propagate(errp, session->rerr);
@@ -580,7 +578,11 @@ qcrypto_tls_session_read(QCryptoTLSSession *session,
                            "Cannot read from TLS channel: %s",
                            gnutls_strerror(ret));
             }
-            return -1;
+            if (ret == GNUTLS_E_PREMATURE_TERMINATION) {
+                return QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION;
+            } else {
+                return -1;
+            }
         }
     }
 
io/channel-tls.c +1 -0
index 1fbed4be0c..70fad38d18 100644
@@ -368,6 +368,7 @@ static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
                                      int flags,
                                      Error **errp)
 {
+    ERRP_GUARD();
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
     size_t i;
     ssize_t got = 0;

io/channel-tls.c static ssize_t qio_channel_tls_readv(QIOChannel *ioc, · line 390

Quoted 7 lines
io/channel-tls.c +0 -4
@@ -384,13 +385,13 @@ static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
             } else {
                 return QIO_CHANNEL_ERR_BLOCK;
             }
-        } else if (ret == QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION) {
-            if (qio_channel_tls_allow_premature_termination(tioc, flags)) {
-                ret = 0;
-            } else {

Would it be simpler to set error only here (and also we can have an explicit error message for the premature termination)?

No strong opinions.. feel free to use whatever version:

Thanks!

Quoted 15 lines
io/channel-tls.c +6 -2
-                return -1;
-            }
         } else if (ret < 0) {
+            if (ret == QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION &&
+                qio_channel_tls_allow_premature_termination(tioc, flags)) {
+                error_free(*errp);
+                *errp = NULL;
+                return got;
+            }
             return -1;
         }
         got += ret;
Signature · 1 line
2.50.1
Signature · 1 line
Peter Xu
#2 lore raw
Quoted 69 lines · Peter Xu
Quoted 7 lines · Daniel P. Berrangé

The way that premature termination was handled in TLS connections was changed to handle an ordering problem during graceful shutdown in the migration code.

Unfortunately one of the codepaths returned -1 to indicate an error condition, but failed to set the 'errp' parameter.

Quoted 6 lines

This broke error handling in the qio_channel_tls_handshake function, as the QTask callback would no longer see that an error was raised. As a result, the client will go on to try to use the already closed TLS connection, resulting in misleading errors.

This was evidenced in the I/O test 233 which showed changes such as

Oops..

Quoted 52 lines
(quoted diff) +1 -1
-qemu-nbd: Certificate does not match the hostname localhost
+qemu-nbd: Failed to read initial magic: Unable to read from socket: Connection reset by peer
Fixes 7e0c22d585581b8083ffdeb332ea497218665daf
---
 crypto/tlssession.c |  8 +++++---
 io/channel-tls.c    | 13 +++++++------
 2 files changed, 12 insertions(+), 9 deletions(-)
crypto/tlssession.c +5 -3
index ac38c2121d..8c0bf457ad 100644
@@ -569,8 +569,6 @@ qcrypto_tls_session_read(QCryptoTLSSession *session,
     if (ret < 0) {
         if (ret == GNUTLS_E_AGAIN) {
             return QCRYPTO_TLS_SESSION_ERR_BLOCK;
-        } else if (ret == GNUTLS_E_PREMATURE_TERMINATION) {
-            return QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION;
         } else {
             if (session->rerr) {
                 error_propagate(errp, session->rerr);
@@ -580,7 +578,11 @@ qcrypto_tls_session_read(QCryptoTLSSession *session,
                            "Cannot read from TLS channel: %s",
                            gnutls_strerror(ret));
             }
-            return -1;
+            if (ret == GNUTLS_E_PREMATURE_TERMINATION) {
+                return QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION;
+            } else {
+                return -1;
+            }
         }
     }
 
io/channel-tls.c +1 -0
index 1fbed4be0c..70fad38d18 100644
@@ -368,6 +368,7 @@ static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
                                      int flags,
                                      Error **errp)
 {
+    ERRP_GUARD();
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
     size_t i;
     ssize_t got = 0;
@@ -384,13 +385,13 @@ static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
             } else {
                 return QIO_CHANNEL_ERR_BLOCK;
Quoted 8 lines

io/channel-tls.c static ssize_t qio_channel_tls_readv(QIOChannel *ioc, · line 390

Quoted 5 lines
io/channel-tls.c +0 -4
             }
-        } else if (ret == QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION) {
-            if (qio_channel_tls_allow_premature_termination(tioc, flags)) {
-                ret = 0;
-            } else {

Would it be simpler to set error only here (and also we can have an explicit error message for the premature termination)?

I wanted to retain the original GNUTLS provided error message, which is not available here, so must set it in the tlssession.c file.

Quoted 6 lines

No strong opinions.. feel free to use whatever version:

Thanks!

Quoted 19 lines
Quoted 15 lines
io/channel-tls.c +6 -2
-                return -1;
-            }
         } else if (ret < 0) {
+            if (ret == QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION &&
+                qio_channel_tls_allow_premature_termination(tioc, flags)) {
+                error_free(*errp);
+                *errp = NULL;
+                return got;
+            }
             return -1;
         }
         got += ret;
Signature · 1 line
2.50.1
Signature · 1 line
Peter Xu

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 :|
Tested-by Reviewed-by on v1 1/1
#3 lore raw
In reply to Daniel P. Berrangé
Quoted 17 lines · Daniel P. Berrangé

The way that premature termination was handled in TLS connections was changed to handle an ordering problem during graceful shutdown in the migration code.

Unfortunately one of the codepaths returned -1 to indicate an error condition, but failed to set the 'errp' parameter.

This broke error handling in the qio_channel_tls_handshake function, as the QTask callback would no longer see that an error was raised. As a result, the client will go on to try to use the already closed TLS connection, resulting in misleading errors.

This was evidenced in the I/O test 233 which showed changes such as

(quoted diff) +1 -1
-qemu-nbd: Certificate does not match the hostname localhost
+qemu-nbd: Failed to read initial magic: Unable to read from socket: Connection reset by peer
Quoted 6 lines
Fixes 7e0c22d585581b8083ffdeb332ea497218665daf
---
 crypto/tlssession.c |  8 +++++---
 io/channel-tls.c    | 13 +++++++------
 2 files changed, 12 insertions(+), 9 deletions(-)

Thanks; queued in my NBD tree, since I have further patches that will further alter the output of 233 to give more details about NBD devices.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org