diff mbox series

[1/2] crypto: avoid leak of ctx when bad cipher mode is given

Message ID 20240610094049.851127-2-berrange@redhat.com
State New
Headers show
Series crypto: two minor fixes | expand

Commit Message

Daniel P. Berrangé June 10, 2024, 9:40 a.m. UTC
This fixes Coverity CID-1546884

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 crypto/cipher-nettle.c.inc | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Peter Maydell June 10, 2024, 9:44 a.m. UTC | #1
On Mon, 10 Jun 2024 at 10:42, Daniel P. Berrangé <berrange@redhat.com> wrote:
>
> This fixes Coverity CID-1546884
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM
Philippe Mathieu-Daudé June 10, 2024, 11:50 a.m. UTC | #2
On 10/6/24 11:40, Daniel P. Berrangé wrote:
> This fixes Coverity CID-1546884
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   crypto/cipher-nettle.c.inc | 7 +++++--
>   1 file changed, 5 insertions(+), 2 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Markus Armbruster June 10, 2024, 1:43 p.m. UTC | #3
Daniel P. Berrangé <berrange@redhat.com> writes:

> This fixes Coverity CID-1546884

We usually say

  Fixes: Coverity CID 1546884

> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
diff mbox series

Patch

diff --git a/crypto/cipher-nettle.c.inc b/crypto/cipher-nettle.c.inc
index 42b39e18a2..766de036ba 100644
--- a/crypto/cipher-nettle.c.inc
+++ b/crypto/cipher-nettle.c.inc
@@ -734,16 +734,19 @@  static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
 #ifdef CONFIG_CRYPTO_SM4
     case QCRYPTO_CIPHER_ALG_SM4:
         {
-            QCryptoNettleSm4 *ctx = g_new0(QCryptoNettleSm4, 1);
+            QCryptoNettleSm4 *ctx;
+            const QCryptoCipherDriver *drv;
 
             switch (mode) {
             case QCRYPTO_CIPHER_MODE_ECB:
-                ctx->base.driver = &qcrypto_nettle_sm4_driver_ecb;
+                drv = &qcrypto_nettle_sm4_driver_ecb;
                 break;
             default:
                 goto bad_cipher_mode;
             }
 
+            ctx = g_new0(QCryptoNettleSm4, 1);
+            ctx->base.driver = drv;
             sm4_set_encrypt_key(&ctx->key[0], key);
             sm4_set_decrypt_key(&ctx->key[1], key);