Skip to contentSkip to conversation

Build and fuzzing related fixes

PATCH In review Ian Rogers 2 participants 10 messages Jun 12, 2021 – Jun 17, 2021 1 message not in the archive

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

cover Build and fuzzing related fixes

EOF checks are missing in a number of cases that cause the parser to enter an infinite loop if an EOF is encountered.
Some build systems are picky about angled vs quotes, fix this minor issue.

Claire Jensen (2):
libtraceevent: Add eof checks.
libtraceevent: Changed angled brackets to double quotes.

 src/event-parse.c | 62 ++++++++++++++++++++++++++++++++++++-----------
 src/event-utils.h |  2 +-
 2 files changed, 49 insertions(+), 15 deletions(-)
Signature · 1 line
2.32.0.272.g935e593368-goog
author
#1 lore raw

PATCH 1/2 libtraceevent: Add eof checks.

From: Claire Jensen <[email protected]>

Added checking for __read_char and peek_char to make sure value is not at end of file.

---
 src/event-parse.c | 62 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 48 insertions(+), 14 deletions(-)
src/event-parse.c +48 -14
index 9915cb4..ac11887 100644
@@ -1155,17 +1155,16 @@ static enum tep_event_type force_token(const char *str, char **tok);
 static enum tep_event_type __read_token(char **tok)
 {
 	char buf[BUFSIZ];
-	int ch, last_ch, quote_ch, next_ch;
+	int ch, last_ch, quote_ch, next_ch, read_ch, peek_ch;
 	int i = 0;
 	int tok_size = 0;
 	enum tep_event_type type;
 
 	*tok = NULL;
 
-
-	ch = __read_char();
+        ch = __read_char();
 	if (ch < 0)
-		return TEP_EVENT_NONE;
+		goto out_eof_error;
 
 	type = get_type(ch);
 	if (type == TEP_EVENT_NONE)
@@ -1184,9 +1183,15 @@ static enum tep_event_type __read_token(char **tok)
 	case TEP_EVENT_OP:
 		switch (ch) {
 		case '-':
-			next_ch = peek_char();
+			peek_ch = peek_char();
+			if (peek_ch < 0)
+				goto out_eof_error;
+			next_ch = peek_ch;
 			if (next_ch == '>') {
-				buf[i++] = __read_char();
+				read_ch = __read_char();
+				if (read_ch < 0)
+					goto out_eof_error;
+				buf[i++] = read_ch;
 				break;
 			}
 			/* fall through */
@@ -1197,9 +1202,14 @@ static enum tep_event_type __read_token(char **tok)
 		case '<':
 			last_ch = ch;
 			ch = peek_char();
+			if (ch < 0)
+				goto out_eof_error;
 			if (ch != last_ch)
 				goto test_equal;
-			buf[i++] = __read_char();
+			read_ch = __read_char();
+			if (read_ch < 0)
+				goto out_eof_error;
+			buf[i++] = read_ch;
 			switch (last_ch) {
 			case '>':
 			case '<':
@@ -1219,10 +1229,17 @@ static enum tep_event_type __read_token(char **tok)
 		return type;
 
  test_equal:
-		ch = peek_char();
-		if (ch == '=')
-			buf[i++] = __read_char();
-		goto out;
+		peek_ch = peek_char();
+		if (peek_ch < 0)
+			goto out_eof_error;
+		ch = peek_ch;
+		if (ch == '=') {
+			read_ch = __read_char();
+			if (read_ch < 0)
+				goto out_eof_error;
+			buf[i++] = read_ch;
+			goto out;
+		}
 
 	case TEP_EVENT_DQUOTE:
 	case TEP_EVENT_SQUOTE:
@@ -1242,6 +1259,8 @@ static enum tep_event_type __read_token(char **tok)
 			}
 			last_ch = ch;
 			ch = __read_char();
+			if(ch < 0)
+				goto out_eof_error;
 			buf[i++] = ch;
 			/* the '\' '\' will cancel itself */
 			if (ch == '\\' && last_ch == '\\')
@@ -1259,6 +1278,8 @@ static enum tep_event_type __read_token(char **tok)
 
 			do {
 				ch = __read_char();
+				if(ch < 0)
+					return TEP_EVENT_NONE;
 			} while (isspace(ch));
 			if (ch == '"')
 				goto concat;
@@ -1273,7 +1294,13 @@ static enum tep_event_type __read_token(char **tok)
 		break;
 	}
 
-	while (get_type(peek_char()) == type) {
+	while (1) {
+		peek_ch = peek_char();
+		if (peek_ch < 0)
+			goto out_eof_error;
+		if (get_type(peek_ch) != type)
+			break;
+
 		if (i == (BUFSIZ - 1)) {
 			buf[i] = 0;
 			tok_size += BUFSIZ;
@@ -1282,8 +1309,10 @@ static enum tep_event_type __read_token(char **tok)
 				return TEP_EVENT_NONE;
 			i = 0;
 		}
-		ch = __read_char();
-		buf[i++] = ch;
+		read_ch = __read_char();
+		if (read_ch < 0)
+			goto out_eof_error;
+		buf[i++] = read_ch;
 	}
 
  out:
@@ -1316,6 +1345,11 @@ static enum tep_event_type __read_token(char **tok)
 	}
 
 	return type;
+
+out_eof_error:
+	free(*tok);
+	*tok = NULL;
+	return TEP_EVENT_NONE;
 }
 
 static enum tep_event_type force_token(const char *str, char **tok)
