Skip to contentSkip to conversation

trace: Avoid memory leak in predicate_parse()

PATCH In review Tomas Bortoli 2 participants 10 messages May 28, 2019 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trace: Avoid memory leak in predicate_parse()

In case of errors, predicate_parse() goes to the out_free label to free memory and to return an error code.

However, predicate_parse() does not free the predicates of the temporary prog_stack array, thence leaking them.

---
 kernel/trace/trace_events_filter.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/trace/trace_events_filter.c +4 -0
index d3e59312ef40..98eafad750d3 100644
@@ -433,6 +433,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 		parse_error(pe, -ENOMEM, 0);
 		goto out_free;
 	}
+	memset(prog_stack, 0, nr_preds * sizeof(*prog_stack));
+
 	inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
 	if (!inverts) {
 		parse_error(pe, -ENOMEM, 0);
@@ -579,6 +581,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);
 	kfree(inverts);
+	for (i = 0; prog_stack[i].pred; i++)
+		kfree(prog_stack[i].pred);
 	kfree(prog_stack);
 	return ERR_PTR(ret);
 }
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 18 lines · On Tue, 28 May 2019 15:46:59 +0200 Tomas Bortoli <[email protected]> wrote:

In case of errors, predicate_parse() goes to the out_free label to free memory and to return an error code.

However, predicate_parse() does not free the predicates of the temporary prog_stack array, thence leaking them.

---
 kernel/trace/trace_events_filter.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/trace/trace_events_filter.c +0 -0
index d3e59312ef40..98eafad750d3 100644

kernel/trace/trace_events_filter.c predicate_parse(const char *str, int nr_parens, int nr_preds, · line 437

Quoted 5 lines
kernel/trace/trace_events_filter.c +2 -0
@@ -433,6 +433,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 		parse_error(pe, -ENOMEM, 0);
 		goto out_free;
 	}
+	memset(prog_stack, 0, nr_preds * sizeof(*prog_stack));
+

Can you instead just switch the allocation of prog_stack to use kcalloc()?

Thanks,

-- Steve

Quoted 12 lines
kernel/trace/trace_events_filter.c +2 -0
 	inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
 	if (!inverts) {
 		parse_error(pe, -ENOMEM, 0);
@@ -579,6 +581,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);
 	kfree(inverts);
+	for (i = 0; prog_stack[i].pred; i++)
+		kfree(prog_stack[i].pred);
 	kfree(prog_stack);
 	return ERR_PTR(ret);
 }
#2 lore raw
Quoted 16 lines · Steven Rostedt
Quoted 13 lines · On Tue, 28 May 2019 15:46:59 +0200 Tomas Bortoli <[email protected]> wrote:

In case of errors, predicate_parse() goes to the out_free label to free memory and to return an error code.

However, predicate_parse() does not free the predicates of the temporary prog_stack array, thence leaking them.

---
 kernel/trace/trace_events_filter.c | 4 ++++
 1 file changed, 4 insertions(+)
Quoted 13 lines

kernel/trace/trace_events_filter.c predicate_parse(const char *str, int nr_parens, int nr_preds, · line 437

Quoted 10 lines
kernel/trace/trace_events_filter.c +2 -0
index d3e59312ef40..98eafad750d3 100644
@@ -433,6 +433,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 		parse_error(pe, -ENOMEM, 0);
 		goto out_free;
 	}
+	memset(prog_stack, 0, nr_preds * sizeof(*prog_stack));
+

Can you instead just switch the allocation of prog_stack to use kcalloc()?

kmalloc_array() is safe against arithmetic overflow of the arguments. Using kcalloc() directly we wouldn't check for that. Not really ideal in my opinion. And there's no kcalloc_array() apparently!

Cheers,
Tomas

maintainer
#3 lore raw
Quoted 9 lines · On Tue, 28 May 2019 17:18:59 +0200 Tomas Bortoli <[email protected]> wrote:
Quoted 5 lines

