Linux-Amlogic Archive on lore.kernel.org
 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>,
	"David S . Miller" <davem@davemloft.net>
Cc: linux-crypto@vger.kernel.org, linux-amlogic@lists.infradead.org,
	devicetree@vger.kernel.org,
	"Neil Armstrong" <neil.armstrong@linaro.org>,
	"Kevin Hilman" <khilman@baylibre.com>,
	"Jerome Brunet" <jbrunet@baylibre.com>,
	"Martin Blumenstingl" <martin.blumenstingl@googlemail.com>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Alexey Romanov" <romanov.alexey2000@gmail.com>
Subject: Re: [PATCH v15 13/35] crypto: amlogic - Introduce hasher
Date: Wed, 02 Dec 2026 10:49:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-245519@kernel.org> (raw)
In-Reply-To: <20261202100714.115460-14-jnieto.alaber@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] crypto: amlogic: 0-byte SHA DMA descriptor in
  meson_hasher_do_one_request()
- [High] crypto: amlogic - req->src accessed and DMA-mapped when
  ctx.nbytes is 0
--

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

crypto: amlogic - Introduce hasher

This commit adds 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
> @@ -0,0 +1,485 @@

[ ... ]

> +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);
> +}

[ ... ]

> +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 crypto_ahash *tfm = crypto_ahash_reqtfm(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];
> +	struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
> +	struct meson_alg_template *algt = container_of(alg,
> +		struct meson_alg_template, alg.ahash.base.halg);
> +	struct meson_desc *desc = &flow->tl[ctx->tloffset];
> +	u32 v;
> +
> +	ctx->tloffset++;
> +
> +	v = DESC_OWN | DESC_ENCRYPTION | DESC_OPMODE_SHA |
> +	    ctx->todo | algt->blockmode;
> +	if (rctx->begin_req) {
> +		rctx->begin_req = false;
> +		v |= DESC_BEGIN;
> +	}
> +
> +	if (!ctx->nbytes && rctx->final_req) {
> +		rctx->final_req = false;
> +		v |= DESC_END;
> +	}
> +
> +	if (!ctx->nbytes || ctx->tloffset == MAXDESC || rctx->partial_mapped)
> +		v |= DESC_LAST;
> +
> +	desc->t_src = cpu_to_le32(ctx->src_addr);
> +	desc->t_dst = cpu_to_le32(ctx->state_addr);
> +	desc->t_status = cpu_to_le32(v);
> +
> +	return v & DESC_LAST;
> +}

[ ... ]

> +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_tfm_ctx *tctx = crypto_ahash_ctx_dma(tfm);
> +	struct meson_dev *mc = tctx->mc;
> +	struct meson_desc *desc;
> +	int i;
> +
> +	if (ctx->tloffset || rctx->begin_req)
> +		return;
> +
> +	for (i = 0; i < mc->pdata->setup_desc_cnt; i++) {
> +		int offset = i * 16;
> +
> +		desc = &mc->chanlist[rctx->flow].tl[ctx->tloffset];
> +		desc->t_src = cpu_to_le32(ctx->state_addr + offset);
> +		desc->t_dst = cpu_to_le32(offset);
> +		desc->t_status = cpu_to_le32(MESON_SHA_BUFFER_SIZE |
> +					     DESC_MODE_KEY | DESC_OWN);
> +
> +		ctx->tloffset++;
> +	}
> +}
> +
> +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;

[Severity: High]
Can meson_hasher_do_one_request() dereference and DMA-map a stale req->src
during final()?

When rctx->final_req is true, ctx.nbytes is 0, yet ctx.src_sg is still
initialized to req->src and dereferenced via sg_dma_len(ctx.src_sg) before
the loop checks ctx.nbytes.  If a caller frees its scatterlist after
crypto_ahash_update() and then calls crypto_ahash_final() (which sets
req->nbytes = 0 without clearing req->src), this causes a use-after-free
read on ctx.src_sg.

