Linux cryptographic layer development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jose Nieto" <jnieto.alaber@gmail.com>,
	"Corentin Labbe" <clabbe@baylibre.com>,
	"Herbert Xu" <herbert@gondor.apana.org.au>
Cc: linux-crypto@vger.kernel.org, linux-amlogic@lists.infradead.org,
	devicetree@vger.kernel.org,
	"Neil Armstrong" <neil.armstrong@linaro.org>,
	"Martin Blumenstingl" <martin.blumenstingl@googlemail.com>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Alexey Romanov" <romanov.alexey2000@gmail.com>
Subject: Re: [PATCH v14 24/30] arm64: dts: amlogic: g12: add crypto node
Date: Fri, 27 Nov 2026 12:36:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-237415@kernel.org> (raw)
In-Reply-To: <20261127120103.1532882-25-jnieto.alaber@gmail.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] crypto: amlogic: double completion on error in
  meson_hasher_do_one_request()
- [High] crypto: amlogic: zero-byte SHA DMA descriptor in
  meson_hasher_do_one_request()
- [High] crypto: amlogic: unaligned DMA request context in
  meson_hasher_do_one_request()
- [Medium] crypto: amlogic: partial_addr DMA mapping leak in
  meson_kick_hardware()
- [Medium] crypto: amlogic: unexpected req->src DMA map on final in
  meson_hasher_req_map()
--

commit de440de738747f23777b7e1340e71b73e7c85fdd
Author: Alexey Romanov <romanov.alexey2000@gmail.com>

arm64: dts: amlogic: g12: add crypto node

This series adds hardware SHA1/SHA224/SHA256 hashing support to the Amlogic
crypto driver and enables the crypto node on Amlogic G12-series SoCs.

> diff --git a/drivers/crypto/amlogic/amlogic-gxl-hasher.c b/drivers/crypto/amlogic/amlogic-gxl-hasher.c
> new file mode 100644
> index 0000000000000..978cdcd8a4f91
> --- /dev/null
> +++ b/drivers/crypto/amlogic/amlogic-gxl-hasher.c
> @@ -0,0 +1,510 @@

[ ... ]

