]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/rv: Fix substring match bug in monitor name search
authorGabriele Monaco <gmonaco@redhat.com>
Thu, 14 May 2026 15:20:42 +0000 (17:20 +0200)
committerGabriele Monaco <gmonaco@redhat.com>
Thu, 4 Jun 2026 14:44:24 +0000 (16:44 +0200)
__ikm_find_monitor_name() relies on strstr() to find a monitor by name,
which fails if the target monitor is a substring of a previously listed
monitor.

Fix it by tokenizing the available_monitors file and matching full
tokens instead.

Fixes: eba321a16fc6 ("tools/rv: Add support for nested monitors")
Reviewed-by: Nam Cao <namcao@linutronix.de>
Link: https://lore.kernel.org/r/20260514152055.229162-2-gmonaco@redhat.com
Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
tools/verification/rv/src/in_kernel.c

index d324538249d3ab6257b64225f3a7fa3499642061..95eac9ab1484689162a3de0d93eca78e6a13f13c 100644 (file)
@@ -58,38 +58,40 @@ static int __ikm_read_enable(char *monitor_name)
  */
 static int __ikm_find_monitor_name(char *monitor_name, char *out_name)
 {
-       char *available_monitors, container[MAX_DA_NAME_LEN+1], *cursor, *end;
-       int retval = 1;
+       char *available_monitors, *cursor, *line;
+       int len = strlen(monitor_name);
+       int found = 0;
 
        available_monitors = tracefs_instance_file_read(NULL, "rv/available_monitors", NULL);
        if (!available_monitors)
                return -1;
 
-       cursor = strstr(available_monitors, monitor_name);
-       if (!cursor) {
-               retval = 0;
-               goto out_free;
-       }
+       config_is_container = 0;
+       cursor = available_monitors;
+       while ((line = strsep(&cursor, "\n"))) {
+               char *colon = strchr(line, ':');
 
-       for (; cursor > available_monitors; cursor--)
-               if (*(cursor-1) == '\n')
-                       break;
-       end = strstr(cursor, "\n");
-       memcpy(out_name, cursor, end-cursor);
-       out_name[end-cursor] = '\0';
-
-       cursor = strstr(out_name, ":");
-       if (cursor)
-               *cursor = '/';
-       else {
-               sprintf(container, "%s:", monitor_name);
-               if (strstr(available_monitors, container))
-                       config_is_container = 1;
+               if (strcmp(line, monitor_name) && (!colon || strcmp(colon + 1, monitor_name)))
+                       continue;
+
+               strncpy(out_name, line, 2 * MAX_DA_NAME_LEN);
+               out_name[2 * MAX_DA_NAME_LEN - 1] = '\0';
+
+               if (colon) {
+                       out_name[colon - line] = '/';
+               } else {
+                       /* If there are children, they are on the next line. */
+                       line = strsep(&cursor, "\n");
+                       if (line && !strncmp(line, monitor_name, len) && line[len] == ':')
+                               config_is_container = 1;
+               }
+
+               found = 1;
+               break;
        }
 
-out_free:
        free(available_monitors);
-       return retval;
+       return found;
 }
 
 /*