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>
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 v13 23/25] arm64: dts: amlogic: s4: add crypto node
Date: Thu, 26 Nov 2026 20:02:27 +0000	[thread overview]
Message-ID: <sashiko-outbox-236142@kernel.org> (raw)
In-Reply-To: <20261126193241.1471372-24-jnieto.alaber@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] crypto: amlogic: bogus 0-byte DMA descriptor in
  meson_hasher_do_one_request()
- [High] crypto: amlogic: unaligned ahash_request_ctx() used for DMA in
  hasher
- [Medium] arm64: dts: amlogic: s4: fake clk81 fixed-clock instead of
  clkc_periphs CLKID_SYS
- [Medium] crypto: amlogic: double completion on error in
  meson_hasher_do_one_request()
- [Medium] crypto: amlogic: partial_addr DMA mapping leak in
  meson_kick_hardware()
- [Medium] crypto: amlogic: meson_hasher_req_map() maps req->src on
  final() requests
--

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

arm64: dts: amlogic: s4: add crypto node

This commit adds a fixed-clock clk81 node and a crypto device node to the
Amlogic S4 SoC devicetree.

> diff --git a/arch/arm64/boot/dts/amlogic/meson-s4.dtsi b/arch/arm64/boot/dts/amlogic/meson-s4.dtsi
> index 2a6fbd5308362..de173f40e15ed 100644
> --- a/arch/arm64/boot/dts/amlogic/meson-s4.dtsi
> +++ b/arch/arm64/boot/dts/amlogic/meson-s4.dtsi
> @@ -71,6 +71,12 @@ xtal: xtal-clk {
>  		#clock-cells = <0>;
>  	};
>  
> +	clk81: clk81 {
> +		compatible = "fixed-clock";
> +		#clock-cells = <0>;
> +		clock-frequency = <166000000>;
> +	};

[Severity: Medium]
Should crypto@440400 use <&clkc_periphs CLKID_SYS> instead of adding a
separate root clk81 fixed-clock node?

meson-s4.dtsi already includes dt-bindings/clock/amlogic,s4-peripherals-clkc.h
and defines clkc_periphs: clock-controller@0, whose driver
(drivers/clk/meson/s4-peripherals.c) registers the S4 system bus clock
(s4_sys_clk) at CLKID_SYS.  Adding a root fixed-clock duplicates that
internal SoC clock with a hardcoded 166 MHz rate instead of deriving it
from the hardware mux and divider registers, and the node name clk81 does
not follow the Devicetree generic node naming convention.

> @@ -621,6 +627,13 @@ mux {
>  
>  			};
>  
> +			crypto: crypto@440400 {
> +				compatible = "amlogic,s4-crypto", "amlogic,a1-crypto";
> +				reg = <0x0 0x440400 0x0 0x48>;
> +				interrupts = <GIC_SPI 24 IRQ_TYPE_EDGE_RISING>;
> +				clocks = <&clk81>;
> +			};

[Severity: High]
In the Amlogic SHA hasher driver enabled by this node (introduced earlier
in this series in commit "crypto: amlogic - Introduce hasher" and commit
"crypto: amlogic: hasher: add one-shot digest path"), can
meson_hasher_do_one_request() emit a bogus 0-byte DMA descriptor with
t_src = 0 when an update() request ends with a partial block?

