Skip to contentSkip to conversation

tracing: Fix the parser when processing strings w/ or w/o terminated '\0'

PATCH Superseded [email protected] 2 participants 6 messages Jan 15, 2018 – Jan 16, 2018

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover tracing: Fix the parser when processing strings w/ or w/o terminated '\0'

From: Changbin Du <[email protected]>

I found there are some problems in the tracing parser when I investiage the root cause of issues mentioned in below patch.
https://patchwork.kernel.org/patch/10132953/

This serials can fix them.

Changbin Du (3):
tracing: detect the string termination character when parsing user
input string
tracing: clear parser->idx if parser gets nothing
tracing: make sure the parsed string always terminates with '\0'

 kernel/trace/ftrace.c       |  2 --
 kernel/trace/trace.c        | 14 +++++++-------
 kernel/trace/trace_events.c |  2 --
 3 files changed, 7 insertions(+), 11 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/3 tracing: detect the string termination character when parsing user input string

From: Changbin Du <[email protected]>

The usersapce can give a '\0' terminated C string in the input buffer. Before this change, trace_get_user() will return a parsed string "\0" in below case which is not expected (expects it skip all inputs) and cause the caller failed.

open("/sys/kernel/debug/tracing//set_ftrace_pid", O_WRONLY|O_TRUNC) = 3
write(3, " \0", 2) = -1 EINVAL (Invalid argument)

while parse can handle spaces, so below works.

$ echo "" > set_ftrace_pid
$ echo " " > set_ftrace_pid
$ echo -n " " > set_ftrace_pid

This patch try to make the parser '\0' aware to fix such issue. When parser sees a '\0' it stops further parsing. With this change, write(3, " \0", 2) will work.

---
v2: Stop parsing when '\0' found.

---
 kernel/trace/trace.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/trace/trace.c +3 -3
index 2a8d8a2..144d08e 100644
@@ -1237,7 +1237,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 		}
 
 		/* only spaces were written */
