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 12/25] crypto: amlogic - Avoid kzalloc in engine thread
Date: Thu, 26 Nov 2026 19:48:47 +0000	[thread overview]
Message-ID: <sashiko-outbox-236121@kernel.org> (raw)
In-Reply-To: <20261126193241.1471372-13-jnieto.alaber@gmail.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] crypto: amlogic: shared TFM keyiv buffer races across
  concurrent requests
- [Medium] crypto: amlogic: unsynced DMA write and duplicate IV copy in
  meson_cipher()
- [Medium] crypto: amlogic: out-of-bounds keyiv DMA descriptors 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 228a0fe250c8671b89954b4f55b91bfb1b59975f
Author: Alexey Romanov <romanov.alexey2000@gmail.com>

crypto: amlogic - Avoid kzalloc in engine thread

This commit replaces the per-request kzalloc() key/IV buffer in meson_cipher()
and the dynamically allocated key in struct meson_cipher_tfm_ctx with a
fixed-size keyiv array in the transform context, and switches transform and
request context accessors to their DMA-aligned variants.

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

[Severity: Medium]
Does meson_setup_keyiv_descs() modify the DMA-mapped op->keyiv buffer
without calling dma_sync_single_for_cpu() first, while also duplicating the
initial IV copy and DMA sync already performed in meson_cipher()?

In meson_cipher(), CBC requests copy areq->iv into op->keyiv and call
dma_map_single(..., DMA_TO_DEVICE), transferring buffer ownership to the
device before entering the loop with ctx.tloffset == 0.  On the first loop
iteration, meson_setup_keyiv_descs() immediately repeats that memcpy() and
flushes the cache a second time via dma_sync_single_for_device(), and on
every batch it writes to the mapped buffer from the CPU without a preceding
dma_sync_single_for_cpu() call.

[ ... ]
> @@ -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);

[Severity: Medium]
Does setting ctx.keyiv.len to 48 in CBC mode cause meson_setup_keyiv_descs()
to program key/IV DMA descriptors that read past the end of op->keyiv?

meson_setup_keyiv_descs() loops mc->pdata->setup_desc_cnt (3) times at
16-byte offsets (0, 16, 32) and sets each descriptor's length in t_status to
ctx->keyiv.len:

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

With ctx.keyiv.len increased to 48 for CBC (and 16 or 32 for ECB, where
dma_map_single() only maps ctx.keyiv.len bytes), the descriptors at offsets
16 and 32 instruct the DMA engine to read up to 32 bytes past the end of the
48-byte op->keyiv buffer and its DMA mapping.

[ ... ]
> @@ -425,40 +422,37 @@ static int meson_skencrypt(struct skcipher_request *areq)
>  
>  static int meson_cipher_init(struct crypto_tfm *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]
Is crypto_skcipher_set_reqsize_dma() needed here when struct
meson_cipher_req_ctx holds no DMA buffers and all callers still access it
via skcipher_request_ctx()?  Unless the per-request DMA buffer is moved into
struct meson_cipher_req_ctx and accessed with skcipher_request_ctx_dma(),
this adds unused DMA alignment padding to every request allocation.

>  static void meson_cipher_exit(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);
>  
> -	kfree_sensitive(op->key);
>  	crypto_free_skcipher(op->fallback_tfm);
>  }
>  
>  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;

[Severity: Low]
Can re-keying an existing transform with a shorter key leave stale key bytes
in op->keyiv?

Previously, meson_cipher() allocated a zeroed 48-byte buffer on each request.
Because op->keyiv is now only zeroed once in meson_cipher_init(), re-keying a
transform from AES-256 to AES-192 or AES-128 leaves the old key's trailing
bytes in op->keyiv[keylen..AES_MAX_KEY_SIZE - 1], which are then DMA-mapped
and sent to the hardware when ctx.keyiv.len is padded to AES_MAX_KEY_SIZE.

> diff --git a/drivers/crypto/amlogic/amlogic-gxl.h b/drivers/crypto/amlogic/amlogic-gxl.h
> index 6797b44905d3e..c401cbbadaa34 100644
> --- a/drivers/crypto/amlogic/amlogic-gxl.h
> +++ b/drivers/crypto/amlogic/amlogic-gxl.h
> @@ -125,14 +125,14 @@ struct meson_cipher_req_ctx {
>  
>  /*
>   * struct meson_cipher_tfm_ctx - context for a skcipher TFM
> - * @key:		pointer to key data
> + * @keyiv:		key data
>   * @keylen:		len of the key
>   * @keymode:		The keymode(type and size of key) associated with this TFM
>   * @mc:			pointer to the private data of driver handling this TFM
>   * @fallback_tfm:	pointer to the fallback TFM
>   */
>  struct meson_cipher_tfm_ctx {
> -	u32 *key;
> +	u8 keyiv[AES_MAX_KEY_SIZE + AES_BLOCK_SIZE];
>  	u32 keylen;
>  	u32 keymode;
>  	struct meson_dev *mc;

[Severity: High]
Can moving the mutable key/IV DMA buffer into struct meson_cipher_tfm_ctx
cause a data race and CBC data corruption when two requests share a transform?

A single struct crypto_skcipher transform is shared across concurrent
requests, and meson_skencrypt() / meson_skdecrypt() dispatch requests
round-robin across mc->flow_cnt (2) crypto_engine worker threads via
meson_get_engine_number():

flow 0 kworker                    flow 1 kworker
meson_cipher(req1)
  memcpy(op->keyiv + 32,
         req1->iv, 16)
  dma_map_single(..., op->keyiv,
                 ...)
                                  meson_cipher(req2)
                                    memcpy(op->keyiv + 32,
                                           req2->iv, 16)  <-- overwrites IV
                                    dma_map_single(..., op->keyiv, ...)
  meson_kick_hardware()
    meson_dma_start()             <-- DMA reads req2's IV for req1
  dma_unmap_single(...)           <-- unmaps while flow 1 DMA is active

In addition, op->keyiv is not padded to a cacheline boundary inside struct
meson_cipher_tfm_ctx, so it shares a cacheline with keylen, keymode, mc, and
fallback_tfm during streaming DMA mappings.  Should the per-request key/IV
DMA buffer live in struct meson_cipher_req_ctx (accessed via
skcipher_request_ctx_dma()) instead of the shared transform context?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126193241.1471372-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-26 19:48 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 [this message]
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

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