> +static int meson_hasher_req_map(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_dev *mc = tctx->mc;
> +	int ret;
> +
> +	if (!req->nbytes)
> +		return 0;
> +
> +	ret = dma_map_sg(mc->dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
> +	if (!ret) {
> +		dev_err(mc->dev, "Cannot DMA MAP request data\n");
> +		return -ENOMEM;
> +	}
> +
> +	return 0;
> +}
> +
> +static void meson_hasher_req_unmap(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_dev *mc = tctx->mc;
> +
> +	if (!req->nbytes)
> +		return;
> +
> +	dma_unmap_sg(mc->dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
> +}

[Severity: Medium]
Can meson_hasher_req_map() and meson_hasher_req_unmap() dereference a NULL or
dangling req->src during crypto_ahash_final()?

In meson_hasher_do_one_request(), ctx.nbytes is forced to 0 when
rctx->final_req && !rctx->digest_req, because final() does not consume input
from req->src.  However, meson_hasher_req_map() and meson_hasher_req_unmap()
check req->nbytes directly.  If a caller leaves req->nbytes non-zero from an
earlier update() (as ahash_def_finup() does) and releases or clears req->src
before calling crypto_ahash_final(), sg_nents(req->src) and dma_map_sg() will
access an invalid scatterlist.  Checking ctx.nbytes instead of req->nbytes
avoids mapping req->src on final requests.

[ ... ]

> +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_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
> +	struct meson_dev *mc = tctx->mc;
> +	struct meson_flow *flow = &mc->chanlist[rctx->flow];
> +	int ret;
> +
> +	reinit_completion(&flow->complete);
> +	meson_dma_start(mc, rctx->flow);
> +
> +	ret = wait_for_completion_timeout(&flow->complete,
> +					  msecs_to_jiffies(500));
> +	if (ret == 0) {
> +		dev_err(mc->dev, "DMA timeout for flow %d\n", rctx->flow);
> +		return -EINVAL;
> +	} else if (ret < 0) {
> +		dev_err(mc->dev, "Waiting for DMA completion is failed (%d)\n", ret);
> +		return ret;
> +	}
> +
> +	if (rctx->partial_mapped) {
> +		dma_unmap_single(mc->dev, rctx->partial_addr,
> +				 rctx->partial_size,
> +				 DMA_TO_DEVICE);
> +		rctx->partial_size = 0;
> +		rctx->partial_mapped = false;
> +	}
> +
> +	ctx->tloffset = 0;
> +
> +	return 0;
> +}

[Severity: Medium]
Does meson_kick_hardware() leak the rctx->partial_addr DMA mapping when
wait_for_completion_timeout() times out?

When rctx->partial_mapped is true and the wait times out, returning -EINVAL
skips the dma_unmap_single() call below, and the fail path in
meson_hasher_do_one_request() only unmaps req->src and ctx.state_addr.

[ ... ]

> +	for (;;) {
> +		unsigned int len = ctx.src_sg ?
> +			min(sg_dma_len(ctx.src_sg) - ctx.src_offset, ctx.nbytes) : 0;
> +
> +		ctx.src_addr = 0;
> +		ctx.todo = 0;
> +
> +		if (!rctx->final_req && !ctx.nbytes)
> +			break;
> +
> +		meson_setup_state_descs(&ctx);
> +
> +		if (rctx->partial_size && rctx->partial_size < blocksize) {
> +			ret = meson_fill_partial_buffer(&ctx, len);
> +			if (ret)
> +				goto fail;
> +		} else if (len && len < blocksize) {
> +			memcpy(rctx->partial, sg_virt(ctx.src_sg) + ctx.src_offset, len);
> +
> +			rctx->partial_size = len;
> +			ctx.nbytes -= len;
> +			ctx.src_offset += len;
> +		} else if (len) {
> +			ctx.src_addr = sg_dma_address(ctx.src_sg) + ctx.src_offset;
> +			ctx.todo = min(rounddown(DESC_MAXLEN, blocksize),
> +				       rounddown(len, blocksize));
> +			ctx.nbytes -= ctx.todo;
> +			ctx.src_offset += ctx.todo;
> +		}
> +
> +		if (ctx.src_sg && ctx.src_offset == sg_dma_len(ctx.src_sg)) {
> +			ctx.src_offset = 0;
> +			ctx.src_sg = sg_next(ctx.src_sg);
> +		}
> +
> +		if (!ctx.todo && ctx.nbytes)
> +			continue;
> +
> +		if (!ctx.todo && rctx->final_req && rctx->partial_size)
> +			continue;
> +
> +		if (!ctx.todo && !rctx->final_req && !ctx.tloffset)
> +			continue;
> +
> +		if (meson_setup_data_descs(&ctx)) {
> +			ret = meson_kick_hardware(&ctx);
> +			if (ret)
> +				goto fail;
> +		}
> +	}

[Severity: High]
Can this loop submit a 0-byte SHA DMA descriptor with t_src = 0 during
incremental crypto_ahash_update() calls, corrupting the hardware SHA state?

There are two ways a non-final update() reaches meson_setup_data_descs() with
ctx.todo == 0, ctx.src_addr == 0, ctx.nbytes == 0, and ctx.tloffset > 0:

1. If an update() request has full blocks followed by a trailing partial
block (for example 70 bytes in one SG entry), the first iteration emits a
64-byte data descriptor without DESC_LAST (because ctx.nbytes is 6) and sets
ctx.tloffset = 1.  The second iteration copies the trailing 6 bytes into
rctx->partial and sets ctx.nbytes = 0 while ctx.todo stays 0.

2. On a subsequent update() after an earlier hardware kick (so
rctx->begin_req is false), meson_setup_state_descs() runs at the top of the
loop and increments ctx.tloffset before checking whether the input will only
be buffered into rctx->partial without filling a full block.

In both cases, none of the three continue conditions match because
ctx.tloffset is non-zero, so meson_setup_data_descs() appends a 0-length data
descriptor at address 0 with DESC_LAST and kicks the DMA engine.

> +
> +fail:
> +	meson_hasher_req_unmap(req);
> +
> +fail_map_req:
> +	dma_unmap_single(mc->dev, ctx.state_addr, sizeof(rctx->state),
> +			 DMA_BIDIRECTIONAL);
> +
> +fail_map_single:
> +	if (final_req && ret == 0)
> +		memcpy(req->result, rctx->state, digest_size);
> +
> +	local_bh_disable();
> +	crypto_finalize_hash_request(engine, req, ret);
> +	local_bh_enable();
> +
> +	return ret;
> +}

