Skip to content

Commit b170a13

Browse files
committed
crypto: skcipher - Avoid infinite loop when cipher fails selftest
When an skcipher constructed through crypto_givcipher_default fails its selftest, we'll loop forever trying to construct new skcipher objects but failing because it already exists. The crux of the issue is that once a givcipher fails the selftest, we'll ignore it on the next run through crypto_skcipher_lookup and attempt to construct a new givcipher. We should instead return an error to the caller if we find a givcipher that has failed the test. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3f683d6 commit b170a13

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

crypto/ablkcipher.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,25 @@ static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
282282
alg->cra_ablkcipher.ivsize))
283283
return alg;
284284

285+
crypto_mod_put(alg);
286+
alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
287+
mask & ~CRYPTO_ALG_TESTED);
288+
if (IS_ERR(alg))
289+
return alg;
290+
291+
if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
292+
CRYPTO_ALG_TYPE_GIVCIPHER) {
293+
if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
294+
crypto_mod_put(alg);
295+
alg = ERR_PTR(-ENOENT);
296+
}
297+
return alg;
298+
}
299+
300+
BUG_ON(!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
301+
CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
302+
alg->cra_ablkcipher.ivsize));
303+
285304
return ERR_PTR(crypto_givcipher_default(alg, type, mask));
286305
}
287306

crypto/blkcipher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ static int crypto_grab_nivcipher(struct crypto_skcipher_spawn *spawn,
521521
int err;
522522

523523
type = crypto_skcipher_type(type);
524-
mask = crypto_skcipher_mask(mask) | CRYPTO_ALG_GENIV;
524+
mask = crypto_skcipher_mask(mask)| CRYPTO_ALG_GENIV;
525525

526526
alg = crypto_alg_mod_lookup(name, type, mask);
527527
if (IS_ERR(alg))

0 commit comments

Comments
 (0)