]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
crypto: ahash - Enforce MAX_SYNC_HASH_REQSIZE for sync ahash
authorHerbert Xu <herbert@gondor.apana.org.au>
Sun, 4 May 2025 13:33:18 +0000 (21:33 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Mon, 5 May 2025 10:20:46 +0000 (18:20 +0800)
As sync ahash algorithms (currently there are none) are used without
a fallback, ensure that they obey the MAX_SYNC_HASH_REQSIZE rule
just like shash algorithms.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/ahash.c

index 57c131a13067fc79dea2695963fadbc10de27553..736e9fb5d0a49ee10ce6f733d367786682499a22 100644 (file)
@@ -760,23 +760,28 @@ static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
 
        tfm->exit = crypto_ahash_exit_tfm;
 
-       if (!alg->init_tfm) {
-               if (!tfm->__crt_alg->cra_init)
-                       return 0;
-
+       if (alg->init_tfm)
+               err = alg->init_tfm(hash);
+       else if (tfm->__crt_alg->cra_init)
                err = tfm->__crt_alg->cra_init(tfm);
-               if (err)
-                       goto out_free_sync_hash;
-
+       else
                return 0;
-       }
 
-       err = alg->init_tfm(hash);
        if (err)
                goto out_free_sync_hash;
 
+       if (!ahash_is_async(hash) && crypto_ahash_reqsize(hash) >
+                                    MAX_SYNC_HASH_REQSIZE)
+               goto out_exit_tfm;
+
        return 0;
 
+out_exit_tfm:
+       if (alg->exit_tfm)
+               alg->exit_tfm(hash);
+       else if (tfm->__crt_alg->cra_exit)
+               tfm->__crt_alg->cra_exit(tfm);
+       err = -EINVAL;
 out_free_sync_hash:
        crypto_free_ahash(fb);
        return err;
@@ -954,6 +959,10 @@ static int ahash_prepare_alg(struct ahash_alg *alg)
        if (base->cra_reqsize && base->cra_reqsize < alg->halg.statesize)
                return -EINVAL;
 
+       if (!(base->cra_flags & CRYPTO_ALG_ASYNC) &&
+           base->cra_reqsize > MAX_SYNC_HASH_REQSIZE)
+               return -EINVAL;
+
        err = hash_prepare_alg(&alg->halg);
        if (err)
                return err;