Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp404075pxj; Thu, 20 May 2021 12:02:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQaNZIutHg/S1PteAJfKSDbruIub3qLzRuLoeyVtu42ctsKek7oA+ORV5mkTBC6US+eGTA X-Received: by 2002:a05:6e02:8e3:: with SMTP id n3mr2780307ilt.115.1621537330868; Thu, 20 May 2021 12:02:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621537330; cv=none; d=google.com; s=arc-20160816; b=reuJ3rtwGTFF/ttxRHZqhGxQha0SpNF86G9rMMgKAO+2XLfkPtJD5EJjk0T7VvXK+8 8pl9mSFdaQYCGZDWW+lV4TIUo8S0o+L55PnoPVYy4DN4P7mNaH4kCUW4wQF+W+RwV1ui eNUjPi3c9QhQbZWHxi8Yo/SlIZBhY+kLvbvV9Gpw07S4f07qUZJCOpi83XIHah+lmaOt 28vv0KbW07jJTAECz6kjVweG0+XNPRKY+Gx2rTjPO2czKok1RGrZyi7uwQMgw8KY1iA0 9FkqNDOxMjbuAFrARQ+LU+fPxW/HxfRAaVauQjNcsPn4ytGPtCcUXv32jplnLD/sagOU 7+Kg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=v8Vm8j7mkltgMfcBw07fYz30mqZKKbt0JFl9mDDzkMI=; b=PyPN8U/+SJqGbRNZdynNrA4VcA9hCSXhvnISL/+tFTqLrwyqP1sN9/olLhoj5Cm7y3 Cg7b9nXYYHfzcZfNcmPTj7AnNmDQv+MFoGY8ZIkZBmU1rgLHkwsI3ohmx21CfbqGnTu2 yoMO9RFh0s0Lf811hTTiQLTynjuUc+pn8SCxTeVUOh0KxXEcrdAAOPwuVEGOd+VB/3Ez 0gEaP6jM2MZa28+qLEEQO94hd/OYWNfoLjdUDZbsN41NjwTwkEqXXhybeklfXhy/7eyp 457oSkV1NxInDnVfquCEYTIGAZ8vvnF6CKXgl3WKkACf5RkxsnHqz1t1TF00r8hl6P1A vYzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=G02EcLMk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n16si2667115ioh.74.2021.05.20.12.01.56; Thu, 20 May 2021 12:02:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=G02EcLMk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236994AbhETK3X (ORCPT + 99 others); Thu, 20 May 2021 06:29:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:47008 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234896AbhETKQC (ORCPT ); Thu, 20 May 2021 06:16:02 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A47C461993; Thu, 20 May 2021 09:45:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621503959; bh=AOTqmLP8aL5U9MlZAQni2Y63nMN1LwdxJr41+d9ftns=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G02EcLMkV+SnptVCtWjzJuojjRYow6Z4+ZUDDr9ytgeGAB2Av2nNDjm5Ll3ID6q92 WnHV4Y96iqRoNQc9/NVjPkEphapdBnTWh6ExMf3+kmufNEnUGyLOsvDROz7VU6kS56 SiEQOvWX7vbSXjtAflBKvhpQPUF32zr5V0hvWviU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+12cf5fbfdeba210a89dd@syzkaller.appspotmail.com, Eric Biggers , Ard Biesheuvel , Herbert Xu , Sasha Levin Subject: [PATCH 4.14 031/323] crypto: api - check for ERR pointers in crypto_destroy_tfm() Date: Thu, 20 May 2021 11:18:43 +0200 Message-Id: <20210520092121.184725861@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ard Biesheuvel [ Upstream commit 83681f2bebb34dbb3f03fecd8f570308ab8b7c2c ] 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 Signed-off-by: Ard Biesheuvel Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- crypto/api.c | 2 +- include/crypto/acompress.h | 2 ++ include/crypto/aead.h | 2 ++ include/crypto/akcipher.h | 2 ++ include/crypto/hash.h | 4 ++++ include/crypto/kpp.h | 2 ++ include/crypto/rng.h | 2 ++ include/crypto/skcipher.h | 2 ++ 8 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crypto/api.c b/crypto/api.c index 187795a6687d..99bd438fa4a4 100644 --- a/crypto/api.c +++ b/crypto/api.c @@ -567,7 +567,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 e328b52425a8..1ff78365607c 100644 --- a/include/crypto/acompress.h +++ b/include/crypto/acompress.h @@ -152,6 +152,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 03b97629442c..0e257ebf12cc 100644 --- a/include/crypto/aead.h +++ b/include/crypto/aead.h @@ -187,6 +187,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 b5e11de4d497..9817f2e5bff8 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 74827781593c..493ed025f0ca 100644 --- a/include/crypto/hash.h +++ b/include/crypto/hash.h @@ -253,6 +253,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) { @@ -689,6 +691,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) { diff --git a/include/crypto/kpp.h b/include/crypto/kpp.h index 1bde0a6514fa..1a34630fc371 100644 --- a/include/crypto/kpp.h +++ b/include/crypto/kpp.h @@ -159,6 +159,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 b95ede354a66..a788c1e5a121 100644 --- a/include/crypto/rng.h +++ b/include/crypto/rng.h @@ -116,6 +116,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 562001cb412b..32aca5f4e4f0 100644 --- a/include/crypto/skcipher.h +++ b/include/crypto/skcipher.h @@ -206,6 +206,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.2