Linux cryptographic layer development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: ZhaoJinming <zhaojinming@uniontech.com>,
	"Bartosz Golaszewski" <brgl@kernel.org>,
	"Herbert Xu" <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	"Thara Gopinath" <thara.gopinath@linaro.org>
Cc: linux-crypto@vger.kernel.org
Subject: Re: [PATCH v2 6/7] crypto: qce - reject a negative scatterlist count in CCM AAD preparation
Date: Fri, 13 Nov 2026 07:08:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-212320@kernel.org> (raw)
In-Reply-To: <20261113-fix-qce-ccm-adata-leak-v2-6-914501377a62@uniontech.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] crypto: qce: late sg_nents_for_len() check after
  scatterwalk_ffwd()
--

commit 68e95033f32d45aeb2e2a9a17114843648589d9a
Author: ZhaoJinming <zhaojinming@uniontech.com>

crypto: qce - reject a negative scatterlist count in CCM AAD preparation

This commit checks the return value of sg_nents_for_len(req->src, totallen)
in qce_aead_ccm_prepare_buf_assoclen() before passing the entry count to
sg_alloc_table().

> diff --git a/drivers/crypto/qce/aead.c b/drivers/crypto/qce/aead.c
> index 00c5924c057e3..f75e3827d8bcb 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);

[Severity: High]
Is this sg_nents_for_len(req->src, totallen) check placed too late in
qce_aead_ccm_prepare_buf_assoclen()?

Earlier in the function, req->src is already walked and copied before
reaching this check:

drivers/crypto/qce/aead.c:qce_aead_ccm_prepare_buf_assoclen() {
	...
	totallen = cryptlen + req->assoclen;

	/* Get the msg */
	msg_sg = scatterwalk_ffwd(__sg, req->src, req->assoclen);
	...
	/* Copy the associated data */
	if (sg_copy_to_buffer(req->src, sg_nents_for_len(req->src, assoclen),
			      rctx->adata + adata_header_len,
			      assoclen) != assoclen) {
	...
}

If req->src is shorter than req->assoclen, scatterwalk_ffwd() walks past the
end of the scatterlist and triggers a NULL pointer dereference before the new
check is reached, and sg_copy_to_buffer() also receives an unchecked negative
return value from sg_nents_for_len(req->src, assoclen) as its unsigned int
nents argument.  Checking sg_nents_for_len(req->src, totallen) immediately
after computing totallen would validate req->src before scatterwalk_ffwd(),
the rctx->adata allocation, and sg_copy_to_buffer() run.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261113-fix-qce-ccm-adata-leak-v2-0-914501377a62@uniontech.com?part=6

  reply	other threads:[~2026-11-13  7:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13  6:54 [PATCH v2 0/7] crypto: qce - fix CCM AAD buffer leak and AEAD error-path bugs ZhaoJinming
2026-11-13  6:54 ` [PATCH v2 1/7] crypto: qce - unmap the correct scatterlist in the CCM error path ZhaoJinming
2026-11-13  6:54 ` [PATCH v2 2/7] crypto: qce - fix memory leak of the CCM AAD buffer ZhaoJinming
2026-11-13  7:06   ` sashiko-bot
2026-11-13  6:54 ` [PATCH v2 3/7] crypto: qce - check dma_map_sg() result for the source scatterlist ZhaoJinming
2026-11-13  6:54 ` [PATCH v2 4/7] crypto: qce - do not free an uninitialized dst_tbl on the error path ZhaoJinming
2026-11-13  6:54 ` [PATCH v2 5/7] crypto: qce - terminate the DMA channels if descriptor preparation fails ZhaoJinming
2026-11-13  6:54 ` [PATCH v2 6/7] crypto: qce - reject a negative scatterlist count in CCM AAD preparation ZhaoJinming
2026-11-13  7:08   ` sashiko-bot [this message]
2026-11-13  6:54 ` [PATCH v2 7/7] crypto: qce - release dst_tbl when the source scatterlist is invalid ZhaoJinming

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-212320@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=brgl@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=thara.gopinath@linaro.org \
    --cc=zhaojinming@uniontech.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox