Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3731063pxb; Mon, 24 Jan 2022 16:34:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJzu8cv9DyBpcAHbXJPEUfu6B9S12QiRXXWJg6PT/y4jplWuIpWYKXuTURaLHGZtmXFnYvDN X-Received: by 2002:a17:902:6841:b0:149:6791:5a4f with SMTP id f1-20020a170902684100b0014967915a4fmr16787511pln.123.1643070886467; Mon, 24 Jan 2022 16:34:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643070886; cv=none; d=google.com; s=arc-20160816; b=riMA/rvGBw4vTP1Gxad2wX8o+SVL3qWEqlsgFM+28ZpjYsFzDLQ/xwDb1V7gDeOydc AOWObngdNqyKmGWVi2n+bkVurCj5F/DKRinCtjlPuUovIV9xVUm8xA4XVK9l4SSuTRwi dg37hky0sY4vQ4k8jcDC/6GX80ED1SmRGF1vCop/7CH8uJVtOZ7YMnyrAv7prbCKrg6H OSTFacQiigXi3fkNPOrC/sMqd8A+2S5ROg7FQstE5hR9r6sORHHG3TkxXBw+uslyfdRi oI/DZsbZsXkq3KJX7sEcx9d18CgwLTSZrRFe9ZF5wC1nDMkdQSjzYL9I7Mt+vsz/fsqa a25g== 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=tAmpeJXoaMle3vIc2pD4+YFbboXph8aAQOtvMOi+XNA=; b=lhGzhZa5pmg1Hky0xLiB9zK9BEZD1I0+hWLku0TTPZT7U7SxIz+XDXsXpdw/wvtkyX hQ/YKoBDlrv3jdknGoSQBWPUXVWoK52u524Sz1Q4lIjMdiMScvXpDPpd2QItBj1AeSTD cVXxPnx9mV2hXpEimB8HPy0R5W7i9U9Wpmf1AXJF8hvhxmClx1Bkf3sPlhkN8dSHVTMV OjVt/UocSF4/Ie63KTB/Dza8LR2KKP4oKWcGhhXbt+AHVbwAmXJU69ri63MeBAY10GhP CbM01TyeuUX1ZSHUg8nP+eEnleyvimIB3PkBs17UY8/flKCafi6eJxYMiQw3V3VKUafV J6VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0I4TpHxm; 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 d23si703847pje.95.2022.01.24.16.34.32; Mon, 24 Jan 2022 16:34:46 -0800 (PST) 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=0I4TpHxm; 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 S3411143AbiAYAcV (ORCPT + 99 others); Mon, 24 Jan 2022 19:32:21 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:48228 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1455879AbiAXVgm (ORCPT ); Mon, 24 Jan 2022 16:36:42 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 76241B8123D; Mon, 24 Jan 2022 21:36:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0959C340E4; Mon, 24 Jan 2022 21:36:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643060199; bh=ddcl4cBea324E6KzDBct/pV9ygz8c88MAFzgAN9niJk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0I4TpHxm+HYQ6Ibu9Bzb0qd+8VuGgR2AbFu65EHkH2qSfLVHXXcAKzGEwUBlBNp89 +2MJmsumvntznRLUNYi1NFCJxvrGbpnuuOGvautVsw3Pw+UbEwLIqJBtOTMrKyPXJW xbIOiuHMB9KQbN3fs9fysZ00kP3cUtqy1pIfWg/w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lino Sanfilippo , Stefan Berger , Jarkko Sakkinen Subject: [PATCH 5.16 0840/1039] tpm: fix potential NULL pointer access in tpm_del_char_device Date: Mon, 24 Jan 2022 19:43:49 +0100 Message-Id: <20220124184153.532359847@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@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: Lino Sanfilippo commit eabad7ba2c752392ae50f24a795093fb115b686d upstream. Some SPI controller drivers unregister the controller in the shutdown handler (e.g. BCM2835). If such a controller is used with a TPM 2 slave chip->ops may be accessed when it is already NULL: At system shutdown the pre-shutdown handler tpm_class_shutdown() shuts down TPM 2 and sets chip->ops to NULL. Then at SPI controller unregistration tpm_tis_spi_remove() is called and eventually calls tpm_del_char_device() which tries to shut down TPM 2 again. Thereby it accesses chip->ops again: (tpm_del_char_device calls tpm_chip_start which calls tpm_clk_enable which calls chip->ops->clk_enable). Avoid the NULL pointer access by testing if chip->ops is valid and skipping the TPM 2 shutdown procedure in case it is NULL. Cc: stable@vger.kernel.org Signed-off-by: Lino Sanfilippo Fixes: 39d0099f9439 ("powerpc/pseries: Add shutdown() to vio_driver and vio_bus") Reviewed-by: Stefan Berger Tested-by: Stefan Berger Reviewed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Greg Kroah-Hartman --- drivers/char/tpm/tpm-chip.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -474,13 +474,21 @@ static void tpm_del_char_device(struct t /* Make the driver uncallable. */ down_write(&chip->ops_sem); - if (chip->flags & TPM_CHIP_FLAG_TPM2) { - if (!tpm_chip_start(chip)) { - tpm2_shutdown(chip, TPM2_SU_CLEAR); - tpm_chip_stop(chip); + + /* + * Check if chip->ops is still valid: In case that the controller + * drivers shutdown handler unregisters the controller in its + * shutdown handler we are called twice and chip->ops to NULL. + */ + if (chip->ops) { + if (chip->flags & TPM_CHIP_FLAG_TPM2) { + if (!tpm_chip_start(chip)) { + tpm2_shutdown(chip, TPM2_SU_CLEAR); + tpm_chip_stop(chip); + } } + chip->ops = NULL; } - chip->ops = NULL; up_write(&chip->ops_sem); }