Signature · 1 line
2.32.0.272.g935e593368-goog
#2 lore raw

Re: [PATCH 1/2] libtraceevent: Add eof checks.

Quoted 4 lines · On Fri, 11 Jun 2021 18:45:00 -0700 Ian Rogers <[email protected]> wrote:

From: Claire Jensen <[email protected]>

Added checking for __read_char and peek_char to make sure value is not at end of file.

Was there a case where this happened?

-- Steve

A message that is not in the archive (sent off-list or not yet received): CAFPGG2iQK8XMv6Z1-KurgjOnYuk=m=uWNWJXj6OEb_SBQkokZA@mail.gmail.com

#4 lore raw

Re: [PATCH 1/2] libtraceevent: Add eof checks.

Quoted 10 lines · On Thu, 17 Jun 2021 13:56:49 -0500 Claire Jensen <[email protected]> wrote:

Hi Steve,

This issue was found while fuzz testing. One of the test cases created an infinite loop because __read_token had reached end of file. Checking was added to all cases where this may occur.

Sorry for the late reply, I just got an issue surrounding email permissions resolved so I was unable to respond earlier.

Can you resend the patch with this information in the change log.

Thanks!

-- Steve

author
#5 lore raw

PATCH 2/2 libtraceevent: Changed angled brackets to double quotes.

In reply to Ian Rogers

From: Claire Jensen <[email protected]>

---
 src/event-utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/event-utils.h +1 -1
index d377201..44f7968 100644
@@ -10,7 +10,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 
-#include <event-parse.h>
+#include "event-parse.h"
 
 void tep_warning(const char *fmt, ...);
 void tep_info(const char *fmt, ...);
Signature · 1 line
2.32.0.272.g935e593368-goog
#6 lore raw

Re: [PATCH 2/2] libtraceevent: Changed angled brackets to double quotes.

Quoted 13 lines · On Fri, 11 Jun 2021 18:45:01 -0700 Ian Rogers <[email protected]> wrote:

From: Claire Jensen <[email protected]>

---
 src/event-utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/event-utils.h +0 -0
index d377201..44f7968 100644

src/event-utils.h line 13

Quoted 5 lines
src/event-utils.h +1 -1
@@ -10,7 +10,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 
-#include <event-parse.h>
+#include "event-parse.h"

Thanks for the update, but event-parse.h is part of the exported library and even though it happens to be local to this file, we use the '<' and '>' to denote that it is a library header and not a local one.

-- Steve

Quoted 3 lines
src/event-utils.h +0 -0
 void tep_warning(const char *fmt, ...);
 void tep_info(const char *fmt, ...);
author
#7 lore raw

Re: [PATCH 2/2] libtraceevent: Changed angled brackets to double quotes.

Quoted 20 lines · Steven Rostedt
Quoted 16 lines · On Fri, 11 Jun 2021 18:45:01 -0700 Ian Rogers <[email protected]> wrote:

From: Claire Jensen <[email protected]>

---
 src/event-utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/event-utils.h +0 -0
