* [PATCH v8 0/1] tpm: fix reference counting for struct tpm_chip
@ 2022-03-01 2:21 Lino Sanfilippo
2022-03-01 2:21 ` [PATCH v8 1/1] " Lino Sanfilippo
2022-03-01 13:56 ` [PATCH v8 0/1] " Stefan Berger
0 siblings, 2 replies; 9+ messages in thread
From: Lino Sanfilippo @ 2022-03-01 2:21 UTC (permalink / raw)
To: peterhuewe, jarkko, jgg
Cc: stefanb, James.Bottomley, David.Laight, linux-integrity,
linux-kernel, p.rosenberger, LinoSanfilippo
This patch fixes a reference count issue in the TPM core code.
Changes in v8:
- adjust names of jump labels for error cases
Changes in v7:
- adjust naming of jump labels to fit better the used label naming scheme
Changes in v6:
- rename function tpm2_add_device() to tpm_devs_add() as requested by Jarko
- add function descriptions
- fix source code formatting
Changes in v5:
- move function tpm_add_tpm2_char_device() to tpm2-space.c and rename
it to tpm2_add_device() as requested by Jarko
- put "cc" tag before all other tags
- ensure that the error path in tpm2_add_device() always calls
the release() function of chip->devs as requested by Jason
- reformat a code line as suggested by David Laight
Changes in v4:
- drop patch 2 (tpm: in tpm2_del_space check if ops pointer is still
valid) since James Bottomley offered a cleaner solution for this
- reimplement patch 1 to setup the /dev/tpmrm device only in case of TPM2
and avoid the installation of another action handler. This is based on a
suggestion and basic implementation done by Jason Gunthorpe.
- added tag to CC stable
Changes in v3:
- drop the patch that introduces the new function tpm_chip_free()
- rework the commit messages for the patches (style, typos, etc.)
- add fixes tag to patch 2
- add James Bottomley to cc list
- add stable mailing list to cc list
Changes in v2:
- drop the patch that erroneously cleaned up after failed installation of
an action handler in tpmm_chip_alloc() (pointed out by Jarkko Sakkinen)
- make the commit message for patch 1 more detailed
- add fixes tags and kernel logs
Lino Sanfilippo (1):
tpm: fix reference counting for struct tpm_chip
drivers/char/tpm/tpm-chip.c | 48 +++++++-----------------------
drivers/char/tpm/tpm.h | 1 +
drivers/char/tpm/tpm2-space.c | 55 +++++++++++++++++++++++++++++++++++
3 files changed, 66 insertions(+), 38 deletions(-)
base-commit: 719fce7539cd3e186598e2aed36325fe892150cf
--
2.35.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v8 1/1] tpm: fix reference counting for struct tpm_chip
2022-03-01 2:21 [PATCH v8 0/1] tpm: fix reference counting for struct tpm_chip Lino Sanfilippo
@ 2022-03-01 2:21 ` Lino Sanfilippo
2022-03-01 12:36 ` Stefan Berger
2022-03-01 14:05 ` Jason Gunthorpe
2022-03-01 13:56 ` [PATCH v8 0/1] " Stefan Berger
1 sibling, 2 replies; 9+ messages in thread
From: Lino Sanfilippo @ 2022-03-01 2:21 UTC (permalink / raw)
To: peterhuewe, jarkko, jgg
Cc: stefanb, James.Bottomley, David.Laight, linux-integrity,
linux-kernel, p.rosenberger, LinoSanfilippo, Lino Sanfilippo,
stable
From: Lino Sanfilippo <l.sanfilippo@kunbus.com>
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 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
[<c0410c3c>] (unwind_backtrace) from [<c040b580>] (show_stack+0x10/0x14)
[<c040b580>] (show_stack) from [<c1092174>] (dump_stack+0xc4/0xd8)
[<c1092174>] (dump_stack) from [<c0445a30>] (__warn+0x104/0x108)
[<c0445a30>] (__warn) from [<c0445aa8>] (warn_slowpath_fmt+0x74/0xb8)
[<c0445aa8>] (warn_slowpath_fmt) from [<c08435d0>] (kobject_get+0xa0/0xa4)
[<c08435d0>] (kobject_get) from [<bf0a715c>] (tpm_try_get_ops+0x14/0x54 [tpm])
[<bf0a715c>] (tpm_try_get_ops [tpm]) from [<bf0a7d6c>] (tpm_common_write+0x38/0x60 [tpm])
[<bf0a7d6c>] (tpm_common_write [tpm]) from [<c05a7ac0>] (vfs_write+0xc4/0x3c0)
[<c05a7ac0>] (vfs_write) from [<c05a7ee4>] (ksys_write+0x58/0xcc)
[<c05a7ee4>] (ksys_write) from [<c04001a0>] (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<n>")
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 implementation 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.
Cc: stable@vger.kernel.org
Fixes: fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm<n>")
Fixes: 8979b02aaf1d ("tpm: Fix reference count to main device")
Co-developed-by: Jason Gunthorpe <jgg@ziepe.ca>
Signed-off-by: Jason Gunthorpe <jgg@ziepe.ca>
Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
drivers/char/tpm/tpm-chip.c | 48 +++++++-----------------------
drivers/char/tpm/tpm.h | 1 +
drivers/char/tpm/tpm2-space.c | 55 +++++++++++++++++++++++++++++++++++
3 files changed, 66 insertions(+), 38 deletions(-)
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index b009e7479b70..06beee4da808 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -274,14 +274,6 @@ static void tpm_dev_release(struct device *dev)
kfree(chip);
}
-static void tpm_devs_release(struct device *dev)
-{
- struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
-
- /* release the master device reference */
- put_device(&chip->dev);
-}
-
/**
* tpm_class_shutdown() - prepare the TPM device for loss of power.
* @dev: device to which the chip is associated.
@@ -344,7 +336,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,29 +343,12 @@ 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;
@@ -382,9 +356,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *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 +368,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);
}
@@ -445,14 +416,9 @@ static int tpm_add_char_device(struct tpm_chip *chip)
}
if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
- 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_devs_add(chip);
+ if (rc)
+ goto err_del_cdev;
}
/* Make the chip available. */
@@ -460,6 +426,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;
+
+err_del_cdev:
+ cdev_device_del(&chip->cdev, &chip->dev);
return rc;
}
@@ -653,8 +623,10 @@ void tpm_chip_unregister(struct tpm_chip *chip)
if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
hwrng_unregister(&chip->hwrng);
tpm_bios_log_teardown(chip);
- if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
+ if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
cdev_device_del(&chip->cdevs, &chip->devs);
+ put_device(&chip->devs);
+ }
tpm_del_char_device(chip);
}
EXPORT_SYMBOL_GPL(tpm_chip_unregister);
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 283f78211c3a..b7070ea9212a 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -234,6 +234,7 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
size_t cmdsiz);
int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
size_t *bufsiz);
+int tpm_devs_add(struct tpm_chip *chip);
void tpm_bios_log_setup(struct tpm_chip *chip);
void tpm_bios_log_teardown(struct tpm_chip *chip);
diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c
index 97e916856cf3..bd9fbd32bc01 100644
--- a/drivers/char/tpm/tpm2-space.c
+++ b/drivers/char/tpm/tpm2-space.c
@@ -574,3 +574,58 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
dev_err(&chip->dev, "%s: error %d\n", __func__, rc);
return rc;
}
+
+/*
+ * Put the reference to the main device.
+ */
+static void tpm_devs_release(struct device *dev)
+{
+ struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
+
+ /* release the master device reference */
+ put_device(&chip->dev);
+}
+
+/*
+ * Add a device file to expose TPM spaces. Also take a reference to the
+ * main device.
+ */
+int tpm_devs_add(struct tpm_chip *chip)
+{
+ int rc;
+
+ device_initialize(&chip->devs);
+ chip->devs.parent = chip->dev.parent;
+ chip->devs.class = tpmrm_class;
+
+ /*
+ * 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 = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
+ if (rc)
+ goto err_put_devs;
+
+ 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 err_put_devs;
+ }
+
+ return 0;
+
+err_put_devs:
+ put_device(&chip->devs);
+
+ return rc;
+}
--
2.35.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v8 1/1] tpm: fix reference counting for struct tpm_chip
2022-03-01 2:21 ` [PATCH v8 1/1] " Lino Sanfilippo
@ 2022-03-01 12:36 ` Stefan Berger
2022-03-01 15:39 ` Lino Sanfilippo
2022-03-03 22:41 ` Jarkko Sakkinen
2022-03-01 14:05 ` Jason Gunthorpe
1 sibling, 2 replies; 9+ messages in thread
From: Stefan Berger @ 2022-03-01 12:36 UTC (permalink / raw)
To: Lino Sanfilippo, peterhuewe, jarkko, jgg
Cc: stefanb, James.Bottomley, David.Laight, linux-integrity,
linux-kernel, p.rosenberger, Lino Sanfilippo, stable
On 2/28/22 21:21, Lino Sanfilippo wrote:
> From: Lino Sanfilippo <l.sanfilippo@kunbus.com>
>
> 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 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
> [<c0410c3c>] (unwind_backtrace) from [<c040b580>] (show_stack+0x10/0x14)
> [<c040b580>] (show_stack) from [<c1092174>] (dump_stack+0xc4/0xd8)
> [<c1092174>] (dump_stack) from [<c0445a30>] (__warn+0x104/0x108)
> [<c0445a30>] (__warn) from [<c0445aa8>] (warn_slowpath_fmt+0x74/0xb8)
> [<c0445aa8>] (warn_slowpath_fmt) from [<c08435d0>] (kobject_get+0xa0/0xa4)
> [<c08435d0>] (kobject_get) from [<bf0a715c>] (tpm_try_get_ops+0x14/0x54 [tpm])
> [<bf0a715c>] (tpm_try_get_ops [tpm]) from [<bf0a7d6c>] (tpm_common_write+0x38/0x60 [tpm])
> [<bf0a7d6c>] (tpm_common_write [tpm]) from [<c05a7ac0>] (vfs_write+0xc4/0x3c0)
> [<c05a7ac0>] (vfs_write) from [<c05a7ee4>] (ksys_write+0x58/0xcc)
> [<c05a7ee4>] (ksys_write) from [<c04001a0>] (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<n>")
> 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 implementation 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.
>
> Cc: stable@vger.kernel.org
> Fixes: fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm<n>")
> Fixes: 8979b02aaf1d ("tpm: Fix reference count to main device")
> Co-developed-by: Jason Gunthorpe <jgg@ziepe.ca>
> Signed-off-by: Jason Gunthorpe <jgg@ziepe.ca>
> Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
> ---
> drivers/char/tpm/tpm-chip.c | 48 +++++++-----------------------
> drivers/char/tpm/tpm.h | 1 +
> drivers/char/tpm/tpm2-space.c | 55 +++++++++++++++++++++++++++++++++++
> 3 files changed, 66 insertions(+), 38 deletions(-)
>
> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
> index b009e7479b70..06beee4da808 100644
> --- a/drivers/char/tpm/tpm-chip.c
> +++ b/drivers/char/tpm/tpm-chip.c
> @@ -274,14 +274,6 @@ static void tpm_dev_release(struct device *dev)
> kfree(chip);
> }
>
> -static void tpm_devs_release(struct device *dev)
> -{
> - struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
> -
> - /* release the master device reference */
> - put_device(&chip->dev);
> -}
> -
> /**
> * tpm_class_shutdown() - prepare the TPM device for loss of power.
> * @dev: device to which the chip is associated.
> @@ -344,7 +336,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,29 +343,12 @@ 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;
>
> @@ -382,9 +356,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *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 +368,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);
> }
> @@ -445,14 +416,9 @@ static int tpm_add_char_device(struct tpm_chip *chip)
> }
>
> if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
> - 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_devs_add(chip);
> + if (rc)
> + goto err_del_cdev;
> }
>
> /* Make the chip available. */
> @@ -460,6 +426,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;
> +
> +err_del_cdev:
> + cdev_device_del(&chip->cdev, &chip->dev);
> return rc;
> }
>
> @@ -653,8 +623,10 @@ void tpm_chip_unregister(struct tpm_chip *chip)
> if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
> hwrng_unregister(&chip->hwrng);
> tpm_bios_log_teardown(chip);
> - if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
> + if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
> cdev_device_del(&chip->cdevs, &chip->devs);
> + put_device(&chip->devs);
> + }
> tpm_del_char_device(chip);
> }
> EXPORT_SYMBOL_GPL(tpm_chip_unregister);
> diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
> index 283f78211c3a..b7070ea9212a 100644
> --- a/drivers/char/tpm/tpm.h
> +++ b/drivers/char/tpm/tpm.h
> @@ -234,6 +234,7 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
> size_t cmdsiz);
> int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
> size_t *bufsiz);
> +int tpm_devs_add(struct tpm_chip *chip);
>
> void tpm_bios_log_setup(struct tpm_chip *chip);
> void tpm_bios_log_teardown(struct tpm_chip *chip);
> diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c
> index 97e916856cf3..bd9fbd32bc01 100644
> --- a/drivers/char/tpm/tpm2-space.c
> +++ b/drivers/char/tpm/tpm2-space.c
> @@ -574,3 +574,58 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
> dev_err(&chip->dev, "%s: error %d\n", __func__, rc);
> return rc;
> }
> +
> +/*
> + * Put the reference to the main device.
> + */
> +static void tpm_devs_release(struct device *dev)
> +{
> + struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
> +
> + /* release the master device reference */
> + put_device(&chip->dev);
> +}
> +
> +/*
> + * Add a device file to expose TPM spaces. Also take a reference to the
> + * main device.
> + */
> +int tpm_devs_add(struct tpm_chip *chip)
> +{
> + int rc;
> +
> + device_initialize(&chip->devs);
> + chip->devs.parent = chip->dev.parent;
> + chip->devs.class = tpmrm_class;
> +
> + /*
> + * 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 = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
> + if (rc)
> + goto err_put_devs;
> +
> + 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 err_put_devs;
> + }
> +
> + return 0;
> +
> +err_put_devs:
> + put_device(&chip->devs);
> +
> + return rc;
> +}
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v8 0/1] tpm: fix reference counting for struct tpm_chip
2022-03-01 2:21 [PATCH v8 0/1] tpm: fix reference counting for struct tpm_chip Lino Sanfilippo
2022-03-01 2:21 ` [PATCH v8 1/1] " Lino Sanfilippo
@ 2022-03-01 13:56 ` Stefan Berger
2022-03-01 15:45 ` Lino Sanfilippo
1 sibling, 1 reply; 9+ messages in thread
From: Stefan Berger @ 2022-03-01 13:56 UTC (permalink / raw)
To: Lino Sanfilippo, peterhuewe, jarkko, jgg
Cc: stefanb, James.Bottomley, David.Laight, linux-integrity,
linux-kernel, p.rosenberger
On 2/28/22 21:21, Lino Sanfilippo wrote:
> This patch fixes a reference count issue in the TPM core code.
We also need to apply this patch here to fix another crash:
https://lore.kernel.org/all/20210615091410.17007-2-vincent.whitchurch@axis.com/
A reproducer for the crash below is here:
t1:
# cd /tmp
# modprobe tpm_vtpm_proxy
# swtpm chardev --vtpm-proxy --tpmstate dir=. --tpm2
New TPM device: /dev/tpm1 (major/minor = 253/1)
t2:
# exec 100<>/dev/tpm1
# exec 101<>/dev/tpmrm1
t1:
# terminate swtpm with ctrl-c
# rmmod tpm_vtpm_proxy
t2:
# echo -en '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00' >&100
t1: [ may not be necessary ]
# modprobe tpm_vtpm_proxy
# swtpm chardev --vtpm-proxy --tpmstate dir=. --tpm2
New TPM device: /dev/tpm2 (major/minor = 253/1)
t2:
exit -> crash
[ 1746.443429]
==================================================================
[ 1746.447007] BUG: KASAN: null-ptr-deref in tpm_chip_start+0x2f/0x120
[ 1746.447856] Read of size 8 at addr 0000000000000060 by task bash/3307
[ 1746.448930] CPU: 129 PID: 3307 Comm: bash Not tainted 5.17.0-rc6+ #477
[ 1746.449818] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS
1.13.0-1ubuntu1.1 04/01/2014
[ 1746.451064] Call Trace:
[ 1746.451399] <TASK>
[ 1746.451689] dump_stack_lvl+0x48/0x5e
[ 1746.452188] ? tpm_chip_start+0x2f/0x120
[ 1746.452717] kasan_report.cold+0x116/0x11b
[ 1746.453277] ? tpm_chip_start+0x2f/0x120
[ 1746.453812] tpm_chip_start+0x2f/0x120
[ 1746.454317] tpm2_del_space+0x2a/0xa0
[ 1746.454816] tpmrm_release+0x42/0x50
[ 1746.455297] __fput+0x104/0x400
[ 1746.455733] task_work_run+0x8b/0xc0
[ 1746.456218] do_exit+0x59e/0x1200
[ 1746.456672] ? handle_mm_fault+0x161/0x340
[ 1746.457254] ? mm_update_next_owner+0x3d0/0x3d0
[ 1746.457878] ? up_read+0x15/0x80
[ 1746.458318] ? do_user_addr_fault+0x29c/0x890
[ 1746.458913] do_group_exit+0x58/0x100
[ 1746.459406] __x64_sys_exit_group+0x28/0x30
[ 1746.459974] do_syscall_64+0x3b/0x90
[ 1746.460457] entry_SYSCALL_64_after_hwframe+0x44/0xae
[ 1746.461157] RIP: 0033:0x7f211cf1a021
[ 1746.461647] Code: Unable to access opcode bytes at RIP 0x7f211cf19ff7.
[ 1746.462511] RSP: 002b:00007ffc9fd6f798 EFLAGS: 00000246 ORIG_RAX:
00000000000000e7
[ 1746.463508] RAX: ffffffffffffffda RBX: 00007f211d012470 RCX:
00007f211cf1a021
[ 1746.464448] RDX: 000000000000003c RSI: 00000000000000e7 RDI:
0000000000000082
[ 1746.465419] RBP: 0000000000000082 R08: ffffffffffffff88 R09:
0000000000000001
[ 1746.466364] R10: 0000000000000004 R11: 0000000000000246 R12:
00007f211d012470
[ 1746.467310] R13: 0000000000000001 R14: 00007f211d012948 R15:
0000000000000000
[ 1746.468252] </TASK>
[ 1746.468552]
==================================================================
[ 1746.469522] Disabling lock debugging due to kernel taint
[ 1746.470367] BUG: kernel NULL pointer dereference, address:
0000000000000060
[ 1746.471325] #PF: supervisor read access in kernel mode
[ 1746.472018] #PF: error_code(0x0000) - not-present page
[ 1746.472706] PGD 0 P4D 0
[ 1746.473060] Oops: 0000 [#1] PREEMPT SMP KASAN NOPTI
[ 1746.475951] CPU: 129 PID: 3307 Comm: bash Tainted: G B
5.17.0-rc6+ #477
[ 1746.479218] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS
1.13.0-1ubuntu1.1 04/01/2014
[ 1746.482636] RIP: 0010:tpm_chip_start+0x2f/0x120
[ 1746.485538] Code: 41 56 41 55 4c 8d af c8 06 00 00 41 54 55 48 89 fd
4c 89 ef 53 e8 a1 18 7f ff 48 8b 9d c8 06 00 00 48 8d 7b 60 e8 91 18 7f
ff <48> 8b 43 60 48 85 c0 74 1c be 01 00 00 00 48 89 ef 0f ae e8 ff d0
[ 1746.492530] RSP: 0018:ffff888109c07d48 EFLAGS: 00010286
[ 1746.495496] RAX: 0000000000000001 RBX: 0000000000000000 RCX:
0000000000000000
[ 1746.498708] RDX: 0000000000000001 RSI: 0000000000000282 RDI:
ffffffffbbbf1ba0
[ 1746.501925] RBP: ffff8881ab138000 R08: ffffffffb8189884 R09:
ffffffffbbba73e7
[ 1746.505098] R10: fffffbfff7774e7c R11: 0000000000000001 R12:
ffff8881adf6bc00
[ 1746.508333] R13: ffff8881ab1386c8 R14: ffff8881adf6bc28 R15:
ffffffffb9d9a0a0
[ 1746.511474] FS: 0000000000000000(0000) GS:ffff888abba80000(0000)
knlGS:0000000000000000
[ 1746.514779] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1746.517799] CR2: 0000000000000060 CR3: 000000071c814003 CR4:
0000000000370ee0
[ 1746.520987] Call Trace:
[ 1746.523627] <TASK>
[ 1746.526210] tpm2_del_space+0x2a/0xa0
[ 1746.528979] tpmrm_release+0x42/0x50
[ 1746.531826] __fput+0x104/0x400
[ 1746.534586] task_work_run+0x8b/0xc0
[ 1746.537452] do_exit+0x59e/0x1200
[ 1746.540259] ? handle_mm_fault+0x161/0x340
[ 1746.543358] ? mm_update_next_owner+0x3d0/0x3d0
[ 1746.546284] ? up_read+0x15/0x80
[ 1746.549108] ? do_user_addr_fault+0x29c/0x890
[ 1746.552037] do_group_exit+0x58/0x100
[ 1746.554894] __x64_sys_exit_group+0x28/0x30
[ 1746.557857] do_syscall_64+0x3b/0x90
[ 1746.560725] entry_SYSCALL_64_after_hwframe+0x44/0xae
[ 1746.563726] RIP: 0033:0x7f211cf1a021
[ 1746.566545] Code: Unable to access opcode bytes at RIP 0x7f211cf19ff7.
[ 1746.569915] RSP: 002b:00007ffc9fd6f798 EFLAGS: 00000246 ORIG_RAX:
00000000000000e7
[ 1746.573289] RAX: ffffffffffffffda RBX: 00007f211d012470 RCX:
00007f211cf1a021
[ 1746.576580] RDX: 000000000000003c RSI: 00000000000000e7 RDI:
0000000000000082
[ 1746.579881] RBP: 0000000000000082 R08: ffffffffffffff88 R09:
0000000000000001
[ 1746.583140] R10: 0000000000000004 R11: 0000000000000246 R12:
00007f211d012470
[ 1746.586446] R13: 0000000000000001 R14: 00007f211d012948 R15:
0000000000000000
[ 1746.589720] </TASK>
[ 1746.592422] Modules linked in: tpm_vtpm_proxy nft_objref
nf_conntrack_netbios_ns nf_conntrack_broadcast nft_fib_inet nft_fib_ipv4
nft_fib_ipv6 nft_fib nft_reject_inet nf_reject_ipv4 nf_reject_ipv6
nft_reject nft_ct nft_chain_nat ip6table_nat ip6table_mangle
ip6table_raw ip6table_security iptable_nat nf_nat nf_conntrack
nf_defrag_ipv6 nf_defrag_ipv4 iptable_mangle iptable_raw
iptable_security ip_set rfkill nf_tables nfnetlink ip6table_filter
ip6_tables iptable_filter sunrpc iTCO_wdt intel_pmc_bxt
iTCO_vendor_support intel_rapl_msr intel_rapl_common kvm_amd ccp kvm
joydev irqbypass virtio_balloon pcspkr i2c_i801 i2c_smbus lpc_ich
tpm_crb tpm_tis tpm_tis_core ip_tables qxl drm_ttm_helper ttm
drm_kms_helper cec crct10dif_pclmul crc32_pclmul crc32c_intel drm
virtio_net ghash_clmulni_intel net_failover virtio_console virtio_scsi
serio_raw failover qemu_fw_cfg ipmi_devintf ipmi_msghandler fuse [last
unloaded: tpm_vtpm_proxy]
[ 1746.623961] CR2: 0000000000000060
[ 1746.627261] ---[ end trace 0000000000000000 ]---
[ 1746.630675] RIP: 0010:tpm_chip_start+0x2f/0x120
[ 1746.634161] Code: 41 56 41 55 4c 8d af c8 06 00 00 41 54 55 48 89 fd
4c 89 ef 53 e8 a1 18 7f ff 48 8b 9d c8 06 00 00 48 8d 7b 60 e8 91 18 7f
ff <48> 8b 43 60 48 85 c0 74 1c be 01 00 00 00 48 89 ef 0f ae e8 ff d0
[ 1746.642208] RSP: 0018:ffff888109c07d48 EFLAGS: 00010286
[ 1746.645658] RAX: 0000000000000001 RBX: 0000000000000000 RCX:
0000000000000000
[ 1746.649335] RDX: 0000000000000001 RSI: 0000000000000282 RDI:
ffffffffbbbf1ba0
[ 1746.652984] RBP: ffff8881ab138000 R08: ffffffffb8189884 R09:
ffffffffbbba73e7
[ 1746.656648] R10: fffffbfff7774e7c R11: 0000000000000001 R12:
ffff8881adf6bc00
[ 1746.660314] R13: ffff8881ab1386c8 R14: ffff8881adf6bc28 R15:
ffffffffb9d9a0a0
[ 1746.664008] FS: 0000000000000000(0000) GS:ffff888abba80000(0000)
knlGS:0000000000000000
[ 1746.667850] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1746.671352] CR2: 0000000000000060 CR3: 000000071c814003 CR4:
0000000000370ee0
[ 1746.675082] Fixing recursive fault but reboot is needed!
[ 1746.678534] BUG: scheduling while atomic: bash/3307/0x00000000
[ 1746.682040] Modules linked in: tpm_vtpm_proxy nft_objref
nf_conntrack_netbios_ns nf_conntrack_broadcast nft_fib_inet nft_fib_ipv4
nft_fib_ipv6 nft_fib nft_reject_inet nf_reject_ipv4 nf_reject_ipv6
nft_reject nft_ct nft_chain_nat ip6table_nat ip6table_mangle
ip6table_raw ip6table_security iptable_nat nf_nat nf_conntrack
nf_defrag_ipv6 nf_defrag_ipv4 iptable_mangle iptable_raw
iptable_security ip_set rfkill nf_tables nfnetlink ip6table_filter
ip6_tables iptable_filter sunrpc iTCO_wdt intel_pmc_bxt
iTCO_vendor_support intel_rapl_msr intel_rapl_common kvm_amd ccp kvm
joydev irqbypass virtio_balloon pcspkr i2c_i801 i2c_smbus lpc_ich
tpm_crb tpm_tis tpm_tis_core ip_tables qxl drm_ttm_helper ttm
drm_kms_helper cec crct10dif_pclmul crc32_pclmul crc32c_intel drm
virtio_net ghash_clmulni_intel net_failover virtio_console virtio_scsi
serio_raw failover qemu_fw_cfg ipmi_devintf ipmi_msghandler fuse [last
unloaded: tpm_vtpm_proxy]
[ 1746.717767] Preemption disabled at:
[ 1746.717769] [<0000000000000000>] 0x0
[ 1746.725600] CPU: 129 PID: 3307 Comm: bash Tainted: G B D
5.17.0-rc6+ #477
[ 1746.730099] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS
1.13.0-1ubuntu1.1 04/01/2014
[ 1746.734880] Call Trace:
[ 1746.738768] <TASK>
[ 1746.742585] dump_stack_lvl+0x48/0x5e
[ 1746.746589] __schedule_bug.cold+0xa3/0xb5
[ 1746.750618] __schedule+0x19a6/0x1e30
[ 1746.754558] ? preempt_count_sub+0x14/0xc0
[ 1746.758507] ? wake_up_klogd.part.0+0x5d/0x80
[ 1746.762478] ? vprintk_emit+0x130/0x280
[ 1746.766398] ? _printk+0x96/0xb2
[ 1746.770202] ? io_schedule_timeout+0xc0/0xc0
[ 1746.774173] ? __cpuidle_text_end+0x8/0x8
[ 1746.778126] do_task_dead+0x56/0x60
[ 1746.781980] make_task_dead.cold+0x77/0xe9
[ 1746.785724] rewind_stack_and_make_dead+0x17/0x17
[ 1746.789457] RIP: 0033:0x7f211cf1a021
[ 1746.792962] Code: Unable to access opcode bytes at RIP 0x7f211cf19ff7.
[ 1746.796809] RSP: 002b:00007ffc9fd6f798 EFLAGS: 00000246 ORIG_RAX:
00000000000000e7
[ 1746.800730] RAX: ffffffffffffffda RBX: 00007f211d012470 RCX:
00007f211cf1a021
[ 1746.804547] RDX: 000000000000003c RSI: 00000000000000e7 RDI:
0000000000000082
[ 1746.808300] RBP: 0000000000000082 R08: ffffffffffffff88 R09:
0000000000000001
[ 1746.812049] R10: 0000000000000004 R11: 0000000000000246 R12:
00007f211d012470
[ 1746.815798] R13: 0000000000000001 R14: 00007f211d012948 R15:
0000000000000000
[ 1746.819553] </TASK>
>
> Changes in v8:
> - adjust names of jump labels for error cases
>
> Changes in v7:
> - adjust naming of jump labels to fit better the used label naming scheme
>
> Changes in v6:
> - rename function tpm2_add_device() to tpm_devs_add() as requested by Jarko
> - add function descriptions
> - fix source code formatting
>
> Changes in v5:
> - move function tpm_add_tpm2_char_device() to tpm2-space.c and rename
> it to tpm2_add_device() as requested by Jarko
> - put "cc" tag before all other tags
> - ensure that the error path in tpm2_add_device() always calls
> the release() function of chip->devs as requested by Jason
> - reformat a code line as suggested by David Laight
>
> Changes in v4:
> - drop patch 2 (tpm: in tpm2_del_space check if ops pointer is still
> valid) since James Bottomley offered a cleaner solution for this
> - reimplement patch 1 to setup the /dev/tpmrm device only in case of TPM2
> and avoid the installation of another action handler. This is based on a
> suggestion and basic implementation done by Jason Gunthorpe.
> - added tag to CC stable
>
> Changes in v3:
> - drop the patch that introduces the new function tpm_chip_free()
> - rework the commit messages for the patches (style, typos, etc.)
> - add fixes tag to patch 2
> - add James Bottomley to cc list
> - add stable mailing list to cc list
>
> Changes in v2:
> - drop the patch that erroneously cleaned up after failed installation of
> an action handler in tpmm_chip_alloc() (pointed out by Jarkko Sakkinen)
> - make the commit message for patch 1 more detailed
> - add fixes tags and kernel logs
>
>
> Lino Sanfilippo (1):
> tpm: fix reference counting for struct tpm_chip
>
> drivers/char/tpm/tpm-chip.c | 48 +++++++-----------------------
> drivers/char/tpm/tpm.h | 1 +
> drivers/char/tpm/tpm2-space.c | 55 +++++++++++++++++++++++++++++++++++
> 3 files changed, 66 insertions(+), 38 deletions(-)
>
>
> base-commit: 719fce7539cd3e186598e2aed36325fe892150cf
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v8 1/1] tpm: fix reference counting for struct tpm_chip
2022-03-01 2:21 ` [PATCH v8 1/1] " Lino Sanfilippo
2022-03-01 12:36 ` Stefan Berger
@ 2022-03-01 14:05 ` Jason Gunthorpe
2022-03-01 16:18 ` Lino Sanfilippo
1 sibling, 1 reply; 9+ messages in thread
From: Jason Gunthorpe @ 2022-03-01 14:05 UTC (permalink / raw)
To: Lino Sanfilippo
Cc: peterhuewe, jarkko, stefanb, James.Bottomley, David.Laight,
linux-integrity, linux-kernel, p.rosenberger, Lino Sanfilippo,
stable
On Tue, Mar 01, 2022 at 03:21:08AM +0100, Lino Sanfilippo wrote:
> @@ -653,8 +623,10 @@ void tpm_chip_unregister(struct tpm_chip *chip)
> if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
> hwrng_unregister(&chip->hwrng);
> tpm_bios_log_teardown(chip);
> - if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
> + if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
> cdev_device_del(&chip->cdevs, &chip->devs);
> + put_device(&chip->devs);
> + }
I would put those two lines in a function bside tpm_devs_add() as
well, more modular.
It looks like a good idea to me
Jason
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v8 1/1] tpm: fix reference counting for struct tpm_chip
2022-03-01 12:36 ` Stefan Berger
@ 2022-03-01 15:39 ` Lino Sanfilippo
2022-03-03 22:41 ` Jarkko Sakkinen
1 sibling, 0 replies; 9+ messages in thread
From: Lino Sanfilippo @ 2022-03-01 15:39 UTC (permalink / raw)
To: Stefan Berger, peterhuewe, jarkko, jgg
Cc: stefanb, James.Bottomley, David.Laight, linux-integrity,
linux-kernel, p.rosenberger, Lino Sanfilippo, stable
Hi,
On 01.03.22 at 13:36, Stefan Berger wrote:
>
> On 2/28/22 21:21, Lino Sanfilippo wrote:
>> From: Lino Sanfilippo <l.sanfilippo@kunbus.com>
>>
>> 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 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
>> [<c0410c3c>] (unwind_backtrace) from [<c040b580>] (show_stack+0x10/0x14)
>> [<c040b580>] (show_stack) from [<c1092174>] (dump_stack+0xc4/0xd8)
>> [<c1092174>] (dump_stack) from [<c0445a30>] (__warn+0x104/0x108)
>> [<c0445a30>] (__warn) from [<c0445aa8>] (warn_slowpath_fmt+0x74/0xb8)
>> [<c0445aa8>] (warn_slowpath_fmt) from [<c08435d0>] (kobject_get+0xa0/0xa4)
>> [<c08435d0>] (kobject_get) from [<bf0a715c>] (tpm_try_get_ops+0x14/0x54 [tpm])
>> [<bf0a715c>] (tpm_try_get_ops [tpm]) from [<bf0a7d6c>] (tpm_common_write+0x38/0x60 [tpm])
>> [<bf0a7d6c>] (tpm_common_write [tpm]) from [<c05a7ac0>] (vfs_write+0xc4/0x3c0)
>> [<c05a7ac0>] (vfs_write) from [<c05a7ee4>] (ksys_write+0x58/0xcc)
>> [<c05a7ee4>] (ksys_write) from [<c04001a0>] (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<n>")
>> 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 implementation 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.
>>
>> Cc: stable@vger.kernel.org
>> Fixes: fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm<n>")
>> Fixes: 8979b02aaf1d ("tpm: Fix reference count to main device")
>> Co-developed-by: Jason Gunthorpe <jgg@ziepe.ca>
>> Signed-off-by: Jason Gunthorpe <jgg@ziepe.ca>
>> Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
>
> Tested-by: Stefan Berger <stefanb@linux.ibm.com>
>
Thanks for testing this!
Regards,
Lino
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v8 0/1] tpm: fix reference counting for struct tpm_chip
2022-03-01 13:56 ` [PATCH v8 0/1] " Stefan Berger
@ 2022-03-01 15:45 ` Lino Sanfilippo
0 siblings, 0 replies; 9+ messages in thread
From: Lino Sanfilippo @ 2022-03-01 15:45 UTC (permalink / raw)
To: Stefan Berger, peterhuewe, jarkko, jgg
Cc: stefanb, James.Bottomley, David.Laight, linux-integrity,
linux-kernel, p.rosenberger
On 01.03.22 at 14:56, Stefan Berger wrote:
>
> We also need to apply this patch here to fix another crash:
>
> https://lore.kernel.org/all/20210615091410.17007-2-vincent.whitchurch@axis.com/
>
Right, this is another issue that should be fixed. The bugfix you mention is essentially the same
as James proposed over two years ago:
https://lore.kernel.org/linux-integrity/e7566e1e48f5be9dca034b4bfb67683b5d3cb88f.camel@HansenPartnership.com/
Regards,
Lino
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v8 1/1] tpm: fix reference counting for struct tpm_chip
2022-03-01 14:05 ` Jason Gunthorpe
@ 2022-03-01 16:18 ` Lino Sanfilippo
0 siblings, 0 replies; 9+ messages in thread
From: Lino Sanfilippo @ 2022-03-01 16:18 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: peterhuewe, jarkko, stefanb, James.Bottomley, David.Laight,
linux-integrity, linux-kernel, p.rosenberger, Lino Sanfilippo,
stable
On 01.03.22 at 15:05, Jason Gunthorpe wrote:
> On Tue, Mar 01, 2022 at 03:21:08AM +0100, Lino Sanfilippo wrote:
>> @@ -653,8 +623,10 @@ void tpm_chip_unregister(struct tpm_chip *chip)
>> if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
>> hwrng_unregister(&chip->hwrng);
>> tpm_bios_log_teardown(chip);
>> - if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
>> + if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
>> cdev_device_del(&chip->cdevs, &chip->devs);
>> + put_device(&chip->devs);
>> + }
>
> I would put those two lines in a function bside tpm_devs_add() as
> well, more modular.
>
Agreed, I will put this in a tpm_devs_remove() function as counterpart to tpm_devs_add().
Regards,
Lino
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v8 1/1] tpm: fix reference counting for struct tpm_chip
2022-03-01 12:36 ` Stefan Berger
2022-03-01 15:39 ` Lino Sanfilippo
@ 2022-03-03 22:41 ` Jarkko Sakkinen
1 sibling, 0 replies; 9+ messages in thread
From: Jarkko Sakkinen @ 2022-03-03 22:41 UTC (permalink / raw)
To: Stefan Berger
Cc: Lino Sanfilippo, peterhuewe, jgg, stefanb, James.Bottomley,
David.Laight, linux-integrity, linux-kernel, p.rosenberger,
Lino Sanfilippo, stable
On Tue, Mar 01, 2022 at 07:36:34AM -0500, Stefan Berger wrote:
>
> On 2/28/22 21:21, Lino Sanfilippo wrote:
> > From: Lino Sanfilippo <l.sanfilippo@kunbus.com>
> >
> > 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 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
> > [<c0410c3c>] (unwind_backtrace) from [<c040b580>] (show_stack+0x10/0x14)
> > [<c040b580>] (show_stack) from [<c1092174>] (dump_stack+0xc4/0xd8)
> > [<c1092174>] (dump_stack) from [<c0445a30>] (__warn+0x104/0x108)
> > [<c0445a30>] (__warn) from [<c0445aa8>] (warn_slowpath_fmt+0x74/0xb8)
> > [<c0445aa8>] (warn_slowpath_fmt) from [<c08435d0>] (kobject_get+0xa0/0xa4)
> > [<c08435d0>] (kobject_get) from [<bf0a715c>] (tpm_try_get_ops+0x14/0x54 [tpm])
> > [<bf0a715c>] (tpm_try_get_ops [tpm]) from [<bf0a7d6c>] (tpm_common_write+0x38/0x60 [tpm])
> > [<bf0a7d6c>] (tpm_common_write [tpm]) from [<c05a7ac0>] (vfs_write+0xc4/0x3c0)
> > [<c05a7ac0>] (vfs_write) from [<c05a7ee4>] (ksys_write+0x58/0xcc)
> > [<c05a7ee4>] (ksys_write) from [<c04001a0>] (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<n>")
> > 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 implementation 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.
> >
> > Cc: stable@vger.kernel.org
> > Fixes: fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm<n>")
> > Fixes: 8979b02aaf1d ("tpm: Fix reference count to main device")
> > Co-developed-by: Jason Gunthorpe <jgg@ziepe.ca>
> > Signed-off-by: Jason Gunthorpe <jgg@ziepe.ca>
> > Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
>
> Tested-by: Stefan Berger <stefanb@linux.ibm.com>
Thanks, would be it be possible to also check v9?
BR, Jarkko
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2022-03-03 22:42 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-01 2:21 [PATCH v8 0/1] tpm: fix reference counting for struct tpm_chip Lino Sanfilippo
2022-03-01 2:21 ` [PATCH v8 1/1] " Lino Sanfilippo
2022-03-01 12:36 ` Stefan Berger
2022-03-01 15:39 ` Lino Sanfilippo
2022-03-03 22:41 ` Jarkko Sakkinen
2022-03-01 14:05 ` Jason Gunthorpe
2022-03-01 16:18 ` Lino Sanfilippo
2022-03-01 13:56 ` [PATCH v8 0/1] " Stefan Berger
2022-03-01 15:45 ` Lino Sanfilippo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).