Linux cryptographic layer development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "T Pratham" <t-pratham@ti.com>,
	"Herbert Xu" <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>
Cc: "Sebin Francis" <sebin.francis@ti.com>,
	"Manorit Chawdhry" <m-chawdhry@ti.com>,
	"Vishal Mahaveer" <vishalm@ti.com>,
	"Praneeth Bajjuri" <praneeth@ti.com>,
	linux-crypto@vger.kernel.org
Subject: Re: [PATCH v11 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver
Date: Fri, 04 Dec 2026 17:21:24 +0000	[thread overview]
Message-ID: <sashiko-outbox-250299@kernel.org> (raw)
In-Reply-To: <20261204164236.489112-2-t-pratham@ti.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] crypto: ti: re-registering hash_algs inflates statesize and
  cra_reqsize
- [High] crypto: ti: unchecked 32-bit digestcnt overflow corrupts >= 4
  GiB hashes
- [Medium] crypto: ti - high-order kzalloc_objs() and double-zeroing in
  dthe_hash_run()
- [Medium] crypto: ti: 10 ms busy-poll with sleep_us=0 in SCHED_FIFO
  engine kthreads
- [Low] crypto: ti: dthev2: spurious CRYPTO_ALG_OPTIONAL_KEY on unkeyed
  SHA-2 algs
--

commit 114a10ac679742bd6b521e9f3aec91e9406695f5
Author: T Pratham <t-pratham@ti.com>

crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver

This commit adds SHA224, SHA256, SHA384, and SHA512 hashing engine support
to the TI DTHEv2 crypto driver, splitting the crypto engine into separate
AES and hash engine instances.

> diff --git a/drivers/crypto/ti/dthev2-hash.c b/drivers/crypto/ti/dthev2-hash.c
> new file mode 100644
> index 0000000000000..bc5931de55378
> --- /dev/null
> +++ b/drivers/crypto/ti/dthev2-hash.c
> @@ -0,0 +1,780 @@

[ ... ]

> +	/* Poll output ready status register */
> +	if (!ret)
> +		ret = readl_relaxed_poll_timeout(sha_base_reg + DTHE_P_HASH_IRQSTATUS, _status,
> +						 (_status & DTHE_HASH_IRQSTATUS_OP_READY),
> +						 0, DTHE_POLL_TIMEOUT_INTERVAL);

[Severity: Medium]
Should this poll pass a non-zero sleep_us instead of busy-waiting for up to
10 ms inside a SCHED_FIFO kthread?

Both hash_engine and aes_engine are created with crypto_engine_alloc_init(dev,
1), so dthe_hash_run_dma() and dthe_aead_read_tag() run at SCHED_FIFO
priority.  With DTHE_POLL_TIMEOUT_INTERVAL set to 10 * USEC_PER_MSEC and
sleep_us = 0, a slow or stalled operation (or an AEAD DMA timeout, where
dthe_aead_run() still calls dthe_aead_read_tag()) spins on the CPU for up to
10 ms, starving normal tasks, workqueues, and ksoftirqd.

Since readl_relaxed_poll_timeout() reads the register before sleeping, a
small non-zero sleep_us such as 10 us avoids extra latency when the status
bit is already set while preventing multi-millisecond real-time spins.

[ ... ]

> +	rctx->digestcnt = readl_relaxed(sha_base_reg + DTHE_P_HASH512_DIGEST_COUNT);
> +	dthe_hash_flag_set(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL);

[Severity: High]
Can streaming 4 GiB or more silently overflow rctx->digestcnt and return a
corrupted digest?

DTHE_P_HASH512_DIGEST_COUNT and rctx->digestcnt are 32-bit byte counters,
which dthe_hash_import() already enforces with a U32_MAX check.  Neither
dthe_hash_run() nor dthe_hash_run_dma() checks whether
(u64)rctx->digestcnt + len_to_process exceeds U32_MAX, so a streamed message
of 4 GiB or larger wraps the hardware counter modulo 2^32 and finalizes with
the wrong length padding while returning 0.

If a state exported at an exact 4 GiB boundary wraps to 0, dthe_hash_import()
also takes the early return for bytecnt == 0 and silently resets the hash to
its initial state without restoring rctx->idigest.

[ ... ]