index d377201..44f7968 100644
@@ -10,7 +10,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 
Quoted 8 lines

src/event-utils.h line 13

Quoted 2 lines
src/event-utils.h +1 -1
-#include <event-parse.h>
+#include "event-parse.h"

Thanks for the update, but event-parse.h is part of the exported library and even though it happens to be local to this file, we use the '<' and '>' to denote that it is a library header and not a local one.

-- Steve

Would it be more conventional in this situation to use quotes within the library and angles outside? This change is within the library and not with code trying to link against it.

Thanks,
Ian

Quoted 5 lines
Quoted 4 lines
src/event-utils.h +0 -0
 void tep_warning(const char *fmt, ...);
 void tep_info(const char *fmt, ...);
#8 lore raw

Re: [PATCH 2/2] libtraceevent: Changed angled brackets to double quotes.

Quoted 3 lines · On Sun, 13 Jun 2021 14:31:42 -0700 Ian Rogers <[email protected]> wrote:

Would it be more conventional in this situation to use quotes within the library and angles outside? This change is within the library and not with code trying to link against it.

Does it matter? If not, what's the purpose of changing it?

Note, the change log is empty, and is where rationale for the change is suppose to be added.

-- Steve

author
#9 lore raw

Re: [PATCH 2/2] libtraceevent: Changed angled brackets to double quotes.

Quoted 10 lines · Steven Rostedt
Quoted 3 lines · On Sun, 13 Jun 2021 14:31:42 -0700 Ian Rogers <[email protected]> wrote:

Would it be more conventional in this situation to use quotes within the library and angles outside? This change is within the library and not with code trying to link against it.

Does it matter? If not, what's the purpose of changing it?

Sorry for not having this in the body, in the cover message I added to Claire's email with:

Quoted 1 line

Some build systems are picky about angled vs quotes, fix this minor issue.

The problem is with certain build system, we use bazel at Google, where iquote is used for includes within the library, and other flavors outside the library. There's a similar conversation here:
https://groups.google.com/g/bazel-discuss/c/6MNuZ5bKoa8?pli=1

the failure you see when building is:

In file included from src/event-parse.c:27:
src/event-utils.h:13:10: error: 'event-parse.h' file not found with <angled> include; use "quotes" instead

#include <event-parse.h>
         ^~~~~~~~~~~~~~~
         "event-parse.h"

Arguably -iquote is more correct within the library build, hence bazel using it. The libtraceevent Makefile is using -I, hence this not being a problem for others. We prefer not to carry internal patches, hence wanting to send this upstream.

Thanks,
Ian

Quoted 4 lines

Note, the change log is empty, and is where rationale for the change is suppose to be added.

-- Steve

#10 lore raw

Re: [PATCH 2/2] libtraceevent: Changed angled brackets to double quotes.

Quoted 27 lines · On Mon, 14 Jun 2021 09:14:08 -0700 Ian Rogers <[email protected]> wrote:
Quoted 10 lines · Steven Rostedt
Quoted 3 lines · On Sun, 13 Jun 2021 14:31:42 -0700 Ian Rogers <[email protected]> wrote:

Would it be more conventional in this situation to use quotes within the library and angles outside? This change is within the library and not with code trying to link against it.

Does it matter? If not, what's the purpose of changing it?

Sorry for not having this in the body, in the cover message I added to Claire's email with:

Quoted 1 line

Some build systems are picky about angled vs quotes, fix this minor issue.

The problem is with certain build system, we use bazel at Google, where iquote is used for includes within the library, and other flavors outside the library. There's a similar conversation here:
https://groups.google.com/g/bazel-discuss/c/6MNuZ5bKoa8?pli=1

the failure you see when building is:

In file included from src/event-parse.c:27:
src/event-utils.h:13:10: error: 'event-parse.h' file not found with <angled> include; use "quotes" instead

Quoted 8 lines
#include <event-parse.h>
         ^~~~~~~~~~~~~~~
         "event-parse.h"

Arguably -iquote is more correct within the library build, hence bazel using it. The libtraceevent Makefile is using -I, hence this not being a problem for others. We prefer not to carry internal patches, hence wanting to send this upstream.

OK, context is very important :-)

With the above explanation, it makes sense, and I'm OK with taking it. Please resend with the above in the commit log.

Thanks!

-- Steve