In addition, meson_hasher_req_map() and meson_hasher_req_unmap() check
req->nbytes instead of ctx.nbytes, so the final() step of
crypto_ahash_finup() (via ahash_def_finup_finish1(), which leaves
req->nbytes non-zero) still walks and DMA-maps req->src even though
final() consumes no input bytes.  Even with later commits in this series
("crypto: amlogic: fix hasher request completion and DMA context" and
"crypto: amlogic: hasher: add one-shot digest path"), req->src is still
accessed and mapped whenever ctx.nbytes is 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 && !ctx.tloffset)
> +			continue;
> +
> +		if (meson_setup_data_descs(&ctx)) {
> +			ret = meson_kick_hardware(&ctx);
> +			if (ret)
> +				goto fail;
> +		}
> +	}

[Severity: High]
Does a non-final update() that leaves trailing sub-block bytes in
rctx->partial emit and kick a bogus 0-byte SHA DMA descriptor with
t_src = 0?

This happens in two ways when a non-final update() buffers < blocksize
bytes into rctx->partial, reducing ctx.nbytes to 0 with ctx.todo == 0:

1. If an earlier block was already processed by the hardware (so
   rctx->begin_req is false), meson_setup_state_descs() runs before the
   sub-block check and advances ctx.tloffset to setup_desc_cnt.
2. If the same update() queued one or more full blocks before the trailing
   sub-block tail (for example, a 74-byte update), meson_setup_data_descs()
   did not set DESC_LAST on the full block because ctx->nbytes was still
   non-zero, leaving ctx.tloffset > 0.

In both cases, when the trailing bytes are copied into rctx->partial,
ctx.todo is 0, ctx.nbytes becomes 0, rctx->final_req is false, and
ctx.tloffset is non-zero.  Neither continue guard matches (even after
commit "crypto: amlogic: hasher: add one-shot digest path"), so execution
falls through to meson_setup_data_descs() and meson_kick_hardware() with
ctx.src_addr = 0 and ctx.todo = 0, programming a 0-length non-final SHA
descriptor at physical address 0 and corrupting the hash state or timing
out DMA.

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