-		if (isspace(ch)) {
+		if (isspace(ch) || !ch) {
 			*ppos += read;
 			ret = read;
 			goto out;
@@ -1247,7 +1247,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 	}
 
 	/* read the non-space input */
-	while (cnt && !isspace(ch)) {
+	while (cnt && !isspace(ch) && ch) {
 		if (parser->idx < parser->size - 1)
 			parser->buffer[parser->idx++] = ch;
 		else {
@@ -1262,7 +1262,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 	}
 
 	/* We either got finished input or we have to wait for another call. */
-	if (isspace(ch)) {
+	if (isspace(ch) || !ch) {
 		parser->buffer[parser->idx] = 0;
 		parser->cont = false;
 	} else if (parser->idx < parser->size - 1) {
Signature · 1 line
2.7.4
#4 lore raw

PATCH v2 2/3 tracing: clear parser->idx if parser gets nothing

In reply to [email protected]

From: Changbin Du <[email protected]>

If only spaces was got in that cycle, we should clear parser->idx to make trace_parser_loaded() return false.

---
 kernel/trace/trace.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace.c +2 -2
index 144d08e..b44926e 100644
@@ -1236,14 +1236,14 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 			cnt--;
 		}
 
+		parser->idx = 0;
+
 		/* only spaces were written */
 		if (isspace(ch) || !ch) {
 			*ppos += read;
 			ret = read;
 			goto out;
 		}
-
-		parser->idx = 0;
 	}
 
 	/* read the non-space input */
Signature · 1 line
2.7.4
#5 lore raw

PATCH v2 3/3 tracing: make sure the parsed string always terminates with '\0'

In reply to [email protected]

From: Changbin Du <[email protected]>

Always mark the parsed string with a terminated '\0' even parser expects another input to be parsed. Thus the users needn't append '0' before using parsed string if new input is not given.

---
 kernel/trace/ftrace.c       | 2 --
 kernel/trace/trace.c        | 4 ++--
 kernel/trace/trace_events.c | 2 --
 3 files changed, 2 insertions(+), 6 deletions(-)
kernel/trace/ftrace.c +0 -2
index ccdf366..3addb82 100644
@@ -5010,7 +5010,6 @@ int ftrace_regex_release(struct inode *inode, struct file *file)
 
 	parser = &iter->parser;
 	if (trace_parser_loaded(parser)) {
-		parser->buffer[parser->idx] = 0;
 		ftrace_match_records(iter->hash, parser->buffer, parser->idx);
 	}
 
@@ -5324,7 +5323,6 @@ ftrace_graph_release(struct inode *inode, struct file *file)
 		parser = &fgd->parser;
 
 		if (trace_parser_loaded((parser))) {
-			parser->buffer[parser->idx] = 0;
 			ret = ftrace_graph_set_hash(fgd->new_hash,
 						    parser->buffer);
 		}
kernel/trace/trace.c +2 -2
index b44926e..8f7fea2 100644
@@ -530,8 +530,6 @@ int trace_pid_write(struct trace_pid_list *filtered_pids,
 		ubuf += ret;
 		cnt -= ret;
 
-		parser.buffer[parser.idx] = 0;
-
 		ret = -EINVAL;
 		if (kstrtoul(parser.buffer, 0, &val))
 			break;
@@ -1268,6 +1266,8 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 	} else if (parser->idx < parser->size - 1) {
 		parser->cont = true;
 		parser->buffer[parser->idx++] = ch;
+		/* Make sure the parsed string always terminates with '\0'. */
+		parser->buffer[parser->idx] = 0;
 	} else {
 		ret = -EINVAL;
 		goto out;
kernel/trace/trace_events.c +0 -2
index ec0f9aa..7f8027c 100644
@@ -885,8 +885,6 @@ ftrace_event_write(struct file *file, const char __user *ubuf,
 		if (*parser.buffer == '!')
 			set = 0;
 
-		parser.buffer[parser.idx] = 0;
-
 		ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
 		if (ret)
 			goto out_put;
Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH v2 1/3] tracing: detect the string termination character when parsing user input string

In reply to [email protected]
Quoted 6 lines · On Mon, 15 Jan 2018 19:41:12 +0800 [email protected] wrote:

From: Changbin Du <[email protected]>

The usersapce can give a '\0' terminated C string in the input buffer. Before this change, trace_get_user() will return a parsed string "\0" in below case which is not expected (expects it skip all inputs) and cause the caller failed.

The above totally does not parse (no pun intended).

Are you trying to say:

"User space can pass in a C nul character '\0' along with its input. The function trace_get_user() will try to process it as a normal character, and that will fail to parse.

Quoted 6 lines

open("/sys/kernel/debug/tracing//set_ftrace_pid", O_WRONLY|O_TRUNC) = 3
write(3, " \0", 2) = -1 EINVAL (Invalid argument)

while parse can handle spaces, so below works.

Quoted 7 lines
$ echo "" > set_ftrace_pid
$ echo " " > set_ftrace_pid
$ echo -n " " > set_ftrace_pid

This patch try to make the parser '\0' aware to fix such issue. When parser sees a '\0' it stops further parsing. With this change, write(3, " \0", 2) will work.

The above should be something like:

"Have the parser stop on '\0' and cease any further parsing. Only process the characters up to the nul '\0' character and do not process it."

-- Steve

Quoted 40 lines

---
v2: Stop parsing when '\0' found.

---
 kernel/trace/trace.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/trace/trace.c +3 -3
index 2a8d8a2..144d08e 100644
@@ -1237,7 +1237,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 		}
 
 		/* only spaces were written */
-		if (isspace(ch)) {
+		if (isspace(ch) || !ch) {
 			*ppos += read;
 			ret = read;
 			goto out;
@@ -1247,7 +1247,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 	}
 
 	/* read the non-space input */
-	while (cnt && !isspace(ch)) {
+	while (cnt && !isspace(ch) && ch) {
 		if (parser->idx < parser->size - 1)
 			parser->buffer[parser->idx++] = ch;
 		else {
@@ -1262,7 +1262,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 	}
 
 	/* We either got finished input or we have to wait for another call. */
-	if (isspace(ch)) {
+	if (isspace(ch) || !ch) {
 		parser->buffer[parser->idx] = 0;
 		parser->cont = false;
 	} else if (parser->idx < parser->size - 1) {
#3 lore raw

Re: [PATCH v2 1/3] tracing: detect the string termination character when parsing user input string

Hi Rostedt,
Thanks for your polish, let me update commit msg with your words.

Quoted 82 lines · Steven Rostedt
Quoted 6 lines · On Mon, 15 Jan 2018 19:41:12 +0800 [email protected] wrote:

From: Changbin Du <[email protected]>

The usersapce can give a '\0' terminated C string in the input buffer. Before this change, trace_get_user() will return a parsed string "\0" in below case which is not expected (expects it skip all inputs) and cause the caller failed.

The above totally does not parse (no pun intended).

Are you trying to say:

"User space can pass in a C nul character '\0' along with its input. The function trace_get_user() will try to process it as a normal character, and that will fail to parse.

Quoted 6 lines

open("/sys/kernel/debug/tracing//set_ftrace_pid", O_WRONLY|O_TRUNC) = 3
write(3, " \0", 2) = -1 EINVAL (Invalid argument)

while parse can handle spaces, so below works.

Quoted 7 lines
$ echo "" > set_ftrace_pid
$ echo " " > set_ftrace_pid
$ echo -n " " > set_ftrace_pid

This patch try to make the parser '\0' aware to fix such issue. When parser sees a '\0' it stops further parsing. With this change, write(3, " \0", 2) will work.

The above should be something like:

"Have the parser stop on '\0' and cease any further parsing. Only process the characters up to the nul '\0' character and do not process it."

-- Steve

Quoted 40 lines

---
v2: Stop parsing when '\0' found.

---
 kernel/trace/trace.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/trace/trace.c +3 -3
index 2a8d8a2..144d08e 100644
@@ -1237,7 +1237,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 		}
 
 		/* only spaces were written */
-		if (isspace(ch)) {
+		if (isspace(ch) || !ch) {
 			*ppos += read;
 			ret = read;
 			goto out;
@@ -1247,7 +1247,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 	}
 
 	/* read the non-space input */
-	while (cnt && !isspace(ch)) {
+	while (cnt && !isspace(ch) && ch) {
 		if (parser->idx < parser->size - 1)
 			parser->buffer[parser->idx++] = ch;
 		else {
@@ -1262,7 +1262,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 	}
 
 	/* We either got finished input or we have to wait for another call. */
-	if (isspace(ch)) {
+	if (isspace(ch) || !ch) {
 		parser->buffer[parser->idx] = 0;
 		parser->cont = false;
 	} else if (parser->idx < parser->size - 1) {
Signature · 2 lines
Thanks,
Changbin Du