crypto: api - check for ERR pointers in crypto_destroy_tfm()
authorArd Biesheuvel <ardb@kernel.org>
Tue, 2 Mar 2021 20:33:03 +0000 (21:33 +0100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 12 Mar 2021 13:04:01 +0000 (00:04 +1100)
Given that crypto_alloc_tfm() may return ERR pointers, and to avoid
crashes on obscure error paths where such pointers are presented to
crypto_destroy_tfm() (such as [0]), add an ERR_PTR check there
before dereferencing the second argument as a struct crypto_tfm
pointer.

[0] https://lore.kernel.org/linux-crypto/000000000000de949705bc59e0f6@google.com/

Reported-by: syzbot+12cf5fbfdeba210a89dd@syzkaller.appspotmail.com
Reviewed-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/api.c
include/crypto/acompress.h
include/crypto/aead.h
include/crypto/akcipher.h
include/crypto/hash.h
include/crypto/kpp.h
include/crypto/rng.h
include/crypto/skcipher.h

index ed08cbd5b9d3f41e70509bd1c00d346cd5d55c86..c4eda56cff8917e08c2de3b0ada13df291c785ab 100644 (file)
@@ -562,7 +562,7 @@ void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
 {
        struct crypto_alg *alg;
 
-       if (unlikely(!mem))
+       if (IS_ERR_OR_NULL(mem))
                return;
 
        alg = tfm->__crt_alg;
index fcde59c65a81b2a9ff6204ae79416a520ee1f057..cb3d6b1c655decb8bf092c7a97b73897fe1a79b8 100644 (file)
@@ -165,6 +165,8 @@ static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
  * crypto_free_acomp() -- free ACOMPRESS tfm handle
  *
  * @tfm:       ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
+ *
+ * If @tfm is a NULL or error pointer, this function does nothing.
  */
 static inline void crypto_free_acomp(struct crypto_acomp *tfm)
 {
index fcc12c593ef8b4a59ca9e20e6e88588e84a62bc2..e728469c4cccb3aa56835b042ee83373377acf24 100644 (file)
@@ -185,6 +185,8 @@ static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
 /**
  * crypto_free_aead() - zeroize and free aead handle
  * @tfm: cipher handle to be freed
+ *
+ * If @tfm is a NULL or error pointer, this function does nothing.
  */
 static inline void crypto_free_aead(struct crypto_aead *tfm)
 {
index 1d3aa252cabafba10df19f4128d3718c9a106cc2..5764b46bd1ec18165b70e7b88003145e34da0dac 100644 (file)
@@ -174,6 +174,8 @@ static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
  * crypto_free_akcipher() - free AKCIPHER tfm handle
  *
  * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
+ *
+ * If @tfm is a NULL or error pointer, this function does nothing.
  */
 static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
 {
index 13f8a6a54ca878f8761d7d95376be283de5698f3..b2bc1e46e86a766cd19de68150c6870a40dcb0de 100644 (file)
@@ -281,6 +281,8 @@ static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
 /**
  * crypto_free_ahash() - zeroize and free the ahash handle
  * @tfm: cipher handle to be freed
+ *
+ * If @tfm is a NULL or error pointer, this function does nothing.
  */
 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
 {
@@ -724,6 +726,8 @@ static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
 /**
  * crypto_free_shash() - zeroize and free the message digest handle
  * @tfm: cipher handle to be freed
+ *
+ * If @tfm is a NULL or error pointer, this function does nothing.
  */
 static inline void crypto_free_shash(struct crypto_shash *tfm)
 {
index 88b591215d5c8f5872374391c7aa34c2838da3db..cccceadc164b9e6041aa8afa07adf13dbe0dbbc0 100644 (file)
@@ -154,6 +154,8 @@ static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
  * crypto_free_kpp() - free KPP tfm handle
  *
  * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
+ *
+ * If @tfm is a NULL or error pointer, this function does nothing.
  */
 static inline void crypto_free_kpp(struct crypto_kpp *tfm)
 {
index 8b4b844b4eef8841b3790c6c2c0e08512e94fdb8..17bb3673d3c1700359515c3d79f7d0da531908f8 100644 (file)
@@ -111,6 +111,8 @@ static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
 /**
  * crypto_free_rng() - zeroize and free RNG handle
  * @tfm: cipher handle to be freed
+ *
+ * If @tfm is a NULL or error pointer, this function does nothing.
  */
 static inline void crypto_free_rng(struct crypto_rng *tfm)
 {
index 6a733b171a5d0ae5ca8edd8abfc74addadedc58f..ef0fc9ed4342e6dca5f48efb2e705877c576d7f1 100644 (file)
@@ -196,6 +196,8 @@ static inline struct crypto_tfm *crypto_skcipher_tfm(
 /**
  * crypto_free_skcipher() - zeroize and free cipher handle
  * @tfm: cipher handle to be freed
+ *
+ * If @tfm is a NULL or error pointer, this function does nothing.
  */
 static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
 {