All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alex Ousherovitch" <aousherovitch@rambus.com>
Cc: conor+dt@kernel.org, robh@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v4 04/19] crypto: cmh - add SHA-2/SHA-3/SHAKE ahash
Date: Tue, 25 Aug 2026 22:27:09 +0000	[thread overview]
Message-ID: <20260825222710.256441F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260825221539.255951-5-aousherovitch@rambus.com>

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] DMA mapping the inline `rctx->checkpoint` buffer violates DMA API rules and causes memory corruption due to cache-line sharing or stack mapping.
- [High] The driver linearizes arbitrary-length hash updates into a single kmalloc buffer, causing valid large updates to predictably fail with -ENOMEM.
- [High] Misusing the crypto API `cra_blocksize` for hardware alignment breaks SHAKE holdback logic, leading to hardware state corruption and potential buffer overflows.
- [High] Accessing `req->src` and `rctx` after submitting an asynchronous hardware request causes a Use-After-Free.
- [Medium] The commit message claims zero-copy support for virtual buffers, but the implementation unconditionally copies the data.
--

commit 100670a57b48ff51f5668c01fd9ada227bac5247
Author: Alex Ousherovitch <aousherovitch@rambus.com>

crypto: cmh - add SHA-2/SHA-3/SHAKE ahash

This commit registers various ahash algorithms (SHA-2, SHA-3, SHAKE) using
the CMH hash core. It implements the necessary operations for incremental
updates, request cloning, and handles hardware submissions.

>     Supports incremental update/finup/final, init/export/import for
>     request cloning, and the CRYPTO_AHASH_REQ_VIRT flag for zero-copy
>     from virtual buffers.

[Severity: Medium]
Does the driver actually perform zero-copy DMA for virtual buffers?

Looking at cmh_hash_update() and cmh_hash_finup(), the implementation
appears to unconditionally allocate a bounce buffer with kmalloc() and
then executes a memcpy() from req->svirt into the bounce buffer. Is there
a code path that avoids the allocation and directly maps the virtual buffer
to DMA, or should the commit message be updated to reflect the copy?