kernel/trace/trace_events_filter.c predicate_parse(const char *str, int nr_parens, int nr_preds, · line 437

Quoted 2 lines
kernel/trace/trace_events_filter.c +2 -0
+	memset(prog_stack, 0, nr_preds * sizeof(*prog_stack));
+  

Can you instead just switch the allocation of prog_stack to use kcalloc()?

kmalloc_array() is safe against arithmetic overflow of the arguments. Using kcalloc() directly we wouldn't check for that. Not really ideal in my opinion. And there's no kcalloc_array() apparently!

But doesn't kcalloc() simply call kmalloc_array() with the GFP_ZERO flag?

-- Steve

#4 lore raw
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Tue, 28 May 2019 17:18:59 +0200 Tomas Bortoli <[email protected]> wrote:
Quoted 5 lines

kernel/trace/trace_events_filter.c predicate_parse(const char *str, int nr_parens, int nr_preds, · line 437

Quoted 2 lines
kernel/trace/trace_events_filter.c +2 -0
+	memset(prog_stack, 0, nr_preds * sizeof(*prog_stack));
+  

Can you instead just switch the allocation of prog_stack to use kcalloc()?

Quoted 7 lines
Quoted 3 lines

kmalloc_array() is safe against arithmetic overflow of the arguments. Using kcalloc() directly we wouldn't check for that. Not really ideal in my opinion. And there's no kcalloc_array() apparently!

But doesn't kcalloc() simply call kmalloc_array() with the GFP_ZERO flag?

It does! Oops, I'll send it shortly

Tomas

#5 lore raw

PATCH 1/1 trace: Avoid memory leak in predicate_parse()

In reply to Steven Rostedt

In case of errors, predicate_parse() goes to the out_free label to free memory and to return an error code.

However, predicate_parse() does not free the predicates of the temporary prog_stack array, thence leaking them.

---
 kernel/trace/trace_events_filter.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/trace_events_filter.c +3 -1
index 05a66493a164..ecfa6f0f1c7e 100644
@@ -427,7 +427,7 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 	op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
 	if (!op_stack)
 		return ERR_PTR(-ENOMEM);
-	prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
+	prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
 	if (!prog_stack) {
 		parse_error(pe, -ENOMEM, 0);
 		goto out_free;
@@ -578,6 +578,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);
 	kfree(inverts);
+	for (i = 0; prog_stack[i].pred; i++)
+		kfree(prog_stack[i].pred);
 	kfree(prog_stack);
 	return ERR_PTR(ret);
 }
Signature · 1 line
2.11.0
maintainer
#6 lore raw
Quoted 5 lines · On Tue, 28 May 2019 17:43:38 +0200 Tomas Bortoli <[email protected]> wrote:

In case of errors, predicate_parse() goes to the out_free label to free memory and to return an error code.

However, predicate_parse() does not free the predicates of the temporary prog_stack array, thence leaking them.

Thanks, I applied this and I'm running it through my tests. But just an FYI, when sending updated patches please add a "v2" to the subject:

[PATCH v2] tracing: Avoid memory leak in predicate_parse()

That way struggling maintainers like myself don't get confused about which patch to apply ;-)

Thanks!

-- Steve

Quoted 29 lines
---
 kernel/trace/trace_events_filter.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/trace_events_filter.c +3 -1
index 05a66493a164..ecfa6f0f1c7e 100644
@@ -427,7 +427,7 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 	op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
 	if (!op_stack)
 		return ERR_PTR(-ENOMEM);
-	prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
+	prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
 	if (!prog_stack) {
 		parse_error(pe, -ENOMEM, 0);
 		goto out_free;
@@ -578,6 +578,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);
 	kfree(inverts);
+	for (i = 0; prog_stack[i].pred; i++)
+		kfree(prog_stack[i].pred);
 	kfree(prog_stack);
 	return ERR_PTR(ret);
 }
#7 lore raw
Quoted 12 lines · Steven Rostedt
Quoted 5 lines · On Tue, 28 May 2019 17:43:38 +0200 Tomas Bortoli <[email protected]> wrote:

In case of errors, predicate_parse() goes to the out_free label to free memory and to return an error code.

However, predicate_parse() does not free the predicates of the temporary prog_stack array, thence leaking them.

Thanks, I applied this and I'm running it through my tests. But just an FYI, when sending updated patches please add a "v2" to the subject:

Quoted 7 lines

[PATCH v2] tracing: Avoid memory leak in predicate_parse()

That way struggling maintainers like myself don't get confused about which patch to apply ;-)

Thanks!

Yeah, sorry about that, will make sure it doesn't happen again!

Thank you,
Tomas

Quoted 31 lines
Quoted 30 lines
---
 kernel/trace/trace_events_filter.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/trace_events_filter.c +3 -1
index 05a66493a164..ecfa6f0f1c7e 100644
@@ -427,7 +427,7 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 	op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
 	if (!op_stack)
 		return ERR_PTR(-ENOMEM);
-	prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
+	prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
 	if (!prog_stack) {
 		parse_error(pe, -ENOMEM, 0);
 		goto out_free;
@@ -578,6 +578,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);
 	kfree(inverts);
+	for (i = 0; prog_stack[i].pred; i++)
+		kfree(prog_stack[i].pred);
 	kfree(prog_stack);
 	return ERR_PTR(ret);
 }
maintainer
#8 lore raw
In reply to Tomas Bortoli

