[PATCH 09/18] wpa_debug: use separate buffer for path and improve error checking

Benjamin Berg benjamin at sipsolutions.net
Thu Oct 30 01:24:40 PDT 2025


From: Benjamin Berg <benjamin.berg at intel.com>

Using the same buffer for output and input could already result in
an overlapping source and destination in snprintf. This was working
fine, however, with the patch to also find tracefs, we continue to parse
the buffer. In that case, the continued parsing can corrupt the found
path causing an error.

Fix this problem and reshuffle the code a bit to make it a bit more
clear and improve the condition to skip lines that could not be parsed
properly.

Fixes: 0a76c7ed64de ("wpa_debug: Prefer tracefs over debugfs")
Signed-off-by: Benjamin Berg <benjamin.berg at intel.com>
---
 src/utils/wpa_debug.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/utils/wpa_debug.c b/src/utils/wpa_debug.c
index 826a9a3738..0cd94a038c 100644
--- a/src/utils/wpa_debug.c
+++ b/src/utils/wpa_debug.c
@@ -128,6 +128,7 @@ static int syslog_priority(int level)
 int wpa_debug_open_linux_tracing(void)
 {
 	int mounts, trace_fd;
+	char path[128] = {};
 	char buf[4096] = {};
 	ssize_t buflen;
 	char *line, *tmp1;
@@ -146,16 +147,20 @@ int wpa_debug_open_linux_tracing(void)
 	}
 	buf[buflen] = '\0';
 
-	line = strtok_r(buf, "\n", &tmp1);
-	while (line) {
+	for (line = strtok_r(buf, "\n", &tmp1);
+	     line;
+	     line = strtok_r(NULL, "\n", &tmp1)) {
 		char *tmp2, *tmp_path, *fstype;
 		/* "<dev> <mountpoint> <fs type> ..." */
 		strtok_r(line, " ", &tmp2);
 		tmp_path = strtok_r(NULL, " ", &tmp2);
 		fstype = strtok_r(NULL, " ", &tmp2);
 
-		if (!buf[0] && fstype && os_strcmp(fstype, "debugfs") == 0) {
-			os_snprintf(buf, sizeof(buf) - 1,
+		if (!line[0] || !tmp_path || !fstype)
+			continue;
+
+		if (os_strcmp(fstype, "debugfs") == 0) {
+			os_snprintf(path, sizeof(path) - 1,
 				    "%s/tracing/trace_marker",
 				    tmp_path);
 			/*
@@ -165,21 +170,19 @@ int wpa_debug_open_linux_tracing(void)
 			 */
 		}
 
-		if (fstype && os_strcmp(fstype, "tracefs") == 0) {
-			os_snprintf(buf, sizeof(buf) - 1, "%s/trace_marker",
+		if (os_strcmp(fstype, "tracefs") == 0) {
+			os_snprintf(path, sizeof(path) - 1, "%s/trace_marker",
 				    tmp_path);
 			break;
 		}
-
-		line = strtok_r(NULL, "\n", &tmp1);
 	}
 
-	if (!buf[0]) {
+	if (!path[0]) {
 		printf("tracefs/debugfs not found\n");
 		return -1;
 	}
 
-	trace_fd = open(buf, O_WRONLY);
+	trace_fd = open(path, O_WRONLY);
 	if (trace_fd < 0) {
 		printf("failed to open trace_marker file\n");
 		return -1;
-- 
2.51.0




More information about the Hostap mailing list