Re: [PATCH 23/30] crypto: sa2ul - fix stack overflow in sa_prepare_iopads

From: T Pratham

Date: Thu Sep 17 2026 - 06:18:19 EST


On 9/15/26 15:25, Manorit Chawdhry wrote:
[...]
>
> -static int sa_export_shash(void *state, struct shash_desc *hash,
> - int digest_size, __be32 *out)
> +static int sa_export_shash(struct shash_desc *hash, int digest_size,
> + __be32 *out)
> {
> - struct sha1_state *sha1;
> - struct sha256_state *sha256;
> u32 *result;
> int ret = 0;
> + int state_size;
> + u8 *sha;
> +
> + state_size = crypto_shash_statesize(hash->tfm);
> + if (state_size <= 0) {
> + dev_err(sa_k3_dev, "%s: invalid state_size=%d\n", __func__,
> + state_size);
> + return -EINVAL;
> + }
> +
> + sha = kmalloc(state_size, GFP_KERNEL);
> + if (!sha)
> + return -ENOMEM;
>
> /* Export the intermediate digest to program into SA2UL */
> - ret = crypto_shash_export(hash, state);
> + ret = crypto_shash_export(hash, sha);
> if (ret) {
> dev_err(sa_k3_dev, "%s: crypto_shash_export failed\n",
> __func__);
> + kfree_sensitive(sha);
> return ret;
> }
>
> switch (digest_size) {
> case SHA1_DIGEST_SIZE:
> - sha1 = state;
> - result = sha1->state;
> + result = (u32 *)sha;
> break;
> case SHA256_DIGEST_SIZE:
> - sha256 = state;
> - result = sha256->state;
> + result = (u32 *)sha;
> break;
> default:
> dev_err(sa_k3_dev, "%s: bad digest_size=%d\n", __func__,
> digest_size);
> + kfree_sensitive(sha);
> return -EINVAL;
> }
>
All the arms in this switch-case are now doing the exact same thing. If
digest_size is guaranteed to be supplied correct in this function, this
can be removed.

--
Regards
T Pratham <t-pratham@xxxxxx>