[Severity: High]
Does returning a negative ret after crypto_finalize_hash_request() complete
the request twice on error?

crypto_finalize_hash_request() already completes req via
crypto_request_complete().  Because the engine is initialized with
crypto_engine_alloc_init(mc->dev, true) (so engine->retry_support is false),
returning a negative error code from do_one_request() causes
crypto_pump_requests() to call crypto_request_complete() a second time on the
same request, leading to a double completion and use-after-free in callers
that free the request in their completion callback.
meson_hasher_do_one_request() should return 0 after finalizing the request,
matching meson_handle_cipher_request().

[ ... ]

> +static int meson_hasher_init(struct crypto_tfm *tfm)
> +{
> +	struct meson_hasher_tfm_ctx *tctx = crypto_tfm_ctx_dma(tfm);
> +	struct crypto_ahash *atfm = __crypto_ahash_cast(tfm);
> +	struct hash_alg_common *alg = crypto_hash_alg_common(atfm);
> +	struct meson_alg_template *algt = container_of(alg,
> +		struct meson_alg_template, alg.ahash.base.halg);
> +
> +	crypto_ahash_set_reqsize_dma(atfm, crypto_ahash_statesize(atfm));
> +
> +	memset(tctx, 0, sizeof(struct meson_hasher_tfm_ctx));
> +
> +	tctx->mc = algt->mc;
> +
> +	return 0;
> +}

[ ... ]

> diff --git a/drivers/crypto/amlogic/amlogic-gxl.h b/drivers/crypto/amlogic/amlogic-gxl.h
> index d68094ffb70af..9c3df0d162b9b 100644
> --- a/drivers/crypto/amlogic/amlogic-gxl.h
> +++ b/drivers/crypto/amlogic/amlogic-gxl.h

[ ... ]

> +struct meson_hasher_req_ctx {
> +	u8 state[SHA256_DIGEST_SIZE + 16] ____cacheline_aligned;
> +	u8 partial[SHA256_BLOCK_SIZE] ____cacheline_aligned;
> +	unsigned int partial_size ____cacheline_aligned;
> +	dma_addr_t partial_addr;
> +	bool partial_mapped;
> +
> +	bool begin_req;
> +	bool final_req;
> +	bool digest_req;
> +	int flow;
> +};

[Severity: High]
Can DMA cache maintenance on rctx->state and rctx->partial corrupt adjacent
fields on non-coherent arm64 platforms?

meson_hasher_init() reserves DMA alignment padding via
crypto_ahash_set_reqsize_dma(), but the hasher callbacks retrieve the request
context with ahash_request_ctx(req) instead of ahash_request_ctx_dma(req),
leaving rctx only 8-byte aligned and sharing a cacheline with struct
ahash_request.

