driver: tx2: Add NULL check in tx2_uncore_pmu_register
PATCH v3 1/1 driver: tx2: Add NULL check in tx2_uncore_pmu_register
devm_kasprintf() returns NULL if memory allocation fails. Currently, tx2_uncore_pmu_register() does not check for this case, leading to a NULL pointer dereference.
Add NULL check after devm_kasprintf() to prevent this issue. No automated tools were used; this was found during manual code review.
---
V2 -> V3: Dropped the redundant error message as suggested by Markus.
drivers/perf/thunderx2_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/perf/thunderx2_pmu.c +2 -1
@@ -738,7 +738,8 @@ static int tx2_uncore_pmu_register( tx2_pmu->pmu.name =devm_kasprintf (dev,GFP_KERNEL ,"%s" , name); - +if (!tx2_pmu->pmu.name) +return -ENOMEM ;return perf_pmu_register (&tx2_pmu->pmu, tx2_pmu->pmu.name,-1 ); }
Signature · 1 line
2.34.1
Quoted 1 line
devm_kasprintf()returns NULL if memory allocation fails.
call failed?
Quoted 3 lines
Currently,
tx2_uncore_pmu_register()does not check for this case, leading to a NULL pointer dereference.
This (temporary) view should be reconsidered in more detail.
Quoted 1 line
No automated tools were used;
Further development tools can help for such analysis attempts.
Quoted 1 line
this was found during manual code review.
This approach might be misleading so far.
…
drivers/perf/thunderx2_pmu.c static int tx2_uncore_pmu_register( · line 744
Quoted 10 lines
+++
b/drivers/perf/thunderx2_pmu.cdrivers/perf/thunderx2_pmu.c +2
-1@@ -738,7 +738,8 @@ static int tx2_uncore_pmu_register( tx2_pmu->pmu.name =devm_kasprintf (dev,GFP_KERNEL ,"%s" , name); - +if (!tx2_pmu->pmu.name) +return -ENOMEM ;return perf_pmu_register (&tx2_pmu->pmu, tx2_pmu->pmu.name,-1 ); }
…
Should your source code analysis approaches take further implementation details better into account?
Example:perf_pmu_register()
https://elixir.bootlin.com/linux/v6.14-rc6/source/kernel/events/core.c#L11859-L11862
…
if (WARN_ONCE(!name, "Can not register anonymous pmu.\n")) {
ret = -EINVAL;…
Regards,
Markus