Skip to contentSkip to conversation

tracing: Add *iter check for NULL

PATCH In review [email protected] 4 participants 5 messages Jun 1, 2016 – Jun 20, 2016 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: Add *iter check for NULL

From: xingzhen <[email protected]>

3debb0a9ddb adding a "__used" to the variable in the __trace_printk_fmt section. Sometimes it will cause *iter to be NULL, then strcmp in lookup_format and strcpy in hold_module_trace_bprintk_format will panic.

---
 kernel/trace/trace_printk.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_printk.c +3 -0
index f96f038..82ecfb5 100644
@@ -55,6 +55,8 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 
 	mutex_lock(&btrace_mutex);
 	for (iter = start; iter < end; iter++) {
+		if (!*iter)
+			goto err;
 		struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
 		if (tb_fmt) {
 			*iter = tb_fmt->fmt;
@@ -75,6 +77,7 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 		*iter = fmt;
 
 	}
+err:
 	mutex_unlock(&btrace_mutex);
 }
 
Signature · 1 line
1.9.1
Acked-by on v1 1/1
#1 lore raw

Hello,

Quoted 8 lines · On Wed, Jun 1, 2016 at 5:31 PM, <[email protected]> wrote:

From: xingzhen <[email protected]>

3debb0a9ddb adding a "__used" to the variable in the __trace_printk_fmt section. Sometimes it will cause *iter to be NULL, then strcmp in lookup_format and strcpy in hold_module_trace_bprintk_format will panic.

Thanks,
Namhyung

Quoted 28 lines
---
 kernel/trace/trace_printk.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_printk.c +3 -1
index f96f038..82ecfb5 100644
@@ -55,6 +55,8 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 
        mutex_lock(&btrace_mutex);
        for (iter = start; iter < end; iter++) {
+               if (!*iter)
+                       goto err;
                struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
                if (tb_fmt) {
                        *iter = tb_fmt->fmt;
@@ -75,6 +77,7 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
                *iter = fmt;
 
        }
+err:
        mutex_unlock(&btrace_mutex);
 }
 
--

1.9.1

maintainer
#2 lore raw
In reply to [email protected]
Quoted 6 lines · On Wed, 1 Jun 2016 16:31:10 +0800 [email protected] wrote:

From: xingzhen <[email protected]>

3debb0a9ddb adding a "__used" to the variable in the __trace_printk_fmt section. Sometimes it will cause *iter to be NULL, then strcmp in lookup_format and strcpy in hold_module_trace_bprintk_format will panic.

Could you show an example of this happening?

Quoted 11 lines
---
 kernel/trace/trace_printk.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_printk.c +0 -0
index f96f038..82ecfb5 100644

kernel/trace/trace_printk.c void hold_module_trace_bprintk_format(const char **start, const char **end) · line 60

Quoted 6 lines
kernel/trace/trace_printk.c +2 -0
@@ -55,6 +55,8 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 
 	mutex_lock(&btrace_mutex);
 	for (iter = start; iter < end; iter++) {
+		if (!*iter)
+			goto err;
 		struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);

First, you can't place logic before a declaration. Not all compilers will allow that.

Also, do we really want to error on this or just skip it? Because it will miss out on all other trace_printks in the module.

-- Steve

Quoted 10 lines
kernel/trace/trace_printk.c +1 -0
 		if (tb_fmt) {
 			*iter = tb_fmt->fmt;
@@ -75,6 +77,7 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 		*iter = fmt;
 
 	}
+err:
 	mutex_unlock(&btrace_mutex);
 }
 
maintainer
#3 lore raw
Quoted 11 lines · On Fri, 17 Jun 2016 12:38:41 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Wed, 1 Jun 2016 16:31:10 +0800 [email protected] wrote:

From: xingzhen <[email protected]>

3debb0a9ddb adding a "__used" to the variable in the __trace_printk_fmt section. Sometimes it will cause *iter to be NULL, then strcmp in lookup_format and strcpy in hold_module_trace_bprintk_format will panic.

Could you show an example of this happening?

Ha! While adding a trace_printk() test module (to test someone else's change) I triggered this bug.

Quoted 22 lines
Quoted 12 lines
---
 kernel/trace/trace_printk.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_printk.c +0 -0
index f96f038..82ecfb5 100644

kernel/trace/trace_printk.c void hold_module_trace_bprintk_format(const char **start, const char **end) · line 60

Quoted 6 lines
kernel/trace/trace_printk.c +2 -0
@@ -55,6 +55,8 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 
 	mutex_lock(&btrace_mutex);
 	for (iter = start; iter < end; iter++) {
+		if (!*iter)
+			goto err;
 		struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);

First, you can't place logic before a declaration. Not all compilers will allow that.

Quoted 2 lines

Also, do we really want to error on this or just skip it? Because it will miss out on all other trace_printks in the module.

I tried your patch and it works until you remove the module and try reading the trace again. As I said, you left out later processing. This should not exit on error. Below is a patch I wrote, and it works well.

I'll add you as reported by.

Thanks!

-- Steve

Quoted 7 lines
Quoted 7 lines
kernel/trace/trace_printk.c +0 -0
 		if (tb_fmt) {
 			*iter = tb_fmt->fmt;
@@ -75,6 +77,7 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 		*iter = fmt;
 
Quoted 6 lines

kernel/trace/trace_printk.c void hold_module_trace_bprintk_format(const char **start, const char **end) · line 83

Quoted 5 lines
kernel/trace/trace_printk.c +1 -0
 	}
+err:
 	mutex_unlock(&btrace_mutex);
 }
 
kernel/trace/trace_printk.c +6 -1
index f96f0383f6c6..ad1d6164e946 100644
@@ -36,6 +36,10 @@ struct trace_bprintk_fmt {
 static inline struct trace_bprintk_fmt *lookup_format(const char *fmt)
 {
 	struct trace_bprintk_fmt *pos;
+
+	if (!fmt)
+		return ERR_PTR(-EINVAL);
+
 	list_for_each_entry(pos, &trace_bprintk_fmt_list, list) {
 		if (!strcmp(pos->fmt, fmt))
 			return pos;
@@ -57,7 +61,8 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 	for (iter = start; iter < end; iter++) {
 		struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
 		if (tb_fmt) {
-			*iter = tb_fmt->fmt;
+			if (!IS_ERR(tb_fmt))
+				*iter = tb_fmt->fmt;
 			continue;
 		}
 
Acked-by
#4 lore raw
Quoted 7 lines · Steven Rostedt

I tried your patch and it works until you remove the module and try reading the trace again. As I said, you left out later processing. This should not exit on error. Below is a patch I wrote, and it works well.

I'll add you as reported by.

Thanks!

Thanks,
Namhyung

Quoted 25 lines
kernel/trace/trace_printk.c +6 -1
index f96f0383f6c6..ad1d6164e946 100644
@@ -36,6 +36,10 @@ struct trace_bprintk_fmt {
 static inline struct trace_bprintk_fmt *lookup_format(const char *fmt)
 {
 	struct trace_bprintk_fmt *pos;
+
+	if (!fmt)
+		return ERR_PTR(-EINVAL);
+
 	list_for_each_entry(pos, &trace_bprintk_fmt_list, list) {
 		if (!strcmp(pos->fmt, fmt))
 			return pos;
@@ -57,7 +61,8 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)
 	for (iter = start; iter < end; iter++) {
 		struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
 		if (tb_fmt) {
-			*iter = tb_fmt->fmt;
+			if (!IS_ERR(tb_fmt))
+				*iter = tb_fmt->fmt;
 			continue;
 		}