From 828c3b265b6df877dced3dde56ea41daa2e6181e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Jean?= Date: Thu, 8 Oct 2026 09:35:53 +0000 Subject: [PATCH 1/3] crypto: caam - reject overlong RSA CRT parameters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit caam_read_rsa_crt() right-aligns dP, dQ, and qInv in buffers sized from the corresponding prime. rsa_parse_priv_key() only bounds these integers against n, so a malformed key can provide, for example, a two-byte dP with a one-byte p. dstlen - nbytes then underflows and memcpy() writes outside the allocation during set_priv_key(). KASAN reports a slab-out-of-bounds write in caam_read_rsa_crt(). Reject empty or overlong CRT parameters after stripping leading zeros. qInv is consumed by CAAM as a p-sized value, so size it from p rather than q to match the DMA mapping in set_rsa_priv_f3_pdb(). Let the top-level key cleanup handle partial form-3 allocations and return the specific error to the caller. Also test nbytes before dereferencing it while stripping zeros, so an all-zero integer does not read one byte beyond its input. Fixes: 4a651b122adb ("crypto: caam - add support for RSA key form 3") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Jérémy Jean Reviewed-by: Sahil Malhotra Signed-off-by: Herbert Xu --- drivers/crypto/caam/caampkc.c | 75 ++++++++++++++--------------------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c index ac515ed8c575..2082adaae585 100644 --- a/drivers/crypto/caam/caampkc.c +++ b/drivers/crypto/caam/caampkc.c @@ -880,7 +880,7 @@ static void caam_rsa_free_key(struct caam_rsa_key *key) static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes) { - while (!**ptr && *nbytes) { + while (*nbytes && !**ptr) { (*ptr)++; (*nbytes)--; } @@ -896,22 +896,22 @@ static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes) * @ptr : pointer to {dP, dQ, qInv} CRT member * @nbytes: length in bytes of {dP, dQ, qInv} CRT member * @dstlen: length in bytes of corresponding p or q prime factor + * @dst : pointer to the zero-padded output buffer */ -static u8 *caam_read_rsa_crt(const u8 *ptr, size_t nbytes, size_t dstlen) +static int caam_read_rsa_crt(const u8 *ptr, size_t nbytes, size_t dstlen, + u8 **dst) { - u8 *dst; - caam_rsa_drop_leading_zeros(&ptr, &nbytes); - if (!nbytes) - return NULL; + if (!nbytes || nbytes > dstlen) + return -EINVAL; - dst = kzalloc(dstlen, GFP_KERNEL); - if (!dst) - return NULL; + *dst = kzalloc(dstlen, GFP_KERNEL); + if (!*dst) + return -ENOMEM; - memcpy(dst + (dstlen - nbytes), ptr, nbytes); + memcpy(*dst + (dstlen - nbytes), ptr, nbytes); - return dst; + return 0; } /** @@ -991,6 +991,7 @@ static int caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx, size_t p_sz = raw_key->p_sz; size_t q_sz = raw_key->q_sz; unsigned aligned_size; + int ret; rsa_key->p = caam_read_raw_data(raw_key->p, &p_sz); if (!rsa_key->p) @@ -999,57 +1000,39 @@ static int caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx, rsa_key->q = caam_read_raw_data(raw_key->q, &q_sz); if (!rsa_key->q) - goto free_p; + return -ENOMEM; rsa_key->q_sz = q_sz; aligned_size = ALIGN(raw_key->p_sz, dma_get_cache_alignment()); rsa_key->tmp1 = kzalloc(aligned_size, GFP_KERNEL); if (!rsa_key->tmp1) - goto free_q; + return -ENOMEM; aligned_size = ALIGN(raw_key->q_sz, dma_get_cache_alignment()); rsa_key->tmp2 = kzalloc(aligned_size, GFP_KERNEL); if (!rsa_key->tmp2) - goto free_tmp1; + return -ENOMEM; rsa_key->priv_form = FORM2; - rsa_key->dp = caam_read_rsa_crt(raw_key->dp, raw_key->dp_sz, p_sz); - if (!rsa_key->dp) - goto free_tmp2; + ret = caam_read_rsa_crt(raw_key->dp, raw_key->dp_sz, p_sz, + &rsa_key->dp); + if (ret) + return ret; - rsa_key->dq = caam_read_rsa_crt(raw_key->dq, raw_key->dq_sz, q_sz); - if (!rsa_key->dq) - goto free_dp; + ret = caam_read_rsa_crt(raw_key->dq, raw_key->dq_sz, q_sz, + &rsa_key->dq); + if (ret) + return ret; - rsa_key->qinv = caam_read_rsa_crt(raw_key->qinv, raw_key->qinv_sz, - q_sz); - if (!rsa_key->qinv) - goto free_dq; + ret = caam_read_rsa_crt(raw_key->qinv, raw_key->qinv_sz, p_sz, + &rsa_key->qinv); + if (ret) + return ret; rsa_key->priv_form = FORM3; return 0; - -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; } static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, @@ -1067,6 +1050,8 @@ static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, if (ret) return ret; + ret = -ENOMEM; + /* Copy key in DMA zone */ rsa_key->d = kmemdup(raw_key.d, raw_key.d_sz, GFP_KERNEL); if (!rsa_key->d) @@ -1103,7 +1088,7 @@ static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, err: caam_rsa_free_key(rsa_key); - return -ENOMEM; + return ret; } static unsigned int caam_rsa_max_size(struct crypto_akcipher *tfm) From e9ce64dce975a14700e835003847415e29918474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Jean?= Date: Fri, 16 Oct 2026 07:50:31 +0000 Subject: [PATCH 2/3] crypto: essiv - fix use-after-free in AEAD path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The request that ESSIV submits to its wrapped AEAD transform may finish before crypto_aead_encrypt() or crypto_aead_decrypt() returns. In case this happens, essiv_aead_done() calls aead_request_complete() on the original ESSIV request, which can end up freeing the request before essiv_aead_crypt() resumes, creating a UAF: BUG: KASAN: slab-use-after-free in essiv_aead_crypt+0x8e6/0x950 Read of size 8 at addr ffff888102af1360 by task init/66 Check the return status before dereferencing rctx after submitting that request. If the operation did not return -EINPROGRESS or -EBUSY, then the request cannot have been freed yet. Fixes: be1eb7f78aa8 ("crypto: essiv - create wrapper template for ESSIV generation") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Jérémy Jean Signed-off-by: Herbert Xu --- crypto/essiv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/essiv.c b/crypto/essiv.c index a47a3eab6935..3505b2769d93 100644 --- a/crypto/essiv.c +++ b/crypto/essiv.c @@ -250,7 +250,7 @@ static int essiv_aead_crypt(struct aead_request *req, bool enc) err = enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq); - if (rctx->assoc && err != -EINPROGRESS && err != -EBUSY) + if (err != -EINPROGRESS && err != -EBUSY) kfree(rctx->assoc); return err; } From 26ca5f34841f0a86d0883664a1c13be89a01d351 Mon Sep 17 00:00:00 2001 From: Michael Roth Date: Wed, 21 Oct 2026 14:15:17 -0500 Subject: [PATCH 3/3] crypto: ccp - Make usage of sev_cmd_mutex more granular The current code makes very coarse usage of the sev_cmd_mutex; for SEV firmware ioctls, it is essentially taken upon entry, and ends up wrapping a lot of potentially time-consuming prep work that may need to be done before issuing the actual firmware call, which is the main thing the mutex is meant to protect. Additionally, some of that prep work involves copy_from_user()/copy_to_user() calls which can generate page faults and unecessarily lead to potential deadlock scenarios. No known case exists in current upstream, but with the upcoming KVM/guest_memfd support for in-place conversion of the same PFN between shared/private state, a special fault handler is needed that could theoretically deadlock if a malicious user tries to use guest_memfd pages as the source/target of previously mentioned copy_from_user()/copy_to_user() scenarios. There's no reason to hold the mutex across these problematic scenarios however. Push sev_cmd_mutex down from the top-level sev_ioctl() into each individual command handler, acquiring the mutex mainly around the platform state transitions and firmware command execution. Suggested-by: Sean Christopherson Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Signed-off-by: Michael Roth Tested-by: Sean Christopherson Reviewed-by: Tom Lendacky Signed-off-by: Herbert Xu --- drivers/crypto/ccp/sev-dev.c | 101 ++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 25 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 5c996ab63895..b980dfc7f3a3 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1916,6 +1916,8 @@ static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable) if (!writable) return -EPERM; + mutex_lock(&sev_cmd_mutex); + /* * The SEV spec requires that FACTORY_RESET must be issued in * UNINIT state. Before we go further lets check if any guest is @@ -1927,18 +1929,24 @@ static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable) */ rc = sev_get_platform_state(&state, &argp->error); if (rc) - return rc; + goto out_unlock; - if (state == SEV_STATE_WORKING) - return -EBUSY; + if (state == SEV_STATE_WORKING) { + rc = -EBUSY; + goto out_unlock; + } if (state == SEV_STATE_INIT) { rc = __sev_platform_shutdown_locked(&argp->error); if (rc) - return rc; + goto out_unlock; } - return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); + rc = __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); + +out_unlock: + mutex_unlock(&sev_cmd_mutex); + return rc; } static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) @@ -1948,7 +1956,9 @@ static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) memset(&data, 0, sizeof(data)); + mutex_lock(&sev_cmd_mutex); ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error); + mutex_unlock(&sev_cmd_mutex); if (ret) return ret; @@ -1967,10 +1977,12 @@ static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool wr if (!writable) return -EPERM; + mutex_lock(&sev_cmd_mutex); + if (sev->sev_plat_status.state == SEV_STATE_UNINIT) { rc = sev_move_to_init_state(argp, &shutdown_required); if (rc) - return rc; + goto out_unlock; } rc = __sev_do_cmd_locked(cmd, NULL, &argp->error); @@ -1978,6 +1990,9 @@ static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool wr if (shutdown_required) __sev_firmware_shutdown(sev, false); +out_unlock: + mutex_unlock(&sev_cmd_mutex); + return rc; } @@ -2016,14 +2031,24 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable) data.len = input.length; cmd: + mutex_lock(&sev_cmd_mutex); + if (sev->sev_plat_status.state == SEV_STATE_UNINIT) { ret = sev_move_to_init_state(argp, &shutdown_required); - if (ret) + if (ret) { + mutex_unlock(&sev_cmd_mutex); + /* No shutdown is needed if init fails */ goto e_free_blob; + } } ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error); + if (shutdown_required) + __sev_firmware_shutdown(sev, false); + + mutex_unlock(&sev_cmd_mutex); + /* * Firmware will returns the length of the CSR blob (either the minimum * required length or the actual length written), return it to the user. @@ -2044,9 +2069,6 @@ cmd: } e_free_blob: - if (shutdown_required) - __sev_firmware_shutdown(sev, false); - kfree(blob); return ret; } @@ -2319,19 +2341,26 @@ static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) data.oca_cert_address = __psp_pa(oca_blob); data.oca_cert_len = input.oca_cert_len; + mutex_lock(&sev_cmd_mutex); + /* If platform is not in INIT state then transition it to INIT */ if (sev->sev_plat_status.state != SEV_STATE_INIT) { ret = sev_move_to_init_state(argp, &shutdown_required); - if (ret) + if (ret) { + mutex_unlock(&sev_cmd_mutex); + /* No shutdown is needed if init fails */ goto e_free_oca; + } } ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error); -e_free_oca: if (shutdown_required) __sev_firmware_shutdown(sev, false); + mutex_unlock(&sev_cmd_mutex); + +e_free_oca: kfree(oca_blob); e_free_pek: kfree(pek_blob); @@ -2374,7 +2403,9 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) data.len = 0; } + mutex_lock(&sev_cmd_mutex); ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error); + mutex_unlock(&sev_cmd_mutex); /* * Firmware will return the length of the ID value (either the minimum @@ -2432,7 +2463,9 @@ static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp) data->address = __psp_pa(id_blob); data->len = user_size; + mutex_lock(&sev_cmd_mutex); ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); + mutex_unlock(&sev_cmd_mutex); if (!ret) { if (copy_to_user((void __user *)argp->data, id_blob, data->len)) ret = -EFAULT; @@ -2494,19 +2527,30 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) data.cert_chain_len = input.cert_chain_len; cmd: + mutex_lock(&sev_cmd_mutex); + /* If platform is not in INIT state then transition it to INIT. */ if (sev->sev_plat_status.state != SEV_STATE_INIT) { if (!writable) { ret = -EPERM; + mutex_unlock(&sev_cmd_mutex); goto e_free_cert; } ret = sev_move_to_init_state(argp, &shutdown_required); - if (ret) + if (ret) { + /* No shutdown is needed if init fails */ + mutex_unlock(&sev_cmd_mutex); goto e_free_cert; + } } ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error); + if (shutdown_required) + __sev_firmware_shutdown(sev, false); + + mutex_unlock(&sev_cmd_mutex); + /* * Firmware will return the length of the blobs (either the minimum * required length or the actual length written), return 'em to the user. @@ -2537,9 +2581,6 @@ cmd: } e_free_cert: - if (shutdown_required) - __sev_firmware_shutdown(sev, false); - kfree(cert_blob); e_free_pdh: kfree(pdh_blob); @@ -2561,6 +2602,8 @@ static int __sev_do_snp_platform_status(struct sev_user_data_snp_status *status, data = page_address(status_page); + mutex_lock(&sev_cmd_mutex); + /* * SNP_PLATFORM_STATUS can be executed in any SNP state. But if executed * when SNP has been initialized, the status page must be firmware-owned. @@ -2572,6 +2615,7 @@ static int __sev_do_snp_platform_status(struct sev_user_data_snp_status *status, */ if (rmp_mark_pages_firmware(__pa(data), 1, true)) { ret = -EFAULT; + mutex_unlock(&sev_cmd_mutex); goto cleanup; } } @@ -2585,10 +2629,14 @@ static int __sev_do_snp_platform_status(struct sev_user_data_snp_status *status, * in Firmware state on failure. Use snp_reclaim_pages() to * transition either case back to Hypervisor-owned state. */ - if (snp_reclaim_pages(__pa(data), 1, true)) + if (snp_reclaim_pages(__pa(data), 1, true)) { + mutex_unlock(&sev_cmd_mutex); return -EFAULT; + } } + mutex_unlock(&sev_cmd_mutex); + if (ret) goto cleanup; @@ -2625,7 +2673,9 @@ static int sev_ioctl_do_snp_commit(struct sev_issue_cmd *argp) buf.len = sizeof(buf); + mutex_lock(&sev_cmd_mutex); ret = __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error); + mutex_unlock(&sev_cmd_mutex); return ret; } @@ -2634,6 +2684,7 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable { struct sev_device *sev = psp_master->sev_data; struct sev_user_data_snp_config config; + int ret; if (!argp->data) return -EINVAL; @@ -2647,7 +2698,11 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable if (copy_from_user(&config, (void __user *)argp->data, sizeof(config))) return -EFAULT; - return __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error); + mutex_lock(&sev_cmd_mutex); + ret = __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error); + mutex_unlock(&sev_cmd_mutex); + + return ret; } static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable) @@ -2679,7 +2734,10 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable) input.vlek_wrapped_address = __psp_pa(blob); + mutex_lock(&sev_cmd_mutex); ret = __sev_do_cmd_locked(SEV_CMD_SNP_VLEK_LOAD, &input, &argp->error); + mutex_unlock(&sev_cmd_mutex); + kfree(blob); return ret; @@ -2704,8 +2762,6 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) if (input.cmd > SEV_MAX) return -EINVAL; - mutex_lock(&sev_cmd_mutex); - switch (input.cmd) { case SEV_FACTORY_RESET: @@ -2756,7 +2812,6 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) ret = -EFAULT; out: - mutex_unlock(&sev_cmd_mutex); return ret; } @@ -3105,9 +3160,7 @@ static int get_v1_svn(struct sev_device *sev) struct sev_user_data_snp_status status; int ret, error = 0; - mutex_lock(&sev_cmd_mutex); ret = __sev_do_snp_platform_status(&status, &error); - mutex_unlock(&sev_cmd_mutex); if (ret < 0) return ret; @@ -3122,9 +3175,7 @@ static int get_v2_svn(struct sev_device *sev) struct sev_snp_tcb_version_turin *tcb; int ret, error = 0; - mutex_lock(&sev_cmd_mutex); ret = __sev_do_snp_platform_status(&status, &error); - mutex_unlock(&sev_cmd_mutex); if (ret < 0) return ret;