Skip to contentSkip to conversation

ftrace: Avoid double-free of dyn_ftrace

PATCH Applied Zhaolei 3 participants 7 messages Mar 13, 2009 – Mar 25, 2009

Subsystems (2)
lore
Share & tools
lore

Zhaolei

author
#0 lore raw

PATCH 1/1 ftrace: Avoid double-free of dyn_ftrace

If dyn_ftrace is free before ftrace_release(), ftrace_release() will free it again and make ftrace_free_records wrong.

---
 kernel/trace/ftrace.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/trace/ftrace.c +2 -1
index d33d306..26c45aa 100644
@@ -356,7 +356,8 @@ void ftrace_release(void *start, unsigned long size)
 
 	mutex_lock(&ftrace_lock);
 	do_for_each_ftrace_rec(pg, rec) {
-		if ((rec->ip >= s) && (rec->ip < e))
+		if ((rec->ip >= s) && (rec->ip < e) &&
+		    !(rec->flags & FTRACE_FL_FREE))
 			ftrace_free_rec(rec);
 	} while_for_each_ftrace_rec();
 	mutex_unlock(&ftrace_lock);
Signature · 1 line
1.5.5.3
Applied
#1 lore raw
Quoted 14 lines · Zhaolei

If dyn_ftrace is free before ftrace_release(),
ftrace_release() will free it again and make
ftrace_free_records wrong.

---
 kernel/trace/ftrace.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/trace/ftrace.c +0 -0
index d33d306..26c45aa 100644

kernel/trace/ftrace.c void ftrace_release(void *start, unsigned long size) · line 361