_______________________________________________
linux-amlogic mailing list
linux-amlogic@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-amlogic

  reply	other threads:[~2026-12-02 10:49 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 10:06 [PATCH v15 00/35] crypto: amlogic: add support for G12/AXG/A1/S4 and misc fixes Jose Nieto
2026-12-02 10:06 ` [PATCH v15 01/35] dt-bindings: crypto: amlogic,gxl-crypto: correct clk and interrupt lines Jose Nieto
2026-12-02 10:19   ` sashiko-bot
2026-12-02 10:06 ` [PATCH v15 02/35] dt-bindings: crypto: amlogic,gxl-crypto: support new SoC's Jose Nieto
2026-12-02 10:06 ` [PATCH v15 03/35] crypto: amlogic - Don't hardcode IRQ count Jose Nieto
2026-12-02 10:06 ` [PATCH v15 04/35] crypto: amlogic - Add platform data Jose Nieto
2026-12-02 10:06 ` [PATCH v15 05/35] crypto: amlogic - Remove clock input Jose Nieto
2026-12-02 10:06 ` [PATCH v15 06/35] crypto: amlogic - Add MMIO helpers Jose Nieto
2026-12-02 10:06 ` [PATCH v15 07/35] crypto: amlogic - Move get_engine_number() Jose Nieto
2026-12-02 10:06 ` [PATCH v15 08/35] crypto: amlogic - Drop status field from meson_flow Jose Nieto
2026-12-02 10:06 ` [PATCH v15 09/35] crypto: amlogic - Move algs definition and cipher API to cipher.c Jose Nieto
2026-12-02 10:06 ` [PATCH v15 10/35] crypto: amlogic - Cleanup defines Jose Nieto
2026-12-02 10:06 ` [PATCH v15 11/35] crypto: amlogic - Process more than MAXDESCS descriptors Jose Nieto
2026-12-02 10:41   ` sashiko-bot
2026-12-02 10:06 ` [PATCH v15 12/35] crypto: amlogic - Avoid kzalloc in engine thread Jose Nieto
2026-12-02 10:06 ` [PATCH v15 13/35] crypto: amlogic - Introduce hasher Jose Nieto
2026-12-02 10:49   ` sashiko-bot [this message]
2026-12-02 10:06 ` [PATCH v15 14/35] crypto: amlogic - Use fallback for 192-bit keys Jose Nieto
2026-12-02 10:06 ` [PATCH v15 15/35] crypto: amlogic - Add support for G12-series Jose Nieto
2026-12-02 10:44   ` sashiko-bot
2026-12-02 10:06 ` [PATCH v15 16/35] crypto: amlogic - Add support for AXG-series Jose Nieto
2026-12-02 10:44   ` sashiko-bot
2026-12-02 10:06 ` [PATCH v15 17/35] crypto: amlogic - Add support for A1-series Jose Nieto
2026-12-02 11:01   ` sashiko-bot
2026-12-02 10:06 ` [PATCH v15 18/35] crypto: amlogic: g12: drop CRYPTO_ALG_ALLOCATES_MEMORY Jose Nieto
2026-12-02 10:06 ` [PATCH v15 19/35] crypto: amlogic: hasher: add one-shot digest path Jose Nieto
2026-12-02 10:06 ` [PATCH v15 20/35] crypto: amlogic: drop unused field and stale includes Jose Nieto
2026-12-02 10:10 ` [PATCH v15 21/35] arm64: dts: amlogic: gxl: correct crypto node definition Jose Nieto
2026-12-02 10:10   ` [PATCH v15 22/35] arm64: dts: amlogic: a1: add crypto node Jose Nieto
2026-12-02 11:04     ` sashiko-bot
2026-12-02 10:10   ` [PATCH v15 23/35] arm64: dts: amlogic: s4: " Jose Nieto
2026-12-02 10:10   ` [PATCH v15 24/35] arm64: dts: amlogic: g12: " Jose Nieto
2026-12-02 11:16     ` sashiko-bot
2026-12-02 10:10   ` [PATCH v15 25/35] arm64: dts: amlogic: axg: " Jose Nieto
2026-12-02 10:58     ` sashiko-bot
2026-12-02 10:10   ` [PATCH v15 26/35] crypto: amlogic: fix probe and algorithm registration error paths Jose Nieto
2026-12-02 10:10   ` [PATCH v15 27/35] crypto: amlogic: fix setup descriptor lengths and CBC IV reload Jose Nieto
2026-12-02 10:10   ` [PATCH v15 28/35] crypto: amlogic: drop redundant A1 platform data Jose Nieto
2026-12-02 10:10   ` [PATCH v15 29/35] arm64: dts: amlogic: use the system clock for A1/S4 crypto Jose Nieto
2026-12-02 10:10   ` [PATCH v15 30/35] crypto: amlogic: fix hasher request completion and DMA context Jose Nieto
2026-12-02 10:10   ` [PATCH v15 31/35] crypto: amlogic: clear algorithm pointers on unregister Jose Nieto
2026-12-02 11:04     ` sashiko-bot
2026-12-02 10:10   ` [PATCH v15 32/35] crypto: amlogic: minor cipher cleanups Jose Nieto
2026-12-02 10:10   ` [PATCH v15 33/35] crypto: amlogic: initialize flow completion before requesting IRQ Jose Nieto
2026-12-02 10:10   ` [PATCH v15 34/35] crypto: amlogic: keep the cipher key/IV buffer per request Jose Nieto
2026-12-02 10:10   ` [PATCH v15 35/35] crypto: amlogic: keep the bus clock enabled Jose Nieto

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-245519@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=clabbe@baylibre.com \
    --cc=conor+dt@kernel.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=jbrunet@baylibre.com \
    --cc=jnieto.alaber@gmail.com \
    --cc=khilman@baylibre.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