Skip to contentSkip to conversation

Fix: Double free in bt_context_remove_trace().

PATCH Mathieu Desnoyers 2 participants 3 messages Jan 21, 2013 3 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 11 lines · * Jérémie Galarneau ([email protected]) wrote:

ctf_close_trace was being called twice when calling bt_context_remove_trace thus causing free() to be called on an invalid pointer.

Calling bt_context_remove_trace() would call ctf_close_trace() once via the close_handle callback registered on the ctf format struct and a second call would take place from bt_trace_handle_destroy() which is registered as the value_destroy_func on the trace_handles hash table of the current context.

The first explicit call to handle->format->close_trace is unnecessary.

The crash is reproducible by invoking the tests-python.py script.

I think we should leave the close_trace calls in both locations, but set handle->td to -1 when we close it, and test for negative handle ID before doing the close.

Thanks,

Mathieu

Quoted 57 lines
---
 lib/context.c      | 8 +-------
 lib/trace-handle.c | 5 ++++-
 2 files changed, 5 insertions(+), 8 deletions(-)
lib/context.c +1 -7
index 5516e49..05fb994 100644
@@ -156,7 +156,6 @@ end:
 int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
 {
 	struct bt_trace_handle *handle;
-	int ret;
 
 	if (!ctx)
 		return -EINVAL;
@@ -168,12 +167,7 @@ int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
 
 	/* Remove from containers */
 	trace_collection_remove(ctx->tc, handle->td);
-	/* Close the trace */
-	ret = handle->format->close_trace(handle->td);
-	if (ret) {
-		fprintf(stderr, "Error in close_trace callback\n");
-		return ret;
-	}
+
 	/* Remove and free the handle */
 	g_hash_table_remove(ctx->trace_handles,
 			(gpointer) (unsigned long) handle_id);
lib/trace-handle.c +4 -1
index 0da565b..17a6e0d 100644
@@ -49,7 +49,10 @@ struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx)
 
 void bt_trace_handle_destroy(struct bt_trace_handle *th)
 {
-	th->format->close_trace(th->td);
+	if (th->format->close_trace(th->td)) {
+		fprintf(stderr, "Error in close_trace callback\n");
+	}
+
 	g_free(th);
 }
 
Signature · 7 lines
1.8.1.1


_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20130121193225.GA8005@Krystal

#3 lore raw
Quoted 9 lines · Mathieu Desnoyers
Quoted 8 lines · * Jérémie Galarneau ([email protected]) wrote:

ctf_close_trace was being called twice when calling bt_context_remove_trace thus causing free() to be called on an invalid pointer.

Calling bt_context_remove_trace() would call ctf_close_trace() once via the close_handle callback registered on the ctf format struct and a second call would take place from bt_trace_handle_destroy() which is registered as the value_destroy_func on the trace_handles hash table of the current context.

Quoted 7 lines
Quoted 3 lines

The first explicit call to handle->format->close_trace is unnecessary.

The crash is reproducible by invoking the tests-python.py script.

I think we should leave the close_trace calls in both locations, but set handle->td to -1 when we close it, and test for negative handle ID before doing the close.

I agree, we need the two calls to close, one is called when we remove a trace manually and the other one is called when we destroy the context. Setting and testing the td to -1 will solve the problem and will avoid calling close_trace at all if not needed.

Thanks,

Julien

Quoted 63 lines

Thanks,

Mathieu

Quoted 57 lines
---
 lib/context.c      | 8 +-------
 lib/trace-handle.c | 5 ++++-
 2 files changed, 5 insertions(+), 8 deletions(-)
lib/context.c +1 -7
index 5516e49..05fb994 100644
@@ -156,7 +156,6 @@ end:
 int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
 {
 	struct bt_trace_handle *handle;
-	int ret;
 
 	if (!ctx)
 		return -EINVAL;
@@ -168,12 +167,7 @@ int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
 
 	/* Remove from containers */
 	trace_collection_remove(ctx->tc, handle->td);
-	/* Close the trace */
-	ret = handle->format->close_trace(handle->td);
-	if (ret) {
-		fprintf(stderr, "Error in close_trace callback\n");
-		return ret;
-	}
+
 	/* Remove and free the handle */
 	g_hash_table_remove(ctx->trace_handles,
 			(gpointer) (unsigned long) handle_id);
lib/trace-handle.c +4 -1
index 0da565b..17a6e0d 100644
@@ -49,7 +49,10 @@ struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx)
 
 void bt_trace_handle_destroy(struct bt_trace_handle *th)
 {
-	th->format->close_trace(th->td);
+	if (th->format->close_trace(th->td)) {
+		fprintf(stderr, "Error in close_trace callback\n");
+	}
+
 	g_free(th);
 }
 
Signature · 7 lines
1.8.1.1


_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 14 lines · * Julien Desfossez ([email protected]) wrote:
Quoted 13 lines · Mathieu Desnoyers
Quoted 11 lines · * Jérémie Galarneau ([email protected]) wrote:

ctf_close_trace was being called twice when calling bt_context_remove_trace thus causing free() to be called on an invalid pointer.

Calling bt_context_remove_trace() would call ctf_close_trace() once via the close_handle callback registered on the ctf format struct and a second call would take place from bt_trace_handle_destroy() which is registered as the value_destroy_func on the trace_handles hash table of the current context.

The first explicit call to handle->format->close_trace is unnecessary.

The crash is reproducible by invoking the tests-python.py script.

Quoted 8 lines
Quoted 3 lines

I think we should leave the close_trace calls in both locations, but set handle->td to -1 when we close it, and test for negative handle ID before doing the close.

I agree, we need the two calls to close, one is called when we remove a trace manually and the other one is called when we destroy the context. Setting and testing the td to -1 will solve the problem and will avoid calling close_trace at all if not needed.

Can we ever call bt_trace_handle_destroy() manually on a handle that needs the trace to be closed ? After careful review, I don't think so... (see patch v2 from Jérémie).

Thanks,

Mathieu

Quoted 68 lines

Thanks,

Julien

Quoted 63 lines

Thanks,

Mathieu

Quoted 57 lines
---
 lib/context.c      | 8 +-------
 lib/trace-handle.c | 5 ++++-
 2 files changed, 5 insertions(+), 8 deletions(-)
lib/context.c +1 -7
index 5516e49..05fb994 100644
@@ -156,7 +156,6 @@ end:
 int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
 {
 	struct bt_trace_handle *handle;
-	int ret;
 
 	if (!ctx)
 		return -EINVAL;
@@ -168,12 +167,7 @@ int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
 
 	/* Remove from containers */
 	trace_collection_remove(ctx->tc, handle->td);
-	/* Close the trace */
-	ret = handle->format->close_trace(handle->td);
-	if (ret) {
-		fprintf(stderr, "Error in close_trace callback\n");
-		return ret;
-	}
+
 	/* Remove and free the handle */
 	g_hash_table_remove(ctx->trace_handles,
 			(gpointer) (unsigned long) handle_id);
lib/trace-handle.c +4 -1
index 0da565b..17a6e0d 100644
@@ -49,7 +49,10 @@ struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx)
 
 void bt_trace_handle_destroy(struct bt_trace_handle *th)
 {
-	th->format->close_trace(th->td);
+	if (th->format->close_trace(th->td)) {
+		fprintf(stderr, "Error in close_trace callback\n");
+	}
+
 	g_free(th);
 }
 
Signature · 7 lines
1.8.1.1


_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com