Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Kuldeep Singh <kuldeep.singh@oss.qualcomm.com>
To: Wenjia Zhang <wenjia.zhang@oss.qualcomm.com>,
	Bartosz Golaszewski <brgl@kernel.org>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>
Cc: linux-arm-msm@vger.kernel.org, linux-crypto@vger.kernel.org,
	Wenjia Zhang <wenjz@qti.qualcomm.com>
Subject: Re: [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment
Date: Tue, 1 Dec 2026 10:36:38 +0530	[thread overview]
Message-ID: <67ee24a7-c39a-425c-85f0-4a2adec83eeb@oss.qualcomm.com> (raw)
In-Reply-To: <20261201-codex-qce-dma-bounds-upstream-20261201-v1-1-c15d01b45bce@oss.qualcomm.com>

On 01-12-2026 08:24, Wenjia Zhang wrote:
> From: Wenjia Zhang <wenjz@qti.qualcomm.com>

Please check below.

> 
> An AEAD source scatterlist can contain bytes beyond the input segment,
> such as space reserved for an output authentication tag.
> sg_nents_for_len() counts the entries covering a length but does not
> shorten the final entry. Mapping the original list can therefore pass
> those trailing bytes to BAM, exceeding the programmed crypto segment and
> causing transfer errors or timeouts.
> 
> Build a bounded source table for separately mapped input lists,
> referencing the original pages and limiting the final entry to the
> required length. Validate the total mapped input length against the
> configured segment. Include the authentication tag for CCM decryption,
> where hardware consumes it, but exclude it for authenc decryption, where
> software verifies it.
> 
> Handle the zero return from dma_map_sg() as a mapping failure and
> release the allocated scatterlist tables on completion and preparation
> or submission errors.
> 
> Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>

Please fix your S-o-b/From email address for all the submissions.
No submissions should be from qti email.

Did you run full selftests and userspace kcapi_convenience test both?

> ---
>  drivers/crypto/qce/aead.c | 117 +++++++++++++++++++++++++++++++---------------
>  1 file changed, 79 insertions(+), 38 deletions(-)
> 
> diff --git a/drivers/crypto/qce/aead.c b/drivers/crypto/qce/aead.c
> index 2065c46162c217484016021bbe3ee2f2db082e65..793913b33da6fc01f96a1e7612807f5e5d413439 100644
> --- a/drivers/crypto/qce/aead.c
> +++ b/drivers/crypto/qce/aead.c
> @@ -48,18 +48,8 @@ static void qce_aead_done(void *data)
>  
>  	dma_unmap_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
>  
> -	if (IS_CCM(rctx->flags)) {
> -		if (req->assoclen) {
> -			sg_free_table(&rctx->src_tbl);
> -			if (diff_dst)
> -				sg_free_table(&rctx->dst_tbl);
> -		} else {
> -			if (!(IS_DECRYPT(rctx->flags) && !diff_dst))
> -				sg_free_table(&rctx->dst_tbl);
> -		}
> -	} else {
> -		sg_free_table(&rctx->dst_tbl);
> -	}
> +	sg_free_table(&rctx->src_tbl);
> +	sg_free_table(&rctx->dst_tbl);
>  
>  	error = qce_check_status(qce, &status);
>  	if (error < 0 && (error != -EBADMSG))
> @@ -291,11 +281,40 @@ qce_aead_ccm_prepare_buf_assoclen(struct aead_request *req)
>  	return ret;
>  }
>  
> +static int qce_aead_prepare_src_buf(struct aead_request *req,
> +				    unsigned int len)
> +{
> +	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
> +	struct scatterlist *sg;
> +	gfp_t gfp;
> +	int ret;
> +
> +	rctx->src_nents = sg_nents_for_len(req->src, len);
> +	if (rctx->src_nents < 0)
> +		return rctx->src_nents;
> +
> +	gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
> +		GFP_KERNEL : GFP_ATOMIC;
> +	ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
> +	if (ret)
> +		return ret;
> +
> +	/* The last entry may include tag space or unrelated trailing bytes. */
> +	sg = qce_sgtable_add(&rctx->src_tbl, req->src, len);
> +	if (IS_ERR(sg)) {
> +		ret = PTR_ERR(sg);
> +		sg_free_table(&rctx->src_tbl);
> +		return ret;
> +	}
> +	sg_mark_end(sg);
> +	rctx->src_sg = rctx->src_tbl.sgl;
> +
> +	return 0;
> +}
> +
>  static int qce_aead_prepare_buf(struct aead_request *req)
>  {
>  	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
> -	struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
> -	struct qce_device *qce = tmpl->qce;
>  	struct scatterlist *sg;
>  	bool diff_dst = (req->src != req->dst) ? true : false;
>  	unsigned int totallen;
> @@ -305,17 +324,11 @@ static int qce_aead_prepare_buf(struct aead_request *req)
>  	sg = qce_aead_prepare_dst_buf(req);
>  	if (IS_ERR(sg))
>  		return PTR_ERR(sg);
> -	if (diff_dst) {
> -		rctx->src_nents = sg_nents_for_len(req->src, totallen);
> -		if (rctx->src_nents < 0) {
> -			dev_err(qce->dev, "Invalid numbers of src SG.\n");
> -			return -EINVAL;
> -		}
> -		rctx->src_sg = req->src;
> -	} else {
> -		rctx->src_nents = rctx->dst_nents - 1;
> -		rctx->src_sg = rctx->dst_sg;
> -	}
> +	if (diff_dst)
> +		return qce_aead_prepare_src_buf(req, totallen);
> +
> +	rctx->src_nents = rctx->dst_nents - 1;
> +	rctx->src_sg = rctx->dst_sg;
>  	return 0;
>  }
>  
> @@ -327,6 +340,7 @@ static int qce_aead_ccm_prepare_buf(struct aead_request *req)
>  	struct scatterlist *sg;
>  	bool diff_dst = (req->src != req->dst) ? true : false;
>  	unsigned int cryptlen;
> +	int ret;
>  
>  	if (rctx->assoclen)
>  		return qce_aead_ccm_prepare_buf_assoclen(req);
> @@ -336,8 +350,9 @@ static int qce_aead_ccm_prepare_buf(struct aead_request *req)
>  
>  	cryptlen = rctx->cryptlen + ctx->authsize;
>  	if (diff_dst) {
> -		rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
> -		rctx->src_sg = req->src;
> +		ret = qce_aead_prepare_src_buf(req, cryptlen);
> +		if (ret)
> +			return ret;
>  		sg = qce_aead_prepare_dst_buf(req);
>  		if (IS_ERR(sg))
>  			return PTR_ERR(sg);
> @@ -397,6 +412,30 @@ static int qce_aead_create_ccm_nonce(struct qce_aead_reqctx *rctx, struct qce_ae
>  	return 0;
>  }
>  
> +static int qce_aead_check_input(struct aead_request *req, int mapped_nents)
> +{
> +	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
> +	struct qce_aead_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
> +	struct scatterlist *sg;
> +	u64 dma_len = 0;
> +	unsigned int expected = rctx->cryptlen + rctx->assoclen;
> +	int nents, i, ret;
> +
> +	/* CCM decrypt consumes the tag; authenc verifies it in software. */
> +	if (IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags))
> +		expected += ctx->authsize;
> +
> +	nents = sg_nents_for_len(req->src, req->assoclen + req->cryptlen);
> +	if (nents < 0)
> +		return nents;
> +	for_each_sg(rctx->src_sg, sg, mapped_nents, i)
> +		dma_len += sg_dma_len(sg);
> +
> +	ret = dma_len == expected ? 0 : -EINVAL;
> +
> +	return ret;
> +}
> +
>  static int
>  qce_aead_async_req_handle(struct crypto_async_request *async_req)
>  {
> @@ -410,6 +449,9 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
>  	bool diff_dst;
>  	int dst_nents, src_nents, ret;
>  
> +	rctx->src_tbl.sgl = NULL;
> +	rctx->dst_tbl.sgl = NULL;
> +
>  	if (IS_CCM_RFC4309(rctx->flags)) {
>  		memset(rctx->ccm_rfc4309_iv, 0, QCE_MAX_IV_SIZE);
>  		rctx->ccm_rfc4309_iv[0] = 3;
> @@ -441,7 +483,7 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
>  		ret = qce_aead_prepare_buf(req);
>  
>  	if (ret)
> -		return ret;
> +		goto error_free;
>  	dst_nents = dma_map_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
>  	if (!dst_nents) {
>  		ret = -EIO;
> @@ -450,8 +492,8 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
>  
>  	if (diff_dst) {
>  		src_nents = dma_map_sg(qce->dma_dev, rctx->src_sg, rctx->src_nents, dir_src);
> -		if (src_nents < 0) {
> -			ret = src_nents;
> +		if (!src_nents) {
> +			ret = -EIO;
>  			goto error_unmap_dst;
>  		}
>  	} else {
> @@ -461,6 +503,10 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
>  			src_nents = dst_nents - 1;
>  	}
>  
> +	ret = qce_aead_check_input(req, src_nents);
> +	if (ret)
> +		goto error_unmap_src;
> +
>  	ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents, rctx->dst_sg, dst_nents,
>  			       qce_aead_done, async_req);
>  	if (ret)
> @@ -478,17 +524,12 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
>  	qce_dma_terminate_all(&qce->dma);
>  error_unmap_src:
>  	if (diff_dst)
> -		dma_unmap_sg(qce->dma_dev, req->src, rctx->src_nents, dir_src);
> +		dma_unmap_sg(qce->dma_dev, rctx->src_sg, rctx->src_nents, dir_src);
>  error_unmap_dst:
>  	dma_unmap_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
>  error_free:
> -	if (IS_CCM(rctx->flags) && rctx->assoclen) {
> -		sg_free_table(&rctx->src_tbl);
> -		if (diff_dst)
> -			sg_free_table(&rctx->dst_tbl);
> -	} else {
> -		sg_free_table(&rctx->dst_tbl);
> -	}
> +	sg_free_table(&rctx->src_tbl);
> +	sg_free_table(&rctx->dst_tbl);
>  	return ret;
>  }
>  
> 

-- 
Regards
Kuldeep


  parent reply	other threads:[~2026-12-01  5:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  2:54 [PATCH 0/4] QCE data DMA transfer length fixes Wenjia Zhang
2026-12-01  2:54 ` [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment Wenjia Zhang
2026-12-01  3:06   ` sashiko-bot
2026-12-01  5:06   ` Kuldeep Singh [this message]
2026-12-01  2:54 ` [PATCH 2/4] crypto: qce - Size CCM decrypt output DMA for the tag length Wenjia Zhang
2026-12-01  3:13   ` sashiko-bot
2026-12-01  2:54 ` [PATCH 3/4] crypto: qce - Bound ahash input DMA to the current segment Wenjia Zhang
2026-12-01  3:02   ` sashiko-bot
2026-12-01  2:54 ` [PATCH 4/4] crypto: qce - Bound skcipher input DMA to cryptlen Wenjia Zhang
2026-12-01  3:01   ` sashiko-bot

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=67ee24a7-c39a-425c-85f0-4a2adec83eeb@oss.qualcomm.com \
    --to=kuldeep.singh@oss.qualcomm.com \
    --cc=brgl@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=wenjia.zhang@oss.qualcomm.com \
    --cc=wenjz@qti.qualcomm.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