Skip to content

Commit

Permalink
Fix a key repointing in various ciphers
Browse files Browse the repository at this point in the history
In the dupctx fixups I missed a pointer that needed to be repointed to
the surrounding structures AES_KEY structure for the sm4/aes/aria
ccm/gcm variants.  This caused a colliding use of the key and possible
use after free issues.

Fixes #22076

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from #23102)

(cherry picked from commit 0398bc2)
  • Loading branch information
nhorman authored and t8m committed Jan 5, 2024
1 parent 291796b commit 780b38c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion providers/implementations/ciphers/cipher_aes_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ static void *aes_gcm_newctx(void *provctx, size_t keybits)
static void *aes_gcm_dupctx(void *provctx)
{
PROV_AES_GCM_CTX *ctx = provctx;
PROV_AES_GCM_CTX *dctx = NULL;

if (ctx == NULL)
return NULL;
return OPENSSL_memdup(ctx, sizeof(*ctx));

dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
if (dctx != NULL && dctx->base.gcm.key != NULL)
dctx->base.gcm.key = &dctx->ks.ks;

return dctx;
}

static OSSL_FUNC_cipher_freectx_fn aes_gcm_freectx;
Expand Down
8 changes: 7 additions & 1 deletion providers/implementations/ciphers/cipher_aria_ccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ static void *aria_ccm_newctx(void *provctx, size_t keybits)
static void *aria_ccm_dupctx(void *provctx)
{
PROV_ARIA_CCM_CTX *ctx = provctx;
PROV_ARIA_CCM_CTX *dctx = NULL;

if (ctx == NULL)
return NULL;
return OPENSSL_memdup(ctx, sizeof(*ctx));

dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
if (dctx != NULL && dctx->base.ccm_ctx.key != NULL)
dctx->base.ccm_ctx.key = &dctx->ks.ks;

return dctx;
}

static void aria_ccm_freectx(void *vctx)
Expand Down
8 changes: 7 additions & 1 deletion providers/implementations/ciphers/cipher_aria_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ static void *aria_gcm_newctx(void *provctx, size_t keybits)
static void *aria_gcm_dupctx(void *provctx)
{
PROV_ARIA_GCM_CTX *ctx = provctx;
PROV_ARIA_GCM_CTX *dctx = NULL;

if (ctx == NULL)
return NULL;
return OPENSSL_memdup(ctx, sizeof(*ctx));

dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
if (dctx != NULL && dctx->base.gcm.key != NULL)
dctx->base.gcm.key = &dctx->ks.ks;

return dctx;
}

static OSSL_FUNC_cipher_freectx_fn aria_gcm_freectx;
Expand Down

0 comments on commit 780b38c

Please sign in to comment.