Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp5534329pxb; Tue, 16 Feb 2021 00:32:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxOKpjPk525i+djeoyc8W7yqsyJM5/djm6ZHFf9nFEj74jjiAjJmFRWDWhG2w5CJXb9/apr X-Received: by 2002:a17:907:2d10:: with SMTP id gs16mr19520139ejc.0.1613464327204; Tue, 16 Feb 2021 00:32:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613464327; cv=none; d=google.com; s=arc-20160816; b=I89O/k2w6fP7chQY56MbynnY/DJ2DL0V4o030boUlxqca6ruheG0cU0eOP5i6mMco3 Q4YhcuMZmv8tzmaAX4N7v7ttASFoTZ3zEkGq74pYV0VzDUNEo+zLOPXz0fvnajB8B7wM zdU6KSE6IfogNqEiaaVishkwtSTLBHrdOpMiQ12wWBjLXF9uEFsWhmuV4IwwJC5HHRIu 3iyLcWmt+qgxBliqKlITOjLGG+pgkaSarSFJQ7P8wwfSNTCfnh0JrUvPgDr7d7A3jyzH 4gAEyu6WtlJyF/uurOA4TSvZxPNqHgDLykASUlGxz8K0KQdEmcK9hKke5pGOb4Nr0PbB 2FdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=N8CrTibeSMS8ogokL5D6fnDWD5B1O9Zj481opUhUAgc=; b=ZllnU6ChGI/JypHyCl5taUW9Qtyy5u1RIC/mtFEMIDwqvMAwiXj2Zy3VYtTvWabgdT jb+fcylXyX6G4g4vvawYz2IIwgn0/Gd33cuwLsSeOUI939RrKJBP3cPovTPILQTvEmFU 8u0EY7ujQXXB0necvozWDvq7hhPotmW26Zk0fi2K6S4AIX1NfQf8ZJrW68Td1zIgwVxF 9wB8J38lwY4R887M7hUeLmepEEumxC6mecISR0TLBGBcZCtDQ0M8PMRVRxK/d+pq5Aww pRIET/FCvBJS1yM57iff7A/KPSLKJJp4ZVXCfzBQziyKeLQquBzOxJYVAPj8dH3qzY3w YwLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZnibMyn0; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rs11si56842ejb.81.2021.02.16.00.31.44; Tue, 16 Feb 2021 00:32: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=@kernel.org header.s=k20201202 header.b=ZnibMyn0; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229722AbhBPI2n (ORCPT + 99 others); Tue, 16 Feb 2021 03:28:43 -0500 Received: from mail.kernel.org ([198.145.29.99]:43094 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229803AbhBPI2C (ORCPT ); Tue, 16 Feb 2021 03:28:02 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 33A5164E04; Tue, 16 Feb 2021 08:27:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1613464037; bh=Le2Eb2HspI0V1QIwLnnFJ5M4qLTd6wfC5sgfrFaVXdo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ZnibMyn0sFl9gyI6r4uUD6W0vOzg37DiSUcsVlJXH0BsQVHvEfOZSUSarU50nUhS3 2NrSJiTdZjwRdBy+zm7WM9+HZrLpp8YBwsvtUWQHhpa9FVorCYIlYYoLkqY8LucLj3 8Bi1AeZQ0C2wWSRl890DABaJCxTAsv94JcyM3ThpeU7gPl1Kz10nFsk/1YuF69zhTI YcOv3oL/zFDX7KVNDxhTW4LfsNC1+FTXxMY0iCl0gIH8uw0mJzLxYzVssu2DhmCi7B YOotpk4IMDoGtFuADgnWAlkUX1+JBIKXBVuFNvIDFpixXynDrcJbImrDIYdvA3Y2U0 q9eWcjaMKGZQA== Date: Tue, 16 Feb 2021 10:27:05 +0200 From: Jarkko Sakkinen To: Lino Sanfilippo Cc: peterhuewe@gmx.de, jgg@ziepe.ca, stefanb@linux.vnet.ibm.com, James.Bottomley@hansenpartnership.com, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, Lino Sanfilippo , stable@vger.kernel.org Subject: Re: [PATCH v4] tpm: fix reference counting for struct tpm_chip Message-ID: References: <1613435460-4377-1-git-send-email-LinoSanfilippo@gmx.de> <1613435460-4377-2-git-send-email-LinoSanfilippo@gmx.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1613435460-4377-2-git-send-email-LinoSanfilippo@gmx.de> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 16, 2021 at 01:31:00AM +0100, Lino Sanfilippo wrote: > From: Lino Sanfilippo > > The following sequence of operations results in a refcount warning: > > 1. Open device /dev/tpmrm Add '.' to end. > 2. Remove module tpm_tis_spi Add '.' to end. > 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 brcmfmac > sha256_generic libsha256 sha256_arm hci_uart btbcm bluetooth cfg80211 vc4 > 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/0xa4) > [] (kobject_get) from [] (tpm_try_get_ops+0x14/0x54 [tpm]) > [] (tpm_try_get_ops [tpm]) from [] (tpm_common_write+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: 00000000 000105b4 00000003 beafe664 00000014 00000000 > bfc0: 00000000 000105b4 000103f8 00000004 00000000 00000000 b6f9c000 beafe684 > bfe0: 0000006c beafe648 0001056c b6eb6944 > ---[ end trace d4b8409def9b8b1f ]--- > > The reason for this warning is the attempt to get the chip->dev reference > in tpm_common_write() although the reference counter is already zero. > > Since commit 8979b02aaf1d ("tpm: Fix reference count to main device") the > 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 point > 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 results > in the call of this function. > > Fix this by putting chip->devs in tpm_chip_unregister(). > > Finally move the new implemenation for the TPM 2 handling into a new > function to avoid multiple checks for the TPM_CHIP_FLAG_TPM2 flag in the > good case and error cases. > > 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 > Cc: stable@vger.kernel.org Put Cc first. > --- > drivers/char/tpm/tpm-chip.c | 80 ++++++++++++++++++++++++++++----------------- > 1 file changed, 50 insertions(+), 30 deletions(-) > > diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c > index ddaeceb..44cac3a 100644 > --- a/drivers/char/tpm/tpm-chip.c > +++ b/drivers/char/tpm/tpm-chip.c > @@ -344,7 +344,6 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev, > chip->dev_num = rc; > > device_initialize(&chip->dev); > - device_initialize(&chip->devs); > > chip->dev.class = tpm_class; > chip->dev.class->shutdown_pre = tpm_class_shutdown; > @@ -352,39 +351,20 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev, > chip->dev.parent = pdev; > chip->dev.groups = chip->groups; > > - chip->devs.parent = pdev; > - chip->devs.class = tpmrm_class; > - chip->devs.release = tpm_devs_release; > - /* get extra reference on main device to hold on > - * behalf of devs. This holds the chip structure > - * while cdevs is in use. The corresponding put > - * is in the tpm_devs_release (TPM2 only) > - */ > - if (chip->flags & TPM_CHIP_FLAG_TPM2) > - get_device(&chip->dev); > - > if (chip->dev_num == 0) > chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); > else > chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); > > - chip->devs.devt = > - MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES); > - > rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); > if (rc) > goto out; > - rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num); > - if (rc) > - goto out; > > if (!pdev) > chip->flags |= TPM_CHIP_FLAG_VIRTUAL; > > cdev_init(&chip->cdev, &tpm_fops); > - cdev_init(&chip->cdevs, &tpmrm_fops); > chip->cdev.owner = THIS_MODULE; > - chip->cdevs.owner = THIS_MODULE; > > rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE); > if (rc) { > @@ -396,7 +376,6 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev, > return chip; > > out: > - put_device(&chip->devs); > put_device(&chip->dev); > return ERR_PTR(rc); > } > @@ -431,6 +410,46 @@ struct tpm_chip *tpmm_chip_alloc(struct device *pdev, > } > EXPORT_SYMBOL_GPL(tpmm_chip_alloc); > > +static int tpm_add_tpm2_char_device(struct tpm_chip *chip) > +{ > + int rc; > + > + device_initialize(&chip->devs); > + chip->devs.parent = chip->dev.parent; > + chip->devs.class = tpmrm_class; > + > + rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num); > + if (rc) > + goto out_put_devs; > + /* > + * get extra reference on main device to hold on behalf of devs. > + * This holds the chip structure while cdevs is in use. The > + * corresponding put is in the tpm_devs_release. > + */ > + get_device(&chip->dev); > + chip->devs.release = tpm_devs_release; > + chip->devs.devt = > + MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES); > + cdev_init(&chip->cdevs, &tpmrm_fops); > + chip->cdevs.owner = THIS_MODULE; > + > + rc = cdev_device_add(&chip->cdevs, &chip->devs); > + if (rc) { > + dev_err(&chip->devs, > + "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", > + dev_name(&chip->devs), MAJOR(chip->devs.devt), > + MINOR(chip->devs.devt), rc); > + goto out_put_devs; > + } > + > + return 0; > + > +out_put_devs: > + put_device(&chip->devs); > + > + return rc; > +} > + > static int tpm_add_char_device(struct tpm_chip *chip) > { > int rc; > @@ -445,14 +464,9 @@ static int tpm_add_char_device(struct tpm_chip *chip) > } > > if (chip->flags & TPM_CHIP_FLAG_TPM2) { > - rc = cdev_device_add(&chip->cdevs, &chip->devs); > - if (rc) { > - dev_err(&chip->devs, > - "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", > - dev_name(&chip->devs), MAJOR(chip->devs.devt), > - MINOR(chip->devs.devt), rc); > - return rc; > - } > + rc = tpm_add_tpm2_char_device(chip); > + if (rc) > + goto del_cdev; > } > > /* Make the chip available. */ > @@ -460,6 +474,10 @@ static int tpm_add_char_device(struct tpm_chip *chip) > idr_replace(&dev_nums_idr, chip, chip->dev_num); > mutex_unlock(&idr_lock); > > + return 0; > + > +del_cdev: > + cdev_device_del(&chip->cdev, &chip->dev); > return rc; > } > > @@ -640,8 +658,10 @@ void tpm_chip_unregister(struct tpm_chip *chip) > if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) > hwrng_unregister(&chip->hwrng); > tpm_bios_log_teardown(chip); > - if (chip->flags & TPM_CHIP_FLAG_TPM2) > + if (chip->flags & TPM_CHIP_FLAG_TPM2) { > cdev_device_del(&chip->cdevs, &chip->devs); > + put_device(&chip->devs); > + } > tpm_del_char_device(chip); > } > EXPORT_SYMBOL_GPL(tpm_chip_unregister); > -- > 2.7.4 > Other than that, this looks good to me. If this passes testing, I can fix those nit's. I'll test this post 5.12 PR. /Jarkko