Quoted 7 lines
kernel/trace/ftrace.c +2 -1
@@ -356,7 +356,8 @@ void ftrace_release(void *start, unsigned long size)
 
 	mutex_lock(&ftrace_lock);
 	do_for_each_ftrace_rec(pg, rec) {
-		if ((rec->ip >= s) && (rec->ip < e))
+		if ((rec->ip >= s) && (rec->ip < e) &&
+		    !(rec->flags & FTRACE_FL_FREE))
 			ftrace_free_rec(rec);

Applied to tip:tracing/ftrace, thanks!

I'm wondering, did you trigger this in practice (if yes, how?), or have you found it via code review?

Ingo

author
#2 lore raw

* From: "Ingo Molnar" <[email protected]>

Quoted 27 lines · Zhaolei
Quoted 15 lines

If dyn_ftrace is free before ftrace_release(),
ftrace_release() will free it again and make
ftrace_free_records wrong.

---
 kernel/trace/ftrace.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/trace/ftrace.c +0 -0
index d33d306..26c45aa 100644

kernel/trace/ftrace.c void ftrace_release(void *start, unsigned long size) · line 361

Quoted 7 lines
kernel/trace/ftrace.c +2 -1
@@ -356,7 +356,8 @@ void ftrace_release(void *start, unsigned long size)
 
 mutex_lock(&ftrace_lock);
 do_for_each_ftrace_rec(pg, rec) {
- if ((rec->ip >= s) && (rec->ip < e))
+ if ((rec->ip >= s) && (rec->ip < e) &&
+     !(rec->flags & FTRACE_FL_FREE))
 ftrace_free_rec(rec);

Applied to tip:tracing/ftrace, thanks!

Quoted 2 lines

I'm wondering, did you trigger this in practice (if yes, how?), or have you found it via code review?

Hello, Ingo

It is found via code review.

B.R.
Zhaolei

Quoted 4 lines

Ingo

ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ¥Šwÿº{.nÇ+‰·¥Š{±þG«�éÿŠ{ayºʇڙë,j­¢f£¢·hš�ï�êÿ‘êçz_è®(­éšŽŠÝ¢j"�ú¶m§ÿÿ¾«þG«�éÿ¢¸?™¨è­Ú&£ø§~�á¶iO•æ¬z·švØ^¶m§ÿÿà ÿ¶ìÿ¢¸?–I¥

maintainer
#3 lore raw
Quoted 28 lines · Zhaolei

* From: "Ingo Molnar" <[email protected]>

Quoted 27 lines · Zhaolei
Quoted 15 lines

If dyn_ftrace is free before ftrace_release(),
ftrace_release() will free it again and make
ftrace_free_records wrong.

---
 kernel/trace/ftrace.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/trace/ftrace.c +0 -0
index d33d306..26c45aa 100644

kernel/trace/ftrace.c void ftrace_release(void *start, unsigned long size) · line 361

Quoted 7 lines
kernel/trace/ftrace.c +2 -1
@@ -356,7 +356,8 @@ void ftrace_release(void *start, unsigned long size)
 
 mutex_lock(&ftrace_lock);
 do_for_each_ftrace_rec(pg, rec) {
- if ((rec->ip >= s) && (rec->ip < e))
+ if ((rec->ip >= s) && (rec->ip < e) &&
+     !(rec->flags & FTRACE_FL_FREE))
 ftrace_free_rec(rec);

Applied to tip:tracing/ftrace, thanks!

Quoted 5 lines
Quoted 2 lines

I'm wondering, did you trigger this in practice (if yes, how?), or have you found it via code review?

Hello, Ingo

It is found via code review.

Hmm, could you explain this more. I'm thinking that this scenario should not happen, and if it does, it should probably be a bug.

Because when we call ftrace_free_rec we change the rec->ip to point to the next record in the chain. Something is very wrong if rec->ip >= s && rec->ip < e and the record is already free.

We can add a:

	WARN_ON(rec->flags & FTRACE_FL_FREE);

in ftrace_free_rec if you are worried about this happening.

-- Steve

author
#4 lore raw

* From: "Steven Rostedt" <[email protected]>

Quoted 34 lines · Zhaolei
Quoted 33 lines

* From: "Ingo Molnar" <[email protected]>

Quoted 27 lines · Zhaolei
Quoted 15 lines

If dyn_ftrace is free before ftrace_release(),
ftrace_release() will free it again and make
ftrace_free_records wrong.

---
 kernel/trace/ftrace.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/trace/ftrace.c +0 -0
index d33d306..26c45aa 100644

kernel/trace/ftrace.c void ftrace_release(void *start, unsigned long size) · line 361

Quoted 7 lines
kernel/trace/ftrace.c +2 -1
@@ -356,7 +356,8 @@ void ftrace_release(void *start, unsigned long size)
 
 mutex_lock(&ftrace_lock);
 do_for_each_ftrace_rec(pg, rec) {
- if ((rec->ip >= s) && (rec->ip < e))
+ if ((rec->ip >= s) && (rec->ip < e) &&
+     !(rec->flags & FTRACE_FL_FREE))
 ftrace_free_rec(rec);

Applied to tip:tracing/ftrace, thanks!

Quoted 2 lines

I'm wondering, did you trigger this in practice (if yes, how?), or have you found it via code review?

Hello, Ingo

Quoted 8 lines
Quoted 1 line

It is found via code review.

Hmm, could you explain this more. I'm thinking that this scenario should not happen, and if it does, it should probably be a bug.

Because when we call ftrace_free_rec we change the rec->ip to point to the next record in the chain. Something is very wrong if rec->ip >= s && rec->ip < e and the record is already free.

Hello, Steven

Thanks for your comment.
I got your meaning, and I agree that if rec->ip >= s && rec->ip < e, this record is not freed.
But IMHO, "if rec->ip >= s && rec->ip < e" is used to select records in the module, and function of ignore "freed record" is only its side-effect.
So, add a special judgement to avoid "freed record" is not a bad idea. And I also agree your suggestion of add a WARN_ON, because this should not happened.

B.R.
Zhaolei

Quoted 11 lines

We can add a:

WARN_ON(rec->flags & FTRACE_FL_FREE);

in ftrace_free_rec if you are worried about this happening.

-- Steve

ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ¥Šwÿº{.nÇ+‰·¥Š{±þG«�éÿŠ{ayºʇڙë,j­¢f£¢·hš�ï�êÿ‘êçz_è®(­éšŽŠÝ¢j"�ú¶m§ÿÿ¾«þG«�éÿ¢¸?™¨è­Ú&£ø§~�á¶iO•æ¬z·švØ^¶m§ÿÿà ÿ¶ìÿ¢¸?–I¥

maintainer
#5 lore raw
Quoted 15 lines · Zhaolei
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

I'm wondering, did you trigger this in practice (if yes, how?), or have you found it via code review?

Hello, Ingo

Quoted 8 lines
Quoted 1 line

It is found via code review.

Hmm, could you explain this more. I'm thinking that this scenario should not happen, and if it does, it should probably be a bug.

Because when we call ftrace_free_rec we change the rec->ip to point to the next record in the chain. Something is very wrong if rec->ip >= s && rec->ip < e and the record is already free.

Hello, Steven

Quoted 7 lines

Thanks for your comment.
I got your meaning, and I agree that if rec->ip >= s && rec->ip < e, this record is not freed.
But IMHO, "if rec->ip >= s && rec->ip < e" is used to select records in the module, and function of ignore "freed record" is only its side-effect.
So, add a special judgement to avoid "freed record" is not a bad idea. And I also agree your suggestion of add a WARN_ON, because this should not happened.

Hi Zhaolei,

Great! Feel free to send another patch ;-)

Note, use FTRACE_WARN_ON() macro. This way it shuts down ftrace if it is hit and helps to avoid further damage later.

-- Steve

author
#6 lore raw

[tip:tracing/ftrace] ftrace: avoid double-free of dyn_ftrace

In reply to Zhaolei
Commit-ID:  b00f0b6dc1773b4c8f538503247da050b5ea631b
Gitweb:     http://git.kernel.org/tip/b00f0b6dc1773b4c8f538503247da050b5ea631b
Author:     Zhaolei <[email protected]>
AuthorDate: Fri, 13 Mar 2009 17:14:01 +0800
Commit:     Ingo Molnar <[email protected]>
CommitDate: Fri, 13 Mar 2009 10:25:06 +0100

ftrace: avoid double-free of dyn_ftrace

If dyn_ftrace is freed before ftrace_release(), ftrace_release() will free it again and make ftrace_free_records wrong.

Cc "Steven Rostedt ;" <[email protected]>

LKML-Reference: <[email protected]>

---
 kernel/trace/ftrace.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/trace/ftrace.c +2 -1
index d33d306..26c45aa 100644
@@ -356,7 +356,8 @@ void ftrace_release(void *start, unsigned long size)
 
 	mutex_lock(&ftrace_lock);
 	do_for_each_ftrace_rec(pg, rec) {
-		if ((rec->ip >= s) && (rec->ip < e))
+		if ((rec->ip >= s) && (rec->ip < e) &&
+		    !(rec->flags & FTRACE_FL_FREE))
 			ftrace_free_rec(rec);
 	} while_for_each_ftrace_rec();
 	mutex_unlock(&ftrace_lock);