Skip to contentSkip to conversation

perf ftrace: Fix the buffer size in __write_tracing_file

PATCH Superseded [email protected] 3 participants 5 messages Dec 26, 2017 – Jan 9, 2018 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf ftrace: Fix the buffer size in __write_tracing_file

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account as size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist.

I didn't dive into kernel ftrace fops, but strace shows that if usersapce writes a non-terminated string, the kernel side will return success but no filter applied. After this fix in userspace, the kernel will return an error.

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled.

---
 tools/perf/builtin-ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
index 25a42ac..2604a64 100644
@@ -69,7 +69,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
 {
 	char *file;
 	int fd, ret = -1;
-	ssize_t size = strlen(val);
+	ssize_t size = strlen(val) + 1;
 	int flags = O_WRONLY;
 	char errbuf[512];
 
Signature · 1 line
2.7.4
#1 lore raw

Hi Olsa,
What about this fix now? Thanks!

Quoted 47 lines · On Tue, Dec 26, 2017 at 05:26:56PM +0800, [email protected] wrote:

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account as size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist.

I didn't dive into kernel ftrace fops, but strace shows that if usersapce writes a non-terminated string, the kernel side will return success but no filter applied. After this fix in userspace, the kernel will return an error.

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled.

---
 tools/perf/builtin-ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
index 25a42ac..2604a64 100644
@@ -69,7 +69,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
 {
 	char *file;
 	int fd, ret = -1;
-	ssize_t size = strlen(val);
+	ssize_t size = strlen(val) + 1;
 	int flags = O_WRONLY;
 	char errbuf[512];
 
Signature · 1 line
2.7.4
Signature · 2 lines
Thanks,
Changbin Du
#2 lore raw
Quoted 29 lines · Changbin

Hi Olsa,
What about this fix now? Thanks!

Quoted 25 lines · On Tue, Dec 26, 2017 at 05:26:56PM +0800, [email protected] wrote:

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account as size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist.

I didn't dive into kernel ftrace fops, but strace shows that if usersapce writes a non-terminated string, the kernel side will return success but no filter applied. After this fix in userspace, the kernel will return an error.

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled.

hum, haven't checked, but looks like the filter is not working at all now:

[root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs proc_sys_read write ' ' to tracing/set_ftrace_pid failed: Invalid argument [root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs SyS_read write ' ' to tracing/set_ftrace_pid failed: Invalid argument [root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs fsnotify write ' ' to tracing/set_ftrace_pid failed: Invalid argument

jirka

Quoted 26 lines
Quoted 22 lines
---
 tools/perf/builtin-ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
index 25a42ac..2604a64 100644
@@ -69,7 +69,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
 {
 	char *file;
 	int fd, ret = -1;
-	ssize_t size = strlen(val);
+	ssize_t size = strlen(val) + 1;
 	int flags = O_WRONLY;
 	char errbuf[512];
 
Signature · 1 line
2.7.4
Signature · 2 lines
Thanks,
Changbin Du
#3 lore raw
Quoted 31 lines · Jiri Olsa
Quoted 29 lines · Changbin

Hi Olsa,
What about this fix now? Thanks!

Quoted 25 lines · On Tue, Dec 26, 2017 at 05:26:56PM +0800, [email protected] wrote:

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account as size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist.

I didn't dive into kernel ftrace fops, but strace shows that if usersapce writes a non-terminated string, the kernel side will return success but no filter applied. After this fix in userspace, the kernel will return an error.

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled.

Quoted 9 lines

hum, haven't checked, but looks like the filter is not working at all now:

[root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs proc_sys_read write ' ' to tracing/set_ftrace_pid failed: Invalid argument [root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs SyS_read write ' ' to tracing/set_ftrace_pid failed: Invalid argument [root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs fsnotify write ' ' to tracing/set_ftrace_pid failed: Invalid argument

Thanks for your test. I forgot to test normal case and thought the err is expected...

This time I dived into kernel side, and found 3 issues (if I am all right) at the kernel function trace_get_user(). This function has problems to process both complete C string or not.

I will send the kernel patches and Cc you guys. And I still think it is better let perf write a complete C string.

Thanks!
Changbin Du

Quoted 2 lines

jirka

[...]

maintainer
#4 lore raw
In reply to Jiri Olsa

Hi,

Quoted 31 lines · Jiri Olsa
Quoted 29 lines · Changbin

Hi Olsa,
What about this fix now? Thanks!

Quoted 25 lines · On Tue, Dec 26, 2017 at 05:26:56PM +0800, [email protected] wrote:

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account as size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist.

I didn't dive into kernel ftrace fops, but strace shows that if usersapce writes a non-terminated string, the kernel side will return success but no filter applied. After this fix in userspace, the kernel will return an error.

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled.

Quoted 8 lines

hum, haven't checked, but looks like the filter is not working at all now:

[root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs proc_sys_read write ' ' to tracing/set_ftrace_pid failed: Invalid argument [root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs SyS_read write ' ' to tracing/set_ftrace_pid failed: Invalid argument [root@krava perf]# ./perf ftrace -vv -a --graph-depth 1 --graph-funcs fsnotify write ' ' to tracing/set_ftrace_pid failed: Invalid argument

Hmm.. it seems writing a whitespace alone caused the failure.

The filter files are handled little bit different in that they process the given string when it find a whitespace or at the close() if not. But the thing is that it's gonna lose the return value if handled at close().

Anyway adding a NUL character at the end won't make different IMHO since it's not a whitespace. And I think it needs a separate function to set filters with a whitespace. But this will change the program behavior on invalid filter inputs.

Thanks,
Namhyung