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 13/30] crypto: amlogic - Introduce hasher
Date: Fri, 27 Nov 2026 12:24:43 +0000 [thread overview]
Message-ID: <sashiko-outbox-237372@kernel.org> (raw)
In-Reply-To: <20261127120103.1532882-14-jnieto.alaber@gmail.com>
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] crypto: amlogic: 0-byte SHA DMA descriptor in
meson_hasher_do_one_request()
- [High] crypto: amlogic: double completion on error in
meson_hasher_do_one_request()
- [High] crypto: amlogic: unaligned DMA request ctx via
ahash_request_ctx()
- [High] crypto: amlogic: invalid src DMA map on final in
meson_hasher_req_map()
- [Medium] crypto: amlogic: leaked partial_addr DMA mapping in
meson_kick_hardware()
- [Medium] crypto: amlogic: sg_virt() indexed by sg_dma_len() in
amlogic-gxl-hasher
--
commit 531139f52bee7e6e883c924298c554e02337a015
Author: Alexey Romanov <romanov.alexey2000@gmail.com>
crypto: amlogic - Introduce hasher
This commit adds hardware asynchronous SHA1, SHA224, and SHA256 hash
algorithm support to the Amlogic GXL crypto driver.
> diff --git a/drivers/crypto/amlogic/amlogic-gxl-hasher.c b/drivers/crypto/amlogic/amlogic-gxl-hasher.c
> new file mode 100644
> index 0000000000000..c83739ec34566
> --- /dev/null
> +++ b/drivers/crypto/amlogic/amlogic-gxl-hasher.c
[ ... ]
> +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: High]
Can meson_hasher_req_map() and meson_hasher_req_unmap() dereference an
invalid req->src during crypto_ahash_final()?
The ahash final() callback does not consume input data, and callers such
as ahash_def_finup_finish1() invoke final(req) after update(req) without
clearing req->nbytes. Although meson_hasher_do_one_request() sets
ctx.nbytes = 0 when rctx->final_req is true, it still sets
ctx.src_sg = req->src and calls meson_hasher_req_map() and
meson_hasher_req_unmap(), which check req->nbytes instead of
rctx->final_req. If the caller freed or nulled req->src after update()
completed, sg_nents(req->src) and sg_dma_len(ctx.src_sg) will dereference
a dangling or NULL scatterlist pointer.
[ ... ]
> +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;
> + }
[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 fails, returning early
skips dma_unmap_single() here, and the fail label in
meson_hasher_do_one_request() only unmaps req->src and ctx.state_addr.
> +
> + ctx->tloffset = 0;
> +
> + return 0;
> +}
[ ... ]
> +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_dev *mc = tctx->mc;
> + struct hasher_ctx ctx = {
> + .tloffset = 0,
> + .src_offset = 0,
> + .nbytes = rctx->final_req ? 0 : req->nbytes,
> + .src_sg = req->src,
> + .areq = areq,
> + };
> + unsigned int blocksize = crypto_ahash_blocksize(tfm);
> + unsigned int digest_size = crypto_ahash_digestsize(tfm);
> + bool final_req = rctx->final_req;
> + int ret;
> +
> + ctx.state_addr = dma_map_single(mc->dev, rctx->state,
> + sizeof(rctx->state), DMA_BIDIRECTIONAL);
> + ret = dma_mapping_error(mc->dev, ctx.state_addr);
> + if (ret) {
> + dev_err(mc->dev, "Cannot DMA MAP SHA state buffer");
> + goto fail_map_single;
> + }
> +
> + ret = meson_hasher_req_map(req);
> + if (ret)
> + goto fail_map_req;
> +
> + 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);
[Severity: Medium]
Can indexing sg_virt(ctx.src_sg) by ctx.src_offset read past the end of
ctx.src_sg's virtual buffer when dma_map_sg() coalesces scatterlist
entries?
ctx.src_offset and len are bounded by sg_dma_len(ctx.src_sg), and
ctx.src_sg only advances when ctx.src_offset reaches
sg_dma_len(ctx.src_sg). If dma_map_sg() merges adjacent entries so that
sg_dma_len(ctx.src_sg) exceeds ctx.src_sg->length, ctx.src_offset can grow
larger than ctx.src_sg->length, causing memcpy() here and in
meson_fill_partial_buffer() to read out of bounds from
sg_virt(ctx.src_sg).
> +
> + 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 && !ctx.tloffset)
> + continue;
> +
> + if (meson_setup_data_descs(&ctx)) {
> + ret = meson_kick_hardware(&ctx);
> + if (ret)
> + goto fail;
> + }
> + }
[Severity: High]
Does meson_hasher_do_one_request() emit and kick a 0-byte SHA DMA
descriptor with t_src = 0 when a non-final update() leaves trailing bytes
in rctx->partial?
When a non-final update() has trailing sub-block bytes, ctx.tloffset is
already non-zero when those bytes are copied into rctx->partial: either
meson_setup_state_descs() just queued state descriptors (when
!rctx->begin_req), or an earlier iteration in the same request queued full
blocks without setting DESC_LAST (because ctx.nbytes was still non-zero).
Buffering the trailing bytes reduces ctx.nbytes to 0 while leaving
ctx.todo = 0 and ctx.src_addr = 0. Because ctx.nbytes is 0,
rctx->final_req is false, and ctx.tloffset > 0, neither continue guard
triggers, so meson_setup_data_descs() and meson_kick_hardware() submit a
0-length descriptor at physical address 0 to the hardware.
> +
> +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
req twice on error?
crypto_finalize_hash_request() calls crypto_request_complete(), waking the
caller who may free req. When do_one_request() then returns ret < 0,
crypto_pump_requests() calls crypto_request_complete(async_req, ret) a
second time on the already-completed request:
crypto/crypto_engine.c:crypto_pump_requests() {
...
ret = op->do_one_request(engine, async_req);
/* Request unsuccessfully executed by hardware */
if (ret < 0) {
if (!engine->retry_support ||
(ret != -ENOSPC)) {
dev_err(engine->dev,
"Failed to do one request from queue: %d\n",
ret);
goto req_err_1;
}
...
req_err_1:
crypto_request_complete(async_req, ret);
...
}
meson_hasher_do_one_request() should return 0 after calling
crypto_finalize_hash_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));
[Severity: High]
Should the request callbacks retrieve rctx with ahash_request_ctx_dma(req)
instead of ahash_request_ctx(req), and align the DMA buffers in
struct meson_hasher_req_ctx to ARCH_DMA_MINALIGN?
meson_hasher_init() reserves DMA alignment padding via
crypto_ahash_set_reqsize_dma(), but every callback in this file uses
ahash_request_ctx(req), which is only aligned to CRYPTO_MINALIGN (8 bytes
on arm64), and struct meson_hasher_req_ctx marks state and partial with
____cacheline_aligned (64 bytes on arm64, smaller than
ARCH_DMA_MINALIGN = 128). As a result, the DMA-mapped state and partial
buffers share DMA cachelines with struct ahash_request and CPU-updated
fields in struct meson_hasher_req_ctx, which can corrupt adjacent fields
during DMA cacheline invalidation on non-coherent systems.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127120103.1532882-1-jnieto.alaber@gmail.com?part=13
next prev parent reply other threads:[~2026-11-27 12:24 UTC|newest]
Thread overview: 82+ 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 [this message]
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
2026-11-27 12:00 ` [PATCH v14 25/30] arm64: dts: amlogic: axg: " Jose Nieto
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-237372@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