spi: offload: check ops and match pointers before use
Subsystems (2)
PATCH 1/1 spi: offload: check ops and match pointers before use
Before checking if one of the triggers matches, check if 'ops' and 'match' exist
--- drivers/spi/spi-offload.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/spi-offload.c +3 -1
@@ -173,7 +173,9 @@ static struct spi_offload_triggerif (trigger->fwnode != args->fwnode)continue ; - match = trigger->ops->match (trigger, type, args->args, args->nargs); +if (trigger->ops && trigger->ops->match) + match = trigger->ops->match (trigger, type, args->args, args->nargs); +if (match)break ; }
Signature · 1 line
2.43.0
David Lechner
reviewerQuoted 2 lines · Andres Urian Florez
Before checking if one of the triggers matches, check if 'ops' and 'match' exist
Can you please explain in more detail why this change is needed? For example, show the code path where we could actually have null pointer de-reference here that would be fixed by this change.
Quoted 11 lines
Signed-off-by Andres Urian Florez <[email protected]>--- drivers/spi/spi-offload.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/spi/spi-offload.c +0
-0
drivers/spi/spi-offload.c static struct spi_offload_trigger · line 176
Quoted 5 lines
drivers/spi/spi-offload.c +1
-1@@ -173,7 +173,9 @@ static struct spi_offload_triggerif (trigger->fwnode != args->fwnode)continue ; - match = trigger->ops->match (trigger, type, args->args, args->nargs); +if (trigger->ops && trigger->ops->match)
The check for trigger->ops != NULL here should not be necessary. The only place where trigger->ops = NULL is when the trigger is removed from the list and that operation is done with the spi_offload_triggers_lock held. The same lock is also currently held here, so it should not be possible for ops to be set to NULL here.
In fact, there is this later in the same function:
if (!trigger->ops) return ERR_PTR(-ENODEV);
that could be removed (since we have shown that the condition can never be true).
drivers/spi/spi-offload.c static struct spi_offload_trigger · line 181
Quoted 5 lines
drivers/spi/spi-offload.c +2
-0+ match = trigger->ops->match (trigger, type, args->args, args->nargs); +if (match)break ; }
If trigger->ops->match == NULL then the trigger could never be used because it would never be matched. So instead, I think it would be better to check for that in devm_spi_offload_trigger_register() and fail registration if it is missing. In other words, make match a required callback.
Quoted 45 lines · David Lechner
Quoted 2 lines · Andres Urian Florez
Before checking if one of the triggers matches, check if 'ops' and 'match' exist
Can you please explain in more detail why this change is needed? For example, show the code path where we could actually have null pointer de-reference here that would be fixed by this change.
Quoted 11 lines
Signed-off-by Andres Urian Florez <[email protected]>--- drivers/spi/spi-offload.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/spi/spi-offload.c +0
-0drivers/spi/spi-offload.c static struct spi_offload_trigger · line 176
Quoted 5 lines
drivers/spi/spi-offload.c +1
-1@@ -173,7 +173,9 @@ static struct spi_offload_triggerif (trigger->fwnode != args->fwnode)continue ; - match = trigger->ops->match (trigger, type, args->args, args->nargs); +if (trigger->ops && trigger->ops->match)The check for
trigger->ops!= NULL here should not be necessary. The only place wheretrigger->ops= NULL is when the trigger is removed from the list and that operation is done with thespi_offload_triggers_lockheld. The same lock is also currently held here, so it should not be possible for ops to be set to NULL here.In fact, there is this later in the same function:
if (!trigger->ops) return ERR_PTR(-ENODEV);that could be removed (since we have shown that the condition can never be true).
drivers/spi/spi-offload.c static struct spi_offload_trigger · line 181
Quoted 5 lines
drivers/spi/spi-offload.c +2
-0+ match = trigger->ops->match (trigger, type, args->args, args->nargs); +if (match)break ; }
Quoted 4 lines
If
trigger->ops->match== NULL then the trigger could never be used because it would never be matched. So instead, I think it would be better to check for that indevm_spi_offload_trigger_register()and fail registration if it is missing. In other words, make match a required callback.
Hi David.
Thanks for your comments! I did not see the full picture and now it is clear to me that it is not required to check the trigger->ops in spi_offload_trigger_get().
Then I will create another patch to remove the trigger->ops check that you mentioned in spi_offload_trigger_get, and make match a required callback in devm_spi_offload_trigger_register() instead.
David Lechner
reviewerQuoted 9 lines · Andres Urian
Hi David.
Thanks for your comments! I did not see the full picture and now it is clear to me that it is not required to check the
trigger->opsinspi_offload_trigger_get().Then I will create another patch to remove the
trigger->opscheck that you mentioned inspi_offload_trigger_get, and make match a required callback indevm_spi_offload_trigger_register()instead.
To save some future review, I would consider these two separate changes and put them in two separate patches.