Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp4064047pxm; Tue, 1 Mar 2022 10:28:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJx2k2WxKHQYP432CLL1Z3eq9Z/BoUceOIJoL2dgwP8/WsI8gFinwvPwCF8wPIOxxOJa+5ev X-Received: by 2002:a05:6402:528f:b0:410:e24f:91fd with SMTP id en15-20020a056402528f00b00410e24f91fdmr26312367edb.99.1646159311754; Tue, 01 Mar 2022 10:28:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646159311; cv=none; d=google.com; s=arc-20160816; b=I+ABR/owDRMyMcuNEKudqcRbQ8S9bIUGhY+PrWJ6eHnno1pGQujVy2IA0DDQ3tUs38 qqwseh+XJkaQcBxMrNmP/uJUzpgOfNRm5bX+o1IA0uvfJrT9S1ewK24Pwj/s6VptPO5p 404h19rUetKrS4xHfGQF3TFmkyWmrirl3sQEwueClGI6hIhLhbyV0Yewo18AkgX0h2RJ nl3kGoYyrih4Weyo40KZ2biIaV7UDvTB1gTGmb+5/qtaVV3i1TxoFXIg2bd3s8TTzF0/ zbhe4tXA3gYLub8b/jsLa6eYksk86X9qEYjlRT8+a5VBaP8uq5cCilerHI2GZc7cTvT4 VeKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=7iN7wcARzdGBmULFO2icCMhivv1YtW36RUkP8rVrSrM=; b=srum/hV3r8iuK3j6zOAMQcBvcbTDqL+Rq9T+GKmf/QyAQB5S2XUBBt+gZv2nijeMfI Sr9BmkFFJIyC6WUhB2uPGDRn2bNO/YUvZiUnTJaVrkAfPD6EymNit2S8C9Dt8T9lcD7S iFo6FqyQLBXWvuGg/aPj5l92uK/aKy5ucBn9Ny15kVzqfpEfiTE3xSOqSHyl0RTosOB3 xFZOsNcUNj/BnZDhYIxJ/aoEzzJZM5DLbYAo9yxH1YlyTCsLtStbs7Q4W55s6X/QTW40 Mngxw1ktq9AfF6fhH+otTecUr9d43EcFUzrjc5CPiEm1rU5xnNEqaaHHIaJZPH6l/OW8 TVOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=c2UlLgYd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmx.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ga29-20020a1709070c1d00b006d7097b4866si1158792ejc.589.2022.03.01.10.28.09; Tue, 01 Mar 2022 10:28:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=c2UlLgYd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235796AbiCAPlE (ORCPT + 99 others); Tue, 1 Mar 2022 10:41:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230153AbiCAPlA (ORCPT ); Tue, 1 Mar 2022 10:41:00 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA850AA02B; Tue, 1 Mar 2022 07:40:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1646149200; bh=7iN7wcARzdGBmULFO2icCMhivv1YtW36RUkP8rVrSrM=; h=X-UI-Sender-Class:Subject:To:Cc:References:From:Date:In-Reply-To; b=c2UlLgYddABpidhgBmZvQx48ECshAMbK04+kjC+kKKtYLq0Ri4Os2T4HGk6Vtv7bc djjKtYvcmwlEh/+HcQu5lEVvJwVqIUMPoT6V3QpXlCS39p7OpyJdXIdk4S5UIm7037 xp/rpQfti9gJDHnw46xkLSgUZv/v67zTljxHhveE= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.74] ([149.172.237.68]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N8XPt-1oKhVm3lYH-014WYG; Tue, 01 Mar 2022 16:39:59 +0100 Subject: Re: [PATCH v8 1/1] tpm: fix reference counting for struct tpm_chip To: Stefan Berger , peterhuewe@gmx.de, jarkko@kernel.org, jgg@ziepe.ca Cc: stefanb@linux.vnet.ibm.com, James.Bottomley@hansenpartnership.com, David.Laight@ACULAB.COM, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, p.rosenberger@kunbus.com, Lino Sanfilippo , stable@vger.kernel.org References: <20220301022108.30310-1-LinoSanfilippo@gmx.de> <20220301022108.30310-2-LinoSanfilippo@gmx.de> <99eff469-3faf-1e9a-9ad9-e087aeafc301@linux.ibm.com> From: Lino Sanfilippo Message-ID: Date: Tue, 1 Mar 2022 16:39:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <99eff469-3faf-1e9a-9ad9-e087aeafc301@linux.ibm.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:yw2hVG/vwJ0AplBDvF3xK7a4ydQWdm4fO9VnoPeQVY4JYaMdFEi bx9WVpl86BWCq+8q3VE6H6pAqjO/jeVAtBj9in8t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mZjR6R2BoC4aFSjy2pUKaULH5Pr/HQgznChygatTS/xR3ilbzncxofkabMn0F dp1FA72DILXIWanf7Z3iAt19zmLRYoGp4rIyVn4k05YUnD52MLfb47GupJ59Yhzr2kV3vin/P a6HgeX+PGZyFFvDnGL1SoXnAGrchH2tesk05ZAJx2CScgyJBgjmosm4ngITHO5GmgQKJ/jnc/ J7MPc5czYraxrc= X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,NICE_REPLY_A,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On 01.03.22 at 13:36, Stefan Berger wrote: > > On 2/28/22 21:21, Lino Sanfilippo wrote: >> From: Lino Sanfilippo >> >> The following sequence of operations results in a refcount warning: >> >> 1. Open device /dev/tpmrm. >> 2. Remove module tpm_tis_spi. >> 3. Write a TPM command to the file descriptor opened at step 1. >> >> ------------[ cut here ]------------ >> WARNING: CPU: 3 PID: 1161 at lib/refcount.c:25 kobject_get+0xa0/0xa4 >> refcount_t: addition on 0; use-after-free. >> Modules linked in: tpm_tis_spi tpm_tis_core tpm mdio_bcm_unimac brcmfma= c >> sha256_generic libsha256 sha256_arm hci_uart btbcm bluetooth cfg80211 v= c4 >> brcmutil ecdh_generic ecc snd_soc_core crc32_arm_ce libaes >> raspberrypi_hwmon ac97_bus snd_pcm_dmaengine bcm2711_thermal snd_pcm >> snd_timer genet snd phy_generic soundcore [last unloaded: spi_bcm2835] >> CPU: 3 PID: 1161 Comm: hold_open Not tainted 5.10.0ls-main-dirty #2 >> Hardware name: BCM2711 >> [] (unwind_backtrace) from [] (show_stack+0x10/0x14= ) >> [] (show_stack) from [] (dump_stack+0xc4/0xd8) >> [] (dump_stack) from [] (__warn+0x104/0x108) >> [] (__warn) from [] (warn_slowpath_fmt+0x74/0xb8) >> [] (warn_slowpath_fmt) from [] (kobject_get+0xa0/0x= a4) >> [] (kobject_get) from [] (tpm_try_get_ops+0x14/0x54= [tpm]) >> [] (tpm_try_get_ops [tpm]) from [] (tpm_common_writ= e+0x38/0x60 [tpm]) >> [] (tpm_common_write [tpm]) from [] (vfs_write+0xc4= /0x3c0) >> [] (vfs_write) from [] (ksys_write+0x58/0xcc) >> [] (ksys_write) from [] (ret_fast_syscall+0x0/0x4c) >> Exception stack(0xc226bfa8 to 0xc226bff0) >> bfa0:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 00000000 000105b4 00000003 beaf= e664 00000014 00000000 >> bfc0: 00000000 000105b4 000103f8 00000004 00000000 00000000 b6f9c000 be= afe684 >> bfe0: 0000006c beafe648 0001056c b6eb6944 >> ---[ end trace d4b8409def9b8b1f ]--- >> >> The reason for this warning is the attempt to get the chip->dev referen= ce >> in tpm_common_write() although the reference counter is already zero. >> >> Since commit 8979b02aaf1d ("tpm: Fix reference count to main device") t= he >> extra reference used to prevent a premature zero counter is never taken= , >> because the required TPM_CHIP_FLAG_TPM2 flag is never set. >> >> Fix this by moving the TPM 2 character device handling from >> tpm_chip_alloc() to tpm_add_char_device() which is called at a later po= int >> in time when the flag has been set in case of TPM2. >> >> Commit fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm") >> already introduced function tpm_devs_release() to release the extra >> reference but did not implement the required put on chip->devs that res= ults >> in the call of this function. >> >> Fix this by putting chip->devs in tpm_chip_unregister(). >> >> Finally move the new implementation for the TPM 2 handling into a new >> function to avoid multiple checks for the TPM_CHIP_FLAG_TPM2 flag in th= e >> good case and error cases. >> >> Cc: stable@vger.kernel.org >> Fixes: fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm") >> Fixes: 8979b02aaf1d ("tpm: Fix reference count to main device") >> Co-developed-by: Jason Gunthorpe >> Signed-off-by: Jason Gunthorpe >> Signed-off-by: Lino Sanfilippo > > Tested-by: Stefan Berger > Thanks for testing this! Regards, Lino