Skip to content

Commit 8a1862d

Browse files
LinoSanfilippo333smb49
authored andcommitted
tpm: fix reference counting for struct tpm_chip
BugLink: https://bugs.launchpad.net/bugs/1969110 commit 7e0438f upstream. 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 beafe66 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 8979b02 ("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 fdc915f ("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: [email protected] Fixes: fdc915f ("tpm: expose spaces via a device link /dev/tpmrm<n>") Fixes: 8979b02 ("tpm: Fix reference count to main device") Co-developed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Signed-off-by: Lino Sanfilippo <[email protected]> Tested-by: Stefan Berger <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 662893b4f6bd466ff9e1cd454c44c26d32d554fe) Signed-off-by: Paolo Pisati <[email protected]>
1 parent 965da34 commit 8a1862d

File tree

3 files changed

+75
-38
lines changed

3 files changed

+75
-38
lines changed

drivers/char/tpm/tpm-chip.c

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,6 @@ static void tpm_dev_release(struct device *dev)
274274
kfree(chip);
275275
}
276276

277-
static void tpm_devs_release(struct device *dev)
278-
{
279-
struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
280-
281-
/* release the master device reference */
282-
put_device(&chip->dev);
283-
}
284-
285277
/**
286278
* tpm_class_shutdown() - prepare the TPM device for loss of power.
287279
* @dev: device to which the chip is associated.
@@ -344,47 +336,27 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
344336
chip->dev_num = rc;
345337

346338
device_initialize(&chip->dev);
347-
device_initialize(&chip->devs);
348339

349340
chip->dev.class = tpm_class;
350341
chip->dev.class->shutdown_pre = tpm_class_shutdown;
351342
chip->dev.release = tpm_dev_release;
352343
chip->dev.parent = pdev;
353344
chip->dev.groups = chip->groups;
354345

355-
chip->devs.parent = pdev;
356-
chip->devs.class = tpmrm_class;
357-
chip->devs.release = tpm_devs_release;
358-
/* get extra reference on main device to hold on
359-
* behalf of devs. This holds the chip structure
360-
* while cdevs is in use. The corresponding put
361-
* is in the tpm_devs_release (TPM2 only)
362-
*/
363-
if (chip->flags & TPM_CHIP_FLAG_TPM2)
364-
get_device(&chip->dev);
365-
366346
if (chip->dev_num == 0)
367347
chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
368348
else
369349
chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
370350

371-
chip->devs.devt =
372-
MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
373-
374351
rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
375-
if (rc)
376-
goto out;
377-
rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
378352
if (rc)
379353
goto out;
380354

381355
if (!pdev)
382356
chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
383357

384358
cdev_init(&chip->cdev, &tpm_fops);
385-
cdev_init(&chip->cdevs, &tpmrm_fops);
386359
chip->cdev.owner = THIS_MODULE;
387-
chip->cdevs.owner = THIS_MODULE;
388360

389361
rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
390362
if (rc) {
@@ -396,7 +368,6 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
396368
return chip;
397369

398370
out:
399-
put_device(&chip->devs);
400371
put_device(&chip->dev);
401372
return ERR_PTR(rc);
402373
}
@@ -445,21 +416,20 @@ static int tpm_add_char_device(struct tpm_chip *chip)
445416
}
446417

447418
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
448-
rc = cdev_device_add(&chip->cdevs, &chip->devs);
449-
if (rc) {
450-
dev_err(&chip->devs,
451-
"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
452-
dev_name(&chip->devs), MAJOR(chip->devs.devt),
453-
MINOR(chip->devs.devt), rc);
454-
return rc;
455-
}
419+
rc = tpm_devs_add(chip);
420+
if (rc)
421+
goto err_del_cdev;
456422
}
457423

458424
/* Make the chip available. */
459425
mutex_lock(&idr_lock);
460426
idr_replace(&dev_nums_idr, chip, chip->dev_num);
461427
mutex_unlock(&idr_lock);
462428

429+
return 0;
430+
431+
err_del_cdev:
432+
cdev_device_del(&chip->cdev, &chip->dev);
463433
return rc;
464434
}
465435

@@ -649,7 +619,7 @@ void tpm_chip_unregister(struct tpm_chip *chip)
649619
hwrng_unregister(&chip->hwrng);
650620
tpm_bios_log_teardown(chip);
651621
if (chip->flags & TPM_CHIP_FLAG_TPM2)
652-
cdev_device_del(&chip->cdevs, &chip->devs);
622+
tpm_devs_remove(chip);
653623
tpm_del_char_device(chip);
654624
}
655625
EXPORT_SYMBOL_GPL(tpm_chip_unregister);

drivers/char/tpm/tpm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
234234
size_t cmdsiz);
235235
int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
236236
size_t *bufsiz);
237+
int tpm_devs_add(struct tpm_chip *chip);
238+
void tpm_devs_remove(struct tpm_chip *chip);
237239

238240
void tpm_bios_log_setup(struct tpm_chip *chip);
239241
void tpm_bios_log_teardown(struct tpm_chip *chip);

drivers/char/tpm/tpm2-space.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,68 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
574574
dev_err(&chip->dev, "%s: error %d\n", __func__, rc);
575575
return rc;
576576
}
577+
578+
/*
579+
* Put the reference to the main device.
580+
*/
581+
static void tpm_devs_release(struct device *dev)
582+
{
583+
struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
584+
585+
/* release the master device reference */
586+
put_device(&chip->dev);
587+
}
588+
589+
/*
590+
* Remove the device file for exposed TPM spaces and release the device
591+
* reference. This may also release the reference to the master device.
592+
*/
593+
void tpm_devs_remove(struct tpm_chip *chip)
594+
{
595+
cdev_device_del(&chip->cdevs, &chip->devs);
596+
put_device(&chip->devs);
597+
}
598+
599+
/*
600+
* Add a device file to expose TPM spaces. Also take a reference to the
601+
* main device.
602+
*/
603+
int tpm_devs_add(struct tpm_chip *chip)
604+
{
605+
int rc;
606+
607+
device_initialize(&chip->devs);
608+
chip->devs.parent = chip->dev.parent;
609+
chip->devs.class = tpmrm_class;
610+
611+
/*
612+
* Get extra reference on main device to hold on behalf of devs.
613+
* This holds the chip structure while cdevs is in use. The
614+
* corresponding put is in the tpm_devs_release.
615+
*/
616+
get_device(&chip->dev);
617+
chip->devs.release = tpm_devs_release;
618+
chip->devs.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
619+
cdev_init(&chip->cdevs, &tpmrm_fops);
620+
chip->cdevs.owner = THIS_MODULE;
621+
622+
rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
623+
if (rc)
624+
goto err_put_devs;
625+
626+
rc = cdev_device_add(&chip->cdevs, &chip->devs);
627+
if (rc) {
628+
dev_err(&chip->devs,
629+
"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
630+
dev_name(&chip->devs), MAJOR(chip->devs.devt),
631+
MINOR(chip->devs.devt), rc);
632+
goto err_put_devs;
633+
}
634+
635+
return 0;
636+
637+
err_put_devs:
638+
put_device(&chip->devs);
639+
640+
return rc;
641+
}

0 commit comments

Comments
 (0)