* [PATCH] crypto: caam - fix double-free in caam_rsa_set_priv_key_form()
@ 2026-09-23 10:14 ZhaoJinming
2026-09-28 9:27 ` [EXT] " Sahil Malhotra (OSS)
2026-10-02 10:10 ` Herbert Xu
0 siblings, 2 replies; 3+ messages in thread
From: ZhaoJinming @ 2026-09-23 10:14 UTC (permalink / raw)
To: Horia Geantă,
Pankaj Gupta, Gaurav Jain, Herbert Xu, David S . Miller
Cc: Tudor Ambarus, Radu Alexe, linux-crypto, linux-kernel, stable,
ZhaoJinming
caam_rsa_set_priv_key_form() frees the partially allocated key
components (p, q, tmp1, tmp2, dp, dq) with kfree_sensitive() on its
error path, but does not set those pointers to NULL afterward.
When the function returns an error, caam_rsa_set_priv_key() jumps to
its err label and calls caam_rsa_free_key(), which frees all of those
same pointers again, resulting in a double-free.
This is reachable when the allocation of q, tmp1, tmp2, dp, dq or qinv
fails, e.g. under memory pressure, or for a malformed key whose CRT
members decode to zero length and make caam_read_rsa_crt() return NULL.
Set each pointer to NULL right after freeing it so that the subsequent
caam_rsa_free_key() call becomes a no-op for the already-freed fields.
Fixes: 52e26d77b8b3 ("crypto: caam - add support for RSA key form 2")
Cc: stable@vger.kernel.org
Signed-off-by: ZhaoJinming <zhaojinming@uniontech.com>
---
drivers/crypto/caam/caampkc.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
index cb001aa1de66..ac515ed8c575 100644
--- a/drivers/crypto/caam/caampkc.c
+++ b/drivers/crypto/caam/caampkc.c
@@ -1033,16 +1033,22 @@ static int caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx,
free_dq:
kfree_sensitive(rsa_key->dq);
+ rsa_key->dq = NULL;
free_dp:
kfree_sensitive(rsa_key->dp);
+ rsa_key->dp = NULL;
free_tmp2:
kfree_sensitive(rsa_key->tmp2);
+ rsa_key->tmp2 = NULL;
free_tmp1:
kfree_sensitive(rsa_key->tmp1);
+ rsa_key->tmp1 = NULL;
free_q:
kfree_sensitive(rsa_key->q);
+ rsa_key->q = NULL;
free_p:
kfree_sensitive(rsa_key->p);
+ rsa_key->p = NULL;
return -ENOMEM;
}
--
2.51.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: [EXT] [PATCH] crypto: caam - fix double-free in caam_rsa_set_priv_key_form()
2026-09-23 10:14 [PATCH] crypto: caam - fix double-free in caam_rsa_set_priv_key_form() ZhaoJinming
@ 2026-09-28 9:27 ` Sahil Malhotra (OSS)
2026-10-02 10:10 ` Herbert Xu
1 sibling, 0 replies; 3+ messages in thread
From: Sahil Malhotra (OSS) @ 2026-09-28 9:27 UTC (permalink / raw)
To: ZhaoJinming, Horia Geanta, Pankaj Gupta, Gaurav Jain, Herbert Xu,
David S . Miller
Cc: Tudor Ambarus, Radu Andrei Alexe, linux-crypto, linux-kernel, stable
Thanks, ZhaoJinming for the fix.
The fix looks correct.
Since RSA key form 3 support was later added by
4a651b122adb ("crypto: caam - add support for RSA key form 3"),
does this double-free affect both form 2 and form 3 parsing paths?
If so, it may be worth mentioning that in the commit message, although
the root cause still appears to have been introduced by 52e26d77b8b3
("crypto: caam - add support for RSA key form 2") .
Reviewed-by: Sahil Malhotra <sahil.malhotra@nxp.com>
NXP Confidential
> -----Original Message-----
> From: ZhaoJinming <zhaojinming@uniontech.com>
> Sent: 23 September 2026 15:45
> To: Horia Geanta <horia.geanta@nxp.com>; Pankaj Gupta
> <pankaj.gupta@nxp.com>; Gaurav Jain <gaurav.jain@nxp.com>; Herbert Xu
> <herbert@gondor.apana.org.au>; David S . Miller <davem@davemloft.net>
> Cc: Tudor Ambarus <tudor-dan.ambarus@nxp.com>; Radu Andrei Alexe
> <radu.alexe@nxp.com>; linux-crypto@vger.kernel.org; linux-
> kernel@vger.kernel.org; stable@vger.kernel.org; ZhaoJinming
> <zhaojinming@uniontech.com>
> Subject: [EXT] [PATCH] crypto: caam - fix double-free in
> caam_rsa_set_priv_key_form()
>
> Caution: This is an external email. Please take care when clicking links or
> opening attachments. When in doubt, report the message using the 'Report
> this email' button
>
>
> caam_rsa_set_priv_key_form() frees the partially allocated key components
> (p, q, tmp1, tmp2, dp, dq) with kfree_sensitive() on its error path, but does
> not set those pointers to NULL afterward.
>
> When the function returns an error, caam_rsa_set_priv_key() jumps to its err
> label and calls caam_rsa_free_key(), which frees all of those same pointers
> again, resulting in a double-free.
>
> This is reachable when the allocation of q, tmp1, tmp2, dp, dq or qinv fails,
> e.g. under memory pressure, or for a malformed key whose CRT members
> decode to zero length and make caam_read_rsa_crt() return NULL.
>
> Set each pointer to NULL right after freeing it so that the subsequent
> caam_rsa_free_key() call becomes a no-op for the already-freed fields.
>
> Fixes: 52e26d77b8b3 ("crypto: caam - add support for RSA key form 2")
> Cc: stable@vger.kernel.org
> Signed-off-by: ZhaoJinming <zhaojinming@uniontech.com>
> ---
> drivers/crypto/caam/caampkc.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
> index cb001aa1de66..ac515ed8c575 100644
> --- a/drivers/crypto/caam/caampkc.c
> +++ b/drivers/crypto/caam/caampkc.c
> @@ -1033,16 +1033,22 @@ static int caam_rsa_set_priv_key_form(struct
> caam_rsa_ctx *ctx,
>
> free_dq:
> kfree_sensitive(rsa_key->dq);
> + rsa_key->dq = NULL;
> free_dp:
> kfree_sensitive(rsa_key->dp);
> + rsa_key->dp = NULL;
> free_tmp2:
> kfree_sensitive(rsa_key->tmp2);
> + rsa_key->tmp2 = NULL;
> free_tmp1:
> kfree_sensitive(rsa_key->tmp1);
> + rsa_key->tmp1 = NULL;
> free_q:
> kfree_sensitive(rsa_key->q);
> + rsa_key->q = NULL;
> free_p:
> kfree_sensitive(rsa_key->p);
> + rsa_key->p = NULL;
> return -ENOMEM;
> }
>
> --
> 2.51.0
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] crypto: caam - fix double-free in caam_rsa_set_priv_key_form()
2026-09-23 10:14 [PATCH] crypto: caam - fix double-free in caam_rsa_set_priv_key_form() ZhaoJinming
2026-09-28 9:27 ` [EXT] " Sahil Malhotra (OSS)
@ 2026-10-02 10:10 ` Herbert Xu
1 sibling, 0 replies; 3+ messages in thread
From: Herbert Xu @ 2026-10-02 10:10 UTC (permalink / raw)
To: ZhaoJinming
Cc: Horia Geantă,
Pankaj Gupta, Gaurav Jain, David S . Miller, Tudor Ambarus,
Radu Alexe, linux-crypto, linux-kernel, stable
On Wed, Sep 23, 2026 at 06:14:57PM +0800, ZhaoJinming wrote:
> caam_rsa_set_priv_key_form() frees the partially allocated key
> components (p, q, tmp1, tmp2, dp, dq) with kfree_sensitive() on its
> error path, but does not set those pointers to NULL afterward.
>
> When the function returns an error, caam_rsa_set_priv_key() jumps to
> its err label and calls caam_rsa_free_key(), which frees all of those
> same pointers again, resulting in a double-free.
>
> This is reachable when the allocation of q, tmp1, tmp2, dp, dq or qinv
> fails, e.g. under memory pressure, or for a malformed key whose CRT
> members decode to zero length and make caam_read_rsa_crt() return NULL.
>
> Set each pointer to NULL right after freeing it so that the subsequent
> caam_rsa_free_key() call becomes a no-op for the already-freed fields.
>
> Fixes: 52e26d77b8b3 ("crypto: caam - add support for RSA key form 2")
> Cc: stable@vger.kernel.org
> Signed-off-by: ZhaoJinming <zhaojinming@uniontech.com>
> ---
> drivers/crypto/caam/caampkc.c | 6 ++++++
> 1 file changed, 6 insertions(+)
Patch applied. Thanks.
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-10-02 10:10 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-23 10:14 [PATCH] crypto: caam - fix double-free in caam_rsa_set_priv_key_form() ZhaoJinming
2026-09-28 9:27 ` [EXT] " Sahil Malhotra (OSS)
2026-10-02 10:10 ` Herbert Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®