crypto: sm2 - fix a memory leak in sm2
authorHongbo Li <herberthbli@tencent.com>
Fri, 4 Jun 2021 06:30:35 +0000 (14:30 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 11 Jun 2021 07:03:30 +0000 (15:03 +0800)
SM2 module alloc ec->Q in sm2_set_pub_key(), when doing alg test in
test_akcipher_one(), it will set public key for every test vector,
and don't free ec->Q. This will cause a memory leak.

This patch alloc ec->Q in sm2_ec_ctx_init().

Fixes: ea7ecb66440b ("crypto: sm2 - introduce OSCCA SM2 asymmetric cipher algorithm")
Signed-off-by: Hongbo Li <herberthbli@tencent.com>
Reviewed-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/sm2.c

index b21addc..db8a4a2 100644 (file)
@@ -79,10 +79,17 @@ static int sm2_ec_ctx_init(struct mpi_ec_ctx *ec)
                goto free;
 
        rc = -ENOMEM;
+
+       ec->Q = mpi_point_new(0);
+       if (!ec->Q)
+               goto free;
+
        /* mpi_ec_setup_elliptic_curve */
        ec->G = mpi_point_new(0);
-       if (!ec->G)
+       if (!ec->G) {
+               mpi_point_release(ec->Q);
                goto free;
+       }
 
        mpi_set(ec->G->x, x);
        mpi_set(ec->G->y, y);
@@ -91,6 +98,7 @@ static int sm2_ec_ctx_init(struct mpi_ec_ctx *ec)
        rc = -EINVAL;
        ec->n = mpi_scanval(ecp->n);
        if (!ec->n) {
+               mpi_point_release(ec->Q);
                mpi_point_release(ec->G);
                goto free;
        }
@@ -386,27 +394,15 @@ static int sm2_set_pub_key(struct crypto_akcipher *tfm,
        MPI a;
        int rc;
 
-       ec->Q = mpi_point_new(0);
-       if (!ec->Q)
-               return -ENOMEM;
-
        /* include the uncompressed flag '0x04' */
-       rc = -ENOMEM;
        a = mpi_read_raw_data(key, keylen);
        if (!a)
-               goto error;
+               return -ENOMEM;
 
        mpi_normalize(a);
        rc = sm2_ecc_os2ec(ec->Q, a);
        mpi_free(a);
-       if (rc)
-               goto error;
-
-       return 0;
 
-error:
-       mpi_point_release(ec->Q);
-       ec->Q = NULL;
        return rc;
 }