]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
wpa_debug: Use separate buffer for path and improve error checking
authorBenjamin Berg <benjamin.berg@intel.com>
Thu, 30 Oct 2025 08:24:40 +0000 (09:24 +0100)
committerJouni Malinen <j@w1.fi>
Sat, 13 Dec 2025 20:04:01 +0000 (22:04 +0200)
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@intel.com>
src/utils/wpa_debug.c

index 826a9a3738d7f43e0e26ca7d6970fa5aada372b4..0cd94a038cb1b56b7e3e9c266a8265dd852b3b9e 100644 (file)
@@ -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;