In addition, on arm64 ____cacheline_aligned is 64 bytes while
ARCH_DMA_MINALIGN is 128 bytes.  As a result, state (mapped
DMA_BIDIRECTIONAL) and partial (written by the CPU while state is mapped, and
mapped DMA_TO_DEVICE) share the same 128-byte DMA cacheline granule.  Using
ahash_request_ctx_dma() and aligning DMA buffers in struct
meson_hasher_req_ctx with __aligned(ARCH_DMA_MINALIGN) avoids cacheline
sharing during DMA unmap invalidation.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127120103.1532882-1-jnieto.alaber@gmail.com?part=24

  reply	other threads:[~2026-11-27 12:36 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 19:32 [PATCH v13 00/25] crypto: amlogic: add support for G12/AXG/A1/S4 and misc fixes Jose Nieto
2026-11-26 19:32 ` [PATCH v13 01/25] dt-bindings: crypto: amlogic,gxl-crypto: correct clk and interrupt lines Jose Nieto
2026-11-26 19:43   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 02/25] dt-bindings: crypto: amlogic,gxl-crypto: support new SoC's Jose Nieto
2026-11-26 19:32 ` [PATCH v13 03/25] crypto: amlogic - Don't hardcode IRQ count Jose Nieto
2026-11-26 19:42   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 04/25] crypto: amlogic - Add platform data Jose Nieto
2026-11-26 19:32 ` [PATCH v13 05/25] crypto: amlogic - Remove clock input Jose Nieto
2026-11-26 19:44   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 06/25] crypto: amlogic - Add MMIO helpers Jose Nieto
2026-11-26 19:32 ` [PATCH v13 07/25] crypto: amlogic - Move get_engine_number() Jose Nieto
2026-11-26 19:32 ` [PATCH v13 08/25] crypto: amlogic - Drop status field from meson_flow Jose Nieto
2026-11-26 19:32 ` [PATCH v13 09/25] crypto: amlogic - Move algs definition and cipher API to cipher.c Jose Nieto
2026-11-26 19:41   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 10/25] crypto: amlogic - Cleanup defines Jose Nieto
2026-11-26 19:32 ` [PATCH v13 11/25] crypto: amlogic - Process more than MAXDESCS descriptors Jose Nieto
2026-11-26 19:45   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 12/25] crypto: amlogic - Avoid kzalloc in engine thread Jose Nieto
2026-11-26 19:48   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 13/25] crypto: amlogic - Introduce hasher Jose Nieto
2026-11-26 19:56   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 14/25] crypto: amlogic - Use fallback for 192-bit keys Jose Nieto
2026-11-26 19:32 ` [PATCH v13 15/25] crypto: amlogic - Add support for G12-series Jose Nieto
2026-11-26 19:55   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 16/25] crypto: amlogic - Add support for AXG-series Jose Nieto
2026-11-26 19:32 ` [PATCH v13 17/25] crypto: amlogic - Add support for A1-series Jose Nieto
2026-11-26 19:48   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 18/25] crypto: amlogic: g12: drop CRYPTO_ALG_ALLOCATES_MEMORY Jose Nieto
2026-11-26 19:32 ` [PATCH v13 19/25] crypto: amlogic: hasher: add one-shot digest path Jose Nieto
2026-11-26 19:50   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 20/25] crypto: amlogic: drop unused field and stale includes Jose Nieto
2026-11-26 19:32 ` [PATCH v13 21/25] arm64: dts: amlogic: gxl: correct crypto node definition Jose Nieto
2026-11-26 19:32 ` [PATCH v13 22/25] arm64: dts: amlogic: a1: add crypto node Jose Nieto
2026-11-26 20:01   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 23/25] arm64: dts: amlogic: s4: " Jose Nieto
2026-11-26 20:02   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 24/25] arm64: dts: amlogic: g12: " Jose Nieto
2026-11-26 19:32 ` [PATCH v13 25/25] arm64: dts: amlogic: axg: " Jose Nieto
2026-11-26 20:00   ` sashiko-bot
2026-11-27 12:00 ` [PATCH v14 00/30] crypto: amlogic: add support for G12/AXG/A1/S4 and misc fixes Jose Nieto
2026-11-27 12:00   ` [PATCH v14 01/30] dt-bindings: crypto: amlogic,gxl-crypto: correct clk and interrupt lines Jose Nieto
2026-11-27 12:00   ` [PATCH v14 02/30] dt-bindings: crypto: amlogic,gxl-crypto: support new SoC's Jose Nieto
2026-11-27 12:00   ` [PATCH v14 03/30] crypto: amlogic - Don't hardcode IRQ count Jose Nieto
2026-11-27 12:11     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 04/30] crypto: amlogic - Add platform data Jose Nieto
2026-11-27 12:00   ` [PATCH v14 05/30] crypto: amlogic - Remove clock input Jose Nieto
2026-11-27 12:14     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 06/30] crypto: amlogic - Add MMIO helpers Jose Nieto
2026-11-27 12:00   ` [PATCH v14 07/30] crypto: amlogic - Move get_engine_number() Jose Nieto
2026-11-27 12:00   ` [PATCH v14 08/30] crypto: amlogic - Drop status field from meson_flow Jose Nieto
2026-11-27 12:00   ` [PATCH v14 09/30] crypto: amlogic - Move algs definition and cipher API to cipher.c Jose Nieto
2026-11-27 12:14     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 10/30] crypto: amlogic - Cleanup defines Jose Nieto
2026-11-27 12:00   ` [PATCH v14 11/30] crypto: amlogic - Process more than MAXDESCS descriptors Jose Nieto
2026-11-27 12:13     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 12/30] crypto: amlogic - Avoid kzalloc in engine thread Jose Nieto
2026-11-27 12:18     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 13/30] crypto: amlogic - Introduce hasher Jose Nieto
2026-11-27 12:24     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 14/30] crypto: amlogic - Use fallback for 192-bit keys Jose Nieto
2026-11-27 12:00   ` [PATCH v14 15/30] crypto: amlogic - Add support for G12-series Jose Nieto
2026-11-27 12:27     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 16/30] crypto: amlogic - Add support for AXG-series Jose Nieto
2026-11-27 12:00   ` [PATCH v14 17/30] crypto: amlogic - Add support for A1-series Jose Nieto
2026-11-27 12:00   ` [PATCH v14 18/30] crypto: amlogic: g12: drop CRYPTO_ALG_ALLOCATES_MEMORY Jose Nieto
2026-11-27 12:00   ` [PATCH v14 19/30] crypto: amlogic: hasher: add one-shot digest path Jose Nieto
2026-11-27 12:22     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 20/30] crypto: amlogic: drop unused field and stale includes Jose Nieto
2026-11-27 12:00   ` [PATCH v14 21/30] arm64: dts: amlogic: gxl: correct crypto node definition Jose Nieto
2026-11-27 12:00   ` [PATCH v14 22/30] arm64: dts: amlogic: a1: add crypto node Jose Nieto
2026-11-27 12:00   ` [PATCH v14 23/30] arm64: dts: amlogic: s4: " Jose Nieto
2026-11-27 12:00   ` [PATCH v14 24/30] arm64: dts: amlogic: g12: " Jose Nieto
2026-11-27 12:36     ` sashiko-bot [this message]
2026-11-27 12:00   ` [PATCH v14 25/30] arm64: dts: amlogic: axg: " Jose Nieto
2026-11-27 12:39     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 26/30] dt-bindings: crypto: amlogic,gxl-crypto: keep standard property order Jose Nieto
2026-11-27 12:01   ` [PATCH v14 27/30] crypto: amlogic: fix probe and algorithm registration error paths Jose Nieto
2026-11-27 12:21     ` sashiko-bot
2026-11-27 12:01   ` [PATCH v14 28/30] crypto: amlogic: fix setup descriptor lengths and CBC IV copy Jose Nieto
2026-11-27 12:20     ` sashiko-bot
2026-11-27 12:01   ` [PATCH v14 29/30] crypto: amlogic: drop redundant A1 platform data Jose Nieto
2026-11-27 12:01   ` [PATCH v14 30/30] arm64: dts: amlogic: use the system clock for A1/S4 crypto Jose Nieto
2026-11-27 12:37     ` 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=sashiko-outbox-237415@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=clabbe@baylibre.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=jnieto.alaber@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=martin.blumenstingl@googlemail.com \
    --cc=neil.armstrong@linaro.org \
    --cc=robh@kernel.org \
    --cc=romanov.alexey2000@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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