Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp982799pxb; Wed, 3 Mar 2021 23:26:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3FqJHg6xdl47k6IFhjsaEGU5zyrffCUm5VRc/ZP0CQh+k5qmHT3qdhztjU/38j6+0BB5F X-Received: by 2002:a17:907:3da5:: with SMTP id he37mr2854342ejc.300.1614842795627; Wed, 03 Mar 2021 23:26:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614842795; cv=none; d=google.com; s=arc-20160816; b=Lxtf9KqZmIvjhgNnzMiWu5w0JLAMw4TiGP9Q7CJ/xX7NYodnQTgeLXIdQEhF9OxF6e xWsz+aHMyDSQ0tOqEyDVDb7oVXNsS9MvLNcJjwMx7qQJSN1SqLm5PyAdMlTZZaCxTwuy 2rh2z0ZfKPHMxbVi+CFYF4NG1HLCpgBo+WmhPjzd4ij7YCEpZxz1PjPQYm7CXq4gueru Bdz1gyzz/5qVbiqDSzM64WLnxMwdw7If1cnn4VYJYcum7tc9NQThlC/PU56lKKnkkaSc rJVV9FEuSLVmdPyOojXlBUBR+P+r9AiarkJ2q7uzlw5gYB8s85Vj4G7PJKp5RhA5ALG0 zPbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=EeKMSLyQoppxULgQ7HwKcEkH8efA6/YKASk8z+/k5zU=; b=tF3IcQqbghF2jgTSzPdZWRoPofp1Kd5xCZDIXm8VQWsY8QJcWIULeHa8fIpihh5ZFm F/T58wtGGCKZrXwKDncyJ842H5G2HLgAX5twXJG2o6fesVmyqHQvDVLKgGWx1IirQuyF M02kB/Rh3TUoKZT8A6kxRySIjlBge1zZzwGJfXRVRHxiSWBh3NothfqjfcTdWteaDkrh rlXknspIXq454G41+PJ2TvUxT6i+WBw6jBsWHtOBTx5IFu8vrgkkJL9Irl6CY46qqJfw +tZ1mNSqn8OvIcwX4vyUIV2rjK4cA3Ses1ZMtTEjiEy7n4mB96XAFfI6YOj1cthr+eRs p/TA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q8gbzPSL; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z7si5294032eja.451.2021.03.03.23.26.13; Wed, 03 Mar 2021 23:26:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q8gbzPSL; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233204AbhCCB0r (ORCPT + 99 others); Tue, 2 Mar 2021 20:26:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:34948 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376795AbhCBH4g (ORCPT ); Tue, 2 Mar 2021 02:56:36 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 838BA64DE5; Tue, 2 Mar 2021 07:55:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614671741; bh=U52npwZXvOiYHgDjNITsEfJDzhXXBzaGfsDnn9Jv/co=; h=From:To:Cc:Subject:Date:From; b=Q8gbzPSLwCq7CM19bgtQxFKZDPat9ro4lnl2qTUoKa/N5Mp2oWqSXO67zWTb0/u5Z 9eJJc2fJkJzhAwM1/IT7w4ncXjGQsVyAGq6Ij0gh7PsO8bgyT8wCn6hPtnlofMMnLb yojn58BPxFTyCan0/aa1BqrHjYozApdD85JE7p23L4lvDUGIDr+H8hFua2Jgi75l8b rT8x+E3a4um6Aq15V8xb/8H/MTWJ/FMz+5efXLKVaDJyWiIwCz+E6CeO1PTP7F4zLU E8aqdSTlu9V3NJaxD2Fjr4qXQK9cTFMxpXrQmM5RUWxGYmHSKrZL9YAlSij0MsM/5A KWdJTSf0XhoWQ== From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@kernel.org, Ard Biesheuvel , syzbot+12cf5fbfdeba210a89dd@syzkaller.appspotmail.com Subject: [PATCH v2] crypto: api - check for ERR pointers in crypto_destroy_tfm() Date: Tue, 2 Mar 2021 08:55:30 +0100 Message-Id: <20210302075530.29315-1-ardb@kernel.org> X-Mailer: git-send-email 2.30.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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 Signed-off-by: Ard Biesheuvel --- v2: update kerneldoc comments of callers to crypto_destroy_tfm() that NULL or error pointers are ignored. crypto/api.c | 2 +- include/crypto/acompress.h | 2 ++ include/crypto/aead.h | 2 ++ include/crypto/akcipher.h | 2 ++ include/crypto/hash.h | 2 ++ include/crypto/kpp.h | 2 ++ include/crypto/rng.h | 2 ++ include/crypto/skcipher.h | 2 ++ 8 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crypto/api.c b/crypto/api.c index ed08cbd5b9d3..c4eda56cff89 100644 --- a/crypto/api.c +++ b/crypto/api.c @@ -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; diff --git a/include/crypto/acompress.h b/include/crypto/acompress.h index fcde59c65a81..cb3d6b1c655d 100644 --- a/include/crypto/acompress.h +++ b/include/crypto/acompress.h @@ -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) { diff --git a/include/crypto/aead.h b/include/crypto/aead.h index fcc12c593ef8..e728469c4ccc 100644 --- a/include/crypto/aead.h +++ b/include/crypto/aead.h @@ -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) { diff --git a/include/crypto/akcipher.h b/include/crypto/akcipher.h index 1d3aa252caba..5764b46bd1ec 100644 --- a/include/crypto/akcipher.h +++ b/include/crypto/akcipher.h @@ -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) { diff --git a/include/crypto/hash.h b/include/crypto/hash.h index 13f8a6a54ca8..f065dbe2205c 100644 --- a/include/crypto/hash.h +++ b/include/crypto/hash.h @@ -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) { diff --git a/include/crypto/kpp.h b/include/crypto/kpp.h index 88b591215d5c..cccceadc164b 100644 --- a/include/crypto/kpp.h +++ b/include/crypto/kpp.h @@ -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) { diff --git a/include/crypto/rng.h b/include/crypto/rng.h index 8b4b844b4eef..17bb3673d3c1 100644 --- a/include/crypto/rng.h +++ b/include/crypto/rng.h @@ -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) { diff --git a/include/crypto/skcipher.h b/include/crypto/skcipher.h index 6a733b171a5d..ef0fc9ed4342 100644 --- a/include/crypto/skcipher.h +++ b/include/crypto/skcipher.h @@ -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) { -- 2.30.1