[PATCH v15 30/35] crypto: amlogic: fix hasher request completion and DMA context

Jose Nieto jnieto.alaber at gmail.com
Wed Dec 2 02:10:31 PST 2026


Address the review findings on the hardware hasher:

  - Return 0 from meson_hasher_do_one_request() after finalizing the
    request; the crypto engine also completes the request when a negative
    value is returned, leading to a double completion (use-after-free).
  - Fetch the request context with ahash_request_ctx_dma(); the tfm
    request size is reserved with crypto_ahash_set_reqsize_dma() and the
    state/partial buffers are used for DMA.
  - Always unmap the partial buffer in meson_kick_hardware(), including on
    timeout/error.
  - Guard the request scatterlist mapping against a NULL req->src.

Signed-off-by: Jose Nieto <jnieto.alaber at gmail.com>
---
 drivers/crypto/amlogic/amlogic-gxl-hasher.c | 37 +++++++++++----------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/drivers/crypto/amlogic/amlogic-gxl-hasher.c b/drivers/crypto/amlogic/amlogic-gxl-hasher.c
index 710dd413bd51..c8216de20784 100644
--- a/drivers/crypto/amlogic/amlogic-gxl-hasher.c
+++ b/drivers/crypto/amlogic/amlogic-gxl-hasher.c
@@ -19,7 +19,7 @@ static int meson_sha_init(struct ahash_request *req)
 {
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 
 	memset(rctx, 0, sizeof(struct meson_hasher_req_ctx));
 
@@ -33,7 +33,7 @@ static int meson_sha_update(struct ahash_request *req)
 {
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 	struct crypto_engine *engine = tctx->mc->chanlist[rctx->flow].engine;
 
 	return crypto_transfer_hash_request_to_engine(engine, req);
@@ -43,7 +43,7 @@ static int meson_sha_final(struct ahash_request *req)
 {
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 	struct crypto_engine *engine = tctx->mc->chanlist[rctx->flow].engine;
 
 	rctx->final_req = true;
@@ -55,7 +55,7 @@ static int meson_sha_digest(struct ahash_request *req)
 {
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 	struct crypto_engine *engine;
 
 	memset(rctx, 0, sizeof(struct meson_hasher_req_ctx));
@@ -77,7 +77,7 @@ static int meson_hasher_req_map(struct ahash_request *req)
 	struct meson_dev *mc = tctx->mc;
 	int ret;
 
-	if (!req->nbytes)
+	if (!req->nbytes || !req->src)
 		return 0;
 
 	ret = dma_map_sg(mc->dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
@@ -95,7 +95,7 @@ static void meson_hasher_req_unmap(struct ahash_request *req)
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
 	struct meson_dev *mc = tctx->mc;
 
-	if (!req->nbytes)
+	if (!req->nbytes || !req->src)
 		return;
 
 	dma_unmap_sg(mc->dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
@@ -117,7 +117,7 @@ struct hasher_ctx {
 static bool meson_final(struct hasher_ctx *ctx)
 {
 	struct ahash_request *req = ahash_request_cast(ctx->areq);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 
 	return !ctx->nbytes && rctx->final_req;
 }
@@ -127,7 +127,7 @@ static int meson_fill_partial_buffer(struct hasher_ctx *ctx, unsigned int len)
 	struct ahash_request *req = ahash_request_cast(ctx->areq);
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 	struct meson_dev *mc = tctx->mc;
 	unsigned int blocksize = crypto_ahash_blocksize(tfm);
 	unsigned int copy;
@@ -163,7 +163,7 @@ static int meson_fill_partial_buffer(struct hasher_ctx *ctx, unsigned int len)
 static unsigned int meson_setup_data_descs(struct hasher_ctx *ctx)
 {
 	struct ahash_request *req = ahash_request_cast(ctx->areq);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
 	struct meson_dev *mc = tctx->mc;
@@ -202,7 +202,7 @@ static int meson_kick_hardware(struct hasher_ctx *ctx)
 {
 	struct ahash_request *req = ahash_request_cast(ctx->areq);
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
 	struct meson_dev *mc = tctx->mc;
 	struct meson_flow *flow = &mc->chanlist[rctx->flow];
@@ -215,10 +215,11 @@ static int meson_kick_hardware(struct hasher_ctx *ctx)
 					  msecs_to_jiffies(500));
 	if (ret == 0) {
 		dev_err(mc->dev, "DMA timeout for flow %d\n", rctx->flow);
-		return -EINVAL;
+		ret = -EINVAL;
 	} else if (ret < 0) {
 		dev_err(mc->dev, "Waiting for DMA completion is failed (%d)\n", ret);
-		return ret;
+	} else {
+		ret = 0;
 	}
 
 	if (rctx->partial_mapped) {
@@ -231,14 +232,14 @@ static int meson_kick_hardware(struct hasher_ctx *ctx)
 
 	ctx->tloffset = 0;
 
-	return 0;
+	return ret;
 }
 
 static void meson_setup_state_descs(struct hasher_ctx *ctx)
 {
 	struct ahash_request *req = ahash_request_cast(ctx->areq);
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
 	struct meson_dev *mc = tctx->mc;
 	struct meson_desc *desc;
@@ -266,7 +267,7 @@ static int meson_hasher_do_one_request(struct crypto_engine *engine, void *areq)
 	struct ahash_request *req = ahash_request_cast(areq);
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 	struct meson_hasher_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 	struct meson_dev *mc = tctx->mc;
 	struct hasher_ctx ctx = {
 		.tloffset = 0,
@@ -358,12 +359,12 @@ static int meson_hasher_do_one_request(struct crypto_engine *engine, void *areq)
 	crypto_finalize_hash_request(engine, req, ret);
 	local_bh_enable();
 
-	return ret;
+	return 0;
 }
 
 static int meson_hasher_export(struct ahash_request *req, void *out)
 {
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 
 	memcpy(out, rctx, sizeof(*rctx));
 	return 0;
@@ -371,7 +372,7 @@ static int meson_hasher_export(struct ahash_request *req, void *out)
 
 static int meson_hasher_import(struct ahash_request *req, const void *in)
 {
-	struct meson_hasher_req_ctx *rctx = ahash_request_ctx(req);
+	struct meson_hasher_req_ctx *rctx = ahash_request_ctx_dma(req);
 
 	memcpy(rctx, in, sizeof(*rctx));
 	return 0;
-- 
2.47.3




More information about the linux-arm-kernel mailing list