Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3678108pxb; Mon, 24 Jan 2022 15:08:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJzklrlg6NW+2DMUQFtKo+CcKNc4CKIpIoMk2UIq3N4oNJnkEbIRBkK793LP9DLMFz9Z+dUl X-Received: by 2002:a17:902:8693:b0:148:a2e8:278d with SMTP id g19-20020a170902869300b00148a2e8278dmr15646313plo.148.1643065687140; Mon, 24 Jan 2022 15:08:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643065687; cv=none; d=google.com; s=arc-20160816; b=j3HEaLkLfDLUO2Sb5S3d0X/wvf0/l4eS8OGZ0PYftUX6eKpnMY22k+uSrtcIVuhRaq xbU4LCI2KMINCWL3X/ji1svepQ8oq/5gwS+IRdejLh+5tKojEvD4HpHlDO8WH6YPMlyO m08A9E5IQgB7W17DLdr2wjx7A2aSPMvnMIdO4vW5hF8viUxc1L9FjwzThoEyd6n/C76D PdJjmf7pQQTeR9Qzq3QqOKE58myfixHnNZprfuzt4kOhI9dQ7Rsmp6zGyv5+A1o5ujAY RWaTIULxnGFm+MSZpvan76wWoMtIkW6V5/9sPGWGpZzA9YoSPMAm75MV0ZrZ1FsaFFBx odPw== 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=Q5G7oaztrADKyFAPCJLgNSqDbdhkCP/moubl+hzlIJd0oX0MQnDx+DkJ+T6aPtQ7fZ X8Ib+5SuFE/XRwO1dgeTXvT3qVfnbDRtquf3fRymj0FmSGmEkrseEWbsqk9IWqFkhtZo ZGzLLGjEbfLOiVgr7XZwTmlZRXTGyYthMuZmnQWUOxOkBaDcfA5XGPYOeTgVg7tbctiE ARaLfL8ZbSIH30Q9ts/l5+ZuBGq316fRwYlLSZxAISEmKR5kxdG8SmQXTYsGC4oQ+22f 4dafc3CjxPHbEK9ARdmeSUY5X2lqU/FH4Rs4nnnHwdtlc4hzLosFfprZyD+E8RQRJzFD 2nWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fCS3moWe; 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 x207si2355647pfc.69.2022.01.24.15.07.54; Mon, 24 Jan 2022 15:08:07 -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=fCS3moWe; 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 S1843573AbiAXXE6 (ORCPT + 99 others); Mon, 24 Jan 2022 18:04:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1581211AbiAXWLY (ORCPT ); Mon, 24 Jan 2022 17:11:24 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 924AEC0DF2FE; Mon, 24 Jan 2022 12:43:34 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id E164360907; Mon, 24 Jan 2022 20:43:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABA5DC340E5; Mon, 24 Jan 2022 20:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643057013; bh=ddcl4cBea324E6KzDBct/pV9ygz8c88MAFzgAN9niJk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fCS3moWeixOMvs3MW9U7sILYSO73qsoLA1n2geE3c2TvS8MghnhBJ4WzkN7ZJOPzs xvsAGvtB+sJvuqbxcV0t+miLP/AKXDd1G2gBRrwmNA+8aFMsRR6kRvWXSPV++oKapN keeJsAOkNAeAL6lxcBUqP8Xmtdead9vPmj8SUFkM= 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.15 673/846] tpm: fix potential NULL pointer access in tpm_del_char_device Date: Mon, 24 Jan 2022 19:43:10 +0100 Message-Id: <20220124184124.292354647@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@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); }