Skip to content

Commit 83681f2

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: api - check for ERR pointers in crypto_destroy_tfm()
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>
1 parent 98b5ef3 commit 83681f2

8 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎crypto/api.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
562562
{
563563
struct crypto_alg *alg;
564564

565-
if (unlikely(!mem))
565+
if (IS_ERR_OR_NULL(mem))
566566
return;
567567

568568
alg = tfm->__crt_alg;

‎include/crypto/acompress.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
165165
* crypto_free_acomp() -- free ACOMPRESS tfm handle
166166
*
167167
* @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
168+
*
169+
* If @tfm is a NULL or error pointer, this function does nothing.
168170
*/
169171
static inline void crypto_free_acomp(struct crypto_acomp *tfm)
170172
{

‎include/crypto/aead.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
185185
/**
186186
* crypto_free_aead() - zeroize and free aead handle
187187
* @tfm: cipher handle to be freed
188+
*
189+
* If @tfm is a NULL or error pointer, this function does nothing.
188190
*/
189191
static inline void crypto_free_aead(struct crypto_aead *tfm)
190192
{

‎include/crypto/akcipher.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
174174
* crypto_free_akcipher() - free AKCIPHER tfm handle
175175
*
176176
* @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
177+
*
178+
* If @tfm is a NULL or error pointer, this function does nothing.
177179
*/
178180
static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
179181
{

‎include/crypto/hash.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
281281
/**
282282
* crypto_free_ahash() - zeroize and free the ahash handle
283283
* @tfm: cipher handle to be freed
284+
*
285+
* If @tfm is a NULL or error pointer, this function does nothing.
284286
*/
285287
static inline void crypto_free_ahash(struct crypto_ahash *tfm)
286288
{
@@ -724,6 +726,8 @@ static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
724726
/**
725727
* crypto_free_shash() - zeroize and free the message digest handle
726728
* @tfm: cipher handle to be freed
729+
*
730+
* If @tfm is a NULL or error pointer, this function does nothing.
727731
*/
728732
static inline void crypto_free_shash(struct crypto_shash *tfm)
729733
{

‎include/crypto/kpp.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
154154
* crypto_free_kpp() - free KPP tfm handle
155155
*
156156
* @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
157+
*
158+
* If @tfm is a NULL or error pointer, this function does nothing.
157159
*/
158160
static inline void crypto_free_kpp(struct crypto_kpp *tfm)
159161
{

‎include/crypto/rng.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
111111
/**
112112
* crypto_free_rng() - zeroize and free RNG handle
113113
* @tfm: cipher handle to be freed
114+
*
115+
* If @tfm is a NULL or error pointer, this function does nothing.
114116
*/
115117
static inline void crypto_free_rng(struct crypto_rng *tfm)
116118
{

‎include/crypto/skcipher.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ static inline struct crypto_tfm *crypto_skcipher_tfm(
196196
/**
197197
* crypto_free_skcipher() - zeroize and free cipher handle
198198
* @tfm: cipher handle to be freed
199+
*
200+
* If @tfm is a NULL or error pointer, this function does nothing.
199201
*/
200202
static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
201203
{

0 commit comments

Comments
 (0)