Skip to contentSkip to conversation

libtracefs: Fix tracefs_event_enable/disable() to not have open regex

PATCH In review Steven Rostedt 2 participants 3 messages Jun 29, 2021 – Jul 2, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libtracefs: Fix tracefs_event_enable/disable() to not have open regex

From: "Steven Rostedt (VMware)" <[email protected]>

Found that calling tracefs_event_enable/disable() will match all content of the passed in strings and not just what is passed in.

That is, if you have two events, "open" and "open2", and call

tracefs_event_enable(NULL, NULL, "open");

it will match both the "open" event as well as the "open2" event. This is because the regex is open ended.

To fix this, add a '^' to the beginning of the match and a '$' to the end (if they do not already exist).

Fixes fc94d1a8 ("libtracefs: Add tracefs_event_enable/disable() API")
---
 src/tracefs-events.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
src/tracefs-events.c +22 -1
index 0fef64f..b6d3136 100644
@@ -828,6 +828,26 @@ static int enable_disable_all(struct tracefs_instance *instance,
 	return ret < 0 ? ret : 0;
 }
 
+static int make_regex(regex_t *re, const char *match)
+{
+	int len = strlen(match);
+	char str[len + 3];
+	char *p = &str[0];
+
+	if (!len || match[0] != '^')
+		*(p++) = '^';
+
+	strcpy(p, match);
+	p += len;
+
+	if (!len || match[len-1] != '$')
+		*(p++) = '$';
+
+	*p = '\0';
+
+	return regcomp(re, str, REG_ICASE|REG_NOSUB);
+}
+
 static int event_enable_disable(struct tracefs_instance *instance,
 				const char *system, const char *event,
 				bool enable)
@@ -847,12 +867,13 @@ static int event_enable_disable(struct tracefs_instance *instance,
 		goto out_free;
 
 	if (system) {
+		ret = make_regex(&system_re, system);
 		ret = regcomp(&system_re, system, REG_ICASE|REG_NOSUB);
 		if (ret < 0)
 			goto out_free;
 	}
 	if (event) {
-		ret = regcomp(&event_re, event, REG_ICASE|REG_NOSUB);
+		ret = make_regex(&event_re, event);
 		if (ret < 0) {
 			if (system)
 				regfree(&system_re);
Signature · 1 line
2.29.2
#1 lore raw
Quoted 53 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

Found that calling tracefs_event_enable/disable() will match all content of the passed in strings and not just what is passed in.

That is, if you have two events, "open" and "open2", and call

  tracefs_event_enable(NULL, NULL, "open");

it will match both the "open" event as well as the "open2" event. This is because the regex is open ended.

To fix this, add a '^' to the beginning of the match and a '$' to the end (if they do not already exist).

Fixes fc94d1a8 ("libtracefs: Add tracefs_event_enable/disable() API")
---
  src/tracefs-events.c | 23 ++++++++++++++++++++++-
  1 file changed, 22 insertions(+), 1 deletion(-)
src/tracefs-events.c +20 -0
index 0fef64f..b6d3136 100644
@@ -828,6 +828,26 @@ static int enable_disable_all(struct tracefs_instance *instance,
  	return ret < 0 ? ret : 0;
  }
 
+static int make_regex(regex_t *re, const char *match)
+{
+	int len = strlen(match);
+	char str[len + 3];
+	char *p = &str[0];
+
+	if (!len || match[0] != '^')
+		*(p++) = '^';
+
+	strcpy(p, match);
+	p += len;
+
+	if (!len || match[len-1] != '$')
+		*(p++) = '$';
+
+	*p = '\0';
+
+	return regcomp(re, str, REG_ICASE|REG_NOSUB);
+}
+
  static int event_enable_disable(struct tracefs_instance *instance,
  				const char *system, const char *event,
  				bool enable)

src/tracefs-events.c static int event_enable_disable(struct tracefs_instance *instance, · line 871

Quoted 5 lines
src/tracefs-events.c +1 -0
@@ -847,12 +867,13 @@ static int event_enable_disable(struct tracefs_instance *instance,
  		goto out_free;
 
  	if (system) {
+		ret = make_regex(&system_re, system);
  		ret = regcomp(&system_re, system, REG_ICASE|REG_NOSUB);

I think we forgot to remove the line above.
Thanks!
Y.

Quoted 10 lines
src/tracefs-events.c +1 -1
  		if (ret < 0)
  			goto out_free;
  	}
  	if (event) {
-		ret = regcomp(&event_re, event, REG_ICASE|REG_NOSUB);
+		ret = make_regex(&event_re, event);
  		if (ret < 0) {
  			if (system)
  				regfree(&system_re);
#2 lore raw
Quoted 8 lines · On Fri, 2 Jul 2021 14:17:52 +0300 "Yordan Karadzhov (VMware)"…

src/tracefs-events.c static int event_enable_disable(struct tracefs_instance *instance, · line 871

Quoted 6 lines
src/tracefs-events.c +1 -0
@@ -847,12 +867,13 @@ static int event_enable_disable(struct tracefs_instance *instance,
  		goto out_free;
 
  	if (system) {
+		ret = make_regex(&system_re, system);
  		ret = regcomp(&system_re, system, REG_ICASE|REG_NOSUB);  

I think we forgot to remove the line above.

Yes I did, good catch.

-- Steve