Skip to content

Commit ea68394

Browse files
herbertxgregkh
authored andcommitted
lib/mpi: Fix karactx leak in mpi_powm
commit c8ea9fc upstream. Sometimes mpi_powm will leak karactx because a memory allocation failure causes a bail-out that skips the freeing of karactx. This patch moves the freeing of karactx to the end of the function like everything else so that it can't be skipped. Reported-by: [email protected] Fixes: cdec9cb ("crypto: GnuPG based MPI lib - source files...") Cc: <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Reviewed-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 443449d commit ea68394

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

lib/mpi/mpi-pow.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
3838
{
3939
mpi_ptr_t mp_marker = NULL, bp_marker = NULL, ep_marker = NULL;
40+
struct karatsuba_ctx karactx = {};
4041
mpi_ptr_t xp_marker = NULL;
4142
mpi_ptr_t tspace = NULL;
4243
mpi_ptr_t rp, ep, mp, bp;
@@ -164,13 +165,11 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
164165
int c;
165166
mpi_limb_t e;
166167
mpi_limb_t carry_limb;
167-
struct karatsuba_ctx karactx;
168168

169169
xp = xp_marker = mpi_alloc_limb_space(2 * (msize + 1));
170170
if (!xp)
171171
goto enomem;
172172

173-
memset(&karactx, 0, sizeof karactx);
174173
negative_result = (ep[0] & 1) && base->sign;
175174

176175
i = esize - 1;
@@ -295,8 +294,6 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
295294
if (mod_shift_cnt)
296295
mpihelp_rshift(rp, rp, rsize, mod_shift_cnt);
297296
MPN_NORMALIZE(rp, rsize);
298-
299-
mpihelp_release_karatsuba_ctx(&karactx);
300297
}
301298

302299
if (negative_result && rsize) {
@@ -313,6 +310,7 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
313310
leave:
314311
rc = 0;
315312
enomem:
313+
mpihelp_release_karatsuba_ctx(&karactx);
316314
if (assign_rp)
317315
mpi_assign_limb_space(res, rp, size);
318316
if (mp_marker)

0 commit comments

Comments
 (0)