linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] coresight: etm3x: Fix buffer overwrite in cntr_val_show()
@ 2025-11-21  0:23 Kuan-Wei Chiu
  2025-11-21  9:50 ` James Clark
  2025-11-26 12:09 ` Leo Yan
  0 siblings, 2 replies; 21+ messages in thread
From: Kuan-Wei Chiu @ 2025-11-21  0:23 UTC (permalink / raw)
  To: suzuki.poulose
  Cc: mike.leach, james.clark, alexander.shishkin, pratikp,
	mathieu.poirier, gregkh, jserv, marscheng, ericchancf, milesjiang,
	nickpan, coresight, linux-arm-kernel, linux-kernel, Kuan-Wei Chiu

The cntr_val_show() function is meant to display the values of all
available counters. However, the sprintf() call inside the loop was
always writing to the beginning of the buffer, causing the output of
previous iterations to be overwritten. As a result, only the value of
the last counter was actually returned to the user.

Fix this by using the return value of sprintf() to calculate the
correct offset into the buffer for the next write, ensuring that all
counter values are appended sequentially.

Fixes: a939fc5a71ad ("coresight-etm: add CoreSight ETM/PTM driver")
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
Build tested only. I do not have the hardware to run the etm3x driver,
so I would be grateful if someone could verify this on actual hardware.

I noticed this issue while browsing the coresight code after attending
a technical talk on the subject. This code dates back to the initial
driver submission over 10 years ago, so I was surprised it hadn't been
caught earlier. Although I cannot perform runtime testing, the logic
error seems obvious to me, so I still decided to submit this patch.

 drivers/hwtracing/coresight/coresight-etm3x-sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-etm3x-sysfs.c b/drivers/hwtracing/coresight/coresight-etm3x-sysfs.c
index 762109307b86..312033e74b7a 100644
--- a/drivers/hwtracing/coresight/coresight-etm3x-sysfs.c
+++ b/drivers/hwtracing/coresight/coresight-etm3x-sysfs.c
@@ -725,7 +725,7 @@ static ssize_t cntr_val_show(struct device *dev,
 	if (!coresight_get_mode(drvdata->csdev)) {
 		spin_lock(&drvdata->spinlock);
 		for (i = 0; i < drvdata->nr_cntr; i++)
-			ret += sprintf(buf, "counter %d: %x\n",
+			ret += sprintf(buf + ret, "counter %d: %x\n",
 				       i, config->cntr_val[i]);
 		spin_unlock(&drvdata->spinlock);
 		return ret;
@@ -733,7 +733,7 @@ static ssize_t cntr_val_show(struct device *dev,
 
 	for (i = 0; i < drvdata->nr_cntr; i++) {
 		val = etm_readl(drvdata, ETMCNTVRn(i));
-		ret += sprintf(buf, "counter %d: %x\n", i, val);
+		ret += sprintf(buf + ret, "counter %d: %x\n", i, val);
 	}
 
 	return ret;
-- 
2.52.0.rc2.455.g230fcf2819-goog



^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2025-11-28 15:15 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-21  0:23 [PATCH] coresight: etm3x: Fix buffer overwrite in cntr_val_show() Kuan-Wei Chiu
2025-11-21  9:50 ` James Clark
2025-11-21 17:02   ` Kuan-Wei Chiu
2025-11-24 16:12     ` James Clark
2025-11-26 10:49       ` Mike Leach
2025-11-26 10:57         ` James Clark
2025-11-27  8:44           ` Kuan-Wei Chiu
2025-11-27  9:17             ` James Clark
2025-11-27  9:22             ` Leo Yan
2025-11-27  9:30               ` James Clark
2025-11-27  9:57                 ` Leo Yan
2025-11-27 14:30                 ` Mike Leach
2025-11-26 12:09 ` Leo Yan
2025-11-26 12:11   ` James Clark
2025-11-26 12:31     ` Leo Yan
2025-11-26 13:42       ` Mike Leach
2025-11-26 15:33         ` James Clark
2025-11-26 16:14           ` Mike Leach
2025-11-27  9:29             ` Leo Yan
2025-11-28 14:53               ` James Clark
2025-11-28 15:14                 ` Al Grant

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).