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 v14 12/30] crypto: amlogic - Avoid kzalloc in engine thread
Date: Fri, 27 Nov 2026 12:18:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-237345@kernel.org> (raw)
In-Reply-To: <20261127120103.1532882-13-jnieto.alaber@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] crypto: amlogic: shared TFM keyiv buffer races across
  concurrent requests
- [Medium] crypto: amlogic: keyiv DMA length mismatch in meson_cipher()
- [Low] crypto: amlogic: unused DMA reqsize padding in
  meson_cipher_init()
- [Low] crypto: amlogic: stale trailing key bytes on re-key in
  meson_aes_setkey()
--

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

crypto: amlogic - Avoid kzalloc in engine thread

This commit replaces the per-request dynamically allocated key and IV
buffer with a fixed keyiv buffer in struct meson_cipher_tfm_ctx and
switches the transform context to DMA-aligned crypto helpers.

> diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> index cb6b959bb1f96..df42940f1112a 100644
> --- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> +++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
[ ... ]
> @@ -147,17 +146,17 @@ static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
>  	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
>  	struct meson_alg_template *algt = container_of(alg,
>  		struct meson_alg_template, alg.skcipher.base);
> -	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
> +	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx_dma(tfm);
>  	struct meson_dev *mc = op->mc;
>  	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
>  	unsigned int blockmode = algt->blockmode;
>  	int i;
>  
>  	if (ctx->tloffset)
>  		return;
>  
>  	if (blockmode == DESC_OPMODE_CBC) {
> -		memcpy(ctx->bkeyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize);
> +		memcpy(op->keyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize);
>  		dma_sync_single_for_device(mc->dev, ctx->keyiv.addr,
>  					   ctx->keyiv.len, DMA_TO_DEVICE);
>  	}
[ ... ]
> @@ -320,21 +320,21 @@ static int meson_cipher(struct skcipher_request *areq)
>  	mc->chanlist[rctx->flow].stat_req++;
>  #endif
>  
> -	ctx.bkeyiv = kzalloc(48, GFP_KERNEL | GFP_DMA);
> -	if (!ctx.bkeyiv)
> -		return -ENOMEM;
> -
> -	memcpy(ctx.bkeyiv, op->key, op->keylen);
>  	ctx.keyiv.len = op->keylen;
>  	if (ctx.keyiv.len == AES_KEYSIZE_192)
>  		ctx.keyiv.len = AES_MAX_KEY_SIZE;
>  
> -	ctx.keyiv.addr = dma_map_single(mc->dev, ctx.bkeyiv, ctx.keyiv.len,
> +	if (algt->blockmode == DESC_OPMODE_CBC) {
> +		memcpy(op->keyiv + AES_MAX_KEY_SIZE, areq->iv, ivsize);
> +		ctx.keyiv.len = AES_MAX_KEY_SIZE + ivsize;
> +	}
> +
> +	ctx.keyiv.addr = dma_map_single(mc->dev, op->keyiv, ctx.keyiv.len,
>  					DMA_TO_DEVICE);
>  	err = dma_mapping_error(mc->dev, ctx.keyiv.addr);
>  	if (err) {
>  		dev_err(mc->dev, "Cannot DMA MAP KEY IV\n");
> -		goto free_keyiv;
> +		return err;
>  	}

[Severity: High]
Can two concurrent CBC requests sharing a crypto_skcipher transform race
on op->keyiv?

meson_skencrypt() and meson_skdecrypt() dispatch requests round-robin
across mc->flow_cnt independent crypto_engine worker threads via
meson_get_engine_number().  Because op is per-transform, two requests on
the same transform can run meson_cipher() in parallel on different engine
threads:

flow 0 kworker                    flow 1 kworker
meson_cipher(req0)
  memcpy(op->keyiv + 32,
         req0->iv, ivsize)
  dma_map_single(..., op->keyiv, ...)
                                  meson_cipher(req1)
                                    memcpy(op->keyiv + 32,
                                           req1->iv, ivsize)  <-- overwrites IV
                                    dma_map_single(..., op->keyiv, ...)
  meson_kick_hardware()
  dma_unmap_single(...)
                                    meson_kick_hardware()  <-- DMA on unmapped
                                                               or corrupted IV

Moving the mutable per-request DMA buffer into struct meson_cipher_req_ctx
(or per-channel storage) would avoid sharing it across concurrent requests.

[Severity: Medium]
Does ctx.keyiv.len match the descriptors programmed in
meson_setup_keyiv_descs()?

In ECB mode, ctx.keyiv.len remains 16 (AES-128) or 32 (AES-192/256), and
only ctx.keyiv.len bytes of op->keyiv are mapped with dma_map_single().
meson_setup_keyiv_descs() then unconditionally emits
mc->pdata->setup_desc_cnt (3 on GXL and AXG) descriptors at offsets 0, 16,
and 32, each transferring ctx->keyiv.len bytes:

drivers/crypto/amlogic/amlogic-gxl-cipher.c:meson_setup_keyiv_descs() {
    ...
	for (i = 0; i < mc->pdata->setup_desc_cnt; i++) {
		struct meson_desc *desc =
			&mc->chanlist[rctx->flow].tl[ctx->tloffset];
		int offset = i * 16;

		desc->t_src = cpu_to_le32(ctx->keyiv.addr + offset);
		desc->t_dst = cpu_to_le32(offset);
		desc->t_status = cpu_to_le32(DESC_OWN | DESC_MODE_KEY | ctx->keyiv.len);

		ctx->tloffset++;
	}
}

At this commit, descriptors at offsets 16 and 32 read past the DMA mapping
and past the 48-byte op->keyiv buffer.  Even with the later commit
"crypto: amlogic: fix setup descriptor lengths and CBC IV copy" in this
series changing the descriptor length to
ctx->keyiv.len / mc->pdata->setup_desc_cnt, ECB mode on GXL and AXG still
divides 16 or 32 by 3 (truncating each descriptor length to 5 or 10 bytes)
and accesses offsets up to 32 beyond a 16- or 32-byte DMA mapping.

[ ... ]

> @@ -425,42 +422,39 @@ static int meson_skencrypt(struct skcipher_request *areq)
>  
>  static int meson_cipher_init(struct crypto_tfm *tfm)
>  {
> -	struct meson_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
> +	struct meson_cipher_tfm_ctx *op = crypto_tfm_ctx_dma(tfm);
>  	struct meson_alg_template *algt;
>  	const char *name = crypto_tfm_alg_name(tfm);
>  	struct crypto_skcipher *sktfm = __crypto_skcipher_cast(tfm);
> @@ -443,7 +440,7 @@ static int meson_cipher_init(struct crypto_tfm *tfm)
>  		return PTR_ERR(op->fallback_tfm);
>  	}
>  
> -	crypto_skcipher_set_reqsize(sktfm, sizeof(struct meson_cipher_req_ctx) +
> +	crypto_skcipher_set_reqsize_dma(sktfm, sizeof(struct meson_cipher_req_ctx) +
>  				    crypto_skcipher_reqsize(op->fallback_tfm));
>  
>  	return 0;
>  }

[Severity: Low]
Why use crypto_skcipher_set_reqsize_dma() here when struct
meson_cipher_req_ctx holds no DMA buffers and every caller still retrieves
it via skcipher_request_ctx() rather than skcipher_request_ctx_dma()?

[ ... ]

>  static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
>  			    unsigned int keylen)
>  {
> -	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
> +	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx_dma(tfm);
>  	struct meson_dev *mc = op->mc;
>  
>  	switch (keylen) {
> @@ -477,11 +473,9 @@ static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
>  		dev_dbg(mc->dev, "ERROR: Invalid keylen %u\n", keylen);
>  		return -EINVAL;
>  	}
> -	kfree_sensitive(op->key);
> +
> +	memcpy(op->keyiv, key, keylen);
>  	op->keylen = keylen;
> -	op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
> -	if (!op->key)
> -		return -ENOMEM;
>  
>  	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
>  }

[Severity: Low]
If a transform is keyed with AES-256 and later re-keyed with AES-128 or
AES-192, does op->keyiv retain stale key bytes in
op->keyiv[keylen..AES_MAX_KEY_SIZE - 1]?

Previously kzalloc() zeroed the 48-byte buffer on each request.  Now
op->keyiv is only zeroed in meson_cipher_init(), so re-keying with a
shorter key leaves the old trailing bytes in place when meson_cipher()
maps AES_MAX_KEY_SIZE (for AES-192) or AES_MAX_KEY_SIZE + ivsize (for CBC).

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

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

  reply	other threads:[~2026-11-27 12:18 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 [this message]
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
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-237345@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