coresight: platform: defer connection counter increment until alloc succeeds
PATCH 1/1 coresight: platform: defer connection counter increment until alloc succeeds
coresight_add_out_conn() increments nr_outconns before calling devm_krealloc_array() and again before devm_kmalloc(). If either allocation fails, the counter is already bumped while the corresponding array entry is NULL or uninitialized garbage.
coresight_add_in_conn() has the same problem with nr_inconns and devm_krealloc_array().
In both cases the probe returns -ENOMEM, which causes coresight_get_platform_data() to call coresight_release_platform_data() for cleanup. That function iterates up to nr_outconns (or nr_inconns) entries and dereferences each pointer unconditionally, hitting the NULL or garbage entry and panicking instead of failing gracefully.
Fix by moving the counter increments to after all allocations succeed, so the struct is always consistent on any error path.
--- drivers/hwtracing/coresight/coresight-platform.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/hwtracing/coresight/coresight-platform.c +6 -6
@@ -45,9 +45,8 @@ coresight_add_out_conn(struct device *dev, } } - pdata->nr_outconns++; pdata->out_conns = -devm_krealloc_array (dev, pdata->out_conns, pdata->nr_outconns, +devm_krealloc_array (dev, pdata->out_conns, pdata->nr_outconns +1 ,sizeof (*pdata->out_conns),GFP_KERNEL );if (!pdata->out_conns)return ERR_PTR (-ENOMEM );
@@ -63,7 +62,8 @@ coresight_add_out_conn(struct device *dev,* used right away. */ *conn = *new_conn; - pdata->out_conns[pdata->nr_outconns -1 ] = conn; + pdata->out_conns[pdata->nr_outconns] = conn; + pdata->nr_outconns++;return conn; }EXPORT_SYMBOL_GPL (coresight_add_out_conn);
@@ -86,13 +86,13 @@ int coresight_add_in_conn(struct coresight_connection *out_conn)return 0 ; } - pdata->nr_inconns++; pdata->in_conns = -devm_krealloc_array (dev, pdata->in_conns, pdata->nr_inconns, +devm_krealloc_array (dev, pdata->in_conns, pdata->nr_inconns +1 ,sizeof (*pdata->in_conns),GFP_KERNEL );if (!pdata->in_conns)return -ENOMEM ; - pdata->in_conns[pdata->nr_inconns -1 ] = out_conn; + pdata->in_conns[pdata->nr_inconns] = out_conn; + pdata->nr_inconns++;return 0 ; }EXPORT_SYMBOL_GPL (coresight_add_in_conn);
---
base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260511-fix-ref-count-issue-7c44ce39700f
Best regards,
Signature · 1 line
Jie Gan <[email protected]>
Quoted 14 lines · Jie Gan
coresight_add_out_conn()incrementsnr_outconnsbefore callingdevm_krealloc_array()and again beforedevm_kmalloc(). If either allocation fails, the counter is already bumped while the corresponding array entry is NULL or uninitialized garbage.
coresight_add_in_conn()has the same problem withnr_inconnsanddevm_krealloc_array().In both cases the probe returns
-ENOMEM, which causescoresight_get_platform_data()to callcoresight_release_platform_data()for cleanup. That function iterates up tonr_outconns(ornr_inconns) entries and dereferences each pointer unconditionally, hitting the NULL or garbage entry and panicking instead of failing gracefully.
Quoted 6 lines
Fix by moving the counter increments to after all allocations succeed, so the struct is always consistent on any error path.
Fixes 3d4ff657e454 ("coresight: Dynamically add connections")Fixes e3f4e68797a9 ("coresight: Store in-connections as well as out-connections")Signed-off-by Jie Gan <[email protected]>
Quoted 52 lines
--- drivers/hwtracing/coresight/coresight-platform.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)drivers/hwtracing/coresight/coresight-platform.c +6
-7@@ -45,9 +45,8 @@ coresight_add_out_conn(struct device *dev, } } - pdata->nr_outconns++; pdata->out_conns = -devm_krealloc_array (dev, pdata->out_conns, pdata->nr_outconns, +devm_krealloc_array (dev, pdata->out_conns, pdata->nr_outconns +1 ,sizeof (*pdata->out_conns),GFP_KERNEL );if (!pdata->out_conns)return ERR_PTR (-ENOMEM );@@ -63,7 +62,8 @@ coresight_add_out_conn(struct device *dev,* used right away. */ *conn = *new_conn; - pdata->out_conns[pdata->nr_outconns -1 ] = conn; + pdata->out_conns[pdata->nr_outconns] = conn; + pdata->nr_outconns++;return conn; }EXPORT_SYMBOL_GPL (coresight_add_out_conn);@@ -86,13 +86,13 @@ int coresight_add_in_conn(struct coresight_connection *out_conn)return 0 ; } - pdata->nr_inconns++; pdata->in_conns = -devm_krealloc_array (dev, pdata->in_conns, pdata->nr_inconns, +devm_krealloc_array (dev, pdata->in_conns, pdata->nr_inconns +1 ,sizeof (*pdata->in_conns),GFP_KERNEL );if (!pdata->in_conns)return -ENOMEM ; - pdata->in_conns[pdata->nr_inconns -1 ] = out_conn; + pdata->in_conns[pdata->nr_inconns] = out_conn; + pdata->nr_inconns++;return 0 ; }EXPORT_SYMBOL_GPL (coresight_add_in_conn); ---base-commit:
e98d21c170b01ddef366f023bbfcf6b31509fa83
change-id: 20260511-fix-ref-count-issue-7c44ce39700fBest regards,
Quoted 9 lines · Jie Gan
coresight_add_out_conn()incrementsnr_outconnsbefore callingdevm_krealloc_array()and again beforedevm_kmalloc(). If either allocation fails, the counter is already bumped while the corresponding array entry is NULL or uninitialized garbage.
coresight_add_in_conn()has the same problem withnr_inconnsanddevm_krealloc_array().[...]
Applied, thanks!
[1/1] coresight: platform: defer connection counter increment until alloc succeeds
https://git.kernel.org/coresight/c/1563ae33dc4f
Best regards,
Signature · 1 line
Suzuki K Poulose <[email protected]>