[PATCH v2 6/7] crypto: qce - reject a negative scatterlist count in CCM AAD preparation
From: ZhaoJinming
Date: Fri Nov 13 2026 - 04:17:43 EST
qce_aead_ccm_prepare_buf_assoclen() computes rctx->src_nents from
sg_nents_for_len() without checking for an error. If the source
scatterlist is shorter than the requested length the helper returns a
negative errno, which is added to the extra AAD/result entries and then
passed to sg_alloc_table(). That argument is unsigned int, so a negative
value turns into a huge entry count and sg_alloc_table() keeps allocating
memory until the system runs out.
Check the return value and bail out, releasing the already allocated AAD
buffer through err_free_adata.
Fixes: 9363efb4181c ("crypto: qce - Add support for AEAD algorithms")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: ZhaoJinming <zhaojinming@xxxxxxxxxxxxx>
---
drivers/crypto/qce/aead.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/crypto/qce/aead.c b/drivers/crypto/qce/aead.c
index 00c5924c057e..f75e3827d8bc 100644
--- a/drivers/crypto/qce/aead.c
+++ b/drivers/crypto/qce/aead.c
@@ -186,6 +186,7 @@ qce_aead_ccm_prepare_buf_assoclen(struct aead_request *req)
unsigned int adata_header_len, cryptlen, totallen;
gfp_t gfp;
bool diff_dst;
+ int nents;
int ret;
if (IS_DECRYPT(rctx->flags))
@@ -231,10 +232,16 @@ qce_aead_ccm_prepare_buf_assoclen(struct aead_request *req)
diff_dst = (req->src != req->dst) ? true : false;
+ nents = sg_nents_for_len(req->src, totallen);
+ if (nents < 0) {
+ ret = nents;
+ goto err_free_adata;
+ }
+
if (diff_dst)
- rctx->src_nents = sg_nents_for_len(req->src, totallen) + 1;
+ rctx->src_nents = nents + 1;
else
- rctx->src_nents = sg_nents_for_len(req->src, totallen) + 2;
+ rctx->src_nents = nents + 2;
gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC;
ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
--
2.50.1