drivers/crypto/amlogic/amlogic-gxl-hasher.c:meson_hasher_do_one_request() {
    ...
		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;
    ...
		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 an update() call passes 74 bytes, the first iteration queues a 64-byte
descriptor in flow->tl[0] (setting ctx.tloffset = 1 and ctx.nbytes = 10),
and the second iteration copies the remaining 10 bytes into rctx->partial,
leaving ctx.nbytes = 0, ctx.todo = 0, and ctx.src_addr = 0.  The same state
occurs on an update() smaller than 64 bytes after an earlier update()
already ran, because meson_setup_state_descs() advances ctx.tloffset before
the sub-block data is copied into rctx->partial.

Because ctx.todo is 0, ctx.nbytes is 0, rctx->final_req is false, and
ctx.tloffset is non-zero, none of the three continue checks match.
meson_setup_data_descs() then appends a 0-byte SHA descriptor with
t_src = 0 and DESC_LAST set, and meson_kick_hardware() starts DMA from
physical address 0, corrupting the hash state or timing out.

[Severity: Medium]
Does meson_hasher_do_one_request() complete req twice when an error occurs?

drivers/crypto/amlogic/amlogic-gxl-hasher.c:meson_hasher_do_one_request() {
    ...
	local_bh_disable();
	crypto_finalize_hash_request(engine, req, ret);
	local_bh_enable();

	return ret;
}

When ret is negative (such as on a DMA mapping failure or a DMA timeout in
meson_kick_hardware()), crypto_finalize_hash_request() calls
crypto_request_complete(&req->base, ret).  Returning that negative ret to
crypto_pump_requests() tells crypto_engine that do_one_request() failed
without finalizing, so crypto_pump_requests() calls
crypto_request_complete() on the same request a second time and can trigger
a use-after-free.  meson_handle_cipher_request() in amlogic-gxl-cipher.c
returns 0 after finalizing the request.

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

drivers/crypto/amlogic/amlogic-gxl-hasher.c:meson_kick_hardware() {
    ...
	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);
    ...
}

When meson_fill_partial_buffer() maps rctx->partial and sets
rctx->partial_mapped = true, an early return on timeout skips the
dma_unmap_single() block above, and the fail unwind path in
meson_hasher_do_one_request() only unmaps req->src and ctx.state_addr.

[Severity: High]
Can using ahash_request_ctx(req) instead of ahash_request_ctx_dma(req) in
amlogic-gxl-hasher.c cause DMA cacheline corruption on non-coherent arm64
platforms?

drivers/crypto/amlogic/amlogic-gxl-hasher.c:meson_hasher_init() {
    ...
	crypto_ahash_set_reqsize_dma(atfm, crypto_ahash_statesize(atfm));
    ...
}

meson_hasher_init() calls crypto_ahash_set_reqsize_dma(), which reserves
padding for ahash_request_ctx_dma(req), but meson_hasher_do_one_request()
and the other callbacks in amlogic-gxl-hasher.c retrieve rctx with
ahash_request_ctx(req).  ahash_request_ctx(req) is only aligned to
crypto_tfm_ctx_alignment() (8 bytes) at offset 64 of struct ahash_request,
not ARCH_DMA_MINALIGN (128 bytes on arm64), so rctx->state (mapped
DMA_BIDIRECTIONAL) shares a 128-byte cacheline with struct ahash_request.

[Severity: Medium]
Can meson_hasher_req_map() and meson_hasher_req_unmap() access an unused or
dangling req->src scatterlist during crypto_ahash_final()?

drivers/crypto/amlogic/amlogic-gxl-hasher.c:meson_hasher_req_map() {
    ...
	if (!req->nbytes)
		return 0;

	ret = dma_map_sg(mc->dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
    ...
}

meson_hasher_do_one_request() sets ctx.nbytes to 0 when
rctx->final_req && !rctx->digest_req, because crypto_ahash_final() only
writes the digest to req->result and ignores req->src and req->nbytes.
However, meson_hasher_req_map() and meson_hasher_req_unmap() test
req->nbytes directly instead of ctx.nbytes, so a final() call where
req->nbytes was left non-zero from an earlier update() (including
crypto_ahash_finup() via ahash_def_finup()) will DMA-map req->src again, or
dereference a NULL or freed req->src in sg_nents(req->src).

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

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

  reply	other threads:[~2026-11-26 20:02 UTC|newest]

Thread overview: 39+ 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 [this message]
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

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-236142@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