> diff --git a/drivers/crypto/cmh/cmh_hash.c b/drivers/crypto/cmh/cmh_hash.c
> new file mode 100644
> index 0000000000000..72d09c5b0c91f
> --- /dev/null
> +++ b/drivers/crypto/cmh/cmh_hash.c
[ ... ]
> +	{
> +		.hc_algo     = HC_ALGO_SHAKE128,
> +		.digest_size = CMH_SHAKE128_DIGEST_SIZE,
> +		.block_size  = 1,    /* XOF: no meaningful block for crypto API */
> +		.alg_name    = "shake128",
> +		.drv_name    = "rambus-cmh-shake128",
> +	},
[ ... ]
> +static int cmh_hash_update(struct ahash_request *req)
> +{
[ ... ]
> +	/* Not enough for a full block -- just buffer */
> +	if (total_avail < block_size) {
> +		if (req->base.flags & CRYPTO_AHASH_REQ_VIRT)
> +			memcpy(rctx->buf + rctx->buf_len,
> +			       req->svirt, req->nbytes);

[Severity: High]
Does this logic bypass the software holdback for SHAKE algorithms?

Because block_size is explicitly defined as 1 to indicate byte-oriented
operation for the crypto API, the condition total_avail < block_size is
practically never met (only when total_avail is 0).

The hardware Keccak cores require intermediate updates to be multiples of
the algorithm's block rate (e.g., 168 bytes for SHAKE-128) to correctly
save the state. Submitting unaligned arbitrary byte fragments might
corrupt the hardware state. Additionally, CMH_HASH_MAX_BLOCK is 144, which
would not be able to hold back a true SHAKE-128 block of 168 bytes without
overflowing the array.

[ ... ]
> +	/*
> +	 * Linearise: holdback prefix + full blocks from scatterlist.
> +	 * full_len is user-controlled and can exceed KMALLOC_MAX_SIZE for a
> +	 * single large update; __GFP_NOWARN avoids splatting the page
> +	 * allocator on an oversized request.
> +	 */
> +	rctx->data_buf = kmalloc(full_len, gfp | __GFP_NOWARN);
> +	if (!rctx->data_buf)
> +		return -ENOMEM;

[Severity: High]
Will legitimate large hash requests deterministically fail with -ENOMEM
here?

The ahash API does not enforce a maximum size on req->nbytes, and clients
like IPsec, IMA, or AF_ALG can submit very large scatterlists. Because
kmalloc() is strictly limited to KMALLOC_MAX_SIZE and can fail earlier due
to fragmentation, dynamically allocating a contiguous buffer for an
arbitrary-length user-provided req->nbytes causes large updates to predictably
fail with -ENOMEM.

[ ... ]
> +	rctx->ckpt_dma = DMA_MAPPING_ERROR;
> +	if (rctx->has_checkpoint) {
> +		rctx->ckpt_dma = cmh_dma_map_single(rctx->checkpoint,
> +						    HC_CONTEXT_SIZE,
> +						     DMA_TO_DEVICE);

[Severity: High]
Does DMA mapping the inline rctx->checkpoint violate the DMA API rules?

The checkpoint array is embedded inline in struct cmh_hash_reqctx without
being aligned to the cacheline. If the request context is allocated on the
stack (e.g., via AHASH_REQUEST_ON_STACK), this maps stack memory for DMA,
which is prohibited by the DMA API and may trigger a kernel panic.

Even if heap-allocated, the unaligned array shares a cache line with
adjacent members like rctx->packed. Modifying these adjacent fields while
DMA is active could corrupt the checkpoint data (or vice versa) on
non-coherent architectures. This same mapping also happens in
cmh_hash_submit_final().

[ ... ]
> +	ret = cmh_vcq_pack_and_submit_async(cmds, idx, rctx->packed,
> +					    CMH_HASH_MAX_PACKED,
> +					    d.mbx_idx,
> +					    cmh_hash_update_complete, req,
> +					    !!(req->base.flags &
> +					       CRYPTO_TFM_REQ_MAY_BACKLOG),
> +					    cmh_tm_async_timeout_jiffies());
> +	if (ret && ret != -EBUSY)
> +		goto err_unmap_ckpt;
> +
> +	/*
> +	 * Submit accepted (in flight or backlogged) -- only now move the tail
> +	 * into the holdback.  A synchronous rejection above (e.g. -EAGAIN)
> +	 * leaves rctx->buf/buf_len untouched so the caller can retry the
> +	 * identical update.
> +	 */
> +	if (tail_len > 0) {
> +		if (req->base.flags & CRYPTO_AHASH_REQ_VIRT)
> +			memcpy(rctx->buf, req->svirt + from_src, tail_len);
> +		else
> +			scatterwalk_map_and_copy(rctx->buf, req->src,
> +						 from_src, tail_len, 0);
> +	}
> +	rctx->buf_len = tail_len;

[Severity: High]
Can accessing req->src and rctx after submitting an asynchronous hardware
request result in a use-after-free?

Once cmh_vcq_pack_and_submit_async() submits the request to the hardware,
the completion callback cmh_hash_update_complete() could run immediately
on another CPU and signal completion to the client. The client is then
free to release the ahash_request (which contains rctx) and the source
scatterlist (req->src).

If the original thread continues execution and attempts to copy the tail
data into rctx->buf using req->src, it will access freed memory.

[ ... ]
> +static int cmh_hash_finup(struct ahash_request *req)
> +{
[ ... ]
> +	gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
> +	      GFP_KERNEL : GFP_ATOMIC;
> +
> +	data_buf = kmalloc(data_len, gfp | __GFP_NOWARN);
> +	if (!data_buf)
> +		return -ENOMEM;

[Severity: High]
Similar to the allocation in cmh_hash_update(), could this unbounded
contiguous allocation fail deterministically for valid large requests?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260825221539.255951-1-aousherovitch@rambus.com?part=4

  reply	other threads:[~2026-08-25 22:27 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-25 22:15 [PATCH v4 00/19] crypto: cmh - add Rambus CryptoManager Hub driver Alex Ousherovitch
2026-08-25 22:15 ` Alex Ousherovitch
2026-08-25 22:15 ` [PATCH v4 01/19] dt-bindings: crypto: add Rambus CryptoManager Hub Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:28   ` sashiko-bot
2026-08-26 17:02   ` Conor Dooley
2026-08-26 17:02     ` Conor Dooley
2026-08-27  1:39     ` Ousherovitch, Alex
2026-08-27  1:39       ` Ousherovitch, Alex
2026-08-27 17:14       ` Conor Dooley
2026-08-27 17:14         ` Conor Dooley
2026-08-27 18:21         ` Ousherovitch, Alex
2026-08-27 18:21           ` Ousherovitch, Alex
2026-08-25 22:15 ` [PATCH v4 02/19] crypto: cmh - add core platform driver Alex Ousherovitch
2026-08-25 22:34   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 03/19] crypto: cmh - add key provisioning and management Alex Ousherovitch
2026-08-25 22:30   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 04/19] crypto: cmh - add SHA-2/SHA-3/SHAKE ahash Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:27   ` sashiko-bot [this message]
2026-09-11  4:31   ` Herbert Xu
2026-09-11  4:31     ` Herbert Xu
2026-09-11 21:29     ` Ousherovitch, Alex
2026-09-11 21:29       ` Ousherovitch, Alex
2026-08-25 22:15 ` [PATCH v4 05/19] crypto: cmh - add HMAC ahash Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:30   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 06/19] crypto: cmh - add CSHAKE/KMAC ahash Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:30   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 07/19] crypto: cmh - add SM3 ahash Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:28   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 08/19] crypto: cmh - add AES skcipher/aead/cmac Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:28   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 09/19] crypto: cmh - add SM4 skcipher/aead/cmac/xcbc Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:25   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 10/19] crypto: cmh - add ChaCha20-Poly1305 Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:30   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 11/19] crypto: cmh - add DRBG hwrng Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:32   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 12/19] crypto: cmh - add RSA akcipher Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:15 ` [PATCH v4 13/19] crypto: cmh - add ECDSA/SM2 sig Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:15 ` [PATCH v4 14/19] crypto: cmh - add ECDH/X25519 kpp Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:41   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 15/19] crypto: cmh - add ML-KEM/ML-DSA (QSE) Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:36   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 16/19] crypto: cmh - add SLH-DSA/LMS/XMSS (HCQ) Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:37   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 17/19] Documentation: ioctl: add CMH ioctl documentation and register 'J' Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:34   ` sashiko-bot
2026-08-25 22:15 ` [PATCH v4 18/19] selftests: crypto: cmh - add kselftest for management ioctl Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch
2026-08-25 22:15 ` [PATCH v4 19/19] MAINTAINERS: add Rambus CryptoManager Hub (CMH) Alex Ousherovitch
2026-08-25 22:15   ` Alex Ousherovitch

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=20260825222710.256441F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aousherovitch@rambus.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.