> +	src = kzalloc_objs(*src, src_nents, GFP_KERNEL);
> +	if (!src) {
> +		ret = -ENOMEM;
> +		goto hash_buf_all;
> +	}
> +
> +	sg_init_table(src, src_nents);

[Severity: Medium]
Can large multi-page scatterlists trigger costly high-order GFP_KERNEL
allocations here, and does sg_init_table() redundantly zero the array a
second time?

For non-contiguous requests larger than 4 MB (over 1024 4 KB pages),
kzalloc_objs() requests a high-order contiguous allocation inside the
SCHED_FIFO hash_engine kthread, which can stall all queued hash requests in
direct reclaim/compaction and fail with -ENOMEM under fragmentation since
dthe_hash_run() has no software fallback.  Switching to sg_alloc_table() and
sg_free_table() avoids both the contiguous allocation and the double-zeroing,
as dthe_copy_sg() and k3-udma already walk chained scatterlists with
sg_next() and for_each_sg().

[ ... ]

> +				.cra_flags	 = CRYPTO_ALG_TYPE_AHASH |
> +						   CRYPTO_ALG_ASYNC |
> +						   CRYPTO_ALG_OPTIONAL_KEY |
> +						   CRYPTO_ALG_KERN_DRIVER_ONLY |
> +						   CRYPTO_ALG_ALLOCATES_MEMORY |
> +						   CRYPTO_AHASH_ALG_BLOCK_ONLY,

[Severity: Low]
Why is CRYPTO_ALG_OPTIONAL_KEY set on the four unkeyed SHA-2 entries in
hash_algs[] when none of them define a .setkey callback?

CRYPTO_ALG_OPTIONAL_KEY is only for algorithms that provide a ->setkey()
method with a default key; setting it here exposes a bogus flag to userspace
via crypto_report_one() and would fail registration if ahash_prepare_alg()
adds a consistency check.

[ ... ]

> +int dthe_register_hash_algs(void)
> +{
> +	return crypto_engine_register_ahashes(hash_algs, ARRAY_SIZE(hash_algs));
> +}
> +
> +void dthe_unregister_hash_algs(void)
> +{
> +	crypto_engine_unregister_ahashes(hash_algs, ARRAY_SIZE(hash_algs));
> +}

[Severity: High]
Does re-registering the static hash_algs[] array inflate statesize and
cra_reqsize on every unbind/rebind?

Because hash_algs[] sets CRYPTO_AHASH_ALG_BLOCK_ONLY, ahash_prepare_alg()
adds cra_blocksize + 1 to both fields in place on every registration, and
crypto_unregister_ahash() does not undo it:

crypto/ahash.c:ahash_prepare_alg() {
	...
	if (base->cra_flags & CRYPTO_AHASH_ALG_BLOCK_ONLY) {
		...
		base->cra_reqsize += base->cra_blocksize + 1;
		alg->halg.statesize += base->cra_blocksize + 1;
	...
}

After a single sysfs unbind/rebind (or probe retry after
dthe_register_aes_algs() fails), sha512-dthev2 statesize grows from 209 to
338, so crypto_ahash_export() copies the partial block to offsets 209..337
while dthe_hash_export() only writes offsets 0..79.  That leaves 129 bytes
of uninitialized stack memory in ahash_do_req_chain()'s state buffer to be
imported and hashed into the fallback digest, and a third rebind pushes
statesize past HASH_MAX_STATESIZE (345) and breaks ahash_do_req_chain() with
-ENOSYS.

Probing a second DTHEv2 instance while one is bound also calls
dthe_register_algs() unconditionally, which mutates the live hash_algs[] and
runs INIT_LIST_HEAD(&alg->cra_users) in __crypto_register_alg() on the
already-registered algorithm before returning -EEXIST.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204164236.489112-1-t-pratham@ti.com?part=1

  reply	other threads:[~2026-12-04 17:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:42 [PATCH v11 0/2] Add support for hashing algorithms in TI DTHE V2 T Pratham
2026-12-04 16:42 ` [PATCH v11 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver T Pratham
2026-12-04 17:21   ` sashiko-bot [this message]
2026-12-04 16:42 ` [PATCH v11 2/2] crypto: ti - Add support for HMAC " T Pratham
2026-12-04 17:47   ` 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-250299@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=m-chawdhry@ti.com \
    --cc=praneeth@ti.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sebin.francis@ti.com \
    --cc=t-pratham@ti.com \
    --cc=vishalm@ti.com \
    /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