Skip to content

Commit 5cd259c

Browse files
herberthbliherbertx
authored andcommitted
crypto: sm2 - fix a memory leak in sm2
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: ea7ecb6 ("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>
1 parent 9039878 commit 5cd259c

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

‎crypto/sm2.c‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,17 @@ static int sm2_ec_ctx_init(struct mpi_ec_ctx *ec)
7979
goto free;
8080

8181
rc = -ENOMEM;
82+
83+
ec->Q = mpi_point_new(0);
84+
if (!ec->Q)
85+
goto free;
86+
8287
/* mpi_ec_setup_elliptic_curve */
8388
ec->G = mpi_point_new(0);
84-
if (!ec->G)
89+
if (!ec->G) {
90+
mpi_point_release(ec->Q);
8591
goto free;
92+
}
8693

8794
mpi_set(ec->G->x, x);
8895
mpi_set(ec->G->y, y);
@@ -91,6 +98,7 @@ static int sm2_ec_ctx_init(struct mpi_ec_ctx *ec)
9198
rc = -EINVAL;
9299
ec->n = mpi_scanval(ecp->n);
93100
if (!ec->n) {
101+
mpi_point_release(ec->Q);
94102
mpi_point_release(ec->G);
95103
goto free;
96104
}
@@ -386,27 +394,15 @@ static int sm2_set_pub_key(struct crypto_akcipher *tfm,
386394
MPI a;
387395
int rc;
388396

389-
ec->Q = mpi_point_new(0);
390-
if (!ec->Q)
391-
return -ENOMEM;
392-
393397
/* include the uncompressed flag '0x04' */
394-
rc = -ENOMEM;
395398
a = mpi_read_raw_data(key, keylen);
396399
if (!a)
397-
goto error;
400+
return -ENOMEM;
398401

399402
mpi_normalize(a);
400403
rc = sm2_ecc_os2ec(ec->Q, a);
401404
mpi_free(a);
402-
if (rc)
403-
goto error;
404-
405-
return 0;
406405

407-
error:
408-
mpi_point_release(ec->Q);
409-
ec->Q = NULL;
410406
return rc;
411407
}
412408

0 commit comments

Comments
 (0)