tracing: Have traceprobe_probes_write() not access userspace unnecessarily
Steven Rostedt
maintainerRFC PATCH 1/1 tracing: Have traceprobe_probes_write() not access userspace unnecessarily
The code in traceprobe_probes_write() reads up to 4096 bytes from userpace for each line. If userspace passes in several lines to execute, the code will do a large read for each line, even though, it is highly likely that the first read from userspace received all of the lines at one.
I changed the logic to do a single read from userspace, and to only read from userspace again if not all of the read from userspace made it in.
I tested this by adding printk()s and writing files that would test -1, ==, and +1 the buffer size, to make sure that there's no overflows and that if a single line is written with +1 the buffer size, that it fails properly.
--- kernel/trace/trace_probe.c | 48 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-)
kernel/trace/trace_probe.c +29 -19
@@ -647,7 +647,7 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos, int (*createfn)(int, char **)) { - char *kbuf, *tmp; + char *kbuf, *buf, *tmp;int ret =0 ;size_t done =0 ;size_t size;
@@ -667,27 +667,37 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer, goto out; } kbuf[size] ='\0' ; - tmp =strchr (kbuf,'\n' ); + buf = kbuf; +do { + tmp =strchr (buf,'\n' ); +if (tmp) { + *tmp ='\0' ; + size = tmp - buf +1 ; + }else { + size =strlen (buf); +if (done + size < count) { +if (buf != kbuf) +break ; +pr_warn ("Line length is too long: Should be less than %d\n" , +WRITE_BUFSIZE ); + ret = -EINVAL ; + goto out; + } + } + done += size; -if (tmp) { - *tmp ='\0' ; - size = tmp - kbuf +1 ; - }else if (done + size < count) { -pr_warn ("Line length is too long: Should be less than %d\n" , -WRITE_BUFSIZE ); - ret = -EINVAL ; - goto out; - } - done += size; -/* Remove comments */ - tmp =strchr (kbuf,'#' ); +/* Remove comments */ + tmp =strchr (buf,'#' ); -if (tmp) - *tmp ='\0' ; +if (tmp) + *tmp ='\0' ; - ret =traceprobe_command (kbuf, createfn); -if (ret) - goto out; + ret =traceprobe_command (buf, createfn); +if (ret) + goto out; + buf += size; + + }while (done < count); } ret = done;
Signature · 1 line
2.9.3
Quoted 6 lines · On Thu, 9 Feb 2017 18:04:58 -0500 Steven Rostedt <[email protected]> wrote:
The code in
traceprobe_probes_write()reads up to 4096 bytes from userpace for each line. If userspace passes in several lines to execute, the code will do a large read for each line, even though, it is highly likely that the first read from userspace received all of the lines at one.
Quoted 7 lines
I changed the logic to do a single read from userspace, and to only read from userspace again if not all of the read from userspace made it in.
I tested this by adding printk()s and writing files that would test -1, ==, and +1 the buffer size, to make sure that there's no overflows and that if a single line is written with +1 the buffer size, that it fails properly.
Thanks Steve!
BTW, this can conflict with my previous patch.
https://lkml.org/lkml/2017/2/6/1048
https://lkml.org/lkml/2017/2/7/203
I'll update this. Ingo, Can I send these patch to Steve?
Thank you,
Quoted 77 lines
Signed-off-by Steven Rostedt (VMware) <[email protected]>--- kernel/trace/trace_probe.c | 48 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-)kernel/trace/trace_probe.c +29
-19@@ -647,7 +647,7 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos, int (*createfn)(int, char **)) { - char *kbuf, *tmp; + char *kbuf, *buf, *tmp;int ret =0 ;size_t done =0 ;size_t size;@@ -667,27 +667,37 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer, goto out; } kbuf[size] ='\0' ; - tmp =strchr (kbuf,'\n' ); + buf = kbuf; +do { + tmp =strchr (buf,'\n' ); +if (tmp) { + *tmp ='\0' ; + size = tmp - buf +1 ; + }else { + size =strlen (buf); +if (done + size < count) { +if (buf != kbuf) +break ; +pr_warn ("Line length is too long: Should be less than %d\n" , +WRITE_BUFSIZE ); + ret = -EINVAL ; + goto out; + } + } + done += size; -if (tmp) { - *tmp ='\0' ; - size = tmp - kbuf +1 ; - }else if (done + size < count) { -pr_warn ("Line length is too long: Should be less than %d\n" , -WRITE_BUFSIZE ); - ret = -EINVAL ; - goto out; - } - done += size; -/* Remove comments */ - tmp =strchr (kbuf,'#' ); +/* Remove comments */ + tmp =strchr (buf,'#' ); -if (tmp) - *tmp ='\0' ; +if (tmp) + *tmp ='\0' ; - ret =traceprobe_command (kbuf, createfn); -if (ret) - goto out; + ret =traceprobe_command (buf, createfn); +if (ret) + goto out; + buf += size; + + }while (done < count); } ret = done;Signature · 1 line
2.9.3
Signature · 1 line
Masami Hiramatsu <[email protected]>
Quoted 19 lines · Masami Hiramatsu
Quoted 6 lines · On Thu, 9 Feb 2017 18:04:58 -0500 Steven Rostedt <[email protected]> wrote:
The code in
traceprobe_probes_write()reads up to 4096 bytes from userpace for each line. If userspace passes in several lines to execute, the code will do a large read for each line, even though, it is highly likely that the first read from userspace received all of the lines at one.Quoted 7 lines
I changed the logic to do a single read from userspace, and to only read from userspace again if not all of the read from userspace made it in.
I tested this by adding printk()s and writing files that would test -1, ==, and +1 the buffer size, to make sure that there's no overflows and that if a single line is written with +1 the buffer size, that it fails properly.
Thanks Steve!
Quoted 8 lines
Acked-by Masami Hiramatsu <[email protected]>BTW, this can conflict with my previous patch.
https://lkml.org/lkml/2017/2/6/1048
https://lkml.org/lkml/2017/2/7/203I'll update this. Ingo, Can I send these patch to Steve?
Sure, I've not applied your patch yet - mind sending it to Steve on top of Steve's patch?
Thanks,
Ingo
Masami Hiramatsu
maintainerQuoted 26 lines · On Fri, 10 Feb 2017 08:53:02 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 23 lines · Masami Hiramatsu
Quoted 6 lines · On Thu, 9 Feb 2017 18:04:58 -0500 Steven Rostedt <[email protected]> wrote:
The code in
traceprobe_probes_write()reads up to 4096 bytes from userpace for each line. If userspace passes in several lines to execute, the code will do a large read for each line, even though, it is highly likely that the first read from userspace received all of the lines at one.Quoted 7 lines
I changed the logic to do a single read from userspace, and to only read from userspace again if not all of the read from userspace made it in.
I tested this by adding printk()s and writing files that would test -1, ==, and +1 the buffer size, to make sure that there's no overflows and that if a single line is written with +1 the buffer size, that it fails properly.
Thanks Steve!
Acked-by Masami Hiramatsu <[email protected]>BTW, this can conflict with my previous patch.
Quoted 7 lines
Quoted 4 lines
https://lkml.org/lkml/2017/2/6/1048
https://lkml.org/lkml/2017/2/7/203I'll update this. Ingo, Can I send these patch to Steve?
Sure, I've not applied your patch yet - mind sending it to Steve on top of Steve's patch?
Of course, yes. :)
Thanks!
Quoted 4 lines
Thanks,
Ingo
Signature · 1 line
Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerQuoted 5 lines · On Fri, 10 Feb 2017 19:37:53 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 2 lines
Sure, I've not applied your patch yet - mind sending it to Steve on top of Steve's patch?
Of course, yes. :)
Thanks! I'll apply them on top of mine then.
-- Steve
Masami Hiramatsu
maintainerPATCH v2 1/2 tracing/probes: Fix a warning message to show correct maximum length
In reply to Ingo MolnarSince tracing/*probe_events will accept a probe definition up to 4096 - 2 ('\n' and '\0') bytes, it must show 4094 instead of 4096 in warning message.
Note that there is one possible case of exceed 4094. If user prepare 4096 bytes null-terminated string and syscall write it with the count == 4095, then it can be accepted. However, if user puts a '\n' after that, it must rejected. So IMHO, the warning message should indicate shorter one, since it is safer.
--- kernel/trace/trace_probe.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_probe.c +2 -1
@@ -678,8 +678,9 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,if (done + size < count) {if (buf != kbuf)break ; +/* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */ pr_warn ("Line length is too long: Should be less than %d\n" , -WRITE_BUFSIZE ); +WRITE_BUFSIZE -2 ); ret = -EINVAL ; goto out; }
Steven Rostedt
maintainerRe: [PATCH V2 1/2] tracing/probes: Fix a warning message to show correct maximum length
Quoted 3 lines · On Fri, 10 Feb 2017 22:21:55 +0900 Masami Hiramatsu <[email protected]> wrote:
Since tracing/*probe_events will accept a probe definition up to 4096 - 2 ('\n' and '\0') bytes, it must show 4094 instead of 4096 in warning message.
Actually, during the testing I found that we don't need the '\n'.
echo -n 'p:irq do_IRQ a=@jiffies_64' > kprobe_events
works just fine. My tests work with 4095 characters. Before and after my patch.
-- Steve
Quoted 28 lines
Note that there is one possible case of exceed 4094. If user prepare 4096 bytes null-terminated string and syscall write it with the count == 4095, then it can be accepted. However, if user puts a '\n' after that, it must rejected. So IMHO, the warning message should indicate shorter one, since it is safer.
Signed-off-by Masami Hiramatsu <[email protected]>--- kernel/trace/trace_probe.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/trace/trace_probe.c +2
-1@@ -678,8 +678,9 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,if (done + size < count) {if (buf != kbuf)break ; +/* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */ pr_warn ("Line length is too long: Should be less than %d\n" , -WRITE_BUFSIZE ); +WRITE_BUFSIZE -2 ); ret = -EINVAL ; goto out; }
Masami Hiramatsu
maintainerRe: [PATCH V2 1/2] tracing/probes: Fix a warning message to show correct maximum length
Quoted 7 lines · On Fri, 10 Feb 2017 11:04:34 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 3 lines · On Fri, 10 Feb 2017 22:21:55 +0900 Masami Hiramatsu <[email protected]> wrote:
Since tracing/*probe_events will accept a probe definition up to 4096 - 2 ('\n' and '\0') bytes, it must show 4094 instead of 4096 in warning message.
Quoted 6 lines
Actually, during the testing I found that we don't need the '\n'.
echo -n 'p:irq
do_IRQa=@jiffies_64' >kprobe_eventsworks just fine. My tests work with 4095 characters. Before and after my patch.
Yeah, I see. I concider the case that if the writer writes 4095 chars command + '\n' + next command, it will fail, that is what I pointed out below note.
Quoted 7 lines
Quoted 7 lines
Note that there is one possible case of exceed 4094. If user prepare 4096 bytes null-terminated string and syscall write it with the count == 4095, then it can be accepted. However, if user puts a '\n' after that, it must rejected. So IMHO, the warning message should indicate shorter one, since it is safer.
Thank you,
Quoted 22 lines
Quoted 21 lines
Signed-off-by Masami Hiramatsu <[email protected]>--- kernel/trace/trace_probe.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/trace/trace_probe.c +2
-1@@ -678,8 +678,9 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,if (done + size < count) {if (buf != kbuf)break ; +/* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */ pr_warn ("Line length is too long: Should be less than %d\n" , -WRITE_BUFSIZE ); +WRITE_BUFSIZE -2 ); ret = -EINVAL ; goto out; }
Signature · 1 line
Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerRe: [PATCH V2 1/2] tracing/probes: Fix a warning message to show correct maximum length
Quoted 22 lines · On Thu, 16 Feb 2017 00:10:30 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 7 lines · On Fri, 10 Feb 2017 11:04:34 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 3 lines · On Fri, 10 Feb 2017 22:21:55 +0900 Masami Hiramatsu <[email protected]> wrote:
Since tracing/*probe_events will accept a probe definition up to 4096 - 2 ('\n' and '\0') bytes, it must show 4094 instead of 4096 in warning message.
Quoted 6 lines
Actually, during the testing I found that we don't need the '\n'.
echo -n 'p:irq
do_IRQa=@jiffies_64' >kprobe_eventsworks just fine. My tests work with 4095 characters. Before and after my patch.
Yeah, I see. I concider the case that if the writer writes 4095 chars command + '\n' + next command, it will fail, that is what I pointed out below note.
Quoted 1 line
Quoted 1 line
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Note that there is one possible case of exceed 4094. If user prepare 4096 bytes null-terminated string and syscall write it with the count == 4095, then it can be accepted. However, if user puts a '\n' after that, it must rejected. So IMHO, the warning message should indicate shorter one, since it is safer.
Thank you,
OK, I'll just add the patch as is.
Thanks,
-- Steve
Masami Hiramatsu
maintainerPATCH v2 2/2 tracing/probe: Show subsystem name in messages
In reply to Ingo MolnarShow "trace_probe:", "trace_kprobe:" and "trace_uprobe:" headers for each warning/error/info message. This will help people to notice that kprobe/uprobe events caused those messages.
--- kernel/trace/trace_kprobe.c | 1 + kernel/trace/trace_probe.c | 1 + kernel/trace/trace_uprobe.c | 1 + 3 files changed, 3 insertions(+)
kernel/trace/trace_kprobe.c +1 -0
@@ -16,6 +16,7 @@* along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define pr_fmt (fmt) "trace_kprobe: " fmt#include <linux/module.h> #include <linux/uaccess.h>
kernel/trace/trace_probe.c +1 -0
@@ -21,6 +21,7 @@* Copyright (C) IBM Corporation, 2010-2011 * Author: Srikar Dronamraju */ +#define pr_fmt (fmt) "trace_probe: " fmt#include "trace_probe.h"
kernel/trace/trace_uprobe.c +1 -0
@@ -17,6 +17,7 @@* Copyright (C) IBM Corporation, 2010-2012 * Author: Srikar Dronamraju <[email protected]> */ +#define pr_fmt (fmt) "trace_kprobe: " fmt#include <linux/module.h> #include <linux/uaccess.h>
Re: [PATCH V2 2/2] tracing/probe: Show subsystem name in messages
Quoted 41 lines · Masami Hiramatsu
Show "
trace_probe:", "trace_kprobe:" and "trace_uprobe:" headers for each warning/error/info message. This will help people to notice that kprobe/uprobe events caused those messages.Signed-off-by Masami Hiramatsu <[email protected]>--- kernel/trace/trace_kprobe.c | 1 + kernel/trace/trace_probe.c | 1 + kernel/trace/trace_uprobe.c | 1 + 3 files changed, 3 insertions(+)kernel/trace/trace_kprobe.c +1
-0@@ -16,6 +16,7 @@* along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define pr_fmt (fmt) "trace_kprobe: " fmt#include <linux/module.h> #include <linux/uaccess.h> kernel/trace/trace_probe.c +1
-0@@ -21,6 +21,7 @@* Copyright (C) IBM Corporation, 2010-2011 * Author: Srikar Dronamraju */ +#define pr_fmt (fmt) "trace_probe: " fmt#include "trace_probe.h" kernel/trace/trace_uprobe.c +0
-0
kernel/trace/trace_uprobe.c line 20
Quoted 4 lines
kernel/trace/trace_uprobe.c +1
-0@@ -17,6 +17,7 @@* Copyright (C) IBM Corporation, 2010-2012 * Author: Srikar Dronamraju <[email protected]> */ +#define pr_fmt (fmt) "trace_kprobe: " fmt
s/kprobe/uprobe/ ?
Thanks,
Namhyung
Quoted 4 lines
kernel/trace/trace_uprobe.c +0
-0#include <linux/module.h> #include <linux/uaccess.h>
Steven Rostedt
maintainerRe: [PATCH V2 2/2] tracing/probe: Show subsystem name in messages
Quoted 11 lines · On Sat, 11 Feb 2017 01:01:55 +0900 Namhyung Kim <[email protected]> wrote:
kernel/trace/trace_uprobe.c line 20
Quoted 9 lines
kernel/trace/trace_uprobe.c +1
-0@@ -17,6 +17,7 @@* Copyright (C) IBM Corporation, 2010-2012 * Author: Srikar Dronamraju <[email protected]> */ +#define pr_fmt (fmt) "trace_kprobe: " fmts/kprobe/uprobe/ ?
Masami,
Can you update both patches and resend?
-- Steve
Masami Hiramatsu
maintainerRe: [PATCH V2 2/2] tracing/probe: Show subsystem name in messages
Quoted 15 lines · On Fri, 10 Feb 2017 11:17:29 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 11 lines · On Sat, 11 Feb 2017 01:01:55 +0900 Namhyung Kim <[email protected]> wrote:
kernel/trace/trace_uprobe.c line 20
Quoted 9 lines
kernel/trace/trace_uprobe.c +1
-0@@ -17,6 +17,7 @@* Copyright (C) IBM Corporation, 2010-2012 * Author: Srikar Dronamraju <[email protected]> */ +#define pr_fmt (fmt) "trace_kprobe: " fmts/kprobe/uprobe/ ?
Quoted 3 lines
Masami,
Can you update both patches and resend?
Oops! OK.
Thanks,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerPATCH v3 1/1 tracing/probe: Show subsystem name in messages
In reply to Steven RostedtShow "trace_probe:", "trace_kprobe:" and "trace_uprobe:" headers for each warning/error/info message. This will help people to notice that kprobe/uprobe events caused those messages.
--
Changes in v3:
- Fix a typo.
--- kernel/trace/trace_kprobe.c | 1 + kernel/trace/trace_probe.c | 1 + kernel/trace/trace_uprobe.c | 1 + 3 files changed, 3 insertions(+)
kernel/trace/trace_kprobe.c +1 -0
@@ -16,6 +16,7 @@* along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define pr_fmt (fmt) "trace_kprobe: " fmt#include <linux/module.h> #include <linux/uaccess.h>
kernel/trace/trace_probe.c +1 -0
@@ -21,6 +21,7 @@* Copyright (C) IBM Corporation, 2010-2011 * Author: Srikar Dronamraju */ +#define pr_fmt (fmt) "trace_probe: " fmt#include "trace_probe.h"
kernel/trace/trace_uprobe.c +1 -0
@@ -17,6 +17,7 @@* Copyright (C) IBM Corporation, 2010-2012 * Author: Srikar Dronamraju <[email protected]> */ +#define pr_fmt (fmt) "trace_uprobe: " fmt#include <linux/module.h> #include <linux/uaccess.h>
Quoted 6 lines · Steven Rostedt
The code in
traceprobe_probes_write()reads up to 4096 bytes from userpace for each line. If userspace passes in several lines to execute, the code will do a large read for each line, even though, it is highly likely that the first read from userspace received all of the lines at one.
Quoted 8 lines
I changed the logic to do a single read from userspace, and to only read from userspace again if not all of the read from userspace made it in.
I tested this by adding printk()s and writing files that would test -1, ==, and +1 the buffer size, to make sure that there's no overflows and that if a single line is written with +1 the buffer size, that it fails properly.
Signed-off-by Steven Rostedt (VMware) <[email protected]>
Thanks,
Namhyung
Quoted 76 lines
--- kernel/trace/trace_probe.c | 48 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-)kernel/trace/trace_probe.c +29
-19@@ -647,7 +647,7 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos, int (*createfn)(int, char **)) { - char *kbuf, *tmp; + char *kbuf, *buf, *tmp;int ret =0 ;size_t done =0 ;size_t size;@@ -667,27 +667,37 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer, goto out; } kbuf[size] ='\0' ; - tmp =strchr (kbuf,'\n' ); + buf = kbuf; +do { + tmp =strchr (buf,'\n' ); +if (tmp) { + *tmp ='\0' ; + size = tmp - buf +1 ; + }else { + size =strlen (buf); +if (done + size < count) { +if (buf != kbuf) +break ; +pr_warn ("Line length is too long: Should be less than %d\n" , +WRITE_BUFSIZE ); + ret = -EINVAL ; + goto out; + } + } + done += size; -if (tmp) { - *tmp ='\0' ; - size = tmp - kbuf +1 ; - }else if (done + size < count) { -pr_warn ("Line length is too long: Should be less than %d\n" , -WRITE_BUFSIZE ); - ret = -EINVAL ; - goto out; - } - done += size; -/* Remove comments */ - tmp =strchr (kbuf,'#' ); +/* Remove comments */ + tmp =strchr (buf,'#' ); -if (tmp) - *tmp ='\0' ; +if (tmp) + *tmp ='\0' ; - ret =traceprobe_command (kbuf, createfn); -if (ret) - goto out; + ret =traceprobe_command (buf, createfn); +if (ret) + goto out; + buf += size; + + }while (done < count); } ret = done;Signature · 1 line
2.9.3