Review predicate_parse(const char *str, int nr_parens, int nr_preds, · line 585

Quoted 9 lines · On Tue, 28 May 2019 17:43:38 +0200 Tomas Bortoli <[email protected]> wrote:
(quoted diff) +2 -0
@@ -578,6 +578,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);
 	kfree(inverts);
+	for (i = 0; prog_stack[i].pred; i++)
+		kfree(prog_stack[i].pred);
 	kfree(prog_stack);
 	return ERR_PTR(ret);
 }

I should have caught this, but thanks to the zero day bot, it found it first:

kernel/trace/trace_events_filter.c:582:27-31: ERROR: prog_stack is NULL but dereferenced.

I changed the patch with the following:

From dfb4a6f2191a80c8b790117d0ff592fd712d3296 Mon Sep 17 00:00:00 2001
From: Tomas Bortoli <[email protected]>
Date: Tue, 28 May 2019 17:43:38 +0200
Subject: [PATCH] tracing: Avoid memory leak in predicate_parse()

In case of errors, predicate_parse() goes to the out_free label to free memory and to return an error code.

However, predicate_parse() does not free the predicates of the temporary prog_stack array, thence leaking them.

Fixes 80765597bc587 ("tracing: Rewrite filter logic to be simpler and faster")
Reported-by [email protected]
[ Added protection around freeing prog_stack[i].pred ]
---
 kernel/trace/trace_events_filter.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/trace/trace_events_filter.c +6 -2
index d3e59312ef40..5079d1db3754 100644
@@ -428,7 +428,7 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 	op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
 	if (!op_stack)
 		return ERR_PTR(-ENOMEM);
-	prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
+	prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
 	if (!prog_stack) {
 		parse_error(pe, -ENOMEM, 0);
 		goto out_free;
@@ -579,7 +579,11 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);
 	kfree(inverts);
-	kfree(prog_stack);
+	if (prog_stack) {
+		for (i = 0; prog_stack[i].pred; i++)
+			kfree(prog_stack[i].pred);
+		kfree(prog_stack);
+	}
 	return ERR_PTR(ret);
 }
 
Signature · 1 line
2.20.1
#9 lore raw
Quoted 60 lines · Steven Rostedt

kernel/trace/trace_events_filter.c predicate_parse(const char *str, int nr_parens, int nr_preds, · line 585

Quoted 9 lines · On Tue, 28 May 2019 17:43:38 +0200 Tomas Bortoli <[email protected]> wrote:
kernel/trace/trace_events_filter.c +2 -0
@@ -578,6 +578,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);
 	kfree(inverts);
+	for (i = 0; prog_stack[i].pred; i++)
+		kfree(prog_stack[i].pred);
 	kfree(prog_stack);
 	return ERR_PTR(ret);
 }

I should have caught this, but thanks to the zero day bot, it found it first:

kernel/trace/trace_events_filter.c:582:27-31: ERROR: prog_stack is NULL but dereferenced.

I changed the patch with the following:

From dfb4a6f2191a80c8b790117d0ff592fd712d3296 Mon Sep 17 00:00:00 2001
From: Tomas Bortoli <[email protected]>
Date: Tue, 28 May 2019 17:43:38 +0200
Subject: [PATCH] tracing: Avoid memory leak in predicate_parse()

In case of errors, predicate_parse() goes to the out_free label to free memory and to return an error code.

However, predicate_parse() does not free the predicates of the temporary prog_stack array, thence leaking them.

Fixes 80765597bc587 ("tracing: Rewrite filter logic to be simpler and faster")
Reported-by [email protected]
[ Added protection around freeing prog_stack[i].pred ]
---
 kernel/trace/trace_events_filter.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/trace/trace_events_filter.c +1 -1
index d3e59312ef40..5079d1db3754 100644
@@ -428,7 +428,7 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 	op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
 	if (!op_stack)
 		return ERR_PTR(-ENOMEM);
-	prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
+	prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
 	if (!prog_stack) {
 		parse_error(pe, -ENOMEM, 0);
 		goto out_free;
@@ -579,7 +579,11 @@ predicate_parse(const char *str, int nr_parens, int nr_preds,
 out_free:
 	kfree(op_stack);

kernel/trace/trace_events_filter.c predicate_parse(const char *str, int nr_parens, int nr_preds, · line 589

Quoted 10 lines
kernel/trace/trace_events_filter.c +5 -1
 	kfree(inverts);
-	kfree(prog_stack);
+	if (prog_stack) {
+		for (i = 0; prog_stack[i].pred; i++)
+			kfree(prog_stack[i].pred);
+		kfree(prog_stack);
+	}
 	return ERR_PTR(ret);
 }
 

Oops again, I should have been more careful.

Thanks.