Re: [PATCH v2] tpm: fix potential NULL pointer access in tpm_del_char_device

From: Lino Sanfilippo
Date: Wed Dec 22 2021 - 01:56:39 EST



Hi Stefan,


On 22.12.21 at 05:53, Stefan Berger wrote:

>>
>>   drivers/char/tpm/tpm-chip.c | 16 +++++++++++-----
>>   1 file changed, 11 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
>> index ddaeceb7e109..7960da490e72 100644
>> --- a/drivers/char/tpm/tpm-chip.c
>> +++ b/drivers/char/tpm/tpm-chip.c
>> @@ -474,13 +474,19 @@ static void tpm_del_char_device(struct tpm_chip *chip)
>>         /* Make the driver uncallable. */
>>       down_write(&chip->ops_sem);
>> -    if (chip->flags & TPM_CHIP_FLAG_TPM2) {
>> -        if (!tpm_chip_start(chip)) {
>> -            tpm2_shutdown(chip, TPM2_SU_CLEAR);
>> -            tpm_chip_stop(chip);
>> +    /* Check if chip->ops is still valid: In case that the controller
>> +     * drivers shutdown handler unregisters the controller in its
>> +     * shutdown handler we are called twice and chip->ops to NULL.
>> +     */
>> +    if (chip->ops) {
>> +        if (chip->flags & TPM_CHIP_FLAG_TPM2) {
>> +            if (!tpm_chip_start(chip)) {
>> +                tpm2_shutdown(chip, TPM2_SU_CLEAR);
>> +                tpm_chip_stop(chip);
>> +            }
>>           }
>> +        chip->ops = NULL;
>>       }
>> -    chip->ops = NULL;
>>       up_write(&chip->ops_sem);
>>   }
>>  
>> base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
>
>
> Fixes: 39d0099f9439 ("powerpc/pseries: Add shutdown() to vio_driver and vio_bus")
>
> Reviewed-by: Stefan Berger <stefanb@xxxxxxxxxxxxx>
>
> Tested-by: Stefan Berger <stefanb@xxxxxxxxxxxxx>
>
>

Thanks a lot for testing this.

Best regards,
Lino