tpm-chip: Move idr_replace calls to appropriate places
PATCH 1/3 tpm-chip: Move idr_replace calls to appropriate places
According to the comments, adding/removing the chip from the list should be the first/last action in (un)register. But currently it is done in a subfunction in the middle of the process. Moving the code from the subfunctions to the appropriate places within (un)register ensures that the code matches the comments.
--- drivers/char/tpm/tpm-chip.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/char/tpm/tpm-chip.c +10 -10
@@ -327,11 +327,6 @@ static int tpm_add_char_device(struct tpm_chip *chip) } } -/* Make the chip available. */ -mutex_lock (&idr_lock); -idr_replace (&dev_nums_idr, chip, chip->dev_num); -mutex_unlock (&idr_lock); -return rc; }
@@ -339,11 +334,6 @@ static void tpm_del_char_device(struct tpm_chip *chip) {cdev_device_del (&chip->cdev, &chip->dev); -/* Make the chip unavailable. */ -mutex_lock (&idr_lock); -idr_replace (&dev_nums_idr,NULL , chip->dev_num); -mutex_unlock (&idr_lock); -/* Make the driver uncallable. */ down_write (&chip->ops_sem);if (chip->flags &TPM_CHIP_FLAG_TPM2 )
@@ -438,6 +428,11 @@ int tpm_chip_register(struct tpm_chip *chip)return rc; } +/* Make the chip available. */ +mutex_lock (&idr_lock); +idr_replace (&dev_nums_idr, chip, chip->dev_num); +mutex_unlock (&idr_lock); +return 0 ; }EXPORT_SYMBOL_GPL (tpm_chip_register);
@@ -457,6 +452,11 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);*/ void tpm_chip_unregister (struct tpm_chip *chip) { +/* Make the chip unavailable. */ +mutex_lock (&idr_lock); +idr_replace (&dev_nums_idr,NULL , chip->dev_num); +mutex_unlock (&idr_lock); +tpm_del_legacy_sysfs (chip);tpm_bios_log_teardown (chip);if (chip->flags &TPM_CHIP_FLAG_TPM2 )
Signature · 6 lines
2.7.4 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
A message that is not in the archive (sent off-list or not yet received): 20170704135648.7360-1-Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/[email protected]
PATCH 2/3 tpm-chip: Return TPM error codes from auto_startup functions
The auto_startup functions for TPM1 and TPM2 convert all TPM error codes to ENODEV on exit. But since there is only one caller for those functions, this code can be consolidated within the caller. Additionally, this allows the caller to distinguish between a TPM being present (but returning error responses for some commands) and no TPM being present (or the TPM malfunctioning completely).
--- drivers/char/tpm/tpm-chip.c | 4 +++- drivers/char/tpm/tpm-interface.c | 6 ++---- drivers/char/tpm/tpm2-cmd.c | 5 ++--- 3 files changed, 7 insertions(+), 8 deletions(-)
drivers/char/tpm/tpm-chip.c +3 -1
@@ -404,8 +404,10 @@ int tpm_chip_register(struct tpm_chip *chip) rc =tpm2_auto_startup (chip); else rc =tpm1_auto_startup (chip); -if (rc) +if (rc <0 )return rc; +else if (rc >0 ) +return -ENODEV ; }tpm_sysfs_add_device (chip);
drivers/char/tpm/tpm-interface.c +2 -4
@@ -997,7 +997,8 @@ EXPORT_SYMBOL_GPL(tpm_do_selftest);* sequence * @chip: TPM chip to use * -* Returns 0 on success, < 0 in case of fatal error. +* Returns 0 on success, < 0 in case of fatal error or a value > 0 representing +* a TPM error code. */ int tpm1_auto_startup (struct tpm_chip *chip) {
@@ -1012,10 +1013,7 @@ int tpm1_auto_startup(struct tpm_chip *chip) goto out; } -return rc; out: -if (rc >0 ) - rc = -ENODEV ;return rc; }
drivers/char/tpm/tpm2-cmd.c +2 -3
@@ -1074,7 +1074,8 @@ static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)* sequence * @chip: TPM chip to use * -* Returns 0 on success, < 0 in case of fatal error. +* Returns 0 on success, < 0 in case of fatal error or a value > 0 representing +* a TPM error code. */ int tpm2_auto_startup (struct tpm_chip *chip) {
@@ -1109,8 +1110,6 @@ int tpm2_auto_startup(struct tpm_chip *chip) rc =tpm2_get_cc_attrs_tbl (chip); out: -if (rc >0 ) - rc = -ENODEV ;return rc; }
Signature · 6 lines
2.7.4 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
PATCH 3/3 tpm-chip: Export TPM device to user space even when startup failed
In reply to a message not in the archiveWhen one of the commands during the auto_startup sequences does not return TPM_RC_SUCCESS, tpm_chip_register misleadingly returns ENODEV, even though a TPM device is definitely present.
An error response during those sequences is indeed unexpected, so to prevent subsequent errors, the kernel should not make use of the TPM device. But user space applications still might be able to communicate with the TPM, so they can be used to further diagnose and/or fix the problem. To allow this, with this patch the device is still exported to user space, even if a TPM error code has been received, but the kernel itself will not be allowed to use the device for anything.
This is not a hypothetical scenario, but there are devices in the wild that show this behavior. With this fix, those devices can be recovered from their failed state.
--- drivers/char/tpm/tpm-chip.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/char/tpm/tpm-chip.c +9 -6
@@ -390,7 +390,7 @@ static int tpm_add_legacy_sysfs(struct tpm_chip *chip)* * Creates a character device for the TPM chip and adds sysfs attributes for * the device. As the last step this function adds the chip to the list of TPM -* chips available for in-kernel use. +* chips available for in-kernel use, if the TPM startup was successful. * * This function should be only called after the chip initialization is * complete.
@@ -398,6 +398,7 @@ static int tpm_add_legacy_sysfs(struct tpm_chip *chip)int tpm_chip_register (struct tpm_chip *chip) {int rc; +bool startup_successful = true;if (chip->ops->flags &TPM_OPS_AUTO_STARTUP ) {if (chip->flags &TPM_CHIP_FLAG_TPM2 )
@@ -407,7 +408,7 @@ int tpm_chip_register(struct tpm_chip *chip)if (rc <0 )return rc;else if (rc >0 ) -return -ENODEV ; + startup_successful = false; }tpm_sysfs_add_device (chip);
@@ -430,10 +431,12 @@ int tpm_chip_register(struct tpm_chip *chip)return rc; } -/* Make the chip available. */ -mutex_lock (&idr_lock); -idr_replace (&dev_nums_idr, chip, chip->dev_num); -mutex_unlock (&idr_lock); +if (startup_successful) { +/* Make the chip available. */ +mutex_lock (&idr_lock); +idr_replace (&dev_nums_idr, chip, chip->dev_num); +mutex_unlock (&idr_lock); + }return 0 ; }
Signature · 6 lines
2.7.4 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Quoted 2 lines · Alexander Steffen
According to the comments, adding/removing the chip from the list should be the first/last action in (un)register.
The comments are misleading..
drivers/char/tpm/tpm-chip.c static int tpm_add_char_device(struct tpm_chip *chip) · line 333
Quoted 11 lines
drivers/char/tpm/tpm-chip.c +1
-4+++ b/drivers/char/tpm/tpm-chip.c@@ -327,11 +327,6 @@ static int tpm_add_char_device(struct tpm_chip *chip) } } -/* Make the chip available. */ -mutex_lock (&idr_lock); -idr_replace (&dev_nums_idr, chip, chip->dev_num); -mutex_unlock (&idr_lock);
This is actually in the wrong place already, it needs to be done before cdev_device_add - this is because cdev_device_add generates the uevent to userspace which could trigger userspace to use the kernel device. So a patch to move it to the start of this function woud be good. The function would be better described as 'make visible'
Maybe resend this patch with only that change..
drivers/char/tpm/tpm-chip.c static void tpm_del_char_device(struct tpm_chip *chip) · line 346
Quoted 8 lines
drivers/char/tpm/tpm-chip.c +0
-5{cdev_device_del (&chip->cdev, &chip->dev); -/* Make the chip unavailable. */ -mutex_lock (&idr_lock); -idr_replace (&dev_nums_idr,NULL , chip->dev_num); -mutex_unlock (&idr_lock); -
The placement of this does not matter so much, but keeping it after the cdev_device_del is easier to understand as it matches the (corrected) setup order..
Jason
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
A message that is not in the archive (sent off-list or not yet received): 20170709212430.GB19327-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org
Quoted 8 lines
drivers/char/tpm/tpm-chip.c static int tpm_add_char_device(struct tpm_chip
Quoted 4 lines
drivers/char/tpm/tpm-chip.c +1
-0+++ b/drivers/char/tpm/tpm-chip.c@@ -327,11 +327,6 @@ static int tpm_add_char_device(struct tpm_chip*chip)
Quoted 3 lines
drivers/char/tpm/tpm-chip.c +0
-0} }
Quoted 10 lines
drivers/char/tpm/tpm-chip.c static int tpm_add_char_device(struct tpm_chip *chip) · line 333
Quoted 4 lines
drivers/char/tpm/tpm-chip.c +0
-4-/* Make the chip available. */ -mutex_lock (&idr_lock); -idr_replace (&dev_nums_idr, chip, chip->dev_num); -mutex_unlock (&idr_lock);This is actually in the wrong place already, it needs to be done before
cdev_device_add- this is becausecdev_device_addgenerates the uevent to userspace which could trigger userspace to use the kernel device. So a patch to move it to the start of this function woud be good. The function would be better described as 'make visible'
I have looked again at the code and I am not sure this is an issue. The call to idr_replace is only necessary to enable in-kernel usage (i.e. RNG, IMA, ...) of the TPM, but it should not affect userspace in any way. So the location of the idr_replace call does not matter much, as long as the TPM is already initialized. In fact, the main purpose of this patch series (please see PATCH 3/3) is to export the device to userspace without calling idr_replace at all under some circumstances.
Or is there something I missed? The only function that ever tries to access the value stored by idr_replace is tpm_chip_find_get. It is usually called with TPM_ANY_NUM, selecting any TPM that might be present. If no TPM is present (or if idr_replace has not been called) the caller needs to deal with the situation already.
Alexander
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot